Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 740 lines (653 sloc) 18.133 kb
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
1 /* ref.c */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
2
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
3 /* This program locates a tag, and displays its context. A handy reference. */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
4
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
5 #include "elvis.h"
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
6
7 /* This is the default path that is searched for tags */
8 #if OSK
9 # define DEFTAGPATH ".:/dd/defs:/dd/defs/sys:/dd/usr/src/lib:../lib:/dd/usr/lib"
10 #else
11 # if ANY_UNIX
12 # define DEFTAGPATH ".:/usr/include:/usr/include/sys:/usr/src/lib:../lib:/usr/local/lib"
13 # else
14 # if MSDOS || TOS || OS2
15 # define DEFTAGPATH ".;C:\\include;C:\\include\\sys;C:\\lib;..\\lib"
16 # else
17 # if AMIGA
18 # define DEFTAGPATH ".;Include:;Include:sys"
19 # else /* any other OS */
20 # define DEFTAGPATH "."
21 # endif
22 # endif
23 # endif
24 #endif
25
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
26 /* maximum length of a path list */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
27 #define MAXPATH 2048
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
28
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
29 /* compile some of the osdir functions into this module */
30 #define JUST_DIRPATH
31 #include "osdir.c"
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
32
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
33 /* This data type is used for classifying lines */
34 typedef enum
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
35 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
36 LC_BLANK, /* an empty line */
37 LC_COMMENT, /* a comment, or continuation of a comment */
38 LC_PARTIAL, /* partial definition -- no semicolon, or in brackets */
39 LC_COMPLETE /* anything else */
40 } LINECLS;
41
42
43 #if USE_PROTOTYPES
44 static void usage(char *argv0);
45 static char *getline(FILE *fp);
46 static void store(char *line, char **list);
47 static LINECLS classify(char *line, LINECLS prev);
48 static void lookup(TAG *tag);
49 static void add_to_path(char *file);
50 int main(int argc, char **argv);
51 #endif /* USE_PROTOTYPES */
52
53
54 /* support for elvis' ctype macros */
55 #ifdef ELVCT_DIGIT
56 CHAR elvct_class[256] = {
57 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
58 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59 ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,
60 ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,ELVCT_DIGIT,
61 0,0,0,0,0,0,0,
62 ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,
63 ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,
64 ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,
65 ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,
66 ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,ELVCT_UPPER,
67 ELVCT_UPPER,
68 0,0,0,0,0,0,
69 ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,
70 ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,
71 ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,
72 ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,
73 ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,ELVCT_LOWER,
74 ELVCT_LOWER
75 };
76 #endif
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
77
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
78
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
79 /* These reflect the command-line options */
80 static int output_tag_info;
81 static int output_verbose_info;
82 static int output_html_browser;
83 static int output_all_matches;
84 static int omit_comment_lines;
85 static int omit_other_lines;
86 static int search_all_files;
87 static char tag_path[MAXPATH] = DEFTAGPATH;
88 static long tag_length;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
89
90
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
91 /* These store lines which preceed the tag definition. */
92 static char *comments[20]; /* comment lines before tag */
93 static int ncomments; /* number of comment lines */
94 static char *members[20]; /* partial definitions before tag */
95 static int nmembers; /* number of partial definition lines */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
96
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
97 static void usage(argv0)
98 char *argv0; /* name of program */
99 {
100 fprintf(stderr, "Usage: %s [options] [restrictions]...\n", argv0);
101 fprintf(stderr, "Options:\n");
102 fprintf(stderr, " -t Output tag info, instead of the function header\n");
103 fprintf(stderr, " -v Output verbose tag info, instead of the function header\n");
104 fprintf(stderr, " -h Output HTML browser info, instead of the function header\n");
105 fprintf(stderr, " -c Don't output introductory comments\n");
106 fprintf(stderr, " -d Don't output other lines of the definition\n");
107 fprintf(stderr, " -a List all matches (else just the most likely one)\n");
108 fprintf(stderr, " -s Search all tags files (else stop after first with matches)\n");
109 fprintf(stderr, " -p tagpath List of directories or tags files to search\n");
110 fprintf(stderr, " -l taglength Only check the first 'taglength' characters of tag names\n");
111 fprintf(stderr, "Restrictions:\n");
112 fprintf(stderr, " tag A tag to search for, short for tagname:tag\n");
113 fprintf(stderr, " attrib:value An optional attribute (global tags permitted)\n");
114 fprintf(stderr, " attrib:=value A mandatory attribute (global tags rejected)\n");
115 fprintf(stderr, " attrib:/value An optional attribute, but require as substring of tagaddr\n");
116 fprintf(stderr, " attrib:+value List tags with given attribute first (more likely)\n");
117 fprintf(stderr, " attrib:-value List tags with given attribute last (less likely)\n");
118 fprintf(stderr, "A single attribute can be given multiple acceptable values. The easiest way\n");
119 fprintf(stderr, "to specify them is \"attrib:value,value,value\". This works for any of the\n");
120 fprintf(stderr, "restriction operators. A global tag is one which has no value for a given\n");
121 fprintf(stderr, "attribute; e.g., for the \"class\" attribute, a tag with no class is global.\n");
122 exit(0);
123 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
124
125
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
126 /* display a fatal error message from safe.c */
127 #if USE_PROTOTYPES
128 void msg(MSGIMP type, char *msg, ...)
129 #else
130 void msg(type, msg)
131 MSGIMP type;
132 char *msg;
133 #endif
134 {
135 fprintf(stderr, "%s\n", msg);
136 abort();
137 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
138
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
139
140 /* some custom versions of elvis text I/O functions */
141 static FILE *iofp;
142 #ifdef DEBUG_ALLOC
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
143 ELVBOOL _ioopen(file, line, name, rwa, prgsafe, force, eol)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
144 char *file;
145 int line;
146 #else
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
147 ELVBOOL ioopen(name, rwa, prgsafe, force, eol)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
148 #endif
149 char *name; /* name of file to open */
150 _char_ rwa; /* ignored; 'r'=read, 'w'=write, 'a'=append */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
151 ELVBOOL prgsafe;/* ignored; safe to use "!prg" as file name? */
152 ELVBOOL force; /* ignored; okay to overwrite existing files? */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
153 _char_ eol; /* ignored; open in binary mode? */
154 {
155 iofp = fopen(name, "r");
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
156 return (ELVBOOL)(iofp != NULL);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
157 }
158 int ioread(iobuf, len)
159 CHAR *iobuf; /* Input buffer */
160 int len; /* maximum number of CHARs to read into iobuf */
161 {
162 return fread(iobuf, sizeof(CHAR), len, iofp);
163 }
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
164 ELVBOOL ioclose()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
165 {
166 fclose(iofp);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
167 return ElvTrue;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
168 }
169
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
170
171 /* This function reads a single line, and replaces the terminating newline with
172 * a '\0' byte. The string will be in a static buffer. Returns NULL at EOF.
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
173 */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
174 static char *getline(fp)
175 FILE *fp;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
176 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
177 int ch;
178 static char buf[1024];
179 int len;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
180
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
181 /* initialize ch just to avoid a bogus compiler warning */
182 ch = 0;
183
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
184 for (len = 0; len < QTY(buf) - 1 && (ch = getc(fp)) != EOF && ch != '\n'; )
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
185 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
186 buf[len++] = ch;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
187 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
188 if (ch == EOF)
189 return NULL;
190 buf[len] = '\0';
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
191 if (len >= 1 && buf[len - 1] == '\r')
192 buf[len - 1] = '\0';
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
193 return buf;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
194 }
195
196
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
197 /* Store a line in a list, or clobber the list. */
198 static void store(line, list)
199 char *line; /* the text to store, or NULL to clobber */
200 char **list; /* either comments[] or members[] */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
201 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
202 int qty; /* size of the array */
203 int *nptr; /* pointer to number of items already in the list */
204 int i; /* in case we need to scroll the list */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
205
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
206
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
207 /* get the list specifics */
208 if (list == comments)
209 qty = QTY(comments), nptr = &ncomments;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
210 else
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
211 qty = QTY(members), nptr = &nmembers;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
212
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
213 /* if supposed to clobber, then clobber */
214 if (!line)
215 {
216 for (i = 0; i < *nptr; i++)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
217 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
218 safefree(list[i]);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
219 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
220 *nptr = 0;
221 return;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
222 }
223
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
224 /* if list is full, then scroll it */
225 if (*nptr == qty)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
226 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
227 safefree(list[0]);
228 for (i = 1; i < *nptr; i++)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
229 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
230 list[i - 1] = list[i];
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
231 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
232 (*nptr)--;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
233 }
234
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
235 /* add the new line to the list */
236 list[(*nptr)++] = safedup(line);
237 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
238
239
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
240 /* This function classifies a line */
241 static LINECLS classify(line, prev)
242 char *line; /* the line to classify */
243 LINECLS prev; /* classification of previous line */
244 {
245 char *front; /* line, after skipping indentation */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
246
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
247 /* find the front of the line */
248 for (front = line; *front == ' ' || *front == '\t'; front++)
249 {
250 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
251
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
252 /* blank line? */
253 if (!*front)
254 return LC_BLANK;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
255
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
256 /* is it the start of a comment? */
257 if ((front[0] == '/' && (front[1] == '*' || front[1] == '/'))
258 || (front[0] == '(' && front[1] == '*')
259 || (front[0] == '-' && front[1] == '-'))
260 {
261 return LC_COMMENT;
262 }
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
263
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
264 /* is it a continuation of a comment? */
265 if (prev == LC_COMMENT
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
266 && !(front[0] == '#' || elvalnum(*front)))
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
267 {
268 return LC_COMMENT;
269 }
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
270
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
271 /* is it a partial declaration? */
272 if (*front == '#'
273 ? front[strlen(front) - 1] == '\\'
274 : (strchr(front, ';') == NULL
275 || (prev == LC_PARTIAL && front != line)))
276 {
277 return LC_PARTIAL;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
278 }
279
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
280 /* anything else is considered to be a complete declaration */
281 return LC_COMPLETE;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
282 }
283
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
284 /* display the source lines which define a given tag */
285 static void lookup(tag)
286 TAG *tag; /* the tag to be displayed */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
287 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
288 char *line; /* current line from file */
289 long lnum; /* current line number */
290 char *l, *t; /* for scanning chars in line and tag->TAGADDR*/
291 FILE *fp; /* source file */
292 long taglnum;/* line number of number tag address, or 0 */
293 char *tagline;/* text form of regexp tag address */
294 LINECLS lc; /* line classification */
97d8998 @mbert Import Elvis 2.1_4 (written by Steve Kirkendall)
authored
295 int len;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
296 int i;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
297
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
298 /* some initializations just to avoid bogus compiler warnings */
299 tagline = NULL;
300 len = 0;
301
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
302 /* open the file, or the "refs" file if the source file is unreadable */
303 fp = fopen(tag->TAGFILE, "r");
304 if (!fp)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
305 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
306 fp = fopen(dirpath(dirdir(tag->TAGFILE), "refs"), "r");
307 if (!fp)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
308 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
309 /* can't open anything -- give error for source file */
310 (void)fopen(tag->TAGFILE, "r");
311 perror(tag->TAGFILE);
312 exit(1);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
313 }
314 }
315
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
316 /* initially we have no stored lines */
317 store(NULL, comments);
318 store(NULL, members);
319
320 /* Convert tag address */
321 taglnum = atol(tag->TAGADDR);
322 if (!taglnum)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
323 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
324 /* NOTE: We alter the regexp string! This is okay since "ref"
325 * only searches for each tag once.
326 */
327 tagline = tag->TAGADDR;
328 for (l = tagline, t = tag->TAGADDR + 2; t[2]; )
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
329 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
330 if (*t == '\\')
331 t++;
332 *l++ = *t++;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
333 }
97d8998 @mbert Import Elvis 2.1_4 (written by Steve Kirkendall)
authored
334 len = (int)(l - tagline);
335 if (*t == '$')
336 len++;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
337 *l = '\0';
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
338
339 /* If only supposed to output the source line (no comments or
340 * other definition lines), and the tagaddress contains the
341 * source line, then just output that.
342 */
343 if (omit_comment_lines && omit_other_lines && *t == '$')
344 {
345 puts(tagline);
346 return;
347 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
348 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
349
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
350 /* for each line... */
351 for (lnum = 1, lc = LC_COMPLETE; (line = getline(fp)) != NULL; lnum++)
352 {
353 /* is this the tag definition? */
97d8998 @mbert Import Elvis 2.1_4 (written by Steve Kirkendall)
authored
354 if (taglnum > 0 ? taglnum == lnum : !strncmp(tagline, line, len))
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
355 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
356 /* output the tag location */
357 if (!omit_comment_lines)
358 printf("\"%s\", %s, line %ld:\n", tag->TAGNAME, tag->TAGFILE, lnum);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
359
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
360 /* output any introductory comments */
361 if (!omit_comment_lines)
362 for (i = 0; i < ncomments; i++)
363 puts(comments[i]);
364
365 /* output any partial definition lines */
366 if (!omit_other_lines)
367 for (i = 0; i < nmembers; i++)
368 puts(members[i]);
369
370 /* output this line */
371 puts(line);
372
373 /* output any following argument lines, unless the
374 * line ends with a semicolon.
375 */
376 if (!omit_other_lines && line[strlen(line) - 1] != ';')
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
377 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
378 if (strchr(line, '(') != NULL)
379 {
380 while ((line = getline(fp)) != NULL
381 && *line
382 && ((*line != '#' && *line != '{')
383 || line[strlen(line) - 1] == '\\'))
384 {
385 puts(line);
386 }
387 }
388 else if ((lc = classify(line, lc)) == LC_PARTIAL)
389 {
390 while ((line = getline(fp)) != NULL
391 && (lc = classify(line, lc)) == LC_PARTIAL)
392 {
393 puts(line);
394 }
395 if (lc == LC_COMPLETE)
396 puts(line);
397 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
398 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
399
400 /* done! */
401 goto Succeed;
402 }
403
404 /* classify this line */
405 lc = classify(line, lc);
406
407 /* process each line, to adjust the stored lines */
408 switch (lc)
409 {
410 case LC_COMMENT:
411 store(line, comments);
412 store(NULL, members);
413 break;
414
415 case LC_PARTIAL:
416 /* leave comments unchanged, but... */
417 store(line, members);
418 break;
419
420 default:
421 store(NULL, comments);
422 store(NULL, members);
423 break;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
424 }
425 }
426
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
427 /* Fail:*/
428 /* complain: not found */
429 fprintf(stderr, "%s: not found in %s\n", tag->TAGNAME, tag->TAGFILE);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
430
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
431 Succeed:
432 /* close the file */
433 fclose(fp);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
434 }
435
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
436 /* Add the directory portion of a file name to the path */
437 static void add_to_path(file)
438 char *file;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
439 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
440 int len;
441
442 len = strlen(tag_path);
443 tag_path[len++] = OSPATHDELIM;
444 strcpy(tag_path + len, dirdir(file));
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
445 }
446
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
447 /* The main function */
448 int main(argc, argv)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
449 int argc;
450 char **argv;
451 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
452 int i, j;
453 char *dir, *file, *scan;
454 TAG *tag;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
455 char kindf[10];
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
456
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
457 /* check for some standard arguments */
458 if (argc > 1)
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
459 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
460 if (!strcmp(argv[1], "-help") /* old GNU */
461 || !strcmp(argv[1], "--help") /* new GNU */
462 || !strcmp(argv[1], "/?") /* DOS */
463 || !strcmp(argv[1], "-?")) /* common */
464 {
465 usage(argv[0]);
466 }
467 if (!strcmp(argv[1], "-version") /* old GNU */
468 || !strcmp(argv[1], "--version"))/* new GNU */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
469 {
470 printf("ref (elvis) %s\n", VERSION);
471 #ifdef COPY1
472 puts(COPY1);
473 #endif
474 #ifdef COPY2
475 puts(COPY2);
476 #endif
477 #ifdef COPY3
478 puts(COPY3);
479 #endif
480 #ifdef COPY4
481 puts(COPY4);
482 #endif
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
483 #ifdef COPY5
484 puts(COPY5);
485 #endif
486 #ifdef PORTEDBY
487 puts(PORTEDBY);
488 #endif
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
489 exit(0);
490 }
491 }
492
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
493 /* check the environment for TAGPATH */
494 scan = getenv("TAGPATH");
495 if (scan)
496 strcpy(tag_path, scan);
497
498 /* parse the options */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
499 for (i = 1; i < argc && argv[i][0] == '-'; i++)
500 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
501 for (j = 1; argv[i][j]; j++)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
502 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
503 switch (argv[i][j])
504 {
505 case 't':
506 output_tag_info = 1;
507 break;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
508
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
509 case 'v':
510 output_verbose_info = 1;
511 break;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
512
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
513 case 'h':
514 output_html_browser = 1;
515 break;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
516
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
517 case 'c':
518 omit_comment_lines = 1;
519 break;
520
521 case 'd':
522 omit_other_lines = 1;
523 break;
524
525 case 'a':
526 output_all_matches = 1;
527 break;
528
529 case 's':
530 search_all_files = 1;
531 break;
532
533 case 'p':
534 if (argv[i][j + 1])
535 strcpy(tag_path, &argv[i][j + 1]);
536 else if (i + 1 < argc)
537 strcpy(tag_path, argv[++i]);
538 else
539 usage(argv[0]);
540 j = strlen(argv[i]) - 1; /* skip to next argv */
541 break;
542
543 case 'l':
544 if (argv[i][j + 1])
545 tag_length = atol(&argv[i][j + 1]);
546 else if (i + 1 < argc)
547 tag_length = atol(argv[++i]);
548 else
549 usage(argv[0]);
550 if (tag_length < 0)
551 usage(argv[0]);
552 j = strlen(argv[i]) - 1; /* skip to next argv */
553 break;
554
555 default:
556 usage(argv[0]);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
557 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
558 }
559 }
560
561 /* -h implies -a; nobody would want to browse a single tag */
562 if (output_html_browser)
3a9bb55 @mbert Import Elvis 2.1_3 (written by Steve Kirkendall)
authored
563 output_all_matches = 1;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
564 if (output_html_browser + output_tag_info + output_verbose_info > 1)
565 {
566 fprintf(stderr, "%s: can't mix -t, -v, and -h\n", argv[0]);
567 exit(1);
568 }
569
570 /* parse any restrictions */
571 tsreset();
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
572 strcpy(kindf, "kind:+f");
573 tsparse(kindf);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
574 j = i;
575 for ( ; i < argc; i++)
576 {
577 /* a little extra work for "file:" -- add its directory name
578 * to the tag path.
579 */
580 if (!strncmp(argv[i], "file:", 5))
581 {
582 if (strchr("+-=/", argv[i][5]))
583 add_to_path(&argv[i][6]);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
584 else
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
585 add_to_path(&argv[i][5]);
586 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
587
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
588 tsparse(argv[i]);
589 }
590
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
591 /* As a last resort, add elvis' data directory path to the tag_path */
592 i = strlen(tag_path);
593 tag_path[i++] = OSPATHDELIM;
594 scan = getenv("ELVISPATH");
595 if (!scan)
596 scan = OSLIBPATH;
597 strcpy(tag_path + i, scan);
598
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
599 /* for each element of the tag path... */
600 for (dir = tag_path; *dir && (search_all_files || !taglist); dir = scan)
601 {
602 /* find the end of this directory name */
603 for (scan = dir; *scan && *scan != OSPATHDELIM; scan++)
604 {
605 }
606 if (*scan)
607 *scan++ = '\0';
608
609 /* first check to see if there is a tags file there */
610 file = dirpath(*dir ? dir : ".", "tags");
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
611 if (ioopen(file, 'r', ElvFalse, ElvFalse, 't'))
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
612 {
613 /* yes, scan the tags file */
614 ioclose();
615 tsfile(file, tag_length);
616 }
617 else
618 {
619 /* no, perhaps this tag element is a file? */
620 tsfile(dir, tag_length);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
621 }
622 }
623
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
624 /* if nothing found, then complain */
625 if (!taglist)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
626 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
627 fprintf(stderr, "%s: tag not found\n", argv[0]);
628 exit(1);
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
629 }
630
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
631 /* output HTML header, if appropriate */
632 if (output_html_browser)
633 {
634 printf("<html><head>\n");
635 printf("<title>Tag Browser</title>\n");
636 printf("</head><body>\n");
637 printf("<h1>Tag Browser</h1>\n");
638 printf("<table border=2 cellspacing=0>\n");
639 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
640
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
641 /* output the information */
642 for (tag = taglist; tag; tag = tag->next)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
643 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
644 /* skip tags whose name starts with "!_". They're pseudo-tags
645 * added to describe the tags file, not any of the user's
646 * source files. The user doesn't care about them.
647 */
648 if (!strncmp(tag->TAGNAME, "!_", 2))
649 continue;
650
651 /* output the tag */
652 if (output_tag_info)
653 {
654 printf("%s\t%s\t%s\n", tag->TAGNAME, tag->TAGFILE, tag->TAGADDR);
655 }
656 else if (output_verbose_info)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
657 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
658 for (i = 0; i < MAXATTR; i++)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
659 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
660 if (tag->attr[i])
661 {
662 printf("%10s:%s\n",
663 tagattrname[i], tag->attr[i]);
664 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
665 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
666 printf(" match:%ld\n", tag->match);
667 if (tag->next && output_all_matches)
668 putchar('\n');
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
669 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
670 else if (output_html_browser)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
671 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
672 printf("<tr>\n");
673 printf("<th><a href=\"%s?", tag->TAGFILE);
674 for (scan = tag->TAGADDR; *scan; scan++)
675 {
676 switch (*scan)
677 {
678 case '\t':
679 case '+':
680 case '"':
681 case '%':
682 printf("%%%02X", *scan);
683 break;
684
685 case ' ':
686 putchar('+');
687 break;
688
689 default:
690 putchar(*scan);
691 }
692 }
693 printf("\">%s</a></th>\n", tag->TAGNAME);
694 printf("<td>%s</td>\n", tag->TAGFILE);
695 printf("<td>");
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
696 if (elvdigit(*tag->TAGADDR))
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
697 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
698 printf("<em>line %s</em>", tag->TAGADDR);
699 }
700 else
701 {
702 for (scan = tag->TAGADDR + 2; scan[2]; scan++)
703 {
704 switch (*scan)
705 {
706 case '&': printf("&amp;"); break;
707 case '<': printf("&lt;"); break;
708 case '>': printf("&gt"); break;
709 case '"': printf("&quot;"); break;
710 case '\\': scan++; /* and fall thru */
711 default: putchar(*scan);
712 }
713 }
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
714 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
715 printf("</td>\n</tr>\n");
716 }
717 else
718 {
719 lookup(tag);
720 if (tag->next && output_all_matches && !omit_comment_lines)
721 puts("-------------------------------------------------------------------------------");
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
722 }
723
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
724 /* if supposed to stop after first, then stop */
725 if (!output_all_matches)
726 break;
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
727 }
728
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
729 /* output the HTML trailer, if appropriate */
730 if (output_html_browser)
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
731 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
732 printf("</table>\n");
733 printf("</body></html>");
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
734 }
735
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
736 /* done! */
737 exit(0);
738 return 0; /* <- to silence a compiler warning */
6335386 @mbert Import Elvis 1.8 (written by Steve Kirkendall)
authored
739 }
Something went wrong with that request. Please try again.