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
[] = "$Id: ex_tag.c,v 10.46 2000/07/16 20:49:32 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:32 $";
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_T
*, size_t, char *));
49 static int ctag_sfile
__P((SCR
*, TAGF
*, TAGQ
*, char *));
50 static TAGQ
*ctag_slist
__P((SCR
*, CHAR_T
*));
51 static char *linear_search
__P((char *, char *, char *));
52 static int tag_copy
__P((SCR
*, TAG
*, TAG
**));
53 static int tag_pop
__P((SCR
*, TAGQ
*, int));
54 static int tagf_copy
__P((SCR
*, TAGF
*, TAGF
**));
55 static int tagf_free
__P((SCR
*, TAGF
*));
56 static int tagq_copy
__P((SCR
*, TAGQ
*, TAGQ
**));
60 * The tag code can be entered from main, e.g., "vi -t tag".
62 * PUBLIC: int ex_tag_first __P((SCR *, CHAR_T *));
65 ex_tag_first(sp
, tagarg
)
71 /* Build an argument for the ex :tag command. */
72 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, OOBLNO
, 0);
73 argv_exp0(sp
, &cmd
, tagarg
, v_strlen(tagarg
));
77 * Historic vi went ahead and created a temporary file when it failed
78 * to find the tag. We match historic practice, but don't distinguish
79 * between real error and failure to find the tag.
81 if (ex_tag_push(sp
, &cmd
))
84 /* Display tags in the center of the screen. */
85 F_CLR(sp
, SC_SCR_TOP
);
86 F_SET(sp
, SC_SCR_CENTER
);
95 * Enter a new TAGQ context based on a ctag string.
97 * PUBLIC: int ex_tag_push __P((SCR *, EXCMD *));
100 ex_tag_push(sp
, cmdp
)
109 switch (cmdp
->argc
) {
111 if (exp
->tag_last
!= NULL
)
114 if ((exp
->tag_last
= v_wstrdup(sp
, cmdp
->argv
[0]->bp
,
115 cmdp
->argv
[0]->len
)) == NULL
) {
116 msgq(sp
, M_SYSERR
, NULL
);
120 /* Taglength may limit the number of characters. */
122 O_VAL(sp
, O_TAGLENGTH
)) != 0 && v_strlen(exp
->tag_last
) > tl
)
123 exp
->tag_last
[tl
] = '\0';
126 if (exp
->tag_last
== NULL
) {
127 msgq(sp
, M_ERR
, "158|No previous tag entered");
135 /* Get the tag information. */
136 if ((tqp
= ctag_slist(sp
, exp
->tag_last
)) == NULL
)
139 if (tagq_push(sp
, tqp
, F_ISSET(cmdp
, E_NEWSCREEN
),
140 FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
148 * Switch context to the next TAG.
150 * PUBLIC: int ex_tag_next __P((SCR *, EXCMD *));
153 ex_tag_next(sp
, cmdp
)
164 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
165 tag_msg(sp
, TAG_EMPTY
, NULL
);
168 if ((tp
= tqp
->current
->q
.cqe_next
) == (void *)&tqp
->tagq
) {
169 msgq(sp
, M_ERR
, "282|Already at the last tag of this group");
172 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
176 if (F_ISSET(tqp
, TAG_CSCOPE
))
177 (void)cscope_search(sp
, tqp
, tp
);
179 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
180 if (tqp
->current
->msg
) {
181 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
183 msgq(sp
, M_INFO
, np
);
190 * Switch context to the next TAG.
192 * PUBLIC: int ex_tag_prev __P((SCR *, EXCMD *));
195 ex_tag_prev(sp
, cmdp
)
206 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
207 tag_msg(sp
, TAG_EMPTY
, NULL
);
210 if ((tp
= tqp
->current
->q
.cqe_prev
) == (void *)&tqp
->tagq
) {
211 msgq(sp
, M_ERR
, "255|Already at the first tag of this group");
214 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
218 if (F_ISSET(tqp
, TAG_CSCOPE
))
219 (void)cscope_search(sp
, tqp
, tp
);
221 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
222 if (tqp
->current
->msg
) {
223 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
225 msgq(sp
, M_INFO
, np
);
232 * Switch context to the specified TAG.
234 * PUBLIC: int ex_tag_nswitch __P((SCR *, TAG *, int));
237 ex_tag_nswitch(sp
, tp
, force
)
242 /* Get a file structure. */
243 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
246 /* If not changing files, return, we're done. */
247 if (tp
->frp
== sp
->frp
)
250 /* Check for permission to leave. */
251 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
254 /* Initialize the new file. */
255 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
258 /* Display tags in the center of the screen. */
259 F_CLR(sp
, SC_SCR_TOP
);
260 F_SET(sp
, SC_SCR_CENTER
);
263 F_SET(sp
, SC_FSWITCH
);
269 * Switch context to the specified TAG in a new screen.
271 * PUBLIC: int ex_tag_Nswitch __P((SCR *, TAG *, int));
274 ex_tag_Nswitch(sp
, tp
, force
)
281 /* Get a file structure. */
282 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
285 /* Get a new screen. */
286 if (screen_init(sp
->gp
, sp
, &new))
288 if (vs_split(sp
, new, 0)) {
289 (void)file_end(new, new->ep
, 1);
290 (void)screen_end(new);
294 /* Get a backing file. */
295 if (tp
->frp
== sp
->frp
) {
296 /* Copy file state. */
301 new->frp
->flags
= sp
->frp
->flags
;
302 } else if (file_init(new, tp
->frp
, NULL
, force
)) {
303 (void)vs_discard(new, NULL
);
304 (void)screen_end(new);
308 /* Create the argument list. */
309 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, tp
->frp
->name
);
311 /* Display tags in the center of the screen. */
312 F_CLR(new, SC_SCR_TOP
);
313 F_SET(new, SC_SCR_CENTER
);
317 F_SET(sp
, SC_SSWITCH
);
324 * :tagp[op][!] [number | file]
326 * Pop to a previous TAGQ context.
328 * PUBLIC: int ex_tag_pop __P((SCR *, EXCMD *));
342 /* Check for an empty stack. */
344 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
345 tag_msg(sp
, TAG_EMPTY
, NULL
);
349 /* Find the last TAG structure that we're going to DISCARD! */
350 switch (cmdp
->argc
) {
351 case 0: /* Pop one tag. */
352 dtqp
= exp
->tq
.cqh_first
;
354 case 1: /* Name or number. */
355 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+1,
357 off
= strtol(arg
, &p
, 10);
361 /* Number: pop that many queue entries. */
364 for (tqp
= exp
->tq
.cqh_first
;
365 tqp
!= (void *)&exp
->tq
&& --off
> 1;
366 tqp
= tqp
->q
.cqe_next
);
367 if (tqp
== (void *)&exp
->tq
) {
369 "159|Less than %s entries on the tags stack; use :display t[ags]",
376 /* File argument: pop to that queue entry. */
377 filearg
: arglen
= strlen(arg
);
378 for (tqp
= exp
->tq
.cqh_first
;
379 tqp
!= (void *)&exp
->tq
;
380 dtqp
= tqp
, tqp
= tqp
->q
.cqe_next
) {
381 /* Don't pop to the current file. */
382 if (tqp
== exp
->tq
.cqh_first
)
384 p
= tqp
->current
->frp
->name
;
385 if ((t
= strrchr(p
, '/')) == NULL
)
389 if (!strncmp(arg
, t
, arglen
))
392 if (tqp
== (void *)&exp
->tq
) {
393 msgq_str(sp
, M_ERR
, arg
,
394 "160|No file %s on the tags stack to return to; use :display t[ags]");
397 if (tqp
== exp
->tq
.cqh_first
)
404 return (tag_pop(sp
, dtqp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
408 * ex_tag_top -- :tagt[op][!]
409 * Clear the tag stack.
411 * PUBLIC: int ex_tag_top __P((SCR *, EXCMD *));
422 /* Check for an empty stack. */
423 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
424 tag_msg(sp
, TAG_EMPTY
, NULL
);
428 /* Return to the oldest information. */
430 exp
->tq
.cqh_last
->q
.cqe_prev
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
435 * Pop up to and including the specified TAGQ context.
438 tag_pop(sp
, dtqp
, force
)
450 * Update the cursor from the saved TAG information of the TAG
451 * structure we're moving to.
453 tp
= dtqp
->q
.cqe_next
->current
;
454 if (tp
->frp
== sp
->frp
) {
458 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
461 tp
->frp
->lno
= tp
->lno
;
462 tp
->frp
->cno
= tp
->cno
;
463 F_SET(sp
->frp
, FR_CURSORSET
);
464 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
467 F_SET(sp
, SC_FSWITCH
);
470 /* Pop entries off the queue up to and including dtqp. */
472 tqp
= exp
->tq
.cqh_first
;
473 if (tagq_free(sp
, tqp
))
475 } while (tqp
!= dtqp
);
478 * If only a single tag left, we've returned to the first tag point,
479 * and the stack is now empty.
481 if (exp
->tq
.cqh_first
->q
.cqe_next
== (void *)&exp
->tq
)
482 tagq_free(sp
, exp
->tq
.cqh_first
);
489 * Display the list of tags.
491 * PUBLIC: int ex_tag_display __P((SCR *));
505 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
506 tag_msg(sp
, TAG_EMPTY
, NULL
);
511 * We give the file name 20 columns and the search string the rest.
512 * If there's not enough room, we don't do anything special, it's
513 * not worth the effort, it just makes the display more confusing.
515 * We also assume that characters in file names map 1-1 to printing
516 * characters. This might not be true, but I don't think it's worth
517 * fixing. (The obvious fix is to pass the filenames through the
518 * msg_print function.)
520 #define L_NAME 30 /* Name. */
521 #define L_SLOP 4 /* Leading number plus trailing *. */
522 #define L_SPACE 5 /* Spaces after name, before tag. */
523 #define L_TAG 20 /* Tag. */
524 if (sp
->cols
<= L_NAME
+ L_SLOP
) {
525 msgq(sp
, M_ERR
, "292|Display too small.");
530 * Display the list of tags for each queue entry. The first entry
531 * is numbered, and the current tag entry has an asterisk appended.
533 for (cnt
= 1, tqp
= exp
->tq
.cqh_first
; !INTERRUPTED(sp
) &&
534 tqp
!= (void *)&exp
->tq
; ++cnt
, tqp
= tqp
->q
.cqe_next
)
535 for (tp
= tqp
->tagq
.cqh_first
;
536 tp
!= (void *)&tqp
->tagq
; tp
= tp
->q
.cqe_next
) {
537 if (tp
== tqp
->tagq
.cqh_first
)
538 (void)ex_printf(sp
, "%2d ", cnt
);
540 (void)ex_printf(sp
, " ");
541 p
= tp
->frp
== NULL
? tp
->fname
: tp
->frp
->name
;
542 if ((len
= strlen(p
)) > L_NAME
) {
543 len
= len
- (L_NAME
- 4);
544 (void)ex_printf(sp
, " ... %*.*s",
545 L_NAME
- 4, L_NAME
- 4, p
+ len
);
548 " %*.*s", L_NAME
, L_NAME
, p
);
549 if (tqp
->current
== tp
)
550 (void)ex_printf(sp
, "*");
552 if (tp
== tqp
->tagq
.cqh_first
&& tqp
->tag
!= NULL
&&
553 (sp
->cols
- L_NAME
) >= L_TAG
+ L_SPACE
) {
554 len
= strlen(tqp
->tag
);
555 if (len
> sp
->cols
- (L_NAME
+ L_SPACE
))
556 len
= sp
->cols
- (L_NAME
+ L_SPACE
);
557 (void)ex_printf(sp
, "%s%.*s",
558 tqp
->current
== tp
? " " : " ",
561 (void)ex_printf(sp
, "\n");
568 * Copy a screen's tag structures.
570 * PUBLIC: int ex_tag_copy __P((SCR *, SCR *));
573 ex_tag_copy(orig
, sp
)
576 EX_PRIVATE
*oexp
, *nexp
;
584 /* Copy tag queue and tags stack. */
585 for (aqp
= oexp
->tq
.cqh_first
;
586 aqp
!= (void *)&oexp
->tq
; aqp
= aqp
->q
.cqe_next
) {
587 if (tagq_copy(sp
, aqp
, &tqp
))
589 for (ap
= aqp
->tagq
.cqh_first
;
590 ap
!= (void *)&aqp
->tagq
; ap
= ap
->q
.cqe_next
) {
591 if (tag_copy(sp
, ap
, &tp
))
593 /* Set the current pointer. */
594 if (aqp
->current
== ap
)
596 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
598 CIRCLEQ_INSERT_TAIL(&nexp
->tq
, tqp
, q
);
601 /* Copy list of tag files. */
602 for (atfp
= oexp
->tagfq
.tqh_first
;
603 atfp
!= NULL
; atfp
= atfp
->q
.tqe_next
) {
604 if (tagf_copy(sp
, atfp
, &tfp
))
606 TAILQ_INSERT_TAIL(&nexp
->tagfq
, tfp
, q
);
609 /* Copy the last tag. */
610 if (oexp
->tag_last
!= NULL
&&
611 (nexp
->tag_last
= v_wstrdup(sp
, oexp
->tag_last
,
612 v_strlen(oexp
->tag_last
))) == NULL
) {
613 msgq(sp
, M_SYSERR
, NULL
);
621 * Copy a TAGF structure and return it in new memory.
624 tagf_copy(sp
, otfp
, tfpp
)
630 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
633 /* XXX: Allocate as part of the TAGF structure!!! */
634 if ((tfp
->name
= strdup(otfp
->name
)) == NULL
)
643 * Copy a TAGQ structure and return it in new memory.
646 tagq_copy(sp
, otqp
, tqpp
)
654 if (otqp
->tag
!= NULL
)
655 len
+= otqp
->tlen
+ 1;
656 MALLOC_RET(sp
, tqp
, TAGQ
*, len
);
657 memcpy(tqp
, otqp
, len
);
659 CIRCLEQ_INIT(&tqp
->tagq
);
661 if (otqp
->tag
!= NULL
)
670 * Copy a TAG structure and return it in new memory.
673 tag_copy(sp
, otp
, tpp
)
681 if (otp
->fname
!= NULL
)
682 len
+= otp
->fnlen
+ 1;
683 if (otp
->search
!= NULL
)
684 len
+= otp
->slen
+ 1;
685 if (otp
->msg
!= NULL
)
686 len
+= otp
->mlen
+ 1;
687 MALLOC_RET(sp
, tp
, TAG
*, len
);
688 memcpy(tp
, otp
, len
);
690 if (otp
->fname
!= NULL
)
691 tp
->fname
= (char *)tp
->buf
;
692 if (otp
->search
!= NULL
)
693 tp
->search
= tp
->buf
+ (otp
->search
- otp
->buf
);
694 if (otp
->msg
!= NULL
)
695 tp
->msg
= tp
->buf
+ (otp
->msg
- otp
->buf
);
703 * Free a TAGF structure.
713 TAILQ_REMOVE(&exp
->tagfq
, tfp
, q
);
721 * Free a TAGQ structure (and associated TAG structures).
723 * PUBLIC: int tagq_free __P((SCR *, TAGQ *));
734 while ((tp
= tqp
->tagq
.cqh_first
) != (void *)&tqp
->tagq
) {
735 CIRCLEQ_REMOVE(&tqp
->tagq
, tp
, q
);
740 * If allocated and then the user failed to switch files, the TAGQ
741 * structure was never attached to any list.
743 if (tqp
->q
.cqe_next
!= NULL
)
744 CIRCLEQ_REMOVE(&exp
->tq
, tqp
, q
);
750 * PUBLIC: int tagq_push __P((SCR*, TAGQ*, int, int ));
753 tagq_push(sp
, tqp
, new_screen
, force
)
756 int new_screen
, force
;
771 * Allocate all necessary memory before swapping screens. Initialize
772 * flags so we know what to free.
776 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
777 /* Initialize the `local context' tag queue structure. */
778 CALLOC_GOTO(sp
, rtqp
, TAGQ
*, 1, sizeof(TAGQ
));
779 CIRCLEQ_INIT(&rtqp
->tagq
);
781 /* Initialize and link in its tag structure. */
782 CALLOC_GOTO(sp
, rtp
, TAG
*, 1, sizeof(TAG
));
783 CIRCLEQ_INSERT_HEAD(&rtqp
->tagq
, rtp
, q
);
788 * Stick the current context information in a convenient place, we're
789 * about to lose it. Note, if we're called on editor startup, there
790 * will be no FREF structure.
795 istmp
= frp
== NULL
||
796 F_ISSET(frp
, FR_TMPFILE
) && !new_screen
;
798 /* Try to switch to the tag. */
800 if (ex_tag_Nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
803 /* Everything else gets done in the new screen. */
807 if (ex_tag_nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
811 * If this is the first tag, put a `current location' queue entry
812 * in place, so we can pop all the way back to the current mark.
813 * Note, it doesn't point to much of anything, it's a placeholder.
815 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
816 CIRCLEQ_INSERT_HEAD(&exp
->tq
, rtqp
, q
);
818 rtqp
= exp
->tq
.cqh_first
;
820 /* Link the new TAGQ structure into place. */
821 CIRCLEQ_INSERT_HEAD(&exp
->tq
, tqp
, q
);
823 (void)ctag_search(sp
,
824 tqp
->current
->search
, tqp
->current
->slen
, tqp
->tag
);
825 if (tqp
->current
->msg
) {
826 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
828 msgq(sp
, M_INFO
, np
);
832 * Move the current context from the temporary save area into the
835 * If we were in a temporary file, we don't have a context to which
836 * we can return, so just make it be the same as what we're moving
837 * to. It will be a little odd that ^T doesn't change anything, but
838 * I don't think it's a big deal.
841 rtqp
->current
->frp
= sp
->frp
;
842 rtqp
->current
->lno
= sp
->lno
;
843 rtqp
->current
->cno
= sp
->cno
;
845 rtqp
->current
->frp
= frp
;
846 rtqp
->current
->lno
= lno
;
847 rtqp
->current
->cno
= cno
;
863 * A few common messages.
865 * PUBLIC: void tag_msg __P((SCR *, tagmsg_t, char *));
868 tag_msg(sp
, msg
, tag
)
875 msgq_str(sp
, M_ERR
, tag
,
876 "164|%s: the tag's line number is past the end of the file");
879 msgq(sp
, M_INFO
, "165|The tags stack is empty");
882 msgq_str(sp
, M_ERR
, tag
, "166|%s: search pattern not found");
891 * Create a new list of ctag files.
893 * PUBLIC: int ex_tagf_alloc __P((SCR *, char *));
896 ex_tagf_alloc(sp
, str
)
905 /* Free current queue. */
907 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
910 /* Create new queue. */
911 for (p
= t
= str
;; ++p
) {
912 if (*p
== '\0' || isblank(*p
)) {
913 if ((len
= p
- t
) > 1) {
914 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
915 MALLOC(sp
, tfp
->name
, char *, len
+ 1);
916 if (tfp
->name
== NULL
) {
920 memcpy(tfp
->name
, t
, len
);
921 tfp
->name
[len
] = '\0';
923 TAILQ_INSERT_TAIL(&exp
->tagfq
, tfp
, q
);
932 /* Free previous queue. */
935 * Free the ex tag information.
937 * PUBLIC: int ex_tag_free __P((SCR *));
947 /* Free up tag information. */
949 while ((tqp
= exp
->tq
.cqh_first
) != (void *)&exp
->tq
)
951 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
953 if (exp
->tag_last
!= NULL
)
960 * Search a file for a tag.
963 ctag_search(sp
, search
, slen
, tag
)
976 * The historic tags file format (from a long, long time ago...)
977 * used a line number, not a search string. I got complaints, so
978 * people are still using the format. POSIX 1003.2 permits it.
980 if (ISDIGIT(search
[0])) {
981 INT2CHAR(sp
, search
, slen
+1, np
, nlen
);
983 if (!db_exist(sp
, m
.lno
)) {
984 tag_msg(sp
, TAG_BADLNO
, tag
);
989 * Search for the tag; cheap fallback for C functions
990 * if the name is the same but the arguments have changed.
994 if (f_search(sp
, &m
, &m
,
996 SEARCH_FIRST
| SEARCH_TAG
| SEARCH_PARSE
)) {
997 INT2CHAR(sp
, search
, slen
, np
, nlen
);
998 if ((p
= strrchr(np
, '(')) != NULL
) {
1000 if (f_search(sp
, &m
, &m
, search
, slen
,
1001 NULL
, SEARCH_FIRST
| SEARCH_TAG
))
1004 notfound
: tag_msg(sp
, TAG_SEARCH
, tag
);
1010 * Historically, tags set the search direction if it wasn't
1013 if (sp
->searchdir
== NOTSET
)
1014 sp
->searchdir
= FORWARD
;
1019 * Tags move to the first non-blank, NOT the search pattern start.
1023 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1029 * Search the list of tags files for a tag, and return tag queue.
1046 /* Allocate and initialize the tag queue structure. */
1047 INT2CHAR(sp
, tag
, v_strlen(tag
) + 1, np
, nlen
);
1049 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
1050 CIRCLEQ_INIT(&tqp
->tagq
);
1051 tqp
->tag
= tqp
->buf
;
1052 memcpy(tqp
->tag
, np
, (tqp
->tlen
= len
) + 1);
1055 * Find the tag, only display missing file messages once, and
1056 * then only if we didn't find the tag.
1059 tfp
= exp
->tagfq
.tqh_first
; tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1060 if (ctag_sfile(sp
, tfp
, tqp
, tqp
->tag
)) {
1062 F_SET(tfp
, TAGF_ERR
);
1064 F_CLR(tfp
, TAGF_ERR
| TAGF_ERR_WARN
);
1066 /* Check to see if we found anything. */
1067 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
1068 msgq_str(sp
, M_ERR
, tqp
->tag
, "162|%s: tag not found");
1070 for (tfp
= exp
->tagfq
.tqh_first
;
1071 tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1072 if (F_ISSET(tfp
, TAGF_ERR
) &&
1073 !F_ISSET(tfp
, TAGF_ERR_WARN
)) {
1074 errno
= tfp
->errnum
;
1075 msgq_str(sp
, M_SYSERR
, tfp
->name
, "%s");
1076 F_SET(tfp
, TAGF_ERR_WARN
);
1082 tqp
->current
= tqp
->tagq
.cqh_first
;
1091 * Search a tags file for a tag, adding any found to the tag queue.
1094 ctag_sfile(sp
, tfp
, tqp
, tname
)
1102 size_t dlen
, nlen
, slen
;
1103 int fd
, i
, nf1
, nf2
;
1104 char *back
, *cname
, *dname
, *front
, *map
, *name
, *p
, *search
, *t
;
1108 if ((fd
= open(tfp
->name
, O_RDONLY
, 0)) < 0) {
1109 tfp
->errnum
= errno
;
1115 * Some old BSD systems require MAP_FILE as an argument when mapping
1123 * We'd like to test if the file is too big to mmap. Since we don't
1124 * know what size or type off_t's or size_t's are, what the largest
1125 * unsigned integral type is, or what random insanity the local C
1126 * compiler will perpetrate, doing the comparison in a portable way
1127 * is flatly impossible. Hope mmap fails if the file is too large.
1129 if (fstat(fd
, &sb
) != 0 ||
1130 (map
= mmap(NULL
, (size_t)sb
.st_size
, PROT_READ
| PROT_WRITE
,
1131 MAP_FILE
| MAP_PRIVATE
, fd
, (off_t
)0)) == (caddr_t
)-1) {
1132 tfp
->errnum
= errno
;
1138 back
= front
+ sb
.st_size
;
1139 front
= binary_search(tname
, front
, back
);
1140 front
= linear_search(tname
, front
, back
);
1145 * Initialize and link in the tag structure(s). The historic ctags
1146 * file format only permitted a single tag location per tag. The
1147 * obvious extension to permit multiple tags locations per tag is to
1148 * output multiple records in the standard format. Unfortunately,
1149 * this won't work correctly with historic ex/vi implementations,
1150 * because their binary search assumes that there's only one record
1151 * per tag, and so will use a random tag entry if there si more than
1152 * one. This code handles either format.
1154 * The tags file is in the following format:
1156 * <tag> <filename> <line number> | <pattern>
1158 * Figure out how long everything is so we can allocate in one swell
1159 * foop, but discard anything that looks wrong.
1162 /* Nul-terminate the end of the line. */
1163 for (p
= front
; p
< back
&& *p
!= '\n'; ++p
);
1164 if (p
== back
|| *p
!= '\n')
1168 /* Update the pointers for the next time. */
1173 /* Break the line into tokens. */
1174 for (i
= 0; i
< 2 && (t
= strsep(&p
, "\t ")) != NULL
; ++i
)
1179 case 1: /* Filename. */
1181 nlen
= strlen(name
);
1185 /* Check for corruption. */
1186 if (i
!= 2 || p
== NULL
|| t
== NULL
)
1189 /* The rest of the string is the search pattern. */
1191 if ((slen
= strlen(p
)) == 0) {
1192 corrupt
: p
= msg_print(sp
, tname
, &nf1
);
1193 t
= msg_print(sp
, tfp
->name
, &nf2
);
1194 msgq(sp
, M_ERR
, "163|%s: corrupted tag in %s", p
, t
);
1196 FREE_SPACE(sp
, p
, 0);
1198 FREE_SPACE(sp
, t
, 0);
1202 /* Check for passing the last entry. */
1203 if (strcmp(tname
, cname
))
1206 /* Resolve the file name. */
1207 ctag_file(sp
, tfp
, name
, &dname
, &dlen
);
1210 TAG
*, 1, sizeof(TAG
) + dlen
+ 2 + nlen
+ 1 +
1211 (slen
+ 1) * sizeof(CHAR_T
));
1212 tp
->fname
= (char *)tp
->buf
;
1214 memcpy(tp
->fname
, dname
, dlen
);
1215 tp
->fname
[dlen
] = '/';
1218 memcpy(tp
->fname
+ dlen
, name
, nlen
+ 1);
1219 tp
->fnlen
= dlen
+ nlen
;
1220 tp
->search
= (CHAR_T
*)(tp
->fname
+ tp
->fnlen
+ 1);
1221 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
1222 MEMCPYW(tp
->search
, wp
, (tp
->slen
= slen
) + 1);
1223 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
1227 done
: if (munmap(map
, (size_t)sb
.st_size
))
1228 msgq(sp
, M_SYSERR
, "munmap");
1230 msgq(sp
, M_SYSERR
, "close");
1236 * Search for the right path to this file.
1239 ctag_file(sp
, tfp
, name
, dirp
, dlenp
)
1247 char *p
, buf
[MAXPATHLEN
];
1251 * If the tag file path is a relative path, see if it exists. If it
1252 * doesn't, look relative to the tags file path. It's okay for a tag
1253 * file to not exist, and historically, vi simply displayed a "new"
1254 * file. However, if the path exists relative to the tag file, it's
1255 * pretty clear what's happening, so we may as well get it right.
1258 if (name
[0] != '/' &&
1259 stat(name
, &sb
) && (p
= strrchr(tfp
->name
, '/')) != NULL
) {
1261 len
= snprintf(buf
, sizeof(buf
), "%s/%s", tfp
->name
, name
);
1262 if (stat(buf
, &sb
) == 0) {
1264 *dlenp
= strlen(*dirp
);
1271 * Binary search for "string" in memory between "front" and "back".
1273 * This routine is expected to return a pointer to the start of a line at
1274 * *or before* the first word matching "string". Relaxing the constraint
1275 * this way simplifies the algorithm.
1278 * front points to the beginning of a line at or before the first
1281 * back points to the beginning of a line at or after the first
1284 * Base of the Invariants.
1288 * Advancing the Invariants:
1290 * p = first newline after halfway point from front to back.
1292 * If the string at "p" is not greater than the string to match,
1293 * p is the new front. Otherwise it is the new back.
1297 * The definition of the routine allows it return at any point,
1298 * since front is always at or before the line to print.
1300 * In fact, it returns when the chosen "p" equals "back". This
1301 * implies that there exists a string is least half as long as
1302 * (back - front), which in turn implies that a linear search will
1303 * be no more expensive than the cost of simply printing a string or two.
1305 * Trying to continue with binary search at this point would be
1306 * more trouble than it's worth.
1312 #define SKIP_PAST_NEWLINE(p, back) while (p < back && *p++ != '\n');
1315 binary_search(string
, front
, back
)
1316 register char *string
, *front
, *back
;
1320 p
= front
+ (back
- front
) / 2;
1321 SKIP_PAST_NEWLINE(p
, back
);
1324 if (compare(string
, p
, back
) == GREATER
)
1328 p
= front
+ (back
- front
) / 2;
1329 SKIP_PAST_NEWLINE(p
, back
);
1335 * Find the first line that starts with string, linearly searching from front
1338 * Return NULL for no such line.
1340 * This routine assumes:
1342 * o front points at the first character in a line.
1343 * o front is before or at the first line to be printed.
1346 linear_search(string
, front
, back
)
1347 char *string
, *front
, *back
;
1349 while (front
< back
) {
1350 switch (compare(string
, front
, back
)) {
1351 case EQUAL
: /* Found it. */
1353 case LESS
: /* No such string. */
1355 case GREATER
: /* Keep going. */
1358 SKIP_PAST_NEWLINE(front
, back
);
1364 * Return LESS, GREATER, or EQUAL depending on how the string1 compares
1365 * with string2 (s1 ??? s2).
1367 * o Matches up to len(s1) are EQUAL.
1368 * o Matches up to len(s2) are GREATER.
1370 * The string "s1" is null terminated. The string s2 is '\t', space, (or
1371 * "back") terminated.
1374 * Reasonably modern ctags programs use tabs as separators, not spaces.
1375 * However, historic programs did use spaces, and, I got complaints.
1378 compare(s1
, s2
, back
)
1379 register char *s1
, *s2
, *back
;
1381 for (; *s1
&& s2
< back
&& (*s2
!= '\t' && *s2
!= ' '); ++s1
, ++s2
)
1383 return (*s1
< *s2
? LESS
: GREATER
);
1384 return (*s1
? GREATER
: s2
< back
&&
1385 (*s2
!= '\t' && *s2
!= ' ') ? LESS
: EQUAL
);