Clean up file handling in main.c a bit
authornorly <ny-git@enpas.org>
Fri, 22 Mar 2013 13:43:05 +0000 (13:43 +0000)
committernorly <ny-git@enpas.org>
Fri, 22 Mar 2013 13:43:05 +0000 (13:43 +0000)
src/main.c

index 34e757010e7d26c43fba6aac2c8076374805d073..6fc5bc0d4c314bcf00a336d346cdc0abd5c774a8 100644 (file)
@@ -18,6 +18,7 @@ int main(int argc, char **argv)
   ELFHandles hIn = { 0 };
   ELFHandles hOut = { 0 };
   int exitval = EXIT_SUCCESS;
+  ElfuElf *me;
 
   /* Is libelf alive and well? */
   if (elf_version(EV_CURRENT) == EV_NONE) {
@@ -36,16 +37,10 @@ int main(int argc, char **argv)
     goto EXIT;
   }
 
-  if (opts.fnOutput) {
-    openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
-    if (!hOut.e) {
-      exitval = EXIT_FAILURE;
-      goto EXIT;
-    }
-  }
-
 
-  /* Now that we have a (hopefully) sane environment, execute commands */
+  /* Now that we have a (hopefully) sane environment, execute commands.
+   * Printing will ahve to be reimplemented based on the memory model.
+   */
   if (opts.printHeader) {
     printHeader(hIn.e);
   }
@@ -59,36 +54,48 @@ int main(int argc, char **argv)
   }
 
 
-  /* Copy the input ELF to the output file if the latter is specified */
-  if (opts.fnOutput) {
-    ElfuElf *me;
+  me = elfu_mFromElf(hIn.e);
+  if (me) {
+    closeElf(&hIn);
+    printf("Model successfully loaded.\n");
+    elfu_mCheck(me);
+    printf("Input model checked.\n");
+  } else {
+    printf("Failed to load model, aborting.\n");
+    goto EXIT;
+  }
 
-    me = elfu_mFromElf(hIn.e);
 
-    if (me) {
-      printf("Model successfully loaded.\n");
-      elfu_mCheck(me);
-      printf("Input model checked.\n");
+  /* Copy the input ELF to the output file if the latter is specified.
+   * Perform requested transformations on the memory model on-the-fly. */
+  if (!opts.fnOutput) {
+    printf("No output file specified - no further operations performed.\n");
+  } else {
+    if (opts.expandNobitsOffs) {
+      elfu_mExpandNobits(me, opts.expandNobitsOffs);
+    }
 
-      if (opts.expandNobitsOffs) {
-        elfu_mExpandNobits(me, opts.expandNobitsOffs);
-      }
+    if (opts.insertBeforeSz) {
+      elfu_mInsertBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
+    }
 
-      if (opts.insertBeforeSz) {
-        elfu_mInsertBefore(me, opts.insertBeforeOffs, opts.insertBeforeSz);
-      }
+    if (opts.insertAfterSz) {
+      elfu_mInsertAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
+    }
 
-      if (opts.insertAfterSz) {
-        elfu_mInsertAfter(me, opts.insertAfterOffs, opts.insertAfterSz);
-      }
+    elfu_mCheck(me);
+    printf("Output model checked.\n");
 
-      elfu_mCheck(me);
-      printf("Output model checked.\n");
-      elfu_mToElf(me, hOut.e);
-      printf("Model converted to ELF, ready to be written.\n");
-    } else {
-      printf("Failed to load model.\n");
+
+    openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
+    if (!hOut.e) {
+      printf("Failed to open output file. Aborting.\n");
+      exitval = EXIT_FAILURE;
+      goto EXIT;
     }
+
+    elfu_mToElf(me, hOut.e);
+    printf("Model converted to ELF, ready to be written.\n");
   }