-
Notifications
You must be signed in to change notification settings - Fork 6
/
mkiocccentry.c
5575 lines (5111 loc) · 163 KB
/
mkiocccentry.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mkiocccentry - form IOCCC entry compressed tarball
*
* Make an IOCCC compressed tarball for an IOCCC entry.
*
* We will form the IOCCC entry compressed tarball "by hand" in C.
* Not in some high level language, but standard vanilla (with a healthy
* overdose of chocolate :-) ) C. Why? Because this is an obfuscated C
* contest. But then why isn't this code obfuscated? Because the IOCCC judges
* prefer to write in robust unobfuscated code. Besides, the IOCCC was started
* as an ironic commentary on the Bourne shell source and finger daemon source.
* Moreover, irony is well baked-in to the IOCCC. :-)
*
* OK, we do make use of shell scripts to help build and test
* this repo: but who doesn't use a bit of shell scripting now and then? :-)
* Nevertheless, the core of building your IOCCC entry compressed tarball,
* the code that you, the IOCCC contestant will use, is all written in
* C because this is the IOCCC.
*
* If you do find a problem with this code, please let the judges know.
* To contact the judges please see:
*
* https://www.ioccc.org/contact.html
*
* "Because even printf has a return value worth paying attention to." :-)
*
* Many thanks are due to a number of people who provided important
* and valuable testing, suggestions, issue reports and GitHub pull
* requests to this code. Without their time and effort, this tool
* would not work very well!
*
* Among the GitHub users we wish to thank include these fine developers
* in alphabetical GitHub username order:
*
* @ilyakurdyukov Ilya Kurdyukov
* @SirWumpus Anthony Howe
* @vog Volker Diels-Grabsch
* @xexyl Cody Boone Ferguson
*
* Copyright (c) 2021-2024 by Landon Curt Noll. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* Share and enjoy! :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h> /* for WEXITSTATUS() */
#include <fcntl.h> /* for open() */
#include <string.h> /* for strdup() */
/*
* mkiocccentry - form IOCCC entry compressed tarball
*/
#include "mkiocccentry.h"
/*
* definitions
*/
#define REQUIRED_ARGS (4) /* number of required arguments on the command line */
/*
* usage message
*
* Use the usage() function to print the usage_msg([0-9]?)+ strings.
*/
static const char * const usage_msg0 =
"usage: %s [options] work_dir prog.c Makefile remarks.md [file ...]\n"
"\noptions:\n"
"\t-h\t\tprint help message and exit\n"
"\t-v level\tset verbosity level: (def level: %d)\n"
"\t-J level\tset JSON verbosity level (def level: %d)\n"
"\t-q\t\tquiet mode (def: loud :-) )\n"
"\t\t\t NOTE: -q will also silence msg(), warn(), warnp() if -v 0\n"
"\t-V\t\tprint version string and exit\n"
"\t-W\t\tignore all warnings (this does NOT mean the judges will! :) )\n"
"\t-y\t\tanswer yes to most questions (use with EXTREME caution!)\n";
static const char * const usage_msg1 =
"\t-t tar\t\tpath to tar executable that supports the -J (xz) option (def: %s)\n"
"\t-c cp\t\tpath to cp executable (def: %s)\n"
"\t-l ls\t\tpath to ls executable (def: %s)\n"
"\t-T txzchk\tpath to txzchk executable (def: %s)\n"
"\t-e\t\tentertainment mode\n"
"\t-F fnamchk\tpath to fnamchk executable used by txzchk (def: %s)";
static const char * const usage_msg2 =
"\t-C chkentry path to chkentry executable (def: %s)\n";
static const char * const usage_msg3 =
"\t-a answers\twrite answers to a file for easier updating of a submission\n"
"\t-A answers\twrite answers file even if it already exists\n"
"\t-i answers\tread answers from file previously written by -a|-A answers\n\n"
"\t NOTE: One cannot use both -a/-A answers and -i answers at the same time.\n"
"\n"
"\twork_dir\tdirectory where the submission directory and tarball are formed\n"
"\tprog.c\t\tpath to the C source for your submission\n";
static const char * const usage_msg4 =
"\n"
"\tMakefile\tMakefile to build (make all) and cleanup (make clean & make clobber)\n"
"\n"
"\tremarks.md\tRemarks about your submission in markdown format\n"
"\t\t\tNOTE: The following is a guide to markdown:\n"
"\n"
"\t\t\t https://www.markdownguide.org/basic-syntax\n"
"\n"
"\t[file ...]\textra data files to include with your submission\n"
"\n"
"Exit codes:\n"
" 0 all OK\n"
" 2 -h and help string printed or -V and version string printed\n"
" 3 invalid command line, invalid option or option missing an argument\n"
" >= 10 internal error\n"
"\n"
"%s version: %s\n"
"jparse UTF-8 version: %s\n"
"jparse library version: %s";
/*
* globals
*/
static bool quiet = false; /* true ==> quiet mode */
static bool need_confirm = true; /* true ==> ask for confirmations */
static bool need_hints = true; /* true ==> show hints */
static bool ignore_warnings = false; /* true ==> ignore all warnings (this does NOT mean the judges will! :) */
static FILE *input_stream = NULL; /* input file: stdin or answers file */
static unsigned answers_errors; /* > 0 ==> output errors on answers file */
static bool answer_yes = false; /* true ==> -y used: always answer yes (use with EXTREME caution!) */
static bool entertain = false; /* for -e mode for txzchk -e */
/*
* forward declarations
*/
static void usage(int exitcode, char const *program, char const *str) __attribute__((noreturn));
int
main(int argc, char *argv[])
{
char const *program = NULL; /* our name */
extern char *optarg; /* option argument */
extern int optind; /* argv index of the next arg */
struct timeval tp; /* gettimeofday time value */
char const *work_dir = NULL; /* where the submission directory and tarball are formed */
char const *prog_c = NULL; /* path to prog.c */
char const *Makefile = NULL; /* path to Makefile */
char const *remarks_md = NULL; /* path to remarks.md */
char *tar = TAR_PATH_0; /* path to tar executable that supports the -J (xz) option */
char *cp = CP_PATH_0; /* path to cp executable */
char *ls = LS_PATH_0; /* path to ls executable */
char *txzchk = TXZCHK_PATH_0; /* path to txzchk executable */
char *fnamchk = FNAMCHK_PATH_0; /* path to fnamchk executable */
char *chkentry = CHKENTRY_PATH_0; /* path to chkentry executable */
char const *answers = NULL; /* path to the answers file (recording input given on stdin) */
FILE *answerp = NULL; /* file pointer to the answers file */
char *submission_dir = NULL; /* submission directory from which to form a compressed tarball */
char *tarball_path = NULL; /* path of the compressed tarball to form */
int extra_count = 0; /* number of extra files */
char **extra_list = NULL; /* list of extra files (if any) */
struct info info; /* data to form .info.json */
struct auth auth; /* data to form .auth.json */
int author_count = 0; /* number of authors */
struct author *author_set = NULL; /* list of authors */
bool tar_flag_used = false; /* true ==> -t /path/to/tar was given */
bool cp_flag_used = false; /* true ==> -c /path/to/cp was given */
bool ls_flag_used = false; /* true ==> -l /path/to/ls was given */
bool answers_flag_used = false; /* true ==> -a write answers to answers file */
bool read_answers_flag_used = false; /* true ==> -i read answers from answers file */
bool overwrite_answers_flag_used = false; /* true ==> don't prompt to overwrite answers if it already exists */
bool txzchk_flag_used = false; /* true ==> -T /path/to/txzchk was given */
bool fnamchk_flag_used = false; /* true ==> -F /path/to/fnamchk was given */
bool chkentry_flag_used = false; /* true ==> -C /path/to/chkentry was given */
bool overwrite_answers = true; /* true ==> overwrite answers file even if it already exists */
RuleCount size; /* rule_count() processing results */
int ret; /* libc return code */
int i;
/*
* parse args
*/
input_stream = stdin; /* default to reading from standard in */
program = argv[0];
while ((i = getopt(argc, argv, ":hv:J:qVt:c:l:a:i:A:WT:eF:C:y")) != -1) {
switch (i) {
case 'h': /* -h - print help to stderr and exit 2 */
usage(2, program, ""); /*ooo*/
not_reached();
break;
case 'v': /* -v verbosity */
/*
* parse verbosity
*/
verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -v verbosity"); /*ooo*/
not_reached();
}
break;
case 'J': /* -J json_verbosity */
/*
* parse json verbosity level
*/
json_verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -J json_verbosity"); /*ooo*/
not_reached();
}
break;
case 'q':
quiet = true;
msg_warn_silent = true;
break;
case 'V': /* -V - print version and exit 2 */
print("%s version: %s\n", MKIOCCCENTRY_BASENAME, MKIOCCCENTRY_VERSION);
print("jparse UTF-8 version: %s\n", JPARSE_UTF8_VERSION);
print("jparse library version: %s\n", JPARSE_LIBRARY_VERSION);
exit(2); /*ooo*/
not_reached();
break;
case 't': /* -t /path/to/tar */
tar = optarg;
tar_flag_used = true;
break;
case 'c': /* -c /path/to/cp */
cp = optarg;
cp_flag_used = true;
break;
case 'l': /* -l /path/to/ls */
ls = optarg;
ls_flag_used = true;
break;
case 'A': /* -A answers overwrite answers file */
answers = optarg;
overwrite_answers_flag_used = true;
/* FALL THROUGH */
case 'a': /* -a record_answers */
answers = optarg;
answers_flag_used = true;
break;
case 'i': /* -i input_recorded_answers */
answers = optarg;
read_answers_flag_used = true;
need_confirm = false;
need_hints = false;
break;
case 'W': /* -W ignores all warnings (this does NOT the judges will! :) ) */
ignore_warnings = true;
break;
case 'T':
txzchk_flag_used = true;
txzchk = optarg;
break;
case 'e':
entertain = true;
break;
case 'F':
fnamchk_flag_used = true;
fnamchk = optarg;
break;
case 'C':
chkentry_flag_used = true;
chkentry = optarg;
break;
case 'y':
answer_yes = true;
need_confirm = false;
ignore_warnings = true;
break;
case ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
check_invalid_option(program, i, optopt);
usage(3, program, ""); /*ooo*/
not_reached();
break;
}
}
/* must have at least the required number of args */
if (argc - optind < REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
/*
* guess where tar, cp and ls utilities are located
*
* If the user did not give a -t, -c and/or -l /path/to/x path, then look at
* the historic location for the utility. If the historic location of the utility
* isn't executable, look for an executable in the alternate location.
*
* On some systems where /usr/bin != /bin, the distribution made the mistake of
* moving historic critical applications, look to see if the alternate path works instead.
*/
find_utils(tar_flag_used, &tar, cp_flag_used, &cp, ls_flag_used, &ls,
txzchk_flag_used, &txzchk, fnamchk_flag_used, &fnamchk,
chkentry_flag_used, &chkentry);
/* check that conflicting answers file options are not used together */
if (answers_flag_used && read_answers_flag_used) {
err(3, __func__, "-a answers and -i answers cannot be used together"); /*ooo*/
not_reached();
}
/* collect required the required args */
extra_count = (argc - optind > REQUIRED_ARGS) ? argc - optind - REQUIRED_ARGS : 0;
extra_list = argv + optind + REQUIRED_ARGS;
dbg(DBG_LOW, "tar: %s", tar);
dbg(DBG_LOW, "cp: %s", cp);
dbg(DBG_LOW, "ls: %s", ls);
work_dir = argv[optind];
dbg(DBG_LOW, "work_dir: %s", work_dir);
prog_c = argv[optind + 1];
dbg(DBG_LOW, "prog.c: %s", prog_c);
Makefile = argv[optind + 2];
dbg(DBG_LOW, "Makefile: %s", Makefile);
remarks_md = argv[optind + 3];
dbg(DBG_LOW, "remarks: %s", remarks_md);
dbg(DBG_LOW, "number of extra data file args: %d", extra_count);
dbg(DBG_LOW, "answers file: %s", answers);
/*
* zeroize info
*/
memset(&info, 0, sizeof(info));
/*
* record the time
*/
errno = 0; /* pre-clear errno for errp() */
ret = gettimeofday(&tp, NULL);
if (ret < 0) {
errp(10, __func__, "gettimeofday failed");
not_reached();
}
info.tstamp = tp.tv_sec;
if ((time_t)(-1) > 0) {
/* case: unsigned time_t */
dbg(DBG_HIGH, "info.tstamp: %ju", (uintmax_t)info.tstamp);
} else {
/* case: signed time_t */
dbg(DBG_HIGH, "info.tstamp: %jd", (intmax_t)info.tstamp);
}
info.usec = tp.tv_usec;
dbg(DBG_HIGH, "info.usec: %jd", (intmax_t)info.usec);
/*
* Welcome
*/
if (!quiet) {
print("Welcome to mkiocccentry version: %s\n", MKIOCCCENTRY_VERSION);
}
/*
* save our version
*/
info.mkiocccentry_ver = MKIOCCCENTRY_VERSION;
dbg(DBG_HIGH, "info.mkiocccentry_ver: %s", info.mkiocccentry_ver);
/* warn about -y option */
if (answer_yes) {
para("",
"WARNING: you've chosen to answer yes to almost all prompts. If this was",
"unintentional, run the program again without specifying -y. We cannot",
"stress the importance of this enough! Well OK, we can over-stress most things",
"but you get the point. Do not use the -y option without EXTREME caution!",
"",
NULL);
}
/* if the user requested to ignore warnings, ignore this once and warn them :) */
if (ignore_warnings) {
para("",
"WARNING: You've chosen to ignore all warnings. While we will not show",
"you any additional warnings, you should note that The Judges will NOT",
"ignore warnings! If this was unintentional, run the program again",
"without specifying -W. We cannot stress the importance of this enough!",
"Well OK, we can over-stress most things, but you get the point.",
"Do not use the -W option!",
"",
NULL);
}
/*
* environment sanity checks
*/
if (!quiet) {
para("", "Performing sanity checks on your environment ...", NULL);
}
mkiocccentry_sanity_chks(&info, work_dir, tar, cp, ls, txzchk, fnamchk, chkentry);
if (!quiet) {
para("... environment looks OK", "", NULL);
}
/*
* if -a answers was specified and answers file exists, prompt user if they
* want to overwrite it; if they don't tell them how to use it and abort.
* Else it will be overwritten.
*/
if (answers_flag_used && !overwrite_answers_flag_used && answers != NULL && strlen(answers) > 0 && exists(answers)) {
overwrite_answers = yes_or_no("WARNING: The answers file already exists! Do you wish to overwrite it? [yn]");
if (!overwrite_answers) {
errno = 0; /* pre-clear errno for errp() */
ret = printf("\nTo use the answers file, try:\n\n\t./mkiocccentry -i %s [...]\n\n", answers);
if (ret <= 0) {
errp(11, __func__, "printf error telling the user how to use the answers file");
not_reached();
}
err(12, __func__, "won't overwrite answers file");
not_reached();
}
}
/*
* check if we should read input from answers file
*/
if (read_answers_flag_used && answers != NULL && strlen(answers) > 0) {
if (!is_read(answers)) {
errp(13, __func__, "answers file not readable");
not_reached();
}
errno = 0; /* pre-clear errno for errp() */
answerp = fopen(answers, "r");
if (answerp == NULL) {
errp(14, __func__, "cannot open answers file");
not_reached();
}
input_stream = answerp;
}
/*
* obtain the IOCCC contest ID
*/
info.ioccc_id = get_contest_id(&info.test_mode, &read_answers_flag_used);
dbg(DBG_MED, "IOCCC contest ID: %s", info.ioccc_id);
/*
* found the answer file header in stdin
*/
if (read_answers_flag_used && answers == NULL) {
answerp = stdin;
}
/*
* obtain submit slot number
*/
info.submit_slot = get_submit_slot(&info);
dbg(DBG_MED, "submit slot number: %d", info.submit_slot);
/*
* create submission directory
*/
submission_dir = mk_submission_dir(work_dir, info.ioccc_id, info.submit_slot, &tarball_path, info.tstamp, info.test_mode);
errno = 0;
info.tarball = strdup(tarball_path);
if (info.tarball == NULL) {
errp(15, __func__, "strdup() tarball path %s failed", tarball_path);
not_reached();
}
dbg(DBG_LOW, "formed submission directory: %s", submission_dir);
/*
* if -a, open the answers file. We only do it after verifying that we can
* make the submission directory because if we get input before and find out the
* directory already exists then the answers file will have invalid data.
*/
if (answers_flag_used && answers != NULL && strlen(answers) > 0) {
errno = 0; /* pre-clear errno for errp() */
answerp = fopen(answers, "w");
if (answerp == NULL) {
errp(16, __func__, "cannot create answers file: %s", answers);
not_reached();
}
ret = fprintf(answerp, "%s\n", MKIOCCCENTRY_ANSWERS_VERSION);
if (ret <= 0) {
warnp(__func__, "fprintf error printing header to the answers file");
++answers_errors;
}
}
/*
* write the IOCCC id and submit slot number to the answers file
*/
if (answerp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%s\n", info.ioccc_id);
if (ret <= 0) {
warnp(__func__, "fprintf error printing IOCCC contest id to the answers file");
++answers_errors;
}
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%d\n", info.submit_slot);
if (ret <= 0) {
warnp(__func__, "fprintf error printing submit slot number to the answers file");
++answers_errors;
}
}
/*
* check prog.c
*/
if (!quiet) {
para("", "Checking prog.c ...", NULL);
}
size = check_prog_c(&info, submission_dir, cp, prog_c);
if (!quiet) {
para("... completed prog.c check.", "", NULL);
}
/*
* check Makefile
*/
if (!quiet) {
para("Checking Makefile ...", NULL);
}
check_Makefile(&info, submission_dir, cp, Makefile);
if (!quiet) {
para("... completed Makefile check.", "", NULL);
}
/*
* check remarks.md
*/
if (!quiet) {
para("Checking remarks.md ...", NULL);
}
check_remarks_md(&info, submission_dir, cp, remarks_md);
if (!quiet) {
para("... completed remarks.md check.", "", NULL);
}
/*
* check, if needed, extra data files
*/
if (!quiet) {
para("Checking extra data files ...", NULL);
}
check_extra_data_files(&info, submission_dir, cp, extra_count, extra_list);
if (!quiet) {
para("... completed extra data files check.", "", NULL);
}
/*
* obtain the title
*/
info.title = get_title(&info);
dbg(DBG_LOW, "submission title: %s", info.title);
if (answerp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%s\n", info.title);
if (ret <= 0) {
warnp(__func__, "fprintf error printing title to the answers file");
++answers_errors;
}
}
/*
* obtain the abstract
*/
info.abstract = get_abstract(&info);
dbg(DBG_LOW, "submission abstract: %s", info.abstract);
if (answerp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%s\n", info.abstract);
if (ret <= 0) {
warnp(__func__, "fprintf error printing abstract to the answers file");
++answers_errors;
}
}
/*
* obtain author information
*/
author_count = get_author_info(&author_set);
dbg(DBG_LOW, "collected information on %d authors", author_count);
/*
* if we have an answers file, record the verified author information
*/
if (answerp != NULL && !read_answers_flag_used) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%d\n", author_count);
if (ret <= 0) {
warnp(__func__, "fprintf error printing IOCCC author count to the answers file");
++answers_errors;
}
/*
* write answers for each author to the answers file
*/
for (i = 0; i < author_count; i++) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp,
"%s\n" /* name */
"%s\n" /* location code */
"%s\n" /* email */
"%s\n" /* url */
"%s\n" /* alt_url */
"%s\n" /* mastodon handle */
"%s\n" /* GitHub */
"%s\n" /* affiliation */
"%s\n" /* past winning author */
"%s\n" /* author_handle */
,
author_set[i].name,
author_set[i].location_code,
author_set[i].email,
author_set[i].url,
author_set[i].alt_url,
author_set[i].mastodon,
author_set[i].github,
author_set[i].affiliation,
author_set[i].past_winning_author?"y":"n",
author_set[i].author_handle);
if (ret <= 0) {
warnp(__func__, "fprintf error printing author info to the answers file");
++answers_errors;
}
}
}
/*
* write the .info.json file
*/
if (!quiet) {
para("", "Forming the .info.json file ...", NULL);
}
write_info(&info, submission_dir, chkentry, fnamchk);
if (!quiet) {
para("... completed the .info.json file.", "", NULL);
}
/*
* form auth
*/
form_auth(&auth, &info, author_count, author_set);
/*
* write the .auth.json file
*/
if (!quiet) {
para("", "Forming the .auth.json file ...", NULL);
}
write_auth(&auth, submission_dir, chkentry, fnamchk);
if (!quiet) {
para("... completed .auth.json file.", "", NULL);
}
/*
* finalize the answers file: either write the final answers (if writing
* answers) or read the answers EOF marker and then finally closing the
* stream.
*/
if (answerp != NULL) {
if (read_answers_flag_used) {
char *linep = NULL;
char *line;
bool error = true;
line = readline_dup(&linep, true, NULL, answerp);
if (linep != NULL) {
error = strcmp(line, MKIOCCCENTRY_ANSWERS_EOF) != 0;
free(linep);
linep = NULL;
}
if (error) {
errp(17, __func__, "expected ANSWERS_EOF marker at the end of the answers file");
not_reached();
}
input_stream = stdin;
if (line != NULL) {
free(line);
line = NULL;
}
} else {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(answerp, "%s\n", MKIOCCCENTRY_ANSWERS_EOF);
if (ret <= 0) {
warnp(__func__, "fprintf error writing ANSWERS_EOF marker to the answers file");
++answers_errors;
}
}
if (answers != NULL) {
errno = 0; /* pre-clear errno for warnp() */
ret = fclose(answerp);
if (ret != 0) {
warnp(__func__, "error in fclose to the answers file");
++answers_errors;
}
}
answerp = NULL;
}
/*
* remind the user about their answers file
*/
if (answers != NULL) {
if (need_hints) {
errno = 0; /* pre-clear errno for warnp() */
ret = printf("\nTo more easily update this submission you can run:\n\n ./mkiocccentry -i %s ...\n", answers);
if (ret <= 0) {
warnp(__func__, "unable to tell user how to more easily update submission");
}
}
}
/*
* warn the user if there were I/O errors while writing the answers file
*/
if (answers_flag_used) {
if (answers_errors > 0) {
errno = 0; /* pre-clear errno for warnp() */
ret = printf("Warning: There were %u I/O error%s on the answers file. Make SURE to verify that using the file\n"
"results in the proper input before re-uploading!\n",
answers_errors, answers_errors == 1 ? "" : "s" );
if (ret <= 0) {
warnp(__func__, "unable to warn user that there were I/O errors on the answers file");
}
}
}
/*
* If the answers file (-i answers) was used, and there were warnings/problems
* discovered with the submission that were overridden, warn the user.
*/
if (read_answers_flag_used) {
if (info.empty_override ||
info.rule_2a_override ||
info.rule_2a_mismatch ||
info.rule_2b_override ||
info.highbit_warning ||
info.nul_warning ||
info.trigraph_warning ||
info.wordbuf_warning ||
info.ungetc_warning ||
info.Makefile_override) {
do {
if (!ignore_warnings) {
need_confirm = true;
if (info.empty_override) {
warn_empty_prog(prog_c);
}
if (info.rule_2a_override) {
warn_rule_2a_size(&info, prog_c, RULE_2A_BIG_FILE_WARNING, size);
}
if (info.rule_2a_mismatch) {
warn_rule_2a_size(&info, prog_c, RULE_2A_IOCCCSIZE_MISMATCH, size);
}
if (info.rule_2b_override) {
warn_rule_2b_size(&info, prog_c);
}
if (info.highbit_warning) {
warn_high_bit(prog_c);
}
if (info.nul_warning) {
warn_nul_chars(prog_c);
}
if (info.trigraph_warning) {
warn_trigraph(prog_c);
}
if (info.wordbuf_warning) {
warn_wordbuf(prog_c);
}
if (info.ungetc_warning) {
warn_ungetc(prog_c);
}
if (info.Makefile_override) {
warn_Makefile(Makefile, &info);
}
}
} while (0);
}
}
/*
* form the .txz file
*/
form_tarball(work_dir, submission_dir, tarball_path, tar, ls, txzchk, fnamchk);
/*
* remind user various things e.g., to upload (unless in test mode)
*/
remind_user(work_dir, submission_dir, tar, tarball_path, info.test_mode);
/*
* free storage
*/
if (submission_dir != NULL) {
free(submission_dir);
submission_dir = NULL;
}
if (tarball_path != NULL) {
free(tarball_path);
tarball_path = NULL;
}
free_info(&info);
memset(&info, 0, sizeof(info));
free_auth(&auth);
memset(&auth, 0, sizeof(auth));
/*
* All Done!!! All Done!!! -- Jessica Noll, Age 2
*/
exit(0); /*ooo*/
}
/*
* usage - print usage to stderr
*
* Example:
* usage(3, program, "wrong number of arguments");
*
* given:
* exitcode value to exit with
* program our program name
* str top level usage message
*
* NOTE: We warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function does not return.
*/
static void
usage(int exitcode, char const *prog, char const *str)
{
/*
* firewall
*/
if (str == NULL) {
str = "((NULL str))";
warn(__func__, "\nin usage(): str was NULL, forcing it to be: %s\n", str);
}
if (prog == NULL) {
prog = MKIOCCCENTRY_BASENAME;
warn(__func__, "\nin usage(): prog was NULL, forcing it to be: %s\n", prog);
}
/*
* print the formatted usage stream
*/
if (*str != '\0') {
fprintf_usage(DO_NOT_EXIT, stderr, "%s\n", str);
}
fprintf_usage(DO_NOT_EXIT, stderr, usage_msg0, prog, DBG_DEFAULT, JSON_DBG_DEFAULT);
fprintf_usage(DO_NOT_EXIT, stderr, usage_msg1, TAR_PATH_0, CP_PATH_0, LS_PATH_0, TXZCHK_PATH_0, FNAMCHK_PATH_0);
fprintf_usage(DO_NOT_EXIT, stderr, usage_msg2, CHKENTRY_PATH_0);
fprintf_usage(DO_NOT_EXIT, stderr, "%s", usage_msg3);
fprintf_usage(exitcode, stderr, usage_msg4, MKIOCCCENTRY_BASENAME, MKIOCCCENTRY_VERSION,
JPARSE_UTF8_VERSION, JPARSE_LIBRARY_VERSION);
exit(exitcode); /*ooo*/
not_reached();
}
/*
* mkiocccentry_sanity_chks - perform basic sanity checks
*
* We perform basic sanity checks on paths and the IOCCC contest ID as well as
* the IOCCC toolkit tables.
*
* given:
*
* infop - pointer to info structure
* work_dir - where the submission directory and tarball are formed
* tar - path to tar that supports the -J (xz) option
* cp - path to the cp utility
* ls - path to the ls utility
* txzchk - path to txzchk tool
* fnamchk - path to fnamchk tool
* chkentry - path to chkentry tool
*
* NOTE: This function does not return on error or if things are not sane.
*/
static void
mkiocccentry_sanity_chks(struct info *infop, char const *work_dir, char const *tar, char const *cp,
char const *ls, char const *txzchk, char const *fnamchk, char const *chkentry)
{
/*
* firewall
*/
if (infop == NULL || work_dir == NULL || tar == NULL || cp == NULL || ls == NULL ||
txzchk == NULL || fnamchk == NULL || chkentry == NULL) {
err(18, __func__, "called with NULL arg(s)");
not_reached();
}
/*
* tar must be executable
*/
if (!exists(tar)) {
fpara(stderr,
"",
"We cannot find the tar program.",
"",
"A tar program that supports the -J (xz) option is required to build a compressed tarball.",
"Perhaps you need to use:",
"",
" mkiocccentry -t /path/to/tar ...",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(19, __func__, "tar does not exist: %s", tar);
not_reached();
}
if (!is_file(tar)) {
fpara(stderr,
"",
"The tar path, while it exists, is not a regular file.",
"",
"Perhaps you need to use another path:",
"",
" mkiocccentry -t /path/to/tar ...",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(20, __func__, "tar is not a regular file: %s", tar);
not_reached();
}
if (!is_exec(tar)) {
fpara(stderr,
"",
"The tar path, while it is a file, is not an executable.",
"",
"We suggest you check the permissions on the tar program, or use another path:",
"",
" mkiocccentry -t /path/to/tar ...",
"",
"and/or install a tar program? You can find the source for tar:",
"",
" https://www.gnu.org/software/tar/",
"",
NULL);
err(21, __func__, "tar is not an executable program: %s", tar);
not_reached();
}
/*
* cp must be executable
*/
if (!exists(cp)) {
fpara(stderr,
"",
"We cannot find the cp program.",
"",
"The cp program is required to copy files into a directory under work_dir.",
"Perhaps you need to use:",
"",
" mkiocccentry -c /path/to/cp ...",
"",
"and/or install a cp program? You can find the source for cp in core utilities:",
"",
" https://www.gnu.org/software/coreutils/",
"",
NULL);
err(22, __func__, "cp does not exist: %s", cp);