main.c 16.8 KB
Newer Older
1
#define _XOPEN_SOURCE 600
Drew DeVault's avatar
Drew DeVault committed
2
3
#include <assert.h>
#include <ctype.h>
4
5
#include <errno.h>
#include <limits.h>
Drew DeVault's avatar
Drew DeVault committed
6
#include <stdbool.h>
Drew DeVault's avatar
Drew DeVault committed
7
8
#include <stdio.h>
#include <stdlib.h>
Drew DeVault's avatar
Drew DeVault committed
9
#include <string.h>
Drew DeVault's avatar
Drew DeVault committed
10
11
#include <time.h>
#include <unistd.h>
Drew DeVault's avatar
Drew DeVault committed
12
#include "str.h"
Drew DeVault's avatar
Drew DeVault committed
13
14
15
#include "unicode.h"
#include "util.h"

Drew DeVault's avatar
Drew DeVault committed
16
char *strstr(const char *haystack, const char *needle);
17
char *strerror(int errnum);
Drew DeVault's avatar
Drew DeVault committed
18

19
static struct str *parse_section(struct parser *p) {
Drew DeVault's avatar
Drew DeVault committed
20
	struct str *section = str_create();
Drew DeVault's avatar
Drew DeVault committed
21
	uint32_t ch;
22
	char *subsection;
Drew DeVault's avatar
Drew DeVault committed
23
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
24
		if (ch < 0x80 && isalnum(ch)) {
25
26
			int ret = str_append_ch(section, ch);
			assert(ret != -1);
Drew DeVault's avatar
Drew DeVault committed
27
		} else if (ch == ')') {
28
			if (section->len == 0) {
Drew DeVault's avatar
Drew DeVault committed
29
30
				break;
			}
31
32
33
34
35
			int sec = strtol(section->str, &subsection, 10);
			if (section->str == subsection) {
				parser_fatal(p, "Expected section digit");
				break;
			}
Drew DeVault's avatar
Drew DeVault committed
36
37
			if (sec < 0 || sec > 9) {
				parser_fatal(p, "Expected section between 0 and 9");
Drew DeVault's avatar
Drew DeVault committed
38
39
				break;
			}
40
			return section;
Drew DeVault's avatar
Drew DeVault committed
41
		} else {
42
			parser_fatal(p, "Expected alphanumerical character or )");
Drew DeVault's avatar
Drew DeVault committed
43
44
45
46
			break;
		}
	};
	parser_fatal(p, "Expected manual section");
47
	return NULL;
Drew DeVault's avatar
Drew DeVault committed
48
49
}

Drew DeVault's avatar
Drew DeVault committed
50
51
static struct str *parse_extra(struct parser *p) {
	struct str *extra = str_create();
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	int ret = str_append_ch(extra, '"');
	assert(ret != -1);
	uint32_t ch;
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
		if (ch == '"') {
			ret = str_append_ch(extra, ch);
			assert(ret != -1);
			return extra;
		} else if (ch == '\n') {
			parser_fatal(p, "Unclosed extra preamble field");
			break;
		} else {
			ret = str_append_ch(extra, ch);
			assert(ret != -1);
		}
	}
68
	str_free(extra);
69
70
71
	return NULL;
}

