-
Notifications
You must be signed in to change notification settings - Fork 0
/
xref.c
156 lines (139 loc) · 3.34 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
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
/******************************************************
*xref 用于输出其各个输入文件中标识符的交叉引用列表
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "atom.h"
#include "table.h"
#include "set.h"
#include "mem.h"
int getword(FILE *fp, char *buf, int size, int first(int c),int rest(int c));
int compare(const void *x,const void *y);
void print(Table_T table);
int first(int c);
int rest(int c);
void xref(const char *, FILE *,Table_T);
int getword(FILE *fp, char *buf, int sisz,int first(int c),int rest(int c));
int cmpint(const void *x,const void *y);
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)",argv[0],argv[i],strerror(errno));
return EXIT_FAILURE;
}else{
xref(argv[i],fp,identifiers);
fclose(fp);
}
}
if(argc == 1)
xref(NULL,stdin,identifiers);
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 getword(FILE *fp, char *buf, int size, int first(int c),int rest(int c)){
int i=0,c;
assert(fp && buf && size > 1 && first && rest);
c = getc(fp);
for(;c!=EOF;c=getc(fp))
if(first(c)){
if(i<size-1)
buf[i++] = c;
c = getc(fp);
break;
}
for(;c!=EOF && rest(c);c=getc(fp))
if(i<size-1)
buf[i++] = c;
if(i<size)
buf[i] = '\0';
else
buf[size-1] = '\0';
if(c != EOF)
ungetc(c,fp);
return i>0;
}
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");
}
}
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;
}