File: uahelper.c

package info (click to toggle)
imap 4.5-0slink2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 5,376 kB
  • ctags: 4,844
  • sloc: ansic: 53,832; makefile: 887; sh: 78
file content (249 lines) | stat: -rw-r--r-- 7,495 bytes parent folder | download | duplicates (6)
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
/*
 * Program:	unANSIify
 *
 * Author:	Mark Crispin
 *		Networks and Distributed Computing
 *		Computing & Communications
 *		University of Washington
 *		4545 15th Ave NE
 *		Seattle, WA  98105-4527
 *		Internet: MRC@CAC.Washington.EDU
 *
 * Date:	1 June 1995
 * Last Edited:	8 June 1996
 *
 * Copyright 1997 by the University of Washington
 *
 *  Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appears in all copies and that both the
 * above copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the University of Washington not be
 * used in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  This software is made
 * available "as is", and
 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */


/* This program is designed to make traditional C sources out of my source
 * files which use ANSI syntax.  This program depends heavily upon knowledge
 * of the way I write code, and is not a general purpose tool.  I hope that
 * someday someone will provide a general purpose tool...
 */

#include <stdio.h>

#define LINELENGTH 1000


/* Find first non-whitespace
 * Accepts: string
 * Returns: 0 if no non-whitespace found, or pointer to non-whitespace
 */

char *fndnws (s)
     char *s;
{
  while ((*s == ' ') || (*s == '\t'))
    if ((*s++ == '\n') || !*s) return (char *) 0;
  return s;
}


/* Find first whitespace
 * Accepts: string
 * Returns: 0 if no whitespace found, or pointer to whitespace
 */

char *fndws (s)
     char *s;
{
  while ((*s != ' ') && (*s != '\t'))
    if ((*s++ == '\n') || !*s) return (char *) 0;
  return s;
}


/* Find end of commend
 * Accepts: string
 * Returns: -1 if end of comment found, else 0
 */

int fndcmt (s)
     char *s;
{
  while (*s && (*s != '\n'))
    if ((*s++ == '*') && (*s == '/')) return -1;
  return 0;
}

/* Output a line
 * Accepts: string
 */

void poot (s)
     char *s;
{
  if (s) fputs (s,stdout);
}


/* Skip prefix
 * Accepts: string
 * Returns: updated string
 */

char *skipfx (s)
     char *s;
{
  char c,*t = s,*tt;
				/* skip leading whitespace too */
  while ((*s == ' ') || (*s == '\t')) *s++;
  if (s != t) {
    c = *s;			/* save character */
    *s = '\0';			/* tie off prefix */
    poot (t);			/* output prefix */
    *s = c;			/* restore character */
  }
				/* a typedef? */
  if (((s[0] == 't') && (s[1] == 'y') && (s[2] == 'p') && (s[3] == 'e') &&
       (s[4] == 'd') && (s[5] == 'e') && (s[6] == 'f')) &&
      (t = fndws (s)) && (t = fndnws (t))) {
    if ((t[0] == 'u') && (t[1] == 'n') && (t[2] == 's') && (t[3] == 'i') &&
	(t[4] == 'g') && (t[5] == 'n') && (t[6] == 'e') && (t[7] == 'd') &&
	(tt = fndws (t+7)) && (tt = fndnws (tt))) t = tt;
    c = *t;			/* save character */
    *t = '\0';			/* tie off prefix */
    poot (s);			/* output prefix */
    *t = c;			/* restore character */
    s = t;			/* new string pointer */
  }
				/* one of the known prefixes? */
  else if ((((s[0] == 'u') && (s[1] == 'n') && (s[2] == 's') &&
	     (s[3] == 'i') && (s[4] == 'g') && (s[5] == 'n') &&
	     (s[6] == 'e') && (s[7] == 'd')) ||
	    ((s[0] == 's') && (s[1] == 't') && (s[2] == 'r') &&
	     (s[3] == 'u') && (s[4] == 'c') && (s[5] == 't')) ||
	    ((s[0] == 's') && (s[1] == 't') && (s[2] == 'a') &&
	     (s[3] == 't') && (s[4] == 'i') && (s[5] == 'c')) ||
	    ((s[0] == 'd') && (s[1] == 'o')) ||
	    ((s[0] == 'e') && (s[1] == 'l') && (s[2] == 's') &&
	     (s[3] == 'e'))) &&
	   (t = fndws (s)) && (t = fndnws (t))) {
    c = *t;			/* save character */
    *t = '\0';			/* tie off prefix */
    poot (s);			/* output prefix */
    *t = c;			/* restore character */
    s = t;			/* new string pointer */
  }
				/* may look like a proto, but isn't */
  else if ((s[0] == 'r') && (s[1] == 'e') && (s[2] == 't') && (s[3] == 'u') &&
	   (s[4] == 'r') && (s[5] == 'n')) {
    poot (s);
    return 0;
  }
  return s;
}

