2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * David Hitz of Auspex Systems, Inc.
10 * See the LICENSE file for redistribution information.
16 static const char sccsid
[] = "@(#)ex_tag.c 10.36 (Berkeley) 9/15/96";
19 #include <sys/param.h>
20 #include <sys/types.h> /* XXX: param.h may not have included types.h */
22 #ifdef HAVE_SYS_MMAN_H
26 #include <sys/queue.h>
30 #include <bitstring.h>
41 #include "../common/common.h"
45 static char *binary_search
__P((char *, char *, char *));
46 static int compare
__P((char *, char *, char *));
47 static void ctag_file
__P((SCR
*, TAGF
*, char *, char **, size_t *));
48 static int ctag_search
__P((SCR
*, char *, size_t, char *));
50 static int getentry
__P((char *, char **, char **, char **));
51 static TAGQ
*gtag_slist
__P((SCR
*, char *, int));
53 static int ctag_sfile
__P((SCR
*, TAGF
*, TAGQ
*, char *));
54 static TAGQ
*ctag_slist
__P((SCR
*, char *));
55 static char *linear_search
__P((char *, char *, char *));
56 static int tag_copy
__P((SCR
*, TAG
*, TAG
**));
57 static int tag_pop
__P((SCR
*, TAGQ
*, int));
58 static int tagf_copy
__P((SCR
*, TAGF
*, TAGF
**));
59 static int tagf_free
__P((SCR
*, TAGF
*));
60 static int tagq_copy
__P((SCR
*, TAGQ
*, TAGQ
**));
64 * The tag code can be entered from main, e.g., "vi -t tag".
66 * PUBLIC: int ex_tag_first __P((SCR *, char *));
69 ex_tag_first(sp
, tagarg
)
76 /* Build an argument for the ex :tag command. */
77 ex_cinit(&cmd
, C_TAG
, 0, OOBLNO
, OOBLNO
, 0, ap
);
78 ex_cadd(&cmd
, &a
, tagarg
, strlen(tagarg
));
82 * Historic vi went ahead and created a temporary file when it failed
83 * to find the tag. We match historic practice, but don't distinguish
84 * between real error and failure to find the tag.
86 if (ex_tag_push(sp
, &cmd
))
89 /* Display tags in the center of the screen. */
90 F_CLR(sp
, SC_SCR_TOP
);
91 F_SET(sp
, SC_SCR_CENTER
);
101 * Enter a new TAGQ context based on a ctag string.
103 * PUBLIC: int ex_rtag_push __P((SCR *, EXCMD *));
106 ex_rtag_push(sp
, cmdp
)
110 F_SET(cmdp
, E_REFERENCE
);
111 return ex_tag_push(sp
, cmdp
);
119 * Enter a new TAGQ context based on a ctag string.
121 * PUBLIC: int ex_tag_push __P((SCR *, EXCMD *));
124 ex_tag_push(sp
, cmdp
)
138 switch (cmdp
->argc
) {
140 if (exp
->tag_last
!= NULL
)
143 if ((exp
->tag_last
= strdup(cmdp
->argv
[0]->bp
)) == NULL
) {
144 msgq(sp
, M_SYSERR
, NULL
);
148 /* Taglength may limit the number of characters. */
150 O_VAL(sp
, O_TAGLENGTH
)) != 0 && strlen(exp
->tag_last
) > tl
)
151 exp
->tag_last
[tl
] = '\0';
154 if (exp
->tag_last
== NULL
) {
155 msgq(sp
, M_ERR
, "158|No previous tag entered");
163 /* Get the tag information. */
165 if (O_ISSET(sp
, O_GTAGSMODE
)) {
166 if ((tqp
= gtag_slist(sp
, exp
->tag_last
, F_ISSET(cmdp
, E_REFERENCE
))) == NULL
)
170 if ((tqp
= ctag_slist(sp
, exp
->tag_last
)) == NULL
)
174 * Allocate all necessary memory before swapping screens. Initialize
175 * flags so we know what to free.
179 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
180 /* Initialize the `local context' tag queue structure. */
181 CALLOC_GOTO(sp
, rtqp
, TAGQ
*, 1, sizeof(TAGQ
));
182 CIRCLEQ_INIT(&rtqp
->tagq
);
184 /* Initialize and link in its tag structure. */
185 CALLOC_GOTO(sp
, rtp
, TAG
*, 1, sizeof(TAG
));
186 CIRCLEQ_INSERT_HEAD(&rtqp
->tagq
, rtp
, q
);
191 * Stick the current context information in a convenient place, we're
192 * about to lose it. Note, if we're called on editor startup, there
193 * will be no FREF structure.
198 istmp
= frp
== NULL
||
199 F_ISSET(frp
, FR_TMPFILE
) && !F_ISSET(cmdp
, E_NEWSCREEN
);
201 /* Try to switch to the tag. */
202 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
203 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
204 if (ex_tag_Nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
207 /* Everything else gets done in the new screen. */
211 if (ex_tag_nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
215 * If this is the first tag, put a `current location' queue entry
216 * in place, so we can pop all the way back to the current mark.
217 * Note, it doesn't point to much of anything, it's a placeholder.
219 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
220 CIRCLEQ_INSERT_HEAD(&exp
->tq
, rtqp
, q
);
222 rtqp
= exp
->tq
.cqh_first
;
224 /* Link the new TAGQ structure into place. */
225 CIRCLEQ_INSERT_HEAD(&exp
->tq
, tqp
, q
);
227 (void)ctag_search(sp
,
228 tqp
->current
->search
, tqp
->current
->slen
, tqp
->tag
);
231 * Move the current context from the temporary save area into the
234 * If we were in a temporary file, we don't have a context to which
235 * we can return, so just make it be the same as what we're moving
236 * to. It will be a little odd that ^T doesn't change anything, but
237 * I don't think it's a big deal.
240 rtqp
->current
->frp
= sp
->frp
;
241 rtqp
->current
->lno
= sp
->lno
;
242 rtqp
->current
->cno
= sp
->cno
;
244 rtqp
->current
->frp
= frp
;
245 rtqp
->current
->lno
= lno
;
246 rtqp
->current
->cno
= cno
;
262 * Switch context to the next TAG.
264 * PUBLIC: int ex_tag_next __P((SCR *, EXCMD *));
267 ex_tag_next(sp
, cmdp
)
276 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
277 tag_msg(sp
, TAG_EMPTY
, NULL
);
280 if ((tp
= tqp
->current
->q
.cqe_next
) == (void *)&tqp
->tagq
) {
281 msgq(sp
, M_ERR
, "282|Already at the last tag of this group");
284 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
288 if (F_ISSET(tqp
, TAG_CSCOPE
))
289 (void)cscope_search(sp
, tqp
, tp
);
291 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
297 * Switch context to the next TAG.
299 * PUBLIC: int ex_tag_prev __P((SCR *, EXCMD *));
302 ex_tag_prev(sp
, cmdp
)
311 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
312 tag_msg(sp
, TAG_EMPTY
, NULL
);
315 if ((tp
= tqp
->current
->q
.cqe_prev
) == (void *)&tqp
->tagq
) {
316 msgq(sp
, M_ERR
, "255|Already at the first tag of this group");
319 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
323 if (F_ISSET(tqp
, TAG_CSCOPE
))
324 (void)cscope_search(sp
, tqp
, tp
);
326 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
332 * Switch context to the specified TAG.
334 * PUBLIC: int ex_tag_nswitch __P((SCR *, TAG *, int));
337 ex_tag_nswitch(sp
, tp
, force
)
342 /* Get a file structure. */
343 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
346 /* If not changing files, return, we're done. */
347 if (tp
->frp
== sp
->frp
)
350 /* Check for permission to leave. */
351 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
354 /* Initialize the new file. */
355 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
358 /* Display tags in the center of the screen. */
359 F_CLR(sp
, SC_SCR_TOP
);
360 F_SET(sp
, SC_SCR_CENTER
);
363 F_SET(sp
, SC_FSWITCH
);
369 * Switch context to the specified TAG in a new screen.
371 * PUBLIC: int ex_tag_Nswitch __P((SCR *, TAG *, int));
374 ex_tag_Nswitch(sp
, tp
, force
)
381 /* Get a file structure. */
382 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
385 /* Get a new screen. */
386 if (screen_init(sp
->gp
, sp
, &new))
388 if (vs_split(sp
, new, 0)) {
389 (void)file_end(new, new->ep
, 1);
390 (void)screen_end(new);
394 /* Get a backing file. */
395 if (tp
->frp
== sp
->frp
) {
396 /* Copy file state. */
401 new->frp
->flags
= sp
->frp
->flags
;
402 } else if (file_init(new, tp
->frp
, NULL
, force
)) {
403 (void)vs_discard(new, NULL
);
404 (void)screen_end(new);
408 /* Create the argument list. */
409 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, tp
->frp
->name
);
411 /* Display tags in the center of the screen. */
412 F_CLR(new, SC_SCR_TOP
);
413 F_SET(new, SC_SCR_CENTER
);
417 F_SET(sp
, SC_SSWITCH
);
424 * :tagp[op][!] [number | file]
426 * Pop to a previous TAGQ context.
428 * PUBLIC: int ex_tag_pop __P((SCR *, EXCMD *));
441 /* Check for an empty stack. */
443 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
444 tag_msg(sp
, TAG_EMPTY
, NULL
);
448 /* Find the last TAG structure that we're going to DISCARD! */
449 switch (cmdp
->argc
) {
450 case 0: /* Pop one tag. */
451 dtqp
= exp
->tq
.cqh_first
;
453 case 1: /* Name or number. */
454 arg
= cmdp
->argv
[0]->bp
;
455 off
= strtol(arg
, &p
, 10);
459 /* Number: pop that many queue entries. */
462 for (tqp
= exp
->tq
.cqh_first
;
463 tqp
!= (void *)&exp
->tq
&& --off
> 1;
464 tqp
= tqp
->q
.cqe_next
);
465 if (tqp
== (void *)&exp
->tq
) {
467 "159|Less than %s entries on the tags stack; use :display t[ags]",
474 /* File argument: pop to that queue entry. */
475 filearg
: arglen
= strlen(arg
);
476 for (tqp
= exp
->tq
.cqh_first
;
477 tqp
!= (void *)&exp
->tq
;
478 dtqp
= tqp
, tqp
= tqp
->q
.cqe_next
) {
479 /* Don't pop to the current file. */
480 if (tqp
== exp
->tq
.cqh_first
)
482 p
= tqp
->current
->frp
->name
;
483 if ((t
= strrchr(p
, '/')) == NULL
)
487 if (!strncmp(arg
, t
, arglen
))
490 if (tqp
== (void *)&exp
->tq
) {
491 msgq_str(sp
, M_ERR
, arg
,
492 "160|No file %s on the tags stack to return to; use :display t[ags]");
495 if (tqp
== exp
->tq
.cqh_first
)
502 return (tag_pop(sp
, dtqp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
506 * ex_tag_top -- :tagt[op][!]
507 * Clear the tag stack.
509 * PUBLIC: int ex_tag_top __P((SCR *, EXCMD *));
520 /* Check for an empty stack. */
521 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
522 tag_msg(sp
, TAG_EMPTY
, NULL
);
526 /* Return to the oldest information. */
528 exp
->tq
.cqh_last
->q
.cqe_prev
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
533 * Pop up to and including the specified TAGQ context.
536 tag_pop(sp
, dtqp
, force
)
548 * Update the cursor from the saved TAG information of the TAG
549 * structure we're moving to.
551 tp
= dtqp
->q
.cqe_next
->current
;
552 if (tp
->frp
== sp
->frp
) {
556 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
559 tp
->frp
->lno
= tp
->lno
;
560 tp
->frp
->cno
= tp
->cno
;
561 F_SET(sp
->frp
, FR_CURSORSET
);
562 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
565 F_SET(sp
, SC_FSWITCH
);
568 /* Pop entries off the queue up to and including dtqp. */
570 tqp
= exp
->tq
.cqh_first
;
571 if (tagq_free(sp
, tqp
))
573 } while (tqp
!= dtqp
);
576 * If only a single tag left, we've returned to the first tag point,
577 * and the stack is now empty.
579 if (exp
->tq
.cqh_first
->q
.cqe_next
== (void *)&exp
->tq
)
580 tagq_free(sp
, exp
->tq
.cqh_first
);
587 * Display the list of tags.
589 * PUBLIC: int ex_tag_display __P((SCR *));
603 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
604 tag_msg(sp
, TAG_EMPTY
, NULL
);
609 * We give the file name 20 columns and the search string the rest.
610 * If there's not enough room, we don't do anything special, it's
611 * not worth the effort, it just makes the display more confusing.
613 * We also assume that characters in file names map 1-1 to printing
614 * characters. This might not be true, but I don't think it's worth
615 * fixing. (The obvious fix is to pass the filenames through the
616 * msg_print function.)
618 #define L_NAME 30 /* Name. */
619 #define L_SLOP 4 /* Leading number plus trailing *. */
620 #define L_SPACE 5 /* Spaces after name, before tag. */
621 #define L_TAG 20 /* Tag. */
622 if (sp
->cols
<= L_NAME
+ L_SLOP
) {
623 msgq(sp
, M_ERR
, "292|Display too small.");
628 * Display the list of tags for each queue entry. The first entry
629 * is numbered, and the current tag entry has an asterisk appended.
631 for (cnt
= 1, tqp
= exp
->tq
.cqh_first
; !INTERRUPTED(sp
) &&
632 tqp
!= (void *)&exp
->tq
; ++cnt
, tqp
= tqp
->q
.cqe_next
)
633 for (tp
= tqp
->tagq
.cqh_first
;
634 tp
!= (void *)&tqp
->tagq
; tp
= tp
->q
.cqe_next
) {
635 if (tp
== tqp
->tagq
.cqh_first
)
636 (void)ex_printf(sp
, "%2d ", cnt
);
638 (void)ex_printf(sp
, " ");
639 p
= tp
->frp
== NULL
? tp
->fname
: tp
->frp
->name
;
640 if ((len
= strlen(p
)) > L_NAME
) {
641 len
= len
- (L_NAME
- 4);
642 (void)ex_printf(sp
, " ... %*.*s",
643 L_NAME
- 4, L_NAME
- 4, p
+ len
);
646 " %*.*s", L_NAME
, L_NAME
, p
);
647 if (tqp
->current
== tp
)
648 (void)ex_printf(sp
, "*");
650 if (tp
== tqp
->tagq
.cqh_first
&& tqp
->tag
!= NULL
&&
651 (sp
->cols
- L_NAME
) >= L_TAG
+ L_SPACE
) {
652 len
= strlen(tqp
->tag
);
653 if (len
> sp
->cols
- (L_NAME
+ L_SPACE
))
654 len
= sp
->cols
- (L_NAME
+ L_SPACE
);
655 (void)ex_printf(sp
, "%s%.*s",
656 tqp
->current
== tp
? " " : " ",
659 (void)ex_printf(sp
, "\n");
666 * Copy a screen's tag structures.
668 * PUBLIC: int ex_tag_copy __P((SCR *, SCR *));
671 ex_tag_copy(orig
, sp
)
674 EX_PRIVATE
*oexp
, *nexp
;
682 /* Copy tag queue and tags stack. */
683 for (aqp
= oexp
->tq
.cqh_first
;
684 aqp
!= (void *)&oexp
->tq
; aqp
= aqp
->q
.cqe_next
) {
685 if (tagq_copy(sp
, aqp
, &tqp
))
687 for (ap
= aqp
->tagq
.cqh_first
;
688 ap
!= (void *)&aqp
->tagq
; ap
= ap
->q
.cqe_next
) {
689 if (tag_copy(sp
, ap
, &tp
))
691 /* Set the current pointer. */
692 if (aqp
->current
== ap
)
694 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
696 CIRCLEQ_INSERT_TAIL(&nexp
->tq
, tqp
, q
);
699 /* Copy list of tag files. */
700 for (atfp
= oexp
->tagfq
.tqh_first
;
701 atfp
!= NULL
; atfp
= atfp
->q
.tqe_next
) {
702 if (tagf_copy(sp
, atfp
, &tfp
))
704 TAILQ_INSERT_TAIL(&nexp
->tagfq
, tfp
, q
);
707 /* Copy the last tag. */
708 if (oexp
->tag_last
!= NULL
&&
709 (nexp
->tag_last
= strdup(oexp
->tag_last
)) == NULL
) {
710 msgq(sp
, M_SYSERR
, NULL
);
718 * Copy a TAGF structure and return it in new memory.
721 tagf_copy(sp
, otfp
, tfpp
)
727 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
730 /* XXX: Allocate as part of the TAGF structure!!! */
731 if ((tfp
->name
= strdup(otfp
->name
)) == NULL
)
740 * Copy a TAGQ structure and return it in new memory.
743 tagq_copy(sp
, otqp
, tqpp
)
751 if (otqp
->tag
!= NULL
)
752 len
+= otqp
->tlen
+ 1;
753 MALLOC_RET(sp
, tqp
, TAGQ
*, len
);
754 memcpy(tqp
, otqp
, len
);
756 CIRCLEQ_INIT(&tqp
->tagq
);
758 if (otqp
->tag
!= NULL
)
767 * Copy a TAG structure and return it in new memory.
770 tag_copy(sp
, otp
, tpp
)
778 if (otp
->fname
!= NULL
)
779 len
+= otp
->fnlen
+ 1;
780 if (otp
->search
!= NULL
)
781 len
+= otp
->slen
+ 1;
782 MALLOC_RET(sp
, tp
, TAG
*, len
);
783 memcpy(tp
, otp
, len
);
785 if (otp
->fname
!= NULL
)
787 if (otp
->search
!= NULL
)
788 tp
->search
= tp
->fname
+ otp
->fnlen
+ 1;
796 * Free a TAGF structure.
806 TAILQ_REMOVE(&exp
->tagfq
, tfp
, q
);
814 * Free a TAGQ structure (and associated TAG structures).
816 * PUBLIC: int tagq_free __P((SCR *, TAGQ *));
827 while ((tp
= tqp
->tagq
.cqh_first
) != (void *)&tqp
->tagq
) {
828 CIRCLEQ_REMOVE(&tqp
->tagq
, tp
, q
);
833 * If allocated and then the user failed to switch files, the TAGQ
834 * structure was never attached to any list.
836 if (tqp
->q
.cqe_next
!= NULL
)
837 CIRCLEQ_REMOVE(&exp
->tq
, tqp
, q
);
844 * A few common messages.
846 * PUBLIC: void tag_msg __P((SCR *, tagmsg_t, char *));
849 tag_msg(sp
, msg
, tag
)
856 msgq_str(sp
, M_ERR
, tag
,
857 "164|%s: the tag's line number is past the end of the file");
860 msgq(sp
, M_INFO
, "165|The tags stack is empty");
863 msgq_str(sp
, M_ERR
, tag
, "166|%s: search pattern not found");
872 * Create a new list of ctag files.
874 * PUBLIC: int ex_tagf_alloc __P((SCR *, char *));
877 ex_tagf_alloc(sp
, str
)
886 /* Free current queue. */
888 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
891 /* Create new queue. */
892 for (p
= t
= str
;; ++p
) {
893 if (*p
== '\0' || isblank(*p
)) {
894 if ((len
= p
- t
) > 1) {
895 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
896 MALLOC(sp
, tfp
->name
, char *, len
+ 1);
897 if (tfp
->name
== NULL
) {
901 memcpy(tfp
->name
, t
, len
);
902 tfp
->name
[len
] = '\0';
904 TAILQ_INSERT_TAIL(&exp
->tagfq
, tfp
, q
);
913 /* Free previous queue. */
916 * Free the ex tag information.
918 * PUBLIC: int ex_tag_free __P((SCR *));
928 /* Free up tag information. */
930 while ((tqp
= exp
->tq
.cqh_first
) != (void *)&exp
->tq
)
932 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
934 if (exp
->tag_last
!= NULL
)
941 * Search a file for a tag.
944 ctag_search(sp
, search
, slen
, tag
)
954 * The historic tags file format (from a long, long time ago...)
955 * used a line number, not a search string. I got complaints, so
956 * people are still using the format. POSIX 1003.2 permits it.
958 if (isdigit(search
[0])) {
959 m
.lno
= atoi(search
);
960 if (!db_exist(sp
, m
.lno
)) {
961 tag_msg(sp
, TAG_BADLNO
, tag
);
966 * Search for the tag; cheap fallback for C functions
967 * if the name is the same but the arguments have changed.
971 if (f_search(sp
, &m
, &m
,
972 search
, slen
, NULL
, SEARCH_FILE
| SEARCH_TAG
))
973 if ((p
= strrchr(search
, '(')) != NULL
) {
975 if (f_search(sp
, &m
, &m
, search
, slen
,
976 NULL
, SEARCH_FILE
| SEARCH_TAG
))
979 notfound
: tag_msg(sp
, TAG_SEARCH
, tag
);
984 * Historically, tags set the search direction if it wasn't
987 if (sp
->searchdir
== NOTSET
)
988 sp
->searchdir
= FORWARD
;
993 * Tags move to the first non-blank, NOT the search pattern start.
997 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1004 * get tag information from current line.
1006 * gtags temporary file format.
1007 * <tag> <lineno> <file> <image>
1010 * +------------------------------------------------
1011 * |main 30 main.c main(argc, argv)
1012 * |func 21 subr.c func(arg)
1015 getentry(buf
, tag
, file
, line
)
1016 char *buf
, **tag
, **file
, **line
;
1020 for (*tag
= p
; *p
&& !isspace(*p
); p
++) /* tag name */
1025 for (; *p
&& isspace(*p
); p
++) /* (skip blanks) */
1029 *line
= p
; /* line no */
1030 for (*line
= p
; *p
&& !isspace(*p
); p
++)
1035 for (; *p
&& isspace(*p
); p
++) /* (skip blanks) */
1039 *file
= p
; /* file name */
1040 for (*file
= p
; *p
&& !isspace(*p
); p
++)
1047 if (strlen(*tag
) && strlen(*line
) && strlen(*file
) && atoi(*line
) > 0)
1050 return 0; /* ERROR */
1055 * Search the list of tags files for a tag, and return tag queue.
1058 gtag_slist(sp
, tag
, ref
)
1069 char *name
, *file
, *line
;
1070 char command
[BUFSIZ
];
1074 /* Allocate and initialize the tag queue structure. */
1076 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
1077 CIRCLEQ_INIT(&tqp
->tagq
);
1078 tqp
->tag
= tqp
->buf
;
1079 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
1082 * Find the tag, only display missing file messages once, and
1083 * then only if we didn't find the tag.
1085 snprintf(command
, sizeof(command
), "global -%s '%s'", ref
? "rx" : "x", tag
);
1086 if (fp
= popen(command
, "r")) {
1087 while (fgets(buf
, sizeof(buf
), fp
)) {
1088 if (buf
[strlen(buf
)-1] == '\n') /* chop(buf) */
1089 buf
[strlen(buf
)-1] = 0;
1091 while (fgetc(fp
) != '\n')
1093 if (getentry(buf
, &name
, &file
, &line
) == 0) {
1095 F_SET(tfp
, TAGF_ERR
);
1099 TAG
*, 1, sizeof(TAG
) + strlen(file
) + 1 + strlen(line
) + 1);
1100 tp
->fname
= tp
->buf
;
1101 strcpy(tp
->fname
, file
);
1102 tp
->fnlen
= strlen(file
);
1103 tp
->search
= tp
->fname
+ tp
->fnlen
+ 1;
1104 strcpy(tp
->search
, line
);
1105 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
1110 /* Check to see if we found anything. */
1111 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
1112 msgq_str(sp
, M_ERR
, tag
, "162|%s: tag not found");
1117 tqp
->current
= tqp
->tagq
.cqh_first
;
1126 * Search the list of tags files for a tag, and return tag queue.
1141 /* Allocate and initialize the tag queue structure. */
1143 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
1144 CIRCLEQ_INIT(&tqp
->tagq
);
1145 tqp
->tag
= tqp
->buf
;
1146 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
1149 * Find the tag, only display missing file messages once, and
1150 * then only if we didn't find the tag.
1153 tfp
= exp
->tagfq
.tqh_first
; tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1154 if (ctag_sfile(sp
, tfp
, tqp
, tag
)) {
1156 F_SET(tfp
, TAGF_ERR
);
1158 F_CLR(tfp
, TAGF_ERR
| TAGF_ERR_WARN
);
1160 /* Check to see if we found anything. */
1161 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
1162 msgq_str(sp
, M_ERR
, tag
, "162|%s: tag not found");
1164 for (tfp
= exp
->tagfq
.tqh_first
;
1165 tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1166 if (F_ISSET(tfp
, TAGF_ERR
) &&
1167 !F_ISSET(tfp
, TAGF_ERR_WARN
)) {
1168 errno
= tfp
->errnum
;
1169 msgq_str(sp
, M_SYSERR
, tfp
->name
, "%s");
1170 F_SET(tfp
, TAGF_ERR_WARN
);
1176 tqp
->current
= tqp
->tagq
.cqh_first
;
1185 * Search a tags file for a tag, adding any found to the tag queue.
1188 ctag_sfile(sp
, tfp
, tqp
, tname
)
1196 size_t dlen
, nlen
, slen
;
1197 int fd
, i
, nf1
, nf2
;
1198 char *back
, *cname
, *dname
, *front
, *map
, *name
, *p
, *search
, *t
;
1200 if ((fd
= open(tfp
->name
, O_RDONLY
, 0)) < 0) {
1201 tfp
->errnum
= errno
;
1207 * Some old BSD systems require MAP_FILE as an argument when mapping
1215 * We'd like to test if the file is too big to mmap. Since we don't
1216 * know what size or type off_t's or size_t's are, what the largest
1217 * unsigned integral type is, or what random insanity the local C
1218 * compiler will perpetrate, doing the comparison in a portable way
1219 * is flatly impossible. Hope mmap fails if the file is too large.
1221 if (fstat(fd
, &sb
) != 0 ||
1222 (map
= mmap(NULL
, (size_t)sb
.st_size
, PROT_READ
| PROT_WRITE
,
1223 MAP_FILE
| MAP_PRIVATE
, fd
, (off_t
)0)) == (caddr_t
)-1) {
1224 tfp
->errnum
= errno
;
1230 back
= front
+ sb
.st_size
;
1231 front
= binary_search(tname
, front
, back
);
1232 front
= linear_search(tname
, front
, back
);
1237 * Initialize and link in the tag structure(s). The historic ctags
1238 * file format only permitted a single tag location per tag. The
1239 * obvious extension to permit multiple tags locations per tag is to
1240 * output multiple records in the standard format. Unfortunately,
1241 * this won't work correctly with historic ex/vi implementations,
1242 * because their binary search assumes that there's only one record
1243 * per tag, and so will use a random tag entry if there si more than
1244 * one. This code handles either format.
1246 * The tags file is in the following format:
1248 * <tag> <filename> <line number> | <pattern>
1250 * Figure out how long everything is so we can allocate in one swell
1251 * foop, but discard anything that looks wrong.
1254 /* Nul-terminate the end of the line. */
1255 for (p
= front
; p
< back
&& *p
!= '\n'; ++p
);
1256 if (p
== back
|| *p
!= '\n')
1260 /* Update the pointers for the next time. */
1265 /* Break the line into tokens. */
1266 for (i
= 0; i
< 2 && (t
= strsep(&p
, "\t ")) != NULL
; ++i
)
1271 case 1: /* Filename. */
1273 nlen
= strlen(name
);
1277 /* Check for corruption. */
1278 if (i
!= 2 || p
== NULL
|| t
== NULL
)
1281 /* The rest of the string is the search pattern. */
1283 if ((slen
= strlen(p
)) == 0) {
1284 corrupt
: p
= msg_print(sp
, tname
, &nf1
);
1285 t
= msg_print(sp
, tfp
->name
, &nf2
);
1286 msgq(sp
, M_ERR
, "163|%s: corrupted tag in %s", p
, t
);
1288 FREE_SPACE(sp
, p
, 0);
1290 FREE_SPACE(sp
, t
, 0);
1294 /* Check for passing the last entry. */
1295 if (strcmp(tname
, cname
))
1298 /* Resolve the file name. */
1299 ctag_file(sp
, tfp
, name
, &dname
, &dlen
);
1302 TAG
*, 1, sizeof(TAG
) + dlen
+ 2 + nlen
+ 1 + slen
+ 1);
1303 tp
->fname
= tp
->buf
;
1305 memcpy(tp
->fname
, dname
, dlen
);
1306 tp
->fname
[dlen
] = '/';
1309 memcpy(tp
->fname
+ dlen
, name
, nlen
+ 1);
1310 tp
->fnlen
= dlen
+ nlen
;
1311 tp
->search
= tp
->fname
+ tp
->fnlen
+ 1;
1312 memcpy(tp
->search
, search
, (tp
->slen
= slen
) + 1);
1313 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
1317 done
: if (munmap(map
, (size_t)sb
.st_size
))
1318 msgq(sp
, M_SYSERR
, "munmap");
1320 msgq(sp
, M_SYSERR
, "close");
1326 * Search for the right path to this file.
1329 ctag_file(sp
, tfp
, name
, dirp
, dlenp
)
1337 char *p
, buf
[MAXPATHLEN
];
1341 * If the tag file path is a relative path, see if it exists. If it
1342 * doesn't, look relative to the tags file path. It's okay for a tag
1343 * file to not exist, and historically, vi simply displayed a "new"
1344 * file. However, if the path exists relative to the tag file, it's
1345 * pretty clear what's happening, so we may as well get it right.
1348 if (name
[0] != '/' &&
1349 stat(name
, &sb
) && (p
= strrchr(tfp
->name
, '/')) != NULL
) {
1351 len
= snprintf(buf
, sizeof(buf
), "%s/%s", tfp
->name
, name
);
1353 if (stat(buf
, &sb
) == 0) {
1355 *dlenp
= strlen(*dirp
);
1361 * Binary search for "string" in memory between "front" and "back".
1363 * This routine is expected to return a pointer to the start of a line at
1364 * *or before* the first word matching "string". Relaxing the constraint
1365 * this way simplifies the algorithm.
1368 * front points to the beginning of a line at or before the first
1371 * back points to the beginning of a line at or after the first
1374 * Base of the Invariants.
1378 * Advancing the Invariants:
1380 * p = first newline after halfway point from front to back.
1382 * If the string at "p" is not greater than the string to match,
1383 * p is the new front. Otherwise it is the new back.
1387 * The definition of the routine allows it return at any point,
1388 * since front is always at or before the line to print.
1390 * In fact, it returns when the chosen "p" equals "back". This
1391 * implies that there exists a string is least half as long as
1392 * (back - front), which in turn implies that a linear search will
1393 * be no more expensive than the cost of simply printing a string or two.
1395 * Trying to continue with binary search at this point would be
1396 * more trouble than it's worth.
1402 #define SKIP_PAST_NEWLINE(p, back) while (p < back && *p++ != '\n');
1405 binary_search(string
, front
, back
)
1406 register char *string
, *front
, *back
;
1410 p
= front
+ (back
- front
) / 2;
1411 SKIP_PAST_NEWLINE(p
, back
);
1414 if (compare(string
, p
, back
) == GREATER
)
1418 p
= front
+ (back
- front
) / 2;
1419 SKIP_PAST_NEWLINE(p
, back
);
1425 * Find the first line that starts with string, linearly searching from front
1428 * Return NULL for no such line.
1430 * This routine assumes:
1432 * o front points at the first character in a line.
1433 * o front is before or at the first line to be printed.
1436 linear_search(string
, front
, back
)
1437 char *string
, *front
, *back
;
1439 while (front
< back
) {
1440 switch (compare(string
, front
, back
)) {
1441 case EQUAL
: /* Found it. */
1443 case LESS
: /* No such string. */
1445 case GREATER
: /* Keep going. */
1448 SKIP_PAST_NEWLINE(front
, back
);
1454 * Return LESS, GREATER, or EQUAL depending on how the string1 compares
1455 * with string2 (s1 ??? s2).
1457 * o Matches up to len(s1) are EQUAL.
1458 * o Matches up to len(s2) are GREATER.
1460 * The string "s1" is null terminated. The string s2 is '\t', space, (or
1461 * "back") terminated.
1464 * Reasonably modern ctags programs use tabs as separators, not spaces.
1465 * However, historic programs did use spaces, and, I got complaints.
1468 compare(s1
, s2
, back
)
1469 register char *s1
, *s2
, *back
;
1471 for (; *s1
&& s2
< back
&& (*s2
!= '\t' && *s2
!= ' '); ++s1
, ++s2
)
1473 return (*s1
< *s2
? LESS
: GREATER
);
1474 return (*s1
? GREATER
: s2
< back
&&
1475 (*s2
!= '\t' && *s2
!= ' ') ? LESS
: EQUAL
);