-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkconf.c
581 lines (475 loc) · 13.1 KB
/
mkconf.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* SPDX-License-Identifier: BSD-3-Clause */
#include "mkconf.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdnoreturn.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <err.h>
#include "y.tab.h"
struct mkconf_args {
const char *bconf;
const char *output;
const char *output_template;
const char *makefile_template;
};
void
mkconf_err(struct mkconf *mkconf, const char *format, ...) {
struct mkconf_error *error = malloc(sizeof (*error));
va_list ap;
int err;
if (error == NULL) {
return;
}
va_start(ap, format);
err = vasprintf(&error->message, format, ap);
va_end(ap);
if (err > 0) {
error->next = NULL;
if (mkconf->errors.last != NULL) {
mkconf->errors.last->next = error;
} else {
mkconf->errors.first = error;
}
mkconf->errors.last = error;
} else {
free(error);
}
}
static void
mkconf_init_from_name_and_version(struct mkconf *mkconf, char *name, char *version) {
if (*name == '\0') {
errx(EXIT_FAILURE, "Package name cannot be empty");
}
if (*name == '.') {
errx(EXIT_FAILURE, "Package name '%s' cannot begin with a '.'", name);
}
for (unsigned int i = 0; name[i] != '\0'; i++) {
if (name[i] == '/' || name[i] == '-') {
errx(EXIT_FAILURE, "Unexpected '%c' in package name '%s'", name[i], name);
}
}
if (*version == 'v') {
version++;
}
if (*version == '\0') {
version = "none";
}
for (unsigned int i = 0; version[i] != '\0'; i++) {
if (version[i] == '/' || version[i] == '-') {
errx(EXIT_FAILURE, "Unexpected '%c' in package version '%s'", version[i], version);
}
}
mkconf->package.name = name;
mkconf->package.version = version;
mkconf->errors.first = mkconf->errors.last = NULL;
}
static void
mkconf_init_from_name(struct mkconf *mkconf, char *namever) {
char *separator = strchr(namever, '-');
char *name, *version;
name = namever;
if (separator == NULL) {
version = "";
} else {
version = separator + 1;
}
mkconf_init_from_name_and_version(mkconf, name, version);
}
static void
mkconf_init(struct mkconf *mkconf) {
char *workdir, *namever;
workdir = getcwd(NULL, 0);
if (workdir == NULL) {
err(EXIT_FAILURE, "getcwd");
}
namever = basename(workdir);
if (namever == NULL) {
errx(EXIT_FAILURE, "basename %s", workdir);
}
namever = strdup(namever);
free(workdir);
mkconf_init_from_name(mkconf, namever);
}
static void
mkconf_print_errors(struct mkconf *mkconf, const char *prefix) {
struct mkconf_error *error = mkconf->errors.first;
do {
fprintf(stderr, "%s:%s\n", prefix, error->message);
error = error->next;
} while (error != NULL);
}
static void
mkconf_parse_features(struct mkconf *mkconf, const char *path) {
extern int yyparse(struct mkconf *);
extern FILE *yyin;
yyin = fopen(path, "r");
if (yyin == NULL) {
if (errno == ENOENT) {
mkconf->features = NULL;
return;
}
err(EXIT_FAILURE, "fopen '%s'", path);
}
if (yyparse(mkconf) != 0) {
mkconf_print_errors(mkconf, path);
errx(EXIT_FAILURE, "Unable to parse feature file '%s'", path);
}
}
static void
mkconf_open_files(struct mkconf *mkconf, const struct mkconf_args *args) {
mkconf->makefile_template = fopen(args->makefile_template, "r");
if (mkconf->makefile_template == NULL) {
err(EXIT_FAILURE, "fopen '%s'", args->makefile_template);
}
mkconf->output_template = fopen(args->output_template, "r");
if (mkconf->output_template == NULL) {
err(EXIT_FAILURE, "fopen '%s'", args->output_template);
}
mkconf->output = fopen(args->output, "w");
if (mkconf->output == NULL) {
err(EXIT_FAILURE, "fopen '%s'", args->output);
}
}
static void
strntolower(char *dst, const char *src, size_t n) {
size_t i = 0;
while (src[i] != '\0') {
dst[i] = tolower((unsigned char)src[i]);
i++;
}
memset(dst + i, 0, n - i);
}
static void
und2dashes(char *dst, char *src, size_t n) {
size_t i = 0;
while (src[i] != '\0') {
if (src[i] != '_') {
dst[i] = src[i];
} else {
dst[i] = '-';
}
i++;
}
memset(dst + i, 0, n - i);
}
static void
fputs_escape_sh(const char *str, FILE *out) {
static const char accept[] = "$\t\r\n\"";
const char *prev = str, *curr;
while (curr = strpbrk(prev, accept), curr != NULL) {
fwrite(prev, 1, curr - prev, out);
switch (*curr) {
case '\t':
fputs("\\t", out);
break;
case '\r':
fputs("\\r", out);
break;
case '\n':
fputs("\\n", out);
break;
default:
fputc('\\', out);
fputc(*curr, out);
break;
}
prev = curr + 1;
}
fputs(prev, out);
}
static void
mkconf_preprocess_usage(struct mkconf *mkconf) {
struct mkconf_feature *feature = mkconf->features;
size_t longest = 0;
while (feature != NULL) {
const size_t length = strlen(feature->name);
if (length > longest) {
longest = length;
}
feature = feature->next;
}
feature = mkconf->features;
while (feature != NULL) {
const size_t length = strlen(feature->name);
const int padding = (int)(longest - length);
char opt[length + 1];
strntolower(opt, feature->name, sizeof (opt));
und2dashes(opt, opt, sizeof (opt));
fprintf(mkconf->output, "\t--[enable|disable|without]-%s %*s", opt, padding, "");
fputs_escape_sh(feature->description, mkconf->output);
fprintf(mkconf->output, ".\n\t--with-%s=<value> %*s", opt, padding + 4, "");
if (feature->defaults != NULL) {
fputs("where <value> defaults to '", mkconf->output);
fputs_escape_sh(feature->defaults, mkconf->output);
fputs("'.\n", mkconf->output);
} else {
fputs("where <value> is not set by default.\n", mkconf->output);
}
feature = feature->next;
}
}
static void
mkconf_preprocess_defaults(struct mkconf *mkconf) {
struct mkconf_feature *feature = mkconf->features;
while (feature != NULL) {
if (feature->defaults != NULL) {
char lwr[strlen(feature->name) + 1];
strntolower(lwr, feature->name, sizeof (lwr));
fprintf(mkconf->output, "m_feature_%s=\"", lwr);
fputs_escape_sh(feature->defaults, mkconf->output);
fputs("\"\n", mkconf->output);
}
feature = feature->next;
}
}
static void
mkconf_preprocess_getopt_long(struct mkconf *mkconf) {
struct mkconf_feature *feature = mkconf->features;
while (feature != NULL) {
char lwr[strlen(feature->name) + 1];
char opt[sizeof (lwr)];
strntolower(lwr, feature->name, sizeof (lwr));
und2dashes(opt, lwr, sizeof (opt));
fprintf(mkconf->output,
"\t--enable-%s) m_feature_%s=1 ;;\n"
"\t--with-%s=*) m_feature_%s=$m_optarg ;;\n"
"\t--disable-%s|--without-%s) unset m_feature_%s ;;\n",
opt, lwr, opt, lwr, opt, opt, lwr);
feature = feature->next;
}
}
static void
mkconf_preprocess_package_vars(struct mkconf *mkconf) {
/* No need to escape the strings, they have been checked earlier and should be safe */
fprintf(mkconf->output,
"m_package_name='%s'\n"
"m_package_version='%s'\n",
mkconf->package.name, mkconf->package.version);
}
static void
mkconf_preprocess_features_summary(struct mkconf *mkconf) {
struct mkconf_feature *feature = mkconf->features;
if (feature != NULL) {
fputs("printf 'Selected features:\\n'\n", mkconf->output);
do {
char lwr[strlen(feature->name) + 1];
strntolower(lwr, feature->name, sizeof (lwr));
fprintf(mkconf->output, "printf '\\tCONFIG_%s is \"%%s\"\\n' \"${m_feature_%s}\"\n",
feature->name, lwr);
feature = feature->next;
} while (feature != NULL);
} else {
fputs("printf 'No features defined.\\n'\n", mkconf->output);
}
}
static void
mkconf_preprocess_makefile_heredoc_features(struct mkconf *mkconf) {
struct mkconf_feature *feature = mkconf->features;
while (feature != NULL) {
char lwr[strlen(feature->name) + 1];
strntolower(lwr, feature->name, sizeof (lwr));
fprintf(mkconf->output, "CONFIG_%s=${m_feature_%s}\n", feature->name, lwr);
feature = feature->next;
}
if (mkconf->features != NULL) {
fputc('\n', mkconf->output);
}
}
static void
mkconf_preprocess_makefile_heredoc(struct mkconf *mkconf) {
static const char accept[] = "$\\@";
char *line = NULL;
size_t n = 0;
ssize_t len;
rewind(mkconf->makefile_template);
fputs("cat > GNUmakefile <<EOF\n", mkconf->output);
mkconf_preprocess_makefile_heredoc_features(mkconf);
while (len = getline(&line, &n, mkconf->makefile_template), len > 0) {
const char *prev = line, *curr;
while (curr = strpbrk(prev, accept), curr != NULL) {
fwrite(prev, 1, curr - prev, mkconf->output);
if (*curr == '@') {
const char * const var = curr + 1;
const char *end = var;
while (isalnum(*end) || *end == '_') {
end++;
}
if (*end == '@') {
fprintf(mkconf->output, "${%.*s}", (int)(end - var), var);
prev = end + 1;
} else {
fputc('@', mkconf->output);
prev = var;
}
} else {
fputc('\\', mkconf->output);
fputc(*curr, mkconf->output);
prev = curr + 1;
}
}
fputs(prev, mkconf->output);
}
fputs("EOF\n", mkconf->output);
free(line);
}
static void
mkconf_preprocess(struct mkconf *mkconf) {
static const struct {
const char * const name;
void (* const func)(struct mkconf *);
} tags[] = {
{ "usage", mkconf_preprocess_usage },
{ "defaults", mkconf_preprocess_defaults },
{ "getopt_long", mkconf_preprocess_getopt_long },
{ "package_vars", mkconf_preprocess_package_vars },
{ "features_summary", mkconf_preprocess_features_summary },
{ "makefile_heredoc", mkconf_preprocess_makefile_heredoc },
};
static const char tagprefix[] = "@{mkconf_";
const int tagend = '}';
char *line = NULL;
int lineno = 0;
size_t n = 0;
int err = 0;
ssize_t len;
while (++lineno, len = getline(&line, &n, mkconf->output_template), len > 0) {
if (strncmp(line, tagprefix, sizeof (tagprefix) - 1) != 0) {
fputs(line, mkconf->output);
continue;
}
const char * const name = line + sizeof (tagprefix) - 1;
const char * const end = strchr(name, tagend);
if (end == NULL) {
mkconf_err(mkconf, "output:%d: unterminated tag", lineno);
continue;
}
const size_t namelen = end - name;
unsigned int index = 0;
while (index < sizeof (tags) / sizeof (*tags)
&& (strncmp(tags[index].name, name, namelen) != 0
|| strlen(tags[index].name) != namelen)) {
index++;
}
if (index == sizeof (tags) / sizeof (*tags)) {
mkconf_err(mkconf, "output:%d: unknown tag '%.*s'", lineno, (int)namelen, name);
continue;
}
tags[index].func(mkconf);
}
free(line);
}
static void
mkconf_close_files(struct mkconf *mkconf) {
fclose(mkconf->output);
fclose(mkconf->makefile_template);
fclose(mkconf->output_template);
}
static void noreturn
mkconf_usage(const char *progname) {
fprintf(stderr, "usage: %s [-c <bconf>] [-o <output>] [-t <template-set>]"
" [-O <output-template>] [-M <makefile-template>] [<name> [<version>]]\n", progname);
exit(EXIT_FAILURE);
}
static struct mkconf_args
mkconf_parse_args(int argc, char **argv) {
const char *template_set = CONFIG_DEFAULT_TEMPLATE_SET;
struct mkconf_args args = {
.bconf = CONFIG_DEFAULT_BCONF,
.output = CONFIG_DEFAULT_OUTPUT,
};
int c;
while ((c = getopt(argc, argv, ":c:o:t:O:M:")) >= 0) {
switch (c) {
case 'c':
args.bconf = optarg;
break;
case 'o':
args.output = optarg;
break;
case 't':
template_set = optarg;
break;
case 'O':
args.output_template = optarg;
break;
case 'M':
args.makefile_template = optarg;
break;
case ':':
warnx("Option -%c requires an operand", optopt);
mkconf_usage(*argv);
case '?':
warnx("Unrecognized option -%c", optopt);
mkconf_usage(*argv);
}
}
if (argc - optind > 2) {
warnx("Too many arguments");
mkconf_usage(*argv);
}
if (args.output_template == NULL || args.makefile_template == NULL) {
static const char * const formats[][2] = {
{ CONFIG_DATADIR"/%s/configure.in", CONFIG_DATADIR"/%s/GNUmakefile.in", },
{ "%s/configure.in", "%s/GNUmakefile.in", },
};
const unsigned int index = strchr(template_set, '/') != NULL;
char path[PATH_MAX];
int len;
if (*template_set == '\0' || (!index && *template_set == '.')) {
errx(EXIT_FAILURE, "Invalid template set '%s'", template_set);
}
if (args.output_template == NULL) {
len = snprintf(path, sizeof (path), formats[index][0], template_set);
if (len < 0 || len >= PATH_MAX) {
errx(EXIT_FAILURE, "Invalid output template path");
}
args.output_template = strdup(path);
}
if (args.makefile_template == NULL) {
len = snprintf(path, sizeof (path), formats[index][1], template_set);
if (len < 0 || len >= PATH_MAX) {
errx(EXIT_FAILURE, "Invalid Makefile template path");
}
args.makefile_template = strdup(path);
}
}
return args;
}
int
main(int argc, char **argv) {
const struct mkconf_args args = mkconf_parse_args(argc, argv);
struct mkconf mkconf;
switch (argc - optind) {
case 0:
mkconf_init(&mkconf);
break;
case 1:
mkconf_init_from_name(&mkconf, argv[optind]);
break;
case 2:
mkconf_init_from_name_and_version(&mkconf, argv[optind], argv[optind + 1]);
break;
default:
abort();
}
mkconf_parse_features(&mkconf, args.bconf);
mkconf_open_files(&mkconf, &args);
mkconf_preprocess(&mkconf);
if (mkconf.errors.first != NULL) {
mkconf_print_errors(&mkconf, args.output);
unlink(args.output);
} else {
if (fchmod(fileno(mkconf.output), 0755) != 0) {
warn("chmod %s", args.output);
}
}
mkconf_close_files(&mkconf);
return EXIT_SUCCESS;
}