Drew DeVault's avatar
Drew DeVault committed
72
static void parse_preamble(struct parser *p) {
Drew DeVault's avatar
Drew DeVault committed
73
	struct str *name = str_create();
74
	int ex = 0;
Drew DeVault's avatar
Drew DeVault committed
75
	struct str *extras[2] = { NULL };
76
	struct str *section = NULL;
Drew DeVault's avatar
Drew DeVault committed
77
	uint32_t ch;
78
	time_t date_time;
Drew DeVault's avatar
Drew DeVault committed
79
	char date[256];
80
81
	char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
	if (source_date_epoch != NULL) {
82
83
84
85
86
87
88
89
90
		unsigned long long epoch;
		char *endptr;
		errno = 0;
		epoch = strtoull(source_date_epoch, &endptr, 10);
		if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
				|| (errno != 0 && epoch == 0)) {
			fprintf(stderr, "$SOURCE_DATE_EPOCH: strtoull: %s\n",
					strerror(errno));
			exit(EXIT_FAILURE);
91
		}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
		if (endptr == source_date_epoch) {
			fprintf(stderr, "$SOURCE_DATE_EPOCH: No digits were found: %s\n",
					endptr);
			exit(EXIT_FAILURE);
		}
		if (*endptr != '\0') {
			fprintf(stderr, "$SOURCE_DATE_EPOCH: Trailing garbage: %s\n",
					endptr);
			exit(EXIT_FAILURE);
		}
		if (epoch > ULONG_MAX) {
			fprintf(stderr, "$SOURCE_DATE_EPOCH: value must be smaller than or "
					"equal to %lu but was found to be: %llu \n",
					ULONG_MAX, epoch);
			exit(EXIT_FAILURE);
		}
		date_time = epoch;
109
	} else {
110
		date_time = time(NULL);
111
	}
Birger Schacht's avatar
Birger Schacht committed
112
	struct tm *date_tm = gmtime(&date_time);
113
	strftime(date, sizeof(date), "%F", date_tm);
Drew DeVault's avatar
Drew DeVault committed
114
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
115
		if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') {
116
117
			int ret = str_append_ch(name, ch);
			assert(ret != -1);
Drew DeVault's avatar
Drew DeVault committed
118
119
		} else if (ch == '(') {
			section = parse_section(p);
120
121
122
123
124
		} else if (ch == '"') {
			if (ex == 2) {
				parser_fatal(p, "Too many extra preamble fields");
			}
			extras[ex++] = parse_extra(p);
Drew DeVault's avatar
Drew DeVault committed
125
126
127
128
		} else if (ch == '\n') {
			if (name->len == 0) {
				parser_fatal(p, "Expected preamble");
			}
129
			if (section == NULL) {
Drew DeVault's avatar
Drew DeVault committed
130
131
				parser_fatal(p, "Expected manual section");
			}
132
133
			char *ex2 = extras[0] != NULL ? extras[0]->str : NULL;
			char *ex3 = extras[1] != NULL ? extras[1]->str : NULL;
134
			fprintf(p->output, ".TH \"%s\" \"%s\" \"%s\"", name->str, section->str, date);
135
136
137
138
139
140
141
142
			/* ex2 and ex3 are already double-quoted */
			if (ex2) {
				fprintf(p->output, " %s", ex2);
			}
			if (ex3) {
				fprintf(p->output, " %s", ex3);
			}
			fprintf(p->output, "\n");
Drew DeVault's avatar
Drew DeVault committed
143
			break;
144
		} else if (section == NULL) {
145
			parser_fatal(p, "Name characters must be A-Z, a-z, 0-9, `-`, `_`, or `.`");
Drew DeVault's avatar
Drew DeVault committed
146
		}
Drew DeVault's avatar
Drew DeVault committed
147
	}
Drew DeVault's avatar
Drew DeVault committed
148
	str_free(name);
149
150
151
152
153
	for (int i = 0; i < 2; ++i) {
		if (extras[i] != NULL) {
			str_free(extras[i]);
		}
	}
Drew DeVault's avatar
Drew DeVault committed
154
155
}

