forked from misc0110/pdf-webslides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
118 lines (112 loc) · 3.41 KB
/
cli.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "cli.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ---------------------------------------------------------------------------
struct option *getopt_get_long_options(getopt_arg_t *opt) {
size_t count = 0;
getopt_arg_t null_option = {0};
do {
if (!memcmp((void *)&opt[count], (void *)&null_option,
sizeof(getopt_arg_t))) {
break;
} else {
count++;
}
} while (1);
struct option *gopt = (struct option *)malloc(sizeof(struct option) * count);
if (!gopt) {
return NULL;
}
for (size_t i = 0; i < count; i++) {
gopt[i].name = opt[i].name;
gopt[i].has_arg = opt[i].has_arg;
gopt[i].flag = opt[i].flag;
gopt[i].val = opt[i].val;
}
return gopt;
}
// ---------------------------------------------------------------------------
void show_usage(char *binary, getopt_arg_t *cli_options) {
printf("USAGE\n %s [options] <pdf file> \n\n", binary);
getopt_arg_t null_option = {0};
size_t count = 0;
printf("\nOPTIONS\n");
do {
if (!memcmp((void *)&cli_options[count], (void *)&null_option,
sizeof(getopt_arg_t))) {
break;
} else if (cli_options[count].description) {
printf(" -%c%s%s%s %s%s%s%s\n ", cli_options[count].val,
cli_options[count].has_arg != no_argument ? " " : "",
cli_options[count].has_arg != no_argument
? cli_options[count].arg_name
: "",
cli_options[count].name ? "," : "",
cli_options[count].name ? "--" : "", cli_options[count].name,
cli_options[count].has_arg != no_argument ? "=" : "",
cli_options[count].has_arg != no_argument
? cli_options[count].arg_name
: "");
printf("%s", cli_options[count].description);
printf("\n\n");
}
count++;
} while (1);
printf("\n");
printf("EXAMPLE\n %s slides.pdf\n\n\n", binary);
printf("AUTHORS\n Written by Michael Schwarz.\n\n");
}
// ---------------------------------------------------------------------------
int parse_cli_options(Options *options, getopt_arg_t *cli_options, int argc,
char *argv[]) {
struct option *long_options =
getopt_get_long_options((getopt_arg_t *)cli_options);
int c;
while ((c = getopt_long(argc, argv, ":spo:nhvc:", long_options, NULL)) != EOF) {
switch (c) {
case 's':
options->single = 1;
break;
case 'h':
show_usage(argv[0], cli_options);
return 1;
case 'p':
options->presenter = 1;
break;
case 'o':
options->name = strdup(optarg);
break;
case 'n':
options->nonotes = 1;
break;
case 'v':
printf("pdf-webslides %s\n", APP_VERSION);
return 1;
case 'c':
options->compress = strdup(optarg);
break;
case ':':
printf("Option -%c requires an argument.\n", optopt);
printf("\n");
show_usage(argv[0], cli_options);
return 1;
case '?':
if (isprint(optopt)) {
printf("Unknown option -%c.\n", optopt);
} else {
printf("Unknown option character \\x%x.\n", optopt);
}
printf("\n");
show_usage(argv[0], cli_options);
return 1;
default:
show_usage(argv[0], cli_options);
return 1;
}
}
free(long_options);
return 0;
}