]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/troff/t11.c
ip/ipconfig: format ipmask with %M instead of %I
[plan9front.git] / sys / src / cmd / troff / t11.c
1 #include "tdef.h"
2 #include "fns.h"
3 #include "ext.h"
4
5 #define MAXCH NCHARS            /* maximum number of global char names */
6 char    *chnames[MAXCH];        /* chnames[n-ALPHABET] -> name of char n */
7 int     nchnames;               /* number of Cxy names currently seen */
8
9 #define MAXPS   100             /* max number of point sizes */
10 int     pstab[MAXPS];           /* point sizes */
11 int     nsizes;                 /* number in DESC */
12
13 Font    fonts[MAXFONTS+1];      /* font info + ptr to width info */
14
15
16 #define skipline(f)     while (getc(f) != '\n')
17
18 #define eq(s1, s2)      (strcmp(s1, s2) == 0)
19
20 getdesc(char *name)
21 {
22         FILE *fin;
23         char cmd[100], s[100];
24         int i, v;
25
26         if ((fin = fopen(name, "r")) == NULL)
27                 return -1;
28         while (fscanf(fin, "%s", cmd) != EOF) {
29                 if (strcmp(cmd, "res") == 0) {
30                         fscanf(fin, "%d", &Inch);
31                 } else if (strcmp(cmd, "hor") == 0) {
32                         fscanf(fin, "%d", &Hor);
33                 } else if (strcmp(cmd, "vert") == 0) {
34                         fscanf(fin, "%d", &Vert);
35                 } else if (strcmp(cmd, "unitwidth") == 0) {
36                         fscanf(fin, "%d", &Unitwidth);
37                 } else if (strcmp(cmd, "sizes") == 0) {
38                         nsizes = 0;
39                         while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
40                                 pstab[nsizes++] = v;
41                 } else if (strcmp(cmd, "fonts") == 0) {
42                         fscanf(fin, "%d", &nfonts);
43                         for (i = 1; i <= nfonts; i++) {
44                                 fscanf(fin, "%s", s);
45                                 fontlab[i] = PAIR(s[0], s[1]);
46                         }
47                 } else if (strcmp(cmd, "charset") == 0) {       /* add any names */
48                         while (fscanf(fin, "%s", s) != EOF)
49                                 chadd(s, Troffchar, Install);
50                         break;
51                 }
52                 /* else 
53                         just skip anything else */
54                 skipline(fin);
55         }
56         fclose(fin);
57         return 1;
58 }
59
60 static int checkfont(char *name)
61 {               /* in case it's not really a font description file */
62                 /* really paranoid, but consider \f. */
63         FILE *fp;
64         char buf[300], buf2[300];
65         int i, status = -1;
66
67         if ((fp = fopen(name, "r")) == NULL)
68                 return -1;
69         for (i = 1; i <= 10; i++) {
70                 if (fgets(buf, sizeof buf, fp) == NULL)
71                         break;
72                 sscanf(buf, "%s", buf2);
73                 if (buf2[0] == '#') {
74                         i--;
75                         continue;
76                 }
77                 if (eq(buf2, "name") || eq(buf2, "fontname") ||
78                     eq(buf2, "special") || eq(buf2, "charset")) {
79                         status = 1;
80                         break;
81                 }
82         }
83         fclose(fp);
84         return status;
85         
86 }
87
88 getfont(char *name, int pos)    /* create width tab for font */
89 {
90         FILE *fin;
91         Font *ftemp = &fonts[pos];
92         Chwid chtemp[MAXCH];
93         static Chwid chinit;
94         int i, nw, n, wid, kern, code, type;
95         char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
96
97         /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
98         if (checkfont(name) == -1)
99                 return -1;
100         if ((fin = fopen(name, "r")) == NULL)
101                 return -1;
102         for (i = 0; i < ALPHABET; i++)
103                 chtemp[i] = chinit;     /* zero out to begin with */
104         ftemp->specfont = ftemp->ligfont = 0;
105         ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
106         while (fscanf(fin, "%s", cmd) != EOF) {
107                 if (strcmp(cmd, "name") == 0)
108                         fscanf(fin, "%s", ftemp->longname);
109                 else if (strcmp(cmd, "special") == 0)
110                         ftemp->specfont = 1;
111                 else if (strcmp(cmd, "ligatures") == 0) {
112                         ftemp->ligfont = getlig(fin);
113                 } else if (strcmp(cmd, "spacewidth") == 0) {
114                         fscanf(fin, "%d", &ftemp->spacewidth);
115                 } else if (strcmp(cmd, "defaultwidth") == 0) {
116                         fscanf(fin, "%d", &ftemp->defaultwidth);
117                 } else if (strcmp(cmd, "charset") == 0) {
118                         wchar_t wc;
119                         skipline(fin);
120                         nw = ALPHABET;
121                         while (fgets(buf, sizeof buf, fin) != NULL) {
122                                 sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
123                                 if (s1[0] != '"') {     /* genuine new character */
124                                         sscanf(s1, "%d", &wid);
125                                         sscanf(s2, "%d", &kern);
126                                         code = strtol(s3, 0, 0);        /* dec/oct/hex */
127                                 }
128                                 /* otherwise it's a synonym for prev character, */
129                                 /* so leave previous values intact */
130
131
132                                 /* decide what kind of alphabet it might come from here */
133
134
135                                 if (strlen(ch) == 1) {  /* it's ascii */
136                                         n = ch[0];      /* origin includes non-graphics */
137                                         chtemp[n].num = ch[0];
138                                 } else if (ch[0] == '\\' && ch[1] == '0') {
139                                         n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
140                                         chtemp[n].num = n;
141 #ifdef UNICODE
142                                 } else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
143                                         chtemp[nw].num = chadd(ch,  MBchar, Install);
144                                         n = nw;
145                                         nw++;
146 #endif  /*UNICODE*/
147                                 } else {
148                                         if (strcmp(ch, "---") == 0) { /* no name */
149                                                 sprintf(ch, "%d", code);
150                                                 type = Number;
151                                         } else
152                                                 type = Troffchar;
153                                         chtemp[nw].num = chadd(ch, type, Install);
154                                         n = nw;
155                                         nw++;
156                                 }
157                                 chtemp[n].wid = wid;
158                                 chtemp[n].kern = kern;
159                                 chtemp[n].code = code;
160                                 /*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
161                                         ftemp->longname, ch, n, wid, code);
162                                 */
163                         }
164                         break;
165                 }
166                 skipline(fin);
167         }
168         fclose(fin);
169         chtemp[' '].wid = ftemp->spacewidth;    /* width of space on this font */
170         ftemp->nchars = nw;
171         if (ftemp->wp)
172                 free(ftemp->wp);        /* god help us if this wasn't allocated */
173         ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
174         if (ftemp->wp == NULL)
175                 return -1;
176         for (i = 0; i < nw; i++)
177                 ftemp->wp[i] = chtemp[i];
178 /*
179  *      printf("%d chars: ", nw);
180  *      for (i = 0; i < nw; i++)
181  *              if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
182  *                      printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
183  *              else if (i >= ALPHABET)
184  *                      printf("%d (%s) %d ", ftemp->wp[i].num,
185  *                              chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
186  *      }
187  *      printf("\n");
188  */
189         return 1;
190 }
191
192 chadd(char *s, int type, int install)   /* add s to global character name table; */
193 {                                       /* or just look it up */
194
195         /* a temporary kludge:  store the "type" as the first character */
196         /* of the string, so we can remember from whence it came */
197
198         char *p;
199         int i;
200
201 /* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
202         for (i = 0; i < nchnames; i++)
203                 if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
204                         break;
205 /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
206         if (i < nchnames)               /* found same type and bytes at position i */
207                 return ALPHABET + i;
208         else if (install == Lookup)     /* not found, and we were just looking */
209                 return -1;
210
211         chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1); /* type + \0 */
212         if (p == NULL) {
213                 ERROR "out of space adding character %s", s WARN;
214                 return LEFTHAND;
215         }
216         if (nchnames >= NCHARS - ALPHABET) {
217                 ERROR "out of table space adding character %s", s WARN;
218                 return LEFTHAND;
219         }
220         strcpy(chnames[nchnames]+1, s);
221         chnames[nchnames][0] = type;
222 /* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
223         return nchnames++ + ALPHABET;
224 }
225
226 char *chname(int n)     /* return string for char with index n */
227 {                       /* includes type char at front, to be peeled off elsewhere */
228         if (n >= ALPHABET && n < nchnames + ALPHABET)
229                 return chnames[n-ALPHABET];
230         else
231                 return "";
232 }
233
234 getlig(FILE *fin)       /* pick up ligature list */
235 {
236         int lig;
237         char temp[200];
238
239         lig = 0;
240         while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
241                 if (strcmp(temp, "fi") == 0)
242                         lig |= LFI;
243                 else if (strcmp(temp, "fl") == 0)
244                         lig |= LFL;
245                 else if (strcmp(temp, "ff") == 0)
246                         lig |= LFF;
247                 else if (strcmp(temp, "ffi") == 0)
248                         lig |= LFFI;
249                 else if (strcmp(temp, "ffl") == 0)
250                         lig |= LFFL;
251                 else
252                         fprintf(stderr, "illegal ligature %s ignored\n", temp);
253         }
254         return lig;
255 }