blob: f9e318dde1444fa79aa8c34b0ca5bd3ea0a5520b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
6 * The basic idea/structure of cscope for Vim was borrowed from Nvi. There
7 * might be a few lines of code that look similar to what Nvi has.
8 *
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
14#if defined(FEAT_CSCOPE) || defined(PROTO)
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include <sys/types.h>
17#include <sys/stat.h>
18#if defined(UNIX)
19# include <sys/wait.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000020#endif
21#include "if_cscope.h"
22
23static void cs_usage_msg __ARGS((csid_e x));
24static int cs_add __ARGS((exarg_T *eap));
25static void cs_stat_emsg __ARGS((char *fname));
26static int cs_add_common __ARGS((char *, char *, char *));
27static int cs_check_for_connections __ARGS((void));
28static int cs_check_for_tags __ARGS((void));
29static int cs_cnt_connections __ARGS((void));
30static void cs_reading_emsg __ARGS((int idx));
31static int cs_cnt_matches __ARGS((int idx));
32static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
33static int cs_create_connection __ARGS((int i));
34static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000035#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void cs_fill_results __ARGS((char *, int , int *, char ***,
39 char ***, int *));
40static int cs_find __ARGS((exarg_T *eap));
Bram Moolenaar7fd73202010-07-25 16:58:46 +020041static int cs_find_common __ARGS((char *opt, char *pat, int, int, int, char_u *cmdline));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static int cs_help __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000043static void clear_csinfo __ARGS((int i));
44static int cs_insert_filelist __ARGS((char *, char *, char *,
45 struct stat *));
46static int cs_kill __ARGS((exarg_T *eap));
47static void cs_kill_execute __ARGS((int, char *));
48static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
49static char * cs_make_vim_style_matches __ARGS((char *, char *,
50 char *, char *));
51static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
52static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
53static char * cs_pathcomponents __ARGS((char *path));
54static void cs_print_tags_priv __ARGS((char **, char **, int));
Bram Moolenaar02b06312007-09-06 15:39:22 +000055static int cs_read_prompt __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void cs_release_csp __ARGS((int, int freefnpp));
57static int cs_reset __ARGS((exarg_T *eap));
58static char * cs_resolve_file __ARGS((int, char *));
59static int cs_show __ARGS((exarg_T *eap));
60
61
Bram Moolenaar9fa49da2009-07-10 13:11:26 +000062static csinfo_T * csinfo = NULL;
63static int csinfo_size = 0; /* number of items allocated in
64 csinfo[] */
65
Bram Moolenaard2ac9842007-08-21 16:03:51 +000066static int eap_arg_len; /* length of eap->arg, set in
67 cs_lookup_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static cscmd_T cs_cmds[] =
69{
70 { "add", cs_add,
71 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
72 { "find", cs_find,
Bram Moolenaar627943d2008-08-25 02:35:59 +000073 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 { "help", cs_help,
75 N_("Show this message"), "help", 0 },
76 { "kill", cs_kill,
77 N_("Kill a connection"), "kill #", 0 },
78 { "reset", cs_reset,
79 N_("Reinit all connections"), "reset", 0 },
80 { "show", cs_show,
81 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000082 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000083};
84
85 static void
86cs_usage_msg(x)
87 csid_e x;
88{
89 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
90}
91
Bram Moolenaarf4580d82009-03-18 11:52:53 +000092#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
93
94static enum
95{
96 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +000097 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +000098 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
99 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
100} expand_what;
101
102/*
103 * Function given to ExpandGeneric() to obtain the cscope command
104 * expansion.
105 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000106 char_u *
107get_cscope_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000108 expand_T *xp UNUSED;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000109 int idx;
110{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000111 int current_idx;
112 int i;
113
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000114 switch (expand_what)
115 {
116 case EXP_CSCOPE_SUBCMD:
117 /* Complete with sub-commands of ":cscope":
118 * add, find, help, kill, reset, show */
119 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000120 case EXP_SCSCOPE_SUBCMD:
121 /* Complete with sub-commands of ":scscope": same sub-commands as
122 * ":cscope" but skip commands which don't support split windows */
123 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
124 if (cs_cmds[i].cansplit)
125 if (current_idx++ == idx)
126 break;
127 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000128 case EXP_CSCOPE_FIND:
129 {
130 const char *query_type[] =
131 {
132 "c", "d", "e", "f", "g", "i", "s", "t", NULL
133 };
134
135 /* Complete with query type of ":cscope find {query_type}".
136 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
137 * ..., 8) but only complete with letters, since numbers are
138 * redundant. */
139 return (char_u *)query_type[idx];
140 }
141 case EXP_CSCOPE_KILL:
142 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000143 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000144
145 /* ":cscope kill" accepts connection numbers or partial names of
146 * the pathname of the cscope database as argument. Only complete
147 * with connection numbers. -1 can also be used to kill all
148 * connections. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000149 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000150 {
151 if (csinfo[i].fname == NULL)
152 continue;
153 if (current_idx++ == idx)
154 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000155 vim_snprintf(connection, sizeof(connection), "%d", i);
156 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000157 }
158 }
159 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
160 }
161 default:
162 return NULL;
163 }
164}
165
166/*
167 * Handle command line completion for :cscope command.
168 */
169 void
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000170set_context_in_cscope_cmd(xp, arg, cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000171 expand_T *xp;
172 char_u *arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000173 cmdidx_T cmdidx;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000174{
175 char_u *p;
176
177 /* Default: expand subcommands */
178 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000179 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000180 expand_what = (cmdidx == CMD_scscope)
181 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000182
183 /* (part of) subcommand already typed */
184 if (*arg != NUL)
185 {
186 p = skiptowhite(arg);
187 if (*p != NUL) /* past first word */
188 {
189 xp->xp_pattern = skipwhite(p);
190 if (*skiptowhite(xp->xp_pattern) != NUL)
191 xp->xp_context = EXPAND_NOTHING;
192 else if (STRNICMP(arg, "add", p - arg) == 0)
193 xp->xp_context = EXPAND_FILES;
194 else if (STRNICMP(arg, "kill", p - arg) == 0)
195 expand_what = EXP_CSCOPE_KILL;
196 else if (STRNICMP(arg, "find", p - arg) == 0)
197 expand_what = EXP_CSCOPE_FIND;
198 else
199 xp->xp_context = EXPAND_NOTHING;
200 }
201 }
202}
203
204#endif /* FEAT_CMDL_COMPL */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/*
207 * PRIVATE: do_cscope_general
208 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000209 * Find the command, print help if invalid, and then call the corresponding
210 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 */
212 static void
213do_cscope_general(eap, make_split)
214 exarg_T *eap;
215 int make_split; /* whether to split window */
216{
217 cscmd_T *cmdp;
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
220 {
221 cs_help(eap);
222 return;
223 }
224
225#ifdef FEAT_WINDOWS
226 if (make_split)
227 {
228 if (!cmdp->cansplit)
229 {
230 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
231 return;
232 }
233 postponed_split = -1;
234 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000235 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237#endif
238
239 cmdp->func(eap);
240
241#ifdef FEAT_WINDOWS
242 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000243 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244#endif
245}
246
247/*
248 * PUBLIC: do_cscope
249 */
250 void
251do_cscope(eap)
252 exarg_T *eap;
253{
254 do_cscope_general(eap, FALSE);
255}
256
257/*
258 * PUBLIC: do_scscope
259 *
260 * same as do_cscope, but splits window, too.
261 */
262 void
263do_scscope(eap)
264 exarg_T *eap;
265{
266 do_cscope_general(eap, TRUE);
267}
268
269/*
270 * PUBLIC: do_cstag
271 *
272 */
273 void
274do_cstag(eap)
275 exarg_T *eap;
276{
277 int ret = FALSE;
278
Bram Moolenaar446cb832008-06-24 21:56:24 +0000279 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 {
281 (void)EMSG(_("E562: Usage: cstag <ident>"));
282 return;
283 }
284
285 switch (p_csto)
286 {
287 case 0 :
288 if (cs_check_for_connections())
289 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000290 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200291 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (ret == FALSE)
293 {
294 cs_free_tags();
295 if (msg_col)
296 msg_putchar('\n');
297
298 if (cs_check_for_tags())
299 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
300 }
301 }
302 else if (cs_check_for_tags())
303 {
304 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
305 }
306 break;
307 case 1 :
308 if (cs_check_for_tags())
309 {
310 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
311 if (ret == FALSE)
312 {
313 if (msg_col)
314 msg_putchar('\n');
315
316 if (cs_check_for_connections())
317 {
318 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200319 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 if (ret == FALSE)
321 cs_free_tags();
322 }
323 }
324 }
325 else if (cs_check_for_connections())
326 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000327 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200328 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 if (ret == FALSE)
330 cs_free_tags();
331 }
332 break;
333 default :
334 break;
335 }
336
337 if (!ret)
338 {
339 (void)EMSG(_("E257: cstag: tag not found"));
340#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
341 g_do_tagpreview = 0;
342#endif
343 }
344
345} /* do_cscope */
346
347
348/*
349 * PUBLIC: cs_find
350 *
351 * this simulates a vim_fgets(), but for cscope, returns the next line
352 * from the cscope output. should only be called from find_tags()
353 *
354 * returns TRUE if eof, FALSE otherwise
355 */
356 int
357cs_fgets(buf, size)
358 char_u *buf;
359 int size;
360{
361 char *p;
362
363 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
364 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000365 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 return FALSE;
368} /* cs_fgets */
369
370
371/*
372 * PUBLIC: cs_free_tags
373 *
374 * called only from do_tag(), when popping the tag stack
375 */
376 void
377cs_free_tags()
378{
379 cs_manage_matches(NULL, NULL, -1, Free);
380}
381
382
383/*
384 * PUBLIC: cs_print_tags
385 *
386 * called from do_tag()
387 */
388 void
389cs_print_tags()
390{
391 cs_manage_matches(NULL, NULL, -1, Print);
392}
393
394
395/*
396 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
397 *
398 * Checks for the existence of a |cscope| connection. If no
399 * parameters are specified, then the function returns:
400 *
401 * 0, if cscope was not available (not compiled in), or if there
402 * are no cscope connections; or
403 * 1, if there is at least one cscope connection.
404 *
405 * If parameters are specified, then the value of {num}
406 * determines how existence of a cscope connection is checked:
407 *
408 * {num} Description of existence check
409 * ----- ------------------------------
410 * 0 Same as no parameters (e.g., "cscope_connection()").
411 * 1 Ignore {prepend}, and use partial string matches for
412 * {dbpath}.
413 * 2 Ignore {prepend}, and use exact string matches for
414 * {dbpath}.
415 * 3 Use {prepend}, use partial string matches for both
416 * {dbpath} and {prepend}.
417 * 4 Use {prepend}, use exact string matches for both
418 * {dbpath} and {prepend}.
419 *
420 * Note: All string comparisons are case sensitive!
421 */
422#if defined(FEAT_EVAL) || defined(PROTO)
423 int
424cs_connection(num, dbpath, ppath)
425 int num;
426 char_u *dbpath;
427 char_u *ppath;
428{
429 int i;
430
431 if (num < 0 || num > 4 || (num > 0 && !dbpath))
432 return FALSE;
433
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000434 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
436 if (!csinfo[i].fname)
437 continue;
438
439 if (num == 0)
440 return TRUE;
441
442 switch (num)
443 {
444 case 1:
445 if (strstr(csinfo[i].fname, (char *)dbpath))
446 return TRUE;
447 break;
448 case 2:
449 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
450 return TRUE;
451 break;
452 case 3:
453 if (strstr(csinfo[i].fname, (char *)dbpath)
454 && ((!ppath && !csinfo[i].ppath)
455 || (ppath
456 && csinfo[i].ppath
457 && strstr(csinfo[i].ppath, (char *)ppath))))
458 return TRUE;
459 break;
460 case 4:
461 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
462 && ((!ppath && !csinfo[i].ppath)
463 || (ppath
464 && csinfo[i].ppath
465 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
466 return TRUE;
467 break;
468 }
469 }
470
471 return FALSE;
472} /* cs_connection */
473#endif
474
475
476/*
477 * PRIVATE functions
478 ****************************************************************************/
479
480/*
481 * PRIVATE: cs_add
482 *
483 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000484 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 *
486 * MAXPATHL 256
487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 static int
489cs_add(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000490 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 char *fname, *ppath, *flags = NULL;
493
494 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
495 {
496 cs_usage_msg(Add);
497 return CSCOPE_FAILURE;
498 }
499 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
500 flags = strtok((char *)NULL, (const char *)" ");
501
502 return cs_add_common(fname, ppath, flags);
503}
504
505 static void
506cs_stat_emsg(fname)
507 char *fname;
508{
509 char *stat_emsg = _("E563: stat(%s) error: %d");
510 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
511
512 if (buf != NULL)
513 {
514 (void)sprintf(buf, stat_emsg, fname, errno);
515 (void)EMSG(buf);
516 vim_free(buf);
517 }
518 else
519 (void)EMSG(_("E563: stat error"));
520}
521
522
523/*
524 * PRIVATE: cs_add_common
525 *
526 * the common routine to add a new cscope connection. called by
527 * cs_add() and cs_reset(). i really don't like to do this, but this
528 * routine uses a number of goto statements.
529 */
530 static int
531cs_add_common(arg1, arg2, flags)
532 char *arg1; /* filename - may contain environment variables */
533 char *arg2; /* prepend path - may contain environment variables */
534 char *flags;
535{
536 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000537 int ret;
538 char *fname = NULL;
539 char *fname2 = NULL;
540 char *ppath = NULL;
541 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000544 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 goto add_err;
546
547 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
548 ret = stat(fname, &statbuf);
549 if (ret < 0)
550 {
551staterr:
552 if (p_csverbose)
553 cs_stat_emsg(fname);
554 goto add_err;
555 }
556
557 /* get the prepend path (arg2), expand it, and try to stat it */
558 if (arg2 != NULL)
559 {
560 struct stat statbuf2;
561
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000562 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 goto add_err;
564
565 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
566 ret = stat(ppath, &statbuf2);
567 if (ret < 0)
568 goto staterr;
569 }
570
571 /* if filename is a directory, append the cscope database name to it */
572 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
573 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000574 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (fname2 == NULL)
576 goto add_err;
577
578 while (fname[strlen(fname)-1] == '/'
579#ifdef WIN32
580 || fname[strlen(fname)-1] == '\\'
581#endif
582 )
583 {
584 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200585 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 break;
587 }
588 if (fname[0] == '\0')
589 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
590 else
591 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
592
593 ret = stat(fname2, &statbuf);
594 if (ret < 0)
595 {
596 if (p_csverbose)
597 cs_stat_emsg(fname2);
598 goto add_err;
599 }
600
601 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
602 }
603#if defined(UNIX)
604 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
605#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000606 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
608#endif
609 {
610 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
611 }
612 else
613 {
614 if (p_csverbose)
615 (void)EMSG2(
616 _("E564: %s is not a directory or a valid cscope database"),
617 fname);
618 goto add_err;
619 }
620
621 if (i != -1)
622 {
623 if (cs_create_connection(i) == CSCOPE_FAILURE
624 || cs_read_prompt(i) == CSCOPE_FAILURE)
625 {
626 cs_release_csp(i, TRUE);
627 goto add_err;
628 }
629
630 if (p_csverbose)
631 {
632 msg_clr_eos();
633 (void)smsg_attr(hl_attr(HLF_R),
634 (char_u *)_("Added cscope database %s"),
635 csinfo[i].fname);
636 }
637 }
638
639 vim_free(fname);
640 vim_free(fname2);
641 vim_free(ppath);
642 return CSCOPE_SUCCESS;
643
644add_err:
645 vim_free(fname2);
646 vim_free(fname);
647 vim_free(ppath);
648 return CSCOPE_FAILURE;
649} /* cs_add_common */
650
651
652 static int
653cs_check_for_connections()
654{
655 return (cs_cnt_connections() > 0);
656} /* cs_check_for_connections */
657
658
659 static int
660cs_check_for_tags()
661{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000662 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663} /* cs_check_for_tags */
664
665
666/*
667 * PRIVATE: cs_cnt_connections
668 *
669 * count the number of cscope connections
670 */
671 static int
672cs_cnt_connections()
673{
674 short i;
675 short cnt = 0;
676
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000677 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 if (csinfo[i].fname != NULL)
680 cnt++;
681 }
682 return cnt;
683} /* cs_cnt_connections */
684
685 static void
686cs_reading_emsg(idx)
687 int idx; /* connection index */
688{
689 EMSGN(_("E262: error reading cscope connection %ld"), idx);
690}
691
692#define CSREAD_BUFSIZE 2048
693/*
694 * PRIVATE: cs_cnt_matches
695 *
696 * count the number of matches for a given cscope connection.
697 */
698 static int
699cs_cnt_matches(idx)
700 int idx;
701{
702 char *stok;
703 char *buf;
704 int nlines;
705
706 buf = (char *)alloc(CSREAD_BUFSIZE);
707 if (buf == NULL)
708 return 0;
709 for (;;)
710 {
711 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
712 {
713 if (feof(csinfo[idx].fr_fp))
714 errno = EIO;
715
716 cs_reading_emsg(idx);
717
718 vim_free(buf);
719 return -1;
720 }
721
722 /*
723 * If the database is out of date, or there's some other problem,
724 * cscope will output error messages before the number-of-lines output.
725 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000726 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
728 if ((stok = strtok(buf, (const char *)" ")) == NULL)
729 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000730 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 continue;
732
733 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
734 continue;
735 nlines = atoi(stok);
736 if (nlines < 0)
737 {
738 nlines = 0;
739 break;
740 }
741
742 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
743 continue;
744 if (strncmp((const char *)stok, "lines", 5))
745 continue;
746
747 break;
748 }
749
750 vim_free(buf);
751 return nlines;
752} /* cs_cnt_matches */
753
754
755/*
756 * PRIVATE: cs_create_cmd
757 *
758 * Creates the actual cscope command query from what the user entered.
759 */
760 static char *
761cs_create_cmd(csoption, pattern)
762 char *csoption;
763 char *pattern;
764{
765 char *cmd;
766 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000767 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769 switch (csoption[0])
770 {
771 case '0' : case 's' :
772 search = 0;
773 break;
774 case '1' : case 'g' :
775 search = 1;
776 break;
777 case '2' : case 'd' :
778 search = 2;
779 break;
780 case '3' : case 'c' :
781 search = 3;
782 break;
783 case '4' : case 't' :
784 search = 4;
785 break;
786 case '6' : case 'e' :
787 search = 6;
788 break;
789 case '7' : case 'f' :
790 search = 7;
791 break;
792 case '8' : case 'i' :
793 search = 8;
794 break;
795 default :
796 (void)EMSG(_("E561: unknown cscope search type"));
797 cs_usage_msg(Find);
798 return NULL;
799 }
800
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000801 /* Skip white space before the patter, except for text and pattern search,
802 * they may want to use the leading white space. */
803 pat = pattern;
804 if (search != 4 && search != 6)
805 while vim_iswhite(*pat)
806 ++pat;
807
808 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 return NULL;
810
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000811 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813 return cmd;
814} /* cs_create_cmd */
815
816
817/*
818 * PRIVATE: cs_create_connection
819 *
820 * This piece of code was taken/adapted from nvi. do we need to add
821 * the BSD license notice?
822 */
823 static int
824cs_create_connection(i)
825 int i;
826{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000827#ifdef UNIX
828 int to_cs[2], from_cs[2];
829#endif
830 int len;
831 char *prog, *cmd, *ppath = NULL;
832#ifdef WIN32
833 int fd;
834 SECURITY_ATTRIBUTES sa;
835 PROCESS_INFORMATION pi;
836 STARTUPINFO si;
837 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
838 HANDLE stdin_rd, stdout_rd;
839 HANDLE stdout_wr, stdin_wr;
840 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000841# ifdef __BORLANDC__
842# define OPEN_OH_ARGTYPE long
843# else
844# if (_MSC_VER >= 1300)
845# define OPEN_OH_ARGTYPE intptr_t
846# else
847# define OPEN_OH_ARGTYPE long
848# endif
849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
851
Bram Moolenaar02b06312007-09-06 15:39:22 +0000852#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 /*
854 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
855 * from_cs[0] and writes to to_cs[1].
856 */
857 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
858 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
859 {
860 (void)EMSG(_("E566: Could not create cscope pipes"));
861err_closing:
862 if (to_cs[0] != -1)
863 (void)close(to_cs[0]);
864 if (to_cs[1] != -1)
865 (void)close(to_cs[1]);
866 if (from_cs[0] != -1)
867 (void)close(from_cs[0]);
868 if (from_cs[1] != -1)
869 (void)close(from_cs[1]);
870 return CSCOPE_FAILURE;
871 }
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 switch (csinfo[i].pid = fork())
874 {
875 case -1:
876 (void)EMSG(_("E622: Could not fork for cscope"));
877 goto err_closing;
878 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (dup2(to_cs[0], STDIN_FILENO) == -1)
880 PERROR("cs_create_connection 1");
881 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
882 PERROR("cs_create_connection 2");
883 if (dup2(from_cs[1], STDERR_FILENO) == -1)
884 PERROR("cs_create_connection 3");
885
886 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 (void)close(to_cs[1]);
888 (void)close(from_cs[0]);
889#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000890 /* WIN32 */
891 /* Create pipes to communicate with cscope */
892 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
893 sa.bInheritHandle = TRUE;
894 sa.lpSecurityDescriptor = NULL;
895
896 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
897 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
898 {
899 (void)EMSG(_("E566: Could not create cscope pipes"));
900err_closing:
901 if (pipe_stdin)
902 {
903 CloseHandle(stdin_rd);
904 CloseHandle(stdin_wr);
905 }
906 if (pipe_stdout)
907 {
908 CloseHandle(stdout_rd);
909 CloseHandle(stdout_wr);
910 }
911 return CSCOPE_FAILURE;
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#endif
914 /* expand the cscope exec for env var's */
915 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
916 {
917#ifdef UNIX
918 return CSCOPE_FAILURE;
919#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000920 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 goto err_closing;
922#endif
923 }
924 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
925
926 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000927 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (csinfo[i].ppath)
929 {
930 /* expand the prepend path for env var's */
931 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
932 {
933 vim_free(prog);
934#ifdef UNIX
935 return CSCOPE_FAILURE;
936#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000937 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 goto err_closing;
939#endif
940 }
941 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000943 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945
946 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000947 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 if ((cmd = (char *)alloc(len)) == NULL)
950 {
951 vim_free(prog);
952 vim_free(ppath);
953#ifdef UNIX
954 return CSCOPE_FAILURE;
955#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000956 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 goto err_closing;
958#endif
959 }
960
961 /* run the cscope command; is there execl for non-unix systems? */
962#if defined(UNIX)
963 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
964#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000965 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
967#endif
968 if (csinfo[i].ppath != NULL)
969 {
970 (void)strcat(cmd, " -P");
971 (void)strcat(cmd, csinfo[i].ppath);
972 }
973 if (csinfo[i].flags != NULL)
974 {
975 (void)strcat(cmd, " ");
976 (void)strcat(cmd, csinfo[i].flags);
977 }
978# ifdef UNIX
979 /* on Win32 we still need prog */
980 vim_free(prog);
981# endif
982 vim_free(ppath);
983
984#if defined(UNIX)
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000985 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 PERROR(_("cs_create_connection exec failed"));
987
988 exit(127);
989 /* NOTREACHED */
990 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 /*
992 * Save the file descriptors for later duplication, and
993 * reopen as streams.
994 */
995 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
996 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
997 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
998 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 /* close unused */
1001 (void)close(to_cs[0]);
1002 (void)close(from_cs[1]);
1003
1004 break;
1005 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001008 /* WIN32 */
1009 /* Create a new process to run cscope and use pipes to talk with it */
1010 GetStartupInfo(&si);
1011 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1012 si.wShowWindow = SW_HIDE; /* Hide child application window */
1013 si.hStdOutput = stdout_wr;
1014 si.hStdError = stdout_wr;
1015 si.hStdInput = stdin_rd;
1016 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1017 NULL, NULL, &si, &pi);
1018 vim_free(prog);
1019 vim_free(cmd);
1020
1021 if (!created)
1022 {
1023 PERROR(_("cs_create_connection exec failed"));
1024 (void)EMSG(_("E623: Could not spawn cscope process"));
1025 goto err_closing;
1026 }
1027 /* else */
1028 csinfo[i].pid = pi.dwProcessId;
1029 csinfo[i].hProc = pi.hProcess;
1030 CloseHandle(pi.hThread);
1031
1032 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001033 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1034 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001035 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1036 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001037 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1038 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001039 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1040 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1041
1042 /* Close handles for file descriptors inherited by the cscope process */
1043 CloseHandle(stdin_rd);
1044 CloseHandle(stdout_wr);
1045
1046#endif /* !UNIX */
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 return CSCOPE_SUCCESS;
1049} /* cs_create_connection */
1050
1051
1052/*
1053 * PRIVATE: cs_find
1054 *
1055 * query cscope using command line interface. parse the output and use tselect
1056 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1057 *
1058 * returns TRUE if we jump to a tag or abort, FALSE if not.
1059 */
1060 static int
1061cs_find(eap)
1062 exarg_T *eap;
1063{
1064 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001065 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 if (cs_check_for_connections() == FALSE)
1068 {
1069 (void)EMSG(_("E567: no cscope connections"));
1070 return FALSE;
1071 }
1072
1073 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1074 {
1075 cs_usage_msg(Find);
1076 return FALSE;
1077 }
1078
1079 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001080 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 cs_usage_msg(Find);
1083 return FALSE;
1084 }
1085
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001086 /*
1087 * Let's replace the NULs written by strtok() with spaces - we need the
1088 * spaces to correctly display the quickfix/location list window's title.
1089 */
1090 for (i = 0; i < eap_arg_len; ++i)
1091 if (NUL == eap->arg[i])
1092 eap->arg[i] = ' ';
1093
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001094 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001095 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096} /* cs_find */
1097
1098
1099/*
1100 * PRIVATE: cs_find_common
1101 *
1102 * common code for cscope find, shared by cs_find() and do_cstag()
1103 */
1104 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001105cs_find_common(opt, pat, forceit, verbose, use_ll, cmdline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 char *opt;
1107 char *pat;
1108 int forceit;
1109 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001110 int use_ll;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001111 char_u *cmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int i;
1114 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001115 int *nummatches;
1116 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#ifdef FEAT_QUICKFIX
1118 char cmdletter;
1119 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001120
1121 /* get cmd letter */
1122 switch (opt[0])
1123 {
1124 case '0' :
1125 cmdletter = 's';
1126 break;
1127 case '1' :
1128 cmdletter = 'g';
1129 break;
1130 case '2' :
1131 cmdletter = 'd';
1132 break;
1133 case '3' :
1134 cmdletter = 'c';
1135 break;
1136 case '4' :
1137 cmdletter = 't';
1138 break;
1139 case '6' :
1140 cmdletter = 'e';
1141 break;
1142 case '7' :
1143 cmdletter = 'f';
1144 break;
1145 case '8' :
1146 cmdletter = 'i';
1147 break;
1148 default :
1149 cmdletter = opt[0];
1150 }
1151
1152 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1153 if (qfpos != NULL)
1154 {
1155 qfpos++;
1156 /* next symbol must be + or - */
1157 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1158 {
1159 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1160 char *buf = (char *)alloc((unsigned)strlen(nf));
1161
1162 /* strlen will be enough because we use chars */
1163 if (buf != NULL)
1164 {
1165 sprintf(buf, nf, *qfpos, *(qfpos-1));
1166 (void)EMSG(buf);
1167 vim_free(buf);
1168 }
1169 return FALSE;
1170 }
1171
1172# ifdef FEAT_AUTOCMD
1173 if (*qfpos != '0')
1174 {
1175 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1176 curbuf->b_fname, TRUE, curbuf);
1177# ifdef FEAT_EVAL
1178 if (did_throw || force_abort)
1179 return FALSE;
1180# endif
1181 }
1182# endif
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185
1186 /* create the actual command to send to cscope */
1187 cmd = cs_create_cmd(opt, pat);
1188 if (cmd == NULL)
1189 return FALSE;
1190
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001191 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1192 if (nummatches == NULL)
1193 return FALSE;
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 /* send query to all open connections, then count the total number
1196 * of matches so we can alloc matchesp all in one swell foop
1197 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001198 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 nummatches[i] = 0;
1200 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001201 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001203 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 continue;
1205
1206 /* send cmd to cscope */
1207 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1208 (void)fflush(csinfo[i].to_fp);
1209
1210 nummatches[i] = cs_cnt_matches(i);
1211
1212 if (nummatches[i] > -1)
1213 totmatches += nummatches[i];
1214
1215 if (nummatches[i] == 0)
1216 (void)cs_read_prompt(i);
1217 }
1218 vim_free(cmd);
1219
1220 if (totmatches == 0)
1221 {
1222 char *nf = _("E259: no matches found for cscope query %s of %s");
1223 char *buf;
1224
1225 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001226 {
1227 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001231 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (buf == NULL)
1233 (void)EMSG(nf);
1234 else
1235 {
1236 sprintf(buf, nf, opt, pat);
1237 (void)EMSG(buf);
1238 vim_free(buf);
1239 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001240 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 return FALSE;
1242 }
1243
1244#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001245 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001248 FILE *f;
1249 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001250 qf_info_T *qi = NULL;
1251 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001253 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001254 if (f == NULL)
1255 EMSG2(_(e_notopen), tmp);
1256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001258 cs_file_results(f, nummatches);
1259 fclose(f);
1260 if (use_ll) /* Use location list */
1261 wp = curwin;
1262 /* '-' starts a new error list */
1263 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001264 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001266# ifdef FEAT_WINDOWS
1267 if (postponed_split != 0)
1268 {
1269 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001271 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001272 postponed_split = 0;
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001275
1276# ifdef FEAT_AUTOCMD
1277 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1278 curbuf->b_fname, TRUE, curbuf);
1279# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001280 if (use_ll)
1281 /*
1282 * In the location list window, use the displayed location
1283 * list. Otherwise, use the location list for the window.
1284 */
1285 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1286 ? wp->w_llist_ref : wp->w_llist;
1287 qf_jump(qi, 0, 0, forceit);
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 mch_remove(tmp);
1291 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001292 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 return TRUE;
1294 }
1295 else
1296#endif /* FEAT_QUICKFIX */
1297 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001298 char **matches = NULL, **contexts = NULL;
1299 int matched = 0;
1300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 /* read output */
1302 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1303 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001304 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (matches == NULL)
1306 return FALSE;
1307
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001308 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1311 }
1312
1313} /* cs_find_common */
1314
1315/*
1316 * PRIVATE: cs_help
1317 *
1318 * print help
1319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 static int
1321cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001322 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 cscmd_T *cmdp = cs_cmds;
1325
1326 (void)MSG_PUTS(_("cscope commands:\n"));
1327 while (cmdp->name != NULL)
1328 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001329 char *help = _(cmdp->help);
1330 int space_cnt = 30 - vim_strsize((char_u *)help);
1331
1332 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1333 if (space_cnt < 0)
1334 space_cnt = 0;
1335 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1336 cmdp->name,
1337 help, space_cnt, " ",
1338 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001340 MSG_PUTS(_("\n"
1341 " c: Find functions calling this function\n"
1342 " d: Find functions called by this function\n"
1343 " e: Find this egrep pattern\n"
1344 " f: Find this file\n"
1345 " g: Find this definition\n"
1346 " i: Find files #including this file\n"
1347 " s: Find this C symbol\n"
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001348 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 cmdp++;
1351 }
1352
1353 wait_return(TRUE);
1354 return 0;
1355} /* cs_help */
1356
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 static void
1359clear_csinfo(i)
1360 int i;
1361{
1362 csinfo[i].fname = NULL;
1363 csinfo[i].ppath = NULL;
1364 csinfo[i].flags = NULL;
1365#if defined(UNIX)
1366 csinfo[i].st_dev = (dev_t)0;
1367 csinfo[i].st_ino = (ino_t)0;
1368#else
1369 csinfo[i].nVolume = 0;
1370 csinfo[i].nIndexHigh = 0;
1371 csinfo[i].nIndexLow = 0;
1372#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001373 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 csinfo[i].fr_fp = NULL;
1375 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001376#if defined(WIN32)
1377 csinfo[i].hProc = NULL;
1378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
1381#ifndef UNIX
1382static char *GetWin32Error __ARGS((void));
1383
1384 static char *
1385GetWin32Error()
1386{
1387 char *msg = NULL;
1388 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1389 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1390 if (msg != NULL)
1391 {
1392 /* remove trailing \r\n */
1393 char *pcrlf = strstr(msg, "\r\n");
1394 if (pcrlf != NULL)
1395 *pcrlf = '\0';
1396 }
1397 return msg;
1398}
1399#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401/*
1402 * PRIVATE: cs_insert_filelist
1403 *
1404 * insert a new cscope database filename into the filelist
1405 */
1406 static int
1407cs_insert_filelist(fname, ppath, flags, sb)
1408 char *fname;
1409 char *ppath;
1410 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001411 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 short i, j;
1414#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 BY_HANDLE_FILE_INFORMATION bhfi;
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1418 if (!mch_windows95())
1419 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001420 switch (win32_fileinfo(fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001422 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1423 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (p_csverbose)
1425 {
1426 char *cant_msg = _("E625: cannot open cscope database: %s");
1427 char *winmsg = GetWin32Error();
1428
1429 if (winmsg != NULL)
1430 {
1431 (void)EMSG2(cant_msg, winmsg);
1432 LocalFree(winmsg);
1433 }
1434 else
1435 /* subst filename if can't get error text */
1436 (void)EMSG2(cant_msg, fname);
1437 }
1438 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001439
1440 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (p_csverbose)
1442 (void)EMSG(_("E626: cannot get cscope database information"));
1443 return -1;
1444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446#endif
1447
1448 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001449 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 {
1451 if (csinfo[j].fname != NULL
1452#if defined(UNIX)
1453 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1454#else
1455 /* compare pathnames first */
1456 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001457 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 || (!mch_windows95()
1459 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1460 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1461 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1462#endif
1463 )
1464 {
1465 if (p_csverbose)
1466 (void)EMSG(_("E568: duplicate cscope database not added"));
1467 return -1;
1468 }
1469
1470 if (csinfo[j].fname == NULL && i == -1)
1471 i = j; /* remember first empty entry */
1472 }
1473
1474 if (i == -1)
1475 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001476 i = csinfo_size;
1477 if (csinfo_size == 0)
1478 {
1479 /* First time allocation: allocate only 1 connection. It should
1480 * be enough for most users. If more is needed, csinfo will be
1481 * reallocated. */
1482 csinfo_size = 1;
1483 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1484 }
1485 else
1486 {
1487 /* Reallocate space for more connections. */
1488 csinfo_size *= 2;
1489 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1490 }
1491 if (csinfo == NULL)
1492 return -1;
1493 for (j = csinfo_size/2; j < csinfo_size; j++)
1494 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001497 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 return -1;
1499
1500 (void)strcpy(csinfo[i].fname, (const char *)fname);
1501
1502 if (ppath != NULL)
1503 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001504 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
1506 vim_free(csinfo[i].fname);
1507 csinfo[i].fname = NULL;
1508 return -1;
1509 }
1510 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1511 } else
1512 csinfo[i].ppath = NULL;
1513
1514 if (flags != NULL)
1515 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001516 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 vim_free(csinfo[i].fname);
1519 vim_free(csinfo[i].ppath);
1520 csinfo[i].fname = NULL;
1521 csinfo[i].ppath = NULL;
1522 return -1;
1523 }
1524 (void)strcpy(csinfo[i].flags, (const char *)flags);
1525 } else
1526 csinfo[i].flags = NULL;
1527
1528#if defined(UNIX)
1529 csinfo[i].st_dev = sb->st_dev;
1530 csinfo[i].st_ino = sb->st_ino;
1531
1532#else
1533 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1534 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1535 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1536#endif
1537 return i;
1538} /* cs_insert_filelist */
1539
1540
1541/*
1542 * PRIVATE: cs_lookup_cmd
1543 *
1544 * find cscope command in command table
1545 */
1546 static cscmd_T *
1547cs_lookup_cmd(eap)
1548 exarg_T *eap;
1549{
1550 cscmd_T *cmdp;
1551 char *stok;
1552 size_t len;
1553
1554 if (eap->arg == NULL)
1555 return NULL;
1556
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001557 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001558 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1561 return NULL;
1562
1563 len = strlen(stok);
1564 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1565 {
1566 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1567 return (cmdp);
1568 }
1569 return NULL;
1570} /* cs_lookup_cmd */
1571
1572
1573/*
1574 * PRIVATE: cs_kill
1575 *
1576 * nuke em
1577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 static int
1579cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001580 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 char *stok;
1583 short i;
1584
1585 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1586 {
1587 cs_usage_msg(Kill);
1588 return CSCOPE_FAILURE;
1589 }
1590
1591 /* only single digit positive and negative integers are allowed */
1592 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1593 || (strlen(stok) < 3 && stok[0] == '-'
1594 && VIM_ISDIGIT((int)(stok[1]))))
1595 i = atoi(stok);
1596 else
1597 {
1598 /* It must be part of a name. We will try to find a match
1599 * within all the names in the csinfo data structure
1600 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001601 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1604 break;
1605 }
1606 }
1607
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001608 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 if (p_csverbose)
1611 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1612 }
1613 else
1614 {
1615 if (i == -1)
1616 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001617 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 if (csinfo[i].fname)
1620 cs_kill_execute(i, csinfo[i].fname);
1621 }
1622 }
1623 else
1624 cs_kill_execute(i, stok);
1625 }
1626
1627 return 0;
1628} /* cs_kill */
1629
1630
1631/*
1632 * PRIVATE: cs_kill_execute
1633 *
1634 * Actually kills a specific cscope connection.
1635 */
1636 static void
1637cs_kill_execute(i, cname)
1638 int i; /* cscope table index */
1639 char *cname; /* cscope database name */
1640{
1641 if (p_csverbose)
1642 {
1643 msg_clr_eos();
1644 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1645 (char_u *)_("cscope connection %s closed"), cname);
1646 }
1647 cs_release_csp(i, TRUE);
1648}
1649
1650
1651/*
1652 * PRIVATE: cs_make_vim_style_matches
1653 *
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001654 * convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1656 * the type of the tag you are looking for. for example, in Darren Hiebert's
1657 * ctags (the one that comes with vim), #define's use a line number to find the
1658 * tag in a file while function definitions use a regexp search pattern.
1659 *
1660 * i'm going to always use the line number because cscope does something
1661 * quirky (and probably other things i don't know about):
1662 *
1663 * if you have "# define" in your source file, which is
1664 * perfectly legal, cscope thinks you have "#define". this
1665 * will result in a failed regexp search. :(
1666 *
1667 * besides, even if this particular case didn't happen, the search pattern
1668 * would still have to be modified to escape all the special regular expression
1669 * characters to comply with ctags formatting.
1670 */
1671 static char *
1672cs_make_vim_style_matches(fname, slno, search, tagstr)
1673 char *fname;
1674 char *slno;
1675 char *search;
1676 char *tagstr;
1677{
1678 /* vim style is ctags:
1679 *
1680 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1681 *
1682 * but as mentioned above, we'll always use the line number and
1683 * put the search pattern (if one exists) as "extra"
1684 *
1685 * buf is used as part of vim's method of handling tags, and
1686 * (i think) vim frees it when you pop your tags and get replaced
1687 * by new ones on the tag stack.
1688 */
1689 char *buf;
1690 int amt;
1691
1692 if (search != NULL)
1693 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001694 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if ((buf = (char *)alloc(amt)) == NULL)
1696 return NULL;
1697
1698 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1699 }
1700 else
1701 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001702 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if ((buf = (char *)alloc(amt)) == NULL)
1704 return NULL;
1705
1706 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1707 }
1708
1709 return buf;
1710} /* cs_make_vim_style_matches */
1711
1712
1713/*
1714 * PRIVATE: cs_manage_matches
1715 *
1716 * this is kind of hokey, but i don't see an easy way round this..
1717 *
1718 * Store: keep a ptr to the (malloc'd) memory of matches originally
1719 * generated from cs_find(). the matches are originally lines directly
1720 * from cscope output, but transformed to look like something out of a
1721 * ctags. see cs_make_vim_style_matches for more details.
1722 *
1723 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1724 * the next line from the cscope output. it basically keeps track of which
1725 * lines have been "used" and returns the next one.
1726 *
1727 * Free: frees up everything and resets
1728 *
1729 * Print: prints the tags
1730 */
1731 static char *
1732cs_manage_matches(matches, contexts, totmatches, cmd)
1733 char **matches;
1734 char **contexts;
1735 int totmatches;
1736 mcmd_e cmd;
1737{
1738 static char **mp = NULL;
1739 static char **cp = NULL;
1740 static int cnt = -1;
1741 static int next = -1;
1742 char *p = NULL;
1743
1744 switch (cmd)
1745 {
1746 case Store:
1747 assert(matches != NULL);
1748 assert(totmatches > 0);
1749 if (mp != NULL || cp != NULL)
1750 (void)cs_manage_matches(NULL, NULL, -1, Free);
1751 mp = matches;
1752 cp = contexts;
1753 cnt = totmatches;
1754 next = 0;
1755 break;
1756 case Get:
1757 if (next >= cnt)
1758 return NULL;
1759
1760 p = mp[next];
1761 next++;
1762 break;
1763 case Free:
1764 if (mp != NULL)
1765 {
1766 if (cnt > 0)
1767 while (cnt--)
1768 {
1769 vim_free(mp[cnt]);
1770 if (cp != NULL)
1771 vim_free(cp[cnt]);
1772 }
1773 vim_free(mp);
1774 vim_free(cp);
1775 }
1776 mp = NULL;
1777 cp = NULL;
1778 cnt = 0;
1779 next = 0;
1780 break;
1781 case Print:
1782 cs_print_tags_priv(mp, cp, cnt);
1783 break;
1784 default: /* should not reach here */
1785 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1786 return NULL;
1787 }
1788
1789 return p;
1790} /* cs_manage_matches */
1791
1792
1793/*
1794 * PRIVATE: cs_parse_results
1795 *
1796 * parse cscope output
1797 */
1798 static char *
1799cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1800 int cnumber;
1801 char *buf;
1802 int bufsize;
1803 char **context;
1804 char **linenumber;
1805 char **search;
1806{
1807 int ch;
1808 char *p;
1809 char *name;
1810
1811 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1812 {
1813 if (feof(csinfo[cnumber].fr_fp))
1814 errno = EIO;
1815
1816 cs_reading_emsg(cnumber);
1817
1818 return NULL;
1819 }
1820
1821 /* If the line's too long for the buffer, discard it. */
1822 if ((p = strchr(buf, '\n')) == NULL)
1823 {
1824 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1825 ;
1826 return NULL;
1827 }
1828 *p = '\0';
1829
1830 /*
1831 * cscope output is in the following format:
1832 *
1833 * <filename> <context> <line number> <pattern>
1834 */
1835 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1836 return NULL;
1837 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1838 return NULL;
1839 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1840 return NULL;
1841 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1842
1843 /* --- nvi ---
1844 * If the file is older than the cscope database, that is,
1845 * the database was built since the file was last modified,
1846 * or there wasn't a search string, use the line number.
1847 */
1848 if (strcmp(*search, "<unknown>") == 0)
1849 *search = NULL;
1850
1851 name = cs_resolve_file(cnumber, name);
1852 return name;
1853}
1854
Bram Moolenaarc716c302006-01-21 22:12:51 +00001855#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
1857 * PRIVATE: cs_file_results
1858 *
1859 * write cscope find results to file
1860 */
1861 static void
1862cs_file_results(f, nummatches_a)
1863 FILE *f;
1864 int *nummatches_a;
1865{
1866 int i, j;
1867 char *buf;
1868 char *search, *slno;
1869 char *fullname;
1870 char *cntx;
1871 char *context;
1872
1873 buf = (char *)alloc(CSREAD_BUFSIZE);
1874 if (buf == NULL)
1875 return;
1876
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001877 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 if (nummatches_a[i] < 1)
1880 continue;
1881
1882 for (j = 0; j < nummatches_a[i]; j++)
1883 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001884 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1885 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 continue;
1887
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001888 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001889 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 continue;
1891
1892 if (strcmp(cntx, "<global>")==0)
1893 strcpy(context, "<<global>>");
1894 else
1895 sprintf(context, "<<%s>>", cntx);
1896
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001897 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1899 else
1900 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1901
1902 vim_free(context);
1903 vim_free(fullname);
1904 } /* for all matches */
1905
1906 (void)cs_read_prompt(i);
1907
1908 } /* for all cscope connections */
1909 vim_free(buf);
1910}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913/*
1914 * PRIVATE: cs_fill_results
1915 *
1916 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1917 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001918 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 */
1920 static void
1921cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1922 char *tagstr;
1923 int totmatches;
1924 int *nummatches_a;
1925 char ***matches_p;
1926 char ***cntxts_p;
1927 int *matched;
1928{
1929 int i, j;
1930 char *buf;
1931 char *search, *slno;
1932 int totsofar = 0;
1933 char **matches = NULL;
1934 char **cntxts = NULL;
1935 char *fullname;
1936 char *cntx;
1937
1938 assert(totmatches > 0);
1939
1940 buf = (char *)alloc(CSREAD_BUFSIZE);
1941 if (buf == NULL)
1942 return;
1943
1944 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1945 goto parse_out;
1946 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1947 goto parse_out;
1948
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001949 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 if (nummatches_a[i] < 1)
1952 continue;
1953
1954 for (j = 0; j < nummatches_a[i]; j++)
1955 {
1956 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1957 &slno, &search)) == NULL)
1958 continue;
1959
1960 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1961 search, tagstr);
1962
1963 vim_free(fullname);
1964
1965 if (strcmp(cntx, "<global>") == 0)
1966 cntxts[totsofar] = NULL;
1967 else
1968 /* note: if vim_strsave returns NULL, then the context
1969 * will be "<global>", which is misleading.
1970 */
1971 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1972
1973 if (matches[totsofar] != NULL)
1974 totsofar++;
1975
1976 } /* for all matches */
1977
1978 (void)cs_read_prompt(i);
1979
1980 } /* for all cscope connections */
1981
1982parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001983 if (totsofar == 0)
1984 {
1985 /* No matches, free the arrays and return NULL in "*matches_p". */
1986 vim_free(matches);
1987 matches = NULL;
1988 vim_free(cntxts);
1989 cntxts = NULL;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 *matched = totsofar;
1992 *matches_p = matches;
1993 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 vim_free(buf);
1996} /* cs_fill_results */
1997
1998
1999/* get the requested path components */
2000 static char *
2001cs_pathcomponents(path)
2002 char *path;
2003{
2004 int i;
2005 char *s;
2006
2007 if (p_cspc == 0)
2008 return path;
2009
2010 s = path + strlen(path) - 1;
2011 for (i = 0; i < p_cspc; ++i)
2012 while (s > path && *--s != '/'
2013#ifdef WIN32
2014 && *--s != '\\'
2015#endif
2016 )
2017 ;
2018 if ((s > path && *s == '/')
2019#ifdef WIN32
2020 || (s > path && *s == '\\')
2021#endif
2022 )
2023 ++s;
2024 return s;
2025}
2026
2027/*
2028 * PRIVATE: cs_print_tags_priv
2029 *
2030 * called from cs_manage_matches()
2031 */
2032 static void
2033cs_print_tags_priv(matches, cntxts, num_matches)
2034 char **matches;
2035 char **cntxts;
2036 int num_matches;
2037{
2038 char *buf = NULL;
2039 int bufsize = 0; /* Track available bufsize */
2040 int newsize = 0;
2041 char *ptag;
2042 char *fname, *lno, *extra, *tbuf;
2043 int i, idx, num;
2044 char *globalcntx = "GLOBAL";
2045 char *cntxformat = " <<%s>>";
2046 char *context;
2047 char *cstag_msg = _("Cscope tag: %s");
2048 char *csfmt_str = "%4d %6s ";
2049
2050 assert (num_matches > 0);
2051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002052 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return;
2054
2055 strcpy(tbuf, matches[0]);
2056 ptag = strtok(tbuf, "\t");
2057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002058 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 buf = (char *)alloc(newsize);
2060 if (buf != NULL)
2061 {
2062 bufsize = newsize;
2063 (void)sprintf(buf, cstag_msg, ptag);
2064 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2065 }
2066
2067 vim_free(tbuf);
2068
2069 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2070 msg_advance(msg_col + 2);
2071 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2072
2073 num = 1;
2074 for (i = 0; i < num_matches; i++)
2075 {
2076 idx = i;
2077
2078 /* if we really wanted to, we could avoid this malloc and strcpy
2079 * by parsing matches[i] on the fly and placing stuff into buf
2080 * directly, but that's too much of a hassle
2081 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 continue;
2084 (void)strcpy(tbuf, matches[idx]);
2085
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002086 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 continue;
2088 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2089 continue;
2090 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002091 continue;
2092 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2095
2096 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002097 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 if (bufsize < newsize)
2099 {
2100 buf = (char *)vim_realloc(buf, newsize);
2101 if (buf == NULL)
2102 bufsize = 0;
2103 else
2104 bufsize = newsize;
2105 }
2106 if (buf != NULL)
2107 {
2108 /* csfmt_str = "%4d %6s "; */
2109 (void)sprintf(buf, csfmt_str, num, lno);
2110 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2111 }
2112 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2113
2114 /* compute the required space for the context */
2115 if (cntxts[idx] != NULL)
2116 context = cntxts[idx];
2117 else
2118 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 if (bufsize < newsize)
2122 {
2123 buf = (char *)vim_realloc(buf, newsize);
2124 if (buf == NULL)
2125 bufsize = 0;
2126 else
2127 bufsize = newsize;
2128 }
2129 if (buf != NULL)
2130 {
2131 (void)sprintf(buf, cntxformat, context);
2132
2133 /* print the context only if it fits on the same line */
2134 if (msg_col + (int)strlen(buf) >= (int)Columns)
2135 msg_putchar('\n');
2136 msg_advance(12);
2137 MSG_PUTS_LONG(buf);
2138 msg_putchar('\n');
2139 }
2140 if (extra != NULL)
2141 {
2142 msg_advance(13);
2143 MSG_PUTS_LONG(extra);
2144 }
2145
2146 vim_free(tbuf); /* only after printing extra due to strtok use */
2147
2148 if (msg_col)
2149 msg_putchar('\n');
2150
2151 ui_breakcheck();
2152 if (got_int)
2153 {
2154 got_int = FALSE; /* don't print any more matches */
2155 break;
2156 }
2157
2158 num++;
2159 } /* for all matches */
2160
2161 vim_free(buf);
2162} /* cs_print_tags_priv */
2163
2164
2165/*
2166 * PRIVATE: cs_read_prompt
2167 *
2168 * read a cscope prompt (basically, skip over the ">> ")
2169 */
2170 static int
2171cs_read_prompt(i)
2172 int i;
2173{
2174 int ch;
2175 char *buf = NULL; /* buffer for possible error message from cscope */
2176 int bufpos = 0;
2177 char *cs_emsg;
2178 int maxlen;
2179 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002180 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 int n;
2182
2183 cs_emsg = _("E609: Cscope error: %s");
2184 /* compute maximum allowed len for Cscope error message */
2185 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2186
2187 for (;;)
2188 {
2189 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2190 /* if there is room and char is printable */
2191 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2192 {
2193 if (buf == NULL) /* lazy buffer allocation */
2194 buf = (char *)alloc(maxlen);
2195 if (buf != NULL)
2196 {
2197 /* append character to the message */
2198 buf[bufpos++] = ch;
2199 buf[bufpos] = NUL;
2200 if (bufpos >= epromptlen
2201 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2202 {
2203 /* remove eprompt from buf */
2204 buf[bufpos - epromptlen] = NUL;
2205
2206 /* print message to user */
2207 (void)EMSG2(cs_emsg, buf);
2208
2209 /* send RETURN to cscope */
2210 (void)putc('\n', csinfo[i].to_fp);
2211 (void)fflush(csinfo[i].to_fp);
2212
2213 /* clear buf */
2214 bufpos = 0;
2215 buf[bufpos] = NUL;
2216 }
2217 }
2218 }
2219
2220 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2221 {
2222 if (n > 0)
2223 ch = getc(csinfo[i].fr_fp);
2224 if (ch == EOF)
2225 {
2226 PERROR("cs_read_prompt EOF");
2227 if (buf != NULL && buf[0] != NUL)
2228 (void)EMSG2(cs_emsg, buf);
2229 else if (p_csverbose)
2230 cs_reading_emsg(i); /* don't have additional information */
2231 cs_release_csp(i, TRUE);
2232 vim_free(buf);
2233 return CSCOPE_FAILURE;
2234 }
2235
2236 if (ch != CSCOPE_PROMPT[n])
2237 {
2238 ch = EOF;
2239 break;
2240 }
2241 }
2242
2243 if (ch == EOF)
2244 continue; /* didn't find the prompt */
2245 break; /* did find the prompt */
2246 }
2247
2248 vim_free(buf);
2249 return CSCOPE_SUCCESS;
2250}
2251
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002252#if defined(UNIX) && defined(SIGALRM)
2253/*
2254 * Used to catch and ignore SIGALRM below.
2255 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002256 static RETSIGTYPE
2257sig_handler SIGDEFARG(sigarg)
2258{
2259 /* do nothing */
2260 SIGRETURN;
2261}
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264/*
2265 * PRIVATE: cs_release_csp
2266 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002267 * Does the actual free'ing for the cs ptr with an optional flag of whether
2268 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 */
2270 static void
2271cs_release_csp(i, freefnpp)
2272 int i;
2273 int freefnpp;
2274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /*
2276 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2277 */
2278 if (csinfo[i].to_fp != NULL)
2279 {
2280 (void)fputs("q\n", csinfo[i].to_fp);
2281 (void)fflush(csinfo[i].to_fp);
2282 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002283#if defined(UNIX)
2284 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002285 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002286 int pstat;
2287 pid_t pid;
2288
2289# if defined(HAVE_SIGACTION)
2290 struct sigaction sa, old;
2291
Bram Moolenaar9701da02008-03-16 12:09:58 +00002292 /* Use sigaction() to limit the waiting time to two seconds. */
2293 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002294 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002295# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002296 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002297# else
2298 sa.sa_flags = 0;
2299# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002300 sigaction(SIGALRM, &sa, &old);
2301 alarm(2); /* 2 sec timeout */
2302
2303 /* Block until cscope exits or until timer expires */
2304 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002305 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002306
2307 /* cancel pending alarm if still there and restore signal */
2308 alarm(0);
2309 sigaction(SIGALRM, &old, NULL);
2310# else
2311 int waited;
2312
2313 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2314 * to give cscope a chance to exit quickly. */
2315 sleep(0);
2316 for (waited = 0; waited < 40; ++waited)
2317 {
2318 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002319 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002320 if (pid != 0)
2321 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002322 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002323 }
2324# endif
2325 /*
2326 * If the cscope process is still running: kill it.
2327 * Safety check: If the PID would be zero here, the entire X session
2328 * would be killed. -1 and 1 are dangerous as well.
2329 */
2330 if (pid < 0 && csinfo[i].pid > 1)
2331 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002332# ifdef ECHILD
2333 int alive = TRUE;
2334
2335 if (waitpid_errno == ECHILD)
2336 {
2337 /*
2338 * When using 'vim -g', vim is forked and cscope process is
2339 * no longer a child process but a sibling. So waitpid()
2340 * fails with errno being ECHILD (No child processes).
2341 * Don't send SIGKILL to cscope immediately but wait
2342 * (polling) for it to exit normally as result of sending
2343 * the "q" command, hence giving it a chance to clean up
2344 * its temporary files.
2345 */
2346 int waited;
2347
2348 sleep(0);
2349 for (waited = 0; waited < 40; ++waited)
2350 {
2351 /* Check whether cscope process is still alive */
2352 if (kill(csinfo[i].pid, 0) != 0)
2353 {
2354 alive = FALSE; /* cscope process no longer exists */
2355 break;
2356 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002357 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002358 }
2359 }
2360 if (alive)
2361# endif
2362 {
2363 kill(csinfo[i].pid, SIGKILL);
2364 (void)waitpid(csinfo[i].pid, &pstat, 0);
2365 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002366 }
2367 }
2368#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002369 if (csinfo[i].hProc != NULL)
2370 {
2371 /* Give cscope a chance to exit normally */
2372 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2373 TerminateProcess(csinfo[i].hProc, 0);
2374 CloseHandle(csinfo[i].hProc);
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#endif
2377
2378 if (csinfo[i].fr_fp != NULL)
2379 (void)fclose(csinfo[i].fr_fp);
2380 if (csinfo[i].to_fp != NULL)
2381 (void)fclose(csinfo[i].to_fp);
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (freefnpp)
2384 {
2385 vim_free(csinfo[i].fname);
2386 vim_free(csinfo[i].ppath);
2387 vim_free(csinfo[i].flags);
2388 }
2389
2390 clear_csinfo(i);
2391} /* cs_release_csp */
2392
2393
2394/*
2395 * PRIVATE: cs_reset
2396 *
2397 * calls cs_kill on all cscope connections then reinits
2398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 static int
2400cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002401 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2404 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002405 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002407 if (csinfo_size == 0)
2408 return CSCOPE_SUCCESS;
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002411 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2412 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2413 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (dblist == NULL || pplist == NULL || fllist == NULL)
2415 {
2416 vim_free(dblist);
2417 vim_free(pplist);
2418 vim_free(fllist);
2419 return CSCOPE_FAILURE;
2420 }
2421
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002422 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 dblist[i] = csinfo[i].fname;
2425 pplist[i] = csinfo[i].ppath;
2426 fllist[i] = csinfo[i].flags;
2427 if (csinfo[i].fname != NULL)
2428 cs_release_csp(i, FALSE);
2429 }
2430
2431 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002432 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 if (dblist[i] != NULL)
2435 {
2436 cs_add_common(dblist[i], pplist[i], fllist[i]);
2437 if (p_csverbose)
2438 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002439 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 * connection number in the same line as
2441 * "Added cscope database..."
2442 */
2443 sprintf(buf, " (#%d)", i);
2444 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2445 }
2446 }
2447 vim_free(dblist[i]);
2448 vim_free(pplist[i]);
2449 vim_free(fllist[i]);
2450 }
2451 vim_free(dblist);
2452 vim_free(pplist);
2453 vim_free(fllist);
2454
2455 if (p_csverbose)
2456 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2457 return CSCOPE_SUCCESS;
2458} /* cs_reset */
2459
2460
2461/*
2462 * PRIVATE: cs_resolve_file
2463 *
2464 * construct the full pathname to a file found in the cscope database.
2465 * (Prepends ppath, if there is one and if it's not already prepended,
2466 * otherwise just uses the name found.)
2467 *
2468 * we need to prepend the prefix because on some cscope's (e.g., the one that
2469 * ships with Solaris 2.6), the output never has the prefix prepended.
2470 * contrast this with my development system (Digital Unix), which does.
2471 */
2472 static char *
2473cs_resolve_file(i, name)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002474 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 char *name;
2476{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002477 char *fullname;
2478 int len;
2479 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002482 * Ppath is freed when we destroy the cscope connection.
2483 * Fullname is freed after cs_make_vim_style_matches, after it's been
2484 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002486 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002488 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002489 else if (p_csre && csinfo[i].fname != NULL)
2490 {
2491 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2492 * path in path resolution. */
2493 csdir = alloc(MAXPATHL);
2494 if (csdir != NULL)
2495 {
2496 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
2497 gettail((char_u *)csinfo[i].fname) - 1 - (char_u *)csinfo[i].fname);
2498 len += (int)STRLEN(csdir);
2499 }
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 if ((fullname = (char *)alloc(len)) == NULL)
2503 return NULL;
2504
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002505 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002507 * happens, you are screwed up and need to fix how you're using cscope. */
2508 if (csinfo[i].ppath != NULL
2509 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2510 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002512 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002514 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002516 else if (csdir != NULL && csinfo[i].fname != NULL && STRLEN(csdir) > 0)
2517 {
2518 /* Check for csdir to be non empty to avoid empty path concatenated to
2519 * cscope output. TODO: avoid the unnecessary alloc/free of fullname. */
2520 vim_free(fullname);
2521 fullname = concat_fnames(csdir, (char_u *)name, TRUE);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 else
2524 (void)sprintf(fullname, "%s", name);
2525
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002526 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002528}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530
2531/*
2532 * PRIVATE: cs_show
2533 *
2534 * show all cscope connections
2535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 static int
2537cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002538 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 short i;
2541 if (cs_cnt_connections() == 0)
2542 MSG_PUTS(_("no cscope connections\n"));
2543 else
2544 {
2545 MSG_PUTS_ATTR(
2546 _(" # pid database name prepend path\n"),
2547 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002548 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 if (csinfo[i].fname == NULL)
2551 continue;
2552
2553 if (csinfo[i].ppath != NULL)
2554 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2555 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2556 else
2557 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2558 i, (long)csinfo[i].pid, csinfo[i].fname);
2559 }
2560 }
2561
2562 wait_return(TRUE);
2563 return CSCOPE_SUCCESS;
2564} /* cs_show */
2565
Bram Moolenaar02b06312007-09-06 15:39:22 +00002566
2567/*
2568 * PUBLIC: cs_end
2569 *
2570 * Only called when VIM exits to quit any cscope sessions.
2571 */
2572 void
2573cs_end()
2574{
2575 int i;
2576
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002577 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002578 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002579 vim_free(csinfo);
2580 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002581}
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#endif /* FEAT_CSCOPE */
2584
2585/* the end */