[go: up one dir, main page]

File: ttman.c

package info (click to toggle)
cmus 2.10.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,040 kB
  • sloc: ansic: 38,844; sh: 1,578; makefile: 257; python: 157
file content (883 lines) | stat: -rw-r--r-- 16,011 bytes parent folder | download | duplicates (6)
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
/*
 * ttman - text to man converter
 *
 * Copyright 2006 Timo Hirvonen <tihirvon@gmail.com>
 *
 * This file is licensed under the GPLv2.
 */
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

struct token {
	struct token *next;
	struct token *prev;
	enum {
		TOK_TEXT,	// max one line w/o \n
		TOK_NL,		// \n
		TOK_ITALIC,	// `
		TOK_BOLD,	// *
		TOK_INDENT,	// \t

		// keywords (@...)
		TOK_H1,
		TOK_H2,
		TOK_LI,
		TOK_BR,
		TOK_PRE,
		TOK_ENDPRE,	// must be after TOK_PRE
		TOK_RAW,
		TOK_ENDRAW,	// must be after TOK_RAW
		TOK_TITLE,	// WRITE 2 2001-12-13 "Linux 2.0.32" "Linux Programmer's Manual"
	} type;
	int line;

	// not NUL-terminated
	const char *text;
	// length of text
	int len;
};

static const char *program;
static const char *filename;
static char tmp_file[1024];
static FILE *outfile;
static int cur_line = 1;
static struct token head = { &head, &head, TOK_TEXT, 0, NULL, 0 };

#define CONST_STR(str) { str, sizeof(str) - 1 }
static const struct {
	const char *str;
	int len;
} token_names[] = {
	CONST_STR("text"),
	CONST_STR("nl"),
	CONST_STR("italic"),
	CONST_STR("bold"),
	CONST_STR("indent"),

	// keywords
	CONST_STR("h1"),
	CONST_STR("h2"),
	CONST_STR("li"),
	CONST_STR("br"),
	CONST_STR("pre"),
	CONST_STR("endpre"),
	CONST_STR("raw"),
	CONST_STR("endraw"),
	CONST_STR("title")
};
#define NR_TOKEN_NAMES (sizeof(token_names) / sizeof(token_names[0]))
#define BUG() die("BUG in %s\n", __FUNCTION__)

#ifdef __GNUC__
#define CMUS_NORETURN	__attribute__((__noreturn__))
#else
#define CMUS_NORETURN
#endif

static CMUS_NORETURN void quit(void)
{
	if (tmp_file[0])
		unlink(tmp_file);
	exit(1);
}

static CMUS_NORETURN void die(const char *format, ...)
{
	va_list ap;

	fprintf(stderr, "%s: ", program);
	va_start(ap, format);
	vfprintf(stderr, format, ap);
	va_end(ap);
	quit();
}

static CMUS_NORETURN void syntax(int line, const char *format, ...)
{
	va_list ap;

	fprintf(stderr, "%s:%d: error: ", filename, line);
	va_start(ap, format);
	vfprintf(stderr, format, ap);
	va_end(ap);
	quit();
}

static inline const char *keyword_name(int type)
{
	if (type < TOK_H1 || type > TOK_TITLE)
		die("BUG: no keyword name for type %d\n", type);
	return token_names[type].str;
}

static void *xmalloc(size_t size)
{
	void *ret = malloc(size);

	if (!ret)
		die("OOM when allocating %ul bytes\n", size);
	return ret;
}

static char *memdup(const char *str, int len)
{
	char *s = xmalloc(len + 1);
	memcpy(s, str, len);
	s[len] = 0;
	return s;
}

static struct token *new_token(int type)
{
	struct token *tok = xmalloc(sizeof(struct token));

	tok->prev = NULL;
	tok->next = NULL;
	tok->type = type;
	tok->line = cur_line;
	return tok;
}

static void free_token(struct token *tok)
{
	struct token *prev = tok->prev;
	struct token *next = tok->next;

	if (tok == &head)
		BUG();

	prev->next = next;
	next->prev = prev;
	free(tok);
}

