2 * Copyright (C) 1984-2019 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, see the README file.
13 #define WHITESP(c) ((c)==' ' || (c)=='\t')
17 public char ztags[] = "tags";
18 public char *tags = ztags;
39 T_CTAGS, /* 'tags': standard and extended format (ctags) */
40 T_CTAGS_X, /* stdin: cross reference format (ctags) */
41 T_GTAGS, /* 'GTAGS': function definition (global) */
42 T_GRTAGS, /* 'GRTAGS': function reference (global) */
43 T_GSYMS, /* 'GSYMS': other symbols (global) */
44 T_GPATH /* 'GPATH': path name (global) */
47 static enum tag_result findctag();
48 static enum tag_result findgtag();
49 static char *nextgtag();
50 static char *prevgtag();
51 static POSITION ctagsearch();
52 static POSITION gtagsearch();
53 static int getentry();
56 * The list of tags generated by the last findgtag() call.
58 * Use either pattern or line number.
59 * findgtag() always uses line number, so pattern is always NULL.
60 * findctag() uses either pattern (in which case line number is 0),
61 * or line number (in which case pattern is NULL).
68 struct tag *next, *prev; /* List links */
69 char *tag_file; /* Source file containing the tag */
70 LINENUM tag_linenum; /* Appropriate line number in source file */
71 char *tag_pattern; /* Pattern used to find the tag */
72 char tag_endline; /* True if the pattern includes '$' */
74 #define TAG_END ((struct tag *) &taglist)
75 static struct taglist taglist = { TAG_END, TAG_END };
76 static struct tag *curtag;
79 (tp)->next = TAG_END; \
80 (tp)->prev = taglist.tl_last; \
81 taglist.tl_last->next = (tp); \
82 taglist.tl_last = (tp);
85 (tp)->next->prev = (tp)->prev; \
86 (tp)->prev->next = (tp)->next;
89 * Delete tag structures.
97 * Delete any existing tag list.
98 * {{ Ideally, we wouldn't do this until after we know that we
99 * can load some other tag information. }}
101 while ((tp = taglist.tl_first) != TAG_END)
105 free(tp->tag_pattern);
113 * Create a new tag entry.
116 maketagent(name, file, linenum, pattern, endline)
125 tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
126 tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
127 strcpy(tp->tag_file, file);
128 tp->tag_linenum = linenum;
129 tp->tag_endline = endline;
131 tp->tag_pattern = NULL;
134 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
135 strcpy(tp->tag_pattern, pattern);
144 gettagtype(VOID_PARAM)
148 if (strcmp(tags, "GTAGS") == 0)
150 if (strcmp(tags, "GRTAGS") == 0)
152 if (strcmp(tags, "GSYMS") == 0)
154 if (strcmp(tags, "GPATH") == 0)
156 if (strcmp(tags, "-") == 0)
158 f = open(tags, OPEN_READ);
168 * Find tags in tag file.
169 * Find a tag in the "tags" file.
170 * Sets "tag_file" to the name of the file containing the tag,
171 * and "tagpattern" to the search pattern which should be used
178 int type = gettagtype();
179 enum tag_result result;
182 result = findctag(tag);
184 result = findgtag(tag, type);
191 error("No tags file", NULL_PARG);
194 error("No such tag in tags file", NULL_PARG);
197 error("unknown tag type", NULL_PARG);
206 tagsearch(VOID_PARAM)
209 return (NULL_POSITION); /* No gtags loaded! */
210 if (curtag->tag_linenum != 0)
217 * Go to the next tag.
223 char *tagfile = (char *) NULL;
226 tagfile = nextgtag();
231 * Go to the previous tag.
237 char *tagfile = (char *) NULL;
240 tagfile = prevgtag();
245 * Return the total number of tags.
254 * Return the sequence number of current tag.
262 /*****************************************************************************
267 * Find tags in the "tags" file.
268 * Sets curtag to the first tag entry.
270 static enum tag_result
283 char tline[TAGLINE_SIZE];
286 p = shell_unquote(tags);
294 taglen = (int) strlen(tag);
297 * Search the tags file for the desired tag.
299 while (fgets(tline, sizeof(tline), f) != NULL)
302 /* Skip header of extended format. */
304 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
309 * The line contains the tag, the filename and the
310 * location in the file, separated by white space.
311 * The location is either a decimal line number,
312 * or a search pattern surrounded by a pair of delimiters.
313 * Parse the line and extract these parts.
318 * Skip over the whitespace after the tag name.
320 p = skipsp(tline+taglen);
322 /* File name is missing! */
326 * Save the file name.
327 * Skip over the whitespace after the file name.
330 while (!WHITESP(*p) && *p != '\0')
335 /* Pattern is missing! */
339 * First see if it is a line number.
342 taglinenum = getnum(&p, 0, &err);
346 * No, it must be a pattern.
347 * Delete the initial "^" (if present) and
348 * the final "$" from the pattern.
349 * Delete any backslash in the pattern.
356 while (*p != search_char && *p != '\0')
362 tagendline = (p[-1] == '$');
367 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
374 curtag = taglist.tl_first;
380 * Edit current tagged file.
383 edit_tagfile(VOID_PARAM)
387 return (edit(curtag->tag_file));
391 curtag_match(char const *line, POSITION linepos)
394 * Test the line to see if we have a match.
395 * Use strncmp because the pattern may be
396 * truncated (in the tags file) if it is too long.
397 * If tagendline is set, make sure we match all
398 * the way to end of line (no extra chars after the match).
400 int len = (int) strlen(curtag->tag_pattern);
401 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
402 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
404 curtag->tag_linenum = find_linenum(linepos);
412 * This is a stripped-down version of search().
413 * We don't use search() for several reasons:
414 * - We don't want to blow away any search string we may have saved.
415 * - The various regular-expression functions (from different systems:
416 * regcmp vs. re_comp) behave differently in the presence of
417 * parentheses (which are almost always found in a tag).
420 ctagsearch(VOID_PARAM)
422 POSITION pos, linepos;
429 linenum = find_linenum(pos);
431 for (found = 0; !found;)
434 * Get lines until we find a matching one or
435 * until we hit end-of-file.
438 return (NULL_POSITION);
441 * Read the next line, and save the
442 * starting position of that line in linepos.
445 pos = forw_raw_line(pos, &line, &line_len);
449 if (pos == NULL_POSITION)
452 * We hit EOF without a match.
454 error("Tag not found", NULL_PARG);
455 return (NULL_POSITION);
459 * If we're using line numbers, we might as well
460 * remember the information we have now (the position
461 * and line number of the current line).
464 add_lnum(linenum, pos);
466 if (ctldisp != OPT_ONPLUS)
468 if (curtag_match(line, linepos))
472 int cvt_ops = CVT_ANSI;
473 int cvt_len = cvt_length(line_len, cvt_ops);
474 int *chpos = cvt_alloc_chpos(cvt_len);
475 char *cline = (char *) ecalloc(1, cvt_len);
476 cvt_text(cline, line, chpos, &line_len, cvt_ops);
477 if (curtag_match(cline, linepos))
487 /*******************************************************************************
492 * Find tags in the GLOBAL's tag file.
493 * The findgtag() will try and load information about the requested tag.
494 * It does this by calling "global -x tag" and storing the parsed output
495 * for future use by gtagsearch().
496 * Sets curtag to the first tag entry.
498 static enum tag_result
500 char *tag; /* tag to load */
501 int type; /* tags type */
507 if (type != T_CTAGS_X && tag == NULL)
514 * If type == T_CTAGS_X then read ctags's -x format from stdin
515 * else execute global(1) and read from it.
517 if (type == T_CTAGS_X)
520 /* Set tag default because we cannot read stdin again. */
530 char *cmd = lgetenv("LESSGLOBALTAGS");
534 /* Get suitable flag value for global(1). */
553 /* Get our data from global(1). */
554 qtag = shell_quote(tag);
557 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
558 strlen(qtag) + 5, sizeof(char));
559 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
562 fp = popen(command, "r");
568 while (fgets(buf, sizeof(buf), fp))
570 char *name, *file, *line;
581 len = (int) strlen(buf);
582 if (len > 0 && buf[len-1] == '\n')
589 } while (c != '\n' && c != EOF);
592 if (getentry(buf, &name, &file, &line))
595 * Couldn't parse this line for some reason.
596 * We'll just pretend it never happened.
601 /* Make new entry and add to list. */
602 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
617 /* Check to see if we found anything. */
618 tp = taglist.tl_first;
626 static int circular = 0; /* 1: circular tag structure */
629 * Return the filename required for the next gtag in the queue that was setup
630 * by findgtag(). The next call to gtagsearch() will try to position at the
647 /* Wrapped around to the head of the queue */
648 curtag = taglist.tl_first;
655 return (curtag->tag_file);
659 * Return the filename required for the previous gtag in the queue that was
660 * setup by findgtat(). The next call to gtagsearch() will try to position
661 * at the appropriate tag.
677 /* Wrapped around to the tail of the queue */
678 curtag = taglist.tl_last;
685 return (curtag->tag_file);
689 * Position the current file at at what is hopefully the tag that was chosen
690 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
691 * if it was unable to position at the tag, 0 if successful.
694 gtagsearch(VOID_PARAM)
697 return (NULL_POSITION); /* No gtags loaded! */
698 return (find_pos(curtag->tag_linenum));
702 * The getentry() parses both standard and extended ctags -x format.
705 * <tag> <lineno> <file> <image>
706 * +------------------------------------------------
707 * |main 30 main.c main(argc, argv)
708 * |func 21 subr.c func(arg)
710 * The following commands write this format.
711 * o Traditinal Ctags with -x option
712 * o Global with -x option
713 * See <http://www.gnu.org/software/global/global.html>
716 * <tag> <type> <lineno> <file> <image>
717 * +----------------------------------------------------------
718 * |main function 30 main.c main(argc, argv)
719 * |func function 21 subr.c func(arg)
721 * The following commands write this format.
722 * o Exuberant Ctags with -x option
723 * See <http://ctags.sourceforge.net>
725 * Returns 0 on success, -1 on error.
726 * The tag, file, and line will each be NUL-terminated pointers
730 getentry(buf, tag, file, line)
731 char *buf; /* standard or extended ctags -x format data */
732 char **tag; /* name of the tag we actually found */
733 char **file; /* file in which to find this tag */
734 char **line; /* line number of file where this tag is found */
738 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
743 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
748 * If the second part begin with other than digit,
749 * it is assumed tag type. Skip it.
753 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
755 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
760 *line = p; /* line number */
761 for (*line = p; *p && !IS_SPACE(*p); p++)
766 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
770 *file = p; /* file name */
771 for (*file = p; *p && !IS_SPACE(*p); p++)
778 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)