Drew DeVault's avatar
Drew DeVault committed
156
157
158
159
160
static void parse_format(struct parser *p, enum formatting fmt) {
	char formats[FORMAT_LAST] = {
		[FORMAT_BOLD] = 'B',
		[FORMAT_UNDERLINE] = 'I',
	};
161
	char error[512];
Drew DeVault's avatar
Drew DeVault committed
162
163
	if (p->flags) {
		if ((p->flags & ~fmt)) {
164
165
166
167
168
			snprintf(error, sizeof(error), "Cannot nest inline formatting "
						"(began with %c at %d:%d)",
					p->flags == FORMAT_BOLD ? '*' : '_',
					p->fmt_line, p->fmt_col);
			parser_fatal(p, error);
Drew DeVault's avatar
Drew DeVault committed
169
170
171
172
		}
		fprintf(p->output, "\\fR");
	} else {
		fprintf(p->output, "\\f%c", formats[fmt]);
173
174
		p->fmt_line = p->line;
		p->fmt_col = p->col;
Drew DeVault's avatar
Drew DeVault committed
175
176
177
178
	}
	p->flags ^= fmt;
}

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
static void parse_linebreak(struct parser *p) {
	uint32_t plus = parser_getch(p);
	if (plus != '+') {
		fprintf(p->output, "+");
		parser_pushch(p, plus);
		return;
	}
	uint32_t lf = parser_getch(p);
	if (lf != '\n') {
		fprintf(p->output, "+");
		parser_pushch(p, plus);
		parser_pushch(p, '\n');
		return;
	}
	uint32_t ch = parser_getch(p);
	if (ch == '\n') {
		parser_fatal(
				p, "Explicit line breaks cannot be followed by a blank line");
	}
	parser_pushch(p, ch);
	fprintf(p->output, "\n.br\n");
}

Drew DeVault's avatar
Drew DeVault committed
202
static void parse_text(struct parser *p) {
203
	uint32_t ch, next, last = ' ';
Drew DeVault's avatar
Drew DeVault committed
204
	int i = 0;
Drew DeVault's avatar
Drew DeVault committed
205
206
207
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
		switch (ch) {
		case '\\':
Drew DeVault's avatar
Drew DeVault committed
208
209
210
211
212
213
214
215
			ch = parser_getch(p);
			if (ch == UTF8_INVALID) {
				parser_fatal(p, "Unexpected EOF");
			} else if (ch == '\\') {
				fprintf(p->output, "\\\\");
			} else {
				utf8_fputch(p->output, ch);
			}
Drew DeVault's avatar
Drew DeVault committed
216
			break;
Drew DeVault's avatar
Drew DeVault committed
217
218
219
220
		case '*':
			parse_format(p, FORMAT_BOLD);
			break;
		case '_':
221
222
			next = parser_getch(p);
			if (!isalnum(last) || ((p->flags & FORMAT_UNDERLINE) && !isalnum(next))) {
223
224
225
226
				parse_format(p, FORMAT_UNDERLINE);
			} else {
				utf8_fputch(p->output, ch);
			}
227
228
229
			if (next == UTF8_INVALID) {
				return;
			}
230
			parser_pushch(p, next);
Drew DeVault's avatar
Drew DeVault committed
231
			break;
232
233
234
		case '+':
			parse_linebreak(p);
			break;
Drew DeVault's avatar
Drew DeVault committed
235
236
237
		case '\n':
			utf8_fputch(p->output, ch);
			return;
Drew DeVault's avatar
Drew DeVault committed
238
239
240
241
242
243
244
		case '.':
			if (!i) {
				// Escape . if it's the first character
				fprintf(p->output, "\\&.");
				break;
			}
			/* fallthrough */
Drew DeVault's avatar
Drew DeVault committed
245
		default:
246
			last = ch;
Drew DeVault's avatar
Drew DeVault committed
247
248
249
			utf8_fputch(p->output, ch);
			break;
		}
Drew DeVault's avatar
Drew DeVault committed
250
		++i;
Drew DeVault's avatar
Drew DeVault committed
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
	}
}

static void parse_heading(struct parser *p) {
	uint32_t ch;
	int level = 1;
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
		if (ch == '#') {
			++level;
		} else if (ch == ' ') {
			break;
		} else {
			parser_fatal(p, "Invalid start of heading (probably needs a space)");
		}
	}
	switch (level) {
	case 1:
		fprintf(p->output, ".SH ");
		break;
	case 2:
		fprintf(p->output, ".SS ");
		break;
	default:
		parser_fatal(p, "Only headings up to two levels deep are permitted");
		break;
	}
	while ((ch = parser_getch(p)) != UTF8_INVALID) {
		utf8_fputch(p->output, ch);
		if (ch == '\n') {
			break;
		}
	}
}

