AM_INIT_AUTOMAKE seems to include AC_ARG_PROGRAM; remove explicit call
[nvi.git] / ex / ex_cscope.c
blob038957a89009abda7c8b24bb386af8326724cc88
1 /*-
2 * Copyright (c) 1994, 1996
3 * Rob Mayoff. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_cscope.c,v 10.17 2000/07/16 20:49:32 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:32 $";
14 #endif /* not lint */
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/wait.h>
23 #include <bitstring.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <unistd.h>
35 #include "../common/common.h"
36 #include "pathnames.h"
37 #include "tag.h"
39 #define CSCOPE_DBFILE "cscope.out"
40 #define CSCOPE_PATHS "cscope.tpath"
43 * 0name find all uses of name
44 * 1name find definition of name
45 * 2name find all function calls made from name
46 * 3name find callers of name
47 * 4string find text string (cscope 12.9)
48 * 4name find assignments to name (cscope 13.3)
49 * 5pattern change pattern -- NOT USED
50 * 6pattern find pattern
51 * 7name find files with name as substring
52 * 8name find files #including name
54 #define FINDHELP "\
55 find c|d|e|f|g|i|s|t buffer|pattern\n\
56 c: find callers of name\n\
57 d: find all function calls made from name\n\
58 e: find pattern\n\
59 f: find files with name as substring\n\
60 g: find definition of name\n\
61 i: find files #including name\n\
62 s: find all uses of name\n\
63 t: find assignments to name"
65 static int cscope_add __P((SCR *, EXCMD *, CHAR_T *));
66 static int cscope_find __P((SCR *, EXCMD*, CHAR_T *));
67 static int cscope_help __P((SCR *, EXCMD *, CHAR_T *));
68 static int cscope_kill __P((SCR *, EXCMD *, CHAR_T *));
69 static int cscope_reset __P((SCR *, EXCMD *, CHAR_T *));
71 typedef struct _cc {
72 char *name;
73 int (*function) __P((SCR *, EXCMD *, CHAR_T *));
74 char *help_msg;
75 char *usage_msg;
76 } CC;
78 static CC const cscope_cmds[] = {
79 { "add", cscope_add,
80 "Add a new cscope database", "add file | directory" },
81 { "find", cscope_find,
82 "Query the databases for a pattern", FINDHELP },
83 { "help", cscope_help,
84 "Show help for cscope commands", "help [command]" },
85 { "kill", cscope_kill,
86 "Kill a cscope connection", "kill number" },
87 { "reset", cscope_reset,
88 "Discard all current cscope connections", "reset" },
89 { NULL }
92 static TAGQ *create_cs_cmd __P((SCR *, char *, size_t *));
93 static int csc_help __P((SCR *, char *));
94 static void csc_file __P((SCR *,
95 CSC *, char *, char **, size_t *, int *));
96 static int get_paths __P((SCR *, CSC *));
97 static CC const *lookup_ccmd __P((char *));
98 static int parse __P((SCR *, CSC *, TAGQ *, int *));
99 static int read_prompt __P((SCR *, CSC *));
100 static int run_cscope __P((SCR *, CSC *, char *));
101 static int start_cscopes __P((SCR *, EXCMD *));
102 static int terminate __P((SCR *, CSC *, int));
105 * ex_cscope --
106 * Perform an ex cscope.
108 * PUBLIC: int ex_cscope __P((SCR *, EXCMD *));
111 ex_cscope(sp, cmdp)
112 SCR *sp;
113 EXCMD *cmdp;
115 CC const *ccp;
116 EX_PRIVATE *exp;
117 int i;
118 CHAR_T *cmd;
119 CHAR_T *p;
120 char *np;
121 size_t nlen;
123 /* Initialize the default cscope directories. */
124 exp = EXP(sp);
125 if (!F_ISSET(exp, EXP_CSCINIT) && start_cscopes(sp, cmdp))
126 return (1);
127 F_SET(exp, EXP_CSCINIT);
129 /* Skip leading whitespace. */
130 for (p = cmdp->argv[0]->bp, i = cmdp->argv[0]->len; i > 0; --i, ++p)
131 if (!isspace(*p))
132 break;
133 if (i == 0)
134 goto usage;
136 /* Skip the command to any arguments. */
137 for (cmd = p; i > 0; --i, ++p)
138 if (isspace(*p))
139 break;
140 if (*p != '\0') {
141 *p++ = '\0';
142 for (; *p && isspace(*p); ++p);
145 INT2CHAR(sp, cmd, v_strlen(cmd) + 1, np, nlen);
146 if ((ccp = lookup_ccmd(np)) == NULL) {
147 usage: msgq(sp, M_ERR, "309|Use \"cscope help\" for help");
148 return (1);
151 /* Call the underlying function. */
152 return (ccp->function(sp, cmdp, p));
156 * start_cscopes --
157 * Initialize the cscope package.
159 static int
160 start_cscopes(sp, cmdp)
161 SCR *sp;
162 EXCMD *cmdp;
164 size_t blen, len;
165 char *bp, *cscopes, *p, *t;
166 CHAR_T *wp;
167 size_t wlen;
170 * EXTENSION #1:
172 * If the CSCOPE_DIRS environment variable is set, we treat it as a
173 * list of cscope directories that we're using, similar to the tags
174 * edit option.
176 * XXX
177 * This should probably be an edit option, although that implies that
178 * we start/stop cscope processes periodically, instead of once when
179 * the editor starts.
181 if ((cscopes = getenv("CSCOPE_DIRS")) == NULL)
182 return (0);
183 len = strlen(cscopes);
184 GET_SPACE_RET(sp, bp, blen, len);
185 memcpy(bp, cscopes, len + 1);
187 for (cscopes = t = bp; (p = strsep(&t, "\t :")) != NULL;)
188 if (*p != '\0') {
189 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
190 (void)cscope_add(sp, cmdp, wp);
193 FREE_SPACE(sp, bp, blen);
194 return (0);
198 * cscope_add --
199 * The cscope add command.
201 static int
202 cscope_add(sp, cmdp, dname)
203 SCR *sp;
204 EXCMD *cmdp;
205 CHAR_T *dname;
207 struct stat sb;
208 EX_PRIVATE *exp;
209 CSC *csc;
210 size_t len;
211 int cur_argc;
212 char *dbname, path[MAXPATHLEN];
213 char *np;
214 size_t nlen;
216 exp = EXP(sp);
219 * 0 additional args: usage.
220 * 1 additional args: matched a file.
221 * >1 additional args: object, too many args.
223 cur_argc = cmdp->argc;
224 if (argv_exp2(sp, cmdp, dname, v_strlen(dname))) {
225 return (1);
227 if (cmdp->argc == cur_argc) {
228 (void)csc_help(sp, "add");
229 return (1);
231 if (cmdp->argc == cur_argc + 1)
232 dname = cmdp->argv[cur_argc]->bp;
233 else {
234 ex_emsg(sp, np, EXM_FILECOUNT);
235 return (1);
238 INT2CHAR(sp, dname, v_strlen(dname)+1, np, nlen);
241 * The user can specify a specific file (so they can have multiple
242 * Cscope databases in a single directory) or a directory. If the
243 * file doesn't exist, we're done. If it's a directory, append the
244 * standard database file name and try again. Store the directory
245 * name regardless so that we can use it as a base for searches.
247 if (stat(np, &sb)) {
248 msgq(sp, M_SYSERR, np);
249 return (1);
251 if (S_ISDIR(sb.st_mode)) {
252 (void)snprintf(path, sizeof(path),
253 "%s/%s", np, CSCOPE_DBFILE);
254 if (stat(path, &sb)) {
255 msgq(sp, M_SYSERR, path);
256 return (1);
258 dbname = CSCOPE_DBFILE;
259 } else if ((dbname = strrchr(np, '/')) != NULL)
260 *dbname++ = '\0';
262 /* Allocate a cscope connection structure and initialize its fields. */
263 len = strlen(np);
264 CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
265 csc->dname = csc->buf;
266 csc->dlen = len;
267 memcpy(csc->dname, np, len);
268 csc->mtime = sb.st_mtime;
270 /* Get the search paths for the cscope. */
271 if (get_paths(sp, csc))
272 goto err;
274 /* Start the cscope process. */
275 if (run_cscope(sp, csc, dbname))
276 goto err;
279 * Add the cscope connection to the screen's list. From now on,
280 * on error, we have to call terminate, which expects the csc to
281 * be on the chain.
283 LIST_INSERT_HEAD(&exp->cscq, csc, q);
285 /* Read the initial prompt from the cscope to make sure it's okay. */
286 if (read_prompt(sp, csc)) {
287 terminate(sp, csc, 0);
288 return (1);
291 return (0);
293 err: free(csc);
294 return (1);
298 * get_paths --
299 * Get the directories to search for the files associated with this
300 * cscope database.
302 static int
303 get_paths(sp, csc)
304 SCR *sp;
305 CSC *csc;
307 struct stat sb;
308 int fd, nentries;
309 size_t len;
310 char *p, **pathp, buf[MAXPATHLEN * 2];
313 * EXTENSION #2:
315 * If there's a cscope directory with a file named CSCOPE_PATHS, it
316 * contains a colon-separated list of paths in which to search for
317 * files returned by cscope.
319 * XXX
320 * These paths are absolute paths, and not relative to the cscope
321 * directory. To fix this, rewrite the each path using the cscope
322 * directory as a prefix.
324 (void)snprintf(buf, sizeof(buf), "%s/%s", csc->dname, CSCOPE_PATHS);
325 if (stat(buf, &sb) == 0) {
326 /* Read in the CSCOPE_PATHS file. */
327 len = sb.st_size;
328 MALLOC_RET(sp, csc->pbuf, char *, len + 1);
329 if ((fd = open(buf, O_RDONLY, 0)) < 0 ||
330 read(fd, csc->pbuf, len) != len) {
331 msgq_str(sp, M_SYSERR, buf, "%s");
332 if (fd >= 0)
333 (void)close(fd);
334 return (1);
336 (void)close(fd);
337 csc->pbuf[len] = '\0';
339 /* Count up the entries. */
340 for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p)
341 if (p[0] == ':' && p[1] != '\0')
342 ++nentries;
344 /* Build an array of pointers to the paths. */
345 CALLOC_GOTO(sp,
346 csc->paths, char **, nentries + 1, sizeof(char **));
347 for (pathp = csc->paths, p = strtok(csc->pbuf, ":");
348 p != NULL; p = strtok(NULL, ":"))
349 *pathp++ = p;
350 return (0);
354 * If the CSCOPE_PATHS file doesn't exist, we look for files
355 * relative to the cscope directory.
357 if ((csc->pbuf = strdup(csc->dname)) == NULL) {
358 msgq(sp, M_SYSERR, NULL);
359 return (1);
361 CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
362 csc->paths[0] = csc->pbuf;
363 return (0);
365 alloc_err:
366 if (csc->pbuf != NULL) {
367 free(csc->pbuf);
368 csc->pbuf = NULL;
370 return (1);
374 * run_cscope --
375 * Fork off the cscope process.
377 static int
378 run_cscope(sp, csc, dbname)
379 SCR *sp;
380 CSC *csc;
381 char *dbname;
383 int to_cs[2], from_cs[2];
384 char cmd[MAXPATHLEN * 2];
387 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
388 * from_cs[0] and writes to to_cs[1].
390 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[0] = -1;
391 if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
392 msgq(sp, M_SYSERR, "pipe");
393 goto err;
395 switch (csc->pid = vfork()) {
396 case -1:
397 msgq(sp, M_SYSERR, "vfork");
398 err: if (to_cs[0] != -1)
399 (void)close(to_cs[0]);
400 if (to_cs[1] != -1)
401 (void)close(to_cs[1]);
402 if (from_cs[0] != -1)
403 (void)close(from_cs[0]);
404 if (from_cs[1] != -1)
405 (void)close(from_cs[1]);
406 return (1);
407 case 0: /* child: run cscope. */
408 (void)dup2(to_cs[0], STDIN_FILENO);
409 (void)dup2(from_cs[1], STDOUT_FILENO);
410 (void)dup2(from_cs[1], STDERR_FILENO);
412 /* Close unused file descriptors. */
413 (void)close(to_cs[1]);
414 (void)close(from_cs[0]);
416 /* Run the cscope command. */
417 #define CSCOPE_CMD_FMT "cd '%s' && exec cscope -dl -f %s"
418 (void)snprintf(cmd, sizeof(cmd),
419 CSCOPE_CMD_FMT, csc->dname, dbname);
420 (void)execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
421 msgq_str(sp, M_SYSERR, cmd, "execl: %s");
422 _exit (127);
423 /* NOTREACHED */
424 default: /* parent. */
425 /* Close unused file descriptors. */
426 (void)close(to_cs[0]);
427 (void)close(from_cs[1]);
430 * Save the file descriptors for later duplication, and
431 * reopen as streams.
433 csc->to_fd = to_cs[1];
434 csc->to_fp = fdopen(to_cs[1], "w");
435 csc->from_fd = from_cs[0];
436 csc->from_fp = fdopen(from_cs[0], "r");
437 break;
439 return (0);
443 * cscope_find --
444 * The cscope find command.
446 static int
447 cscope_find(sp, cmdp, pattern)
448 SCR *sp;
449 EXCMD *cmdp;
450 CHAR_T *pattern;
452 CSC *csc, *csc_next;
453 EX_PRIVATE *exp;
454 FREF *frp;
455 TAGQ *rtqp, *tqp;
456 TAG *rtp;
457 db_recno_t lno;
458 size_t cno, search;
459 int force, istmp, matches;
460 char *np = NULL;
461 size_t nlen;
463 exp = EXP(sp);
465 /* Check for connections. */
466 if (exp->cscq.lh_first == NULL) {
467 msgq(sp, M_ERR, "310|No cscope connections running");
468 return (1);
472 * Allocate all necessary memory before doing anything hard. If the
473 * tags stack is empty, we'll need the `local context' TAGQ structure
474 * later.
476 rtp = NULL;
477 rtqp = NULL;
478 if (exp->tq.cqh_first == (void *)&exp->tq) {
479 /* Initialize the `local context' tag queue structure. */
480 CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
481 CIRCLEQ_INIT(&rtqp->tagq);
483 /* Initialize and link in its tag structure. */
484 CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
485 CIRCLEQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
486 rtqp->current = rtp;
489 /* Create the cscope command. */
490 INT2CHAR(sp, pattern, v_strlen(pattern) + 1, np, nlen);
491 np = strdup(np);
492 if ((tqp = create_cs_cmd(sp, np, &search)) == NULL)
493 goto err;
496 * Stick the current context in a convenient place, we'll lose it
497 * when we switch files.
499 frp = sp->frp;
500 lno = sp->lno;
501 cno = sp->cno;
502 istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN);
504 /* Search all open connections for a match. */
505 matches = 0;
506 for (csc = exp->cscq.lh_first; csc != NULL; csc = csc_next) {
507 /* Copy csc->q.lh_next here in case csc is killed. */
508 csc_next = csc->q.le_next;
511 * Send the command to the cscope program. (We skip the
512 * first two bytes of the command, because we stored the
513 * search cscope command character and a leading space
514 * there.)
516 (void)fprintf(csc->to_fp, "%d%s\n", search, tqp->tag + 2);
517 (void)fflush(csc->to_fp);
519 /* Read the output. */
520 if (parse(sp, csc, tqp, &matches)) {
521 if (rtqp != NULL)
522 free(rtqp);
523 tagq_free(sp, tqp);
524 return (1);
528 if (matches == 0) {
529 msgq(sp, M_INFO, "278|No matches for query");
530 return (0);
533 tqp->current = tqp->tagq.cqh_first;
535 /* Try to switch to the first tag. */
536 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
537 if (F_ISSET(cmdp, E_NEWSCREEN)) {
538 if (ex_tag_Nswitch(sp, tqp->current, force))
539 goto err;
541 /* Everything else gets done in the new screen. */
542 sp = sp->nextdisp;
543 exp = EXP(sp);
544 } else
545 if (ex_tag_nswitch(sp, tqp->current, force))
546 goto err;
549 * If this is the first tag, put a `current location' queue entry
550 * in place, so we can pop all the way back to the current mark.
551 * Note, it doesn't point to much of anything, it's a placeholder.
553 if (exp->tq.cqh_first == (void *)&exp->tq) {
554 CIRCLEQ_INSERT_HEAD(&exp->tq, rtqp, q);
555 } else
556 rtqp = exp->tq.cqh_first;
558 /* Link the current TAGQ structure into place. */
559 CIRCLEQ_INSERT_HEAD(&exp->tq, tqp, q);
561 (void)cscope_search(sp, tqp, tqp->current);
564 * Move the current context from the temporary save area into the
565 * right structure.
567 * If we were in a temporary file, we don't have a context to which
568 * we can return, so just make it be the same as what we're moving
569 * to. It will be a little odd that ^T doesn't change anything, but
570 * I don't think it's a big deal.
572 if (istmp) {
573 rtqp->current->frp = sp->frp;
574 rtqp->current->lno = sp->lno;
575 rtqp->current->cno = sp->cno;
576 } else {
577 rtqp->current->frp = frp;
578 rtqp->current->lno = lno;
579 rtqp->current->cno = cno;
582 return (0);
584 err:
585 alloc_err:
586 if (rtqp != NULL)
587 free(rtqp);
588 if (rtp != NULL)
589 free(rtp);
590 if (np != NULL)
591 free(np);
592 return (1);
596 * create_cs_cmd --
597 * Build a cscope command, creating and initializing the base TAGQ.
599 static TAGQ *
600 create_cs_cmd(sp, pattern, searchp)
601 SCR *sp;
602 char *pattern;
603 size_t *searchp;
605 CB *cbp;
606 TAGQ *tqp;
607 size_t tlen;
608 char *p;
611 * Cscope supports a "change pattern" command which we never use,
612 * cscope command 5. Set CSCOPE_QUERIES[5] to " " since the user
613 * can't pass " " as the first character of pattern. That way the
614 * user can't ask for pattern 5 so we don't need any special-case
615 * code.
617 #define CSCOPE_QUERIES "sgdct efi"
619 if (pattern == NULL)
620 goto usage;
622 /* Skip leading blanks, check for command character. */
623 for (; isblank(pattern[0]); ++pattern);
624 if (pattern[0] == '\0' || !isblank(pattern[1]))
625 goto usage;
626 for (*searchp = 0, p = CSCOPE_QUERIES;
627 *p != '\0' && *p != pattern[0]; ++*searchp, ++p);
628 if (*p == '\0') {
629 msgq(sp, M_ERR,
630 "311|%s: unknown search type: use one of %s",
631 KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES);
632 return (NULL);
635 /* Skip <blank> characters to the pattern. */
636 for (p = pattern + 1; *p != '\0' && isblank(*p); ++p);
637 if (*p == '\0') {
638 usage: (void)csc_help(sp, "find");
639 return (NULL);
642 /* The user can specify the contents of a buffer as the pattern. */
643 cbp = NULL;
644 if (p[0] == '"' && p[1] != '\0' && p[2] == '\0')
645 CBNAME(sp, cbp, p[1]);
646 if (cbp != NULL) {
647 INT2CHAR(sp, cbp->textq.cqh_first->lb,
648 cbp->textq.cqh_first->len, p, tlen);
649 } else
650 tlen = strlen(p);
652 /* Allocate and initialize the TAGQ structure. */
653 CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
654 if (tqp == NULL)
655 return (NULL);
656 CIRCLEQ_INIT(&tqp->tagq);
657 tqp->tag = tqp->buf;
658 tqp->tag[0] = pattern[0];
659 tqp->tag[1] = ' ';
660 tqp->tlen = tlen + 2;
661 memcpy(tqp->tag + 2, p, tlen);
662 tqp->tag[tlen + 2] = '\0';
663 F_SET(tqp, TAG_CSCOPE);
665 return (tqp);
669 * parse --
670 * Parse the cscope output.
672 static int
673 parse(sp, csc, tqp, matchesp)
674 SCR *sp;
675 CSC *csc;
676 TAGQ *tqp;
677 int *matchesp;
679 TAG *tp;
680 db_recno_t slno;
681 size_t dlen, nlen, slen;
682 int ch, i, isolder, nlines;
683 char *dname, *name, *search, *p, *t, dummy[2], buf[2048];
685 for (;;) {
686 if (!fgets(buf, sizeof(buf), csc->from_fp))
687 goto io_err;
690 * If the database is out of date, or there's some other
691 * problem, cscope will output error messages before the
692 * number-of-lines output. Display/discard any output
693 * that doesn't match what we want.
695 #define CSCOPE_NLINES_FMT "cscope: %d lines%1[\n]"
696 if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2)
697 break;
698 if ((p = strchr(buf, '\n')) != NULL)
699 *p = '\0';
700 msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf);
703 while (nlines--) {
704 if (fgets(buf, sizeof(buf), csc->from_fp) == NULL)
705 goto io_err;
707 /* If the line's too long for the buffer, discard it. */
708 if ((p = strchr(buf, '\n')) == NULL) {
709 while ((ch = getc(csc->from_fp)) != EOF && ch != '\n');
710 continue;
712 *p = '\0';
715 * The cscope output is in the following format:
717 * <filename> <context> <line number> <pattern>
719 * Figure out how long everything is so we can allocate in one
720 * swell foop, but discard anything that looks wrong.
722 for (p = buf, i = 0;
723 i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i)
724 switch (i) {
725 case 0: /* Filename. */
726 name = t;
727 nlen = strlen(name);
728 break;
729 case 1: /* Context. */
730 break;
731 case 2: /* Line number. */
732 slno = (db_recno_t)atol(t);
733 break;
735 if (i != 3 || p == NULL || t == NULL)
736 continue;
738 /* The rest of the string is the search pattern. */
739 search = p;
740 slen = strlen(p);
742 /* Resolve the file name. */
743 csc_file(sp, csc, name, &dname, &dlen, &isolder);
746 * If the file is older than the cscope database, that is,
747 * the database was built since the file was last modified,
748 * or there wasn't a search string, use the line number.
750 if (isolder || strcmp(search, "<unknown>") == 0) {
751 search = NULL;
752 slen = 0;
756 * Allocate and initialize a tag structure plus the variable
757 * length cscope information that follows it.
759 CALLOC_RET(sp, tp,
760 TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
761 tp->fname = (char *)tp->buf;
762 if (dlen != 0) {
763 memcpy(tp->fname, dname, dlen);
764 tp->fname[dlen] = '/';
765 ++dlen;
767 memcpy(tp->fname + dlen, name, nlen + 1);
768 tp->fnlen = dlen + nlen;
769 tp->slno = slno;
770 if (slen != 0) {
771 tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1);
772 MEMCPYW(tp->search, search, (tp->slen = slen) + 1);
774 CIRCLEQ_INSERT_TAIL(&tqp->tagq, tp, q);
776 ++*matchesp;
779 (void)read_prompt(sp, csc);
780 return (0);
782 io_err: if (feof(csc->from_fp))
783 errno = EIO;
784 msgq_str(sp, M_SYSERR, "%s", csc->dname);
785 terminate(sp, csc, 0);
786 return (1);
790 * csc_file --
791 * Search for the right path to this file.
793 static void
794 csc_file(sp, csc, name, dirp, dlenp, isolderp)
795 SCR *sp;
796 CSC *csc;
797 char *name, **dirp;
798 size_t *dlenp;
799 int *isolderp;
801 struct stat sb;
802 char **pp, buf[MAXPATHLEN];
805 * Check for the file in all of the listed paths. If we don't
806 * find it, we simply return it unchanged. We have to do this
807 * now, even though it's expensive, because if the user changes
808 * directories, we can't change our minds as to where the file
809 * lives.
811 for (pp = csc->paths; *pp != NULL; ++pp) {
812 (void)snprintf(buf, sizeof(buf), "%s/%s", *pp, name);
813 if (stat(buf, &sb) == 0) {
814 *dirp = *pp;
815 *dlenp = strlen(*pp);
816 *isolderp = sb.st_mtime < csc->mtime;
817 return;
820 *dlenp = 0;
824 * cscope_help --
825 * The cscope help command.
827 static int
828 cscope_help(sp, cmdp, subcmd)
829 SCR *sp;
830 EXCMD *cmdp;
831 CHAR_T *subcmd;
833 char *np;
834 size_t nlen;
836 INT2CHAR(sp, subcmd, v_strlen(subcmd) + 1, np, nlen);
837 return (csc_help(sp, np));
841 * csc_help --
842 * Display help/usage messages.
844 static int
845 csc_help(sp, cmd)
846 SCR *sp;
847 char *cmd;
849 CC const *ccp;
851 if (cmd != NULL && *cmd != '\0')
852 if ((ccp = lookup_ccmd(cmd)) == NULL) {
853 ex_printf(sp,
854 "%s doesn't match any cscope command\n", cmd);
855 return (1);
856 } else {
857 ex_printf(sp,
858 "Command: %s (%s)\n", ccp->name, ccp->help_msg);
859 ex_printf(sp, " Usage: %s\n", ccp->usage_msg);
860 return (0);
863 ex_printf(sp, "cscope commands:\n");
864 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
865 ex_printf(sp, " %*s: %s\n", 5, ccp->name, ccp->help_msg);
866 return (0);
870 * cscope_kill --
871 * The cscope kill command.
873 static int
874 cscope_kill(sp, cmdp, cn)
875 SCR *sp;
876 EXCMD *cmdp;
877 CHAR_T *cn;
879 char *np;
880 size_t nlen;
882 INT2CHAR(sp, cn, v_strlen(cn) + 1, np, nlen);
883 return (terminate(sp, NULL, atoi(np)));
887 * terminate --
888 * Detach from a cscope process.
890 static int
891 terminate(sp, csc, n)
892 SCR *sp;
893 CSC *csc;
894 int n;
896 EX_PRIVATE *exp;
897 int i, pstat;
899 exp = EXP(sp);
902 * We either get a csc structure or a number. If not provided a
903 * csc structure, find the right one.
905 if (csc == NULL) {
906 if (n < 1)
907 goto badno;
908 for (i = 1, csc = exp->cscq.lh_first;
909 csc != NULL; csc = csc->q.le_next, i++)
910 if (i == n)
911 break;
912 if (csc == NULL) {
913 badno: msgq(sp, M_ERR, "312|%d: no such cscope session", n);
914 return (1);
919 * XXX
920 * Theoretically, we have the only file descriptors to the process,
921 * so closing them should let it exit gracefully, deleting temporary
922 * files, etc. The original vi cscope integration sent the cscope
923 * connection a SIGTERM signal, so I'm not sure if closing the file
924 * descriptors is sufficient.
926 if (csc->from_fp != NULL)
927 (void)fclose(csc->from_fp);
928 if (csc->to_fp != NULL)
929 (void)fclose(csc->to_fp);
930 (void)waitpid(csc->pid, &pstat, 0);
932 /* Discard cscope connection information. */
933 LIST_REMOVE(csc, q);
934 if (csc->pbuf != NULL)
935 free(csc->pbuf);
936 if (csc->paths != NULL)
937 free(csc->paths);
938 free(csc);
939 return (0);
943 * cscope_reset --
944 * The cscope reset command.
946 static int
947 cscope_reset(sp, cmdp, notusedp)
948 SCR *sp;
949 EXCMD *cmdp;
950 CHAR_T *notusedp;
952 EX_PRIVATE *exp;
954 for (exp = EXP(sp); exp->cscq.lh_first != NULL;) {
955 static CHAR_T one[] = {'1', 0};
956 if (cscope_kill(sp, cmdp, one))
957 return (1);
959 return (0);
963 * cscope_display --
964 * Display current connections.
966 * PUBLIC: int cscope_display __P((SCR *));
969 cscope_display(sp)
970 SCR *sp;
972 EX_PRIVATE *exp;
973 CSC *csc;
974 int i;
976 exp = EXP(sp);
977 if (exp->cscq.lh_first == NULL) {
978 ex_printf(sp, "No cscope connections.\n");
979 return (0);
981 for (i = 1,
982 csc = exp->cscq.lh_first; csc != NULL; ++i, csc = csc->q.le_next)
983 ex_printf(sp,
984 "%2d %s (process %lu)\n", i, csc->dname, (u_long)csc->pid);
985 return (0);
989 * cscope_search --
990 * Search a file for a cscope entry.
992 * PUBLIC: int cscope_search __P((SCR *, TAGQ *, TAG *));
995 cscope_search(sp, tqp, tp)
996 SCR *sp;
997 TAGQ *tqp;
998 TAG *tp;
1000 MARK m;
1002 /* If we don't have a search pattern, use the line number. */
1003 if (tp->search == NULL) {
1004 if (!db_exist(sp, tp->slno)) {
1005 tag_msg(sp, TAG_BADLNO, tqp->tag);
1006 return (1);
1008 m.lno = tp->slno;
1009 } else {
1011 * Search for the tag; cheap fallback for C functions
1012 * if the name is the same but the arguments have changed.
1014 m.lno = 1;
1015 m.cno = 0;
1016 if (f_search(sp, &m, &m,
1017 tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FIRST)) {
1018 tag_msg(sp, TAG_SEARCH, tqp->tag);
1019 return (1);
1023 * !!!
1024 * Historically, tags set the search direction if it wasn't
1025 * already set.
1027 if (sp->searchdir == NOTSET)
1028 sp->searchdir = FORWARD;
1032 * !!!
1033 * Tags move to the first non-blank, NOT the search pattern start.
1035 sp->lno = m.lno;
1036 sp->cno = 0;
1037 (void)nonblank(sp, sp->lno, &sp->cno);
1038 return (0);
1043 * lookup_ccmd --
1044 * Return a pointer to the command structure.
1046 static CC const *
1047 lookup_ccmd(name)
1048 char *name;
1050 CC const *ccp;
1051 size_t len;
1053 len = strlen(name);
1054 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
1055 if (strncmp(name, ccp->name, len) == 0)
1056 return (ccp);
1057 return (NULL);
1061 * read_prompt --
1062 * Read a prompt from cscope.
1064 static int
1065 read_prompt(sp, csc)
1066 SCR *sp;
1067 CSC *csc;
1069 int ch;
1071 #define CSCOPE_PROMPT ">> "
1072 for (;;) {
1073 while ((ch =
1074 getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]);
1075 if (ch == EOF) {
1076 terminate(sp, csc, 0);
1077 return (1);
1079 if (getc(csc->from_fp) != CSCOPE_PROMPT[1])
1080 continue;
1081 if (getc(csc->from_fp) != CSCOPE_PROMPT[2])
1082 continue;
1083 break;
1085 return (0);