42637f04c1940961ea00c22bd91e27f5edd402f7
[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     {"model", 0, 0, 10004},
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 10004:
60         opts->model = 1;
61         break;
62       case '?':
63       default:
64         goto USAGE;
65     }
66   }
67
68   while (optind < argc) {
69     if (!opts->fnInput) {
70       opts->fnInput = argv[optind];
71     }
72     optind++;
73   }
74
75   if (!opts->fnInput) {
76     fprintf(stderr, "Error: No input file specified.\n\n");
77     goto USAGE;
78   }
79
80   return;
81
82 USAGE:
83   printUsage(progname);
84   exit(1);
85 }