-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuno.c
More file actions
339 lines (303 loc) · 7.99 KB
/
uno.c
File metadata and controls
339 lines (303 loc) · 7.99 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
/***** uno: uno.c *****/
/* Copyright (c) 2000-2003 by Lucent Technologies - Bell Laboratories */
/* All Rights Reserved. This software is for educational purposes only. */
/* Permission is given to distribute this code provided that this intro- */
/* ductory message is not removed and no monies are exchanged. */
/* No guarantee is expressed or implied by the distribution of this code. */
/* Software written by Gerard J. Holzmann -- http://spinroot.com/gerard */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#if !defined(_WIN32)
#ifndef BINDIR
#define BINDIR "/usr/local/bin"
#endif
static char *lx = BINDIR"/uno_local";
static char *gx = BINDIR"/uno_global";
#else
static char *lx = "uno_local";
static char *gx = "uno_global";
#endif
static int localonly, usecheck, quiet;
static int glob_base, verbose, glob_prop;
static char *w_dir;
#ifdef DEBUG
void System(char *cmd) { printf("<%s>\n", cmd); }
#else
#define System(x) { if (verbose) { printf("<%s>\n", x); } system(x); }
#endif
#define BSIZE 4096
static char loc_args[BSIZE];
static char glob_cmd[BSIZE];
static char buf[BSIZE];
typedef struct gfnm Fnm;
static struct gfnm {
char *f;
Fnm *nxt;
} *fnames;
static void
uno_usage(void)
{ /* duplicates function of /v/bin/uno shellscript in c-program */
fprintf(stderr, "usage: uno [options] *.c\n");
fprintf(stderr, "uno options:\n");
fprintf(stderr, "\t-CPP=x set preprocessor to x\n");
fprintf(stderr, "\t-Dname=def define compiler directive\n");
fprintf(stderr, "\t-Dname define compiler directive\n");
fprintf(stderr, "\t-Uname undefine compiler directive\n");
fprintf(stderr, "\t-Iname add name to list of dirs searched for include-files\n");
fprintf(stderr, "\t-Wname use name as working directory to store .uno files\n\n");
fprintf(stderr, "\t-Lname replace uno_local with name\n");
fprintf(stderr, "\t-Gname replace uno_global with name\n");
fprintf(stderr, "\t-n ignore preprocessing directives in source files\n");
fprintf(stderr, "\t-o arg ignored, for modest compatability with cc arguments\n");
fprintf(stderr, "\t-m uno.dfn use master (type) definitions file uno.dfn\n");
fprintf(stderr, "\t-x f declare f to be a function that does not return\n\n");
fprintf(stderr, "\t-V print version number and exit\n");
fprintf(stderr, "\t-s print symbol table information and exit\n\n");
fprintf(stderr, "\t-l or -c perform only local analysis, not global\n");
fprintf(stderr, "\t-p x check local property def stored in file x\n");
fprintf(stderr, "\t-g x check global property def stored in file x\n\n");
fprintf(stderr, "\t-a report all error paths (local analysis)\n");
fprintf(stderr, "\t-e add the else-chain checker\n");
fprintf(stderr, "\t-q quiet mode, no hints at the end for other checks\n");
fprintf(stderr, "\t-r check some coding-style rules\n");
fprintf(stderr, "\t-t more detailed execution traces (global analysis)\n");
fprintf(stderr, "\t-u complain about redundancies of all sorts\n");
fprintf(stderr, "\t-v verbose mode (mostly for debugging)\n");
fprintf(stderr, "\t-w more picky, includes -u and -t\n");
exit(1);
}
static void
add_target(char *f)
{ Fnm *n;
if (f[0] == '_')
{ return; /* avoid re-preprocessing stale uno intermediaries */
}
n = (Fnm *) malloc(sizeof(Fnm));
n->f = (char *) malloc(strlen(f) + 1);
strcpy(n->f, f);
n->nxt = fnames;
fnames = n;
}
static void
pass_loc(char *pref, char *par)
{
strcat(loc_args, pref);
if (par)
{ strcat(loc_args, par);
strcat(loc_args, " ");
}
}
static void
set_glb(char *par)
{
glob_prop++;
#ifndef DEBUG
int fd;
fd = creat("_uno_.c", 0644);
if (fd < 0)
{ fprintf(stderr, "uno: cannot create temp files\n");
exit(1);
}
close(fd);
#else
System("echo \"/* empty file */\" > _uno_.c");
#endif
sprintf(buf, "%s -prop %s _uno_.c", lx, par);
System(buf);
#if 1
unlink("_uno_.c");
#else
System("rm -f _uno_.c");
#endif
}
static void
cleanup(int unused)
{
if (verbose)
{ return;
}
if ((int) strlen(glob_cmd) > glob_base && glob_base > 5)
{ memset(glob_cmd, ' ', glob_base);
#if defined(_WIN32)
memcpy(glob_cmd, "del /Q", 6);
#else
strncpy(glob_cmd, "rm -f", 5);
#endif
if (w_dir)
{ char *p = malloc(strlen(glob_cmd) + strlen("cd ;") + strlen(w_dir) + 1);
sprintf(p, "cd %s; %s\n", w_dir, glob_cmd);
if (verbose) { printf("%s\n", p); }
System(p);
} else
{
if (verbose) { printf("%s\n", glob_cmd); }
System(glob_cmd);
} }
}
static void
version_info(void)
{ strcat(loc_args, "-V");
System(loc_args);
exit(0);
}
static void
run_uno(void)
{ char *p;
while (fnames)
{ strcpy(buf, loc_args);
strcat(buf, fnames->f);
if (verbose)
{ printf("call: %s\n", buf);
}
System(buf);
strcat(glob_cmd, fnames->f);
p = strrchr(glob_cmd, '.');
if (!p)
{ fprintf(stderr, "uno: cannot happen1\n");
exit(1);
}
*p = '\0'; /* replace .c with .uno */
strcat(glob_cmd, ".uno ");
fnames = fnames->nxt;
}
if (!localonly)
{ if (glob_prop)
{ strcat(glob_cmd, "_uno_.uno ");
}
if (w_dir)
{ p = malloc(strlen(glob_cmd) + strlen("cd ;") + strlen(w_dir) + 1);
sprintf(p, "cd %s; %s\n", w_dir, glob_cmd);
System(p);
} else
{ System(glob_cmd);
} }
cleanup(0);
}
int
main(int argc, char *argv[])
{
strcpy(loc_args, lx);
strcat(loc_args, " ");
strcpy(glob_cmd, gx);
strcat(glob_cmd, " ");
argc--;
while (argc-- > 0) /* @uno_suppress side-effect */
{ argv++;
if (0) { printf("%3d: '%s'\n", argc, *argv); }
if ((*argv)[0] == '-')
{ if (0) { printf("option '%c' -- '%s'\n", (*argv)[1], *argv); }
switch((*argv)[1]) {
case 'C': /* -CPP=... */
case 'D':
case 'U':
case 'I':
pass_loc(*argv, "");
break;
case 'L':
strcpy(lx, &(*argv)[2]);
if (verbose) { printf("uno: using %s for uno_local\n", lx); }
strcpy(loc_args, lx);
strcat(loc_args, " ");
break;
case 'G':
strcpy(gx, &(*argv)[2]);
if (verbose) { printf("uno: using %s for uno_global\n", gx); }
strcpy(glob_cmd, gx);
strcat(glob_cmd, " ");
break;
case 'W':
w_dir = malloc(strlen(*argv)+1-2);
strcpy(w_dir, &(*argv)[2]);
pass_loc(*argv, "");
break;
case 'a': /* l */
pass_loc("-allerr ", NULL);
break;
case 'e': /* add the else-chain and compound checkers */
pass_loc("-else ", NULL);
/* pass_loc("-compound ", NULL); * not working */
break;
case 'g': /* g */
argv++; argc--;
set_glb(*argv);
break;
case 'c': /* absorb cc args */
case 'l': /* l */
localonly = 1;
pass_loc("-localonly ", NULL);
break;
case 'm': /* l */
argv++; argc--;
pass_loc("-master ", *argv);
break;
case 'n': /* l */
pass_loc("-nopre ", NULL);
break;
case 'o': /* absorb cc args */
argv++; argc--;
break;
case 'p': /* l */
argv++; argc--;
pass_loc("-prop ", *argv);
break;
case 'q':
quiet = 1;
break;
case 'r': /* experimental XXX */
pass_loc("-rules ", NULL);
localonly = 1;
break;
case 's': /* l */
pass_loc("-s ", NULL);
localonly = 1;
break;
case 't': /* g */
strcat(glob_cmd, "-l ");
break; /* g */
case 'V': /* l */
version_info();
break;
case 'v': /* g,l */
pass_loc("-v ", NULL);
strcat(glob_cmd, "-v ");
verbose = 1;
break;
case 'w': /* g,l */
pass_loc("-picky ", NULL);
strcat(glob_cmd, "-l ");
case 'u': /* g,l @uno_suppress: fall through on switch */
usecheck = 1;
pass_loc("-use ", NULL);
strcat(glob_cmd, "-u ");
break;
case 'x': /* l */
argv++; argc--;
pass_loc("-exit ", *argv);
break;
default :
fprintf(stderr, "uno: saw -%c\n", (*argv)[1]);
uno_usage();
break;
}
} else if (strstr(*argv, ".c") != NULL)
{ add_target(*argv);
} else
{ uno_usage();
} }
if (!fnames)
{ uno_usage();
}
signal(SIGINT, cleanup);
glob_base = (int) strlen(glob_cmd);
run_uno();
if (!usecheck && !quiet)
{ printf("uno:\tcheck completed, try 'uno -h' for different checks\n");
}
return 0;
}