Lines 1-185
Link Here
|
1 |
--- c2html.l.orig Mon Sep 1 21:18:51 1997 |
|
|
2 |
+++ c2html.l Sun Feb 21 20:03:48 1999 |
3 |
@@ -1,15 +1,22 @@ |
4 |
-%{ |
5 |
+%{ /* $Id: patch-ad,v 1.1 1999/02/22 04:22:23 steve Exp $ -*- mode: C -*- */ |
6 |
/* |
7 |
* We build a lexical analyzer that converts a C source file to |
8 |
* a beautifully highlighted HTML file now. |
9 |
*/ |
10 |
|
11 |
+#define C2HTML_VERSION "0.2" |
12 |
+ |
13 |
#include <stdio.h> |
14 |
-#include <malloc.h> |
15 |
+#include <stdlib.h> |
16 |
#include <string.h> |
17 |
+#include <unistd.h> |
18 |
|
19 |
#include "colors.h" |
20 |
|
21 |
+#ifndef COMPRESSION |
22 |
+#define COMPRESSION 0 |
23 |
+#endif |
24 |
+ |
25 |
FILE *actin; |
26 |
FILE *actout; |
27 |
|
28 |
@@ -293,51 +300,113 @@ |
29 |
* path this file) into the output file. |
30 |
*/ |
31 |
|
32 |
+static char *prog; |
33 |
+static void usage() { |
34 |
+ fprintf (stderr, "usage: %s [-t title] [-w width] [file_to_convert ...]\n", prog); |
35 |
+ exit(3); |
36 |
+} |
37 |
+ |
38 |
+ |
39 |
int |
40 |
main(int argc, char *argv[]) |
41 |
{ |
42 |
char *outfilename; |
43 |
- int i = 1; |
44 |
+ int i, ch, width=80, rc=0; |
45 |
+ char *title=(char *)0, *pt; |
46 |
+ char gzipcmd[10]; |
47 |
+ |
48 |
+ prog = (pt=strrchr(argv[0], '/')) ? ++pt : argv[0]; /* basename */ |
49 |
+ while ((ch = getopt(argc, argv, "h?t:w:V")) != -1) |
50 |
+ switch(ch) { |
51 |
+ case 't': |
52 |
+ title = optarg; |
53 |
+ break; |
54 |
+ case 'w': |
55 |
+ if (sscanf(optarg, "%d", &width) != 1) |
56 |
+ usage(); |
57 |
+ break; |
58 |
+ case 'V': |
59 |
+ fprintf(stderr, "c2html version %s%s\n", |
60 |
+ C2HTML_VERSION, COMPRESSION ? "" : " (no compression)"); |
61 |
+ exit(0); |
62 |
+ default: |
63 |
+ usage(); |
64 |
+ } |
65 |
+ argc -= optind; |
66 |
+ argv += optind; |
67 |
|
68 |
- if (1 == argc) |
69 |
- { |
70 |
+ if (argc == 0) { |
71 |
actin = stdin; |
72 |
actout = stdout; |
73 |
- fprintf (actout, "<HTML>\n<HEAD>\n"); |
74 |
- fprintf (actout, "<TITLE>stdout" ); |
75 |
+ |
76 |
+ if ((pt=getenv("PATH_TRANSLATED")) && getenv("GATEWAY_INTERFACE")) { |
77 |
+ /* CGI */ |
78 |
+ if (!title) |
79 |
+ title = pt; |
80 |
+ actin = fopen (pt, "r"); |
81 |
+ if (!actin) { |
82 |
+ fprintf (actout, "Content-Type: text/html\n\n"); |
83 |
+ fprintf (actout, "<HTML><HEAD><TITLE>error in CGI '%s'\ |
84 |
+</TITLE></HEAD>\n\ |
85 |
+<BODY><H1>error in CGI program '%s': cannot read file '%s'</H1></BODY>\n\ |
86 |
+</HTML>\n", prog, prog, pt); |
87 |
+ exit(1); |
88 |
+ } |
89 |
+ fprintf (actout, "Content-Type: text/html\n"); |
90 |
+#if COMPRESSION |
91 |
+ /* should we compress output with gzip? */ |
92 |
+ do { |
93 |
+ if (!(pt=getenv("HTTP_ACCEPT_ENCODING"))) |
94 |
+ break; /* no header Accept-Encoding: */ |
95 |
+ if (!strstr(pt, "gzip")) |
96 |
+ break; /* browser doesn't understand gzip format */ |
97 |
+ if ((pt=getenv("REMOTE_ADDR")) && !strncmp(pt, "127", 3)) |
98 |
+ break; /* never compress to local client */ |
99 |
+ /* OK, compress output with gzip */ |
100 |
+ fprintf(actout, "Content-Encoding: x-gzip\n\n"); |
101 |
+ fflush(actout); |
102 |
+ snprintf(gzipcmd, sizeof(gzipcmd), "gzip -%d", COMPRESSION); |
103 |
+ if (!(actout = popen(gzipcmd, "w"))) |
104 |
+ exit(4); /* cannot fork, gzip not found, ... */ |
105 |
+ } while (0); |
106 |
+#endif /* COMPRESSION */ |
107 |
+ } /* end of CGI section */ |
108 |
+ fprintf (actout, "\n<HTML>\n<HEAD>\n"); |
109 |
+ fprintf (actout, "<TITLE>%s", title ? title : "stdin"); |
110 |
fprintf (actout, "</TITLE>\n</HEAD>\n\n"); |
111 |
fprintf (actout, "<BODY BGCOLOR=\"%s\">\n", bgcolor); |
112 |
(void) insert (actout, ownhead); |
113 |
- fprintf (actout, "<PRE WIDTH=\"80\">"); |
114 |
- |
115 |
- yyin = stdin; |
116 |
- yyout = stdout; |
117 |
- |
118 |
+ fprintf (actout, "<PRE WIDTH=\"%d\">", width); |
119 |
+ |
120 |
+ yyin = actin; |
121 |
+ yyout = actout; |
122 |
yylex(); |
123 |
- |
124 |
- fprintf (actout,"</PRE>\n</BODY>\n\n</HTML>\n"); |
125 |
- } |
126 |
|
127 |
- while (i < argc) |
128 |
- { |
129 |
+ fprintf (actout,"</PRE>\n</BODY>\n\n</HTML>\n"); |
130 |
+ } else for (i=0; i < argc; i++) { |
131 |
actin = fopen (argv[i], "r"); |
132 |
- if (!actin) |
133 |
- { |
134 |
- fprintf (stderr, "usage: %s [files_to_convert]\n", argv[0]); |
135 |
- fprintf (stderr, "%s: cannot open file %s\n", argv[0], argv[i]); |
136 |
- exit (1); |
137 |
+ if (!actin) { |
138 |
+ fprintf (stderr, "%s: cannot read file '%s'\n", prog, argv[i]); |
139 |
+ rc = rc < 1 ? 1 : rc; |
140 |
+ continue; |
141 |
} |
142 |
|
143 |
outfilename = malloc (sizeof (char)*strlen (argv[i]) + 6); |
144 |
strcat (strcpy (outfilename, argv[i]), ".html"); |
145 |
actout = fopen (outfilename, "w"); |
146 |
+ if (!actout) { |
147 |
+ fprintf (stderr, "%s: cannot write file '%s'\n", prog, outfilename); |
148 |
+ rc = rc < 2 ? 2 : rc; |
149 |
+ continue; |
150 |
+ } |
151 |
+ outfilename[strlen(outfilename) - 5] = '\0'; /* chop '.html' suffix */ |
152 |
fprintf (actout, "<HTML>\n<HEAD>\n"); |
153 |
fprintf (actout, |
154 |
"<TITLE>%s", outfilename ); |
155 |
fprintf (actout, "</TITLE>\n</HEAD>\n\n"); |
156 |
fprintf (actout, "<BODY BGCOLOR=\"%s\">\n", bgcolor); |
157 |
(void) insert (actout, ownhead); |
158 |
- fprintf (actout, "<PRE WIDTH=\"80\">"); |
159 |
+ fprintf (actout, "<PRE WIDTH=\"%d\">", width); |
160 |
|
161 |
yyin = actin; |
162 |
yyout = actout; |
163 |
@@ -351,9 +420,8 @@ |
164 |
free (outfilename); |
165 |
fclose (actin); |
166 |
fclose (actout); |
167 |
- i++; |
168 |
- } |
169 |
- return 0; |
170 |
+ } |
171 |
+ return rc; |
172 |
} |
173 |
|
174 |
int |
175 |
@@ -372,8 +440,8 @@ |
176 |
c = fgetc (infile); |
177 |
} |
178 |
fclose (infile); |
179 |
- return (1); |
180 |
+ return 1; |
181 |
} |
182 |
else |
183 |
- return (0); |
184 |
+ return 0; |
185 |
} |