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
|
#include "a.h"
enum
{
MAXREQ = 100,
MAXRAW = 40,
MAXESC = 60,
MAXLINE = 1024,
MAXIF = 20,
MAXARG = 10
};
typedef struct Esc Esc;
typedef struct Req Req;
typedef struct Raw Raw;
/* escape sequence handler, like for \c */
struct Esc
{
Rune r;
int (*f)(void);
int mode;
};
/* raw request handler, like for .ie */
struct Raw
{
Rune *name;
void (*f)(Rune*);
};
/* regular request handler, like for .ft */
struct Req
{
int argc;
Rune *name;
void (*f)(int, Rune**);
};
int dot = '.';
int tick = '\'';
int backslash = '\\';
int inputmode;
Req req[MAXREQ];
int nreq;
Raw raw[MAXRAW];
int nraw;
Esc esc[MAXESC];
int nesc;
int iftrue[MAXIF];
int niftrue;
int isoutput;
int linepos;
void
addraw(Rune *name, void (*f)(Rune*))
{
Raw *r;
if(nraw >= nelem(raw)){
fprint(2, "too many raw requets\n");
return;
}
r = &raw[nraw++];
r->name = erunestrdup(name);
r->f = f;
}
void
delraw(Rune *name)
{
int i;
for(i=0; i<nraw; i++){
if(runestrcmp(raw[i].name, name) == 0){
if(i != --nraw){
free(raw[i].name);
raw[i] = raw[nraw];
}
return;
}
}
}
void
renraw(Rune *from, Rune *to)
{
int i;
delraw(to);
for(i=0; i<nraw; i++)
if(runestrcmp(raw[i].name, from) == 0){
free(raw[i].name);
raw[i].name = erunestrdup(to);
return;
}
}
void
addreq(Rune *s, void (*f)(int, Rune**), int argc)
{
Req *r;
if(nreq >= nelem(req)){
fprint(2, "too many requests\n");
return;
}
r = &req[nreq++];
r->name = erunestrdup(s);
r->f = f;
r->argc = argc;
}
void
delreq(Rune *name)
{
int i;
for(i=0; i<nreq; i++){
if(runestrcmp(req[i].name, name) == 0){
if(i != --nreq){
free(req[i].name);
req[i] = req[nreq];
}
return;
}
}
}
void
renreq(Rune *from, Rune *to)
{
int i;
delreq(to);
for(i=0; i<nreq; i++)
if(runestrcmp(req[i].name, from) == 0){
free(req[i].name);
req[i].name = erunestrdup(to);
return;
}
}
void
addesc(Rune r, int (*f)(void), int mode)
{
Esc *e;
if(nesc >= nelem(esc)){
fprint(2, "too many escapes\n");
return;
}
e = &esc[nesc++];
e->r = r;
e->f = f;
e->mode = mode;
}
/*
* Get the next logical character in the input stream.
*/
int
getnext(void)
{
int i, r;
next:
r = getrune();
if(r < 0)
return -1;
if(r == Uformatted){
br();
assert(!isoutput);
while((r = getrune()) >= 0 && r != Uunformatted){
if(r == Uformatted)
continue;
outrune(r);
}
goto next;
}
if(r == Uunformatted)
goto next;
if(r == backslash){
r = getrune();
if(r < 0)
return -1;
for(i=0; i<nesc; i++){
if(r == esc[i].r && (inputmode&esc[i].mode)==inputmode){
if(esc[i].f == e_warn)
warn("ignoring %C%C", backslash, r);
r = esc[i].f();
if(r <= 0)
goto next;
return r;
}
}
if(inputmode&(ArgMode|CopyMode)){
ungetrune(r);
r = backslash;
}
}
return r;
}
void
ungetnext(Rune r)
{
/*
* really we want to undo the getrunes that led us here,
* since the call after ungetnext might be getrune!
*/
ungetrune(r);
}
int
_readx(Rune *p, int n, int nmode, int line)
{
int c, omode;
Rune *e;
while((c = getrune()) == ' ' || c == '\t')
;
ungetrune(c);
omode = inputmode;
inputmode = nmode;
e = p+n-1;
for(c=getnext(); p<e; c=getnext()){
if(c < 0)
break;
if(!line && (c == ' ' || c == '\t'))
break;
if(c == '\n'){
if(!line)
ungetnext(c);
break;
}
*p++ = c;
}
inputmode = omode;
*p = 0;
if(c < 0)
return -1;
return 0;
}
/*
* Get the next argument from the current line.
*/
Rune*
copyarg(void)
{
static Rune buf[MaxLine];
int c;
Rune *r;
if(_readx(buf, sizeof buf, ArgMode, 0) < 0)
return nil;
r = runestrstr(buf, L("\\\""));
if(r){
*r = 0;
while((c = getrune()) >= 0 && c != '\n')
;
ungetrune('\n');
}
r = erunestrdup(buf);
return r;
}
/*
* Read the current line in given mode. Newline not kept.
* Uses different buffer from copyarg!
*/
Rune*
readline(int m)
{
static Rune buf[MaxLine];
Rune *r;
if(_readx(buf, sizeof buf, m, 1) < 0)
return nil;
r = erunestrdup(buf);
return r;
}
/*
* Given the argument line (already read in copy+arg mode),
* parse into arguments. Note that \" has been left in place
* during copy+arg mode parsing, so comments still need to be stripped.
*/
int
parseargs(Rune *p, Rune **argv)
{
int argc;
Rune *w;
for(argc=0; argc<MAXARG; argc++){
while(*p == ' ' || *p == '\t')
p++;
if(*p == 0)
break;
argv[argc] = p;
if(*p == '"'){
/* quoted argument */
if(*(p+1) == '"'){
/* empty argument */
*p = 0;
p += 2;
}else{
/* parse quoted string */
w = p++;
for(; *p; p++){
if(*p == '"' && *(p+1) == '"')
*w++ = '"';
else if(*p == '"'){
p++;
break;
}else
*w++ = *p;
}
*w = 0;
}
}else{
/* unquoted argument - need to watch out for \" comment */
for(; *p; p++){
if(*p == ' ' || *p == '\t'){
*p++ = 0;
break;
}
if(*p == '\\' && *(p+1) == '"'){
*p = 0;
if(p != argv[argc])
argc++;
return argc;
}
}
}
}
return argc;
}
/*
* Process a dot line. The dot has been read.
*/
void
dotline(int dot)
{
int argc, i;
Rune *a, *argv[1+MAXARG];
/*
* Read request/macro name
*/
a = copyarg();
if(a == nil || a[0] == 0){
free(a);
getrune(); /* \n */
return;
}
argv[0] = a;
/*
* Check for .if, .ie, and others with special parsing.
*/
for(i=0; i<nraw; i++){
if(runestrcmp(raw[i].name, a) == 0){
raw[i].f(raw[i].name);
free(a);
return;
}
}
/*
* Read rest of line in copy mode, invoke regular request.
*/
a = readline(ArgMode);
if(a == nil){
free(argv[0]);
return;
}
argc = 1+parseargs(a, argv+1);
for(i=0; i<nreq; i++){
if(runestrcmp(req[i].name, argv[0]) == 0){
if(req[i].argc != -1){
if(argc < 1+req[i].argc){
warn("not enough arguments for %C%S", dot, req[i].name);
free(argv[0]);
free(a);
return;
}
if(argc > 1+req[i].argc)
warn("too many arguments for %C%S", dot, req[i].name);
}
req[i].f(argc, argv);
free(argv[0]);
free(a);
return;
}
}
/*
* Invoke user-defined macros.
*/
runmacro(dot, argc, argv);
free(argv[0]);
free(a);
}
/*
* newlines are magical in various ways.
*/
int bol;
void
newline(void)
{
int n;
if(bol)
sp(eval(L("1v")));
bol = 1;
if((n=getnr(L(".ce"))) > 0){
nr(L(".ce"), n-1);
br();
}
if(getnr(L(".fi")) == 0)
br();
outrune('\n');
}
void
startoutput(void)
{
char *align;
double ps, vs, lm, rm, ti;
Rune buf[200];
if(isoutput)
return;
isoutput = 1;
if(getnr(L(".paragraph")) == 0)
return;
nr(L(".ns"), 0);
isoutput = 1;
ps = getnr(L(".s"));
if(ps <= 1)
ps = 10;
ps /= 72.0;
USED(ps);
vs = getnr(L(".v"))*getnr(L(".ls")) * 1.0/UPI;
vs /= (10.0/72.0); /* ps */
if(vs == 0)
vs = 1.2;
lm = (getnr(L(".o"))+getnr(L(".i"))) * 1.0/UPI;
ti = getnr(L(".ti")) * 1.0/UPI;
nr(L(".ti"), 0);
rm = 8.0 - getnr(L(".l"))*1.0/UPI - getnr(L(".o"))*1.0/UPI;
if(rm < 0)
rm = 0;
switch(getnr(L(".j"))){
default:
case 0:
align = "left";
break;
case 1:
align = "justify";
break;
case 3:
align = "center";
break;
case 5:
align = "right";
break;
}
if(getnr(L(".ce")))
align = "center";
if(!getnr(L(".margin")))
runesnprint(buf, nelem(buf), "<p style=\"line-height: %.1fem; text-indent: %.2fin; margin-top: 0; margin-bottom: 0; text-align: %s;\">\n",
vs, ti, align);
else
runesnprint(buf, nelem(buf), "<p style=\"line-height: %.1fem; margin-left: %.2fin; text-indent: %.2fin; margin-right: %.2fin; margin-top: 0; margin-bottom: 0; text-align: %s;\">\n",
vs, lm, ti, rm, align);
outhtml(buf);
}
void
br(void)
{
if(!isoutput)
return;
isoutput = 0;
nr(L(".dv"), 0);
dv(0);
hideihtml();
if(getnr(L(".paragraph")))
outhtml(L("</p>"));
}
void
r_margin(int argc, Rune **argv)
{
USED(argc);
nr(L(".margin"), eval(argv[1]));
}
int inrequest;
void
runinput(void)
{
int c;
bol = 1;
for(;;){
c = getnext();
if(c < 0)
break;
if((c == dot || c == tick) && bol){
inrequest = 1;
dotline(c);
bol = 1;
inrequest = 0;
}else if(c == '\n'){
newline();
itrap();
linepos = 0;
}else{
outtrap();
startoutput();
showihtml();
if(c == '\t'){
/* XXX do better */
outrune(' ');
while(++linepos%4)
outrune(' ');
}else{
outrune(c);
linepos++;
}
bol = 0;
}
}
}
void
run(void)
{
t1init();
t2init();
t3init();
t4init();
t5init();
t6init();
t7init();
t8init();
/* t9init(); t9.c */
t10init();
t11init();
/* t12init(); t12.c */
t13init();
t14init();
t15init();
t16init();
t17init();
t18init();
t19init();
t20init();
htmlinit();
hideihtml();
addreq(L("margin"), r_margin, 1);
nr(L(".margin"), 1);
nr(L(".paragraph"), 1);
runinput();
while(popinput())
;
dot = '.';
if(verbose)
fprint(2, "eof\n");
runmacro1(L("eof"));
closehtml();
}
void
out(Rune *s)
{
if(s == nil)
return;
for(; *s; s++)
outrune(*s);
}
void (*outcb)(Rune);
void
inroman(Rune r)
{
int f;
f = getnr(L(".f"));
nr(L(".f"), 1);
runmacro1(L("font"));
outrune(r);
nr(L(".f"), f);
runmacro1(L("font"));
}
void
Brune(Rune r)
{
if(r == '&')
Bprint(&bout, "&");
else if(r == '<')
Bprint(&bout, "<");
else if(r == '>')
Bprint(&bout, ">");
else if(r < Runeself || utf8)
Bprint(&bout, "%C", r);
else
Bprint(&bout, "%S", rune2html(r));
}
void
outhtml(Rune *s)
{
Rune r;
for(; *s; s++){
switch(r = *s){
case '<':
r = Ult;
break;
case '>':
r = Ugt;
break;
case '&':
r = Uamp;
break;
case ' ':
r = Uspace;
break;
}
outrune(r);
}
}
void
outrune(Rune r)
{
switch(r){
case ' ':
if(getnr(L(".fi")) == 0)
r = Unbsp;
break;
case Uformatted:
case Uunformatted:
abort();
}
if(outcb){
if(r == ' ')
r = Uspace;
outcb(r);
return;
}
/* writing to bout */
switch(r){
case Uempty:
return;
case Upl:
inroman('+');
return;
case Ueq:
inroman('=');
return;
case Umi:
inroman(0x2212);
return;
case Utick:
r = '\'';
break;
case Ubtick:
r = '`';
break;
case Uminus:
r = '-';
break;
case '\'':
Bprint(&bout, "’");
return;
case '`':
Bprint(&bout, "‘");
return;
case Uamp:
Bputrune(&bout, '&');
return;
case Ult:
Bputrune(&bout, '<');
return;
case Ugt:
Bputrune(&bout, '>');
return;
case Uspace:
Bputrune(&bout, ' ');
return;
case 0x2032:
/*
* In Firefox, at least, the prime is not
* a superscript by default.
*/
Bprint(&bout, "<sup>");
Brune(r);
Bprint(&bout, "</sup>");
return;
}
Brune(r);
}
void
r_nop(int argc, Rune **argv)
{
USED(argc);
USED(argv);
}
void
r_warn(int argc, Rune **argv)
{
USED(argc);
warn("ignoring %C%S", dot, argv[0]);
}
int
e_warn(void)
{
/* dispatch loop prints a warning for us */
return 0;
}
int
e_nop(void)
{
return 0;
}
|