Indentation fix, cleanup.
[AROS.git] / tools / cxref / query / input.c
blob51bce0aae15b5e6b52c42d4a49e22285ec2f8736
1 /***************************************
2 $Header$
4 C Cross Referencing & Documentation tool. Version 1.5b.
5 ******************/ /******************
6 Written by Andrew M. Bishop
8 This file Copyright 1995,96,97,99 Andrew M. Bishop
9 It may be distributed under the GNU Public License, version 2, or
10 any higher version. See section COPYING of the GNU Public license
11 for conditions under which this file may be redistributed.
12 ***************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <limits.h>
19 #define TYPE_MAX_LEN 256 /*+ The maximum type definition length +*/
20 #define FUNC_MAX_LEN 64 /*+ The maximum function name length. +*/
21 #if defined(PATH_MAX) && defined(NAME_MAX)
22 #define FILE_MAX_LEN (PATH_MAX+NAME_MAX) /*+ The maximum filename length. +*/
23 #elif defined(PATH_MAX)
24 #define FILE_MAX_LEN (PATH_MAX+256) /*+ The maximum filename length. +*/
25 #else
26 #define FILE_MAX_LEN 512 /*+ The maximum filename length. +*/
27 #endif
29 #include "../memory.h"
30 #include "../datatype.h"
31 #include "../cxref.h"
32 #include "query.h"
34 /*+ The names of the function cross reference files. +*/
35 #define XREF_FUNC_FILE ".function"
37 /*+ The names of the variable cross reference files. +*/
38 #define XREF_VAR_FILE ".variable"
40 /*+ The names of the include cross reference files. +*/
41 #define XREF_INC_FILE ".include"
43 /*+ The names of the type cross reference files. +*/
44 #define XREF_TYPE_FILE ".typedef"
46 /*+ The command line switch that sets the amount of cross referencing to do. +*/
47 extern int option_xref;
49 /*+ The command line switch for the output name, +*/
50 extern char *option_odir, /*+ The directory to use. +*/
51 *option_name; /*+ The base part of the name. +*/
53 extern File *files; /*+ The files that are queried. +*/
54 extern int n_files; /*+ The number of files referenced. +*/
56 extern Function *functions; /*+ The functions that are queried. +*/
57 extern int n_functions; /*+ The number of functions referenced. +*/
59 extern Variable *variables; /*+ The variables that are queried. +*/
60 extern int n_variables; /*+ The number of variables referenced. +*/
62 extern Typedef *typedefs; /*+ The type definitions that are queried. +*/
63 extern int n_typedefs; /*+ The number of typedefs referenced. +*/
65 /* Local functions */
67 static void cross_reference_files(void);
68 static void cross_reference_functions(void);
69 static void cross_reference_variables(void);
71 /*++++++++++++++++++++++++++++++++++++++
72 Read in all the information from the cross reference files.
73 ++++++++++++++++++++++++++++++++++++++*/
75 void LoadInCrossRefs(void)
77 FILE *in;
78 char *ifile;
80 /* Format: filename [[%]include1] [[%]include2] ... : Files include1, include2, ... are included in filename;
81 those with a % are local. */
83 /* First do the files */
85 char include[FILE_MAX_LEN],filename[FILE_MAX_LEN+1],ch;
87 ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_INC_FILE);
89 in =fopen(ifile,"r");
91 if(!in)
92 {fprintf(stderr,"cxref-query: Failed to open the include cross reference file '%s'\n",ifile);exit(1);}
94 while(fscanf(in,"%s%c",filename,&ch)==2)
96 if(n_files)
97 files=(File*)Realloc(files,(n_files+1)*sizeof(File*));
98 else
99 files=(File*)Malloc(sizeof(File*));
101 files[n_files]=(File)Calloc(1,sizeof(struct _File));
102 files[n_files]->name=MallocString(filename);
103 files[n_files]->inc_in=NewStringList();
104 files[n_files]->f_refs=NewStringList2();
105 files[n_files]->v_refs=NewStringList2();
107 while(ch==' ')
109 Include inc=(Include)Calloc(1,sizeof(struct _Include));
111 fscanf(in,"%s%c",include,&ch);
113 if(include[0]=='%')
114 {inc->scope=LOCAL;
115 inc->name=MallocString(&include[1]);}
116 else
117 {inc->scope=GLOBAL;
118 inc->name=MallocString(include);}
120 AddToLinkedList(files[n_files]->includes,Include,inc);
122 n_files++;
125 cross_reference_files();
127 fclose(in);
130 /* Format: filename funcname scope [[%][&]funcname1] [[%][&]funcname2] ... : The function funcname in file filename
131 calls or references functions funcname1, funcname2 ... ; those with a % are local, with a & are references. */
132 /* Format: filename $ 0 [[%]&funcname1] [[%]&funcname2] ... : The file references functions funcname1, funcname2 ... ;
133 those with a % are local. */
135 /* Now do the functions */
137 char ch,funcname[FUNC_MAX_LEN+1],filename[FILE_MAX_LEN+1],called[FUNC_MAX_LEN+1];
138 int scope;
140 ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_FUNC_FILE);
142 in =fopen(ifile,"r");
144 if(!in)
145 {fprintf(stderr,"cxref-query: Failed to open the functional cross reference file '%s'\n",ifile);exit(1);}
147 while(fscanf(in,"%s %s %d%c",filename,funcname,&scope,&ch)==4)
149 int i;
150 StringList2 f_refs;
152 for(i=0;i<n_files;i++)
153 if(!strcmp(files[i]->name,filename))
154 break;
156 if(funcname[0]=='$')
157 f_refs=files[i]->f_refs;
158 else
160 if(n_functions)
161 functions=(Function*)Realloc(functions,(n_functions+1)*sizeof(Function*));
162 else
163 functions=(Function*)Malloc(sizeof(Function*));
165 functions[n_functions]=(Function)Calloc(1,sizeof(struct _Function));
167 AddToLinkedList(files[i]->functions,Function,functions[n_functions]);
169 functions[n_functions]->comment=MallocString(filename); /* Use comment field for filename */
170 functions[n_functions]->name=MallocString(funcname);
171 functions[n_functions]->scope=scope;
172 functions[n_functions]->used=NewStringList2();
173 functions[n_functions]->calls=NewStringList2();
174 functions[n_functions]->called=NewStringList2();
175 functions[n_functions]->f_refs=NewStringList2();
176 functions[n_functions]->v_refs=NewStringList2();
178 f_refs=functions[n_functions]->f_refs;
181 while(ch==' ')
183 char* c;
184 fscanf(in,"%s%c",called,&ch);
186 c=called;
187 if(c[0]=='%') c++;
188 if(c[0]=='&')
190 if(c==called)
191 AddToStringList2(f_refs,c+1,NULL,1,1);
192 else
193 AddToStringList2(f_refs,c+1,filename,1,1);
195 else
197 if(c==called)
198 AddToStringList2(functions[n_functions]->calls,c,NULL,1,1);
199 else
200 AddToStringList2(functions[n_functions]->calls,c,filename,1,1);
204 if(funcname[0]!='$')
205 n_functions++;
208 cross_reference_functions();
210 fclose(in);
213 /* Format: filename varname scope [$] [[%]funcname1] [[%]funcname2] ... : variable varname is used in
214 the file filename if $, and functions funcname1, funcname2 ... Those with a % are local. */
216 /* Now do the variables */
218 char varname[FUNC_MAX_LEN+1],filename[FILE_MAX_LEN+1],funcname[FUNC_MAX_LEN+1],ch;
219 int scope;
221 ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_VAR_FILE);
223 in =fopen(ifile,"r");
225 if(!in)
226 {fprintf(stderr,"cxref-query: Failed to open the variable cross reference file '%s'\n",ifile);exit(1);}
228 while(fscanf(in,"%s %s %d%c",filename,varname,&scope,&ch)==4)
230 int i;
232 if(n_variables)
233 variables=(Variable*)Realloc(variables,(n_variables+1)*sizeof(Variable*));
234 else
235 variables=(Variable*)Malloc(sizeof(Variable*));
237 variables[n_variables]=(Variable)Calloc(1,sizeof(struct _Variable));
239 for(i=0;i<n_files;i++)
240 if(!strcmp(files[i]->name,filename))
241 AddToLinkedList(files[i]->variables,Variable,variables[n_variables]);
243 variables[n_variables]->comment=MallocString(filename); /* Use comment field for filename */
244 variables[n_variables]->name=MallocString(varname);
245 variables[n_variables]->visible=NewStringList2();
246 variables[n_variables]->used=NewStringList2();
247 variables[n_variables]->scope=scope;
249 while(ch==' ')
251 fscanf(in,"%s%c",funcname,&ch);
253 if(funcname[0]=='$')
254 AddToStringList2(variables[n_variables]->used,"$",filename,1,0);
255 else
256 if(funcname[0]=='%')
257 AddToStringList2(variables[n_variables]->used,&funcname[1],filename,1,1);
258 else
259 AddToStringList2(variables[n_variables]->used,funcname,NULL,1,1);
261 n_variables++;
264 cross_reference_variables();
266 fclose(in);
269 /* Format: filename typename type... : For a typedef type. */
270 /* Format: filename # type... : For a non typedef type. */
272 /* Now do the types */
274 char typename[FILE_MAX_LEN+1],filename[FILE_MAX_LEN+1],typetype[TYPE_MAX_LEN+1];
276 ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_TYPE_FILE);
278 in =fopen(ifile,"r");
280 if(!in)
281 {fprintf(stderr,"cxref-query: Failed to open the typedef reference file '%s'\n",ifile);exit(1);}
283 while(fscanf(in,"%s %s",filename,typename)==2)
285 int i;
287 fgets(typetype,TYPE_MAX_LEN,in);
288 typetype[strlen(typetype)-1]=0;
290 if(n_typedefs)
291 typedefs=(Typedef*)Realloc(typedefs,(n_typedefs+1)*sizeof(Typedef*));
292 else
293 typedefs=(Typedef*)Malloc(sizeof(Typedef*));
295 typedefs[n_typedefs]=(Typedef)Calloc(1,sizeof(struct _Typedef));
297 for(i=0;i<n_files;i++)
298 if(!strcmp(files[i]->name,filename))
299 AddToLinkedList(files[i]->typedefs,Typedef,typedefs[n_typedefs]);
301 typedefs[n_typedefs]->comment=MallocString(filename); /* Use comment field for filename */
303 if(typename[0]!='#')
305 typedefs[n_typedefs]->name=MallocString(typename);
306 typedefs[n_typedefs]->type=MallocString(&typetype[1]);
308 else
310 typedefs[n_typedefs]->name=MallocString(&typetype[1]);
311 typedefs[n_typedefs]->type=NULL;
314 n_typedefs++;
317 fclose(in);
323 /*++++++++++++++++++++++++++++++++++++++
324 Performs all of the cross referencing between files, includes and included in.
325 ++++++++++++++++++++++++++++++++++++++*/
327 static void cross_reference_files(void)
329 int i;
331 for(i=0;i<n_files;i++)
333 int j;
334 Include inc=files[i]->includes;
336 while(inc)
338 for(j=0;j<n_files;j++)
339 if(!strcmp(inc->name,files[j]->name))
341 inc->includes=files[j]->includes;
342 AddToStringList(files[j]->inc_in,files[i]->name,1,1);
344 inc=inc->next;
350 /*++++++++++++++++++++++++++++++++++++++
351 Performs all of the cross referencing between global functions and functions that they call.
352 ++++++++++++++++++++++++++++++++++++++*/
354 static void cross_reference_functions(void)
356 int i1,j1,i2;
358 for(i1=0;i1<n_functions;i1++)
360 Function func1=functions[i1];
362 for(j1=0;j1<func1->calls->n;j1++)
364 if(!func1->calls->s2[j1])
365 for(i2=0;i2<n_functions;i2++)
367 Function func2=functions[i2];
369 if(!strcmp(func1->calls->s1[j1],func2->name))
371 func1->calls->s2[j1]=MallocString(func2->comment);
372 break;
376 if(func1->calls->s2[j1])
377 for(i2=0;i2<n_functions;i2++)
379 Function func2=functions[i2];
381 if(!strcmp(func1->calls->s1[j1],func2->name) && !strcmp(func1->calls->s2[j1],func2->comment))
383 AddToStringList2(func2->called,func1->name,func1->comment,1,1);
384 break;
389 for(j1=0;j1<func1->f_refs->n;j1++)
391 if(!func1->f_refs->s2[j1])
392 for(i2=0;i2<n_functions;i2++)
394 Function func2=functions[i2];
396 if(!strcmp(func1->f_refs->s1[j1],func2->name))
398 func1->f_refs->s2[j1]=MallocString(func2->comment);
399 break;
403 if(func1->f_refs->s2[j1])
404 for(i2=0;i2<n_functions;i2++)
406 Function func2=functions[i2];
408 if(!strcmp(func1->f_refs->s1[j1],func2->name) && !strcmp(func1->f_refs->s2[j1],func2->comment))
410 AddToStringList2(func2->used,func1->name,func1->comment,1,1);
411 break;
417 for(i1=0;i1<n_files;i1++)
419 File file1=files[i1];
421 for(j1=0;j1<file1->f_refs->n;j1++)
423 if(!file1->f_refs->s2[j1])
424 for(i2=0;i2<n_functions;i2++)
426 Function func2=functions[i2];
428 if(!strcmp(file1->f_refs->s1[j1],func2->name))
430 file1->f_refs->s2[j1]=MallocString(func2->comment);
431 break;
435 if(file1->f_refs->s2[j1])
436 for(i2=0;i2<n_functions;i2++)
438 Function func2=functions[i2];
440 if(!strcmp(file1->f_refs->s1[j1],func2->name) && !strcmp(file1->f_refs->s2[j1],func2->comment))
442 AddToStringList2(func2->used,"$",file1->name,1,1);
443 break;
451 /*++++++++++++++++++++++++++++++++++++++
452 Performs all of the cross referencing between global variables and functions that use them.
453 ++++++++++++++++++++++++++++++++++++++*/
455 static void cross_reference_variables(void)
457 int i1,j1,i2;
459 for(i1=0;i1<n_variables;i1++)
461 Variable var1=variables[i1];
463 for(j1=0;j1<var1->used->n;j1++)
465 if(var1->used->s1[j1][0]!='$' && !var1->used->s2[j1])
466 for(i2=0;i2<n_functions;i2++)
468 Function func2=functions[i2];
470 if(!strcmp(var1->used->s1[j1],func2->name))
472 var1->used->s2[j1]=MallocString(func2->comment);
473 break;
477 if(var1->used->s1[j1][0]=='$')
478 for(i2=0;i2<n_files;i2++)
480 File file2=files[i2];
482 if(!strcmp(var1->used->s2[j1],file2->name))
484 AddToStringList2(file2->v_refs,var1->name,var1->comment,1,1);
485 break;
488 else if(var1->used->s2[j1])
489 for(i2=0;i2<n_functions;i2++)
491 Function func2=functions[i2];
493 if(!strcmp(var1->used->s1[j1],func2->name) && !strcmp(var1->used->s2[j1],func2->comment))
495 AddToStringList2(func2->v_refs,var1->name,var1->comment,1,1);
496 break;