-
Notifications
You must be signed in to change notification settings - Fork 3
/
hardv.c
744 lines (689 loc) · 14.9 KB
/
hardv.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
#define _XOPEN_SOURCE 700
#include <sys/wait.h>
#include <libgen.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#define KCHAR "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"0123456789_"
#define SHELL "/bin/sh"
#define DAY (60*60*24)
#define PATHSZ 32767
#define NLINE INT_MAX
#define LINESZ 32767
#define NCARD 32767
#define NFIELD 16
#define KEYSZ 8
#define VALSZ 32767
#define Q "Q"
#define A "A"
#define MOD "MOD"
#define PREV "PREV"
#define NEXT "NEXT"
struct opt {
int exact;
int rand;
int maxn;
};
struct field {
char *key;
char *val;
struct field *next;
};
struct card {
int leadnewl;
struct field *field;
char *sep;
char *filepath;
};
char *progname;
int loadctab(struct card *ctab, char *path);
void learn(struct card *ctab, int ncard, time_t now, struct opt *opt);
void help(void);
void version(void);
int stdquiz(struct card *card, time_t now, int card1);
int modquiz(struct card *card, time_t now, int card1);
void dump(struct card *ctab, int n, char *path);
int loadcard(char *fn, FILE *fp, int *lineno, struct card *card);
void dumpcard(FILE *fp, struct card *card);
void shuf(struct card *a[], int n);
void sety(struct card *card, time_t now);
void setn(struct card *card, time_t now);
char *timev(time_t clock, char *buf, int n);
char *getv(struct card *card, char *k);
char *setv(struct card *card, char *k, char *v);
int isvalidf(struct field *f);
time_t getdiff(struct card *card, time_t now);
struct field *revfield(struct field *f);
int isnow(struct card *card, time_t now, int exact);
char *normv(char *s, char *buf, int n);
int pindent(char *s);
time_t tmparse(char *s);
char *swapname(char *sn, char *fn);
void err(char *s, ...);
void parserr(char *fn, int ln, char *s);
void syserr();
int main(int argc, char **argv)
{
static struct card ctab[NCARD];
static int ncard;
struct opt opt;
struct flock lock;
int n, ch, *sig;
time_t now;
progname = argv[0];
srand(getpid()^time(NULL));
if (argc > 1 && !strcmp(argv[1], "--help"))
help();
if (argc > 1 && !strcmp(argv[1], "--version"))
version();
if ((now = tmparse(getenv("HARDV_NOW"))) <= 0)
time(&now);
memset(&opt, 0, sizeof opt);
opt.maxn = -1;
while ((ch = getopt(argc, argv, "ern:")) != -1)
switch (ch) {
case 'e':
opt.exact = 1;
break;
case 'r':
opt.rand = 1;
break;
case 'n':
if ((opt.maxn = atoi(optarg)) <= 0)
err(
"Not valid positive integer: "
"%s\n",
optarg
);
break;
default:
return 1;
}
argv += optind;
argc -= optind;
if (argc <= 0)
err("File operand expected\n");
for (; *argv != NULL; argv++) {
int fd;
if ((fd = open(*argv, O_RDWR)) == -1)
syserr();
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1) {
if (errno == EAGAIN || errno == EACCES)
err("%s: Other process is accessing\n",
*argv);
syserr();
}
ncard += loadctab(&ctab[ncard], *argv);
}
learn(ctab, ncard, now, &opt);
return 0;
}
void help(void)
{
puts("Usage:\n");
printf("\t%s [options] FILE...\n", progname);
printf("\t%s --help|--version\n", progname);
puts("Options:");
puts(" -e enable exact quiz time");
puts(" -r randomize the quiz order");
puts(" -n N quiz at most N cards");
puts(" --help print the brief help");
puts(" --version");
puts(" print the version information");
exit(0);
}
void version(void)
{
puts("HardV 5.0.0-alpha.1 <https://github.com/dongyx/hardv>");
puts("Copyright (c) 2022 DONG Yuxuan <https://www.dyx.name>");
putchar('\n');
printf("NLINE: %d\n", NLINE);
printf("LINESZ: %d\n", LINESZ);
printf("NCARD: %d\n", NCARD);
printf("NFIELD: %d\n", NFIELD);
printf("KEYSZ: %d\n", KEYSZ);
printf("VALSZ: %d\n", VALSZ);
printf("PATHSZ: %d\n", PATHSZ);
exit(0);
}
int loadctab(struct card *ctab, char *path)
{
FILE *in;
int n, ln;
in = fopen(path, "r");
if (in == NULL)
syserr();
ln = 0;
n = 0;
while (n < NCARD && loadcard(path, in, &ln, &ctab[n]))
n++;
if (!feof(in))
err("Too many cards\n");
return n;
}
void learn(struct card *ctab, int ncard, time_t now, struct opt *opt)
{
static int card1 = 1;
struct card *plan[NCARD];
struct card *card;
int n, i, dirty;
n = 0;
for (card = ctab; card < &ctab[ncard]; card++)
if (card->field && isnow(card, now, opt->exact))
plan[n++] = card;
if (opt->rand)
shuf(plan, n);
for (i = 0; opt->maxn != 0 && i < n; i++) {
card = plan[i];
if (getv(card, MOD))
dirty = modquiz(card, now, card1);
else
dirty = stdquiz(card, now, card1);
if (dirty)
dump(ctab, ncard, card->filepath);
if (opt->maxn > 0)
opt->maxn--;
card1 = 0;
}
}
void dump(struct card *ctab, int n, char *path)
{
char sn[PATHSZ];
FILE *fp, *sp;
int fd;
struct card *card;
sigset_t bset, oset;
sigemptyset(&bset);
sigaddset(&bset, SIGHUP);
sigaddset(&bset, SIGINT);
sigaddset(&bset, SIGTERM);
sigaddset(&bset, SIGQUIT);
sigaddset(&bset, SIGTSTP);
sigprocmask(SIG_BLOCK, &bset, &oset);
swapname(sn, path);
if ((fd = open(sn, O_WRONLY|O_CREAT|O_EXCL, 0600)) == -1)
err("%s: %s\n", sn, strerror(errno));
if (!(sp = fdopen(fd, "w")))
syserr();
for (card = ctab; card < ctab+n; card++)
if (strcmp(card->filepath, path) == 0)
dumpcard(sp, card);
if (
fflush(sp) == EOF ||
fsync(fileno(sp)) == -1
)
syserr();
fclose(sp);
if ((fp = fopen(path, "w")) == NULL)
syserr();
for (card = ctab; card < ctab+n; card++)
if (strcmp(card->filepath, path) == 0)
dumpcard(fp, card);
if (
fflush(fp) == EOF ||
fsync(fileno(fp)) == -1
)
syserr();
if (unlink(sn) == -1)
syserr();
sigprocmask(SIG_SETMASK, &oset, NULL);
}
char *swapname(char *sn, char *fn)
{
static char *epsz = "File path too long\n";
char buf[PATHSZ], dn[PATHSZ], *bn;
strncpy(buf, fn, PATHSZ);
if (buf[PATHSZ - 1])
err(epsz);
strcpy(dn, dirname(buf));
bn = basename(strcpy(buf, fn));
if (snprintf(sn, PATHSZ, "%s/.%s.swp", dn, bn) >= PATHSZ)
err(epsz);
return sn;
}
int stdquiz(struct card *card, time_t now, int card1)
{
char in[LINESZ], ques[VALSZ], answ[VALSZ];
char *act;
if (!card1)
putchar('\n');
normv(getv(card, Q), ques, VALSZ);
normv(getv(card, A), answ, VALSZ);
puts("Q:\n");
pindent(ques);
puts("\n");
fflush(stdout);
do {
fputs("Press <ENTER> to check the answer.\n", stdout);
fflush(stdout);
while (!fgets(in, LINESZ, stdin)) {
if (feof(stdin))
exit(0);
if (errno != EINTR)
syserr();
}
} while (strcmp(in, "\n"));
puts("A:\n");
pindent(answ);
puts("\n");
fflush(stdout);
do {
fputs("Do you recall? (y/n/s)\n", stdout);
fflush(stdout);
while (!fgets(in, LINESZ, stdin)) {
if (feof(stdin))
exit(0);
if (errno != EINTR)
syserr();
}
act = strstr("y\nn\ns\n", in);
} while (!act || *act == '\n');
switch (*act) {
case 'y':
sety(card, now);
return 1;
case 'n':
setn(card, now);
return 1;
}
return 0;
}
int modquiz(struct card *card, time_t now, int card1)
{
char mod[VALSZ], q[VALSZ], a[VALSZ];
char pfx[] = "HARDV_F_";
char sprev[64], snext[64], snow[64], first[2];
char k[KEYSZ + sizeof(pfx) - 1];
struct field *f;
pid_t pid;
int st;
if ((pid = fork()) == -1)
syserr();
if (pid == 0) {
normv(getv(card, MOD), mod, VALSZ);
normv(getv(card, Q), q, VALSZ);
normv(getv(card, A), a, VALSZ);
sprintf(snext, "%ld", (long)tmparse(getv(card, NEXT)));
sprintf(sprev, "%ld", (long)tmparse(getv(card, PREV)));
sprintf(snow, "%ld", (long)now);
first[0] = first[1] = '\0';
if (card1)
first[0] = '1';
if (
setenv("HARDV_Q", q, 1) == -1 ||
setenv("HARDV_A", a, 1) == -1 ||
setenv("HARDV_NEXT", snext, 1) == -1 ||
setenv("HARDV_PREV", sprev, 1) == -1 ||
setenv("HARDV_NOW", snow, 1) == -1 ||
setenv("HARDV_FIRST", first, 1) == -1
)
syserr();
strcpy(k, pfx);
for (f = card->field; f != NULL; f = f->next) {
strcpy(&k[sizeof(pfx) - 1], f->key);
if (setenv(k, f->val, 1) == -1)
syserr();
}
execl(SHELL, SHELL, "-c", mod, NULL);
syserr();
}
while (waitpid(pid, &st, 0) == -1)
if (errno != EINTR)
syserr();
switch (WEXITSTATUS(st)) {
case 0:
sety(card, now);
return 1;
case 1:
setn(card, now);
return 1;
}
return 0;
}
void sety(struct card *card, time_t now)
{
char buf[VALSZ];
time_t diff;
diff = getdiff(card, now);
setv(card, PREV, timev(now, buf, VALSZ));
setv(card, NEXT, timev(now + 2 * diff, buf, VALSZ));
}
void setn(struct card *card, time_t now)
{
char buf[VALSZ];
setv(card, PREV, timev(now, buf, VALSZ));
setv(card, NEXT, timev(now + DAY, buf, VALSZ));
}
int loadcard(char *fn, FILE *fp, int *lineno, struct card *card)
{
static char *enl = "Too many lines";
static char *evf = "Invalid value";
static char *evsz = "Value too large";
char lb[LINESZ], k[KEYSZ], v[VALSZ], *vp;
struct field *f;
int s, n, ch;
int nf, nq, na;
int start, kl;
start = *lineno;
if (feof(fp))
return 0;
memset(card, 0, sizeof *card);
nf = 0;
while ((ch = fgetc(fp)) == '\n') {
if (*lineno >= NLINE)
parserr(fn, *lineno, enl);
(*lineno)++;
card->leadnewl++;
}
ungetc(ch, fp);
vp = NULL;
f = NULL;
nq = na = 0;
kl = start + card->leadnewl;
while (fgets(lb, LINESZ, fp)) {
if (*lineno >= NLINE)
parserr(fn, *lineno, enl);
(*lineno)++;
n = strlen(lb);
if (lb[n - 1] != '\n' && !feof(fp))
parserr(fn, *lineno, "Line too long");
if (lb[0] == '%') {
/* end */
if (!(card->sep = strdup(lb)))
syserr();
break;
} else if (lb[0] == '\t' || lb[0] == '\n') {
/* successive lines in value */
if (!f)
parserr(fn, *lineno, "Key is expected");
if (vp - v >= VALSZ - n)
parserr(fn, *lineno, evsz);
vp = stpcpy(vp, lb);
} else {
/* new field */
if (nf >= NFIELD)
parserr(fn, *lineno, "Too many fields");
if (f) {
if (!(f->key = strdup(k)))
syserr();
if (!(f->val = strdup(v)))
syserr();
if (!isvalidf(f))
parserr(fn, kl, evf);
}
kl = *lineno;
nf++;
vp = v;
*vp = '\0';
s = strcspn(lb, "\n\t");
if (s >= KEYSZ)
parserr(fn, *lineno, "Key too large");
*stpncpy(k, lb, s) = '\0';
if (k[strspn(k, KCHAR)])
parserr(fn, *lineno, "Invalid key");
for (f = card->field; f; f = f->next)
if (!strcmp(f->key, k))
parserr(
fn,
*lineno,
"Duplicated key"
);
if (!strcmp(k, Q))
nq++;
if (!strcmp(k, A))
na++;
if (!(f = malloc(sizeof *f)))
syserr();
f->next = card->field;
card->field = f;
if (!lb[s])
continue;
if (vp - v >= VALSZ - (n - s))
parserr(fn, *lineno, evsz);
vp = stpcpy(vp, &lb[s]);
}
}
if (ferror(fp))
syserr();
if (f) {
if (!(f->key = strdup(k)) || !(f->val = strdup(v)))
syserr();
if (!isvalidf(f))
parserr(fn, kl, evf);
if (nq != 1 || na != 1)
parserr(
fn,
start + card->leadnewl + 1,
"No mandatory field"
);
card->field = revfield(card->field);
}
if ((card->filepath = strdup(fn)) == NULL)
syserr();
return 1;
}
void dumpcard(FILE *fp, struct card *card)
{
struct field *f;
int i;
for (i = 0; i < card->leadnewl; i++)
if (fputc('\n', fp) == EOF)
syserr();
for (f = card->field; f != NULL; f = f->next)
if (fprintf(fp, "%s%s", f->key, f->val) < 0)
syserr();
if (card->sep && fputs(card->sep, fp) == EOF)
syserr();
}
void parserr(char *fn, int ln, char *s)
{
fprintf(
stderr,
"%s: %s: line %d: %s\n",
progname,
fn,
ln,
s
);
exit(-1);
}
char *normv(char *s, char *buf, int n)
{
char *sp, *bp;
while (*s == '\n')
s++;
for (sp = s, bp = buf; bp != &buf[n] && *sp; sp++)
if (*sp != '\t' || sp > s && sp[-1] != '\n')
*bp++ = *sp;
if (bp == &buf[n])
err("Buffer too small to normalize value\n");
while (bp > buf && bp[-1] == '\n')
bp--;
*bp = '\0';
return buf;
}
int isnow(struct card *card, time_t now, int exact)
{
struct tm today, theday;
time_t next;
next = tmparse(getv(card, NEXT));
if (next <= 0)
next = now;
if (exact)
return now >= next;
memcpy(&today, localtime(&now), sizeof today);
memcpy(&theday, localtime(&next), sizeof theday);
if (today.tm_year > theday.tm_year)
return 1;
if (
today.tm_year == theday.tm_year
&& today.tm_mon > theday.tm_mon
)
return 1;
if (
today.tm_year == theday.tm_year
&& today.tm_mon == theday.tm_mon
&& today.tm_mday >= theday.tm_mday
)
return 1;
return 0;
}
void shuf(struct card *a[], int n)
{
struct card *t;
int i, j;
for (i = 0; i < n; i++) {
j = i + rand() % (n - i);
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int isvalidf(struct field *f)
{
if (!strcmp(f->key, NEXT) || !strcmp(f->key, PREV))
if (tmparse(f->val) <= 0)
return 0;
return 1;
}
time_t tmparse(char *s)
{
/* timegm() is not conforming to any standard,
* but provided by both BSD and Linux.
* Explicit declaration could avoid the trouble caused by
* feature test macros.
*/
extern time_t timegm(struct tm*);
unsigned hr, mi;
char sg;
struct tm tm;
time_t ck;
if (!s)
return 0;
while (isspace((unsigned char)*s))
s++;
memset(&tm, 0, sizeof tm);
if (
!(s = strptime(s, "%Y-%m-%d %H:%M:%S", &tm)) ||
(sscanf(s, " %c%2u%2u", &sg, &hr, &mi) != 3)
)
return 0;
ck = timegm(&tm);
if (sg == '+')
ck -= mi*60 + hr*3600L;
else if (sg == '-')
ck += mi*60 + hr*3600L;
else
return 0;
return ck;
}
struct field *revfield(struct field *f)
{
struct field *r, *swp;
/* r points to the reversed part, f points to the left part */
r = NULL;
while (f) {
swp = f->next;
f->next = r;
r = f;
f = swp;
}
return r;
}
void err(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(-1);
}
void syserr()
{
perror(progname);
exit(-1);
}
time_t getdiff(struct card *card, time_t now)
{
time_t prev, next, diff;
prev = tmparse(getv(card, PREV));
next = tmparse(getv(card, NEXT));
if (prev <= 0)
prev = now;
if (next <= 0)
next = now;
if (next < prev || (diff = next - prev) < DAY)
diff = DAY;
return diff;
}
char *timev(time_t clock, char *buf, int n)
{
struct tm *lc;
int p;
if (n < 3)
goto noroom;
lc = localtime(&clock);
buf[0] = '\t';
p = strftime(&buf[1], n - 2, "%Y-%m-%d %H:%M:%S %z", lc);
if (!p)
goto noroom;
buf[1 + p] = '\n';
buf[2 + p] = '\0';
return buf;
noroom: err("Buffer too small to format timestamp %ld\n", (long)clock);
return NULL;
}
char *getv(struct card *card, char *k)
{
struct field *i;
for (i = card->field; i; i = i->next)
if (!strcmp(i->key, k))
return i->val;
return NULL;
}
char *setv(struct card *card, char *k, char *v)
{
struct field *i;
for (i = card->field; i; i = i->next)
if (!strcmp(i->key, k))
break;
if (!i) {
if (!(i = malloc(sizeof *i)))
syserr();
memset(i, 0, sizeof *i);
if (!(i->key = strdup(k)))
syserr();
i->val = NULL;
i->next = card->field;
card->field = i;
}
free(i->val);
if (!(i->val = strdup(v)))
syserr();
return i->val;
}
int pindent(char *s)
{
char *sp;
for (sp = s; *sp ; sp++) {
if (sp == s || sp[-1] == '\n')
putchar('\t');
putchar(*sp);
}
return sp - s;
}