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