Drew DeVault's avatar
Drew DeVault committed
285
static int parse_indent(struct parser *p, int *indent, bool write) {
Drew DeVault's avatar
Drew DeVault committed
286
287
288
289
290
291
	int i = 0;
	uint32_t ch;
	while ((ch = parser_getch(p)) == '\t') {
		++i;
	}
	parser_pushch(p, ch);
Zandr Martin's avatar
Zandr Martin committed
292
293
	if ((ch == '\n' || ch == UTF8_INVALID) && *indent != 0) {
		// Don't change indent when we encounter empty lines or EOF
Drew DeVault's avatar
Drew DeVault committed
294
295
		return *indent;
	}
Drew DeVault's avatar
Drew DeVault committed
296
	if (write) {
Zandr Martin's avatar
Zandr Martin committed
297
298
299
		if ((i - *indent) > 1) {
			parser_fatal(p, "Indented by an amount greater than 1");
		} else if (i < *indent) {
Drew DeVault's avatar
Drew DeVault committed
300
301
302
			for (int j = *indent; i < j; --j) {
				roff_macro(p, "RE", NULL);
			}
Drew DeVault's avatar
Drew DeVault committed
303
		} else if (i == *indent + 1) {
304
			fprintf(p->output, ".RS 4\n");
Drew DeVault's avatar
Drew DeVault committed
305
		}
Drew DeVault's avatar
Drew DeVault committed
306
	}
Drew DeVault's avatar
Drew DeVault committed
307
	*indent = i;
Drew DeVault's avatar
Drew DeVault committed
308
309
310
	return i;
}

Drew DeVault's avatar
Drew DeVault committed
311
static void list_header(struct parser *p, int *num) {
312
	fprintf(p->output, ".RS 4\n");
Drew DeVault's avatar
Drew DeVault committed
313
	fprintf(p->output, ".ie n \\{\\\n");
Drew DeVault's avatar
Drew DeVault committed
314
315
316
317
318
319
320
	if (*num == -1) {
		fprintf(p->output, "\\h'-0%d'%s\\h'+03'\\c\n",
				*num >= 10 ? 5 : 4, "\\(bu");
	} else {
		fprintf(p->output, "\\h'-0%d'%d.\\h'+03'\\c\n",
				*num >= 10 ? 5 : 4, *num);
	}
Drew DeVault's avatar
Drew DeVault committed
321
322
	fprintf(p->output, ".\\}\n");
	fprintf(p->output, ".el \\{\\\n");
Drew DeVault's avatar
Drew DeVault committed
323
324
325
326
327
328
	if (*num == -1) {
		fprintf(p->output, ".IP %s 4\n", "\\(bu");
	} else {
		fprintf(p->output, ".IP %d. 4\n", *num);
		*num = *num + 1;
	}
Drew DeVault's avatar
Drew DeVault committed
329
330
331
	fprintf(p->output, ".\\}\n");
}

