1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
22 * Seattle, WA 98105-4527
23 * Internet: MRC@CAC.Washington.EDU
26 * Last Edited: 30 August 2006
30 /* This program is designed to make traditional C sources out of my source
31 * files which use ANSI syntax. This program depends heavily upon knowledge
32 * of the way I write code, and is not a general purpose tool. I hope that
33 * someday someone will provide a general purpose tool...
38 #define LINELENGTH 1000
41 /* Find first non-whitespace
43 * Returns: 0 if no non-whitespace found, or pointer to non-whitespace
49 while ((*s
== ' ') || (*s
== '\t'))
50 if ((*s
++ == '\n') || !*s
) return (char *) 0;
55 /* Find first whitespace
57 * Returns: 0 if no whitespace found, or pointer to whitespace
63 while ((*s
!= ' ') && (*s
!= '\t'))
64 if ((*s
++ == '\n') || !*s
) return (char *) 0;
69 /* Find end of commend
71 * Returns: -1 if end of comment found, else 0
77 while (*s
&& (*s
!= '\n'))
78 if ((*s
++ == '*') && (*s
== '/')) return -1;
89 if (s
) fputs (s
,stdout
);
95 * Returns: updated string
102 /* skip leading whitespace too */
103 while ((*s
== ' ') || (*s
== '\t')) *s
++;
105 c
= *s
; /* save character */
106 *s
= '\0'; /* tie off prefix */
107 poot (t
); /* output prefix */
108 *s
= c
; /* restore character */
111 if (((s
[0] == 't') && (s
[1] == 'y') && (s
[2] == 'p') && (s
[3] == 'e') &&
112 (s
[4] == 'd') && (s
[5] == 'e') && (s
[6] == 'f')) &&
113 (t
= fndws (s
)) && (t
= fndnws (t
))) {
114 if ((t
[0] == 'u') && (t
[1] == 'n') && (t
[2] == 's') && (t
[3] == 'i') &&
115 (t
[4] == 'g') && (t
[5] == 'n') && (t
[6] == 'e') && (t
[7] == 'd') &&
116 (tt
= fndws (t
+7)) && (tt
= fndnws (tt
))) t
= tt
;
117 c
= *t
; /* save character */
118 *t
= '\0'; /* tie off prefix */
119 poot (s
); /* output prefix */
120 *t
= c
; /* restore character */
121 s
= t
; /* new string pointer */
123 /* static with known prefix */
124 else if (((s
[0] == 's') && (s
[1] == 't') && (s
[2] == 'a') &&
125 (s
[3] == 't') && (s
[4] == 'i') && (s
[5] == 'c') &&
127 (((s
[7] == 'u') && (s
[8] == 'n') && (s
[9] == 's') &&
128 (s
[10] == 'i') && (s
[11] == 'g') && (s
[12] == 'n') &&
129 (s
[13] == 'e') && (s
[14] == 'd')) ||
130 ((s
[7] == 's') && (s
[8] == 't') && (s
[9] == 'r') &&
131 (s
[10] == 'u') && (s
[11] == 'c') && (s
[12] == 't')) ||
132 ((s
[7] == 'd') && (s
[8] == 'o')) ||
133 ((s
[9] == 'e') && (s
[10] == 'l') && (s
[11] == 's') &&
135 (t
= fndws (s
)) && (t
= fndnws (t
)) &&
136 (t
= fndws (t
)) && (t
= fndnws (t
))) {
137 c
= *t
; /* save character */
138 *t
= '\0'; /* tie off prefix */
139 poot (s
); /* output prefix */
140 *t
= c
; /* restore character */
141 s
= t
; /* new string pointer */
143 /* one of the known prefixes? */
144 else if ((((s
[0] == 'u') && (s
[1] == 'n') && (s
[2] == 's') &&
145 (s
[3] == 'i') && (s
[4] == 'g') && (s
[5] == 'n') &&
146 (s
[6] == 'e') && (s
[7] == 'd')) ||
147 ((s
[0] == 's') && (s
[1] == 't') && (s
[2] == 'r') &&
148 (s
[3] == 'u') && (s
[4] == 'c') && (s
[5] == 't')) ||
149 ((s
[0] == 's') && (s
[1] == 't') && (s
[2] == 'a') &&
150 (s
[3] == 't') && (s
[4] == 'i') && (s
[5] == 'c')) ||
151 ((s
[0] == 'd') && (s
[1] == 'o')) ||
152 ((s
[0] == 'e') && (s
[1] == 'l') && (s
[2] == 's') &&
154 (t
= fndws (s
)) && (t
= fndnws (t
))) {
155 c
= *t
; /* save character */
156 *t
= '\0'; /* tie off prefix */
157 poot (s
); /* output prefix */
158 *t
= c
; /* restore character */
159 s
= t
; /* new string pointer */
161 /* may look like a proto, but isn't */
162 else if ((s
[0] == 'r') && (s
[1] == 'e') && (s
[2] == 't') && (s
[3] == 'u') &&
163 (s
[4] == 'r') && (s
[5] == 'n')) {
178 while (t
[1] && (t
[1] != '\n')) t
++;
180 case ',': /* continued on next line? */
181 /* slurp remainder of line */
182 fgets (t
+ 1,LINELENGTH
- (t
+ 1 - s
),stdin
);
183 unansi (s
); /* try again */
185 case ';': /* function prototype? */
186 /* yes, tie it off */
187 *(fndws (fndnws (fndws (fndnws (s
))))) = '\0';
188 printf ("%s ();\n",s
); /* and output non-ANSI form */
191 *t
= '\0'; /* tie off args */
192 if (*(t
= fndnws (fndws (fndnws (fndws (fndnws (s
)))))) == '(') {
193 *t
++ = '\0'; /* tie off */
194 while ((*t
== ' ') || (*t
== '\t')) t
++;
195 if ((t
[0] == 'v') && (t
[1] == 'o') && (t
[2] == 'i') && (t
[3] == 'd') &&
196 !t
[4]) *t
= '\0'; /* make void be same as null */
197 printf ("%s(",s
); /* output start of function */
199 while (*s
) { /* for each argument */
200 while ((*s
== ' ') || (*s
== '\t')) s
++;
201 for (u
= v
= s
; (*u
&& (*u
!= ',') && (*u
!= '[')); u
++)
202 if ((*u
== ' ') || (*u
== '\t')) v
= u
;
203 c
= *u
; /* remember delimiter */
204 *u
= '\0'; /* tie off argument name */
205 while (*++v
== '*'); /* remove leading pointer indication */
206 fputs (v
,stdout
); /* write variable name */
207 *(s
= u
) = c
; /* restore delimiter */
208 while (*s
&& (*s
!= ',')) *s
++;
209 if (*s
) fputc (*s
++,stdout
);
211 puts (")"); /* end of function */
212 while (*t
) { /* for each argument */
214 while ((*t
== ' ') || (*t
== '\t')) t
++;
215 while (*t
&& (*t
!= ',')) fputc (*t
++,stdout
);
220 else printf ("%s)",s
); /* say what?? */
222 default: /* doesn't look like a function */
229 char *s
,*t
,line
[LINELENGTH
];
231 while (s
= fgets (line
,LINELENGTH
,stdin
)) switch (line
[0]) {
232 case '/': /* comment */
233 if ((s
[1] != '*') || fndcmt (s
+2)) poot (line
);
235 while (!fndcmt (line
) && (s
= fgets (line
,LINELENGTH
,stdin
)));
237 case '{': /* open function */
238 case '}': /* close function */
239 case '\f': /* formfeed */
240 case '\n': /* newline */
241 case '#': /* preprocessor command */
244 case '\t': /* whitespace */
246 /* look like function arg def in structure? */
247 if ((t
= skipfx (line
)) && (s
= fndws (t
)) && (s
= fndnws (s
)) &&
248 (((*s
== '(') && (s
[1] == '*')) ||
249 ((*s
== '*') && (s
[1] == '(') && (s
[2] == '*'))) &&
250 (s
= fndws (s
)) && (s
[-1] == ')') && (s
= fndnws (s
)) && (*s
== '('))
254 default: /* begins with anything else */
255 /* look like function proto or def? */
256 if ((t
= skipfx (line
)) && (s
= fndws (t
)) && (s
= fndnws (s
)) &&
257 (s
= fndws (s
)) && (s
= fndnws (s
)) && (*s
== '('))