/* UnANSI a line
 * Accepts: string
 */

void unansi (s)
     char *s;
{
  char c,*t = s,*u,*v;
  while (t[1] && (t[1] != '\n')) t++;
  switch (*t) {
  case ',':			/* continued on next line? */
				/* slurp remainder of line */
    fgets (t + 1,LINELENGTH - (t + 1 - s),stdin);
    unansi (s);			/* try again */
    break;
  case ';':			/* function prototype? */
				/* yes, tie it off */
    *(fndws (fndnws (fndws (fndnws (s))))) = '\0';
    printf ("%s ();\n",s);	/* and output non-ANSI form */
    break;
  case ')':
    *t = '\0';			/* tie off args */
    if (*(t = fndnws (fndws (fndnws (fndws (fndnws (s)))))) == '(') {
      *t++ = '\0';		/* tie off */
      while ((*t == ' ') || (*t == '\t')) t++;
      if ((t[0] == 'v') && (t[1] == 'o') && (t[2] == 'i') && (t[3] == 'd') &&
	  !t[4]) *t = '\0';	/* make void be same as null */
      printf ("%s(",s);		/* output start of function */
      s = t;
      while (*s) {		/* for each argument */
	while ((*s == ' ') || (*s == '\t')) s++;
	for (u = v = s; (*u && (*u != ',') && (*u != '[')); u++)
	  if ((*u == ' ') || (*u == '\t')) v = u;
	c = *u;			/* remember delimiter */
	*u = '\0';		/* tie off argument name */
	while (*++v == '*');	/* remove leading pointer indication */
	fputs (v,stdout);	/* write variable name */
	*(s = u) = c;		/* restore delimiter */
	while (*s && (*s != ',')) *s++;
	if (*s) fputc (*s++,stdout);
      }
      puts (")");		/* end of function */
      while (*t) {		/* for each argument */
	fputs ("     ",stdout);
	while ((*t == ' ') || (*t == '\t')) t++;
	while (*t && (*t != ',')) fputc (*t++,stdout);
	puts (";");
	if (*t == ',') t++;
      }
    }
    else printf ("%s)",s);	/* say what?? */
    break;
  default:			/* doesn't look like a function */
    poot (s);
  }
}

main ()
{
  char *s,*t,line[LINELENGTH];
  int c;
  while (s = fgets (line,LINELENGTH,stdin)) switch (line[0]) {
  case '/':			/* comment */
    if ((s[1] != '*') || fndcmt (s+2)) poot (line);
    else do poot (line);
    while (!fndcmt (line) && (s = fgets (line,LINELENGTH,stdin)));
    break;
  case '{':			/* open function */
  case '}':			/* close function */
  case '\f':			/* formfeed */
  case '\n':			/* newline */
  case '#':			/* preprocessor command */
    poot (line);
    break;
  case '\t':			/* whitespace */
  case ' ':
				/* look like function arg def in structure? */
    if ((t = skipfx (line)) && (s = fndws (t)) && (s = fndnws (s)) &&
	(((*s == '(') && (s[1] == '*')) ||
	 ((*s == '*') && (s[1] == '(') && (s[2] == '*'))) &&
	(s = fndws (s)) && (s[-1] == ')') && (s = fndnws (s)) && (*s == '('))
      unansi (t);
    else poot (t);
    break;
  default:			/* begins with anything else */
				/* look like function proto or def? */
    if ((t = skipfx (line)) && (s = fndws (t)) && (s = fndnws (s)) &&
	(s = fndws (s)) && (s = fndnws (s)) && (*s == '('))
      unansi (t);
    else poot (t);
    break;
  }
}