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 /*
326 * Line numbers start from 1.
327 */
328 if (n == 0)
329 continue;
330 taglinenum = n;
331 }
332 else
333 {
334 /*
335 * No, it must be a pattern.
336 * Delete the initial "^" (if present) and
337 * the final "$" from the pattern.
338 * Delete any backslash in the pattern.
339 */
340 taglinenum = 0;
341 search_char = *p++;
342 if (*p == '^')
343 p++;
344 tagpattern = q = p;
345 while (*p != search_char && *p != '\0')
346 {
347 if (*p == '\\')
348 p++;
349 if (q != p)
350 {
351 *q++ = *p++;
352 } else
353 {
354 q++;
355 p++;
356 }
357 }
358 tagendline = (q[-1] == '$');
359 if (tagendline)
360 q--;
361 *q = '\0';
362 }
363 tp = maketagent(tagfile, taglinenum, tagpattern, tagendline);
364 TAG_INS(tp);
365 total++;
366 }
367 fclose(f);
368 if (total == 0)
369 return TAG_NOTAG;
370 curtag = taglist.tl_first;
371 curseq = 1;
372 return TAG_FOUND;
373 }
374
375 /*
376 * Edit current tagged file.
377 */
edit_tagfile(void)378 public int edit_tagfile(void)
379 {
380 if (curtag == NULL)
381 return (1);
382 return (edit(curtag->tag_file));
383 }
384
curtag_match(char constant * line,POSITION linepos)385 static int curtag_match(char constant *line, POSITION linepos)
386 {
387 /*
388 * Test the line to see if we have a match.
389 * Use strncmp because the pattern may be
390 * truncated (in the tags file) if it is too long.
391 * If tagendline is set, make sure we match all
392 * the way to end of line (no extra chars after the match).
393 */
394 size_t len = strlen(curtag->tag_pattern);
395 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
396 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
397 {
398 curtag->tag_linenum = find_linenum(linepos);
399 return 1;
400 }
401 return 0;
402 }
403
404 /*
405 * Search for a tag.
406 * This is a stripped-down version of search().
407 * We don't use search() for several reasons:
408 * - We don't want to blow away any search string we may have saved.
409 * - The various regular-expression functions (from different systems:
410 * regcmp vs. re_comp) behave differently in the presence of
411 * parentheses (which are almost always found in a tag).
412 */
ctagsearch(void)413 static POSITION ctagsearch(void)
414 {
415 POSITION pos, linepos;
416 LINENUM linenum;
417 size_t line_len;
418 constant char *line;
419 int found;
420
421 pos = ch_zero();
422 linenum = find_linenum(pos);
423
424 for (found = 0; !found;)
425 {
426 /*
427 * Get lines until we find a matching one or
428 * until we hit end-of-file.
429 */
430 if (ABORT_SIGS())
431 return (NULL_POSITION);
432
433 /*
434 * Read the next line, and save the
435 * starting position of that line in linepos.
436 */
437 linepos = pos;
438 pos = forw_raw_line(pos, &line, &line_len);
439 if (linenum != 0)
440 linenum++;
441
442 if (pos == NULL_POSITION)
443 {
444 /*
445 * We hit EOF without a match.
446 */
447 error("Tag not found", NULL_PARG);
448 return (NULL_POSITION);
449 }
450
451 /*
452 * If we're using line numbers, we might as well
453 * remember the information we have now (the position
454 * and line number of the current line).
455 */
456 if (linenums)
457 add_lnum(linenum, pos);
458
459 if (ctldisp != OPT_ONPLUS)
460 {
461 if (curtag_match(line, linepos))
462 found = 1;
463 } else
464 {
465 int cvt_ops = CVT_ANSI;
466 size_t cvt_len = cvt_length(line_len, cvt_ops);
467 int *chpos = cvt_alloc_chpos(cvt_len);
468 char *cline = (char *) ecalloc(1, cvt_len);
469 cvt_text(cline, line, chpos, &line_len, cvt_ops);
470 if (curtag_match(cline, linepos))
471 found = 1;
472 free(chpos);
473 free(cline);
474 }
475 }
476
477 return (linepos);
478 }
479
480 /*******************************************************************************
481 * gtags
482 */
483
484 /*
485 * Find tags in the GLOBAL's tag file.
486 * The findgtag() will try and load information about the requested tag.
487 * It does this by calling "global -x tag" and storing the parsed output
488 * for future use by gtagsearch().
489 * Sets curtag to the first tag entry.
490 */
findgtag(constant char * tag,int type)491 static enum tag_result findgtag(constant char *tag, int type)
492 {
493 char buf[1024];
494 FILE *fp;
495 struct tag *tp;
496
497 if (type != T_CTAGS_X && tag == NULL)
498 return TAG_NOFILE;
499
500 cleantags();
501 total = 0;
502
503 /*
504 * If type == T_CTAGS_X then read ctags's -x format from stdin
505 * else execute global(1) and read from it.
506 */
507 if (type == T_CTAGS_X)
508 {
509 fp = stdin;
510 /* Set tag default because we cannot read stdin again. */
511 tags = ztags;
512 } else
513 {
514 #if !HAVE_POPEN
515 return TAG_NOFILE;
516 #else
517 char *command;
518 char *flag;
519 char *qtag;
520 constant char *cmd = lgetenv("LESSGLOBALTAGS");
521
522 if (isnullenv(cmd))
523 return TAG_NOFILE;
524 /* Get suitable flag value for global(1). */
525 switch (type)
526 {
527 case T_GTAGS:
528 flag = "" ;
529 break;
530 case T_GRTAGS:
531 flag = "r";
532 break;
533 case T_GSYMS:
534 flag = "s";
535 break;
536 case T_GPATH:
537 flag = "P";
538 break;
539 default:
540 return TAG_NOTYPE;
541 }
542
543 /* Get our data from global(1). */
544 qtag = shell_quote(tag);
545 if (qtag == NULL)
546 qtag = save(tag);
547 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
548 strlen(qtag) + 5, sizeof(char));
549 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
550 free(qtag);
551 fp = popen(command, "r");
552 free(command);
553 #endif
554 }
555 if (fp != NULL)
556 {
557 while (fgets(buf, sizeof(buf), fp))
558 {
559 constant char *name;
560 constant char *file;
561 constant char *line;
562 size_t len;
563
564 if (sigs)
565 {
566 #if HAVE_POPEN
567 if (fp != stdin)
568 pclose(fp);
569 #endif
570 return TAG_INTR;
571 }
572 len = strlen(buf);
573 if (len > 0 && buf[len-1] == '\n')
574 buf[len-1] = '\0';
575 else
576 {
577 int c;
578 do {
579 c = fgetc(fp);
580 } while (c != '\n' && c != EOF);
581 }
582
583 if (getentry(buf, &name, &file, &line))
584 {
585 /*
586 * Couldn't parse this line for some reason.
587 * We'll just pretend it never happened.
588 */
589 break;
590 }
591
592 /* Make new entry and add to list. */
593 tp = maketagent(file, (LINENUM) atoi(line), NULL, FALSE);
594 TAG_INS(tp);
595 total++;
596 }
597 if (fp != stdin)
598 {
599 if (pclose(fp))
600 {
601 curtag = NULL;
602 total = curseq = 0;
603 return TAG_NOFILE;
604 }
605 }
606 }
607
608 /* Check to see if we found anything. */
609 tp = taglist.tl_first;
610 if (tp == TAG_END)
611 return TAG_NOTAG;
612 curtag = tp;
613 curseq = 1;
614 return TAG_FOUND;
615 }
616
617 static int circular = 0; /* 1: circular tag structure */
618
619 /*
620 * Return the filename required for the next gtag in the queue that was setup
621 * by findgtag(). The next call to gtagsearch() will try to position at the
622 * appropriate tag.
623 */
nextgtag(void)624 static constant char * nextgtag(void)
625 {
626 struct tag *tp;
627
628 if (curtag == NULL)
629 /* No tag loaded */
630 return NULL;
631
632 tp = curtag->next;
633 if (tp == TAG_END)
634 {
635 if (!circular)
636 return NULL;
637 /* Wrapped around to the head of the queue */
638 curtag = taglist.tl_first;
639 curseq = 1;
640 } else
641 {
642 curtag = tp;
643 curseq++;
644 }
645 return (curtag->tag_file);
646 }
647
648 /*
649 * Return the filename required for the previous gtag in the queue that was
650 * setup by findgtat(). The next call to gtagsearch() will try to position
651 * at the appropriate tag.
652 */
prevgtag(void)653 static constant char * prevgtag(void)
654 {
655 struct tag *tp;
656
657 if (curtag == NULL)
658 /* No tag loaded */
659 return NULL;
660
661 tp = curtag->prev;
662 if (tp == TAG_END)
663 {
664 if (!circular)
665 return NULL;
666 /* Wrapped around to the tail of the queue */
667 curtag = taglist.tl_last;
668 curseq = total;
669 } else
670 {
671 curtag = tp;
672 curseq--;
673 }
674 return (curtag->tag_file);
675 }
676
677 /*
678 * Position the current file at at what is hopefully the tag that was chosen
679 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
680 * if it was unable to position at the tag, 0 if successful.
681 */
gtagsearch(void)682 static POSITION gtagsearch(void)
683 {
684 if (curtag == NULL)
685 return (NULL_POSITION); /* No gtags loaded! */
686 return (find_pos(curtag->tag_linenum));
687 }
688
689 /*
690 * The getentry() parses both standard and extended ctags -x format.
691 *
692 * [standard format]
693 * <tag> <lineno> <file> <image>
694 * +------------------------------------------------
695 * |main 30 main.c main(argc, argv)
696 * |func 21 subr.c func(arg)
697 *
698 * The following commands write this format.
699 * o Traditional Ctags with -x option
700 * o Global with -x option
701 * See <http://www.gnu.org/software/global/global.html>
702 *
703 * [extended format]
704 * <tag> <type> <lineno> <file> <image>
705 * +----------------------------------------------------------
706 * |main function 30 main.c main(argc, argv)
707 * |func function 21 subr.c func(arg)
708 *
709 * The following commands write this format.
710 * o Exuberant Ctags with -x option
711 * See <http://ctags.sourceforge.net>
712 *
713 * Returns 0 on success, -1 on error.
714 * The tag, file, and line will each be NUL-terminated pointers
715 * into buf.
716 */
getentry(char * buf,constant char ** tag,constant char ** file,constant char ** line)717 static int getentry(char *buf, constant char **tag, constant char **file, constant char **line)
718 {
719 char *p = buf;
720
721 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
722 ;
723 if (*p == 0)
724 return (-1);
725 *p++ = 0;
726 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
727 ;
728 if (*p == 0)
729 return (-1);
730 /*
731 * If the second part begin with other than digit,
732 * it is assumed tag type. Skip it.
733 */
734 if (!IS_DIGIT(*p))
735 {
736 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
737 ;
738 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
739 ;
740 }
741 if (!IS_DIGIT(*p))
742 return (-1);
743 *line = p; /* line number */
744 for (*line = p; *p && !IS_SPACE(*p); p++)
745 ;
746 if (*p == 0)
747 return (-1);
748 *p++ = 0;
749 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
750 ;
751 if (*p == 0)
752 return (-1);
753 *file = p; /* file name */
754 for (*file = p; *p && !IS_SPACE(*p); p++)
755 ;
756 if (*p == 0)
757 return (-1);
758 *p = 0;
759
760 /* value check */
761 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)
762 return (0);
763 return (-1);
764 }
765
766 #endif
767