blob: 57d1984bfadf1b31317a0fdeab5d0b27a374cc9e [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
Bram Moolenaarf4580d82009-03-18 11:52:53 +000096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
97
98static enum
99{
100 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
101 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
102 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
103} expand_what;
104
105/*
106 * Function given to ExpandGeneric() to obtain the cscope command
107 * expansion.
108 */
109/*ARGSUSED*/
110 char_u *
111get_cscope_name(xp, idx)
112 expand_T *xp;
113 int idx;
114{
115 switch (expand_what)
116 {
117 case EXP_CSCOPE_SUBCMD:
118 /* Complete with sub-commands of ":cscope":
119 * add, find, help, kill, reset, show */
120 return (char_u *)cs_cmds[idx].name;
121 case EXP_CSCOPE_FIND:
122 {
123 const char *query_type[] =
124 {
125 "c", "d", "e", "f", "g", "i", "s", "t", NULL
126 };
127
128 /* Complete with query type of ":cscope find {query_type}".
129 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
130 * ..., 8) but only complete with letters, since numbers are
131 * redundant. */
132 return (char_u *)query_type[idx];
133 }
134 case EXP_CSCOPE_KILL:
135 {
136 int i;
137 int current_idx = 0;
138 static char_u connection[2];
139
140 /* ":cscope kill" accepts connection numbers or partial names of
141 * the pathname of the cscope database as argument. Only complete
142 * with connection numbers. -1 can also be used to kill all
143 * connections. */
144 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
145 {
146 if (csinfo[i].fname == NULL)
147 continue;
148 if (current_idx++ == idx)
149 {
150 /* Connection number fits in one character since
151 * CSCOPE_MAX_CONNECTIONS is < 10 */
152 connection[0] = i + '0';
153 connection[1] = NUL;
154 return connection;
155 }
156 }
157 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
158 }
159 default:
160 return NULL;
161 }
162}
163
164/*
165 * Handle command line completion for :cscope command.
166 */
167 void
168set_context_in_cscope_cmd(xp, arg)
169 expand_T *xp;
170 char_u *arg;
171{
172 char_u *p;
173
174 /* Default: expand subcommands */
175 xp->xp_context = EXPAND_CSCOPE;
176 expand_what = EXP_CSCOPE_SUBCMD;
177 xp->xp_pattern = arg;
178
179 /* (part of) subcommand already typed */
180 if (*arg != NUL)
181 {
182 p = skiptowhite(arg);
183 if (*p != NUL) /* past first word */
184 {
185 xp->xp_pattern = skipwhite(p);
186 if (*skiptowhite(xp->xp_pattern) != NUL)
187 xp->xp_context = EXPAND_NOTHING;
188 else if (STRNICMP(arg, "add", p - arg) == 0)
189 xp->xp_context = EXPAND_FILES;
190 else if (STRNICMP(arg, "kill", p - arg) == 0)
191 expand_what = EXP_CSCOPE_KILL;
192 else if (STRNICMP(arg, "find", p - arg) == 0)
193 expand_what = EXP_CSCOPE_FIND;
194 else
195 xp->xp_context = EXPAND_NOTHING;
196 }
197 }
198}
199
200#endif /* FEAT_CMDL_COMPL */
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202/*
203 * PRIVATE: do_cscope_general
204 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000205 * Find the command, print help if invalid, and then call the corresponding
206 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 */
208 static void
209do_cscope_general(eap, make_split)
210 exarg_T *eap;
211 int make_split; /* whether to split window */
212{
213 cscmd_T *cmdp;
214
215 cs_init();
216 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
217 {
218 cs_help(eap);
219 return;
220 }
221
222#ifdef FEAT_WINDOWS
223 if (make_split)
224 {
225 if (!cmdp->cansplit)
226 {
227 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
228 return;
229 }
230 postponed_split = -1;
231 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000232 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234#endif
235
236 cmdp->func(eap);
237
238#ifdef FEAT_WINDOWS
239 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000240 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#endif
242}
243
244/*
245 * PUBLIC: do_cscope
246 */
247 void
248do_cscope(eap)
249 exarg_T *eap;
250{
251 do_cscope_general(eap, FALSE);
252}
253
254/*
255 * PUBLIC: do_scscope
256 *
257 * same as do_cscope, but splits window, too.
258 */
259 void
260do_scscope(eap)
261 exarg_T *eap;
262{
263 do_cscope_general(eap, TRUE);
264}
265
266/*
267 * PUBLIC: do_cstag
268 *
269 */
270 void
271do_cstag(eap)
272 exarg_T *eap;
273{
274 int ret = FALSE;
275
276 cs_init();
277
Bram Moolenaar446cb832008-06-24 21:56:24 +0000278 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {
280 (void)EMSG(_("E562: Usage: cstag <ident>"));
281 return;
282 }
283
284 switch (p_csto)
285 {
286 case 0 :
287 if (cs_check_for_connections())
288 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000289 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
290 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (ret == FALSE)
292 {
293 cs_free_tags();
294 if (msg_col)
295 msg_putchar('\n');
296
297 if (cs_check_for_tags())
298 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
299 }
300 }
301 else if (cs_check_for_tags())
302 {
303 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
304 }
305 break;
306 case 1 :
307 if (cs_check_for_tags())
308 {
309 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
310 if (ret == FALSE)
311 {
312 if (msg_col)
313 msg_putchar('\n');
314
315 if (cs_check_for_connections())
316 {
317 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000318 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 if (ret == FALSE)
320 cs_free_tags();
321 }
322 }
323 }
324 else if (cs_check_for_connections())
325 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000326 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
327 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (ret == FALSE)
329 cs_free_tags();
330 }
331 break;
332 default :
333 break;
334 }
335
336 if (!ret)
337 {
338 (void)EMSG(_("E257: cstag: tag not found"));
339#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
340 g_do_tagpreview = 0;
341#endif
342 }
343
344} /* do_cscope */
345
346
347/*
348 * PUBLIC: cs_find
349 *
350 * this simulates a vim_fgets(), but for cscope, returns the next line
351 * from the cscope output. should only be called from find_tags()
352 *
353 * returns TRUE if eof, FALSE otherwise
354 */
355 int
356cs_fgets(buf, size)
357 char_u *buf;
358 int size;
359{
360 char *p;
361
362 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
363 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000364 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
366 return FALSE;
367} /* cs_fgets */
368
369
370/*
371 * PUBLIC: cs_free_tags
372 *
373 * called only from do_tag(), when popping the tag stack
374 */
375 void
376cs_free_tags()
377{
378 cs_manage_matches(NULL, NULL, -1, Free);
379}
380
381
382/*
383 * PUBLIC: cs_print_tags
384 *
385 * called from do_tag()
386 */
387 void
388cs_print_tags()
389{
390 cs_manage_matches(NULL, NULL, -1, Print);
391}
392
393
394/*
395 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
396 *
397 * Checks for the existence of a |cscope| connection. If no
398 * parameters are specified, then the function returns:
399 *
400 * 0, if cscope was not available (not compiled in), or if there
401 * are no cscope connections; or
402 * 1, if there is at least one cscope connection.
403 *
404 * If parameters are specified, then the value of {num}
405 * determines how existence of a cscope connection is checked:
406 *
407 * {num} Description of existence check
408 * ----- ------------------------------
409 * 0 Same as no parameters (e.g., "cscope_connection()").
410 * 1 Ignore {prepend}, and use partial string matches for
411 * {dbpath}.
412 * 2 Ignore {prepend}, and use exact string matches for
413 * {dbpath}.
414 * 3 Use {prepend}, use partial string matches for both
415 * {dbpath} and {prepend}.
416 * 4 Use {prepend}, use exact string matches for both
417 * {dbpath} and {prepend}.
418 *
419 * Note: All string comparisons are case sensitive!
420 */
421#if defined(FEAT_EVAL) || defined(PROTO)
422 int
423cs_connection(num, dbpath, ppath)
424 int num;
425 char_u *dbpath;
426 char_u *ppath;
427{
428 int i;
429
430 if (num < 0 || num > 4 || (num > 0 && !dbpath))
431 return FALSE;
432
433 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
434 {
435 if (!csinfo[i].fname)
436 continue;
437
438 if (num == 0)
439 return TRUE;
440
441 switch (num)
442 {
443 case 1:
444 if (strstr(csinfo[i].fname, (char *)dbpath))
445 return TRUE;
446 break;
447 case 2:
448 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
449 return TRUE;
450 break;
451 case 3:
452 if (strstr(csinfo[i].fname, (char *)dbpath)
453 && ((!ppath && !csinfo[i].ppath)
454 || (ppath
455 && csinfo[i].ppath
456 && strstr(csinfo[i].ppath, (char *)ppath))))
457 return TRUE;
458 break;
459 case 4:
460 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
461 && ((!ppath && !csinfo[i].ppath)
462 || (ppath
463 && csinfo[i].ppath
464 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
465 return TRUE;
466 break;
467 }
468 }
469
470 return FALSE;
471} /* cs_connection */
472#endif
473
474
475/*
476 * PRIVATE functions
477 ****************************************************************************/
478
479/*
480 * PRIVATE: cs_add
481 *
482 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000483 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 *
485 * MAXPATHL 256
486 */
487/* ARGSUSED */
488 static int
489cs_add(eap)
490 exarg_T *eap;
491{
492 char *fname, *ppath, *flags = NULL;
493
494 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
495 {
496 cs_usage_msg(Add);
497 return CSCOPE_FAILURE;
498 }
499 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
500 flags = strtok((char *)NULL, (const char *)" ");
501
502 return cs_add_common(fname, ppath, flags);
503}
504
505 static void
506cs_stat_emsg(fname)
507 char *fname;
508{
509 char *stat_emsg = _("E563: stat(%s) error: %d");
510 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
511
512 if (buf != NULL)
513 {
514 (void)sprintf(buf, stat_emsg, fname, errno);
515 (void)EMSG(buf);
516 vim_free(buf);
517 }
518 else
519 (void)EMSG(_("E563: stat error"));
520}
521
522
523/*
524 * PRIVATE: cs_add_common
525 *
526 * the common routine to add a new cscope connection. called by
527 * cs_add() and cs_reset(). i really don't like to do this, but this
528 * routine uses a number of goto statements.
529 */
530 static int
531cs_add_common(arg1, arg2, flags)
532 char *arg1; /* filename - may contain environment variables */
533 char *arg2; /* prepend path - may contain environment variables */
534 char *flags;
535{
536 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000537 int ret;
538 char *fname = NULL;
539 char *fname2 = NULL;
540 char *ppath = NULL;
541 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000544 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 goto add_err;
546
547 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
548 ret = stat(fname, &statbuf);
549 if (ret < 0)
550 {
551staterr:
552 if (p_csverbose)
553 cs_stat_emsg(fname);
554 goto add_err;
555 }
556
557 /* get the prepend path (arg2), expand it, and try to stat it */
558 if (arg2 != NULL)
559 {
560 struct stat statbuf2;
561
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000562 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 goto add_err;
564
565 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
566 ret = stat(ppath, &statbuf2);
567 if (ret < 0)
568 goto staterr;
569 }
570
571 /* if filename is a directory, append the cscope database name to it */
572 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
573 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000574 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (fname2 == NULL)
576 goto add_err;
577
578 while (fname[strlen(fname)-1] == '/'
579#ifdef WIN32
580 || fname[strlen(fname)-1] == '\\'
581#endif
582 )
583 {
584 fname[strlen(fname)-1] = '\0';
585 if (strlen(fname) == 0)
586 break;
587 }
588 if (fname[0] == '\0')
589 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
590 else
591 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
592
593 ret = stat(fname2, &statbuf);
594 if (ret < 0)
595 {
596 if (p_csverbose)
597 cs_stat_emsg(fname2);
598 goto add_err;
599 }
600
601 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
602 }
603#if defined(UNIX)
604 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
605#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000606 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
608#endif
609 {
610 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
611 }
612 else
613 {
614 if (p_csverbose)
615 (void)EMSG2(
616 _("E564: %s is not a directory or a valid cscope database"),
617 fname);
618 goto add_err;
619 }
620
621 if (i != -1)
622 {
623 if (cs_create_connection(i) == CSCOPE_FAILURE
624 || cs_read_prompt(i) == CSCOPE_FAILURE)
625 {
626 cs_release_csp(i, TRUE);
627 goto add_err;
628 }
629
630 if (p_csverbose)
631 {
632 msg_clr_eos();
633 (void)smsg_attr(hl_attr(HLF_R),
634 (char_u *)_("Added cscope database %s"),
635 csinfo[i].fname);
636 }
637 }
638
639 vim_free(fname);
640 vim_free(fname2);
641 vim_free(ppath);
642 return CSCOPE_SUCCESS;
643
644add_err:
645 vim_free(fname2);
646 vim_free(fname);
647 vim_free(ppath);
648 return CSCOPE_FAILURE;
649} /* cs_add_common */
650
651
652 static int
653cs_check_for_connections()
654{
655 return (cs_cnt_connections() > 0);
656} /* cs_check_for_connections */
657
658
659 static int
660cs_check_for_tags()
661{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000662 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663} /* cs_check_for_tags */
664
665
666/*
667 * PRIVATE: cs_cnt_connections
668 *
669 * count the number of cscope connections
670 */
671 static int
672cs_cnt_connections()
673{
674 short i;
675 short cnt = 0;
676
677 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
678 {
679 if (csinfo[i].fname != NULL)
680 cnt++;
681 }
682 return cnt;
683} /* cs_cnt_connections */
684
685 static void
686cs_reading_emsg(idx)
687 int idx; /* connection index */
688{
689 EMSGN(_("E262: error reading cscope connection %ld"), idx);
690}
691
692#define CSREAD_BUFSIZE 2048
693/*
694 * PRIVATE: cs_cnt_matches
695 *
696 * count the number of matches for a given cscope connection.
697 */
698 static int
699cs_cnt_matches(idx)
700 int idx;
701{
702 char *stok;
703 char *buf;
704 int nlines;
705
706 buf = (char *)alloc(CSREAD_BUFSIZE);
707 if (buf == NULL)
708 return 0;
709 for (;;)
710 {
711 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
712 {
713 if (feof(csinfo[idx].fr_fp))
714 errno = EIO;
715
716 cs_reading_emsg(idx);
717
718 vim_free(buf);
719 return -1;
720 }
721
722 /*
723 * If the database is out of date, or there's some other problem,
724 * cscope will output error messages before the number-of-lines output.
725 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000726 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
728 if ((stok = strtok(buf, (const char *)" ")) == NULL)
729 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000730 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 continue;
732
733 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
734 continue;
735 nlines = atoi(stok);
736 if (nlines < 0)
737 {
738 nlines = 0;
739 break;
740 }
741
742 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
743 continue;
744 if (strncmp((const char *)stok, "lines", 5))
745 continue;
746
747 break;
748 }
749
750 vim_free(buf);
751 return nlines;
752} /* cs_cnt_matches */
753
754
755/*
756 * PRIVATE: cs_create_cmd
757 *
758 * Creates the actual cscope command query from what the user entered.
759 */
760 static char *
761cs_create_cmd(csoption, pattern)
762 char *csoption;
763 char *pattern;
764{
765 char *cmd;
766 short search;
767
768 switch (csoption[0])
769 {
770 case '0' : case 's' :
771 search = 0;
772 break;
773 case '1' : case 'g' :
774 search = 1;
775 break;
776 case '2' : case 'd' :
777 search = 2;
778 break;
779 case '3' : case 'c' :
780 search = 3;
781 break;
782 case '4' : case 't' :
783 search = 4;
784 break;
785 case '6' : case 'e' :
786 search = 6;
787 break;
788 case '7' : case 'f' :
789 search = 7;
790 break;
791 case '8' : case 'i' :
792 search = 8;
793 break;
794 default :
795 (void)EMSG(_("E561: unknown cscope search type"));
796 cs_usage_msg(Find);
797 return NULL;
798 }
799
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000800 if ((cmd = (char *)alloc((unsigned)(strlen(pattern) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 return NULL;
802
803 (void)sprintf(cmd, "%d%s", search, pattern);
804
805 return cmd;
806} /* cs_create_cmd */
807
808
809/*
810 * PRIVATE: cs_create_connection
811 *
812 * This piece of code was taken/adapted from nvi. do we need to add
813 * the BSD license notice?
814 */
815 static int
816cs_create_connection(i)
817 int i;
818{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000819#ifdef UNIX
820 int to_cs[2], from_cs[2];
821#endif
822 int len;
823 char *prog, *cmd, *ppath = NULL;
824#ifdef WIN32
825 int fd;
826 SECURITY_ATTRIBUTES sa;
827 PROCESS_INFORMATION pi;
828 STARTUPINFO si;
829 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
830 HANDLE stdin_rd, stdout_rd;
831 HANDLE stdout_wr, stdin_wr;
832 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000833# ifdef __BORLANDC__
834# define OPEN_OH_ARGTYPE long
835# else
836# if (_MSC_VER >= 1300)
837# define OPEN_OH_ARGTYPE intptr_t
838# else
839# define OPEN_OH_ARGTYPE long
840# endif
841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842#endif
843
Bram Moolenaar02b06312007-09-06 15:39:22 +0000844#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 /*
846 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
847 * from_cs[0] and writes to to_cs[1].
848 */
849 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
850 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
851 {
852 (void)EMSG(_("E566: Could not create cscope pipes"));
853err_closing:
854 if (to_cs[0] != -1)
855 (void)close(to_cs[0]);
856 if (to_cs[1] != -1)
857 (void)close(to_cs[1]);
858 if (from_cs[0] != -1)
859 (void)close(from_cs[0]);
860 if (from_cs[1] != -1)
861 (void)close(from_cs[1]);
862 return CSCOPE_FAILURE;
863 }
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 switch (csinfo[i].pid = fork())
866 {
867 case -1:
868 (void)EMSG(_("E622: Could not fork for cscope"));
869 goto err_closing;
870 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (dup2(to_cs[0], STDIN_FILENO) == -1)
872 PERROR("cs_create_connection 1");
873 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
874 PERROR("cs_create_connection 2");
875 if (dup2(from_cs[1], STDERR_FILENO) == -1)
876 PERROR("cs_create_connection 3");
877
878 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 (void)close(to_cs[1]);
880 (void)close(from_cs[0]);
881#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000882 /* WIN32 */
883 /* Create pipes to communicate with cscope */
884 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
885 sa.bInheritHandle = TRUE;
886 sa.lpSecurityDescriptor = NULL;
887
888 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
889 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
890 {
891 (void)EMSG(_("E566: Could not create cscope pipes"));
892err_closing:
893 if (pipe_stdin)
894 {
895 CloseHandle(stdin_rd);
896 CloseHandle(stdin_wr);
897 }
898 if (pipe_stdout)
899 {
900 CloseHandle(stdout_rd);
901 CloseHandle(stdout_wr);
902 }
903 return CSCOPE_FAILURE;
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#endif
906 /* expand the cscope exec for env var's */
907 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
908 {
909#ifdef UNIX
910 return CSCOPE_FAILURE;
911#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000912 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 goto err_closing;
914#endif
915 }
916 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
917
918 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000919 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (csinfo[i].ppath)
921 {
922 /* expand the prepend path for env var's */
923 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
924 {
925 vim_free(prog);
926#ifdef UNIX
927 return CSCOPE_FAILURE;
928#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000929 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 goto err_closing;
931#endif
932 }
933 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
934
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000935 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937
938 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000939 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 if ((cmd = (char *)alloc(len)) == NULL)
942 {
943 vim_free(prog);
944 vim_free(ppath);
945#ifdef UNIX
946 return CSCOPE_FAILURE;
947#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000948 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 goto err_closing;
950#endif
951 }
952
953 /* run the cscope command; is there execl for non-unix systems? */
954#if defined(UNIX)
955 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
956#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000957 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
959#endif
960 if (csinfo[i].ppath != NULL)
961 {
962 (void)strcat(cmd, " -P");
963 (void)strcat(cmd, csinfo[i].ppath);
964 }
965 if (csinfo[i].flags != NULL)
966 {
967 (void)strcat(cmd, " ");
968 (void)strcat(cmd, csinfo[i].flags);
969 }
970# ifdef UNIX
971 /* on Win32 we still need prog */
972 vim_free(prog);
973# endif
974 vim_free(ppath);
975
976#if defined(UNIX)
977 if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
978 PERROR(_("cs_create_connection exec failed"));
979
980 exit(127);
981 /* NOTREACHED */
982 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 /*
984 * Save the file descriptors for later duplication, and
985 * reopen as streams.
986 */
987 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
988 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
989 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
990 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 /* close unused */
993 (void)close(to_cs[0]);
994 (void)close(from_cs[1]);
995
996 break;
997 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001000 /* WIN32 */
1001 /* Create a new process to run cscope and use pipes to talk with it */
1002 GetStartupInfo(&si);
1003 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1004 si.wShowWindow = SW_HIDE; /* Hide child application window */
1005 si.hStdOutput = stdout_wr;
1006 si.hStdError = stdout_wr;
1007 si.hStdInput = stdin_rd;
1008 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1009 NULL, NULL, &si, &pi);
1010 vim_free(prog);
1011 vim_free(cmd);
1012
1013 if (!created)
1014 {
1015 PERROR(_("cs_create_connection exec failed"));
1016 (void)EMSG(_("E623: Could not spawn cscope process"));
1017 goto err_closing;
1018 }
1019 /* else */
1020 csinfo[i].pid = pi.dwProcessId;
1021 csinfo[i].hProc = pi.hProcess;
1022 CloseHandle(pi.hThread);
1023
1024 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001025 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1026 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001027 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1028 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001029 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1030 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001031 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1032 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1033
1034 /* Close handles for file descriptors inherited by the cscope process */
1035 CloseHandle(stdin_rd);
1036 CloseHandle(stdout_wr);
1037
1038#endif /* !UNIX */
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 return CSCOPE_SUCCESS;
1041} /* cs_create_connection */
1042
1043
1044/*
1045 * PRIVATE: cs_find
1046 *
1047 * query cscope using command line interface. parse the output and use tselect
1048 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1049 *
1050 * returns TRUE if we jump to a tag or abort, FALSE if not.
1051 */
1052 static int
1053cs_find(eap)
1054 exarg_T *eap;
1055{
1056 char *opt, *pat;
1057
1058 if (cs_check_for_connections() == FALSE)
1059 {
1060 (void)EMSG(_("E567: no cscope connections"));
1061 return FALSE;
1062 }
1063
1064 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1065 {
1066 cs_usage_msg(Find);
1067 return FALSE;
1068 }
1069
1070 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001071 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 cs_usage_msg(Find);
1074 return FALSE;
1075 }
1076
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001077 return cs_find_common(opt, pat, eap->forceit, TRUE,
1078 eap->cmdidx == CMD_lcscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079} /* cs_find */
1080
1081
1082/*
1083 * PRIVATE: cs_find_common
1084 *
1085 * common code for cscope find, shared by cs_find() and do_cstag()
1086 */
1087 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001088cs_find_common(opt, pat, forceit, verbose, use_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 char *opt;
1090 char *pat;
1091 int forceit;
1092 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001093 int use_ll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 int i;
1096 char *cmd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001097 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#ifdef FEAT_QUICKFIX
1099 char cmdletter;
1100 char *qfpos;
1101#endif
1102
1103 /* create the actual command to send to cscope */
1104 cmd = cs_create_cmd(opt, pat);
1105 if (cmd == NULL)
1106 return FALSE;
1107
1108 /* send query to all open connections, then count the total number
1109 * of matches so we can alloc matchesp all in one swell foop
1110 */
1111 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1112 nummatches[i] = 0;
1113 totmatches = 0;
1114 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1115 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001116 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 continue;
1118
1119 /* send cmd to cscope */
1120 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1121 (void)fflush(csinfo[i].to_fp);
1122
1123 nummatches[i] = cs_cnt_matches(i);
1124
1125 if (nummatches[i] > -1)
1126 totmatches += nummatches[i];
1127
1128 if (nummatches[i] == 0)
1129 (void)cs_read_prompt(i);
1130 }
1131 vim_free(cmd);
1132
1133 if (totmatches == 0)
1134 {
1135 char *nf = _("E259: no matches found for cscope query %s of %s");
1136 char *buf;
1137
1138 if (!verbose)
1139 return FALSE;
1140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001141 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (buf == NULL)
1143 (void)EMSG(nf);
1144 else
1145 {
1146 sprintf(buf, nf, opt, pat);
1147 (void)EMSG(buf);
1148 vim_free(buf);
1149 }
1150 return FALSE;
1151 }
1152
1153#ifdef FEAT_QUICKFIX
1154 /* get cmd letter */
1155 switch (opt[0])
1156 {
1157 case '0' :
1158 cmdletter = 's';
1159 break;
1160 case '1' :
1161 cmdletter = 'g';
1162 break;
1163 case '2' :
1164 cmdletter = 'd';
1165 break;
1166 case '3' :
1167 cmdletter = 'c';
1168 break;
1169 case '4' :
1170 cmdletter = 't';
1171 break;
1172 case '6' :
1173 cmdletter = 'e';
1174 break;
1175 case '7' :
1176 cmdletter = 'f';
1177 break;
1178 case '8' :
1179 cmdletter = 'i';
1180 break;
1181 default :
1182 cmdletter = opt[0];
1183 }
1184
1185 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1186 if (qfpos != NULL)
1187 {
1188 qfpos++;
1189 /* next symbol must be + or - */
1190 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1191 {
1192 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001193 char *buf = (char *)alloc((unsigned)strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 /* strlen will be enough because we use chars */
1196 if (buf != NULL)
1197 {
1198 sprintf(buf, nf, *qfpos, *(qfpos-1));
1199 (void)EMSG(buf);
1200 vim_free(buf);
1201 }
1202 return FALSE;
1203 }
1204 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001205 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001208 FILE *f;
1209 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001210 qf_info_T *qi = NULL;
1211 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001213 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001214 if (f == NULL)
1215 EMSG2(_(e_notopen), tmp);
1216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001218 cs_file_results(f, nummatches);
1219 fclose(f);
1220 if (use_ll) /* Use location list */
1221 wp = curwin;
1222 /* '-' starts a new error list */
1223 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1224 *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001226# ifdef FEAT_WINDOWS
1227 if (postponed_split != 0)
1228 {
1229 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 postponed_split_flags);
1231# ifdef FEAT_SCROLLBIND
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001232 curwin->w_p_scb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001234 postponed_split = 0;
1235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001237 if (use_ll)
1238 /*
1239 * In the location list window, use the displayed location
1240 * list. Otherwise, use the location list for the window.
1241 */
1242 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1243 ? wp->w_llist_ref : wp->w_llist;
1244 qf_jump(qi, 0, 0, forceit);
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 }
1247 mch_remove(tmp);
1248 vim_free(tmp);
1249 return TRUE;
1250 }
1251 else
1252#endif /* FEAT_QUICKFIX */
1253 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001254 char **matches = NULL, **contexts = NULL;
1255 int matched = 0;
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /* read output */
1258 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1259 &contexts, &matched);
1260 if (matches == NULL)
1261 return FALSE;
1262
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001263 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1266 }
1267
1268} /* cs_find_common */
1269
1270/*
1271 * PRIVATE: cs_help
1272 *
1273 * print help
1274 */
1275/* ARGSUSED */
1276 static int
1277cs_help(eap)
1278 exarg_T *eap;
1279{
1280 cscmd_T *cmdp = cs_cmds;
1281
1282 (void)MSG_PUTS(_("cscope commands:\n"));
1283 while (cmdp->name != NULL)
1284 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001285 char *help = _(cmdp->help);
1286 int space_cnt = 30 - vim_strsize((char_u *)help);
1287
1288 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1289 if (space_cnt < 0)
1290 space_cnt = 0;
1291 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1292 cmdp->name,
1293 help, space_cnt, " ",
1294 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001296 MSG_PUTS(_("\n"
1297 " c: Find functions calling this function\n"
1298 " d: Find functions called by this function\n"
1299 " e: Find this egrep pattern\n"
1300 " f: Find this file\n"
1301 " g: Find this definition\n"
1302 " i: Find files #including this file\n"
1303 " s: Find this C symbol\n"
1304 " t: Find assignments to\n"));
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 cmdp++;
1307 }
1308
1309 wait_return(TRUE);
1310 return 0;
1311} /* cs_help */
1312
1313
1314/*
1315 * PRIVATE: cs_init
1316 *
1317 * initialize cscope structure if not already
1318 */
1319 static void
1320cs_init()
1321{
1322 short i;
1323 static int init_already = FALSE;
1324
1325 if (init_already)
1326 return;
1327
1328 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1329 clear_csinfo(i);
1330
1331 init_already = TRUE;
1332} /* cs_init */
1333
1334 static void
1335clear_csinfo(i)
1336 int i;
1337{
1338 csinfo[i].fname = NULL;
1339 csinfo[i].ppath = NULL;
1340 csinfo[i].flags = NULL;
1341#if defined(UNIX)
1342 csinfo[i].st_dev = (dev_t)0;
1343 csinfo[i].st_ino = (ino_t)0;
1344#else
1345 csinfo[i].nVolume = 0;
1346 csinfo[i].nIndexHigh = 0;
1347 csinfo[i].nIndexLow = 0;
1348#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001349 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 csinfo[i].fr_fp = NULL;
1351 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001352#if defined(WIN32)
1353 csinfo[i].hProc = NULL;
1354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355}
1356
1357#ifndef UNIX
1358static char *GetWin32Error __ARGS((void));
1359
1360 static char *
1361GetWin32Error()
1362{
1363 char *msg = NULL;
1364 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1365 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1366 if (msg != NULL)
1367 {
1368 /* remove trailing \r\n */
1369 char *pcrlf = strstr(msg, "\r\n");
1370 if (pcrlf != NULL)
1371 *pcrlf = '\0';
1372 }
1373 return msg;
1374}
1375#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377/*
1378 * PRIVATE: cs_insert_filelist
1379 *
1380 * insert a new cscope database filename into the filelist
1381 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001382/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static int
1384cs_insert_filelist(fname, ppath, flags, sb)
1385 char *fname;
1386 char *ppath;
1387 char *flags;
1388 struct stat *sb;
1389{
1390 short i, j;
1391#ifndef UNIX
1392 HANDLE hFile;
1393 BY_HANDLE_FILE_INFORMATION bhfi;
1394
1395 vim_memset(&bhfi, 0, sizeof(bhfi));
1396 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1397 if (!mch_windows95())
1398 {
1399 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1400 FILE_ATTRIBUTE_NORMAL, NULL);
1401 if (hFile == INVALID_HANDLE_VALUE)
1402 {
1403 if (p_csverbose)
1404 {
1405 char *cant_msg = _("E625: cannot open cscope database: %s");
1406 char *winmsg = GetWin32Error();
1407
1408 if (winmsg != NULL)
1409 {
1410 (void)EMSG2(cant_msg, winmsg);
1411 LocalFree(winmsg);
1412 }
1413 else
1414 /* subst filename if can't get error text */
1415 (void)EMSG2(cant_msg, fname);
1416 }
1417 return -1;
1418 }
1419 if (!GetFileInformationByHandle(hFile, &bhfi))
1420 {
1421 CloseHandle(hFile);
1422 if (p_csverbose)
1423 (void)EMSG(_("E626: cannot get cscope database information"));
1424 return -1;
1425 }
1426 CloseHandle(hFile);
1427 }
1428#endif
1429
1430 i = -1; /* can be set to the index of an empty item in csinfo */
1431 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1432 {
1433 if (csinfo[j].fname != NULL
1434#if defined(UNIX)
1435 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1436#else
1437 /* compare pathnames first */
1438 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001439 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 || (!mch_windows95()
1441 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1442 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1443 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1444#endif
1445 )
1446 {
1447 if (p_csverbose)
1448 (void)EMSG(_("E568: duplicate cscope database not added"));
1449 return -1;
1450 }
1451
1452 if (csinfo[j].fname == NULL && i == -1)
1453 i = j; /* remember first empty entry */
1454 }
1455
1456 if (i == -1)
1457 {
1458 if (p_csverbose)
1459 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1460 return -1;
1461 }
1462
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001463 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return -1;
1465
1466 (void)strcpy(csinfo[i].fname, (const char *)fname);
1467
1468 if (ppath != NULL)
1469 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001470 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472 vim_free(csinfo[i].fname);
1473 csinfo[i].fname = NULL;
1474 return -1;
1475 }
1476 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1477 } else
1478 csinfo[i].ppath = NULL;
1479
1480 if (flags != NULL)
1481 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001482 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 vim_free(csinfo[i].fname);
1485 vim_free(csinfo[i].ppath);
1486 csinfo[i].fname = NULL;
1487 csinfo[i].ppath = NULL;
1488 return -1;
1489 }
1490 (void)strcpy(csinfo[i].flags, (const char *)flags);
1491 } else
1492 csinfo[i].flags = NULL;
1493
1494#if defined(UNIX)
1495 csinfo[i].st_dev = sb->st_dev;
1496 csinfo[i].st_ino = sb->st_ino;
1497
1498#else
1499 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1500 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1501 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1502#endif
1503 return i;
1504} /* cs_insert_filelist */
1505
1506
1507/*
1508 * PRIVATE: cs_lookup_cmd
1509 *
1510 * find cscope command in command table
1511 */
1512 static cscmd_T *
1513cs_lookup_cmd(eap)
1514 exarg_T *eap;
1515{
1516 cscmd_T *cmdp;
1517 char *stok;
1518 size_t len;
1519
1520 if (eap->arg == NULL)
1521 return NULL;
1522
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001523 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001524 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1527 return NULL;
1528
1529 len = strlen(stok);
1530 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1531 {
1532 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1533 return (cmdp);
1534 }
1535 return NULL;
1536} /* cs_lookup_cmd */
1537
1538
1539/*
1540 * PRIVATE: cs_kill
1541 *
1542 * nuke em
1543 */
1544/* ARGSUSED */
1545 static int
1546cs_kill(eap)
1547 exarg_T *eap;
1548{
1549 char *stok;
1550 short i;
1551
1552 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1553 {
1554 cs_usage_msg(Kill);
1555 return CSCOPE_FAILURE;
1556 }
1557
1558 /* only single digit positive and negative integers are allowed */
1559 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1560 || (strlen(stok) < 3 && stok[0] == '-'
1561 && VIM_ISDIGIT((int)(stok[1]))))
1562 i = atoi(stok);
1563 else
1564 {
1565 /* It must be part of a name. We will try to find a match
1566 * within all the names in the csinfo data structure
1567 */
1568 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1569 {
1570 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1571 break;
1572 }
1573 }
1574
1575 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1576 && i != -1)
1577 {
1578 if (p_csverbose)
1579 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1580 }
1581 else
1582 {
1583 if (i == -1)
1584 {
1585 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1586 {
1587 if (csinfo[i].fname)
1588 cs_kill_execute(i, csinfo[i].fname);
1589 }
1590 }
1591 else
1592 cs_kill_execute(i, stok);
1593 }
1594
1595 return 0;
1596} /* cs_kill */
1597
1598
1599/*
1600 * PRIVATE: cs_kill_execute
1601 *
1602 * Actually kills a specific cscope connection.
1603 */
1604 static void
1605cs_kill_execute(i, cname)
1606 int i; /* cscope table index */
1607 char *cname; /* cscope database name */
1608{
1609 if (p_csverbose)
1610 {
1611 msg_clr_eos();
1612 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1613 (char_u *)_("cscope connection %s closed"), cname);
1614 }
1615 cs_release_csp(i, TRUE);
1616}
1617
1618
1619/*
1620 * PRIVATE: cs_make_vim_style_matches
1621 *
1622 * convert the cscope output into into a ctags style entry (as might be found
1623 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1624 * the type of the tag you are looking for. for example, in Darren Hiebert's
1625 * ctags (the one that comes with vim), #define's use a line number to find the
1626 * tag in a file while function definitions use a regexp search pattern.
1627 *
1628 * i'm going to always use the line number because cscope does something
1629 * quirky (and probably other things i don't know about):
1630 *
1631 * if you have "# define" in your source file, which is
1632 * perfectly legal, cscope thinks you have "#define". this
1633 * will result in a failed regexp search. :(
1634 *
1635 * besides, even if this particular case didn't happen, the search pattern
1636 * would still have to be modified to escape all the special regular expression
1637 * characters to comply with ctags formatting.
1638 */
1639 static char *
1640cs_make_vim_style_matches(fname, slno, search, tagstr)
1641 char *fname;
1642 char *slno;
1643 char *search;
1644 char *tagstr;
1645{
1646 /* vim style is ctags:
1647 *
1648 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1649 *
1650 * but as mentioned above, we'll always use the line number and
1651 * put the search pattern (if one exists) as "extra"
1652 *
1653 * buf is used as part of vim's method of handling tags, and
1654 * (i think) vim frees it when you pop your tags and get replaced
1655 * by new ones on the tag stack.
1656 */
1657 char *buf;
1658 int amt;
1659
1660 if (search != NULL)
1661 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001662 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if ((buf = (char *)alloc(amt)) == NULL)
1664 return NULL;
1665
1666 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1667 }
1668 else
1669 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001670 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if ((buf = (char *)alloc(amt)) == NULL)
1672 return NULL;
1673
1674 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1675 }
1676
1677 return buf;
1678} /* cs_make_vim_style_matches */
1679
1680
1681/*
1682 * PRIVATE: cs_manage_matches
1683 *
1684 * this is kind of hokey, but i don't see an easy way round this..
1685 *
1686 * Store: keep a ptr to the (malloc'd) memory of matches originally
1687 * generated from cs_find(). the matches are originally lines directly
1688 * from cscope output, but transformed to look like something out of a
1689 * ctags. see cs_make_vim_style_matches for more details.
1690 *
1691 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1692 * the next line from the cscope output. it basically keeps track of which
1693 * lines have been "used" and returns the next one.
1694 *
1695 * Free: frees up everything and resets
1696 *
1697 * Print: prints the tags
1698 */
1699 static char *
1700cs_manage_matches(matches, contexts, totmatches, cmd)
1701 char **matches;
1702 char **contexts;
1703 int totmatches;
1704 mcmd_e cmd;
1705{
1706 static char **mp = NULL;
1707 static char **cp = NULL;
1708 static int cnt = -1;
1709 static int next = -1;
1710 char *p = NULL;
1711
1712 switch (cmd)
1713 {
1714 case Store:
1715 assert(matches != NULL);
1716 assert(totmatches > 0);
1717 if (mp != NULL || cp != NULL)
1718 (void)cs_manage_matches(NULL, NULL, -1, Free);
1719 mp = matches;
1720 cp = contexts;
1721 cnt = totmatches;
1722 next = 0;
1723 break;
1724 case Get:
1725 if (next >= cnt)
1726 return NULL;
1727
1728 p = mp[next];
1729 next++;
1730 break;
1731 case Free:
1732 if (mp != NULL)
1733 {
1734 if (cnt > 0)
1735 while (cnt--)
1736 {
1737 vim_free(mp[cnt]);
1738 if (cp != NULL)
1739 vim_free(cp[cnt]);
1740 }
1741 vim_free(mp);
1742 vim_free(cp);
1743 }
1744 mp = NULL;
1745 cp = NULL;
1746 cnt = 0;
1747 next = 0;
1748 break;
1749 case Print:
1750 cs_print_tags_priv(mp, cp, cnt);
1751 break;
1752 default: /* should not reach here */
1753 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1754 return NULL;
1755 }
1756
1757 return p;
1758} /* cs_manage_matches */
1759
1760
1761/*
1762 * PRIVATE: cs_parse_results
1763 *
1764 * parse cscope output
1765 */
1766 static char *
1767cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1768 int cnumber;
1769 char *buf;
1770 int bufsize;
1771 char **context;
1772 char **linenumber;
1773 char **search;
1774{
1775 int ch;
1776 char *p;
1777 char *name;
1778
1779 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1780 {
1781 if (feof(csinfo[cnumber].fr_fp))
1782 errno = EIO;
1783
1784 cs_reading_emsg(cnumber);
1785
1786 return NULL;
1787 }
1788
1789 /* If the line's too long for the buffer, discard it. */
1790 if ((p = strchr(buf, '\n')) == NULL)
1791 {
1792 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1793 ;
1794 return NULL;
1795 }
1796 *p = '\0';
1797
1798 /*
1799 * cscope output is in the following format:
1800 *
1801 * <filename> <context> <line number> <pattern>
1802 */
1803 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1804 return NULL;
1805 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1806 return NULL;
1807 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1808 return NULL;
1809 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1810
1811 /* --- nvi ---
1812 * If the file is older than the cscope database, that is,
1813 * the database was built since the file was last modified,
1814 * or there wasn't a search string, use the line number.
1815 */
1816 if (strcmp(*search, "<unknown>") == 0)
1817 *search = NULL;
1818
1819 name = cs_resolve_file(cnumber, name);
1820 return name;
1821}
1822
Bram Moolenaarc716c302006-01-21 22:12:51 +00001823#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824/*
1825 * PRIVATE: cs_file_results
1826 *
1827 * write cscope find results to file
1828 */
1829 static void
1830cs_file_results(f, nummatches_a)
1831 FILE *f;
1832 int *nummatches_a;
1833{
1834 int i, j;
1835 char *buf;
1836 char *search, *slno;
1837 char *fullname;
1838 char *cntx;
1839 char *context;
1840
1841 buf = (char *)alloc(CSREAD_BUFSIZE);
1842 if (buf == NULL)
1843 return;
1844
1845 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1846 {
1847 if (nummatches_a[i] < 1)
1848 continue;
1849
1850 for (j = 0; j < nummatches_a[i]; j++)
1851 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001852 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1853 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 continue;
1855
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001856 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001857 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 continue;
1859
1860 if (strcmp(cntx, "<global>")==0)
1861 strcpy(context, "<<global>>");
1862 else
1863 sprintf(context, "<<%s>>", cntx);
1864
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001865 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1867 else
1868 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1869
1870 vim_free(context);
1871 vim_free(fullname);
1872 } /* for all matches */
1873
1874 (void)cs_read_prompt(i);
1875
1876 } /* for all cscope connections */
1877 vim_free(buf);
1878}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881/*
1882 * PRIVATE: cs_fill_results
1883 *
1884 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1885 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001886 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 static void
1889cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1890 char *tagstr;
1891 int totmatches;
1892 int *nummatches_a;
1893 char ***matches_p;
1894 char ***cntxts_p;
1895 int *matched;
1896{
1897 int i, j;
1898 char *buf;
1899 char *search, *slno;
1900 int totsofar = 0;
1901 char **matches = NULL;
1902 char **cntxts = NULL;
1903 char *fullname;
1904 char *cntx;
1905
1906 assert(totmatches > 0);
1907
1908 buf = (char *)alloc(CSREAD_BUFSIZE);
1909 if (buf == NULL)
1910 return;
1911
1912 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1913 goto parse_out;
1914 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1915 goto parse_out;
1916
1917 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1918 {
1919 if (nummatches_a[i] < 1)
1920 continue;
1921
1922 for (j = 0; j < nummatches_a[i]; j++)
1923 {
1924 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1925 &slno, &search)) == NULL)
1926 continue;
1927
1928 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1929 search, tagstr);
1930
1931 vim_free(fullname);
1932
1933 if (strcmp(cntx, "<global>") == 0)
1934 cntxts[totsofar] = NULL;
1935 else
1936 /* note: if vim_strsave returns NULL, then the context
1937 * will be "<global>", which is misleading.
1938 */
1939 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1940
1941 if (matches[totsofar] != NULL)
1942 totsofar++;
1943
1944 } /* for all matches */
1945
1946 (void)cs_read_prompt(i);
1947
1948 } /* for all cscope connections */
1949
1950parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001951 if (totsofar == 0)
1952 {
1953 /* No matches, free the arrays and return NULL in "*matches_p". */
1954 vim_free(matches);
1955 matches = NULL;
1956 vim_free(cntxts);
1957 cntxts = NULL;
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 *matched = totsofar;
1960 *matches_p = matches;
1961 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 vim_free(buf);
1964} /* cs_fill_results */
1965
1966
1967/* get the requested path components */
1968 static char *
1969cs_pathcomponents(path)
1970 char *path;
1971{
1972 int i;
1973 char *s;
1974
1975 if (p_cspc == 0)
1976 return path;
1977
1978 s = path + strlen(path) - 1;
1979 for (i = 0; i < p_cspc; ++i)
1980 while (s > path && *--s != '/'
1981#ifdef WIN32
1982 && *--s != '\\'
1983#endif
1984 )
1985 ;
1986 if ((s > path && *s == '/')
1987#ifdef WIN32
1988 || (s > path && *s == '\\')
1989#endif
1990 )
1991 ++s;
1992 return s;
1993}
1994
1995/*
1996 * PRIVATE: cs_print_tags_priv
1997 *
1998 * called from cs_manage_matches()
1999 */
2000 static void
2001cs_print_tags_priv(matches, cntxts, num_matches)
2002 char **matches;
2003 char **cntxts;
2004 int num_matches;
2005{
2006 char *buf = NULL;
2007 int bufsize = 0; /* Track available bufsize */
2008 int newsize = 0;
2009 char *ptag;
2010 char *fname, *lno, *extra, *tbuf;
2011 int i, idx, num;
2012 char *globalcntx = "GLOBAL";
2013 char *cntxformat = " <<%s>>";
2014 char *context;
2015 char *cstag_msg = _("Cscope tag: %s");
2016 char *csfmt_str = "%4d %6s ";
2017
2018 assert (num_matches > 0);
2019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002020 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 return;
2022
2023 strcpy(tbuf, matches[0]);
2024 ptag = strtok(tbuf, "\t");
2025
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002026 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 buf = (char *)alloc(newsize);
2028 if (buf != NULL)
2029 {
2030 bufsize = newsize;
2031 (void)sprintf(buf, cstag_msg, ptag);
2032 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2033 }
2034
2035 vim_free(tbuf);
2036
2037 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2038 msg_advance(msg_col + 2);
2039 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2040
2041 num = 1;
2042 for (i = 0; i < num_matches; i++)
2043 {
2044 idx = i;
2045
2046 /* if we really wanted to, we could avoid this malloc and strcpy
2047 * by parsing matches[i] on the fly and placing stuff into buf
2048 * directly, but that's too much of a hassle
2049 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002050 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 continue;
2052 (void)strcpy(tbuf, matches[idx]);
2053
2054 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
2055 continue;
2056 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2057 continue;
2058 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002059 continue;
2060 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2063
2064 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002065 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (bufsize < newsize)
2067 {
2068 buf = (char *)vim_realloc(buf, newsize);
2069 if (buf == NULL)
2070 bufsize = 0;
2071 else
2072 bufsize = newsize;
2073 }
2074 if (buf != NULL)
2075 {
2076 /* csfmt_str = "%4d %6s "; */
2077 (void)sprintf(buf, csfmt_str, num, lno);
2078 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2079 }
2080 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2081
2082 /* compute the required space for the context */
2083 if (cntxts[idx] != NULL)
2084 context = cntxts[idx];
2085 else
2086 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 if (bufsize < newsize)
2090 {
2091 buf = (char *)vim_realloc(buf, newsize);
2092 if (buf == NULL)
2093 bufsize = 0;
2094 else
2095 bufsize = newsize;
2096 }
2097 if (buf != NULL)
2098 {
2099 (void)sprintf(buf, cntxformat, context);
2100
2101 /* print the context only if it fits on the same line */
2102 if (msg_col + (int)strlen(buf) >= (int)Columns)
2103 msg_putchar('\n');
2104 msg_advance(12);
2105 MSG_PUTS_LONG(buf);
2106 msg_putchar('\n');
2107 }
2108 if (extra != NULL)
2109 {
2110 msg_advance(13);
2111 MSG_PUTS_LONG(extra);
2112 }
2113
2114 vim_free(tbuf); /* only after printing extra due to strtok use */
2115
2116 if (msg_col)
2117 msg_putchar('\n');
2118
2119 ui_breakcheck();
2120 if (got_int)
2121 {
2122 got_int = FALSE; /* don't print any more matches */
2123 break;
2124 }
2125
2126 num++;
2127 } /* for all matches */
2128
2129 vim_free(buf);
2130} /* cs_print_tags_priv */
2131
2132
2133/*
2134 * PRIVATE: cs_read_prompt
2135 *
2136 * read a cscope prompt (basically, skip over the ">> ")
2137 */
2138 static int
2139cs_read_prompt(i)
2140 int i;
2141{
2142 int ch;
2143 char *buf = NULL; /* buffer for possible error message from cscope */
2144 int bufpos = 0;
2145 char *cs_emsg;
2146 int maxlen;
2147 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 int n;
2150
2151 cs_emsg = _("E609: Cscope error: %s");
2152 /* compute maximum allowed len for Cscope error message */
2153 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2154
2155 for (;;)
2156 {
2157 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2158 /* if there is room and char is printable */
2159 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2160 {
2161 if (buf == NULL) /* lazy buffer allocation */
2162 buf = (char *)alloc(maxlen);
2163 if (buf != NULL)
2164 {
2165 /* append character to the message */
2166 buf[bufpos++] = ch;
2167 buf[bufpos] = NUL;
2168 if (bufpos >= epromptlen
2169 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2170 {
2171 /* remove eprompt from buf */
2172 buf[bufpos - epromptlen] = NUL;
2173
2174 /* print message to user */
2175 (void)EMSG2(cs_emsg, buf);
2176
2177 /* send RETURN to cscope */
2178 (void)putc('\n', csinfo[i].to_fp);
2179 (void)fflush(csinfo[i].to_fp);
2180
2181 /* clear buf */
2182 bufpos = 0;
2183 buf[bufpos] = NUL;
2184 }
2185 }
2186 }
2187
2188 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2189 {
2190 if (n > 0)
2191 ch = getc(csinfo[i].fr_fp);
2192 if (ch == EOF)
2193 {
2194 PERROR("cs_read_prompt EOF");
2195 if (buf != NULL && buf[0] != NUL)
2196 (void)EMSG2(cs_emsg, buf);
2197 else if (p_csverbose)
2198 cs_reading_emsg(i); /* don't have additional information */
2199 cs_release_csp(i, TRUE);
2200 vim_free(buf);
2201 return CSCOPE_FAILURE;
2202 }
2203
2204 if (ch != CSCOPE_PROMPT[n])
2205 {
2206 ch = EOF;
2207 break;
2208 }
2209 }
2210
2211 if (ch == EOF)
2212 continue; /* didn't find the prompt */
2213 break; /* did find the prompt */
2214 }
2215
2216 vim_free(buf);
2217 return CSCOPE_SUCCESS;
2218}
2219
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002220#if defined(UNIX) && defined(SIGALRM)
2221/*
2222 * Used to catch and ignore SIGALRM below.
2223 */
2224/* ARGSUSED */
2225 static RETSIGTYPE
2226sig_handler SIGDEFARG(sigarg)
2227{
2228 /* do nothing */
2229 SIGRETURN;
2230}
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233/*
2234 * PRIVATE: cs_release_csp
2235 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002236 * Does the actual free'ing for the cs ptr with an optional flag of whether
2237 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 static void
2240cs_release_csp(i, freefnpp)
2241 int i;
2242 int freefnpp;
2243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 /*
2245 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2246 */
2247 if (csinfo[i].to_fp != NULL)
2248 {
2249 (void)fputs("q\n", csinfo[i].to_fp);
2250 (void)fflush(csinfo[i].to_fp);
2251 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002252#if defined(UNIX)
2253 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002254 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255 int pstat;
2256 pid_t pid;
2257
2258# if defined(HAVE_SIGACTION)
2259 struct sigaction sa, old;
2260
Bram Moolenaar9701da02008-03-16 12:09:58 +00002261 /* Use sigaction() to limit the waiting time to two seconds. */
2262 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002263 sa.sa_handler = sig_handler;
2264 sa.sa_flags = SA_NODEFER;
2265 sigaction(SIGALRM, &sa, &old);
2266 alarm(2); /* 2 sec timeout */
2267
2268 /* Block until cscope exits or until timer expires */
2269 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002270 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002271
2272 /* cancel pending alarm if still there and restore signal */
2273 alarm(0);
2274 sigaction(SIGALRM, &old, NULL);
2275# else
2276 int waited;
2277
2278 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2279 * to give cscope a chance to exit quickly. */
2280 sleep(0);
2281 for (waited = 0; waited < 40; ++waited)
2282 {
2283 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002284 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002285 if (pid != 0)
2286 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002287 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002288 }
2289# endif
2290 /*
2291 * If the cscope process is still running: kill it.
2292 * Safety check: If the PID would be zero here, the entire X session
2293 * would be killed. -1 and 1 are dangerous as well.
2294 */
2295 if (pid < 0 && csinfo[i].pid > 1)
2296 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002297# ifdef ECHILD
2298 int alive = TRUE;
2299
2300 if (waitpid_errno == ECHILD)
2301 {
2302 /*
2303 * When using 'vim -g', vim is forked and cscope process is
2304 * no longer a child process but a sibling. So waitpid()
2305 * fails with errno being ECHILD (No child processes).
2306 * Don't send SIGKILL to cscope immediately but wait
2307 * (polling) for it to exit normally as result of sending
2308 * the "q" command, hence giving it a chance to clean up
2309 * its temporary files.
2310 */
2311 int waited;
2312
2313 sleep(0);
2314 for (waited = 0; waited < 40; ++waited)
2315 {
2316 /* Check whether cscope process is still alive */
2317 if (kill(csinfo[i].pid, 0) != 0)
2318 {
2319 alive = FALSE; /* cscope process no longer exists */
2320 break;
2321 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002322 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002323 }
2324 }
2325 if (alive)
2326# endif
2327 {
2328 kill(csinfo[i].pid, SIGKILL);
2329 (void)waitpid(csinfo[i].pid, &pstat, 0);
2330 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002331 }
2332 }
2333#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002334 if (csinfo[i].hProc != NULL)
2335 {
2336 /* Give cscope a chance to exit normally */
2337 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2338 TerminateProcess(csinfo[i].hProc, 0);
2339 CloseHandle(csinfo[i].hProc);
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342
2343 if (csinfo[i].fr_fp != NULL)
2344 (void)fclose(csinfo[i].fr_fp);
2345 if (csinfo[i].to_fp != NULL)
2346 (void)fclose(csinfo[i].to_fp);
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (freefnpp)
2349 {
2350 vim_free(csinfo[i].fname);
2351 vim_free(csinfo[i].ppath);
2352 vim_free(csinfo[i].flags);
2353 }
2354
2355 clear_csinfo(i);
2356} /* cs_release_csp */
2357
2358
2359/*
2360 * PRIVATE: cs_reset
2361 *
2362 * calls cs_kill on all cscope connections then reinits
2363 */
2364/* ARGSUSED */
2365 static int
2366cs_reset(eap)
2367 exarg_T *eap;
2368{
2369 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2370 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002371 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 /* malloc our db and ppath list */
2374 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2375 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2376 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2377 if (dblist == NULL || pplist == NULL || fllist == NULL)
2378 {
2379 vim_free(dblist);
2380 vim_free(pplist);
2381 vim_free(fllist);
2382 return CSCOPE_FAILURE;
2383 }
2384
2385 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2386 {
2387 dblist[i] = csinfo[i].fname;
2388 pplist[i] = csinfo[i].ppath;
2389 fllist[i] = csinfo[i].flags;
2390 if (csinfo[i].fname != NULL)
2391 cs_release_csp(i, FALSE);
2392 }
2393
2394 /* rebuild the cscope connection list */
2395 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2396 {
2397 if (dblist[i] != NULL)
2398 {
2399 cs_add_common(dblist[i], pplist[i], fllist[i]);
2400 if (p_csverbose)
2401 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002402 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 * connection number in the same line as
2404 * "Added cscope database..."
2405 */
2406 sprintf(buf, " (#%d)", i);
2407 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2408 }
2409 }
2410 vim_free(dblist[i]);
2411 vim_free(pplist[i]);
2412 vim_free(fllist[i]);
2413 }
2414 vim_free(dblist);
2415 vim_free(pplist);
2416 vim_free(fllist);
2417
2418 if (p_csverbose)
2419 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2420 return CSCOPE_SUCCESS;
2421} /* cs_reset */
2422
2423
2424/*
2425 * PRIVATE: cs_resolve_file
2426 *
2427 * construct the full pathname to a file found in the cscope database.
2428 * (Prepends ppath, if there is one and if it's not already prepended,
2429 * otherwise just uses the name found.)
2430 *
2431 * we need to prepend the prefix because on some cscope's (e.g., the one that
2432 * ships with Solaris 2.6), the output never has the prefix prepended.
2433 * contrast this with my development system (Digital Unix), which does.
2434 */
2435 static char *
2436cs_resolve_file(i, name)
2437 int i;
2438 char *name;
2439{
2440 char *fullname;
2441 int len;
2442
2443 /*
2444 * ppath is freed when we destroy the cscope connection.
2445 * fullname is freed after cs_make_vim_style_matches, after it's been
2446 * copied into the tag buffer used by vim
2447 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002448 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002450 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 if ((fullname = (char *)alloc(len)) == NULL)
2453 return NULL;
2454
2455 /*
2456 * note/example: this won't work if the cscope output already starts
2457 * "../.." and the prefix path is also "../..". if something like this
2458 * happens, you are screwed up and need to fix how you're using cscope.
2459 */
2460 if (csinfo[i].ppath != NULL &&
2461 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2462 (name[0] != '/')
2463#ifdef WIN32
2464 && name[0] != '\\' && name[1] != ':'
2465#endif
2466 )
2467 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2468 else
2469 (void)sprintf(fullname, "%s", name);
2470
2471 return fullname;
2472} /* cs_resolve_file */
2473
2474
2475/*
2476 * PRIVATE: cs_show
2477 *
2478 * show all cscope connections
2479 */
2480/* ARGSUSED */
2481 static int
2482cs_show(eap)
2483 exarg_T *eap;
2484{
2485 short i;
2486 if (cs_cnt_connections() == 0)
2487 MSG_PUTS(_("no cscope connections\n"));
2488 else
2489 {
2490 MSG_PUTS_ATTR(
2491 _(" # pid database name prepend path\n"),
2492 hl_attr(HLF_T));
2493 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2494 {
2495 if (csinfo[i].fname == NULL)
2496 continue;
2497
2498 if (csinfo[i].ppath != NULL)
2499 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2500 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2501 else
2502 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2503 i, (long)csinfo[i].pid, csinfo[i].fname);
2504 }
2505 }
2506
2507 wait_return(TRUE);
2508 return CSCOPE_SUCCESS;
2509} /* cs_show */
2510
Bram Moolenaar02b06312007-09-06 15:39:22 +00002511
2512/*
2513 * PUBLIC: cs_end
2514 *
2515 * Only called when VIM exits to quit any cscope sessions.
2516 */
2517 void
2518cs_end()
2519{
2520 int i;
2521
2522 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2523 cs_release_csp(i, TRUE);
2524}
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#endif /* FEAT_CSCOPE */
2527
2528/* the end */