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