static void emit_token(struct token *tok)
{
	tok->prev = head.prev;
	tok->next = &head;
	head.prev->next = tok;
	head.prev = tok;
}

static void emit(int type)
{
	struct token *tok = new_token(type);
	tok->len = 0;
	tok->text = NULL;
	emit_token(tok);
}

static int emit_keyword(const char *buf, int size)
{
	int i, len;

	for (len = 0; len < size; len++) {
		if (!isalnum((unsigned char)buf[len]))
			break;
	}

	if (!len)
		syntax(cur_line, "keyword expected\n");

	for (i = TOK_H1; i < NR_TOKEN_NAMES; i++) {
		if (len != token_names[i].len)
			continue;
		if (!strncmp(buf, token_names[i].str, len)) {
			emit(i);
			return len;
		}
	}
	syntax(cur_line, "invalid keyword '@%s'\n", memdup(buf, len));
}

static int emit_text(const char *buf, int size)
{
	struct token *tok;
	int i;

	for (i = 0; i < size; i++) {
		int c = buf[i];
		if (c == '@' || c == '`' || c == '*' || c == '\n' || c == '\\' || c == '\t')
			break;
	}
	tok = new_token(TOK_TEXT);
	tok->text = buf;
	tok->len = i;
	emit_token(tok);
	return i;
}

static void tokenize(const char *buf, int size)
{
	int pos = 0;

	while (pos < size) {
		struct token *tok;
		int ch;

		ch = buf[pos++];
		switch (ch) {
		case '@':
			pos += emit_keyword(buf + pos, size - pos);
			break;
		case '`':
			emit(TOK_ITALIC);
			break;
		case '*':
			emit(TOK_BOLD);
			break;
		case '\n':
			emit(TOK_NL);
			cur_line++;
			break;
		case '\t':
			emit(TOK_INDENT);
			break;
		case '\\':
			tok = new_token(TOK_TEXT);
			tok->text = buf + pos;
			tok->len = 1;
			pos++;
			if (pos == size || buf[pos] == '\n') {
				// just one '\\'
				tok->text--;
			}

			if (tok->text[0] == '\\') {
				tok->text = "\\\\";
				tok->len = 2;
			}

			emit_token(tok);
			break;
		default:
			pos--;
			pos += emit_text(buf + pos, size - pos);
			break;
		}
	}
}

static int is_empty_line(const struct token *tok)
{
	while (tok != &head) {
		int i;

		switch (tok->type) {
		case TOK_TEXT:
			for (i = 0; i < tok->len; i++) {
				if (tok->text[i] != ' ')
					return 0;
			}
			break;
		case TOK_INDENT:
			break;
		case TOK_NL:
			return 1;
		default:
			return 0;
		}
		tok = tok->next;
	}
	return 1;
}

static struct token *remove_line(struct token *tok)
{
	while (tok != &head) {
		struct token *next = tok->next;
		int type = tok->type;

		free_token(tok);
		tok = next;
		if (type == TOK_NL)
			break;
	}
	return tok;
}

static struct token *skip_after(struct token *tok, int type)
{
	struct token *save = tok;

	while (tok != &head) {
		if (tok->type == type) {
			tok = tok->next;
			if (tok->type != TOK_NL)
				syntax(tok->line, "newline expected after @%s\n",
						keyword_name(type));
			return tok->next;
		}
		if (tok->type >= TOK_H1)
			syntax(tok->line, "keywords not allowed betweed @%s and @%s\n",
					keyword_name(type-1), keyword_name(type));
		tok = tok->next;
	}
	syntax(save->prev->line, "missing @%s\n", keyword_name(type));
}

static struct token *get_next_line(struct token *tok)
{
	while (tok != &head) {
		int type = tok->type;

		tok = tok->next;
		if (type == TOK_NL)
			break;
	}
	return tok;
}

static struct token *get_indent(struct token *tok, int *ip)
{
	int i = 0;

	while (tok != &head && tok->type == TOK_INDENT) {
		tok = tok->next;
		i++;
	}
	*ip = i;
	return tok;
}

