1/*	$NetBSD: ex_cscope.c,v 1.8 2017/11/12 15:22:09 rin Exp $ */
2/*-
3 * Copyright (c) 1994, 1996
4 *	Rob Mayoff.  All rights reserved.
5 * Copyright (c) 1996
6 *	Keith Bostic.  All rights reserved.
7 *
8 * See the LICENSE file for redistribution information.
9 */
10
11#include "config.h"
12
13#include <sys/cdefs.h>
14#if 0
15#ifndef lint
16static 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 ";
17#endif /* not lint */
18#else
19__RCSID("$NetBSD: ex_cscope.c,v 1.8 2017/11/12 15:22:09 rin Exp $");
20#endif
21
22#include <sys/param.h>
23#include <sys/types.h>		/* XXX: param.h may not have included types.h */
24#include <sys/queue.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/wait.h>
28
29#include <bitstring.h>
30#include <ctype.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <limits.h>
34#include <stddef.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <termios.h>
39#include <unistd.h>
40
41#include "../common/common.h"
42#include "pathnames.h"
43#include "tag.h"
44
45#define	CSCOPE_DBFILE		"cscope.out"
46#define	CSCOPE_PATHS		"cscope.tpath"
47
48/*
49 * 0name	find all uses of name
50 * 1name	find definition of name
51 * 2name	find all function calls made from name
52 * 3name	find callers of name
53 * 4string	find text string (cscope 12.9)
54 * 4name	find assignments to name (cscope 13.3)
55 * 5pattern	change pattern -- NOT USED
56 * 6pattern	find pattern
57 * 7name	find files with name as substring
58 * 8name	find files #including name
59 */
60#define	FINDHELP "\
61find c|d|e|f|g|i|s|t buffer|pattern\n\
62      c: find callers of name\n\
63      d: find all function calls made from name\n\
64      e: find pattern\n\
65      f: find files with name as substring\n\
66      g: find definition of name\n\
67      i: find files #including name\n\
68      s: find all uses of name\n\
69      t: find assignments to name"
70
71static int cscope_add __P((SCR *, EXCMD *, const CHAR_T *));
72static int cscope_find __P((SCR *, EXCMD*, const CHAR_T *));
73static int cscope_help __P((SCR *, EXCMD *, const CHAR_T *));
74static int cscope_kill __P((SCR *, EXCMD *, const CHAR_T *));
75static int cscope_reset __P((SCR *, EXCMD *, const CHAR_T *));
76
77typedef struct _cc {
78	const char	 *name;
79	int	(*function) __P((SCR *, EXCMD *, const CHAR_T *));
80	const char	 *help_msg;
81	const char	 *usage_msg;
82} CC;
83
84static CC const cscope_cmds[] = {
85	{ "add",   cscope_add,
86	  "Add a new cscope database", "add file | directory" },
87	{ "find",  cscope_find,
88	  "Query the databases for a pattern", FINDHELP },
89	{ "help",  cscope_help,
90	  "Show help for cscope commands", "help [command]" },
91	{ "kill",  cscope_kill,
92	  "Kill a cscope connection", "kill number" },
93	{ "reset", cscope_reset,
94	  "Discard all current cscope connections", "reset" },
95	{ NULL, NULL, NULL, NULL }
96};
97
98static TAGQ	*create_cs_cmd __P((SCR *, const char *, size_t *));
99static int	 csc_help __P((SCR *, const char *));
100static void	 csc_file __P((SCR *,
101		    CSC *, char *, char **, size_t *, int *));
102static int	 get_paths __P((SCR *, CSC *));
103static CC const	*lookup_ccmd __P((const char *));
104static int	 parse __P((SCR *, CSC *, TAGQ *, int *));
105static int	 read_prompt __P((SCR *, CSC *));
106static int	 run_cscope __P((SCR *, CSC *, const char *));
107static int	 start_cscopes __P((SCR *, EXCMD *));
108static int	 terminate __P((SCR *, CSC *, int));
109
110/*
111 * ex_cscope --
112 *	Perform an ex cscope.
113 *
114 * PUBLIC: int ex_cscope __P((SCR *, EXCMD *));
115 */
116int
117ex_cscope(SCR *sp, EXCMD *cmdp)
118{
119	CC const *ccp;
120	EX_PRIVATE *exp;
121	int i;
122	CHAR_T *cmd;
123	CHAR_T *p;
124	const char *np;
125	size_t nlen;
126
127	/* Initialize the default cscope directories. */
128	exp = EXP(sp);
129	if (!F_ISSET(exp, EXP_CSCINIT) && start_cscopes(sp, cmdp))
130		return (1);
131	F_SET(exp, EXP_CSCINIT);
132
133	/* Skip leading whitespace. */
134	for (p = cmdp->argv[0]->bp, i = cmdp->argv[0]->len; i > 0; --i, ++p)
135		if (!ISBLANK((UCHAR_T)*p))
136			break;
137	if (i == 0)
138		goto usage;
139
140	/* Skip the command to any arguments. */
141	for (cmd = p; i > 0; --i, ++p)
142		if (ISBLANK((UCHAR_T)*p))
143			break;
144	if (*p != '\0') {
145		*p++ = '\0';
146		for (; *p && ISBLANK((UCHAR_T)*p); ++p);
147	}
148
149	INT2CHAR(sp, cmd, STRLEN(cmd) + 1, np, nlen);
150	if ((ccp = lookup_ccmd(np)) == NULL) {
151usage:		msgq(sp, M_ERR, "309|Use \"cscope help\" for help");
152		return (1);
153	}
154
155	/* Call the underlying function. */
156	return (ccp->function(sp, cmdp, p));
157}
158
159/*
160 * start_cscopes --
161 *	Initialize the cscope package.
162 */
163static int
164start_cscopes(SCR *sp, EXCMD *cmdp)
165{
166	size_t blen, len;
167	char *bp, *cscopes, *p, *t;
168	const CHAR_T *wp;
169	size_t wlen;
170
171	/*
172	 * EXTENSION #1:
173	 *
174	 * If the CSCOPE_DIRS environment variable is set, we treat it as a
175	 * list of cscope directories that we're using, similar to the tags
176	 * edit option.
177	 *
178	 * XXX
179	 * This should probably be an edit option, although that implies that
180	 * we start/stop cscope processes periodically, instead of once when
181	 * the editor starts.
182	 */
183	if ((cscopes = getenv("CSCOPE_DIRS")) == NULL)
184		return (0);
185	len = strlen(cscopes);
186	GET_SPACE_RETC(sp, bp, blen, len);
187	memcpy(bp, cscopes, len + 1);
188
189	for (cscopes = t = bp; (p = strsep(&t, "\t :")) != NULL;)
190		if (*p != '\0') {
191			CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
192			(void)cscope_add(sp, cmdp, wp);
193		}
194
195	FREE_SPACE(sp, bp, blen);
196	return (0);
197}
198
199/*
200 * cscope_add --
201 *	The cscope add command.
202 */
203static int
204cscope_add(SCR *sp, EXCMD *cmdp, const CHAR_T *dname)
205{
206	struct stat sb;
207	EX_PRIVATE *exp;
208	CSC *csc;
209	size_t len;
210	int cur_argc;
211	const char *dbname;
212	char path[MAXPATHLEN];
213	const char *np;
214	char *npp;
215	size_t nlen;
216
217	exp = EXP(sp);
218
219	/*
220	 *  0 additional args: usage.
221	 *  1 additional args: matched a file.
222	 * >1 additional args: object, too many args.
223	 */
224	cur_argc = cmdp->argc;
225	if (argv_exp2(sp, cmdp, dname, STRLEN(dname))) {
226		return (1);
227	}
228	if (cmdp->argc == cur_argc) {
229		(void)csc_help(sp, "add");
230		return (1);
231	}
232	if (cmdp->argc == cur_argc + 1)
233		dname = cmdp->argv[cur_argc]->bp;
234	else {
235		INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);
236		ex_emsg(sp, np, EXM_FILECOUNT);
237		return (1);
238	}
239
240	INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);
241
242	/*
243	 * The user can specify a specific file (so they can have multiple
244	 * Cscope databases in a single directory) or a directory.  If the
245	 * file doesn't exist, we're done.  If it's a directory, append the
246	 * standard database file name and try again.  Store the directory
247	 * name regardless so that we can use it as a base for searches.
248	 */
249	if (stat(np, &sb)) {
250		msgq(sp, M_SYSERR, "%s", np);
251		return (1);
252	}
253	if (S_ISDIR(sb.st_mode)) {
254		(void)snprintf(path, sizeof(path),
255		    "%s/%s", np, CSCOPE_DBFILE);
256		if (stat(path, &sb)) {
257			msgq(sp, M_SYSERR, "%s", path);
258			return (1);
259		}
260		dbname = CSCOPE_DBFILE;
261	} else if ((npp = strrchr(np, '/')) != NULL) {
262		*npp = '\0';
263		dbname = npp + 1;
264	} else {
265		dbname = np;
266		np = ".";
267	}
268
269	/* Allocate a cscope connection structure and initialize its fields. */
270	len = strlen(np);
271	CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
272	csc->dname = csc->buf;
273	csc->dlen = len;
274	memcpy(csc->dname, np, len);
275	csc->mtime = sb.st_mtime;
276
277	/* Get the search paths for the cscope. */
278	if (get_paths(sp, csc))
279		goto err;
280
281	/* Start the cscope process. */
282	if (run_cscope(sp, csc, dbname))
283		goto err;
284
285	/*
286	 * Add the cscope connection to the screen's list.  From now on,
287	 * on error, we have to call terminate, which expects the csc to
288	 * be on the chain.
289	 */
290	LIST_INSERT_HEAD(&exp->cscq, csc, q);
291
292	/* Read the initial prompt from the cscope to make sure it's okay. */
293	return read_prompt(sp, csc);
294
295err:	free(csc);
296	return (1);
297}
298
299/*
300 * get_paths --
301 *	Get the directories to search for the files associated with this
302 *	cscope database.
303 */
304static int
305get_paths(SCR *sp, CSC *csc)
306{
307	struct stat sb;
308	int fd, nentries;
309	size_t len;
310	char *p, **pathp, buf[MAXPATHLEN * 2];
311
312	/*
313	 * EXTENSION #2:
314	 *
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.
318	 *
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.
323	 */
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		    (size_t)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);
335		}
336		(void)close(fd);
337		csc->pbuf[len] = '\0';
338
339		/* Count up the entries. */
340		for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p)
341			if (p[0] == ':' && p[1] != '\0')
342				++nentries;
343
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);
351	}
352
353	/*
354	 * If the CSCOPE_PATHS file doesn't exist, we look for files
355	 * relative to the cscope directory.
356	 */
357	if ((csc->pbuf = strdup(csc->dname)) == NULL) {
358		msgq(sp, M_SYSERR, NULL);
359		return (1);
360	}
361	CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
362	csc->paths[0] = csc->pbuf;
363	return (0);
364
365alloc_err:
366	if (csc->pbuf != NULL) {
367		free(csc->pbuf);
368		csc->pbuf = NULL;
369	}
370	return (1);
371}
372
373/*
374 * run_cscope --
375 *	Fork off the cscope process.
376 */
377static int
378run_cscope(SCR *sp, CSC *csc, const char *dbname)
379{
380	int to_cs[2], from_cs[2];
381	char cmd[MAXPATHLEN * 2];
382
383	/*
384	 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
385	 * from_cs[0] and writes to to_cs[1].
386	 */
387	to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
388	if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
389		msgq(sp, M_SYSERR, "pipe");
390		goto err;
391	}
392	switch (csc->pid = vfork()) {
393	case -1:
394		msgq(sp, M_SYSERR, "vfork");
395err:		if (to_cs[0] != -1)
396			(void)close(to_cs[0]);
397		if (to_cs[1] != -1)
398			(void)close(to_cs[1]);
399		if (from_cs[0] != -1)
400			(void)close(from_cs[0]);
401		if (from_cs[1] != -1)
402			(void)close(from_cs[1]);
403		return (1);
404	case 0:				/* child: run cscope. */
405		(void)dup2(to_cs[0], STDIN_FILENO);
406		(void)dup2(from_cs[1], STDOUT_FILENO);
407		(void)dup2(from_cs[1], STDERR_FILENO);
408
409		/* Close unused file descriptors. */
410		(void)close(to_cs[1]);
411		(void)close(from_cs[0]);
412
413		/* Run the cscope command. */
414#define	CSCOPE_CMD_FMT		"cd '%s' && exec cscope -dl -f %s"
415		(void)snprintf(cmd, sizeof(cmd),
416		    CSCOPE_CMD_FMT, csc->dname, dbname);
417		(void)execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
418		msgq_str(sp, M_SYSERR, cmd, "execl: %s");
419		_exit (127);
420		/* NOTREACHED */
421	default:			/* parent. */
422		/* Close unused file descriptors. */
423		(void)close(to_cs[0]);
424		(void)close(from_cs[1]);
425
426		/*
427		 * Save the file descriptors for later duplication, and
428		 * reopen as streams.
429		 */
430		csc->to_fd = to_cs[1];
431		csc->to_fp = fdopen(to_cs[1], "w");
432		csc->from_fd = from_cs[0];
433		csc->from_fp = fdopen(from_cs[0], "r");
434		break;
435	}
436	return (0);
437}
438
439/*
440 * cscope_find --
441 *	The cscope find command.
442 */
443static int
444cscope_find(SCR *sp, EXCMD *cmdp, const CHAR_T *pattern)
445{
446	CSC *csc, *csc_next;
447	EX_PRIVATE *exp;
448	FREF *frp;
449	TAGQ *rtqp, *tqp;
450	TAG *rtp;
451	db_recno_t lno;
452	size_t cno, search;
453	int force, istmp, matches;
454	const char *np = NULL;
455	size_t nlen;
456
457	exp = EXP(sp);
458
459	/* Check for connections. */
460	if (LIST_EMPTY(&exp->cscq)) {
461		msgq(sp, M_ERR, "310|No cscope connections running");
462		return (1);
463	}
464
465	/*
466	 * Allocate all necessary memory before doing anything hard.  If the
467	 * tags stack is empty, we'll need the `local context' TAGQ structure
468	 * later.
469	 */
470	rtp = NULL;
471	rtqp = NULL;
472	if (TAILQ_EMPTY(&exp->tq)) {
473		/* Initialize the `local context' tag queue structure. */
474		CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
475		TAILQ_INIT(&rtqp->tagq);
476
477		/* Initialize and link in its tag structure. */
478		CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
479		TAILQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
480		rtqp->current = rtp;
481	}
482
483	/* Create the cscope command. */
484	INT2CHAR(sp, pattern, STRLEN(pattern) + 1, np, nlen);
485	np = strdup(np);
486	if ((tqp = create_cs_cmd(sp, np, &search)) == NULL)
487		goto err;
488
489	/*
490	 * Stick the current context in a convenient place, we'll lose it
491	 * when we switch files.
492	 */
493	frp = sp->frp;
494	lno = sp->lno;
495	cno = sp->cno;
496	istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN);
497
498	/* Search all open connections for a match. */
499	matches = 0;
500	LIST_FOREACH_SAFE(csc, &exp->cscq, q, csc_next) {
501		/*
502		 * Send the command to the cscope program.  (We skip the
503		 * first two bytes of the command, because we stored the
504		 * search cscope command character and a leading space
505		 * there.)
506		 */
507		(void)fprintf(csc->to_fp, "%zu%s\n", search, tqp->tag + 2);
508		(void)fflush(csc->to_fp);
509
510		/* Read the output. */
511		if (parse(sp, csc, tqp, &matches)) {
512			free(rtqp);
513			tagq_free(sp, tqp);
514			return (1);
515		}
516	}
517
518	if (matches == 0) {
519		free(rtqp);
520		msgq(sp, M_INFO, "278|No matches for query");
521		return (0);
522	}
523
524	tqp->current = TAILQ_FIRST(&tqp->tagq);
525
526	/* Try to switch to the first tag. */
527	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
528	if (F_ISSET(cmdp, E_NEWSCREEN)) {
529		if (ex_tag_Nswitch(sp, tqp->current, force))
530			goto err;
531
532		/* Everything else gets done in the new screen. */
533		sp = sp->nextdisp;
534		exp = EXP(sp);
535	} else
536		if (ex_tag_nswitch(sp, tqp->current, force))
537			goto err;
538
539	/*
540	 * If this is the first tag, put a `current location' queue entry
541	 * in place, so we can pop all the way back to the current mark.
542	 * Note, it doesn't point to much of anything, it's a placeholder.
543	 */
544	if (TAILQ_EMPTY(&exp->tq)) {
545		TAILQ_INSERT_HEAD(&exp->tq, rtqp, q);
546		F_SET(rtqp, TAG_IS_LINKED);
547	} else {
548		free(rtqp);
549		rtqp = TAILQ_FIRST(&exp->tq);
550	}
551
552	/* Link the current TAGQ structure into place. */
553	TAILQ_INSERT_HEAD(&exp->tq, tqp, q);
554	F_SET(tqp, TAG_IS_LINKED);
555
556	(void)cscope_search(sp, tqp, tqp->current);
557
558	/*
559	 * Move the current context from the temporary save area into the
560	 * right structure.
561	 *
562	 * If we were in a temporary file, we don't have a context to which
563	 * we can return, so just make it be the same as what we're moving
564	 * to.  It will be a little odd that ^T doesn't change anything, but
565	 * I don't think it's a big deal.
566	 */
567	if (istmp) {
568		rtqp->current->frp = sp->frp;
569		rtqp->current->lno = sp->lno;
570		rtqp->current->cno = sp->cno;
571	} else {
572		rtqp->current->frp = frp;
573		rtqp->current->lno = lno;
574		rtqp->current->cno = cno;
575	}
576
577	return (0);
578
579err:
580alloc_err:
581	free(rtqp);
582	free(rtp);
583	free(__UNCONST(np));
584	return (1);
585}
586
587/*
588 * create_cs_cmd --
589 *	Build a cscope command, creating and initializing the base TAGQ.
590 */
591static TAGQ *
592create_cs_cmd(SCR *sp, const char *pattern, size_t *searchp)
593{
594	CB *cbp;
595	TAGQ *tqp;
596	size_t tlen;
597	const char *p;
598
599	/*
600	 * Cscope supports a "change pattern" command which we never use,
601	 * cscope command 5.  Set CSCOPE_QUERIES[5] to " " since the user
602	 * can't pass " " as the first character of pattern.  That way the
603	 * user can't ask for pattern 5 so we don't need any special-case
604	 * code.
605	 */
606#define	CSCOPE_QUERIES		"sgdct efi"
607
608	if (pattern == NULL)
609		goto usage;
610
611	/* Skip leading blanks, check for command character. */
612	for (; isblank((unsigned char)pattern[0]); ++pattern);
613	if (pattern[0] == '\0' || !isblank((unsigned char)pattern[1]))
614		goto usage;
615	for (*searchp = 0, p = CSCOPE_QUERIES;
616	    *p != '\0' && *p != pattern[0]; ++*searchp, ++p);
617	if (*p == '\0') {
618		msgq(sp, M_ERR,
619		    "311|%s: unknown search type: use one of %s",
620		    KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES);
621		return (NULL);
622	}
623
624	/* Skip <blank> characters to the pattern. */
625	for (p = pattern + 1; *p != '\0' && isblank((unsigned char)*p); ++p);
626	if (*p == '\0') {
627usage:		(void)csc_help(sp, "find");
628		return (NULL);
629	}
630
631	/* The user can specify the contents of a buffer as the pattern. */
632	cbp = NULL;
633	if (p[0] == '"' && p[1] != '\0' && p[2] == '\0')
634		CBNAME(sp, cbp, p[1]);
635	if (cbp != NULL) {
636		TEXT *t = TAILQ_FIRST(&cbp->textq);
637		INT2CHAR(sp, t->lb, t->len, p, tlen);
638	} else
639		tlen = strlen(p);
640
641	/* Allocate and initialize the TAGQ structure. */
642	CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
643	if (tqp == NULL)
644		return (NULL);
645	TAILQ_INIT(&tqp->tagq);
646	tqp->tag = tqp->buf;
647	tqp->tag[0] = pattern[0];
648	tqp->tag[1] = ' ';
649	tqp->tlen = tlen + 2;
650	memcpy(tqp->tag + 2, p, tlen);
651	tqp->tag[tlen + 2] = '\0';
652	F_SET(tqp, TAG_CSCOPE);
653
654	return (tqp);
655}
656
657/*
658 * parse --
659 *	Parse the cscope output.
660 */
661static int
662parse(SCR *sp, CSC *csc, TAGQ *tqp, int *matchesp)
663{
664	TAG *tp;
665	db_recno_t slno = 0;
666	size_t dlen, nlen = 0, slen = 0;
667	int ch, i, isolder = 0, nlines;
668	char *dname = NULL, *name = NULL, *search, *p, *t, dummy[2], buf[2048];
669
670	for (;;) {
671		if (!fgets(buf, sizeof(buf), csc->from_fp))
672			goto io_err;
673
674		/*
675		 * If the database is out of date, or there's some other
676		 * problem, cscope will output error messages before the
677		 * number-of-lines output.  Display/discard any output
678		 * that doesn't match what we want.
679		 */
680#define	CSCOPE_NLINES_FMT	"cscope: %d lines%1[\n]"
681		if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2)
682			break;
683		if ((p = strchr(buf, '\n')) != NULL)
684			*p = '\0';
685		msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf);
686	}
687
688	while (nlines--) {
689		if (fgets(buf, sizeof(buf), csc->from_fp) == NULL)
690			goto io_err;
691
692		/* If the line's too long for the buffer, discard it. */
693		if ((p = strchr(buf, '\n')) == NULL) {
694			while ((ch = getc(csc->from_fp)) != EOF && ch != '\n');
695			continue;
696		}
697		*p = '\0';
698
699		/*
700		 * The cscope output is in the following format:
701		 *
702		 *	<filename> <context> <line number> <pattern>
703		 *
704		 * Figure out how long everything is so we can allocate in one
705		 * swell foop, but discard anything that looks wrong.
706		 */
707		for (p = buf, i = 0;
708		    i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i)
709			switch (i) {
710			case 0:			/* Filename. */
711				name = t;
712				nlen = strlen(name);
713				break;
714			case 1:			/* Context. */
715				break;
716			case 2:			/* Line number. */
717				slno = (db_recno_t)atol(t);
718				break;
719			}
720		if (i != 3 || p == NULL || t == NULL)
721			continue;
722
723		/* The rest of the string is the search pattern. */
724		search = p;
725		slen = strlen(p);
726
727		/* Resolve the file name. */
728		csc_file(sp, csc, name, &dname, &dlen, &isolder);
729
730		/*
731		 * If the file is older than the cscope database, that is,
732		 * the database was built since the file was last modified,
733		 * or there wasn't a search string, use the line number.
734		 */
735		if (isolder || strcmp(search, "<unknown>") == 0) {
736			search = NULL;
737			slen = 0;
738		}
739
740		/*
741		 * Allocate and initialize a tag structure plus the variable
742		 * length cscope information that follows it.
743		 */
744		CALLOC_RET(sp, tp,
745		    TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
746		tp->fname = (char *)tp->buf;
747		if (dlen != 0) {
748			memcpy(tp->fname, dname, dlen);
749			tp->fname[dlen] = '/';
750			++dlen;
751		}
752		memcpy(tp->fname + dlen, name, nlen + 1);
753		tp->fnlen = dlen + nlen;
754		tp->slno = slno;
755		if (slen != 0) {
756			tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1);
757			MEMCPYW(tp->search, search, (tp->slen = slen) + 1);
758		}
759		TAILQ_INSERT_TAIL(&tqp->tagq, tp, q);
760
761		++*matchesp;
762	}
763
764	return read_prompt(sp, csc);
765
766io_err:	if (feof(csc->from_fp))
767		errno = EIO;
768	msgq_str(sp, M_SYSERR, "%s", csc->dname);
769	terminate(sp, csc, 0);
770	return (1);
771}
772
773/*
774 * csc_file --
775 *	Search for the right path to this file.
776 */
777static void
778csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolderp)
779{
780	struct stat sb;
781	char **pp, buf[MAXPATHLEN];
782
783	/*
784	 * Check for the file in all of the listed paths.  If we don't
785	 * find it, we simply return it unchanged.  We have to do this
786	 * now, even though it's expensive, because if the user changes
787	 * directories, we can't change our minds as to where the file
788	 * lives.
789	 */
790	for (pp = csc->paths; *pp != NULL; ++pp) {
791		(void)snprintf(buf, sizeof(buf), "%s/%s", *pp, name);
792		if (stat(buf, &sb) == 0) {
793			*dirp = *pp;
794			*dlenp = strlen(*pp);
795			*isolderp = sb.st_mtime < csc->mtime;
796			return;
797		}
798	}
799	*dlenp = 0;
800}
801
802/*
803 * cscope_help --
804 *	The cscope help command.
805 */
806static int
807cscope_help(SCR *sp, EXCMD *cmdp, const CHAR_T *subcmd)
808{
809	const char *np;
810	size_t nlen;
811
812	INT2CHAR(sp, subcmd, STRLEN(subcmd) + 1, np, nlen);
813	return (csc_help(sp, np));
814}
815
816/*
817 * csc_help --
818 *	Display help/usage messages.
819 */
820static int
821csc_help(SCR *sp, const char *cmd)
822{
823	CC const *ccp;
824
825	if (cmd != NULL && *cmd != '\0') {
826		if ((ccp = lookup_ccmd(cmd)) == NULL) {
827			ex_printf(sp,
828			    "%s doesn't match any cscope command\n", cmd);
829			return (1);
830		} else {
831			ex_printf(sp,
832		          "Command: %s (%s)\n", ccp->name, ccp->help_msg);
833			ex_printf(sp, "  Usage: %s\n", ccp->usage_msg);
834			return (0);
835		}
836	}
837
838	ex_printf(sp, "cscope commands:\n");
839	for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
840		ex_printf(sp, "  %*s: %s\n", 5, ccp->name, ccp->help_msg);
841	return (0);
842}
843
844/*
845 * cscope_kill --
846 *	The cscope kill command.
847 */
848static int
849cscope_kill(SCR *sp, EXCMD *cmdp, const CHAR_T *cn)
850{
851	const char *np;
852	size_t nlen;
853
854	INT2CHAR(sp, cn, STRLEN(cn) + 1, np, nlen);
855	return (terminate(sp, NULL, atoi(np)));
856}
857
858/*
859 * terminate --
860 *	Detach from a cscope process.
861 */
862static int
863terminate(SCR *sp, CSC *csc, int n)
864{
865	EX_PRIVATE *exp;
866	int i, pstat;
867
868	exp = EXP(sp);
869
870	/*
871	 * We either get a csc structure or a number.  If not provided a
872	 * csc structure, find the right one.
873	 */
874	if (csc == NULL) {
875		if (n < 1)
876			goto badno;
877		i = 1;
878		LIST_FOREACH(csc, &exp->cscq, q)
879			if (i++ == n)
880				break;
881		if (csc == NULL) {
882badno:			msgq(sp, M_ERR, "312|%d: no such cscope session", n);
883			return (1);
884		}
885	}
886
887	/*
888	 * XXX
889	 * Theoretically, we have the only file descriptors to the process,
890	 * so closing them should let it exit gracefully, deleting temporary
891	 * files, etc.  The original vi cscope integration sent the cscope
892	 * connection a SIGTERM signal, so I'm not sure if closing the file
893	 * descriptors is sufficient.
894	 */
895	if (csc->from_fp != NULL)
896		(void)fclose(csc->from_fp);
897	if (csc->to_fp != NULL)
898		(void)fclose(csc->to_fp);
899	(void)waitpid(csc->pid, &pstat, 0);
900
901	/* Discard cscope connection information. */
902	LIST_REMOVE(csc, q);
903	if (csc->pbuf != NULL)
904		free(csc->pbuf);
905	if (csc->paths != NULL)
906		free(csc->paths);
907	free(csc);
908	return (0);
909}
910
911/*
912 * cscope_reset --
913 *	The cscope reset command.
914 */
915static int
916cscope_reset(SCR *sp, EXCMD *cmdp, const CHAR_T *notusedp)
917{
918	EX_PRIVATE *exp;
919
920	for (exp = EXP(sp); !LIST_EMPTY(&exp->cscq);) {
921		static CHAR_T one[] = {'1', 0};
922		if (cscope_kill(sp, cmdp, one))
923			return (1);
924	}
925	return (0);
926}
927
928/*
929 * cscope_display --
930 *	Display current connections.
931 *
932 * PUBLIC: int cscope_display __P((SCR *));
933 */
934int
935cscope_display(SCR *sp)
936{
937	EX_PRIVATE *exp;
938	CSC *csc;
939	int i;
940
941	exp = EXP(sp);
942	if (LIST_EMPTY(&exp->cscq)) {
943		ex_printf(sp, "No cscope connections.\n");
944		return (0);
945	}
946	i = 1;
947	LIST_FOREACH(csc, &exp->cscq, q)
948		ex_printf(sp,
949		    "%2d %s (process %lu)\n", i++, csc->dname, (u_long)csc->pid);
950	return (0);
951}
952
953/*
954 * cscope_search --
955 *	Search a file for a cscope entry.
956 *
957 * PUBLIC: int cscope_search __P((SCR *, TAGQ *, TAG *));
958 */
959int
960cscope_search(SCR *sp, TAGQ *tqp, TAG *tp)
961{
962	MARK m;
963
964	/* If we don't have a search pattern, use the line number. */
965	if (tp->search == NULL) {
966		if (!db_exist(sp, tp->slno)) {
967			tag_msg(sp, TAG_BADLNO, tqp->tag);
968			return (1);
969		}
970		m.lno = tp->slno;
971	} else {
972		/*
973		 * Search for the tag; cheap fallback for C functions
974		 * if the name is the same but the arguments have changed.
975		 */
976		m.lno = 1;
977		m.cno = 0;
978		if (f_search(sp, &m, &m,
979		    tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FIRST)) {
980			tag_msg(sp, TAG_SEARCH, tqp->tag);
981			return (1);
982		}
983
984		/*
985		 * !!!
986		 * Historically, tags set the search direction if it wasn't
987		 * already set.
988		 */
989		if (sp->searchdir == NOTSET)
990			sp->searchdir = FORWARD;
991	}
992
993	/*
994	 * !!!
995	 * Tags move to the first non-blank, NOT the search pattern start.
996	 */
997	sp->lno = m.lno;
998	sp->cno = 0;
999	(void)nonblank(sp, sp->lno, &sp->cno);
1000	return (0);
1001}
1002
1003
1004/*
1005 * lookup_ccmd --
1006 *	Return a pointer to the command structure.
1007 */
1008static CC const *
1009lookup_ccmd(const char *name)
1010{
1011	CC const *ccp;
1012	size_t len;
1013
1014	len = strlen(name);
1015	for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
1016		if (strncmp(name, ccp->name, len) == 0)
1017			return (ccp);
1018	return (NULL);
1019}
1020
1021/*
1022 * read_prompt --
1023 *	Read a prompt from cscope.
1024 */
1025static int
1026read_prompt(SCR *sp, CSC *csc)
1027{
1028	int ch;
1029
1030#define	CSCOPE_PROMPT		">> "
1031	for (;;) {
1032		while ((ch =
1033		    getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]);
1034		if (ch == EOF) {
1035			terminate(sp, csc, 0);
1036			return (1);
1037		}
1038		if (getc(csc->from_fp) != CSCOPE_PROMPT[1])
1039			continue;
1040		if (getc(csc->from_fp) != CSCOPE_PROMPT[2])
1041			continue;
1042		break;
1043	}
1044	return (0);
1045}
1046