xref: /minix/external/bsd/nvi/dist/ex/ex_cscope.c (revision 0a6a1f1d)
1 /*	$NetBSD: ex_cscope.c,v 1.7 2014/08/26 15:19:38 aymeric 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
16 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 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_cscope.c,v 1.7 2014/08/26 15:19:38 aymeric 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 "\
61 find 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 
71 static int cscope_add __P((SCR *, EXCMD *, const CHAR_T *));
72 static int cscope_find __P((SCR *, EXCMD*, const CHAR_T *));
73 static int cscope_help __P((SCR *, EXCMD *, const CHAR_T *));
74 static int cscope_kill __P((SCR *, EXCMD *, const CHAR_T *));
75 static int cscope_reset __P((SCR *, EXCMD *, const CHAR_T *));
76 
77 typedef 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 
84 static 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 
98 static TAGQ	*create_cs_cmd __P((SCR *, const char *, size_t *));
99 static int	 csc_help __P((SCR *, const char *));
100 static void	 csc_file __P((SCR *,
101 		    CSC *, char *, char **, size_t *, int *));
102 static int	 get_paths __P((SCR *, CSC *));
103 static CC const	*lookup_ccmd __P((const char *));
104 static int	 parse __P((SCR *, CSC *, TAGQ *, int *));
105 static int	 read_prompt __P((SCR *, CSC *));
106 static int	 run_cscope __P((SCR *, CSC *, const char *));
107 static int	 start_cscopes __P((SCR *, EXCMD *));
108 static 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  */
116 int
ex_cscope(SCR * sp,EXCMD * cmdp)117 ex_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) {
151 usage:		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  */
163 static int
start_cscopes(SCR * sp,EXCMD * cmdp)164 start_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  */
203 static int
cscope_add(SCR * sp,EXCMD * cmdp,const CHAR_T * dname)204 cscope_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 		ex_emsg(sp, np, EXM_FILECOUNT);
236 		return (1);
237 	}
238 
239 	INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen);
240 
241 	/*
242 	 * The user can specify a specific file (so they can have multiple
243 	 * Cscope databases in a single directory) or a directory.  If the
244 	 * file doesn't exist, we're done.  If it's a directory, append the
245 	 * standard database file name and try again.  Store the directory
246 	 * name regardless so that we can use it as a base for searches.
247 	 */
248 	if (stat(np, &sb)) {
249 		msgq(sp, M_SYSERR, "%s", np);
250 		return (1);
251 	}
252 	if (S_ISDIR(sb.st_mode)) {
253 		(void)snprintf(path, sizeof(path),
254 		    "%s/%s", np, CSCOPE_DBFILE);
255 		if (stat(path, &sb)) {
256 			msgq(sp, M_SYSERR, "%s", path);
257 			return (1);
258 		}
259 		dbname = CSCOPE_DBFILE;
260 	} else if ((npp = strrchr(np, '/')) != NULL) {
261 		*npp = '\0';
262 		dbname = npp + 1;
263 	} else {
264 		dbname = np;
265 		np = ".";
266 	}
267 
268 	/* Allocate a cscope connection structure and initialize its fields. */
269 	len = strlen(np);
270 	CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
271 	csc->dname = csc->buf;
272 	csc->dlen = len;
273 	memcpy(csc->dname, np, len);
274 	csc->mtime = sb.st_mtime;
275 
276 	/* Get the search paths for the cscope. */
277 	if (get_paths(sp, csc))
278 		goto err;
279 
280 	/* Start the cscope process. */
281 	if (run_cscope(sp, csc, dbname))
282 		goto err;
283 
284 	/*
285 	 * Add the cscope connection to the screen's list.  From now on,
286 	 * on error, we have to call terminate, which expects the csc to
287 	 * be on the chain.
288 	 */
289 	LIST_INSERT_HEAD(&exp->cscq, csc, q);
290 
291 	/* Read the initial prompt from the cscope to make sure it's okay. */
292 	return read_prompt(sp, csc);
293 
294 err:	free(csc);
295 	return (1);
296 }
297 
298 /*
299  * get_paths --
300  *	Get the directories to search for the files associated with this
301  *	cscope database.
302  */
303 static int
get_paths(SCR * sp,CSC * csc)304 get_paths(SCR *sp, CSC *csc)
305 {
306 	struct stat sb;
307 	int fd, nentries;
308 	size_t len;
309 	char *p, **pathp, buf[MAXPATHLEN * 2];
310 
311 	/*
312 	 * EXTENSION #2:
313 	 *
314 	 * If there's a cscope directory with a file named CSCOPE_PATHS, it
315 	 * contains a colon-separated list of paths in which to search for
316 	 * files returned by cscope.
317 	 *
318 	 * XXX
319 	 * These paths are absolute paths, and not relative to the cscope
320 	 * directory.  To fix this, rewrite the each path using the cscope
321 	 * directory as a prefix.
322 	 */
323 	(void)snprintf(buf, sizeof(buf), "%s/%s", csc->dname, CSCOPE_PATHS);
324 	if (stat(buf, &sb) == 0) {
325 		/* Read in the CSCOPE_PATHS file. */
326 		len = sb.st_size;
327 		MALLOC_RET(sp, csc->pbuf, char *, len + 1);
328 		if ((fd = open(buf, O_RDONLY, 0)) < 0 ||
329 		    (size_t)read(fd, csc->pbuf, len) != len) {
330 			 msgq_str(sp, M_SYSERR, buf, "%s");
331 			 if (fd >= 0)
332 				(void)close(fd);
333 			 return (1);
334 		}
335 		(void)close(fd);
336 		csc->pbuf[len] = '\0';
337 
338 		/* Count up the entries. */
339 		for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p)
340 			if (p[0] == ':' && p[1] != '\0')
341 				++nentries;
342 
343 		/* Build an array of pointers to the paths. */
344 		CALLOC_GOTO(sp,
345 		    csc->paths, char **, nentries + 1, sizeof(char **));
346 		for (pathp = csc->paths, p = strtok(csc->pbuf, ":");
347 		    p != NULL; p = strtok(NULL, ":"))
348 			*pathp++ = p;
349 		return (0);
350 	}
351 
352 	/*
353 	 * If the CSCOPE_PATHS file doesn't exist, we look for files
354 	 * relative to the cscope directory.
355 	 */
356 	if ((csc->pbuf = strdup(csc->dname)) == NULL) {
357 		msgq(sp, M_SYSERR, NULL);
358 		return (1);
359 	}
360 	CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
361 	csc->paths[0] = csc->pbuf;
362 	return (0);
363 
364 alloc_err:
365 	if (csc->pbuf != NULL) {
366 		free(csc->pbuf);
367 		csc->pbuf = NULL;
368 	}
369 	return (1);
370 }
371 
372 /*
373  * run_cscope --
374  *	Fork off the cscope process.
375  */
376 static int
run_cscope(SCR * sp,CSC * csc,const char * dbname)377 run_cscope(SCR *sp, CSC *csc, const char *dbname)
378 {
379 	int to_cs[2], from_cs[2];
380 	char cmd[MAXPATHLEN * 2];
381 
382 	/*
383 	 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
384 	 * from_cs[0] and writes to to_cs[1].
385 	 */
386 	to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
387 	if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
388 		msgq(sp, M_SYSERR, "pipe");
389 		goto err;
390 	}
391 	switch (csc->pid = vfork()) {
392 	case -1:
393 		msgq(sp, M_SYSERR, "vfork");
394 err:		if (to_cs[0] != -1)
395 			(void)close(to_cs[0]);
396 		if (to_cs[1] != -1)
397 			(void)close(to_cs[1]);
398 		if (from_cs[0] != -1)
399 			(void)close(from_cs[0]);
400 		if (from_cs[1] != -1)
401 			(void)close(from_cs[1]);
402 		return (1);
403 	case 0:				/* child: run cscope. */
404 		(void)dup2(to_cs[0], STDIN_FILENO);
405 		(void)dup2(from_cs[1], STDOUT_FILENO);
406 		(void)dup2(from_cs[1], STDERR_FILENO);
407 
408 		/* Close unused file descriptors. */
409 		(void)close(to_cs[1]);
410 		(void)close(from_cs[0]);
411 
412 		/* Run the cscope command. */
413 #define	CSCOPE_CMD_FMT		"cd '%s' && exec cscope -dl -f %s"
414 		(void)snprintf(cmd, sizeof(cmd),
415 		    CSCOPE_CMD_FMT, csc->dname, dbname);
416 		(void)execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
417 		msgq_str(sp, M_SYSERR, cmd, "execl: %s");
418 		_exit (127);
419 		/* NOTREACHED */
420 	default:			/* parent. */
421 		/* Close unused file descriptors. */
422 		(void)close(to_cs[0]);
423 		(void)close(from_cs[1]);
424 
425 		/*
426 		 * Save the file descriptors for later duplication, and
427 		 * reopen as streams.
428 		 */
429 		csc->to_fd = to_cs[1];
430 		csc->to_fp = fdopen(to_cs[1], "w");
431 		csc->from_fd = from_cs[0];
432 		csc->from_fp = fdopen(from_cs[0], "r");
433 		break;
434 	}
435 	return (0);
436 }
437 
438 /*
439  * cscope_find --
440  *	The cscope find command.
441  */
442 static int
cscope_find(SCR * sp,EXCMD * cmdp,const CHAR_T * pattern)443 cscope_find(SCR *sp, EXCMD *cmdp, const CHAR_T *pattern)
444 {
445 	CSC *csc, *csc_next;
446 	EX_PRIVATE *exp;
447 	FREF *frp;
448 	TAGQ *rtqp, *tqp;
449 	TAG *rtp;
450 	db_recno_t lno;
451 	size_t cno, search;
452 	int force, istmp, matches;
453 	const char *np = NULL;
454 	size_t nlen;
455 
456 	exp = EXP(sp);
457 
458 	/* Check for connections. */
459 	if (LIST_EMPTY(&exp->cscq)) {
460 		msgq(sp, M_ERR, "310|No cscope connections running");
461 		return (1);
462 	}
463 
464 	/*
465 	 * Allocate all necessary memory before doing anything hard.  If the
466 	 * tags stack is empty, we'll need the `local context' TAGQ structure
467 	 * later.
468 	 */
469 	rtp = NULL;
470 	rtqp = NULL;
471 	if (TAILQ_EMPTY(&exp->tq)) {
472 		/* Initialize the `local context' tag queue structure. */
473 		CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
474 		TAILQ_INIT(&rtqp->tagq);
475 
476 		/* Initialize and link in its tag structure. */
477 		CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
478 		TAILQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
479 		rtqp->current = rtp;
480 	}
481 
482 	/* Create the cscope command. */
483 	INT2CHAR(sp, pattern, STRLEN(pattern) + 1, np, nlen);
484 	np = strdup(np);
485 	if ((tqp = create_cs_cmd(sp, np, &search)) == NULL)
486 		goto err;
487 
488 	/*
489 	 * Stick the current context in a convenient place, we'll lose it
490 	 * when we switch files.
491 	 */
492 	frp = sp->frp;
493 	lno = sp->lno;
494 	cno = sp->cno;
495 	istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN);
496 
497 	/* Search all open connections for a match. */
498 	matches = 0;
499 	LIST_FOREACH_SAFE(csc, &exp->cscq, q, csc_next) {
500 		/*
501 		 * Send the command to the cscope program.  (We skip the
502 		 * first two bytes of the command, because we stored the
503 		 * search cscope command character and a leading space
504 		 * there.)
505 		 */
506 		(void)fprintf(csc->to_fp, "%zu%s\n", search, tqp->tag + 2);
507 		(void)fflush(csc->to_fp);
508 
509 		/* Read the output. */
510 		if (parse(sp, csc, tqp, &matches)) {
511 			free(rtqp);
512 			tagq_free(sp, tqp);
513 			return (1);
514 		}
515 	}
516 
517 	if (matches == 0) {
518 		free(rtqp);
519 		msgq(sp, M_INFO, "278|No matches for query");
520 		return (0);
521 	}
522 
523 	tqp->current = TAILQ_FIRST(&tqp->tagq);
524 
525 	/* Try to switch to the first tag. */
526 	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
527 	if (F_ISSET(cmdp, E_NEWSCREEN)) {
528 		if (ex_tag_Nswitch(sp, tqp->current, force))
529 			goto err;
530 
531 		/* Everything else gets done in the new screen. */
532 		sp = sp->nextdisp;
533 		exp = EXP(sp);
534 	} else
535 		if (ex_tag_nswitch(sp, tqp->current, force))
536 			goto err;
537 
538 	/*
539 	 * If this is the first tag, put a `current location' queue entry
540 	 * in place, so we can pop all the way back to the current mark.
541 	 * Note, it doesn't point to much of anything, it's a placeholder.
542 	 */
543 	if (TAILQ_EMPTY(&exp->tq)) {
544 		TAILQ_INSERT_HEAD(&exp->tq, rtqp, q);
545 		F_SET(rtqp, TAG_IS_LINKED);
546 	} else {
547 		free(rtqp);
548 		rtqp = TAILQ_FIRST(&exp->tq);
549 	}
550 
551 	/* Link the current TAGQ structure into place. */
552 	TAILQ_INSERT_HEAD(&exp->tq, tqp, q);
553 	F_SET(tqp, TAG_IS_LINKED);
554 
555 	(void)cscope_search(sp, tqp, tqp->current);
556 
557 	/*
558 	 * Move the current context from the temporary save area into the
559 	 * right structure.
560 	 *
561 	 * If we were in a temporary file, we don't have a context to which
562 	 * we can return, so just make it be the same as what we're moving
563 	 * to.  It will be a little odd that ^T doesn't change anything, but
564 	 * I don't think it's a big deal.
565 	 */
566 	if (istmp) {
567 		rtqp->current->frp = sp->frp;
568 		rtqp->current->lno = sp->lno;
569 		rtqp->current->cno = sp->cno;
570 	} else {
571 		rtqp->current->frp = frp;
572 		rtqp->current->lno = lno;
573 		rtqp->current->cno = cno;
574 	}
575 
576 	return (0);
577 
578 err:
579 alloc_err:
580 	free(rtqp);
581 	free(rtp);
582 	free(__UNCONST(np));
583 	return (1);
584 }
585 
586 /*
587  * create_cs_cmd --
588  *	Build a cscope command, creating and initializing the base TAGQ.
589  */
590 static TAGQ *
create_cs_cmd(SCR * sp,const char * pattern,size_t * searchp)591 create_cs_cmd(SCR *sp, const char *pattern, size_t *searchp)
592 {
593 	CB *cbp;
594 	TAGQ *tqp;
595 	size_t tlen;
596 	const char *p;
597 
598 	/*
599 	 * Cscope supports a "change pattern" command which we never use,
600 	 * cscope command 5.  Set CSCOPE_QUERIES[5] to " " since the user
601 	 * can't pass " " as the first character of pattern.  That way the
602 	 * user can't ask for pattern 5 so we don't need any special-case
603 	 * code.
604 	 */
605 #define	CSCOPE_QUERIES		"sgdct efi"
606 
607 	if (pattern == NULL)
608 		goto usage;
609 
610 	/* Skip leading blanks, check for command character. */
611 	for (; isblank((unsigned char)pattern[0]); ++pattern);
612 	if (pattern[0] == '\0' || !isblank((unsigned char)pattern[1]))
613 		goto usage;
614 	for (*searchp = 0, p = CSCOPE_QUERIES;
615 	    *p != '\0' && *p != pattern[0]; ++*searchp, ++p);
616 	if (*p == '\0') {
617 		msgq(sp, M_ERR,
618 		    "311|%s: unknown search type: use one of %s",
619 		    KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES);
620 		return (NULL);
621 	}
622 
623 	/* Skip <blank> characters to the pattern. */
624 	for (p = pattern + 1; *p != '\0' && isblank((unsigned char)*p); ++p);
625 	if (*p == '\0') {
626 usage:		(void)csc_help(sp, "find");
627 		return (NULL);
628 	}
629 
630 	/* The user can specify the contents of a buffer as the pattern. */
631 	cbp = NULL;
632 	if (p[0] == '"' && p[1] != '\0' && p[2] == '\0')
633 		CBNAME(sp, cbp, p[1]);
634 	if (cbp != NULL) {
635 		TEXT *t = TAILQ_FIRST(&cbp->textq);
636 		INT2CHAR(sp, t->lb, t->len, p, tlen);
637 	} else
638 		tlen = strlen(p);
639 
640 	/* Allocate and initialize the TAGQ structure. */
641 	CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
642 	if (tqp == NULL)
643 		return (NULL);
644 	TAILQ_INIT(&tqp->tagq);
645 	tqp->tag = tqp->buf;
646 	tqp->tag[0] = pattern[0];
647 	tqp->tag[1] = ' ';
648 	tqp->tlen = tlen + 2;
649 	memcpy(tqp->tag + 2, p, tlen);
650 	tqp->tag[tlen + 2] = '\0';
651 	F_SET(tqp, TAG_CSCOPE);
652 
653 	return (tqp);
654 }
655 
656 /*
657  * parse --
658  *	Parse the cscope output.
659  */
660 static int
parse(SCR * sp,CSC * csc,TAGQ * tqp,int * matchesp)661 parse(SCR *sp, CSC *csc, TAGQ *tqp, int *matchesp)
662 {
663 	TAG *tp;
664 	db_recno_t slno = 0;
665 	size_t dlen, nlen = 0, slen = 0;
666 	int ch, i, isolder = 0, nlines;
667 	char *dname = NULL, *name = NULL, *search, *p, *t, dummy[2], buf[2048];
668 
669 	for (;;) {
670 		if (!fgets(buf, sizeof(buf), csc->from_fp))
671 			goto io_err;
672 
673 		/*
674 		 * If the database is out of date, or there's some other
675 		 * problem, cscope will output error messages before the
676 		 * number-of-lines output.  Display/discard any output
677 		 * that doesn't match what we want.
678 		 */
679 #define	CSCOPE_NLINES_FMT	"cscope: %d lines%1[\n]"
680 		if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2)
681 			break;
682 		if ((p = strchr(buf, '\n')) != NULL)
683 			*p = '\0';
684 		msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf);
685 	}
686 
687 	while (nlines--) {
688 		if (fgets(buf, sizeof(buf), csc->from_fp) == NULL)
689 			goto io_err;
690 
691 		/* If the line's too long for the buffer, discard it. */
692 		if ((p = strchr(buf, '\n')) == NULL) {
693 			while ((ch = getc(csc->from_fp)) != EOF && ch != '\n');
694 			continue;
695 		}
696 		*p = '\0';
697 
698 		/*
699 		 * The cscope output is in the following format:
700 		 *
701 		 *	<filename> <context> <line number> <pattern>
702 		 *
703 		 * Figure out how long everything is so we can allocate in one
704 		 * swell foop, but discard anything that looks wrong.
705 		 */
706 		for (p = buf, i = 0;
707 		    i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i)
708 			switch (i) {
709 			case 0:			/* Filename. */
710 				name = t;
711 				nlen = strlen(name);
712 				break;
713 			case 1:			/* Context. */
714 				break;
715 			case 2:			/* Line number. */
716 				slno = (db_recno_t)atol(t);
717 				break;
718 			}
719 		if (i != 3 || p == NULL || t == NULL)
720 			continue;
721 
722 		/* The rest of the string is the search pattern. */
723 		search = p;
724 		slen = strlen(p);
725 
726 		/* Resolve the file name. */
727 		csc_file(sp, csc, name, &dname, &dlen, &isolder);
728 
729 		/*
730 		 * If the file is older than the cscope database, that is,
731 		 * the database was built since the file was last modified,
732 		 * or there wasn't a search string, use the line number.
733 		 */
734 		if (isolder || strcmp(search, "<unknown>") == 0) {
735 			search = NULL;
736 			slen = 0;
737 		}
738 
739 		/*
740 		 * Allocate and initialize a tag structure plus the variable
741 		 * length cscope information that follows it.
742 		 */
743 		CALLOC_RET(sp, tp,
744 		    TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
745 		tp->fname = (char *)tp->buf;
746 		if (dlen != 0) {
747 			memcpy(tp->fname, dname, dlen);
748 			tp->fname[dlen] = '/';
749 			++dlen;
750 		}
751 		memcpy(tp->fname + dlen, name, nlen + 1);
752 		tp->fnlen = dlen + nlen;
753 		tp->slno = slno;
754 		if (slen != 0) {
755 			tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1);
756 			MEMCPYW(tp->search, search, (tp->slen = slen) + 1);
757 		}
758 		TAILQ_INSERT_TAIL(&tqp->tagq, tp, q);
759 
760 		++*matchesp;
761 	}
762 
763 	return read_prompt(sp, csc);
764 
765 io_err:	if (feof(csc->from_fp))
766 		errno = EIO;
767 	msgq_str(sp, M_SYSERR, "%s", csc->dname);
768 	terminate(sp, csc, 0);
769 	return (1);
770 }
771 
772 /*
773  * csc_file --
774  *	Search for the right path to this file.
775  */
776 static void
csc_file(SCR * sp,CSC * csc,char * name,char ** dirp,size_t * dlenp,int * isolderp)777 csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolderp)
778 {
779 	struct stat sb;
780 	char **pp, buf[MAXPATHLEN];
781 
782 	/*
783 	 * Check for the file in all of the listed paths.  If we don't
784 	 * find it, we simply return it unchanged.  We have to do this
785 	 * now, even though it's expensive, because if the user changes
786 	 * directories, we can't change our minds as to where the file
787 	 * lives.
788 	 */
789 	for (pp = csc->paths; *pp != NULL; ++pp) {
790 		(void)snprintf(buf, sizeof(buf), "%s/%s", *pp, name);
791 		if (stat(buf, &sb) == 0) {
792 			*dirp = *pp;
793 			*dlenp = strlen(*pp);
794 			*isolderp = sb.st_mtime < csc->mtime;
795 			return;
796 		}
797 	}
798 	*dlenp = 0;
799 }
800 
801 /*
802  * cscope_help --
803  *	The cscope help command.
804  */
805 static int
cscope_help(SCR * sp,EXCMD * cmdp,const CHAR_T * subcmd)806 cscope_help(SCR *sp, EXCMD *cmdp, const CHAR_T *subcmd)
807 {
808 	const char *np;
809 	size_t nlen;
810 
811 	INT2CHAR(sp, subcmd, STRLEN(subcmd) + 1, np, nlen);
812 	return (csc_help(sp, np));
813 }
814 
815 /*
816  * csc_help --
817  *	Display help/usage messages.
818  */
819 static int
csc_help(SCR * sp,const char * cmd)820 csc_help(SCR *sp, const char *cmd)
821 {
822 	CC const *ccp;
823 
824 	if (cmd != NULL && *cmd != '\0') {
825 		if ((ccp = lookup_ccmd(cmd)) == NULL) {
826 			ex_printf(sp,
827 			    "%s doesn't match any cscope command\n", cmd);
828 			return (1);
829 		} else {
830 			ex_printf(sp,
831 		          "Command: %s (%s)\n", ccp->name, ccp->help_msg);
832 			ex_printf(sp, "  Usage: %s\n", ccp->usage_msg);
833 			return (0);
834 		}
835 	}
836 
837 	ex_printf(sp, "cscope commands:\n");
838 	for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
839 		ex_printf(sp, "  %*s: %s\n", 5, ccp->name, ccp->help_msg);
840 	return (0);
841 }
842 
843 /*
844  * cscope_kill --
845  *	The cscope kill command.
846  */
847 static int
cscope_kill(SCR * sp,EXCMD * cmdp,const CHAR_T * cn)848 cscope_kill(SCR *sp, EXCMD *cmdp, const CHAR_T *cn)
849 {
850 	const char *np;
851 	size_t nlen;
852 
853 	INT2CHAR(sp, cn, STRLEN(cn) + 1, np, nlen);
854 	return (terminate(sp, NULL, atoi(np)));
855 }
856 
857 /*
858  * terminate --
859  *	Detach from a cscope process.
860  */
861 static int
terminate(SCR * sp,CSC * csc,int n)862 terminate(SCR *sp, CSC *csc, int n)
863 {
864 	EX_PRIVATE *exp;
865 	int i, pstat;
866 
867 	exp = EXP(sp);
868 
869 	/*
870 	 * We either get a csc structure or a number.  If not provided a
871 	 * csc structure, find the right one.
872 	 */
873 	if (csc == NULL) {
874 		if (n < 1)
875 			goto badno;
876 		i = 1;
877 		LIST_FOREACH(csc, &exp->cscq, q)
878 			if (i++ == n)
879 				break;
880 		if (csc == NULL) {
881 badno:			msgq(sp, M_ERR, "312|%d: no such cscope session", n);
882 			return (1);
883 		}
884 	}
885 
886 	/*
887 	 * XXX
888 	 * Theoretically, we have the only file descriptors to the process,
889 	 * so closing them should let it exit gracefully, deleting temporary
890 	 * files, etc.  The original vi cscope integration sent the cscope
891 	 * connection a SIGTERM signal, so I'm not sure if closing the file
892 	 * descriptors is sufficient.
893 	 */
894 	if (csc->from_fp != NULL)
895 		(void)fclose(csc->from_fp);
896 	if (csc->to_fp != NULL)
897 		(void)fclose(csc->to_fp);
898 	(void)waitpid(csc->pid, &pstat, 0);
899 
900 	/* Discard cscope connection information. */
901 	LIST_REMOVE(csc, q);
902 	if (csc->pbuf != NULL)
903 		free(csc->pbuf);
904 	if (csc->paths != NULL)
905 		free(csc->paths);
906 	free(csc);
907 	return (0);
908 }
909 
910 /*
911  * cscope_reset --
912  *	The cscope reset command.
913  */
914 static int
cscope_reset(SCR * sp,EXCMD * cmdp,const CHAR_T * notusedp)915 cscope_reset(SCR *sp, EXCMD *cmdp, const CHAR_T *notusedp)
916 {
917 	EX_PRIVATE *exp;
918 
919 	for (exp = EXP(sp); !LIST_EMPTY(&exp->cscq);) {
920 		static CHAR_T one[] = {'1', 0};
921 		if (cscope_kill(sp, cmdp, one))
922 			return (1);
923 	}
924 	return (0);
925 }
926 
927 /*
928  * cscope_display --
929  *	Display current connections.
930  *
931  * PUBLIC: int cscope_display __P((SCR *));
932  */
933 int
cscope_display(SCR * sp)934 cscope_display(SCR *sp)
935 {
936 	EX_PRIVATE *exp;
937 	CSC *csc;
938 	int i;
939 
940 	exp = EXP(sp);
941 	if (LIST_EMPTY(&exp->cscq)) {
942 		ex_printf(sp, "No cscope connections.\n");
943 		return (0);
944 	}
945 	i = 1;
946 	LIST_FOREACH(csc, &exp->cscq, q)
947 		ex_printf(sp,
948 		    "%2d %s (process %lu)\n", i++, csc->dname, (u_long)csc->pid);
949 	return (0);
950 }
951 
952 /*
953  * cscope_search --
954  *	Search a file for a cscope entry.
955  *
956  * PUBLIC: int cscope_search __P((SCR *, TAGQ *, TAG *));
957  */
958 int
cscope_search(SCR * sp,TAGQ * tqp,TAG * tp)959 cscope_search(SCR *sp, TAGQ *tqp, TAG *tp)
960 {
961 	MARK m;
962 
963 	/* If we don't have a search pattern, use the line number. */
964 	if (tp->search == NULL) {
965 		if (!db_exist(sp, tp->slno)) {
966 			tag_msg(sp, TAG_BADLNO, tqp->tag);
967 			return (1);
968 		}
969 		m.lno = tp->slno;
970 	} else {
971 		/*
972 		 * Search for the tag; cheap fallback for C functions
973 		 * if the name is the same but the arguments have changed.
974 		 */
975 		m.lno = 1;
976 		m.cno = 0;
977 		if (f_search(sp, &m, &m,
978 		    tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FIRST)) {
979 			tag_msg(sp, TAG_SEARCH, tqp->tag);
980 			return (1);
981 		}
982 
983 		/*
984 		 * !!!
985 		 * Historically, tags set the search direction if it wasn't
986 		 * already set.
987 		 */
988 		if (sp->searchdir == NOTSET)
989 			sp->searchdir = FORWARD;
990 	}
991 
992 	/*
993 	 * !!!
994 	 * Tags move to the first non-blank, NOT the search pattern start.
995 	 */
996 	sp->lno = m.lno;
997 	sp->cno = 0;
998 	(void)nonblank(sp, sp->lno, &sp->cno);
999 	return (0);
1000 }
1001 
1002 
1003 /*
1004  * lookup_ccmd --
1005  *	Return a pointer to the command structure.
1006  */
1007 static CC const *
lookup_ccmd(const char * name)1008 lookup_ccmd(const char *name)
1009 {
1010 	CC const *ccp;
1011 	size_t len;
1012 
1013 	len = strlen(name);
1014 	for (ccp = cscope_cmds; ccp->name != NULL; ++ccp)
1015 		if (strncmp(name, ccp->name, len) == 0)
1016 			return (ccp);
1017 	return (NULL);
1018 }
1019 
1020 /*
1021  * read_prompt --
1022  *	Read a prompt from cscope.
1023  */
1024 static int
read_prompt(SCR * sp,CSC * csc)1025 read_prompt(SCR *sp, CSC *csc)
1026 {
1027 	int ch;
1028 
1029 #define	CSCOPE_PROMPT		">> "
1030 	for (;;) {
1031 		while ((ch =
1032 		    getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]);
1033 		if (ch == EOF) {
1034 			terminate(sp, csc, 0);
1035 			return (1);
1036 		}
1037 		if (getc(csc->from_fp) != CSCOPE_PROMPT[1])
1038 			continue;
1039 		if (getc(csc->from_fp) != CSCOPE_PROMPT[2])
1040 			continue;
1041 		break;
1042 	}
1043 	return (0);
1044 }
1045