1 | /* |
---|
2 | ** cxref.c |
---|
3 | ** |
---|
4 | ** C driver for Cxref program. |
---|
5 | ** does argument handling, then builds the right |
---|
6 | ** shell commands for passing to the system() routine. |
---|
7 | ** |
---|
8 | ** A possible but difficult speed improvement would be to |
---|
9 | ** set up the argument vectors ourselves, the i/o with a pipe() |
---|
10 | ** call, and do all the forking and execing ourselves. |
---|
11 | ** But, in keeping w/the philosophy of "Let someone else do |
---|
12 | ** the hard part", we leave well enough alone and let the shell do it. |
---|
13 | ** |
---|
14 | ** Arnold Robbins, Information and Computer Science, Georgia Tech |
---|
15 | ** gatech!arnold |
---|
16 | ** Copyright (c) 1984 by Arnold Robbins |
---|
17 | ** All rights reserved |
---|
18 | ** This program may not be sold, but may be distributed |
---|
19 | ** provided this header is included. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <ctype.h> |
---|
24 | |
---|
25 | #ifdef DEBUG |
---|
26 | #define system(str) printf("%s\n", str) |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef TESTOUT |
---|
30 | dosystem(str) |
---|
31 | char *str; |
---|
32 | { |
---|
33 | int pid; |
---|
34 | |
---|
35 | fprintf(stderr, "%s\n", str); |
---|
36 | system(str); |
---|
37 | } |
---|
38 | |
---|
39 | #define system(str) dosystem(str) /* takes effect after above routine */ |
---|
40 | #endif |
---|
41 | |
---|
42 | #define TRUE 1 |
---|
43 | #define FALSE 0 |
---|
44 | |
---|
45 | char *name; /* save command name */ |
---|
46 | |
---|
47 | int xargc; /* make argc and argv available globally */ |
---|
48 | char **xargv; |
---|
49 | |
---|
50 | int width = 0; /* output width */ |
---|
51 | |
---|
52 | int sepflag = FALSE; /* do each one separately */ |
---|
53 | |
---|
54 | int iflag = TRUE; /* print out ints */ |
---|
55 | int fflag = TRUE; /* print out floats */ |
---|
56 | int cflag = TRUE; /* print out chars */ |
---|
57 | int sflag = TRUE; /* print out strings */ |
---|
58 | |
---|
59 | char *filename(); /* turns "-" into "stdin" */ |
---|
60 | |
---|
61 | main(argc, argv) |
---|
62 | int argc; |
---|
63 | char **argv; |
---|
64 | { |
---|
65 | int i; |
---|
66 | int extra_arg = FALSE; |
---|
67 | |
---|
68 | name = filename(argv[0]); |
---|
69 | |
---|
70 | for(argv++, argc--; argc > 0; argv++, argc--) |
---|
71 | if (argv[0][0] != '-') |
---|
72 | break; |
---|
73 | else if(argv[0][1] == '\0') /* filename of "-" */ |
---|
74 | break; |
---|
75 | else |
---|
76 | for(i = 1; argv[0][i] != '\0'; i++) |
---|
77 | { |
---|
78 | switch(argv[0][i]) { |
---|
79 | case 'S': |
---|
80 | sepflag = TRUE; |
---|
81 | break; |
---|
82 | |
---|
83 | case 'C': |
---|
84 | /* leave out all constants */ |
---|
85 | cflag = |
---|
86 | iflag = |
---|
87 | fflag = |
---|
88 | sflag = FALSE; |
---|
89 | break; |
---|
90 | |
---|
91 | case 'c': |
---|
92 | cflag = FALSE; |
---|
93 | break; |
---|
94 | |
---|
95 | case 'i': |
---|
96 | iflag = FALSE; |
---|
97 | break; |
---|
98 | |
---|
99 | case 'f': |
---|
100 | fflag = FALSE; |
---|
101 | break; |
---|
102 | |
---|
103 | case 's': |
---|
104 | sflag = FALSE; |
---|
105 | break; |
---|
106 | |
---|
107 | case 'w': |
---|
108 | if (isdigit(argv[0][i+1])) |
---|
109 | { |
---|
110 | width = 0; |
---|
111 | for(i++; isdigit(argv[0][i]); i++) |
---|
112 | width = width * 10 + argv[0][i] - '0'; |
---|
113 | i--; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | width = atoi(argv[1]); |
---|
118 | extra_arg = TRUE; |
---|
119 | } |
---|
120 | break; |
---|
121 | |
---|
122 | default: |
---|
123 | usage(); |
---|
124 | break; |
---|
125 | } |
---|
126 | |
---|
127 | if (extra_arg) /* skip column width */ |
---|
128 | { |
---|
129 | extra_arg = FALSE; |
---|
130 | /* do this only once */ |
---|
131 | /* inside the for loop */ |
---|
132 | argv++; |
---|
133 | argc--; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if (width != 0) |
---|
138 | if (width < 51) |
---|
139 | width = 80; |
---|
140 | else if (width > 132) |
---|
141 | width = 132; |
---|
142 | |
---|
143 | xargc = argc; |
---|
144 | xargv = argv; |
---|
145 | |
---|
146 | runprogs(); |
---|
147 | } |
---|
148 | |
---|
149 | char command[BUFSIZ * 10]; /* may need LOTS of room */ |
---|
150 | char com_buf[BUFSIZ * 10]; /* use short name for portability */ |
---|
151 | |
---|
152 | char *docxref(); /* functions to generate commands with args */ |
---|
153 | char *filter(); |
---|
154 | char *fmtxref(); |
---|
155 | |
---|
156 | #define ONLYONE 1 |
---|
157 | #define ALLOFTHEM 2 |
---|
158 | |
---|
159 | runprogs() /* execute the programs */ |
---|
160 | { |
---|
161 | int i; |
---|
162 | |
---|
163 | if (sepflag) /* do each file separately */ |
---|
164 | { |
---|
165 | for (i = 0; i < xargc; i++) |
---|
166 | { |
---|
167 | printf("\tC Cross Refence Listing of %s\n\n", |
---|
168 | filename(xargv[i])); |
---|
169 | fflush(stdout); |
---|
170 | /* send to ouput before commands start */ |
---|
171 | sprintf(command, |
---|
172 | "%s | sort -u +0b -2 +2n | %s | %s", |
---|
173 | docxref(i, ONLYONE), filter(), fmtxref()); |
---|
174 | system(command); |
---|
175 | if (iflag) |
---|
176 | { |
---|
177 | sprintf(com_buf, |
---|
178 | "sort -u +0n -1 +1b -2 +2n < /tmp/cxr.%d.1 | %s/cxrfilt -i | %s", |
---|
179 | getpid(), SRCDIR, fmtxref()); |
---|
180 | sprintf(command, |
---|
181 | "if test -s /tmp/cxr.%d.1 ; then %s ; fi", |
---|
182 | getpid(), com_buf); |
---|
183 | system(command); |
---|
184 | } |
---|
185 | if (fflag) |
---|
186 | { |
---|
187 | sprintf(com_buf, |
---|
188 | "sort -u +0n +1n -2 +2b -3 +3n < /tmp/cxr.%d.2 | %s/cxrfilt -f | %s", |
---|
189 | getpid(), SRCDIR, fmtxref()); |
---|
190 | sprintf(command, |
---|
191 | "if test -s /tmp/cxr.%d.2 ; then %s ; fi", |
---|
192 | getpid(), com_buf); |
---|
193 | system(command); |
---|
194 | } |
---|
195 | fflush(stdout); |
---|
196 | if (! isatty(fileno(stdout))) |
---|
197 | putchar('\f'); |
---|
198 | } |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | if (xargc == 1) |
---|
203 | printf("\tC Cross Refence Listing of %s\n\n", |
---|
204 | filename(xargv[0])); |
---|
205 | else |
---|
206 | printf("\tC Cross Reference Listing\n\n"); |
---|
207 | fflush(stdout); |
---|
208 | sprintf(command, "%s | sort -u +0b -2 +2n | %s | %s", |
---|
209 | docxref(0, ALLOFTHEM), filter(), fmtxref()); |
---|
210 | system (command); |
---|
211 | if (iflag) |
---|
212 | { |
---|
213 | sprintf(com_buf, |
---|
214 | "sort -u +0n -1 +1b -2 +2n < /tmp/cxr.%d.1 | %s/cxrfilt -i | %s", |
---|
215 | getpid(), SRCDIR, fmtxref()); |
---|
216 | |
---|
217 | sprintf(command, |
---|
218 | "if test -s /tmp/cxr.%d.1 ; then %s ; fi", |
---|
219 | getpid(), com_buf); |
---|
220 | system(command); |
---|
221 | } |
---|
222 | if (fflag) |
---|
223 | { |
---|
224 | sprintf(com_buf, |
---|
225 | "sort -u +0n +1n -2 +2b -3 +3n < /tmp/cxr.%d.2 | %s/cxrfilt -f | %s", |
---|
226 | getpid(), SRCDIR, fmtxref()); |
---|
227 | sprintf(command, |
---|
228 | "if test -s /tmp/cxr.%d.2 ; then %s ; fi", |
---|
229 | getpid(), com_buf); |
---|
230 | system(command); |
---|
231 | } |
---|
232 | fflush(stdout); |
---|
233 | if (! isatty(fileno(stdout))) |
---|
234 | putchar('\f'); |
---|
235 | } |
---|
236 | |
---|
237 | sprintf(command, "rm -f /tmp/cxr.%d.[12]", getpid()); |
---|
238 | system(command); |
---|
239 | } |
---|
240 | |
---|
241 | char *docxref(index, howmany) |
---|
242 | int index, howmany; |
---|
243 | { |
---|
244 | static char buf[BUFSIZ * 10]; |
---|
245 | int i, j; |
---|
246 | |
---|
247 | if (howmany == ONLYONE) |
---|
248 | sprintf(buf, "%s/docxref %s", SRCDIR, xargv[index]); |
---|
249 | else |
---|
250 | { |
---|
251 | /* put all the args on one command line */ |
---|
252 | sprintf(buf, "%s/docxref ", SRCDIR); |
---|
253 | |
---|
254 | i = strlen(buf); |
---|
255 | for(; xargc > 0; xargc--, xargv++) |
---|
256 | { |
---|
257 | for(j = 0; xargv[0][j] != '\0'; j++) |
---|
258 | buf[i++] = xargv[0][j]; |
---|
259 | buf[i++] = ' '; |
---|
260 | } |
---|
261 | buf[i] = '\0'; |
---|
262 | } |
---|
263 | |
---|
264 | return (buf); |
---|
265 | } |
---|
266 | |
---|
267 | char *filter() /* command line for splitting off ints and floats */ |
---|
268 | { |
---|
269 | static char buf[40]; |
---|
270 | |
---|
271 | if (! cflag && sflag) |
---|
272 | sprintf(buf, "%s/cxrfilt -c %d", SRCDIR, getpid()); |
---|
273 | else if (cflag && ! sflag) |
---|
274 | sprintf(buf, "%s/cxrfilt -s %d", SRCDIR, getpid()); |
---|
275 | else if (! cflag && ! sflag) |
---|
276 | sprintf(buf, "%s/cxrfilt -cs %d", SRCDIR, getpid()); |
---|
277 | else |
---|
278 | sprintf(buf, "%s/cxrfilt %d", SRCDIR, getpid()); |
---|
279 | |
---|
280 | return (buf); |
---|
281 | } |
---|
282 | |
---|
283 | char *fmtxref() |
---|
284 | { |
---|
285 | static char buf[40]; |
---|
286 | |
---|
287 | if (width != 0) |
---|
288 | sprintf(buf, "%s/fmtxref -w %d", SRCDIR, width); |
---|
289 | else |
---|
290 | sprintf(buf, "%s/fmtxref", SRCDIR); |
---|
291 | |
---|
292 | return(buf); |
---|
293 | } |
---|
294 | |
---|
295 | usage() |
---|
296 | { |
---|
297 | fprintf(stderr, "usage: %s [-SCcsif] [-w width] [files]\n", name); |
---|
298 | exit (1); |
---|
299 | } |
---|
300 | |
---|
301 | char *filename(fname) |
---|
302 | char *fname; |
---|
303 | { |
---|
304 | char *cp, *basename(); |
---|
305 | |
---|
306 | cp = basename(fname); |
---|
307 | |
---|
308 | return ( strcmp(cp, "-") == 0 ? "stdin" : cp); |
---|
309 | } |
---|
310 | |
---|
311 | #include "basename.c" |
---|