2 * Copyright (C) 1984-2023 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(char *tag
);
48 static enum tag_result
findgtag(char *tag
, int type
);
49 static char *nextgtag(void);
50 static char *prevgtag(void);
51 static POSITION
ctagsearch(void);
52 static POSITION
gtagsearch(void);
53 static int getentry(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.
91 public void cleantags(void)
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
)
104 free(tp
->tag_pattern
);
112 * Create a new tag entry.
114 static struct tag
* maketagent(char *name
, char *file
, LINENUM linenum
, char *pattern
, int endline
)
118 tp
= (struct tag
*) ecalloc(sizeof(struct tag
), 1);
119 tp
->tag_file
= (char *) ecalloc(strlen(file
) + 1, sizeof(char));
120 strcpy(tp
->tag_file
, file
);
121 tp
->tag_linenum
= linenum
;
122 tp
->tag_endline
= endline
;
124 tp
->tag_pattern
= NULL
;
127 tp
->tag_pattern
= (char *) ecalloc(strlen(pattern
) + 1, sizeof(char));
128 strcpy(tp
->tag_pattern
, pattern
);
136 public int gettagtype(void)
140 if (strcmp(tags
, "GTAGS") == 0)
142 if (strcmp(tags
, "GRTAGS") == 0)
144 if (strcmp(tags
, "GSYMS") == 0)
146 if (strcmp(tags
, "GPATH") == 0)
148 if (strcmp(tags
, "-") == 0)
150 f
= open(tags
, OPEN_READ
);
160 * Find tags in tag file.
161 * Find a tag in the "tags" file.
162 * Sets "tag_file" to the name of the file containing the tag,
163 * and "tagpattern" to the search pattern which should be used
166 public void findtag(char *tag
)
168 int type
= gettagtype();
169 enum tag_result result
;
172 result
= findctag(tag
);
174 result
= findgtag(tag
, type
);
181 error("No tags file", NULL_PARG
);
184 error("No such tag in tags file", NULL_PARG
);
187 error("unknown tag type", NULL_PARG
);
195 public POSITION
tagsearch(void)
198 return (NULL_POSITION
); /* No gtags loaded! */
199 if (curtag
->tag_linenum
!= 0)
206 * Go to the next tag.
208 public char * nexttag(int n
)
210 char *tagfile
= (char *) NULL
;
213 tagfile
= nextgtag();
218 * Go to the previous tag.
220 public char * prevtag(int n
)
222 char *tagfile
= (char *) NULL
;
225 tagfile
= prevgtag();
230 * Return the total number of tags.
232 public int ntags(void)
238 * Return the sequence number of current tag.
240 public int curr_tag(void)
245 /*****************************************************************************
250 * Find tags in the "tags" file.
251 * Sets curtag to the first tag entry.
253 static enum tag_result
findctag(char *tag
)
265 char tline
[TAGLINE_SIZE
];
268 p
= shell_unquote(tags
);
276 taglen
= (int) strlen(tag
);
279 * Search the tags file for the desired tag.
281 while (fgets(tline
, sizeof(tline
), f
) != NULL
)
284 /* Skip header of extended format. */
286 if (strncmp(tag
, tline
, taglen
) != 0 || !WHITESP(tline
[taglen
]))
291 * The line contains the tag, the filename and the
292 * location in the file, separated by white space.
293 * The location is either a decimal line number,
294 * or a search pattern surrounded by a pair of delimiters.
295 * Parse the line and extract these parts.
300 * Skip over the whitespace after the tag name.
302 p
= skipsp(tline
+taglen
);
304 /* File name is missing! */
308 * Save the file name.
309 * Skip over the whitespace after the file name.
312 while (!WHITESP(*p
) && *p
!= '\0')
317 /* Pattern is missing! */
321 * First see if it is a line number.
324 taglinenum
= getnum(&p
, 0, &err
);
328 * No, it must be a pattern.
329 * Delete the initial "^" (if present) and
330 * the final "$" from the pattern.
331 * Delete any backslash in the pattern.
338 while (*p
!= search_char
&& *p
!= '\0')
351 tagendline
= (q
[-1] == '$');
356 tp
= maketagent(tag
, tagfile
, taglinenum
, tagpattern
, tagendline
);
363 curtag
= taglist
.tl_first
;
369 * Edit current tagged file.
371 public int edit_tagfile(void)
375 return (edit(curtag
->tag_file
));
378 static int curtag_match(char constant
*line
, POSITION linepos
)
381 * Test the line to see if we have a match.
382 * Use strncmp because the pattern may be
383 * truncated (in the tags file) if it is too long.
384 * If tagendline is set, make sure we match all
385 * the way to end of line (no extra chars after the match).
387 int len
= (int) strlen(curtag
->tag_pattern
);
388 if (strncmp(curtag
->tag_pattern
, line
, len
) == 0 &&
389 (!curtag
->tag_endline
|| line
[len
] == '\0' || line
[len
] == '\r'))
391 curtag
->tag_linenum
= find_linenum(linepos
);
399 * This is a stripped-down version of search().
400 * We don't use search() for several reasons:
401 * - We don't want to blow away any search string we may have saved.
402 * - The various regular-expression functions (from different systems:
403 * regcmp vs. re_comp) behave differently in the presence of
404 * parentheses (which are almost always found in a tag).
406 static POSITION
ctagsearch(void)
408 POSITION pos
, linepos
;
415 linenum
= find_linenum(pos
);
417 for (found
= 0; !found
;)
420 * Get lines until we find a matching one or
421 * until we hit end-of-file.
424 return (NULL_POSITION
);
427 * Read the next line, and save the
428 * starting position of that line in linepos.
431 pos
= forw_raw_line(pos
, &line
, &line_len
);
435 if (pos
== NULL_POSITION
)
438 * We hit EOF without a match.
440 error("Tag not found", NULL_PARG
);
441 return (NULL_POSITION
);
445 * If we're using line numbers, we might as well
446 * remember the information we have now (the position
447 * and line number of the current line).
450 add_lnum(linenum
, pos
);
452 if (ctldisp
!= OPT_ONPLUS
)
454 if (curtag_match(line
, linepos
))
458 int cvt_ops
= CVT_ANSI
;
459 int cvt_len
= cvt_length(line_len
, cvt_ops
);
460 int *chpos
= cvt_alloc_chpos(cvt_len
);
461 char *cline
= (char *) ecalloc(1, cvt_len
);
462 cvt_text(cline
, line
, chpos
, &line_len
, cvt_ops
);
463 if (curtag_match(cline
, linepos
))
473 /*******************************************************************************
478 * Find tags in the GLOBAL's tag file.
479 * The findgtag() will try and load information about the requested tag.
480 * It does this by calling "global -x tag" and storing the parsed output
481 * for future use by gtagsearch().
482 * Sets curtag to the first tag entry.
484 static enum tag_result
findgtag(char *tag
, int type
)
490 if (type
!= T_CTAGS_X
&& tag
== NULL
)
497 * If type == T_CTAGS_X then read ctags's -x format from stdin
498 * else execute global(1) and read from it.
500 if (type
== T_CTAGS_X
)
503 /* Set tag default because we cannot read stdin again. */
513 char *cmd
= lgetenv("LESSGLOBALTAGS");
517 /* Get suitable flag value for global(1). */
536 /* Get our data from global(1). */
537 qtag
= shell_quote(tag
);
540 command
= (char *) ecalloc(strlen(cmd
) + strlen(flag
) +
541 strlen(qtag
) + 5, sizeof(char));
542 sprintf(command
, "%s -x%s %s", cmd
, flag
, qtag
);
545 fp
= popen(command
, "r");
551 while (fgets(buf
, sizeof(buf
), fp
))
553 char *name
, *file
, *line
;
564 len
= (int) strlen(buf
);
565 if (len
> 0 && buf
[len
-1] == '\n')
572 } while (c
!= '\n' && c
!= EOF
);
575 if (getentry(buf
, &name
, &file
, &line
))
578 * Couldn't parse this line for some reason.
579 * We'll just pretend it never happened.
584 /* Make new entry and add to list. */
585 tp
= maketagent(name
, file
, (LINENUM
) atoi(line
), NULL
, 0);
600 /* Check to see if we found anything. */
601 tp
= taglist
.tl_first
;
609 static int circular
= 0; /* 1: circular tag structure */
612 * Return the filename required for the next gtag in the queue that was setup
613 * by findgtag(). The next call to gtagsearch() will try to position at the
616 static char * nextgtag(void)
629 /* Wrapped around to the head of the queue */
630 curtag
= taglist
.tl_first
;
637 return (curtag
->tag_file
);
641 * Return the filename required for the previous gtag in the queue that was
642 * setup by findgtat(). The next call to gtagsearch() will try to position
643 * at the appropriate tag.
645 static char * prevgtag(void)
658 /* Wrapped around to the tail of the queue */
659 curtag
= taglist
.tl_last
;
666 return (curtag
->tag_file
);
670 * Position the current file at at what is hopefully the tag that was chosen
671 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
672 * if it was unable to position at the tag, 0 if successful.
674 static POSITION
gtagsearch(void)
677 return (NULL_POSITION
); /* No gtags loaded! */
678 return (find_pos(curtag
->tag_linenum
));
682 * The getentry() parses both standard and extended ctags -x format.
685 * <tag> <lineno> <file> <image>
686 * +------------------------------------------------
687 * |main 30 main.c main(argc, argv)
688 * |func 21 subr.c func(arg)
690 * The following commands write this format.
691 * o Traditinal Ctags with -x option
692 * o Global with -x option
693 * See <http://www.gnu.org/software/global/global.html>
696 * <tag> <type> <lineno> <file> <image>
697 * +----------------------------------------------------------
698 * |main function 30 main.c main(argc, argv)
699 * |func function 21 subr.c func(arg)
701 * The following commands write this format.
702 * o Exuberant Ctags with -x option
703 * See <http://ctags.sourceforge.net>
705 * Returns 0 on success, -1 on error.
706 * The tag, file, and line will each be NUL-terminated pointers
709 static int getentry(char *buf
, char **tag
, char **file
, char **line
)
713 for (*tag
= p
; *p
&& !IS_SPACE(*p
); p
++) /* tag name */
718 for ( ; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
723 * If the second part begin with other than digit,
724 * it is assumed tag type. Skip it.
728 for ( ; *p
&& !IS_SPACE(*p
); p
++) /* (skip tag type) */
730 for (; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
735 *line
= p
; /* line number */
736 for (*line
= p
; *p
&& !IS_SPACE(*p
); p
++)
741 for ( ; *p
&& IS_SPACE(*p
); p
++) /* (skip blanks) */
745 *file
= p
; /* file name */
746 for (*file
= p
; *p
&& !IS_SPACE(*p
); p
++)
753 if (strlen(*tag
) && strlen(*line
) && strlen(*file
) && atoi(*line
) > 0)