-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoday.c
More file actions
658 lines (607 loc) · 20.7 KB
/
Copy pathtoday.c
File metadata and controls
658 lines (607 loc) · 20.7 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
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
/*
* today.c -- prints birthdays, historical events, a thought for the day,
* and reminders for a given date, as an HTML fragment styled to match
* the DOS TODAY36 screen (see ../README.md).
*
* Copyright (c) 2026, Leo Bicknell. Distributed under the 3-Clause BSD
* License; see ../LICENSE.
*
* Based on the original Unix reader recovered in the `today` research
* repo (https://github.com/bicknell/today/blob/main/today/today.c),
* kept as close to that structure as possible: the same record format,
* the same struct line, the same weekdays/months tables, and the same
* gather-into-a-buffer-per-section approach. What's added is the header
* banner, the box-drawn border, HTML color markup, a fourth (F, Fortune/
* Thought-for-the-day) record type read from today.wit, and pagination.
*
* Usage: today [MMDD [YYYY]]
* With no arguments, uses the current date (including the current
* year). MMDD alone uses the current year; MMDD YYYY sets both
* explicitly. The year is never used to look up events (the data files
* are year-agnostic) -- it only resolves column 10's day-of-week
* gating (see matches()), which needs some concrete year to compute a
* weekday against. Data files (today.1..12, today.wit, today.own) are
* read from the current directory, same as the original.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct line {
char code;
char month[3];
char day[3];
char year[5];
char special;
char message[80];
};
char months[12][20] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
FILE *birthdayfile;
FILE *eventfile;
FILE *reminderfile;
FILE *thoughtfile;
int birthday, event, reminder, thought;
void add(struct line *, FILE *);
void gather(FILE *fp, int wantmonth, int wantday, int wantyear);
int matches(struct line *rec, int wantmonth, int wantday, int wantyear);
/* Screen geometry, matching README.md "Screen Layout". */
#define COLS 78 /* interior width, between the two border chars */
#define PAGEROWS 21 /* content rows per screen, border to border */
#define PAGEBUDGET 18 /* content lines shown before "Press ENTER..." */
/* One line of final, laid-out output. kind 'Y' is a body line of the
* form "In YYYY <text>", where the year prints in its own color; text
* holds everything after "In YYYY " for that kind, or the whole
* (already-margined) line for the other three kinds. */
struct outline {
char kind; /* 'H' hdr, 'S' sec, 'B' body, 'Y' body-with-year */
char year[5];
char text[300];
};
#define MAXOUT 400
struct outline outbuf[MAXOUT];
int nout = 0;
void addout(char kind, const char *year, const char *text);
void layout(FILE *fp, const char *header);
void emitpages(void);
void htmlescape(const char *in, char *out, size_t outsize);
void printborder(const char *left, const char *right);
void printplain(char kind, const char *text);
void printyearline(const char *year, const char *text);
void printblank(void);
int main(int argc, char *argv[]) {
struct tm *t;
FILE *fp;
char filename[500];
time_t now;
int wantmonth, wantday, wantyear;
char famous[250];
char escaped[150];
int needblank;
birthday = event = reminder = thought = 0;
if (argc > 1 && strlen(argv[1]) == 4) {
if (sscanf(argv[1], "%2d%2d", &wantmonth, &wantday) != 2) {
printf("Usage: %s [MMDD [YYYY]]\n", argv[0]);
exit(1);
}
if (argc > 2) {
wantyear = atoi(argv[2]);
} else {
now = time(NULL);
t = localtime(&now);
wantyear = t->tm_year + 1900;
}
} else {
now = time(NULL);
t = localtime(&now);
wantmonth = t->tm_mon + 1;
wantday = t->tm_mday;
wantyear = t->tm_year + 1900;
}
if (wantmonth < 1 || wantmonth > 12 || wantday < 1 || wantday > 31 || wantyear < 1) {
printf("Usage: %s [MMDD [YYYY]]\n", argv[0]);
exit(1);
}
if ((birthdayfile = tmpfile()) == NULL) {
printf("Error opening temporary file.\n");
exit(10);
}
if ((eventfile = tmpfile()) == NULL) {
printf("Error opening temporary file.\n");
exit(10);
}
if ((reminderfile = tmpfile()) == NULL) {
printf("Error opening temporary file.\n");
exit(10);
}
if ((thoughtfile = tmpfile()) == NULL) {
printf("Error opening temporary file.\n");
exit(10);
}
sprintf(filename, "today.%d", wantmonth);
if ((fp = fopen(filename, "r")) == NULL) {
printf("Unable to open file %s.\n", filename);
exit(1);
}
gather(fp, wantmonth, wantday, wantyear);
fclose(fp);
/* today.wit isn't split by month, and its F records use the same
* layout as B/S/R -- gather() already only picks up matching dates. */
if ((fp = fopen("today.wit", "r")) != NULL) {
gather(fp, wantmonth, wantday, wantyear);
fclose(fp);
}
/* today.own (optional personal reminders) is also searched alongside
* the monthly files -- see TODAY36.DOC "Data File Format" and its
* WHEN section ("TODAY.OWN is also searched"). */
if ((fp = fopen("today.own", "r")) != NULL) {
gather(fp, wantmonth, wantday, wantyear);
fclose(fp);
}
htmlescape(months[wantmonth - 1], escaped, sizeof(escaped));
sprintf(famous, " Famous events for %s %d:", escaped, wantday);
addout('H', NULL, famous);
addout('B', NULL, "");
needblank = 0;
if (birthday) {
if (needblank) addout('B', NULL, "");
layout(birthdayfile, " Happy Birthday to...");
needblank = 1;
}
if (event) {
if (needblank) addout('B', NULL, "");
layout(eventfile, " On this day...");
needblank = 1;
}
if (thought) {
if (needblank) addout('B', NULL, "");
layout(thoughtfile, " Thought for the day...");
needblank = 1;
}
if (reminder) {
if (needblank) addout('B', NULL, "");
layout(reminderfile, " And remember...");
needblank = 1;
}
emitpages();
fclose(birthdayfile);
fclose(eventfile);
fclose(reminderfile);
fclose(thoughtfile);
return 0;
}
/* Reads one data file, bucketing matching-date records into the three
* (well, four, with today.wit) section buffers by record code -- one
* pass, same as the original. */
void gather(FILE *fp, int wantmonth, int wantday, int wantyear) {
struct line rec;
char temp[500];
while (fgets(temp, 500, fp) != NULL) {
if ((strlen(temp) > 11) &&
((temp[0] == 'B') || (temp[0] == 'S') || (temp[0] == 'R') || (temp[0] == 'F'))) {
sscanf(temp, "%c%c%c%c%c%c%c%c%c%c%60s", &rec.code, &rec.month[0],
&rec.month[1], &rec.day[0], &rec.day[1], &rec.year[0],
&rec.year[1], &rec.year[2], &rec.year[3], &rec.special,
rec.message);
rec.month[2] = '\0';
rec.day[2] = '\0';
rec.year[4] = '\0';
strcpy(rec.message, &temp[10]);
{
/* Original data files are DOS text: CRLF line endings, which
* fgets() leaves on the end of temp (and so on rec.message). */
size_t mlen = strlen(rec.message);
while (mlen > 0 && (rec.message[mlen - 1] == '\n' || rec.message[mlen - 1] == '\r')) {
rec.message[--mlen] = '\0';
}
}
if (matches(&rec, wantmonth, wantday, wantyear)) {
switch (rec.code) {
case 'B':
add(&rec, birthdayfile);
birthday = 1;
break;
case 'S':
add(&rec, eventfile);
event = 1;
break;
case 'R':
add(&rec, reminderfile);
reminder = 1;
break;
case 'F':
add(&rec, thoughtfile);
thought = 1;
break;
default:
break;
}
}
}
}
}
/* Cols 6-9 (rec->year) are a plain year for B/S records, ignored for F
* records, and -- for R (Reminder) records only -- may instead hold a
* second MMDD marking the end of a date range, per TODAY36.DOC "Data
* File Format" (cols 6-9) and the worked example in core/data/today.own
* (R00270004 "Pay the mortgage!", confirmed against the June 28
* reference screenshot). Two range shapes actually occur in that file:
*
* - Both start and end month wildcarded (0): a day-of-month range
* that recurs every month, e.g. day 27 through day 4 of the next
* month for the mortgage reminder. Wraps when start day > end day.
* - Both start and end month concrete: an ordinary (month, day) range,
* e.g. Sep 17 - Sep 23 for a birthday reminder window. Wraps across
* the year boundary if start > end, though no current data exercises
* that case.
*
* A range with only one end wildcarded doesn't appear anywhere in the
* data and isn't documented, so it deliberately never matches rather
* than guessing at unspecified semantics. */
static int daterangematch(struct line *rec, int wantmonth, int wantday) {
int sm = atoi(rec->month), sd = atoi(rec->day);
if (rec->code == 'R' && rec->year[0] != ' ') {
char emonth[3], eday[3];
int em, ed;
emonth[0] = rec->year[0]; emonth[1] = rec->year[1]; emonth[2] = '\0';
eday[0] = rec->year[2]; eday[1] = rec->year[3]; eday[2] = '\0';
em = atoi(emonth);
ed = atoi(eday);
if ((sm == 0) != (em == 0)) return 0;
if (sm == 0 && em == 0) {
if (sd <= ed) return wantday >= sd && wantday <= ed;
return wantday >= sd || wantday <= ed;
}
{
int qs = sm * 100 + sd, qe = em * 100 + ed, qd = wantmonth * 100 + wantday;
if (qs <= qe) return qd >= qs && qd <= qe;
return qd >= qs || qd <= qe;
}
}
return (sm == wantmonth || sm == 0) && (sd == wantday || sd == 0);
}
/* Sakamoto's algorithm: day of week (0=Sunday..6=Saturday) for a
* Gregorian calendar date. Used only to resolve column 10's day-of-week
* gating (see below) -- our MMDD-only data format has no year of its
* own, so the caller supplies one (see main()'s [MMDD [YYYY]] usage and
* the Makefile's YEAR variable, currently defaulted to 1993, the year
* TODAY 3.6 itself shipped in per its own "11/14/93" banner). */
static int dayofweek(int year, int month, int day) {
static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int y = year;
if (month < 3) y -= 1;
return (y + y / 4 - y / 100 + y / 400 + t[month - 1] + day) % 7;
}
/* Column 10 may hold a day-of-week digit (1=Sunday...7=Saturday, per
* TODAY36.DOC "Data File Format"), restricting an otherwise-matching
* line to just one day a year -- e.g. today.4's seven near-identical
* "Spring Forward" candidates for April 1-7, only one of which (the
* actual first Sunday in wantyear) is meant to display. */
int matches(struct line *rec, int wantmonth, int wantday, int wantyear) {
if (!daterangematch(rec, wantmonth, wantday)) return 0;
if (rec->special >= '1' && rec->special <= '7') {
return dayofweek(wantyear, wantmonth, wantday) + 1 == rec->special - '0';
}
return 1;
}
void add(struct line *rec, FILE *fptr) {
if (rec->special == 'C') {
fprintf(fptr, "\t\t%s\n", rec->message);
} else if (rec->code == 'R') {
/* cols 6-9 hold a range end date here, not a year -- never print it
* (see matches(), above). */
fprintf(fptr, "\t%s\n", rec->message);
} else {
if (rec->year[0] != ' ') {
fprintf(fptr, "\tIn %s %s\n", rec->year, rec->message);
} else {
fprintf(fptr, "\t%s\n", rec->message);
}
}
}
/* Turns a gathered, tab-prefixed buffer (see add(), above) into outbuf
* entries: a yellow section header, then one cyan body line per record,
* splitting out "In YYYY " into its own red run where present. */
void layout(FILE *fp, const char *header) {
char buffer[300];
char line[310];
char *p;
size_t len;
int continuation;
addout('S', NULL, header);
rewind(fp);
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
p = buffer;
continuation = 0;
if (*p == '\t') {
p++;
if (*p == '\t') {
p++;
continuation = 1;
}
}
len = strlen(p);
if (len > 0 && p[len - 1] == '\n') p[len - 1] = '\0';
if (!continuation && strncmp(p, "In ", 3) == 0 &&
isdigit((unsigned char)p[3]) && isdigit((unsigned char)p[4]) &&
isdigit((unsigned char)p[5]) && isdigit((unsigned char)p[6]) &&
p[7] == ' ') {
char year[5];
strncpy(year, p + 3, 4);
year[4] = '\0';
addout('Y', year, p + 8);
} else {
sprintf(line, " %s", p);
addout('B', NULL, line);
}
}
}
void addout(char kind, const char *year, const char *text) {
if (nout >= MAXOUT) return; /* generous cap; silently drop overflow */
outbuf[nout].kind = kind;
if (year) {
strncpy(outbuf[nout].year, year, 4);
outbuf[nout].year[4] = '\0';
} else {
outbuf[nout].year[0] = '\0';
}
strncpy(outbuf[nout].text, text, sizeof(outbuf[nout].text) - 1);
outbuf[nout].text[sizeof(outbuf[nout].text) - 1] = '\0';
nout++;
}
/* The data files are DOS/CP437 text, not UTF-8: bytes 0x80-0xFF are
* accented Latin letters, box-drawing, and a few symbols, in the
* standard IBM PC OEM codepage layout -- not ASCII and not UTF-8, so
* passing them through raw produces mojibake in the browser (found via
* "Boetes", CP437 for "Bo{o-diaeresis}tes", rendering as a replacement
* character before this table was added). */
static const char *cp437_utf8[128] = {
/* 0x80 */ "\xc3\x87",
/* 0x81 */ "\xc3\xbc",
/* 0x82 */ "\xc3\xa9",
/* 0x83 */ "\xc3\xa2",
/* 0x84 */ "\xc3\xa4",
/* 0x85 */ "\xc3\xa0",
/* 0x86 */ "\xc3\xa5",
/* 0x87 */ "\xc3\xa7",
/* 0x88 */ "\xc3\xaa",
/* 0x89 */ "\xc3\xab",
/* 0x8A */ "\xc3\xa8",
/* 0x8B */ "\xc3\xaf",
/* 0x8C */ "\xc3\xae",
/* 0x8D */ "\xc3\xac",
/* 0x8E */ "\xc3\x84",
/* 0x8F */ "\xc3\x85",
/* 0x90 */ "\xc3\x89",
/* 0x91 */ "\xc3\xa6",
/* 0x92 */ "\xc3\x86",
/* 0x93 */ "\xc3\xb4",
/* 0x94 */ "\xc3\xb6",
/* 0x95 */ "\xc3\xb2",
/* 0x96 */ "\xc3\xbb",
/* 0x97 */ "\xc3\xb9",
/* 0x98 */ "\xc3\xbf",
/* 0x99 */ "\xc3\x96",
/* 0x9A */ "\xc3\x9c",
/* 0x9B */ "\xc2\xa2",
/* 0x9C */ "\xc2\xa3",
/* 0x9D */ "\xc2\xa5",
/* 0x9E */ "\xe2\x82\xa7",
/* 0x9F */ "\xc6\x92",
/* 0xA0 */ "\xc3\xa1",
/* 0xA1 */ "\xc3\xad",
/* 0xA2 */ "\xc3\xb3",
/* 0xA3 */ "\xc3\xba",
/* 0xA4 */ "\xc3\xb1",
/* 0xA5 */ "\xc3\x91",
/* 0xA6 */ "\xc2\xaa",
/* 0xA7 */ "\xc2\xba",
/* 0xA8 */ "\xc2\xbf",
/* 0xA9 */ "\xe2\x8c\x90",
/* 0xAA */ "\xc2\xac",
/* 0xAB */ "\xc2\xbd",
/* 0xAC */ "\xc2\xbc",
/* 0xAD */ "\xc2\xa1",
/* 0xAE */ "\xc2\xab",
/* 0xAF */ "\xc2\xbb",
/* 0xB0 */ "\xe2\x96\x91",
/* 0xB1 */ "\xe2\x96\x92",
/* 0xB2 */ "\xe2\x96\x93",
/* 0xB3 */ "\xe2\x94\x82",
/* 0xB4 */ "\xe2\x94\xa4",
/* 0xB5 */ "\xe2\x95\xa1",
/* 0xB6 */ "\xe2\x95\xa2",
/* 0xB7 */ "\xe2\x95\x96",
/* 0xB8 */ "\xe2\x95\x95",
/* 0xB9 */ "\xe2\x95\xa3",
/* 0xBA */ "\xe2\x95\x91",
/* 0xBB */ "\xe2\x95\x97",
/* 0xBC */ "\xe2\x95\x9d",
/* 0xBD */ "\xe2\x95\x9c",
/* 0xBE */ "\xe2\x95\x9b",
/* 0xBF */ "\xe2\x94\x90",
/* 0xC0 */ "\xe2\x94\x94",
/* 0xC1 */ "\xe2\x94\xb4",
/* 0xC2 */ "\xe2\x94\xac",
/* 0xC3 */ "\xe2\x94\x9c",
/* 0xC4 */ "\xe2\x94\x80",
/* 0xC5 */ "\xe2\x94\xbc",
/* 0xC6 */ "\xe2\x95\x9e",
/* 0xC7 */ "\xe2\x95\x9f",
/* 0xC8 */ "\xe2\x95\x9a",
/* 0xC9 */ "\xe2\x95\x94",
/* 0xCA */ "\xe2\x95\xa9",
/* 0xCB */ "\xe2\x95\xa6",
/* 0xCC */ "\xe2\x95\xa0",
/* 0xCD */ "\xe2\x95\x90",
/* 0xCE */ "\xe2\x95\xac",
/* 0xCF */ "\xe2\x95\xa7",
/* 0xD0 */ "\xe2\x95\xa8",
/* 0xD1 */ "\xe2\x95\xa4",
/* 0xD2 */ "\xe2\x95\xa5",
/* 0xD3 */ "\xe2\x95\x99",
/* 0xD4 */ "\xe2\x95\x98",
/* 0xD5 */ "\xe2\x95\x92",
/* 0xD6 */ "\xe2\x95\x93",
/* 0xD7 */ "\xe2\x95\xab",
/* 0xD8 */ "\xe2\x95\xaa",
/* 0xD9 */ "\xe2\x94\x98",
/* 0xDA */ "\xe2\x94\x8c",
/* 0xDB */ "\xe2\x96\x88",
/* 0xDC */ "\xe2\x96\x84",
/* 0xDD */ "\xe2\x96\x8c",
/* 0xDE */ "\xe2\x96\x90",
/* 0xDF */ "\xe2\x96\x80",
/* 0xE0 */ "\xce\xb1",
/* 0xE1 */ "\xc3\x9f",
/* 0xE2 */ "\xce\x93",
/* 0xE3 */ "\xcf\x80",
/* 0xE4 */ "\xce\xa3",
/* 0xE5 */ "\xcf\x83",
/* 0xE6 */ "\xc2\xb5",
/* 0xE7 */ "\xcf\x84",
/* 0xE8 */ "\xce\xa6",
/* 0xE9 */ "\xce\x98",
/* 0xEA */ "\xce\xa9",
/* 0xEB */ "\xce\xb4",
/* 0xEC */ "\xe2\x88\x9e",
/* 0xED */ "\xcf\x86",
/* 0xEE */ "\xce\xb5",
/* 0xEF */ "\xe2\x88\xa9",
/* 0xF0 */ "\xe2\x89\xa1",
/* 0xF1 */ "\xc2\xb1",
/* 0xF2 */ "\xe2\x89\xa5",
/* 0xF3 */ "\xe2\x89\xa4",
/* 0xF4 */ "\xe2\x8c\xa0",
/* 0xF5 */ "\xe2\x8c\xa1",
/* 0xF6 */ "\xc3\xb7",
/* 0xF7 */ "\xe2\x89\x88",
/* 0xF8 */ "\xc2\xb0",
/* 0xF9 */ "\xe2\x88\x99",
/* 0xFA */ "\xc2\xb7",
/* 0xFB */ "\xe2\x88\x9a",
/* 0xFC */ "\xe2\x81\xbf",
/* 0xFD */ "\xc2\xb2",
/* 0xFE */ "\xe2\x96\xa0",
/* 0xFF */ "\xc2\xa0",
};
/* Escapes &, <, > for safe embedding in HTML, and transcodes CP437's
* high 128 characters to UTF-8 -- the data files are decades of
* externally mailed-in trivia, not text this program wrote itself, so
* both of these always run, not just for a hostile-input edge case. */
void htmlescape(const char *in, char *out, size_t outsize) {
size_t used = 0;
const char *rep;
for (; *in != '\0' && used < outsize - 1; in++) {
unsigned char c = (unsigned char)*in;
if (c == '&') rep = "&";
else if (c == '<') rep = "<";
else if (c == '>') rep = ">";
else if (c >= 0x80) rep = cp437_utf8[c - 0x80];
else {
out[used++] = *in;
continue;
}
if (used + strlen(rep) >= outsize - 1) break;
strcpy(out + used, rep);
used += strlen(rep);
}
out[used] = '\0';
}
void printborder(const char *left, const char *right) {
int i;
printf("<span class=\"brd\">%s", left);
for (i = 0; i < COLS; i++) printf("\xe2\x94\x80"); /* U+2500 BOX DRAWINGS LIGHT HORIZONTAL */
printf("%s</span>\n", right);
}
/* Padding is computed from the RAW (pre-htmlescape) text length, not
* strlen(escaped): CP437 is one byte per glyph, so raw length equals
* the visual column count, but an HTML entity like "&" or a
* transcoded UTF-8 sequence like the two bytes for "o with diaeresis"
* takes more source bytes than the single column it renders as --
* padding against the escaped length under-pads any line containing
* one, throwing the right border off by that many columns. */
void printplain(char kind, const char *text) {
char escaped[300];
const char *class;
size_t rawlen = strlen(text);
size_t pad;
htmlescape(text, escaped, sizeof(escaped));
class = (kind == 'H') ? "hdr" : (kind == 'S') ? "sec" : "body";
pad = (rawlen < COLS) ? (COLS - rawlen) : 0;
printf("<span class=\"brd\">\xe2\x94\x82</span>");
if (escaped[0] != '\0')
printf("<span class=\"%s\">%s</span>", class, escaped);
if (pad > 0) printf("<span class=\"body\">%*s</span>", (int)pad, "");
printf("<span class=\"brd\">\xe2\x94\x82</span>\n");
}
void printyearline(const char *year, const char *text) {
char escaped[300];
char prefix[20];
size_t rawlen;
size_t pad;
htmlescape(text, escaped, sizeof(escaped));
sprintf(prefix, " In ");
/* +1 for the literal space between year and text in the format
* string below -- easy to forget since it isn't in any variable. */
rawlen = strlen(prefix) + strlen(year) + 1 + strlen(text);
pad = (rawlen < COLS) ? (COLS - rawlen) : 0;
printf("<span class=\"brd\">\xe2\x94\x82</span>"
"<span class=\"body\">%s</span><span class=\"yr\">%s</span>"
"<span class=\"body\"> %s</span>",
prefix, year, escaped);
if (pad > 0) printf("<span class=\"body\">%*s</span>", (int)pad, "");
printf("<span class=\"brd\">\xe2\x94\x82</span>\n");
}
void printblank(void) {
printf("<span class=\"brd\">\xe2\x94\x82</span><span class=\"body\">%*s</span>"
"<span class=\"brd\">\xe2\x94\x82</span>\n", COLS, "");
}
/* Slices outbuf into 21-row pages, PAGEBUDGET content lines at a time,
* each wrapped in the same border/header/separator frame, with either a
* "Press ENTER for more..." continuation prompt or the closing
* "Enter a new date..." prompt as the last non-blank row. See
* README.md "Pagination" for how PAGEBUDGET (18) was derived from the
* reference screenshots. */
void emitpages(void) {
static const char header[] =
" TODAY Version 3.6 11/14/93 Copyright 1986, 1993 By Patrick Kincaid ";
int i = 0;
int show, k, rows;
do {
int remaining = nout - i;
show = (remaining > PAGEBUDGET) ? PAGEBUDGET : remaining;
printf("<pre class=\"screen\">\n");
printborder("\xe2\x94\x8c", "\xe2\x94\x90"); /* top-left / top-right */
printplain('H', header);
printborder("\xe2\x94\x9c", "\xe2\x94\xa4"); /* left-tee / right-tee */
for (k = 0; k < show; k++) {
struct outline *o = &outbuf[i + k];
if (o->kind == 'Y') printyearline(o->year, o->text);
else printplain(o->kind, o->text);
}
i += show;
rows = show;
if (i < nout) {
printplain('H', " Press ENTER for more...");
} else {
printplain('H', " Enter a new date as MMDD or just <ENTER> to quit:");
}
rows++;
for (; rows < PAGEROWS; rows++) printblank();
printborder("\xe2\x94\x94", "\xe2\x94\x98"); /* bottom-left / bottom-right */
printf("</pre>\n");
} while (i < nout);
}