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