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
|
/* clectio -- tiny, fast, suckless daily readings.
*
* Computes nothing at runtime: the liturgical calendar, its reading citations
* and the scripture text are all compiled in (see gen/ and the Makefile). For a
* date it does a table lookup and prints the day and its readings. Configuration
* is compile-time (config.h), suckless-style: edit and recompile.
*
* Copyright (C) 2026 Ćukasz Kasprzak. SPDX-License-Identifier: GPL-3.0-or-later
*
* clectio is free software: you may redistribute and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version, and WITHOUT ANY WARRANTY. See the LICENSE file for the full text.
*
* The compiled scripture text is a separate work under its own terms; the
* default Latin Vulgate is public domain (see README).
*/
#define _POSIX_C_SOURCE 200809L /* localtime_r, isatty under -std=c99 */
#define _DEFAULT_SOURCE /* struct winsize / TIOCGWINSZ under -std=c99 */
#include "config.h"
#include LITURGY /* gen/liturgy_<form>.h: names,parts,cites_*,vpool,readings,rpool,days,cal,EPOCH,NDAYS */
#include "text.lz.h" /* embedded DEFLATE text blob: text_lz[], text_lz_len */
#include "puff.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
static char *textbuf; /* inflated verse text */
static const char **verse; /* verse[i] -> the text of the i-th key, nul-terminated */
static size_t nverse; /* how many the packed text actually holds */
static int usecolor;
static const char *const colname[] = { "green", "white", "red", "violet", "rose", "black" };
static const char *const colansi[] = { "\033[32m", "\033[37m", "\033[31m", "\033[35m", "\033[95m", "\033[90m" };
/* Local calendar overrides: replace one fixed date's name + colour. The colour
* names below match colname[]/colansi[] above (index order). The user table
* lives in calendar.h (copied from calendar.def.h); see it for the limits. */
enum { GREEN, WHITE, RED, VIOLET, ROSE, BLACK };
typedef struct { int mon, day, colour; const char *name; } Override;
#include "calendar.h" /* static const Override overrides[]; ends with a {0,...} row */
static const Override *find_override(int mon, int day) {
const Override *o;
for (o = overrides; o->mon; o++)
if (o->mon == mon && o->day == day)
return o;
return NULL;
}
/* Days since the civil epoch 1970-01-01 (Howard Hinnant's algorithm). */
static long days_from_civil(long y, unsigned m, unsigned d) {
y -= m <= 2;
long era = (y >= 0 ? y : y - 399) / 400;
unsigned yoe = (unsigned)(y - era * 400);
unsigned doy = (153u * (m + (m > 2 ? -3u : 9u)) + 2u) / 5u + d - 1;
unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
return era * 146097 + (long)doe - 719468;
}
static void load_text(void) {
unsigned long rawlen = text_lz[0] | text_lz[1] << 8 | text_lz[2] << 16 | (unsigned long)text_lz[3] << 24;
unsigned long dstlen = rawlen, srclen = text_lz_len - 4;
size_t cap = 8192, n = 0;
char *p;
textbuf = malloc(rawlen + 1);
puff((unsigned char *)textbuf, &dstlen, text_lz + 4, &srclen);
textbuf[dstlen] = 0;
verse = malloc(cap * sizeof(*verse));
verse[n++] = textbuf;
for (p = textbuf; *p; p++)
if (*p == '\n') {
*p = 0;
if (p[1]) {
if (n == cap) { cap *= 2; verse = realloc(verse, cap * sizeof(*verse)); }
verse[n++] = p + 1;
}
}
nverse = n;
}
static const char *citation(int r) {
#ifdef SIGLA_LATIN
return cites_la[readings_cite_la[r]];
#else
return cites_en[readings[r].cite];
#endif
}
/* Column width of the terminal on stdout, or 72 if it cannot be determined. */
static int termwidth(void) {
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0)
return ws.ws_col;
return 72;
}
/* Print one verse: a leading "chap:verse" number in a fixed left column (VNUM
* wide), then the text wrapped at w columns, breaking on spaces (greedy), with
* continuation lines hang-indented under the text so its left edge stays
* straight. w <= 0 prints the text on one line after the number column. UTF-8
* aware: continuation bytes (0x80-0xBF) do not count as a column, so multibyte
* letters measure as one. A verse that does not begin with a digit gets no
* number column (indent 0) and wraps flush-left, as before. */
static void putwrapped(const char *s, int w) {
const char *text = s;
int indent = 0, first = 1;
if (*s >= '0' && *s <= '9') { /* peel the leading "chap:verse " token */
const char *sp = strchr(s, ' ');
if (sp) {
int numlen = (int)(sp - s), pad = VNUM - numlen;
fwrite(s, 1, (size_t)numlen, stdout);
if (pad < 1)
pad = 1; /* always a space, even after a long number */
while (pad-- > 0)
putchar(' ');
indent = VNUM;
text = sp + 1;
}
}
if (w <= 0) {
printf("%s\n", text);
return;
}
while (*text) {
const char *p = text, *brk = NULL;
int col = indent, k;
if (!first) /* hang-indent continuation lines under the text */
for (k = 0; k < indent; k++)
putchar(' ');
while (*p && col < w) {
if (*p == ' ')
brk = p;
if ((*p & 0xC0) != 0x80)
col++;
p++;
}
if (!*p) { /* the rest fits on this line */
printf("%s\n", text);
return;
}
if (!brk) /* a single word longer than the line: hard-break */
brk = p;
fwrite(text, 1, (size_t)(brk - text), stdout);
putchar('\n');
for (text = brk; *text == ' '; text++) /* skip the break space(s) */
;
first = 0;
}
}
/* One line per day of month `mon` in year `y`: weekday, day, colour, celebration.
* Needs no scripture text, so it never touches the text blob. Returns an exit
* code (1 if the month falls outside the compiled range). */
static int print_month(int y, int mon) {
static const char *const wd[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
long base = days_from_civil(EPOCH_Y, EPOCH_M, EPOCH_D);
long civ1 = days_from_civil(y, (unsigned)mon, 1);
long nextfirst = days_from_civil(mon == 12 ? y + 1 : y, (unsigned)(mon == 12 ? 1 : mon + 1), 1);
int dim = (int)(nextfirst - civ1); /* days in the month (leap-correct) */
long off1 = civ1 - base;
int day, n;
if (off1 < 0 || off1 + dim - 1 >= NDAYS) {
fprintf(stderr, "clectio: %04d-%02d is outside the compiled range %d..%d\n",
y, mon, EPOCH_Y, EPOCH_Y + (NDAYS / 366));
return 1;
}
n = printf("%04d-%02d %s\n", y, mon, FORMNAME) - 1; /* underline that line */
while (n-- > 0)
putchar('=');
putchar('\n');
for (day = 1; day <= dim; day++) {
long civ = civ1 + (day - 1);
const Day *d = &days[cal[off1 + (day - 1)]];
const Override *ov = find_override(mon, day);
const char *dname = ov ? ov->name : names[d->name];
int dcol = ov ? ov->colour : d->colour;
int w = (int)((civ + 4) % 7); /* 1970-01-01 was Thursday; 0 = Sunday */
printf("%s %02d %-6s ", wd[w], day, colname[dcol]);
if (usecolor)
printf("%s%s\033[0m\n", colansi[dcol], dname);
else
printf("%s\n", dname);
}
return 0;
}
/* Parse a --month spec ("YYYY-MM", or "N" for month N of the current year, or
* NULL for the current month) and print it. Returns an exit code. */
static int run_month(const char *spec) {
struct tm tm;
time_t t = time(NULL);
int y, mon;
localtime_r(&t, &tm);
y = tm.tm_year + 1900;
mon = tm.tm_mon + 1;
if (spec) {
int ok = strchr(spec, '-') ? sscanf(spec, "%d-%d", &y, &mon) == 2
: sscanf(spec, "%d", &mon) == 1;
if (!ok) {
fprintf(stderr, "clectio: bad month %s (want YYYY-MM or 1-12)\n", spec);
return 2;
}
}
if (mon < 1 || mon > 12) {
fprintf(stderr, "clectio: bad month %s (want YYYY-MM or 1-12)\n", spec ? spec : "");
return 2;
}
return print_month(y, mon);
}
static void print_day(long off, const char *datestr, int mon, int day, int gospel, int wrap) {
const Day *d = &days[cal[off]];
const Override *ov = find_override(mon, day);
const char *dname = ov ? ov->name : names[d->name];
int dcol = ov ? ov->colour : d->colour;
unsigned ri;
int n;
if (usecolor)
printf("%s%s\033[0m\n", colansi[dcol], dname);
else
printf("%s \xc2\xb7 %s\n", dname, colname[dcol]);
n = printf("%s for %s\n", FORMNAME, datestr) - 1; /* underline that line */
while (n-- > 0)
putchar('=');
putchar('\n');
for (ri = 0; ri < d->rlen; ri++) {
const Reading *r = &readings[rpool[d->roff + ri]];
unsigned v;
if (gospel && strcmp(parts[r->part], "Gospel") != 0)
continue;
printf("\n%s (%s)\n\n", parts[r->part], citation(rpool[d->roff + ri]));
for (v = 0; v < r->vlen; v++) {
/* gen/liturgy_<form>.h and the packed text are generated
* together but shipped apart, and the Makefile unpacks the
* verse keys from the COMMITTED gen/verses_<form>.keys.gz --
* so regenerating the tables without also refreshing that
* .gz leaves the tables indexing verses the text does not
* hold. That shipped once: 1246 of the 10957 EF dates
* segfaulted here on a NULL verse. Missing text is now a
* visible gap, never a crash. */
if (vpool[r->voff + v] >= nverse) {
fputs(" [verse text missing]\n", stdout);
continue;
}
putwrapped(verse[vpool[r->voff + v]], wrap);
}
}
}
int main(int argc, char **argv) {
struct tm tm;
long off;
char datestr[16];
int y, m, dd, i;
int gospel = GOSPEL_ONLY; /* compiled default, overridable per run */
int istty = isatty(1);
int wrap, monthmode = 0;
const char *datearg = NULL;
usecolor = COLOR && istty;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
fprintf(stderr,
"usage: clectio [-a|--all | -g|--gospel] [YYYY-MM-DD]\n"
" clectio --month [YYYY-MM | N]\n"
" -a, --all print all readings\n"
" -g, --gospel print the Gospel only\n"
" --month list a whole month (default: current month)\n"
" (default: today, %s)\n"
" compiled: %s, %s, wrap %s, range %d-%d\n",
READMODE, FORMNAME, SIGLANAME, WRAPNAME, EPOCH_Y, EPOCH_Y + (NDAYS / 366));
return 0;
} else if (!strcmp(argv[i], "-a") || !strcmp(argv[i], "--all")) {
gospel = 0;
} else if (!strcmp(argv[i], "-g") || !strcmp(argv[i], "--gospel")) {
gospel = 1;
} else if (!strcmp(argv[i], "--month")) {
monthmode = 1;
} else if (argv[i][0] == '-' && argv[i][1]) {
fprintf(stderr, "clectio: unknown option %s (try -h)\n", argv[i]);
return 2;
} else if (!datearg) {
datearg = argv[i];
} else {
fprintf(stderr, "clectio: unexpected argument %s (try -h)\n", argv[i]);
return 2;
}
}
if (monthmode)
return run_month(datearg); /* month overview: no readings, no text loaded */
if (datearg) {
if (sscanf(datearg, "%d-%d-%d", &y, &m, &dd) != 3) {
fprintf(stderr, "clectio: bad date %s (want YYYY-MM-DD)\n", datearg);
return 2;
}
snprintf(datestr, sizeof datestr, "%04d-%02d-%02d", y, m, dd);
} else {
time_t t = time(NULL);
localtime_r(&t, &tm);
y = tm.tm_year + 1900;
m = tm.tm_mon + 1;
dd = tm.tm_mday;
strftime(datestr, sizeof datestr, "%Y-%m-%d", &tm);
}
off = days_from_civil(y, (unsigned)m, (unsigned)dd) -
days_from_civil(EPOCH_Y, EPOCH_M, EPOCH_D);
if (off < 0 || off >= NDAYS) {
fprintf(stderr, "clectio: %s is outside the compiled range %d..%d\n",
datestr, EPOCH_Y, EPOCH_Y + (NDAYS / 366));
return 1;
}
/* WRAP>0: always wrap to that width. WRAP==0: fit the terminal, but leave
* piped output as one line per verse (0 -> putwrapped prints unwrapped). */
if (WRAP > 0)
wrap = WRAP;
else
wrap = istty ? termwidth() : 0;
load_text();
print_day(off, datestr, m, dd, gospel, wrap);
return 0;
}
|