Drew DeVault's avatar
Drew DeVault committed
332
static void parse_list(struct parser *p, int *indent, int num) {
Drew DeVault's avatar
Drew DeVault committed
333
	uint32_t ch;
Drew DeVault's avatar
Drew DeVault committed
334
335
336
	if ((ch = parser_getch(p)) != ' ') {
		parser_fatal(p, "Expected space before start of list entry");
	}
Drew DeVault's avatar
Drew DeVault committed
337
	list_header(p, &num);
Drew DeVault's avatar
Drew DeVault committed
338
	parse_text(p);
Drew DeVault's avatar
Drew DeVault committed
339
	do {
Drew DeVault's avatar
Drew DeVault committed
340
		parse_indent(p, indent, true);
Drew DeVault's avatar
Drew DeVault committed
341
		if ((ch = parser_getch(p)) == UTF8_INVALID) {
Drew DeVault's avatar
Drew DeVault committed
342
343
			break;
		}
Drew DeVault's avatar
Drew DeVault committed
344
345
		switch (ch) {
		case ' ':
Drew DeVault's avatar
Drew DeVault committed
346
347
348
349
			if ((ch = parser_getch(p)) != ' ') {
				parser_fatal(p, "Expected two spaces for list entry continuation");
			}
			parse_text(p);
Drew DeVault's avatar
Drew DeVault committed
350
351
			break;
		case '-':
Drew DeVault's avatar
Drew DeVault committed
352
		case '.':
Drew DeVault's avatar
Drew DeVault committed
353
354
355
			if ((ch = parser_getch(p)) != ' ') {
				parser_fatal(p, "Expected space before start of list entry");
			}
356
			roff_macro(p, "RE", NULL);
Drew DeVault's avatar
Drew DeVault committed
357
			list_header(p, &num);
Drew DeVault's avatar
Drew DeVault committed
358
			parse_text(p);
Drew DeVault's avatar
Drew DeVault committed
359
360
361
362
			break;
		default:
			fprintf(p->output, "\n");
			parser_pushch(p, ch);
Drew DeVault's avatar
Drew DeVault committed
363
			goto ret;
Drew DeVault's avatar
Drew DeVault committed
364
		}
Drew DeVault's avatar
Drew DeVault committed
365
	} while (ch != UTF8_INVALID);
Drew DeVault's avatar
Drew DeVault committed
366
ret:
367
	roff_macro(p, "RE", NULL);
Drew DeVault's avatar
Drew DeVault committed
368
}
Drew DeVault's avatar
Drew DeVault committed
369

Drew DeVault's avatar
Drew DeVault committed
370
371
372
373
374
375
376
377
378
static void parse_literal(struct parser *p, int *indent) {
	uint32_t ch;
	if ((ch = parser_getch(p)) != '`' ||
		(ch = parser_getch(p)) != '`' ||
		(ch = parser_getch(p)) != '\n') {
		parser_fatal(p, "Expected ``` and a newline to begin literal block");
	}
	int stops = 0;
	roff_macro(p, "nf", NULL);
379
	fprintf(p->output, ".RS 4\n");
380
	bool check_indent = true;
Drew DeVault's avatar
Drew DeVault committed
381
	do {
382
383
384
385
386
387
388
389
390
391
392
		if (check_indent) {
			int _indent = *indent;
			parse_indent(p, &_indent, false);
			if (_indent < *indent) {
				parser_fatal(p, "Cannot deindent in literal block");
			}
			while (_indent > *indent) {
				--_indent;
				fprintf(p->output, "\t");
			}
			check_indent = false;
Drew DeVault's avatar
Drew DeVault committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
		}
		if ((ch = parser_getch(p)) == UTF8_INVALID) {
			break;
		}
		if (ch == '`') {
			if (++stops == 3) {
				if ((ch = parser_getch(p)) != '\n') {
					parser_fatal(p, "Expected literal block to end with newline");
				}
				roff_macro(p, "fi", NULL);
				roff_macro(p, "RE", NULL);
				return;
			}
		} else {
407
408
409
410
			while (stops != 0) {
				fputc('`', p->output);
				--stops;
			}
Drew DeVault's avatar
Drew DeVault committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
			switch (ch) {
			case '.':
				fprintf(p->output, "\\&.");
				break;
			case '\\':
				ch = parser_getch(p);
				if (ch == UTF8_INVALID) {
					parser_fatal(p, "Unexpected EOF");
				} else if (ch == '\\') {
					fprintf(p->output, "\\\\");
				} else {
					utf8_fputch(p->output, ch);
				}
				break;
425
426
427
			case '\n':
				check_indent = true;
				/* fallthrough */
Drew DeVault's avatar
Drew DeVault committed
428
429
430
431
432
433
434
435
			default:
				utf8_fputch(p->output, ch);
				break;
			}
		}
	} while (ch != UTF8_INVALID);
}

Drew DeVault's avatar
Drew DeVault committed
436
437
438
439
enum table_align {
	ALIGN_LEFT,
	ALIGN_CENTER,
	ALIGN_RIGHT,
Drew DeVault's avatar
Drew DeVault committed
440
441
442
	ALIGN_LEFT_EXPAND,
	ALIGN_CENTER_EXPAND,
	ALIGN_RIGHT_EXPAND,
Drew DeVault's avatar
Drew DeVault committed
443
444
445
446
447
448
449
450
451
};

