Update ./missing to version that includes GPL exemption.
[nvi.git] / ex / ex_cscope.c
blobcdcca6aa9b263bc54623431224fb3a488fe72321
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.19 2001/06/25 15:19:15 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:15 $";
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(SCR *sp, EXCMD *cmdp)
113 CC const *ccp;
114 EX_PRIVATE *exp;
115 int i;
116 CHAR_T *cmd;
117 CHAR_T *p;
118 char *np;
119 size_t nlen;
121 /* Initialize the default cscope directories. */
122 exp = EXP(sp);
123 if (!F_ISSET(exp, EXP_CSCINIT) && start_cscopes(sp, cmdp))
124 return (1);
125 F_SET(exp, EXP_CSCINIT);
127 /* Skip leading whitespace. */
128 for (p = cmdp->argv[0]->bp, i = cmdp->argv[0]->len; i > 0; --i, ++p)
129 if (!isspace(*p))
130 break;
131 if (i == 0)
132 goto usage;
134 /* Skip the command to any arguments. */
135 for (cmd = p; i > 0; --i, ++p)
136 if (isspace(*p))
137 break;
138 if (*p != '\0') {
139 *p++ = '\0';
140 for (; *p && isspace(*p); ++p);
143 INT2CHAR(sp, cmd, STRLEN(cmd) + 1, np, nlen);
144 if ((ccp = lookup_ccmd(np)) == NULL) {
145 usage: msgq(sp, M_ERR, "309|Use \"cscope help\" for help");
146 return (1);
149 /* Call the underlying function. */
150 return (ccp->function(sp, cmdp, p));
154 * start_cscopes --
155 * Initialize the cscope package.
157 static int
158 start_cscopes(SCR *sp, EXCMD *cmdp)
160 size_t blen, len;
161 char *bp, *cscopes, *p, *t;
162 CHAR_T *wp;
163 size_t wlen;
166 * EXTENSION #1:
168 * If the CSCOPE_DIRS environment variable is set, we treat it as a
169 * list of cscope directories that we're using, similar to the tags
170 * edit option.
172 * XXX
173 * This should probably be an edit option, although that implies that
174 * we start/stop cscope processes periodically, instead of once when
175 * the editor starts.
177 if ((cscopes = getenv("CSCOPE_DIRS")) == NULL)
178 return (0);
179 len = strlen(cscopes);
180 GET_SPACE_RET(sp, bp, blen, len);
181 memcpy(bp, cscopes, len + 1);
183 for (cscopes = t = bp; (p = strsep(&t, "\t :")) != NULL;)
184 if (*p != '\0') {
185 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
186 (void)cscope_add(sp, cmdp, wp);
189 FREE_SPACE(sp, bp, blen);
190 return (0);
194 * cscope_add --
195 * The cscope add command.
197 static int
198 cscope_add(SCR *sp, EXCMD *cmdp, CHAR_T *dname)
200 struct stat sb;
201 EX_PRIVATE *exp;
202 CSC *csc;
203 size_t len;
204 int cur_argc;
205 char *dbname, path[MAXPATHLEN];
206 char *np;
207 size_t nlen;
209 exp = EXP(sp);
212 * 0 additional args: usage.
213 * 1 additional args: matched a file.
214 * >1 additional args: object, too many args.
216 cur_argc = cmdp->argc;
217 if (argv_exp2(sp, cmdp, dname, STRLEN(dname))) {
218 return (1);
220 if (cmdp->argc == cur_argc) {
221 (void)csc_help(sp, "add");
222 return (1);
224 if (cmdp->argc == cur_argc + 1)
225 dname = cmdp->argv[cur_argc]->bp;
226 else {
227 ex_emsg(sp, np, EXM_FILECOUNT);
228 return (1);
231 INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);
234 * The user can specify a specific file (so they can have multiple
235 * Cscope databases in a single directory) or a directory. If the
236 * file doesn't exist, we're done. If it's a directory, append the
237 * standard database file name and try again. Store the directory
238 * name regardless so that we can use it as a base for searches.
240 if (stat(np, &sb)) {
241 msgq(sp, M_SYSERR, np);
242 return (1);
244 if (S_ISDIR(sb.st_mode)) {
245 (void)snprintf(path, sizeof(path),
246 "%s/%s", np, CSCOPE_DBFILE);
247 if (stat(path, &sb)) {
248 msgq(sp, M_SYSERR, path);
249 return (1);
251 dbname = CSCOPE_DBFILE;
252 } else if ((dbname = strrchr(np, '/')) != NULL)
253 *dbname++ = '\0';
255 /* Allocate a cscope connection structure and initialize its fields. */
256 len = strlen(np);
257 CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
258 csc->dname = csc->buf;
259 csc->dlen = len;
260 memcpy(csc->dname, np, len);
261 csc->mtime = sb.st_mtime;
263 /* Get the search paths for the cscope. */
264 if (get_paths(sp, csc))
265 goto err;
267 /* Start the cscope process. */
268 if (run_cscope(sp, csc, dbname))
269 goto err;
272 * Add the cscope connection to the screen's list. From now on,
273 * on error, we have to call terminate, which expects the csc to
274 * be on the chain.
276 LIST_INSERT_HEAD(&exp->cscq, csc, q);
278 /* Read the initial prompt from the cscope to make sure it's okay. */
279 if (read_prompt(sp, csc)) {
280 terminate(sp, csc, 0);
281 return (1);
284 return (0);
286 err: free(csc);
287 return (1);
291 * get_paths --
292 * Get the directories to search for the files associated with this
293 * cscope database.
295 static int
296 get_paths(SCR *sp, CSC *csc)
298 struct stat sb;
299 int fd, nentries;
300 size_t len;
301 char *p, **pathp, buf[MAXPATHLEN * 2];
304 * EXTENSION #2:
306 * If there's a cscope directory with a file named CSCOPE_PATHS, it
307 * contains a colon-separated list of paths in which to search for
308 * files returned by cscope.
310 * XXX
311 * These paths are absolute paths, and not relative to the cscope
312 * directory. To fix this, rewrite the each path using the cscope
313 * directory as a prefix.
315 (void)snprintf(buf, sizeof(buf), "%s/%s", csc->dname, CSCOPE_PATHS);
316 if (stat(buf, &sb) == 0) {
317 /* Read in the CSCOPE_PATHS file. */
318 len = sb.st_size;
319 MALLOC_RET(sp, csc->pbuf, char *, len + 1);
320 if ((fd = open(buf, O_RDONLY, 0)) < 0 ||
321 read(fd, csc->pbuf, len) != len) {
322 msgq_str(sp, M_SYSERR, buf, "%s");
323 if (fd >= 0)
324 (void)close(fd);
325 return (1);
327 (void)close(fd);
328 csc->pbuf[len] = '\0';
330 /* Count up the entries. */
331 for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p)
332 if (p[0] == ':' && p[1] != '\0')
333 ++nentries;
335 /* Build an array of pointers to the paths. */
336 CALLOC_GOTO(sp,
337 csc->paths, char **, nentries + 1, sizeof(char **));
338 for (pathp = csc->paths, p = strtok(csc->pbuf, ":");
339 p != NULL; p = strtok(NULL, ":"))
340 *pathp++ = p;
341 return (0);
345 * If the CSCOPE_PATHS file doesn't exist, we look for files
346 * relative to the cscope directory.
348 if ((csc->pbuf = strdup(csc->dname)) == NULL) {
349 msgq(sp, M_SYSERR, NULL);
350 return (1);
352 CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
353 csc->paths[0] = csc->pbuf;
354 return (0);
356 alloc_err:
357 if (csc->pbuf != NULL) {
358 free(csc->pbuf);
359 csc->pbuf = NULL;
361 return (1);
365 * run_cscope --
366 * Fork off the cscope process.
368 static int
369 run_cscope(SCR *sp, CSC *csc, char *dbname)
371 int to_cs[2], from_cs[2];
372 char cmd[MAXPATHLEN * 2];
375 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
376 * from_cs[0] and writes to to_cs[1].
378 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[0] = -1;
379 if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
380 msgq(sp, M_SYSERR, "pipe");
381 goto err;
383 switch (csc->pid = vfork()) {
384 case -1:
385 msgq(sp, M_SYSERR, "vfork");
386 err: if (to_cs[0] != -1)
387 (void)close(to_cs[0]);
388 if (to_cs[1] != -1)
389 (void)close(to_cs[1]);
390 if (from_cs[0] != -1)
391 (void)close(from_cs[0]);
392 if (from_cs[1] != -1)
393 (void)close(from_cs[1]);
394 return (1);
395 case 0: /* child: run cscope. */
396 (void)dup2(to_cs[0], STDIN_FILENO);
397 (void)dup2(from_cs[1], STDOUT_FILENO);
398 (void)dup2(from_cs[1], STDERR_FILENO);
400 /* Close unused file descriptors. */
401 (void)close(to_cs[1]);
402 (void)close(from_cs[0]);
404 /* Run the cscope command. */
405 #define CSCOPE_CMD_FMT "cd '%s' && exec cscope -dl -f %s"
406 (void)snprintf(cmd, sizeof(cmd),
407 CSCOPE_CMD_FMT, csc->dname, dbname);
408 (void)execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
409 msgq_str(sp, M_SYSERR, cmd, "execl: %s");
410 _exit (127);
411 /* NOTREACHED */
412 default: /* parent. */
413 /* Close unused file descriptors. */
414 (void)close(to_cs[0]);
415 (void)close(from_cs[1]);
418 * Save the file descriptors for later duplication, and
419 * reopen as streams.
421 csc->to_fd = to_cs[1];
422 csc->to_fp = fdopen(to_cs[1], "w");
423 csc->from_fd = from_cs[0];
424 csc->from_fp = fdopen(from_cs[0], "r");
425 break;
427 return (0);
431 * cscope_find --
432 * The cscope find command.
434 static int
435 cscope_find(SCR *sp, EXCMD *cmdp, CHAR_T *pattern)
437 CSC *csc, *csc_next;
438 EX_PRIVATE *exp;
439 FREF *frp;
440 TAGQ *rtqp, *tqp;
441 TAG *rtp;
442 db_recno_t lno;
443 size_t cno, search;
444 int force, istmp, matches;
445 char *np = NULL;
446 size_t nlen;
448 exp = EXP(sp);
450 /* Check for connections. */
451 if (exp->cscq.lh_first == NULL) {
452 msgq(sp, M_ERR, "310|No cscope connections running");
453 return (1);
457 * Allocate all necessary memory before doing anything hard. If the
458 * tags stack is empty, we'll need the `local context' TAGQ structure
459 * later.
461 rtp = NULL;
462 rtqp = NULL;
463 if (exp->tq.cqh_first == (void *)&exp->tq) {
464 /* Initialize the `local context' tag queue structure. */
465 CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
466 CIRCLEQ_INIT(&rtqp->tagq);
468 /* Initialize and link in its tag structure. */
469 CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
470 CIRCLEQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
471 rtqp->current = rtp;
474 /* Create the cscope command. */
475 INT2CHAR(sp, pattern, STRLEN(pattern) + 1, np, nlen);
476 np = strdup(np);
477 if ((tqp = create_cs_cmd(sp, np, &search)) == NULL)
478 goto err;
481 * Stick the current context in a convenient place, we'll lose it
482 * when we switch files.
484 frp = sp->frp;
485 lno = sp->lno;
486 cno = sp->cno;
487 istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN);
489 /* Search all open connections for a match. */
490 matches = 0;
491 for (csc = exp->cscq.lh_first; csc != NULL; csc = csc_next) {
492 /* Copy csc->q.lh_next here in case csc is killed. */
493 csc_next = csc->q.le_next;
496 * Send the command to the cscope program. (We skip the
497 * first two bytes of the command, because we stored the
498 * search cscope command character and a leading space
499 * there.)
501 (void)fprintf(csc->to_fp, "%d%s\n", search, tqp->tag + 2);
502 (void)fflush(csc->to_fp);
504 /* Read the output. */
505 if (parse(sp, csc, tqp, &matches)) {
506 if (rtqp != NULL)
507 free(rtqp);
508 tagq_free(sp, tqp);
509 return (1);
513 if (matches == 0) {
514 msgq(sp, M_INFO, "278|No matches for query");
515 return (0);
518 tqp->current = tqp->tagq.cqh_first;
520 /* Try to switch to the first tag. */
521 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
522 if (F_ISSET(cmdp, E_NEWSCREEN)) {
523 if (ex_tag_Nswitch(sp, tqp->current, force))
524 goto err;
526 /* Everything else gets done in the new screen. */
527 sp = sp->nextdisp;
528 exp = EXP(sp);
529 } else
530 if (ex_tag_nswitch(sp, tqp->current, force))
531 goto err;
534 * If this is the first tag, put a `current location' queue entry
535 * in place, so we can pop all the way back to the current mark.
536 * Note, it doesn't point to much of anything, it's a placeholder.
538 if (exp->tq.cqh_first == (void *)&exp->tq) {
539 CIRCLEQ_INSERT_HEAD(&exp->tq, rtqp, q);
540 } else
541 rtqp = exp->tq.cqh_first;
543 /* Link the current TAGQ structure into place. */
544 CIRCLEQ_INSERT_HEAD(&exp->tq, tqp, q);
546 (void)cscope_search(sp, tqp, tqp->current);
549 * Move the current context from the temporary save area into the
550 * right structure.
552 * If we were in a temporary file, we don't have a context to which
553 * we can return, so just make it be the same as what we're moving
554 * to. It will be a little odd that ^T doesn't change anything, but
555 * I don't think it's a big deal.
557 if (istmp) {
558 rtqp->current->frp = sp->frp;
559 rtqp->current->lno = sp->lno;
560 rtqp->current->cno = sp->cno;
561 } else {
562 rtqp->current->frp = frp;
563 rtqp->current->lno = lno;
564 rtqp->current->cno = cno;
567 return (0);
569 err:
570 alloc_err:
571 if (rtqp != NULL)
572 free(rtqp);
573 if (rtp != NULL)
574 free(rtp);
575 if (np != NULL)
576 free(np);
577 return (1);
581 * create_cs_cmd --
582 * Build a cscope command, creating and initializing the base TAGQ.
584 static TAGQ *
585 create_cs_cmd(SCR *sp, char *pattern, size_t *searchp)
587 CB *cbp;
588 TAGQ *tqp;
589 size_t tlen;
590 char *p;
593 * Cscope supports a "change pattern" command which we never use,
594 * cscope command 5. Set CSCOPE_QUERIES[5] to " " since the user
595 * can't pass " " as the first character of pattern. That way the
596 * user can't ask for pattern 5 so we don't need any special-case
597 * code.
599 #define CSCOPE_QUERIES "sgdct efi"
601 if (pattern == NULL)
602 goto usage;
604 /* Skip leading blanks, check for command character. */
605 for (; isblank(pattern[0]); ++pattern);
606 if (pattern[0] == '\0' || !isblank(pattern[1]))
607 goto usage;
608 for (*searchp = 0, p = CSCOPE_QUERIES;
609 *p != '\0' && *p != pattern[0]; ++*searchp, ++p);
610 if (*p == '\0') {
611 msgq(sp, M_ERR,
612 "311|%s: unknown search type: use one of %s",
613 KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES);
614 return (NULL);
617 /* Skip <blank> characters to the pattern. */
618 for (p = pattern + 1; *p != '\0' && isblank(*p); ++p);
619 if (*p == '\0') {
620 usage: (void)csc_help(sp, "find");
621 return (NULL);
624 /* The user can specify the contents of a buffer as the pattern. */
625 cbp = NULL;
626 if (p[0] == '"' && p[1] != '\0' && p[2] == '\0')
627 CBNAME(sp, cbp, p[1]);
628 if (cbp != NULL) {
629 INT2CHAR(sp, cbp->textq.cqh_first->lb,
630 cbp->textq.cqh_first->len, p, tlen);
631 } else
632 tlen = strlen(p);
634 /* Allocate and initialize the TAGQ structure. */
635 CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
636 if (tqp == NULL)
637 return (NULL);
638 CIRCLEQ_INIT(&tqp->tagq);
639 tqp->tag = tqp->buf;
640 tqp->tag[0] = pattern[0];
641 tqp->tag[1] = ' ';
642 tqp->tlen = tlen + 2;
643 memcpy(tqp->tag + 2, p, tlen);
644 tqp->tag[tlen + 2] = '\0';
645 F_SET(tqp, TAG_CSCOPE);
647 return (tqp);
651 * parse --
652 * Parse the cscope output.
654 static int
655 parse(SCR *sp, CSC *csc, TAGQ *tqp, int *matchesp)
657 TAG *tp;
658 db_recno_t slno;
659 size_t dlen, nlen, slen;
660 int ch, i, isolder, nlines;
661 char *dname, *name, *search, *p, *t, dummy[2], buf[2048];
663 for (;;) {
664 if (!fgets(buf, sizeof(buf), csc->from_fp))
665 goto io_err;
668 * If the database is out of date, or there's some other
669 * problem, cscope will output error messages before the
670 * number-of-lines output. Display/discard any output
671 * that doesn't match what we want.
673 #define CSCOPE_NLINES_FMT "cscope: %d lines%1[\n]"
674 if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2)
675 break;
676 if ((p = strchr(buf, '\n')) != NULL)
677 *p = '\0';
678 msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf);
681 while (nlines--) {
682 if (fgets(buf, sizeof(buf), csc->from_fp) == NULL)
683 goto io_err;
685 /* If the line's too long for the buffer, discard it. */
686 if ((p = strchr(buf, '\n')) == NULL) {
687 while ((ch = getc(csc->from_fp)) != EOF && ch != '\n');
688 continue;
690 *p = '\0';
693 * The cscope output is in the following format:
695 * <filename> <context> <line number> <pattern>
697 * Figure out how long everything is so we can allocate in one
698 * swell foop, but discard anything that looks wrong.
700 for (p = buf, i = 0;
701 i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i)
702 switch (i) {
703 case 0: /* Filename. */
704 name = t;
705 nlen = strlen(name);
706 break;
707 case 1: /* Context. */
708 break;
709 case 2: /* Line number. */
710 slno = (db_recno_t)atol(t);
711 break;
713 if (i != 3 || p == NULL || t == NULL)
714 continue;
716 /* The rest of the string is the search pattern. */
717 search = p;
718 slen = strlen(p);
720 /* Resolve the file name. */
721 csc_file(sp, csc, name, &dname, &dlen, &isolder);
724 * If the file is older than the cscope database, that is,
725 * the database was built since the file was last modified,
726 * or there wasn't a search string, use the line number.
728 if (isolder || strcmp(search, "<unknown>") == 0) {
729 search = NULL;
730 slen = 0;
734 * Allocate and initialize a tag structure plus the variable
735 * length cscope information that follows it.
737 CALLOC_RET(sp, tp,
738 TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
739 tp->fname = (char *)tp->buf;
740 if (dlen != 0) {
741 memcpy(tp->fname, dname, dlen);
742 tp->fname[dlen] = '/';
743 ++dlen;
745 memcpy(tp->fname + dlen, name, nlen + 1);
746 tp->fnlen = dlen + nlen;
747 tp->slno = slno;
748 if (slen != 0) {
749 tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1);
750 MEMCPYW(tp->search, search, (tp->slen = slen) + 1);
752 CIRCLEQ_INSERT_TAIL(&tqp->tagq, tp, q);
754 ++*matchesp;
757 (void)read_prompt(sp, csc);
758 return (0);
760 io_err: if (feof(csc->from_fp))
761 errno = EIO;
762 msgq_str(sp, M_SYSERR, "%s", csc->dname);
763 terminate(sp, csc, 0);
764 return (1);
768 * csc_file --
769 * Search for the right path to this file.
771 static void
772 csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolderp)
774 struct stat sb;
775 char **pp, buf[MAXPATHLEN];
778 * Check for the file in all of the listed paths. If we don't
779 * find it, we simply return it unchanged. We have to do this
780 * now, even though it's expensive, because if the user changes
781 * directories, we can't change our minds as to where the file
782 * lives.
784 for (pp = csc->paths; *pp != NULL; ++pp) {
785 (void)snprintf(buf, sizeof(buf), "%s/%s", *pp, name);
786 if (stat(buf, &sb) == 0) {
787 *dirp = *pp;
788 *dlenp = strlen(*pp);
789 *isolderp = sb.st_mtime < csc->mtime;
790 return;
793 *dlenp = 0;
797 * cscope_help --
798 * The cscope help command.
800 static int
801 cscope_help(SCR *sp, EXCMD *cmdp, CHAR_T *subcmd)
803 char *np;
804 size_t nlen;
806 INT2CHAR(sp, subcmd, STRLEN(subcmd) + 1, np, nlen);
807 return (csc_help(sp, np));
811 * csc_help --
812 * Display help/usage messages.
814 static int
815 csc_help(SCR *sp, char *cmd)
817 CC const *ccp;
819 if (cmd != NULL && *cmd != '\0')
820 if ((ccp = lookup_ccmd(cmd)) == NULL) {
821 ex_printf(sp,
822 "%s doesn't match any cscope command\n", cmd);
823 return (1);
824 } else {
825 ex_printf(sp,
826 "Command: %s (%s)\n", ccp->name, ccp->help_msg);
827 ex_printf(sp, " Usage: %s\n", ccp->usage_msg);
828 return (0);
831 ex_printf(sp, "cscope commands:\n");
832 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
833 ex_printf(sp, " %*s: %s\n", 5, ccp->name, ccp->help_msg);
834 return (0);
838 * cscope_kill --
839 * The cscope kill command.
841 static int
842 cscope_kill(SCR *sp, EXCMD *cmdp, CHAR_T *cn)
844 char *np;
845 size_t nlen;
847 INT2CHAR(sp, cn, STRLEN(cn) + 1, np, nlen);
848 return (terminate(sp, NULL, atoi(np)));
852 * terminate --
853 * Detach from a cscope process.
855 static int
856 terminate(SCR *sp, CSC *csc, int n)
858 EX_PRIVATE *exp;
859 int i, pstat;
861 exp = EXP(sp);
864 * We either get a csc structure or a number. If not provided a
865 * csc structure, find the right one.
867 if (csc == NULL) {
868 if (n < 1)
869 goto badno;
870 for (i = 1, csc = exp->cscq.lh_first;
871 csc != NULL; csc = csc->q.le_next, i++)
872 if (i == n)
873 break;
874 if (csc == NULL) {
875 badno: msgq(sp, M_ERR, "312|%d: no such cscope session", n);
876 return (1);
881 * XXX
882 * Theoretically, we have the only file descriptors to the process,
883 * so closing them should let it exit gracefully, deleting temporary
884 * files, etc. The original vi cscope integration sent the cscope
885 * connection a SIGTERM signal, so I'm not sure if closing the file
886 * descriptors is sufficient.
888 if (csc->from_fp != NULL)
889 (void)fclose(csc->from_fp);
890 if (csc->to_fp != NULL)
891 (void)fclose(csc->to_fp);
892 (void)waitpid(csc->pid, &pstat, 0);
894 /* Discard cscope connection information. */
895 LIST_REMOVE(csc, q);
896 if (csc->pbuf != NULL)
897 free(csc->pbuf);
898 if (csc->paths != NULL)
899 free(csc->paths);
900 free(csc);
901 return (0);
905 * cscope_reset --
906 * The cscope reset command.
908 static int
909 cscope_reset(SCR *sp, EXCMD *cmdp, CHAR_T *notusedp)
911 EX_PRIVATE *exp;
913 for (exp = EXP(sp); exp->cscq.lh_first != NULL;) {
914 static CHAR_T one[] = {'1', 0};
915 if (cscope_kill(sp, cmdp, one))
916 return (1);
918 return (0);
922 * cscope_display --
923 * Display current connections.
925 * PUBLIC: int cscope_display __P((SCR *));
928 cscope_display(SCR *sp)
930 EX_PRIVATE *exp;
931 CSC *csc;
932 int i;
934 exp = EXP(sp);
935 if (exp->cscq.lh_first == NULL) {
936 ex_printf(sp, "No cscope connections.\n");
937 return (0);
939 for (i = 1,
940 csc = exp->cscq.lh_first; csc != NULL; ++i, csc = csc->q.le_next)
941 ex_printf(sp,
942 "%2d %s (process %lu)\n", i, csc->dname, (u_long)csc->pid);
943 return (0);
947 * cscope_search --
948 * Search a file for a cscope entry.
950 * PUBLIC: int cscope_search __P((SCR *, TAGQ *, TAG *));
953 cscope_search(SCR *sp, TAGQ *tqp, TAG *tp)
955 MARK m;
957 /* If we don't have a search pattern, use the line number. */
958 if (tp->search == NULL) {
959 if (!db_exist(sp, tp->slno)) {
960 tag_msg(sp, TAG_BADLNO, tqp->tag);
961 return (1);
963 m.lno = tp->slno;
964 } else {
966 * Search for the tag; cheap fallback for C functions
967 * if the name is the same but the arguments have changed.
969 m.lno = 1;
970 m.cno = 0;
971 if (f_search(sp, &m, &m,
972 tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FIRST)) {
973 tag_msg(sp, TAG_SEARCH, tqp->tag);
974 return (1);
978 * !!!
979 * Historically, tags set the search direction if it wasn't
980 * already set.
982 if (sp->searchdir == NOTSET)
983 sp->searchdir = FORWARD;
987 * !!!
988 * Tags move to the first non-blank, NOT the search pattern start.
990 sp->lno = m.lno;
991 sp->cno = 0;
992 (void)nonblank(sp, sp->lno, &sp->cno);
993 return (0);
998 * lookup_ccmd --
999 * Return a pointer to the command structure.
1001 static CC const *
1002 lookup_ccmd(char *name)
1004 CC const *ccp;
1005 size_t len;
1007 len = strlen(name);
1008 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
1009 if (strncmp(name, ccp->name, len) == 0)
1010 return (ccp);
1011 return (NULL);
1015 * read_prompt --
1016 * Read a prompt from cscope.
1018 static int
1019 read_prompt(SCR *sp, CSC *csc)
1021 int ch;
1023 #define CSCOPE_PROMPT ">> "
1024 for (;;) {
1025 while ((ch =
1026 getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]);
1027 if (ch == EOF) {
1028 terminate(sp, csc, 0);
1029 return (1);
1031 if (getc(csc->from_fp) != CSCOPE_PROMPT[1])
1032 continue;
1033 if (getc(csc->from_fp) != CSCOPE_PROMPT[2])
1034 continue;
1035 break;
1037 return (0);