summaryrefslogtreecommitdiff
path: root/main.c
blob: 02e7dfaea1620384eca068c5b5b342c3c667159a (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
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "global.h"

char *progname = NULL;

#define X(name) int entry_ ## name (int argc, char **argv);
PROGNAMES_XLIST
#undef X

static char* basename(char *path) {
  size_t len = strlen(path);
  for (size_t i = len - 1; i != 0; i--) {
    if (path[i] == '/') return path + (i + 1);
  }
  return path;
}

int main(int argc, char **argv) {
  // Ensure getopt(3) does not print an (English) error
  opterr = 0;

  progname = basename(argv[0]);

  if (strcmp(progname, "drukkedoos") == 0) {
    argv++;
    argc--;
    if (argc == 0) {
#define X(name) "  " #name "\n"
      fprintf(stderr, "Gebruik: drukkedoos <commando>\n\n"
                      "Hulpmiddelen:\n"
                      PROGNAMES_XLIST);
#undef X
      return 1;
    }
    progname = basename(argv[0]);
  }

#define X(name) if (strcmp(progname, #name) == 0) return entry_ ## name (argc, argv);
PROGNAMES_XLIST
#undef X

  fprintf(stderr, "Onbekend hulpmiddel: '%s'\n", progname);
  return 1;
}