xref: /vim-8.2.3635/src/if_cscope.c (revision b005cd80)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * CSCOPE support for Vim added by Andy Kahn <[email protected]>
4  * Ported to Win32 by Sergey Khorev <[email protected]>
5  *
6  * The basic idea/structure of cscope for Vim was borrowed from Nvi.  There
7  * might be a few lines of code that look similar to what Nvi has.
8  *
9  * See README.txt for an overview of the Vim source code.
10  */
11 
12 #include "vim.h"
13 
14 #if defined(FEAT_CSCOPE) || defined(PROTO)
15 
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #if defined(UNIX)
19 # include <sys/wait.h>
20 #endif
21 #include "if_cscope.h"
22 
23 static int	    cs_add(exarg_T *eap);
24 static int	    cs_add_common(char *, char *, char *);
25 static int	    cs_check_for_connections(void);
26 static int	    cs_check_for_tags(void);
27 static int	    cs_cnt_connections(void);
28 static int	    cs_create_connection(int i);
29 #ifdef FEAT_QUICKFIX
30 static void	    cs_file_results(FILE *, int *);
31 #endif
32 static void	    cs_fill_results(char *, int , int *, char ***,
33 			char ***, int *);
34 static int	    cs_find(exarg_T *eap);
35 static int	    cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
36 static int	    cs_help(exarg_T *eap);
37 static int	    cs_insert_filelist(char *, char *, char *,
38 			stat_T *);
39 static int	    cs_kill(exarg_T *eap);
40 static void	    cs_kill_execute(int, char *);
41 static cscmd_T *    cs_lookup_cmd(exarg_T *eap);
42 static char *	    cs_make_vim_style_matches(char *, char *,
43 			char *, char *);
44 static char *	    cs_manage_matches(char **, char **, int, mcmd_e);
45 static void	    cs_print_tags_priv(char **, char **, int);
46 static int	    cs_read_prompt(int);
47 static void	    cs_release_csp(int, int freefnpp);
48 static int	    cs_reset(exarg_T *eap);
49 static char *	    cs_resolve_file(int, char *);
50 static int	    cs_show(exarg_T *eap);
51 
52 
53 static csinfo_T *   csinfo = NULL;
54 static int	    csinfo_size = 0;	/* number of items allocated in
55 					   csinfo[] */
56 
57 static int	    eap_arg_len;    /* length of eap->arg, set in
58 				       cs_lookup_cmd() */
59 static cscmd_T	    cs_cmds[] =
60 {
61     { "add",	cs_add,
62 		N_("Add a new database"),     "add file|dir [pre-path] [flags]", 0 },
63     { "find",	cs_find,
64 		N_("Query for a pattern"),    "find a|c|d|e|f|g|i|s|t name", 1 },
65     { "help",	cs_help,
66 		N_("Show this message"),      "help", 0 },
67     { "kill",	cs_kill,
68 		N_("Kill a connection"),      "kill #", 0 },
69     { "reset",	cs_reset,
70 		N_("Reinit all connections"), "reset", 0 },
71     { "show",	cs_show,
72 		N_("Show connections"),       "show", 0 },
73     { NULL, NULL, NULL, NULL, 0 }
74 };
75 
76     static void
77 cs_usage_msg(csid_e x)
78 {
79     (void)semsg(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
80 }
81 
82 static enum
83 {
84     EXP_CSCOPE_SUBCMD,	/* expand ":cscope" sub-commands */
85     EXP_SCSCOPE_SUBCMD,	/* expand ":scscope" sub-commands */
86     EXP_CSCOPE_FIND,	/* expand ":cscope find" arguments */
87     EXP_CSCOPE_KILL	/* expand ":cscope kill" arguments */
88 } expand_what;
89 
90 /*
91  * Function given to ExpandGeneric() to obtain the cscope command
92  * expansion.
93  */
94     char_u *
95 get_cscope_name(expand_T *xp UNUSED, int idx)
96 {
97     int		current_idx;
98     int		i;
99 
100     switch (expand_what)
101     {
102     case EXP_CSCOPE_SUBCMD:
103 	/* Complete with sub-commands of ":cscope":
104 	 * add, find, help, kill, reset, show */
105 	return (char_u *)cs_cmds[idx].name;
106     case EXP_SCSCOPE_SUBCMD:
107 	/* Complete with sub-commands of ":scscope": same sub-commands as
108 	 * ":cscope" but skip commands which don't support split windows */
109 	for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
110 	    if (cs_cmds[i].cansplit)
111 		if (current_idx++ == idx)
112 		    break;
113 	return (char_u *)cs_cmds[i].name;
114     case EXP_CSCOPE_FIND:
115 	{
116 	    const char *query_type[] =
117 	    {
118 		"a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
119 	    };
120 
121 	    /* Complete with query type of ":cscope find {query_type}".
122 	     * {query_type} can be letters (c, d, ... a) or numbers (0, 1,
123 	     * ..., 9) but only complete with letters, since numbers are
124 	     * redundant. */
125 	    return (char_u *)query_type[idx];
126 	}
127     case EXP_CSCOPE_KILL:
128 	{
129 	    static char	connection[5];
130 
131 	    /* ":cscope kill" accepts connection numbers or partial names of
132 	     * the pathname of the cscope database as argument.  Only complete
133 	     * with connection numbers. -1 can also be used to kill all
134 	     * connections. */
135 	    for (i = 0, current_idx = 0; i < csinfo_size; i++)
136 	    {
137 		if (csinfo[i].fname == NULL)
138 		    continue;
139 		if (current_idx++ == idx)
140 		{
141 		    vim_snprintf(connection, sizeof(connection), "%d", i);
142 		    return (char_u *)connection;
143 		}
144 	    }
145 	    return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
146 	}
147     default:
148 	return NULL;
149     }
150 }
151 
152 /*
153  * Handle command line completion for :cscope command.
154  */
155     void
156 set_context_in_cscope_cmd(
157     expand_T	*xp,
158     char_u	*arg,
159     cmdidx_T	cmdidx)
160 {
161     char_u	*p;
162 
163     /* Default: expand subcommands */
164     xp->xp_context = EXPAND_CSCOPE;
165     xp->xp_pattern = arg;
166     expand_what = (cmdidx == CMD_scscope)
167 			? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
168 
169     /* (part of) subcommand already typed */
170     if (*arg != NUL)
171     {
172 	p = skiptowhite(arg);
173 	if (*p != NUL)		    /* past first word */
174 	{
175 	    xp->xp_pattern = skipwhite(p);
176 	    if (*skiptowhite(xp->xp_pattern) != NUL)
177 		xp->xp_context = EXPAND_NOTHING;
178 	    else if (STRNICMP(arg, "add", p - arg) == 0)
179 		xp->xp_context = EXPAND_FILES;
180 	    else if (STRNICMP(arg, "kill", p - arg) == 0)
181 		expand_what = EXP_CSCOPE_KILL;
182 	    else if (STRNICMP(arg, "find", p - arg) == 0)
183 		expand_what = EXP_CSCOPE_FIND;
184 	    else
185 		xp->xp_context = EXPAND_NOTHING;
186 	}
187     }
188 }
189 
190 /*
191  * Find the command, print help if invalid, and then call the corresponding
192  * command function.
193  */
194     static void
195 do_cscope_general(
196     exarg_T	*eap,
197     int		make_split UNUSED) /* whether to split window */
198 {
199     cscmd_T *cmdp;
200 
201     if ((cmdp = cs_lookup_cmd(eap)) == NULL)
202     {
203 	cs_help(eap);
204 	return;
205     }
206 
207     if (make_split)
208     {
209 	if (!cmdp->cansplit)
210 	{
211 	    (void)msg_puts(_("This cscope command does not support splitting the window.\n"));
212 	    return;
213 	}
214 	postponed_split = -1;
215 	postponed_split_flags = cmdmod.split;
216 	postponed_split_tab = cmdmod.tab;
217     }
218 
219     cmdp->func(eap);
220 
221     postponed_split_flags = 0;
222     postponed_split_tab = 0;
223 }
224 
225 /*
226  * Implementation of ":cscope" and ":lcscope"
227  */
228     void
229 ex_cscope(exarg_T *eap)
230 {
231     do_cscope_general(eap, FALSE);
232 }
233 
234 /*
235  * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
236  */
237     void
238 ex_scscope(exarg_T *eap)
239 {
240     do_cscope_general(eap, TRUE);
241 }
242 
243 /*
244  * Implementation of ":cstag"
245  */
246     void
247 ex_cstag(exarg_T *eap)
248 {
249     int ret = FALSE;
250 
251     if (*eap->arg == NUL)
252     {
253 	(void)emsg(_("E562: Usage: cstag <ident>"));
254 	return;
255     }
256 
257     switch (p_csto)
258     {
259     case 0 :
260 	if (cs_check_for_connections())
261 	{
262 	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
263 						       FALSE, *eap->cmdlinep);
264 	    if (ret == FALSE)
265 	    {
266 		cs_free_tags();
267 		if (msg_col)
268 		    msg_putchar('\n');
269 
270 		if (cs_check_for_tags())
271 		    ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
272 	    }
273 	}
274 	else if (cs_check_for_tags())
275 	{
276 	    ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
277 	}
278 	break;
279     case 1 :
280 	if (cs_check_for_tags())
281 	{
282 	    ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
283 	    if (ret == FALSE)
284 	    {
285 		if (msg_col)
286 		    msg_putchar('\n');
287 
288 		if (cs_check_for_connections())
289 		{
290 		    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
291 						FALSE, FALSE, *eap->cmdlinep);
292 		    if (ret == FALSE)
293 			cs_free_tags();
294 		}
295 	    }
296 	}
297 	else if (cs_check_for_connections())
298 	{
299 	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
300 						       FALSE, *eap->cmdlinep);
301 	    if (ret == FALSE)
302 		cs_free_tags();
303 	}
304 	break;
305     default :
306 	break;
307     }
308 
309     if (!ret)
310     {
311 	(void)emsg(_("E257: cstag: tag not found"));
312 #if defined(FEAT_QUICKFIX)
313 	g_do_tagpreview = 0;
314 #endif
315     }
316 
317 }
318 
319 
320 /*
321  * This simulates a vim_fgets(), but for cscope, returns the next line
322  * from the cscope output.  should only be called from find_tags()
323  *
324  * returns TRUE if eof, FALSE otherwise
325  */
326     int
327 cs_fgets(char_u *buf, int size)
328 {
329     char *p;
330 
331     if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
332 	return TRUE;
333     vim_strncpy(buf, (char_u *)p, size - 1);
334 
335     return FALSE;
336 } /* cs_fgets */
337 
338 
339 /*
340  * Called only from do_tag(), when popping the tag stack.
341  */
342     void
343 cs_free_tags(void)
344 {
345     cs_manage_matches(NULL, NULL, -1, Free);
346 }
347 
348 
349 /*
350  * Called from do_tag().
351  */
352     void
353 cs_print_tags(void)
354 {
355     cs_manage_matches(NULL, NULL, -1, Print);
356 }
357 
358 
359 /*
360  * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
361  *
362  *		Checks for the existence of a |cscope| connection.  If no
363  *		parameters are specified, then the function returns:
364  *
365  *		0, if cscope was not available (not compiled in), or if there
366  *		are no cscope connections; or
367  *		1, if there is at least one cscope connection.
368  *
369  *		If parameters are specified, then the value of {num}
370  *		determines how existence of a cscope connection is checked:
371  *
372  *		{num}	Description of existence check
373  *		-----	------------------------------
374  *		0	Same as no parameters (e.g., "cscope_connection()").
375  *		1	Ignore {prepend}, and use partial string matches for
376  *			{dbpath}.
377  *		2	Ignore {prepend}, and use exact string matches for
378  *			{dbpath}.
379  *		3	Use {prepend}, use partial string matches for both
380  *			{dbpath} and {prepend}.
381  *		4	Use {prepend}, use exact string matches for both
382  *			{dbpath} and {prepend}.
383  *
384  *		Note: All string comparisons are case sensitive!
385  */
386 #if defined(FEAT_EVAL) || defined(PROTO)
387     int
388 cs_connection(int num, char_u *dbpath, char_u *ppath)
389 {
390     int i;
391 
392     if (num < 0 || num > 4 || (num > 0 && !dbpath))
393 	return FALSE;
394 
395     for (i = 0; i < csinfo_size; i++)
396     {
397 	if (!csinfo[i].fname)
398 	    continue;
399 
400 	if (num == 0)
401 	    return TRUE;
402 
403 	switch (num)
404 	{
405 	case 1:
406 	    if (strstr(csinfo[i].fname, (char *)dbpath))
407 		return TRUE;
408 	    break;
409 	case 2:
410 	    if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
411 		return TRUE;
412 	    break;
413 	case 3:
414 	    if (strstr(csinfo[i].fname, (char *)dbpath)
415 		    && ((!ppath && !csinfo[i].ppath)
416 			|| (ppath
417 			    && csinfo[i].ppath
418 			    && strstr(csinfo[i].ppath, (char *)ppath))))
419 		return TRUE;
420 	    break;
421 	case 4:
422 	    if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
423 		    && ((!ppath && !csinfo[i].ppath)
424 			|| (ppath
425 			    && csinfo[i].ppath
426 			    && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
427 		return TRUE;
428 	    break;
429 	}
430     }
431 
432     return FALSE;
433 } /* cs_connection */
434 #endif
435 
436 
437 /*
438  * PRIVATE functions
439  ****************************************************************************/
440 
441 /*
442  * Add cscope database or a directory name (to look for cscope.out)
443  * to the cscope connection list.
444  */
445     static int
446 cs_add(exarg_T *eap UNUSED)
447 {
448     char *fname, *ppath, *flags = NULL;
449 
450     if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
451     {
452 	cs_usage_msg(Add);
453 	return CSCOPE_FAILURE;
454     }
455     if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
456 	flags = strtok((char *)NULL, (const char *)" ");
457 
458     return cs_add_common(fname, ppath, flags);
459 }
460 
461     static void
462 cs_stat_emsg(char *fname)
463 {
464     char *stat_emsg = _("E563: stat(%s) error: %d");
465     char *buf = alloc(strlen(stat_emsg) + MAXPATHL + 10);
466 
467     if (buf != NULL)
468     {
469 	(void)sprintf(buf, stat_emsg, fname, errno);
470 	(void)emsg(buf);
471 	vim_free(buf);
472     }
473     else
474 	(void)emsg(_("E563: stat error"));
475 }
476 
477 
478 /*
479  * The common routine to add a new cscope connection.  Called by
480  * cs_add() and cs_reset().  I really don't like to do this, but this
481  * routine uses a number of goto statements.
482  */
483     static int
484 cs_add_common(
485     char *arg1,	    /* filename - may contain environment variables */
486     char *arg2,	    /* prepend path - may contain environment variables */
487     char *flags)
488 {
489     stat_T	statbuf;
490     int		ret;
491     char	*fname = NULL;
492     char	*fname2 = NULL;
493     char	*ppath = NULL;
494     int		i;
495     int		len;
496     int		usedlen = 0;
497     char_u	*fbuf = NULL;
498 
499     /* get the filename (arg1), expand it, and try to stat it */
500     if ((fname = alloc(MAXPATHL + 1)) == NULL)
501 	goto add_err;
502 
503     expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
504     len = (int)STRLEN(fname);
505     fbuf = (char_u *)fname;
506     (void)modify_fname((char_u *)":p", FALSE, &usedlen,
507 					      (char_u **)&fname, &fbuf, &len);
508     if (fname == NULL)
509 	goto add_err;
510     fname = (char *)vim_strnsave((char_u *)fname, len);
511     vim_free(fbuf);
512 
513     ret = mch_stat(fname, &statbuf);
514     if (ret < 0)
515     {
516 staterr:
517 	if (p_csverbose)
518 	    cs_stat_emsg(fname);
519 	goto add_err;
520     }
521 
522     /* get the prepend path (arg2), expand it, and try to stat it */
523     if (arg2 != NULL)
524     {
525 	stat_T	    statbuf2;
526 
527 	if ((ppath = alloc(MAXPATHL + 1)) == NULL)
528 	    goto add_err;
529 
530 	expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
531 	ret = mch_stat(ppath, &statbuf2);
532 	if (ret < 0)
533 	    goto staterr;
534     }
535 
536     /* if filename is a directory, append the cscope database name to it */
537     if (S_ISDIR(statbuf.st_mode))
538     {
539 	fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
540 	if (fname2 == NULL)
541 	    goto add_err;
542 
543 	while (fname[strlen(fname)-1] == '/'
544 #ifdef MSWIN
545 		|| fname[strlen(fname)-1] == '\\'
546 #endif
547 		)
548 	{
549 	    fname[strlen(fname)-1] = '\0';
550 	    if (fname[0] == '\0')
551 		break;
552 	}
553 	if (fname[0] == '\0')
554 	    (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
555 	else
556 	    (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
557 
558 	ret = mch_stat(fname2, &statbuf);
559 	if (ret < 0)
560 	{
561 	    if (p_csverbose)
562 		cs_stat_emsg(fname2);
563 	    goto add_err;
564 	}
565 
566 	i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
567     }
568     else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
569     {
570 	i = cs_insert_filelist(fname, ppath, flags, &statbuf);
571     }
572     else
573     {
574 	if (p_csverbose)
575 	    (void)semsg(
576 		_("E564: %s is not a directory or a valid cscope database"),
577 		fname);
578 	goto add_err;
579     }
580 
581     if (i != -1)
582     {
583 	if (cs_create_connection(i) == CSCOPE_FAILURE
584 		|| cs_read_prompt(i) == CSCOPE_FAILURE)
585 	{
586 	    cs_release_csp(i, TRUE);
587 	    goto add_err;
588 	}
589 
590 	if (p_csverbose)
591 	{
592 	    msg_clr_eos();
593 	    (void)smsg_attr(HL_ATTR(HLF_R),
594 			    _("Added cscope database %s"),
595 			    csinfo[i].fname);
596 	}
597     }
598 
599     vim_free(fname);
600     vim_free(fname2);
601     vim_free(ppath);
602     return CSCOPE_SUCCESS;
603 
604 add_err:
605     vim_free(fname2);
606     vim_free(fname);
607     vim_free(ppath);
608     return CSCOPE_FAILURE;
609 } /* cs_add_common */
610 
611 
612     static int
613 cs_check_for_connections(void)
614 {
615     return (cs_cnt_connections() > 0);
616 } /* cs_check_for_connections */
617 
618 
619     static int
620 cs_check_for_tags(void)
621 {
622     return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
623 } /* cs_check_for_tags */
624 
625 
626 /*
627  * Count the number of cscope connections.
628  */
629     static int
630 cs_cnt_connections(void)
631 {
632     short i;
633     short cnt = 0;
634 
635     for (i = 0; i < csinfo_size; i++)
636     {
637 	if (csinfo[i].fname != NULL)
638 	    cnt++;
639     }
640     return cnt;
641 } /* cs_cnt_connections */
642 
643     static void
644 cs_reading_emsg(
645     int idx)	/* connection index */
646 {
647     semsg(_("E262: error reading cscope connection %d"), idx);
648 }
649 
650 #define	CSREAD_BUFSIZE	2048
651 /*
652  * Count the number of matches for a given cscope connection.
653  */
654     static int
655 cs_cnt_matches(int idx)
656 {
657     char *stok;
658     char *buf;
659     int nlines = 0;
660 
661     buf = alloc(CSREAD_BUFSIZE);
662     if (buf == NULL)
663 	return 0;
664     for (;;)
665     {
666 	if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
667 	{
668 	    if (feof(csinfo[idx].fr_fp))
669 		errno = EIO;
670 
671 	    cs_reading_emsg(idx);
672 
673 	    vim_free(buf);
674 	    return -1;
675 	}
676 
677 	/*
678 	 * If the database is out of date, or there's some other problem,
679 	 * cscope will output error messages before the number-of-lines output.
680 	 * Display/discard any output that doesn't match what we want.
681 	 * Accept "\S*cscope: X lines", also matches "mlcscope".
682 	 * Bail out for the "Unable to search" error.
683 	 */
684 	if (strstr((const char *)buf, "Unable to search database") != NULL)
685 	    break;
686 	if ((stok = strtok(buf, (const char *)" ")) == NULL)
687 	    continue;
688 	if (strstr((const char *)stok, "cscope:") == NULL)
689 	    continue;
690 
691 	if ((stok = strtok(NULL, (const char *)" ")) == NULL)
692 	    continue;
693 	nlines = atoi(stok);
694 	if (nlines < 0)
695 	{
696 	    nlines = 0;
697 	    break;
698 	}
699 
700 	if ((stok = strtok(NULL, (const char *)" ")) == NULL)
701 	    continue;
702 	if (strncmp((const char *)stok, "lines", 5))
703 	    continue;
704 
705 	break;
706     }
707 
708     vim_free(buf);
709     return nlines;
710 } /* cs_cnt_matches */
711 
712 
713 /*
714  * Creates the actual cscope command query from what the user entered.
715  */
716     static char *
717 cs_create_cmd(char *csoption, char *pattern)
718 {
719     char *cmd;
720     short search;
721     char *pat;
722 
723     switch (csoption[0])
724     {
725     case '0' : case 's' :
726 	search = 0;
727 	break;
728     case '1' : case 'g' :
729 	search = 1;
730 	break;
731     case '2' : case 'd' :
732 	search = 2;
733 	break;
734     case '3' : case 'c' :
735 	search = 3;
736 	break;
737     case '4' : case 't' :
738 	search = 4;
739 	break;
740     case '6' : case 'e' :
741 	search = 6;
742 	break;
743     case '7' : case 'f' :
744 	search = 7;
745 	break;
746     case '8' : case 'i' :
747 	search = 8;
748 	break;
749     case '9' : case 'a' :
750 	search = 9;
751 	break;
752     default :
753 	(void)emsg(_("E561: unknown cscope search type"));
754 	cs_usage_msg(Find);
755 	return NULL;
756     }
757 
758     /* Skip white space before the patter, except for text and pattern search,
759      * they may want to use the leading white space. */
760     pat = pattern;
761     if (search != 4 && search != 6)
762 	while VIM_ISWHITE(*pat)
763 	    ++pat;
764 
765     if ((cmd = alloc(strlen(pat) + 2)) == NULL)
766 	return NULL;
767 
768     (void)sprintf(cmd, "%d%s", search, pat);
769 
770     return cmd;
771 } /* cs_create_cmd */
772 
773 
774 /*
775  * This piece of code was taken/adapted from nvi.  do we need to add
776  * the BSD license notice?
777  */
778     static int
779 cs_create_connection(int i)
780 {
781 #ifdef UNIX
782     int		to_cs[2], from_cs[2];
783 #endif
784     int		len;
785     char	*prog, *cmd, *ppath = NULL;
786 #ifdef MSWIN
787     int		fd;
788     SECURITY_ATTRIBUTES sa;
789     PROCESS_INFORMATION pi;
790     STARTUPINFO si;
791     BOOL	pipe_stdin = FALSE, pipe_stdout = FALSE;
792     HANDLE	stdin_rd, stdout_rd;
793     HANDLE	stdout_wr, stdin_wr;
794     BOOL	created;
795 # if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
796 #  define OPEN_OH_ARGTYPE intptr_t
797 # else
798 #  define OPEN_OH_ARGTYPE long
799 # endif
800 #endif
801 
802 #if defined(UNIX)
803     /*
804      * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
805      * from_cs[0] and writes to to_cs[1].
806      */
807     to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
808     if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
809     {
810 	(void)emsg(_("E566: Could not create cscope pipes"));
811 err_closing:
812 	if (to_cs[0] != -1)
813 	    (void)close(to_cs[0]);
814 	if (to_cs[1] != -1)
815 	    (void)close(to_cs[1]);
816 	if (from_cs[0] != -1)
817 	    (void)close(from_cs[0]);
818 	if (from_cs[1] != -1)
819 	    (void)close(from_cs[1]);
820 	return CSCOPE_FAILURE;
821     }
822 
823     switch (csinfo[i].pid = fork())
824     {
825     case -1:
826 	(void)emsg(_("E622: Could not fork for cscope"));
827 	goto err_closing;
828     case 0:				/* child: run cscope. */
829 	if (dup2(to_cs[0], STDIN_FILENO) == -1)
830 	    PERROR("cs_create_connection 1");
831 	if (dup2(from_cs[1], STDOUT_FILENO) == -1)
832 	    PERROR("cs_create_connection 2");
833 	if (dup2(from_cs[1], STDERR_FILENO) == -1)
834 	    PERROR("cs_create_connection 3");
835 
836 	/* close unused */
837 	(void)close(to_cs[1]);
838 	(void)close(from_cs[0]);
839 #else
840 	/* MSWIN */
841 	/* Create pipes to communicate with cscope */
842 	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
843 	sa.bInheritHandle = TRUE;
844 	sa.lpSecurityDescriptor = NULL;
845 
846 	if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
847 		|| !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
848 	{
849 	    (void)emsg(_("E566: Could not create cscope pipes"));
850 err_closing:
851 	    if (pipe_stdin)
852 	    {
853 		CloseHandle(stdin_rd);
854 		CloseHandle(stdin_wr);
855 	    }
856 	    if (pipe_stdout)
857 	    {
858 		CloseHandle(stdout_rd);
859 		CloseHandle(stdout_wr);
860 	    }
861 	    return CSCOPE_FAILURE;
862 	}
863 #endif
864 	/* expand the cscope exec for env var's */
865 	if ((prog = alloc(MAXPATHL + 1)) == NULL)
866 	{
867 #ifdef UNIX
868 	    return CSCOPE_FAILURE;
869 #else
870 	    /* MSWIN */
871 	    goto err_closing;
872 #endif
873 	}
874 	expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
875 
876 	/* alloc space to hold the cscope command */
877 	len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
878 	if (csinfo[i].ppath)
879 	{
880 	    /* expand the prepend path for env var's */
881 	    if ((ppath = alloc(MAXPATHL + 1)) == NULL)
882 	    {
883 		vim_free(prog);
884 #ifdef UNIX
885 		return CSCOPE_FAILURE;
886 #else
887 		/* MSWIN */
888 		goto err_closing;
889 #endif
890 	    }
891 	    expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
892 
893 	    len += (int)strlen(ppath);
894 	}
895 
896 	if (csinfo[i].flags)
897 	    len += (int)strlen(csinfo[i].flags);
898 
899 	if ((cmd = alloc(len)) == NULL)
900 	{
901 	    vim_free(prog);
902 	    vim_free(ppath);
903 #ifdef UNIX
904 	    return CSCOPE_FAILURE;
905 #else
906 	    /* MSWIN */
907 	    goto err_closing;
908 #endif
909 	}
910 
911 	/* run the cscope command; is there execl for non-unix systems? */
912 #if defined(UNIX)
913 	(void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
914 #else
915 	/* MSWIN */
916 	(void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
917 #endif
918 	if (csinfo[i].ppath != NULL)
919 	{
920 	    (void)strcat(cmd, " -P");
921 	    (void)strcat(cmd, csinfo[i].ppath);
922 	}
923 	if (csinfo[i].flags != NULL)
924 	{
925 	    (void)strcat(cmd, " ");
926 	    (void)strcat(cmd, csinfo[i].flags);
927 	}
928 # ifdef UNIX
929       /* on Win32 we still need prog */
930 	vim_free(prog);
931 # endif
932 	vim_free(ppath);
933 
934 #if defined(UNIX)
935 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
936 	/* Change our process group to avoid cscope receiving SIGWINCH. */
937 #  if defined(HAVE_SETSID)
938 	(void)setsid();
939 #  else
940 	if (setpgid(0, 0) == -1)
941 	    PERROR(_("cs_create_connection setpgid failed"));
942 #  endif
943 # endif
944 	if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
945 	    PERROR(_("cs_create_connection exec failed"));
946 
947 	exit(127);
948 	/* NOTREACHED */
949     default:	/* parent. */
950 	/*
951 	 * Save the file descriptors for later duplication, and
952 	 * reopen as streams.
953 	 */
954 	if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
955 	    PERROR(_("cs_create_connection: fdopen for to_fp failed"));
956 	if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
957 	    PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
958 
959 	/* close unused */
960 	(void)close(to_cs[0]);
961 	(void)close(from_cs[1]);
962 
963 	break;
964     }
965 
966 #else
967     /* MSWIN */
968     /* Create a new process to run cscope and use pipes to talk with it */
969     GetStartupInfo(&si);
970     si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
971     si.wShowWindow = SW_HIDE;  /* Hide child application window */
972     si.hStdOutput = stdout_wr;
973     si.hStdError  = stdout_wr;
974     si.hStdInput  = stdin_rd;
975     created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
976 							NULL, NULL, &si, &pi);
977     vim_free(prog);
978     vim_free(cmd);
979 
980     if (!created)
981     {
982 	PERROR(_("cs_create_connection exec failed"));
983 	(void)emsg(_("E623: Could not spawn cscope process"));
984 	goto err_closing;
985     }
986     /* else */
987     csinfo[i].pid = pi.dwProcessId;
988     csinfo[i].hProc = pi.hProcess;
989     CloseHandle(pi.hThread);
990 
991     /* TODO - tidy up after failure to create files on pipe handles. */
992     if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
993 						      _O_TEXT|_O_APPEND)) < 0)
994 	    || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
995 	PERROR(_("cs_create_connection: fdopen for to_fp failed"));
996     if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
997 						      _O_TEXT|_O_RDONLY)) < 0)
998 	    || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
999 	PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1000 
1001     /* Close handles for file descriptors inherited by the cscope process */
1002     CloseHandle(stdin_rd);
1003     CloseHandle(stdout_wr);
1004 
1005 #endif /* !UNIX */
1006 
1007     return CSCOPE_SUCCESS;
1008 } /* cs_create_connection */
1009 
1010 
1011 /*
1012  * Query cscope using command line interface.  Parse the output and use tselect
1013  * to allow choices.  Like Nvi, creates a pipe to send to/from query/cscope.
1014  *
1015  * returns TRUE if we jump to a tag or abort, FALSE if not.
1016  */
1017     static int
1018 cs_find(exarg_T *eap)
1019 {
1020     char *opt, *pat;
1021     int i;
1022 
1023     if (cs_check_for_connections() == FALSE)
1024     {
1025 	(void)emsg(_("E567: no cscope connections"));
1026 	return FALSE;
1027     }
1028 
1029     if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1030     {
1031 	cs_usage_msg(Find);
1032 	return FALSE;
1033     }
1034 
1035     pat = opt + strlen(opt) + 1;
1036     if (pat >= (char *)eap->arg + eap_arg_len)
1037     {
1038 	cs_usage_msg(Find);
1039 	return FALSE;
1040     }
1041 
1042     /*
1043      * Let's replace the NULs written by strtok() with spaces - we need the
1044      * spaces to correctly display the quickfix/location list window's title.
1045      */
1046     for (i = 0; i < eap_arg_len; ++i)
1047 	if (NUL == eap->arg[i])
1048 	    eap->arg[i] = ' ';
1049 
1050     return cs_find_common(opt, pat, eap->forceit, TRUE,
1051 				  eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
1052 } /* cs_find */
1053 
1054 
1055 /*
1056  * Common code for cscope find, shared by cs_find() and ex_cstag().
1057  */
1058     static int
1059 cs_find_common(
1060     char *opt,
1061     char *pat,
1062     int forceit,
1063     int verbose,
1064     int	use_ll UNUSED,
1065     char_u *cmdline UNUSED)
1066 {
1067     int i;
1068     char *cmd;
1069     int *nummatches;
1070     int totmatches;
1071 #ifdef FEAT_QUICKFIX
1072     char cmdletter;
1073     char *qfpos;
1074 
1075     /* get cmd letter */
1076     switch (opt[0])
1077     {
1078     case '0' :
1079 	cmdletter = 's';
1080 	break;
1081     case '1' :
1082 	cmdletter = 'g';
1083 	break;
1084     case '2' :
1085 	cmdletter = 'd';
1086 	break;
1087     case '3' :
1088 	cmdletter = 'c';
1089 	break;
1090     case '4' :
1091 	cmdletter = 't';
1092 	break;
1093     case '6' :
1094 	cmdletter = 'e';
1095 	break;
1096     case '7' :
1097 	cmdletter = 'f';
1098 	break;
1099     case '8' :
1100 	cmdletter = 'i';
1101 	break;
1102     case '9' :
1103 	cmdletter = 'a';
1104 	break;
1105     default :
1106 	cmdletter = opt[0];
1107     }
1108 
1109     qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1110     if (qfpos != NULL)
1111     {
1112 	qfpos++;
1113 	/* next symbol must be + or - */
1114 	if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1115 	{
1116 	    char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1117 	    char *buf = alloc(strlen(nf));
1118 
1119 	    /* strlen will be enough because we use chars */
1120 	    if (buf != NULL)
1121 	    {
1122 		sprintf(buf, nf, *qfpos, *(qfpos-1));
1123 		(void)emsg(buf);
1124 		vim_free(buf);
1125 	    }
1126 	    return FALSE;
1127 	}
1128 
1129 	if (*qfpos != '0'
1130 		&& apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1131 					       curbuf->b_fname, TRUE, curbuf))
1132 	{
1133 # ifdef FEAT_EVAL
1134 	    if (aborting())
1135 		return FALSE;
1136 # endif
1137 	}
1138     }
1139 #endif
1140 
1141     /* create the actual command to send to cscope */
1142     cmd = cs_create_cmd(opt, pat);
1143     if (cmd == NULL)
1144 	return FALSE;
1145 
1146     nummatches = ALLOC_MULT(int, csinfo_size);
1147     if (nummatches == NULL)
1148     {
1149 	vim_free(cmd);
1150 	return FALSE;
1151     }
1152 
1153     /* Send query to all open connections, then count the total number
1154      * of matches so we can alloc all in one swell foop. */
1155     for (i = 0; i < csinfo_size; i++)
1156 	nummatches[i] = 0;
1157     totmatches = 0;
1158     for (i = 0; i < csinfo_size; i++)
1159     {
1160 	if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
1161 	    continue;
1162 
1163 	/* send cmd to cscope */
1164 	(void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1165 	(void)fflush(csinfo[i].to_fp);
1166 
1167 	nummatches[i] = cs_cnt_matches(i);
1168 
1169 	if (nummatches[i] > -1)
1170 	    totmatches += nummatches[i];
1171 
1172 	if (nummatches[i] == 0)
1173 	    (void)cs_read_prompt(i);
1174     }
1175     vim_free(cmd);
1176 
1177     if (totmatches == 0)
1178     {
1179 	char *nf = _("E259: no matches found for cscope query %s of %s");
1180 	char *buf;
1181 
1182 	if (!verbose)
1183 	{
1184 	    vim_free(nummatches);
1185 	    return FALSE;
1186 	}
1187 
1188 	buf = alloc(strlen(opt) + strlen(pat) + strlen(nf));
1189 	if (buf == NULL)
1190 	    (void)emsg(nf);
1191 	else
1192 	{
1193 	    sprintf(buf, nf, opt, pat);
1194 	    (void)emsg(buf);
1195 	    vim_free(buf);
1196 	}
1197 	vim_free(nummatches);
1198 	return FALSE;
1199     }
1200 
1201 #ifdef FEAT_QUICKFIX
1202     if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
1203     {
1204 	/* fill error list */
1205 	FILE	    *f;
1206 	char_u	    *tmp = vim_tempname('c', TRUE);
1207 	qf_info_T   *qi = NULL;
1208 	win_T	    *wp = NULL;
1209 
1210 	f = mch_fopen((char *)tmp, "w");
1211 	if (f == NULL)
1212 	    semsg(_(e_notopen), tmp);
1213 	else
1214 	{
1215 	    cs_file_results(f, nummatches);
1216 	    fclose(f);
1217 	    if (use_ll)	    /* Use location list */
1218 		wp = curwin;
1219 	    /* '-' starts a new error list */
1220 	    if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1221 					  *qfpos == '-', cmdline, NULL) > 0)
1222 	    {
1223 		if (postponed_split != 0)
1224 		{
1225 		    (void)win_split(postponed_split > 0 ? postponed_split : 0,
1226 						       postponed_split_flags);
1227 		    RESET_BINDING(curwin);
1228 		    postponed_split = 0;
1229 		}
1230 
1231 		apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1232 					       curbuf->b_fname, TRUE, curbuf);
1233 		if (use_ll)
1234 		    /*
1235 		     * In the location list window, use the displayed location
1236 		     * list. Otherwise, use the location list for the window.
1237 		     */
1238 		    qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1239 			?  wp->w_llist_ref : wp->w_llist;
1240 		qf_jump(qi, 0, 0, forceit);
1241 	    }
1242 	}
1243 	mch_remove(tmp);
1244 	vim_free(tmp);
1245 	vim_free(nummatches);
1246 	return TRUE;
1247     }
1248     else
1249 #endif /* FEAT_QUICKFIX */
1250     {
1251 	char **matches = NULL, **contexts = NULL;
1252 	int matched = 0;
1253 
1254 	/* read output */
1255 	cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1256 							 &contexts, &matched);
1257 	vim_free(nummatches);
1258 	if (matches == NULL)
1259 	    return FALSE;
1260 
1261 	(void)cs_manage_matches(matches, contexts, matched, Store);
1262 
1263 	return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1264     }
1265 
1266 } /* cs_find_common */
1267 
1268 /*
1269  * Print help.
1270  */
1271     static int
1272 cs_help(exarg_T *eap UNUSED)
1273 {
1274     cscmd_T *cmdp = cs_cmds;
1275 
1276     (void)msg_puts(_("cscope commands:\n"));
1277     while (cmdp->name != NULL)
1278     {
1279 	char *help = _(cmdp->help);
1280 	int  space_cnt = 30 - vim_strsize((char_u *)help);
1281 
1282 	/* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1283 	if (space_cnt < 0)
1284 	    space_cnt = 0;
1285 	(void)smsg(_("%-5s: %s%*s (Usage: %s)"),
1286 				      cmdp->name,
1287 				      help, space_cnt, " ",
1288 				      cmdp->usage);
1289 	if (strcmp(cmdp->name, "find") == 0)
1290 	    msg_puts(_("\n"
1291 		       "       a: Find assignments to this symbol\n"
1292 		       "       c: Find functions calling this function\n"
1293 		       "       d: Find functions called by this function\n"
1294 		       "       e: Find this egrep pattern\n"
1295 		       "       f: Find this file\n"
1296 		       "       g: Find this definition\n"
1297 		       "       i: Find files #including this file\n"
1298 		       "       s: Find this C symbol\n"
1299 		       "       t: Find this text string\n"));
1300 
1301 	cmdp++;
1302     }
1303 
1304     wait_return(TRUE);
1305     return 0;
1306 } /* cs_help */
1307 
1308 
1309     static void
1310 clear_csinfo(int i)
1311 {
1312     csinfo[i].fname  = NULL;
1313     csinfo[i].ppath  = NULL;
1314     csinfo[i].flags  = NULL;
1315 #if defined(UNIX)
1316     csinfo[i].st_dev = (dev_t)0;
1317     csinfo[i].st_ino = (ino_t)0;
1318 #else
1319     csinfo[i].nVolume = 0;
1320     csinfo[i].nIndexHigh = 0;
1321     csinfo[i].nIndexLow = 0;
1322 #endif
1323     csinfo[i].pid    = 0;
1324     csinfo[i].fr_fp  = NULL;
1325     csinfo[i].to_fp  = NULL;
1326 #if defined(MSWIN)
1327     csinfo[i].hProc = NULL;
1328 #endif
1329 }
1330 
1331 #ifndef UNIX
1332     static char *
1333 GetWin32Error(void)
1334 {
1335     char *msg = NULL;
1336     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1337 	    NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1338     if (msg != NULL)
1339     {
1340 	/* remove trailing \r\n */
1341 	char *pcrlf = strstr(msg, "\r\n");
1342 	if (pcrlf != NULL)
1343 	    *pcrlf = '\0';
1344     }
1345     return msg;
1346 }
1347 #endif
1348 
1349 /*
1350  * Insert a new cscope database filename into the filelist.
1351  */
1352     static int
1353 cs_insert_filelist(
1354     char *fname,
1355     char *ppath,
1356     char *flags,
1357     stat_T *sb UNUSED)
1358 {
1359     short	i, j;
1360 #ifndef UNIX
1361     BY_HANDLE_FILE_INFORMATION bhfi;
1362 
1363     switch (win32_fileinfo((char_u *)fname, &bhfi))
1364     {
1365 	case FILEINFO_ENC_FAIL:		/* enc_to_utf16() failed */
1366 	case FILEINFO_READ_FAIL:	/* CreateFile() failed */
1367 	    if (p_csverbose)
1368 	    {
1369 		char *cant_msg = _("E625: cannot open cscope database: %s");
1370 		char *winmsg = GetWin32Error();
1371 
1372 		if (winmsg != NULL)
1373 		{
1374 		    (void)semsg(cant_msg, winmsg);
1375 		    LocalFree(winmsg);
1376 		}
1377 		else
1378 		    /* subst filename if can't get error text */
1379 		    (void)semsg(cant_msg, fname);
1380 	    }
1381 	    return -1;
1382 
1383 	case FILEINFO_INFO_FAIL:    /* GetFileInformationByHandle() failed */
1384 	    if (p_csverbose)
1385 		(void)emsg(_("E626: cannot get cscope database information"));
1386 	    return -1;
1387     }
1388 #endif
1389 
1390     i = -1; /* can be set to the index of an empty item in csinfo */
1391     for (j = 0; j < csinfo_size; j++)
1392     {
1393 	if (csinfo[j].fname != NULL
1394 #if defined(UNIX)
1395 	    && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1396 #else
1397 	    // compare pathnames first
1398 	    && ((fullpathcmp((char_u *)csinfo[j].fname,
1399 			(char_u *)fname, FALSE, TRUE) & FPC_SAME)
1400 		// test index file attributes too
1401 		|| (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1402 		    && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1403 		    && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1404 #endif
1405 	    )
1406 	{
1407 	    if (p_csverbose)
1408 		(void)emsg(_("E568: duplicate cscope database not added"));
1409 	    return -1;
1410 	}
1411 
1412 	if (csinfo[j].fname == NULL && i == -1)
1413 	    i = j; /* remember first empty entry */
1414     }
1415 
1416     if (i == -1)
1417     {
1418 	i = csinfo_size;
1419 	if (csinfo_size == 0)
1420 	{
1421 	    /* First time allocation: allocate only 1 connection. It should
1422 	     * be enough for most users.  If more is needed, csinfo will be
1423 	     * reallocated. */
1424 	    csinfo_size = 1;
1425 	    csinfo = ALLOC_CLEAR_ONE(csinfo_T);
1426 	}
1427 	else
1428 	{
1429 	    csinfo_T *t_csinfo = csinfo;
1430 
1431 	    /* Reallocate space for more connections. */
1432 	    csinfo_size *= 2;
1433 	    csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1434 	    if (csinfo == NULL)
1435 	    {
1436 		vim_free(t_csinfo);
1437 		csinfo_size = 0;
1438 	    }
1439 	}
1440 	if (csinfo == NULL)
1441 	    return -1;
1442 	for (j = csinfo_size/2; j < csinfo_size; j++)
1443 	    clear_csinfo(j);
1444     }
1445 
1446     if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
1447 	return -1;
1448 
1449     (void)strcpy(csinfo[i].fname, (const char *)fname);
1450 
1451     if (ppath != NULL)
1452     {
1453 	if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
1454 	{
1455 	    VIM_CLEAR(csinfo[i].fname);
1456 	    return -1;
1457 	}
1458 	(void)strcpy(csinfo[i].ppath, (const char *)ppath);
1459     } else
1460 	csinfo[i].ppath = NULL;
1461 
1462     if (flags != NULL)
1463     {
1464 	if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
1465 	{
1466 	    VIM_CLEAR(csinfo[i].fname);
1467 	    VIM_CLEAR(csinfo[i].ppath);
1468 	    return -1;
1469 	}
1470 	(void)strcpy(csinfo[i].flags, (const char *)flags);
1471     } else
1472 	csinfo[i].flags = NULL;
1473 
1474 #if defined(UNIX)
1475     csinfo[i].st_dev = sb->st_dev;
1476     csinfo[i].st_ino = sb->st_ino;
1477 
1478 #else
1479     csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1480     csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1481     csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1482 #endif
1483     return i;
1484 } /* cs_insert_filelist */
1485 
1486 
1487 /*
1488  * Find cscope command in command table.
1489  */
1490     static cscmd_T *
1491 cs_lookup_cmd(exarg_T *eap)
1492 {
1493     cscmd_T *cmdp;
1494     char *stok;
1495     size_t len;
1496 
1497     if (eap->arg == NULL)
1498 	return NULL;
1499 
1500     /* Store length of eap->arg before it gets modified by strtok(). */
1501     eap_arg_len = (int)STRLEN(eap->arg);
1502 
1503     if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1504 	return NULL;
1505 
1506     len = strlen(stok);
1507     for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1508     {
1509 	if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1510 	    return (cmdp);
1511     }
1512     return NULL;
1513 } /* cs_lookup_cmd */
1514 
1515 
1516 /*
1517  * Nuke em.
1518  */
1519     static int
1520 cs_kill(exarg_T *eap UNUSED)
1521 {
1522     char *stok;
1523     short i;
1524 
1525     if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1526     {
1527 	cs_usage_msg(Kill);
1528 	return CSCOPE_FAILURE;
1529     }
1530 
1531     /* only single digit positive and negative integers are allowed */
1532     if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1533 	    || (strlen(stok) < 3 && stok[0] == '-'
1534 					      && VIM_ISDIGIT((int)(stok[1]))))
1535 	i = atoi(stok);
1536     else
1537     {
1538 	/* It must be part of a name.  We will try to find a match
1539 	 * within all the names in the csinfo data structure
1540 	 */
1541 	for (i = 0; i < csinfo_size; i++)
1542 	{
1543 	    if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1544 		break;
1545 	}
1546     }
1547 
1548     if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
1549     {
1550 	if (p_csverbose)
1551 	    (void)semsg(_("E261: cscope connection %s not found"), stok);
1552     }
1553     else
1554     {
1555 	if (i == -1)
1556 	{
1557 	    for (i = 0; i < csinfo_size; i++)
1558 	    {
1559 		if (csinfo[i].fname)
1560 		    cs_kill_execute(i, csinfo[i].fname);
1561 	    }
1562 	}
1563 	else
1564 	    cs_kill_execute(i, stok);
1565     }
1566 
1567     return 0;
1568 } /* cs_kill */
1569 
1570 
1571 /*
1572  * Actually kills a specific cscope connection.
1573  */
1574     static void
1575 cs_kill_execute(
1576     int i,	    /* cscope table index */
1577     char *cname)    /* cscope database name */
1578 {
1579     if (p_csverbose)
1580     {
1581 	msg_clr_eos();
1582 	(void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
1583 			_("cscope connection %s closed"), cname);
1584     }
1585     cs_release_csp(i, TRUE);
1586 }
1587 
1588 
1589 /*
1590  * Convert the cscope output into a ctags style entry (as might be found
1591  * in a ctags tags file).  there's one catch though: cscope doesn't tell you
1592  * the type of the tag you are looking for.  for example, in Darren Hiebert's
1593  * ctags (the one that comes with vim), #define's use a line number to find the
1594  * tag in a file while function definitions use a regexp search pattern.
1595  *
1596  * I'm going to always use the line number because cscope does something
1597  * quirky (and probably other things i don't know about):
1598  *
1599  *     if you have "#  define" in your source file, which is
1600  *     perfectly legal, cscope thinks you have "#define".  this
1601  *     will result in a failed regexp search. :(
1602  *
1603  * Besides, even if this particular case didn't happen, the search pattern
1604  * would still have to be modified to escape all the special regular expression
1605  * characters to comply with ctags formatting.
1606  */
1607     static char *
1608 cs_make_vim_style_matches(
1609     char *fname,
1610     char *slno,
1611     char *search,
1612     char *tagstr)
1613 {
1614     /* vim style is ctags:
1615      *
1616      *	    <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1617      *
1618      * but as mentioned above, we'll always use the line number and
1619      * put the search pattern (if one exists) as "extra"
1620      *
1621      * buf is used as part of vim's method of handling tags, and
1622      * (i think) vim frees it when you pop your tags and get replaced
1623      * by new ones on the tag stack.
1624      */
1625     char *buf;
1626     int amt;
1627 
1628     if (search != NULL)
1629     {
1630 	amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
1631 	if ((buf = alloc(amt)) == NULL)
1632 	    return NULL;
1633 
1634 	(void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1635     }
1636     else
1637     {
1638 	amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
1639 	if ((buf = alloc(amt)) == NULL)
1640 	    return NULL;
1641 
1642 	(void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1643     }
1644 
1645     return buf;
1646 } /* cs_make_vim_style_matches */
1647 
1648 
1649 /*
1650  * This is kind of hokey, but i don't see an easy way round this.
1651  *
1652  * Store: keep a ptr to the (malloc'd) memory of matches originally
1653  * generated from cs_find().  the matches are originally lines directly
1654  * from cscope output, but transformed to look like something out of a
1655  * ctags.  see cs_make_vim_style_matches for more details.
1656  *
1657  * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1658  * the next line from the cscope output.  it basically keeps track of which
1659  * lines have been "used" and returns the next one.
1660  *
1661  * Free: frees up everything and resets
1662  *
1663  * Print: prints the tags
1664  */
1665     static char *
1666 cs_manage_matches(
1667     char **matches,
1668     char **contexts,
1669     int totmatches,
1670     mcmd_e cmd)
1671 {
1672     static char **mp = NULL;
1673     static char **cp = NULL;
1674     static int cnt = -1;
1675     static int next = -1;
1676     char *p = NULL;
1677 
1678     switch (cmd)
1679     {
1680     case Store:
1681 	assert(matches != NULL);
1682 	assert(totmatches > 0);
1683 	if (mp != NULL || cp != NULL)
1684 	    (void)cs_manage_matches(NULL, NULL, -1, Free);
1685 	mp = matches;
1686 	cp = contexts;
1687 	cnt = totmatches;
1688 	next = 0;
1689 	break;
1690     case Get:
1691 	if (next >= cnt)
1692 	    return NULL;
1693 
1694 	p = mp[next];
1695 	next++;
1696 	break;
1697     case Free:
1698 	if (mp != NULL)
1699 	{
1700 	    if (cnt > 0)
1701 		while (cnt--)
1702 		{
1703 		    vim_free(mp[cnt]);
1704 		    if (cp != NULL)
1705 			vim_free(cp[cnt]);
1706 		}
1707 	    vim_free(mp);
1708 	    vim_free(cp);
1709 	}
1710 	mp = NULL;
1711 	cp = NULL;
1712 	cnt = 0;
1713 	next = 0;
1714 	break;
1715     case Print:
1716 	cs_print_tags_priv(mp, cp, cnt);
1717 	break;
1718     default:	/* should not reach here */
1719 	iemsg(_("E570: fatal error in cs_manage_matches"));
1720 	return NULL;
1721     }
1722 
1723     return p;
1724 } /* cs_manage_matches */
1725 
1726 
1727 /*
1728  * Parse cscope output.
1729  */
1730     static char *
1731 cs_parse_results(
1732     int cnumber,
1733     char *buf,
1734     int bufsize,
1735     char **context,
1736     char **linenumber,
1737     char **search)
1738 {
1739     int ch;
1740     char *p;
1741     char *name;
1742 
1743     if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1744     {
1745 	if (feof(csinfo[cnumber].fr_fp))
1746 	    errno = EIO;
1747 
1748 	cs_reading_emsg(cnumber);
1749 
1750 	return NULL;
1751     }
1752 
1753 	/* If the line's too long for the buffer, discard it. */
1754     if ((p = strchr(buf, '\n')) == NULL)
1755     {
1756 	while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1757 	    ;
1758 	return NULL;
1759     }
1760     *p = '\0';
1761 
1762     /*
1763      * cscope output is in the following format:
1764      *
1765      *	<filename> <context> <line number> <pattern>
1766      */
1767     if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1768 	return NULL;
1769     if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1770 	return NULL;
1771     if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1772 	return NULL;
1773     *search = *linenumber + strlen(*linenumber) + 1;	/* +1 to skip \0 */
1774 
1775     /* --- nvi ---
1776      * If the file is older than the cscope database, that is,
1777      * the database was built since the file was last modified,
1778      * or there wasn't a search string, use the line number.
1779      */
1780     if (strcmp(*search, "<unknown>") == 0)
1781 	*search = NULL;
1782 
1783     name = cs_resolve_file(cnumber, name);
1784     return name;
1785 }
1786 
1787 #ifdef FEAT_QUICKFIX
1788 /*
1789  * Write cscope find results to file.
1790  */
1791     static void
1792 cs_file_results(FILE *f, int *nummatches_a)
1793 {
1794     int i, j;
1795     char *buf;
1796     char *search, *slno;
1797     char *fullname;
1798     char *cntx;
1799     char *context;
1800 
1801     buf = alloc(CSREAD_BUFSIZE);
1802     if (buf == NULL)
1803 	return;
1804 
1805     for (i = 0; i < csinfo_size; i++)
1806     {
1807 	if (nummatches_a[i] < 1)
1808 	    continue;
1809 
1810 	for (j = 0; j < nummatches_a[i]; j++)
1811 	{
1812 	   if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1813 			   &slno, &search)) == NULL)
1814 	       continue;
1815 
1816 	   context = alloc(strlen(cntx)+5);
1817 	   if (context == NULL)
1818 	       continue;
1819 
1820 	   if (strcmp(cntx, "<global>")==0)
1821 	       strcpy(context, "<<global>>");
1822 	   else
1823 	       sprintf(context, "<<%s>>", cntx);
1824 
1825 	   if (search == NULL)
1826 	       fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1827 	   else
1828 	       fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1829 
1830 	   vim_free(context);
1831 	   vim_free(fullname);
1832 	} /* for all matches */
1833 
1834 	(void)cs_read_prompt(i);
1835 
1836     } /* for all cscope connections */
1837     vim_free(buf);
1838 }
1839 #endif
1840 
1841 /*
1842  * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1843  * into ctags format.
1844  * When there are no matches sets "*matches_p" to NULL.
1845  */
1846     static void
1847 cs_fill_results(
1848     char *tagstr,
1849     int totmatches,
1850     int *nummatches_a,
1851     char ***matches_p,
1852     char ***cntxts_p,
1853     int *matched)
1854 {
1855     int i, j;
1856     char *buf;
1857     char *search, *slno;
1858     int totsofar = 0;
1859     char **matches = NULL;
1860     char **cntxts = NULL;
1861     char *fullname;
1862     char *cntx;
1863 
1864     assert(totmatches > 0);
1865 
1866     buf = alloc(CSREAD_BUFSIZE);
1867     if (buf == NULL)
1868 	return;
1869 
1870     if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
1871 	goto parse_out;
1872     if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
1873 	goto parse_out;
1874 
1875     for (i = 0; i < csinfo_size; i++)
1876     {
1877 	if (nummatches_a[i] < 1)
1878 	    continue;
1879 
1880 	for (j = 0; j < nummatches_a[i]; j++)
1881 	{
1882 	   if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1883 			   &slno, &search)) == NULL)
1884 		continue;
1885 
1886 	    matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1887 							  search, tagstr);
1888 
1889 	    vim_free(fullname);
1890 
1891 	    if (strcmp(cntx, "<global>") == 0)
1892 		cntxts[totsofar] = NULL;
1893 	    else
1894 		/* note: if vim_strsave returns NULL, then the context
1895 		 * will be "<global>", which is misleading.
1896 		 */
1897 		cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1898 
1899 	    if (matches[totsofar] != NULL)
1900 		totsofar++;
1901 
1902 	} /* for all matches */
1903 
1904 	(void)cs_read_prompt(i);
1905 
1906     } /* for all cscope connections */
1907 
1908 parse_out:
1909     if (totsofar == 0)
1910     {
1911 	/* No matches, free the arrays and return NULL in "*matches_p". */
1912 	VIM_CLEAR(matches);
1913 	VIM_CLEAR(cntxts);
1914     }
1915     *matched = totsofar;
1916     *matches_p = matches;
1917     *cntxts_p = cntxts;
1918 
1919     vim_free(buf);
1920 } /* cs_fill_results */
1921 
1922 
1923 /* get the requested path components */
1924     static char *
1925 cs_pathcomponents(char *path)
1926 {
1927     int		i;
1928     char	*s;
1929 
1930     if (p_cspc == 0)
1931 	return path;
1932 
1933     s = path + strlen(path) - 1;
1934     for (i = 0; i < p_cspc; ++i)
1935 	while (s > path && *--s != '/'
1936 #ifdef MSWIN
1937 		&& *--s != '\\'
1938 #endif
1939 		)
1940 	    ;
1941     if ((s > path && *s == '/')
1942 #ifdef MSWIN
1943 	|| (s > path && *s == '\\')
1944 #endif
1945 	    )
1946 	++s;
1947     return s;
1948 }
1949 
1950 /*
1951  * Called from cs_manage_matches().
1952  */
1953     static void
1954 cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
1955 {
1956     char	*buf = NULL;
1957     char	*t_buf;
1958     int		bufsize = 0; /* Track available bufsize */
1959     int		newsize = 0;
1960     char	*ptag;
1961     char	*fname, *lno, *extra, *tbuf;
1962     int		i, idx, num;
1963     char	*globalcntx = "GLOBAL";
1964     char	*cntxformat = " <<%s>>";
1965     char	*context;
1966     char	*cstag_msg = _("Cscope tag: %s");
1967     char	*csfmt_str = "%4d %6s  ";
1968 
1969     assert(num_matches > 0);
1970 
1971     if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
1972 	return;
1973 
1974     strcpy(tbuf, matches[0]);
1975     ptag = strtok(tbuf, "\t");
1976     if (ptag == NULL)
1977     {
1978 	vim_free(tbuf);
1979 	return;
1980     }
1981 
1982     newsize = (int)(strlen(cstag_msg) + strlen(ptag));
1983     buf = alloc(newsize);
1984     if (buf != NULL)
1985     {
1986 	bufsize = newsize;
1987 	(void)sprintf(buf, cstag_msg, ptag);
1988 	msg_puts_attr(buf, HL_ATTR(HLF_T));
1989     }
1990 
1991     vim_free(tbuf);
1992 
1993     msg_puts_attr(_("\n   #   line"), HL_ATTR(HLF_T));    /* strlen is 7 */
1994     msg_advance(msg_col + 2);
1995     msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
1996 
1997     num = 1;
1998     for (i = 0; i < num_matches; i++)
1999     {
2000 	idx = i;
2001 
2002 	/* if we really wanted to, we could avoid this malloc and strcpy
2003 	 * by parsing matches[i] on the fly and placing stuff into buf
2004 	 * directly, but that's too much of a hassle
2005 	 */
2006 	if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
2007 	    continue;
2008 	(void)strcpy(tbuf, matches[idx]);
2009 
2010 	if (strtok(tbuf, (const char *)"\t") == NULL
2011 		|| (fname = strtok(NULL, (const char *)"\t")) == NULL
2012 		|| (lno = strtok(NULL, (const char *)"\t")) == NULL)
2013 	{
2014 	    vim_free(tbuf);
2015 	    continue;
2016 	}
2017 	extra = strtok(NULL, (const char *)"\t");
2018 
2019 	lno[strlen(lno)-2] = '\0';  /* ignore ;" at the end */
2020 
2021 	/* hopefully 'num' (num of matches) will be less than 10^16 */
2022 	newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
2023 	if (bufsize < newsize)
2024 	{
2025 	    t_buf = buf;
2026 	    buf = vim_realloc(buf, newsize);
2027 	    if (buf == NULL)
2028 	    {
2029 		bufsize = 0;
2030 		vim_free(t_buf);
2031 	    }
2032 	    else
2033 		bufsize = newsize;
2034 	}
2035 	if (buf != NULL)
2036 	{
2037 	    /* csfmt_str = "%4d %6s  "; */
2038 	    (void)sprintf(buf, csfmt_str, num, lno);
2039 	    msg_puts_attr(buf, HL_ATTR(HLF_CM));
2040 	}
2041 	msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2042 							      HL_ATTR(HLF_CM));
2043 
2044 	/* compute the required space for the context */
2045 	if (cntxts[idx] != NULL)
2046 	    context = cntxts[idx];
2047 	else
2048 	    context = globalcntx;
2049 	newsize = (int)(strlen(context) + strlen(cntxformat));
2050 
2051 	if (bufsize < newsize)
2052 	{
2053 	    t_buf = buf;
2054 	    buf = vim_realloc(buf, newsize);
2055 	    if (buf == NULL)
2056 	    {
2057 		bufsize = 0;
2058 		vim_free(t_buf);
2059 	    }
2060 	    else
2061 		bufsize = newsize;
2062 	}
2063 	if (buf != NULL)
2064 	{
2065 	    (void)sprintf(buf, cntxformat, context);
2066 
2067 	    /* print the context only if it fits on the same line */
2068 	    if (msg_col + (int)strlen(buf) >= (int)Columns)
2069 		msg_putchar('\n');
2070 	    msg_advance(12);
2071 	    msg_outtrans_long_attr((char_u *)buf, 0);
2072 	    msg_putchar('\n');
2073 	}
2074 	if (extra != NULL)
2075 	{
2076 	    msg_advance(13);
2077 	    msg_outtrans_long_attr((char_u *)extra, 0);
2078 	}
2079 
2080 	vim_free(tbuf); /* only after printing extra due to strtok use */
2081 
2082 	if (msg_col)
2083 	    msg_putchar('\n');
2084 
2085 	ui_breakcheck();
2086 	if (got_int)
2087 	{
2088 	    got_int = FALSE;	/* don't print any more matches */
2089 	    break;
2090 	}
2091 
2092 	num++;
2093     } /* for all matches */
2094 
2095     vim_free(buf);
2096 } /* cs_print_tags_priv */
2097 
2098 
2099 /*
2100  * Read a cscope prompt (basically, skip over the ">> ").
2101  */
2102     static int
2103 cs_read_prompt(int i)
2104 {
2105     int		ch;
2106     char	*buf = NULL; /* buffer for possible error message from cscope */
2107     int		bufpos = 0;
2108     char	*cs_emsg;
2109     int		maxlen;
2110     static char *eprompt = "Press the RETURN key to continue:";
2111     int		epromptlen = (int)strlen(eprompt);
2112     int		n;
2113 
2114     cs_emsg = _("E609: Cscope error: %s");
2115     /* compute maximum allowed len for Cscope error message */
2116     maxlen = (int)(IOSIZE - strlen(cs_emsg));
2117 
2118     for (;;)
2119     {
2120 	while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2121 	    /* if there is room and char is printable */
2122 	    if (bufpos < maxlen - 1 && vim_isprintc(ch))
2123 	    {
2124 		if (buf == NULL) /* lazy buffer allocation */
2125 		    buf = alloc(maxlen);
2126 		if (buf != NULL)
2127 		{
2128 		    /* append character to the message */
2129 		    buf[bufpos++] = ch;
2130 		    buf[bufpos] = NUL;
2131 		    if (bufpos >= epromptlen
2132 			    && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2133 		    {
2134 			/* remove eprompt from buf */
2135 			buf[bufpos - epromptlen] = NUL;
2136 
2137 			/* print message to user */
2138 			(void)semsg(cs_emsg, buf);
2139 
2140 			/* send RETURN to cscope */
2141 			(void)putc('\n', csinfo[i].to_fp);
2142 			(void)fflush(csinfo[i].to_fp);
2143 
2144 			/* clear buf */
2145 			bufpos = 0;
2146 			buf[bufpos] = NUL;
2147 		    }
2148 		}
2149 	    }
2150 
2151 	for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2152 	{
2153 	    if (n > 0)
2154 		ch = getc(csinfo[i].fr_fp);
2155 	    if (ch == EOF)
2156 	    {
2157 		PERROR("cs_read_prompt EOF");
2158 		if (buf != NULL && buf[0] != NUL)
2159 		    (void)semsg(cs_emsg, buf);
2160 		else if (p_csverbose)
2161 		    cs_reading_emsg(i); /* don't have additional information */
2162 		cs_release_csp(i, TRUE);
2163 		vim_free(buf);
2164 		return CSCOPE_FAILURE;
2165 	    }
2166 
2167 	    if (ch != CSCOPE_PROMPT[n])
2168 	    {
2169 		ch = EOF;
2170 		break;
2171 	    }
2172 	}
2173 
2174 	if (ch == EOF)
2175 	    continue;	    /* didn't find the prompt */
2176 	break;		    /* did find the prompt */
2177     }
2178 
2179     vim_free(buf);
2180     return CSCOPE_SUCCESS;
2181 }
2182 
2183 #if defined(UNIX) && defined(SIGALRM)
2184 /*
2185  * Used to catch and ignore SIGALRM below.
2186  */
2187     static RETSIGTYPE
2188 sig_handler SIGDEFARG(sigarg)
2189 {
2190     /* do nothing */
2191     SIGRETURN;
2192 }
2193 #endif
2194 
2195 /*
2196  * Does the actual free'ing for the cs ptr with an optional flag of whether
2197  * or not to free the filename.  Called by cs_kill and cs_reset.
2198  */
2199     static void
2200 cs_release_csp(int i, int freefnpp)
2201 {
2202     /*
2203      * Trying to exit normally (not sure whether it is fit to UNIX cscope
2204      */
2205     if (csinfo[i].to_fp != NULL)
2206     {
2207 	(void)fputs("q\n", csinfo[i].to_fp);
2208 	(void)fflush(csinfo[i].to_fp);
2209     }
2210 #if defined(UNIX)
2211     {
2212 	int waitpid_errno;
2213 	int pstat;
2214 	pid_t pid;
2215 
2216 # if defined(HAVE_SIGACTION)
2217 	struct sigaction sa, old;
2218 
2219 	/* Use sigaction() to limit the waiting time to two seconds. */
2220 	sigemptyset(&sa.sa_mask);
2221 	sa.sa_handler = sig_handler;
2222 #  ifdef SA_NODEFER
2223 	sa.sa_flags = SA_NODEFER;
2224 #  else
2225 	sa.sa_flags = 0;
2226 #  endif
2227 	sigaction(SIGALRM, &sa, &old);
2228 	alarm(2); /* 2 sec timeout */
2229 
2230 	/* Block until cscope exits or until timer expires */
2231 	pid = waitpid(csinfo[i].pid, &pstat, 0);
2232 	waitpid_errno = errno;
2233 
2234 	/* cancel pending alarm if still there and restore signal */
2235 	alarm(0);
2236 	sigaction(SIGALRM, &old, NULL);
2237 # else
2238 	int waited;
2239 
2240 	/* Can't use sigaction(), loop for two seconds.  First yield the CPU
2241 	 * to give cscope a chance to exit quickly. */
2242 	sleep(0);
2243 	for (waited = 0; waited < 40; ++waited)
2244 	{
2245 	    pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
2246 	    waitpid_errno = errno;
2247 	    if (pid != 0)
2248 		break;  /* break unless the process is still running */
2249 	    mch_delay(50L, FALSE); /* sleep 50 ms */
2250 	}
2251 # endif
2252 	/*
2253 	 * If the cscope process is still running: kill it.
2254 	 * Safety check: If the PID would be zero here, the entire X session
2255 	 * would be killed.  -1 and 1 are dangerous as well.
2256 	 */
2257 	if (pid < 0 && csinfo[i].pid > 1)
2258 	{
2259 # ifdef ECHILD
2260 	    int alive = TRUE;
2261 
2262 	    if (waitpid_errno == ECHILD)
2263 	    {
2264 		/*
2265 		 * When using 'vim -g', vim is forked and cscope process is
2266 		 * no longer a child process but a sibling.  So waitpid()
2267 		 * fails with errno being ECHILD (No child processes).
2268 		 * Don't send SIGKILL to cscope immediately but wait
2269 		 * (polling) for it to exit normally as result of sending
2270 		 * the "q" command, hence giving it a chance to clean up
2271 		 * its temporary files.
2272 		 */
2273 		int waited;
2274 
2275 		sleep(0);
2276 		for (waited = 0; waited < 40; ++waited)
2277 		{
2278 		    /* Check whether cscope process is still alive */
2279 		    if (kill(csinfo[i].pid, 0) != 0)
2280 		    {
2281 			alive = FALSE; /* cscope process no longer exists */
2282 			break;
2283 		    }
2284 		    mch_delay(50L, FALSE); /* sleep 50ms */
2285 		}
2286 	    }
2287 	    if (alive)
2288 # endif
2289 	    {
2290 		kill(csinfo[i].pid, SIGKILL);
2291 		(void)waitpid(csinfo[i].pid, &pstat, 0);
2292 	    }
2293 	}
2294     }
2295 #else  /* !UNIX */
2296     if (csinfo[i].hProc != NULL)
2297     {
2298 	/* Give cscope a chance to exit normally */
2299 	if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2300 	    TerminateProcess(csinfo[i].hProc, 0);
2301 	CloseHandle(csinfo[i].hProc);
2302     }
2303 #endif
2304 
2305     if (csinfo[i].fr_fp != NULL)
2306 	(void)fclose(csinfo[i].fr_fp);
2307     if (csinfo[i].to_fp != NULL)
2308 	(void)fclose(csinfo[i].to_fp);
2309 
2310     if (freefnpp)
2311     {
2312 	vim_free(csinfo[i].fname);
2313 	vim_free(csinfo[i].ppath);
2314 	vim_free(csinfo[i].flags);
2315     }
2316 
2317     clear_csinfo(i);
2318 } /* cs_release_csp */
2319 
2320 
2321 /*
2322  * Calls cs_kill on all cscope connections then reinits.
2323  */
2324     static int
2325 cs_reset(exarg_T *eap UNUSED)
2326 {
2327     char	**dblist = NULL, **pplist = NULL, **fllist = NULL;
2328     int	i;
2329     char buf[20]; /* for sprintf " (#%d)" */
2330 
2331     if (csinfo_size == 0)
2332 	return CSCOPE_SUCCESS;
2333 
2334     /* malloc our db and ppath list */
2335     dblist = ALLOC_MULT(char *, csinfo_size);
2336     pplist = ALLOC_MULT(char *, csinfo_size);
2337     fllist = ALLOC_MULT(char *, csinfo_size);
2338     if (dblist == NULL || pplist == NULL || fllist == NULL)
2339     {
2340 	vim_free(dblist);
2341 	vim_free(pplist);
2342 	vim_free(fllist);
2343 	return CSCOPE_FAILURE;
2344     }
2345 
2346     for (i = 0; i < csinfo_size; i++)
2347     {
2348 	dblist[i] = csinfo[i].fname;
2349 	pplist[i] = csinfo[i].ppath;
2350 	fllist[i] = csinfo[i].flags;
2351 	if (csinfo[i].fname != NULL)
2352 	    cs_release_csp(i, FALSE);
2353     }
2354 
2355     /* rebuild the cscope connection list */
2356     for (i = 0; i < csinfo_size; i++)
2357     {
2358 	if (dblist[i] != NULL)
2359 	{
2360 	    cs_add_common(dblist[i], pplist[i], fllist[i]);
2361 	    if (p_csverbose)
2362 	    {
2363 		/* don't use smsg_attr() because we want to display the
2364 		 * connection number in the same line as
2365 		 * "Added cscope database..."
2366 		 */
2367 		sprintf(buf, " (#%d)", i);
2368 		msg_puts_attr(buf, HL_ATTR(HLF_R));
2369 	    }
2370 	}
2371 	vim_free(dblist[i]);
2372 	vim_free(pplist[i]);
2373 	vim_free(fllist[i]);
2374     }
2375     vim_free(dblist);
2376     vim_free(pplist);
2377     vim_free(fllist);
2378 
2379     if (p_csverbose)
2380 	msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
2381     return CSCOPE_SUCCESS;
2382 } /* cs_reset */
2383 
2384 
2385 /*
2386  * Construct the full pathname to a file found in the cscope database.
2387  * (Prepends ppath, if there is one and if it's not already prepended,
2388  * otherwise just uses the name found.)
2389  *
2390  * We need to prepend the prefix because on some cscope's (e.g., the one that
2391  * ships with Solaris 2.6), the output never has the prefix prepended.
2392  * Contrast this with my development system (Digital Unix), which does.
2393  */
2394     static char *
2395 cs_resolve_file(int i, char *name)
2396 {
2397     char	*fullname;
2398     int		len;
2399     char_u	*csdir = NULL;
2400 
2401     /*
2402      * Ppath is freed when we destroy the cscope connection.
2403      * Fullname is freed after cs_make_vim_style_matches, after it's been
2404      * copied into the tag buffer used by Vim.
2405      */
2406     len = (int)(strlen(name) + 2);
2407     if (csinfo[i].ppath != NULL)
2408 	len += (int)strlen(csinfo[i].ppath);
2409     else if (p_csre && csinfo[i].fname != NULL)
2410     {
2411 	/* If 'cscoperelative' is set and ppath is not set, use cscope.out
2412 	 * path in path resolution. */
2413 	csdir = alloc(MAXPATHL);
2414 	if (csdir != NULL)
2415 	{
2416 	    vim_strncpy(csdir, (char_u *)csinfo[i].fname,
2417 					  gettail((char_u *)csinfo[i].fname)
2418 						 - (char_u *)csinfo[i].fname);
2419 	    len += (int)STRLEN(csdir);
2420 	}
2421     }
2422 
2423     /* Note/example: this won't work if the cscope output already starts
2424      * "../.." and the prefix path is also "../..".  if something like this
2425      * happens, you are screwed up and need to fix how you're using cscope. */
2426     if (csinfo[i].ppath != NULL
2427 	    && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2428 	    && (name[0] != '/')
2429 #ifdef MSWIN
2430 	    && name[0] != '\\' && name[1] != ':'
2431 #endif
2432        )
2433     {
2434 	if ((fullname = alloc(len)) != NULL)
2435 	    (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2436     }
2437     else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
2438     {
2439 	/* Check for csdir to be non empty to avoid empty path concatenated to
2440 	 * cscope output. */
2441 	fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
2442     }
2443     else
2444     {
2445 	fullname = (char *)vim_strsave((char_u *)name);
2446     }
2447 
2448     vim_free(csdir);
2449     return fullname;
2450 }
2451 
2452 
2453 /*
2454  * Show all cscope connections.
2455  */
2456     static int
2457 cs_show(exarg_T *eap UNUSED)
2458 {
2459     short i;
2460     if (cs_cnt_connections() == 0)
2461 	msg_puts(_("no cscope connections\n"));
2462     else
2463     {
2464 	msg_puts_attr(
2465 	    _(" # pid    database name                       prepend path\n"),
2466 	    HL_ATTR(HLF_T));
2467 	for (i = 0; i < csinfo_size; i++)
2468 	{
2469 	    if (csinfo[i].fname == NULL)
2470 		continue;
2471 
2472 	    if (csinfo[i].ppath != NULL)
2473 		(void)smsg("%2d %-5ld  %-34s  %-32s",
2474 		    i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2475 	    else
2476 		(void)smsg("%2d %-5ld  %-34s  <none>",
2477 			   i, (long)csinfo[i].pid, csinfo[i].fname);
2478 	}
2479     }
2480 
2481     wait_return(TRUE);
2482     return CSCOPE_SUCCESS;
2483 } /* cs_show */
2484 
2485 
2486 /*
2487  * Only called when VIM exits to quit any cscope sessions.
2488  */
2489     void
2490 cs_end(void)
2491 {
2492     int i;
2493 
2494     for (i = 0; i < csinfo_size; i++)
2495 	cs_release_csp(i, TRUE);
2496     vim_free(csinfo);
2497     csinfo_size = 0;
2498 }
2499 
2500 #endif	/* FEAT_CSCOPE */
2501 
2502 /* the end */
2503