blob: c11fc2acbe799d62f21a6c15b3ed990d2d2734d1 [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 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000086 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000087};
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 */
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000101 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000102 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
103 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
104} expand_what;
105
106/*
107 * Function given to ExpandGeneric() to obtain the cscope command
108 * expansion.
109 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000110 char_u *
111get_cscope_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000112 expand_T *xp UNUSED;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000113 int idx;
114{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000115 int current_idx;
116 int i;
117
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000118 switch (expand_what)
119 {
120 case EXP_CSCOPE_SUBCMD:
121 /* Complete with sub-commands of ":cscope":
122 * add, find, help, kill, reset, show */
123 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000124 case EXP_SCSCOPE_SUBCMD:
125 /* Complete with sub-commands of ":scscope": same sub-commands as
126 * ":cscope" but skip commands which don't support split windows */
127 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
128 if (cs_cmds[i].cansplit)
129 if (current_idx++ == idx)
130 break;
131 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000132 case EXP_CSCOPE_FIND:
133 {
134 const char *query_type[] =
135 {
136 "c", "d", "e", "f", "g", "i", "s", "t", NULL
137 };
138
139 /* Complete with query type of ":cscope find {query_type}".
140 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
141 * ..., 8) but only complete with letters, since numbers are
142 * redundant. */
143 return (char_u *)query_type[idx];
144 }
145 case EXP_CSCOPE_KILL:
146 {
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000147 static char_u connection[2];
148
149 /* ":cscope kill" accepts connection numbers or partial names of
150 * the pathname of the cscope database as argument. Only complete
151 * with connection numbers. -1 can also be used to kill all
152 * connections. */
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000153 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000154 {
155 if (csinfo[i].fname == NULL)
156 continue;
157 if (current_idx++ == idx)
158 {
159 /* Connection number fits in one character since
160 * CSCOPE_MAX_CONNECTIONS is < 10 */
161 connection[0] = i + '0';
162 connection[1] = NUL;
163 return connection;
164 }
165 }
166 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
167 }
168 default:
169 return NULL;
170 }
171}
172
173/*
174 * Handle command line completion for :cscope command.
175 */
176 void
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000177set_context_in_cscope_cmd(xp, arg, cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000178 expand_T *xp;
179 char_u *arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000180 cmdidx_T cmdidx;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000181{
182 char_u *p;
183
184 /* Default: expand subcommands */
185 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000186 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000187 expand_what = (cmdidx == CMD_scscope)
188 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000189
190 /* (part of) subcommand already typed */
191 if (*arg != NUL)
192 {
193 p = skiptowhite(arg);
194 if (*p != NUL) /* past first word */
195 {
196 xp->xp_pattern = skipwhite(p);
197 if (*skiptowhite(xp->xp_pattern) != NUL)
198 xp->xp_context = EXPAND_NOTHING;
199 else if (STRNICMP(arg, "add", p - arg) == 0)
200 xp->xp_context = EXPAND_FILES;
201 else if (STRNICMP(arg, "kill", p - arg) == 0)
202 expand_what = EXP_CSCOPE_KILL;
203 else if (STRNICMP(arg, "find", p - arg) == 0)
204 expand_what = EXP_CSCOPE_FIND;
205 else
206 xp->xp_context = EXPAND_NOTHING;
207 }
208 }
209}
210
211#endif /* FEAT_CMDL_COMPL */
212
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213/*
214 * PRIVATE: do_cscope_general
215 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000216 * Find the command, print help if invalid, and then call the corresponding
217 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 */
219 static void
220do_cscope_general(eap, make_split)
221 exarg_T *eap;
222 int make_split; /* whether to split window */
223{
224 cscmd_T *cmdp;
225
226 cs_init();
227 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
228 {
229 cs_help(eap);
230 return;
231 }
232
233#ifdef FEAT_WINDOWS
234 if (make_split)
235 {
236 if (!cmdp->cansplit)
237 {
238 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
239 return;
240 }
241 postponed_split = -1;
242 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000243 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245#endif
246
247 cmdp->func(eap);
248
249#ifdef FEAT_WINDOWS
250 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000251 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#endif
253}
254
255/*
256 * PUBLIC: do_cscope
257 */
258 void
259do_cscope(eap)
260 exarg_T *eap;
261{
262 do_cscope_general(eap, FALSE);
263}
264
265/*
266 * PUBLIC: do_scscope
267 *
268 * same as do_cscope, but splits window, too.
269 */
270 void
271do_scscope(eap)
272 exarg_T *eap;
273{
274 do_cscope_general(eap, TRUE);
275}
276
277/*
278 * PUBLIC: do_cstag
279 *
280 */
281 void
282do_cstag(eap)
283 exarg_T *eap;
284{
285 int ret = FALSE;
286
287 cs_init();
288
Bram Moolenaar446cb832008-06-24 21:56:24 +0000289 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 {
291 (void)EMSG(_("E562: Usage: cstag <ident>"));
292 return;
293 }
294
295 switch (p_csto)
296 {
297 case 0 :
298 if (cs_check_for_connections())
299 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000300 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
301 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if (ret == FALSE)
303 {
304 cs_free_tags();
305 if (msg_col)
306 msg_putchar('\n');
307
308 if (cs_check_for_tags())
309 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
310 }
311 }
312 else if (cs_check_for_tags())
313 {
314 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
315 }
316 break;
317 case 1 :
318 if (cs_check_for_tags())
319 {
320 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
321 if (ret == FALSE)
322 {
323 if (msg_col)
324 msg_putchar('\n');
325
326 if (cs_check_for_connections())
327 {
328 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000329 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 if (ret == FALSE)
331 cs_free_tags();
332 }
333 }
334 }
335 else if (cs_check_for_connections())
336 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000337 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
338 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (ret == FALSE)
340 cs_free_tags();
341 }
342 break;
343 default :
344 break;
345 }
346
347 if (!ret)
348 {
349 (void)EMSG(_("E257: cstag: tag not found"));
350#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
351 g_do_tagpreview = 0;
352#endif
353 }
354
355} /* do_cscope */
356
357
358/*
359 * PUBLIC: cs_find
360 *
361 * this simulates a vim_fgets(), but for cscope, returns the next line
362 * from the cscope output. should only be called from find_tags()
363 *
364 * returns TRUE if eof, FALSE otherwise
365 */
366 int
367cs_fgets(buf, size)
368 char_u *buf;
369 int size;
370{
371 char *p;
372
373 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
374 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000375 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
377 return FALSE;
378} /* cs_fgets */
379
380
381/*
382 * PUBLIC: cs_free_tags
383 *
384 * called only from do_tag(), when popping the tag stack
385 */
386 void
387cs_free_tags()
388{
389 cs_manage_matches(NULL, NULL, -1, Free);
390}
391
392
393/*
394 * PUBLIC: cs_print_tags
395 *
396 * called from do_tag()
397 */
398 void
399cs_print_tags()
400{
401 cs_manage_matches(NULL, NULL, -1, Print);
402}
403
404
405/*
406 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
407 *
408 * Checks for the existence of a |cscope| connection. If no
409 * parameters are specified, then the function returns:
410 *
411 * 0, if cscope was not available (not compiled in), or if there
412 * are no cscope connections; or
413 * 1, if there is at least one cscope connection.
414 *
415 * If parameters are specified, then the value of {num}
416 * determines how existence of a cscope connection is checked:
417 *
418 * {num} Description of existence check
419 * ----- ------------------------------
420 * 0 Same as no parameters (e.g., "cscope_connection()").
421 * 1 Ignore {prepend}, and use partial string matches for
422 * {dbpath}.
423 * 2 Ignore {prepend}, and use exact string matches for
424 * {dbpath}.
425 * 3 Use {prepend}, use partial string matches for both
426 * {dbpath} and {prepend}.
427 * 4 Use {prepend}, use exact string matches for both
428 * {dbpath} and {prepend}.
429 *
430 * Note: All string comparisons are case sensitive!
431 */
432#if defined(FEAT_EVAL) || defined(PROTO)
433 int
434cs_connection(num, dbpath, ppath)
435 int num;
436 char_u *dbpath;
437 char_u *ppath;
438{
439 int i;
440
441 if (num < 0 || num > 4 || (num > 0 && !dbpath))
442 return FALSE;
443
444 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
445 {
446 if (!csinfo[i].fname)
447 continue;
448
449 if (num == 0)
450 return TRUE;
451
452 switch (num)
453 {
454 case 1:
455 if (strstr(csinfo[i].fname, (char *)dbpath))
456 return TRUE;
457 break;
458 case 2:
459 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
460 return TRUE;
461 break;
462 case 3:
463 if (strstr(csinfo[i].fname, (char *)dbpath)
464 && ((!ppath && !csinfo[i].ppath)
465 || (ppath
466 && csinfo[i].ppath
467 && strstr(csinfo[i].ppath, (char *)ppath))))
468 return TRUE;
469 break;
470 case 4:
471 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
472 && ((!ppath && !csinfo[i].ppath)
473 || (ppath
474 && csinfo[i].ppath
475 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
476 return TRUE;
477 break;
478 }
479 }
480
481 return FALSE;
482} /* cs_connection */
483#endif
484
485
486/*
487 * PRIVATE functions
488 ****************************************************************************/
489
490/*
491 * PRIVATE: cs_add
492 *
493 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000494 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 *
496 * MAXPATHL 256
497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int
499cs_add(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000500 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 char *fname, *ppath, *flags = NULL;
503
504 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
505 {
506 cs_usage_msg(Add);
507 return CSCOPE_FAILURE;
508 }
509 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
510 flags = strtok((char *)NULL, (const char *)" ");
511
512 return cs_add_common(fname, ppath, flags);
513}
514
515 static void
516cs_stat_emsg(fname)
517 char *fname;
518{
519 char *stat_emsg = _("E563: stat(%s) error: %d");
520 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
521
522 if (buf != NULL)
523 {
524 (void)sprintf(buf, stat_emsg, fname, errno);
525 (void)EMSG(buf);
526 vim_free(buf);
527 }
528 else
529 (void)EMSG(_("E563: stat error"));
530}
531
532
533/*
534 * PRIVATE: cs_add_common
535 *
536 * the common routine to add a new cscope connection. called by
537 * cs_add() and cs_reset(). i really don't like to do this, but this
538 * routine uses a number of goto statements.
539 */
540 static int
541cs_add_common(arg1, arg2, flags)
542 char *arg1; /* filename - may contain environment variables */
543 char *arg2; /* prepend path - may contain environment variables */
544 char *flags;
545{
546 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000547 int ret;
548 char *fname = NULL;
549 char *fname2 = NULL;
550 char *ppath = NULL;
551 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000554 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 goto add_err;
556
557 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
558 ret = stat(fname, &statbuf);
559 if (ret < 0)
560 {
561staterr:
562 if (p_csverbose)
563 cs_stat_emsg(fname);
564 goto add_err;
565 }
566
567 /* get the prepend path (arg2), expand it, and try to stat it */
568 if (arg2 != NULL)
569 {
570 struct stat statbuf2;
571
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000572 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 goto add_err;
574
575 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
576 ret = stat(ppath, &statbuf2);
577 if (ret < 0)
578 goto staterr;
579 }
580
581 /* if filename is a directory, append the cscope database name to it */
582 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
583 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000584 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (fname2 == NULL)
586 goto add_err;
587
588 while (fname[strlen(fname)-1] == '/'
589#ifdef WIN32
590 || fname[strlen(fname)-1] == '\\'
591#endif
592 )
593 {
594 fname[strlen(fname)-1] = '\0';
595 if (strlen(fname) == 0)
596 break;
597 }
598 if (fname[0] == '\0')
599 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
600 else
601 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
602
603 ret = stat(fname2, &statbuf);
604 if (ret < 0)
605 {
606 if (p_csverbose)
607 cs_stat_emsg(fname2);
608 goto add_err;
609 }
610
611 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
612 }
613#if defined(UNIX)
614 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
615#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000616 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
618#endif
619 {
620 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
621 }
622 else
623 {
624 if (p_csverbose)
625 (void)EMSG2(
626 _("E564: %s is not a directory or a valid cscope database"),
627 fname);
628 goto add_err;
629 }
630
631 if (i != -1)
632 {
633 if (cs_create_connection(i) == CSCOPE_FAILURE
634 || cs_read_prompt(i) == CSCOPE_FAILURE)
635 {
636 cs_release_csp(i, TRUE);
637 goto add_err;
638 }
639
640 if (p_csverbose)
641 {
642 msg_clr_eos();
643 (void)smsg_attr(hl_attr(HLF_R),
644 (char_u *)_("Added cscope database %s"),
645 csinfo[i].fname);
646 }
647 }
648
649 vim_free(fname);
650 vim_free(fname2);
651 vim_free(ppath);
652 return CSCOPE_SUCCESS;
653
654add_err:
655 vim_free(fname2);
656 vim_free(fname);
657 vim_free(ppath);
658 return CSCOPE_FAILURE;
659} /* cs_add_common */
660
661
662 static int
663cs_check_for_connections()
664{
665 return (cs_cnt_connections() > 0);
666} /* cs_check_for_connections */
667
668
669 static int
670cs_check_for_tags()
671{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000672 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673} /* cs_check_for_tags */
674
675
676/*
677 * PRIVATE: cs_cnt_connections
678 *
679 * count the number of cscope connections
680 */
681 static int
682cs_cnt_connections()
683{
684 short i;
685 short cnt = 0;
686
687 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
688 {
689 if (csinfo[i].fname != NULL)
690 cnt++;
691 }
692 return cnt;
693} /* cs_cnt_connections */
694
695 static void
696cs_reading_emsg(idx)
697 int idx; /* connection index */
698{
699 EMSGN(_("E262: error reading cscope connection %ld"), idx);
700}
701
702#define CSREAD_BUFSIZE 2048
703/*
704 * PRIVATE: cs_cnt_matches
705 *
706 * count the number of matches for a given cscope connection.
707 */
708 static int
709cs_cnt_matches(idx)
710 int idx;
711{
712 char *stok;
713 char *buf;
714 int nlines;
715
716 buf = (char *)alloc(CSREAD_BUFSIZE);
717 if (buf == NULL)
718 return 0;
719 for (;;)
720 {
721 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
722 {
723 if (feof(csinfo[idx].fr_fp))
724 errno = EIO;
725
726 cs_reading_emsg(idx);
727
728 vim_free(buf);
729 return -1;
730 }
731
732 /*
733 * If the database is out of date, or there's some other problem,
734 * cscope will output error messages before the number-of-lines output.
735 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000736 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
738 if ((stok = strtok(buf, (const char *)" ")) == NULL)
739 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000740 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 continue;
742
743 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
744 continue;
745 nlines = atoi(stok);
746 if (nlines < 0)
747 {
748 nlines = 0;
749 break;
750 }
751
752 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
753 continue;
754 if (strncmp((const char *)stok, "lines", 5))
755 continue;
756
757 break;
758 }
759
760 vim_free(buf);
761 return nlines;
762} /* cs_cnt_matches */
763
764
765/*
766 * PRIVATE: cs_create_cmd
767 *
768 * Creates the actual cscope command query from what the user entered.
769 */
770 static char *
771cs_create_cmd(csoption, pattern)
772 char *csoption;
773 char *pattern;
774{
775 char *cmd;
776 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000777 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 switch (csoption[0])
780 {
781 case '0' : case 's' :
782 search = 0;
783 break;
784 case '1' : case 'g' :
785 search = 1;
786 break;
787 case '2' : case 'd' :
788 search = 2;
789 break;
790 case '3' : case 'c' :
791 search = 3;
792 break;
793 case '4' : case 't' :
794 search = 4;
795 break;
796 case '6' : case 'e' :
797 search = 6;
798 break;
799 case '7' : case 'f' :
800 search = 7;
801 break;
802 case '8' : case 'i' :
803 search = 8;
804 break;
805 default :
806 (void)EMSG(_("E561: unknown cscope search type"));
807 cs_usage_msg(Find);
808 return NULL;
809 }
810
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000811 /* Skip white space before the patter, except for text and pattern search,
812 * they may want to use the leading white space. */
813 pat = pattern;
814 if (search != 4 && search != 6)
815 while vim_iswhite(*pat)
816 ++pat;
817
818 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 return NULL;
820
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000821 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823 return cmd;
824} /* cs_create_cmd */
825
826
827/*
828 * PRIVATE: cs_create_connection
829 *
830 * This piece of code was taken/adapted from nvi. do we need to add
831 * the BSD license notice?
832 */
833 static int
834cs_create_connection(i)
835 int i;
836{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000837#ifdef UNIX
838 int to_cs[2], from_cs[2];
839#endif
840 int len;
841 char *prog, *cmd, *ppath = NULL;
842#ifdef WIN32
843 int fd;
844 SECURITY_ATTRIBUTES sa;
845 PROCESS_INFORMATION pi;
846 STARTUPINFO si;
847 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
848 HANDLE stdin_rd, stdout_rd;
849 HANDLE stdout_wr, stdin_wr;
850 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000851# ifdef __BORLANDC__
852# define OPEN_OH_ARGTYPE long
853# else
854# if (_MSC_VER >= 1300)
855# define OPEN_OH_ARGTYPE intptr_t
856# else
857# define OPEN_OH_ARGTYPE long
858# endif
859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif
861
Bram Moolenaar02b06312007-09-06 15:39:22 +0000862#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 /*
864 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
865 * from_cs[0] and writes to to_cs[1].
866 */
867 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
868 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
869 {
870 (void)EMSG(_("E566: Could not create cscope pipes"));
871err_closing:
872 if (to_cs[0] != -1)
873 (void)close(to_cs[0]);
874 if (to_cs[1] != -1)
875 (void)close(to_cs[1]);
876 if (from_cs[0] != -1)
877 (void)close(from_cs[0]);
878 if (from_cs[1] != -1)
879 (void)close(from_cs[1]);
880 return CSCOPE_FAILURE;
881 }
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 switch (csinfo[i].pid = fork())
884 {
885 case -1:
886 (void)EMSG(_("E622: Could not fork for cscope"));
887 goto err_closing;
888 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (dup2(to_cs[0], STDIN_FILENO) == -1)
890 PERROR("cs_create_connection 1");
891 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
892 PERROR("cs_create_connection 2");
893 if (dup2(from_cs[1], STDERR_FILENO) == -1)
894 PERROR("cs_create_connection 3");
895
896 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 (void)close(to_cs[1]);
898 (void)close(from_cs[0]);
899#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000900 /* WIN32 */
901 /* Create pipes to communicate with cscope */
902 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
903 sa.bInheritHandle = TRUE;
904 sa.lpSecurityDescriptor = NULL;
905
906 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
907 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
908 {
909 (void)EMSG(_("E566: Could not create cscope pipes"));
910err_closing:
911 if (pipe_stdin)
912 {
913 CloseHandle(stdin_rd);
914 CloseHandle(stdin_wr);
915 }
916 if (pipe_stdout)
917 {
918 CloseHandle(stdout_rd);
919 CloseHandle(stdout_wr);
920 }
921 return CSCOPE_FAILURE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#endif
924 /* expand the cscope exec for env var's */
925 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
926 {
927#ifdef UNIX
928 return CSCOPE_FAILURE;
929#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000930 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 goto err_closing;
932#endif
933 }
934 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
935
936 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000937 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (csinfo[i].ppath)
939 {
940 /* expand the prepend path for env var's */
941 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
942 {
943 vim_free(prog);
944#ifdef UNIX
945 return CSCOPE_FAILURE;
946#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000947 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 goto err_closing;
949#endif
950 }
951 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000953 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955
956 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000957 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 if ((cmd = (char *)alloc(len)) == NULL)
960 {
961 vim_free(prog);
962 vim_free(ppath);
963#ifdef UNIX
964 return CSCOPE_FAILURE;
965#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000966 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 goto err_closing;
968#endif
969 }
970
971 /* run the cscope command; is there execl for non-unix systems? */
972#if defined(UNIX)
973 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
974#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000975 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
977#endif
978 if (csinfo[i].ppath != NULL)
979 {
980 (void)strcat(cmd, " -P");
981 (void)strcat(cmd, csinfo[i].ppath);
982 }
983 if (csinfo[i].flags != NULL)
984 {
985 (void)strcat(cmd, " ");
986 (void)strcat(cmd, csinfo[i].flags);
987 }
988# ifdef UNIX
989 /* on Win32 we still need prog */
990 vim_free(prog);
991# endif
992 vim_free(ppath);
993
994#if defined(UNIX)
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000995 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 PERROR(_("cs_create_connection exec failed"));
997
998 exit(127);
999 /* NOTREACHED */
1000 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1004 */
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /* close unused */
1011 (void)close(to_cs[0]);
1012 (void)close(from_cs[1]);
1013
1014 break;
1015 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001018 /* WIN32 */
1019 /* Create a new process to run cscope and use pipes to talk with it */
1020 GetStartupInfo(&si);
1021 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1022 si.wShowWindow = SW_HIDE; /* Hide child application window */
1023 si.hStdOutput = stdout_wr;
1024 si.hStdError = stdout_wr;
1025 si.hStdInput = stdin_rd;
1026 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1027 NULL, NULL, &si, &pi);
1028 vim_free(prog);
1029 vim_free(cmd);
1030
1031 if (!created)
1032 {
1033 PERROR(_("cs_create_connection exec failed"));
1034 (void)EMSG(_("E623: Could not spawn cscope process"));
1035 goto err_closing;
1036 }
1037 /* else */
1038 csinfo[i].pid = pi.dwProcessId;
1039 csinfo[i].hProc = pi.hProcess;
1040 CloseHandle(pi.hThread);
1041
1042 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1044 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001045 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1046 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001047 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1048 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001049 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1050 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1051
1052 /* Close handles for file descriptors inherited by the cscope process */
1053 CloseHandle(stdin_rd);
1054 CloseHandle(stdout_wr);
1055
1056#endif /* !UNIX */
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 return CSCOPE_SUCCESS;
1059} /* cs_create_connection */
1060
1061
1062/*
1063 * PRIVATE: cs_find
1064 *
1065 * query cscope using command line interface. parse the output and use tselect
1066 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1067 *
1068 * returns TRUE if we jump to a tag or abort, FALSE if not.
1069 */
1070 static int
1071cs_find(eap)
1072 exarg_T *eap;
1073{
1074 char *opt, *pat;
1075
1076 if (cs_check_for_connections() == FALSE)
1077 {
1078 (void)EMSG(_("E567: no cscope connections"));
1079 return FALSE;
1080 }
1081
1082 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1083 {
1084 cs_usage_msg(Find);
1085 return FALSE;
1086 }
1087
1088 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001089 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
1091 cs_usage_msg(Find);
1092 return FALSE;
1093 }
1094
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001095 return cs_find_common(opt, pat, eap->forceit, TRUE,
1096 eap->cmdidx == CMD_lcscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097} /* cs_find */
1098
1099
1100/*
1101 * PRIVATE: cs_find_common
1102 *
1103 * common code for cscope find, shared by cs_find() and do_cstag()
1104 */
1105 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001106cs_find_common(opt, pat, forceit, verbose, use_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 char *opt;
1108 char *pat;
1109 int forceit;
1110 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001111 int use_ll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int i;
1114 char *cmd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001115 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116#ifdef FEAT_QUICKFIX
1117 char cmdletter;
1118 char *qfpos;
1119#endif
1120
1121 /* create the actual command to send to cscope */
1122 cmd = cs_create_cmd(opt, pat);
1123 if (cmd == NULL)
1124 return FALSE;
1125
1126 /* send query to all open connections, then count the total number
1127 * of matches so we can alloc matchesp all in one swell foop
1128 */
1129 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1130 nummatches[i] = 0;
1131 totmatches = 0;
1132 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1133 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001134 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 continue;
1136
1137 /* send cmd to cscope */
1138 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1139 (void)fflush(csinfo[i].to_fp);
1140
1141 nummatches[i] = cs_cnt_matches(i);
1142
1143 if (nummatches[i] > -1)
1144 totmatches += nummatches[i];
1145
1146 if (nummatches[i] == 0)
1147 (void)cs_read_prompt(i);
1148 }
1149 vim_free(cmd);
1150
1151 if (totmatches == 0)
1152 {
1153 char *nf = _("E259: no matches found for cscope query %s of %s");
1154 char *buf;
1155
1156 if (!verbose)
1157 return FALSE;
1158
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001159 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (buf == NULL)
1161 (void)EMSG(nf);
1162 else
1163 {
1164 sprintf(buf, nf, opt, pat);
1165 (void)EMSG(buf);
1166 vim_free(buf);
1167 }
1168 return FALSE;
1169 }
1170
1171#ifdef FEAT_QUICKFIX
1172 /* get cmd letter */
1173 switch (opt[0])
1174 {
1175 case '0' :
1176 cmdletter = 's';
1177 break;
1178 case '1' :
1179 cmdletter = 'g';
1180 break;
1181 case '2' :
1182 cmdletter = 'd';
1183 break;
1184 case '3' :
1185 cmdletter = 'c';
1186 break;
1187 case '4' :
1188 cmdletter = 't';
1189 break;
1190 case '6' :
1191 cmdletter = 'e';
1192 break;
1193 case '7' :
1194 cmdletter = 'f';
1195 break;
1196 case '8' :
1197 cmdletter = 'i';
1198 break;
1199 default :
1200 cmdletter = opt[0];
1201 }
1202
1203 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1204 if (qfpos != NULL)
1205 {
1206 qfpos++;
1207 /* next symbol must be + or - */
1208 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1209 {
1210 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001211 char *buf = (char *)alloc((unsigned)strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213 /* strlen will be enough because we use chars */
1214 if (buf != NULL)
1215 {
1216 sprintf(buf, nf, *qfpos, *(qfpos-1));
1217 (void)EMSG(buf);
1218 vim_free(buf);
1219 }
1220 return FALSE;
1221 }
1222 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001223 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001226 FILE *f;
1227 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001228 qf_info_T *qi = NULL;
1229 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001231 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001232 if (f == NULL)
1233 EMSG2(_(e_notopen), tmp);
1234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 cs_file_results(f, nummatches);
1237 fclose(f);
1238 if (use_ll) /* Use location list */
1239 wp = curwin;
1240 /* '-' starts a new error list */
1241 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1242 *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001244# ifdef FEAT_WINDOWS
1245 if (postponed_split != 0)
1246 {
1247 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 postponed_split_flags);
1249# ifdef FEAT_SCROLLBIND
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001250 curwin->w_p_scb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001252 postponed_split = 0;
1253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001255 if (use_ll)
1256 /*
1257 * In the location list window, use the displayed location
1258 * list. Otherwise, use the location list for the window.
1259 */
1260 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1261 ? wp->w_llist_ref : wp->w_llist;
1262 qf_jump(qi, 0, 0, forceit);
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 mch_remove(tmp);
1266 vim_free(tmp);
1267 return TRUE;
1268 }
1269 else
1270#endif /* FEAT_QUICKFIX */
1271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001272 char **matches = NULL, **contexts = NULL;
1273 int matched = 0;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /* read output */
1276 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1277 &contexts, &matched);
1278 if (matches == NULL)
1279 return FALSE;
1280
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001281 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1284 }
1285
1286} /* cs_find_common */
1287
1288/*
1289 * PRIVATE: cs_help
1290 *
1291 * print help
1292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 static int
1294cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001295 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 cscmd_T *cmdp = cs_cmds;
1298
1299 (void)MSG_PUTS(_("cscope commands:\n"));
1300 while (cmdp->name != NULL)
1301 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001302 char *help = _(cmdp->help);
1303 int space_cnt = 30 - vim_strsize((char_u *)help);
1304
1305 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1306 if (space_cnt < 0)
1307 space_cnt = 0;
1308 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1309 cmdp->name,
1310 help, space_cnt, " ",
1311 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001313 MSG_PUTS(_("\n"
1314 " c: Find functions calling this function\n"
1315 " d: Find functions called by this function\n"
1316 " e: Find this egrep pattern\n"
1317 " f: Find this file\n"
1318 " g: Find this definition\n"
1319 " i: Find files #including this file\n"
1320 " s: Find this C symbol\n"
1321 " t: Find assignments to\n"));
1322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 cmdp++;
1324 }
1325
1326 wait_return(TRUE);
1327 return 0;
1328} /* cs_help */
1329
1330
1331/*
1332 * PRIVATE: cs_init
1333 *
1334 * initialize cscope structure if not already
1335 */
1336 static void
1337cs_init()
1338{
1339 short i;
1340 static int init_already = FALSE;
1341
1342 if (init_already)
1343 return;
1344
1345 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1346 clear_csinfo(i);
1347
1348 init_already = TRUE;
1349} /* cs_init */
1350
1351 static void
1352clear_csinfo(i)
1353 int i;
1354{
1355 csinfo[i].fname = NULL;
1356 csinfo[i].ppath = NULL;
1357 csinfo[i].flags = NULL;
1358#if defined(UNIX)
1359 csinfo[i].st_dev = (dev_t)0;
1360 csinfo[i].st_ino = (ino_t)0;
1361#else
1362 csinfo[i].nVolume = 0;
1363 csinfo[i].nIndexHigh = 0;
1364 csinfo[i].nIndexLow = 0;
1365#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001366 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 csinfo[i].fr_fp = NULL;
1368 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001369#if defined(WIN32)
1370 csinfo[i].hProc = NULL;
1371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372}
1373
1374#ifndef UNIX
1375static char *GetWin32Error __ARGS((void));
1376
1377 static char *
1378GetWin32Error()
1379{
1380 char *msg = NULL;
1381 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1382 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1383 if (msg != NULL)
1384 {
1385 /* remove trailing \r\n */
1386 char *pcrlf = strstr(msg, "\r\n");
1387 if (pcrlf != NULL)
1388 *pcrlf = '\0';
1389 }
1390 return msg;
1391}
1392#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394/*
1395 * PRIVATE: cs_insert_filelist
1396 *
1397 * insert a new cscope database filename into the filelist
1398 */
1399 static int
1400cs_insert_filelist(fname, ppath, flags, sb)
1401 char *fname;
1402 char *ppath;
1403 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001404 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
1406 short i, j;
1407#ifndef UNIX
1408 HANDLE hFile;
1409 BY_HANDLE_FILE_INFORMATION bhfi;
1410
1411 vim_memset(&bhfi, 0, sizeof(bhfi));
1412 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1413 if (!mch_windows95())
1414 {
1415 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1416 FILE_ATTRIBUTE_NORMAL, NULL);
1417 if (hFile == INVALID_HANDLE_VALUE)
1418 {
1419 if (p_csverbose)
1420 {
1421 char *cant_msg = _("E625: cannot open cscope database: %s");
1422 char *winmsg = GetWin32Error();
1423
1424 if (winmsg != NULL)
1425 {
1426 (void)EMSG2(cant_msg, winmsg);
1427 LocalFree(winmsg);
1428 }
1429 else
1430 /* subst filename if can't get error text */
1431 (void)EMSG2(cant_msg, fname);
1432 }
1433 return -1;
1434 }
1435 if (!GetFileInformationByHandle(hFile, &bhfi))
1436 {
1437 CloseHandle(hFile);
1438 if (p_csverbose)
1439 (void)EMSG(_("E626: cannot get cscope database information"));
1440 return -1;
1441 }
1442 CloseHandle(hFile);
1443 }
1444#endif
1445
1446 i = -1; /* can be set to the index of an empty item in csinfo */
1447 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1448 {
1449 if (csinfo[j].fname != NULL
1450#if defined(UNIX)
1451 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1452#else
1453 /* compare pathnames first */
1454 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001455 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 || (!mch_windows95()
1457 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1458 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1459 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1460#endif
1461 )
1462 {
1463 if (p_csverbose)
1464 (void)EMSG(_("E568: duplicate cscope database not added"));
1465 return -1;
1466 }
1467
1468 if (csinfo[j].fname == NULL && i == -1)
1469 i = j; /* remember first empty entry */
1470 }
1471
1472 if (i == -1)
1473 {
1474 if (p_csverbose)
1475 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1476 return -1;
1477 }
1478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001479 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 return -1;
1481
1482 (void)strcpy(csinfo[i].fname, (const char *)fname);
1483
1484 if (ppath != NULL)
1485 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001486 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 {
1488 vim_free(csinfo[i].fname);
1489 csinfo[i].fname = NULL;
1490 return -1;
1491 }
1492 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1493 } else
1494 csinfo[i].ppath = NULL;
1495
1496 if (flags != NULL)
1497 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001498 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 vim_free(csinfo[i].fname);
1501 vim_free(csinfo[i].ppath);
1502 csinfo[i].fname = NULL;
1503 csinfo[i].ppath = NULL;
1504 return -1;
1505 }
1506 (void)strcpy(csinfo[i].flags, (const char *)flags);
1507 } else
1508 csinfo[i].flags = NULL;
1509
1510#if defined(UNIX)
1511 csinfo[i].st_dev = sb->st_dev;
1512 csinfo[i].st_ino = sb->st_ino;
1513
1514#else
1515 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1516 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1517 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1518#endif
1519 return i;
1520} /* cs_insert_filelist */
1521
1522
1523/*
1524 * PRIVATE: cs_lookup_cmd
1525 *
1526 * find cscope command in command table
1527 */
1528 static cscmd_T *
1529cs_lookup_cmd(eap)
1530 exarg_T *eap;
1531{
1532 cscmd_T *cmdp;
1533 char *stok;
1534 size_t len;
1535
1536 if (eap->arg == NULL)
1537 return NULL;
1538
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001539 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001540 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1543 return NULL;
1544
1545 len = strlen(stok);
1546 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1547 {
1548 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1549 return (cmdp);
1550 }
1551 return NULL;
1552} /* cs_lookup_cmd */
1553
1554
1555/*
1556 * PRIVATE: cs_kill
1557 *
1558 * nuke em
1559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 static int
1561cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001562 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 char *stok;
1565 short i;
1566
1567 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1568 {
1569 cs_usage_msg(Kill);
1570 return CSCOPE_FAILURE;
1571 }
1572
1573 /* only single digit positive and negative integers are allowed */
1574 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1575 || (strlen(stok) < 3 && stok[0] == '-'
1576 && VIM_ISDIGIT((int)(stok[1]))))
1577 i = atoi(stok);
1578 else
1579 {
1580 /* It must be part of a name. We will try to find a match
1581 * within all the names in the csinfo data structure
1582 */
1583 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1584 {
1585 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1586 break;
1587 }
1588 }
1589
1590 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1591 && i != -1)
1592 {
1593 if (p_csverbose)
1594 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1595 }
1596 else
1597 {
1598 if (i == -1)
1599 {
1600 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1601 {
1602 if (csinfo[i].fname)
1603 cs_kill_execute(i, csinfo[i].fname);
1604 }
1605 }
1606 else
1607 cs_kill_execute(i, stok);
1608 }
1609
1610 return 0;
1611} /* cs_kill */
1612
1613
1614/*
1615 * PRIVATE: cs_kill_execute
1616 *
1617 * Actually kills a specific cscope connection.
1618 */
1619 static void
1620cs_kill_execute(i, cname)
1621 int i; /* cscope table index */
1622 char *cname; /* cscope database name */
1623{
1624 if (p_csverbose)
1625 {
1626 msg_clr_eos();
1627 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1628 (char_u *)_("cscope connection %s closed"), cname);
1629 }
1630 cs_release_csp(i, TRUE);
1631}
1632
1633
1634/*
1635 * PRIVATE: cs_make_vim_style_matches
1636 *
1637 * convert the cscope output into into a ctags style entry (as might be found
1638 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1639 * the type of the tag you are looking for. for example, in Darren Hiebert's
1640 * ctags (the one that comes with vim), #define's use a line number to find the
1641 * tag in a file while function definitions use a regexp search pattern.
1642 *
1643 * i'm going to always use the line number because cscope does something
1644 * quirky (and probably other things i don't know about):
1645 *
1646 * if you have "# define" in your source file, which is
1647 * perfectly legal, cscope thinks you have "#define". this
1648 * will result in a failed regexp search. :(
1649 *
1650 * besides, even if this particular case didn't happen, the search pattern
1651 * would still have to be modified to escape all the special regular expression
1652 * characters to comply with ctags formatting.
1653 */
1654 static char *
1655cs_make_vim_style_matches(fname, slno, search, tagstr)
1656 char *fname;
1657 char *slno;
1658 char *search;
1659 char *tagstr;
1660{
1661 /* vim style is ctags:
1662 *
1663 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1664 *
1665 * but as mentioned above, we'll always use the line number and
1666 * put the search pattern (if one exists) as "extra"
1667 *
1668 * buf is used as part of vim's method of handling tags, and
1669 * (i think) vim frees it when you pop your tags and get replaced
1670 * by new ones on the tag stack.
1671 */
1672 char *buf;
1673 int amt;
1674
1675 if (search != NULL)
1676 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001677 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if ((buf = (char *)alloc(amt)) == NULL)
1679 return NULL;
1680
1681 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1682 }
1683 else
1684 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001685 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if ((buf = (char *)alloc(amt)) == NULL)
1687 return NULL;
1688
1689 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1690 }
1691
1692 return buf;
1693} /* cs_make_vim_style_matches */
1694
1695
1696/*
1697 * PRIVATE: cs_manage_matches
1698 *
1699 * this is kind of hokey, but i don't see an easy way round this..
1700 *
1701 * Store: keep a ptr to the (malloc'd) memory of matches originally
1702 * generated from cs_find(). the matches are originally lines directly
1703 * from cscope output, but transformed to look like something out of a
1704 * ctags. see cs_make_vim_style_matches for more details.
1705 *
1706 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1707 * the next line from the cscope output. it basically keeps track of which
1708 * lines have been "used" and returns the next one.
1709 *
1710 * Free: frees up everything and resets
1711 *
1712 * Print: prints the tags
1713 */
1714 static char *
1715cs_manage_matches(matches, contexts, totmatches, cmd)
1716 char **matches;
1717 char **contexts;
1718 int totmatches;
1719 mcmd_e cmd;
1720{
1721 static char **mp = NULL;
1722 static char **cp = NULL;
1723 static int cnt = -1;
1724 static int next = -1;
1725 char *p = NULL;
1726
1727 switch (cmd)
1728 {
1729 case Store:
1730 assert(matches != NULL);
1731 assert(totmatches > 0);
1732 if (mp != NULL || cp != NULL)
1733 (void)cs_manage_matches(NULL, NULL, -1, Free);
1734 mp = matches;
1735 cp = contexts;
1736 cnt = totmatches;
1737 next = 0;
1738 break;
1739 case Get:
1740 if (next >= cnt)
1741 return NULL;
1742
1743 p = mp[next];
1744 next++;
1745 break;
1746 case Free:
1747 if (mp != NULL)
1748 {
1749 if (cnt > 0)
1750 while (cnt--)
1751 {
1752 vim_free(mp[cnt]);
1753 if (cp != NULL)
1754 vim_free(cp[cnt]);
1755 }
1756 vim_free(mp);
1757 vim_free(cp);
1758 }
1759 mp = NULL;
1760 cp = NULL;
1761 cnt = 0;
1762 next = 0;
1763 break;
1764 case Print:
1765 cs_print_tags_priv(mp, cp, cnt);
1766 break;
1767 default: /* should not reach here */
1768 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1769 return NULL;
1770 }
1771
1772 return p;
1773} /* cs_manage_matches */
1774
1775
1776/*
1777 * PRIVATE: cs_parse_results
1778 *
1779 * parse cscope output
1780 */
1781 static char *
1782cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1783 int cnumber;
1784 char *buf;
1785 int bufsize;
1786 char **context;
1787 char **linenumber;
1788 char **search;
1789{
1790 int ch;
1791 char *p;
1792 char *name;
1793
1794 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1795 {
1796 if (feof(csinfo[cnumber].fr_fp))
1797 errno = EIO;
1798
1799 cs_reading_emsg(cnumber);
1800
1801 return NULL;
1802 }
1803
1804 /* If the line's too long for the buffer, discard it. */
1805 if ((p = strchr(buf, '\n')) == NULL)
1806 {
1807 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1808 ;
1809 return NULL;
1810 }
1811 *p = '\0';
1812
1813 /*
1814 * cscope output is in the following format:
1815 *
1816 * <filename> <context> <line number> <pattern>
1817 */
1818 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1819 return NULL;
1820 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1821 return NULL;
1822 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1823 return NULL;
1824 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1825
1826 /* --- nvi ---
1827 * If the file is older than the cscope database, that is,
1828 * the database was built since the file was last modified,
1829 * or there wasn't a search string, use the line number.
1830 */
1831 if (strcmp(*search, "<unknown>") == 0)
1832 *search = NULL;
1833
1834 name = cs_resolve_file(cnumber, name);
1835 return name;
1836}
1837
Bram Moolenaarc716c302006-01-21 22:12:51 +00001838#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839/*
1840 * PRIVATE: cs_file_results
1841 *
1842 * write cscope find results to file
1843 */
1844 static void
1845cs_file_results(f, nummatches_a)
1846 FILE *f;
1847 int *nummatches_a;
1848{
1849 int i, j;
1850 char *buf;
1851 char *search, *slno;
1852 char *fullname;
1853 char *cntx;
1854 char *context;
1855
1856 buf = (char *)alloc(CSREAD_BUFSIZE);
1857 if (buf == NULL)
1858 return;
1859
1860 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1861 {
1862 if (nummatches_a[i] < 1)
1863 continue;
1864
1865 for (j = 0; j < nummatches_a[i]; j++)
1866 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001867 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1868 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 continue;
1870
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001871 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001872 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 continue;
1874
1875 if (strcmp(cntx, "<global>")==0)
1876 strcpy(context, "<<global>>");
1877 else
1878 sprintf(context, "<<%s>>", cntx);
1879
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001880 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1882 else
1883 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1884
1885 vim_free(context);
1886 vim_free(fullname);
1887 } /* for all matches */
1888
1889 (void)cs_read_prompt(i);
1890
1891 } /* for all cscope connections */
1892 vim_free(buf);
1893}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896/*
1897 * PRIVATE: cs_fill_results
1898 *
1899 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1900 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001901 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 */
1903 static void
1904cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1905 char *tagstr;
1906 int totmatches;
1907 int *nummatches_a;
1908 char ***matches_p;
1909 char ***cntxts_p;
1910 int *matched;
1911{
1912 int i, j;
1913 char *buf;
1914 char *search, *slno;
1915 int totsofar = 0;
1916 char **matches = NULL;
1917 char **cntxts = NULL;
1918 char *fullname;
1919 char *cntx;
1920
1921 assert(totmatches > 0);
1922
1923 buf = (char *)alloc(CSREAD_BUFSIZE);
1924 if (buf == NULL)
1925 return;
1926
1927 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1928 goto parse_out;
1929 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1930 goto parse_out;
1931
1932 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1933 {
1934 if (nummatches_a[i] < 1)
1935 continue;
1936
1937 for (j = 0; j < nummatches_a[i]; j++)
1938 {
1939 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1940 &slno, &search)) == NULL)
1941 continue;
1942
1943 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1944 search, tagstr);
1945
1946 vim_free(fullname);
1947
1948 if (strcmp(cntx, "<global>") == 0)
1949 cntxts[totsofar] = NULL;
1950 else
1951 /* note: if vim_strsave returns NULL, then the context
1952 * will be "<global>", which is misleading.
1953 */
1954 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1955
1956 if (matches[totsofar] != NULL)
1957 totsofar++;
1958
1959 } /* for all matches */
1960
1961 (void)cs_read_prompt(i);
1962
1963 } /* for all cscope connections */
1964
1965parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001966 if (totsofar == 0)
1967 {
1968 /* No matches, free the arrays and return NULL in "*matches_p". */
1969 vim_free(matches);
1970 matches = NULL;
1971 vim_free(cntxts);
1972 cntxts = NULL;
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 *matched = totsofar;
1975 *matches_p = matches;
1976 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 vim_free(buf);
1979} /* cs_fill_results */
1980
1981
1982/* get the requested path components */
1983 static char *
1984cs_pathcomponents(path)
1985 char *path;
1986{
1987 int i;
1988 char *s;
1989
1990 if (p_cspc == 0)
1991 return path;
1992
1993 s = path + strlen(path) - 1;
1994 for (i = 0; i < p_cspc; ++i)
1995 while (s > path && *--s != '/'
1996#ifdef WIN32
1997 && *--s != '\\'
1998#endif
1999 )
2000 ;
2001 if ((s > path && *s == '/')
2002#ifdef WIN32
2003 || (s > path && *s == '\\')
2004#endif
2005 )
2006 ++s;
2007 return s;
2008}
2009
2010/*
2011 * PRIVATE: cs_print_tags_priv
2012 *
2013 * called from cs_manage_matches()
2014 */
2015 static void
2016cs_print_tags_priv(matches, cntxts, num_matches)
2017 char **matches;
2018 char **cntxts;
2019 int num_matches;
2020{
2021 char *buf = NULL;
2022 int bufsize = 0; /* Track available bufsize */
2023 int newsize = 0;
2024 char *ptag;
2025 char *fname, *lno, *extra, *tbuf;
2026 int i, idx, num;
2027 char *globalcntx = "GLOBAL";
2028 char *cntxformat = " <<%s>>";
2029 char *context;
2030 char *cstag_msg = _("Cscope tag: %s");
2031 char *csfmt_str = "%4d %6s ";
2032
2033 assert (num_matches > 0);
2034
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002035 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return;
2037
2038 strcpy(tbuf, matches[0]);
2039 ptag = strtok(tbuf, "\t");
2040
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002041 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 buf = (char *)alloc(newsize);
2043 if (buf != NULL)
2044 {
2045 bufsize = newsize;
2046 (void)sprintf(buf, cstag_msg, ptag);
2047 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2048 }
2049
2050 vim_free(tbuf);
2051
2052 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2053 msg_advance(msg_col + 2);
2054 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2055
2056 num = 1;
2057 for (i = 0; i < num_matches; i++)
2058 {
2059 idx = i;
2060
2061 /* if we really wanted to, we could avoid this malloc and strcpy
2062 * by parsing matches[i] on the fly and placing stuff into buf
2063 * directly, but that's too much of a hassle
2064 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002065 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 continue;
2067 (void)strcpy(tbuf, matches[idx]);
2068
2069 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
2070 continue;
2071 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2072 continue;
2073 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002074 continue;
2075 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2078
2079 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002080 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (bufsize < newsize)
2082 {
2083 buf = (char *)vim_realloc(buf, newsize);
2084 if (buf == NULL)
2085 bufsize = 0;
2086 else
2087 bufsize = newsize;
2088 }
2089 if (buf != NULL)
2090 {
2091 /* csfmt_str = "%4d %6s "; */
2092 (void)sprintf(buf, csfmt_str, num, lno);
2093 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2094 }
2095 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2096
2097 /* compute the required space for the context */
2098 if (cntxts[idx] != NULL)
2099 context = cntxts[idx];
2100 else
2101 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002102 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104 if (bufsize < newsize)
2105 {
2106 buf = (char *)vim_realloc(buf, newsize);
2107 if (buf == NULL)
2108 bufsize = 0;
2109 else
2110 bufsize = newsize;
2111 }
2112 if (buf != NULL)
2113 {
2114 (void)sprintf(buf, cntxformat, context);
2115
2116 /* print the context only if it fits on the same line */
2117 if (msg_col + (int)strlen(buf) >= (int)Columns)
2118 msg_putchar('\n');
2119 msg_advance(12);
2120 MSG_PUTS_LONG(buf);
2121 msg_putchar('\n');
2122 }
2123 if (extra != NULL)
2124 {
2125 msg_advance(13);
2126 MSG_PUTS_LONG(extra);
2127 }
2128
2129 vim_free(tbuf); /* only after printing extra due to strtok use */
2130
2131 if (msg_col)
2132 msg_putchar('\n');
2133
2134 ui_breakcheck();
2135 if (got_int)
2136 {
2137 got_int = FALSE; /* don't print any more matches */
2138 break;
2139 }
2140
2141 num++;
2142 } /* for all matches */
2143
2144 vim_free(buf);
2145} /* cs_print_tags_priv */
2146
2147
2148/*
2149 * PRIVATE: cs_read_prompt
2150 *
2151 * read a cscope prompt (basically, skip over the ">> ")
2152 */
2153 static int
2154cs_read_prompt(i)
2155 int i;
2156{
2157 int ch;
2158 char *buf = NULL; /* buffer for possible error message from cscope */
2159 int bufpos = 0;
2160 char *cs_emsg;
2161 int maxlen;
2162 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002163 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 int n;
2165
2166 cs_emsg = _("E609: Cscope error: %s");
2167 /* compute maximum allowed len for Cscope error message */
2168 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2169
2170 for (;;)
2171 {
2172 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2173 /* if there is room and char is printable */
2174 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2175 {
2176 if (buf == NULL) /* lazy buffer allocation */
2177 buf = (char *)alloc(maxlen);
2178 if (buf != NULL)
2179 {
2180 /* append character to the message */
2181 buf[bufpos++] = ch;
2182 buf[bufpos] = NUL;
2183 if (bufpos >= epromptlen
2184 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2185 {
2186 /* remove eprompt from buf */
2187 buf[bufpos - epromptlen] = NUL;
2188
2189 /* print message to user */
2190 (void)EMSG2(cs_emsg, buf);
2191
2192 /* send RETURN to cscope */
2193 (void)putc('\n', csinfo[i].to_fp);
2194 (void)fflush(csinfo[i].to_fp);
2195
2196 /* clear buf */
2197 bufpos = 0;
2198 buf[bufpos] = NUL;
2199 }
2200 }
2201 }
2202
2203 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2204 {
2205 if (n > 0)
2206 ch = getc(csinfo[i].fr_fp);
2207 if (ch == EOF)
2208 {
2209 PERROR("cs_read_prompt EOF");
2210 if (buf != NULL && buf[0] != NUL)
2211 (void)EMSG2(cs_emsg, buf);
2212 else if (p_csverbose)
2213 cs_reading_emsg(i); /* don't have additional information */
2214 cs_release_csp(i, TRUE);
2215 vim_free(buf);
2216 return CSCOPE_FAILURE;
2217 }
2218
2219 if (ch != CSCOPE_PROMPT[n])
2220 {
2221 ch = EOF;
2222 break;
2223 }
2224 }
2225
2226 if (ch == EOF)
2227 continue; /* didn't find the prompt */
2228 break; /* did find the prompt */
2229 }
2230
2231 vim_free(buf);
2232 return CSCOPE_SUCCESS;
2233}
2234
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002235#if defined(UNIX) && defined(SIGALRM)
2236/*
2237 * Used to catch and ignore SIGALRM below.
2238 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002239 static RETSIGTYPE
2240sig_handler SIGDEFARG(sigarg)
2241{
2242 /* do nothing */
2243 SIGRETURN;
2244}
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247/*
2248 * PRIVATE: cs_release_csp
2249 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002250 * Does the actual free'ing for the cs ptr with an optional flag of whether
2251 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 static void
2254cs_release_csp(i, freefnpp)
2255 int i;
2256 int freefnpp;
2257{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /*
2259 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2260 */
2261 if (csinfo[i].to_fp != NULL)
2262 {
2263 (void)fputs("q\n", csinfo[i].to_fp);
2264 (void)fflush(csinfo[i].to_fp);
2265 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002266#if defined(UNIX)
2267 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002268 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002269 int pstat;
2270 pid_t pid;
2271
2272# if defined(HAVE_SIGACTION)
2273 struct sigaction sa, old;
2274
Bram Moolenaar9701da02008-03-16 12:09:58 +00002275 /* Use sigaction() to limit the waiting time to two seconds. */
2276 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002277 sa.sa_handler = sig_handler;
2278 sa.sa_flags = SA_NODEFER;
2279 sigaction(SIGALRM, &sa, &old);
2280 alarm(2); /* 2 sec timeout */
2281
2282 /* Block until cscope exits or until timer expires */
2283 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002284 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002285
2286 /* cancel pending alarm if still there and restore signal */
2287 alarm(0);
2288 sigaction(SIGALRM, &old, NULL);
2289# else
2290 int waited;
2291
2292 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2293 * to give cscope a chance to exit quickly. */
2294 sleep(0);
2295 for (waited = 0; waited < 40; ++waited)
2296 {
2297 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002298 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002299 if (pid != 0)
2300 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002301 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002302 }
2303# endif
2304 /*
2305 * If the cscope process is still running: kill it.
2306 * Safety check: If the PID would be zero here, the entire X session
2307 * would be killed. -1 and 1 are dangerous as well.
2308 */
2309 if (pid < 0 && csinfo[i].pid > 1)
2310 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002311# ifdef ECHILD
2312 int alive = TRUE;
2313
2314 if (waitpid_errno == ECHILD)
2315 {
2316 /*
2317 * When using 'vim -g', vim is forked and cscope process is
2318 * no longer a child process but a sibling. So waitpid()
2319 * fails with errno being ECHILD (No child processes).
2320 * Don't send SIGKILL to cscope immediately but wait
2321 * (polling) for it to exit normally as result of sending
2322 * the "q" command, hence giving it a chance to clean up
2323 * its temporary files.
2324 */
2325 int waited;
2326
2327 sleep(0);
2328 for (waited = 0; waited < 40; ++waited)
2329 {
2330 /* Check whether cscope process is still alive */
2331 if (kill(csinfo[i].pid, 0) != 0)
2332 {
2333 alive = FALSE; /* cscope process no longer exists */
2334 break;
2335 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002336 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002337 }
2338 }
2339 if (alive)
2340# endif
2341 {
2342 kill(csinfo[i].pid, SIGKILL);
2343 (void)waitpid(csinfo[i].pid, &pstat, 0);
2344 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002345 }
2346 }
2347#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002348 if (csinfo[i].hProc != NULL)
2349 {
2350 /* Give cscope a chance to exit normally */
2351 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2352 TerminateProcess(csinfo[i].hProc, 0);
2353 CloseHandle(csinfo[i].hProc);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#endif
2356
2357 if (csinfo[i].fr_fp != NULL)
2358 (void)fclose(csinfo[i].fr_fp);
2359 if (csinfo[i].to_fp != NULL)
2360 (void)fclose(csinfo[i].to_fp);
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (freefnpp)
2363 {
2364 vim_free(csinfo[i].fname);
2365 vim_free(csinfo[i].ppath);
2366 vim_free(csinfo[i].flags);
2367 }
2368
2369 clear_csinfo(i);
2370} /* cs_release_csp */
2371
2372
2373/*
2374 * PRIVATE: cs_reset
2375 *
2376 * calls cs_kill on all cscope connections then reinits
2377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 static int
2379cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002380 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2383 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002384 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 /* malloc our db and ppath list */
2387 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2388 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2389 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2390 if (dblist == NULL || pplist == NULL || fllist == NULL)
2391 {
2392 vim_free(dblist);
2393 vim_free(pplist);
2394 vim_free(fllist);
2395 return CSCOPE_FAILURE;
2396 }
2397
2398 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2399 {
2400 dblist[i] = csinfo[i].fname;
2401 pplist[i] = csinfo[i].ppath;
2402 fllist[i] = csinfo[i].flags;
2403 if (csinfo[i].fname != NULL)
2404 cs_release_csp(i, FALSE);
2405 }
2406
2407 /* rebuild the cscope connection list */
2408 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2409 {
2410 if (dblist[i] != NULL)
2411 {
2412 cs_add_common(dblist[i], pplist[i], fllist[i]);
2413 if (p_csverbose)
2414 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002415 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * connection number in the same line as
2417 * "Added cscope database..."
2418 */
2419 sprintf(buf, " (#%d)", i);
2420 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2421 }
2422 }
2423 vim_free(dblist[i]);
2424 vim_free(pplist[i]);
2425 vim_free(fllist[i]);
2426 }
2427 vim_free(dblist);
2428 vim_free(pplist);
2429 vim_free(fllist);
2430
2431 if (p_csverbose)
2432 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2433 return CSCOPE_SUCCESS;
2434} /* cs_reset */
2435
2436
2437/*
2438 * PRIVATE: cs_resolve_file
2439 *
2440 * construct the full pathname to a file found in the cscope database.
2441 * (Prepends ppath, if there is one and if it's not already prepended,
2442 * otherwise just uses the name found.)
2443 *
2444 * we need to prepend the prefix because on some cscope's (e.g., the one that
2445 * ships with Solaris 2.6), the output never has the prefix prepended.
2446 * contrast this with my development system (Digital Unix), which does.
2447 */
2448 static char *
2449cs_resolve_file(i, name)
2450 int i;
2451 char *name;
2452{
2453 char *fullname;
2454 int len;
2455
2456 /*
2457 * ppath is freed when we destroy the cscope connection.
2458 * fullname is freed after cs_make_vim_style_matches, after it's been
2459 * copied into the tag buffer used by vim
2460 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002461 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002463 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 if ((fullname = (char *)alloc(len)) == NULL)
2466 return NULL;
2467
2468 /*
2469 * note/example: this won't work if the cscope output already starts
2470 * "../.." and the prefix path is also "../..". if something like this
2471 * happens, you are screwed up and need to fix how you're using cscope.
2472 */
2473 if (csinfo[i].ppath != NULL &&
2474 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2475 (name[0] != '/')
2476#ifdef WIN32
2477 && name[0] != '\\' && name[1] != ':'
2478#endif
2479 )
2480 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2481 else
2482 (void)sprintf(fullname, "%s", name);
2483
2484 return fullname;
2485} /* cs_resolve_file */
2486
2487
2488/*
2489 * PRIVATE: cs_show
2490 *
2491 * show all cscope connections
2492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 static int
2494cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002495 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 short i;
2498 if (cs_cnt_connections() == 0)
2499 MSG_PUTS(_("no cscope connections\n"));
2500 else
2501 {
2502 MSG_PUTS_ATTR(
2503 _(" # pid database name prepend path\n"),
2504 hl_attr(HLF_T));
2505 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2506 {
2507 if (csinfo[i].fname == NULL)
2508 continue;
2509
2510 if (csinfo[i].ppath != NULL)
2511 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2512 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2513 else
2514 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2515 i, (long)csinfo[i].pid, csinfo[i].fname);
2516 }
2517 }
2518
2519 wait_return(TRUE);
2520 return CSCOPE_SUCCESS;
2521} /* cs_show */
2522
Bram Moolenaar02b06312007-09-06 15:39:22 +00002523
2524/*
2525 * PUBLIC: cs_end
2526 *
2527 * Only called when VIM exits to quit any cscope sessions.
2528 */
2529 void
2530cs_end()
2531{
2532 int i;
2533
2534 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2535 cs_release_csp(i, TRUE);
2536}
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538#endif /* FEAT_CSCOPE */
2539
2540/* the end */