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