1 /*
2 * Copyright (C) 1984-2026 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */
9
10
11 #include "less.h"
12
13 #define WHITESP(c) ((c)==' ' || (c)=='\t')
14
15 #if TAGS
16
17 public constant char ztags[] = "tags";
18 public constant char *tags = ztags;
19
20 static int total;
21 static int curseq;
22
23 extern int linenums;
24 extern int sigs;
25 extern int ctldisp;
26
27 enum tag_result {
28 TAG_FOUND,
29 TAG_NOFILE,
30 TAG_NOTAG,
31 TAG_NOTYPE,
32 TAG_INTR
33 };
34
35 /*
36 * Tag type
37 */
38 enum {
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) */
45 };
46
47 static enum tag_result findctag(constant char *tag);
48 static enum tag_result findgtag(constant char *tag, int type);
49 static constant char *nextgtag(void);
50 static constant char *prevgtag(void);
51 static POSITION ctagsearch(void);
52 static POSITION gtagsearch(void);
53 static int getentry(char *buf, constant char **tag, constant char **file, constant char **line);
54
55 /*
56 * The list of tags generated by the last findgtag() call.
57 *
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).
62 */
63 struct taglist {
64 struct tag *tl_first;
65 struct tag *tl_last;
66 };
67 struct tag {
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 lbool tag_endline; /* True if the pattern includes '$' */
73 };
74 #define TAG_END ((struct tag *) &taglist)
75 static struct taglist taglist = { TAG_END, TAG_END };
76 static struct tag *curtag;
77
78 #define TAG_INS(tp) \
79 (tp)->next = TAG_END; \
80 (tp)->prev = taglist.tl_last; \
81 taglist.tl_last->next = (tp); \
82 taglist.tl_last = (tp);
83
84 #define TAG_RM(tp) \
85 (tp)->next->prev = (tp)->prev; \
86 (tp)->prev->next = (tp)->next;
87
88 /*
89 * Delete tag structures.
90 */
cleantags(void)91 public void cleantags(void)
92 {
93 struct tag *tp;
94
95 /*
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. }}
99 */
100 while ((tp = taglist.tl_first) != TAG_END)
101 {
102 TAG_RM(tp);
103 free(tp->tag_file);
104 free(tp->tag_pattern);
105 free(tp);
106 }
107 curtag = NULL;
108 total = curseq = 0;
109 }
110
111 /*
112 * Create a new tag entry.
113 */
maketagent(constant char * file,LINENUM linenum,constant char * pattern,lbool endline)114 static struct tag * maketagent(constant char *file, LINENUM linenum, constant char *pattern, lbool endline)
115 {
116 struct tag *tp;
117
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;
123 if (pattern == NULL)
124 tp->tag_pattern = NULL;
125 else
126 {
127 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
128 strcpy(tp->tag_pattern, pattern);
129 }
130 return (tp);
131 }
132
133 /*
134 * Get tag mode.
135 */
gettagtype(void)136 public int gettagtype(void)
137 {
138 int f;
139
140 if (strcmp(tags, "GTAGS") == 0)
141 return T_GTAGS;
142 if (strcmp(tags, "GRTAGS") == 0)
143 return T_GRTAGS;
144 if (strcmp(tags, "GSYMS") == 0)
145 return T_GSYMS;
146 if (strcmp(tags, "GPATH") == 0)
147 return T_GPATH;
148 if (strcmp(tags, "-") == 0)
149 return T_CTAGS_X;
150 f = open(tags, OPEN_READ);
151 if (f >= 0)
152 {
153 close(f);
154 return T_CTAGS;
155 }
156 return T_GTAGS;
157 }
158
159 /*
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
164 * to find the tag.
165 */
findtag(constant char * tag)166 public void findtag(constant char *tag)
167 {
168 int type = gettagtype();
169 enum tag_result result;
170
171 if (type == T_CTAGS)
172 result = findctag(tag);
173 else
174 result = findgtag(tag, type);
175 switch (result)
176 {
177 case TAG_FOUND:
178 case TAG_INTR:
179 break;
180 case TAG_NOFILE:
181 error("No tags file", NULL_PARG);
182 break;
183 case TAG_NOTAG:
184 error("No such tag in tags file", NULL_PARG);
185 break;
186 case TAG_NOTYPE:
187 error("unknown tag type", NULL_PARG);
188 break;
189 }
190 }
191
192 /*
193 * Search for a tag.
194 */
tagsearch(void)195 public POSITION tagsearch(void)
196 {
197 if (curtag == NULL)
198 return (NULL_POSITION); /* No gtags loaded! */
199 if (curtag->tag_linenum != 0)
200 return gtagsearch();
201 else
202 return ctagsearch();
203 }
204
205 /*
206 * Go to the next tag.
207 */
nexttag(int n)208 public constant char * nexttag(int n)
209 {
210 constant char *tagfile = (char *) NULL;
211
212 while (n-- > 0)
213 tagfile = nextgtag();
214 return tagfile;
215 }
216
217 /*
218 * Go to the previous tag.
219 */
prevtag(int n)220 public constant char * prevtag(int n)
221 {
222 constant char *tagfile = (char *) NULL;
223
224 while (n-- > 0)
225 tagfile = prevgtag();
226 return tagfile;
227 }
228
229 /*
230 * Return the total number of tags.
231 */
ntags(void)232 public int ntags(void)
233 {
234 return total;
235 }
236
237 /*
238 * Return the sequence number of current tag.
239 */
curr_tag(void)240 public int curr_tag(void)
241 {
242 return curseq;
243 }
244
245 /*****************************************************************************
246 * ctags
247 */
248
249 /*
250 * Find tags in the "tags" file.
251 * Sets curtag to the first tag entry.
252 */
findctag(constant char * tag)253 static enum tag_result findctag(constant char *tag)
254 {
255 char *p;
256 char *q;
257 FILE *f;
258 size_t taglen;
259 int n;
260 LINENUM taglinenum;
261 char *tagfile;
262 char *tagpattern;
263 lbool tagendline;
264 int search_char;
265 char tline[TAGLINE_SIZE];
266 struct tag *tp;
267
268 p = shell_unquote(tags);
269 f = fopen(p, "r");
270 free(p);
271 if (f == NULL)
272 return TAG_NOFILE;
273
274 cleantags();
275 total = 0;
276 taglen = strlen(tag);
277
278 /*
279 * Search the tags file for the desired tag.
280 */
281 while (fgets(tline, sizeof(tline), f) != NULL)
282 {
283 if (tline[0] == '!')
284 /* Skip header of extended format. */
285 continue;
286 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
287 continue;
288
289 /*
290 * Found it.
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.
296 */
297 tagpattern = NULL;
298
299 /*
300 * Skip over the whitespace after the tag name.
301 */
302 p = skipsp(tline+taglen);
303 if (*p == '\0')
304 /* File name is missing! */
305 continue;
306
307 /*
308 * Save the file name.
309 * Skip over the whitespace after the file name.
310 */
311 tagfile = p;
312 while (!WHITESP(*p) && *p != '\0')
313 p++;
314 *p++ = '\0';
315 p = skipsp(p);
316 if (*p == '\0')
317 /* Pattern is missing! */
318 continue;
319
320 /*
321 * First see if it is a line number.
322 */
323 tagendline = FALSE;
324 if (getnum(&p, NULL, FALSE, &n))
325 taglinenum = n;
326 else
327 {
328 /*
329 * No, it must be a pattern.
330 * Delete the initial "^" (if present) and
331 * the final "$" from the pattern.
332 * Delete any backslash in the pattern.
333 */
334 taglinenum = 0;
335 search_char = *p++;
336 if (*p == '^')
337 p++;
338 tagpattern = q = p;
339 while (*p != search_char && *p != '\0')
340 {
341 if (*p == '\\')
342 p++;
343 if (q != p)
344 {
345 *q++ = *p++;
346 } else
347 {
348 q++;
349 p++;
350 }
351 }
352 tagendline = (q[-1] == '$');
353 if (tagendline)
354 q--;
355 *q = '\0';
356 }
357 tp = maketagent(tagfile, taglinenum, tagpattern, tagendline);
358 TAG_INS(tp);
359 total++;
360 }
361 fclose(f);
362 if (total == 0)
363 return TAG_NOTAG;
364 curtag = taglist.tl_first;
365 curseq = 1;
366 return TAG_FOUND;
367 }
368
369 /*
370 * Edit current tagged file.
371 */
edit_tagfile(void)372 public int edit_tagfile(void)
373 {
374 if (curtag == NULL)
375 return (1);
376 return (edit(curtag->tag_file));
377 }
378
curtag_match(char constant * line,POSITION linepos)379 static int curtag_match(char constant *line, POSITION linepos)
380 {
381 /*
382 * Test the line to see if we have a match.
383 * Use strncmp because the pattern may be
384 * truncated (in the tags file) if it is too long.
385 * If tagendline is set, make sure we match all
386 * the way to end of line (no extra chars after the match).
387 */
388 size_t len = strlen(curtag->tag_pattern);
389 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
390 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
391 {
392 curtag->tag_linenum = find_linenum(linepos);
393 return 1;
394 }
395 return 0;
396 }
397
398 /*
399 * Search for a tag.
400 * This is a stripped-down version of search().
401 * We don't use search() for several reasons:
402 * - We don't want to blow away any search string we may have saved.
403 * - The various regular-expression functions (from different systems:
404 * regcmp vs. re_comp) behave differently in the presence of
405 * parentheses (which are almost always found in a tag).
406 */
ctagsearch(void)407 static POSITION ctagsearch(void)
408 {
409 POSITION pos, linepos;
410 LINENUM linenum;
411 size_t line_len;
412 constant char *line;
413 int found;
414
415 pos = ch_zero();
416 linenum = find_linenum(pos);
417
418 for (found = 0; !found;)
419 {
420 /*
421 * Get lines until we find a matching one or
422 * until we hit end-of-file.
423 */
424 if (ABORT_SIGS())
425 return (NULL_POSITION);
426
427 /*
428 * Read the next line, and save the
429 * starting position of that line in linepos.
430 */
431 linepos = pos;
432 pos = forw_raw_line(pos, &line, &line_len);
433 if (linenum != 0)
434 linenum++;
435
436 if (pos == NULL_POSITION)
437 {
438 /*
439 * We hit EOF without a match.
440 */
441 error("Tag not found", NULL_PARG);
442 return (NULL_POSITION);
443 }
444
445 /*
446 * If we're using line numbers, we might as well
447 * remember the information we have now (the position
448 * and line number of the current line).
449 */
450 if (linenums)
451 add_lnum(linenum, pos);
452
453 if (ctldisp != OPT_ONPLUS)
454 {
455 if (curtag_match(line, linepos))
456 found = 1;
457 } else
458 {
459 int cvt_ops = CVT_ANSI;
460 size_t cvt_len = cvt_length(line_len, cvt_ops);
461 int *chpos = cvt_alloc_chpos(cvt_len);
462 char *cline = (char *) ecalloc(1, cvt_len);
463 cvt_text(cline, line, chpos, &line_len, cvt_ops);
464 if (curtag_match(cline, linepos))
465 found = 1;
466 free(chpos);
467 free(cline);
468 }
469 }
470
471 return (linepos);
472 }
473
474 /*******************************************************************************
475 * gtags
476 */
477
478 /*
479 * Find tags in the GLOBAL's tag file.
480 * The findgtag() will try and load information about the requested tag.
481 * It does this by calling "global -x tag" and storing the parsed output
482 * for future use by gtagsearch().
483 * Sets curtag to the first tag entry.
484 */
findgtag(constant char * tag,int type)485 static enum tag_result findgtag(constant char *tag, int type)
486 {
487 char buf[1024];
488 FILE *fp;
489 struct tag *tp;
490
491 if (type != T_CTAGS_X && tag == NULL)
492 return TAG_NOFILE;
493
494 cleantags();
495 total = 0;
496
497 /*
498 * If type == T_CTAGS_X then read ctags's -x format from stdin
499 * else execute global(1) and read from it.
500 */
501 if (type == T_CTAGS_X)
502 {
503 fp = stdin;
504 /* Set tag default because we cannot read stdin again. */
505 tags = ztags;
506 } else
507 {
508 #if !HAVE_POPEN
509 return TAG_NOFILE;
510 #else
511 char *command;
512 char *flag;
513 char *qtag;
514 constant char *cmd = lgetenv("LESSGLOBALTAGS");
515
516 if (isnullenv(cmd))
517 return TAG_NOFILE;
518 /* Get suitable flag value for global(1). */
519 switch (type)
520 {
521 case T_GTAGS:
522 flag = "" ;
523 break;
524 case T_GRTAGS:
525 flag = "r";
526 break;
527 case T_GSYMS:
528 flag = "s";
529 break;
530 case T_GPATH:
531 flag = "P";
532 break;
533 default:
534 return TAG_NOTYPE;
535 }
536
537 /* Get our data from global(1). */
538 qtag = shell_quote(tag);
539 if (qtag == NULL)
540 qtag = save(tag);
541 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
542 strlen(qtag) + 5, sizeof(char));
543 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
544 free(qtag);
545 fp = popen(command, "r");
546 free(command);
547 #endif
548 }
549 if (fp != NULL)
550 {
551 while (fgets(buf, sizeof(buf), fp))
552 {
553 constant char *name;
554 constant char *file;
555 constant char *line;
556 size_t len;
557
558 if (sigs)
559 {
560 #if HAVE_POPEN
561 if (fp != stdin)
562 pclose(fp);
563 #endif
564 return TAG_INTR;
565 }
566 len = strlen(buf);
567 if (len > 0 && buf[len-1] == '\n')
568 buf[len-1] = '\0';
569 else
570 {
571 int c;
572 do {
573 c = fgetc(fp);
574 } while (c != '\n' && c != EOF);
575 }
576
577 if (getentry(buf, &name, &file, &line))
578 {
579 /*
580 * Couldn't parse this line for some reason.
581 * We'll just pretend it never happened.
582 */
583 break;
584 }
585
586 /* Make new entry and add to list. */
587 tp = maketagent(file, (LINENUM) atoi(line), NULL, FALSE);
588 TAG_INS(tp);
589 total++;
590 }
591 if (fp != stdin)
592 {
593 if (pclose(fp))
594 {
595 curtag = NULL;
596 total = curseq = 0;
597 return TAG_NOFILE;
598 }
599 }
600 }
601
602 /* Check to see if we found anything. */
603 tp = taglist.tl_first;
604 if (tp == TAG_END)
605 return TAG_NOTAG;
606 curtag = tp;
607 curseq = 1;
608 return TAG_FOUND;
609 }
610
611 static int circular = 0; /* 1: circular tag structure */
612
613 /*
614 * Return the filename required for the next gtag in the queue that was setup
615 * by findgtag(). The next call to gtagsearch() will try to position at the
616 * appropriate tag.
617 */
nextgtag(void)618 static constant char * nextgtag(void)
619 {
620 struct tag *tp;
621
622 if (curtag == NULL)
623 /* No tag loaded */
624 return NULL;
625
626 tp = curtag->next;
627 if (tp == TAG_END)
628 {
629 if (!circular)
630 return NULL;
631 /* Wrapped around to the head of the queue */
632 curtag = taglist.tl_first;
633 curseq = 1;
634 } else
635 {
636 curtag = tp;
637 curseq++;
638 }
639 return (curtag->tag_file);
640 }
641
642 /*
643 * Return the filename required for the previous gtag in the queue that was
644 * setup by findgtat(). The next call to gtagsearch() will try to position
645 * at the appropriate tag.
646 */
prevgtag(void)647 static constant char * prevgtag(void)
648 {
649 struct tag *tp;
650
651 if (curtag == NULL)
652 /* No tag loaded */
653 return NULL;
654
655 tp = curtag->prev;
656 if (tp == TAG_END)
657 {
658 if (!circular)
659 return NULL;
660 /* Wrapped around to the tail of the queue */
661 curtag = taglist.tl_last;
662 curseq = total;
663 } else
664 {
665 curtag = tp;
666 curseq--;
667 }
668 return (curtag->tag_file);
669 }
670
671 /*
672 * Position the current file at at what is hopefully the tag that was chosen
673 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
674 * if it was unable to position at the tag, 0 if successful.
675 */
gtagsearch(void)676 static POSITION gtagsearch(void)
677 {
678 if (curtag == NULL)
679 return (NULL_POSITION); /* No gtags loaded! */
680 return (find_pos(curtag->tag_linenum));
681 }
682
683 /*
684 * The getentry() parses both standard and extended ctags -x format.
685 *
686 * [standard format]
687 * <tag> <lineno> <file> <image>
688 * +------------------------------------------------
689 * |main 30 main.c main(argc, argv)
690 * |func 21 subr.c func(arg)
691 *
692 * The following commands write this format.
693 * o Traditional Ctags with -x option
694 * o Global with -x option
695 * See <http://www.gnu.org/software/global/global.html>
696 *
697 * [extended format]
698 * <tag> <type> <lineno> <file> <image>
699 * +----------------------------------------------------------
700 * |main function 30 main.c main(argc, argv)
701 * |func function 21 subr.c func(arg)
702 *
703 * The following commands write this format.
704 * o Exuberant Ctags with -x option
705 * See <http://ctags.sourceforge.net>
706 *
707 * Returns 0 on success, -1 on error.
708 * The tag, file, and line will each be NUL-terminated pointers
709 * into buf.
710 */
getentry(char * buf,constant char ** tag,constant char ** file,constant char ** line)711 static int getentry(char *buf, constant char **tag, constant char **file, constant char **line)
712 {
713 char *p = buf;
714
715 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
716 ;
717 if (*p == 0)
718 return (-1);
719 *p++ = 0;
720 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
721 ;
722 if (*p == 0)
723 return (-1);
724 /*
725 * If the second part begin with other than digit,
726 * it is assumed tag type. Skip it.
727 */
728 if (!IS_DIGIT(*p))
729 {
730 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
731 ;
732 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
733 ;
734 }
735 if (!IS_DIGIT(*p))
736 return (-1);
737 *line = p; /* line number */
738 for (*line = p; *p && !IS_SPACE(*p); p++)
739 ;
740 if (*p == 0)
741 return (-1);
742 *p++ = 0;
743 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
744 ;
745 if (*p == 0)
746 return (-1);
747 *file = p; /* file name */
748 for (*file = p; *p && !IS_SPACE(*p); p++)
749 ;
750 if (*p == 0)
751 return (-1);
752 *p = 0;
753
754 /* value check */
755 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)
756 return (0);
757 return (-1);
758 }
759
760 #endif
761