blob: 1a377da85d0d35445dd5a788947409ab2b7b32ef [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 Moolenaar7fd73202010-07-25 16:58:46 +020047static int cs_find_common __ARGS((char *opt, char *pat, int, int, int, char_u *cmdline));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int cs_help __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void clear_csinfo __ARGS((int i));
50static int cs_insert_filelist __ARGS((char *, char *, char *,
51 struct stat *));
52static int cs_kill __ARGS((exarg_T *eap));
53static void cs_kill_execute __ARGS((int, char *));
54static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
55static char * cs_make_vim_style_matches __ARGS((char *, char *,
56 char *, char *));
57static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
58static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
59static char * cs_pathcomponents __ARGS((char *path));
60static void cs_print_tags_priv __ARGS((char **, char **, int));
Bram Moolenaar02b06312007-09-06 15:39:22 +000061static int cs_read_prompt __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static void cs_release_csp __ARGS((int, int freefnpp));
63static int cs_reset __ARGS((exarg_T *eap));
64static char * cs_resolve_file __ARGS((int, char *));
65static int cs_show __ARGS((exarg_T *eap));
66
67
Bram Moolenaar9fa49da2009-07-10 13:11:26 +000068static csinfo_T * csinfo = NULL;
69static int csinfo_size = 0; /* number of items allocated in
70 csinfo[] */
71
Bram Moolenaard2ac9842007-08-21 16:03:51 +000072static int eap_arg_len; /* length of eap->arg, set in
73 cs_lookup_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074static cscmd_T cs_cmds[] =
75{
76 { "add", cs_add,
77 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
78 { "find", cs_find,
Bram Moolenaar627943d2008-08-25 02:35:59 +000079 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 { "help", cs_help,
81 N_("Show this message"), "help", 0 },
82 { "kill", cs_kill,
83 N_("Kill a connection"), "kill #", 0 },
84 { "reset", cs_reset,
85 N_("Reinit all connections"), "reset", 0 },
86 { "show", cs_show,
87 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000088 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000089};
90
91 static void
92cs_usage_msg(x)
93 csid_e x;
94{
95 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
96}
97
Bram Moolenaarf4580d82009-03-18 11:52:53 +000098#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
99
100static enum
101{
102 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000103 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000104 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
105 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
106} expand_what;
107
108/*
109 * Function given to ExpandGeneric() to obtain the cscope command
110 * expansion.
111 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000112 char_u *
113get_cscope_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000114 expand_T *xp UNUSED;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000115 int idx;
116{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000117 int current_idx;
118 int i;
119
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000120 switch (expand_what)
121 {
122 case EXP_CSCOPE_SUBCMD:
123 /* Complete with sub-commands of ":cscope":
124 * add, find, help, kill, reset, show */
125 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000126 case EXP_SCSCOPE_SUBCMD:
127 /* Complete with sub-commands of ":scscope": same sub-commands as
128 * ":cscope" but skip commands which don't support split windows */
129 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
130 if (cs_cmds[i].cansplit)
131 if (current_idx++ == idx)
132 break;
133 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000134 case EXP_CSCOPE_FIND:
135 {
136 const char *query_type[] =
137 {
138 "c", "d", "e", "f", "g", "i", "s", "t", NULL
139 };
140
141 /* Complete with query type of ":cscope find {query_type}".
142 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
143 * ..., 8) but only complete with letters, since numbers are
144 * redundant. */
145 return (char_u *)query_type[idx];
146 }
147 case EXP_CSCOPE_KILL:
148 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000149 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000150
151 /* ":cscope kill" accepts connection numbers or partial names of
152 * the pathname of the cscope database as argument. Only complete
153 * with connection numbers. -1 can also be used to kill all
154 * connections. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000155 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000156 {
157 if (csinfo[i].fname == NULL)
158 continue;
159 if (current_idx++ == idx)
160 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000161 vim_snprintf(connection, sizeof(connection), "%d", i);
162 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000163 }
164 }
165 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
166 }
167 default:
168 return NULL;
169 }
170}
171
172/*
173 * Handle command line completion for :cscope command.
174 */
175 void
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000176set_context_in_cscope_cmd(xp, arg, cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000177 expand_T *xp;
178 char_u *arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000179 cmdidx_T cmdidx;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000180{
181 char_u *p;
182
183 /* Default: expand subcommands */
184 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000185 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000186 expand_what = (cmdidx == CMD_scscope)
187 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000188
189 /* (part of) subcommand already typed */
190 if (*arg != NUL)
191 {
192 p = skiptowhite(arg);
193 if (*p != NUL) /* past first word */
194 {
195 xp->xp_pattern = skipwhite(p);
196 if (*skiptowhite(xp->xp_pattern) != NUL)
197 xp->xp_context = EXPAND_NOTHING;
198 else if (STRNICMP(arg, "add", p - arg) == 0)
199 xp->xp_context = EXPAND_FILES;
200 else if (STRNICMP(arg, "kill", p - arg) == 0)
201 expand_what = EXP_CSCOPE_KILL;
202 else if (STRNICMP(arg, "find", p - arg) == 0)
203 expand_what = EXP_CSCOPE_FIND;
204 else
205 xp->xp_context = EXPAND_NOTHING;
206 }
207 }
208}
209
210#endif /* FEAT_CMDL_COMPL */
211
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212/*
213 * PRIVATE: do_cscope_general
214 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000215 * Find the command, print help if invalid, and then call the corresponding
216 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 */
218 static void
219do_cscope_general(eap, make_split)
220 exarg_T *eap;
221 int make_split; /* whether to split window */
222{
223 cscmd_T *cmdp;
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
226 {
227 cs_help(eap);
228 return;
229 }
230
231#ifdef FEAT_WINDOWS
232 if (make_split)
233 {
234 if (!cmdp->cansplit)
235 {
236 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
237 return;
238 }
239 postponed_split = -1;
240 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000241 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 }
243#endif
244
245 cmdp->func(eap);
246
247#ifdef FEAT_WINDOWS
248 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000249 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#endif
251}
252
253/*
254 * PUBLIC: do_cscope
255 */
256 void
257do_cscope(eap)
258 exarg_T *eap;
259{
260 do_cscope_general(eap, FALSE);
261}
262
263/*
264 * PUBLIC: do_scscope
265 *
266 * same as do_cscope, but splits window, too.
267 */
268 void
269do_scscope(eap)
270 exarg_T *eap;
271{
272 do_cscope_general(eap, TRUE);
273}
274
275/*
276 * PUBLIC: do_cstag
277 *
278 */
279 void
280do_cstag(eap)
281 exarg_T *eap;
282{
283 int ret = FALSE;
284
Bram Moolenaar446cb832008-06-24 21:56:24 +0000285 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {
287 (void)EMSG(_("E562: Usage: cstag <ident>"));
288 return;
289 }
290
291 switch (p_csto)
292 {
293 case 0 :
294 if (cs_check_for_connections())
295 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000296 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200297 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 if (ret == FALSE)
299 {
300 cs_free_tags();
301 if (msg_col)
302 msg_putchar('\n');
303
304 if (cs_check_for_tags())
305 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
306 }
307 }
308 else if (cs_check_for_tags())
309 {
310 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
311 }
312 break;
313 case 1 :
314 if (cs_check_for_tags())
315 {
316 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
317 if (ret == FALSE)
318 {
319 if (msg_col)
320 msg_putchar('\n');
321
322 if (cs_check_for_connections())
323 {
324 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200325 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (ret == FALSE)
327 cs_free_tags();
328 }
329 }
330 }
331 else if (cs_check_for_connections())
332 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000333 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200334 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (ret == FALSE)
336 cs_free_tags();
337 }
338 break;
339 default :
340 break;
341 }
342
343 if (!ret)
344 {
345 (void)EMSG(_("E257: cstag: tag not found"));
346#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
347 g_do_tagpreview = 0;
348#endif
349 }
350
351} /* do_cscope */
352
353
354/*
355 * PUBLIC: cs_find
356 *
357 * this simulates a vim_fgets(), but for cscope, returns the next line
358 * from the cscope output. should only be called from find_tags()
359 *
360 * returns TRUE if eof, FALSE otherwise
361 */
362 int
363cs_fgets(buf, size)
364 char_u *buf;
365 int size;
366{
367 char *p;
368
369 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
370 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000371 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373 return FALSE;
374} /* cs_fgets */
375
376
377/*
378 * PUBLIC: cs_free_tags
379 *
380 * called only from do_tag(), when popping the tag stack
381 */
382 void
383cs_free_tags()
384{
385 cs_manage_matches(NULL, NULL, -1, Free);
386}
387
388
389/*
390 * PUBLIC: cs_print_tags
391 *
392 * called from do_tag()
393 */
394 void
395cs_print_tags()
396{
397 cs_manage_matches(NULL, NULL, -1, Print);
398}
399
400
401/*
402 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
403 *
404 * Checks for the existence of a |cscope| connection. If no
405 * parameters are specified, then the function returns:
406 *
407 * 0, if cscope was not available (not compiled in), or if there
408 * are no cscope connections; or
409 * 1, if there is at least one cscope connection.
410 *
411 * If parameters are specified, then the value of {num}
412 * determines how existence of a cscope connection is checked:
413 *
414 * {num} Description of existence check
415 * ----- ------------------------------
416 * 0 Same as no parameters (e.g., "cscope_connection()").
417 * 1 Ignore {prepend}, and use partial string matches for
418 * {dbpath}.
419 * 2 Ignore {prepend}, and use exact string matches for
420 * {dbpath}.
421 * 3 Use {prepend}, use partial string matches for both
422 * {dbpath} and {prepend}.
423 * 4 Use {prepend}, use exact string matches for both
424 * {dbpath} and {prepend}.
425 *
426 * Note: All string comparisons are case sensitive!
427 */
428#if defined(FEAT_EVAL) || defined(PROTO)
429 int
430cs_connection(num, dbpath, ppath)
431 int num;
432 char_u *dbpath;
433 char_u *ppath;
434{
435 int i;
436
437 if (num < 0 || num > 4 || (num > 0 && !dbpath))
438 return FALSE;
439
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000440 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {
442 if (!csinfo[i].fname)
443 continue;
444
445 if (num == 0)
446 return TRUE;
447
448 switch (num)
449 {
450 case 1:
451 if (strstr(csinfo[i].fname, (char *)dbpath))
452 return TRUE;
453 break;
454 case 2:
455 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
456 return TRUE;
457 break;
458 case 3:
459 if (strstr(csinfo[i].fname, (char *)dbpath)
460 && ((!ppath && !csinfo[i].ppath)
461 || (ppath
462 && csinfo[i].ppath
463 && strstr(csinfo[i].ppath, (char *)ppath))))
464 return TRUE;
465 break;
466 case 4:
467 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
468 && ((!ppath && !csinfo[i].ppath)
469 || (ppath
470 && csinfo[i].ppath
471 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
472 return TRUE;
473 break;
474 }
475 }
476
477 return FALSE;
478} /* cs_connection */
479#endif
480
481
482/*
483 * PRIVATE functions
484 ****************************************************************************/
485
486/*
487 * PRIVATE: cs_add
488 *
489 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000490 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 *
492 * MAXPATHL 256
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 static int
495cs_add(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000496 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 char *fname, *ppath, *flags = NULL;
499
500 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
501 {
502 cs_usage_msg(Add);
503 return CSCOPE_FAILURE;
504 }
505 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
506 flags = strtok((char *)NULL, (const char *)" ");
507
508 return cs_add_common(fname, ppath, flags);
509}
510
511 static void
512cs_stat_emsg(fname)
513 char *fname;
514{
515 char *stat_emsg = _("E563: stat(%s) error: %d");
516 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
517
518 if (buf != NULL)
519 {
520 (void)sprintf(buf, stat_emsg, fname, errno);
521 (void)EMSG(buf);
522 vim_free(buf);
523 }
524 else
525 (void)EMSG(_("E563: stat error"));
526}
527
528
529/*
530 * PRIVATE: cs_add_common
531 *
532 * the common routine to add a new cscope connection. called by
533 * cs_add() and cs_reset(). i really don't like to do this, but this
534 * routine uses a number of goto statements.
535 */
536 static int
537cs_add_common(arg1, arg2, flags)
538 char *arg1; /* filename - may contain environment variables */
539 char *arg2; /* prepend path - may contain environment variables */
540 char *flags;
541{
542 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000543 int ret;
544 char *fname = NULL;
545 char *fname2 = NULL;
546 char *ppath = NULL;
547 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000550 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 goto add_err;
552
553 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
554 ret = stat(fname, &statbuf);
555 if (ret < 0)
556 {
557staterr:
558 if (p_csverbose)
559 cs_stat_emsg(fname);
560 goto add_err;
561 }
562
563 /* get the prepend path (arg2), expand it, and try to stat it */
564 if (arg2 != NULL)
565 {
566 struct stat statbuf2;
567
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000568 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 goto add_err;
570
571 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
572 ret = stat(ppath, &statbuf2);
573 if (ret < 0)
574 goto staterr;
575 }
576
577 /* if filename is a directory, append the cscope database name to it */
578 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
579 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000580 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (fname2 == NULL)
582 goto add_err;
583
584 while (fname[strlen(fname)-1] == '/'
585#ifdef WIN32
586 || fname[strlen(fname)-1] == '\\'
587#endif
588 )
589 {
590 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200591 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 break;
593 }
594 if (fname[0] == '\0')
595 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
596 else
597 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
598
599 ret = stat(fname2, &statbuf);
600 if (ret < 0)
601 {
602 if (p_csverbose)
603 cs_stat_emsg(fname2);
604 goto add_err;
605 }
606
607 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
608 }
609#if defined(UNIX)
610 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
611#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000612 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
614#endif
615 {
616 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
617 }
618 else
619 {
620 if (p_csverbose)
621 (void)EMSG2(
622 _("E564: %s is not a directory or a valid cscope database"),
623 fname);
624 goto add_err;
625 }
626
627 if (i != -1)
628 {
629 if (cs_create_connection(i) == CSCOPE_FAILURE
630 || cs_read_prompt(i) == CSCOPE_FAILURE)
631 {
632 cs_release_csp(i, TRUE);
633 goto add_err;
634 }
635
636 if (p_csverbose)
637 {
638 msg_clr_eos();
639 (void)smsg_attr(hl_attr(HLF_R),
640 (char_u *)_("Added cscope database %s"),
641 csinfo[i].fname);
642 }
643 }
644
645 vim_free(fname);
646 vim_free(fname2);
647 vim_free(ppath);
648 return CSCOPE_SUCCESS;
649
650add_err:
651 vim_free(fname2);
652 vim_free(fname);
653 vim_free(ppath);
654 return CSCOPE_FAILURE;
655} /* cs_add_common */
656
657
658 static int
659cs_check_for_connections()
660{
661 return (cs_cnt_connections() > 0);
662} /* cs_check_for_connections */
663
664
665 static int
666cs_check_for_tags()
667{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000668 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669} /* cs_check_for_tags */
670
671
672/*
673 * PRIVATE: cs_cnt_connections
674 *
675 * count the number of cscope connections
676 */
677 static int
678cs_cnt_connections()
679{
680 short i;
681 short cnt = 0;
682
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000683 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 if (csinfo[i].fname != NULL)
686 cnt++;
687 }
688 return cnt;
689} /* cs_cnt_connections */
690
691 static void
692cs_reading_emsg(idx)
693 int idx; /* connection index */
694{
695 EMSGN(_("E262: error reading cscope connection %ld"), idx);
696}
697
698#define CSREAD_BUFSIZE 2048
699/*
700 * PRIVATE: cs_cnt_matches
701 *
702 * count the number of matches for a given cscope connection.
703 */
704 static int
705cs_cnt_matches(idx)
706 int idx;
707{
708 char *stok;
709 char *buf;
710 int nlines;
711
712 buf = (char *)alloc(CSREAD_BUFSIZE);
713 if (buf == NULL)
714 return 0;
715 for (;;)
716 {
717 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
718 {
719 if (feof(csinfo[idx].fr_fp))
720 errno = EIO;
721
722 cs_reading_emsg(idx);
723
724 vim_free(buf);
725 return -1;
726 }
727
728 /*
729 * If the database is out of date, or there's some other problem,
730 * cscope will output error messages before the number-of-lines output.
731 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000732 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
734 if ((stok = strtok(buf, (const char *)" ")) == NULL)
735 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000736 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 continue;
738
739 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
740 continue;
741 nlines = atoi(stok);
742 if (nlines < 0)
743 {
744 nlines = 0;
745 break;
746 }
747
748 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
749 continue;
750 if (strncmp((const char *)stok, "lines", 5))
751 continue;
752
753 break;
754 }
755
756 vim_free(buf);
757 return nlines;
758} /* cs_cnt_matches */
759
760
761/*
762 * PRIVATE: cs_create_cmd
763 *
764 * Creates the actual cscope command query from what the user entered.
765 */
766 static char *
767cs_create_cmd(csoption, pattern)
768 char *csoption;
769 char *pattern;
770{
771 char *cmd;
772 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000773 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 switch (csoption[0])
776 {
777 case '0' : case 's' :
778 search = 0;
779 break;
780 case '1' : case 'g' :
781 search = 1;
782 break;
783 case '2' : case 'd' :
784 search = 2;
785 break;
786 case '3' : case 'c' :
787 search = 3;
788 break;
789 case '4' : case 't' :
790 search = 4;
791 break;
792 case '6' : case 'e' :
793 search = 6;
794 break;
795 case '7' : case 'f' :
796 search = 7;
797 break;
798 case '8' : case 'i' :
799 search = 8;
800 break;
801 default :
802 (void)EMSG(_("E561: unknown cscope search type"));
803 cs_usage_msg(Find);
804 return NULL;
805 }
806
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000807 /* Skip white space before the patter, except for text and pattern search,
808 * they may want to use the leading white space. */
809 pat = pattern;
810 if (search != 4 && search != 6)
811 while vim_iswhite(*pat)
812 ++pat;
813
814 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return NULL;
816
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000817 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 return cmd;
820} /* cs_create_cmd */
821
822
823/*
824 * PRIVATE: cs_create_connection
825 *
826 * This piece of code was taken/adapted from nvi. do we need to add
827 * the BSD license notice?
828 */
829 static int
830cs_create_connection(i)
831 int i;
832{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000833#ifdef UNIX
834 int to_cs[2], from_cs[2];
835#endif
836 int len;
837 char *prog, *cmd, *ppath = NULL;
838#ifdef WIN32
839 int fd;
840 SECURITY_ATTRIBUTES sa;
841 PROCESS_INFORMATION pi;
842 STARTUPINFO si;
843 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
844 HANDLE stdin_rd, stdout_rd;
845 HANDLE stdout_wr, stdin_wr;
846 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000847# ifdef __BORLANDC__
848# define OPEN_OH_ARGTYPE long
849# else
850# if (_MSC_VER >= 1300)
851# define OPEN_OH_ARGTYPE intptr_t
852# else
853# define OPEN_OH_ARGTYPE long
854# endif
855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
Bram Moolenaar02b06312007-09-06 15:39:22 +0000858#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 /*
860 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
861 * from_cs[0] and writes to to_cs[1].
862 */
863 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
864 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
865 {
866 (void)EMSG(_("E566: Could not create cscope pipes"));
867err_closing:
868 if (to_cs[0] != -1)
869 (void)close(to_cs[0]);
870 if (to_cs[1] != -1)
871 (void)close(to_cs[1]);
872 if (from_cs[0] != -1)
873 (void)close(from_cs[0]);
874 if (from_cs[1] != -1)
875 (void)close(from_cs[1]);
876 return CSCOPE_FAILURE;
877 }
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 switch (csinfo[i].pid = fork())
880 {
881 case -1:
882 (void)EMSG(_("E622: Could not fork for cscope"));
883 goto err_closing;
884 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (dup2(to_cs[0], STDIN_FILENO) == -1)
886 PERROR("cs_create_connection 1");
887 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
888 PERROR("cs_create_connection 2");
889 if (dup2(from_cs[1], STDERR_FILENO) == -1)
890 PERROR("cs_create_connection 3");
891
892 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 (void)close(to_cs[1]);
894 (void)close(from_cs[0]);
895#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000896 /* WIN32 */
897 /* Create pipes to communicate with cscope */
898 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
899 sa.bInheritHandle = TRUE;
900 sa.lpSecurityDescriptor = NULL;
901
902 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
903 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
904 {
905 (void)EMSG(_("E566: Could not create cscope pipes"));
906err_closing:
907 if (pipe_stdin)
908 {
909 CloseHandle(stdin_rd);
910 CloseHandle(stdin_wr);
911 }
912 if (pipe_stdout)
913 {
914 CloseHandle(stdout_rd);
915 CloseHandle(stdout_wr);
916 }
917 return CSCOPE_FAILURE;
918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#endif
920 /* expand the cscope exec for env var's */
921 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
922 {
923#ifdef UNIX
924 return CSCOPE_FAILURE;
925#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000926 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 goto err_closing;
928#endif
929 }
930 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
931
932 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000933 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (csinfo[i].ppath)
935 {
936 /* expand the prepend path for env var's */
937 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
938 {
939 vim_free(prog);
940#ifdef UNIX
941 return CSCOPE_FAILURE;
942#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000943 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 goto err_closing;
945#endif
946 }
947 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000949 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951
952 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000953 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 if ((cmd = (char *)alloc(len)) == NULL)
956 {
957 vim_free(prog);
958 vim_free(ppath);
959#ifdef UNIX
960 return CSCOPE_FAILURE;
961#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000962 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 goto err_closing;
964#endif
965 }
966
967 /* run the cscope command; is there execl for non-unix systems? */
968#if defined(UNIX)
969 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
970#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000971 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
973#endif
974 if (csinfo[i].ppath != NULL)
975 {
976 (void)strcat(cmd, " -P");
977 (void)strcat(cmd, csinfo[i].ppath);
978 }
979 if (csinfo[i].flags != NULL)
980 {
981 (void)strcat(cmd, " ");
982 (void)strcat(cmd, csinfo[i].flags);
983 }
984# ifdef UNIX
985 /* on Win32 we still need prog */
986 vim_free(prog);
987# endif
988 vim_free(ppath);
989
990#if defined(UNIX)
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000991 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 PERROR(_("cs_create_connection exec failed"));
993
994 exit(127);
995 /* NOTREACHED */
996 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /*
998 * Save the file descriptors for later duplication, and
999 * reopen as streams.
1000 */
1001 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1002 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1003 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1004 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /* close unused */
1007 (void)close(to_cs[0]);
1008 (void)close(from_cs[1]);
1009
1010 break;
1011 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001014 /* WIN32 */
1015 /* Create a new process to run cscope and use pipes to talk with it */
1016 GetStartupInfo(&si);
1017 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1018 si.wShowWindow = SW_HIDE; /* Hide child application window */
1019 si.hStdOutput = stdout_wr;
1020 si.hStdError = stdout_wr;
1021 si.hStdInput = stdin_rd;
1022 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1023 NULL, NULL, &si, &pi);
1024 vim_free(prog);
1025 vim_free(cmd);
1026
1027 if (!created)
1028 {
1029 PERROR(_("cs_create_connection exec failed"));
1030 (void)EMSG(_("E623: Could not spawn cscope process"));
1031 goto err_closing;
1032 }
1033 /* else */
1034 csinfo[i].pid = pi.dwProcessId;
1035 csinfo[i].hProc = pi.hProcess;
1036 CloseHandle(pi.hThread);
1037
1038 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001039 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1040 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001041 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1042 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1044 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001045 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1046 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1047
1048 /* Close handles for file descriptors inherited by the cscope process */
1049 CloseHandle(stdin_rd);
1050 CloseHandle(stdout_wr);
1051
1052#endif /* !UNIX */
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 return CSCOPE_SUCCESS;
1055} /* cs_create_connection */
1056
1057
1058/*
1059 * PRIVATE: cs_find
1060 *
1061 * query cscope using command line interface. parse the output and use tselect
1062 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1063 *
1064 * returns TRUE if we jump to a tag or abort, FALSE if not.
1065 */
1066 static int
1067cs_find(eap)
1068 exarg_T *eap;
1069{
1070 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001071 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
1073 if (cs_check_for_connections() == FALSE)
1074 {
1075 (void)EMSG(_("E567: no cscope connections"));
1076 return FALSE;
1077 }
1078
1079 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1080 {
1081 cs_usage_msg(Find);
1082 return FALSE;
1083 }
1084
1085 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001086 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
1088 cs_usage_msg(Find);
1089 return FALSE;
1090 }
1091
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001092 /*
1093 * Let's replace the NULs written by strtok() with spaces - we need the
1094 * spaces to correctly display the quickfix/location list window's title.
1095 */
1096 for (i = 0; i < eap_arg_len; ++i)
1097 if (NUL == eap->arg[i])
1098 eap->arg[i] = ' ';
1099
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001100 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001101 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102} /* cs_find */
1103
1104
1105/*
1106 * PRIVATE: cs_find_common
1107 *
1108 * common code for cscope find, shared by cs_find() and do_cstag()
1109 */
1110 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001111cs_find_common(opt, pat, forceit, verbose, use_ll, cmdline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 char *opt;
1113 char *pat;
1114 int forceit;
1115 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001116 int use_ll;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001117 char_u *cmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 int i;
1120 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001121 int *nummatches;
1122 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef FEAT_QUICKFIX
1124 char cmdletter;
1125 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001126
1127 /* get cmd letter */
1128 switch (opt[0])
1129 {
1130 case '0' :
1131 cmdletter = 's';
1132 break;
1133 case '1' :
1134 cmdletter = 'g';
1135 break;
1136 case '2' :
1137 cmdletter = 'd';
1138 break;
1139 case '3' :
1140 cmdletter = 'c';
1141 break;
1142 case '4' :
1143 cmdletter = 't';
1144 break;
1145 case '6' :
1146 cmdletter = 'e';
1147 break;
1148 case '7' :
1149 cmdletter = 'f';
1150 break;
1151 case '8' :
1152 cmdletter = 'i';
1153 break;
1154 default :
1155 cmdletter = opt[0];
1156 }
1157
1158 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1159 if (qfpos != NULL)
1160 {
1161 qfpos++;
1162 /* next symbol must be + or - */
1163 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1164 {
1165 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1166 char *buf = (char *)alloc((unsigned)strlen(nf));
1167
1168 /* strlen will be enough because we use chars */
1169 if (buf != NULL)
1170 {
1171 sprintf(buf, nf, *qfpos, *(qfpos-1));
1172 (void)EMSG(buf);
1173 vim_free(buf);
1174 }
1175 return FALSE;
1176 }
1177
1178# ifdef FEAT_AUTOCMD
1179 if (*qfpos != '0')
1180 {
1181 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1182 curbuf->b_fname, TRUE, curbuf);
1183# ifdef FEAT_EVAL
1184 if (did_throw || force_abort)
1185 return FALSE;
1186# endif
1187 }
1188# endif
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#endif
1191
1192 /* create the actual command to send to cscope */
1193 cmd = cs_create_cmd(opt, pat);
1194 if (cmd == NULL)
1195 return FALSE;
1196
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001197 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1198 if (nummatches == NULL)
1199 return FALSE;
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 /* send query to all open connections, then count the total number
1202 * of matches so we can alloc matchesp all in one swell foop
1203 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001204 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 nummatches[i] = 0;
1206 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001207 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001209 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 continue;
1211
1212 /* send cmd to cscope */
1213 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1214 (void)fflush(csinfo[i].to_fp);
1215
1216 nummatches[i] = cs_cnt_matches(i);
1217
1218 if (nummatches[i] > -1)
1219 totmatches += nummatches[i];
1220
1221 if (nummatches[i] == 0)
1222 (void)cs_read_prompt(i);
1223 }
1224 vim_free(cmd);
1225
1226 if (totmatches == 0)
1227 {
1228 char *nf = _("E259: no matches found for cscope query %s of %s");
1229 char *buf;
1230
1231 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001232 {
1233 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001237 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (buf == NULL)
1239 (void)EMSG(nf);
1240 else
1241 {
1242 sprintf(buf, nf, opt, pat);
1243 (void)EMSG(buf);
1244 vim_free(buf);
1245 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001246 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 return FALSE;
1248 }
1249
1250#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001251 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001254 FILE *f;
1255 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001256 qf_info_T *qi = NULL;
1257 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001259 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001260 if (f == NULL)
1261 EMSG2(_(e_notopen), tmp);
1262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001264 cs_file_results(f, nummatches);
1265 fclose(f);
1266 if (use_ll) /* Use location list */
1267 wp = curwin;
1268 /* '-' starts a new error list */
1269 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001270 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001272# ifdef FEAT_WINDOWS
1273 if (postponed_split != 0)
1274 {
1275 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001277 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001278 postponed_split = 0;
1279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001281
1282# ifdef FEAT_AUTOCMD
1283 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1284 curbuf->b_fname, TRUE, curbuf);
1285# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001286 if (use_ll)
1287 /*
1288 * In the location list window, use the displayed location
1289 * list. Otherwise, use the location list for the window.
1290 */
1291 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1292 ? wp->w_llist_ref : wp->w_llist;
1293 qf_jump(qi, 0, 0, forceit);
1294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296 mch_remove(tmp);
1297 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001298 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 return TRUE;
1300 }
1301 else
1302#endif /* FEAT_QUICKFIX */
1303 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001304 char **matches = NULL, **contexts = NULL;
1305 int matched = 0;
1306
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 /* read output */
1308 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1309 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001310 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (matches == NULL)
1312 return FALSE;
1313
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001314 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1317 }
1318
1319} /* cs_find_common */
1320
1321/*
1322 * PRIVATE: cs_help
1323 *
1324 * print help
1325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 static int
1327cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001328 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 cscmd_T *cmdp = cs_cmds;
1331
1332 (void)MSG_PUTS(_("cscope commands:\n"));
1333 while (cmdp->name != NULL)
1334 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001335 char *help = _(cmdp->help);
1336 int space_cnt = 30 - vim_strsize((char_u *)help);
1337
1338 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1339 if (space_cnt < 0)
1340 space_cnt = 0;
1341 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1342 cmdp->name,
1343 help, space_cnt, " ",
1344 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001346 MSG_PUTS(_("\n"
1347 " c: Find functions calling this function\n"
1348 " d: Find functions called by this function\n"
1349 " e: Find this egrep pattern\n"
1350 " f: Find this file\n"
1351 " g: Find this definition\n"
1352 " i: Find files #including this file\n"
1353 " s: Find this C symbol\n"
1354 " t: Find assignments to\n"));
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 cmdp++;
1357 }
1358
1359 wait_return(TRUE);
1360 return 0;
1361} /* cs_help */
1362
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 static void
1365clear_csinfo(i)
1366 int i;
1367{
1368 csinfo[i].fname = NULL;
1369 csinfo[i].ppath = NULL;
1370 csinfo[i].flags = NULL;
1371#if defined(UNIX)
1372 csinfo[i].st_dev = (dev_t)0;
1373 csinfo[i].st_ino = (ino_t)0;
1374#else
1375 csinfo[i].nVolume = 0;
1376 csinfo[i].nIndexHigh = 0;
1377 csinfo[i].nIndexLow = 0;
1378#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001379 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 csinfo[i].fr_fp = NULL;
1381 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001382#if defined(WIN32)
1383 csinfo[i].hProc = NULL;
1384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385}
1386
1387#ifndef UNIX
1388static char *GetWin32Error __ARGS((void));
1389
1390 static char *
1391GetWin32Error()
1392{
1393 char *msg = NULL;
1394 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1395 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1396 if (msg != NULL)
1397 {
1398 /* remove trailing \r\n */
1399 char *pcrlf = strstr(msg, "\r\n");
1400 if (pcrlf != NULL)
1401 *pcrlf = '\0';
1402 }
1403 return msg;
1404}
1405#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407/*
1408 * PRIVATE: cs_insert_filelist
1409 *
1410 * insert a new cscope database filename into the filelist
1411 */
1412 static int
1413cs_insert_filelist(fname, ppath, flags, sb)
1414 char *fname;
1415 char *ppath;
1416 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001417 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 short i, j;
1420#ifndef UNIX
1421 HANDLE hFile;
1422 BY_HANDLE_FILE_INFORMATION bhfi;
1423
1424 vim_memset(&bhfi, 0, sizeof(bhfi));
1425 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1426 if (!mch_windows95())
1427 {
1428 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1429 FILE_ATTRIBUTE_NORMAL, NULL);
1430 if (hFile == INVALID_HANDLE_VALUE)
1431 {
1432 if (p_csverbose)
1433 {
1434 char *cant_msg = _("E625: cannot open cscope database: %s");
1435 char *winmsg = GetWin32Error();
1436
1437 if (winmsg != NULL)
1438 {
1439 (void)EMSG2(cant_msg, winmsg);
1440 LocalFree(winmsg);
1441 }
1442 else
1443 /* subst filename if can't get error text */
1444 (void)EMSG2(cant_msg, fname);
1445 }
1446 return -1;
1447 }
1448 if (!GetFileInformationByHandle(hFile, &bhfi))
1449 {
1450 CloseHandle(hFile);
1451 if (p_csverbose)
1452 (void)EMSG(_("E626: cannot get cscope database information"));
1453 return -1;
1454 }
1455 CloseHandle(hFile);
1456 }
1457#endif
1458
1459 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001460 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
1462 if (csinfo[j].fname != NULL
1463#if defined(UNIX)
1464 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1465#else
1466 /* compare pathnames first */
1467 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001468 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 || (!mch_windows95()
1470 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1471 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1472 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1473#endif
1474 )
1475 {
1476 if (p_csverbose)
1477 (void)EMSG(_("E568: duplicate cscope database not added"));
1478 return -1;
1479 }
1480
1481 if (csinfo[j].fname == NULL && i == -1)
1482 i = j; /* remember first empty entry */
1483 }
1484
1485 if (i == -1)
1486 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001487 i = csinfo_size;
1488 if (csinfo_size == 0)
1489 {
1490 /* First time allocation: allocate only 1 connection. It should
1491 * be enough for most users. If more is needed, csinfo will be
1492 * reallocated. */
1493 csinfo_size = 1;
1494 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1495 }
1496 else
1497 {
1498 /* Reallocate space for more connections. */
1499 csinfo_size *= 2;
1500 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1501 }
1502 if (csinfo == NULL)
1503 return -1;
1504 for (j = csinfo_size/2; j < csinfo_size; j++)
1505 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001508 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 return -1;
1510
1511 (void)strcpy(csinfo[i].fname, (const char *)fname);
1512
1513 if (ppath != NULL)
1514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001515 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
1517 vim_free(csinfo[i].fname);
1518 csinfo[i].fname = NULL;
1519 return -1;
1520 }
1521 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1522 } else
1523 csinfo[i].ppath = NULL;
1524
1525 if (flags != NULL)
1526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001527 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 vim_free(csinfo[i].fname);
1530 vim_free(csinfo[i].ppath);
1531 csinfo[i].fname = NULL;
1532 csinfo[i].ppath = NULL;
1533 return -1;
1534 }
1535 (void)strcpy(csinfo[i].flags, (const char *)flags);
1536 } else
1537 csinfo[i].flags = NULL;
1538
1539#if defined(UNIX)
1540 csinfo[i].st_dev = sb->st_dev;
1541 csinfo[i].st_ino = sb->st_ino;
1542
1543#else
1544 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1545 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1546 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1547#endif
1548 return i;
1549} /* cs_insert_filelist */
1550
1551
1552/*
1553 * PRIVATE: cs_lookup_cmd
1554 *
1555 * find cscope command in command table
1556 */
1557 static cscmd_T *
1558cs_lookup_cmd(eap)
1559 exarg_T *eap;
1560{
1561 cscmd_T *cmdp;
1562 char *stok;
1563 size_t len;
1564
1565 if (eap->arg == NULL)
1566 return NULL;
1567
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001568 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001569 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1572 return NULL;
1573
1574 len = strlen(stok);
1575 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1576 {
1577 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1578 return (cmdp);
1579 }
1580 return NULL;
1581} /* cs_lookup_cmd */
1582
1583
1584/*
1585 * PRIVATE: cs_kill
1586 *
1587 * nuke em
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 static int
1590cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001591 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 char *stok;
1594 short i;
1595
1596 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1597 {
1598 cs_usage_msg(Kill);
1599 return CSCOPE_FAILURE;
1600 }
1601
1602 /* only single digit positive and negative integers are allowed */
1603 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1604 || (strlen(stok) < 3 && stok[0] == '-'
1605 && VIM_ISDIGIT((int)(stok[1]))))
1606 i = atoi(stok);
1607 else
1608 {
1609 /* It must be part of a name. We will try to find a match
1610 * within all the names in the csinfo data structure
1611 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001612 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1615 break;
1616 }
1617 }
1618
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001619 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 if (p_csverbose)
1622 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1623 }
1624 else
1625 {
1626 if (i == -1)
1627 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001628 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 if (csinfo[i].fname)
1631 cs_kill_execute(i, csinfo[i].fname);
1632 }
1633 }
1634 else
1635 cs_kill_execute(i, stok);
1636 }
1637
1638 return 0;
1639} /* cs_kill */
1640
1641
1642/*
1643 * PRIVATE: cs_kill_execute
1644 *
1645 * Actually kills a specific cscope connection.
1646 */
1647 static void
1648cs_kill_execute(i, cname)
1649 int i; /* cscope table index */
1650 char *cname; /* cscope database name */
1651{
1652 if (p_csverbose)
1653 {
1654 msg_clr_eos();
1655 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1656 (char_u *)_("cscope connection %s closed"), cname);
1657 }
1658 cs_release_csp(i, TRUE);
1659}
1660
1661
1662/*
1663 * PRIVATE: cs_make_vim_style_matches
1664 *
1665 * convert the cscope output into into a ctags style entry (as might be found
1666 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1667 * the type of the tag you are looking for. for example, in Darren Hiebert's
1668 * ctags (the one that comes with vim), #define's use a line number to find the
1669 * tag in a file while function definitions use a regexp search pattern.
1670 *
1671 * i'm going to always use the line number because cscope does something
1672 * quirky (and probably other things i don't know about):
1673 *
1674 * if you have "# define" in your source file, which is
1675 * perfectly legal, cscope thinks you have "#define". this
1676 * will result in a failed regexp search. :(
1677 *
1678 * besides, even if this particular case didn't happen, the search pattern
1679 * would still have to be modified to escape all the special regular expression
1680 * characters to comply with ctags formatting.
1681 */
1682 static char *
1683cs_make_vim_style_matches(fname, slno, search, tagstr)
1684 char *fname;
1685 char *slno;
1686 char *search;
1687 char *tagstr;
1688{
1689 /* vim style is ctags:
1690 *
1691 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1692 *
1693 * but as mentioned above, we'll always use the line number and
1694 * put the search pattern (if one exists) as "extra"
1695 *
1696 * buf is used as part of vim's method of handling tags, and
1697 * (i think) vim frees it when you pop your tags and get replaced
1698 * by new ones on the tag stack.
1699 */
1700 char *buf;
1701 int amt;
1702
1703 if (search != NULL)
1704 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001705 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if ((buf = (char *)alloc(amt)) == NULL)
1707 return NULL;
1708
1709 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1710 }
1711 else
1712 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001713 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if ((buf = (char *)alloc(amt)) == NULL)
1715 return NULL;
1716
1717 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1718 }
1719
1720 return buf;
1721} /* cs_make_vim_style_matches */
1722
1723
1724/*
1725 * PRIVATE: cs_manage_matches
1726 *
1727 * this is kind of hokey, but i don't see an easy way round this..
1728 *
1729 * Store: keep a ptr to the (malloc'd) memory of matches originally
1730 * generated from cs_find(). the matches are originally lines directly
1731 * from cscope output, but transformed to look like something out of a
1732 * ctags. see cs_make_vim_style_matches for more details.
1733 *
1734 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1735 * the next line from the cscope output. it basically keeps track of which
1736 * lines have been "used" and returns the next one.
1737 *
1738 * Free: frees up everything and resets
1739 *
1740 * Print: prints the tags
1741 */
1742 static char *
1743cs_manage_matches(matches, contexts, totmatches, cmd)
1744 char **matches;
1745 char **contexts;
1746 int totmatches;
1747 mcmd_e cmd;
1748{
1749 static char **mp = NULL;
1750 static char **cp = NULL;
1751 static int cnt = -1;
1752 static int next = -1;
1753 char *p = NULL;
1754
1755 switch (cmd)
1756 {
1757 case Store:
1758 assert(matches != NULL);
1759 assert(totmatches > 0);
1760 if (mp != NULL || cp != NULL)
1761 (void)cs_manage_matches(NULL, NULL, -1, Free);
1762 mp = matches;
1763 cp = contexts;
1764 cnt = totmatches;
1765 next = 0;
1766 break;
1767 case Get:
1768 if (next >= cnt)
1769 return NULL;
1770
1771 p = mp[next];
1772 next++;
1773 break;
1774 case Free:
1775 if (mp != NULL)
1776 {
1777 if (cnt > 0)
1778 while (cnt--)
1779 {
1780 vim_free(mp[cnt]);
1781 if (cp != NULL)
1782 vim_free(cp[cnt]);
1783 }
1784 vim_free(mp);
1785 vim_free(cp);
1786 }
1787 mp = NULL;
1788 cp = NULL;
1789 cnt = 0;
1790 next = 0;
1791 break;
1792 case Print:
1793 cs_print_tags_priv(mp, cp, cnt);
1794 break;
1795 default: /* should not reach here */
1796 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1797 return NULL;
1798 }
1799
1800 return p;
1801} /* cs_manage_matches */
1802
1803
1804/*
1805 * PRIVATE: cs_parse_results
1806 *
1807 * parse cscope output
1808 */
1809 static char *
1810cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1811 int cnumber;
1812 char *buf;
1813 int bufsize;
1814 char **context;
1815 char **linenumber;
1816 char **search;
1817{
1818 int ch;
1819 char *p;
1820 char *name;
1821
1822 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1823 {
1824 if (feof(csinfo[cnumber].fr_fp))
1825 errno = EIO;
1826
1827 cs_reading_emsg(cnumber);
1828
1829 return NULL;
1830 }
1831
1832 /* If the line's too long for the buffer, discard it. */
1833 if ((p = strchr(buf, '\n')) == NULL)
1834 {
1835 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1836 ;
1837 return NULL;
1838 }
1839 *p = '\0';
1840
1841 /*
1842 * cscope output is in the following format:
1843 *
1844 * <filename> <context> <line number> <pattern>
1845 */
1846 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1847 return NULL;
1848 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1849 return NULL;
1850 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1851 return NULL;
1852 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1853
1854 /* --- nvi ---
1855 * If the file is older than the cscope database, that is,
1856 * the database was built since the file was last modified,
1857 * or there wasn't a search string, use the line number.
1858 */
1859 if (strcmp(*search, "<unknown>") == 0)
1860 *search = NULL;
1861
1862 name = cs_resolve_file(cnumber, name);
1863 return name;
1864}
1865
Bram Moolenaarc716c302006-01-21 22:12:51 +00001866#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867/*
1868 * PRIVATE: cs_file_results
1869 *
1870 * write cscope find results to file
1871 */
1872 static void
1873cs_file_results(f, nummatches_a)
1874 FILE *f;
1875 int *nummatches_a;
1876{
1877 int i, j;
1878 char *buf;
1879 char *search, *slno;
1880 char *fullname;
1881 char *cntx;
1882 char *context;
1883
1884 buf = (char *)alloc(CSREAD_BUFSIZE);
1885 if (buf == NULL)
1886 return;
1887
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001888 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 if (nummatches_a[i] < 1)
1891 continue;
1892
1893 for (j = 0; j < nummatches_a[i]; j++)
1894 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001895 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1896 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 continue;
1898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001899 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001900 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 continue;
1902
1903 if (strcmp(cntx, "<global>")==0)
1904 strcpy(context, "<<global>>");
1905 else
1906 sprintf(context, "<<%s>>", cntx);
1907
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001908 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1910 else
1911 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1912
1913 vim_free(context);
1914 vim_free(fullname);
1915 } /* for all matches */
1916
1917 (void)cs_read_prompt(i);
1918
1919 } /* for all cscope connections */
1920 vim_free(buf);
1921}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924/*
1925 * PRIVATE: cs_fill_results
1926 *
1927 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1928 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001929 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 static void
1932cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1933 char *tagstr;
1934 int totmatches;
1935 int *nummatches_a;
1936 char ***matches_p;
1937 char ***cntxts_p;
1938 int *matched;
1939{
1940 int i, j;
1941 char *buf;
1942 char *search, *slno;
1943 int totsofar = 0;
1944 char **matches = NULL;
1945 char **cntxts = NULL;
1946 char *fullname;
1947 char *cntx;
1948
1949 assert(totmatches > 0);
1950
1951 buf = (char *)alloc(CSREAD_BUFSIZE);
1952 if (buf == NULL)
1953 return;
1954
1955 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1956 goto parse_out;
1957 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1958 goto parse_out;
1959
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001960 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 if (nummatches_a[i] < 1)
1963 continue;
1964
1965 for (j = 0; j < nummatches_a[i]; j++)
1966 {
1967 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1968 &slno, &search)) == NULL)
1969 continue;
1970
1971 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1972 search, tagstr);
1973
1974 vim_free(fullname);
1975
1976 if (strcmp(cntx, "<global>") == 0)
1977 cntxts[totsofar] = NULL;
1978 else
1979 /* note: if vim_strsave returns NULL, then the context
1980 * will be "<global>", which is misleading.
1981 */
1982 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1983
1984 if (matches[totsofar] != NULL)
1985 totsofar++;
1986
1987 } /* for all matches */
1988
1989 (void)cs_read_prompt(i);
1990
1991 } /* for all cscope connections */
1992
1993parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001994 if (totsofar == 0)
1995 {
1996 /* No matches, free the arrays and return NULL in "*matches_p". */
1997 vim_free(matches);
1998 matches = NULL;
1999 vim_free(cntxts);
2000 cntxts = NULL;
2001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 *matched = totsofar;
2003 *matches_p = matches;
2004 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 vim_free(buf);
2007} /* cs_fill_results */
2008
2009
2010/* get the requested path components */
2011 static char *
2012cs_pathcomponents(path)
2013 char *path;
2014{
2015 int i;
2016 char *s;
2017
2018 if (p_cspc == 0)
2019 return path;
2020
2021 s = path + strlen(path) - 1;
2022 for (i = 0; i < p_cspc; ++i)
2023 while (s > path && *--s != '/'
2024#ifdef WIN32
2025 && *--s != '\\'
2026#endif
2027 )
2028 ;
2029 if ((s > path && *s == '/')
2030#ifdef WIN32
2031 || (s > path && *s == '\\')
2032#endif
2033 )
2034 ++s;
2035 return s;
2036}
2037
2038/*
2039 * PRIVATE: cs_print_tags_priv
2040 *
2041 * called from cs_manage_matches()
2042 */
2043 static void
2044cs_print_tags_priv(matches, cntxts, num_matches)
2045 char **matches;
2046 char **cntxts;
2047 int num_matches;
2048{
2049 char *buf = NULL;
2050 int bufsize = 0; /* Track available bufsize */
2051 int newsize = 0;
2052 char *ptag;
2053 char *fname, *lno, *extra, *tbuf;
2054 int i, idx, num;
2055 char *globalcntx = "GLOBAL";
2056 char *cntxformat = " <<%s>>";
2057 char *context;
2058 char *cstag_msg = _("Cscope tag: %s");
2059 char *csfmt_str = "%4d %6s ";
2060
2061 assert (num_matches > 0);
2062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002063 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return;
2065
2066 strcpy(tbuf, matches[0]);
2067 ptag = strtok(tbuf, "\t");
2068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002069 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 buf = (char *)alloc(newsize);
2071 if (buf != NULL)
2072 {
2073 bufsize = newsize;
2074 (void)sprintf(buf, cstag_msg, ptag);
2075 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2076 }
2077
2078 vim_free(tbuf);
2079
2080 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2081 msg_advance(msg_col + 2);
2082 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2083
2084 num = 1;
2085 for (i = 0; i < num_matches; i++)
2086 {
2087 idx = i;
2088
2089 /* if we really wanted to, we could avoid this malloc and strcpy
2090 * by parsing matches[i] on the fly and placing stuff into buf
2091 * directly, but that's too much of a hassle
2092 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 continue;
2095 (void)strcpy(tbuf, matches[idx]);
2096
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002097 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 continue;
2099 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2100 continue;
2101 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002102 continue;
2103 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2106
2107 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002108 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (bufsize < newsize)
2110 {
2111 buf = (char *)vim_realloc(buf, newsize);
2112 if (buf == NULL)
2113 bufsize = 0;
2114 else
2115 bufsize = newsize;
2116 }
2117 if (buf != NULL)
2118 {
2119 /* csfmt_str = "%4d %6s "; */
2120 (void)sprintf(buf, csfmt_str, num, lno);
2121 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2122 }
2123 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2124
2125 /* compute the required space for the context */
2126 if (cntxts[idx] != NULL)
2127 context = cntxts[idx];
2128 else
2129 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002130 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 if (bufsize < newsize)
2133 {
2134 buf = (char *)vim_realloc(buf, newsize);
2135 if (buf == NULL)
2136 bufsize = 0;
2137 else
2138 bufsize = newsize;
2139 }
2140 if (buf != NULL)
2141 {
2142 (void)sprintf(buf, cntxformat, context);
2143
2144 /* print the context only if it fits on the same line */
2145 if (msg_col + (int)strlen(buf) >= (int)Columns)
2146 msg_putchar('\n');
2147 msg_advance(12);
2148 MSG_PUTS_LONG(buf);
2149 msg_putchar('\n');
2150 }
2151 if (extra != NULL)
2152 {
2153 msg_advance(13);
2154 MSG_PUTS_LONG(extra);
2155 }
2156
2157 vim_free(tbuf); /* only after printing extra due to strtok use */
2158
2159 if (msg_col)
2160 msg_putchar('\n');
2161
2162 ui_breakcheck();
2163 if (got_int)
2164 {
2165 got_int = FALSE; /* don't print any more matches */
2166 break;
2167 }
2168
2169 num++;
2170 } /* for all matches */
2171
2172 vim_free(buf);
2173} /* cs_print_tags_priv */
2174
2175
2176/*
2177 * PRIVATE: cs_read_prompt
2178 *
2179 * read a cscope prompt (basically, skip over the ">> ")
2180 */
2181 static int
2182cs_read_prompt(i)
2183 int i;
2184{
2185 int ch;
2186 char *buf = NULL; /* buffer for possible error message from cscope */
2187 int bufpos = 0;
2188 char *cs_emsg;
2189 int maxlen;
2190 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002191 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 int n;
2193
2194 cs_emsg = _("E609: Cscope error: %s");
2195 /* compute maximum allowed len for Cscope error message */
2196 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2197
2198 for (;;)
2199 {
2200 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2201 /* if there is room and char is printable */
2202 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2203 {
2204 if (buf == NULL) /* lazy buffer allocation */
2205 buf = (char *)alloc(maxlen);
2206 if (buf != NULL)
2207 {
2208 /* append character to the message */
2209 buf[bufpos++] = ch;
2210 buf[bufpos] = NUL;
2211 if (bufpos >= epromptlen
2212 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2213 {
2214 /* remove eprompt from buf */
2215 buf[bufpos - epromptlen] = NUL;
2216
2217 /* print message to user */
2218 (void)EMSG2(cs_emsg, buf);
2219
2220 /* send RETURN to cscope */
2221 (void)putc('\n', csinfo[i].to_fp);
2222 (void)fflush(csinfo[i].to_fp);
2223
2224 /* clear buf */
2225 bufpos = 0;
2226 buf[bufpos] = NUL;
2227 }
2228 }
2229 }
2230
2231 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2232 {
2233 if (n > 0)
2234 ch = getc(csinfo[i].fr_fp);
2235 if (ch == EOF)
2236 {
2237 PERROR("cs_read_prompt EOF");
2238 if (buf != NULL && buf[0] != NUL)
2239 (void)EMSG2(cs_emsg, buf);
2240 else if (p_csverbose)
2241 cs_reading_emsg(i); /* don't have additional information */
2242 cs_release_csp(i, TRUE);
2243 vim_free(buf);
2244 return CSCOPE_FAILURE;
2245 }
2246
2247 if (ch != CSCOPE_PROMPT[n])
2248 {
2249 ch = EOF;
2250 break;
2251 }
2252 }
2253
2254 if (ch == EOF)
2255 continue; /* didn't find the prompt */
2256 break; /* did find the prompt */
2257 }
2258
2259 vim_free(buf);
2260 return CSCOPE_SUCCESS;
2261}
2262
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002263#if defined(UNIX) && defined(SIGALRM)
2264/*
2265 * Used to catch and ignore SIGALRM below.
2266 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002267 static RETSIGTYPE
2268sig_handler SIGDEFARG(sigarg)
2269{
2270 /* do nothing */
2271 SIGRETURN;
2272}
2273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275/*
2276 * PRIVATE: cs_release_csp
2277 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002278 * Does the actual free'ing for the cs ptr with an optional flag of whether
2279 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
2281 static void
2282cs_release_csp(i, freefnpp)
2283 int i;
2284 int freefnpp;
2285{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 /*
2287 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2288 */
2289 if (csinfo[i].to_fp != NULL)
2290 {
2291 (void)fputs("q\n", csinfo[i].to_fp);
2292 (void)fflush(csinfo[i].to_fp);
2293 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002294#if defined(UNIX)
2295 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002296 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002297 int pstat;
2298 pid_t pid;
2299
2300# if defined(HAVE_SIGACTION)
2301 struct sigaction sa, old;
2302
Bram Moolenaar9701da02008-03-16 12:09:58 +00002303 /* Use sigaction() to limit the waiting time to two seconds. */
2304 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002305 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002306# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002307 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002308# else
2309 sa.sa_flags = 0;
2310# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002311 sigaction(SIGALRM, &sa, &old);
2312 alarm(2); /* 2 sec timeout */
2313
2314 /* Block until cscope exits or until timer expires */
2315 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002316 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002317
2318 /* cancel pending alarm if still there and restore signal */
2319 alarm(0);
2320 sigaction(SIGALRM, &old, NULL);
2321# else
2322 int waited;
2323
2324 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2325 * to give cscope a chance to exit quickly. */
2326 sleep(0);
2327 for (waited = 0; waited < 40; ++waited)
2328 {
2329 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002330 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002331 if (pid != 0)
2332 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002333 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002334 }
2335# endif
2336 /*
2337 * If the cscope process is still running: kill it.
2338 * Safety check: If the PID would be zero here, the entire X session
2339 * would be killed. -1 and 1 are dangerous as well.
2340 */
2341 if (pid < 0 && csinfo[i].pid > 1)
2342 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002343# ifdef ECHILD
2344 int alive = TRUE;
2345
2346 if (waitpid_errno == ECHILD)
2347 {
2348 /*
2349 * When using 'vim -g', vim is forked and cscope process is
2350 * no longer a child process but a sibling. So waitpid()
2351 * fails with errno being ECHILD (No child processes).
2352 * Don't send SIGKILL to cscope immediately but wait
2353 * (polling) for it to exit normally as result of sending
2354 * the "q" command, hence giving it a chance to clean up
2355 * its temporary files.
2356 */
2357 int waited;
2358
2359 sleep(0);
2360 for (waited = 0; waited < 40; ++waited)
2361 {
2362 /* Check whether cscope process is still alive */
2363 if (kill(csinfo[i].pid, 0) != 0)
2364 {
2365 alive = FALSE; /* cscope process no longer exists */
2366 break;
2367 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002368 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002369 }
2370 }
2371 if (alive)
2372# endif
2373 {
2374 kill(csinfo[i].pid, SIGKILL);
2375 (void)waitpid(csinfo[i].pid, &pstat, 0);
2376 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002377 }
2378 }
2379#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002380 if (csinfo[i].hProc != NULL)
2381 {
2382 /* Give cscope a chance to exit normally */
2383 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2384 TerminateProcess(csinfo[i].hProc, 0);
2385 CloseHandle(csinfo[i].hProc);
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
2388
2389 if (csinfo[i].fr_fp != NULL)
2390 (void)fclose(csinfo[i].fr_fp);
2391 if (csinfo[i].to_fp != NULL)
2392 (void)fclose(csinfo[i].to_fp);
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (freefnpp)
2395 {
2396 vim_free(csinfo[i].fname);
2397 vim_free(csinfo[i].ppath);
2398 vim_free(csinfo[i].flags);
2399 }
2400
2401 clear_csinfo(i);
2402} /* cs_release_csp */
2403
2404
2405/*
2406 * PRIVATE: cs_reset
2407 *
2408 * calls cs_kill on all cscope connections then reinits
2409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 static int
2411cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002412 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2415 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002416 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002418 if (csinfo_size == 0)
2419 return CSCOPE_SUCCESS;
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002422 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2423 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2424 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (dblist == NULL || pplist == NULL || fllist == NULL)
2426 {
2427 vim_free(dblist);
2428 vim_free(pplist);
2429 vim_free(fllist);
2430 return CSCOPE_FAILURE;
2431 }
2432
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002433 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 dblist[i] = csinfo[i].fname;
2436 pplist[i] = csinfo[i].ppath;
2437 fllist[i] = csinfo[i].flags;
2438 if (csinfo[i].fname != NULL)
2439 cs_release_csp(i, FALSE);
2440 }
2441
2442 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002443 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 if (dblist[i] != NULL)
2446 {
2447 cs_add_common(dblist[i], pplist[i], fllist[i]);
2448 if (p_csverbose)
2449 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002450 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 * connection number in the same line as
2452 * "Added cscope database..."
2453 */
2454 sprintf(buf, " (#%d)", i);
2455 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2456 }
2457 }
2458 vim_free(dblist[i]);
2459 vim_free(pplist[i]);
2460 vim_free(fllist[i]);
2461 }
2462 vim_free(dblist);
2463 vim_free(pplist);
2464 vim_free(fllist);
2465
2466 if (p_csverbose)
2467 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2468 return CSCOPE_SUCCESS;
2469} /* cs_reset */
2470
2471
2472/*
2473 * PRIVATE: cs_resolve_file
2474 *
2475 * construct the full pathname to a file found in the cscope database.
2476 * (Prepends ppath, if there is one and if it's not already prepended,
2477 * otherwise just uses the name found.)
2478 *
2479 * we need to prepend the prefix because on some cscope's (e.g., the one that
2480 * ships with Solaris 2.6), the output never has the prefix prepended.
2481 * contrast this with my development system (Digital Unix), which does.
2482 */
2483 static char *
2484cs_resolve_file(i, name)
2485 int i;
2486 char *name;
2487{
2488 char *fullname;
2489 int len;
2490
2491 /*
2492 * ppath is freed when we destroy the cscope connection.
2493 * fullname is freed after cs_make_vim_style_matches, after it's been
2494 * copied into the tag buffer used by vim
2495 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002496 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002498 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 if ((fullname = (char *)alloc(len)) == NULL)
2501 return NULL;
2502
2503 /*
2504 * note/example: this won't work if the cscope output already starts
2505 * "../.." and the prefix path is also "../..". if something like this
2506 * happens, you are screwed up and need to fix how you're using cscope.
2507 */
2508 if (csinfo[i].ppath != NULL &&
2509 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2510 (name[0] != '/')
2511#ifdef WIN32
2512 && name[0] != '\\' && name[1] != ':'
2513#endif
2514 )
2515 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2516 else
2517 (void)sprintf(fullname, "%s", name);
2518
2519 return fullname;
2520} /* cs_resolve_file */
2521
2522
2523/*
2524 * PRIVATE: cs_show
2525 *
2526 * show all cscope connections
2527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 static int
2529cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002530 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 short i;
2533 if (cs_cnt_connections() == 0)
2534 MSG_PUTS(_("no cscope connections\n"));
2535 else
2536 {
2537 MSG_PUTS_ATTR(
2538 _(" # pid database name prepend path\n"),
2539 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002540 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 if (csinfo[i].fname == NULL)
2543 continue;
2544
2545 if (csinfo[i].ppath != NULL)
2546 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2547 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2548 else
2549 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2550 i, (long)csinfo[i].pid, csinfo[i].fname);
2551 }
2552 }
2553
2554 wait_return(TRUE);
2555 return CSCOPE_SUCCESS;
2556} /* cs_show */
2557
Bram Moolenaar02b06312007-09-06 15:39:22 +00002558
2559/*
2560 * PUBLIC: cs_end
2561 *
2562 * Only called when VIM exits to quit any cscope sessions.
2563 */
2564 void
2565cs_end()
2566{
2567 int i;
2568
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002569 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002570 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002571 vim_free(csinfo);
2572 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002573}
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#endif /* FEAT_CSCOPE */
2576
2577/* the end */