2 * Copyright (C) 1984-2000 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
14 #define WHITESP(c) ((c)==' ' || (c)=='\t')
18 public char *tags = "tags";
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) */
46 static enum tag_result findctag();
47 static enum tag_result findgtag();
48 static char *nextgtag();
49 static char *prevgtag();
50 static POSITION ctagsearch();
51 static POSITION gtagsearch();
52 static int getentry();
55 * The list of tags generated by the last findgtag() call.
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).
66 #define TAG_END ((struct tag *) &taglist)
67 static struct taglist taglist = { TAG_END, TAG_END };
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 '$' */
75 static struct tag *curtag;
78 (tp)->next = taglist.tl_first; \
79 (tp)->prev = TAG_END; \
80 taglist.tl_first->prev = (tp); \
81 taglist.tl_first = (tp);
84 (tp)->next->prev = (tp)->prev; \
85 (tp)->prev->next = (tp)->next;
88 * Delete tag structures.
93 register struct tag *tp;
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. }}
100 while ((tp = taglist.tl_first) != TAG_END)
110 * Create a new tag entry.
113 maketagent(name, file, linenum, pattern, endline)
120 register struct tag *tp;
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;
128 tp->tag_pattern = NULL;
131 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
132 strcpy(tp->tag_pattern, pattern);
145 if (strcmp(tags, "GTAGS") == 0)
147 if (strcmp(tags, "GRTAGS") == 0)
149 if (strcmp(tags, "GSYMS") == 0)
151 if (strcmp(tags, "GPATH") == 0)
153 if (strcmp(tags, "-") == 0)
155 f = open(tags, OPEN_READ);
165 * Find tags in tag file.
166 * Find a tag in the "tags" file.
167 * Sets "tag_file" to the name of the file containing the tag,
168 * and "tagpattern" to the search pattern which should be used
175 int type = gettagtype();
176 enum tag_result result;
179 result = findctag(tag);
181 result = findgtag(tag, type);
188 error("No tags file", NULL_PARG);
191 error("No such tag in tags file", NULL_PARG);
194 error("unknown tag type", NULL_PARG);
206 return (NULL_POSITION); /* No gtags loaded! */
207 if (curtag->tag_linenum != 0)
214 * Go to the next tag.
223 tagfile = nextgtag();
228 * Go to the previous tag.
237 tagfile = prevgtag();
242 * Return the total number of tags.
251 * Return the sequence number of current tag.
259 /*****************************************************************************
264 * Find tags in the "tags" file.
265 * Sets curtag to the first tag entry.
267 static enum tag_result
274 register int taglinenum;
280 char tline[TAGLINE_SIZE];
283 p = unquote_file(tags);
291 taglen = strlen(tag);
294 * Search the tags file for the desired tag.
296 while (fgets(tline, sizeof(tline), f) != NULL)
299 /* Skip header of extended format. */
301 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
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.
315 * Skip over the whitespace after the tag name.
317 p = skipsp(tline+taglen);
319 /* File name is missing! */
323 * Save the file name.
324 * Skip over the whitespace after the file name.
327 while (!WHITESP(*p) && *p != '\0')
332 /* Pattern is missing! */
336 * First see if it is a line number.
338 taglinenum = getnum(&p, 0, &err);
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.
352 while (*p != search_char && *p != '\0')
358 tagendline = (p[-1] == '$');
363 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
370 curtag = taglist.tl_first;
376 * Edit current tagged file.
383 return (edit(curtag->tag_file));
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).
398 POSITION pos, linepos;
404 linenum = find_linenum(pos);
409 * Get lines until we find a matching one or
410 * until we hit end-of-file.
413 return (NULL_POSITION);
416 * Read the next line, and save the
417 * starting position of that line in linepos.
420 pos = forw_raw_line(pos, &line);
424 if (pos == NULL_POSITION)
427 * We hit EOF without a match.
429 error("Tag not found", NULL_PARG);
430 return (NULL_POSITION);
434 * If we're using line numbers, we might as well
435 * remember the information we have now (the position
436 * and line number of the current line).
439 add_lnum(linenum, pos);
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).
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'))
452 curtag->tag_linenum = find_linenum(linepos);
460 /*******************************************************************************
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.
471 static enum tag_result
473 char *tag; /* tag to load */
474 int type; /* tags type */
480 if (type != T_CTAGS_X && tag == NULL)
487 * If type == T_CTAGS_X then read ctags's -x format from stdin
488 * else execute global(1) and read from it.
490 if (type == T_CTAGS_X)
493 /* Set tag default because we cannot read stdin again. */
502 char *cmd = lgetenv("LESSGLOBALTAGS");
504 if (cmd == NULL || *cmd == '\0')
506 /* Get suitable flag value for global(1). */
525 /* Get our data from global(1). */
526 tag = esc_metachars(tag);
527 sprintf(command, "%s -x%s %s", cmd, flag, tag);
529 fp = popen(command, "r");
534 while (fgets(buf, sizeof(buf), fp))
536 char *name, *file, *line;
546 if (buf[strlen(buf) - 1] == '\n')
547 buf[strlen(buf) - 1] = 0;
553 } while (c != '\n' && c != EOF);
556 if (getentry(buf, &name, &file, &line))
559 * Couldn't parse this line for some reason.
560 * We'll just pretend it never happened.
565 /* Make new entry and add to list. */
566 tp = maketagent(name, file, atoi(line), NULL, 0);
581 /* Check to see if we found anything. */
582 tp = taglist.tl_first;
590 static int circular = 0; /* 1: circular tag structure */
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
611 /* Wrapped around to the head of the queue */
612 curtag = taglist.tl_first;
619 return (curtag->tag_file);
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.
641 /* Wrapped around to the tail of the queue */
642 curtag = taglist.tl_last;
649 return (curtag->tag_file);
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.
661 return (NULL_POSITION); /* No gtags loaded! */
662 return (find_pos(curtag->tag_linenum));
666 * The getentry() parses both standard and extended ctags -x format.
669 * <tag> <lineno> <file> <image>
670 * +------------------------------------------------
671 * |main 30 main.c main(argc, argv)
672 * |func 21 subr.c func(arg)
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>
680 * <tag> <type> <lineno> <file> <image>
681 * +----------------------------------------------------------
682 * |main function 30 main.c main(argc, argv)
683 * |func function 21 subr.c func(arg)
685 * The following commands write this format.
686 * o Exuberant Ctags with -x option
687 * See <http://ctags.sourceforge.net>
689 * Returns 0 on success, -1 on error.
690 * The tag, file, and line will each be NUL-terminated pointers
695 #define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
698 #define isdigit(c) ((c) >= '0' && (c <= '9'))
702 getentry(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 */
710 for (*tag = p; *p && !isspace(*p); p++) /* tag name */
715 for ( ; *p && isspace(*p); p++) /* (skip blanks) */
720 * If the second part begin with other than digit,
721 * it is assumed tag type. Skip it.
725 for ( ; *p && !isspace(*p); p++) /* (skip tag type) */
727 for (; *p && isspace(*p); p++) /* (skip blanks) */
732 *line = p; /* line number */
733 for (*line = p; *p && !isspace(*p); p++)
738 for ( ; *p && isspace(*p); p++) /* (skip blanks) */
742 *file = p; /* file name */
743 for (*file = p; *p && !isspace(*p); p++)
750 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)