blob: 8866435ddbad3cce0b5a8c380219288b8f14b5a9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
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 <string.h>
17#include <errno.h>
18#include <assert.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#if defined(UNIX)
22# include <sys/wait.h>
23#else
24 /* not UNIX, must be WIN32 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000025# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000026# include <fcntl.h>
27# include <process.h>
28# define STDIN_FILENO 0
29# define STDOUT_FILENO 1
30# define STDERR_FILENO 2
31# define pipe(fds) _pipe(fds, 256, O_TEXT|O_NOINHERIT)
32#endif
33#include "if_cscope.h"
34
35static void cs_usage_msg __ARGS((csid_e x));
36static int cs_add __ARGS((exarg_T *eap));
37static void cs_stat_emsg __ARGS((char *fname));
38static int cs_add_common __ARGS((char *, char *, char *));
39static int cs_check_for_connections __ARGS((void));
40static int cs_check_for_tags __ARGS((void));
41static int cs_cnt_connections __ARGS((void));
42static void cs_reading_emsg __ARGS((int idx));
43static int cs_cnt_matches __ARGS((int idx));
44static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
45static int cs_create_connection __ARGS((int i));
46static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000047#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void cs_fill_results __ARGS((char *, int , int *, char ***,
51 char ***, int *));
52static int cs_find __ARGS((exarg_T *eap));
Bram Moolenaarc7453f52006-02-10 23:20:28 +000053static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static int cs_help __ARGS((exarg_T *eap));
55static void cs_init __ARGS((void));
56static void clear_csinfo __ARGS((int i));
57static int cs_insert_filelist __ARGS((char *, char *, char *,
58 struct stat *));
59static int cs_kill __ARGS((exarg_T *eap));
60static void cs_kill_execute __ARGS((int, char *));
61static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
62static char * cs_make_vim_style_matches __ARGS((char *, char *,
63 char *, char *));
64static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
65static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
66static char * cs_pathcomponents __ARGS((char *path));
67static void cs_print_tags_priv __ARGS((char **, char **, int));
68static int cs_read_prompt __ARGS((int ));
69static void cs_release_csp __ARGS((int, int freefnpp));
70static int cs_reset __ARGS((exarg_T *eap));
71static char * cs_resolve_file __ARGS((int, char *));
72static int cs_show __ARGS((exarg_T *eap));
73
74
75static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS];
76static cscmd_T cs_cmds[] =
77{
78 { "add", cs_add,
79 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
80 { "find", cs_find,
81 N_("Query for a pattern"), FIND_USAGE, 1 },
82 { "help", cs_help,
83 N_("Show this message"), "help", 0 },
84 { "kill", cs_kill,
85 N_("Kill a connection"), "kill #", 0 },
86 { "reset", cs_reset,
87 N_("Reinit all connections"), "reset", 0 },
88 { "show", cs_show,
89 N_("Show connections"), "show", 0 },
90 { NULL }
91};
92
93 static void
94cs_usage_msg(x)
95 csid_e x;
96{
97 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
98}
99
100/*
101 * PRIVATE: do_cscope_general
102 *
103 * find the command, print help if invalid, and the then call the
104 * corresponding command function,
105 * called from do_cscope and do_scscope
106 */
107 static void
108do_cscope_general(eap, make_split)
109 exarg_T *eap;
110 int make_split; /* whether to split window */
111{
112 cscmd_T *cmdp;
113
114 cs_init();
115 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
116 {
117 cs_help(eap);
118 return;
119 }
120
121#ifdef FEAT_WINDOWS
122 if (make_split)
123 {
124 if (!cmdp->cansplit)
125 {
126 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
127 return;
128 }
129 postponed_split = -1;
130 postponed_split_flags = cmdmod.split;
131 }
132#endif
133
134 cmdp->func(eap);
135
136#ifdef FEAT_WINDOWS
137 postponed_split_flags = 0;
138#endif
139}
140
141/*
142 * PUBLIC: do_cscope
143 */
144 void
145do_cscope(eap)
146 exarg_T *eap;
147{
148 do_cscope_general(eap, FALSE);
149}
150
151/*
152 * PUBLIC: do_scscope
153 *
154 * same as do_cscope, but splits window, too.
155 */
156 void
157do_scscope(eap)
158 exarg_T *eap;
159{
160 do_cscope_general(eap, TRUE);
161}
162
163/*
164 * PUBLIC: do_cstag
165 *
166 */
167 void
168do_cstag(eap)
169 exarg_T *eap;
170{
171 int ret = FALSE;
172
173 cs_init();
174
175 if (eap->arg == NULL || strlen((const char *)(eap->arg)) == 0)
176 {
177 (void)EMSG(_("E562: Usage: cstag <ident>"));
178 return;
179 }
180
181 switch (p_csto)
182 {
183 case 0 :
184 if (cs_check_for_connections())
185 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000186 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
187 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (ret == FALSE)
189 {
190 cs_free_tags();
191 if (msg_col)
192 msg_putchar('\n');
193
194 if (cs_check_for_tags())
195 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
196 }
197 }
198 else if (cs_check_for_tags())
199 {
200 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
201 }
202 break;
203 case 1 :
204 if (cs_check_for_tags())
205 {
206 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
207 if (ret == FALSE)
208 {
209 if (msg_col)
210 msg_putchar('\n');
211
212 if (cs_check_for_connections())
213 {
214 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000215 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (ret == FALSE)
217 cs_free_tags();
218 }
219 }
220 }
221 else if (cs_check_for_connections())
222 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000223 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
224 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (ret == FALSE)
226 cs_free_tags();
227 }
228 break;
229 default :
230 break;
231 }
232
233 if (!ret)
234 {
235 (void)EMSG(_("E257: cstag: tag not found"));
236#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
237 g_do_tagpreview = 0;
238#endif
239 }
240
241} /* do_cscope */
242
243
244/*
245 * PUBLIC: cs_find
246 *
247 * this simulates a vim_fgets(), but for cscope, returns the next line
248 * from the cscope output. should only be called from find_tags()
249 *
250 * returns TRUE if eof, FALSE otherwise
251 */
252 int
253cs_fgets(buf, size)
254 char_u *buf;
255 int size;
256{
257 char *p;
258
259 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
260 return TRUE;
261
262 if ((int)strlen(p) > size)
263 {
264 strncpy((char *)buf, p, size - 1);
265 buf[size] = '\0';
266 }
267 else
268 (void)strcpy((char *)buf, p);
269
270 return FALSE;
271} /* cs_fgets */
272
273
274/*
275 * PUBLIC: cs_free_tags
276 *
277 * called only from do_tag(), when popping the tag stack
278 */
279 void
280cs_free_tags()
281{
282 cs_manage_matches(NULL, NULL, -1, Free);
283}
284
285
286/*
287 * PUBLIC: cs_print_tags
288 *
289 * called from do_tag()
290 */
291 void
292cs_print_tags()
293{
294 cs_manage_matches(NULL, NULL, -1, Print);
295}
296
297
298/*
299 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
300 *
301 * Checks for the existence of a |cscope| connection. If no
302 * parameters are specified, then the function returns:
303 *
304 * 0, if cscope was not available (not compiled in), or if there
305 * are no cscope connections; or
306 * 1, if there is at least one cscope connection.
307 *
308 * If parameters are specified, then the value of {num}
309 * determines how existence of a cscope connection is checked:
310 *
311 * {num} Description of existence check
312 * ----- ------------------------------
313 * 0 Same as no parameters (e.g., "cscope_connection()").
314 * 1 Ignore {prepend}, and use partial string matches for
315 * {dbpath}.
316 * 2 Ignore {prepend}, and use exact string matches for
317 * {dbpath}.
318 * 3 Use {prepend}, use partial string matches for both
319 * {dbpath} and {prepend}.
320 * 4 Use {prepend}, use exact string matches for both
321 * {dbpath} and {prepend}.
322 *
323 * Note: All string comparisons are case sensitive!
324 */
325#if defined(FEAT_EVAL) || defined(PROTO)
326 int
327cs_connection(num, dbpath, ppath)
328 int num;
329 char_u *dbpath;
330 char_u *ppath;
331{
332 int i;
333
334 if (num < 0 || num > 4 || (num > 0 && !dbpath))
335 return FALSE;
336
337 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
338 {
339 if (!csinfo[i].fname)
340 continue;
341
342 if (num == 0)
343 return TRUE;
344
345 switch (num)
346 {
347 case 1:
348 if (strstr(csinfo[i].fname, (char *)dbpath))
349 return TRUE;
350 break;
351 case 2:
352 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
353 return TRUE;
354 break;
355 case 3:
356 if (strstr(csinfo[i].fname, (char *)dbpath)
357 && ((!ppath && !csinfo[i].ppath)
358 || (ppath
359 && csinfo[i].ppath
360 && strstr(csinfo[i].ppath, (char *)ppath))))
361 return TRUE;
362 break;
363 case 4:
364 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
365 && ((!ppath && !csinfo[i].ppath)
366 || (ppath
367 && csinfo[i].ppath
368 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
369 return TRUE;
370 break;
371 }
372 }
373
374 return FALSE;
375} /* cs_connection */
376#endif
377
378
379/*
380 * PRIVATE functions
381 ****************************************************************************/
382
383/*
384 * PRIVATE: cs_add
385 *
386 * add cscope database or a directory name (to look for cscope.out)
387 * the the cscope connection list
388 *
389 * MAXPATHL 256
390 */
391/* ARGSUSED */
392 static int
393cs_add(eap)
394 exarg_T *eap;
395{
396 char *fname, *ppath, *flags = NULL;
397
398 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
399 {
400 cs_usage_msg(Add);
401 return CSCOPE_FAILURE;
402 }
403 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
404 flags = strtok((char *)NULL, (const char *)" ");
405
406 return cs_add_common(fname, ppath, flags);
407}
408
409 static void
410cs_stat_emsg(fname)
411 char *fname;
412{
413 char *stat_emsg = _("E563: stat(%s) error: %d");
414 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
415
416 if (buf != NULL)
417 {
418 (void)sprintf(buf, stat_emsg, fname, errno);
419 (void)EMSG(buf);
420 vim_free(buf);
421 }
422 else
423 (void)EMSG(_("E563: stat error"));
424}
425
426
427/*
428 * PRIVATE: cs_add_common
429 *
430 * the common routine to add a new cscope connection. called by
431 * cs_add() and cs_reset(). i really don't like to do this, but this
432 * routine uses a number of goto statements.
433 */
434 static int
435cs_add_common(arg1, arg2, flags)
436 char *arg1; /* filename - may contain environment variables */
437 char *arg2; /* prepend path - may contain environment variables */
438 char *flags;
439{
440 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000441 int ret;
442 char *fname = NULL;
443 char *fname2 = NULL;
444 char *ppath = NULL;
445 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000448 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 goto add_err;
450
451 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
452 ret = stat(fname, &statbuf);
453 if (ret < 0)
454 {
455staterr:
456 if (p_csverbose)
457 cs_stat_emsg(fname);
458 goto add_err;
459 }
460
461 /* get the prepend path (arg2), expand it, and try to stat it */
462 if (arg2 != NULL)
463 {
464 struct stat statbuf2;
465
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000466 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 goto add_err;
468
469 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
470 ret = stat(ppath, &statbuf2);
471 if (ret < 0)
472 goto staterr;
473 }
474
475 /* if filename is a directory, append the cscope database name to it */
476 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
477 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000478 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (fname2 == NULL)
480 goto add_err;
481
482 while (fname[strlen(fname)-1] == '/'
483#ifdef WIN32
484 || fname[strlen(fname)-1] == '\\'
485#endif
486 )
487 {
488 fname[strlen(fname)-1] = '\0';
489 if (strlen(fname) == 0)
490 break;
491 }
492 if (fname[0] == '\0')
493 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
494 else
495 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
496
497 ret = stat(fname2, &statbuf);
498 if (ret < 0)
499 {
500 if (p_csverbose)
501 cs_stat_emsg(fname2);
502 goto add_err;
503 }
504
505 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
506 }
507#if defined(UNIX)
508 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
509#else
510 /* substitute define S_ISREG from os_unix.h */
511 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
512#endif
513 {
514 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
515 }
516 else
517 {
518 if (p_csverbose)
519 (void)EMSG2(
520 _("E564: %s is not a directory or a valid cscope database"),
521 fname);
522 goto add_err;
523 }
524
525 if (i != -1)
526 {
527 if (cs_create_connection(i) == CSCOPE_FAILURE
528 || cs_read_prompt(i) == CSCOPE_FAILURE)
529 {
530 cs_release_csp(i, TRUE);
531 goto add_err;
532 }
533
534 if (p_csverbose)
535 {
536 msg_clr_eos();
537 (void)smsg_attr(hl_attr(HLF_R),
538 (char_u *)_("Added cscope database %s"),
539 csinfo[i].fname);
540 }
541 }
542
543 vim_free(fname);
544 vim_free(fname2);
545 vim_free(ppath);
546 return CSCOPE_SUCCESS;
547
548add_err:
549 vim_free(fname2);
550 vim_free(fname);
551 vim_free(ppath);
552 return CSCOPE_FAILURE;
553} /* cs_add_common */
554
555
556 static int
557cs_check_for_connections()
558{
559 return (cs_cnt_connections() > 0);
560} /* cs_check_for_connections */
561
562
563 static int
564cs_check_for_tags()
565{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000566 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567} /* cs_check_for_tags */
568
569
570/*
571 * PRIVATE: cs_cnt_connections
572 *
573 * count the number of cscope connections
574 */
575 static int
576cs_cnt_connections()
577{
578 short i;
579 short cnt = 0;
580
581 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
582 {
583 if (csinfo[i].fname != NULL)
584 cnt++;
585 }
586 return cnt;
587} /* cs_cnt_connections */
588
589 static void
590cs_reading_emsg(idx)
591 int idx; /* connection index */
592{
593 EMSGN(_("E262: error reading cscope connection %ld"), idx);
594}
595
596#define CSREAD_BUFSIZE 2048
597/*
598 * PRIVATE: cs_cnt_matches
599 *
600 * count the number of matches for a given cscope connection.
601 */
602 static int
603cs_cnt_matches(idx)
604 int idx;
605{
606 char *stok;
607 char *buf;
608 int nlines;
609
610 buf = (char *)alloc(CSREAD_BUFSIZE);
611 if (buf == NULL)
612 return 0;
613 for (;;)
614 {
615 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
616 {
617 if (feof(csinfo[idx].fr_fp))
618 errno = EIO;
619
620 cs_reading_emsg(idx);
621
622 vim_free(buf);
623 return -1;
624 }
625
626 /*
627 * If the database is out of date, or there's some other problem,
628 * cscope will output error messages before the number-of-lines output.
629 * Display/discard any output that doesn't match what we want.
630 */
631 if ((stok = strtok(buf, (const char *)" ")) == NULL)
632 continue;
633 if (strcmp((const char *)stok, "cscope:"))
634 continue;
635
636 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
637 continue;
638 nlines = atoi(stok);
639 if (nlines < 0)
640 {
641 nlines = 0;
642 break;
643 }
644
645 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
646 continue;
647 if (strncmp((const char *)stok, "lines", 5))
648 continue;
649
650 break;
651 }
652
653 vim_free(buf);
654 return nlines;
655} /* cs_cnt_matches */
656
657
658/*
659 * PRIVATE: cs_create_cmd
660 *
661 * Creates the actual cscope command query from what the user entered.
662 */
663 static char *
664cs_create_cmd(csoption, pattern)
665 char *csoption;
666 char *pattern;
667{
668 char *cmd;
669 short search;
670
671 switch (csoption[0])
672 {
673 case '0' : case 's' :
674 search = 0;
675 break;
676 case '1' : case 'g' :
677 search = 1;
678 break;
679 case '2' : case 'd' :
680 search = 2;
681 break;
682 case '3' : case 'c' :
683 search = 3;
684 break;
685 case '4' : case 't' :
686 search = 4;
687 break;
688 case '6' : case 'e' :
689 search = 6;
690 break;
691 case '7' : case 'f' :
692 search = 7;
693 break;
694 case '8' : case 'i' :
695 search = 8;
696 break;
697 default :
698 (void)EMSG(_("E561: unknown cscope search type"));
699 cs_usage_msg(Find);
700 return NULL;
701 }
702
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000703 if ((cmd = (char *)alloc((unsigned)(strlen(pattern) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 return NULL;
705
706 (void)sprintf(cmd, "%d%s", search, pattern);
707
708 return cmd;
709} /* cs_create_cmd */
710
711
712/*
713 * PRIVATE: cs_create_connection
714 *
715 * This piece of code was taken/adapted from nvi. do we need to add
716 * the BSD license notice?
717 */
718 static int
719cs_create_connection(i)
720 int i;
721{
722 int to_cs[2], from_cs[2], len;
723 char *prog, *cmd, *ppath = NULL;
724#ifndef UNIX
725 int in_save, out_save, err_save;
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000726 long_i ph;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727# ifdef FEAT_GUI
728 HWND activewnd = NULL;
729 HWND consolewnd = NULL;
730# endif
731#endif
732
733 /*
734 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
735 * from_cs[0] and writes to to_cs[1].
736 */
737 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
738 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
739 {
740 (void)EMSG(_("E566: Could not create cscope pipes"));
741err_closing:
742 if (to_cs[0] != -1)
743 (void)close(to_cs[0]);
744 if (to_cs[1] != -1)
745 (void)close(to_cs[1]);
746 if (from_cs[0] != -1)
747 (void)close(from_cs[0]);
748 if (from_cs[1] != -1)
749 (void)close(from_cs[1]);
750 return CSCOPE_FAILURE;
751 }
752
753#if defined(UNIX)
754 switch (csinfo[i].pid = fork())
755 {
756 case -1:
757 (void)EMSG(_("E622: Could not fork for cscope"));
758 goto err_closing;
759 case 0: /* child: run cscope. */
760#else
761 in_save = dup(STDIN_FILENO);
762 out_save = dup(STDOUT_FILENO);
763 err_save = dup(STDERR_FILENO);
764#endif
765 if (dup2(to_cs[0], STDIN_FILENO) == -1)
766 PERROR("cs_create_connection 1");
767 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
768 PERROR("cs_create_connection 2");
769 if (dup2(from_cs[1], STDERR_FILENO) == -1)
770 PERROR("cs_create_connection 3");
771
772 /* close unused */
773#if defined(UNIX)
774 (void)close(to_cs[1]);
775 (void)close(from_cs[0]);
776#else
777 /* On win32 we must close opposite ends because we are the parent */
778 (void)close(to_cs[0]);
779 to_cs[0] = -1;
780 (void)close(from_cs[1]);
781 from_cs[1] = -1;
782#endif
783 /* expand the cscope exec for env var's */
784 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
785 {
786#ifdef UNIX
787 return CSCOPE_FAILURE;
788#else
789 goto err_closing;
790#endif
791 }
792 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
793
794 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000795 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (csinfo[i].ppath)
797 {
798 /* expand the prepend path for env var's */
799 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
800 {
801 vim_free(prog);
802#ifdef UNIX
803 return CSCOPE_FAILURE;
804#else
805 goto err_closing;
806#endif
807 }
808 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
809
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000810 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812
813 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000814 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 if ((cmd = (char *)alloc(len)) == NULL)
817 {
818 vim_free(prog);
819 vim_free(ppath);
820#ifdef UNIX
821 return CSCOPE_FAILURE;
822#else
823 goto err_closing;
824#endif
825 }
826
827 /* run the cscope command; is there execl for non-unix systems? */
828#if defined(UNIX)
829 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
830#else
831 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
832#endif
833 if (csinfo[i].ppath != NULL)
834 {
835 (void)strcat(cmd, " -P");
836 (void)strcat(cmd, csinfo[i].ppath);
837 }
838 if (csinfo[i].flags != NULL)
839 {
840 (void)strcat(cmd, " ");
841 (void)strcat(cmd, csinfo[i].flags);
842 }
843# ifdef UNIX
844 /* on Win32 we still need prog */
845 vim_free(prog);
846# endif
847 vim_free(ppath);
848
849#if defined(UNIX)
850 if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
851 PERROR(_("cs_create_connection exec failed"));
852
853 exit(127);
854 /* NOTREACHED */
855 default: /* parent. */
856#else
857# ifdef FEAT_GUI
858 activewnd = GetForegroundWindow(); /* on win9x cscope steals focus */
859 /* Dirty hack to hide annoying console window */
860 if (AllocConsole())
861 {
862 char *title;
863 title = (char *)alloc(1024);
864 if (title == NULL)
865 FreeConsole();
866 else
867 {
868 GetConsoleTitle(title, 1024); /* save for future restore */
869 SetConsoleTitle(
870 "GVIMCS{5499421B-CBEF-45b0-85EF-38167FDEA5C5}GVIMCS");
871 Sleep(40); /* as stated in MS KB we must wait 40 ms */
872 consolewnd = FindWindow(NULL,
873 "GVIMCS{5499421B-CBEF-45b0-85EF-38167FDEA5C5}GVIMCS");
874 if (consolewnd != NULL)
875 ShowWindow(consolewnd, SW_HIDE);
876 SetConsoleTitle(title);
877 vim_free(title);
878 }
879 }
880# endif
881 /* May be use &shell, &shellquote etc */
882# ifdef __BORLANDC__
883 /* BCC 5.5 uses a different function name for spawnlp */
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000884 ph = (long_i)spawnlp(P_NOWAIT, prog, cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885# else
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000886 ph = (long_i)_spawnlp(_P_NOWAIT, prog, cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887# endif
888 vim_free(prog);
889 vim_free(cmd);
890# ifdef FEAT_GUI
891 /* Dirty hack part two */
892 if (activewnd != NULL)
893 /* restoring focus */
894 SetForegroundWindow(activewnd);
895 if (consolewnd != NULL)
896 FreeConsole();
897
898# endif
899 if (ph == -1)
900 {
901 PERROR(_("cs_create_connection exec failed"));
902 (void)EMSG(_("E623: Could not spawn cscope process"));
903 goto err_closing;
904 }
905 /* else */
906 csinfo[i].pid = 0;
907 csinfo[i].hProc = (HANDLE)ph;
908
909#endif /* !UNIX */
910 /*
911 * Save the file descriptors for later duplication, and
912 * reopen as streams.
913 */
914 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
915 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
916 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
917 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
918
919#if defined(UNIX)
920 /* close unused */
921 (void)close(to_cs[0]);
922 (void)close(from_cs[1]);
923
924 break;
925 }
926#else
927 /* restore stdhandles */
928 dup2(in_save, STDIN_FILENO);
929 dup2(out_save, STDOUT_FILENO);
930 dup2(err_save, STDERR_FILENO);
931 close(in_save);
932 close(out_save);
933 close(err_save);
934#endif
935 return CSCOPE_SUCCESS;
936} /* cs_create_connection */
937
938
939/*
940 * PRIVATE: cs_find
941 *
942 * query cscope using command line interface. parse the output and use tselect
943 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
944 *
945 * returns TRUE if we jump to a tag or abort, FALSE if not.
946 */
947 static int
948cs_find(eap)
949 exarg_T *eap;
950{
951 char *opt, *pat;
952
953 if (cs_check_for_connections() == FALSE)
954 {
955 (void)EMSG(_("E567: no cscope connections"));
956 return FALSE;
957 }
958
959 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
960 {
961 cs_usage_msg(Find);
962 return FALSE;
963 }
964
965 pat = opt + strlen(opt) + 1;
966 if (pat == NULL || (pat != NULL && pat[0] == '\0'))
967 {
968 cs_usage_msg(Find);
969 return FALSE;
970 }
971
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000972 return cs_find_common(opt, pat, eap->forceit, TRUE,
973 eap->cmdidx == CMD_lcscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974} /* cs_find */
975
976
977/*
978 * PRIVATE: cs_find_common
979 *
980 * common code for cscope find, shared by cs_find() and do_cstag()
981 */
982 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000983cs_find_common(opt, pat, forceit, verbose, use_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 char *opt;
985 char *pat;
986 int forceit;
987 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000988 int use_ll;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 int i;
991 char *cmd;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000992 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_QUICKFIX
994 char cmdletter;
995 char *qfpos;
996#endif
997
998 /* create the actual command to send to cscope */
999 cmd = cs_create_cmd(opt, pat);
1000 if (cmd == NULL)
1001 return FALSE;
1002
1003 /* send query to all open connections, then count the total number
1004 * of matches so we can alloc matchesp all in one swell foop
1005 */
1006 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1007 nummatches[i] = 0;
1008 totmatches = 0;
1009 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1010 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001011 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 continue;
1013
1014 /* send cmd to cscope */
1015 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1016 (void)fflush(csinfo[i].to_fp);
1017
1018 nummatches[i] = cs_cnt_matches(i);
1019
1020 if (nummatches[i] > -1)
1021 totmatches += nummatches[i];
1022
1023 if (nummatches[i] == 0)
1024 (void)cs_read_prompt(i);
1025 }
1026 vim_free(cmd);
1027
1028 if (totmatches == 0)
1029 {
1030 char *nf = _("E259: no matches found for cscope query %s of %s");
1031 char *buf;
1032
1033 if (!verbose)
1034 return FALSE;
1035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001036 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (buf == NULL)
1038 (void)EMSG(nf);
1039 else
1040 {
1041 sprintf(buf, nf, opt, pat);
1042 (void)EMSG(buf);
1043 vim_free(buf);
1044 }
1045 return FALSE;
1046 }
1047
1048#ifdef FEAT_QUICKFIX
1049 /* get cmd letter */
1050 switch (opt[0])
1051 {
1052 case '0' :
1053 cmdletter = 's';
1054 break;
1055 case '1' :
1056 cmdletter = 'g';
1057 break;
1058 case '2' :
1059 cmdletter = 'd';
1060 break;
1061 case '3' :
1062 cmdletter = 'c';
1063 break;
1064 case '4' :
1065 cmdletter = 't';
1066 break;
1067 case '6' :
1068 cmdletter = 'e';
1069 break;
1070 case '7' :
1071 cmdletter = 'f';
1072 break;
1073 case '8' :
1074 cmdletter = 'i';
1075 break;
1076 default :
1077 cmdletter = opt[0];
1078 }
1079
1080 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1081 if (qfpos != NULL)
1082 {
1083 qfpos++;
1084 /* next symbol must be + or - */
1085 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1086 {
1087 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001088 char *buf = (char *)alloc((unsigned)strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 /* strlen will be enough because we use chars */
1091 if (buf != NULL)
1092 {
1093 sprintf(buf, nf, *qfpos, *(qfpos-1));
1094 (void)EMSG(buf);
1095 vim_free(buf);
1096 }
1097 return FALSE;
1098 }
1099 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001100 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001103 FILE *f;
1104 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001105 qf_info_T *qi = NULL;
1106 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001108 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001109 if (f == NULL)
1110 EMSG2(_(e_notopen), tmp);
1111 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001113 cs_file_results(f, nummatches);
1114 fclose(f);
1115 if (use_ll) /* Use location list */
1116 wp = curwin;
1117 /* '-' starts a new error list */
1118 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1119 *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001121# ifdef FEAT_WINDOWS
1122 if (postponed_split != 0)
1123 {
1124 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 postponed_split_flags);
1126# ifdef FEAT_SCROLLBIND
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001127 curwin->w_p_scb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001129 postponed_split = 0;
1130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001132 if (use_ll)
1133 /*
1134 * In the location list window, use the displayed location
1135 * list. Otherwise, use the location list for the window.
1136 */
1137 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1138 ? wp->w_llist_ref : wp->w_llist;
1139 qf_jump(qi, 0, 0, forceit);
1140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142 mch_remove(tmp);
1143 vim_free(tmp);
1144 return TRUE;
1145 }
1146 else
1147#endif /* FEAT_QUICKFIX */
1148 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001149 char **matches = NULL, **contexts = NULL;
1150 int matched = 0;
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 /* read output */
1153 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1154 &contexts, &matched);
1155 if (matches == NULL)
1156 return FALSE;
1157
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001158 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1161 }
1162
1163} /* cs_find_common */
1164
1165/*
1166 * PRIVATE: cs_help
1167 *
1168 * print help
1169 */
1170/* ARGSUSED */
1171 static int
1172cs_help(eap)
1173 exarg_T *eap;
1174{
1175 cscmd_T *cmdp = cs_cmds;
1176
1177 (void)MSG_PUTS(_("cscope commands:\n"));
1178 while (cmdp->name != NULL)
1179 {
1180 (void)smsg((char_u *)_("%-5s: %-30s (Usage: %s)"),
1181 cmdp->name, _(cmdp->help), cmdp->usage);
1182 if (strcmp(cmdp->name, "find") == 0)
1183 MSG_PUTS(FIND_HELP);
1184 cmdp++;
1185 }
1186
1187 wait_return(TRUE);
1188 return 0;
1189} /* cs_help */
1190
1191
1192/*
1193 * PRIVATE: cs_init
1194 *
1195 * initialize cscope structure if not already
1196 */
1197 static void
1198cs_init()
1199{
1200 short i;
1201 static int init_already = FALSE;
1202
1203 if (init_already)
1204 return;
1205
1206 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1207 clear_csinfo(i);
1208
1209 init_already = TRUE;
1210} /* cs_init */
1211
1212 static void
1213clear_csinfo(i)
1214 int i;
1215{
1216 csinfo[i].fname = NULL;
1217 csinfo[i].ppath = NULL;
1218 csinfo[i].flags = NULL;
1219#if defined(UNIX)
1220 csinfo[i].st_dev = (dev_t)0;
1221 csinfo[i].st_ino = (ino_t)0;
1222#else
1223 csinfo[i].nVolume = 0;
1224 csinfo[i].nIndexHigh = 0;
1225 csinfo[i].nIndexLow = 0;
1226#endif
1227 csinfo[i].pid = -1;
1228 csinfo[i].fr_fp = NULL;
1229 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001230#if defined(WIN32)
1231 csinfo[i].hProc = NULL;
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234
1235#ifndef UNIX
1236static char *GetWin32Error __ARGS((void));
1237
1238 static char *
1239GetWin32Error()
1240{
1241 char *msg = NULL;
1242 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1243 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1244 if (msg != NULL)
1245 {
1246 /* remove trailing \r\n */
1247 char *pcrlf = strstr(msg, "\r\n");
1248 if (pcrlf != NULL)
1249 *pcrlf = '\0';
1250 }
1251 return msg;
1252}
1253#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255/*
1256 * PRIVATE: cs_insert_filelist
1257 *
1258 * insert a new cscope database filename into the filelist
1259 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001260/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 static int
1262cs_insert_filelist(fname, ppath, flags, sb)
1263 char *fname;
1264 char *ppath;
1265 char *flags;
1266 struct stat *sb;
1267{
1268 short i, j;
1269#ifndef UNIX
1270 HANDLE hFile;
1271 BY_HANDLE_FILE_INFORMATION bhfi;
1272
1273 vim_memset(&bhfi, 0, sizeof(bhfi));
1274 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1275 if (!mch_windows95())
1276 {
1277 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1278 FILE_ATTRIBUTE_NORMAL, NULL);
1279 if (hFile == INVALID_HANDLE_VALUE)
1280 {
1281 if (p_csverbose)
1282 {
1283 char *cant_msg = _("E625: cannot open cscope database: %s");
1284 char *winmsg = GetWin32Error();
1285
1286 if (winmsg != NULL)
1287 {
1288 (void)EMSG2(cant_msg, winmsg);
1289 LocalFree(winmsg);
1290 }
1291 else
1292 /* subst filename if can't get error text */
1293 (void)EMSG2(cant_msg, fname);
1294 }
1295 return -1;
1296 }
1297 if (!GetFileInformationByHandle(hFile, &bhfi))
1298 {
1299 CloseHandle(hFile);
1300 if (p_csverbose)
1301 (void)EMSG(_("E626: cannot get cscope database information"));
1302 return -1;
1303 }
1304 CloseHandle(hFile);
1305 }
1306#endif
1307
1308 i = -1; /* can be set to the index of an empty item in csinfo */
1309 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1310 {
1311 if (csinfo[j].fname != NULL
1312#if defined(UNIX)
1313 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1314#else
1315 /* compare pathnames first */
1316 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
1317 /* if not Windows 9x, test index file atributes too */
1318 || (!mch_windows95()
1319 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1320 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1321 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1322#endif
1323 )
1324 {
1325 if (p_csverbose)
1326 (void)EMSG(_("E568: duplicate cscope database not added"));
1327 return -1;
1328 }
1329
1330 if (csinfo[j].fname == NULL && i == -1)
1331 i = j; /* remember first empty entry */
1332 }
1333
1334 if (i == -1)
1335 {
1336 if (p_csverbose)
1337 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1338 return -1;
1339 }
1340
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001341 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 return -1;
1343
1344 (void)strcpy(csinfo[i].fname, (const char *)fname);
1345
1346 if (ppath != NULL)
1347 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001348 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
1350 vim_free(csinfo[i].fname);
1351 csinfo[i].fname = NULL;
1352 return -1;
1353 }
1354 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1355 } else
1356 csinfo[i].ppath = NULL;
1357
1358 if (flags != NULL)
1359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001360 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 vim_free(csinfo[i].fname);
1363 vim_free(csinfo[i].ppath);
1364 csinfo[i].fname = NULL;
1365 csinfo[i].ppath = NULL;
1366 return -1;
1367 }
1368 (void)strcpy(csinfo[i].flags, (const char *)flags);
1369 } else
1370 csinfo[i].flags = NULL;
1371
1372#if defined(UNIX)
1373 csinfo[i].st_dev = sb->st_dev;
1374 csinfo[i].st_ino = sb->st_ino;
1375
1376#else
1377 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1378 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1379 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1380#endif
1381 return i;
1382} /* cs_insert_filelist */
1383
1384
1385/*
1386 * PRIVATE: cs_lookup_cmd
1387 *
1388 * find cscope command in command table
1389 */
1390 static cscmd_T *
1391cs_lookup_cmd(eap)
1392 exarg_T *eap;
1393{
1394 cscmd_T *cmdp;
1395 char *stok;
1396 size_t len;
1397
1398 if (eap->arg == NULL)
1399 return NULL;
1400
1401 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1402 return NULL;
1403
1404 len = strlen(stok);
1405 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1406 {
1407 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1408 return (cmdp);
1409 }
1410 return NULL;
1411} /* cs_lookup_cmd */
1412
1413
1414/*
1415 * PRIVATE: cs_kill
1416 *
1417 * nuke em
1418 */
1419/* ARGSUSED */
1420 static int
1421cs_kill(eap)
1422 exarg_T *eap;
1423{
1424 char *stok;
1425 short i;
1426
1427 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1428 {
1429 cs_usage_msg(Kill);
1430 return CSCOPE_FAILURE;
1431 }
1432
1433 /* only single digit positive and negative integers are allowed */
1434 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1435 || (strlen(stok) < 3 && stok[0] == '-'
1436 && VIM_ISDIGIT((int)(stok[1]))))
1437 i = atoi(stok);
1438 else
1439 {
1440 /* It must be part of a name. We will try to find a match
1441 * within all the names in the csinfo data structure
1442 */
1443 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1444 {
1445 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1446 break;
1447 }
1448 }
1449
1450 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1451 && i != -1)
1452 {
1453 if (p_csverbose)
1454 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1455 }
1456 else
1457 {
1458 if (i == -1)
1459 {
1460 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1461 {
1462 if (csinfo[i].fname)
1463 cs_kill_execute(i, csinfo[i].fname);
1464 }
1465 }
1466 else
1467 cs_kill_execute(i, stok);
1468 }
1469
1470 return 0;
1471} /* cs_kill */
1472
1473
1474/*
1475 * PRIVATE: cs_kill_execute
1476 *
1477 * Actually kills a specific cscope connection.
1478 */
1479 static void
1480cs_kill_execute(i, cname)
1481 int i; /* cscope table index */
1482 char *cname; /* cscope database name */
1483{
1484 if (p_csverbose)
1485 {
1486 msg_clr_eos();
1487 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1488 (char_u *)_("cscope connection %s closed"), cname);
1489 }
1490 cs_release_csp(i, TRUE);
1491}
1492
1493
1494/*
1495 * PRIVATE: cs_make_vim_style_matches
1496 *
1497 * convert the cscope output into into a ctags style entry (as might be found
1498 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1499 * the type of the tag you are looking for. for example, in Darren Hiebert's
1500 * ctags (the one that comes with vim), #define's use a line number to find the
1501 * tag in a file while function definitions use a regexp search pattern.
1502 *
1503 * i'm going to always use the line number because cscope does something
1504 * quirky (and probably other things i don't know about):
1505 *
1506 * if you have "# define" in your source file, which is
1507 * perfectly legal, cscope thinks you have "#define". this
1508 * will result in a failed regexp search. :(
1509 *
1510 * besides, even if this particular case didn't happen, the search pattern
1511 * would still have to be modified to escape all the special regular expression
1512 * characters to comply with ctags formatting.
1513 */
1514 static char *
1515cs_make_vim_style_matches(fname, slno, search, tagstr)
1516 char *fname;
1517 char *slno;
1518 char *search;
1519 char *tagstr;
1520{
1521 /* vim style is ctags:
1522 *
1523 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1524 *
1525 * but as mentioned above, we'll always use the line number and
1526 * put the search pattern (if one exists) as "extra"
1527 *
1528 * buf is used as part of vim's method of handling tags, and
1529 * (i think) vim frees it when you pop your tags and get replaced
1530 * by new ones on the tag stack.
1531 */
1532 char *buf;
1533 int amt;
1534
1535 if (search != NULL)
1536 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001537 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if ((buf = (char *)alloc(amt)) == NULL)
1539 return NULL;
1540
1541 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1542 }
1543 else
1544 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001545 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if ((buf = (char *)alloc(amt)) == NULL)
1547 return NULL;
1548
1549 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1550 }
1551
1552 return buf;
1553} /* cs_make_vim_style_matches */
1554
1555
1556/*
1557 * PRIVATE: cs_manage_matches
1558 *
1559 * this is kind of hokey, but i don't see an easy way round this..
1560 *
1561 * Store: keep a ptr to the (malloc'd) memory of matches originally
1562 * generated from cs_find(). the matches are originally lines directly
1563 * from cscope output, but transformed to look like something out of a
1564 * ctags. see cs_make_vim_style_matches for more details.
1565 *
1566 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1567 * the next line from the cscope output. it basically keeps track of which
1568 * lines have been "used" and returns the next one.
1569 *
1570 * Free: frees up everything and resets
1571 *
1572 * Print: prints the tags
1573 */
1574 static char *
1575cs_manage_matches(matches, contexts, totmatches, cmd)
1576 char **matches;
1577 char **contexts;
1578 int totmatches;
1579 mcmd_e cmd;
1580{
1581 static char **mp = NULL;
1582 static char **cp = NULL;
1583 static int cnt = -1;
1584 static int next = -1;
1585 char *p = NULL;
1586
1587 switch (cmd)
1588 {
1589 case Store:
1590 assert(matches != NULL);
1591 assert(totmatches > 0);
1592 if (mp != NULL || cp != NULL)
1593 (void)cs_manage_matches(NULL, NULL, -1, Free);
1594 mp = matches;
1595 cp = contexts;
1596 cnt = totmatches;
1597 next = 0;
1598 break;
1599 case Get:
1600 if (next >= cnt)
1601 return NULL;
1602
1603 p = mp[next];
1604 next++;
1605 break;
1606 case Free:
1607 if (mp != NULL)
1608 {
1609 if (cnt > 0)
1610 while (cnt--)
1611 {
1612 vim_free(mp[cnt]);
1613 if (cp != NULL)
1614 vim_free(cp[cnt]);
1615 }
1616 vim_free(mp);
1617 vim_free(cp);
1618 }
1619 mp = NULL;
1620 cp = NULL;
1621 cnt = 0;
1622 next = 0;
1623 break;
1624 case Print:
1625 cs_print_tags_priv(mp, cp, cnt);
1626 break;
1627 default: /* should not reach here */
1628 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1629 return NULL;
1630 }
1631
1632 return p;
1633} /* cs_manage_matches */
1634
1635
1636/*
1637 * PRIVATE: cs_parse_results
1638 *
1639 * parse cscope output
1640 */
1641 static char *
1642cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1643 int cnumber;
1644 char *buf;
1645 int bufsize;
1646 char **context;
1647 char **linenumber;
1648 char **search;
1649{
1650 int ch;
1651 char *p;
1652 char *name;
1653
1654 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1655 {
1656 if (feof(csinfo[cnumber].fr_fp))
1657 errno = EIO;
1658
1659 cs_reading_emsg(cnumber);
1660
1661 return NULL;
1662 }
1663
1664 /* If the line's too long for the buffer, discard it. */
1665 if ((p = strchr(buf, '\n')) == NULL)
1666 {
1667 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1668 ;
1669 return NULL;
1670 }
1671 *p = '\0';
1672
1673 /*
1674 * cscope output is in the following format:
1675 *
1676 * <filename> <context> <line number> <pattern>
1677 */
1678 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1679 return NULL;
1680 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1681 return NULL;
1682 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1683 return NULL;
1684 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1685
1686 /* --- nvi ---
1687 * If the file is older than the cscope database, that is,
1688 * the database was built since the file was last modified,
1689 * or there wasn't a search string, use the line number.
1690 */
1691 if (strcmp(*search, "<unknown>") == 0)
1692 *search = NULL;
1693
1694 name = cs_resolve_file(cnumber, name);
1695 return name;
1696}
1697
Bram Moolenaarc716c302006-01-21 22:12:51 +00001698#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699/*
1700 * PRIVATE: cs_file_results
1701 *
1702 * write cscope find results to file
1703 */
1704 static void
1705cs_file_results(f, nummatches_a)
1706 FILE *f;
1707 int *nummatches_a;
1708{
1709 int i, j;
1710 char *buf;
1711 char *search, *slno;
1712 char *fullname;
1713 char *cntx;
1714 char *context;
1715
1716 buf = (char *)alloc(CSREAD_BUFSIZE);
1717 if (buf == NULL)
1718 return;
1719
1720 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1721 {
1722 if (nummatches_a[i] < 1)
1723 continue;
1724
1725 for (j = 0; j < nummatches_a[i]; j++)
1726 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001727 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1728 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 continue;
1730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001731 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001732 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 continue;
1734
1735 if (strcmp(cntx, "<global>")==0)
1736 strcpy(context, "<<global>>");
1737 else
1738 sprintf(context, "<<%s>>", cntx);
1739
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001740 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1742 else
1743 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1744
1745 vim_free(context);
1746 vim_free(fullname);
1747 } /* for all matches */
1748
1749 (void)cs_read_prompt(i);
1750
1751 } /* for all cscope connections */
1752 vim_free(buf);
1753}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756/*
1757 * PRIVATE: cs_fill_results
1758 *
1759 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1760 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001761 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 */
1763 static void
1764cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1765 char *tagstr;
1766 int totmatches;
1767 int *nummatches_a;
1768 char ***matches_p;
1769 char ***cntxts_p;
1770 int *matched;
1771{
1772 int i, j;
1773 char *buf;
1774 char *search, *slno;
1775 int totsofar = 0;
1776 char **matches = NULL;
1777 char **cntxts = NULL;
1778 char *fullname;
1779 char *cntx;
1780
1781 assert(totmatches > 0);
1782
1783 buf = (char *)alloc(CSREAD_BUFSIZE);
1784 if (buf == NULL)
1785 return;
1786
1787 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1788 goto parse_out;
1789 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1790 goto parse_out;
1791
1792 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1793 {
1794 if (nummatches_a[i] < 1)
1795 continue;
1796
1797 for (j = 0; j < nummatches_a[i]; j++)
1798 {
1799 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1800 &slno, &search)) == NULL)
1801 continue;
1802
1803 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1804 search, tagstr);
1805
1806 vim_free(fullname);
1807
1808 if (strcmp(cntx, "<global>") == 0)
1809 cntxts[totsofar] = NULL;
1810 else
1811 /* note: if vim_strsave returns NULL, then the context
1812 * will be "<global>", which is misleading.
1813 */
1814 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1815
1816 if (matches[totsofar] != NULL)
1817 totsofar++;
1818
1819 } /* for all matches */
1820
1821 (void)cs_read_prompt(i);
1822
1823 } /* for all cscope connections */
1824
1825parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001826 if (totsofar == 0)
1827 {
1828 /* No matches, free the arrays and return NULL in "*matches_p". */
1829 vim_free(matches);
1830 matches = NULL;
1831 vim_free(cntxts);
1832 cntxts = NULL;
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 *matched = totsofar;
1835 *matches_p = matches;
1836 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 vim_free(buf);
1839} /* cs_fill_results */
1840
1841
1842/* get the requested path components */
1843 static char *
1844cs_pathcomponents(path)
1845 char *path;
1846{
1847 int i;
1848 char *s;
1849
1850 if (p_cspc == 0)
1851 return path;
1852
1853 s = path + strlen(path) - 1;
1854 for (i = 0; i < p_cspc; ++i)
1855 while (s > path && *--s != '/'
1856#ifdef WIN32
1857 && *--s != '\\'
1858#endif
1859 )
1860 ;
1861 if ((s > path && *s == '/')
1862#ifdef WIN32
1863 || (s > path && *s == '\\')
1864#endif
1865 )
1866 ++s;
1867 return s;
1868}
1869
1870/*
1871 * PRIVATE: cs_print_tags_priv
1872 *
1873 * called from cs_manage_matches()
1874 */
1875 static void
1876cs_print_tags_priv(matches, cntxts, num_matches)
1877 char **matches;
1878 char **cntxts;
1879 int num_matches;
1880{
1881 char *buf = NULL;
1882 int bufsize = 0; /* Track available bufsize */
1883 int newsize = 0;
1884 char *ptag;
1885 char *fname, *lno, *extra, *tbuf;
1886 int i, idx, num;
1887 char *globalcntx = "GLOBAL";
1888 char *cntxformat = " <<%s>>";
1889 char *context;
1890 char *cstag_msg = _("Cscope tag: %s");
1891 char *csfmt_str = "%4d %6s ";
1892
1893 assert (num_matches > 0);
1894
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001895 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return;
1897
1898 strcpy(tbuf, matches[0]);
1899 ptag = strtok(tbuf, "\t");
1900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001901 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 buf = (char *)alloc(newsize);
1903 if (buf != NULL)
1904 {
1905 bufsize = newsize;
1906 (void)sprintf(buf, cstag_msg, ptag);
1907 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
1908 }
1909
1910 vim_free(tbuf);
1911
1912 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
1913 msg_advance(msg_col + 2);
1914 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
1915
1916 num = 1;
1917 for (i = 0; i < num_matches; i++)
1918 {
1919 idx = i;
1920
1921 /* if we really wanted to, we could avoid this malloc and strcpy
1922 * by parsing matches[i] on the fly and placing stuff into buf
1923 * directly, but that's too much of a hassle
1924 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001925 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 continue;
1927 (void)strcpy(tbuf, matches[idx]);
1928
1929 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
1930 continue;
1931 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
1932 continue;
1933 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
1934 {
1935 /* if NULL, then no "extra", although in cscope's case, there
1936 * should always be "extra".
1937 */
1938 extra = NULL;
1939 }
1940
1941 extra = lno + strlen(lno) + 1;
1942
1943 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
1944
1945 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001946 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (bufsize < newsize)
1948 {
1949 buf = (char *)vim_realloc(buf, newsize);
1950 if (buf == NULL)
1951 bufsize = 0;
1952 else
1953 bufsize = newsize;
1954 }
1955 if (buf != NULL)
1956 {
1957 /* csfmt_str = "%4d %6s "; */
1958 (void)sprintf(buf, csfmt_str, num, lno);
1959 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
1960 }
1961 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
1962
1963 /* compute the required space for the context */
1964 if (cntxts[idx] != NULL)
1965 context = cntxts[idx];
1966 else
1967 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001968 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970 if (bufsize < newsize)
1971 {
1972 buf = (char *)vim_realloc(buf, newsize);
1973 if (buf == NULL)
1974 bufsize = 0;
1975 else
1976 bufsize = newsize;
1977 }
1978 if (buf != NULL)
1979 {
1980 (void)sprintf(buf, cntxformat, context);
1981
1982 /* print the context only if it fits on the same line */
1983 if (msg_col + (int)strlen(buf) >= (int)Columns)
1984 msg_putchar('\n');
1985 msg_advance(12);
1986 MSG_PUTS_LONG(buf);
1987 msg_putchar('\n');
1988 }
1989 if (extra != NULL)
1990 {
1991 msg_advance(13);
1992 MSG_PUTS_LONG(extra);
1993 }
1994
1995 vim_free(tbuf); /* only after printing extra due to strtok use */
1996
1997 if (msg_col)
1998 msg_putchar('\n');
1999
2000 ui_breakcheck();
2001 if (got_int)
2002 {
2003 got_int = FALSE; /* don't print any more matches */
2004 break;
2005 }
2006
2007 num++;
2008 } /* for all matches */
2009
2010 vim_free(buf);
2011} /* cs_print_tags_priv */
2012
2013
2014/*
2015 * PRIVATE: cs_read_prompt
2016 *
2017 * read a cscope prompt (basically, skip over the ">> ")
2018 */
2019 static int
2020cs_read_prompt(i)
2021 int i;
2022{
2023 int ch;
2024 char *buf = NULL; /* buffer for possible error message from cscope */
2025 int bufpos = 0;
2026 char *cs_emsg;
2027 int maxlen;
2028 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002029 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int n;
2031
2032 cs_emsg = _("E609: Cscope error: %s");
2033 /* compute maximum allowed len for Cscope error message */
2034 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2035
2036 for (;;)
2037 {
2038 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2039 /* if there is room and char is printable */
2040 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2041 {
2042 if (buf == NULL) /* lazy buffer allocation */
2043 buf = (char *)alloc(maxlen);
2044 if (buf != NULL)
2045 {
2046 /* append character to the message */
2047 buf[bufpos++] = ch;
2048 buf[bufpos] = NUL;
2049 if (bufpos >= epromptlen
2050 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2051 {
2052 /* remove eprompt from buf */
2053 buf[bufpos - epromptlen] = NUL;
2054
2055 /* print message to user */
2056 (void)EMSG2(cs_emsg, buf);
2057
2058 /* send RETURN to cscope */
2059 (void)putc('\n', csinfo[i].to_fp);
2060 (void)fflush(csinfo[i].to_fp);
2061
2062 /* clear buf */
2063 bufpos = 0;
2064 buf[bufpos] = NUL;
2065 }
2066 }
2067 }
2068
2069 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2070 {
2071 if (n > 0)
2072 ch = getc(csinfo[i].fr_fp);
2073 if (ch == EOF)
2074 {
2075 PERROR("cs_read_prompt EOF");
2076 if (buf != NULL && buf[0] != NUL)
2077 (void)EMSG2(cs_emsg, buf);
2078 else if (p_csverbose)
2079 cs_reading_emsg(i); /* don't have additional information */
2080 cs_release_csp(i, TRUE);
2081 vim_free(buf);
2082 return CSCOPE_FAILURE;
2083 }
2084
2085 if (ch != CSCOPE_PROMPT[n])
2086 {
2087 ch = EOF;
2088 break;
2089 }
2090 }
2091
2092 if (ch == EOF)
2093 continue; /* didn't find the prompt */
2094 break; /* did find the prompt */
2095 }
2096
2097 vim_free(buf);
2098 return CSCOPE_SUCCESS;
2099}
2100
2101
2102/*
2103 * PRIVATE: cs_release_csp
2104 *
2105 * does the actual free'ing for the cs ptr with an optional flag of whether
2106 * or not to free the filename. called by cs_kill and cs_reset.
2107 */
2108 static void
2109cs_release_csp(i, freefnpp)
2110 int i;
2111 int freefnpp;
2112{
2113#if defined(UNIX)
2114 int pstat;
2115#else
2116 /*
2117 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2118 */
2119 if (csinfo[i].to_fp != NULL)
2120 {
2121 (void)fputs("q\n", csinfo[i].to_fp);
2122 (void)fflush(csinfo[i].to_fp);
2123 }
2124 /* give cscope chance to exit normally */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00002125 if (csinfo[i].hProc != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 && WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2127 TerminateProcess(csinfo[i].hProc, 0);
2128#endif
2129
2130 if (csinfo[i].fr_fp != NULL)
2131 (void)fclose(csinfo[i].fr_fp);
2132 if (csinfo[i].to_fp != NULL)
2133 (void)fclose(csinfo[i].to_fp);
2134
2135 /*
2136 * Safety check: If the PID would be zero here, the entire X session would
2137 * be killed. -1 and 1 are dangerous as well.
2138 */
2139#if defined(UNIX)
2140 if (csinfo[i].pid > 1)
2141 {
2142 kill(csinfo[i].pid, SIGTERM);
2143 (void)waitpid(csinfo[i].pid, &pstat, 0);
2144 }
2145#endif
2146
2147 if (freefnpp)
2148 {
2149 vim_free(csinfo[i].fname);
2150 vim_free(csinfo[i].ppath);
2151 vim_free(csinfo[i].flags);
2152 }
2153
2154 clear_csinfo(i);
2155} /* cs_release_csp */
2156
2157
2158/*
2159 * PRIVATE: cs_reset
2160 *
2161 * calls cs_kill on all cscope connections then reinits
2162 */
2163/* ARGSUSED */
2164 static int
2165cs_reset(eap)
2166 exarg_T *eap;
2167{
2168 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2169 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002170 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172 /* malloc our db and ppath list */
2173 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2174 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2175 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2176 if (dblist == NULL || pplist == NULL || fllist == NULL)
2177 {
2178 vim_free(dblist);
2179 vim_free(pplist);
2180 vim_free(fllist);
2181 return CSCOPE_FAILURE;
2182 }
2183
2184 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2185 {
2186 dblist[i] = csinfo[i].fname;
2187 pplist[i] = csinfo[i].ppath;
2188 fllist[i] = csinfo[i].flags;
2189 if (csinfo[i].fname != NULL)
2190 cs_release_csp(i, FALSE);
2191 }
2192
2193 /* rebuild the cscope connection list */
2194 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2195 {
2196 if (dblist[i] != NULL)
2197 {
2198 cs_add_common(dblist[i], pplist[i], fllist[i]);
2199 if (p_csverbose)
2200 {
2201 /* dont' use smsg_attr because want to display
2202 * connection number in the same line as
2203 * "Added cscope database..."
2204 */
2205 sprintf(buf, " (#%d)", i);
2206 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2207 }
2208 }
2209 vim_free(dblist[i]);
2210 vim_free(pplist[i]);
2211 vim_free(fllist[i]);
2212 }
2213 vim_free(dblist);
2214 vim_free(pplist);
2215 vim_free(fllist);
2216
2217 if (p_csverbose)
2218 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2219 return CSCOPE_SUCCESS;
2220} /* cs_reset */
2221
2222
2223/*
2224 * PRIVATE: cs_resolve_file
2225 *
2226 * construct the full pathname to a file found in the cscope database.
2227 * (Prepends ppath, if there is one and if it's not already prepended,
2228 * otherwise just uses the name found.)
2229 *
2230 * we need to prepend the prefix because on some cscope's (e.g., the one that
2231 * ships with Solaris 2.6), the output never has the prefix prepended.
2232 * contrast this with my development system (Digital Unix), which does.
2233 */
2234 static char *
2235cs_resolve_file(i, name)
2236 int i;
2237 char *name;
2238{
2239 char *fullname;
2240 int len;
2241
2242 /*
2243 * ppath is freed when we destroy the cscope connection.
2244 * fullname is freed after cs_make_vim_style_matches, after it's been
2245 * copied into the tag buffer used by vim
2246 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002247 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002249 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 if ((fullname = (char *)alloc(len)) == NULL)
2252 return NULL;
2253
2254 /*
2255 * note/example: this won't work if the cscope output already starts
2256 * "../.." and the prefix path is also "../..". if something like this
2257 * happens, you are screwed up and need to fix how you're using cscope.
2258 */
2259 if (csinfo[i].ppath != NULL &&
2260 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2261 (name[0] != '/')
2262#ifdef WIN32
2263 && name[0] != '\\' && name[1] != ':'
2264#endif
2265 )
2266 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2267 else
2268 (void)sprintf(fullname, "%s", name);
2269
2270 return fullname;
2271} /* cs_resolve_file */
2272
2273
2274/*
2275 * PRIVATE: cs_show
2276 *
2277 * show all cscope connections
2278 */
2279/* ARGSUSED */
2280 static int
2281cs_show(eap)
2282 exarg_T *eap;
2283{
2284 short i;
2285 if (cs_cnt_connections() == 0)
2286 MSG_PUTS(_("no cscope connections\n"));
2287 else
2288 {
2289 MSG_PUTS_ATTR(
2290 _(" # pid database name prepend path\n"),
2291 hl_attr(HLF_T));
2292 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2293 {
2294 if (csinfo[i].fname == NULL)
2295 continue;
2296
2297 if (csinfo[i].ppath != NULL)
2298 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2299 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2300 else
2301 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2302 i, (long)csinfo[i].pid, csinfo[i].fname);
2303 }
2304 }
2305
2306 wait_return(TRUE);
2307 return CSCOPE_SUCCESS;
2308} /* cs_show */
2309
2310#endif /* FEAT_CSCOPE */
2311
2312/* the end */