struct table_row {
	struct table_cell *cell;
	struct table_row *next;
};

struct table_cell {
	enum table_align align;
Drew DeVault's avatar
Drew DeVault committed
452
	struct str *contents;
Drew DeVault's avatar
Drew DeVault committed
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
	struct table_cell *next;
};

static void parse_table(struct parser *p, uint32_t style) {
	struct table_row *table = NULL;
	struct table_row *currow = NULL, *prevrow = NULL;
	struct table_cell *curcell = NULL;
	int column = 0;
	uint32_t ch;
	parser_pushch(p, '|');

	do {
		if ((ch = parser_getch(p)) == UTF8_INVALID) {
			break;
		}
		switch (ch) {
		case '\n':
			goto commit_table;
		case '|':
			prevrow = currow;
			currow = calloc(1, sizeof(struct table_row));
			if (prevrow) {
				// TODO: Verify the number of columns match
				prevrow->next = currow;
			}
			curcell = calloc(1, sizeof(struct table_cell));
			currow->cell = curcell;
			column = 0;
			if (!table) {
				table = currow;
			}
			break;
		case ':':
			if (!currow) {
				parser_fatal(p, "Cannot start a column without "
						"starting a row first");
			} else {
				struct table_cell *prev = curcell;
				curcell = calloc(1, sizeof(struct table_cell));
				if (prev) {
					prev->next = curcell;
				}
				++column;
			}
			break;
498
499
		case ' ':
			goto continue_cell;
Drew DeVault's avatar
Drew DeVault committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
		default:
			parser_fatal(p, "Expected either '|' or ':'");
			break;
		}
		if ((ch = parser_getch(p)) == UTF8_INVALID) {
			break;
		}
		switch (ch) {
		case '[':
			curcell->align = ALIGN_LEFT;
			break;
		case '-':
			curcell->align = ALIGN_CENTER;
			break;
		case ']':
			curcell->align = ALIGN_RIGHT;
			break;
Drew DeVault's avatar
Drew DeVault committed
517
518
519
520
521
522
523
524
525
		case '<':
			curcell->align = ALIGN_LEFT_EXPAND;
			break;
		case '=':
			curcell->align = ALIGN_CENTER_EXPAND;
			break;
		case '>':
			curcell->align = ALIGN_RIGHT_EXPAND;
			break;
Drew DeVault's avatar
Drew DeVault committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
		case ' ':
			if (prevrow) {
				struct table_cell *pcell = prevrow->cell;
				for (int i = 0; i <= column && pcell; ++i, pcell = pcell->next) {
					if (i == column) {
						curcell->align = pcell->align;
						break;
					}
				}
			} else {
				parser_fatal(p, "No previous row to infer alignment from");
			}
			break;
		default:
			parser_fatal(p, "Expected one of '[', '-', ']', or ' '");
			break;
		}
		curcell->contents = str_create();
544
continue_cell:
Jakub Kądziołka's avatar
Jakub Kądziołka committed
545
546
547
548
549
550
551
552
553
554
555
556
		switch (ch = parser_getch(p)) {
		case ' ':
			// Read out remainder of the text
			while ((ch = parser_getch(p)) != UTF8_INVALID) {
				switch (ch) {
				case '\n':
					goto commit_cell;
				default:;
					int ret = str_append_ch(curcell->contents, ch);
					assert(ret != -1);
					break;
				}
Drew DeVault's avatar
Drew DeVault committed
557
			}
Jakub Kądziołka's avatar
Jakub Kądziołka committed
558
559
560
561
562
563
			break;
		case '\n':
			goto commit_cell;
		default:
			parser_fatal(p, "Expected ' ' or a newline");
			break;
Drew DeVault's avatar
Drew DeVault committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
		}
commit_cell:
		if (strstr(curcell->contents->str, "T{")
				|| strstr(curcell->contents->str, "T}")) {
			parser_fatal(p, "Cells cannot contain T{ or T} "
					"due to roff limitations");
		}
	} while (ch != UTF8_INVALID);
commit_table:

	if (ch == UTF8_INVALID) {
		return;
	}

	roff_macro(p, "TS", NULL);

	switch (style) {
	case '[':
Ivan Tham's avatar
Ivan Tham committed
582
		fprintf(p->output, "allbox;");
Drew DeVault's avatar
Drew DeVault committed
583
584
		break;
	case ']':
Ivan Tham's avatar
Ivan Tham committed
585
		fprintf(p->output, "box;");
Drew DeVault's avatar
Drew DeVault committed
586
587
588
589
590
591
592
593
		break;
	}

	// Print alignments first
	currow = table;
	while (currow) {
		curcell = currow->cell;
		while (curcell) {
Drew DeVault's avatar
Drew DeVault committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
			char *align = "";
			switch (curcell->align) {
			case ALIGN_LEFT:
				align = "l";
				break;
			case ALIGN_CENTER:
				align = "c";
				break;
			case ALIGN_RIGHT:
				align = "r";
				break;
			case ALIGN_LEFT_EXPAND:
				align = "lx";
				break;
			case ALIGN_CENTER_EXPAND:
				align = "cx";
				break;
			case ALIGN_RIGHT_EXPAND:
				align = "rx";
				break;
			}
			fprintf(p->output, "%s%s", align, curcell->next ? " " : "");
Drew DeVault's avatar
Drew DeVault committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
			curcell = curcell->next;
		}
		fprintf(p->output, "%s\n", currow->next ? "" : ".");
		currow = currow->next;
	}

	// Then contents
	currow = table;
	while (currow) {
		curcell = currow->cell;
		fprintf(p->output, "T{\n");
		while (curcell) {
			parser_pushstr(p, curcell->contents->str);
			parse_text(p);
			if (curcell->next) {
Ivan Tham's avatar
Ivan Tham committed
631
				fprintf(p->output, "\nT}\tT{\n");
Drew DeVault's avatar
Drew DeVault committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
			} else {
				fprintf(p->output, "\nT}");
			}
			struct table_cell *prev = curcell;
			curcell = curcell->next;
			str_free(prev->contents);
			free(prev);
		}
		fprintf(p->output, "\n");
		struct table_row *prev = currow;
		currow = currow->next;
		free(prev);
	}

	roff_macro(p, "TE", NULL);
Drew DeVault's avatar
Drew DeVault committed
647
	fprintf(p->output, ".sp 1\n");
Drew DeVault's avatar
Drew DeVault committed
648
649
}

