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.49 2001/08/28 13:29:16 skimo Exp $ (Berkeley) $Date: 2001/08/28 13:29:16 $";
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(SCR
*sp
, CHAR_T
*tagarg
)
69 /* Build an argument for the ex :tag command. */
70 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, OOBLNO
, 0);
71 argv_exp0(sp
, &cmd
, tagarg
, STRLEN(tagarg
));
75 * Historic vi went ahead and created a temporary file when it failed
76 * to find the tag. We match historic practice, but don't distinguish
77 * between real error and failure to find the tag.
79 if (ex_tag_push(sp
, &cmd
))
82 /* Display tags in the center of the screen. */
83 F_CLR(sp
, SC_SCR_TOP
);
84 F_SET(sp
, SC_SCR_CENTER
);
93 * Enter a new TAGQ context based on a ctag string.
95 * PUBLIC: int ex_tag_push __P((SCR *, EXCMD *));
98 ex_tag_push(SCR
*sp
, EXCMD
*cmdp
)
105 switch (cmdp
->argc
) {
107 if (exp
->tag_last
!= NULL
)
110 if ((exp
->tag_last
= v_wstrdup(sp
, cmdp
->argv
[0]->bp
,
111 cmdp
->argv
[0]->len
)) == NULL
) {
112 msgq(sp
, M_SYSERR
, NULL
);
116 /* Taglength may limit the number of characters. */
118 O_VAL(sp
, O_TAGLENGTH
)) != 0 && STRLEN(exp
->tag_last
) > tl
)
119 exp
->tag_last
[tl
] = '\0';
122 if (exp
->tag_last
== NULL
) {
123 msgq(sp
, M_ERR
, "158|No previous tag entered");
131 /* Get the tag information. */
132 if ((tqp
= ctag_slist(sp
, exp
->tag_last
)) == NULL
)
135 if (tagq_push(sp
, tqp
, F_ISSET(cmdp
, E_NEWSCREEN
),
136 FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
144 * Switch context to the next TAG.
146 * PUBLIC: int ex_tag_next __P((SCR *, EXCMD *));
149 ex_tag_next(SCR
*sp
, EXCMD
*cmdp
)
158 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
159 tag_msg(sp
, TAG_EMPTY
, NULL
);
162 if ((tp
= tqp
->current
->q
.cqe_next
) == (void *)&tqp
->tagq
) {
163 msgq(sp
, M_ERR
, "282|Already at the last tag of this group");
166 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
170 if (F_ISSET(tqp
, TAG_CSCOPE
))
171 (void)cscope_search(sp
, tqp
, tp
);
173 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
174 if (tqp
->current
->msg
) {
175 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
177 msgq(sp
, M_INFO
, np
);
184 * Switch context to the next TAG.
186 * PUBLIC: int ex_tag_prev __P((SCR *, EXCMD *));
189 ex_tag_prev(SCR
*sp
, EXCMD
*cmdp
)
198 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
199 tag_msg(sp
, TAG_EMPTY
, NULL
);
202 if ((tp
= tqp
->current
->q
.cqe_prev
) == (void *)&tqp
->tagq
) {
203 msgq(sp
, M_ERR
, "255|Already at the first tag of this group");
206 if (ex_tag_nswitch(sp
, tp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
210 if (F_ISSET(tqp
, TAG_CSCOPE
))
211 (void)cscope_search(sp
, tqp
, tp
);
213 (void)ctag_search(sp
, tp
->search
, tp
->slen
, tqp
->tag
);
214 if (tqp
->current
->msg
) {
215 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
217 msgq(sp
, M_INFO
, np
);
224 * Switch context to the specified TAG.
226 * PUBLIC: int ex_tag_nswitch __P((SCR *, TAG *, int));
229 ex_tag_nswitch(SCR
*sp
, TAG
*tp
, int force
)
231 /* Get a file structure. */
232 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
235 /* If not changing files, return, we're done. */
236 if (tp
->frp
== sp
->frp
)
239 /* Check for permission to leave. */
240 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
243 /* Initialize the new file. */
244 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
247 /* Display tags in the center of the screen. */
248 F_CLR(sp
, SC_SCR_TOP
);
249 F_SET(sp
, SC_SCR_CENTER
);
252 F_SET(sp
, SC_FSWITCH
);
258 * Switch context to the specified TAG in a new screen.
260 * PUBLIC: int ex_tag_Nswitch __P((SCR *, TAG *, int));
263 ex_tag_Nswitch(SCR
*sp
, TAG
*tp
, int force
)
267 /* Get a file structure. */
268 if (tp
->frp
== NULL
&& (tp
->frp
= file_add(sp
, tp
->fname
)) == NULL
)
271 /* Get a new screen. */
272 if (screen_init(sp
->gp
, sp
, &new))
274 if (vs_split(sp
, new, 0)) {
275 (void)file_end(new, new->ep
, 1);
276 (void)screen_end(new);
280 /* Get a backing file. */
281 if (tp
->frp
== sp
->frp
) {
282 /* Copy file state. */
285 CIRCLEQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
288 new->frp
->flags
= sp
->frp
->flags
;
289 } else if (file_init(new, tp
->frp
, NULL
, force
)) {
290 (void)vs_discard(new, NULL
);
291 (void)screen_end(new);
295 /* Create the argument list. */
296 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, tp
->frp
->name
);
298 /* Display tags in the center of the screen. */
299 F_CLR(new, SC_SCR_TOP
);
300 F_SET(new, SC_SCR_CENTER
);
304 F_SET(sp
, SC_SSWITCH
);
311 * :tagp[op][!] [number | file]
313 * Pop to a previous TAGQ context.
315 * PUBLIC: int ex_tag_pop __P((SCR *, EXCMD *));
318 ex_tag_pop(SCR
*sp
, EXCMD
*cmdp
)
327 /* Check for an empty stack. */
329 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
330 tag_msg(sp
, TAG_EMPTY
, NULL
);
334 /* Find the last TAG structure that we're going to DISCARD! */
335 switch (cmdp
->argc
) {
336 case 0: /* Pop one tag. */
337 dtqp
= exp
->tq
.cqh_first
;
339 case 1: /* Name or number. */
340 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+1,
342 off
= strtol(arg
, &p
, 10);
346 /* Number: pop that many queue entries. */
349 for (tqp
= exp
->tq
.cqh_first
;
350 tqp
!= (void *)&exp
->tq
&& --off
> 1;
351 tqp
= tqp
->q
.cqe_next
);
352 if (tqp
== (void *)&exp
->tq
) {
354 "159|Less than %s entries on the tags stack; use :display t[ags]",
361 /* File argument: pop to that queue entry. */
362 filearg
: arglen
= strlen(arg
);
363 for (tqp
= exp
->tq
.cqh_first
;
364 tqp
!= (void *)&exp
->tq
;
365 dtqp
= tqp
, tqp
= tqp
->q
.cqe_next
) {
366 /* Don't pop to the current file. */
367 if (tqp
== exp
->tq
.cqh_first
)
369 p
= tqp
->current
->frp
->name
;
370 if ((t
= strrchr(p
, '/')) == NULL
)
374 if (!strncmp(arg
, t
, arglen
))
377 if (tqp
== (void *)&exp
->tq
) {
378 msgq_str(sp
, M_ERR
, arg
,
379 "160|No file %s on the tags stack to return to; use :display t[ags]");
382 if (tqp
== exp
->tq
.cqh_first
)
389 return (tag_pop(sp
, dtqp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
393 * ex_tag_top -- :tagt[op][!]
394 * Clear the tag stack.
396 * PUBLIC: int ex_tag_top __P((SCR *, EXCMD *));
399 ex_tag_top(SCR
*sp
, EXCMD
*cmdp
)
405 /* Check for an empty stack. */
406 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
407 tag_msg(sp
, TAG_EMPTY
, NULL
);
411 /* Return to the oldest information. */
413 exp
->tq
.cqh_last
->q
.cqe_prev
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)));
418 * Pop up to and including the specified TAGQ context.
421 tag_pop(SCR
*sp
, TAGQ
*dtqp
, int force
)
430 * Update the cursor from the saved TAG information of the TAG
431 * structure we're moving to.
433 tp
= dtqp
->q
.cqe_next
->current
;
434 if (tp
->frp
== sp
->frp
) {
438 if (file_m1(sp
, force
, FS_ALL
| FS_POSSIBLE
))
441 tp
->frp
->lno
= tp
->lno
;
442 tp
->frp
->cno
= tp
->cno
;
443 F_SET(sp
->frp
, FR_CURSORSET
);
444 if (file_init(sp
, tp
->frp
, NULL
, FS_SETALT
))
447 F_SET(sp
, SC_FSWITCH
);
450 /* Pop entries off the queue up to and including dtqp. */
452 tqp
= exp
->tq
.cqh_first
;
453 if (tagq_free(sp
, tqp
))
455 } while (tqp
!= dtqp
);
458 * If only a single tag left, we've returned to the first tag point,
459 * and the stack is now empty.
461 if (exp
->tq
.cqh_first
->q
.cqe_next
== (void *)&exp
->tq
)
462 tagq_free(sp
, exp
->tq
.cqh_first
);
469 * Display the list of tags.
471 * PUBLIC: int ex_tag_display __P((SCR *));
474 ex_tag_display(SCR
*sp
)
484 if ((tqp
= exp
->tq
.cqh_first
) == (void *)&exp
->tq
) {
485 tag_msg(sp
, TAG_EMPTY
, NULL
);
490 * We give the file name 20 columns and the search string the rest.
491 * If there's not enough room, we don't do anything special, it's
492 * not worth the effort, it just makes the display more confusing.
494 * We also assume that characters in file names map 1-1 to printing
495 * characters. This might not be true, but I don't think it's worth
496 * fixing. (The obvious fix is to pass the filenames through the
497 * msg_print function.)
499 #define L_NAME 30 /* Name. */
500 #define L_SLOP 4 /* Leading number plus trailing *. */
501 #define L_SPACE 5 /* Spaces after name, before tag. */
502 #define L_TAG 20 /* Tag. */
503 if (sp
->cols
<= L_NAME
+ L_SLOP
) {
504 msgq(sp
, M_ERR
, "292|Display too small.");
509 * Display the list of tags for each queue entry. The first entry
510 * is numbered, and the current tag entry has an asterisk appended.
512 for (cnt
= 1, tqp
= exp
->tq
.cqh_first
; !INTERRUPTED(sp
) &&
513 tqp
!= (void *)&exp
->tq
; ++cnt
, tqp
= tqp
->q
.cqe_next
)
514 for (tp
= tqp
->tagq
.cqh_first
;
515 tp
!= (void *)&tqp
->tagq
; tp
= tp
->q
.cqe_next
) {
516 if (tp
== tqp
->tagq
.cqh_first
)
517 (void)ex_printf(sp
, "%2d ", cnt
);
519 (void)ex_printf(sp
, " ");
520 p
= tp
->frp
== NULL
? tp
->fname
: tp
->frp
->name
;
521 if ((len
= strlen(p
)) > L_NAME
) {
522 len
= len
- (L_NAME
- 4);
523 (void)ex_printf(sp
, " ... %*.*s",
524 L_NAME
- 4, L_NAME
- 4, p
+ len
);
527 " %*.*s", L_NAME
, L_NAME
, p
);
528 if (tqp
->current
== tp
)
529 (void)ex_printf(sp
, "*");
531 if (tp
== tqp
->tagq
.cqh_first
&& tqp
->tag
!= NULL
&&
532 (sp
->cols
- L_NAME
) >= L_TAG
+ L_SPACE
) {
533 len
= strlen(tqp
->tag
);
534 if (len
> sp
->cols
- (L_NAME
+ L_SPACE
))
535 len
= sp
->cols
- (L_NAME
+ L_SPACE
);
536 (void)ex_printf(sp
, "%s%.*s",
537 tqp
->current
== tp
? " " : " ",
540 (void)ex_printf(sp
, "\n");
547 * Copy a screen's tag structures.
549 * PUBLIC: int ex_tag_copy __P((SCR *, SCR *));
552 ex_tag_copy(SCR
*orig
, SCR
*sp
)
554 EX_PRIVATE
*oexp
, *nexp
;
562 /* Copy tag queue and tags stack. */
563 for (aqp
= oexp
->tq
.cqh_first
;
564 aqp
!= (void *)&oexp
->tq
; aqp
= aqp
->q
.cqe_next
) {
565 if (tagq_copy(sp
, aqp
, &tqp
))
567 for (ap
= aqp
->tagq
.cqh_first
;
568 ap
!= (void *)&aqp
->tagq
; ap
= ap
->q
.cqe_next
) {
569 if (tag_copy(sp
, ap
, &tp
))
571 /* Set the current pointer. */
572 if (aqp
->current
== ap
)
574 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
576 CIRCLEQ_INSERT_TAIL(&nexp
->tq
, tqp
, q
);
579 /* Copy list of tag files. */
580 for (atfp
= oexp
->tagfq
.tqh_first
;
581 atfp
!= NULL
; atfp
= atfp
->q
.tqe_next
) {
582 if (tagf_copy(sp
, atfp
, &tfp
))
584 TAILQ_INSERT_TAIL(&nexp
->tagfq
, tfp
, q
);
587 /* Copy the last tag. */
588 if (oexp
->tag_last
!= NULL
&&
589 (nexp
->tag_last
= v_wstrdup(sp
, oexp
->tag_last
,
590 STRLEN(oexp
->tag_last
))) == NULL
) {
591 msgq(sp
, M_SYSERR
, NULL
);
599 * Copy a TAGF structure and return it in new memory.
602 tagf_copy(SCR
*sp
, TAGF
*otfp
, TAGF
**tfpp
)
606 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
609 /* XXX: Allocate as part of the TAGF structure!!! */
610 if ((tfp
->name
= strdup(otfp
->name
)) == NULL
)
619 * Copy a TAGQ structure and return it in new memory.
622 tagq_copy(SCR
*sp
, TAGQ
*otqp
, TAGQ
**tqpp
)
628 if (otqp
->tag
!= NULL
)
629 len
+= otqp
->tlen
+ 1;
630 MALLOC_RET(sp
, tqp
, TAGQ
*, len
);
631 memcpy(tqp
, otqp
, len
);
633 CIRCLEQ_INIT(&tqp
->tagq
);
635 if (otqp
->tag
!= NULL
)
644 * Copy a TAG structure and return it in new memory.
647 tag_copy(SCR
*sp
, TAG
*otp
, TAG
**tpp
)
653 if (otp
->fname
!= NULL
)
654 len
+= otp
->fnlen
+ 1;
655 if (otp
->search
!= NULL
)
656 len
+= otp
->slen
+ 1;
657 if (otp
->msg
!= NULL
)
658 len
+= otp
->mlen
+ 1;
659 MALLOC_RET(sp
, tp
, TAG
*, len
);
660 memcpy(tp
, otp
, len
);
662 if (otp
->fname
!= NULL
)
663 tp
->fname
= (char *)tp
->buf
;
664 if (otp
->search
!= NULL
)
665 tp
->search
= tp
->buf
+ (otp
->search
- otp
->buf
);
666 if (otp
->msg
!= NULL
)
667 tp
->msg
= tp
->buf
+ (otp
->msg
- otp
->buf
);
675 * Free a TAGF structure.
678 tagf_free(SCR
*sp
, TAGF
*tfp
)
683 TAILQ_REMOVE(&exp
->tagfq
, tfp
, q
);
691 * Free a TAGQ structure (and associated TAG structures).
693 * PUBLIC: int tagq_free __P((SCR *, TAGQ *));
696 tagq_free(SCR
*sp
, TAGQ
*tqp
)
702 while ((tp
= tqp
->tagq
.cqh_first
) != (void *)&tqp
->tagq
) {
703 CIRCLEQ_REMOVE(&tqp
->tagq
, tp
, q
);
708 * If allocated and then the user failed to switch files, the TAGQ
709 * structure was never attached to any list.
711 if (tqp
->q
.cqe_next
!= NULL
)
712 CIRCLEQ_REMOVE(&exp
->tq
, tqp
, q
);
718 * PUBLIC: int tagq_push __P((SCR*, TAGQ*, int, int ));
721 tagq_push(SCR
*sp
, TAGQ
*tqp
, int new_screen
, int force
)
736 * Allocate all necessary memory before swapping screens. Initialize
737 * flags so we know what to free.
741 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
742 /* Initialize the `local context' tag queue structure. */
743 CALLOC_GOTO(sp
, rtqp
, TAGQ
*, 1, sizeof(TAGQ
));
744 CIRCLEQ_INIT(&rtqp
->tagq
);
746 /* Initialize and link in its tag structure. */
747 CALLOC_GOTO(sp
, rtp
, TAG
*, 1, sizeof(TAG
));
748 CIRCLEQ_INSERT_HEAD(&rtqp
->tagq
, rtp
, q
);
753 * Stick the current context information in a convenient place, we're
754 * about to lose it. Note, if we're called on editor startup, there
755 * will be no FREF structure.
760 istmp
= frp
== NULL
||
761 F_ISSET(frp
, FR_TMPFILE
) && !new_screen
;
763 /* Try to switch to the tag. */
765 if (ex_tag_Nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
768 /* Everything else gets done in the new screen. */
772 if (ex_tag_nswitch(sp
, tqp
->tagq
.cqh_first
, force
))
776 * If this is the first tag, put a `current location' queue entry
777 * in place, so we can pop all the way back to the current mark.
778 * Note, it doesn't point to much of anything, it's a placeholder.
780 if (exp
->tq
.cqh_first
== (void *)&exp
->tq
) {
781 CIRCLEQ_INSERT_HEAD(&exp
->tq
, rtqp
, q
);
783 rtqp
= exp
->tq
.cqh_first
;
785 /* Link the new TAGQ structure into place. */
786 CIRCLEQ_INSERT_HEAD(&exp
->tq
, tqp
, q
);
788 (void)ctag_search(sp
,
789 tqp
->current
->search
, tqp
->current
->slen
, tqp
->tag
);
790 if (tqp
->current
->msg
) {
791 INT2CHAR(sp
, tqp
->current
->msg
, tqp
->current
->mlen
+ 1,
793 msgq(sp
, M_INFO
, np
);
797 * Move the current context from the temporary save area into the
800 * If we were in a temporary file, we don't have a context to which
801 * we can return, so just make it be the same as what we're moving
802 * to. It will be a little odd that ^T doesn't change anything, but
803 * I don't think it's a big deal.
806 rtqp
->current
->frp
= sp
->frp
;
807 rtqp
->current
->lno
= sp
->lno
;
808 rtqp
->current
->cno
= sp
->cno
;
810 rtqp
->current
->frp
= frp
;
811 rtqp
->current
->lno
= lno
;
812 rtqp
->current
->cno
= cno
;
828 * A few common messages.
830 * PUBLIC: void tag_msg __P((SCR *, tagmsg_t, char *));
833 tag_msg(SCR
*sp
, tagmsg_t msg
, char *tag
)
837 msgq_str(sp
, M_ERR
, tag
,
838 "164|%s: the tag's line number is past the end of the file");
841 msgq(sp
, M_INFO
, "165|The tags stack is empty");
844 msgq_str(sp
, M_ERR
, tag
, "166|%s: search pattern not found");
853 * Create a new list of ctag files.
855 * PUBLIC: int ex_tagf_alloc __P((SCR *, char *));
858 ex_tagf_alloc(SCR
*sp
, char *str
)
865 /* Free current queue. */
867 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
870 /* Create new queue. */
871 for (p
= t
= str
;; ++p
) {
872 if (*p
== '\0' || isblank(*p
)) {
873 if ((len
= p
- t
) > 1) {
874 MALLOC_RET(sp
, tfp
, TAGF
*, sizeof(TAGF
));
875 MALLOC(sp
, tfp
->name
, char *, len
+ 1);
876 if (tfp
->name
== NULL
) {
880 memcpy(tfp
->name
, t
, len
);
881 tfp
->name
[len
] = '\0';
883 TAILQ_INSERT_TAIL(&exp
->tagfq
, tfp
, q
);
892 /* Free previous queue. */
895 * Free the ex tag information.
897 * PUBLIC: int ex_tag_free __P((SCR *));
906 /* Free up tag information. */
908 while ((tqp
= exp
->tq
.cqh_first
) != (void *)&exp
->tq
)
910 while ((tfp
= exp
->tagfq
.tqh_first
) != NULL
)
912 if (exp
->tag_last
!= NULL
)
919 * Search a file for a tag.
922 ctag_search(SCR
*sp
, CHAR_T
*search
, size_t slen
, char *tag
)
931 * The historic tags file format (from a long, long time ago...)
932 * used a line number, not a search string. I got complaints, so
933 * people are still using the format. POSIX 1003.2 permits it.
935 if (ISDIGIT(search
[0])) {
936 INT2CHAR(sp
, search
, slen
+1, np
, nlen
);
938 if (!db_exist(sp
, m
.lno
)) {
939 tag_msg(sp
, TAG_BADLNO
, tag
);
944 * Search for the tag; cheap fallback for C functions
945 * if the name is the same but the arguments have changed.
949 if (f_search(sp
, &m
, &m
,
951 SEARCH_FIRST
| SEARCH_TAG
| SEARCH_PARSE
)) {
952 INT2CHAR(sp
, search
, slen
, np
, nlen
);
953 if ((p
= strrchr(np
, '(')) != NULL
) {
955 if (f_search(sp
, &m
, &m
, search
, slen
,
956 NULL
, SEARCH_FIRST
| SEARCH_TAG
))
959 notfound
: tag_msg(sp
, TAG_SEARCH
, tag
);
965 * Historically, tags set the search direction if it wasn't
968 if (sp
->searchdir
== NOTSET
)
969 sp
->searchdir
= FORWARD
;
974 * Tags move to the first non-blank, NOT the search pattern start.
978 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
984 * Search the list of tags files for a tag, and return tag queue.
987 ctag_slist(SCR
*sp
, CHAR_T
*tag
)
999 /* Allocate and initialize the tag queue structure. */
1000 INT2CHAR(sp
, tag
, STRLEN(tag
) + 1, np
, nlen
);
1002 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
1003 CIRCLEQ_INIT(&tqp
->tagq
);
1004 tqp
->tag
= tqp
->buf
;
1005 memcpy(tqp
->tag
, np
, (tqp
->tlen
= len
) + 1);
1008 * Find the tag, only display missing file messages once, and
1009 * then only if we didn't find the tag.
1012 tfp
= exp
->tagfq
.tqh_first
; tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1013 if (ctag_sfile(sp
, tfp
, tqp
, tqp
->tag
)) {
1015 F_SET(tfp
, TAGF_ERR
);
1017 F_CLR(tfp
, TAGF_ERR
| TAGF_ERR_WARN
);
1019 /* Check to see if we found anything. */
1020 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
1021 msgq_str(sp
, M_ERR
, tqp
->tag
, "162|%s: tag not found");
1023 for (tfp
= exp
->tagfq
.tqh_first
;
1024 tfp
!= NULL
; tfp
= tfp
->q
.tqe_next
)
1025 if (F_ISSET(tfp
, TAGF_ERR
) &&
1026 !F_ISSET(tfp
, TAGF_ERR_WARN
)) {
1027 errno
= tfp
->errnum
;
1028 msgq_str(sp
, M_SYSERR
, tfp
->name
, "%s");
1029 F_SET(tfp
, TAGF_ERR_WARN
);
1035 tqp
->current
= tqp
->tagq
.cqh_first
;
1044 * Search a tags file for a tag, adding any found to the tag queue.
1047 ctag_sfile(SCR
*sp
, TAGF
*tfp
, TAGQ
*tqp
, char *tname
)
1051 size_t dlen
, nlen
, slen
;
1052 int fd
, i
, nf1
, nf2
;
1053 char *back
, *cname
, *dname
, *front
, *map
, *name
, *p
, *search
, *t
;
1057 if ((fd
= open(tfp
->name
, O_RDONLY
, 0)) < 0) {
1058 tfp
->errnum
= errno
;
1064 * Some old BSD systems require MAP_FILE as an argument when mapping
1072 * We'd like to test if the file is too big to mmap. Since we don't
1073 * know what size or type off_t's or size_t's are, what the largest
1074 * unsigned integral type is, or what random insanity the local C
1075 * compiler will perpetrate, doing the comparison in a portable way
1076 * is flatly impossible. Hope mmap fails if the file is too large.
1078 if (fstat(fd
, &sb
) != 0 ||
1079 (map
= mmap(NULL
, (size_t)sb
.st_size
, PROT_READ
| PROT_WRITE
,
1080 MAP_FILE
| MAP_PRIVATE
, fd
, (off_t
)0)) == (caddr_t
)-1) {
1081 tfp
->errnum
= errno
;
1087 back
= front
+ sb
.st_size
;
1088 front
= binary_search(tname
, front
, back
);
1089 front
= linear_search(tname
, front
, back
);
1094 * Initialize and link in the tag structure(s). The historic ctags
1095 * file format only permitted a single tag location per tag. The
1096 * obvious extension to permit multiple tags locations per tag is to
1097 * output multiple records in the standard format. Unfortunately,
1098 * this won't work correctly with historic ex/vi implementations,
1099 * because their binary search assumes that there's only one record
1100 * per tag, and so will use a random tag entry if there si more than
1101 * one. This code handles either format.
1103 * The tags file is in the following format:
1105 * <tag> <filename> <line number> | <pattern>
1107 * Figure out how long everything is so we can allocate in one swell
1108 * foop, but discard anything that looks wrong.
1111 /* Nul-terminate the end of the line. */
1112 for (p
= front
; p
< back
&& *p
!= '\n'; ++p
);
1113 if (p
== back
|| *p
!= '\n')
1117 /* Update the pointers for the next time. */
1122 /* Break the line into tokens. */
1123 for (i
= 0; i
< 2 && (t
= strsep(&p
, "\t ")) != NULL
; ++i
)
1128 case 1: /* Filename. */
1130 nlen
= strlen(name
);
1134 /* Check for corruption. */
1135 if (i
!= 2 || p
== NULL
|| t
== NULL
)
1138 /* The rest of the string is the search pattern. */
1140 if ((slen
= strlen(p
)) == 0) {
1141 corrupt
: p
= msg_print(sp
, tname
, &nf1
);
1142 t
= msg_print(sp
, tfp
->name
, &nf2
);
1143 msgq(sp
, M_ERR
, "163|%s: corrupted tag in %s", p
, t
);
1145 FREE_SPACE(sp
, p
, 0);
1147 FREE_SPACE(sp
, t
, 0);
1151 /* Check for passing the last entry. */
1152 if (strcmp(tname
, cname
))
1155 /* Resolve the file name. */
1156 ctag_file(sp
, tfp
, name
, &dname
, &dlen
);
1159 TAG
*, 1, sizeof(TAG
) + dlen
+ 2 + nlen
+ 1 +
1160 (slen
+ 1) * sizeof(CHAR_T
));
1161 tp
->fname
= (char *)tp
->buf
;
1163 memcpy(tp
->fname
, dname
, dlen
);
1164 tp
->fname
[dlen
] = '/';
1167 memcpy(tp
->fname
+ dlen
, name
, nlen
+ 1);
1168 tp
->fnlen
= dlen
+ nlen
;
1169 tp
->search
= (CHAR_T
*)(tp
->fname
+ tp
->fnlen
+ 1);
1170 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
1171 MEMCPYW(tp
->search
, wp
, (tp
->slen
= slen
) + 1);
1172 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
1176 done
: if (munmap(map
, (size_t)sb
.st_size
))
1177 msgq(sp
, M_SYSERR
, "munmap");
1179 msgq(sp
, M_SYSERR
, "close");
1185 * Search for the right path to this file.
1188 ctag_file(SCR
*sp
, TAGF
*tfp
, char *name
, char **dirp
, size_t *dlenp
)
1192 char *p
, buf
[MAXPATHLEN
];
1196 * If the tag file path is a relative path, see if it exists. If it
1197 * doesn't, look relative to the tags file path. It's okay for a tag
1198 * file to not exist, and historically, vi simply displayed a "new"
1199 * file. However, if the path exists relative to the tag file, it's
1200 * pretty clear what's happening, so we may as well get it right.
1203 if (name
[0] != '/' &&
1204 stat(name
, &sb
) && (p
= strrchr(tfp
->name
, '/')) != NULL
) {
1206 len
= snprintf(buf
, sizeof(buf
), "%s/%s", tfp
->name
, name
);
1207 if (stat(buf
, &sb
) == 0) {
1209 *dlenp
= strlen(*dirp
);
1216 * Binary search for "string" in memory between "front" and "back".
1218 * This routine is expected to return a pointer to the start of a line at
1219 * *or before* the first word matching "string". Relaxing the constraint
1220 * this way simplifies the algorithm.
1223 * front points to the beginning of a line at or before the first
1226 * back points to the beginning of a line at or after the first
1229 * Base of the Invariants.
1233 * Advancing the Invariants:
1235 * p = first newline after halfway point from front to back.
1237 * If the string at "p" is not greater than the string to match,
1238 * p is the new front. Otherwise it is the new back.
1242 * The definition of the routine allows it return at any point,
1243 * since front is always at or before the line to print.
1245 * In fact, it returns when the chosen "p" equals "back". This
1246 * implies that there exists a string is least half as long as
1247 * (back - front), which in turn implies that a linear search will
1248 * be no more expensive than the cost of simply printing a string or two.
1250 * Trying to continue with binary search at this point would be
1251 * more trouble than it's worth.
1257 #define SKIP_PAST_NEWLINE(p, back) while (p < back && *p++ != '\n');
1260 binary_search(register char *string
, register char *front
, register char *back
)
1264 p
= front
+ (back
- front
) / 2;
1265 SKIP_PAST_NEWLINE(p
, back
);
1268 if (compare(string
, p
, back
) == GREATER
)
1272 p
= front
+ (back
- front
) / 2;
1273 SKIP_PAST_NEWLINE(p
, back
);
1279 * Find the first line that starts with string, linearly searching from front
1282 * Return NULL for no such line.
1284 * This routine assumes:
1286 * o front points at the first character in a line.
1287 * o front is before or at the first line to be printed.
1290 linear_search(char *string
, char *front
, char *back
)
1292 while (front
< back
) {
1293 switch (compare(string
, front
, back
)) {
1294 case EQUAL
: /* Found it. */
1296 case LESS
: /* No such string. */
1298 case GREATER
: /* Keep going. */
1301 SKIP_PAST_NEWLINE(front
, back
);
1307 * Return LESS, GREATER, or EQUAL depending on how the string1 compares
1308 * with string2 (s1 ??? s2).
1310 * o Matches up to len(s1) are EQUAL.
1311 * o Matches up to len(s2) are GREATER.
1313 * The string "s1" is null terminated. The string s2 is '\t', space, (or
1314 * "back") terminated.
1317 * Reasonably modern ctags programs use tabs as separators, not spaces.
1318 * However, historic programs did use spaces, and, I got complaints.
1321 compare(register char *s1
, register char *s2
, register char *back
)
1323 for (; *s1
&& s2
< back
&& (*s2
!= '\t' && *s2
!= ' '); ++s1
, ++s2
)
1325 return (*s1
< *s2
? LESS
: GREATER
);
1326 return (*s1
? GREATER
: s2
< back
&&
1327 (*s2
!= '\t' && *s2
!= ' ') ? LESS
: EQUAL
);