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