dhcpcd: update README.DRAGONFLY
[dragonfly.git] / contrib / less / tags.c
blob41f6f220e6101f371481ff46e50f61c9cfd97173
1 /*
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.
8 */
11 #include "less.h"
13 #define WHITESP(c) ((c)==' ' || (c)=='\t')
15 #if TAGS
17 public char ztags[] = "tags";
18 public char *tags = ztags;
20 static int total;
21 static int curseq;
23 extern int linenums;
24 extern int sigs;
25 extern int ctldisp;
27 enum tag_result {
28 TAG_FOUND,
29 TAG_NOFILE,
30 TAG_NOTAG,
31 TAG_NOTYPE,
32 TAG_INTR
36 * Tag type
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) */
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).
63 struct taglist {
64 struct tag *tl_first;
65 struct tag *tl_last;
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 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;
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);
84 #define TAG_RM(tp) \
85 (tp)->next->prev = (tp)->prev; \
86 (tp)->prev->next = (tp)->next;
89 * Delete tag structures.
91 public void cleantags(void)
93 struct tag *tp;
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)
102 TAG_RM(tp);
103 free(tp->tag_file);
104 free(tp->tag_pattern);
105 free(tp);
107 curtag = NULL;
108 total = curseq = 0;
112 * Create a new tag entry.
114 static struct tag * maketagent(char *name, char *file, LINENUM linenum, char *pattern, int endline)
116 struct tag *tp;
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
127 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
128 strcpy(tp->tag_pattern, pattern);
130 return (tp);
134 * Get tag mode.
136 public int gettagtype(void)
138 int f;
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)
153 close(f);
154 return T_CTAGS;
156 return T_GTAGS;
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.
166 public void findtag(char *tag)
168 int type = gettagtype();
169 enum tag_result result;
171 if (type == T_CTAGS)
172 result = findctag(tag);
173 else
174 result = findgtag(tag, type);
175 switch (result)
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;
193 * Search for a tag.
195 public POSITION tagsearch(void)
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();
206 * Go to the next tag.
208 public char * nexttag(int n)
210 char *tagfile = (char *) NULL;
212 while (n-- > 0)
213 tagfile = nextgtag();
214 return tagfile;
218 * Go to the previous tag.
220 public char * prevtag(int n)
222 char *tagfile = (char *) NULL;
224 while (n-- > 0)
225 tagfile = prevgtag();
226 return tagfile;
230 * Return the total number of tags.
232 public int ntags(void)
234 return total;
238 * Return the sequence number of current tag.
240 public int curr_tag(void)
242 return curseq;
245 /*****************************************************************************
246 * ctags
250 * Find tags in the "tags" file.
251 * Sets curtag to the first tag entry.
253 static enum tag_result findctag(char *tag)
255 char *p;
256 char *q;
257 FILE *f;
258 int taglen;
259 LINENUM taglinenum;
260 char *tagfile;
261 char *tagpattern;
262 int tagendline;
263 int search_char;
264 int err;
265 char tline[TAGLINE_SIZE];
266 struct tag *tp;
268 p = shell_unquote(tags);
269 f = fopen(p, "r");
270 free(p);
271 if (f == NULL)
272 return TAG_NOFILE;
274 cleantags();
275 total = 0;
276 taglen = (int) strlen(tag);
279 * Search the tags file for the desired tag.
281 while (fgets(tline, sizeof(tline), f) != NULL)
283 if (tline[0] == '!')
284 /* Skip header of extended format. */
285 continue;
286 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
287 continue;
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.
297 tagpattern = NULL;
300 * Skip over the whitespace after the tag name.
302 p = skipsp(tline+taglen);
303 if (*p == '\0')
304 /* File name is missing! */
305 continue;
308 * Save the file name.
309 * Skip over the whitespace after the file name.
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;
321 * First see if it is a line number.
323 tagendline = 0;
324 taglinenum = getnum(&p, 0, &err);
325 if (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.
333 taglinenum = 0;
334 search_char = *p++;
335 if (*p == '^')
336 p++;
337 tagpattern = q = p;
338 while (*p != search_char && *p != '\0')
340 if (*p == '\\')
341 p++;
342 if (q != p)
344 *q++ = *p++;
345 } else
347 q++;
348 p++;
351 tagendline = (q[-1] == '$');
352 if (tagendline)
353 q--;
354 *q = '\0';
356 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
357 TAG_INS(tp);
358 total++;
360 fclose(f);
361 if (total == 0)
362 return TAG_NOTAG;
363 curtag = taglist.tl_first;
364 curseq = 1;
365 return TAG_FOUND;
369 * Edit current tagged file.
371 public int edit_tagfile(void)
373 if (curtag == NULL)
374 return (1);
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);
392 return 1;
394 return 0;
398 * Search for a tag.
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;
409 LINENUM linenum;
410 int line_len;
411 char *line;
412 int found;
414 pos = ch_zero();
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.
423 if (ABORT_SIGS())
424 return (NULL_POSITION);
427 * Read the next line, and save the
428 * starting position of that line in linepos.
430 linepos = pos;
431 pos = forw_raw_line(pos, &line, &line_len);
432 if (linenum != 0)
433 linenum++;
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).
449 if (linenums)
450 add_lnum(linenum, pos);
452 if (ctldisp != OPT_ONPLUS)
454 if (curtag_match(line, linepos))
455 found = 1;
456 } else
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))
464 found = 1;
465 free(chpos);
466 free(cline);
470 return (linepos);
473 /*******************************************************************************
474 * gtags
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)
486 char buf[1024];
487 FILE *fp;
488 struct tag *tp;
490 if (type != T_CTAGS_X && tag == NULL)
491 return TAG_NOFILE;
493 cleantags();
494 total = 0;
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)
502 fp = stdin;
503 /* Set tag default because we cannot read stdin again. */
504 tags = ztags;
505 } else
507 #if !HAVE_POPEN
508 return TAG_NOFILE;
509 #else
510 char *command;
511 char *flag;
512 char *qtag;
513 char *cmd = lgetenv("LESSGLOBALTAGS");
515 if (isnullenv(cmd))
516 return TAG_NOFILE;
517 /* Get suitable flag value for global(1). */
518 switch (type)
520 case T_GTAGS:
521 flag = "" ;
522 break;
523 case T_GRTAGS:
524 flag = "r";
525 break;
526 case T_GSYMS:
527 flag = "s";
528 break;
529 case T_GPATH:
530 flag = "P";
531 break;
532 default:
533 return TAG_NOTYPE;
536 /* Get our data from global(1). */
537 qtag = shell_quote(tag);
538 if (qtag == NULL)
539 qtag = 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);
543 if (qtag != tag)
544 free(qtag);
545 fp = popen(command, "r");
546 free(command);
547 #endif
549 if (fp != NULL)
551 while (fgets(buf, sizeof(buf), fp))
553 char *name, *file, *line;
554 int len;
556 if (sigs)
558 #if HAVE_POPEN
559 if (fp != stdin)
560 pclose(fp);
561 #endif
562 return TAG_INTR;
564 len = (int) strlen(buf);
565 if (len > 0 && buf[len-1] == '\n')
566 buf[len-1] = '\0';
567 else
569 int c;
570 do {
571 c = fgetc(fp);
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.
581 break;
584 /* Make new entry and add to list. */
585 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
586 TAG_INS(tp);
587 total++;
589 if (fp != stdin)
591 if (pclose(fp))
593 curtag = NULL;
594 total = curseq = 0;
595 return TAG_NOFILE;
600 /* Check to see if we found anything. */
601 tp = taglist.tl_first;
602 if (tp == TAG_END)
603 return TAG_NOTAG;
604 curtag = tp;
605 curseq = 1;
606 return TAG_FOUND;
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
614 * appropriate tag.
616 static char * nextgtag(void)
618 struct tag *tp;
620 if (curtag == NULL)
621 /* No tag loaded */
622 return NULL;
624 tp = curtag->next;
625 if (tp == TAG_END)
627 if (!circular)
628 return NULL;
629 /* Wrapped around to the head of the queue */
630 curtag = taglist.tl_first;
631 curseq = 1;
632 } else
634 curtag = tp;
635 curseq++;
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)
647 struct tag *tp;
649 if (curtag == NULL)
650 /* No tag loaded */
651 return NULL;
653 tp = curtag->prev;
654 if (tp == TAG_END)
656 if (!circular)
657 return NULL;
658 /* Wrapped around to the tail of the queue */
659 curtag = taglist.tl_last;
660 curseq = total;
661 } else
663 curtag = tp;
664 curseq--;
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)
676 if (curtag == NULL)
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.
684 * [standard 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>
695 * [extended format]
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
707 * into buf.
709 static int getentry(char *buf, char **tag, char **file, char **line)
711 char *p = buf;
713 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
715 if (*p == 0)
716 return (-1);
717 *p++ = 0;
718 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
720 if (*p == 0)
721 return (-1);
723 * If the second part begin with other than digit,
724 * it is assumed tag type. Skip it.
726 if (!IS_DIGIT(*p))
728 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
730 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
733 if (!IS_DIGIT(*p))
734 return (-1);
735 *line = p; /* line number */
736 for (*line = p; *p && !IS_SPACE(*p); p++)
738 if (*p == 0)
739 return (-1);
740 *p++ = 0;
741 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
743 if (*p == 0)
744 return (-1);
745 *file = p; /* file name */
746 for (*file = p; *p && !IS_SPACE(*p); p++)
748 if (*p == 0)
749 return (-1);
750 *p = 0;
752 /* value check */
753 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)
754 return (0);
755 return (-1);
758 #endif