Drew DeVault's avatar
Drew DeVault committed
650
651
652
653
static void parse_document(struct parser *p) {
	uint32_t ch;
	int indent = 0;
	do {
Drew DeVault's avatar
Drew DeVault committed
654
		parse_indent(p, &indent, true);
Drew DeVault's avatar
Drew DeVault committed
655
656
657
		if ((ch = parser_getch(p)) == UTF8_INVALID) {
			break;
		}
Drew DeVault's avatar
Drew DeVault committed
658
		switch (ch) {
Drew DeVault's avatar
Drew DeVault committed
659
660
661
662
663
664
665
666
		case ';':
			if ((ch = parser_getch(p)) != ' ') {
				parser_fatal(p, "Expected space after ; to begin comment");
			}
			do {
				ch = parser_getch(p);
			} while (ch != UTF8_INVALID && ch != '\n');
			break;
Drew DeVault's avatar
Drew DeVault committed
667
		case '#':
Drew DeVault's avatar
Drew DeVault committed
668
669
670
671
672
			if (indent != 0) {
				parser_pushch(p, ch);
				parse_text(p);
				break;
			}
Drew DeVault's avatar
Drew DeVault committed
673
674
			parse_heading(p);
			break;
Drew DeVault's avatar
Drew DeVault committed
675
		case '-':
Drew DeVault's avatar
Drew DeVault committed
676
677
678
			parse_list(p, &indent, -1);
			break;
		case '.':
679
680
681
682
683
684
685
			if ((ch = parser_getch(p)) == ' ') {
				parser_pushch(p, ch);
				parse_list(p, &indent, 1);
			} else {
				parser_pushch(p, ch);
				parse_text(p);
			}
Drew DeVault's avatar
Drew DeVault committed
686
			break;
Drew DeVault's avatar
Drew DeVault committed
687
688
689
		case '`':
			parse_literal(p, &indent);
			break;
Drew DeVault's avatar
Drew DeVault committed
690
691
692
693
694
695
696
697
		case '[':
		case '|':
		case ']':
			if (indent != 0) {
				parser_fatal(p, "Tables cannot be indented");
			}
			parse_table(p, ch);
			break;
Drew DeVault's avatar
Drew DeVault committed
698
699
700
		case ' ':
			parser_fatal(p, "Tabs are required for indentation");
			break;
Drew DeVault's avatar
Drew DeVault committed
701
		case '\n':
702
703
704
705
706
707
708
709
710
			if (p->flags) {
				char error[512];
				snprintf(error, sizeof(error), "Expected %c before starting "
						"new paragraph (began with %c at %d:%d)",
						p->flags == FORMAT_BOLD ? '*' : '_',
						p->flags == FORMAT_BOLD ? '*' : '_',
						p->fmt_line, p->fmt_col);
				parser_fatal(p, error);
			}
Drew DeVault's avatar
Drew DeVault committed
711
712
			roff_macro(p, "P", NULL);
			break;
Drew DeVault's avatar
Drew DeVault committed
713
		default:
Drew DeVault's avatar
Drew DeVault committed
714
			parser_pushch(p, ch);
Drew DeVault's avatar
Drew DeVault committed
715
716
717
			parse_text(p);
			break;
		}
Drew DeVault's avatar
Drew DeVault committed
718
	} while (ch != UTF8_INVALID);
Drew DeVault's avatar
Drew DeVault committed
719
720
721
}