// line must be non-empty
static struct token *check_line(struct token *tok, int *ip)
{
	struct token *start;
	int tok_type;

	start = tok = get_indent(tok, ip);

	tok_type = tok->type;
	switch (tok_type) {
	case TOK_TEXT:
	case TOK_BOLD:
	case TOK_ITALIC:
	case TOK_BR:
		tok = tok->next;
		while (tok != &head) {
			switch (tok->type) {
			case TOK_TEXT:
			case TOK_BOLD:
			case TOK_ITALIC:
			case TOK_BR:
			case TOK_INDENT:
				break;
			case TOK_NL:
				return start;
			default:
				syntax(tok->line, "@%s not allowed inside paragraph\n",
						keyword_name(tok->type));
			}
			tok = tok->next;
		}
		break;
	case TOK_H1:
	case TOK_H2:
	case TOK_TITLE:
		if (*ip)
			goto indentation;

		// check arguments
		tok = tok->next;
		while (tok != &head) {
			switch (tok->type) {
			case TOK_TEXT:
			case TOK_INDENT:
				break;
			case TOK_NL:
				return start;
			default:
				syntax(tok->line, "@%s can contain only text\n",
						keyword_name(tok_type));
			}
			tok = tok->next;
		}
		break;
	case TOK_LI:
		// check arguments
		tok = tok->next;
		while (tok != &head) {
			switch (tok->type) {
			case TOK_TEXT:
			case TOK_BOLD:
			case TOK_ITALIC:
			case TOK_INDENT:
				break;
			case TOK_NL:
				return start;
			default:
				syntax(tok->line, "@%s not allowed inside @li\n",
						keyword_name(tok->type));
			}
			tok = tok->next;
		}
		break;
	case TOK_PRE:
		// checked later
		break;
	case TOK_RAW:
		if (*ip)
			goto indentation;
		// checked later
		break;
	case TOK_ENDPRE:
	case TOK_ENDRAW:
		syntax(tok->line, "@%s not expected\n", keyword_name(tok->type));
		break;
	case TOK_NL:
	case TOK_INDENT:
		BUG();
		break;
	}
	return start;
indentation:
	syntax(tok->line, "indentation before @%s\n", keyword_name(tok->type));
}

static void insert_nl_before(struct token *next)
{
	struct token *prev = next->prev;
	struct token *new = new_token(TOK_NL);

	new->prev = prev;
	new->next = next;
	prev->next = new;
	next->prev = new;
}

static void normalize(void)
{
	struct token *tok = head.next;
	/*
	 * >= 0 if previous line was text (== amount of indent)
	 *   -1 if previous block was @pre (amount of indent doesn't matter)
	 *   -2 otherwise (@h1 etc., indent was 0)
	 */
	int prev_indent = -2;

	while (tok != &head) {
		struct token *start;
		int i, new_para = 0;

		// remove empty lines
		while (is_empty_line(tok)) {
			tok = remove_line(tok);
			new_para = 1;
			if (tok == &head)
				return;
		}

		// skips indent
		start = tok;
		tok = check_line(tok, &i);

		switch (tok->type) {
		case TOK_TEXT:
		case TOK_ITALIC:
		case TOK_BOLD:
		case TOK_BR:
			// normal text
			if (new_para && prev_indent >= -1) {
				// previous line/block was text or @pre
				// and there was a empty line after it
				insert_nl_before(start);
			}

			if (!new_para && prev_indent == i) {
				// join with previous line
				struct token *nl = start->prev;

				if (nl->type != TOK_NL)
					BUG();

				if ((nl->prev != &head && nl->prev->type == TOK_BR) ||
						tok->type == TOK_BR) {
					// don't convert \n after/before @br to ' '
					free_token(nl);
				} else {
					// convert "\n" to " "
					nl->type = TOK_TEXT;
					nl->text = " ";
					nl->len = 1;
				}

				// remove indent
				while (start->type == TOK_INDENT) {
					struct token *next = start->next;
					free_token(start);
					start = next;
				}
			}

			prev_indent = i;
			tok = get_next_line(tok);
			break;
		case TOK_PRE:
		case TOK_RAW:
			// these can be directly after normal text
			// but not joined with the previous line
			if (new_para && prev_indent >= -1) {
				// previous line/block was text or @pre
				// and there was a empty line after it
				insert_nl_before(start);
			}
			tok = skip_after(tok->next, tok->type + 1);
			prev_indent = -1;
			break;
		case TOK_H1:
		case TOK_H2:
		case TOK_LI:
		case TOK_TITLE:
			// remove white space after H1, H2, L1 and TITLE
			tok = tok->next;
			while (tok != &head) {
				int type = tok->type;
				struct token *next;

				if (type == TOK_TEXT) {
					while (tok->len && *tok->text == ' ') {
						tok->text++;
						tok->len--;
					}
					if (tok->len)
						break;
				}
				if (type != TOK_INDENT)
					break;

				// empty TOK_TEXT or TOK_INDENT
				next = tok->next;
				free_token(tok);
				tok = next;
			}
			// not normal text. can't be joined
			prev_indent = -2;
			tok = get_next_line(tok);
			break;
		case TOK_NL:
		case TOK_INDENT:
		case TOK_ENDPRE:
		case TOK_ENDRAW:
			BUG();
			break;
		}
	}
}

