blob: f72a96b9ce96bfa32bcce47300ab15f2cb8dd343 [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 Moolenaarcab465a2013-06-12 21:25:23 +0200542#ifdef FEAT_MODIFY_FNAME
543 int len;
544 int usedlen = 0;
545 char_u *fbuf = NULL;
546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000549 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 goto add_err;
551
552 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200553#ifdef FEAT_MODIFY_FNAME
554 len = (int)STRLEN(fname);
555 fbuf = (char_u *)fname;
556 (void)modify_fname((char_u *)":p", &usedlen,
557 (char_u **)&fname, &fbuf, &len);
558 if (fname == NULL)
559 goto add_err;
560 fname = (char *)vim_strnsave((char_u *)fname, len);
561 vim_free(fbuf);
562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 ret = stat(fname, &statbuf);
564 if (ret < 0)
565 {
566staterr:
567 if (p_csverbose)
568 cs_stat_emsg(fname);
569 goto add_err;
570 }
571
572 /* get the prepend path (arg2), expand it, and try to stat it */
573 if (arg2 != NULL)
574 {
575 struct stat statbuf2;
576
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000577 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 goto add_err;
579
580 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
581 ret = stat(ppath, &statbuf2);
582 if (ret < 0)
583 goto staterr;
584 }
585
586 /* if filename is a directory, append the cscope database name to it */
587 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
588 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000589 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (fname2 == NULL)
591 goto add_err;
592
593 while (fname[strlen(fname)-1] == '/'
594#ifdef WIN32
595 || fname[strlen(fname)-1] == '\\'
596#endif
597 )
598 {
599 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200600 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 break;
602 }
603 if (fname[0] == '\0')
604 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
605 else
606 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
607
608 ret = stat(fname2, &statbuf);
609 if (ret < 0)
610 {
611 if (p_csverbose)
612 cs_stat_emsg(fname2);
613 goto add_err;
614 }
615
616 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
617 }
618#if defined(UNIX)
619 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
620#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000621 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
623#endif
624 {
625 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
626 }
627 else
628 {
629 if (p_csverbose)
630 (void)EMSG2(
631 _("E564: %s is not a directory or a valid cscope database"),
632 fname);
633 goto add_err;
634 }
635
636 if (i != -1)
637 {
638 if (cs_create_connection(i) == CSCOPE_FAILURE
639 || cs_read_prompt(i) == CSCOPE_FAILURE)
640 {
641 cs_release_csp(i, TRUE);
642 goto add_err;
643 }
644
645 if (p_csverbose)
646 {
647 msg_clr_eos();
648 (void)smsg_attr(hl_attr(HLF_R),
649 (char_u *)_("Added cscope database %s"),
650 csinfo[i].fname);
651 }
652 }
653
654 vim_free(fname);
655 vim_free(fname2);
656 vim_free(ppath);
657 return CSCOPE_SUCCESS;
658
659add_err:
660 vim_free(fname2);
661 vim_free(fname);
662 vim_free(ppath);
663 return CSCOPE_FAILURE;
664} /* cs_add_common */
665
666
667 static int
668cs_check_for_connections()
669{
670 return (cs_cnt_connections() > 0);
671} /* cs_check_for_connections */
672
673
674 static int
675cs_check_for_tags()
676{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000677 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678} /* cs_check_for_tags */
679
680
681/*
682 * PRIVATE: cs_cnt_connections
683 *
684 * count the number of cscope connections
685 */
686 static int
687cs_cnt_connections()
688{
689 short i;
690 short cnt = 0;
691
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000692 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
694 if (csinfo[i].fname != NULL)
695 cnt++;
696 }
697 return cnt;
698} /* cs_cnt_connections */
699
700 static void
701cs_reading_emsg(idx)
702 int idx; /* connection index */
703{
704 EMSGN(_("E262: error reading cscope connection %ld"), idx);
705}
706
707#define CSREAD_BUFSIZE 2048
708/*
709 * PRIVATE: cs_cnt_matches
710 *
711 * count the number of matches for a given cscope connection.
712 */
713 static int
714cs_cnt_matches(idx)
715 int idx;
716{
717 char *stok;
718 char *buf;
719 int nlines;
720
721 buf = (char *)alloc(CSREAD_BUFSIZE);
722 if (buf == NULL)
723 return 0;
724 for (;;)
725 {
726 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
727 {
728 if (feof(csinfo[idx].fr_fp))
729 errno = EIO;
730
731 cs_reading_emsg(idx);
732
733 vim_free(buf);
734 return -1;
735 }
736
737 /*
738 * If the database is out of date, or there's some other problem,
739 * cscope will output error messages before the number-of-lines output.
740 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000741 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 if ((stok = strtok(buf, (const char *)" ")) == NULL)
744 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000745 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 continue;
747
748 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
749 continue;
750 nlines = atoi(stok);
751 if (nlines < 0)
752 {
753 nlines = 0;
754 break;
755 }
756
757 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
758 continue;
759 if (strncmp((const char *)stok, "lines", 5))
760 continue;
761
762 break;
763 }
764
765 vim_free(buf);
766 return nlines;
767} /* cs_cnt_matches */
768
769
770/*
771 * PRIVATE: cs_create_cmd
772 *
773 * Creates the actual cscope command query from what the user entered.
774 */
775 static char *
776cs_create_cmd(csoption, pattern)
777 char *csoption;
778 char *pattern;
779{
780 char *cmd;
781 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000782 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784 switch (csoption[0])
785 {
786 case '0' : case 's' :
787 search = 0;
788 break;
789 case '1' : case 'g' :
790 search = 1;
791 break;
792 case '2' : case 'd' :
793 search = 2;
794 break;
795 case '3' : case 'c' :
796 search = 3;
797 break;
798 case '4' : case 't' :
799 search = 4;
800 break;
801 case '6' : case 'e' :
802 search = 6;
803 break;
804 case '7' : case 'f' :
805 search = 7;
806 break;
807 case '8' : case 'i' :
808 search = 8;
809 break;
810 default :
811 (void)EMSG(_("E561: unknown cscope search type"));
812 cs_usage_msg(Find);
813 return NULL;
814 }
815
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000816 /* Skip white space before the patter, except for text and pattern search,
817 * they may want to use the leading white space. */
818 pat = pattern;
819 if (search != 4 && search != 6)
820 while vim_iswhite(*pat)
821 ++pat;
822
823 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 return NULL;
825
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000826 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 return cmd;
829} /* cs_create_cmd */
830
831
832/*
833 * PRIVATE: cs_create_connection
834 *
835 * This piece of code was taken/adapted from nvi. do we need to add
836 * the BSD license notice?
837 */
838 static int
839cs_create_connection(i)
840 int i;
841{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000842#ifdef UNIX
843 int to_cs[2], from_cs[2];
844#endif
845 int len;
846 char *prog, *cmd, *ppath = NULL;
847#ifdef WIN32
848 int fd;
849 SECURITY_ATTRIBUTES sa;
850 PROCESS_INFORMATION pi;
851 STARTUPINFO si;
852 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
853 HANDLE stdin_rd, stdout_rd;
854 HANDLE stdout_wr, stdin_wr;
855 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000856# ifdef __BORLANDC__
857# define OPEN_OH_ARGTYPE long
858# else
859# if (_MSC_VER >= 1300)
860# define OPEN_OH_ARGTYPE intptr_t
861# else
862# define OPEN_OH_ARGTYPE long
863# endif
864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866
Bram Moolenaar02b06312007-09-06 15:39:22 +0000867#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 /*
869 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
870 * from_cs[0] and writes to to_cs[1].
871 */
872 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
873 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
874 {
875 (void)EMSG(_("E566: Could not create cscope pipes"));
876err_closing:
877 if (to_cs[0] != -1)
878 (void)close(to_cs[0]);
879 if (to_cs[1] != -1)
880 (void)close(to_cs[1]);
881 if (from_cs[0] != -1)
882 (void)close(from_cs[0]);
883 if (from_cs[1] != -1)
884 (void)close(from_cs[1]);
885 return CSCOPE_FAILURE;
886 }
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 switch (csinfo[i].pid = fork())
889 {
890 case -1:
891 (void)EMSG(_("E622: Could not fork for cscope"));
892 goto err_closing;
893 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (dup2(to_cs[0], STDIN_FILENO) == -1)
895 PERROR("cs_create_connection 1");
896 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
897 PERROR("cs_create_connection 2");
898 if (dup2(from_cs[1], STDERR_FILENO) == -1)
899 PERROR("cs_create_connection 3");
900
901 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 (void)close(to_cs[1]);
903 (void)close(from_cs[0]);
904#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000905 /* WIN32 */
906 /* Create pipes to communicate with cscope */
907 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
908 sa.bInheritHandle = TRUE;
909 sa.lpSecurityDescriptor = NULL;
910
911 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
912 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
913 {
914 (void)EMSG(_("E566: Could not create cscope pipes"));
915err_closing:
916 if (pipe_stdin)
917 {
918 CloseHandle(stdin_rd);
919 CloseHandle(stdin_wr);
920 }
921 if (pipe_stdout)
922 {
923 CloseHandle(stdout_rd);
924 CloseHandle(stdout_wr);
925 }
926 return CSCOPE_FAILURE;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#endif
929 /* expand the cscope exec for env var's */
930 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
931 {
932#ifdef UNIX
933 return CSCOPE_FAILURE;
934#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000935 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 goto err_closing;
937#endif
938 }
939 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
940
941 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000942 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (csinfo[i].ppath)
944 {
945 /* expand the prepend path for env var's */
946 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
947 {
948 vim_free(prog);
949#ifdef UNIX
950 return CSCOPE_FAILURE;
951#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000952 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 goto err_closing;
954#endif
955 }
956 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
957
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000958 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960
961 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000962 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964 if ((cmd = (char *)alloc(len)) == NULL)
965 {
966 vim_free(prog);
967 vim_free(ppath);
968#ifdef UNIX
969 return CSCOPE_FAILURE;
970#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000971 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 goto err_closing;
973#endif
974 }
975
976 /* run the cscope command; is there execl for non-unix systems? */
977#if defined(UNIX)
978 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
979#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000980 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
982#endif
983 if (csinfo[i].ppath != NULL)
984 {
985 (void)strcat(cmd, " -P");
986 (void)strcat(cmd, csinfo[i].ppath);
987 }
988 if (csinfo[i].flags != NULL)
989 {
990 (void)strcat(cmd, " ");
991 (void)strcat(cmd, csinfo[i].flags);
992 }
993# ifdef UNIX
994 /* on Win32 we still need prog */
995 vim_free(prog);
996# endif
997 vim_free(ppath);
998
999#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +02001000# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
1001 /* Change our process group to avoid cscope receiving SIGWINCH. */
1002# if defined(HAVE_SETSID)
1003 (void)setsid();
1004# else
1005 if (setpgid(0, 0) == -1)
1006 PERROR(_("cs_create_connection setpgid failed"));
1007# endif
1008# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +00001009 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 PERROR(_("cs_create_connection exec failed"));
1011
1012 exit(127);
1013 /* NOTREACHED */
1014 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 /*
1016 * Save the file descriptors for later duplication, and
1017 * reopen as streams.
1018 */
1019 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1020 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1021 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1022 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 /* close unused */
1025 (void)close(to_cs[0]);
1026 (void)close(from_cs[1]);
1027
1028 break;
1029 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001032 /* WIN32 */
1033 /* Create a new process to run cscope and use pipes to talk with it */
1034 GetStartupInfo(&si);
1035 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1036 si.wShowWindow = SW_HIDE; /* Hide child application window */
1037 si.hStdOutput = stdout_wr;
1038 si.hStdError = stdout_wr;
1039 si.hStdInput = stdin_rd;
1040 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1041 NULL, NULL, &si, &pi);
1042 vim_free(prog);
1043 vim_free(cmd);
1044
1045 if (!created)
1046 {
1047 PERROR(_("cs_create_connection exec failed"));
1048 (void)EMSG(_("E623: Could not spawn cscope process"));
1049 goto err_closing;
1050 }
1051 /* else */
1052 csinfo[i].pid = pi.dwProcessId;
1053 csinfo[i].hProc = pi.hProcess;
1054 CloseHandle(pi.hThread);
1055
1056 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001057 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1058 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001059 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1060 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001061 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1062 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001063 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1064 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1065
1066 /* Close handles for file descriptors inherited by the cscope process */
1067 CloseHandle(stdin_rd);
1068 CloseHandle(stdout_wr);
1069
1070#endif /* !UNIX */
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return CSCOPE_SUCCESS;
1073} /* cs_create_connection */
1074
1075
1076/*
1077 * PRIVATE: cs_find
1078 *
1079 * query cscope using command line interface. parse the output and use tselect
1080 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1081 *
1082 * returns TRUE if we jump to a tag or abort, FALSE if not.
1083 */
1084 static int
1085cs_find(eap)
1086 exarg_T *eap;
1087{
1088 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001089 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 if (cs_check_for_connections() == FALSE)
1092 {
1093 (void)EMSG(_("E567: no cscope connections"));
1094 return FALSE;
1095 }
1096
1097 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1098 {
1099 cs_usage_msg(Find);
1100 return FALSE;
1101 }
1102
1103 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001104 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 cs_usage_msg(Find);
1107 return FALSE;
1108 }
1109
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001110 /*
1111 * Let's replace the NULs written by strtok() with spaces - we need the
1112 * spaces to correctly display the quickfix/location list window's title.
1113 */
1114 for (i = 0; i < eap_arg_len; ++i)
1115 if (NUL == eap->arg[i])
1116 eap->arg[i] = ' ';
1117
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001118 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001119 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120} /* cs_find */
1121
1122
1123/*
1124 * PRIVATE: cs_find_common
1125 *
1126 * common code for cscope find, shared by cs_find() and do_cstag()
1127 */
1128 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001129cs_find_common(opt, pat, forceit, verbose, use_ll, cmdline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 char *opt;
1131 char *pat;
1132 int forceit;
1133 int verbose;
Bram Moolenaard96c52c2014-12-13 03:20:14 +01001134 int use_ll UNUSED;
1135 char_u *cmdline UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 int i;
1138 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001139 int *nummatches;
1140 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#ifdef FEAT_QUICKFIX
1142 char cmdletter;
1143 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001144
1145 /* get cmd letter */
1146 switch (opt[0])
1147 {
1148 case '0' :
1149 cmdletter = 's';
1150 break;
1151 case '1' :
1152 cmdletter = 'g';
1153 break;
1154 case '2' :
1155 cmdletter = 'd';
1156 break;
1157 case '3' :
1158 cmdletter = 'c';
1159 break;
1160 case '4' :
1161 cmdletter = 't';
1162 break;
1163 case '6' :
1164 cmdletter = 'e';
1165 break;
1166 case '7' :
1167 cmdletter = 'f';
1168 break;
1169 case '8' :
1170 cmdletter = 'i';
1171 break;
1172 default :
1173 cmdletter = opt[0];
1174 }
1175
1176 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1177 if (qfpos != NULL)
1178 {
1179 qfpos++;
1180 /* next symbol must be + or - */
1181 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1182 {
1183 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1184 char *buf = (char *)alloc((unsigned)strlen(nf));
1185
1186 /* strlen will be enough because we use chars */
1187 if (buf != NULL)
1188 {
1189 sprintf(buf, nf, *qfpos, *(qfpos-1));
1190 (void)EMSG(buf);
1191 vim_free(buf);
1192 }
1193 return FALSE;
1194 }
1195
1196# ifdef FEAT_AUTOCMD
1197 if (*qfpos != '0')
1198 {
1199 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1200 curbuf->b_fname, TRUE, curbuf);
1201# ifdef FEAT_EVAL
1202 if (did_throw || force_abort)
1203 return FALSE;
1204# endif
1205 }
1206# endif
1207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#endif
1209
1210 /* create the actual command to send to cscope */
1211 cmd = cs_create_cmd(opt, pat);
1212 if (cmd == NULL)
1213 return FALSE;
1214
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001215 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1216 if (nummatches == NULL)
1217 return FALSE;
1218
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001219 /* Send query to all open connections, then count the total number
1220 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001221 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 nummatches[i] = 0;
1223 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001224 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001226 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 continue;
1228
1229 /* send cmd to cscope */
1230 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1231 (void)fflush(csinfo[i].to_fp);
1232
1233 nummatches[i] = cs_cnt_matches(i);
1234
1235 if (nummatches[i] > -1)
1236 totmatches += nummatches[i];
1237
1238 if (nummatches[i] == 0)
1239 (void)cs_read_prompt(i);
1240 }
1241 vim_free(cmd);
1242
1243 if (totmatches == 0)
1244 {
1245 char *nf = _("E259: no matches found for cscope query %s of %s");
1246 char *buf;
1247
1248 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001249 {
1250 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001254 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (buf == NULL)
1256 (void)EMSG(nf);
1257 else
1258 {
1259 sprintf(buf, nf, opt, pat);
1260 (void)EMSG(buf);
1261 vim_free(buf);
1262 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001263 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return FALSE;
1265 }
1266
1267#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001268 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001271 FILE *f;
1272 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001273 qf_info_T *qi = NULL;
1274 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001276 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001277 if (f == NULL)
1278 EMSG2(_(e_notopen), tmp);
1279 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001281 cs_file_results(f, nummatches);
1282 fclose(f);
1283 if (use_ll) /* Use location list */
1284 wp = curwin;
1285 /* '-' starts a new error list */
1286 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001287 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001289# ifdef FEAT_WINDOWS
1290 if (postponed_split != 0)
1291 {
1292 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001294 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001295 postponed_split = 0;
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001298
1299# ifdef FEAT_AUTOCMD
1300 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1301 curbuf->b_fname, TRUE, curbuf);
1302# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001303 if (use_ll)
1304 /*
1305 * In the location list window, use the displayed location
1306 * list. Otherwise, use the location list for the window.
1307 */
1308 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1309 ? wp->w_llist_ref : wp->w_llist;
1310 qf_jump(qi, 0, 0, forceit);
1311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 mch_remove(tmp);
1314 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001315 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return TRUE;
1317 }
1318 else
1319#endif /* FEAT_QUICKFIX */
1320 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001321 char **matches = NULL, **contexts = NULL;
1322 int matched = 0;
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 /* read output */
1325 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1326 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001327 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (matches == NULL)
1329 return FALSE;
1330
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001331 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1334 }
1335
1336} /* cs_find_common */
1337
1338/*
1339 * PRIVATE: cs_help
1340 *
1341 * print help
1342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 static int
1344cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001345 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
1347 cscmd_T *cmdp = cs_cmds;
1348
1349 (void)MSG_PUTS(_("cscope commands:\n"));
1350 while (cmdp->name != NULL)
1351 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001352 char *help = _(cmdp->help);
1353 int space_cnt = 30 - vim_strsize((char_u *)help);
1354
1355 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1356 if (space_cnt < 0)
1357 space_cnt = 0;
1358 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1359 cmdp->name,
1360 help, space_cnt, " ",
1361 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001363 MSG_PUTS(_("\n"
1364 " c: Find functions calling this function\n"
1365 " d: Find functions called by this function\n"
1366 " e: Find this egrep pattern\n"
1367 " f: Find this file\n"
1368 " g: Find this definition\n"
1369 " i: Find files #including this file\n"
1370 " s: Find this C symbol\n"
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001371 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 cmdp++;
1374 }
1375
1376 wait_return(TRUE);
1377 return 0;
1378} /* cs_help */
1379
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 static void
1382clear_csinfo(i)
1383 int i;
1384{
1385 csinfo[i].fname = NULL;
1386 csinfo[i].ppath = NULL;
1387 csinfo[i].flags = NULL;
1388#if defined(UNIX)
1389 csinfo[i].st_dev = (dev_t)0;
1390 csinfo[i].st_ino = (ino_t)0;
1391#else
1392 csinfo[i].nVolume = 0;
1393 csinfo[i].nIndexHigh = 0;
1394 csinfo[i].nIndexLow = 0;
1395#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001396 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 csinfo[i].fr_fp = NULL;
1398 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001399#if defined(WIN32)
1400 csinfo[i].hProc = NULL;
1401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402}
1403
1404#ifndef UNIX
1405static char *GetWin32Error __ARGS((void));
1406
1407 static char *
1408GetWin32Error()
1409{
1410 char *msg = NULL;
1411 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1412 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1413 if (msg != NULL)
1414 {
1415 /* remove trailing \r\n */
1416 char *pcrlf = strstr(msg, "\r\n");
1417 if (pcrlf != NULL)
1418 *pcrlf = '\0';
1419 }
1420 return msg;
1421}
1422#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * PRIVATE: cs_insert_filelist
1426 *
1427 * insert a new cscope database filename into the filelist
1428 */
1429 static int
1430cs_insert_filelist(fname, ppath, flags, sb)
1431 char *fname;
1432 char *ppath;
1433 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001434 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 short i, j;
1437#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 BY_HANDLE_FILE_INFORMATION bhfi;
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1441 if (!mch_windows95())
1442 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001443 switch (win32_fileinfo(fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001445 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1446 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (p_csverbose)
1448 {
1449 char *cant_msg = _("E625: cannot open cscope database: %s");
1450 char *winmsg = GetWin32Error();
1451
1452 if (winmsg != NULL)
1453 {
1454 (void)EMSG2(cant_msg, winmsg);
1455 LocalFree(winmsg);
1456 }
1457 else
1458 /* subst filename if can't get error text */
1459 (void)EMSG2(cant_msg, fname);
1460 }
1461 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001462
1463 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (p_csverbose)
1465 (void)EMSG(_("E626: cannot get cscope database information"));
1466 return -1;
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469#endif
1470
1471 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001472 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
1474 if (csinfo[j].fname != NULL
1475#if defined(UNIX)
1476 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1477#else
1478 /* compare pathnames first */
1479 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001480 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 || (!mch_windows95()
1482 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1483 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1484 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1485#endif
1486 )
1487 {
1488 if (p_csverbose)
1489 (void)EMSG(_("E568: duplicate cscope database not added"));
1490 return -1;
1491 }
1492
1493 if (csinfo[j].fname == NULL && i == -1)
1494 i = j; /* remember first empty entry */
1495 }
1496
1497 if (i == -1)
1498 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001499 i = csinfo_size;
1500 if (csinfo_size == 0)
1501 {
1502 /* First time allocation: allocate only 1 connection. It should
1503 * be enough for most users. If more is needed, csinfo will be
1504 * reallocated. */
1505 csinfo_size = 1;
1506 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1507 }
1508 else
1509 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001510 csinfo_T *t_csinfo = csinfo;
1511
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001512 /* Reallocate space for more connections. */
1513 csinfo_size *= 2;
1514 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001515 if (csinfo == NULL)
1516 {
1517 vim_free(t_csinfo);
1518 csinfo_size = 0;
1519 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001520 }
1521 if (csinfo == NULL)
1522 return -1;
1523 for (j = csinfo_size/2; j < csinfo_size; j++)
1524 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001527 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 return -1;
1529
1530 (void)strcpy(csinfo[i].fname, (const char *)fname);
1531
1532 if (ppath != NULL)
1533 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001534 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 vim_free(csinfo[i].fname);
1537 csinfo[i].fname = NULL;
1538 return -1;
1539 }
1540 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1541 } else
1542 csinfo[i].ppath = NULL;
1543
1544 if (flags != NULL)
1545 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001546 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548 vim_free(csinfo[i].fname);
1549 vim_free(csinfo[i].ppath);
1550 csinfo[i].fname = NULL;
1551 csinfo[i].ppath = NULL;
1552 return -1;
1553 }
1554 (void)strcpy(csinfo[i].flags, (const char *)flags);
1555 } else
1556 csinfo[i].flags = NULL;
1557
1558#if defined(UNIX)
1559 csinfo[i].st_dev = sb->st_dev;
1560 csinfo[i].st_ino = sb->st_ino;
1561
1562#else
1563 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1564 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1565 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1566#endif
1567 return i;
1568} /* cs_insert_filelist */
1569
1570
1571/*
1572 * PRIVATE: cs_lookup_cmd
1573 *
1574 * find cscope command in command table
1575 */
1576 static cscmd_T *
1577cs_lookup_cmd(eap)
1578 exarg_T *eap;
1579{
1580 cscmd_T *cmdp;
1581 char *stok;
1582 size_t len;
1583
1584 if (eap->arg == NULL)
1585 return NULL;
1586
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001587 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001588 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1591 return NULL;
1592
1593 len = strlen(stok);
1594 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1595 {
1596 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1597 return (cmdp);
1598 }
1599 return NULL;
1600} /* cs_lookup_cmd */
1601
1602
1603/*
1604 * PRIVATE: cs_kill
1605 *
1606 * nuke em
1607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 static int
1609cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001610 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 char *stok;
1613 short i;
1614
1615 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1616 {
1617 cs_usage_msg(Kill);
1618 return CSCOPE_FAILURE;
1619 }
1620
1621 /* only single digit positive and negative integers are allowed */
1622 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1623 || (strlen(stok) < 3 && stok[0] == '-'
1624 && VIM_ISDIGIT((int)(stok[1]))))
1625 i = atoi(stok);
1626 else
1627 {
1628 /* It must be part of a name. We will try to find a match
1629 * within all the names in the csinfo data structure
1630 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001631 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 {
1633 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1634 break;
1635 }
1636 }
1637
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001638 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
1640 if (p_csverbose)
1641 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1642 }
1643 else
1644 {
1645 if (i == -1)
1646 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001647 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 if (csinfo[i].fname)
1650 cs_kill_execute(i, csinfo[i].fname);
1651 }
1652 }
1653 else
1654 cs_kill_execute(i, stok);
1655 }
1656
1657 return 0;
1658} /* cs_kill */
1659
1660
1661/*
1662 * PRIVATE: cs_kill_execute
1663 *
1664 * Actually kills a specific cscope connection.
1665 */
1666 static void
1667cs_kill_execute(i, cname)
1668 int i; /* cscope table index */
1669 char *cname; /* cscope database name */
1670{
1671 if (p_csverbose)
1672 {
1673 msg_clr_eos();
1674 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1675 (char_u *)_("cscope connection %s closed"), cname);
1676 }
1677 cs_release_csp(i, TRUE);
1678}
1679
1680
1681/*
1682 * PRIVATE: cs_make_vim_style_matches
1683 *
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001684 * convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1686 * the type of the tag you are looking for. for example, in Darren Hiebert's
1687 * ctags (the one that comes with vim), #define's use a line number to find the
1688 * tag in a file while function definitions use a regexp search pattern.
1689 *
1690 * i'm going to always use the line number because cscope does something
1691 * quirky (and probably other things i don't know about):
1692 *
1693 * if you have "# define" in your source file, which is
1694 * perfectly legal, cscope thinks you have "#define". this
1695 * will result in a failed regexp search. :(
1696 *
1697 * besides, even if this particular case didn't happen, the search pattern
1698 * would still have to be modified to escape all the special regular expression
1699 * characters to comply with ctags formatting.
1700 */
1701 static char *
1702cs_make_vim_style_matches(fname, slno, search, tagstr)
1703 char *fname;
1704 char *slno;
1705 char *search;
1706 char *tagstr;
1707{
1708 /* vim style is ctags:
1709 *
1710 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1711 *
1712 * but as mentioned above, we'll always use the line number and
1713 * put the search pattern (if one exists) as "extra"
1714 *
1715 * buf is used as part of vim's method of handling tags, and
1716 * (i think) vim frees it when you pop your tags and get replaced
1717 * by new ones on the tag stack.
1718 */
1719 char *buf;
1720 int amt;
1721
1722 if (search != NULL)
1723 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001724 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if ((buf = (char *)alloc(amt)) == NULL)
1726 return NULL;
1727
1728 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1729 }
1730 else
1731 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001732 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if ((buf = (char *)alloc(amt)) == NULL)
1734 return NULL;
1735
1736 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1737 }
1738
1739 return buf;
1740} /* cs_make_vim_style_matches */
1741
1742
1743/*
1744 * PRIVATE: cs_manage_matches
1745 *
1746 * this is kind of hokey, but i don't see an easy way round this..
1747 *
1748 * Store: keep a ptr to the (malloc'd) memory of matches originally
1749 * generated from cs_find(). the matches are originally lines directly
1750 * from cscope output, but transformed to look like something out of a
1751 * ctags. see cs_make_vim_style_matches for more details.
1752 *
1753 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1754 * the next line from the cscope output. it basically keeps track of which
1755 * lines have been "used" and returns the next one.
1756 *
1757 * Free: frees up everything and resets
1758 *
1759 * Print: prints the tags
1760 */
1761 static char *
1762cs_manage_matches(matches, contexts, totmatches, cmd)
1763 char **matches;
1764 char **contexts;
1765 int totmatches;
1766 mcmd_e cmd;
1767{
1768 static char **mp = NULL;
1769 static char **cp = NULL;
1770 static int cnt = -1;
1771 static int next = -1;
1772 char *p = NULL;
1773
1774 switch (cmd)
1775 {
1776 case Store:
1777 assert(matches != NULL);
1778 assert(totmatches > 0);
1779 if (mp != NULL || cp != NULL)
1780 (void)cs_manage_matches(NULL, NULL, -1, Free);
1781 mp = matches;
1782 cp = contexts;
1783 cnt = totmatches;
1784 next = 0;
1785 break;
1786 case Get:
1787 if (next >= cnt)
1788 return NULL;
1789
1790 p = mp[next];
1791 next++;
1792 break;
1793 case Free:
1794 if (mp != NULL)
1795 {
1796 if (cnt > 0)
1797 while (cnt--)
1798 {
1799 vim_free(mp[cnt]);
1800 if (cp != NULL)
1801 vim_free(cp[cnt]);
1802 }
1803 vim_free(mp);
1804 vim_free(cp);
1805 }
1806 mp = NULL;
1807 cp = NULL;
1808 cnt = 0;
1809 next = 0;
1810 break;
1811 case Print:
1812 cs_print_tags_priv(mp, cp, cnt);
1813 break;
1814 default: /* should not reach here */
1815 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1816 return NULL;
1817 }
1818
1819 return p;
1820} /* cs_manage_matches */
1821
1822
1823/*
1824 * PRIVATE: cs_parse_results
1825 *
1826 * parse cscope output
1827 */
1828 static char *
1829cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1830 int cnumber;
1831 char *buf;
1832 int bufsize;
1833 char **context;
1834 char **linenumber;
1835 char **search;
1836{
1837 int ch;
1838 char *p;
1839 char *name;
1840
1841 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1842 {
1843 if (feof(csinfo[cnumber].fr_fp))
1844 errno = EIO;
1845
1846 cs_reading_emsg(cnumber);
1847
1848 return NULL;
1849 }
1850
1851 /* If the line's too long for the buffer, discard it. */
1852 if ((p = strchr(buf, '\n')) == NULL)
1853 {
1854 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1855 ;
1856 return NULL;
1857 }
1858 *p = '\0';
1859
1860 /*
1861 * cscope output is in the following format:
1862 *
1863 * <filename> <context> <line number> <pattern>
1864 */
1865 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1866 return NULL;
1867 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1868 return NULL;
1869 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1870 return NULL;
1871 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1872
1873 /* --- nvi ---
1874 * If the file is older than the cscope database, that is,
1875 * the database was built since the file was last modified,
1876 * or there wasn't a search string, use the line number.
1877 */
1878 if (strcmp(*search, "<unknown>") == 0)
1879 *search = NULL;
1880
1881 name = cs_resolve_file(cnumber, name);
1882 return name;
1883}
1884
Bram Moolenaarc716c302006-01-21 22:12:51 +00001885#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886/*
1887 * PRIVATE: cs_file_results
1888 *
1889 * write cscope find results to file
1890 */
1891 static void
1892cs_file_results(f, nummatches_a)
1893 FILE *f;
1894 int *nummatches_a;
1895{
1896 int i, j;
1897 char *buf;
1898 char *search, *slno;
1899 char *fullname;
1900 char *cntx;
1901 char *context;
1902
1903 buf = (char *)alloc(CSREAD_BUFSIZE);
1904 if (buf == NULL)
1905 return;
1906
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001907 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 if (nummatches_a[i] < 1)
1910 continue;
1911
1912 for (j = 0; j < nummatches_a[i]; j++)
1913 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001914 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1915 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 continue;
1917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001918 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001919 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 continue;
1921
1922 if (strcmp(cntx, "<global>")==0)
1923 strcpy(context, "<<global>>");
1924 else
1925 sprintf(context, "<<%s>>", cntx);
1926
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001927 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1929 else
1930 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1931
1932 vim_free(context);
1933 vim_free(fullname);
1934 } /* for all matches */
1935
1936 (void)cs_read_prompt(i);
1937
1938 } /* for all cscope connections */
1939 vim_free(buf);
1940}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943/*
1944 * PRIVATE: cs_fill_results
1945 *
1946 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1947 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001948 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
1950 static void
1951cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1952 char *tagstr;
1953 int totmatches;
1954 int *nummatches_a;
1955 char ***matches_p;
1956 char ***cntxts_p;
1957 int *matched;
1958{
1959 int i, j;
1960 char *buf;
1961 char *search, *slno;
1962 int totsofar = 0;
1963 char **matches = NULL;
1964 char **cntxts = NULL;
1965 char *fullname;
1966 char *cntx;
1967
1968 assert(totmatches > 0);
1969
1970 buf = (char *)alloc(CSREAD_BUFSIZE);
1971 if (buf == NULL)
1972 return;
1973
1974 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1975 goto parse_out;
1976 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1977 goto parse_out;
1978
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001979 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 if (nummatches_a[i] < 1)
1982 continue;
1983
1984 for (j = 0; j < nummatches_a[i]; j++)
1985 {
1986 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1987 &slno, &search)) == NULL)
1988 continue;
1989
1990 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1991 search, tagstr);
1992
1993 vim_free(fullname);
1994
1995 if (strcmp(cntx, "<global>") == 0)
1996 cntxts[totsofar] = NULL;
1997 else
1998 /* note: if vim_strsave returns NULL, then the context
1999 * will be "<global>", which is misleading.
2000 */
2001 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
2002
2003 if (matches[totsofar] != NULL)
2004 totsofar++;
2005
2006 } /* for all matches */
2007
2008 (void)cs_read_prompt(i);
2009
2010 } /* for all cscope connections */
2011
2012parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002013 if (totsofar == 0)
2014 {
2015 /* No matches, free the arrays and return NULL in "*matches_p". */
2016 vim_free(matches);
2017 matches = NULL;
2018 vim_free(cntxts);
2019 cntxts = NULL;
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 *matched = totsofar;
2022 *matches_p = matches;
2023 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 vim_free(buf);
2026} /* cs_fill_results */
2027
2028
2029/* get the requested path components */
2030 static char *
2031cs_pathcomponents(path)
2032 char *path;
2033{
2034 int i;
2035 char *s;
2036
2037 if (p_cspc == 0)
2038 return path;
2039
2040 s = path + strlen(path) - 1;
2041 for (i = 0; i < p_cspc; ++i)
2042 while (s > path && *--s != '/'
2043#ifdef WIN32
2044 && *--s != '\\'
2045#endif
2046 )
2047 ;
2048 if ((s > path && *s == '/')
2049#ifdef WIN32
2050 || (s > path && *s == '\\')
2051#endif
2052 )
2053 ++s;
2054 return s;
2055}
2056
2057/*
2058 * PRIVATE: cs_print_tags_priv
2059 *
2060 * called from cs_manage_matches()
2061 */
2062 static void
2063cs_print_tags_priv(matches, cntxts, num_matches)
2064 char **matches;
2065 char **cntxts;
2066 int num_matches;
2067{
2068 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002069 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 int bufsize = 0; /* Track available bufsize */
2071 int newsize = 0;
2072 char *ptag;
2073 char *fname, *lno, *extra, *tbuf;
2074 int i, idx, num;
2075 char *globalcntx = "GLOBAL";
2076 char *cntxformat = " <<%s>>";
2077 char *context;
2078 char *cstag_msg = _("Cscope tag: %s");
2079 char *csfmt_str = "%4d %6s ";
2080
2081 assert (num_matches > 0);
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 return;
2085
2086 strcpy(tbuf, matches[0]);
2087 ptag = strtok(tbuf, "\t");
2088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 buf = (char *)alloc(newsize);
2091 if (buf != NULL)
2092 {
2093 bufsize = newsize;
2094 (void)sprintf(buf, cstag_msg, ptag);
2095 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2096 }
2097
2098 vim_free(tbuf);
2099
2100 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2101 msg_advance(msg_col + 2);
2102 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2103
2104 num = 1;
2105 for (i = 0; i < num_matches; i++)
2106 {
2107 idx = i;
2108
2109 /* if we really wanted to, we could avoid this malloc and strcpy
2110 * by parsing matches[i] on the fly and placing stuff into buf
2111 * directly, but that's too much of a hassle
2112 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002113 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 continue;
2115 (void)strcpy(tbuf, matches[idx]);
2116
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002117 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 continue;
2119 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2120 continue;
2121 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002122 continue;
2123 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2126
2127 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002128 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (bufsize < newsize)
2130 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002131 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 buf = (char *)vim_realloc(buf, newsize);
2133 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002136 vim_free(t_buf);
2137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139 bufsize = newsize;
2140 }
2141 if (buf != NULL)
2142 {
2143 /* csfmt_str = "%4d %6s "; */
2144 (void)sprintf(buf, csfmt_str, num, lno);
2145 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2146 }
2147 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2148
2149 /* compute the required space for the context */
2150 if (cntxts[idx] != NULL)
2151 context = cntxts[idx];
2152 else
2153 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002154 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 if (bufsize < newsize)
2157 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002158 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 buf = (char *)vim_realloc(buf, newsize);
2160 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002163 vim_free(t_buf);
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else
2166 bufsize = newsize;
2167 }
2168 if (buf != NULL)
2169 {
2170 (void)sprintf(buf, cntxformat, context);
2171
2172 /* print the context only if it fits on the same line */
2173 if (msg_col + (int)strlen(buf) >= (int)Columns)
2174 msg_putchar('\n');
2175 msg_advance(12);
2176 MSG_PUTS_LONG(buf);
2177 msg_putchar('\n');
2178 }
2179 if (extra != NULL)
2180 {
2181 msg_advance(13);
2182 MSG_PUTS_LONG(extra);
2183 }
2184
2185 vim_free(tbuf); /* only after printing extra due to strtok use */
2186
2187 if (msg_col)
2188 msg_putchar('\n');
2189
2190 ui_breakcheck();
2191 if (got_int)
2192 {
2193 got_int = FALSE; /* don't print any more matches */
2194 break;
2195 }
2196
2197 num++;
2198 } /* for all matches */
2199
2200 vim_free(buf);
2201} /* cs_print_tags_priv */
2202
2203
2204/*
2205 * PRIVATE: cs_read_prompt
2206 *
2207 * read a cscope prompt (basically, skip over the ">> ")
2208 */
2209 static int
2210cs_read_prompt(i)
2211 int i;
2212{
2213 int ch;
2214 char *buf = NULL; /* buffer for possible error message from cscope */
2215 int bufpos = 0;
2216 char *cs_emsg;
2217 int maxlen;
2218 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002219 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 int n;
2221
2222 cs_emsg = _("E609: Cscope error: %s");
2223 /* compute maximum allowed len for Cscope error message */
2224 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2225
2226 for (;;)
2227 {
2228 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2229 /* if there is room and char is printable */
2230 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2231 {
2232 if (buf == NULL) /* lazy buffer allocation */
2233 buf = (char *)alloc(maxlen);
2234 if (buf != NULL)
2235 {
2236 /* append character to the message */
2237 buf[bufpos++] = ch;
2238 buf[bufpos] = NUL;
2239 if (bufpos >= epromptlen
2240 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2241 {
2242 /* remove eprompt from buf */
2243 buf[bufpos - epromptlen] = NUL;
2244
2245 /* print message to user */
2246 (void)EMSG2(cs_emsg, buf);
2247
2248 /* send RETURN to cscope */
2249 (void)putc('\n', csinfo[i].to_fp);
2250 (void)fflush(csinfo[i].to_fp);
2251
2252 /* clear buf */
2253 bufpos = 0;
2254 buf[bufpos] = NUL;
2255 }
2256 }
2257 }
2258
2259 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2260 {
2261 if (n > 0)
2262 ch = getc(csinfo[i].fr_fp);
2263 if (ch == EOF)
2264 {
2265 PERROR("cs_read_prompt EOF");
2266 if (buf != NULL && buf[0] != NUL)
2267 (void)EMSG2(cs_emsg, buf);
2268 else if (p_csverbose)
2269 cs_reading_emsg(i); /* don't have additional information */
2270 cs_release_csp(i, TRUE);
2271 vim_free(buf);
2272 return CSCOPE_FAILURE;
2273 }
2274
2275 if (ch != CSCOPE_PROMPT[n])
2276 {
2277 ch = EOF;
2278 break;
2279 }
2280 }
2281
2282 if (ch == EOF)
2283 continue; /* didn't find the prompt */
2284 break; /* did find the prompt */
2285 }
2286
2287 vim_free(buf);
2288 return CSCOPE_SUCCESS;
2289}
2290
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002291#if defined(UNIX) && defined(SIGALRM)
2292/*
2293 * Used to catch and ignore SIGALRM below.
2294 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002295 static RETSIGTYPE
2296sig_handler SIGDEFARG(sigarg)
2297{
2298 /* do nothing */
2299 SIGRETURN;
2300}
2301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303/*
2304 * PRIVATE: cs_release_csp
2305 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002306 * Does the actual free'ing for the cs ptr with an optional flag of whether
2307 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 */
2309 static void
2310cs_release_csp(i, freefnpp)
2311 int i;
2312 int freefnpp;
2313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 /*
2315 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2316 */
2317 if (csinfo[i].to_fp != NULL)
2318 {
2319 (void)fputs("q\n", csinfo[i].to_fp);
2320 (void)fflush(csinfo[i].to_fp);
2321 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002322#if defined(UNIX)
2323 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002324 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002325 int pstat;
2326 pid_t pid;
2327
2328# if defined(HAVE_SIGACTION)
2329 struct sigaction sa, old;
2330
Bram Moolenaar9701da02008-03-16 12:09:58 +00002331 /* Use sigaction() to limit the waiting time to two seconds. */
2332 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002333 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002334# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002335 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002336# else
2337 sa.sa_flags = 0;
2338# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002339 sigaction(SIGALRM, &sa, &old);
2340 alarm(2); /* 2 sec timeout */
2341
2342 /* Block until cscope exits or until timer expires */
2343 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002344 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002345
2346 /* cancel pending alarm if still there and restore signal */
2347 alarm(0);
2348 sigaction(SIGALRM, &old, NULL);
2349# else
2350 int waited;
2351
2352 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2353 * to give cscope a chance to exit quickly. */
2354 sleep(0);
2355 for (waited = 0; waited < 40; ++waited)
2356 {
2357 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002358 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002359 if (pid != 0)
2360 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002361 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002362 }
2363# endif
2364 /*
2365 * If the cscope process is still running: kill it.
2366 * Safety check: If the PID would be zero here, the entire X session
2367 * would be killed. -1 and 1 are dangerous as well.
2368 */
2369 if (pid < 0 && csinfo[i].pid > 1)
2370 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002371# ifdef ECHILD
2372 int alive = TRUE;
2373
2374 if (waitpid_errno == ECHILD)
2375 {
2376 /*
2377 * When using 'vim -g', vim is forked and cscope process is
2378 * no longer a child process but a sibling. So waitpid()
2379 * fails with errno being ECHILD (No child processes).
2380 * Don't send SIGKILL to cscope immediately but wait
2381 * (polling) for it to exit normally as result of sending
2382 * the "q" command, hence giving it a chance to clean up
2383 * its temporary files.
2384 */
2385 int waited;
2386
2387 sleep(0);
2388 for (waited = 0; waited < 40; ++waited)
2389 {
2390 /* Check whether cscope process is still alive */
2391 if (kill(csinfo[i].pid, 0) != 0)
2392 {
2393 alive = FALSE; /* cscope process no longer exists */
2394 break;
2395 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002396 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002397 }
2398 }
2399 if (alive)
2400# endif
2401 {
2402 kill(csinfo[i].pid, SIGKILL);
2403 (void)waitpid(csinfo[i].pid, &pstat, 0);
2404 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002405 }
2406 }
2407#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002408 if (csinfo[i].hProc != NULL)
2409 {
2410 /* Give cscope a chance to exit normally */
2411 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2412 TerminateProcess(csinfo[i].hProc, 0);
2413 CloseHandle(csinfo[i].hProc);
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#endif
2416
2417 if (csinfo[i].fr_fp != NULL)
2418 (void)fclose(csinfo[i].fr_fp);
2419 if (csinfo[i].to_fp != NULL)
2420 (void)fclose(csinfo[i].to_fp);
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (freefnpp)
2423 {
2424 vim_free(csinfo[i].fname);
2425 vim_free(csinfo[i].ppath);
2426 vim_free(csinfo[i].flags);
2427 }
2428
2429 clear_csinfo(i);
2430} /* cs_release_csp */
2431
2432
2433/*
2434 * PRIVATE: cs_reset
2435 *
2436 * calls cs_kill on all cscope connections then reinits
2437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 static int
2439cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002440 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2443 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002444 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002446 if (csinfo_size == 0)
2447 return CSCOPE_SUCCESS;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002450 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2451 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2452 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (dblist == NULL || pplist == NULL || fllist == NULL)
2454 {
2455 vim_free(dblist);
2456 vim_free(pplist);
2457 vim_free(fllist);
2458 return CSCOPE_FAILURE;
2459 }
2460
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002461 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 dblist[i] = csinfo[i].fname;
2464 pplist[i] = csinfo[i].ppath;
2465 fllist[i] = csinfo[i].flags;
2466 if (csinfo[i].fname != NULL)
2467 cs_release_csp(i, FALSE);
2468 }
2469
2470 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002471 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 if (dblist[i] != NULL)
2474 {
2475 cs_add_common(dblist[i], pplist[i], fllist[i]);
2476 if (p_csverbose)
2477 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002478 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * connection number in the same line as
2480 * "Added cscope database..."
2481 */
2482 sprintf(buf, " (#%d)", i);
2483 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2484 }
2485 }
2486 vim_free(dblist[i]);
2487 vim_free(pplist[i]);
2488 vim_free(fllist[i]);
2489 }
2490 vim_free(dblist);
2491 vim_free(pplist);
2492 vim_free(fllist);
2493
2494 if (p_csverbose)
2495 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2496 return CSCOPE_SUCCESS;
2497} /* cs_reset */
2498
2499
2500/*
2501 * PRIVATE: cs_resolve_file
2502 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002503 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * (Prepends ppath, if there is one and if it's not already prepended,
2505 * otherwise just uses the name found.)
2506 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002507 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002509 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 */
2511 static char *
2512cs_resolve_file(i, name)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002513 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 char *name;
2515{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002516 char *fullname;
2517 int len;
2518 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
2520 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002521 * Ppath is freed when we destroy the cscope connection.
2522 * Fullname is freed after cs_make_vim_style_matches, after it's been
2523 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002525 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002527 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002528 else if (p_csre && csinfo[i].fname != NULL)
2529 {
2530 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2531 * path in path resolution. */
2532 csdir = alloc(MAXPATHL);
2533 if (csdir != NULL)
2534 {
2535 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaar28c21912013-05-29 19:18:00 +02002536 gettail((char_u *)csinfo[i].fname)
2537 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002538 len += (int)STRLEN(csdir);
2539 }
2540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002542 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002544 * happens, you are screwed up and need to fix how you're using cscope. */
2545 if (csinfo[i].ppath != NULL
2546 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2547 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002549 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002551 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002552 {
2553 if ((fullname = (char *)alloc(len)) != NULL)
2554 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2555 }
2556 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002557 {
2558 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002559 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002560 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002563 {
2564 fullname = (char *)vim_strsave((char_u *)name);
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002567 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571
2572/*
2573 * PRIVATE: cs_show
2574 *
2575 * show all cscope connections
2576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 static int
2578cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002579 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 short i;
2582 if (cs_cnt_connections() == 0)
2583 MSG_PUTS(_("no cscope connections\n"));
2584 else
2585 {
2586 MSG_PUTS_ATTR(
2587 _(" # pid database name prepend path\n"),
2588 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002589 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
2591 if (csinfo[i].fname == NULL)
2592 continue;
2593
2594 if (csinfo[i].ppath != NULL)
2595 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2596 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2597 else
2598 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2599 i, (long)csinfo[i].pid, csinfo[i].fname);
2600 }
2601 }
2602
2603 wait_return(TRUE);
2604 return CSCOPE_SUCCESS;
2605} /* cs_show */
2606
Bram Moolenaar02b06312007-09-06 15:39:22 +00002607
2608/*
2609 * PUBLIC: cs_end
2610 *
2611 * Only called when VIM exits to quit any cscope sessions.
2612 */
2613 void
2614cs_end()
2615{
2616 int i;
2617
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002618 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002619 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002620 vim_free(csinfo);
2621 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002622}
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#endif /* FEAT_CSCOPE */
2625
2626/* the end */