xref: /plan9/sys/src/cmd/tbl/t3.c (revision 43af371b8d63420d32d0cb0b58914db16dcd7715)
1 /* t3.c: interpret commands affecting whole table */
2 # include "t.h"
3 
4 struct optstr {
5 	char	*optnam;
6 	int	*optadd;
7 } options [] = {
8 	"expand", &expflg,
9 	"EXPAND", &expflg,
10 	"center", &ctrflg,
11 	"CENTER", &ctrflg,
12 	"box", &boxflg,
13 	"BOX", &boxflg,
14 	"allbox", &allflg,
15 	"ALLBOX", &allflg,
16 	"doublebox", &dboxflg,
17 	"DOUBLEBOX", &dboxflg,
18 	"frame", &boxflg,
19 	"FRAME", &boxflg,
20 	"doubleframe", &dboxflg,
21 	"DOUBLEFRAME", &dboxflg,
22 	"tab", &tab,
23 	"TAB", &tab,
24 	"linesize", &linsize,
25 	"LINESIZE", &linsize,
26 	"delim", &delim1,
27 	"DELIM", &delim1,
28 	0, 0
29 };
30 
31 void
getcomm(void)32 getcomm(void)
33 {
34 	char	line[200], *cp, nb[25], *t;
35 	struct optstr *lp;
36 	int	c, ci, found;
37 
38 	for (lp = options; lp->optnam; lp++)
39 		*(lp->optadd) = 0;
40 	texname = texstr[texct=0];
41 	tab = '\t';
42 	Bprint(&tabout, ".nr %d \\n(.s\n", LSIZE);
43 	gets1(line, sizeof(line));
44 	/* see if this is a command line */
45 	if (strchr(line, ';') == 0) {
46 		backrest(line);
47 		return;
48 	}
49 	for (cp = line; (c = *cp) != ';'; cp++) {
50 		if (!letter(c))
51 			continue;
52 		found = 0;
53 		for (lp = options; lp->optadd; lp++) {
54 			if (prefix(lp->optnam, cp)) {
55 				*(lp->optadd) = 1;
56 				cp += strlen(lp->optnam);
57 				if (letter(*cp))
58 					error("Misspelled global option");
59 				while (*cp == ' ')
60 					cp++;
61 				t = nb;
62 				if ( *cp == '(')
63 					while ((ci = *++cp) != ')')
64 						*t++ = ci;
65 				else
66 					cp--;
67 				*t++ = 0;
68 				*t = 0;
69 				if (lp->optadd == &tab) {
70 					if (nb[0])
71 						*(lp->optadd) = nb[0];
72 				}
73 				if (lp->optadd == &linsize)
74 					Bprint(&tabout, ".nr %d %s\n", LSIZE, nb);
75 				if (lp->optadd == &delim1) {
76 					delim1 = nb[0];
77 					delim2 = nb[1];
78 				}
79 				found = 1;
80 				break;
81 			}
82 		}
83 		if (!found)
84 			error("Illegal option");
85 	}
86 	cp++;
87 	backrest(cp);
88 }
89 
90 void
backrest(char * cp)91 backrest(char *cp)
92 {
93 	char	*s;
94 
95 	for (s = cp; *s; s++)
96 		;
97 	un1getc('\n');
98 	while (s > cp)
99 		un1getc(*--s);
100 }
101