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