move configure.in to configure.ac
[nvi.git] / ex / ex_cscope.c
blob698c1ef27a08718a34a1f697b895fe1ad8372a37
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.21 2003/11/05 17:11:54 skimo Exp $ (Berkeley) $Date: 2003/11/05 17:11:54 $";
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_RETC(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';
254 else {
255 dbname = np;
256 np = ".";
259 /* Allocate a cscope connection structure and initialize its fields. */
260 len = strlen(np);
261 CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
262 csc->dname = csc->buf;
263 csc->dlen = len;
264 memcpy(csc->dname, np, len);
265 csc->mtime = sb.st_mtime;
267 /* Get the search paths for the cscope. */
268 if (get_paths(sp, csc))
269 goto err;
271 /* Start the cscope process. */
272 if (run_cscope(sp, csc, dbname))
273 goto err;
276 * Add the cscope connection to the screen's list. From now on,
277 * on error, we have to call terminate, which expects the csc to
278 * be on the chain.
280 LIST_INSERT_HEAD(&exp->cscq, csc, q);
282 /* Read the initial prompt from the cscope to make sure it's okay. */
283 return read_prompt(sp, csc);
285 err: free(csc);
286 return (1);
290 * get_paths --
291 * Get the directories to search for the files associated with this
292 * cscope database.
294 static int
295 get_paths(SCR *sp, CSC *csc)
297 struct stat sb;
298 int fd, nentries;
299 size_t len;
300 char *p, **pathp, buf[MAXPATHLEN * 2];
303 * EXTENSION #2:
305 * If there's a cscope directory with a file named CSCOPE_PATHS, it
306 * contains a colon-separated list of paths in which to search for
307 * files returned by cscope.
309 * XXX
310 * These paths are absolute paths, and not relative to the cscope
311 * directory. To fix this, rewrite the each path using the cscope
312 * directory as a prefix.
314 (void)snprintf(buf, sizeof(buf), "%s/%s", csc->dname, CSCOPE_PATHS);
315 if (stat(buf, &sb) == 0) {
316 /* Read in the CSCOPE_PATHS file. */
317 len = sb.st_size;
318 MALLOC_RET(sp, csc->pbuf, char *, len + 1);
319 if ((fd = open(buf, O_RDONLY, 0)) < 0 ||
320 read(fd, csc->pbuf, len) != len) {
321 msgq_str(sp, M_SYSERR, buf, "%s");
322 if (fd >= 0)
323 (void)close(fd);
324 return (1);
326 (void)close(fd);
327 csc->pbuf[len] = '\0';
329 /* Count up the entries. */
330 for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p)
331 if (p[0] == ':' && p[1] != '\0')
332 ++nentries;
334 /* Build an array of pointers to the paths. */
335 CALLOC_GOTO(sp,
336 csc->paths, char **, nentries + 1, sizeof(char **));
337 for (pathp = csc->paths, p = strtok(csc->pbuf, ":");
338 p != NULL; p = strtok(NULL, ":"))
339 *pathp++ = p;
340 return (0);
344 * If the CSCOPE_PATHS file doesn't exist, we look for files
345 * relative to the cscope directory.
347 if ((csc->pbuf = strdup(csc->dname)) == NULL) {
348 msgq(sp, M_SYSERR, NULL);
349 return (1);
351 CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
352 csc->paths[0] = csc->pbuf;
353 return (0);
355 alloc_err:
356 if (csc->pbuf != NULL) {
357 free(csc->pbuf);
358 csc->pbuf = NULL;
360 return (1);
364 * run_cscope --
365 * Fork off the cscope process.
367 static int
368 run_cscope(SCR *sp, CSC *csc, char *dbname)
370 int to_cs[2], from_cs[2];
371 char cmd[MAXPATHLEN * 2];
374 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
375 * from_cs[0] and writes to to_cs[1].
377 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[0] = -1;
378 if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
379 msgq(sp, M_SYSERR, "pipe");
380 goto err;
382 switch (csc->pid = vfork()) {
383 case -1:
384 msgq(sp, M_SYSERR, "vfork");
385 err: if (to_cs[0] != -1)
386 (void)close(to_cs[0]);
387 if (to_cs[1] != -1)
388 (void)close(to_cs[1]);
389 if (from_cs[0] != -1)
390 (void)close(from_cs[0]);
391 if (from_cs[1] != -1)
392 (void)close(from_cs[1]);
393 return (1);
394 case 0: /* child: run cscope. */
395 (void)dup2(to_cs[0], STDIN_FILENO);
396 (void)dup2(from_cs[1], STDOUT_FILENO);
397 (void)dup2(from_cs[1], STDERR_FILENO);
399 /* Close unused file descriptors. */
400 (void)close(to_cs[1]);
401 (void)close(from_cs[0]);
403 /* Run the cscope command. */
404 #define CSCOPE_CMD_FMT "cd '%s' && exec cscope -dl -f %s"
405 (void)snprintf(cmd, sizeof(cmd),
406 CSCOPE_CMD_FMT, csc->dname, dbname);
407 (void)execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
408 msgq_str(sp, M_SYSERR, cmd, "execl: %s");
409 _exit (127);
410 /* NOTREACHED */
411 default: /* parent. */
412 /* Close unused file descriptors. */
413 (void)close(to_cs[0]);
414 (void)close(from_cs[1]);
417 * Save the file descriptors for later duplication, and
418 * reopen as streams.
420 csc->to_fd = to_cs[1];
421 csc->to_fp = fdopen(to_cs[1], "w");
422 csc->from_fd = from_cs[0];
423 csc->from_fp = fdopen(from_cs[0], "r");
424 break;
426 return (0);
430 * cscope_find --
431 * The cscope find command.
433 static int
434 cscope_find(SCR *sp, EXCMD *cmdp, CHAR_T *pattern)
436 CSC *csc, *csc_next;
437 EX_PRIVATE *exp;
438 FREF *frp;
439 TAGQ *rtqp, *tqp;
440 TAG *rtp;
441 db_recno_t lno;
442 size_t cno, search;
443 int force, istmp, matches;
444 char *np = NULL;
445 size_t nlen;
447 exp = EXP(sp);
449 /* Check for connections. */
450 if (exp->cscq.lh_first == NULL) {
451 msgq(sp, M_ERR, "310|No cscope connections running");
452 return (1);
456 * Allocate all necessary memory before doing anything hard. If the
457 * tags stack is empty, we'll need the `local context' TAGQ structure
458 * later.
460 rtp = NULL;
461 rtqp = NULL;
462 if (exp->tq.cqh_first == (void *)&exp->tq) {
463 /* Initialize the `local context' tag queue structure. */
464 CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
465 CIRCLEQ_INIT(&rtqp->tagq);
467 /* Initialize and link in its tag structure. */
468 CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
469 CIRCLEQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
470 rtqp->current = rtp;
473 /* Create the cscope command. */
474 INT2CHAR(sp, pattern, STRLEN(pattern) + 1, np, nlen);
475 np = strdup(np);
476 if ((tqp = create_cs_cmd(sp, np, &search)) == NULL)
477 goto err;
480 * Stick the current context in a convenient place, we'll lose it
481 * when we switch files.
483 frp = sp->frp;
484 lno = sp->lno;
485 cno = sp->cno;
486 istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN);
488 /* Search all open connections for a match. */
489 matches = 0;
490 for (csc = exp->cscq.lh_first; csc != NULL; csc = csc_next) {
491 /* Copy csc->q.lh_next here in case csc is killed. */
492 csc_next = csc->q.le_next;
495 * Send the command to the cscope program. (We skip the
496 * first two bytes of the command, because we stored the
497 * search cscope command character and a leading space
498 * there.)
500 (void)fprintf(csc->to_fp, "%d%s\n", search, tqp->tag + 2);
501 (void)fflush(csc->to_fp);
503 /* Read the output. */
504 if (parse(sp, csc, tqp, &matches)) {
505 if (rtqp != NULL)
506 free(rtqp);
507 tagq_free(sp, tqp);
508 return (1);
512 if (matches == 0) {
513 msgq(sp, M_INFO, "278|No matches for query");
514 return (0);
517 tqp->current = tqp->tagq.cqh_first;
519 /* Try to switch to the first tag. */
520 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
521 if (F_ISSET(cmdp, E_NEWSCREEN)) {
522 if (ex_tag_Nswitch(sp, tqp->current, force))
523 goto err;
525 /* Everything else gets done in the new screen. */
526 sp = sp->nextdisp;
527 exp = EXP(sp);
528 } else
529 if (ex_tag_nswitch(sp, tqp->current, force))
530 goto err;
533 * If this is the first tag, put a `current location' queue entry
534 * in place, so we can pop all the way back to the current mark.
535 * Note, it doesn't point to much of anything, it's a placeholder.
537 if (exp->tq.cqh_first == (void *)&exp->tq) {
538 CIRCLEQ_INSERT_HEAD(&exp->tq, rtqp, q);
539 } else
540 rtqp = exp->tq.cqh_first;
542 /* Link the current TAGQ structure into place. */
543 CIRCLEQ_INSERT_HEAD(&exp->tq, tqp, q);
545 (void)cscope_search(sp, tqp, tqp->current);
548 * Move the current context from the temporary save area into the
549 * right structure.
551 * If we were in a temporary file, we don't have a context to which
552 * we can return, so just make it be the same as what we're moving
553 * to. It will be a little odd that ^T doesn't change anything, but
554 * I don't think it's a big deal.
556 if (istmp) {
557 rtqp->current->frp = sp->frp;
558 rtqp->current->lno = sp->lno;
559 rtqp->current->cno = sp->cno;
560 } else {
561 rtqp->current->frp = frp;
562 rtqp->current->lno = lno;
563 rtqp->current->cno = cno;
566 return (0);
568 err:
569 alloc_err:
570 if (rtqp != NULL)
571 free(rtqp);
572 if (rtp != NULL)
573 free(rtp);
574 if (np != NULL)
575 free(np);
576 return (1);
580 * create_cs_cmd --
581 * Build a cscope command, creating and initializing the base TAGQ.
583 static TAGQ *
584 create_cs_cmd(SCR *sp, char *pattern, size_t *searchp)
586 CB *cbp;
587 TAGQ *tqp;
588 size_t tlen;
589 char *p;
592 * Cscope supports a "change pattern" command which we never use,
593 * cscope command 5. Set CSCOPE_QUERIES[5] to " " since the user
594 * can't pass " " as the first character of pattern. That way the
595 * user can't ask for pattern 5 so we don't need any special-case
596 * code.
598 #define CSCOPE_QUERIES "sgdct efi"
600 if (pattern == NULL)
601 goto usage;
603 /* Skip leading blanks, check for command character. */
604 for (; isblank(pattern[0]); ++pattern);
605 if (pattern[0] == '\0' || !isblank(pattern[1]))
606 goto usage;
607 for (*searchp = 0, p = CSCOPE_QUERIES;
608 *p != '\0' && *p != pattern[0]; ++*searchp, ++p);
609 if (*p == '\0') {
610 msgq(sp, M_ERR,
611 "311|%s: unknown search type: use one of %s",
612 KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES);
613 return (NULL);
616 /* Skip <blank> characters to the pattern. */
617 for (p = pattern + 1; *p != '\0' && isblank(*p); ++p);
618 if (*p == '\0') {
619 usage: (void)csc_help(sp, "find");
620 return (NULL);
623 /* The user can specify the contents of a buffer as the pattern. */
624 cbp = NULL;
625 if (p[0] == '"' && p[1] != '\0' && p[2] == '\0')
626 CBNAME(sp, cbp, p[1]);
627 if (cbp != NULL) {
628 INT2CHAR(sp, cbp->textq.cqh_first->lb,
629 cbp->textq.cqh_first->len, p, tlen);
630 } else
631 tlen = strlen(p);
633 /* Allocate and initialize the TAGQ structure. */
634 CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
635 if (tqp == NULL)
636 return (NULL);
637 CIRCLEQ_INIT(&tqp->tagq);
638 tqp->tag = tqp->buf;
639 tqp->tag[0] = pattern[0];
640 tqp->tag[1] = ' ';
641 tqp->tlen = tlen + 2;
642 memcpy(tqp->tag + 2, p, tlen);
643 tqp->tag[tlen + 2] = '\0';
644 F_SET(tqp, TAG_CSCOPE);
646 return (tqp);
650 * parse --
651 * Parse the cscope output.
653 static int
654 parse(SCR *sp, CSC *csc, TAGQ *tqp, int *matchesp)
656 TAG *tp;
657 db_recno_t slno;
658 size_t dlen, nlen, slen;
659 int ch, i, isolder, nlines;
660 char *dname, *name, *search, *p, *t, dummy[2], buf[2048];
662 for (;;) {
663 if (!fgets(buf, sizeof(buf), csc->from_fp))
664 goto io_err;
667 * If the database is out of date, or there's some other
668 * problem, cscope will output error messages before the
669 * number-of-lines output. Display/discard any output
670 * that doesn't match what we want.
672 #define CSCOPE_NLINES_FMT "cscope: %d lines%1[\n]"
673 if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2)
674 break;
675 if ((p = strchr(buf, '\n')) != NULL)
676 *p = '\0';
677 msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf);
680 while (nlines--) {
681 if (fgets(buf, sizeof(buf), csc->from_fp) == NULL)
682 goto io_err;
684 /* If the line's too long for the buffer, discard it. */
685 if ((p = strchr(buf, '\n')) == NULL) {
686 while ((ch = getc(csc->from_fp)) != EOF && ch != '\n');
687 continue;
689 *p = '\0';
692 * The cscope output is in the following format:
694 * <filename> <context> <line number> <pattern>
696 * Figure out how long everything is so we can allocate in one
697 * swell foop, but discard anything that looks wrong.
699 for (p = buf, i = 0;
700 i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i)
701 switch (i) {
702 case 0: /* Filename. */
703 name = t;
704 nlen = strlen(name);
705 break;
706 case 1: /* Context. */
707 break;
708 case 2: /* Line number. */
709 slno = (db_recno_t)atol(t);
710 break;
712 if (i != 3 || p == NULL || t == NULL)
713 continue;
715 /* The rest of the string is the search pattern. */
716 search = p;
717 slen = strlen(p);
719 /* Resolve the file name. */
720 csc_file(sp, csc, name, &dname, &dlen, &isolder);
723 * If the file is older than the cscope database, that is,
724 * the database was built since the file was last modified,
725 * or there wasn't a search string, use the line number.
727 if (isolder || strcmp(search, "<unknown>") == 0) {
728 search = NULL;
729 slen = 0;
733 * Allocate and initialize a tag structure plus the variable
734 * length cscope information that follows it.
736 CALLOC_RET(sp, tp,
737 TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
738 tp->fname = (char *)tp->buf;
739 if (dlen != 0) {
740 memcpy(tp->fname, dname, dlen);
741 tp->fname[dlen] = '/';
742 ++dlen;
744 memcpy(tp->fname + dlen, name, nlen + 1);
745 tp->fnlen = dlen + nlen;
746 tp->slno = slno;
747 if (slen != 0) {
748 tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1);
749 MEMCPYW(tp->search, search, (tp->slen = slen) + 1);
751 CIRCLEQ_INSERT_TAIL(&tqp->tagq, tp, q);
753 ++*matchesp;
756 return read_prompt(sp, csc);
758 io_err: if (feof(csc->from_fp))
759 errno = EIO;
760 msgq_str(sp, M_SYSERR, "%s", csc->dname);
761 terminate(sp, csc, 0);
762 return (1);
766 * csc_file --
767 * Search for the right path to this file.
769 static void
770 csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolderp)
772 struct stat sb;
773 char **pp, buf[MAXPATHLEN];
776 * Check for the file in all of the listed paths. If we don't
777 * find it, we simply return it unchanged. We have to do this
778 * now, even though it's expensive, because if the user changes
779 * directories, we can't change our minds as to where the file
780 * lives.
782 for (pp = csc->paths; *pp != NULL; ++pp) {
783 (void)snprintf(buf, sizeof(buf), "%s/%s", *pp, name);
784 if (stat(buf, &sb) == 0) {
785 *dirp = *pp;
786 *dlenp = strlen(*pp);
787 *isolderp = sb.st_mtime < csc->mtime;
788 return;
791 *dlenp = 0;
795 * cscope_help --
796 * The cscope help command.
798 static int
799 cscope_help(SCR *sp, EXCMD *cmdp, CHAR_T *subcmd)
801 char *np;
802 size_t nlen;
804 INT2CHAR(sp, subcmd, STRLEN(subcmd) + 1, np, nlen);
805 return (csc_help(sp, np));
809 * csc_help --
810 * Display help/usage messages.
812 static int
813 csc_help(SCR *sp, char *cmd)
815 CC const *ccp;
817 if (cmd != NULL && *cmd != '\0')
818 if ((ccp = lookup_ccmd(cmd)) == NULL) {
819 ex_printf(sp,
820 "%s doesn't match any cscope command\n", cmd);
821 return (1);
822 } else {
823 ex_printf(sp,
824 "Command: %s (%s)\n", ccp->name, ccp->help_msg);
825 ex_printf(sp, " Usage: %s\n", ccp->usage_msg);
826 return (0);
829 ex_printf(sp, "cscope commands:\n");
830 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
831 ex_printf(sp, " %*s: %s\n", 5, ccp->name, ccp->help_msg);
832 return (0);
836 * cscope_kill --
837 * The cscope kill command.
839 static int
840 cscope_kill(SCR *sp, EXCMD *cmdp, CHAR_T *cn)
842 char *np;
843 size_t nlen;
845 INT2CHAR(sp, cn, STRLEN(cn) + 1, np, nlen);
846 return (terminate(sp, NULL, atoi(np)));
850 * terminate --
851 * Detach from a cscope process.
853 static int
854 terminate(SCR *sp, CSC *csc, int n)
856 EX_PRIVATE *exp;
857 int i, pstat;
859 exp = EXP(sp);
862 * We either get a csc structure or a number. If not provided a
863 * csc structure, find the right one.
865 if (csc == NULL) {
866 if (n < 1)
867 goto badno;
868 for (i = 1, csc = exp->cscq.lh_first;
869 csc != NULL; csc = csc->q.le_next, i++)
870 if (i == n)
871 break;
872 if (csc == NULL) {
873 badno: msgq(sp, M_ERR, "312|%d: no such cscope session", n);
874 return (1);
879 * XXX
880 * Theoretically, we have the only file descriptors to the process,
881 * so closing them should let it exit gracefully, deleting temporary
882 * files, etc. The original vi cscope integration sent the cscope
883 * connection a SIGTERM signal, so I'm not sure if closing the file
884 * descriptors is sufficient.
886 if (csc->from_fp != NULL)
887 (void)fclose(csc->from_fp);
888 if (csc->to_fp != NULL)
889 (void)fclose(csc->to_fp);
890 (void)waitpid(csc->pid, &pstat, 0);
892 /* Discard cscope connection information. */
893 LIST_REMOVE(csc, q);
894 if (csc->pbuf != NULL)
895 free(csc->pbuf);
896 if (csc->paths != NULL)
897 free(csc->paths);
898 free(csc);
899 return (0);
903 * cscope_reset --
904 * The cscope reset command.
906 static int
907 cscope_reset(SCR *sp, EXCMD *cmdp, CHAR_T *notusedp)
909 EX_PRIVATE *exp;
911 for (exp = EXP(sp); exp->cscq.lh_first != NULL;) {
912 static CHAR_T one[] = {'1', 0};
913 if (cscope_kill(sp, cmdp, one))
914 return (1);
916 return (0);
920 * cscope_display --
921 * Display current connections.
923 * PUBLIC: int cscope_display __P((SCR *));
926 cscope_display(SCR *sp)
928 EX_PRIVATE *exp;
929 CSC *csc;
930 int i;
932 exp = EXP(sp);
933 if (exp->cscq.lh_first == NULL) {
934 ex_printf(sp, "No cscope connections.\n");
935 return (0);
937 for (i = 1,
938 csc = exp->cscq.lh_first; csc != NULL; ++i, csc = csc->q.le_next)
939 ex_printf(sp,
940 "%2d %s (process %lu)\n", i, csc->dname, (u_long)csc->pid);
941 return (0);
945 * cscope_search --
946 * Search a file for a cscope entry.
948 * PUBLIC: int cscope_search __P((SCR *, TAGQ *, TAG *));
951 cscope_search(SCR *sp, TAGQ *tqp, TAG *tp)
953 MARK m;
955 /* If we don't have a search pattern, use the line number. */
956 if (tp->search == NULL) {
957 if (!db_exist(sp, tp->slno)) {
958 tag_msg(sp, TAG_BADLNO, tqp->tag);
959 return (1);
961 m.lno = tp->slno;
962 } else {
964 * Search for the tag; cheap fallback for C functions
965 * if the name is the same but the arguments have changed.
967 m.lno = 1;
968 m.cno = 0;
969 if (f_search(sp, &m, &m,
970 tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FIRST)) {
971 tag_msg(sp, TAG_SEARCH, tqp->tag);
972 return (1);
976 * !!!
977 * Historically, tags set the search direction if it wasn't
978 * already set.
980 if (sp->searchdir == NOTSET)
981 sp->searchdir = FORWARD;
985 * !!!
986 * Tags move to the first non-blank, NOT the search pattern start.
988 sp->lno = m.lno;
989 sp->cno = 0;
990 (void)nonblank(sp, sp->lno, &sp->cno);
991 return (0);
996 * lookup_ccmd --
997 * Return a pointer to the command structure.
999 static CC const *
1000 lookup_ccmd(char *name)
1002 CC const *ccp;
1003 size_t len;
1005 len = strlen(name);
1006 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
1007 if (strncmp(name, ccp->name, len) == 0)
1008 return (ccp);
1009 return (NULL);
1013 * read_prompt --
1014 * Read a prompt from cscope.
1016 static int
1017 read_prompt(SCR *sp, CSC *csc)
1019 int ch;
1021 #define CSCOPE_PROMPT ">> "
1022 for (;;) {
1023 while ((ch =
1024 getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]);
1025 if (ch == EOF) {
1026 terminate(sp, csc, 0);
1027 return (1);
1029 if (getc(csc->from_fp) != CSCOPE_PROMPT[1])
1030 continue;
1031 if (getc(csc->from_fp) != CSCOPE_PROMPT[2])
1032 continue;
1033 break;
1035 return (0);