-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxref.c
125 lines (125 loc) · 2.86 KB
/
xref.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
static char rcsid[] = "$Id: H:/drh/idioms/book/RCS/set.doc,v 1.11 1996/06/26 23:02:01 drh Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "table.h"
#include <string.h>
#include "atom.h"
#include "set.h"
#include "mem.h"
#include "getword.h"
#include <ctype.h>
int compare(const void *x, const void *y);
void print(Table_T);
int cmpint(const void *x, const void *y);
void xref(const char *, FILE *, Table_T);
int first(int c);
int rest (int c);
int intcmp (const void *x, const void *y);
unsigned inthash(const void *x);
int linenum;
int main(int argc, char *argv[]) {
int i;
Table_T identifiers = Table_new(0, NULL, NULL);
for (i = 1; i < argc; i++) {
FILE *fp = fopen(argv[i], "r");
if (fp == NULL) {
fprintf(stderr, "%s: can't open '%s' (%s)\n",
argv[0], argv[i], strerror(errno));
return EXIT_FAILURE;
} else {
xref(argv[i], fp, identifiers);
fclose(fp);
}
}
if (argc == 1) xref(NULL, stdin, identifiers);
{
int i;
void **array = Table_toArray(identifiers, NULL);
qsort(array, Table_length(identifiers),
2*sizeof (*array), compare);
for (i = 0; array[i]; i += 2) {
printf("%s", (char *)array[i]);
print(array[i+1]);
}
FREE(array);
}
return EXIT_SUCCESS;
}
int compare(const void *x, const void *y) {
return strcmp(*(char **)x, *(char **)y);
}
void print(Table_T files) {
int i;
void **array = Table_toArray(files, NULL);
qsort(array, Table_length(files), 2*sizeof (*array),
compare);
for (i = 0; array[i]; i += 2) {
if (*(char *)array[i] != '\0')
printf("\t%s:", (char *)array[i]);
{
int j;
void **lines = Set_toArray(array[i+1], NULL);
qsort(lines, Set_length(array[i+1]), sizeof (*lines),
cmpint);
for (j = 0; lines[j]; j++)
printf(" %d", *(int *)lines[j]);
FREE(lines);
}
printf("\n");
}
FREE(array);
}
int cmpint(const void *x, const void *y) {
if (**(int **)x < **(int **)y)
return -1;
else if (**(int **)x > **(int **)y)
return +1;
else
return 0;
}
void xref(const char *name, FILE *fp,
Table_T identifiers){
char buf[128];
if (name == NULL)
name = "";
name = Atom_string(name);
linenum = 1;
while (getword(fp, buf, sizeof buf, first, rest)) {
Set_T set;
Table_T files;
const char *id = Atom_string(buf);
files = Table_get(identifiers, id);
if (files == NULL) {
files = Table_new(0, NULL, NULL);
Table_put(identifiers, id, files);
}
set = Table_get(files, name);
if (set == NULL) {
set = Set_new(0, intcmp, inthash);
Table_put(files, name, set);
}
{
int *p = &linenum;
if (!Set_member(set, p)) {
NEW(p);
*p = linenum;
Set_put(set, p);
}
}
}
}
int first(int c) {
if (c == '\n')
linenum++;
return isalpha(c) || c == '_';
}
int rest(int c) {
return isalpha(c) || c == '_' || isdigit(c);
}
int intcmp(const void *x, const void *y) {
return cmpint(&x, &y);
}
unsigned inthash(const void *x) {
return *(int *)x;
}