2 * Copyright (C) 1984-2022 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
, char **line
));
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
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')
370 tagendline
= (q
[-1] == '$');
375 tp
= maketagent(tag
, tagfile
, taglinenum
, tagpattern
, tagendline
);
382 curtag
= taglist
.tl_first
;
388 * Edit current tagged file.
391 edit_tagfile(VOID_PARAM
)
395 return (edit(curtag
->tag_file
));
399 curtag_match(line
, linepos
)
404 * Test the line to see if we have a match.
405 * Use strncmp because the pattern may be
406 * truncated (in the tags file) if it is too long.
407 * If tagendline is set, make sure we match all
408 * the way to end of line (no extra chars after the match).
410 int len
= (int) strlen(curtag
->tag_pattern
);
411 if (strncmp(curtag
->tag_pattern
, line
, len
) == 0 &&
412 (!curtag
->tag_endline
|| line
[len
] == '\0' || line
[len
] == '\r'))
414 curtag
->tag_linenum
= find_linenum(linepos
);
422 * This is a stripped-down version of search().
423 * We don't use search() for several reasons:
424 * - We don't want to blow away any search string we may have saved.
425 * - The various regular-expression functions (from different systems:
426 * regcmp vs. re_comp) behave differently in the presence of
427 * parentheses (which are almost always found in a tag).
430 ctagsearch(VOID_PARAM
)
432 POSITION pos
, linepos
;
439 linenum
= find_linenum(pos
);
441 for (found
= 0; !found
;)
444 * Get lines until we find a matching one or
445 * until we hit end-of-file.
448 return (NULL_POSITION
);
451 * Read the next line, and save the
452 * starting position of that line in linepos.
455 pos
= forw_raw_line(pos
, &line
, &line_len
);
459 if (pos
== NULL_POSITION
)
462 * We hit EOF without a match.
464 error("Tag not found", NULL_PARG
);
465 return (NULL_POSITION
);
469 * If we're using line numbers, we might as well
470 * remember the information we have now (the position
471 * and line number of the current line).
474 add_lnum(linenum
, pos
);
476 if (ctldisp
!= OPT_ONPLUS
)
478 if (curtag_match(line
, linepos
))
482 int cvt_ops
= CVT_ANSI
;
483 int cvt_len
= cvt_length(line_len
, cvt_ops
);
484 int *chpos
= cvt_alloc_chpos(cvt_len
);
485 char *cline
= (char *) ecalloc(1, cvt_len
);
486 cvt_text(cline
, line
, chpos
, &line_len
, cvt_ops
);
487 if (curtag_match(cline
, linepos
))
497 /*******************************************************************************
502 * Find tags in the GLOBAL's tag file.
503 * The findgtag() will try and load information about the requested tag.
504 * It does this by calling "global -x tag" and storing the parsed output
505 * for future use by gtagsearch().
506 * Sets curtag to the first tag entry.
508 static enum tag_result
510 char *tag
; /* tag to load */
511 int type
; /* tags type */
517 if (type
!= T_CTAGS_X
&& tag
== NULL
)
524 * If type == T_CTAGS_X then read ctags's -x format from stdin
525 * else execute global(1) and read from it.
527 if (type
== T_CTAGS_X
)
530 /* Set tag default because we cannot read stdin again. */
540 char *cmd
= lgetenv("LESSGLOBALTAGS");
544 /* Get suitable flag value for global(1). */
563 /* Get our data from global(1). */
564 qtag
= shell_quote(tag
);
567 command
= (char *) ecalloc(strlen(cmd
) + strlen(flag
) +
568 strlen(qtag
) + 5, sizeof(char));
569 sprintf(command
, "%s -x%s %s", cmd
, flag
, qtag
);
572 fp
= popen(command
, "r");
578 while (fgets(buf
, sizeof(buf
), fp
))
580 char *name
, *file
, *line
;
591 len
= (int) strlen(buf
);
592 if (len
> 0 && buf
[len
-1] == '\n')
599 } while (c
!= '\n' && c
!= EOF
);
602 if (getentry(buf
, &name
, &file
, &line
))
605 * Couldn't parse this line for some reason.
606 * We'll just pretend it never happened.
611 /* Make new entry and add to list. */
612 tp
= maketagent(name
, file
, (LINENUM
) atoi(line
), NULL
, 0);
627 /* Check to see if we found anything. */
628 tp
= taglist
.tl_first
;
636 static int circular
= 0; /* 1: circular tag structure */
639 * Return the filename required for the next gtag in the queue that was setup
640 * by findgtag(). The next call to gtagsearch() will try to position at the
657 /* Wrapped around to the head of the queue */
658 curtag
= taglist
.tl_first
;
665 return (curtag
->tag_file
);
669 * Return the filename required for the previous gtag in the queue that was
670 * setup by findgtat(). The next call to gtagsearch() will try to position
671 * at the appropriate tag.
687 /* Wrapped around to the tail of the queue */
688 curtag
= taglist
.tl_last
;
695 return (curtag
->tag_file
);
699 * Position the current file at at what is hopefully the tag that was chosen
700 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
701 * if it was unable to position at the tag, 0 if successful.
704 gtagsearch(VOID_PARAM
)
707 return (NULL_POSITION
); /* No gtags loaded! */
708 return (find_pos(curtag
->tag_linenum
));
712 * The getentry() parses both standard and extended ctags -x format.
715 * <tag> <lineno> <file> <image>
716 * +------------------------------------------------
717 * |main 30 main.c main(argc, argv)
718 * |func 21 subr.c func(arg)
720 * The following commands write this format.
721 * o Traditinal Ctags with -x option
722 * o Global with -x option
723 * See <http://www.gnu.org/software/global/global.html>
726 * <tag> <type> <lineno> <file> <image>
727 * +----------------------------------------------------------
728 * |main function 30 main.c main(argc, argv)
729 * |func function 21 subr.c func(arg)
731 * The following commands write this format.
732 * o Exuberant Ctags with -x option
733 * See <http://ctags.sourceforge.net>
735 * Returns 0 on success, -1 on error.
736 * The tag, file, and line will each be NUL-terminated pointers
740 getentry(buf
, tag
, file
, line
)
741 char *buf
; /* standard or extended ctags -x format data */
742 char **tag
; /* name of the tag we actually found */
743 char **file
; /* file in which to find this tag */
744 char **line
; /* line number of file where this tag is found */
748 for (*tag
= p
; *p
&& !IS_SPACE(*p
); p
++) /* tag name */
753 for ( ; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
758 * If the second part begin with other than digit,
759 * it is assumed tag type. Skip it.
763 for ( ; *p
&& !IS_SPACE(*p
); p
++) /* (skip tag type) */
765 for (; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
770 *line
= p
; /* line number */
771 for (*line
= p
; *p
&& !IS_SPACE(*p
); p
++)
776 for ( ; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
780 *file
= p
; /* file name */
781 for (*file
= p
; *p
&& !IS_SPACE(*p
); p
++)
788 if (strlen(*tag
) && strlen(*line
) && strlen(*file
) && atoi(*line
) > 0)