summaryrefslogtreecommitdiff
path: root/src/options.c
blob: 55a91c553bfb219beef058c0c824ddc554194b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

#include "options.h"


static void printUsage(char *progname)
{
  printf("Usage: %s [OPTIONS] <elf-file>\n", progname);
  printf("\n"
          "Options:\n"
          "  -h, --help                     Print this help message\n"
          "  -o, --output                   Where to write the modified ELF file to\n"
          "\n"
          "      --print-header             Print ELF header\n"
          "      --print-segments           Print program headers\n"
          "      --print-sections           Print sections\n"
          "\n"
          "      --insert-before off,sz     Insert spacing at given offset\n"
          "\n");
}



void parseOptions(CLIOpts *opts, int argc, char **argv)
{
  char *progname = argv[0];
  int c;
  int option_index = 0;
  char *endptr;

  static struct option long_options[] = {
    {"help", 0, 0, 'h'},
    {"output", 1, 0, 'o'},
    {"print-header", 0, 0, 10001},
    {"print-segments", 0, 0, 10002},
    {"print-sections", 0, 0, 10003},
    {"insert-before", 1, 0, 10004},
    {NULL, 0, NULL, 0}
  };

  while ((c = getopt_long(argc, argv, "e:ho:",
                          long_options, &option_index)) != -1) {
    switch (c) {
      case 'h':
        printUsage(progname);
        exit(EXIT_SUCCESS);
      case 'o':
        opts->fnOutput = optarg;
        break;
      case 10001:
        opts->printHeader = 1;
        break;
      case 10002:
        opts->printSegments = 1;
        break;
      case 10003:
        opts->printSections = 1;
        break;
      case 10004:
        opts->insertBeforeOffs = strtoul(optarg, &endptr, 0);
        if (endptr[0] != ',') {
          goto USAGE;
        }
        opts->insertBeforeSz = strtoul(endptr + 1, &endptr, 0);
        if (endptr[0] != '\0' || opts->insertBeforeSz == 0) {
          goto USAGE;
        }
        break;
      case '?':
      default:
        goto USAGE;
    }
  }

  while (optind < argc) {
    if (!opts->fnInput) {
      opts->fnInput = argv[optind];
    }
    optind++;
  }

  if (!opts->fnInput) {
    fprintf(stderr, "Error: No input file specified.\n\n");
    goto USAGE;
  }

  return;

USAGE:
  printUsage(progname);
  exit(1);
}