blob: b8fef2887ed9ca40288ceb210c857dadbdbc8de4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
6 * The basic idea/structure of cscope for Vim was borrowed from Nvi. There
7 * might be a few lines of code that look similar to what Nvi has.
8 *
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
14#if defined(FEAT_CSCOPE) || defined(PROTO)
15
16#include <string.h>
17#include <errno.h>
18#include <assert.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#if defined(UNIX)
22# include <sys/wait.h>
23#else
24 /* not UNIX, must be WIN32 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000025# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
27#include "if_cscope.h"
28
29static void cs_usage_msg __ARGS((csid_e x));
30static int cs_add __ARGS((exarg_T *eap));
31static void cs_stat_emsg __ARGS((char *fname));
32static int cs_add_common __ARGS((char *, char *, char *));
33static int cs_check_for_connections __ARGS((void));
34static int cs_check_for_tags __ARGS((void));
35static int cs_cnt_connections __ARGS((void));
36static void cs_reading_emsg __ARGS((int idx));
37static int cs_cnt_matches __ARGS((int idx));
38static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
39static int cs_create_connection __ARGS((int i));
40static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000041#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static void cs_fill_results __ARGS((char *, int , int *, char ***,
45 char ***, int *));
46static int cs_find __ARGS((exarg_T *eap));
Bram Moolenaarc7453f52006-02-10 23:20:28 +000047static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int cs_help __ARGS((exarg_T *eap));
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,
297 FALSE);
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 Moolenaarc7453f52006-02-10 23:20:28 +0000325 FALSE, FALSE);
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,
334 FALSE);
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';
591 if (strlen(fname) == 0)
592 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;
1071
1072 if (cs_check_for_connections() == FALSE)
1073 {
1074 (void)EMSG(_("E567: no cscope connections"));
1075 return FALSE;
1076 }
1077
1078 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1079 {
1080 cs_usage_msg(Find);
1081 return FALSE;
1082 }
1083
1084 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001085 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 cs_usage_msg(Find);
1088 return FALSE;
1089 }
1090
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001091 return cs_find_common(opt, pat, eap->forceit, TRUE,
1092 eap->cmdidx == CMD_lcscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093} /* cs_find */
1094
1095
1096/*
1097 * PRIVATE: cs_find_common
1098 *
1099 * common code for cscope find, shared by cs_find() and do_cstag()
1100 */
1101 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001102cs_find_common(opt, pat, forceit, verbose, use_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 char *opt;
1104 char *pat;
1105 int forceit;
1106 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001107 int use_ll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 int i;
1110 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001111 int *nummatches;
1112 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_QUICKFIX
1114 char cmdletter;
1115 char *qfpos;
1116#endif
1117
1118 /* create the actual command to send to cscope */
1119 cmd = cs_create_cmd(opt, pat);
1120 if (cmd == NULL)
1121 return FALSE;
1122
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001123 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1124 if (nummatches == NULL)
1125 return FALSE;
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 /* send query to all open connections, then count the total number
1128 * of matches so we can alloc matchesp all in one swell foop
1129 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001130 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 nummatches[i] = 0;
1132 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001133 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001135 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 continue;
1137
1138 /* send cmd to cscope */
1139 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1140 (void)fflush(csinfo[i].to_fp);
1141
1142 nummatches[i] = cs_cnt_matches(i);
1143
1144 if (nummatches[i] > -1)
1145 totmatches += nummatches[i];
1146
1147 if (nummatches[i] == 0)
1148 (void)cs_read_prompt(i);
1149 }
1150 vim_free(cmd);
1151
1152 if (totmatches == 0)
1153 {
1154 char *nf = _("E259: no matches found for cscope query %s of %s");
1155 char *buf;
1156
1157 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001158 {
1159 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001163 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (buf == NULL)
1165 (void)EMSG(nf);
1166 else
1167 {
1168 sprintf(buf, nf, opt, pat);
1169 (void)EMSG(buf);
1170 vim_free(buf);
1171 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001172 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 return FALSE;
1174 }
1175
1176#ifdef FEAT_QUICKFIX
1177 /* get cmd letter */
1178 switch (opt[0])
1179 {
1180 case '0' :
1181 cmdletter = 's';
1182 break;
1183 case '1' :
1184 cmdletter = 'g';
1185 break;
1186 case '2' :
1187 cmdletter = 'd';
1188 break;
1189 case '3' :
1190 cmdletter = 'c';
1191 break;
1192 case '4' :
1193 cmdletter = 't';
1194 break;
1195 case '6' :
1196 cmdletter = 'e';
1197 break;
1198 case '7' :
1199 cmdletter = 'f';
1200 break;
1201 case '8' :
1202 cmdletter = 'i';
1203 break;
1204 default :
1205 cmdletter = opt[0];
1206 }
1207
1208 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1209 if (qfpos != NULL)
1210 {
1211 qfpos++;
1212 /* next symbol must be + or - */
1213 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1214 {
1215 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001216 char *buf = (char *)alloc((unsigned)strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
1218 /* strlen will be enough because we use chars */
1219 if (buf != NULL)
1220 {
1221 sprintf(buf, nf, *qfpos, *(qfpos-1));
1222 (void)EMSG(buf);
1223 vim_free(buf);
1224 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001225 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 return FALSE;
1227 }
1228 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001229 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
1231 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001232 FILE *f;
1233 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001234 qf_info_T *qi = NULL;
1235 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001237 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 if (f == NULL)
1239 EMSG2(_(e_notopen), tmp);
1240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001242 cs_file_results(f, nummatches);
1243 fclose(f);
1244 if (use_ll) /* Use location list */
1245 wp = curwin;
1246 /* '-' starts a new error list */
1247 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1248 *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001250# ifdef FEAT_WINDOWS
1251 if (postponed_split != 0)
1252 {
1253 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 postponed_split_flags);
1255# ifdef FEAT_SCROLLBIND
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001256 curwin->w_p_scb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001258 postponed_split = 0;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001261 if (use_ll)
1262 /*
1263 * In the location list window, use the displayed location
1264 * list. Otherwise, use the location list for the window.
1265 */
1266 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1267 ? wp->w_llist_ref : wp->w_llist;
1268 qf_jump(qi, 0, 0, forceit);
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 mch_remove(tmp);
1272 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001273 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 return TRUE;
1275 }
1276 else
1277#endif /* FEAT_QUICKFIX */
1278 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001279 char **matches = NULL, **contexts = NULL;
1280 int matched = 0;
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 /* read output */
1283 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1284 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001285 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (matches == NULL)
1287 return FALSE;
1288
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001289 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1292 }
1293
1294} /* cs_find_common */
1295
1296/*
1297 * PRIVATE: cs_help
1298 *
1299 * print help
1300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 static int
1302cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001303 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 cscmd_T *cmdp = cs_cmds;
1306
1307 (void)MSG_PUTS(_("cscope commands:\n"));
1308 while (cmdp->name != NULL)
1309 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001310 char *help = _(cmdp->help);
1311 int space_cnt = 30 - vim_strsize((char_u *)help);
1312
1313 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1314 if (space_cnt < 0)
1315 space_cnt = 0;
1316 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1317 cmdp->name,
1318 help, space_cnt, " ",
1319 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001321 MSG_PUTS(_("\n"
1322 " c: Find functions calling this function\n"
1323 " d: Find functions called by this function\n"
1324 " e: Find this egrep pattern\n"
1325 " f: Find this file\n"
1326 " g: Find this definition\n"
1327 " i: Find files #including this file\n"
1328 " s: Find this C symbol\n"
1329 " t: Find assignments to\n"));
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 cmdp++;
1332 }
1333
1334 wait_return(TRUE);
1335 return 0;
1336} /* cs_help */
1337
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 static void
1340clear_csinfo(i)
1341 int i;
1342{
1343 csinfo[i].fname = NULL;
1344 csinfo[i].ppath = NULL;
1345 csinfo[i].flags = NULL;
1346#if defined(UNIX)
1347 csinfo[i].st_dev = (dev_t)0;
1348 csinfo[i].st_ino = (ino_t)0;
1349#else
1350 csinfo[i].nVolume = 0;
1351 csinfo[i].nIndexHigh = 0;
1352 csinfo[i].nIndexLow = 0;
1353#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001354 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 csinfo[i].fr_fp = NULL;
1356 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001357#if defined(WIN32)
1358 csinfo[i].hProc = NULL;
1359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360}
1361
1362#ifndef UNIX
1363static char *GetWin32Error __ARGS((void));
1364
1365 static char *
1366GetWin32Error()
1367{
1368 char *msg = NULL;
1369 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1370 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1371 if (msg != NULL)
1372 {
1373 /* remove trailing \r\n */
1374 char *pcrlf = strstr(msg, "\r\n");
1375 if (pcrlf != NULL)
1376 *pcrlf = '\0';
1377 }
1378 return msg;
1379}
1380#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382/*
1383 * PRIVATE: cs_insert_filelist
1384 *
1385 * insert a new cscope database filename into the filelist
1386 */
1387 static int
1388cs_insert_filelist(fname, ppath, flags, sb)
1389 char *fname;
1390 char *ppath;
1391 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001392 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 short i, j;
1395#ifndef UNIX
1396 HANDLE hFile;
1397 BY_HANDLE_FILE_INFORMATION bhfi;
1398
1399 vim_memset(&bhfi, 0, sizeof(bhfi));
1400 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1401 if (!mch_windows95())
1402 {
1403 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1404 FILE_ATTRIBUTE_NORMAL, NULL);
1405 if (hFile == INVALID_HANDLE_VALUE)
1406 {
1407 if (p_csverbose)
1408 {
1409 char *cant_msg = _("E625: cannot open cscope database: %s");
1410 char *winmsg = GetWin32Error();
1411
1412 if (winmsg != NULL)
1413 {
1414 (void)EMSG2(cant_msg, winmsg);
1415 LocalFree(winmsg);
1416 }
1417 else
1418 /* subst filename if can't get error text */
1419 (void)EMSG2(cant_msg, fname);
1420 }
1421 return -1;
1422 }
1423 if (!GetFileInformationByHandle(hFile, &bhfi))
1424 {
1425 CloseHandle(hFile);
1426 if (p_csverbose)
1427 (void)EMSG(_("E626: cannot get cscope database information"));
1428 return -1;
1429 }
1430 CloseHandle(hFile);
1431 }
1432#endif
1433
1434 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001435 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 if (csinfo[j].fname != NULL
1438#if defined(UNIX)
1439 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1440#else
1441 /* compare pathnames first */
1442 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001443 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 || (!mch_windows95()
1445 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1446 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1447 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1448#endif
1449 )
1450 {
1451 if (p_csverbose)
1452 (void)EMSG(_("E568: duplicate cscope database not added"));
1453 return -1;
1454 }
1455
1456 if (csinfo[j].fname == NULL && i == -1)
1457 i = j; /* remember first empty entry */
1458 }
1459
1460 if (i == -1)
1461 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001462 i = csinfo_size;
1463 if (csinfo_size == 0)
1464 {
1465 /* First time allocation: allocate only 1 connection. It should
1466 * be enough for most users. If more is needed, csinfo will be
1467 * reallocated. */
1468 csinfo_size = 1;
1469 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1470 }
1471 else
1472 {
1473 /* Reallocate space for more connections. */
1474 csinfo_size *= 2;
1475 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1476 }
1477 if (csinfo == NULL)
1478 return -1;
1479 for (j = csinfo_size/2; j < csinfo_size; j++)
1480 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
1482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001483 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return -1;
1485
1486 (void)strcpy(csinfo[i].fname, (const char *)fname);
1487
1488 if (ppath != NULL)
1489 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001490 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 vim_free(csinfo[i].fname);
1493 csinfo[i].fname = NULL;
1494 return -1;
1495 }
1496 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1497 } else
1498 csinfo[i].ppath = NULL;
1499
1500 if (flags != NULL)
1501 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001502 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 vim_free(csinfo[i].fname);
1505 vim_free(csinfo[i].ppath);
1506 csinfo[i].fname = NULL;
1507 csinfo[i].ppath = NULL;
1508 return -1;
1509 }
1510 (void)strcpy(csinfo[i].flags, (const char *)flags);
1511 } else
1512 csinfo[i].flags = NULL;
1513
1514#if defined(UNIX)
1515 csinfo[i].st_dev = sb->st_dev;
1516 csinfo[i].st_ino = sb->st_ino;
1517
1518#else
1519 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1520 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1521 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1522#endif
1523 return i;
1524} /* cs_insert_filelist */
1525
1526
1527/*
1528 * PRIVATE: cs_lookup_cmd
1529 *
1530 * find cscope command in command table
1531 */
1532 static cscmd_T *
1533cs_lookup_cmd(eap)
1534 exarg_T *eap;
1535{
1536 cscmd_T *cmdp;
1537 char *stok;
1538 size_t len;
1539
1540 if (eap->arg == NULL)
1541 return NULL;
1542
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001543 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001544 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1547 return NULL;
1548
1549 len = strlen(stok);
1550 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1551 {
1552 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1553 return (cmdp);
1554 }
1555 return NULL;
1556} /* cs_lookup_cmd */
1557
1558
1559/*
1560 * PRIVATE: cs_kill
1561 *
1562 * nuke em
1563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 static int
1565cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001566 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 char *stok;
1569 short i;
1570
1571 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1572 {
1573 cs_usage_msg(Kill);
1574 return CSCOPE_FAILURE;
1575 }
1576
1577 /* only single digit positive and negative integers are allowed */
1578 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1579 || (strlen(stok) < 3 && stok[0] == '-'
1580 && VIM_ISDIGIT((int)(stok[1]))))
1581 i = atoi(stok);
1582 else
1583 {
1584 /* It must be part of a name. We will try to find a match
1585 * within all the names in the csinfo data structure
1586 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001587 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1590 break;
1591 }
1592 }
1593
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001594 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 if (p_csverbose)
1597 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1598 }
1599 else
1600 {
1601 if (i == -1)
1602 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001603 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 if (csinfo[i].fname)
1606 cs_kill_execute(i, csinfo[i].fname);
1607 }
1608 }
1609 else
1610 cs_kill_execute(i, stok);
1611 }
1612
1613 return 0;
1614} /* cs_kill */
1615
1616
1617/*
1618 * PRIVATE: cs_kill_execute
1619 *
1620 * Actually kills a specific cscope connection.
1621 */
1622 static void
1623cs_kill_execute(i, cname)
1624 int i; /* cscope table index */
1625 char *cname; /* cscope database name */
1626{
1627 if (p_csverbose)
1628 {
1629 msg_clr_eos();
1630 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1631 (char_u *)_("cscope connection %s closed"), cname);
1632 }
1633 cs_release_csp(i, TRUE);
1634}
1635
1636
1637/*
1638 * PRIVATE: cs_make_vim_style_matches
1639 *
1640 * convert the cscope output into into a ctags style entry (as might be found
1641 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1642 * the type of the tag you are looking for. for example, in Darren Hiebert's
1643 * ctags (the one that comes with vim), #define's use a line number to find the
1644 * tag in a file while function definitions use a regexp search pattern.
1645 *
1646 * i'm going to always use the line number because cscope does something
1647 * quirky (and probably other things i don't know about):
1648 *
1649 * if you have "# define" in your source file, which is
1650 * perfectly legal, cscope thinks you have "#define". this
1651 * will result in a failed regexp search. :(
1652 *
1653 * besides, even if this particular case didn't happen, the search pattern
1654 * would still have to be modified to escape all the special regular expression
1655 * characters to comply with ctags formatting.
1656 */
1657 static char *
1658cs_make_vim_style_matches(fname, slno, search, tagstr)
1659 char *fname;
1660 char *slno;
1661 char *search;
1662 char *tagstr;
1663{
1664 /* vim style is ctags:
1665 *
1666 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1667 *
1668 * but as mentioned above, we'll always use the line number and
1669 * put the search pattern (if one exists) as "extra"
1670 *
1671 * buf is used as part of vim's method of handling tags, and
1672 * (i think) vim frees it when you pop your tags and get replaced
1673 * by new ones on the tag stack.
1674 */
1675 char *buf;
1676 int amt;
1677
1678 if (search != NULL)
1679 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001680 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if ((buf = (char *)alloc(amt)) == NULL)
1682 return NULL;
1683
1684 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1685 }
1686 else
1687 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001688 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if ((buf = (char *)alloc(amt)) == NULL)
1690 return NULL;
1691
1692 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1693 }
1694
1695 return buf;
1696} /* cs_make_vim_style_matches */
1697
1698
1699/*
1700 * PRIVATE: cs_manage_matches
1701 *
1702 * this is kind of hokey, but i don't see an easy way round this..
1703 *
1704 * Store: keep a ptr to the (malloc'd) memory of matches originally
1705 * generated from cs_find(). the matches are originally lines directly
1706 * from cscope output, but transformed to look like something out of a
1707 * ctags. see cs_make_vim_style_matches for more details.
1708 *
1709 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1710 * the next line from the cscope output. it basically keeps track of which
1711 * lines have been "used" and returns the next one.
1712 *
1713 * Free: frees up everything and resets
1714 *
1715 * Print: prints the tags
1716 */
1717 static char *
1718cs_manage_matches(matches, contexts, totmatches, cmd)
1719 char **matches;
1720 char **contexts;
1721 int totmatches;
1722 mcmd_e cmd;
1723{
1724 static char **mp = NULL;
1725 static char **cp = NULL;
1726 static int cnt = -1;
1727 static int next = -1;
1728 char *p = NULL;
1729
1730 switch (cmd)
1731 {
1732 case Store:
1733 assert(matches != NULL);
1734 assert(totmatches > 0);
1735 if (mp != NULL || cp != NULL)
1736 (void)cs_manage_matches(NULL, NULL, -1, Free);
1737 mp = matches;
1738 cp = contexts;
1739 cnt = totmatches;
1740 next = 0;
1741 break;
1742 case Get:
1743 if (next >= cnt)
1744 return NULL;
1745
1746 p = mp[next];
1747 next++;
1748 break;
1749 case Free:
1750 if (mp != NULL)
1751 {
1752 if (cnt > 0)
1753 while (cnt--)
1754 {
1755 vim_free(mp[cnt]);
1756 if (cp != NULL)
1757 vim_free(cp[cnt]);
1758 }
1759 vim_free(mp);
1760 vim_free(cp);
1761 }
1762 mp = NULL;
1763 cp = NULL;
1764 cnt = 0;
1765 next = 0;
1766 break;
1767 case Print:
1768 cs_print_tags_priv(mp, cp, cnt);
1769 break;
1770 default: /* should not reach here */
1771 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1772 return NULL;
1773 }
1774
1775 return p;
1776} /* cs_manage_matches */
1777
1778
1779/*
1780 * PRIVATE: cs_parse_results
1781 *
1782 * parse cscope output
1783 */
1784 static char *
1785cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1786 int cnumber;
1787 char *buf;
1788 int bufsize;
1789 char **context;
1790 char **linenumber;
1791 char **search;
1792{
1793 int ch;
1794 char *p;
1795 char *name;
1796
1797 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1798 {
1799 if (feof(csinfo[cnumber].fr_fp))
1800 errno = EIO;
1801
1802 cs_reading_emsg(cnumber);
1803
1804 return NULL;
1805 }
1806
1807 /* If the line's too long for the buffer, discard it. */
1808 if ((p = strchr(buf, '\n')) == NULL)
1809 {
1810 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1811 ;
1812 return NULL;
1813 }
1814 *p = '\0';
1815
1816 /*
1817 * cscope output is in the following format:
1818 *
1819 * <filename> <context> <line number> <pattern>
1820 */
1821 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1822 return NULL;
1823 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1824 return NULL;
1825 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1826 return NULL;
1827 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1828
1829 /* --- nvi ---
1830 * If the file is older than the cscope database, that is,
1831 * the database was built since the file was last modified,
1832 * or there wasn't a search string, use the line number.
1833 */
1834 if (strcmp(*search, "<unknown>") == 0)
1835 *search = NULL;
1836
1837 name = cs_resolve_file(cnumber, name);
1838 return name;
1839}
1840
Bram Moolenaarc716c302006-01-21 22:12:51 +00001841#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
1843 * PRIVATE: cs_file_results
1844 *
1845 * write cscope find results to file
1846 */
1847 static void
1848cs_file_results(f, nummatches_a)
1849 FILE *f;
1850 int *nummatches_a;
1851{
1852 int i, j;
1853 char *buf;
1854 char *search, *slno;
1855 char *fullname;
1856 char *cntx;
1857 char *context;
1858
1859 buf = (char *)alloc(CSREAD_BUFSIZE);
1860 if (buf == NULL)
1861 return;
1862
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001863 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (nummatches_a[i] < 1)
1866 continue;
1867
1868 for (j = 0; j < nummatches_a[i]; j++)
1869 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001870 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1871 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 continue;
1873
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001874 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001875 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 continue;
1877
1878 if (strcmp(cntx, "<global>")==0)
1879 strcpy(context, "<<global>>");
1880 else
1881 sprintf(context, "<<%s>>", cntx);
1882
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001883 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1885 else
1886 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1887
1888 vim_free(context);
1889 vim_free(fullname);
1890 } /* for all matches */
1891
1892 (void)cs_read_prompt(i);
1893
1894 } /* for all cscope connections */
1895 vim_free(buf);
1896}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
1899/*
1900 * PRIVATE: cs_fill_results
1901 *
1902 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1903 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001904 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 */
1906 static void
1907cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1908 char *tagstr;
1909 int totmatches;
1910 int *nummatches_a;
1911 char ***matches_p;
1912 char ***cntxts_p;
1913 int *matched;
1914{
1915 int i, j;
1916 char *buf;
1917 char *search, *slno;
1918 int totsofar = 0;
1919 char **matches = NULL;
1920 char **cntxts = NULL;
1921 char *fullname;
1922 char *cntx;
1923
1924 assert(totmatches > 0);
1925
1926 buf = (char *)alloc(CSREAD_BUFSIZE);
1927 if (buf == NULL)
1928 return;
1929
1930 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1931 goto parse_out;
1932 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1933 goto parse_out;
1934
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001935 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
1937 if (nummatches_a[i] < 1)
1938 continue;
1939
1940 for (j = 0; j < nummatches_a[i]; j++)
1941 {
1942 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1943 &slno, &search)) == NULL)
1944 continue;
1945
1946 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1947 search, tagstr);
1948
1949 vim_free(fullname);
1950
1951 if (strcmp(cntx, "<global>") == 0)
1952 cntxts[totsofar] = NULL;
1953 else
1954 /* note: if vim_strsave returns NULL, then the context
1955 * will be "<global>", which is misleading.
1956 */
1957 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1958
1959 if (matches[totsofar] != NULL)
1960 totsofar++;
1961
1962 } /* for all matches */
1963
1964 (void)cs_read_prompt(i);
1965
1966 } /* for all cscope connections */
1967
1968parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001969 if (totsofar == 0)
1970 {
1971 /* No matches, free the arrays and return NULL in "*matches_p". */
1972 vim_free(matches);
1973 matches = NULL;
1974 vim_free(cntxts);
1975 cntxts = NULL;
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 *matched = totsofar;
1978 *matches_p = matches;
1979 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 vim_free(buf);
1982} /* cs_fill_results */
1983
1984
1985/* get the requested path components */
1986 static char *
1987cs_pathcomponents(path)
1988 char *path;
1989{
1990 int i;
1991 char *s;
1992
1993 if (p_cspc == 0)
1994 return path;
1995
1996 s = path + strlen(path) - 1;
1997 for (i = 0; i < p_cspc; ++i)
1998 while (s > path && *--s != '/'
1999#ifdef WIN32
2000 && *--s != '\\'
2001#endif
2002 )
2003 ;
2004 if ((s > path && *s == '/')
2005#ifdef WIN32
2006 || (s > path && *s == '\\')
2007#endif
2008 )
2009 ++s;
2010 return s;
2011}
2012
2013/*
2014 * PRIVATE: cs_print_tags_priv
2015 *
2016 * called from cs_manage_matches()
2017 */
2018 static void
2019cs_print_tags_priv(matches, cntxts, num_matches)
2020 char **matches;
2021 char **cntxts;
2022 int num_matches;
2023{
2024 char *buf = NULL;
2025 int bufsize = 0; /* Track available bufsize */
2026 int newsize = 0;
2027 char *ptag;
2028 char *fname, *lno, *extra, *tbuf;
2029 int i, idx, num;
2030 char *globalcntx = "GLOBAL";
2031 char *cntxformat = " <<%s>>";
2032 char *context;
2033 char *cstag_msg = _("Cscope tag: %s");
2034 char *csfmt_str = "%4d %6s ";
2035
2036 assert (num_matches > 0);
2037
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002038 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 return;
2040
2041 strcpy(tbuf, matches[0]);
2042 ptag = strtok(tbuf, "\t");
2043
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002044 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 buf = (char *)alloc(newsize);
2046 if (buf != NULL)
2047 {
2048 bufsize = newsize;
2049 (void)sprintf(buf, cstag_msg, ptag);
2050 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2051 }
2052
2053 vim_free(tbuf);
2054
2055 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2056 msg_advance(msg_col + 2);
2057 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2058
2059 num = 1;
2060 for (i = 0; i < num_matches; i++)
2061 {
2062 idx = i;
2063
2064 /* if we really wanted to, we could avoid this malloc and strcpy
2065 * by parsing matches[i] on the fly and placing stuff into buf
2066 * directly, but that's too much of a hassle
2067 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 continue;
2070 (void)strcpy(tbuf, matches[idx]);
2071
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002072 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 continue;
2074 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2075 continue;
2076 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002077 continue;
2078 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2081
2082 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (bufsize < newsize)
2085 {
2086 buf = (char *)vim_realloc(buf, newsize);
2087 if (buf == NULL)
2088 bufsize = 0;
2089 else
2090 bufsize = newsize;
2091 }
2092 if (buf != NULL)
2093 {
2094 /* csfmt_str = "%4d %6s "; */
2095 (void)sprintf(buf, csfmt_str, num, lno);
2096 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2097 }
2098 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2099
2100 /* compute the required space for the context */
2101 if (cntxts[idx] != NULL)
2102 context = cntxts[idx];
2103 else
2104 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002105 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107 if (bufsize < newsize)
2108 {
2109 buf = (char *)vim_realloc(buf, newsize);
2110 if (buf == NULL)
2111 bufsize = 0;
2112 else
2113 bufsize = newsize;
2114 }
2115 if (buf != NULL)
2116 {
2117 (void)sprintf(buf, cntxformat, context);
2118
2119 /* print the context only if it fits on the same line */
2120 if (msg_col + (int)strlen(buf) >= (int)Columns)
2121 msg_putchar('\n');
2122 msg_advance(12);
2123 MSG_PUTS_LONG(buf);
2124 msg_putchar('\n');
2125 }
2126 if (extra != NULL)
2127 {
2128 msg_advance(13);
2129 MSG_PUTS_LONG(extra);
2130 }
2131
2132 vim_free(tbuf); /* only after printing extra due to strtok use */
2133
2134 if (msg_col)
2135 msg_putchar('\n');
2136
2137 ui_breakcheck();
2138 if (got_int)
2139 {
2140 got_int = FALSE; /* don't print any more matches */
2141 break;
2142 }
2143
2144 num++;
2145 } /* for all matches */
2146
2147 vim_free(buf);
2148} /* cs_print_tags_priv */
2149
2150
2151/*
2152 * PRIVATE: cs_read_prompt
2153 *
2154 * read a cscope prompt (basically, skip over the ">> ")
2155 */
2156 static int
2157cs_read_prompt(i)
2158 int i;
2159{
2160 int ch;
2161 char *buf = NULL; /* buffer for possible error message from cscope */
2162 int bufpos = 0;
2163 char *cs_emsg;
2164 int maxlen;
2165 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002166 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 int n;
2168
2169 cs_emsg = _("E609: Cscope error: %s");
2170 /* compute maximum allowed len for Cscope error message */
2171 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2172
2173 for (;;)
2174 {
2175 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2176 /* if there is room and char is printable */
2177 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2178 {
2179 if (buf == NULL) /* lazy buffer allocation */
2180 buf = (char *)alloc(maxlen);
2181 if (buf != NULL)
2182 {
2183 /* append character to the message */
2184 buf[bufpos++] = ch;
2185 buf[bufpos] = NUL;
2186 if (bufpos >= epromptlen
2187 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2188 {
2189 /* remove eprompt from buf */
2190 buf[bufpos - epromptlen] = NUL;
2191
2192 /* print message to user */
2193 (void)EMSG2(cs_emsg, buf);
2194
2195 /* send RETURN to cscope */
2196 (void)putc('\n', csinfo[i].to_fp);
2197 (void)fflush(csinfo[i].to_fp);
2198
2199 /* clear buf */
2200 bufpos = 0;
2201 buf[bufpos] = NUL;
2202 }
2203 }
2204 }
2205
2206 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2207 {
2208 if (n > 0)
2209 ch = getc(csinfo[i].fr_fp);
2210 if (ch == EOF)
2211 {
2212 PERROR("cs_read_prompt EOF");
2213 if (buf != NULL && buf[0] != NUL)
2214 (void)EMSG2(cs_emsg, buf);
2215 else if (p_csverbose)
2216 cs_reading_emsg(i); /* don't have additional information */
2217 cs_release_csp(i, TRUE);
2218 vim_free(buf);
2219 return CSCOPE_FAILURE;
2220 }
2221
2222 if (ch != CSCOPE_PROMPT[n])
2223 {
2224 ch = EOF;
2225 break;
2226 }
2227 }
2228
2229 if (ch == EOF)
2230 continue; /* didn't find the prompt */
2231 break; /* did find the prompt */
2232 }
2233
2234 vim_free(buf);
2235 return CSCOPE_SUCCESS;
2236}
2237
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002238#if defined(UNIX) && defined(SIGALRM)
2239/*
2240 * Used to catch and ignore SIGALRM below.
2241 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002242 static RETSIGTYPE
2243sig_handler SIGDEFARG(sigarg)
2244{
2245 /* do nothing */
2246 SIGRETURN;
2247}
2248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250/*
2251 * PRIVATE: cs_release_csp
2252 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002253 * Does the actual free'ing for the cs ptr with an optional flag of whether
2254 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
2256 static void
2257cs_release_csp(i, freefnpp)
2258 int i;
2259 int freefnpp;
2260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /*
2262 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2263 */
2264 if (csinfo[i].to_fp != NULL)
2265 {
2266 (void)fputs("q\n", csinfo[i].to_fp);
2267 (void)fflush(csinfo[i].to_fp);
2268 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002269#if defined(UNIX)
2270 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002271 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002272 int pstat;
2273 pid_t pid;
2274
2275# if defined(HAVE_SIGACTION)
2276 struct sigaction sa, old;
2277
Bram Moolenaar9701da02008-03-16 12:09:58 +00002278 /* Use sigaction() to limit the waiting time to two seconds. */
2279 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002280 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002281# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002282 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002283# else
2284 sa.sa_flags = 0;
2285# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002286 sigaction(SIGALRM, &sa, &old);
2287 alarm(2); /* 2 sec timeout */
2288
2289 /* Block until cscope exits or until timer expires */
2290 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002291 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002292
2293 /* cancel pending alarm if still there and restore signal */
2294 alarm(0);
2295 sigaction(SIGALRM, &old, NULL);
2296# else
2297 int waited;
2298
2299 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2300 * to give cscope a chance to exit quickly. */
2301 sleep(0);
2302 for (waited = 0; waited < 40; ++waited)
2303 {
2304 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002305 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002306 if (pid != 0)
2307 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002308 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002309 }
2310# endif
2311 /*
2312 * If the cscope process is still running: kill it.
2313 * Safety check: If the PID would be zero here, the entire X session
2314 * would be killed. -1 and 1 are dangerous as well.
2315 */
2316 if (pid < 0 && csinfo[i].pid > 1)
2317 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002318# ifdef ECHILD
2319 int alive = TRUE;
2320
2321 if (waitpid_errno == ECHILD)
2322 {
2323 /*
2324 * When using 'vim -g', vim is forked and cscope process is
2325 * no longer a child process but a sibling. So waitpid()
2326 * fails with errno being ECHILD (No child processes).
2327 * Don't send SIGKILL to cscope immediately but wait
2328 * (polling) for it to exit normally as result of sending
2329 * the "q" command, hence giving it a chance to clean up
2330 * its temporary files.
2331 */
2332 int waited;
2333
2334 sleep(0);
2335 for (waited = 0; waited < 40; ++waited)
2336 {
2337 /* Check whether cscope process is still alive */
2338 if (kill(csinfo[i].pid, 0) != 0)
2339 {
2340 alive = FALSE; /* cscope process no longer exists */
2341 break;
2342 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002343 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002344 }
2345 }
2346 if (alive)
2347# endif
2348 {
2349 kill(csinfo[i].pid, SIGKILL);
2350 (void)waitpid(csinfo[i].pid, &pstat, 0);
2351 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002352 }
2353 }
2354#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002355 if (csinfo[i].hProc != NULL)
2356 {
2357 /* Give cscope a chance to exit normally */
2358 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2359 TerminateProcess(csinfo[i].hProc, 0);
2360 CloseHandle(csinfo[i].hProc);
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#endif
2363
2364 if (csinfo[i].fr_fp != NULL)
2365 (void)fclose(csinfo[i].fr_fp);
2366 if (csinfo[i].to_fp != NULL)
2367 (void)fclose(csinfo[i].to_fp);
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (freefnpp)
2370 {
2371 vim_free(csinfo[i].fname);
2372 vim_free(csinfo[i].ppath);
2373 vim_free(csinfo[i].flags);
2374 }
2375
2376 clear_csinfo(i);
2377} /* cs_release_csp */
2378
2379
2380/*
2381 * PRIVATE: cs_reset
2382 *
2383 * calls cs_kill on all cscope connections then reinits
2384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 static int
2386cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002387 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2390 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002391 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002393 if (csinfo_size == 0)
2394 return CSCOPE_SUCCESS;
2395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002397 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2398 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2399 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (dblist == NULL || pplist == NULL || fllist == NULL)
2401 {
2402 vim_free(dblist);
2403 vim_free(pplist);
2404 vim_free(fllist);
2405 return CSCOPE_FAILURE;
2406 }
2407
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002408 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 dblist[i] = csinfo[i].fname;
2411 pplist[i] = csinfo[i].ppath;
2412 fllist[i] = csinfo[i].flags;
2413 if (csinfo[i].fname != NULL)
2414 cs_release_csp(i, FALSE);
2415 }
2416
2417 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002418 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 if (dblist[i] != NULL)
2421 {
2422 cs_add_common(dblist[i], pplist[i], fllist[i]);
2423 if (p_csverbose)
2424 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002425 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 * connection number in the same line as
2427 * "Added cscope database..."
2428 */
2429 sprintf(buf, " (#%d)", i);
2430 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2431 }
2432 }
2433 vim_free(dblist[i]);
2434 vim_free(pplist[i]);
2435 vim_free(fllist[i]);
2436 }
2437 vim_free(dblist);
2438 vim_free(pplist);
2439 vim_free(fllist);
2440
2441 if (p_csverbose)
2442 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2443 return CSCOPE_SUCCESS;
2444} /* cs_reset */
2445
2446
2447/*
2448 * PRIVATE: cs_resolve_file
2449 *
2450 * construct the full pathname to a file found in the cscope database.
2451 * (Prepends ppath, if there is one and if it's not already prepended,
2452 * otherwise just uses the name found.)
2453 *
2454 * we need to prepend the prefix because on some cscope's (e.g., the one that
2455 * ships with Solaris 2.6), the output never has the prefix prepended.
2456 * contrast this with my development system (Digital Unix), which does.
2457 */
2458 static char *
2459cs_resolve_file(i, name)
2460 int i;
2461 char *name;
2462{
2463 char *fullname;
2464 int len;
2465
2466 /*
2467 * ppath is freed when we destroy the cscope connection.
2468 * fullname is freed after cs_make_vim_style_matches, after it's been
2469 * copied into the tag buffer used by vim
2470 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002471 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002473 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 if ((fullname = (char *)alloc(len)) == NULL)
2476 return NULL;
2477
2478 /*
2479 * note/example: this won't work if the cscope output already starts
2480 * "../.." and the prefix path is also "../..". if something like this
2481 * happens, you are screwed up and need to fix how you're using cscope.
2482 */
2483 if (csinfo[i].ppath != NULL &&
2484 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2485 (name[0] != '/')
2486#ifdef WIN32
2487 && name[0] != '\\' && name[1] != ':'
2488#endif
2489 )
2490 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2491 else
2492 (void)sprintf(fullname, "%s", name);
2493
2494 return fullname;
2495} /* cs_resolve_file */
2496
2497
2498/*
2499 * PRIVATE: cs_show
2500 *
2501 * show all cscope connections
2502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 static int
2504cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002505 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507 short i;
2508 if (cs_cnt_connections() == 0)
2509 MSG_PUTS(_("no cscope connections\n"));
2510 else
2511 {
2512 MSG_PUTS_ATTR(
2513 _(" # pid database name prepend path\n"),
2514 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002515 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 if (csinfo[i].fname == NULL)
2518 continue;
2519
2520 if (csinfo[i].ppath != NULL)
2521 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2522 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2523 else
2524 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2525 i, (long)csinfo[i].pid, csinfo[i].fname);
2526 }
2527 }
2528
2529 wait_return(TRUE);
2530 return CSCOPE_SUCCESS;
2531} /* cs_show */
2532
Bram Moolenaar02b06312007-09-06 15:39:22 +00002533
2534/*
2535 * PUBLIC: cs_end
2536 *
2537 * Only called when VIM exits to quit any cscope sessions.
2538 */
2539 void
2540cs_end()
2541{
2542 int i;
2543
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002544 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002545 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002546 vim_free(csinfo);
2547 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002548}
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif /* FEAT_CSCOPE */
2551
2552/* the end */