blob: 605131156e688fc75df8281c5183cd6b371e3c1e [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 Moolenaar856b9fe2009-05-16 14:16:02 +00001000 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 PERROR(_("cs_create_connection exec failed"));
1002
1003 exit(127);
1004 /* NOTREACHED */
1005 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /*
1007 * Save the file descriptors for later duplication, and
1008 * reopen as streams.
1009 */
1010 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1011 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1012 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1013 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 /* close unused */
1016 (void)close(to_cs[0]);
1017 (void)close(from_cs[1]);
1018
1019 break;
1020 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001023 /* WIN32 */
1024 /* Create a new process to run cscope and use pipes to talk with it */
1025 GetStartupInfo(&si);
1026 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1027 si.wShowWindow = SW_HIDE; /* Hide child application window */
1028 si.hStdOutput = stdout_wr;
1029 si.hStdError = stdout_wr;
1030 si.hStdInput = stdin_rd;
1031 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1032 NULL, NULL, &si, &pi);
1033 vim_free(prog);
1034 vim_free(cmd);
1035
1036 if (!created)
1037 {
1038 PERROR(_("cs_create_connection exec failed"));
1039 (void)EMSG(_("E623: Could not spawn cscope process"));
1040 goto err_closing;
1041 }
1042 /* else */
1043 csinfo[i].pid = pi.dwProcessId;
1044 csinfo[i].hProc = pi.hProcess;
1045 CloseHandle(pi.hThread);
1046
1047 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001048 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1049 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001050 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1051 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001052 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1053 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001054 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1055 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1056
1057 /* Close handles for file descriptors inherited by the cscope process */
1058 CloseHandle(stdin_rd);
1059 CloseHandle(stdout_wr);
1060
1061#endif /* !UNIX */
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 return CSCOPE_SUCCESS;
1064} /* cs_create_connection */
1065
1066
1067/*
1068 * PRIVATE: cs_find
1069 *
1070 * query cscope using command line interface. parse the output and use tselect
1071 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1072 *
1073 * returns TRUE if we jump to a tag or abort, FALSE if not.
1074 */
1075 static int
1076cs_find(eap)
1077 exarg_T *eap;
1078{
1079 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001080 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
1082 if (cs_check_for_connections() == FALSE)
1083 {
1084 (void)EMSG(_("E567: no cscope connections"));
1085 return FALSE;
1086 }
1087
1088 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1089 {
1090 cs_usage_msg(Find);
1091 return FALSE;
1092 }
1093
1094 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001095 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
1097 cs_usage_msg(Find);
1098 return FALSE;
1099 }
1100
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001101 /*
1102 * Let's replace the NULs written by strtok() with spaces - we need the
1103 * spaces to correctly display the quickfix/location list window's title.
1104 */
1105 for (i = 0; i < eap_arg_len; ++i)
1106 if (NUL == eap->arg[i])
1107 eap->arg[i] = ' ';
1108
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001109 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001110 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111} /* cs_find */
1112
1113
1114/*
1115 * PRIVATE: cs_find_common
1116 *
1117 * common code for cscope find, shared by cs_find() and do_cstag()
1118 */
1119 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001120cs_find_common(opt, pat, forceit, verbose, use_ll, cmdline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char *opt;
1122 char *pat;
1123 int forceit;
1124 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001125 int use_ll;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001126 char_u *cmdline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 int i;
1129 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001130 int *nummatches;
1131 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#ifdef FEAT_QUICKFIX
1133 char cmdletter;
1134 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001135
1136 /* get cmd letter */
1137 switch (opt[0])
1138 {
1139 case '0' :
1140 cmdletter = 's';
1141 break;
1142 case '1' :
1143 cmdletter = 'g';
1144 break;
1145 case '2' :
1146 cmdletter = 'd';
1147 break;
1148 case '3' :
1149 cmdletter = 'c';
1150 break;
1151 case '4' :
1152 cmdletter = 't';
1153 break;
1154 case '6' :
1155 cmdletter = 'e';
1156 break;
1157 case '7' :
1158 cmdletter = 'f';
1159 break;
1160 case '8' :
1161 cmdletter = 'i';
1162 break;
1163 default :
1164 cmdletter = opt[0];
1165 }
1166
1167 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1168 if (qfpos != NULL)
1169 {
1170 qfpos++;
1171 /* next symbol must be + or - */
1172 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1173 {
1174 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1175 char *buf = (char *)alloc((unsigned)strlen(nf));
1176
1177 /* strlen will be enough because we use chars */
1178 if (buf != NULL)
1179 {
1180 sprintf(buf, nf, *qfpos, *(qfpos-1));
1181 (void)EMSG(buf);
1182 vim_free(buf);
1183 }
1184 return FALSE;
1185 }
1186
1187# ifdef FEAT_AUTOCMD
1188 if (*qfpos != '0')
1189 {
1190 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1191 curbuf->b_fname, TRUE, curbuf);
1192# ifdef FEAT_EVAL
1193 if (did_throw || force_abort)
1194 return FALSE;
1195# endif
1196 }
1197# endif
1198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
1200
1201 /* create the actual command to send to cscope */
1202 cmd = cs_create_cmd(opt, pat);
1203 if (cmd == NULL)
1204 return FALSE;
1205
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001206 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1207 if (nummatches == NULL)
1208 return FALSE;
1209
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001210 /* Send query to all open connections, then count the total number
1211 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001212 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 nummatches[i] = 0;
1214 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001215 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001217 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 continue;
1219
1220 /* send cmd to cscope */
1221 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1222 (void)fflush(csinfo[i].to_fp);
1223
1224 nummatches[i] = cs_cnt_matches(i);
1225
1226 if (nummatches[i] > -1)
1227 totmatches += nummatches[i];
1228
1229 if (nummatches[i] == 0)
1230 (void)cs_read_prompt(i);
1231 }
1232 vim_free(cmd);
1233
1234 if (totmatches == 0)
1235 {
1236 char *nf = _("E259: no matches found for cscope query %s of %s");
1237 char *buf;
1238
1239 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001240 {
1241 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001245 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (buf == NULL)
1247 (void)EMSG(nf);
1248 else
1249 {
1250 sprintf(buf, nf, opt, pat);
1251 (void)EMSG(buf);
1252 vim_free(buf);
1253 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001254 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 return FALSE;
1256 }
1257
1258#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001259 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001262 FILE *f;
1263 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001264 qf_info_T *qi = NULL;
1265 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001267 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001268 if (f == NULL)
1269 EMSG2(_(e_notopen), tmp);
1270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001272 cs_file_results(f, nummatches);
1273 fclose(f);
1274 if (use_ll) /* Use location list */
1275 wp = curwin;
1276 /* '-' starts a new error list */
1277 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001278 *qfpos == '-', cmdline) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001280# ifdef FEAT_WINDOWS
1281 if (postponed_split != 0)
1282 {
1283 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001285 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001286 postponed_split = 0;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001289
1290# ifdef FEAT_AUTOCMD
1291 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1292 curbuf->b_fname, TRUE, curbuf);
1293# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001294 if (use_ll)
1295 /*
1296 * In the location list window, use the displayed location
1297 * list. Otherwise, use the location list for the window.
1298 */
1299 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1300 ? wp->w_llist_ref : wp->w_llist;
1301 qf_jump(qi, 0, 0, forceit);
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 mch_remove(tmp);
1305 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001306 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 return TRUE;
1308 }
1309 else
1310#endif /* FEAT_QUICKFIX */
1311 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001312 char **matches = NULL, **contexts = NULL;
1313 int matched = 0;
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 /* read output */
1316 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1317 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001318 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (matches == NULL)
1320 return FALSE;
1321
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001322 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1325 }
1326
1327} /* cs_find_common */
1328
1329/*
1330 * PRIVATE: cs_help
1331 *
1332 * print help
1333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static int
1335cs_help(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001336 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 cscmd_T *cmdp = cs_cmds;
1339
1340 (void)MSG_PUTS(_("cscope commands:\n"));
1341 while (cmdp->name != NULL)
1342 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001343 char *help = _(cmdp->help);
1344 int space_cnt = 30 - vim_strsize((char_u *)help);
1345
1346 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1347 if (space_cnt < 0)
1348 space_cnt = 0;
1349 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1350 cmdp->name,
1351 help, space_cnt, " ",
1352 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001354 MSG_PUTS(_("\n"
1355 " c: Find functions calling this function\n"
1356 " d: Find functions called by this function\n"
1357 " e: Find this egrep pattern\n"
1358 " f: Find this file\n"
1359 " g: Find this definition\n"
1360 " i: Find files #including this file\n"
1361 " s: Find this C symbol\n"
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001362 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 cmdp++;
1365 }
1366
1367 wait_return(TRUE);
1368 return 0;
1369} /* cs_help */
1370
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 static void
1373clear_csinfo(i)
1374 int i;
1375{
1376 csinfo[i].fname = NULL;
1377 csinfo[i].ppath = NULL;
1378 csinfo[i].flags = NULL;
1379#if defined(UNIX)
1380 csinfo[i].st_dev = (dev_t)0;
1381 csinfo[i].st_ino = (ino_t)0;
1382#else
1383 csinfo[i].nVolume = 0;
1384 csinfo[i].nIndexHigh = 0;
1385 csinfo[i].nIndexLow = 0;
1386#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001387 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 csinfo[i].fr_fp = NULL;
1389 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001390#if defined(WIN32)
1391 csinfo[i].hProc = NULL;
1392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393}
1394
1395#ifndef UNIX
1396static char *GetWin32Error __ARGS((void));
1397
1398 static char *
1399GetWin32Error()
1400{
1401 char *msg = NULL;
1402 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1403 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1404 if (msg != NULL)
1405 {
1406 /* remove trailing \r\n */
1407 char *pcrlf = strstr(msg, "\r\n");
1408 if (pcrlf != NULL)
1409 *pcrlf = '\0';
1410 }
1411 return msg;
1412}
1413#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415/*
1416 * PRIVATE: cs_insert_filelist
1417 *
1418 * insert a new cscope database filename into the filelist
1419 */
1420 static int
1421cs_insert_filelist(fname, ppath, flags, sb)
1422 char *fname;
1423 char *ppath;
1424 char *flags;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001425 struct stat *sb UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427 short i, j;
1428#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 BY_HANDLE_FILE_INFORMATION bhfi;
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1432 if (!mch_windows95())
1433 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001434 switch (win32_fileinfo(fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001436 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1437 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (p_csverbose)
1439 {
1440 char *cant_msg = _("E625: cannot open cscope database: %s");
1441 char *winmsg = GetWin32Error();
1442
1443 if (winmsg != NULL)
1444 {
1445 (void)EMSG2(cant_msg, winmsg);
1446 LocalFree(winmsg);
1447 }
1448 else
1449 /* subst filename if can't get error text */
1450 (void)EMSG2(cant_msg, fname);
1451 }
1452 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001453
1454 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (p_csverbose)
1456 (void)EMSG(_("E626: cannot get cscope database information"));
1457 return -1;
1458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460#endif
1461
1462 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001463 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 {
1465 if (csinfo[j].fname != NULL
1466#if defined(UNIX)
1467 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1468#else
1469 /* compare pathnames first */
1470 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001471 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 || (!mch_windows95()
1473 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1474 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1475 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1476#endif
1477 )
1478 {
1479 if (p_csverbose)
1480 (void)EMSG(_("E568: duplicate cscope database not added"));
1481 return -1;
1482 }
1483
1484 if (csinfo[j].fname == NULL && i == -1)
1485 i = j; /* remember first empty entry */
1486 }
1487
1488 if (i == -1)
1489 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001490 i = csinfo_size;
1491 if (csinfo_size == 0)
1492 {
1493 /* First time allocation: allocate only 1 connection. It should
1494 * be enough for most users. If more is needed, csinfo will be
1495 * reallocated. */
1496 csinfo_size = 1;
1497 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1498 }
1499 else
1500 {
1501 /* Reallocate space for more connections. */
1502 csinfo_size *= 2;
1503 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1504 }
1505 if (csinfo == NULL)
1506 return -1;
1507 for (j = csinfo_size/2; j < csinfo_size; j++)
1508 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001511 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 return -1;
1513
1514 (void)strcpy(csinfo[i].fname, (const char *)fname);
1515
1516 if (ppath != NULL)
1517 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001518 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
1520 vim_free(csinfo[i].fname);
1521 csinfo[i].fname = NULL;
1522 return -1;
1523 }
1524 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1525 } else
1526 csinfo[i].ppath = NULL;
1527
1528 if (flags != NULL)
1529 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001530 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 vim_free(csinfo[i].fname);
1533 vim_free(csinfo[i].ppath);
1534 csinfo[i].fname = NULL;
1535 csinfo[i].ppath = NULL;
1536 return -1;
1537 }
1538 (void)strcpy(csinfo[i].flags, (const char *)flags);
1539 } else
1540 csinfo[i].flags = NULL;
1541
1542#if defined(UNIX)
1543 csinfo[i].st_dev = sb->st_dev;
1544 csinfo[i].st_ino = sb->st_ino;
1545
1546#else
1547 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1548 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1549 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1550#endif
1551 return i;
1552} /* cs_insert_filelist */
1553
1554
1555/*
1556 * PRIVATE: cs_lookup_cmd
1557 *
1558 * find cscope command in command table
1559 */
1560 static cscmd_T *
1561cs_lookup_cmd(eap)
1562 exarg_T *eap;
1563{
1564 cscmd_T *cmdp;
1565 char *stok;
1566 size_t len;
1567
1568 if (eap->arg == NULL)
1569 return NULL;
1570
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001571 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001572 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1575 return NULL;
1576
1577 len = strlen(stok);
1578 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1579 {
1580 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1581 return (cmdp);
1582 }
1583 return NULL;
1584} /* cs_lookup_cmd */
1585
1586
1587/*
1588 * PRIVATE: cs_kill
1589 *
1590 * nuke em
1591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 static int
1593cs_kill(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001594 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 char *stok;
1597 short i;
1598
1599 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1600 {
1601 cs_usage_msg(Kill);
1602 return CSCOPE_FAILURE;
1603 }
1604
1605 /* only single digit positive and negative integers are allowed */
1606 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1607 || (strlen(stok) < 3 && stok[0] == '-'
1608 && VIM_ISDIGIT((int)(stok[1]))))
1609 i = atoi(stok);
1610 else
1611 {
1612 /* It must be part of a name. We will try to find a match
1613 * within all the names in the csinfo data structure
1614 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001615 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1618 break;
1619 }
1620 }
1621
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001622 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 if (p_csverbose)
1625 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1626 }
1627 else
1628 {
1629 if (i == -1)
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)
1634 cs_kill_execute(i, csinfo[i].fname);
1635 }
1636 }
1637 else
1638 cs_kill_execute(i, stok);
1639 }
1640
1641 return 0;
1642} /* cs_kill */
1643
1644
1645/*
1646 * PRIVATE: cs_kill_execute
1647 *
1648 * Actually kills a specific cscope connection.
1649 */
1650 static void
1651cs_kill_execute(i, cname)
1652 int i; /* cscope table index */
1653 char *cname; /* cscope database name */
1654{
1655 if (p_csverbose)
1656 {
1657 msg_clr_eos();
1658 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1659 (char_u *)_("cscope connection %s closed"), cname);
1660 }
1661 cs_release_csp(i, TRUE);
1662}
1663
1664
1665/*
1666 * PRIVATE: cs_make_vim_style_matches
1667 *
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001668 * convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1670 * the type of the tag you are looking for. for example, in Darren Hiebert's
1671 * ctags (the one that comes with vim), #define's use a line number to find the
1672 * tag in a file while function definitions use a regexp search pattern.
1673 *
1674 * i'm going to always use the line number because cscope does something
1675 * quirky (and probably other things i don't know about):
1676 *
1677 * if you have "# define" in your source file, which is
1678 * perfectly legal, cscope thinks you have "#define". this
1679 * will result in a failed regexp search. :(
1680 *
1681 * besides, even if this particular case didn't happen, the search pattern
1682 * would still have to be modified to escape all the special regular expression
1683 * characters to comply with ctags formatting.
1684 */
1685 static char *
1686cs_make_vim_style_matches(fname, slno, search, tagstr)
1687 char *fname;
1688 char *slno;
1689 char *search;
1690 char *tagstr;
1691{
1692 /* vim style is ctags:
1693 *
1694 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1695 *
1696 * but as mentioned above, we'll always use the line number and
1697 * put the search pattern (if one exists) as "extra"
1698 *
1699 * buf is used as part of vim's method of handling tags, and
1700 * (i think) vim frees it when you pop your tags and get replaced
1701 * by new ones on the tag stack.
1702 */
1703 char *buf;
1704 int amt;
1705
1706 if (search != NULL)
1707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001708 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if ((buf = (char *)alloc(amt)) == NULL)
1710 return NULL;
1711
1712 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1713 }
1714 else
1715 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001716 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if ((buf = (char *)alloc(amt)) == NULL)
1718 return NULL;
1719
1720 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1721 }
1722
1723 return buf;
1724} /* cs_make_vim_style_matches */
1725
1726
1727/*
1728 * PRIVATE: cs_manage_matches
1729 *
1730 * this is kind of hokey, but i don't see an easy way round this..
1731 *
1732 * Store: keep a ptr to the (malloc'd) memory of matches originally
1733 * generated from cs_find(). the matches are originally lines directly
1734 * from cscope output, but transformed to look like something out of a
1735 * ctags. see cs_make_vim_style_matches for more details.
1736 *
1737 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1738 * the next line from the cscope output. it basically keeps track of which
1739 * lines have been "used" and returns the next one.
1740 *
1741 * Free: frees up everything and resets
1742 *
1743 * Print: prints the tags
1744 */
1745 static char *
1746cs_manage_matches(matches, contexts, totmatches, cmd)
1747 char **matches;
1748 char **contexts;
1749 int totmatches;
1750 mcmd_e cmd;
1751{
1752 static char **mp = NULL;
1753 static char **cp = NULL;
1754 static int cnt = -1;
1755 static int next = -1;
1756 char *p = NULL;
1757
1758 switch (cmd)
1759 {
1760 case Store:
1761 assert(matches != NULL);
1762 assert(totmatches > 0);
1763 if (mp != NULL || cp != NULL)
1764 (void)cs_manage_matches(NULL, NULL, -1, Free);
1765 mp = matches;
1766 cp = contexts;
1767 cnt = totmatches;
1768 next = 0;
1769 break;
1770 case Get:
1771 if (next >= cnt)
1772 return NULL;
1773
1774 p = mp[next];
1775 next++;
1776 break;
1777 case Free:
1778 if (mp != NULL)
1779 {
1780 if (cnt > 0)
1781 while (cnt--)
1782 {
1783 vim_free(mp[cnt]);
1784 if (cp != NULL)
1785 vim_free(cp[cnt]);
1786 }
1787 vim_free(mp);
1788 vim_free(cp);
1789 }
1790 mp = NULL;
1791 cp = NULL;
1792 cnt = 0;
1793 next = 0;
1794 break;
1795 case Print:
1796 cs_print_tags_priv(mp, cp, cnt);
1797 break;
1798 default: /* should not reach here */
1799 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1800 return NULL;
1801 }
1802
1803 return p;
1804} /* cs_manage_matches */
1805
1806
1807/*
1808 * PRIVATE: cs_parse_results
1809 *
1810 * parse cscope output
1811 */
1812 static char *
1813cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1814 int cnumber;
1815 char *buf;
1816 int bufsize;
1817 char **context;
1818 char **linenumber;
1819 char **search;
1820{
1821 int ch;
1822 char *p;
1823 char *name;
1824
1825 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1826 {
1827 if (feof(csinfo[cnumber].fr_fp))
1828 errno = EIO;
1829
1830 cs_reading_emsg(cnumber);
1831
1832 return NULL;
1833 }
1834
1835 /* If the line's too long for the buffer, discard it. */
1836 if ((p = strchr(buf, '\n')) == NULL)
1837 {
1838 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1839 ;
1840 return NULL;
1841 }
1842 *p = '\0';
1843
1844 /*
1845 * cscope output is in the following format:
1846 *
1847 * <filename> <context> <line number> <pattern>
1848 */
1849 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1850 return NULL;
1851 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1852 return NULL;
1853 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1854 return NULL;
1855 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1856
1857 /* --- nvi ---
1858 * If the file is older than the cscope database, that is,
1859 * the database was built since the file was last modified,
1860 * or there wasn't a search string, use the line number.
1861 */
1862 if (strcmp(*search, "<unknown>") == 0)
1863 *search = NULL;
1864
1865 name = cs_resolve_file(cnumber, name);
1866 return name;
1867}
1868
Bram Moolenaarc716c302006-01-21 22:12:51 +00001869#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870/*
1871 * PRIVATE: cs_file_results
1872 *
1873 * write cscope find results to file
1874 */
1875 static void
1876cs_file_results(f, nummatches_a)
1877 FILE *f;
1878 int *nummatches_a;
1879{
1880 int i, j;
1881 char *buf;
1882 char *search, *slno;
1883 char *fullname;
1884 char *cntx;
1885 char *context;
1886
1887 buf = (char *)alloc(CSREAD_BUFSIZE);
1888 if (buf == NULL)
1889 return;
1890
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001891 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 if (nummatches_a[i] < 1)
1894 continue;
1895
1896 for (j = 0; j < nummatches_a[i]; j++)
1897 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001898 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1899 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 continue;
1901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001902 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001903 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 continue;
1905
1906 if (strcmp(cntx, "<global>")==0)
1907 strcpy(context, "<<global>>");
1908 else
1909 sprintf(context, "<<%s>>", cntx);
1910
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001911 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1913 else
1914 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1915
1916 vim_free(context);
1917 vim_free(fullname);
1918 } /* for all matches */
1919
1920 (void)cs_read_prompt(i);
1921
1922 } /* for all cscope connections */
1923 vim_free(buf);
1924}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927/*
1928 * PRIVATE: cs_fill_results
1929 *
1930 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1931 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001932 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 */
1934 static void
1935cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1936 char *tagstr;
1937 int totmatches;
1938 int *nummatches_a;
1939 char ***matches_p;
1940 char ***cntxts_p;
1941 int *matched;
1942{
1943 int i, j;
1944 char *buf;
1945 char *search, *slno;
1946 int totsofar = 0;
1947 char **matches = NULL;
1948 char **cntxts = NULL;
1949 char *fullname;
1950 char *cntx;
1951
1952 assert(totmatches > 0);
1953
1954 buf = (char *)alloc(CSREAD_BUFSIZE);
1955 if (buf == NULL)
1956 return;
1957
1958 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1959 goto parse_out;
1960 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1961 goto parse_out;
1962
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001963 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 if (nummatches_a[i] < 1)
1966 continue;
1967
1968 for (j = 0; j < nummatches_a[i]; j++)
1969 {
1970 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1971 &slno, &search)) == NULL)
1972 continue;
1973
1974 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1975 search, tagstr);
1976
1977 vim_free(fullname);
1978
1979 if (strcmp(cntx, "<global>") == 0)
1980 cntxts[totsofar] = NULL;
1981 else
1982 /* note: if vim_strsave returns NULL, then the context
1983 * will be "<global>", which is misleading.
1984 */
1985 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1986
1987 if (matches[totsofar] != NULL)
1988 totsofar++;
1989
1990 } /* for all matches */
1991
1992 (void)cs_read_prompt(i);
1993
1994 } /* for all cscope connections */
1995
1996parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001997 if (totsofar == 0)
1998 {
1999 /* No matches, free the arrays and return NULL in "*matches_p". */
2000 vim_free(matches);
2001 matches = NULL;
2002 vim_free(cntxts);
2003 cntxts = NULL;
2004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 *matched = totsofar;
2006 *matches_p = matches;
2007 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 vim_free(buf);
2010} /* cs_fill_results */
2011
2012
2013/* get the requested path components */
2014 static char *
2015cs_pathcomponents(path)
2016 char *path;
2017{
2018 int i;
2019 char *s;
2020
2021 if (p_cspc == 0)
2022 return path;
2023
2024 s = path + strlen(path) - 1;
2025 for (i = 0; i < p_cspc; ++i)
2026 while (s > path && *--s != '/'
2027#ifdef WIN32
2028 && *--s != '\\'
2029#endif
2030 )
2031 ;
2032 if ((s > path && *s == '/')
2033#ifdef WIN32
2034 || (s > path && *s == '\\')
2035#endif
2036 )
2037 ++s;
2038 return s;
2039}
2040
2041/*
2042 * PRIVATE: cs_print_tags_priv
2043 *
2044 * called from cs_manage_matches()
2045 */
2046 static void
2047cs_print_tags_priv(matches, cntxts, num_matches)
2048 char **matches;
2049 char **cntxts;
2050 int num_matches;
2051{
2052 char *buf = NULL;
2053 int bufsize = 0; /* Track available bufsize */
2054 int newsize = 0;
2055 char *ptag;
2056 char *fname, *lno, *extra, *tbuf;
2057 int i, idx, num;
2058 char *globalcntx = "GLOBAL";
2059 char *cntxformat = " <<%s>>";
2060 char *context;
2061 char *cstag_msg = _("Cscope tag: %s");
2062 char *csfmt_str = "%4d %6s ";
2063
2064 assert (num_matches > 0);
2065
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002066 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return;
2068
2069 strcpy(tbuf, matches[0]);
2070 ptag = strtok(tbuf, "\t");
2071
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002072 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 buf = (char *)alloc(newsize);
2074 if (buf != NULL)
2075 {
2076 bufsize = newsize;
2077 (void)sprintf(buf, cstag_msg, ptag);
2078 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2079 }
2080
2081 vim_free(tbuf);
2082
2083 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2084 msg_advance(msg_col + 2);
2085 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2086
2087 num = 1;
2088 for (i = 0; i < num_matches; i++)
2089 {
2090 idx = i;
2091
2092 /* if we really wanted to, we could avoid this malloc and strcpy
2093 * by parsing matches[i] on the fly and placing stuff into buf
2094 * directly, but that's too much of a hassle
2095 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 continue;
2098 (void)strcpy(tbuf, matches[idx]);
2099
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002100 if (strtok(tbuf, (const char *)"\t") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 continue;
2102 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2103 continue;
2104 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002105 continue;
2106 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2109
2110 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (bufsize < newsize)
2113 {
2114 buf = (char *)vim_realloc(buf, newsize);
2115 if (buf == NULL)
2116 bufsize = 0;
2117 else
2118 bufsize = newsize;
2119 }
2120 if (buf != NULL)
2121 {
2122 /* csfmt_str = "%4d %6s "; */
2123 (void)sprintf(buf, csfmt_str, num, lno);
2124 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2125 }
2126 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2127
2128 /* compute the required space for the context */
2129 if (cntxts[idx] != NULL)
2130 context = cntxts[idx];
2131 else
2132 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 if (bufsize < newsize)
2136 {
2137 buf = (char *)vim_realloc(buf, newsize);
2138 if (buf == NULL)
2139 bufsize = 0;
2140 else
2141 bufsize = newsize;
2142 }
2143 if (buf != NULL)
2144 {
2145 (void)sprintf(buf, cntxformat, context);
2146
2147 /* print the context only if it fits on the same line */
2148 if (msg_col + (int)strlen(buf) >= (int)Columns)
2149 msg_putchar('\n');
2150 msg_advance(12);
2151 MSG_PUTS_LONG(buf);
2152 msg_putchar('\n');
2153 }
2154 if (extra != NULL)
2155 {
2156 msg_advance(13);
2157 MSG_PUTS_LONG(extra);
2158 }
2159
2160 vim_free(tbuf); /* only after printing extra due to strtok use */
2161
2162 if (msg_col)
2163 msg_putchar('\n');
2164
2165 ui_breakcheck();
2166 if (got_int)
2167 {
2168 got_int = FALSE; /* don't print any more matches */
2169 break;
2170 }
2171
2172 num++;
2173 } /* for all matches */
2174
2175 vim_free(buf);
2176} /* cs_print_tags_priv */
2177
2178
2179/*
2180 * PRIVATE: cs_read_prompt
2181 *
2182 * read a cscope prompt (basically, skip over the ">> ")
2183 */
2184 static int
2185cs_read_prompt(i)
2186 int i;
2187{
2188 int ch;
2189 char *buf = NULL; /* buffer for possible error message from cscope */
2190 int bufpos = 0;
2191 char *cs_emsg;
2192 int maxlen;
2193 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002194 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 int n;
2196
2197 cs_emsg = _("E609: Cscope error: %s");
2198 /* compute maximum allowed len for Cscope error message */
2199 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2200
2201 for (;;)
2202 {
2203 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2204 /* if there is room and char is printable */
2205 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2206 {
2207 if (buf == NULL) /* lazy buffer allocation */
2208 buf = (char *)alloc(maxlen);
2209 if (buf != NULL)
2210 {
2211 /* append character to the message */
2212 buf[bufpos++] = ch;
2213 buf[bufpos] = NUL;
2214 if (bufpos >= epromptlen
2215 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2216 {
2217 /* remove eprompt from buf */
2218 buf[bufpos - epromptlen] = NUL;
2219
2220 /* print message to user */
2221 (void)EMSG2(cs_emsg, buf);
2222
2223 /* send RETURN to cscope */
2224 (void)putc('\n', csinfo[i].to_fp);
2225 (void)fflush(csinfo[i].to_fp);
2226
2227 /* clear buf */
2228 bufpos = 0;
2229 buf[bufpos] = NUL;
2230 }
2231 }
2232 }
2233
2234 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2235 {
2236 if (n > 0)
2237 ch = getc(csinfo[i].fr_fp);
2238 if (ch == EOF)
2239 {
2240 PERROR("cs_read_prompt EOF");
2241 if (buf != NULL && buf[0] != NUL)
2242 (void)EMSG2(cs_emsg, buf);
2243 else if (p_csverbose)
2244 cs_reading_emsg(i); /* don't have additional information */
2245 cs_release_csp(i, TRUE);
2246 vim_free(buf);
2247 return CSCOPE_FAILURE;
2248 }
2249
2250 if (ch != CSCOPE_PROMPT[n])
2251 {
2252 ch = EOF;
2253 break;
2254 }
2255 }
2256
2257 if (ch == EOF)
2258 continue; /* didn't find the prompt */
2259 break; /* did find the prompt */
2260 }
2261
2262 vim_free(buf);
2263 return CSCOPE_SUCCESS;
2264}
2265
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002266#if defined(UNIX) && defined(SIGALRM)
2267/*
2268 * Used to catch and ignore SIGALRM below.
2269 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002270 static RETSIGTYPE
2271sig_handler SIGDEFARG(sigarg)
2272{
2273 /* do nothing */
2274 SIGRETURN;
2275}
2276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278/*
2279 * PRIVATE: cs_release_csp
2280 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002281 * Does the actual free'ing for the cs ptr with an optional flag of whether
2282 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 static void
2285cs_release_csp(i, freefnpp)
2286 int i;
2287 int freefnpp;
2288{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 /*
2290 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2291 */
2292 if (csinfo[i].to_fp != NULL)
2293 {
2294 (void)fputs("q\n", csinfo[i].to_fp);
2295 (void)fflush(csinfo[i].to_fp);
2296 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002297#if defined(UNIX)
2298 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002299 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002300 int pstat;
2301 pid_t pid;
2302
2303# if defined(HAVE_SIGACTION)
2304 struct sigaction sa, old;
2305
Bram Moolenaar9701da02008-03-16 12:09:58 +00002306 /* Use sigaction() to limit the waiting time to two seconds. */
2307 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002308 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002309# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002310 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002311# else
2312 sa.sa_flags = 0;
2313# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002314 sigaction(SIGALRM, &sa, &old);
2315 alarm(2); /* 2 sec timeout */
2316
2317 /* Block until cscope exits or until timer expires */
2318 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002319 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002320
2321 /* cancel pending alarm if still there and restore signal */
2322 alarm(0);
2323 sigaction(SIGALRM, &old, NULL);
2324# else
2325 int waited;
2326
2327 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2328 * to give cscope a chance to exit quickly. */
2329 sleep(0);
2330 for (waited = 0; waited < 40; ++waited)
2331 {
2332 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002333 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002334 if (pid != 0)
2335 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002336 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002337 }
2338# endif
2339 /*
2340 * If the cscope process is still running: kill it.
2341 * Safety check: If the PID would be zero here, the entire X session
2342 * would be killed. -1 and 1 are dangerous as well.
2343 */
2344 if (pid < 0 && csinfo[i].pid > 1)
2345 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002346# ifdef ECHILD
2347 int alive = TRUE;
2348
2349 if (waitpid_errno == ECHILD)
2350 {
2351 /*
2352 * When using 'vim -g', vim is forked and cscope process is
2353 * no longer a child process but a sibling. So waitpid()
2354 * fails with errno being ECHILD (No child processes).
2355 * Don't send SIGKILL to cscope immediately but wait
2356 * (polling) for it to exit normally as result of sending
2357 * the "q" command, hence giving it a chance to clean up
2358 * its temporary files.
2359 */
2360 int waited;
2361
2362 sleep(0);
2363 for (waited = 0; waited < 40; ++waited)
2364 {
2365 /* Check whether cscope process is still alive */
2366 if (kill(csinfo[i].pid, 0) != 0)
2367 {
2368 alive = FALSE; /* cscope process no longer exists */
2369 break;
2370 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002371 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002372 }
2373 }
2374 if (alive)
2375# endif
2376 {
2377 kill(csinfo[i].pid, SIGKILL);
2378 (void)waitpid(csinfo[i].pid, &pstat, 0);
2379 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002380 }
2381 }
2382#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002383 if (csinfo[i].hProc != NULL)
2384 {
2385 /* Give cscope a chance to exit normally */
2386 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2387 TerminateProcess(csinfo[i].hProc, 0);
2388 CloseHandle(csinfo[i].hProc);
2389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#endif
2391
2392 if (csinfo[i].fr_fp != NULL)
2393 (void)fclose(csinfo[i].fr_fp);
2394 if (csinfo[i].to_fp != NULL)
2395 (void)fclose(csinfo[i].to_fp);
2396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (freefnpp)
2398 {
2399 vim_free(csinfo[i].fname);
2400 vim_free(csinfo[i].ppath);
2401 vim_free(csinfo[i].flags);
2402 }
2403
2404 clear_csinfo(i);
2405} /* cs_release_csp */
2406
2407
2408/*
2409 * PRIVATE: cs_reset
2410 *
2411 * calls cs_kill on all cscope connections then reinits
2412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 static int
2414cs_reset(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002415 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2418 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002419 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002421 if (csinfo_size == 0)
2422 return CSCOPE_SUCCESS;
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002425 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2426 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2427 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 if (dblist == NULL || pplist == NULL || fllist == NULL)
2429 {
2430 vim_free(dblist);
2431 vim_free(pplist);
2432 vim_free(fllist);
2433 return CSCOPE_FAILURE;
2434 }
2435
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002436 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 dblist[i] = csinfo[i].fname;
2439 pplist[i] = csinfo[i].ppath;
2440 fllist[i] = csinfo[i].flags;
2441 if (csinfo[i].fname != NULL)
2442 cs_release_csp(i, FALSE);
2443 }
2444
2445 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002446 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 if (dblist[i] != NULL)
2449 {
2450 cs_add_common(dblist[i], pplist[i], fllist[i]);
2451 if (p_csverbose)
2452 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002453 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 * connection number in the same line as
2455 * "Added cscope database..."
2456 */
2457 sprintf(buf, " (#%d)", i);
2458 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2459 }
2460 }
2461 vim_free(dblist[i]);
2462 vim_free(pplist[i]);
2463 vim_free(fllist[i]);
2464 }
2465 vim_free(dblist);
2466 vim_free(pplist);
2467 vim_free(fllist);
2468
2469 if (p_csverbose)
2470 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2471 return CSCOPE_SUCCESS;
2472} /* cs_reset */
2473
2474
2475/*
2476 * PRIVATE: cs_resolve_file
2477 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002478 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * (Prepends ppath, if there is one and if it's not already prepended,
2480 * otherwise just uses the name found.)
2481 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002482 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002484 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
2486 static char *
2487cs_resolve_file(i, name)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002488 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 char *name;
2490{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002491 char *fullname;
2492 int len;
2493 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002496 * Ppath is freed when we destroy the cscope connection.
2497 * Fullname is freed after cs_make_vim_style_matches, after it's been
2498 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002500 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002502 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002503 else if (p_csre && csinfo[i].fname != NULL)
2504 {
2505 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2506 * path in path resolution. */
2507 csdir = alloc(MAXPATHL);
2508 if (csdir != NULL)
2509 {
2510 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaar28c21912013-05-29 19:18:00 +02002511 gettail((char_u *)csinfo[i].fname)
2512 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002513 len += (int)STRLEN(csdir);
2514 }
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002517 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002519 * happens, you are screwed up and need to fix how you're using cscope. */
2520 if (csinfo[i].ppath != NULL
2521 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2522 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002524 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002526 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002527 {
2528 if ((fullname = (char *)alloc(len)) != NULL)
2529 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2530 }
2531 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002532 {
2533 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002534 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002535 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002538 {
2539 fullname = (char *)vim_strsave((char_u *)name);
2540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002542 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546
2547/*
2548 * PRIVATE: cs_show
2549 *
2550 * show all cscope connections
2551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 static int
2553cs_show(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00002554 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 short i;
2557 if (cs_cnt_connections() == 0)
2558 MSG_PUTS(_("no cscope connections\n"));
2559 else
2560 {
2561 MSG_PUTS_ATTR(
2562 _(" # pid database name prepend path\n"),
2563 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002564 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 if (csinfo[i].fname == NULL)
2567 continue;
2568
2569 if (csinfo[i].ppath != NULL)
2570 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2571 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2572 else
2573 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2574 i, (long)csinfo[i].pid, csinfo[i].fname);
2575 }
2576 }
2577
2578 wait_return(TRUE);
2579 return CSCOPE_SUCCESS;
2580} /* cs_show */
2581
Bram Moolenaar02b06312007-09-06 15:39:22 +00002582
2583/*
2584 * PUBLIC: cs_end
2585 *
2586 * Only called when VIM exits to quit any cscope sessions.
2587 */
2588 void
2589cs_end()
2590{
2591 int i;
2592
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002593 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002594 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002595 vim_free(csinfo);
2596 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002597}
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#endif /* FEAT_CSCOPE */
2600
2601/* the end */