-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxrfilt.c
298 lines (243 loc) · 5.86 KB
/
cxrfilt.c
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
/*
** cxrfilt.c
**
** if called with no flags, or with the -c or -s flags, it will
** separate out integer and floating point constants into
** their own files, pass char and string constants on through.
** input: sorted output of docxref, contains identifiers and constants.
** output: identifiers, char and string constants, depending on flags.
** output goes to fmtxref. floats and ints to separate files for sorting.
**
** if called with -i or -f option, behavior is to put sorted ints or floats
** back into their original formats, and then pass the output to fmtxref.
**
** originally, there was a separate program to do float and int, but these two
** have been merged to reduce the total number of programs needed for cxref.
**
** Arnold Robbins, Information and Computer Science, Georgia Tech
** gatech!arnold
** Copyright (c) 1984 by Arnold Robbins
** All rights reserved
** This program may not be sold, but may be distributed
** provided this header is included.
*/
#include <stdio.h>
#include "constdefs.h"
#define MAXFILE 120
#define MAXLINE 120
FILE *fp1, *fp2;
int cflag = 0;
int sflag = 0;
char *name;
char *basename();
main(argc, argv)
int argc;
char **argv;
{
char buf[BUFSIZ];
char file1[MAXFILE], file2[MAXFILE];
int i;
name = basename(argv[0]);
if (argc <= 1)
usage();
if(argv[1][0] == '-')
{
for (i = 1; argv[1][i] != '\0'; i++)
switch (argv[1][i]) {
case 'c':
cflag = 1;
break;
case 's':
sflag = 1;
break;
case 'i':
intfilter();
exit(0);
break;
case 'f':
floatfilter();
exit(0);
break;
default: /* bad option given */
usage();
break;
}
/* if it gets to here, we were called only w/-c or -s */
if (argc == 2)
usage();
argv++;
}
/* code for splitting constants off into separate files */
sprintf(file1, "/tmp/cxr.%d.1", atoi(argv[1]));
sprintf(file2, "/tmp/cxr.%d.2", atoi(argv[1]));
if ((fp1 = fopen(file1, "w")) == NULL)
{
fprintf(stderr,"%s: couldn't create tempfile 1\n");
exit (2);
}
if ((fp2 = fopen(file2, "w")) == NULL)
{
fprintf(stderr,"%s: couldn't create tempfile 2\n");
exit (3);
}
while (gets(buf) != NULL)
{
if (buf[0] != '~')
printf("%s\n", buf);
else
switch (buf[1]) {
case CHAR:
if (! cflag)
printf("%s\n", &buf[2]);
break;
case STRING:
if (! sflag)
printf("%s\n", &buf[2]);
break;
case INT:
outint(buf);
break;
case FLOAT:
outfloat(buf);
break;
default:
fprintf(stderr,"%s: bad input line '%s'\n",
name, buf);
exit (4);
}
}
fclose(fp1);
fclose(fp2);
exit(0);
}
#define OCTAL 1
#define HEX 2
#define DEC 3
outint(buf)
char *buf;
{
char file[MAXLINE], line[MAXLINE];
int val;
int type = 0;
buf += 2; /* skip leading ~INT */
file[0] = line[0] = '\0';
if (buf[0] == '0') /* octal or hex */
{
if (buf[1] == 'x' || buf[1] == 'X') /* hex */
{
type = HEX;
buf += 2; /* skip leading 0x */
sscanf(buf, "%x %s %s", &val, file, line);
}
else
{
type = OCTAL;
sscanf(buf, "%o %s %s", &val, file, line);
}
}
else
{
type = DEC;
sscanf(buf, "%d %s %s", &val, file, line); /* decimal */
}
/*
* strategy is to convert to decimal for numeric sorting,
* then have output filter convert back to right base.
*
* type is used to tell intfilter() what to turn it back into.
*/
fprintf(fp1, "%d\t%s\t%s\t%d\n", val, file, line, type);
}
outfloat(buf)
char *buf;
{
char file[MAXLINE], line[MAXLINE];
char mantissa[MAXLINE], exponent[MAXLINE];
char strval[MAXLINE]; /* character representation of float */
char controlstr[MAXLINE];
double val;
int i, j;
buf += 2; /* skip ~FLOAT */
mantissa[0] = exponent[0] = file[0] = line[0] = '\0';
sscanf(buf, "%lf %s %s", &val, file, line);
for (i = 0; buf[i] != '\t'; i++)
if (buf[i] == '.')
break;
for (j = i + 1; buf[j] != 'E' && buf[j] != 'e' && buf[j] != '\t'; j++)
;
j -= i - 1; /* j is now num digits to right decimal point. */
if (j < 6)
j = 6; /* default */
sprintf(controlstr, "%%1.%dg", j); /* make control string */
sprintf(strval, controlstr, val); /* make character string */
/*
* strategy is a follows:
* 1) convert all floats to a common printed format (%g)
* 2) split up mantissa and exponent into separate parts for sorting
* 3) put them back together later when called w/-f option.
*/
for(i = j = 0; strval[j] != 'e' && strval[j] != 'E' && strval[j] != '\0'; i++, j++)
mantissa[i] = strval[j];
mantissa[i] = '\0';
if (strval[j] == 'e' || strval[j] == 'E')
{
j++;
for(i = 0; strval[j] != '\0'; i++, j++)
exponent[i] = strval[j];
exponent[i] = '\0';
}
else
exponent[0] = '\0';
fprintf(fp2, "%s\t%s\t%s\t%s\n", mantissa,
exponent[0] != '\0' ? exponent : "0", file, line);
}
usage()
{
fprintf(stderr, "usage: %s [-csfi] pid\n", name);
exit (1);
}
intfilter() /* put sorted ints back into their original bases */
{
char buf[BUFSIZ];
char file[MAXLINE], number[MAXLINE];
int val;
int type;
while (gets(buf) != NULL)
{
sscanf(buf, "%d %s %s %d", &val, file, number, &type);
switch (type) {
case OCTAL:
if (val == 0) /* don't print 00 */
printf("0\t%s\t%s\n", file, number);
else
printf("0%o\t%s\t%s\n", val, file, number);
/* supply leading 0 */
break;
case DEC:
printf("%d\t%s\t%s\n", val, file, number);
break;
case HEX:
printf("0x%x\t%s\t%s\n", val, file, number);
break;
default:
fprintf(stderr,"%s: bad input line '%s'\n", name, buf);
exit (4);
}
}
}
floatfilter() /* put sorted floats back together */
{
char buf[BUFSIZ];
char file[MAXLINE], number[MAXLINE];
char mantissa[MAXLINE], exponent[MAXLINE];
while (gets(buf) != NULL)
{
sscanf(buf, "%s %s %s %s", mantissa, exponent, file, number);
if (strcmp(exponent, "0") == 0)
printf("%s", mantissa);
else
printf("%sE%s", mantissa, exponent);
printf("\t%s\t%s\n", file, number);
}
}
#include "basename.c"