static void output_scdoc_preamble(struct parser *p) {
722
	fprintf(p->output, ".\\\" Generated by scdoc " VERSION "\n");
723
724
	fprintf(p->output, ".\\\" Complete documentation for this program is not "
			"available as a GNU info page\n");
725
726
727
	// Fix weird quotation marks
	// http://bugs.debian.org/507673
	// http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
Drew DeVault's avatar
Drew DeVault committed
728
729
	fprintf(p->output, ".ie \\n(.g .ds Aq \\(aq\n");
	fprintf(p->output, ".el       .ds Aq '\n");
730
	// Disable hyphenation:
Drew DeVault's avatar
Drew DeVault committed
731
	roff_macro(p, "nh", NULL);
732
	// Disable justification:
Drew DeVault's avatar
Drew DeVault committed
733
	roff_macro(p, "ad l", NULL);
734
	fprintf(p->output, ".\\\" Begin generated content:\n");
Drew DeVault's avatar
Drew DeVault committed
735
736
737
}

int main(int argc, char **argv) {
Drew DeVault's avatar
Drew DeVault committed
738
739
740
741
	if (argc == 2 && strcmp(argv[1], "-v") == 0) {
		printf("scdoc " VERSION "\n");
		return 0;
	} else if (argc > 1) {
Drew DeVault's avatar
Drew DeVault committed
742
		fprintf(stderr, "Usage: scdoc < input.scd > output.roff\n");
Drew DeVault's avatar
Drew DeVault committed
743
744
745
746
747
748
749
750
		return 1;
	}
	struct parser p = {
		.input = stdin,
		.output = stdout,
		.line = 1,
		.col = 1
	};
Drew DeVault's avatar
Drew DeVault committed
751
	output_scdoc_preamble(&p);
Drew DeVault's avatar
Drew DeVault committed
752
	parse_preamble(&p);
Drew DeVault's avatar
Drew DeVault committed
753
	parse_document(&p);
Drew DeVault's avatar
Drew DeVault committed
754
755
	return 0;
}