-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathman2html.c
More file actions
4088 lines (3901 loc) · 82.8 KB
/
man2html.c
File metadata and controls
4088 lines (3901 loc) · 82.8 KB
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program was written by Richard Verhoeven (NL:5482ZX35)
* at the Eindhoven University of Technology. Email: rcb5@win.tue.nl
*
* Permission is granted to distribute, modify and use this program as long
* as this comment is not removed or changed.
*
* THIS IS A MODIFIED VERSION. IT WAS MODIFIED BY chet@po.cwru.edu FOR
* USE BY BASH.
*/
/*
* man2html will add links to the converted manpages. The function add_links
* is used for that. At the moment it will add links as follows, where
* indicates what should match to start with:
* ^^^
* Recognition Item Link
* ----------------------------------------------------------
* name(*) Manpage ../man?/name.*
* ^
* name@hostname Email address mailto:name@hostname
* ^
* method://string URL method://string
* ^^^
* www.host.name WWW server http://www.host.name
* ^^^^
* ftp.host.name FTP server ftp://ftp.host.name
* ^^^^
* <file.h> Include file file:/usr/include/file.h
* ^^^
*
* Since man2html does not check if manpages, hosts or email addresses exist,
* some links might not work. For manpages, some extra checks are performed
* to make sure not every () pair creates a link. Also out of date pages
* might point to incorrect places.
*
* The program will not allow users to get system specific files, such as
* /etc/passwd. It will check that "man" is part of the specified file and
* that "/../" isn't. Even if someone manages to get such file, man2html will
* handle it like a manpage and will usually not produce any output (or crash).
*
* If you find any bugs when normal manpages are converted, please report
* them to me (rcb5@win.tue.nl) after you have checked that man(1) can handle
* the manpage correct.
*
* Known bugs and missing features:
*
* * Equations are not converted at all.
* * Tables are converted but some features are not possible in html.
* * The tabbing environment is converted by counting characters and adding
* spaces. This might go wrong (outside <PRE>)
* * Some pages look beter if man2html works in troff mode, especially pages
* with tables. You can deside at compile time which made you want to use.
*
* -DNROFF=0 troff mode
* -DNROFF=1 nroff mode (default)
*
* if you install both modes, you should compile with the correct CGIBASE.
* * Some manpages rely on the fact that troff/nroff is used to convert
* them and use features which are not descripted in the man manpages.
* (definitions, calculations, conditionals, requests). I can't guarantee
* that all these features work on all manpages. (I didn't have the
* time to look through all the available manpages.)
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define NROFF 0
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <ctype.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#define NULL_TERMINATED(n) ((n) + 1)
#define HUGE_STR_MAX 10000
#define LARGE_STR_MAX 2000
#define MED_STR_MAX 500
#define SMALL_STR_MAX 100
#define TINY_STR_MAX 10
#define MAX_MAN_PATHS 100 /* Max number of directories */
#define MAX_ZCATS 10 /* Max number of zcat style programs */
#define MAX_WORDLIST 100
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#ifndef EXIT_USAGE
#define EXIT_USAGE 2
#endif
static char location_base[NULL_TERMINATED(MED_STR_MAX)] = "";
static char th_page_and_sec[128] = { '\0' };
static char th_datestr[128] = { '\0' };
static char th_version[128] = { '\0' };
char *signature = "<HR>\nThis document was created by man2html from %s.<BR>\nTime: %s\n";
/* timeformat for signature */
#define TIMEFORMAT "%d %B %Y %T %Z"
char *manpage;
/* BSD mandoc Bl/El lists to HTML list types */
#define BL_DESC_LIST 1
#define BL_BULLET_LIST 2
#define BL_ENUM_LIST 4
/* BSD mandoc Bd/Ed example(?) blocks */
#define BD_LITERAL 1
#define BD_INDENT 2
#ifndef HAVE_STRERROR
static char *
strerror(int e)
{
static char emsg[40];
#if defined (HAVE_SYS_ERRLIST)
extern int sys_nerr;
extern char *sys_errlist[];
if (e > 0 && e < sys_nerr)
return (sys_errlist[e]);
else
#endif /* HAVE_SYS_ERRLIST */
{
sprintf(emsg, "Unknown system error %d", e);
return (&emsg[0]);
}
}
#endif /* !HAVE_STRERROR */
static char *
strgrow(char *old, int len)
{
char *new = realloc(old, (strlen(old) + len + 1) * sizeof(char));
if (!new) {
fprintf(stderr, "man2html: out of memory");
exit(EXIT_FAILURE);
}
return new;
}
static char *
stralloc(int len)
{
/* allocate enough for len + NULL */
char *new = malloc((len + 1) * sizeof(char));
if (!new) {
fprintf(stderr, "man2html: out of memory");
exit(EXIT_FAILURE);
}
return new;
}
void *
xmalloc (size_t size)
{
void *ret;
ret = malloc (size);
if (ret == 0) {
fprintf(stderr, "man2html: out of memory");
exit(EXIT_FAILURE);
}
return ret;
}
/*
* Some systems don't have strdup so lets use our own - which can also
* check for out of memory.
*/
static char *
strduplicate(char *from)
{
char *new = stralloc(strlen(from));
strcpy(new, from);
return new;
}
/* Assumes space for n plus a null */
static char *
strmaxcpy(char *to, char *from, int n)
{
int len = strlen(from);
strncpy(to, from, n);
to[(len <= n) ? len : n] = '\0';
return to;
}
static char *
strmaxcat(char *to, char *from, int n)
{
int to_len = strlen(to);
if (to_len < n) {
int from_len = strlen(from);
int cp = (to_len + from_len <= n) ? from_len : n - to_len;
strncpy(to + to_len, from, cp);
to[to_len + cp] = '\0';
}
return to;
}
/* Assumes space for limit plus a null */
static char *
strlimitcpy(char *to, char *from, int n, int limit)
{
int len = n > limit ? limit : n;
strmaxcpy(to, from, len);
to[len] = '\0';
return to;
}
/*
* takes string and escapes all metacharacters. should be used before
* including string in system() or similar call.
*/
static char *
escape_input(char *str)
{
int i, j = 0;
static char new[NULL_TERMINATED(MED_STR_MAX)];
if (strlen(str) * 2 + 1 > MED_STR_MAX) {
fprintf(stderr,
"man2html: escape_input - str too long:\n%-80s...\n",
str);
exit(EXIT_FAILURE);
}
for (i = 0; i < strlen(str); i++) {
if (!(((str[i] >= 'A') && (str[i] <= 'Z')) ||
((str[i] >= 'a') && (str[i] <= 'z')) ||
((str[i] >= '0') && (str[i] <= '9')))) {
new[j] = '\\';
j++;
}
new[j] = str[i];
j++;
}
new[j] = '\0';
return new;
}
static void
usage(void)
{
fprintf(stderr, "man2html: usage: man2html filename\n");
}
/*
* below this you should not change anything unless you know a lot
* about this program or about troff.
*/
typedef struct STRDEF STRDEF;
struct STRDEF {
int nr, slen;
char *st;
STRDEF *next;
};
typedef struct INTDEF INTDEF;
struct INTDEF {
int nr;
int val;
int incr;
INTDEF *next;
};
static char NEWLINE[2] = "\n";
static char idxlabel[6] = "ixAAA";
#define INDEXFILE "/tmp/manindex.list"
static char *fname;
static FILE *idxfile;
static STRDEF *chardef, *strdef, *defdef;
static INTDEF *intdef;
#define V(A,B) ((A)*256+(B))
static INTDEF standardint[] = {
{V('n', ' '), NROFF, 0, NULL},
{V('t', ' '), 1 - NROFF, 0, NULL},
{V('o', ' '), 1, 0, NULL},
{V('e', ' '), 0, 0, NULL},
{V('.', 'l'), 70, 0, NULL},
{V('.', '$'), 0, 0, NULL},
{V('.', 'A'), NROFF, 0, NULL},
{V('.', 'T'), 1 - NROFF, 0, NULL},
{V('.', 'V'), 1, 0, NULL}, /* the me package tests for this */
{0, 0, 0, NULL}};
static STRDEF standardstring[] = {
{V('R', ' '), 1, "®", NULL},
{V('l', 'q'), 2, "``", NULL},
{V('r', 'q'), 2, "''", NULL},
{0, 0, NULL, NULL}
};
static STRDEF standardchar[] = {
{V('*', '*'), 1, "*", NULL},
{V('*', 'A'), 1, "A", NULL},
{V('*', 'B'), 1, "B", NULL},
{V('*', 'C'), 2, "Xi", NULL},
{V('*', 'D'), 5, "Delta", NULL},
{V('*', 'E'), 1, "E", NULL},
{V('*', 'F'), 3, "Phi", NULL},
{V('*', 'G'), 5, "Gamma", NULL},
{V('*', 'H'), 5, "Theta", NULL},
{V('*', 'I'), 1, "I", NULL},
{V('*', 'K'), 1, "K", NULL},
{V('*', 'L'), 6, "Lambda", NULL},
{V('*', 'M'), 1, "M", NULL},
{V('*', 'N'), 1, "N", NULL},
{V('*', 'O'), 1, "O", NULL},
{V('*', 'P'), 2, "Pi", NULL},
{V('*', 'Q'), 3, "Psi", NULL},
{V('*', 'R'), 1, "P", NULL},
{V('*', 'S'), 5, "Sigma", NULL},
{V('*', 'T'), 1, "T", NULL},
{V('*', 'U'), 1, "Y", NULL},
{V('*', 'W'), 5, "Omega", NULL},
{V('*', 'X'), 1, "X", NULL},
{V('*', 'Y'), 1, "H", NULL},
{V('*', 'Z'), 1, "Z", NULL},
{V('*', 'a'), 5, "alpha", NULL},
{V('*', 'b'), 4, "beta", NULL},
{V('*', 'c'), 2, "xi", NULL},
{V('*', 'd'), 5, "delta", NULL},
{V('*', 'e'), 7, "epsilon", NULL},
{V('*', 'f'), 3, "phi", NULL},
{V('*', 'g'), 5, "gamma", NULL},
{V('*', 'h'), 5, "theta", NULL},
{V('*', 'i'), 4, "iota", NULL},
{V('*', 'k'), 5, "kappa", NULL},
{V('*', 'l'), 6, "lambda", NULL},
{V('*', 'm'), 1, "µ", NULL},
{V('*', 'n'), 2, "nu", NULL},
{V('*', 'o'), 1, "o", NULL},
{V('*', 'p'), 2, "pi", NULL},
{V('*', 'q'), 3, "psi", NULL},
{V('*', 'r'), 3, "rho", NULL},
{V('*', 's'), 5, "sigma", NULL},
{V('*', 't'), 3, "tau", NULL},
{V('*', 'u'), 7, "upsilon", NULL},
{V('*', 'w'), 5, "omega", NULL},
{V('*', 'x'), 3, "chi", NULL},
{V('*', 'y'), 3, "eta", NULL},
{V('*', 'z'), 4, "zeta", NULL},
{V('t', 's'), 5, "sigma", NULL},
{V('+', '-'), 1, "±", NULL},
{V('1', '2'), 1, "½", NULL},
{V('1', '4'), 1, "¼", NULL},
{V('3', '4'), 1, "¾", NULL},
{V('F', 'i'), 3, "ffi", NULL},
{V('F', 'l'), 3, "ffl", NULL},
{V('a', 'a'), 1, "´", NULL},
{V('a', 'p'), 1, "~", NULL},
{V('b', 'r'), 1, "|", NULL},
{V('b', 'u'), 1, "*", NULL},
{V('b', 'v'), 1, "|", NULL},
{V('c', 'i'), 1, "o", NULL},
{V('c', 'o'), 1, "©", NULL},
{V('c', 't'), 1, "¢", NULL},
{V('d', 'e'), 1, "°", NULL},
{V('d', 'g'), 1, "+", NULL},
{V('d', 'i'), 1, "÷", NULL},
{V('e', 'm'), 1, "-", NULL},
{V('e', 'm'), 3, "---", NULL},
{V('e', 'q'), 1, "=", NULL},
{V('e', 's'), 1, "Ø", NULL},
{V('f', 'f'), 2, "ff", NULL},
{V('f', 'i'), 2, "fi", NULL},
{V('f', 'l'), 2, "fl", NULL},
{V('f', 'm'), 1, "´", NULL},
{V('g', 'a'), 1, "`", NULL},
{V('h', 'y'), 1, "-", NULL},
{V('l', 'c'), 2, "|¯", NULL},
{V('l', 'f'), 2, "|_", NULL},
{V('l', 'k'), 1, "<FONT SIZE=+2>{</FONT>", NULL},
{V('m', 'i'), 1, "-", NULL},
{V('m', 'u'), 1, "×", NULL},
{V('n', 'o'), 1, "¬", NULL},
{V('o', 'r'), 1, "|", NULL},
{V('p', 'l'), 1, "+", NULL},
{V('r', 'c'), 2, "¯|", NULL},
{V('r', 'f'), 2, "_|", NULL},
{V('r', 'g'), 1, "®", NULL},
{V('r', 'k'), 1, "<FONT SIZE=+2>}</FONT>", NULL},
{V('r', 'n'), 1, "¯", NULL},
{V('r', 'u'), 1, "_", NULL},
{V('s', 'c'), 1, "§", NULL},
{V('s', 'l'), 1, "/", NULL},
{V('s', 'q'), 2, "[]", NULL},
{V('u', 'l'), 1, "_", NULL},
{0, 0, NULL, NULL}
};
/* default: print code */
static char eqndelimopen = 0, eqndelimclose = 0;
static char escapesym = '\\', nobreaksym = '\'', controlsym = '.', fieldsym = 0, padsym = 0;
static char *buffer = NULL;
static int buffpos = 0, buffmax = 0;
static int scaninbuff = 0;
static int itemdepth = 0;
static int dl_set[20] = {0};
static int still_dd = 0;
static int tabstops[20] = {8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96};
static int maxtstop = 12;
static int curpos = 0;
static char *scan_troff(char *c, int san, char **result);
static char *scan_troff_mandoc(char *c, int san, char **result);
static char **argument = NULL;
static char charb[TINY_STR_MAX];
static void
print_sig(void)
{
char datbuf[NULL_TERMINATED(MED_STR_MAX)];
struct tm *timetm;
time_t clock;
datbuf[0] = '\0';
clock = time(NULL);
timetm = localtime(&clock);
strftime(datbuf, MED_STR_MAX, TIMEFORMAT, timetm);
printf(signature, manpage, datbuf);
}
static char *
expand_char(int nr)
{
STRDEF *h;
h = chardef;
if (!nr)
return NULL;
while (h)
if (h->nr == nr) {
curpos += h->slen;
return h->st;
} else
h = h->next;
charb[0] = nr / 256;
charb[1] = nr % 256;
charb[2] = '\0';
if (charb[0] == '<') { /* Fix up <= */
charb[4] = charb[1];
strncpy(charb, "<", 4);
charb[5] = '\0';
}
curpos += 2;
return charb;
}
static char *
expand_string(int nr)
{
STRDEF *h = strdef;
if (!nr)
return NULL;
while (h)
if (h->nr == nr) {
curpos += h->slen;
return h->st;
} else
h = h->next;
return NULL;
}
static char *
read_man_page(char *filename)
{
char *man_buf = NULL;
int i;
FILE *man_stream = NULL;
struct stat stbuf;
int buf_size;
if (stat(filename, &stbuf) == -1)
return NULL;
buf_size = stbuf.st_size;
man_buf = stralloc(buf_size + 5);
man_stream = fopen(filename, "r");
if (man_stream) {
man_buf[0] = '\n';
if (fread(man_buf + 1, 1, buf_size, man_stream) == buf_size) {
man_buf[buf_size] = '\n';
man_buf[buf_size + 1] = man_buf[buf_size + 2] = '\0';
} else {
man_buf = NULL;
}
fclose(man_stream);
}
return man_buf;
}
static char outbuffer[NULL_TERMINATED(HUGE_STR_MAX)];
static int obp = 0;
static int no_newline_output = 0;
static int newline_for_fun = 0;
static int output_possible = 0;
static int out_length = 0;
/*
* Add the links to the output. At the moment the following are
* recognized:
*
#if 0
* name(*) -> ../man?/name.*
#endif
* method://string -> method://string
* www.host.name -> http://www.host.name
* ftp.host.name -> ftp://ftp.host.name
* name@host -> mailto:name@host
* <name.h> -> file:/usr/include/name.h (guess)
*
* Other possible links to add in the future:
*
* /dir/dir/file -> file:/dir/dir/file
*/
static void
add_links(char *c)
{
int i, j, nr;
char *f, *g, *h;
char *idtest[6]; /* url, mailto, www, ftp, manpage */
out_length += strlen(c);
/* search for (section) */
nr = 0;
idtest[0] = strstr(c + 1, "://");
idtest[1] = strchr(c + 1, '@');
idtest[2] = strstr(c, "www.");
idtest[3] = strstr(c, "ftp.");
#if 0
idtest[4] = strchr(c + 1, '(');
#else
idtest[4] = 0;
#endif
idtest[5] = strstr(c + 1, ".h>");
for (i = 0; i < 6; i++)
nr += (idtest[i] != NULL);
while (nr) {
j = -1;
for (i = 0; i < 6; i++)
if (idtest[i] && (j < 0 || idtest[i] < idtest[j]))
j = i;
switch (j) {
case 5: /* <name.h> */
f = idtest[5];
h = f + 2;
g = f;
while (g > c && g[-1] != ';')
g--;
if (g != c) {
char t;
t = *g;
*g = '\0';
fputs(c, stdout);
*g = t;
*h = '\0';
printf("<A HREF=\"file:/usr/include/%s\">%s</A>>", g, g);
c = f + 6;
} else {
f[5] = '\0';
fputs(c, stdout);
f[5] = ';';
c = f + 5;
}
break;
case 4: /* manpage */
#if 0
f = idtest[j];
/* check section */
g = strchr(f, ')');
if (g && f - g < 6 && (isalnum(f[-1]) || f[-1] == '>') &&
((isdigit(f[1]) && f[1] != '0' &&
(f[2] == ')' || (isalpha(f[2]) && f[3] == ')') || f[2] == 'X')) ||
(f[2] == ')' && (f[1] == 'n' || f[1] == 'l')))) {
/* this might be a link */
h = f - 1;
/* skip html makeup */
while (h > c && *h == '>') {
while (h != c && *h != '<')
h--;
if (h != c)
h--;
}
if (isalnum(*h)) {
char t, sec, subsec, *e;
e = h + 1;
sec = f[1];
subsec = f[2];
if ((subsec == 'X' && f[3] != ')') || subsec == ')')
subsec = '\0';
while (h > c && (isalnum(h[-1]) || h[-1] == '_' ||
h[-1] == '-' || h[-1] == '.'))
h--;
t = *h;
*h = '\0';
fputs(c, stdout);
*h = t;
t = *e;
*e = '\0';
if (subsec)
printf("<A HREF=\""
CGIBASE
"?man%c/%s.%c%c\">%s</A>",
sec, h, sec, tolower(subsec), h);
else
printf("<A HREF=\""
CGIBASE
"?man%c/%s.%c\">%s</A>",
sec, h, sec, h);
*e = t;
c = e;
}
}
*f = '\0';
fputs(c, stdout);
*f = '(';
idtest[4] = f - 1;
c = f;
#endif
break; /* manpage */
case 3: /* ftp */
case 2: /* www */
g = f = idtest[j];
while (*g && (isalnum(*g) || *g == '_' || *g == '-' || *g == '+' ||
*g == '.'))
g++;
if (g[-1] == '.')
g--;
if (g - f > 4) {
char t;
t = *f;
*f = '\0';
fputs(c, stdout);
*f = t;
t = *g;
*g = '\0';
printf("<A HREF=\"%s://%s\">%s</A>", (j == 3 ? "ftp" : "http"),
f, f);
*g = t;
c = g;
} else {
f[3] = '\0';
fputs(c, stdout);
c = f + 3;
f[3] = '.';
}
break;
case 1: /* mailto */
g = f = idtest[1];
while (g > c && (isalnum(g[-1]) || g[-1] == '_' || g[-1] == '-' ||
g[-1] == '+' || g[-1] == '.' || g[-1] == '%'))
g--;
h = f + 1;
while (*h && (isalnum(*h) || *h == '_' || *h == '-' || *h == '+' ||
*h == '.'))
h++;
if (*h == '.')
h--;
if (h - f > 4 && f - g > 1) {
char t;
t = *g;
*g = '\0';
fputs(c, stdout);
*g = t;
t = *h;
*h = '\0';
printf("<A HREF=\"mailto:%s\">%s</A>", g, g);
*h = t;
c = h;
} else {
*f = '\0';
fputs(c, stdout);
*f = '@';
idtest[1] = c;
c = f;
}
break;
case 0: /* url */
g = f = idtest[0];
while (g > c && isalpha(g[-1]) && islower(g[-1]))
g--;
h = f + 3;
while (*h && !isspace(*h) && *h != '<' && *h != '>' && *h != '"' &&
*h != '&')
h++;
if (f - g > 2 && f - g < 7 && h - f > 3) {
char t;
t = *g;
*g = '\0';
fputs(c, stdout);
*g = t;
t = *h;
*h = '\0';
printf("<A HREF=\"%s\">%s</A>", g, g);
*h = t;
c = h;
} else {
f[1] = '\0';
fputs(c, stdout);
f[1] = '/';
c = f + 1;
}
break;
default:
break;
}
nr = 0;
if (idtest[0] && idtest[0] < c)
idtest[0] = strstr(c + 1, "://");
if (idtest[1] && idtest[1] < c)
idtest[1] = strchr(c + 1, '@');
if (idtest[2] && idtest[2] < c)
idtest[2] = strstr(c, "www.");
if (idtest[3] && idtest[3] < c)
idtest[3] = strstr(c, "ftp.");
if (idtest[4] && idtest[4] < c)
idtest[4] = strchr(c + 1, '(');
if (idtest[5] && idtest[5] < c)
idtest[5] = strstr(c + 1, ".h>");
for (i = 0; i < 6; i++)
nr += (idtest[i] != NULL);
}
fputs(c, stdout);
}
static int current_font = 0;
static int current_size = 0;
static int fillout = 1;
static void
out_html(char *c)
{
if (!c)
return;
if (no_newline_output) {
int i = 0;
no_newline_output = 1;
while (c[i]) {
if (!no_newline_output)
c[i - 1] = c[i];
if (c[i] == '\n')
no_newline_output = 1;
i++;
}
if (!no_newline_output)
c[i - 1] = 0;
}
if (scaninbuff) {
while (*c) {
if (buffpos >= buffmax) {
char *h;
h = realloc(buffer, buffmax * 2);
if (!h)
return;
buffer = h;
buffmax *= 2;
}
buffer[buffpos++] = *c++;
}
} else if (output_possible) {
while (*c) {
outbuffer[obp++] = *c;
if (*c == '\n' || obp > HUGE_STR_MAX) {
outbuffer[obp] = '\0';
add_links(outbuffer);
obp = 0;
}
c++;
}
}
}
#define FO0 ""
#define FC0 ""
#define FO1 "<I>"
#define FC1 "</I>"
#define FO2 "<B>"
#define FC2 "</B>"
#define FO3 "<TT>"
#define FC3 "</TT>"
static char *switchfont[16] = {
"", FC0 FO1, FC0 FO2, FC0 FO3,
FC1 FO0, "", FC1 FO2, FC1 FO3,
FC2 FO0, FC2 FO1, "", FC2 FO3,
FC3 FO0, FC3 FO1, FC3 FO2, ""
};
static char *
change_to_font(int nr)
{
int i;
switch (nr) {
case '0':
nr++;
case '1':
case '2':
case '3':
case '4':
nr = nr - '1';
break;
case V('C', 'W'):
nr = 3;
break;
case 'L':
nr = 3;
break;
case 'B':
nr = 2;
break;
case 'I':
nr = 1;
break;
case 'P':
case 'R':
nr = 0;
break;
case 0:
case 1:
case 2:
case 3:
break;
default:
nr = 0;
break;
}
i = current_font * 4 + nr % 4;
current_font = nr % 4;
return switchfont[i];
}
static char sizebuf[200];
static char *
change_to_size(int nr)
{
int i;
switch (nr) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
nr = nr - '0';
break;
case '\0':
break;
default:
nr = current_size + nr;
if (nr > 9)
nr = 9;
if (nr < -9)
nr = -9;
break;
}
if (nr == current_size)
return "";
i = current_font;
sizebuf[0] = '\0';
strcat(sizebuf, change_to_font(0));
if (current_size)
strcat(sizebuf, "</FONT>");
current_size = nr;
if (nr) {
int l;
strcat(sizebuf, "<FONT SIZE=");
l = strlen(sizebuf);
if (nr > 0)
sizebuf[l++] = '+';
else
sizebuf[l++] = '-', nr = -nr;
sizebuf[l++] = nr + '0';
sizebuf[l++] = '>';
sizebuf[l] = '\0';
}
strcat(sizebuf, change_to_font(i));
return sizebuf;
}
static int asint = 0;
static int intresult = 0;
#define SKIPEOL while (*c && *c++!='\n')
static int skip_escape = 0;
static int single_escape = 0;
static char *
scan_escape(char *c)
{
char *h = NULL;
char b[5];
INTDEF *intd;
int exoutputp, exskipescape;
int i, j;
intresult = 0;
switch (*c) {
case 'e':
h = "\\";
curpos++;
break;
case '0':
case ' ':
h = " ";
curpos++;
break;
case '|':
h = "";
break;
case '"':
SKIPEOL;
c--;
h = "";
break;
case '$':
if (argument) {
c++;
i = (*c - '1');
if (!(h = argument[i]))
h = "";
}
break;
case 'z':
c++;
if (*c == '\\') {
c = scan_escape(c + 1);
c--;
h = "";
} else {
b[0] = *c;
b[1] = '\0';
h = "";
}
break;
case 'k':
c++;
if (*c == '(')
c += 2;
case '^':
case '!':
case '%':
case 'a':
case 'd':