blob: 8867f63852c055baa66adac8679b7e4231c2d77c [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include <assert.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#if defined(UNIX)
20# include <sys/wait.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#endif
22#include "if_cscope.h"
23
24static void cs_usage_msg __ARGS((csid_e x));
25static int cs_add __ARGS((exarg_T *eap));
26static void cs_stat_emsg __ARGS((char *fname));
27static int cs_add_common __ARGS((char *, char *, char *));
28static int cs_check_for_connections __ARGS((void));
29static int cs_check_for_tags __ARGS((void));
30static int cs_cnt_connections __ARGS((void));
31static void cs_reading_emsg __ARGS((int idx));
32static int cs_cnt_matches __ARGS((int idx));
33static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
34static int cs_create_connection __ARGS((int i));
35static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000036#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void cs_fill_results __ARGS((char *, int , int *, char ***,
40 char ***, int *));
41static int cs_find __ARGS((exarg_T *eap));
Bram Moolenaar7fd73202010-07-25 16:58:46 +020042static int cs_find_common __ARGS((char *opt, char *pat, int, int, int, char_u *cmdline));
Bram Moolenaar071d4272004-06-13 20:20:40 +000043static int cs_help __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static void clear_csinfo __ARGS((int i));
45static int cs_insert_filelist __ARGS((char *, char *, char *,
46 struct stat *));
47static int cs_kill __ARGS((exarg_T *eap));
48static void cs_kill_execute __ARGS((int, char *));
49static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
50static char * cs_make_vim_style_matches __ARGS((char *, char *,
51 char *, char *));
52static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
53static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
54static char * cs_pathcomponents __ARGS((char *path));
55static void cs_print_tags_priv __ARGS((char **, char **, int));
Bram Moolenaar02b06312007-09-06 15:39:22 +000056static int cs_read_prompt __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void cs_release_csp __ARGS((int, int freefnpp));
58static int cs_reset __ARGS((exarg_T *eap));
59static char * cs_resolve_file __ARGS((int, char *));
60static int cs_show __ARGS((exarg_T *eap));
61
62
Bram Moolenaar9fa49da2009-07-10 13:11:26 +000063static csinfo_T * csinfo = NULL;
64static int csinfo_size = 0; /* number of items allocated in
65 csinfo[] */
66
Bram Moolenaard2ac9842007-08-21 16:03:51 +000067static int eap_arg_len; /* length of eap->arg, set in
68 cs_lookup_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static cscmd_T cs_cmds[] =
70{
71 { "add", cs_add,
72 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
73 { "find", cs_find,
Bram Moolenaar627943d2008-08-25 02:35:59 +000074 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 { "help", cs_help,
76 N_("Show this message"), "help", 0 },
77 { "kill", cs_kill,
78 N_("Kill a connection"), "kill #", 0 },
79 { "reset", cs_reset,
80 N_("Reinit all connections"), "reset", 0 },
81 { "show", cs_show,
82 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000083 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
86 static void
87cs_usage_msg(x)
88 csid_e x;
89{
90 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
91}
92
Bram Moolenaarf4580d82009-03-18 11:52:53 +000093#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
94
95static enum
96{
97 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +000098 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +000099 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
100 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
101} expand_what;
102
103/*
104 * Function given to ExpandGeneric() to obtain the cscope command
105 * expansion.
106 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000107 char_u *
108get_cscope_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000109 expand_T *xp UNUSED;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000110 int idx;
111{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000112 int current_idx;
113 int i;
114
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000115 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;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000121 case EXP_SCSCOPE_SUBCMD:
122 /* Complete with sub-commands of ":scscope": same sub-commands as
123 * ":cscope" but skip commands which don't support split windows */
124 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
125 if (cs_cmds[i].cansplit)
126 if (current_idx++ == idx)
127 break;
128 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000129 case EXP_CSCOPE_FIND:
130 {
131 const char *query_type[] =
132 {
133 "c", "d", "e", "f", "g", "i", "s", "t", NULL
134 };
135
136 /* Complete with query type of ":cscope find {query_type}".
137 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
138 * ..., 8) but only complete with letters, since numbers are
139 * redundant. */
140 return (char_u *)query_type[idx];
141 }
142 case EXP_CSCOPE_KILL:
143 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000144 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000145
146 /* ":cscope kill" accepts connection numbers or partial names of
147 * the pathname of the cscope database as argument. Only complete
148 * with connection numbers. -1 can also be used to kill all
149 * connections. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000150 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000151 {
152 if (csinfo[i].fname == NULL)
153 continue;
154 if (current_idx++ == idx)
155 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000156 vim_snprintf(connection, sizeof(connection), "%d", i);
157 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000158 }
159 }
160 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
161 }
162 default:
163 return NULL;
164 }
165}
166
167/*
168 * Handle command line completion for :cscope command.
169 */
170 void
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000171set_context_in_cscope_cmd(xp, arg, cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000172 expand_T *xp;
173 char_u *arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000174 cmdidx_T cmdidx;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000175{
176 char_u *p;
177
178 /* Default: expand subcommands */
179 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000180 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000181 expand_what = (cmdidx == CMD_scscope)
182 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000183
184 /* (part of) subcommand already typed */
185 if (*arg != NUL)
186 {
187 p = skiptowhite(arg);
188 if (*p != NUL) /* past first word */
189 {
190 xp->xp_pattern = skipwhite(p);
191 if (*skiptowhite(xp->xp_pattern) != NUL)
192 xp->xp_context = EXPAND_NOTHING;
193 else if (STRNICMP(arg, "add", p - arg) == 0)
194 xp->xp_context = EXPAND_FILES;
195 else if (STRNICMP(arg, "kill", p - arg) == 0)
196 expand_what = EXP_CSCOPE_KILL;
197 else if (STRNICMP(arg, "find", p - arg) == 0)
198 expand_what = EXP_CSCOPE_FIND;
199 else
200 xp->xp_context = EXPAND_NOTHING;
201 }
202 }
203}
204
205#endif /* FEAT_CMDL_COMPL */
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
208 * PRIVATE: do_cscope_general
209 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000210 * Find the command, print help if invalid, and then call the corresponding
211 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 */
213 static void
214do_cscope_general(eap, make_split)
215 exarg_T *eap;
216 int make_split; /* whether to split window */
217{
218 cscmd_T *cmdp;
219
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
221 {
222 cs_help(eap);
223 return;
224 }
225
226#ifdef FEAT_WINDOWS
227 if (make_split)
228 {
229 if (!cmdp->cansplit)
230 {
231 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
232 return;
233 }
234 postponed_split = -1;
235 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000236 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 }
238#endif
239
240 cmdp->func(eap);
241
242#ifdef FEAT_WINDOWS
243 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000244 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#endif
246}
247
248/*
249 * PUBLIC: do_cscope
250 */
251 void
252do_cscope(eap)
253 exarg_T *eap;
254{
255 do_cscope_general(eap, FALSE);
256}
257
258/*
259 * PUBLIC: do_scscope
260 *
261 * same as do_cscope, but splits window, too.
262 */
263 void
264do_scscope(eap)
265 exarg_T *eap;
266{
267 do_cscope_general(eap, TRUE);
268}
269
270/*
271 * PUBLIC: do_cstag
272 *
273 */
274 void
275do_cstag(eap)
276 exarg_T *eap;
277{
278 int ret = FALSE;
279
Bram Moolenaar446cb832008-06-24 21:56:24 +0000280 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {
282 (void)EMSG(_("E562: Usage: cstag <ident>"));
283 return;
284 }
285
286 switch (p_csto)
287 {
288 case 0 :
289 if (cs_check_for_connections())
290 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000291 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200292 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 if (ret == FALSE)
294 {
295 cs_free_tags();
296 if (msg_col)
297 msg_putchar('\n');
298
299 if (cs_check_for_tags())
300 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
301 }
302 }
303 else if (cs_check_for_tags())
304 {
305 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
306 }
307 break;
308 case 1 :
309 if (cs_check_for_tags())
310 {
311 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
312 if (ret == FALSE)
313 {
314 if (msg_col)
315 msg_putchar('\n');
316
317 if (cs_check_for_connections())
318 {
319 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200320 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 if (ret == FALSE)
322 cs_free_tags();
323 }
324 }
325 }
326 else if (cs_check_for_connections())
327 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000328 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200329 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 if (ret == FALSE)
331 cs_free_tags();
332 }
333 break;
334 default :
335 break;
336 }
337
338 if (!ret)
339 {
340 (void)EMSG(_("E257: cstag: tag not found"));
341#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
342 g_do_tagpreview = 0;
343#endif
344 }
345
346} /* do_cscope */
347
348
349/*
350 * PUBLIC: cs_find
351 *
352 * this simulates a vim_fgets(), but for cscope, returns the next line
353 * from the cscope output. should only be called from find_tags()
354 *
355 * returns TRUE if eof, FALSE otherwise
356 */
357 int
358cs_fgets(buf, size)
359 char_u *buf;
360 int size;
361{
362 char *p;
363
364 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
365 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000366 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
368 return FALSE;
369} /* cs_fgets */
370
371
372/*
373 * PUBLIC: cs_free_tags
374 *
375 * called only from do_tag(), when popping the tag stack
376 */
377 void
378cs_free_tags()
379{
380 cs_manage_matches(NULL, NULL, -1, Free);
381}
382
383
384/*
385 * PUBLIC: cs_print_tags
386 *
387 * called from do_tag()
388 */
389 void
390cs_print_tags()
391{
392 cs_manage_matches(NULL, NULL, -1, Print);
393}
394
395
396/*
397 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
398 *
399 * Checks for the existence of a |cscope| connection. If no
400 * parameters are specified, then the function returns:
401 *
402 * 0, if cscope was not available (not compiled in), or if there
403 * are no cscope connections; or
404 * 1, if there is at least one cscope connection.
405 *
406 * If parameters are specified, then the value of {num}
407 * determines how existence of a cscope connection is checked:
408 *
409 * {num} Description of existence check
410 * ----- ------------------------------
411 * 0 Same as no parameters (e.g., "cscope_connection()").
412 * 1 Ignore {prepend}, and use partial string matches for
413 * {dbpath}.
414 * 2 Ignore {prepend}, and use exact string matches for
415 * {dbpath}.
416 * 3 Use {prepend}, use partial string matches for both
417 * {dbpath} and {prepend}.
418 * 4 Use {prepend}, use exact string matches for both
419 * {dbpath} and {prepend}.
420 *
421 * Note: All string comparisons are case sensitive!
422 */
423#if defined(FEAT_EVAL) || defined(PROTO)
424 int
425cs_connection(num, dbpath, ppath)
426 int num;
427 char_u *dbpath;
428 char_u *ppath;
429{
430 int i;
431
432 if (num < 0 || num > 4 || (num > 0 && !dbpath))
433 return FALSE;
434
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000435 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {
437 if (!csinfo[i].fname)
438 continue;
439
440 if (num == 0)
441 return TRUE;
442
443 switch (num)
444 {
445 case 1:
446 if (strstr(csinfo[i].fname, (char *)dbpath))
447 return TRUE;
448 break;
449 case 2:
450 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
451 return TRUE;
452 break;
453 case 3:
454 if (strstr(csinfo[i].fname, (char *)dbpath)
455 && ((!ppath && !csinfo[i].ppath)
456 || (ppath
457 && csinfo[i].ppath
458 && strstr(csinfo[i].ppath, (char *)ppath))))
459 return TRUE;
460 break;
461 case 4:
462 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
463 && ((!ppath && !csinfo[i].ppath)
464 || (ppath
465 && csinfo[i].ppath
466 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
467 return TRUE;
468 break;
469 }
470 }
471
472 return FALSE;
473} /* cs_connection */
474#endif
475
476
477/*
478 * PRIVATE functions
479 ****************************************************************************/
480
481/*
482 * PRIVATE: cs_add
483 *
484 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000485 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 *
487 * MAXPATHL 256
488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 static int
490cs_add(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000491 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 char *fname, *ppath, *flags = NULL;
494
495 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
496 {
497 cs_usage_msg(Add);
498 return CSCOPE_FAILURE;
499 }
500 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
501 flags = strtok((char *)NULL, (const char *)" ");
502
503 return cs_add_common(fname, ppath, flags);
504}
505
506 static void
507cs_stat_emsg(fname)
508 char *fname;
509{
510 char *stat_emsg = _("E563: stat(%s) error: %d");
511 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
512
513 if (buf != NULL)
514 {
515 (void)sprintf(buf, stat_emsg, fname, errno);
516 (void)EMSG(buf);
517 vim_free(buf);
518 }
519 else
520 (void)EMSG(_("E563: stat error"));
521}
522
523
524/*
525 * PRIVATE: cs_add_common
526 *
527 * the common routine to add a new cscope connection. called by
528 * cs_add() and cs_reset(). i really don't like to do this, but this
529 * routine uses a number of goto statements.
530 */
531 static int
532cs_add_common(arg1, arg2, flags)
533 char *arg1; /* filename - may contain environment variables */
534 char *arg2; /* prepend path - may contain environment variables */
535 char *flags;
536{
537 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000538 int ret;
539 char *fname = NULL;
540 char *fname2 = NULL;
541 char *ppath = NULL;
542 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
544 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000545 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 goto add_err;
547
548 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
549 ret = stat(fname, &statbuf);
550 if (ret < 0)
551 {
552staterr:
553 if (p_csverbose)
554 cs_stat_emsg(fname);
555 goto add_err;
556 }
557
558 /* get the prepend path (arg2), expand it, and try to stat it */
559 if (arg2 != NULL)
560 {
561 struct stat statbuf2;
562
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000563 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 goto add_err;
565
566 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
567 ret = stat(ppath, &statbuf2);
568 if (ret < 0)
569 goto staterr;
570 }
571
572 /* if filename is a directory, append the cscope database name to it */
573 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
574 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000575 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (fname2 == NULL)
577 goto add_err;
578
579 while (fname[strlen(fname)-1] == '/'
580#ifdef WIN32
581 || fname[strlen(fname)-1] == '\\'
582#endif
583 )
584 {
585 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200586 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 break;
588 }
589 if (fname[0] == '\0')
590 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
591 else
592 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
593
594 ret = stat(fname2, &statbuf);
595 if (ret < 0)
596 {
597 if (p_csverbose)
598 cs_stat_emsg(fname2);
599 goto add_err;
600 }
601
602 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
603 }
604#if defined(UNIX)
605 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
606#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000607 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
609#endif
610 {
611 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
612 }
613 else
614 {
615 if (p_csverbose)
616 (void)EMSG2(
617 _("E564: %s is not a directory or a valid cscope database"),
618 fname);
619 goto add_err;
620 }
621
622 if (i != -1)
623 {
624 if (cs_create_connection(i) == CSCOPE_FAILURE
625 || cs_read_prompt(i) == CSCOPE_FAILURE)
626 {
627 cs_release_csp(i, TRUE);
628 goto add_err;
629 }
630
631 if (p_csverbose)
632 {
633 msg_clr_eos();
634 (void)smsg_attr(hl_attr(HLF_R),
635 (char_u *)_("Added cscope database %s"),
636 csinfo[i].fname);
637 }
638 }
639
640 vim_free(fname);
641 vim_free(fname2);
642 vim_free(ppath);
643 return CSCOPE_SUCCESS;
644
645add_err:
646 vim_free(fname2);
647 vim_free(fname);
648 vim_free(ppath);
649 return CSCOPE_FAILURE;
650} /* cs_add_common */
651
652
653 static int
654cs_check_for_connections()
655{
656 return (cs_cnt_connections() > 0);
657} /* cs_check_for_connections */
658
659
660 static int
661cs_check_for_tags()
662{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000663 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664} /* cs_check_for_tags */
665
666
667/*
668 * PRIVATE: cs_cnt_connections
669 *
670 * count the number of cscope connections
671 */
672 static int
673cs_cnt_connections()
674{
675 short i;
676 short cnt = 0;
677
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000678 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 if (csinfo[i].fname != NULL)
681 cnt++;
682 }
683 return cnt;
684} /* cs_cnt_connections */
685
686 static void
687cs_reading_emsg(idx)
688 int idx; /* connection index */
689{
690 EMSGN(_("E262: error reading cscope connection %ld"), idx);
691}
692
693#define CSREAD_BUFSIZE 2048
694/*
695 * PRIVATE: cs_cnt_matches
696 *
697 * count the number of matches for a given cscope connection.
698 */
699 static int
700cs_cnt_matches(idx)
701 int idx;
702{
703 char *stok;
704 char *buf;
705 int nlines;
706
707 buf = (char *)alloc(CSREAD_BUFSIZE);
708 if (buf == NULL)
709 return 0;
710 for (;;)
711 {
712 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
713 {
714 if (feof(csinfo[idx].fr_fp))
715 errno = EIO;
716
717 cs_reading_emsg(idx);
718
719 vim_free(buf);
720 return -1;
721 }
722
723 /*
724 * If the database is out of date, or there's some other problem,
725 * cscope will output error messages before the number-of-lines output.
726 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000727 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 if ((stok = strtok(buf, (const char *)" ")) == NULL)
730 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000731 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 continue;
733
734 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
735 continue;
736 nlines = atoi(stok);
737 if (nlines < 0)
738 {
739 nlines = 0;
740 break;
741 }
742
743 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
744 continue;
745 if (strncmp((const char *)stok, "lines", 5))
746 continue;
747
748 break;
749 }
750
751 vim_free(buf);
752 return nlines;
753} /* cs_cnt_matches */
754
755
756/*
757 * PRIVATE: cs_create_cmd
758 *
759 * Creates the actual cscope command query from what the user entered.
760 */
761 static char *
762cs_create_cmd(csoption, pattern)
763 char *csoption;
764 char *pattern;
765{
766 char *cmd;
767 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000768 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 switch (csoption[0])
771 {
772 case '0' : case 's' :
773 search = 0;
774 break;
775 case '1' : case 'g' :
776 search = 1;
777 break;
778 case '2' : case 'd' :
779 search = 2;
780 break;
781 case '3' : case 'c' :
782 search = 3;
783 break;
784 case '4' : case 't' :
785 search = 4;
786 break;
787 case '6' : case 'e' :
788 search = 6;
789 break;
790 case '7' : case 'f' :
791 search = 7;
792 break;
793 case '8' : case 'i' :
794 search = 8;
795 break;
796 default :
797 (void)EMSG(_("E561: unknown cscope search type"));
798 cs_usage_msg(Find);
799 return NULL;
800 }
801
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000802 /* Skip white space before the patter, except for text and pattern search,
803 * they may want to use the leading white space. */
804 pat = pattern;
805 if (search != 4 && search != 6)
806 while vim_iswhite(*pat)
807 ++pat;
808
809 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return NULL;
811
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000812 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 return cmd;
815} /* cs_create_cmd */
816
817
818/*
819 * PRIVATE: cs_create_connection
820 *
821 * This piece of code was taken/adapted from nvi. do we need to add
822 * the BSD license notice?
823 */
824 static int
825cs_create_connection(i)
826 int i;
827{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000828#ifdef UNIX
829 int to_cs[2], from_cs[2];
830#endif
831 int len;
832 char *prog, *cmd, *ppath = NULL;
833#ifdef WIN32
834 int fd;
835 SECURITY_ATTRIBUTES sa;
836 PROCESS_INFORMATION pi;
837 STARTUPINFO si;
838 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
839 HANDLE stdin_rd, stdout_rd;
840 HANDLE stdout_wr, stdin_wr;
841 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000842# ifdef __BORLANDC__
843# define OPEN_OH_ARGTYPE long
844# else
845# if (_MSC_VER >= 1300)
846# define OPEN_OH_ARGTYPE intptr_t
847# else
848# define OPEN_OH_ARGTYPE long
849# endif
850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#endif
852
Bram Moolenaar02b06312007-09-06 15:39:22 +0000853#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /*
855 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
856 * from_cs[0] and writes to to_cs[1].
857 */
858 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
859 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
860 {
861 (void)EMSG(_("E566: Could not create cscope pipes"));
862err_closing:
863 if (to_cs[0] != -1)
864 (void)close(to_cs[0]);
865 if (to_cs[1] != -1)
866 (void)close(to_cs[1]);
867 if (from_cs[0] != -1)
868 (void)close(from_cs[0]);
869 if (from_cs[1] != -1)
870 (void)close(from_cs[1]);
871 return CSCOPE_FAILURE;
872 }
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 switch (csinfo[i].pid = fork())
875 {
876 case -1:
877 (void)EMSG(_("E622: Could not fork for cscope"));
878 goto err_closing;
879 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (dup2(to_cs[0], STDIN_FILENO) == -1)
881 PERROR("cs_create_connection 1");
882 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
883 PERROR("cs_create_connection 2");
884 if (dup2(from_cs[1], STDERR_FILENO) == -1)
885 PERROR("cs_create_connection 3");
886
887 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 (void)close(to_cs[1]);
889 (void)close(from_cs[0]);
890#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000891 /* WIN32 */
892 /* Create pipes to communicate with cscope */
893 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
894 sa.bInheritHandle = TRUE;
895 sa.lpSecurityDescriptor = NULL;
896
897 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
898 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
899 {
900 (void)EMSG(_("E566: Could not create cscope pipes"));
901err_closing:
902 if (pipe_stdin)
903 {
904 CloseHandle(stdin_rd);
905 CloseHandle(stdin_wr);
906 }
907 if (pipe_stdout)
908 {
909 CloseHandle(stdout_rd);
910 CloseHandle(stdout_wr);
911 }
912 return CSCOPE_FAILURE;
913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915 /* expand the cscope exec for env var's */
916 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
917 {
918#ifdef UNIX
919 return CSCOPE_FAILURE;
920#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000921 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 goto err_closing;
923#endif
924 }
925 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
926
927 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000928 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (csinfo[i].ppath)
930 {
931 /* expand the prepend path for env var's */
932 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
933 {
934 vim_free(prog);
935#ifdef UNIX
936 return CSCOPE_FAILURE;
937#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000938 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 goto err_closing;
940#endif
941 }
942 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000944 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946
947 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000948 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 if ((cmd = (char *)alloc(len)) == NULL)
951 {
952 vim_free(prog);
953 vim_free(ppath);
954#ifdef UNIX
955 return CSCOPE_FAILURE;
956#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000957 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 goto err_closing;
959#endif
960 }
961
962 /* run the cscope command; is there execl for non-unix systems? */
963#if defined(UNIX)
964 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
965#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000966 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
968#endif
969 if (csinfo[i].ppath != NULL)
970 {
971 (void)strcat(cmd, " -P");
972 (void)strcat(cmd, csinfo[i].ppath);
973 }
974 if (csinfo[i].flags != NULL)
975 {
976 (void)strcat(cmd, " ");
977 (void)strcat(cmd, csinfo[i].flags);
978 }
979# ifdef UNIX
980 /* on Win32 we still need prog */
981 vim_free(prog);
982# endif
983 vim_free(ppath);
984
985#if defined(UNIX)
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000986 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 PERROR(_("cs_create_connection exec failed"));
988
989 exit(127);
990 /* NOTREACHED */
991 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 /*
993 * Save the file descriptors for later duplication, and
994 * reopen as streams.
995 */
996 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
997 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
998 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
999 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /* close unused */
1002 (void)close(to_cs[0]);
1003 (void)close(from_cs[1]);
1004
1005 break;
1006 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001009 /* WIN32 */
1010 /* Create a new process to run cscope and use pipes to talk with it */
1011 GetStartupInfo(&si);
1012 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1013 si.wShowWindow = SW_HIDE; /* Hide child application window */
1014 si.hStdOutput = stdout_wr;
1015 si.hStdError = stdout_wr;
1016 si.hStdInput = stdin_rd;
1017 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1018 NULL, NULL, &si, &pi);
1019 vim_free(prog);
1020 vim_free(cmd);
1021
1022 if (!created)
1023 {
1024 PERROR(_("cs_create_connection exec failed"));
1025 (void)EMSG(_("E623: Could not spawn cscope process"));
1026 goto err_closing;
1027 }
1028 /* else */
1029 csinfo[i].pid = pi.dwProcessId;
1030 csinfo[i].hProc = pi.hProcess;
1031 CloseHandle(pi.hThread);
1032
1033 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001034 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1035 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001036 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1037 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001038 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1039 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001040 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1041 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1042
1043 /* Close handles for file descriptors inherited by the cscope process */
1044 CloseHandle(stdin_rd);
1045 CloseHandle(stdout_wr);
1046
1047#endif /* !UNIX */
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 return CSCOPE_SUCCESS;
1050} /* cs_create_connection */
1051
1052
1053/*
1054 * PRIVATE: cs_find
1055 *
1056 * query cscope using command line interface. parse the output and use tselect
1057 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1058 *
1059 * returns TRUE if we jump to a tag or abort, FALSE if not.
1060 */
1061 static int
1062cs_find(eap)
1063 exarg_T *eap;
1064{
1065 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001066 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 if (cs_check_for_connections() == FALSE)
1069 {
1070 (void)EMSG(_("E567: no cscope connections"));
1071 return FALSE;
1072 }
1073
1074 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1075 {
1076 cs_usage_msg(Find);
1077 return FALSE;
1078 }
1079
1080 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001081 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 cs_usage_msg(Find);
1084 return FALSE;
1085 }
1086
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001087 /*
1088 * Let's replace the NULs written by strtok() with spaces - we need the
1089 * spaces to correctly display the quickfix/location list window's title.
1090 */
1091 for (i = 0; i < eap_arg_len; ++i)
1092 if (NUL == eap->arg[i])
1093 eap->arg[i] = ' ';
1094
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001095 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001096 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
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 Moolenaar7fd73202010-07-25 16:58:46 +02001106cs_find_common(opt, pat, forceit, verbose, use_ll, cmdline)
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 Moolenaar7fd73202010-07-25 16:58:46 +02001112 char_u *cmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int i;
1115 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001116 int *nummatches;
1117 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_QUICKFIX
1119 char cmdletter;
1120 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001121
1122 /* get cmd letter */
1123 switch (opt[0])
1124 {
1125 case '0' :
1126 cmdletter = 's';
1127 break;
1128 case '1' :
1129 cmdletter = 'g';
1130 break;
1131 case '2' :
1132 cmdletter = 'd';
1133 break;
1134 case '3' :
1135 cmdletter = 'c';
1136 break;
1137 case '4' :
1138 cmdletter = 't';
1139 break;
1140 case '6' :
1141 cmdletter = 'e';
1142 break;
1143 case '7' :
1144 cmdletter = 'f';
1145 break;
1146 case '8' :
1147 cmdletter = 'i';
1148 break;
1149 default :
1150 cmdletter = opt[0];
1151 }
1152
1153 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1154 if (qfpos != NULL)
1155 {
1156 qfpos++;
1157 /* next symbol must be + or - */
1158 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1159 {
1160 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1161 char *buf = (char *)alloc((unsigned)strlen(nf));
1162
1163 /* strlen will be enough because we use chars */
1164 if (buf != NULL)
1165 {
1166 sprintf(buf, nf, *qfpos, *(qfpos-1));
1167 (void)EMSG(buf);
1168 vim_free(buf);
1169 }
1170 return FALSE;
1171 }
1172
1173# ifdef FEAT_AUTOCMD
1174 if (*qfpos != '0')
1175 {
1176 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1177 curbuf->b_fname, TRUE, curbuf);
1178# ifdef FEAT_EVAL
1179 if (did_throw || force_abort)
1180 return FALSE;
1181# endif
1182 }
1183# endif
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
1187 /* create the actual command to send to cscope */
1188 cmd = cs_create_cmd(opt, pat);
1189 if (cmd == NULL)
1190 return FALSE;
1191
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001192 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1193 if (nummatches == NULL)
1194 return FALSE;
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 /* send query to all open connections, then count the total number
1197 * of matches so we can alloc matchesp all in one swell foop
1198 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001199 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 nummatches[i] = 0;
1201 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001202 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001204 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 continue;
1206
1207 /* send cmd to cscope */
1208 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1209 (void)fflush(csinfo[i].to_fp);
1210
1211 nummatches[i] = cs_cnt_matches(i);
1212
1213 if (nummatches[i] > -1)
1214 totmatches += nummatches[i];
1215
1216 if (nummatches[i] == 0)
1217 (void)cs_read_prompt(i);
1218 }
1219 vim_free(cmd);
1220
1221 if (totmatches == 0)
1222 {
1223 char *nf = _("E259: no matches found for cscope query %s of %s");
1224 char *buf;
1225
1226 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001227 {
1228 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001232 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (buf == NULL)
1234 (void)EMSG(nf);
1235 else
1236 {
1237 sprintf(buf, nf, opt, pat);
1238 (void)EMSG(buf);
1239 vim_free(buf);
1240 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001241 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return FALSE;
1243 }
1244
1245#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001246 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001249 FILE *f;
1250 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001251 qf_info_T *qi = NULL;
1252 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001254 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001255 if (f == NULL)
1256 EMSG2(_(e_notopen), tmp);
1257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001259 cs_file_results(f, nummatches);
1260 fclose(f);
1261 if (use_ll) /* Use location list */
1262 wp = curwin;
1263 /* '-' starts a new error list */
1264 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001265 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001267# ifdef FEAT_WINDOWS
1268 if (postponed_split != 0)
1269 {
1270 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001272 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001273 postponed_split = 0;
1274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001276
1277# ifdef FEAT_AUTOCMD
1278 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1279 curbuf->b_fname, TRUE, curbuf);
1280# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001281 if (use_ll)
1282 /*
1283 * In the location list window, use the displayed location
1284 * list. Otherwise, use the location list for the window.
1285 */
1286 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1287 ? wp->w_llist_ref : wp->w_llist;
1288 qf_jump(qi, 0, 0, forceit);
1289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 mch_remove(tmp);
1292 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001293 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 return TRUE;
1295 }
1296 else
1297#endif /* FEAT_QUICKFIX */
1298 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001299 char **matches = NULL, **contexts = NULL;
1300 int matched = 0;
1301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 /* read output */
1303 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1304 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001305 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (matches == NULL)
1307 return FALSE;
1308
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001309 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
1311 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1312 }
1313
1314} /* cs_find_common */
1315
1316/*
1317 * PRIVATE: cs_help
1318 *
1319 * print help
1320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static int
1322cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001323 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
1325 cscmd_T *cmdp = cs_cmds;
1326
1327 (void)MSG_PUTS(_("cscope commands:\n"));
1328 while (cmdp->name != NULL)
1329 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001330 char *help = _(cmdp->help);
1331 int space_cnt = 30 - vim_strsize((char_u *)help);
1332
1333 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1334 if (space_cnt < 0)
1335 space_cnt = 0;
1336 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1337 cmdp->name,
1338 help, space_cnt, " ",
1339 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001341 MSG_PUTS(_("\n"
1342 " c: Find functions calling this function\n"
1343 " d: Find functions called by this function\n"
1344 " e: Find this egrep pattern\n"
1345 " f: Find this file\n"
1346 " g: Find this definition\n"
1347 " i: Find files #including this file\n"
1348 " s: Find this C symbol\n"
1349 " t: Find assignments to\n"));
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 cmdp++;
1352 }
1353
1354 wait_return(TRUE);
1355 return 0;
1356} /* cs_help */
1357
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 static void
1360clear_csinfo(i)
1361 int i;
1362{
1363 csinfo[i].fname = NULL;
1364 csinfo[i].ppath = NULL;
1365 csinfo[i].flags = NULL;
1366#if defined(UNIX)
1367 csinfo[i].st_dev = (dev_t)0;
1368 csinfo[i].st_ino = (ino_t)0;
1369#else
1370 csinfo[i].nVolume = 0;
1371 csinfo[i].nIndexHigh = 0;
1372 csinfo[i].nIndexLow = 0;
1373#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001374 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 csinfo[i].fr_fp = NULL;
1376 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001377#if defined(WIN32)
1378 csinfo[i].hProc = NULL;
1379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380}
1381
1382#ifndef UNIX
1383static char *GetWin32Error __ARGS((void));
1384
1385 static char *
1386GetWin32Error()
1387{
1388 char *msg = NULL;
1389 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1390 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1391 if (msg != NULL)
1392 {
1393 /* remove trailing \r\n */
1394 char *pcrlf = strstr(msg, "\r\n");
1395 if (pcrlf != NULL)
1396 *pcrlf = '\0';
1397 }
1398 return msg;
1399}
1400#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * PRIVATE: cs_insert_filelist
1404 *
1405 * insert a new cscope database filename into the filelist
1406 */
1407 static int
1408cs_insert_filelist(fname, ppath, flags, sb)
1409 char *fname;
1410 char *ppath;
1411 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001412 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 short i, j;
1415#ifndef UNIX
1416 HANDLE hFile;
1417 BY_HANDLE_FILE_INFORMATION bhfi;
1418
1419 vim_memset(&bhfi, 0, sizeof(bhfi));
1420 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1421 if (!mch_windows95())
1422 {
1423 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1424 FILE_ATTRIBUTE_NORMAL, NULL);
1425 if (hFile == INVALID_HANDLE_VALUE)
1426 {
1427 if (p_csverbose)
1428 {
1429 char *cant_msg = _("E625: cannot open cscope database: %s");
1430 char *winmsg = GetWin32Error();
1431
1432 if (winmsg != NULL)
1433 {
1434 (void)EMSG2(cant_msg, winmsg);
1435 LocalFree(winmsg);
1436 }
1437 else
1438 /* subst filename if can't get error text */
1439 (void)EMSG2(cant_msg, fname);
1440 }
1441 return -1;
1442 }
1443 if (!GetFileInformationByHandle(hFile, &bhfi))
1444 {
1445 CloseHandle(hFile);
1446 if (p_csverbose)
1447 (void)EMSG(_("E626: cannot get cscope database information"));
1448 return -1;
1449 }
1450 CloseHandle(hFile);
1451 }
1452#endif
1453
1454 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001455 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 {
1457 if (csinfo[j].fname != NULL
1458#if defined(UNIX)
1459 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1460#else
1461 /* compare pathnames first */
1462 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001463 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 || (!mch_windows95()
1465 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1466 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1467 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1468#endif
1469 )
1470 {
1471 if (p_csverbose)
1472 (void)EMSG(_("E568: duplicate cscope database not added"));
1473 return -1;
1474 }
1475
1476 if (csinfo[j].fname == NULL && i == -1)
1477 i = j; /* remember first empty entry */
1478 }
1479
1480 if (i == -1)
1481 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001482 i = csinfo_size;
1483 if (csinfo_size == 0)
1484 {
1485 /* First time allocation: allocate only 1 connection. It should
1486 * be enough for most users. If more is needed, csinfo will be
1487 * reallocated. */
1488 csinfo_size = 1;
1489 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1490 }
1491 else
1492 {
1493 /* Reallocate space for more connections. */
1494 csinfo_size *= 2;
1495 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1496 }
1497 if (csinfo == NULL)
1498 return -1;
1499 for (j = csinfo_size/2; j < csinfo_size; j++)
1500 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001503 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 return -1;
1505
1506 (void)strcpy(csinfo[i].fname, (const char *)fname);
1507
1508 if (ppath != NULL)
1509 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001510 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 vim_free(csinfo[i].fname);
1513 csinfo[i].fname = NULL;
1514 return -1;
1515 }
1516 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1517 } else
1518 csinfo[i].ppath = NULL;
1519
1520 if (flags != NULL)
1521 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001522 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
1524 vim_free(csinfo[i].fname);
1525 vim_free(csinfo[i].ppath);
1526 csinfo[i].fname = NULL;
1527 csinfo[i].ppath = NULL;
1528 return -1;
1529 }
1530 (void)strcpy(csinfo[i].flags, (const char *)flags);
1531 } else
1532 csinfo[i].flags = NULL;
1533
1534#if defined(UNIX)
1535 csinfo[i].st_dev = sb->st_dev;
1536 csinfo[i].st_ino = sb->st_ino;
1537
1538#else
1539 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1540 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1541 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1542#endif
1543 return i;
1544} /* cs_insert_filelist */
1545
1546
1547/*
1548 * PRIVATE: cs_lookup_cmd
1549 *
1550 * find cscope command in command table
1551 */
1552 static cscmd_T *
1553cs_lookup_cmd(eap)
1554 exarg_T *eap;
1555{
1556 cscmd_T *cmdp;
1557 char *stok;
1558 size_t len;
1559
1560 if (eap->arg == NULL)
1561 return NULL;
1562
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001563 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001564 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1567 return NULL;
1568
1569 len = strlen(stok);
1570 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1571 {
1572 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1573 return (cmdp);
1574 }
1575 return NULL;
1576} /* cs_lookup_cmd */
1577
1578
1579/*
1580 * PRIVATE: cs_kill
1581 *
1582 * nuke em
1583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 static int
1585cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001586 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 char *stok;
1589 short i;
1590
1591 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1592 {
1593 cs_usage_msg(Kill);
1594 return CSCOPE_FAILURE;
1595 }
1596
1597 /* only single digit positive and negative integers are allowed */
1598 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1599 || (strlen(stok) < 3 && stok[0] == '-'
1600 && VIM_ISDIGIT((int)(stok[1]))))
1601 i = atoi(stok);
1602 else
1603 {
1604 /* It must be part of a name. We will try to find a match
1605 * within all the names in the csinfo data structure
1606 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001607 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
1609 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1610 break;
1611 }
1612 }
1613
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001614 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 if (p_csverbose)
1617 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1618 }
1619 else
1620 {
1621 if (i == -1)
1622 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001623 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 if (csinfo[i].fname)
1626 cs_kill_execute(i, csinfo[i].fname);
1627 }
1628 }
1629 else
1630 cs_kill_execute(i, stok);
1631 }
1632
1633 return 0;
1634} /* cs_kill */
1635
1636
1637/*
1638 * PRIVATE: cs_kill_execute
1639 *
1640 * Actually kills a specific cscope connection.
1641 */
1642 static void
1643cs_kill_execute(i, cname)
1644 int i; /* cscope table index */
1645 char *cname; /* cscope database name */
1646{
1647 if (p_csverbose)
1648 {
1649 msg_clr_eos();
1650 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1651 (char_u *)_("cscope connection %s closed"), cname);
1652 }
1653 cs_release_csp(i, TRUE);
1654}
1655
1656
1657/*
1658 * PRIVATE: cs_make_vim_style_matches
1659 *
1660 * convert the cscope output into into a ctags style entry (as might be found
1661 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1662 * the type of the tag you are looking for. for example, in Darren Hiebert's
1663 * ctags (the one that comes with vim), #define's use a line number to find the
1664 * tag in a file while function definitions use a regexp search pattern.
1665 *
1666 * i'm going to always use the line number because cscope does something
1667 * quirky (and probably other things i don't know about):
1668 *
1669 * if you have "# define" in your source file, which is
1670 * perfectly legal, cscope thinks you have "#define". this
1671 * will result in a failed regexp search. :(
1672 *
1673 * besides, even if this particular case didn't happen, the search pattern
1674 * would still have to be modified to escape all the special regular expression
1675 * characters to comply with ctags formatting.
1676 */
1677 static char *
1678cs_make_vim_style_matches(fname, slno, search, tagstr)
1679 char *fname;
1680 char *slno;
1681 char *search;
1682 char *tagstr;
1683{
1684 /* vim style is ctags:
1685 *
1686 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1687 *
1688 * but as mentioned above, we'll always use the line number and
1689 * put the search pattern (if one exists) as "extra"
1690 *
1691 * buf is used as part of vim's method of handling tags, and
1692 * (i think) vim frees it when you pop your tags and get replaced
1693 * by new ones on the tag stack.
1694 */
1695 char *buf;
1696 int amt;
1697
1698 if (search != NULL)
1699 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001700 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if ((buf = (char *)alloc(amt)) == NULL)
1702 return NULL;
1703
1704 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1705 }
1706 else
1707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001708 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if ((buf = (char *)alloc(amt)) == NULL)
1710 return NULL;
1711
1712 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1713 }
1714
1715 return buf;
1716} /* cs_make_vim_style_matches */
1717
1718
1719/*
1720 * PRIVATE: cs_manage_matches
1721 *
1722 * this is kind of hokey, but i don't see an easy way round this..
1723 *
1724 * Store: keep a ptr to the (malloc'd) memory of matches originally
1725 * generated from cs_find(). the matches are originally lines directly
1726 * from cscope output, but transformed to look like something out of a
1727 * ctags. see cs_make_vim_style_matches for more details.
1728 *
1729 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1730 * the next line from the cscope output. it basically keeps track of which
1731 * lines have been "used" and returns the next one.
1732 *
1733 * Free: frees up everything and resets
1734 *
1735 * Print: prints the tags
1736 */
1737 static char *
1738cs_manage_matches(matches, contexts, totmatches, cmd)
1739 char **matches;
1740 char **contexts;
1741 int totmatches;
1742 mcmd_e cmd;
1743{
1744 static char **mp = NULL;
1745 static char **cp = NULL;
1746 static int cnt = -1;
1747 static int next = -1;
1748 char *p = NULL;
1749
1750 switch (cmd)
1751 {
1752 case Store:
1753 assert(matches != NULL);
1754 assert(totmatches > 0);
1755 if (mp != NULL || cp != NULL)
1756 (void)cs_manage_matches(NULL, NULL, -1, Free);
1757 mp = matches;
1758 cp = contexts;
1759 cnt = totmatches;
1760 next = 0;
1761 break;
1762 case Get:
1763 if (next >= cnt)
1764 return NULL;
1765
1766 p = mp[next];
1767 next++;
1768 break;
1769 case Free:
1770 if (mp != NULL)
1771 {
1772 if (cnt > 0)
1773 while (cnt--)
1774 {
1775 vim_free(mp[cnt]);
1776 if (cp != NULL)
1777 vim_free(cp[cnt]);
1778 }
1779 vim_free(mp);
1780 vim_free(cp);
1781 }
1782 mp = NULL;
1783 cp = NULL;
1784 cnt = 0;
1785 next = 0;
1786 break;
1787 case Print:
1788 cs_print_tags_priv(mp, cp, cnt);
1789 break;
1790 default: /* should not reach here */
1791 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1792 return NULL;
1793 }
1794
1795 return p;
1796} /* cs_manage_matches */
1797
1798
1799/*
1800 * PRIVATE: cs_parse_results
1801 *
1802 * parse cscope output
1803 */
1804 static char *
1805cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1806 int cnumber;
1807 char *buf;
1808 int bufsize;
1809 char **context;
1810 char **linenumber;
1811 char **search;
1812{
1813 int ch;
1814 char *p;
1815 char *name;
1816
1817 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1818 {
1819 if (feof(csinfo[cnumber].fr_fp))
1820 errno = EIO;
1821
1822 cs_reading_emsg(cnumber);
1823
1824 return NULL;
1825 }
1826
1827 /* If the line's too long for the buffer, discard it. */
1828 if ((p = strchr(buf, '\n')) == NULL)
1829 {
1830 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1831 ;
1832 return NULL;
1833 }
1834 *p = '\0';
1835
1836 /*
1837 * cscope output is in the following format:
1838 *
1839 * <filename> <context> <line number> <pattern>
1840 */
1841 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1842 return NULL;
1843 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1844 return NULL;
1845 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1846 return NULL;
1847 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1848
1849 /* --- nvi ---
1850 * If the file is older than the cscope database, that is,
1851 * the database was built since the file was last modified,
1852 * or there wasn't a search string, use the line number.
1853 */
1854 if (strcmp(*search, "<unknown>") == 0)
1855 *search = NULL;
1856
1857 name = cs_resolve_file(cnumber, name);
1858 return name;
1859}
1860
Bram Moolenaarc716c302006-01-21 22:12:51 +00001861#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862/*
1863 * PRIVATE: cs_file_results
1864 *
1865 * write cscope find results to file
1866 */
1867 static void
1868cs_file_results(f, nummatches_a)
1869 FILE *f;
1870 int *nummatches_a;
1871{
1872 int i, j;
1873 char *buf;
1874 char *search, *slno;
1875 char *fullname;
1876 char *cntx;
1877 char *context;
1878
1879 buf = (char *)alloc(CSREAD_BUFSIZE);
1880 if (buf == NULL)
1881 return;
1882
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001883 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 if (nummatches_a[i] < 1)
1886 continue;
1887
1888 for (j = 0; j < nummatches_a[i]; j++)
1889 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001890 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1891 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 continue;
1893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001894 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001895 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 continue;
1897
1898 if (strcmp(cntx, "<global>")==0)
1899 strcpy(context, "<<global>>");
1900 else
1901 sprintf(context, "<<%s>>", cntx);
1902
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001903 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1905 else
1906 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1907
1908 vim_free(context);
1909 vim_free(fullname);
1910 } /* for all matches */
1911
1912 (void)cs_read_prompt(i);
1913
1914 } /* for all cscope connections */
1915 vim_free(buf);
1916}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919/*
1920 * PRIVATE: cs_fill_results
1921 *
1922 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1923 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001924 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 static void
1927cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1928 char *tagstr;
1929 int totmatches;
1930 int *nummatches_a;
1931 char ***matches_p;
1932 char ***cntxts_p;
1933 int *matched;
1934{
1935 int i, j;
1936 char *buf;
1937 char *search, *slno;
1938 int totsofar = 0;
1939 char **matches = NULL;
1940 char **cntxts = NULL;
1941 char *fullname;
1942 char *cntx;
1943
1944 assert(totmatches > 0);
1945
1946 buf = (char *)alloc(CSREAD_BUFSIZE);
1947 if (buf == NULL)
1948 return;
1949
1950 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1951 goto parse_out;
1952 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1953 goto parse_out;
1954
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001955 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 if (nummatches_a[i] < 1)
1958 continue;
1959
1960 for (j = 0; j < nummatches_a[i]; j++)
1961 {
1962 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1963 &slno, &search)) == NULL)
1964 continue;
1965
1966 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1967 search, tagstr);
1968
1969 vim_free(fullname);
1970
1971 if (strcmp(cntx, "<global>") == 0)
1972 cntxts[totsofar] = NULL;
1973 else
1974 /* note: if vim_strsave returns NULL, then the context
1975 * will be "<global>", which is misleading.
1976 */
1977 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1978
1979 if (matches[totsofar] != NULL)
1980 totsofar++;
1981
1982 } /* for all matches */
1983
1984 (void)cs_read_prompt(i);
1985
1986 } /* for all cscope connections */
1987
1988parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001989 if (totsofar == 0)
1990 {
1991 /* No matches, free the arrays and return NULL in "*matches_p". */
1992 vim_free(matches);
1993 matches = NULL;
1994 vim_free(cntxts);
1995 cntxts = NULL;
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 *matched = totsofar;
1998 *matches_p = matches;
1999 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 vim_free(buf);
2002} /* cs_fill_results */
2003
2004
2005/* get the requested path components */
2006 static char *
2007cs_pathcomponents(path)
2008 char *path;
2009{
2010 int i;
2011 char *s;
2012
2013 if (p_cspc == 0)
2014 return path;
2015
2016 s = path + strlen(path) - 1;
2017 for (i = 0; i < p_cspc; ++i)
2018 while (s > path && *--s != '/'
2019#ifdef WIN32
2020 && *--s != '\\'
2021#endif
2022 )
2023 ;
2024 if ((s > path && *s == '/')
2025#ifdef WIN32
2026 || (s > path && *s == '\\')
2027#endif
2028 )
2029 ++s;
2030 return s;
2031}
2032
2033/*
2034 * PRIVATE: cs_print_tags_priv
2035 *
2036 * called from cs_manage_matches()
2037 */
2038 static void
2039cs_print_tags_priv(matches, cntxts, num_matches)
2040 char **matches;
2041 char **cntxts;
2042 int num_matches;
2043{
2044 char *buf = NULL;
2045 int bufsize = 0; /* Track available bufsize */
2046 int newsize = 0;
2047 char *ptag;
2048 char *fname, *lno, *extra, *tbuf;
2049 int i, idx, num;
2050 char *globalcntx = "GLOBAL";
2051 char *cntxformat = " <<%s>>";
2052 char *context;
2053 char *cstag_msg = _("Cscope tag: %s");
2054 char *csfmt_str = "%4d %6s ";
2055
2056 assert (num_matches > 0);
2057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002058 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return;
2060
2061 strcpy(tbuf, matches[0]);
2062 ptag = strtok(tbuf, "\t");
2063
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002064 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 buf = (char *)alloc(newsize);
2066 if (buf != NULL)
2067 {
2068 bufsize = newsize;
2069 (void)sprintf(buf, cstag_msg, ptag);
2070 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2071 }
2072
2073 vim_free(tbuf);
2074
2075 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2076 msg_advance(msg_col + 2);
2077 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2078
2079 num = 1;
2080 for (i = 0; i < num_matches; i++)
2081 {
2082 idx = i;
2083
2084 /* if we really wanted to, we could avoid this malloc and strcpy
2085 * by parsing matches[i] on the fly and placing stuff into buf
2086 * directly, but that's too much of a hassle
2087 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 continue;
2090 (void)strcpy(tbuf, matches[idx]);
2091
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002092 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 continue;
2094 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2095 continue;
2096 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002097 continue;
2098 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2101
2102 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002103 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 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 /* csfmt_str = "%4d %6s "; */
2115 (void)sprintf(buf, csfmt_str, num, lno);
2116 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2117 }
2118 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2119
2120 /* compute the required space for the context */
2121 if (cntxts[idx] != NULL)
2122 context = cntxts[idx];
2123 else
2124 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002125 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127 if (bufsize < newsize)
2128 {
2129 buf = (char *)vim_realloc(buf, newsize);
2130 if (buf == NULL)
2131 bufsize = 0;
2132 else
2133 bufsize = newsize;
2134 }
2135 if (buf != NULL)
2136 {
2137 (void)sprintf(buf, cntxformat, context);
2138
2139 /* print the context only if it fits on the same line */
2140 if (msg_col + (int)strlen(buf) >= (int)Columns)
2141 msg_putchar('\n');
2142 msg_advance(12);
2143 MSG_PUTS_LONG(buf);
2144 msg_putchar('\n');
2145 }
2146 if (extra != NULL)
2147 {
2148 msg_advance(13);
2149 MSG_PUTS_LONG(extra);
2150 }
2151
2152 vim_free(tbuf); /* only after printing extra due to strtok use */
2153
2154 if (msg_col)
2155 msg_putchar('\n');
2156
2157 ui_breakcheck();
2158 if (got_int)
2159 {
2160 got_int = FALSE; /* don't print any more matches */
2161 break;
2162 }
2163
2164 num++;
2165 } /* for all matches */
2166
2167 vim_free(buf);
2168} /* cs_print_tags_priv */
2169
2170
2171/*
2172 * PRIVATE: cs_read_prompt
2173 *
2174 * read a cscope prompt (basically, skip over the ">> ")
2175 */
2176 static int
2177cs_read_prompt(i)
2178 int i;
2179{
2180 int ch;
2181 char *buf = NULL; /* buffer for possible error message from cscope */
2182 int bufpos = 0;
2183 char *cs_emsg;
2184 int maxlen;
2185 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 int n;
2188
2189 cs_emsg = _("E609: Cscope error: %s");
2190 /* compute maximum allowed len for Cscope error message */
2191 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2192
2193 for (;;)
2194 {
2195 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2196 /* if there is room and char is printable */
2197 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2198 {
2199 if (buf == NULL) /* lazy buffer allocation */
2200 buf = (char *)alloc(maxlen);
2201 if (buf != NULL)
2202 {
2203 /* append character to the message */
2204 buf[bufpos++] = ch;
2205 buf[bufpos] = NUL;
2206 if (bufpos >= epromptlen
2207 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2208 {
2209 /* remove eprompt from buf */
2210 buf[bufpos - epromptlen] = NUL;
2211
2212 /* print message to user */
2213 (void)EMSG2(cs_emsg, buf);
2214
2215 /* send RETURN to cscope */
2216 (void)putc('\n', csinfo[i].to_fp);
2217 (void)fflush(csinfo[i].to_fp);
2218
2219 /* clear buf */
2220 bufpos = 0;
2221 buf[bufpos] = NUL;
2222 }
2223 }
2224 }
2225
2226 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2227 {
2228 if (n > 0)
2229 ch = getc(csinfo[i].fr_fp);
2230 if (ch == EOF)
2231 {
2232 PERROR("cs_read_prompt EOF");
2233 if (buf != NULL && buf[0] != NUL)
2234 (void)EMSG2(cs_emsg, buf);
2235 else if (p_csverbose)
2236 cs_reading_emsg(i); /* don't have additional information */
2237 cs_release_csp(i, TRUE);
2238 vim_free(buf);
2239 return CSCOPE_FAILURE;
2240 }
2241
2242 if (ch != CSCOPE_PROMPT[n])
2243 {
2244 ch = EOF;
2245 break;
2246 }
2247 }
2248
2249 if (ch == EOF)
2250 continue; /* didn't find the prompt */
2251 break; /* did find the prompt */
2252 }
2253
2254 vim_free(buf);
2255 return CSCOPE_SUCCESS;
2256}
2257
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002258#if defined(UNIX) && defined(SIGALRM)
2259/*
2260 * Used to catch and ignore SIGALRM below.
2261 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002262 static RETSIGTYPE
2263sig_handler SIGDEFARG(sigarg)
2264{
2265 /* do nothing */
2266 SIGRETURN;
2267}
2268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270/*
2271 * PRIVATE: cs_release_csp
2272 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002273 * Does the actual free'ing for the cs ptr with an optional flag of whether
2274 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
2276 static void
2277cs_release_csp(i, freefnpp)
2278 int i;
2279 int freefnpp;
2280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 /*
2282 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2283 */
2284 if (csinfo[i].to_fp != NULL)
2285 {
2286 (void)fputs("q\n", csinfo[i].to_fp);
2287 (void)fflush(csinfo[i].to_fp);
2288 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002289#if defined(UNIX)
2290 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002291 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002292 int pstat;
2293 pid_t pid;
2294
2295# if defined(HAVE_SIGACTION)
2296 struct sigaction sa, old;
2297
Bram Moolenaar9701da02008-03-16 12:09:58 +00002298 /* Use sigaction() to limit the waiting time to two seconds. */
2299 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002300 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002301# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002302 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002303# else
2304 sa.sa_flags = 0;
2305# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002306 sigaction(SIGALRM, &sa, &old);
2307 alarm(2); /* 2 sec timeout */
2308
2309 /* Block until cscope exits or until timer expires */
2310 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002311 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002312
2313 /* cancel pending alarm if still there and restore signal */
2314 alarm(0);
2315 sigaction(SIGALRM, &old, NULL);
2316# else
2317 int waited;
2318
2319 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2320 * to give cscope a chance to exit quickly. */
2321 sleep(0);
2322 for (waited = 0; waited < 40; ++waited)
2323 {
2324 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002325 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002326 if (pid != 0)
2327 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002328 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002329 }
2330# endif
2331 /*
2332 * If the cscope process is still running: kill it.
2333 * Safety check: If the PID would be zero here, the entire X session
2334 * would be killed. -1 and 1 are dangerous as well.
2335 */
2336 if (pid < 0 && csinfo[i].pid > 1)
2337 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002338# ifdef ECHILD
2339 int alive = TRUE;
2340
2341 if (waitpid_errno == ECHILD)
2342 {
2343 /*
2344 * When using 'vim -g', vim is forked and cscope process is
2345 * no longer a child process but a sibling. So waitpid()
2346 * fails with errno being ECHILD (No child processes).
2347 * Don't send SIGKILL to cscope immediately but wait
2348 * (polling) for it to exit normally as result of sending
2349 * the "q" command, hence giving it a chance to clean up
2350 * its temporary files.
2351 */
2352 int waited;
2353
2354 sleep(0);
2355 for (waited = 0; waited < 40; ++waited)
2356 {
2357 /* Check whether cscope process is still alive */
2358 if (kill(csinfo[i].pid, 0) != 0)
2359 {
2360 alive = FALSE; /* cscope process no longer exists */
2361 break;
2362 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002363 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002364 }
2365 }
2366 if (alive)
2367# endif
2368 {
2369 kill(csinfo[i].pid, SIGKILL);
2370 (void)waitpid(csinfo[i].pid, &pstat, 0);
2371 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002372 }
2373 }
2374#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002375 if (csinfo[i].hProc != NULL)
2376 {
2377 /* Give cscope a chance to exit normally */
2378 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2379 TerminateProcess(csinfo[i].hProc, 0);
2380 CloseHandle(csinfo[i].hProc);
2381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#endif
2383
2384 if (csinfo[i].fr_fp != NULL)
2385 (void)fclose(csinfo[i].fr_fp);
2386 if (csinfo[i].to_fp != NULL)
2387 (void)fclose(csinfo[i].to_fp);
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (freefnpp)
2390 {
2391 vim_free(csinfo[i].fname);
2392 vim_free(csinfo[i].ppath);
2393 vim_free(csinfo[i].flags);
2394 }
2395
2396 clear_csinfo(i);
2397} /* cs_release_csp */
2398
2399
2400/*
2401 * PRIVATE: cs_reset
2402 *
2403 * calls cs_kill on all cscope connections then reinits
2404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 static int
2406cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002407 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2410 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002411 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002413 if (csinfo_size == 0)
2414 return CSCOPE_SUCCESS;
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002417 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2418 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2419 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (dblist == NULL || pplist == NULL || fllist == NULL)
2421 {
2422 vim_free(dblist);
2423 vim_free(pplist);
2424 vim_free(fllist);
2425 return CSCOPE_FAILURE;
2426 }
2427
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002428 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 dblist[i] = csinfo[i].fname;
2431 pplist[i] = csinfo[i].ppath;
2432 fllist[i] = csinfo[i].flags;
2433 if (csinfo[i].fname != NULL)
2434 cs_release_csp(i, FALSE);
2435 }
2436
2437 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002438 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 if (dblist[i] != NULL)
2441 {
2442 cs_add_common(dblist[i], pplist[i], fllist[i]);
2443 if (p_csverbose)
2444 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002445 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 * connection number in the same line as
2447 * "Added cscope database..."
2448 */
2449 sprintf(buf, " (#%d)", i);
2450 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2451 }
2452 }
2453 vim_free(dblist[i]);
2454 vim_free(pplist[i]);
2455 vim_free(fllist[i]);
2456 }
2457 vim_free(dblist);
2458 vim_free(pplist);
2459 vim_free(fllist);
2460
2461 if (p_csverbose)
2462 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2463 return CSCOPE_SUCCESS;
2464} /* cs_reset */
2465
2466
2467/*
2468 * PRIVATE: cs_resolve_file
2469 *
2470 * construct the full pathname to a file found in the cscope database.
2471 * (Prepends ppath, if there is one and if it's not already prepended,
2472 * otherwise just uses the name found.)
2473 *
2474 * we need to prepend the prefix because on some cscope's (e.g., the one that
2475 * ships with Solaris 2.6), the output never has the prefix prepended.
2476 * contrast this with my development system (Digital Unix), which does.
2477 */
2478 static char *
2479cs_resolve_file(i, name)
2480 int i;
2481 char *name;
2482{
2483 char *fullname;
2484 int len;
2485
2486 /*
2487 * ppath is freed when we destroy the cscope connection.
2488 * fullname is freed after cs_make_vim_style_matches, after it's been
2489 * copied into the tag buffer used by vim
2490 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002491 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002493 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 if ((fullname = (char *)alloc(len)) == NULL)
2496 return NULL;
2497
2498 /*
2499 * note/example: this won't work if the cscope output already starts
2500 * "../.." and the prefix path is also "../..". if something like this
2501 * happens, you are screwed up and need to fix how you're using cscope.
2502 */
2503 if (csinfo[i].ppath != NULL &&
2504 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2505 (name[0] != '/')
2506#ifdef WIN32
2507 && name[0] != '\\' && name[1] != ':'
2508#endif
2509 )
2510 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2511 else
2512 (void)sprintf(fullname, "%s", name);
2513
2514 return fullname;
2515} /* cs_resolve_file */
2516
2517
2518/*
2519 * PRIVATE: cs_show
2520 *
2521 * show all cscope connections
2522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 static int
2524cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002525 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 short i;
2528 if (cs_cnt_connections() == 0)
2529 MSG_PUTS(_("no cscope connections\n"));
2530 else
2531 {
2532 MSG_PUTS_ATTR(
2533 _(" # pid database name prepend path\n"),
2534 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002535 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 if (csinfo[i].fname == NULL)
2538 continue;
2539
2540 if (csinfo[i].ppath != NULL)
2541 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2542 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2543 else
2544 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2545 i, (long)csinfo[i].pid, csinfo[i].fname);
2546 }
2547 }
2548
2549 wait_return(TRUE);
2550 return CSCOPE_SUCCESS;
2551} /* cs_show */
2552
Bram Moolenaar02b06312007-09-06 15:39:22 +00002553
2554/*
2555 * PUBLIC: cs_end
2556 *
2557 * Only called when VIM exits to quit any cscope sessions.
2558 */
2559 void
2560cs_end()
2561{
2562 int i;
2563
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002564 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002565 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002566 vim_free(csinfo);
2567 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002568}
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif /* FEAT_CSCOPE */
2571
2572/* the end */