pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / elvis / ref.c
blob8c6b57aa0ce7b6e94a28d2ebbe0933b526136920
1 /* ref2.c */
3 /* This is a totally rewritten version of ref. This version looks for the
4 * desired function name in the "tags" file, and then reads the header out
5 * from the source file. There is no longer any need for a "refs" file.
7 * Usage: ref [-a] [-t] [-f file] [-c class] tag
8 * Options: -t output tag info, not the description
9 * -f file default filename for static functions
10 * -c class default class names for class functions
13 #include <stdio.h>
14 #include "config.h"
15 extern char *getenv();
16 extern char *fgets();
19 /* This is the default path that is searched for tags */
20 #if OSK
21 # define DEFTAGPATH ".:/dd/defs:/dd/defs/sys:/dd/usr/src/lib:../lib:/dd/usr/lib"
22 #else
23 # if ANY_UNIX
24 # define DEFTAGPATH ".:/usr/include:/usr/include/sys:/usr/src/lib:../lib:/usr/local/lib"
25 # else
26 # if MSDOS || TOS
27 # define DEFTAGPATH ".;C:\\include;C:\\include\\sys;C:\\lib;..\\lib"
28 # define SEP ';'
29 # else
30 # if AMIGA
31 # define DEFTAGPATH ".;Include:;Include:sys"
32 # define SEP ';'
33 # else /* any other OS */
34 # define DEFTAGPATH "."
35 # endif
36 # endif
37 # endif
38 #endif
40 #ifndef SEP
41 # define SEP ':'
42 #endif
45 /* These variables reflect the command-line options given by the user. */
46 int taginfo; /* boolean: give only the tag info? (not header?) */
47 char *def_file; /* default filename for static functions */
48 char *def_class; /* default classname for class members */
49 int colons; /* #colons in tag: 0=normal, 1=static, 2=member */
51 /* This function checks for a tag in the "tags" file of given directory.
52 * If the tag is found, then it returns a pointer to a static buffer which
53 * contains the filename, a tab character, and a linespec for finding the
54 * the tag. If the tag is not found in the "tags" file, or if the "tags"
55 * file cannot be opened or doesn't exist, then this function returns NULL.
57 char *cktagdir(tag, dir)
58 char *tag; /* name of the tag to look for */
59 char *dir; /* name of the directory to check */
61 char buf[BLKSIZE];
62 static char found[BLKSIZE];
63 FILE *tfile;
64 int len;
66 #if AMIGA
67 if (dir[strlen(dir) - 1] == COLON)
68 sprintf(buf, "%s%s", dir, TAGS); /* no slash after colon. */
69 else
70 #endif
71 /* construct the name of the "tags" file in this directory */
72 sprintf(buf, "%s%c%s", dir, SLASH, TAGS);
74 /* Try to open the tags file. Return NULL if can't open */
75 #if AMIGA
76 if (buf[0] == '.' && buf[1] == SLASH)
77 tfile = fopen(&buf[2], "r");
78 else
79 #endif
80 tfile = fopen(buf, "r");
81 if (!tfile)
83 return (char *)0;
86 /* compute the length of the tagname once */
87 len = strlen(tag);
89 /* read lines until we get the one for this tag */
90 found[0] = '\0';
91 while (fgets(buf, sizeof buf, tfile))
93 /* is this the one we want? */
94 if (!strncmp(buf, tag, len) && buf[len] == '\t')
96 /* we've found a match -- remember it */
97 strcpy(found, buf);
99 /* if there is no default file, or this match is in
100 * the default file, then we've definitely found the
101 * one we want. Break out of the loop now.
103 if (!def_file || !strncmp(&buf[len + 1], def_file, strlen(def_file)))
105 break;
110 /* we're through reading */
111 fclose(tfile);
113 /* if there's anything in found[], use it */
114 if (found[0])
116 return &found[len + 1];
119 /* else we didn't find it */
120 return (char *)0;
123 /* This function reads a single textline from a binary file. It returns
124 * the number of bytes read, or 0 at EOF.
126 int getline(buf, limit, fp)
127 char *buf; /* buffer to read into */
128 int limit; /* maximum characters to read */
129 FILE *fp; /* binary stream to read from */
131 int bytes; /* number of bytes read so far */
132 int ch; /* single character from file */
134 for (bytes = 0, ch = 0; ch != '\n' && --limit > 0 && (ch = getc(fp)) != EOF; bytes++)
136 #if MSDOS || TOS
137 /* since this is a binary file, we'll need to manually strip CR's */
138 if (ch == '\r')
140 continue;
142 #endif
143 *buf++ = ch;
145 *buf = '\0';
147 return bytes;
151 /* This function reads a source file, looking for a given tag. If it finds
152 * the tag, then it displays it and returns TRUE. Otherwise it returns FALSE.
153 * To display the tag, it attempts to output any introductory comment, the
154 * tag line itself, and any arguments. Arguments are assumed to immediately
155 * follow the tag line, and start with whitespace. Comments are assumed to
156 * start with lines that begin with "/*", "//", "(*", or "--", and end at the
157 * tag line or at a blank line.
159 int lookup(dir, entry)
160 char *dir; /* name of the directory that contains the source */
161 char *entry; /* source filename, <Tab>, linespec */
163 char buf[BLKSIZE]; /* pathname of sourcefile */
164 long lnum; /* line number */
165 long here; /* seek position where current line began */
166 long comment; /* seek position of introductory comment, or -1L */
167 FILE *sfile; /* used for reading the source file */
168 int len; /* length of string */
169 char *ptr;
172 /* construct the pathname of the source file */
173 strcpy(buf, dir);
174 ptr = buf + strlen(buf);
175 #if AMIGA
176 if (ptr[-1] != COLON)
177 #endif
178 *ptr++ = SLASH;
179 while (*entry != '\t')
181 *ptr++ = *entry++;
183 *ptr = '\0';
184 entry++;
186 /* searching for string or number? */
187 if (*entry >= '0' && *entry <= '9')
189 /* given a specific line number */
190 lnum = atol(entry);
191 entry = (char *)0;
193 else
195 /* given a string -- strip off "/^" and "$/\n" */
196 entry += 2;
197 len = strlen(entry) - 2;
198 if (entry[len - 1] == '$')
200 entry[len - 1] = '\n';
202 lnum = 0L;
205 /* Open the file. Note that we open the file in binary mode even
206 * though we know it is a text file, because ftell() and fseek()
207 * don't work on text files.
209 #if MSDOS || TOS
210 sfile = fopen(buf, "rb");
211 #else
212 # if AMIGA
213 if (buf[0] == '.' && buf[1] == SLASH)
214 sfile = fopen(&buf[2], "r");
215 else
216 # endif
217 sfile = fopen(buf, "r");
218 #endif
219 if (!sfile)
221 /* can't open the real source file. Try "refs" instead */
222 #if AMIGA
223 if (dir[strlen(dir) - 1] == COLON)
224 sprintf(buf, "%srefs", dir);
225 else
226 #endif
227 sprintf(buf, "%s%crefs", dir, SLASH);
228 #if MSDOS || TOS
229 sfile = fopen(buf, "rb");
230 #else
231 # if AMIGA
232 if (buf[0] == '.' && buf[1] == SLASH)
233 sfile = fopen(&buf[2], "r");
234 else
235 # endif
236 sfile = fopen(buf, "r");
237 #endif
238 if (!sfile)
240 /* failed! */
241 return 0;
245 /* search the file */
246 for (comment = -1L; here = ftell(sfile), getline(buf, BLKSIZE, sfile) > 0; )
248 /* Is this the start/end of a comment? */
249 if (comment == -1L)
251 /* starting a comment? */
252 if (buf[0] == '/' && buf[1] == '*'
253 || buf[0] == '/' && buf[1] == '/'
254 || buf[0] == '(' && buf[1] == '*'
255 || buf[0] == '-' && buf[1] == '-')
257 comment = here;
260 else
262 /* ending a comment? */
263 if (buf[0] == '\n' || buf[0] == '#')
265 comment = -1L;
269 /* is this the tag line? */
270 if (--lnum == 0L || (entry && !strncmp(buf, entry, len)))
272 /* if there were introductory comments, show them */
273 if (comment != -1L)
275 fseek(sfile, comment, 0);
276 while (comment != here)
278 getline(buf, BLKSIZE, sfile);
279 fputs(buf, stdout);
280 comment = ftell(sfile);
283 /* re-fetch the tag line */
284 fgets(buf, BLKSIZE, sfile);
287 /* show the tag line */
288 fputs(buf, stdout);
290 /* show any argument lines */
291 while (getline(buf, BLKSIZE, sfile) > 0
292 && buf[0] != '#'
293 && strchr(buf, '{') == (char *)0)
295 fputs(buf, stdout);
298 /* Done! Close the file, and return TRUE */
299 fclose(sfile);
300 return 1;
304 /* not found -- return FALSE */
305 return 0;
308 /* This function searches through the entire search path for a given tag.
309 * If it finds the tag, then it displays the info and returns TRUE;
310 * otherwise it returns FALSE.
312 int find(tag)
313 char *tag; /* the tag to look up */
315 char *tagpath;
316 char dir[80];
317 char *ptr;
318 int len;
320 if (colons == 1)
322 /* looking for static function -- only look in current dir */
323 tagpath = ".";
325 else
327 /* get the tagpath from the environment. Default to DEFTAGPATH */
328 tagpath = getenv("TAGPATH");
329 if (!tagpath)
331 tagpath = DEFTAGPATH;
335 /* for each entry in the path... */
336 while (*tagpath)
338 /* Copy the entry into the dir[] buffer */
339 for (ptr = dir; *tagpath && *tagpath != SEP; tagpath++)
341 *ptr++ = *tagpath;
343 if (*tagpath == SEP)
345 tagpath++;
348 /* if the entry ended with "/tags", then strip that off */
349 len = strlen(TAGS);
350 if (&dir[len] < ptr && ptr[-len - 1] == SLASH && !strncmp(&ptr[-len], TAGS, len))
352 ptr -= len + 1;
355 /* if the entry is now an empty string, then assume "." */
356 if (ptr == dir)
358 *ptr++ = '.';
360 *ptr = '\0';
362 /* look for the tag in this path. If found, then display it
363 * and exit.
365 ptr = cktagdir(tag, dir);
366 if (ptr)
368 /* just supposed to display tag info? */
369 if (taginfo)
371 /* then do only that! */
372 if (strcmp(dir, "."))
374 printf("%s%c%s", dir, SLASH, ptr);
376 else
378 /* avoid leading "./" if possible */
379 fputs(ptr, stdout);
381 return 1;
383 else
385 /* else look up the declaration of the thing */
386 return lookup(dir, ptr);
391 /* if we get here, then the tag wasn't found anywhere */
392 return 0;
395 void usage()
397 fputs("usage: ref [-a] [-t] [-c class] [-f file] tag\n", stderr);
398 fputs(" -a function's args may be flush against left margin\n", stderr);
399 fputs(" -t output tag info, instead of the function header\n", stderr);
400 fputs(" -f File tag might be a static function in File\n", stderr);
401 fputs(" -c Class tag might be a member of class Class\n", stderr);
402 exit(2);
406 int countcolons(str)
407 char *str;
409 while (*str != ':' && *str)
411 str++;
413 if (str[0] != ':')
415 return 0;
417 else if (str[1] != ':')
419 return 1;
421 return 2;
424 int main(argc, argv)
425 int argc;
426 char **argv;
428 char def_tag[100]; /* used to build tag name with default file/class */
429 int i;
431 /* parse flags */
432 for (i = 1; i < argc && argv[i][0] == '-'; i++)
434 switch (argv[i][1])
436 case 't':
437 taginfo = 1;
438 break;
440 case 'f':
441 if (argv[i][2])
443 def_file = &argv[i][2];
445 else if (++i < argc)
447 def_file = argv[i];
449 else
451 usage();
453 break;
455 case 'c':
456 if (argv[i][2])
458 def_class = &argv[i][2];
460 else if (++i < argc)
462 def_class = argv[i];
464 else
466 usage();
468 break;
470 default:
471 usage();
475 /* if no tag was given, complain */
476 if (i + 1 != argc)
478 usage();
481 /* does the tag have an explicit class or file? */
482 colons = countcolons(argv[i]);
484 /* if not, then maybe try some defaults */
485 if (colons == 0)
487 /* try a static function in the file first */
488 if (def_file)
490 sprintf(def_tag, "%s:%s", def_file, argv[i]);
491 colons = 1;
492 if (find(def_tag))
494 exit(0);
498 /* try a member function for a class */
499 if (def_class)
501 sprintf(def_tag, "%s::%s", def_class, argv[i]);
502 colons = 2;
503 if (find(def_tag))
505 exit(0);
509 /* oh, well */
510 colons = 0;
513 /* find the tag */
514 if (find(argv[i]))
516 exit(0);
519 exit(1);
520 /*NOTREACHED*/