Deleted Added
full compact
tags.c (60786) tags.c (89019)
1/*
2 * Copyright (C) 1984-2000 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10
11
12#include "less.h"
13
14#define WHITESP(c) ((c)==' ' || (c)=='\t')
15
16#if TAGS
17
18public char *tags = "tags";
19
1/*
2 * Copyright (C) 1984-2000 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10
11
12#include "less.h"
13
14#define WHITESP(c) ((c)==' ' || (c)=='\t')
15
16#if TAGS
17
18public char *tags = "tags";
19
20static char *tagfile;
21static char *tagpattern;
22static int taglinenum;
23static int tagendline;
20static int total;
21static int curseq;
24
25extern int linenums;
26extern int sigs;
22
23extern int linenums;
24extern int sigs;
27extern int jump_sline;
28
25
26enum tag_result {
27 TAG_FOUND,
28 TAG_NOFILE,
29 TAG_NOTAG,
30 TAG_NOTYPE,
31 TAG_INTR
32};
33
29/*
34/*
35 * Tag type
36 */
37enum {
38 T_CTAGS, /* 'tags': standard and extended format (ctags) */
39 T_CTAGS_X, /* stdin: cross reference format (ctags) */
40 T_GTAGS, /* 'GTAGS': function defenition (global) */
41 T_GRTAGS, /* 'GRTAGS': function reference (global) */
42 T_GSYMS, /* 'GSYMS': other symbols (global) */
43 T_GPATH /* 'GPATH': path name (global) */
44};
45
46static enum tag_result findctag();
47static enum tag_result findgtag();
48static char *nextgtag();
49static char *prevgtag();
50static POSITION ctagsearch();
51static POSITION gtagsearch();
52static int getentry();
53
54/*
55 * The list of tags generated by the last findgtag() call.
56 *
57 * Use either pattern or line number.
58 * findgtag() always uses line number, so pattern is always NULL.
59 * findctag() usually either pattern (in which case line number is 0),
60 * or line number (in which case pattern is NULL).
61 */
62struct taglist {
63 struct tag *tl_first;
64 struct tag *tl_last;
65};
66#define TAG_END ((struct tag *) &taglist)
67static struct taglist taglist = { TAG_END, TAG_END };
68struct tag {
69 struct tag *next, *prev; /* List links */
70 char *tag_file; /* Source file containing the tag */
71 int tag_linenum; /* Appropriate line number in source file */
72 char *tag_pattern; /* Pattern used to find the tag */
73 char tag_endline; /* True if the pattern includes '$' */
74};
75static struct tag *curtag;
76
77#define TAG_INS(tp) \
78 (tp)->next = taglist.tl_first; \
79 (tp)->prev = TAG_END; \
80 taglist.tl_first->prev = (tp); \
81 taglist.tl_first = (tp);
82
83#define TAG_RM(tp) \
84 (tp)->next->prev = (tp)->prev; \
85 (tp)->prev->next = (tp)->next;
86
87/*
88 * Delete tag structures.
89 */
90 public void
91cleantags()
92{
93 register struct tag *tp;
94
95 /*
96 * Delete any existing tag list.
97 * {{ Ideally, we wouldn't do this until after we know that we
98 * can load some other tag information. }}
99 */
100 while ((tp = taglist.tl_first) != TAG_END)
101 {
102 TAG_RM(tp);
103 free(tp);
104 }
105 curtag = NULL;
106 total = curseq = 0;
107}
108
109/*
110 * Create a new tag entry.
111 */
112 static struct tag *
113maketagent(name, file, linenum, pattern, endline)
114 char *name;
115 char *file;
116 int linenum;
117 char *pattern;
118 int endline;
119{
120 register struct tag *tp;
121
122 tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
123 tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
124 strcpy(tp->tag_file, file);
125 tp->tag_linenum = linenum;
126 tp->tag_endline = endline;
127 if (pattern == NULL)
128 tp->tag_pattern = NULL;
129 else
130 {
131 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
132 strcpy(tp->tag_pattern, pattern);
133 }
134 return (tp);
135}
136
137/*
138 * Get tag mode.
139 */
140 public int
141gettagtype()
142{
143 int f;
144
145 if (strcmp(tags, "GTAGS") == 0)
146 return T_GTAGS;
147 if (strcmp(tags, "GRTAGS") == 0)
148 return T_GRTAGS;
149 if (strcmp(tags, "GSYMS") == 0)
150 return T_GSYMS;
151 if (strcmp(tags, "GPATH") == 0)
152 return T_GPATH;
153 if (strcmp(tags, "-") == 0)
154 return T_CTAGS_X;
155 f = open(tags, OPEN_READ);
156 if (f >= 0)
157 {
158 close(f);
159 return T_CTAGS;
160 }
161 return T_GTAGS;
162}
163
164/*
165 * Find tags in tag file.
30 * Find a tag in the "tags" file.
166 * Find a tag in the "tags" file.
31 * Sets "tagfile" to the name of the file containing the tag,
167 * Sets "tag_file" to the name of the file containing the tag,
32 * and "tagpattern" to the search pattern which should be used
33 * to find the tag.
34 */
35 public void
36findtag(tag)
37 register char *tag;
38{
168 * and "tagpattern" to the search pattern which should be used
169 * to find the tag.
170 */
171 public void
172findtag(tag)
173 register char *tag;
174{
175 int type = gettagtype();
176 enum tag_result result;
177
178 if (type == T_CTAGS)
179 result = findctag(tag);
180 else
181 result = findgtag(tag, type);
182 switch (result)
183 {
184 case TAG_FOUND:
185 case TAG_INTR:
186 break;
187 case TAG_NOFILE:
188 error("No tags file", NULL_PARG);
189 break;
190 case TAG_NOTAG:
191 error("No such tag in tags file", NULL_PARG);
192 break;
193 case TAG_NOTYPE:
194 error("unknown tag type", NULL_PARG);
195 break;
196 }
197}
198
199/*
200 * Search for a tag.
201 */
202 public POSITION
203tagsearch()
204{
205 if (curtag == NULL)
206 return (NULL_POSITION); /* No gtags loaded! */
207 if (curtag->tag_linenum != 0)
208 return gtagsearch();
209 else
210 return ctagsearch();
211}
212
213/*
214 * Go to the next tag.
215 */
216 public char *
217nexttag(n)
218 int n;
219{
220 char *tagfile;
221
222 while (n-- > 0)
223 tagfile = nextgtag();
224 return tagfile;
225}
226
227/*
228 * Go to the previous tag.
229 */
230 public char *
231prevtag(n)
232 int n;
233{
234 char *tagfile;
235
236 while (n-- > 0)
237 tagfile = prevgtag();
238 return tagfile;
239}
240
241/*
242 * Return the total number of tags.
243 */
244 public int
245ntags()
246{
247 return total;
248}
249
250/*
251 * Return the sequence number of current tag.
252 */
253 public int
254curr_tag()
255{
256 return curseq;
257}
258
259/*****************************************************************************
260 * ctags
261 */
262
263/*
264 * Find tags in the "tags" file.
265 * Sets curtag to the first tag entry.
266 */
267 static enum tag_result
268findctag(tag)
269 register char *tag;
270{
39 char *p;
271 char *p;
40 char *q;
41 register FILE *f;
42 register int taglen;
272 register FILE *f;
273 register int taglen;
274 register int taglinenum;
275 char *tagfile;
276 char *tagpattern;
277 int tagendline;
43 int search_char;
44 int err;
45 char tline[TAGLINE_SIZE];
278 int search_char;
279 int err;
280 char tline[TAGLINE_SIZE];
281 struct tag *tp;
46
47 p = unquote_file(tags);
48 f = fopen(p, "r");
49 free(p);
50 if (f == NULL)
282
283 p = unquote_file(tags);
284 f = fopen(p, "r");
285 free(p);
286 if (f == NULL)
51 {
52 error("No tags file", NULL_PARG);
53 tagfile = NULL;
54 return;
55 }
287 return TAG_NOFILE;
56
288
289 cleantags();
290 total = 0;
57 taglen = strlen(tag);
58
59 /*
60 * Search the tags file for the desired tag.
61 */
62 while (fgets(tline, sizeof(tline), f) != NULL)
63 {
291 taglen = strlen(tag);
292
293 /*
294 * Search the tags file for the desired tag.
295 */
296 while (fgets(tline, sizeof(tline), f) != NULL)
297 {
298 if (tline[0] == '!')
299 /* Skip header of extended format. */
300 continue;
64 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
65 continue;
66
67 /*
68 * Found it.
69 * The line contains the tag, the filename and the
70 * location in the file, separated by white space.
71 * The location is either a decimal line number,
72 * or a search pattern surrounded by a pair of delimiters.
73 * Parse the line and extract these parts.
74 */
301 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
302 continue;
303
304 /*
305 * Found it.
306 * The line contains the tag, the filename and the
307 * location in the file, separated by white space.
308 * The location is either a decimal line number,
309 * or a search pattern surrounded by a pair of delimiters.
310 * Parse the line and extract these parts.
311 */
75 tagfile = tagpattern = NULL;
76 taglinenum = 0;
312 tagpattern = NULL;
77
78 /*
79 * Skip over the whitespace after the tag name.
80 */
81 p = skipsp(tline+taglen);
82 if (*p == '\0')
83 /* File name is missing! */
84 continue;

--- 5 unchanged lines hidden (view full) ---

90 tagfile = p;
91 while (!WHITESP(*p) && *p != '\0')
92 p++;
93 *p++ = '\0';
94 p = skipsp(p);
95 if (*p == '\0')
96 /* Pattern is missing! */
97 continue;
313
314 /*
315 * Skip over the whitespace after the tag name.
316 */
317 p = skipsp(tline+taglen);
318 if (*p == '\0')
319 /* File name is missing! */
320 continue;

--- 5 unchanged lines hidden (view full) ---

326 tagfile = p;
327 while (!WHITESP(*p) && *p != '\0')
328 p++;
329 *p++ = '\0';
330 p = skipsp(p);
331 if (*p == '\0')
332 /* Pattern is missing! */
333 continue;
98 tagfile = save(tagfile);
99
100 /*
101 * First see if it is a line number.
102 */
103 taglinenum = getnum(&p, 0, &err);
104 if (err)
105 {
106 /*
107 * No, it must be a pattern.
108 * Delete the initial "^" (if present) and
109 * the final "$" from the pattern.
110 * Delete any backslash in the pattern.
111 */
112 taglinenum = 0;
113 search_char = *p++;
114 if (*p == '^')
115 p++;
334
335 /*
336 * First see if it is a line number.
337 */
338 taglinenum = getnum(&p, 0, &err);
339 if (err)
340 {
341 /*
342 * No, it must be a pattern.
343 * Delete the initial "^" (if present) and
344 * the final "$" from the pattern.
345 * Delete any backslash in the pattern.
346 */
347 taglinenum = 0;
348 search_char = *p++;
349 if (*p == '^')
350 p++;
116 tagpattern = (char *) ecalloc(strlen(p)+1, sizeof(char));
117 q = tagpattern;
351 tagpattern = p;
118 while (*p != search_char && *p != '\0')
119 {
120 if (*p == '\\')
121 p++;
352 while (*p != search_char && *p != '\0')
353 {
354 if (*p == '\\')
355 p++;
122 *q++ = *p++;
356 p++;
123 }
357 }
124 tagendline = (q[-1] == '$');
358 tagendline = (p[-1] == '$');
125 if (tagendline)
359 if (tagendline)
126 q--;
127 *q = '\0';
360 p--;
361 *p = '\0';
128 }
362 }
129
130 fclose(f);
131 return;
363 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
364 TAG_INS(tp);
365 total++;
132 }
133 fclose(f);
366 }
367 fclose(f);
134 error("No such tag in tags file", NULL_PARG);
135 tagfile = NULL;
368 if (total == 0)
369 return TAG_NOTAG;
370 curtag = taglist.tl_first;
371 curseq = 1;
372 return TAG_FOUND;
136}
137
373}
374
375/*
376 * Edit current tagged file.
377 */
138 public int
139edit_tagfile()
140{
378 public int
379edit_tagfile()
380{
141 int r;
142
143 if (tagfile == NULL)
381 if (curtag == NULL)
144 return (1);
382 return (1);
145 r = edit(tagfile);
146 free(tagfile);
147 tagfile = NULL;
148 return (r);
383 return (edit(curtag->tag_file));
149}
150
151/*
152 * Search for a tag.
153 * This is a stripped-down version of search().
154 * We don't use search() for several reasons:
155 * - We don't want to blow away any search string we may have saved.
156 * - The various regular-expression functions (from different systems:
157 * regcmp vs. re_comp) behave differently in the presence of
158 * parentheses (which are almost always found in a tag).
159 */
384}
385
386/*
387 * Search for a tag.
388 * This is a stripped-down version of search().
389 * We don't use search() for several reasons:
390 * - We don't want to blow away any search string we may have saved.
391 * - The various regular-expression functions (from different systems:
392 * regcmp vs. re_comp) behave differently in the presence of
393 * parentheses (which are almost always found in a tag).
394 */
160 public POSITION
161tagsearch()
395 static POSITION
396ctagsearch()
162{
163 POSITION pos, linepos;
164 int linenum;
165 int len;
166 char *line;
167
397{
398 POSITION pos, linepos;
399 int linenum;
400 int len;
401 char *line;
402
168 /*
169 * If we have the line number of the tag instead of the pattern,
170 * just use find_pos.
171 */
172 if (taglinenum)
173 return (find_pos(taglinenum));
174
175 pos = ch_zero();
176 linenum = find_linenum(pos);
177
178 for (;;)
179 {
180 /*
181 * Get lines until we find a matching one or
182 * until we hit end-of-file.

--- 29 unchanged lines hidden (view full) ---

212
213 /*
214 * Test the line to see if we have a match.
215 * Use strncmp because the pattern may be
216 * truncated (in the tags file) if it is too long.
217 * If tagendline is set, make sure we match all
218 * the way to end of line (no extra chars after the match).
219 */
403 pos = ch_zero();
404 linenum = find_linenum(pos);
405
406 for (;;)
407 {
408 /*
409 * Get lines until we find a matching one or
410 * until we hit end-of-file.

--- 29 unchanged lines hidden (view full) ---

440
441 /*
442 * Test the line to see if we have a match.
443 * Use strncmp because the pattern may be
444 * truncated (in the tags file) if it is too long.
445 * If tagendline is set, make sure we match all
446 * the way to end of line (no extra chars after the match).
447 */
220 len = strlen(tagpattern);
221 if (strncmp(tagpattern, line, len) == 0 &&
222 (!tagendline || line[len] == '\0' || line[len] == '\r'))
448 len = strlen(curtag->tag_pattern);
449 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
450 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
451 {
452 curtag->tag_linenum = find_linenum(linepos);
223 break;
453 break;
454 }
224 }
225
455 }
456
226 free(tagpattern);
227 tagpattern = NULL;
228 return (linepos);
229}
230
457 return (linepos);
458}
459
460/*******************************************************************************
461 * gtags
462 */
463
464/*
465 * Find tags in the GLOBAL's tag file.
466 * The findgtag() will try and load information about the requested tag.
467 * It does this by calling "global -x tag" and storing the parsed output
468 * for future use by gtagsearch().
469 * Sets curtag to the first tag entry.
470 */
471 static enum tag_result
472findgtag(tag, type)
473 char *tag; /* tag to load */
474 int type; /* tags type */
475{
476 char buf[256];
477 FILE *fp;
478 struct tag *tp;
479
480 if (type != T_CTAGS_X && tag == NULL)
481 return TAG_NOFILE;
482
483 cleantags();
484 total = 0;
485
486 /*
487 * If type == T_CTAGS_X then read ctags's -x format from stdin
488 * else execute global(1) and read from it.
489 */
490 if (type == T_CTAGS_X)
491 {
492 fp = stdin;
493 /* Set tag default because we cannot read stdin again. */
494 tags = "tags";
495 } else
496 {
497#if !HAVE_POPEN
498 return TAG_NOFILE;
499#else
500 char command[512];
501 char *flag;
502 char *cmd = lgetenv("LESSGLOBALTAGS");
503
504 if (cmd == NULL || *cmd == '\0')
505 return TAG_NOFILE;
506 /* Get suitable flag value for global(1). */
507 switch (type)
508 {
509 case T_GTAGS:
510 flag = "" ;
511 break;
512 case T_GRTAGS:
513 flag = "r";
514 break;
515 case T_GSYMS:
516 flag = "s";
517 break;
518 case T_GPATH:
519 flag = "P";
520 break;
521 default:
522 return TAG_NOTYPE;
523 }
524
525 /* Get our data from global(1). */
526 tag = esc_metachars(tag);
527 sprintf(command, "%s -x%s %s", cmd, flag, tag);
528 free(tag);
529 fp = popen(command, "r");
231#endif
530#endif
531 }
532 if (fp != NULL)
533 {
534 while (fgets(buf, sizeof(buf), fp))
535 {
536 char *name, *file, *line;
537
538 if (sigs)
539 {
540#if HAVE_POPEN
541 if (fp != stdin)
542 pclose(fp);
543#endif
544 return TAG_INTR;
545 }
546 if (buf[strlen(buf) - 1] == '\n')
547 buf[strlen(buf) - 1] = 0;
548 else
549 {
550 int c;
551 do {
552 c = fgetc(fp);
553 } while (c != '\n' && c != EOF);
554 }
555
556 if (getentry(buf, &name, &file, &line))
557 {
558 /*
559 * Couldn't parse this line for some reason.
560 * We'll just pretend it never happened.
561 */
562 break;
563 }
564
565 /* Make new entry and add to list. */
566 tp = maketagent(name, file, atoi(line), NULL, 0);
567 TAG_INS(tp);
568 total++;
569 }
570 if (fp != stdin)
571 {
572 if (pclose(fp))
573 {
574 curtag = NULL;
575 total = curseq = 0;
576 return TAG_NOFILE;
577 }
578 }
579 }
580
581 /* Check to see if we found anything. */
582 tp = taglist.tl_first;
583 if (tp == TAG_END)
584 return TAG_NOTAG;
585 curtag = tp;
586 curseq = 1;
587 return TAG_FOUND;
588}
589
590static int circular = 0; /* 1: circular tag structure */
591
592/*
593 * Return the filename required for the next gtag in the queue that was setup
594 * by findgtag(). The next call to gtagsearch() will try to position at the
595 * appropriate tag.
596 */
597 static char *
598nextgtag()
599{
600 struct tag *tp;
601
602 if (curtag == NULL)
603 /* No tag loaded */
604 return NULL;
605
606 tp = curtag->next;
607 if (tp == TAG_END)
608 {
609 if (!circular)
610 return NULL;
611 /* Wrapped around to the head of the queue */
612 curtag = taglist.tl_first;
613 curseq = 1;
614 } else
615 {
616 curtag = tp;
617 curseq++;
618 }
619 return (curtag->tag_file);
620}
621
622/*
623 * Return the filename required for the previous gtag in the queue that was
624 * setup by findgtat(). The next call to gtagsearch() will try to position
625 * at the appropriate tag.
626 */
627 static char *
628prevgtag()
629{
630 struct tag *tp;
631
632 if (curtag == NULL)
633 /* No tag loaded */
634 return NULL;
635
636 tp = curtag->prev;
637 if (tp == TAG_END)
638 {
639 if (!circular)
640 return NULL;
641 /* Wrapped around to the tail of the queue */
642 curtag = taglist.tl_last;
643 curseq = total;
644 } else
645 {
646 curtag = tp;
647 curseq--;
648 }
649 return (curtag->tag_file);
650}
651
652/*
653 * Position the current file at at what is hopefully the tag that was chosen
654 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
655 * if it was unable to position at the tag, 0 if succesful.
656 */
657 static POSITION
658gtagsearch()
659{
660 if (curtag == NULL)
661 return (NULL_POSITION); /* No gtags loaded! */
662 return (find_pos(curtag->tag_linenum));
663}
664
665/*
666 * The getentry() parses both standard and extended ctags -x format.
667 *
668 * [standard format]
669 * <tag> <lineno> <file> <image>
670 * +------------------------------------------------
671 * |main 30 main.c main(argc, argv)
672 * |func 21 subr.c func(arg)
673 *
674 * The following commands write this format.
675 * o Traditinal Ctags with -x option
676 * o Global with -x option
677 * See <http://www.gnu.org/software/global/global.html>
678 *
679 * [extended format]
680 * <tag> <type> <lineno> <file> <image>
681 * +----------------------------------------------------------
682 * |main function 30 main.c main(argc, argv)
683 * |func function 21 subr.c func(arg)
684 *
685 * The following commands write this format.
686 * o Exuberant Ctags with -x option
687 * See <http://ctags.sourceforge.net>
688 *
689 * Returns 0 on success, -1 on error.
690 * The tag, file, and line will each be NUL-terminated pointers
691 * into buf.
692 */
693
694#ifndef isspace
695#define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
696#endif
697#ifndef isdigit
698#define isdigit(c) ((c) >= '0' && (c <= '9'))
699#endif
700
701 static int
702getentry(buf, tag, file, line)
703 char *buf; /* standard or extended ctags -x format data */
704 char **tag; /* name of the tag we actually found */
705 char **file; /* file in which to find this tag */
706 char **line; /* line number of file where this tag is found */
707{
708 char *p = buf;
709
710 for (*tag = p; *p && !isspace(*p); p++) /* tag name */
711 ;
712 if (*p == 0)
713 return (-1);
714 *p++ = 0;
715 for ( ; *p && isspace(*p); p++) /* (skip blanks) */
716 ;
717 if (*p == 0)
718 return (-1);
719 /*
720 * If the second part begin with other than digit,
721 * it is assumed tag type. Skip it.
722 */
723 if (!isdigit(*p))
724 {
725 for ( ; *p && !isspace(*p); p++) /* (skip tag type) */
726 ;
727 for (; *p && isspace(*p); p++) /* (skip blanks) */
728 ;
729 }
730 if (!isdigit(*p))
731 return (-1);
732 *line = p; /* line number */
733 for (*line = p; *p && !isspace(*p); p++)
734 ;
735 if (*p == 0)
736 return (-1);
737 *p++ = 0;
738 for ( ; *p && isspace(*p); p++) /* (skip blanks) */
739 ;
740 if (*p == 0)
741 return (-1);
742 *file = p; /* file name */
743 for (*file = p; *p && !isspace(*p); p++)
744 ;
745 if (*p == 0)
746 return (-1);
747 *p = 0;
748
749 /* value check */
750 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)
751 return (0);
752 return (-1);
753}
754
755#endif