#define output(...) fprintf(outfile, __VA_ARGS__)

static void output_buf(const char *buf, int len)
{
	fwrite(buf, 1, len, outfile);
}

static void output_text(struct token *tok)
{
	char buf[1024];
	const char *str = tok->text;
	int len = tok->len;
	int pos = 0;

	while (len) {
		int c = *str++;

		if (pos >= sizeof(buf) - 1) {
			output_buf(buf, pos);
			pos = 0;
		}
		if (c == '-')
			buf[pos++] = '\\';
		buf[pos++] = c;
		len--;
	}

	if (pos)
		output_buf(buf, pos);
}

static int bold = 0;
static int italic = 0;
static int indent = 0;

static struct token *output_pre(struct token *tok)
{
	int bol = 1;

	if (tok->type != TOK_NL)
		syntax(tok->line, "newline expected after @pre\n");

	output(".nf\n");
	tok = tok->next;
	while (tok != &head) {
		if (bol) {
			int i;

			tok = get_indent(tok, &i);
			if (i != indent && tok->type != TOK_NL)
				syntax(tok->line, "indent changed in @pre\n");
		}

		switch (tok->type) {
		case TOK_TEXT:
			if (bol && tok->len && tok->text[0] == '.')
				output("\\&");
			output_text(tok);
			break;
		case TOK_NL:
			output("\n");
			bol = 1;
			tok = tok->next;
			continue;
		case TOK_ITALIC:
			output("`");
			break;
		case TOK_BOLD:
			output("*");
			break;
		case TOK_INDENT:
			// FIXME: warn
			output(" ");
			break;
		case TOK_ENDPRE:
			output(".fi\n");
			tok = tok->next;
			if (tok != &head && tok->type == TOK_NL)
				tok = tok->next;
			return tok;
		default:
			BUG();
			break;
		}
		bol = 0;
		tok = tok->next;
	}
	return tok;
}

static struct token *output_raw(struct token *tok)
{
	if (tok->type != TOK_NL)
		syntax(tok->line, "newline expected after @raw\n");

	tok = tok->next;
	while (tok != &head) {
		switch (tok->type) {
		case TOK_TEXT:
			if (tok->len == 2 && !strncmp(tok->text, "\\\\", 2)) {
				/* ugly special case
				 * "\\" (\) was converted to "\\\\" (\\) because
				 * nroff does escaping too.
				 */
				output("\\");
			} else {
				output_buf(tok->text, tok->len);
			}
			break;
		case TOK_NL:
			output("\n");
			break;
		case TOK_ITALIC:
			output("`");
			break;
		case TOK_BOLD:
			output("*");
			break;
		case TOK_INDENT:
			output("\t");
			break;
		case TOK_ENDRAW:
			tok = tok->next;
			if (tok != &head && tok->type == TOK_NL)
				tok = tok->next;
			return tok;
		default:
			BUG();
			break;
		}
		tok = tok->next;
	}
	return tok;
}

