2 * Copyright (C) 1984-2011 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() uses 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 LINENUM 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 = TAG_END; \
79 (tp)->prev = taglist.tl_last; \
80 taglist.tl_last->next = (tp); \
81 taglist.tl_last = (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.
220 char *tagfile = (char *) NULL;
223 tagfile = nextgtag();
228 * Go to the previous tag.
234 char *tagfile = (char *) NULL;
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
280 char tline[TAGLINE_SIZE];
283 p = shell_unquote(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.
339 taglinenum = getnum(&p, 0, &err);
343 * No, it must be a pattern.
344 * Delete the initial "^" (if present) and
345 * the final "$" from the pattern.
346 * Delete any backslash in the pattern.
353 while (*p != search_char && *p != '\0')
359 tagendline = (p[-1] == '$');
364 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
371 curtag = taglist.tl_first;
377 * Edit current tagged file.
384 return (edit(curtag->tag_file));
389 * This is a stripped-down version of search().
390 * We don't use search() for several reasons:
391 * - We don't want to blow away any search string we may have saved.
392 * - The various regular-expression functions (from different systems:
393 * regcmp vs. re_comp) behave differently in the presence of
394 * parentheses (which are almost always found in a tag).
399 POSITION pos, linepos;
405 linenum = find_linenum(pos);
410 * Get lines until we find a matching one or
411 * until we hit end-of-file.
414 return (NULL_POSITION);
417 * Read the next line, and save the
418 * starting position of that line in linepos.
421 pos = forw_raw_line(pos, &line, (int *)NULL);
425 if (pos == NULL_POSITION)
428 * We hit EOF without a match.
430 error("Tag not found", NULL_PARG);
431 return (NULL_POSITION);
435 * If we're using line numbers, we might as well
436 * remember the information we have now (the position
437 * and line number of the current line).
440 add_lnum(linenum, pos);
443 * Test the line to see if we have a match.
444 * Use strncmp because the pattern may be
445 * truncated (in the tags file) if it is too long.
446 * If tagendline is set, make sure we match all
447 * the way to end of line (no extra chars after the match).
449 len = strlen(curtag->tag_pattern);
450 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
451 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
453 curtag->tag_linenum = find_linenum(linepos);
461 /*******************************************************************************
466 * Find tags in the GLOBAL's tag file.
467 * The findgtag() will try and load information about the requested tag.
468 * It does this by calling "global -x tag" and storing the parsed output
469 * for future use by gtagsearch().
470 * Sets curtag to the first tag entry.
472 static enum tag_result
474 char *tag; /* tag to load */
475 int type; /* tags type */
481 if (type != T_CTAGS_X && tag == NULL)
488 * If type == T_CTAGS_X then read ctags's -x format from stdin
489 * else execute global(1) and read from it.
491 if (type == T_CTAGS_X)
494 /* Set tag default because we cannot read stdin again. */
504 char *cmd = lgetenv("LESSGLOBALTAGS");
506 if (cmd == NULL || *cmd == '\0')
508 /* Get suitable flag value for global(1). */
527 /* Get our data from global(1). */
528 qtag = shell_quote(tag);
531 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
532 strlen(qtag) + 5, sizeof(char));
533 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
536 fp = popen(command, "r");
542 while (fgets(buf, sizeof(buf), fp))
544 char *name, *file, *line;
556 if (len > 0 && buf[len-1] == '\n')
563 } while (c != '\n' && c != EOF);
566 if (getentry(buf, &name, &file, &line))
569 * Couldn't parse this line for some reason.
570 * We'll just pretend it never happened.
575 /* Make new entry and add to list. */
576 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
591 /* Check to see if we found anything. */
592 tp = taglist.tl_first;
600 static int circular = 0; /* 1: circular tag structure */
603 * Return the filename required for the next gtag in the queue that was setup
604 * by findgtag(). The next call to gtagsearch() will try to position at the
621 /* Wrapped around to the head of the queue */
622 curtag = taglist.tl_first;
629 return (curtag->tag_file);
633 * Return the filename required for the previous gtag in the queue that was
634 * setup by findgtat(). The next call to gtagsearch() will try to position
635 * at the appropriate tag.
651 /* Wrapped around to the tail of the queue */
652 curtag = taglist.tl_last;
659 return (curtag->tag_file);
663 * Position the current file at at what is hopefully the tag that was chosen
664 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
665 * if it was unable to position at the tag, 0 if successful.
671 return (NULL_POSITION); /* No gtags loaded! */
672 return (find_pos(curtag->tag_linenum));
676 * The getentry() parses both standard and extended ctags -x format.
679 * <tag> <lineno> <file> <image>
680 * +------------------------------------------------
681 * |main 30 main.c main(argc, argv)
682 * |func 21 subr.c func(arg)
684 * The following commands write this format.
685 * o Traditinal Ctags with -x option
686 * o Global with -x option
687 * See <http://www.gnu.org/software/global/global.html>
690 * <tag> <type> <lineno> <file> <image>
691 * +----------------------------------------------------------
692 * |main function 30 main.c main(argc, argv)
693 * |func function 21 subr.c func(arg)
695 * The following commands write this format.
696 * o Exuberant Ctags with -x option
697 * See <http://ctags.sourceforge.net>
699 * Returns 0 on success, -1 on error.
700 * The tag, file, and line will each be NUL-terminated pointers
704 getentry(buf, tag, file, line)
705 char *buf; /* standard or extended ctags -x format data */
706 char **tag; /* name of the tag we actually found */
707 char **file; /* file in which to find this tag */
708 char **line; /* line number of file where this tag is found */
712 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
717 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
722 * If the second part begin with other than digit,
723 * it is assumed tag type. Skip it.
727 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
729 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
734 *line = p; /* line number */
735 for (*line = p; *p && !IS_SPACE(*p); p++)
740 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
744 *file = p; /* file name */
745 for (*file = p; *p && !IS_SPACE(*p); p++)
752 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)