1b258fe3e86736b3fe8448caf29869fb8777e3dc
[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           "      --print-header         Print ELF header\n"
18           "      --print-segments       Print program headers\n"
19           "      --print-sections       Print sections\n"
20           "\n"
21           "\n");
22 }
23
24
25
26 void parseOptions(CLIOpts *opts, int argc, char **argv)
27 {
28   char *progname = argv[0];
29   int c;
30   int option_index = 0;
31
32   static struct option long_options[] = {
33     {"help", 0, 0, 'h'},
34     {"output", 1, 0, 'o'},
35     {"print-header", 0, 0, 10001},
36     {"print-segments", 0, 0, 10002},
37     {"print-sections", 0, 0, 10003},
38     {NULL, 0, NULL, 0}
39   };
40
41   while ((c = getopt_long(argc, argv, "e:ho:",
42                           long_options, &option_index)) != -1) {
43     switch (c) {
44       case 'h':
45         printUsage(progname);
46         exit(EXIT_SUCCESS);
47       case 'o':
48         opts->fnOutput = optarg;
49         break;
50       case 10001:
51         opts->printHeader = 1;
52         break;
53       case 10002:
54         opts->printSegments = 1;
55         break;
56       case 10003:
57         opts->printSections = 1;
58         break;
59       case '?':
60       default:
61         goto USAGE;
62     }
63   }
64
65   while (optind < argc) {
66     if (!opts->fnInput) {
67       opts->fnInput = argv[optind];
68     }
69     optind++;
70   }
71
72   if (!opts->fnInput) {
73     fprintf(stderr, "Error: No input file specified.\n\n");
74     goto USAGE;
75   }
76
77   return;
78
79 USAGE:
80   printUsage(progname);
81   exit(1);
82 }