-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
268 lines (256 loc) · 6.98 KB
/
Copy pathmain.c
File metadata and controls
268 lines (256 loc) · 6.98 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "code.h"
#include "shared.h"
/* NOTE: not to be confused with MAX_VERSION */
#define INLINE_VERSION_LIMIT 7
typedef enum cli_argument_e
{
ARG_NONE = 0,
ARG_RAW = 1,
ARG_INFO = 2,
ARG_OPTIMIZE = 4,
ARG_HELP = 8,
ARG_NOLIMIT = 0x10,
ARG_PLACEHOLDER = 0x20,
ARG_RESERVED __attribute__((unavailable("bit mask limit"))) = 0x8000,
ARG_VERBOSE,
ARG_MASK,
ARG_VERSION,
ARG_LEVEL,
ARG_SCALE,
ARG_BMP,
ARG_SVG
} cli_argument_t;
typedef struct cli_option_s
{
const char* flag;
cli_argument_t type;
} cli_option_t;
static const cli_option_t
cli_options_[] = {
{ "--raw", ARG_RAW },
{ "--version", ARG_INFO },
{ "-v", ARG_INFO },
{ "--optimize", ARG_OPTIMIZE },
{ "--help", ARG_HELP },
{ "-h", ARG_HELP },
{ "--nolimit", ARG_NOLIMIT },
{ "--placeholder", ARG_PLACEHOLDER },
{ "-g", ARG_VERBOSE },
{ "-m", ARG_MASK },
{ "-l", ARG_LEVEL },
{ "-s", ARG_SCALE },
{ "-V", ARG_VERSION },
{ "-B", ARG_BMP },
{ "-K", ARG_SVG }
};
#define NUM_CLI_ARGS (sizeof(cli_options_) / sizeof(cli_options_[0]))
#define NUM_MANDATORY_ARGS 1
static __inline__ int
print_help_(const char* __restrict__ cmdln)
{
fprintf(stderr,
"Usage: %s [OPTIONS] <data to encode>" _nl
"OPTIONS:" _nl
" -h, --help show this help message" _nl
" --nolimit ignore inline Version limit (for larger terminals)" _nl
" --optimize reduce data size, encode numeric, alphanumeric, byte" _nl
" segments separately (if any)" _nl
" --placeholder print with empty encoding region" _nl
" --raw print generated code with chars 1, 0 (no box-chars)" _nl
" -v, --version show generator's version and build information" _nl
" -g <uint> level of on-screen information <0-3>" _nl
" -l <char> use a specific error correction level (l, m, q, or h)" _nl
" -m <uint> force use of mask <0-7>, regardless of penalty" _nl
" -s <uint> scale image output <1-" _xstr(MAX_SCALE) "> times" _nl
" -V <uint> force use of version <1-" _xstr(MAX_VERSION) "> code"
" (or lower, if" _nl
" used with --optimize)" _nl
" -B <string> create bitmap file with generated code" _nl
" -K <string> create scalable vector image, disregards -s" _nl,
cmdln
);
return EINVAL;
}
static __inline__ eclevel_t
lvlfromchar_(const char ch)
{
switch (ch) {
case 'l':
return EC_LOW;
case 'm':
return EC_MEDIUM;
case 'q':
return EC_QUARTILE;
default:
return EC_HIGH;
}
}
int
main(const int argc, char* argv[])
{
char* const default_data = "empty";
#if defined(_WIN32) || defined(__CYGWIN__)
/* NOTE: to allow box-drawing characters */
system("chcp 65001>nul");
#endif
cli_argument_t options = ARG_NONE;
imgfmt_t imgfmt = FMT_SVG;
eclevel_t level = EC_LOW;
int verbose = 2;
int mask = -1;
int version = -1;
int scale = -1;
long arg_count = 0;
char* data = default_data;
char* filename = NULL;
pdebug("started parsing cmdln arguments");
for (int arg = 1; arg < argc; arg++)
{
if (argv[arg][0] == '-')
{
for (size_t j = 0; j < NUM_CLI_ARGS; j++)
{
if (!strcmp(argv[arg], cli_options_[j].flag))
{
arg_count++;
bool exclusive_arg = true;
switch (cli_options_[j].type)
{
case ARG_VERBOSE:
verbose = (int)strtol(argv[arg + 1], NULL, 0);
break;
case ARG_MASK:
mask = (int)strtol(argv[arg + 1], NULL, 0);
break;
case ARG_LEVEL:
{
if (strlen(argv[arg + 1]) == 1)
{
const char lvl = (char)tolower((uint8_t)argv[arg + 1][0]);
if (strchr("lmqh", lvl) != NULL)
{
level = lvlfromchar_(lvl);
break;
}
}
eprintf("unsupported error correction level");
perrno(EINVAL);
return EINVAL;
}
case ARG_VERSION:
version = (int)strtol(argv[arg + 1], NULL, 0) - 1;
break;
case ARG_SCALE:
scale = (int)strtol(argv[arg + 1], NULL, 0);
break;
case ARG_BMP:
imgfmt = FMT_BMP;
__attribute__((fallthrough));
case ARG_SVG:
filename = argv[arg + 1];
break;
default:
options |= cli_options_[j].type;
exclusive_arg = false;
break;
} /* switch */
if (exclusive_arg)
{
arg_count++;
arg++;
}
break;
}
}
}
}
pdebug("finished parsing cmdln arguments");
if (options & ARG_INFO)
{
puts(PROJECT_TITLE " " PROJECT_VERSION _nl
"Built with Clang " __clang_version__ " @ " __DATE__ " " __TIME__);
return EXIT_SUCCESS;
}
if (options & ARG_HELP)
{
return print_help_(argv[0]);
}
if (!(options & ARG_PLACEHOLDER))
{
if (argc - arg_count < NUM_MANDATORY_ARGS + 1)
{
eprintf("must provide " _xstr(NUM_MANDATORY_ARGS) " mandatory argument(s)");
return EXIT_FAILURE;
}
data = argv[argc - 1];
}
if (verbose > 1)
{
puts(PROJECT_TITLE " " PROJECT_VERSION _nl
PROJECT_COPYRIGHT _nl PROJECT_LICENSE _nl);
}
pdebug("creating qrcode object");
const qrconfig_t config = {
version,
level,
options & ARG_OPTIMIZE,
verbose > 2,
options & ARG_PLACEHOLDER
};
qrcode_t* qrcode = NULL;
int err = create_qrcode(&qrcode, data, &config);
if (err != 0)
{
eprintf("could not create qrcode");
perrno(err);
goto cleanup;
}
if (mask != -1)
{
err = qrcode_forcemask(qrcode, mask);
if (err != 0)
{
eprintf("could not force qrcode mask choice");
}
else if (verbose > 2)
{
pinfo("Forced mask: %d", mask);
}
}
if (verbose > 0)
{
const uint8_t vers = qrcode_version(qrcode);
if (vers > INLINE_VERSION_LIMIT && !(options & ARG_NOLIMIT))
{
eprintf("could not print inline qrcode, version %uhh too high,"
"use --nolimit and try again", vers);
}
else
{
pdebug("printing inline qrcode");
qrcode_print(qrcode, options & ARG_RAW);
}
}
if (filename != NULL)
{
err = qrcode_output(qrcode, imgfmt, scale, filename);
if (err != 0)
{
eprintf("could not output qrcode image");
}
else if (verbose > 2)
{
pinfo("Image saved to: %s", filename);
}
}
err = 0;
cleanup:
pdebug("resources clean-up");
delete_qrcode(&qrcode);
return err;
}