blob: 7a7a66f1c1a6d1e5aa519e568ac604df4995dddd [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 Moolenaar84a05ac2013-05-06 04:24:17 +02001195 /* Send query to all open connections, then count the total number
1196 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001197 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 nummatches[i] = 0;
1199 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001200 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001202 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 continue;
1204
1205 /* send cmd to cscope */
1206 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1207 (void)fflush(csinfo[i].to_fp);
1208
1209 nummatches[i] = cs_cnt_matches(i);
1210
1211 if (nummatches[i] > -1)
1212 totmatches += nummatches[i];
1213
1214 if (nummatches[i] == 0)
1215 (void)cs_read_prompt(i);
1216 }
1217 vim_free(cmd);
1218
1219 if (totmatches == 0)
1220 {
1221 char *nf = _("E259: no matches found for cscope query %s of %s");
1222 char *buf;
1223
1224 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001225 {
1226 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001230 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (buf == NULL)
1232 (void)EMSG(nf);
1233 else
1234 {
1235 sprintf(buf, nf, opt, pat);
1236 (void)EMSG(buf);
1237 vim_free(buf);
1238 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001239 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return FALSE;
1241 }
1242
1243#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001244 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001247 FILE *f;
1248 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001249 qf_info_T *qi = NULL;
1250 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001252 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001253 if (f == NULL)
1254 EMSG2(_(e_notopen), tmp);
1255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001257 cs_file_results(f, nummatches);
1258 fclose(f);
1259 if (use_ll) /* Use location list */
1260 wp = curwin;
1261 /* '-' starts a new error list */
1262 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001263 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001265# ifdef FEAT_WINDOWS
1266 if (postponed_split != 0)
1267 {
1268 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001270 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001271 postponed_split = 0;
1272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001274
1275# ifdef FEAT_AUTOCMD
1276 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1277 curbuf->b_fname, TRUE, curbuf);
1278# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001279 if (use_ll)
1280 /*
1281 * In the location list window, use the displayed location
1282 * list. Otherwise, use the location list for the window.
1283 */
1284 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1285 ? wp->w_llist_ref : wp->w_llist;
1286 qf_jump(qi, 0, 0, forceit);
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 mch_remove(tmp);
1290 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001291 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 return TRUE;
1293 }
1294 else
1295#endif /* FEAT_QUICKFIX */
1296 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001297 char **matches = NULL, **contexts = NULL;
1298 int matched = 0;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /* read output */
1301 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1302 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001303 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (matches == NULL)
1305 return FALSE;
1306
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001307 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1310 }
1311
1312} /* cs_find_common */
1313
1314/*
1315 * PRIVATE: cs_help
1316 *
1317 * print help
1318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 static int
1320cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001321 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 cscmd_T *cmdp = cs_cmds;
1324
1325 (void)MSG_PUTS(_("cscope commands:\n"));
1326 while (cmdp->name != NULL)
1327 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001328 char *help = _(cmdp->help);
1329 int space_cnt = 30 - vim_strsize((char_u *)help);
1330
1331 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1332 if (space_cnt < 0)
1333 space_cnt = 0;
1334 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1335 cmdp->name,
1336 help, space_cnt, " ",
1337 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001339 MSG_PUTS(_("\n"
1340 " c: Find functions calling this function\n"
1341 " d: Find functions called by this function\n"
1342 " e: Find this egrep pattern\n"
1343 " f: Find this file\n"
1344 " g: Find this definition\n"
1345 " i: Find files #including this file\n"
1346 " s: Find this C symbol\n"
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001347 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 cmdp++;
1350 }
1351
1352 wait_return(TRUE);
1353 return 0;
1354} /* cs_help */
1355
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 static void
1358clear_csinfo(i)
1359 int i;
1360{
1361 csinfo[i].fname = NULL;
1362 csinfo[i].ppath = NULL;
1363 csinfo[i].flags = NULL;
1364#if defined(UNIX)
1365 csinfo[i].st_dev = (dev_t)0;
1366 csinfo[i].st_ino = (ino_t)0;
1367#else
1368 csinfo[i].nVolume = 0;
1369 csinfo[i].nIndexHigh = 0;
1370 csinfo[i].nIndexLow = 0;
1371#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001372 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 csinfo[i].fr_fp = NULL;
1374 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001375#if defined(WIN32)
1376 csinfo[i].hProc = NULL;
1377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378}
1379
1380#ifndef UNIX
1381static char *GetWin32Error __ARGS((void));
1382
1383 static char *
1384GetWin32Error()
1385{
1386 char *msg = NULL;
1387 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1388 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1389 if (msg != NULL)
1390 {
1391 /* remove trailing \r\n */
1392 char *pcrlf = strstr(msg, "\r\n");
1393 if (pcrlf != NULL)
1394 *pcrlf = '\0';
1395 }
1396 return msg;
1397}
1398#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400/*
1401 * PRIVATE: cs_insert_filelist
1402 *
1403 * insert a new cscope database filename into the filelist
1404 */
1405 static int
1406cs_insert_filelist(fname, ppath, flags, sb)
1407 char *fname;
1408 char *ppath;
1409 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001410 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 short i, j;
1413#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 BY_HANDLE_FILE_INFORMATION bhfi;
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1417 if (!mch_windows95())
1418 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001419 switch (win32_fileinfo(fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001421 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1422 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (p_csverbose)
1424 {
1425 char *cant_msg = _("E625: cannot open cscope database: %s");
1426 char *winmsg = GetWin32Error();
1427
1428 if (winmsg != NULL)
1429 {
1430 (void)EMSG2(cant_msg, winmsg);
1431 LocalFree(winmsg);
1432 }
1433 else
1434 /* subst filename if can't get error text */
1435 (void)EMSG2(cant_msg, fname);
1436 }
1437 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001438
1439 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (p_csverbose)
1441 (void)EMSG(_("E626: cannot get cscope database information"));
1442 return -1;
1443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445#endif
1446
1447 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001448 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 {
1450 if (csinfo[j].fname != NULL
1451#if defined(UNIX)
1452 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1453#else
1454 /* compare pathnames first */
1455 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001456 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 || (!mch_windows95()
1458 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1459 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1460 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1461#endif
1462 )
1463 {
1464 if (p_csverbose)
1465 (void)EMSG(_("E568: duplicate cscope database not added"));
1466 return -1;
1467 }
1468
1469 if (csinfo[j].fname == NULL && i == -1)
1470 i = j; /* remember first empty entry */
1471 }
1472
1473 if (i == -1)
1474 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001475 i = csinfo_size;
1476 if (csinfo_size == 0)
1477 {
1478 /* First time allocation: allocate only 1 connection. It should
1479 * be enough for most users. If more is needed, csinfo will be
1480 * reallocated. */
1481 csinfo_size = 1;
1482 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1483 }
1484 else
1485 {
1486 /* Reallocate space for more connections. */
1487 csinfo_size *= 2;
1488 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1489 }
1490 if (csinfo == NULL)
1491 return -1;
1492 for (j = csinfo_size/2; j < csinfo_size; j++)
1493 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001496 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return -1;
1498
1499 (void)strcpy(csinfo[i].fname, (const char *)fname);
1500
1501 if (ppath != NULL)
1502 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001503 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 vim_free(csinfo[i].fname);
1506 csinfo[i].fname = NULL;
1507 return -1;
1508 }
1509 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1510 } else
1511 csinfo[i].ppath = NULL;
1512
1513 if (flags != NULL)
1514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001515 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
1517 vim_free(csinfo[i].fname);
1518 vim_free(csinfo[i].ppath);
1519 csinfo[i].fname = NULL;
1520 csinfo[i].ppath = NULL;
1521 return -1;
1522 }
1523 (void)strcpy(csinfo[i].flags, (const char *)flags);
1524 } else
1525 csinfo[i].flags = NULL;
1526
1527#if defined(UNIX)
1528 csinfo[i].st_dev = sb->st_dev;
1529 csinfo[i].st_ino = sb->st_ino;
1530
1531#else
1532 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1533 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1534 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1535#endif
1536 return i;
1537} /* cs_insert_filelist */
1538
1539
1540/*
1541 * PRIVATE: cs_lookup_cmd
1542 *
1543 * find cscope command in command table
1544 */
1545 static cscmd_T *
1546cs_lookup_cmd(eap)
1547 exarg_T *eap;
1548{
1549 cscmd_T *cmdp;
1550 char *stok;
1551 size_t len;
1552
1553 if (eap->arg == NULL)
1554 return NULL;
1555
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001556 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001557 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1560 return NULL;
1561
1562 len = strlen(stok);
1563 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1564 {
1565 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1566 return (cmdp);
1567 }
1568 return NULL;
1569} /* cs_lookup_cmd */
1570
1571
1572/*
1573 * PRIVATE: cs_kill
1574 *
1575 * nuke em
1576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 static int
1578cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001579 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 char *stok;
1582 short i;
1583
1584 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1585 {
1586 cs_usage_msg(Kill);
1587 return CSCOPE_FAILURE;
1588 }
1589
1590 /* only single digit positive and negative integers are allowed */
1591 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1592 || (strlen(stok) < 3 && stok[0] == '-'
1593 && VIM_ISDIGIT((int)(stok[1]))))
1594 i = atoi(stok);
1595 else
1596 {
1597 /* It must be part of a name. We will try to find a match
1598 * within all the names in the csinfo data structure
1599 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001600 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1603 break;
1604 }
1605 }
1606
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001607 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
1609 if (p_csverbose)
1610 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1611 }
1612 else
1613 {
1614 if (i == -1)
1615 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001616 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618 if (csinfo[i].fname)
1619 cs_kill_execute(i, csinfo[i].fname);
1620 }
1621 }
1622 else
1623 cs_kill_execute(i, stok);
1624 }
1625
1626 return 0;
1627} /* cs_kill */
1628
1629
1630/*
1631 * PRIVATE: cs_kill_execute
1632 *
1633 * Actually kills a specific cscope connection.
1634 */
1635 static void
1636cs_kill_execute(i, cname)
1637 int i; /* cscope table index */
1638 char *cname; /* cscope database name */
1639{
1640 if (p_csverbose)
1641 {
1642 msg_clr_eos();
1643 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1644 (char_u *)_("cscope connection %s closed"), cname);
1645 }
1646 cs_release_csp(i, TRUE);
1647}
1648
1649
1650/*
1651 * PRIVATE: cs_make_vim_style_matches
1652 *
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001653 * convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1655 * the type of the tag you are looking for. for example, in Darren Hiebert's
1656 * ctags (the one that comes with vim), #define's use a line number to find the
1657 * tag in a file while function definitions use a regexp search pattern.
1658 *
1659 * i'm going to always use the line number because cscope does something
1660 * quirky (and probably other things i don't know about):
1661 *
1662 * if you have "# define" in your source file, which is
1663 * perfectly legal, cscope thinks you have "#define". this
1664 * will result in a failed regexp search. :(
1665 *
1666 * besides, even if this particular case didn't happen, the search pattern
1667 * would still have to be modified to escape all the special regular expression
1668 * characters to comply with ctags formatting.
1669 */
1670 static char *
1671cs_make_vim_style_matches(fname, slno, search, tagstr)
1672 char *fname;
1673 char *slno;
1674 char *search;
1675 char *tagstr;
1676{
1677 /* vim style is ctags:
1678 *
1679 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1680 *
1681 * but as mentioned above, we'll always use the line number and
1682 * put the search pattern (if one exists) as "extra"
1683 *
1684 * buf is used as part of vim's method of handling tags, and
1685 * (i think) vim frees it when you pop your tags and get replaced
1686 * by new ones on the tag stack.
1687 */
1688 char *buf;
1689 int amt;
1690
1691 if (search != NULL)
1692 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001693 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if ((buf = (char *)alloc(amt)) == NULL)
1695 return NULL;
1696
1697 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1698 }
1699 else
1700 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001701 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if ((buf = (char *)alloc(amt)) == NULL)
1703 return NULL;
1704
1705 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1706 }
1707
1708 return buf;
1709} /* cs_make_vim_style_matches */
1710
1711
1712/*
1713 * PRIVATE: cs_manage_matches
1714 *
1715 * this is kind of hokey, but i don't see an easy way round this..
1716 *
1717 * Store: keep a ptr to the (malloc'd) memory of matches originally
1718 * generated from cs_find(). the matches are originally lines directly
1719 * from cscope output, but transformed to look like something out of a
1720 * ctags. see cs_make_vim_style_matches for more details.
1721 *
1722 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1723 * the next line from the cscope output. it basically keeps track of which
1724 * lines have been "used" and returns the next one.
1725 *
1726 * Free: frees up everything and resets
1727 *
1728 * Print: prints the tags
1729 */
1730 static char *
1731cs_manage_matches(matches, contexts, totmatches, cmd)
1732 char **matches;
1733 char **contexts;
1734 int totmatches;
1735 mcmd_e cmd;
1736{
1737 static char **mp = NULL;
1738 static char **cp = NULL;
1739 static int cnt = -1;
1740 static int next = -1;
1741 char *p = NULL;
1742
1743 switch (cmd)
1744 {
1745 case Store:
1746 assert(matches != NULL);
1747 assert(totmatches > 0);
1748 if (mp != NULL || cp != NULL)
1749 (void)cs_manage_matches(NULL, NULL, -1, Free);
1750 mp = matches;
1751 cp = contexts;
1752 cnt = totmatches;
1753 next = 0;
1754 break;
1755 case Get:
1756 if (next >= cnt)
1757 return NULL;
1758
1759 p = mp[next];
1760 next++;
1761 break;
1762 case Free:
1763 if (mp != NULL)
1764 {
1765 if (cnt > 0)
1766 while (cnt--)
1767 {
1768 vim_free(mp[cnt]);
1769 if (cp != NULL)
1770 vim_free(cp[cnt]);
1771 }
1772 vim_free(mp);
1773 vim_free(cp);
1774 }
1775 mp = NULL;
1776 cp = NULL;
1777 cnt = 0;
1778 next = 0;
1779 break;
1780 case Print:
1781 cs_print_tags_priv(mp, cp, cnt);
1782 break;
1783 default: /* should not reach here */
1784 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1785 return NULL;
1786 }
1787
1788 return p;
1789} /* cs_manage_matches */
1790
1791
1792/*
1793 * PRIVATE: cs_parse_results
1794 *
1795 * parse cscope output
1796 */
1797 static char *
1798cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1799 int cnumber;
1800 char *buf;
1801 int bufsize;
1802 char **context;
1803 char **linenumber;
1804 char **search;
1805{
1806 int ch;
1807 char *p;
1808 char *name;
1809
1810 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1811 {
1812 if (feof(csinfo[cnumber].fr_fp))
1813 errno = EIO;
1814
1815 cs_reading_emsg(cnumber);
1816
1817 return NULL;
1818 }
1819
1820 /* If the line's too long for the buffer, discard it. */
1821 if ((p = strchr(buf, '\n')) == NULL)
1822 {
1823 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1824 ;
1825 return NULL;
1826 }
1827 *p = '\0';
1828
1829 /*
1830 * cscope output is in the following format:
1831 *
1832 * <filename> <context> <line number> <pattern>
1833 */
1834 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1835 return NULL;
1836 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1837 return NULL;
1838 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1839 return NULL;
1840 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1841
1842 /* --- nvi ---
1843 * If the file is older than the cscope database, that is,
1844 * the database was built since the file was last modified,
1845 * or there wasn't a search string, use the line number.
1846 */
1847 if (strcmp(*search, "<unknown>") == 0)
1848 *search = NULL;
1849
1850 name = cs_resolve_file(cnumber, name);
1851 return name;
1852}
1853
Bram Moolenaarc716c302006-01-21 22:12:51 +00001854#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
1856 * PRIVATE: cs_file_results
1857 *
1858 * write cscope find results to file
1859 */
1860 static void
1861cs_file_results(f, nummatches_a)
1862 FILE *f;
1863 int *nummatches_a;
1864{
1865 int i, j;
1866 char *buf;
1867 char *search, *slno;
1868 char *fullname;
1869 char *cntx;
1870 char *context;
1871
1872 buf = (char *)alloc(CSREAD_BUFSIZE);
1873 if (buf == NULL)
1874 return;
1875
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001876 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 if (nummatches_a[i] < 1)
1879 continue;
1880
1881 for (j = 0; j < nummatches_a[i]; j++)
1882 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001883 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1884 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 continue;
1886
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001887 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001888 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 continue;
1890
1891 if (strcmp(cntx, "<global>")==0)
1892 strcpy(context, "<<global>>");
1893 else
1894 sprintf(context, "<<%s>>", cntx);
1895
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001896 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1898 else
1899 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1900
1901 vim_free(context);
1902 vim_free(fullname);
1903 } /* for all matches */
1904
1905 (void)cs_read_prompt(i);
1906
1907 } /* for all cscope connections */
1908 vim_free(buf);
1909}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912/*
1913 * PRIVATE: cs_fill_results
1914 *
1915 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1916 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001917 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
1919 static void
1920cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1921 char *tagstr;
1922 int totmatches;
1923 int *nummatches_a;
1924 char ***matches_p;
1925 char ***cntxts_p;
1926 int *matched;
1927{
1928 int i, j;
1929 char *buf;
1930 char *search, *slno;
1931 int totsofar = 0;
1932 char **matches = NULL;
1933 char **cntxts = NULL;
1934 char *fullname;
1935 char *cntx;
1936
1937 assert(totmatches > 0);
1938
1939 buf = (char *)alloc(CSREAD_BUFSIZE);
1940 if (buf == NULL)
1941 return;
1942
1943 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1944 goto parse_out;
1945 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1946 goto parse_out;
1947
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001948 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 if (nummatches_a[i] < 1)
1951 continue;
1952
1953 for (j = 0; j < nummatches_a[i]; j++)
1954 {
1955 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1956 &slno, &search)) == NULL)
1957 continue;
1958
1959 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1960 search, tagstr);
1961
1962 vim_free(fullname);
1963
1964 if (strcmp(cntx, "<global>") == 0)
1965 cntxts[totsofar] = NULL;
1966 else
1967 /* note: if vim_strsave returns NULL, then the context
1968 * will be "<global>", which is misleading.
1969 */
1970 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1971
1972 if (matches[totsofar] != NULL)
1973 totsofar++;
1974
1975 } /* for all matches */
1976
1977 (void)cs_read_prompt(i);
1978
1979 } /* for all cscope connections */
1980
1981parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001982 if (totsofar == 0)
1983 {
1984 /* No matches, free the arrays and return NULL in "*matches_p". */
1985 vim_free(matches);
1986 matches = NULL;
1987 vim_free(cntxts);
1988 cntxts = NULL;
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 *matched = totsofar;
1991 *matches_p = matches;
1992 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 vim_free(buf);
1995} /* cs_fill_results */
1996
1997
1998/* get the requested path components */
1999 static char *
2000cs_pathcomponents(path)
2001 char *path;
2002{
2003 int i;
2004 char *s;
2005
2006 if (p_cspc == 0)
2007 return path;
2008
2009 s = path + strlen(path) - 1;
2010 for (i = 0; i < p_cspc; ++i)
2011 while (s > path && *--s != '/'
2012#ifdef WIN32
2013 && *--s != '\\'
2014#endif
2015 )
2016 ;
2017 if ((s > path && *s == '/')
2018#ifdef WIN32
2019 || (s > path && *s == '\\')
2020#endif
2021 )
2022 ++s;
2023 return s;
2024}
2025
2026/*
2027 * PRIVATE: cs_print_tags_priv
2028 *
2029 * called from cs_manage_matches()
2030 */
2031 static void
2032cs_print_tags_priv(matches, cntxts, num_matches)
2033 char **matches;
2034 char **cntxts;
2035 int num_matches;
2036{
2037 char *buf = NULL;
2038 int bufsize = 0; /* Track available bufsize */
2039 int newsize = 0;
2040 char *ptag;
2041 char *fname, *lno, *extra, *tbuf;
2042 int i, idx, num;
2043 char *globalcntx = "GLOBAL";
2044 char *cntxformat = " <<%s>>";
2045 char *context;
2046 char *cstag_msg = _("Cscope tag: %s");
2047 char *csfmt_str = "%4d %6s ";
2048
2049 assert (num_matches > 0);
2050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002051 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 return;
2053
2054 strcpy(tbuf, matches[0]);
2055 ptag = strtok(tbuf, "\t");
2056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002057 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 buf = (char *)alloc(newsize);
2059 if (buf != NULL)
2060 {
2061 bufsize = newsize;
2062 (void)sprintf(buf, cstag_msg, ptag);
2063 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2064 }
2065
2066 vim_free(tbuf);
2067
2068 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2069 msg_advance(msg_col + 2);
2070 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2071
2072 num = 1;
2073 for (i = 0; i < num_matches; i++)
2074 {
2075 idx = i;
2076
2077 /* if we really wanted to, we could avoid this malloc and strcpy
2078 * by parsing matches[i] on the fly and placing stuff into buf
2079 * directly, but that's too much of a hassle
2080 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002081 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 continue;
2083 (void)strcpy(tbuf, matches[idx]);
2084
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002085 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 continue;
2087 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2088 continue;
2089 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002090 continue;
2091 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2094
2095 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (bufsize < newsize)
2098 {
2099 buf = (char *)vim_realloc(buf, newsize);
2100 if (buf == NULL)
2101 bufsize = 0;
2102 else
2103 bufsize = newsize;
2104 }
2105 if (buf != NULL)
2106 {
2107 /* csfmt_str = "%4d %6s "; */
2108 (void)sprintf(buf, csfmt_str, num, lno);
2109 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2110 }
2111 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2112
2113 /* compute the required space for the context */
2114 if (cntxts[idx] != NULL)
2115 context = cntxts[idx];
2116 else
2117 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002118 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 if (bufsize < newsize)
2121 {
2122 buf = (char *)vim_realloc(buf, newsize);
2123 if (buf == NULL)
2124 bufsize = 0;
2125 else
2126 bufsize = newsize;
2127 }
2128 if (buf != NULL)
2129 {
2130 (void)sprintf(buf, cntxformat, context);
2131
2132 /* print the context only if it fits on the same line */
2133 if (msg_col + (int)strlen(buf) >= (int)Columns)
2134 msg_putchar('\n');
2135 msg_advance(12);
2136 MSG_PUTS_LONG(buf);
2137 msg_putchar('\n');
2138 }
2139 if (extra != NULL)
2140 {
2141 msg_advance(13);
2142 MSG_PUTS_LONG(extra);
2143 }
2144
2145 vim_free(tbuf); /* only after printing extra due to strtok use */
2146
2147 if (msg_col)
2148 msg_putchar('\n');
2149
2150 ui_breakcheck();
2151 if (got_int)
2152 {
2153 got_int = FALSE; /* don't print any more matches */
2154 break;
2155 }
2156
2157 num++;
2158 } /* for all matches */
2159
2160 vim_free(buf);
2161} /* cs_print_tags_priv */
2162
2163
2164/*
2165 * PRIVATE: cs_read_prompt
2166 *
2167 * read a cscope prompt (basically, skip over the ">> ")
2168 */
2169 static int
2170cs_read_prompt(i)
2171 int i;
2172{
2173 int ch;
2174 char *buf = NULL; /* buffer for possible error message from cscope */
2175 int bufpos = 0;
2176 char *cs_emsg;
2177 int maxlen;
2178 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002179 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 int n;
2181
2182 cs_emsg = _("E609: Cscope error: %s");
2183 /* compute maximum allowed len for Cscope error message */
2184 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2185
2186 for (;;)
2187 {
2188 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2189 /* if there is room and char is printable */
2190 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2191 {
2192 if (buf == NULL) /* lazy buffer allocation */
2193 buf = (char *)alloc(maxlen);
2194 if (buf != NULL)
2195 {
2196 /* append character to the message */
2197 buf[bufpos++] = ch;
2198 buf[bufpos] = NUL;
2199 if (bufpos >= epromptlen
2200 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2201 {
2202 /* remove eprompt from buf */
2203 buf[bufpos - epromptlen] = NUL;
2204
2205 /* print message to user */
2206 (void)EMSG2(cs_emsg, buf);
2207
2208 /* send RETURN to cscope */
2209 (void)putc('\n', csinfo[i].to_fp);
2210 (void)fflush(csinfo[i].to_fp);
2211
2212 /* clear buf */
2213 bufpos = 0;
2214 buf[bufpos] = NUL;
2215 }
2216 }
2217 }
2218
2219 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2220 {
2221 if (n > 0)
2222 ch = getc(csinfo[i].fr_fp);
2223 if (ch == EOF)
2224 {
2225 PERROR("cs_read_prompt EOF");
2226 if (buf != NULL && buf[0] != NUL)
2227 (void)EMSG2(cs_emsg, buf);
2228 else if (p_csverbose)
2229 cs_reading_emsg(i); /* don't have additional information */
2230 cs_release_csp(i, TRUE);
2231 vim_free(buf);
2232 return CSCOPE_FAILURE;
2233 }
2234
2235 if (ch != CSCOPE_PROMPT[n])
2236 {
2237 ch = EOF;
2238 break;
2239 }
2240 }
2241
2242 if (ch == EOF)
2243 continue; /* didn't find the prompt */
2244 break; /* did find the prompt */
2245 }
2246
2247 vim_free(buf);
2248 return CSCOPE_SUCCESS;
2249}
2250
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002251#if defined(UNIX) && defined(SIGALRM)
2252/*
2253 * Used to catch and ignore SIGALRM below.
2254 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255 static RETSIGTYPE
2256sig_handler SIGDEFARG(sigarg)
2257{
2258 /* do nothing */
2259 SIGRETURN;
2260}
2261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263/*
2264 * PRIVATE: cs_release_csp
2265 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002266 * Does the actual free'ing for the cs ptr with an optional flag of whether
2267 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269 static void
2270cs_release_csp(i, freefnpp)
2271 int i;
2272 int freefnpp;
2273{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /*
2275 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2276 */
2277 if (csinfo[i].to_fp != NULL)
2278 {
2279 (void)fputs("q\n", csinfo[i].to_fp);
2280 (void)fflush(csinfo[i].to_fp);
2281 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002282#if defined(UNIX)
2283 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002284 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002285 int pstat;
2286 pid_t pid;
2287
2288# if defined(HAVE_SIGACTION)
2289 struct sigaction sa, old;
2290
Bram Moolenaar9701da02008-03-16 12:09:58 +00002291 /* Use sigaction() to limit the waiting time to two seconds. */
2292 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002293 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002294# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002295 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002296# else
2297 sa.sa_flags = 0;
2298# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002299 sigaction(SIGALRM, &sa, &old);
2300 alarm(2); /* 2 sec timeout */
2301
2302 /* Block until cscope exits or until timer expires */
2303 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002304 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002305
2306 /* cancel pending alarm if still there and restore signal */
2307 alarm(0);
2308 sigaction(SIGALRM, &old, NULL);
2309# else
2310 int waited;
2311
2312 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2313 * to give cscope a chance to exit quickly. */
2314 sleep(0);
2315 for (waited = 0; waited < 40; ++waited)
2316 {
2317 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002318 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002319 if (pid != 0)
2320 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002321 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002322 }
2323# endif
2324 /*
2325 * If the cscope process is still running: kill it.
2326 * Safety check: If the PID would be zero here, the entire X session
2327 * would be killed. -1 and 1 are dangerous as well.
2328 */
2329 if (pid < 0 && csinfo[i].pid > 1)
2330 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002331# ifdef ECHILD
2332 int alive = TRUE;
2333
2334 if (waitpid_errno == ECHILD)
2335 {
2336 /*
2337 * When using 'vim -g', vim is forked and cscope process is
2338 * no longer a child process but a sibling. So waitpid()
2339 * fails with errno being ECHILD (No child processes).
2340 * Don't send SIGKILL to cscope immediately but wait
2341 * (polling) for it to exit normally as result of sending
2342 * the "q" command, hence giving it a chance to clean up
2343 * its temporary files.
2344 */
2345 int waited;
2346
2347 sleep(0);
2348 for (waited = 0; waited < 40; ++waited)
2349 {
2350 /* Check whether cscope process is still alive */
2351 if (kill(csinfo[i].pid, 0) != 0)
2352 {
2353 alive = FALSE; /* cscope process no longer exists */
2354 break;
2355 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002356 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002357 }
2358 }
2359 if (alive)
2360# endif
2361 {
2362 kill(csinfo[i].pid, SIGKILL);
2363 (void)waitpid(csinfo[i].pid, &pstat, 0);
2364 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002365 }
2366 }
2367#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002368 if (csinfo[i].hProc != NULL)
2369 {
2370 /* Give cscope a chance to exit normally */
2371 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2372 TerminateProcess(csinfo[i].hProc, 0);
2373 CloseHandle(csinfo[i].hProc);
2374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375#endif
2376
2377 if (csinfo[i].fr_fp != NULL)
2378 (void)fclose(csinfo[i].fr_fp);
2379 if (csinfo[i].to_fp != NULL)
2380 (void)fclose(csinfo[i].to_fp);
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (freefnpp)
2383 {
2384 vim_free(csinfo[i].fname);
2385 vim_free(csinfo[i].ppath);
2386 vim_free(csinfo[i].flags);
2387 }
2388
2389 clear_csinfo(i);
2390} /* cs_release_csp */
2391
2392
2393/*
2394 * PRIVATE: cs_reset
2395 *
2396 * calls cs_kill on all cscope connections then reinits
2397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 static int
2399cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002400 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2403 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002404 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002406 if (csinfo_size == 0)
2407 return CSCOPE_SUCCESS;
2408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002410 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2411 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2412 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (dblist == NULL || pplist == NULL || fllist == NULL)
2414 {
2415 vim_free(dblist);
2416 vim_free(pplist);
2417 vim_free(fllist);
2418 return CSCOPE_FAILURE;
2419 }
2420
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002421 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 dblist[i] = csinfo[i].fname;
2424 pplist[i] = csinfo[i].ppath;
2425 fllist[i] = csinfo[i].flags;
2426 if (csinfo[i].fname != NULL)
2427 cs_release_csp(i, FALSE);
2428 }
2429
2430 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002431 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 if (dblist[i] != NULL)
2434 {
2435 cs_add_common(dblist[i], pplist[i], fllist[i]);
2436 if (p_csverbose)
2437 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002438 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * connection number in the same line as
2440 * "Added cscope database..."
2441 */
2442 sprintf(buf, " (#%d)", i);
2443 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2444 }
2445 }
2446 vim_free(dblist[i]);
2447 vim_free(pplist[i]);
2448 vim_free(fllist[i]);
2449 }
2450 vim_free(dblist);
2451 vim_free(pplist);
2452 vim_free(fllist);
2453
2454 if (p_csverbose)
2455 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2456 return CSCOPE_SUCCESS;
2457} /* cs_reset */
2458
2459
2460/*
2461 * PRIVATE: cs_resolve_file
2462 *
2463 * construct the full pathname to a file found in the cscope database.
2464 * (Prepends ppath, if there is one and if it's not already prepended,
2465 * otherwise just uses the name found.)
2466 *
2467 * we need to prepend the prefix because on some cscope's (e.g., the one that
2468 * ships with Solaris 2.6), the output never has the prefix prepended.
2469 * contrast this with my development system (Digital Unix), which does.
2470 */
2471 static char *
2472cs_resolve_file(i, name)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002473 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char *name;
2475{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002476 char *fullname;
2477 int len;
2478 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002481 * Ppath is freed when we destroy the cscope connection.
2482 * Fullname is freed after cs_make_vim_style_matches, after it's been
2483 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002485 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002487 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002488 else if (p_csre && csinfo[i].fname != NULL)
2489 {
2490 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2491 * path in path resolution. */
2492 csdir = alloc(MAXPATHL);
2493 if (csdir != NULL)
2494 {
2495 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
2496 gettail((char_u *)csinfo[i].fname) - 1 - (char_u *)csinfo[i].fname);
2497 len += (int)STRLEN(csdir);
2498 }
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 if ((fullname = (char *)alloc(len)) == NULL)
2502 return NULL;
2503
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002504 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002506 * happens, you are screwed up and need to fix how you're using cscope. */
2507 if (csinfo[i].ppath != NULL
2508 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2509 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002511 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002513 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002515 else if (csdir != NULL && csinfo[i].fname != NULL && STRLEN(csdir) > 0)
2516 {
2517 /* Check for csdir to be non empty to avoid empty path concatenated to
2518 * cscope output. TODO: avoid the unnecessary alloc/free of fullname. */
2519 vim_free(fullname);
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002520 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 else
2523 (void)sprintf(fullname, "%s", name);
2524
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002525 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529
2530/*
2531 * PRIVATE: cs_show
2532 *
2533 * show all cscope connections
2534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 static int
2536cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002537 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 short i;
2540 if (cs_cnt_connections() == 0)
2541 MSG_PUTS(_("no cscope connections\n"));
2542 else
2543 {
2544 MSG_PUTS_ATTR(
2545 _(" # pid database name prepend path\n"),
2546 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002547 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 if (csinfo[i].fname == NULL)
2550 continue;
2551
2552 if (csinfo[i].ppath != NULL)
2553 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2554 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2555 else
2556 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2557 i, (long)csinfo[i].pid, csinfo[i].fname);
2558 }
2559 }
2560
2561 wait_return(TRUE);
2562 return CSCOPE_SUCCESS;
2563} /* cs_show */
2564
Bram Moolenaar02b06312007-09-06 15:39:22 +00002565
2566/*
2567 * PUBLIC: cs_end
2568 *
2569 * Only called when VIM exits to quit any cscope sessions.
2570 */
2571 void
2572cs_end()
2573{
2574 int i;
2575
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002576 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002577 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002578 vim_free(csinfo);
2579 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002580}
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#endif /* FEAT_CSCOPE */
2583
2584/* the end */