2 * Copyright (C) 1984-2020 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 LESSPARAMS((char *tag));
48 static enum tag_result findgtag LESSPARAMS((char *tag, int type));
49 static char *nextgtag(VOID_PARAM);
50 static char *prevgtag(VOID_PARAM);
51 static POSITION ctagsearch(VOID_PARAM);
52 static POSITION gtagsearch(VOID_PARAM);
53 static int getentry LESSPARAMS((char *buf, char **tag, char **file,
57 * The list of tags generated by the last findgtag() call.
59 * Use either pattern or line number.
60 * findgtag() always uses line number, so pattern is always NULL.
61 * findctag() uses either pattern (in which case line number is 0),
62 * or line number (in which case pattern is NULL).
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 #define TAG_END ((struct tag *) &taglist)
76 static struct taglist taglist = { TAG_END, TAG_END };
77 static struct tag *curtag;
80 (tp)->next = TAG_END; \
81 (tp)->prev = taglist.tl_last; \
82 taglist.tl_last->next = (tp); \
83 taglist.tl_last = (tp);
86 (tp)->next->prev = (tp)->prev; \
87 (tp)->prev->next = (tp)->next;
90 * Delete tag structures.
98 * Delete any existing tag list.
99 * {{ Ideally, we wouldn't do this until after we know that we
100 * can load some other tag information. }}
102 while ((tp = taglist.tl_first) != TAG_END)
106 free(tp->tag_pattern);
114 * Create a new tag entry.
117 maketagent(name, file, linenum, pattern, endline)
126 tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
127 tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
128 strcpy(tp->tag_file, file);
129 tp->tag_linenum = linenum;
130 tp->tag_endline = endline;
132 tp->tag_pattern = NULL;
135 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
136 strcpy(tp->tag_pattern, pattern);
145 gettagtype(VOID_PARAM)
149 if (strcmp(tags, "GTAGS") == 0)
151 if (strcmp(tags, "GRTAGS") == 0)
153 if (strcmp(tags, "GSYMS") == 0)
155 if (strcmp(tags, "GPATH") == 0)
157 if (strcmp(tags, "-") == 0)
159 f = open(tags, OPEN_READ);
169 * Find tags in tag file.
170 * Find a tag in the "tags" file.
171 * Sets "tag_file" to the name of the file containing the tag,
172 * and "tagpattern" to the search pattern which should be used
179 int type = gettagtype();
180 enum tag_result result;
183 result = findctag(tag);
185 result = findgtag(tag, type);
192 error("No tags file", NULL_PARG);
195 error("No such tag in tags file", NULL_PARG);
198 error("unknown tag type", NULL_PARG);
207 tagsearch(VOID_PARAM)
210 return (NULL_POSITION); /* No gtags loaded! */
211 if (curtag->tag_linenum != 0)
218 * Go to the next tag.
224 char *tagfile = (char *) NULL;
227 tagfile = nextgtag();
232 * Go to the previous tag.
238 char *tagfile = (char *) NULL;
241 tagfile = prevgtag();
246 * Return the total number of tags.
255 * Return the sequence number of current tag.
263 /*****************************************************************************
268 * Find tags in the "tags" file.
269 * Sets curtag to the first tag entry.
271 static enum tag_result
284 char tline[TAGLINE_SIZE];
287 p = shell_unquote(tags);
295 taglen = (int) strlen(tag);
298 * Search the tags file for the desired tag.
300 while (fgets(tline, sizeof(tline), f) != NULL)
303 /* Skip header of extended format. */
305 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
310 * The line contains the tag, the filename and the
311 * location in the file, separated by white space.
312 * The location is either a decimal line number,
313 * or a search pattern surrounded by a pair of delimiters.
314 * Parse the line and extract these parts.
319 * Skip over the whitespace after the tag name.
321 p = skipsp(tline+taglen);
323 /* File name is missing! */
327 * Save the file name.
328 * Skip over the whitespace after the file name.
331 while (!WHITESP(*p) && *p != '\0')
336 /* Pattern is missing! */
340 * First see if it is a line number.
343 taglinenum = getnum(&p, 0, &err);
347 * No, it must be a pattern.
348 * Delete the initial "^" (if present) and
349 * the final "$" from the pattern.
350 * Delete any backslash in the pattern.
357 while (*p != search_char && *p != '\0')
363 tagendline = (p[-1] == '$');
368 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
375 curtag = taglist.tl_first;
381 * Edit current tagged file.
384 edit_tagfile(VOID_PARAM)
388 return (edit(curtag->tag_file));
392 curtag_match(char const *line, POSITION linepos)
395 * Test the line to see if we have a match.
396 * Use strncmp because the pattern may be
397 * truncated (in the tags file) if it is too long.
398 * If tagendline is set, make sure we match all
399 * the way to end of line (no extra chars after the match).
401 int len = (int) strlen(curtag->tag_pattern);
402 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
403 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
405 curtag->tag_linenum = find_linenum(linepos);
413 * This is a stripped-down version of search().
414 * We don't use search() for several reasons:
415 * - We don't want to blow away any search string we may have saved.
416 * - The various regular-expression functions (from different systems:
417 * regcmp vs. re_comp) behave differently in the presence of
418 * parentheses (which are almost always found in a tag).
421 ctagsearch(VOID_PARAM)
423 POSITION pos, linepos;
430 linenum = find_linenum(pos);
432 for (found = 0; !found;)
435 * Get lines until we find a matching one or
436 * until we hit end-of-file.
439 return (NULL_POSITION);
442 * Read the next line, and save the
443 * starting position of that line in linepos.
446 pos = forw_raw_line(pos, &line, &line_len);
450 if (pos == NULL_POSITION)
453 * We hit EOF without a match.
455 error("Tag not found", NULL_PARG);
456 return (NULL_POSITION);
460 * If we're using line numbers, we might as well
461 * remember the information we have now (the position
462 * and line number of the current line).
465 add_lnum(linenum, pos);
467 if (ctldisp != OPT_ONPLUS)
469 if (curtag_match(line, linepos))
473 int cvt_ops = CVT_ANSI;
474 int cvt_len = cvt_length(line_len, cvt_ops);
475 int *chpos = cvt_alloc_chpos(cvt_len);
476 char *cline = (char *) ecalloc(1, cvt_len);
477 cvt_text(cline, line, chpos, &line_len, cvt_ops);
478 if (curtag_match(cline, linepos))
488 /*******************************************************************************
493 * Find tags in the GLOBAL's tag file.
494 * The findgtag() will try and load information about the requested tag.
495 * It does this by calling "global -x tag" and storing the parsed output
496 * for future use by gtagsearch().
497 * Sets curtag to the first tag entry.
499 static enum tag_result
501 char *tag; /* tag to load */
502 int type; /* tags type */
508 if (type != T_CTAGS_X && tag == NULL)
515 * If type == T_CTAGS_X then read ctags's -x format from stdin
516 * else execute global(1) and read from it.
518 if (type == T_CTAGS_X)
521 /* Set tag default because we cannot read stdin again. */
531 char *cmd = lgetenv("LESSGLOBALTAGS");
535 /* Get suitable flag value for global(1). */
554 /* Get our data from global(1). */
555 qtag = shell_quote(tag);
558 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
559 strlen(qtag) + 5, sizeof(char));
560 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
563 fp = popen(command, "r");
569 while (fgets(buf, sizeof(buf), fp))
571 char *name, *file, *line;
582 len = (int) strlen(buf);
583 if (len > 0 && buf[len-1] == '\n')
590 } while (c != '\n' && c != EOF);
593 if (getentry(buf, &name, &file, &line))
596 * Couldn't parse this line for some reason.
597 * We'll just pretend it never happened.
602 /* Make new entry and add to list. */
603 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
618 /* Check to see if we found anything. */
619 tp = taglist.tl_first;
627 static int circular = 0; /* 1: circular tag structure */
630 * Return the filename required for the next gtag in the queue that was setup
631 * by findgtag(). The next call to gtagsearch() will try to position at the
648 /* Wrapped around to the head of the queue */
649 curtag = taglist.tl_first;
656 return (curtag->tag_file);
660 * Return the filename required for the previous gtag in the queue that was
661 * setup by findgtat(). The next call to gtagsearch() will try to position
662 * at the appropriate tag.
678 /* Wrapped around to the tail of the queue */
679 curtag = taglist.tl_last;
686 return (curtag->tag_file);
690 * Position the current file at at what is hopefully the tag that was chosen
691 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
692 * if it was unable to position at the tag, 0 if successful.
695 gtagsearch(VOID_PARAM)
698 return (NULL_POSITION); /* No gtags loaded! */
699 return (find_pos(curtag->tag_linenum));
703 * The getentry() parses both standard and extended ctags -x format.
706 * <tag> <lineno> <file> <image>
707 * +------------------------------------------------
708 * |main 30 main.c main(argc, argv)
709 * |func 21 subr.c func(arg)
711 * The following commands write this format.
712 * o Traditinal Ctags with -x option
713 * o Global with -x option
714 * See <http://www.gnu.org/software/global/global.html>
717 * <tag> <type> <lineno> <file> <image>
718 * +----------------------------------------------------------
719 * |main function 30 main.c main(argc, argv)
720 * |func function 21 subr.c func(arg)
722 * The following commands write this format.
723 * o Exuberant Ctags with -x option
724 * See <http://ctags.sourceforge.net>
726 * Returns 0 on success, -1 on error.
727 * The tag, file, and line will each be NUL-terminated pointers
731 getentry(buf, tag, file, line)
732 char *buf; /* standard or extended ctags -x format data */
733 char **tag; /* name of the tag we actually found */
734 char **file; /* file in which to find this tag */
735 char **line; /* line number of file where this tag is found */
739 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
744 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
749 * If the second part begin with other than digit,
750 * it is assumed tag type. Skip it.
754 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
756 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
761 *line = p; /* line number */
762 for (*line = p; *p && !IS_SPACE(*p); p++)
767 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
771 *file = p; /* file name */
772 for (*file = p; *p && !IS_SPACE(*p); p++)
779 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)