static struct token *output_para(struct token *tok)
{
	int bol = 1;

	while (tok != &head) {
		switch (tok->type) {
		case TOK_TEXT:
			output_text(tok);
			break;
		case TOK_ITALIC:
			italic ^= 1;
			if (italic) {
				output("\\fI");
			} else {
				output("\\fR");
			}
			break;
		case TOK_BOLD:
			bold ^= 1;
			if (bold) {
				output("\\fB");
			} else {
				output("\\fR");
			}
			break;
		case TOK_BR:
			if (bol) {
				output(".br\n");
			} else {
				output("\n.br\n");
			}
			bol = 1;
			tok = tok->next;
			continue;
		case TOK_NL:
			output("\n");
			return tok->next;
		case TOK_INDENT:
			output(" ");
			break;
		default:
			BUG();
			break;
		}
		bol = 0;
		tok = tok->next;
	}
	return tok;
}

static struct token *title(struct token *tok, const char *cmd)
{
	output("%s", cmd);
	return output_para(tok->next);
}

static struct token *dump_one(struct token *tok)
{
	int i;

	tok = get_indent(tok, &i);
	if (tok->type != TOK_RAW) {
		while (indent < i) {
			output(".RS\n");
			indent++;
		}
		while (indent > i) {
			output(".RE\n");
			indent--;
		}
	}

	switch (tok->type) {
	case TOK_TEXT:
	case TOK_ITALIC:
	case TOK_BOLD:
	case TOK_BR:
		if (tok->type == TOK_TEXT && tok->len && tok->text[0] == '.')
			output("\\&");
		tok = output_para(tok);
		break;
	case TOK_H1:
		tok = title(tok, ".SH ");
		break;
	case TOK_H2:
		tok = title(tok, ".SS ");
		break;
	case TOK_LI:
		tok = title(tok, ".TP\n");
		break;
	case TOK_PRE:
		tok = output_pre(tok->next);
		break;
	case TOK_RAW:
		tok = output_raw(tok->next);
		break;
	case TOK_TITLE:
		tok = title(tok, ".TH ");
		// must be after .TH
		// no hyphenation, adjust left
		output(".nh\n.ad l\n");
		break;
	case TOK_NL:
		output("\n");
		tok = tok->next;
		break;
	case TOK_ENDPRE:
	case TOK_ENDRAW:
	case TOK_INDENT:
		BUG();
		break;
	}
	return tok;
}

static void dump(void)
{
	struct token *tok = head.next;

	while (tok != &head)
		tok = dump_one(tok);
}

static void process(void)
{
	struct stat s = {};
	const char *buf;
	int fd;

	fd = open(filename, O_RDONLY);
	if (fd == -1)
		die("opening `%s' for reading: %s\n", filename, strerror(errno));
	fstat(fd, &s);
	if (s.st_size) {
		buf = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
		if (buf == MAP_FAILED)
			die("mmap: %s\n", strerror(errno));

		tokenize(buf, s.st_size);
		normalize();
	}
	close(fd);
	dump();
}

int main(int argc, char *argv[])
{
	const char *dest;
	int fd;

	program = argv[0];
	if (argc != 3) {
		fprintf(stderr, "Usage: %s <in> <out>\n", program);
		return 1;
	}
	filename = argv[1];
	dest = argv[2];

	snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", dest);
	fd = mkstemp(tmp_file);
	if (fd < 0)
		die("creating %s: %s\n", tmp_file, strerror(errno));
	outfile = fdopen(fd, "w");
	if (!outfile)
		die("opening %s: %s\n", tmp_file, strerror(errno));

	process();
	if (rename(tmp_file, dest))
		die("renaming %s to %s: %s\n", tmp_file, dest, strerror(errno));
	return 0;
}