c728d40ac2cf12ee6807cdd5a43dcdd835320d60
[centaur.git] / src / options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <getopt.h>
5
6 #include "options.h"
7
8
9 static void printUsage(char *progname)
10 {
11   printf("Usage: %s [OPTIONS] <elf-file>\n", progname);
12   printf("\n"
13           "Options:\n"
14           "  -h, --help                     Print this help message\n"
15           "  -o, --output                   Where to write the modified ELF file to\n"
16           "\n"
17           "ELF dump:\n"
18           "      --print-header             Print ELF header\n"
19           "      --print-segments           Print program headers\n"
20           "      --print-sections           Print sections\n"
21           "\n"
22           "Space insertion:\n"
23           "    off: File offset, not within any structure (headers or sections).\n"
24           "    sz:  A multiple of the maximum alignment of all PHDRs.\n"
25           "\n"
26           "      --insert-before off,sz     Insert spacing at given offset,\n"
27           "                                 mapping everything before it to lower mem addresses.\n"
28           "      --insert-after  off,sz     Insert spacing at given offset,\n"
29           "                                 mapping everything after it to higher mem addresses.\n"
30           "\n");
31 }
32
33
34
35 void parseOptions(CLIOpts *opts, int argc, char **argv)
36 {
37   char *progname = argv[0];
38   int c;
39   int option_index = 0;
40   char *endptr;
41
42   static struct option long_options[] = {
43     {"help", 0, 0, 'h'},
44     {"output", 1, 0, 'o'},
45     {"print-header", 0, 0, 10001},
46     {"print-segments", 0, 0, 10002},
47     {"print-sections", 0, 0, 10003},
48     {"insert-before", 1, 0, 10004},
49     {"insert-after", 1, 0, 10005},
50     {NULL, 0, NULL, 0}
51   };
52
53   while ((c = getopt_long(argc, argv, "e:ho:",
54                           long_options, &option_index)) != -1) {
55     switch (c) {
56       case 'h':
57         printUsage(progname);
58         exit(EXIT_SUCCESS);
59       case 'o':
60         opts->fnOutput = optarg;
61         break;
62       case 10001:
63         opts->printHeader = 1;
64         break;
65       case 10002:
66         opts->printSegments = 1;
67         break;
68       case 10003:
69         opts->printSections = 1;
70         break;
71       case 10004:
72         opts->insertBeforeOffs = strtoul(optarg, &endptr, 0);
73         if (endptr[0] != ',') {
74           goto USAGE;
75         }
76         opts->insertBeforeSz = strtoul(endptr + 1, &endptr, 0);
77         if (endptr[0] != '\0' || opts->insertBeforeSz == 0) {
78           goto USAGE;
79         }
80         break;
81       case 10005:
82         opts->insertAfterOffs = strtoul(optarg, &endptr, 0);
83         if (endptr[0] != ',') {
84           goto USAGE;
85         }
86         opts->insertAfterSz = strtoul(endptr + 1, &endptr, 0);
87         if (endptr[0] != '\0' || opts->insertAfterSz == 0) {
88           goto USAGE;
89         }
90         break;
91       case '?':
92       default:
93         goto USAGE;
94     }
95   }
96
97   while (optind < argc) {
98     if (!opts->fnInput) {
99       opts->fnInput = argv[optind];
100     }
101     optind++;
102   }
103
104   if (!opts->fnInput) {
105     fprintf(stderr, "Error: No input file specified.\n\n");
106     goto USAGE;
107   }
108
109   return;
110
111 USAGE:
112   printUsage(progname);
113   exit(1);
114 }