OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / doc / utils / man_xref.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <assert.h>
4
5 /*
6         look through HTMLized man pages
7         convert references like man(1) into HTML links
8
9         somewhat quick & dirty code
10         various dubious assumptions made:
11
12         [a-zA-Z0-9\-_\.]* defines legal characters in name
13         pagename(x) corresponds to pagename.x.html
14         (Fine *if* it's been converted by my scripts)
15         x in the above must be a single digit
16         (or we ignore it, which does no damage)
17         Lazy parsing: malloc() enough RAM to read in whole file
18         Limited syntax: exactly one input file, results to stdout 
19
20         Sandy Harris
21 */
22
23 int do_file( char *, char *) ;
24
25 main(int argc, char **argv)
26 {
27         FILE *in ;
28         char *progname;
29         long lsize ;
30         size_t size, nread;
31         char *buffer, *bufend ; 
32         progname = *argv ;
33         if( argc != 2 ) {
34                 fprintf(stderr,"usage: %s input-file\n", progname);
35                 exit(1) ;
36         }
37         if( (in = fopen(argv[1],"r")) == NULL ) {
38                 fprintf(stderr,"%s Can't open input file\n", progname);
39                 exit(2) ;
40         }
41         if( (lsize = fseek(in, 0L, SEEK_END)) < 0L )    {
42                 fprintf(stderr,"%s fseek() fails\n", progname);
43                 exit(3) ;
44         }
45         lsize = ftell(in) ;
46         rewind(in) ;
47         size = (size_t) lsize ;
48         if( lsize != (long) size )      {
49                 fprintf(stderr,"%s file too large\n", progname);
50                 exit(4) ;
51         }
52         if( (buffer = (char *) malloc(size)) == NULL)   {
53                 fprintf(stderr,"%s malloc() failed\n", progname);
54                 exit(5) ;
55         }
56         bufend = buffer + size ;
57         if( (nread = fread(buffer, size, 1, in)) != 1) { 
58                 fprintf(stderr,"%s fread() failed\n", progname);
59                 exit(6) ;
60         }
61         do_file(buffer,bufend);
62 }
63
64 do_file(char *start, char *end)
65 {
66         /* p is where to start parsing, one past last output    */
67         /* q is how far we've parsed                            */
68         char *p, *q ;
69         int value ;
70         for( p = q = start ; p < end ; q = (q<end) ? (q+1) : q  )       {
71                 /* if p is beyond q, catch up   */
72                 if( q < p )
73                         continue ;
74                 /* move q ahead until we know if we've got manpage name */
75                 if( isalnum(*q) )
76                         continue ;
77                 switch(*q)      {
78                         /* can appear in manpage name   */
79                         case '.':
80                         case '_':
81                         case '-':
82                         case '(':
83                                 continue ;
84                                 break ;
85                         /* whatever's between p and q
86                            is not a manpage name
87                            so output it
88                         */
89                         default:
90                                 /* leave p one past output      */
91                                 for( ; p <= q ; p++ )
92                                         putchar(*p);
93                                 break ;
94                         /* we may have a manpage name   */
95                         case ')':
96                                 value = do_name(p,q);
97                                 if(value)       {
98                                         p = q ;
99                                         p++ ;
100                                 }
101                                 /* unreached with current do_name() */
102                                 else
103                                         for( ; p <= q ; p++ )
104                                                 putchar(*p);
105                                 break ;
106 }       }       }
107
108 do_name(char *p, char *q)
109 {
110         *q = '\0' ;
111         /* if end of string matches RE ([0-9])
112            with at least one legal character before it
113            add HTML xref stuff
114         */
115         if( (q-p > 3) && isdigit(q[-1]) && (q[-2]=='('))        {
116                 q[-2] = '\0' ;
117                 q-- ;  
118                 printf("<a href=\"%s.%s.html\">", p, q);
119                 printf("%s(%s)", p, q);
120                 printf("</a>");
121         }
122         // otherwise just print string
123         else    printf("%s)", p);
124         return 1 ;
125 }