blob: 1b0a8cdad2134c011137224e17e9a5aa5bfbf830 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
6 * The basic idea/structure of cscope for Vim was borrowed from Nvi. There
7 * might be a few lines of code that look similar to what Nvi has.
8 *
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
14#if defined(FEAT_CSCOPE) || defined(PROTO)
15
16#include <string.h>
17#include <errno.h>
18#include <assert.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#if defined(UNIX)
22# include <sys/wait.h>
23#else
24 /* not UNIX, must be WIN32 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000025# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
27#include "if_cscope.h"
28
29static void cs_usage_msg __ARGS((csid_e x));
30static int cs_add __ARGS((exarg_T *eap));
31static void cs_stat_emsg __ARGS((char *fname));
32static int cs_add_common __ARGS((char *, char *, char *));
33static int cs_check_for_connections __ARGS((void));
34static int cs_check_for_tags __ARGS((void));
35static int cs_cnt_connections __ARGS((void));
36static void cs_reading_emsg __ARGS((int idx));
37static int cs_cnt_matches __ARGS((int idx));
38static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
39static int cs_create_connection __ARGS((int i));
40static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
Bram Moolenaarc716c302006-01-21 22:12:51 +000041#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void cs_file_results __ARGS((FILE *, int *));
Bram Moolenaarc716c302006-01-21 22:12:51 +000043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static void cs_fill_results __ARGS((char *, int , int *, char ***,
45 char ***, int *));
46static int cs_find __ARGS((exarg_T *eap));
Bram Moolenaarc7453f52006-02-10 23:20:28 +000047static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int cs_help __ARGS((exarg_T *eap));
49static void cs_init __ARGS((void));
50static void clear_csinfo __ARGS((int i));
51static int cs_insert_filelist __ARGS((char *, char *, char *,
52 struct stat *));
53static int cs_kill __ARGS((exarg_T *eap));
54static void cs_kill_execute __ARGS((int, char *));
55static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
56static char * cs_make_vim_style_matches __ARGS((char *, char *,
57 char *, char *));
58static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
59static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
60static char * cs_pathcomponents __ARGS((char *path));
61static void cs_print_tags_priv __ARGS((char **, char **, int));
Bram Moolenaar02b06312007-09-06 15:39:22 +000062static int cs_read_prompt __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void cs_release_csp __ARGS((int, int freefnpp));
64static int cs_reset __ARGS((exarg_T *eap));
65static char * cs_resolve_file __ARGS((int, char *));
66static int cs_show __ARGS((exarg_T *eap));
67
68
69static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS];
Bram Moolenaard2ac9842007-08-21 16:03:51 +000070static int eap_arg_len; /* length of eap->arg, set in
71 cs_lookup_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static cscmd_T cs_cmds[] =
73{
74 { "add", cs_add,
75 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
76 { "find", cs_find,
Bram Moolenaar627943d2008-08-25 02:35:59 +000077 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 { "help", cs_help,
79 N_("Show this message"), "help", 0 },
80 { "kill", cs_kill,
81 N_("Kill a connection"), "kill #", 0 },
82 { "reset", cs_reset,
83 N_("Reinit all connections"), "reset", 0 },
84 { "show", cs_show,
85 N_("Show connections"), "show", 0 },
86 { NULL }
87};
88
89 static void
90cs_usage_msg(x)
91 csid_e x;
92{
93 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
94}
95
Bram Moolenaarf4580d82009-03-18 11:52:53 +000096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
97
98static enum
99{
100 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000101 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000102 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
103 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
104} expand_what;
105
106/*
107 * Function given to ExpandGeneric() to obtain the cscope command
108 * expansion.
109 */
110/*ARGSUSED*/
111 char_u *
112get_cscope_name(xp, idx)
113 expand_T *xp;
114 int idx;
115{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000116 int current_idx;
117 int i;
118
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000119 switch (expand_what)
120 {
121 case EXP_CSCOPE_SUBCMD:
122 /* Complete with sub-commands of ":cscope":
123 * add, find, help, kill, reset, show */
124 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000125 case EXP_SCSCOPE_SUBCMD:
126 /* Complete with sub-commands of ":scscope": same sub-commands as
127 * ":cscope" but skip commands which don't support split windows */
128 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
129 if (cs_cmds[i].cansplit)
130 if (current_idx++ == idx)
131 break;
132 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000133 case EXP_CSCOPE_FIND:
134 {
135 const char *query_type[] =
136 {
137 "c", "d", "e", "f", "g", "i", "s", "t", NULL
138 };
139
140 /* Complete with query type of ":cscope find {query_type}".
141 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
142 * ..., 8) but only complete with letters, since numbers are
143 * redundant. */
144 return (char_u *)query_type[idx];
145 }
146 case EXP_CSCOPE_KILL:
147 {
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000148 static char_u connection[2];
149
150 /* ":cscope kill" accepts connection numbers or partial names of
151 * the pathname of the cscope database as argument. Only complete
152 * with connection numbers. -1 can also be used to kill all
153 * connections. */
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000154 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000155 {
156 if (csinfo[i].fname == NULL)
157 continue;
158 if (current_idx++ == idx)
159 {
160 /* Connection number fits in one character since
161 * CSCOPE_MAX_CONNECTIONS is < 10 */
162 connection[0] = i + '0';
163 connection[1] = NUL;
164 return connection;
165 }
166 }
167 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
168 }
169 default:
170 return NULL;
171 }
172}
173
174/*
175 * Handle command line completion for :cscope command.
176 */
177 void
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000178set_context_in_cscope_cmd(xp, arg, cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000179 expand_T *xp;
180 char_u *arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000181 cmdidx_T cmdidx;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000182{
183 char_u *p;
184
185 /* Default: expand subcommands */
186 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000187 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000188 expand_what = (cmdidx == CMD_scscope)
189 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000190
191 /* (part of) subcommand already typed */
192 if (*arg != NUL)
193 {
194 p = skiptowhite(arg);
195 if (*p != NUL) /* past first word */
196 {
197 xp->xp_pattern = skipwhite(p);
198 if (*skiptowhite(xp->xp_pattern) != NUL)
199 xp->xp_context = EXPAND_NOTHING;
200 else if (STRNICMP(arg, "add", p - arg) == 0)
201 xp->xp_context = EXPAND_FILES;
202 else if (STRNICMP(arg, "kill", p - arg) == 0)
203 expand_what = EXP_CSCOPE_KILL;
204 else if (STRNICMP(arg, "find", p - arg) == 0)
205 expand_what = EXP_CSCOPE_FIND;
206 else
207 xp->xp_context = EXPAND_NOTHING;
208 }
209 }
210}
211
212#endif /* FEAT_CMDL_COMPL */
213
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214/*
215 * PRIVATE: do_cscope_general
216 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000217 * Find the command, print help if invalid, and then call the corresponding
218 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 */
220 static void
221do_cscope_general(eap, make_split)
222 exarg_T *eap;
223 int make_split; /* whether to split window */
224{
225 cscmd_T *cmdp;
226
227 cs_init();
228 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
229 {
230 cs_help(eap);
231 return;
232 }
233
234#ifdef FEAT_WINDOWS
235 if (make_split)
236 {
237 if (!cmdp->cansplit)
238 {
239 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
240 return;
241 }
242 postponed_split = -1;
243 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000244 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 }
246#endif
247
248 cmdp->func(eap);
249
250#ifdef FEAT_WINDOWS
251 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000252 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#endif
254}
255
256/*
257 * PUBLIC: do_cscope
258 */
259 void
260do_cscope(eap)
261 exarg_T *eap;
262{
263 do_cscope_general(eap, FALSE);
264}
265
266/*
267 * PUBLIC: do_scscope
268 *
269 * same as do_cscope, but splits window, too.
270 */
271 void
272do_scscope(eap)
273 exarg_T *eap;
274{
275 do_cscope_general(eap, TRUE);
276}
277
278/*
279 * PUBLIC: do_cstag
280 *
281 */
282 void
283do_cstag(eap)
284 exarg_T *eap;
285{
286 int ret = FALSE;
287
288 cs_init();
289
Bram Moolenaar446cb832008-06-24 21:56:24 +0000290 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 {
292 (void)EMSG(_("E562: Usage: cstag <ident>"));
293 return;
294 }
295
296 switch (p_csto)
297 {
298 case 0 :
299 if (cs_check_for_connections())
300 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000301 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
302 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 if (ret == FALSE)
304 {
305 cs_free_tags();
306 if (msg_col)
307 msg_putchar('\n');
308
309 if (cs_check_for_tags())
310 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
311 }
312 }
313 else if (cs_check_for_tags())
314 {
315 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
316 }
317 break;
318 case 1 :
319 if (cs_check_for_tags())
320 {
321 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
322 if (ret == FALSE)
323 {
324 if (msg_col)
325 msg_putchar('\n');
326
327 if (cs_check_for_connections())
328 {
329 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000330 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (ret == FALSE)
332 cs_free_tags();
333 }
334 }
335 }
336 else if (cs_check_for_connections())
337 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000338 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
339 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (ret == FALSE)
341 cs_free_tags();
342 }
343 break;
344 default :
345 break;
346 }
347
348 if (!ret)
349 {
350 (void)EMSG(_("E257: cstag: tag not found"));
351#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
352 g_do_tagpreview = 0;
353#endif
354 }
355
356} /* do_cscope */
357
358
359/*
360 * PUBLIC: cs_find
361 *
362 * this simulates a vim_fgets(), but for cscope, returns the next line
363 * from the cscope output. should only be called from find_tags()
364 *
365 * returns TRUE if eof, FALSE otherwise
366 */
367 int
368cs_fgets(buf, size)
369 char_u *buf;
370 int size;
371{
372 char *p;
373
374 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
375 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000376 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378 return FALSE;
379} /* cs_fgets */
380
381
382/*
383 * PUBLIC: cs_free_tags
384 *
385 * called only from do_tag(), when popping the tag stack
386 */
387 void
388cs_free_tags()
389{
390 cs_manage_matches(NULL, NULL, -1, Free);
391}
392
393
394/*
395 * PUBLIC: cs_print_tags
396 *
397 * called from do_tag()
398 */
399 void
400cs_print_tags()
401{
402 cs_manage_matches(NULL, NULL, -1, Print);
403}
404
405
406/*
407 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
408 *
409 * Checks for the existence of a |cscope| connection. If no
410 * parameters are specified, then the function returns:
411 *
412 * 0, if cscope was not available (not compiled in), or if there
413 * are no cscope connections; or
414 * 1, if there is at least one cscope connection.
415 *
416 * If parameters are specified, then the value of {num}
417 * determines how existence of a cscope connection is checked:
418 *
419 * {num} Description of existence check
420 * ----- ------------------------------
421 * 0 Same as no parameters (e.g., "cscope_connection()").
422 * 1 Ignore {prepend}, and use partial string matches for
423 * {dbpath}.
424 * 2 Ignore {prepend}, and use exact string matches for
425 * {dbpath}.
426 * 3 Use {prepend}, use partial string matches for both
427 * {dbpath} and {prepend}.
428 * 4 Use {prepend}, use exact string matches for both
429 * {dbpath} and {prepend}.
430 *
431 * Note: All string comparisons are case sensitive!
432 */
433#if defined(FEAT_EVAL) || defined(PROTO)
434 int
435cs_connection(num, dbpath, ppath)
436 int num;
437 char_u *dbpath;
438 char_u *ppath;
439{
440 int i;
441
442 if (num < 0 || num > 4 || (num > 0 && !dbpath))
443 return FALSE;
444
445 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
446 {
447 if (!csinfo[i].fname)
448 continue;
449
450 if (num == 0)
451 return TRUE;
452
453 switch (num)
454 {
455 case 1:
456 if (strstr(csinfo[i].fname, (char *)dbpath))
457 return TRUE;
458 break;
459 case 2:
460 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
461 return TRUE;
462 break;
463 case 3:
464 if (strstr(csinfo[i].fname, (char *)dbpath)
465 && ((!ppath && !csinfo[i].ppath)
466 || (ppath
467 && csinfo[i].ppath
468 && strstr(csinfo[i].ppath, (char *)ppath))))
469 return TRUE;
470 break;
471 case 4:
472 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
473 && ((!ppath && !csinfo[i].ppath)
474 || (ppath
475 && csinfo[i].ppath
476 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
477 return TRUE;
478 break;
479 }
480 }
481
482 return FALSE;
483} /* cs_connection */
484#endif
485
486
487/*
488 * PRIVATE functions
489 ****************************************************************************/
490
491/*
492 * PRIVATE: cs_add
493 *
494 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000495 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 *
497 * MAXPATHL 256
498 */
499/* ARGSUSED */
500 static int
501cs_add(eap)
502 exarg_T *eap;
503{
504 char *fname, *ppath, *flags = NULL;
505
506 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
507 {
508 cs_usage_msg(Add);
509 return CSCOPE_FAILURE;
510 }
511 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
512 flags = strtok((char *)NULL, (const char *)" ");
513
514 return cs_add_common(fname, ppath, flags);
515}
516
517 static void
518cs_stat_emsg(fname)
519 char *fname;
520{
521 char *stat_emsg = _("E563: stat(%s) error: %d");
522 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
523
524 if (buf != NULL)
525 {
526 (void)sprintf(buf, stat_emsg, fname, errno);
527 (void)EMSG(buf);
528 vim_free(buf);
529 }
530 else
531 (void)EMSG(_("E563: stat error"));
532}
533
534
535/*
536 * PRIVATE: cs_add_common
537 *
538 * the common routine to add a new cscope connection. called by
539 * cs_add() and cs_reset(). i really don't like to do this, but this
540 * routine uses a number of goto statements.
541 */
542 static int
543cs_add_common(arg1, arg2, flags)
544 char *arg1; /* filename - may contain environment variables */
545 char *arg2; /* prepend path - may contain environment variables */
546 char *flags;
547{
548 struct stat statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000549 int ret;
550 char *fname = NULL;
551 char *fname2 = NULL;
552 char *ppath = NULL;
553 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000556 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 goto add_err;
558
559 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
560 ret = stat(fname, &statbuf);
561 if (ret < 0)
562 {
563staterr:
564 if (p_csverbose)
565 cs_stat_emsg(fname);
566 goto add_err;
567 }
568
569 /* get the prepend path (arg2), expand it, and try to stat it */
570 if (arg2 != NULL)
571 {
572 struct stat statbuf2;
573
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000574 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 goto add_err;
576
577 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
578 ret = stat(ppath, &statbuf2);
579 if (ret < 0)
580 goto staterr;
581 }
582
583 /* if filename is a directory, append the cscope database name to it */
584 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
585 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000586 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (fname2 == NULL)
588 goto add_err;
589
590 while (fname[strlen(fname)-1] == '/'
591#ifdef WIN32
592 || fname[strlen(fname)-1] == '\\'
593#endif
594 )
595 {
596 fname[strlen(fname)-1] = '\0';
597 if (strlen(fname) == 0)
598 break;
599 }
600 if (fname[0] == '\0')
601 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
602 else
603 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
604
605 ret = stat(fname2, &statbuf);
606 if (ret < 0)
607 {
608 if (p_csverbose)
609 cs_stat_emsg(fname2);
610 goto add_err;
611 }
612
613 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
614 }
615#if defined(UNIX)
616 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
617#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000618 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
620#endif
621 {
622 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
623 }
624 else
625 {
626 if (p_csverbose)
627 (void)EMSG2(
628 _("E564: %s is not a directory or a valid cscope database"),
629 fname);
630 goto add_err;
631 }
632
633 if (i != -1)
634 {
635 if (cs_create_connection(i) == CSCOPE_FAILURE
636 || cs_read_prompt(i) == CSCOPE_FAILURE)
637 {
638 cs_release_csp(i, TRUE);
639 goto add_err;
640 }
641
642 if (p_csverbose)
643 {
644 msg_clr_eos();
645 (void)smsg_attr(hl_attr(HLF_R),
646 (char_u *)_("Added cscope database %s"),
647 csinfo[i].fname);
648 }
649 }
650
651 vim_free(fname);
652 vim_free(fname2);
653 vim_free(ppath);
654 return CSCOPE_SUCCESS;
655
656add_err:
657 vim_free(fname2);
658 vim_free(fname);
659 vim_free(ppath);
660 return CSCOPE_FAILURE;
661} /* cs_add_common */
662
663
664 static int
665cs_check_for_connections()
666{
667 return (cs_cnt_connections() > 0);
668} /* cs_check_for_connections */
669
670
671 static int
672cs_check_for_tags()
673{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000674 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675} /* cs_check_for_tags */
676
677
678/*
679 * PRIVATE: cs_cnt_connections
680 *
681 * count the number of cscope connections
682 */
683 static int
684cs_cnt_connections()
685{
686 short i;
687 short cnt = 0;
688
689 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
690 {
691 if (csinfo[i].fname != NULL)
692 cnt++;
693 }
694 return cnt;
695} /* cs_cnt_connections */
696
697 static void
698cs_reading_emsg(idx)
699 int idx; /* connection index */
700{
701 EMSGN(_("E262: error reading cscope connection %ld"), idx);
702}
703
704#define CSREAD_BUFSIZE 2048
705/*
706 * PRIVATE: cs_cnt_matches
707 *
708 * count the number of matches for a given cscope connection.
709 */
710 static int
711cs_cnt_matches(idx)
712 int idx;
713{
714 char *stok;
715 char *buf;
716 int nlines;
717
718 buf = (char *)alloc(CSREAD_BUFSIZE);
719 if (buf == NULL)
720 return 0;
721 for (;;)
722 {
723 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
724 {
725 if (feof(csinfo[idx].fr_fp))
726 errno = EIO;
727
728 cs_reading_emsg(idx);
729
730 vim_free(buf);
731 return -1;
732 }
733
734 /*
735 * If the database is out of date, or there's some other problem,
736 * cscope will output error messages before the number-of-lines output.
737 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000738 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 */
740 if ((stok = strtok(buf, (const char *)" ")) == NULL)
741 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000742 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 continue;
744
745 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
746 continue;
747 nlines = atoi(stok);
748 if (nlines < 0)
749 {
750 nlines = 0;
751 break;
752 }
753
754 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
755 continue;
756 if (strncmp((const char *)stok, "lines", 5))
757 continue;
758
759 break;
760 }
761
762 vim_free(buf);
763 return nlines;
764} /* cs_cnt_matches */
765
766
767/*
768 * PRIVATE: cs_create_cmd
769 *
770 * Creates the actual cscope command query from what the user entered.
771 */
772 static char *
773cs_create_cmd(csoption, pattern)
774 char *csoption;
775 char *pattern;
776{
777 char *cmd;
778 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000779 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 switch (csoption[0])
782 {
783 case '0' : case 's' :
784 search = 0;
785 break;
786 case '1' : case 'g' :
787 search = 1;
788 break;
789 case '2' : case 'd' :
790 search = 2;
791 break;
792 case '3' : case 'c' :
793 search = 3;
794 break;
795 case '4' : case 't' :
796 search = 4;
797 break;
798 case '6' : case 'e' :
799 search = 6;
800 break;
801 case '7' : case 'f' :
802 search = 7;
803 break;
804 case '8' : case 'i' :
805 search = 8;
806 break;
807 default :
808 (void)EMSG(_("E561: unknown cscope search type"));
809 cs_usage_msg(Find);
810 return NULL;
811 }
812
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000813 /* Skip white space before the patter, except for text and pattern search,
814 * they may want to use the leading white space. */
815 pat = pattern;
816 if (search != 4 && search != 6)
817 while vim_iswhite(*pat)
818 ++pat;
819
820 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 return NULL;
822
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000823 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 return cmd;
826} /* cs_create_cmd */
827
828
829/*
830 * PRIVATE: cs_create_connection
831 *
832 * This piece of code was taken/adapted from nvi. do we need to add
833 * the BSD license notice?
834 */
835 static int
836cs_create_connection(i)
837 int i;
838{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000839#ifdef UNIX
840 int to_cs[2], from_cs[2];
841#endif
842 int len;
843 char *prog, *cmd, *ppath = NULL;
844#ifdef WIN32
845 int fd;
846 SECURITY_ATTRIBUTES sa;
847 PROCESS_INFORMATION pi;
848 STARTUPINFO si;
849 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
850 HANDLE stdin_rd, stdout_rd;
851 HANDLE stdout_wr, stdin_wr;
852 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000853# ifdef __BORLANDC__
854# define OPEN_OH_ARGTYPE long
855# else
856# if (_MSC_VER >= 1300)
857# define OPEN_OH_ARGTYPE intptr_t
858# else
859# define OPEN_OH_ARGTYPE long
860# endif
861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863
Bram Moolenaar02b06312007-09-06 15:39:22 +0000864#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 /*
866 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
867 * from_cs[0] and writes to to_cs[1].
868 */
869 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
870 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
871 {
872 (void)EMSG(_("E566: Could not create cscope pipes"));
873err_closing:
874 if (to_cs[0] != -1)
875 (void)close(to_cs[0]);
876 if (to_cs[1] != -1)
877 (void)close(to_cs[1]);
878 if (from_cs[0] != -1)
879 (void)close(from_cs[0]);
880 if (from_cs[1] != -1)
881 (void)close(from_cs[1]);
882 return CSCOPE_FAILURE;
883 }
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 switch (csinfo[i].pid = fork())
886 {
887 case -1:
888 (void)EMSG(_("E622: Could not fork for cscope"));
889 goto err_closing;
890 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (dup2(to_cs[0], STDIN_FILENO) == -1)
892 PERROR("cs_create_connection 1");
893 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
894 PERROR("cs_create_connection 2");
895 if (dup2(from_cs[1], STDERR_FILENO) == -1)
896 PERROR("cs_create_connection 3");
897
898 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 (void)close(to_cs[1]);
900 (void)close(from_cs[0]);
901#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000902 /* WIN32 */
903 /* Create pipes to communicate with cscope */
904 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
905 sa.bInheritHandle = TRUE;
906 sa.lpSecurityDescriptor = NULL;
907
908 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
909 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
910 {
911 (void)EMSG(_("E566: Could not create cscope pipes"));
912err_closing:
913 if (pipe_stdin)
914 {
915 CloseHandle(stdin_rd);
916 CloseHandle(stdin_wr);
917 }
918 if (pipe_stdout)
919 {
920 CloseHandle(stdout_rd);
921 CloseHandle(stdout_wr);
922 }
923 return CSCOPE_FAILURE;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#endif
926 /* expand the cscope exec for env var's */
927 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
928 {
929#ifdef UNIX
930 return CSCOPE_FAILURE;
931#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000932 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 goto err_closing;
934#endif
935 }
936 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
937
938 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000939 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (csinfo[i].ppath)
941 {
942 /* expand the prepend path for env var's */
943 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
944 {
945 vim_free(prog);
946#ifdef UNIX
947 return CSCOPE_FAILURE;
948#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000949 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 goto err_closing;
951#endif
952 }
953 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
954
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000955 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
958 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000959 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 if ((cmd = (char *)alloc(len)) == NULL)
962 {
963 vim_free(prog);
964 vim_free(ppath);
965#ifdef UNIX
966 return CSCOPE_FAILURE;
967#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000968 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 goto err_closing;
970#endif
971 }
972
973 /* run the cscope command; is there execl for non-unix systems? */
974#if defined(UNIX)
975 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
976#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000977 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
979#endif
980 if (csinfo[i].ppath != NULL)
981 {
982 (void)strcat(cmd, " -P");
983 (void)strcat(cmd, csinfo[i].ppath);
984 }
985 if (csinfo[i].flags != NULL)
986 {
987 (void)strcat(cmd, " ");
988 (void)strcat(cmd, csinfo[i].flags);
989 }
990# ifdef UNIX
991 /* on Win32 we still need prog */
992 vim_free(prog);
993# endif
994 vim_free(ppath);
995
996#if defined(UNIX)
997 if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
998 PERROR(_("cs_create_connection exec failed"));
999
1000 exit(127);
1001 /* NOTREACHED */
1002 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * Save the file descriptors for later duplication, and
1005 * reopen as streams.
1006 */
1007 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1009 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1010 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /* close unused */
1013 (void)close(to_cs[0]);
1014 (void)close(from_cs[1]);
1015
1016 break;
1017 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001020 /* WIN32 */
1021 /* Create a new process to run cscope and use pipes to talk with it */
1022 GetStartupInfo(&si);
1023 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1024 si.wShowWindow = SW_HIDE; /* Hide child application window */
1025 si.hStdOutput = stdout_wr;
1026 si.hStdError = stdout_wr;
1027 si.hStdInput = stdin_rd;
1028 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1029 NULL, NULL, &si, &pi);
1030 vim_free(prog);
1031 vim_free(cmd);
1032
1033 if (!created)
1034 {
1035 PERROR(_("cs_create_connection exec failed"));
1036 (void)EMSG(_("E623: Could not spawn cscope process"));
1037 goto err_closing;
1038 }
1039 /* else */
1040 csinfo[i].pid = pi.dwProcessId;
1041 csinfo[i].hProc = pi.hProcess;
1042 CloseHandle(pi.hThread);
1043
1044 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001045 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1046 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001047 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1048 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001049 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1050 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001051 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1052 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1053
1054 /* Close handles for file descriptors inherited by the cscope process */
1055 CloseHandle(stdin_rd);
1056 CloseHandle(stdout_wr);
1057
1058#endif /* !UNIX */
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 return CSCOPE_SUCCESS;
1061} /* cs_create_connection */
1062
1063
1064/*
1065 * PRIVATE: cs_find
1066 *
1067 * query cscope using command line interface. parse the output and use tselect
1068 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1069 *
1070 * returns TRUE if we jump to a tag or abort, FALSE if not.
1071 */
1072 static int
1073cs_find(eap)
1074 exarg_T *eap;
1075{
1076 char *opt, *pat;
1077
1078 if (cs_check_for_connections() == FALSE)
1079 {
1080 (void)EMSG(_("E567: no cscope connections"));
1081 return FALSE;
1082 }
1083
1084 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1085 {
1086 cs_usage_msg(Find);
1087 return FALSE;
1088 }
1089
1090 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001091 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 cs_usage_msg(Find);
1094 return FALSE;
1095 }
1096
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001097 return cs_find_common(opt, pat, eap->forceit, TRUE,
1098 eap->cmdidx == CMD_lcscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099} /* cs_find */
1100
1101
1102/*
1103 * PRIVATE: cs_find_common
1104 *
1105 * common code for cscope find, shared by cs_find() and do_cstag()
1106 */
1107 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001108cs_find_common(opt, pat, forceit, verbose, use_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char *opt;
1110 char *pat;
1111 int forceit;
1112 int verbose;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001113 int use_ll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int i;
1116 char *cmd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001117 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_QUICKFIX
1119 char cmdletter;
1120 char *qfpos;
1121#endif
1122
1123 /* create the actual command to send to cscope */
1124 cmd = cs_create_cmd(opt, pat);
1125 if (cmd == NULL)
1126 return FALSE;
1127
1128 /* send query to all open connections, then count the total number
1129 * of matches so we can alloc matchesp all in one swell foop
1130 */
1131 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1132 nummatches[i] = 0;
1133 totmatches = 0;
1134 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1135 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001136 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 continue;
1138
1139 /* send cmd to cscope */
1140 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1141 (void)fflush(csinfo[i].to_fp);
1142
1143 nummatches[i] = cs_cnt_matches(i);
1144
1145 if (nummatches[i] > -1)
1146 totmatches += nummatches[i];
1147
1148 if (nummatches[i] == 0)
1149 (void)cs_read_prompt(i);
1150 }
1151 vim_free(cmd);
1152
1153 if (totmatches == 0)
1154 {
1155 char *nf = _("E259: no matches found for cscope query %s of %s");
1156 char *buf;
1157
1158 if (!verbose)
1159 return FALSE;
1160
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001161 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (buf == NULL)
1163 (void)EMSG(nf);
1164 else
1165 {
1166 sprintf(buf, nf, opt, pat);
1167 (void)EMSG(buf);
1168 vim_free(buf);
1169 }
1170 return FALSE;
1171 }
1172
1173#ifdef FEAT_QUICKFIX
1174 /* get cmd letter */
1175 switch (opt[0])
1176 {
1177 case '0' :
1178 cmdletter = 's';
1179 break;
1180 case '1' :
1181 cmdletter = 'g';
1182 break;
1183 case '2' :
1184 cmdletter = 'd';
1185 break;
1186 case '3' :
1187 cmdletter = 'c';
1188 break;
1189 case '4' :
1190 cmdletter = 't';
1191 break;
1192 case '6' :
1193 cmdletter = 'e';
1194 break;
1195 case '7' :
1196 cmdletter = 'f';
1197 break;
1198 case '8' :
1199 cmdletter = 'i';
1200 break;
1201 default :
1202 cmdletter = opt[0];
1203 }
1204
1205 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1206 if (qfpos != NULL)
1207 {
1208 qfpos++;
1209 /* next symbol must be + or - */
1210 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1211 {
1212 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001213 char *buf = (char *)alloc((unsigned)strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 /* strlen will be enough because we use chars */
1216 if (buf != NULL)
1217 {
1218 sprintf(buf, nf, *qfpos, *(qfpos-1));
1219 (void)EMSG(buf);
1220 vim_free(buf);
1221 }
1222 return FALSE;
1223 }
1224 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
1227 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001228 FILE *f;
1229 char_u *tmp = vim_tempname('c');
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001230 qf_info_T *qi = NULL;
1231 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001233 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001234 if (f == NULL)
1235 EMSG2(_(e_notopen), tmp);
1236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 cs_file_results(f, nummatches);
1239 fclose(f);
1240 if (use_ll) /* Use location list */
1241 wp = curwin;
1242 /* '-' starts a new error list */
1243 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1244 *qfpos == '-') > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001246# ifdef FEAT_WINDOWS
1247 if (postponed_split != 0)
1248 {
1249 win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 postponed_split_flags);
1251# ifdef FEAT_SCROLLBIND
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001252 curwin->w_p_scb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001254 postponed_split = 0;
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001257 if (use_ll)
1258 /*
1259 * In the location list window, use the displayed location
1260 * list. Otherwise, use the location list for the window.
1261 */
1262 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1263 ? wp->w_llist_ref : wp->w_llist;
1264 qf_jump(qi, 0, 0, forceit);
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 mch_remove(tmp);
1268 vim_free(tmp);
1269 return TRUE;
1270 }
1271 else
1272#endif /* FEAT_QUICKFIX */
1273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001274 char **matches = NULL, **contexts = NULL;
1275 int matched = 0;
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 /* read output */
1278 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1279 &contexts, &matched);
1280 if (matches == NULL)
1281 return FALSE;
1282
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001283 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1286 }
1287
1288} /* cs_find_common */
1289
1290/*
1291 * PRIVATE: cs_help
1292 *
1293 * print help
1294 */
1295/* ARGSUSED */
1296 static int
1297cs_help(eap)
1298 exarg_T *eap;
1299{
1300 cscmd_T *cmdp = cs_cmds;
1301
1302 (void)MSG_PUTS(_("cscope commands:\n"));
1303 while (cmdp->name != NULL)
1304 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001305 char *help = _(cmdp->help);
1306 int space_cnt = 30 - vim_strsize((char_u *)help);
1307
1308 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1309 if (space_cnt < 0)
1310 space_cnt = 0;
1311 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1312 cmdp->name,
1313 help, space_cnt, " ",
1314 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001316 MSG_PUTS(_("\n"
1317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
1324 " t: Find assignments to\n"));
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 cmdp++;
1327 }
1328
1329 wait_return(TRUE);
1330 return 0;
1331} /* cs_help */
1332
1333
1334/*
1335 * PRIVATE: cs_init
1336 *
1337 * initialize cscope structure if not already
1338 */
1339 static void
1340cs_init()
1341{
1342 short i;
1343 static int init_already = FALSE;
1344
1345 if (init_already)
1346 return;
1347
1348 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1349 clear_csinfo(i);
1350
1351 init_already = TRUE;
1352} /* cs_init */
1353
1354 static void
1355clear_csinfo(i)
1356 int i;
1357{
1358 csinfo[i].fname = NULL;
1359 csinfo[i].ppath = NULL;
1360 csinfo[i].flags = NULL;
1361#if defined(UNIX)
1362 csinfo[i].st_dev = (dev_t)0;
1363 csinfo[i].st_ino = (ino_t)0;
1364#else
1365 csinfo[i].nVolume = 0;
1366 csinfo[i].nIndexHigh = 0;
1367 csinfo[i].nIndexLow = 0;
1368#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001369 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 csinfo[i].fr_fp = NULL;
1371 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001372#if defined(WIN32)
1373 csinfo[i].hProc = NULL;
1374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375}
1376
1377#ifndef UNIX
1378static char *GetWin32Error __ARGS((void));
1379
1380 static char *
1381GetWin32Error()
1382{
1383 char *msg = NULL;
1384 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1385 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1386 if (msg != NULL)
1387 {
1388 /* remove trailing \r\n */
1389 char *pcrlf = strstr(msg, "\r\n");
1390 if (pcrlf != NULL)
1391 *pcrlf = '\0';
1392 }
1393 return msg;
1394}
1395#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/*
1398 * PRIVATE: cs_insert_filelist
1399 *
1400 * insert a new cscope database filename into the filelist
1401 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 static int
1404cs_insert_filelist(fname, ppath, flags, sb)
1405 char *fname;
1406 char *ppath;
1407 char *flags;
1408 struct stat *sb;
1409{
1410 short i, j;
1411#ifndef UNIX
1412 HANDLE hFile;
1413 BY_HANDLE_FILE_INFORMATION bhfi;
1414
1415 vim_memset(&bhfi, 0, sizeof(bhfi));
1416 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1417 if (!mch_windows95())
1418 {
1419 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1420 FILE_ATTRIBUTE_NORMAL, NULL);
1421 if (hFile == INVALID_HANDLE_VALUE)
1422 {
1423 if (p_csverbose)
1424 {
1425 char *cant_msg = _("E625: cannot open cscope database: %s");
1426 char *winmsg = GetWin32Error();
1427
1428 if (winmsg != NULL)
1429 {
1430 (void)EMSG2(cant_msg, winmsg);
1431 LocalFree(winmsg);
1432 }
1433 else
1434 /* subst filename if can't get error text */
1435 (void)EMSG2(cant_msg, fname);
1436 }
1437 return -1;
1438 }
1439 if (!GetFileInformationByHandle(hFile, &bhfi))
1440 {
1441 CloseHandle(hFile);
1442 if (p_csverbose)
1443 (void)EMSG(_("E626: cannot get cscope database information"));
1444 return -1;
1445 }
1446 CloseHandle(hFile);
1447 }
1448#endif
1449
1450 i = -1; /* can be set to the index of an empty item in csinfo */
1451 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1452 {
1453 if (csinfo[j].fname != NULL
1454#if defined(UNIX)
1455 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1456#else
1457 /* compare pathnames first */
1458 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001459 /* if not Windows 9x, test index file attributes too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 || (!mch_windows95()
1461 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1462 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1463 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1464#endif
1465 )
1466 {
1467 if (p_csverbose)
1468 (void)EMSG(_("E568: duplicate cscope database not added"));
1469 return -1;
1470 }
1471
1472 if (csinfo[j].fname == NULL && i == -1)
1473 i = j; /* remember first empty entry */
1474 }
1475
1476 if (i == -1)
1477 {
1478 if (p_csverbose)
1479 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1480 return -1;
1481 }
1482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001483 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return -1;
1485
1486 (void)strcpy(csinfo[i].fname, (const char *)fname);
1487
1488 if (ppath != NULL)
1489 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001490 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 vim_free(csinfo[i].fname);
1493 csinfo[i].fname = NULL;
1494 return -1;
1495 }
1496 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1497 } else
1498 csinfo[i].ppath = NULL;
1499
1500 if (flags != NULL)
1501 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001502 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 vim_free(csinfo[i].fname);
1505 vim_free(csinfo[i].ppath);
1506 csinfo[i].fname = NULL;
1507 csinfo[i].ppath = NULL;
1508 return -1;
1509 }
1510 (void)strcpy(csinfo[i].flags, (const char *)flags);
1511 } else
1512 csinfo[i].flags = NULL;
1513
1514#if defined(UNIX)
1515 csinfo[i].st_dev = sb->st_dev;
1516 csinfo[i].st_ino = sb->st_ino;
1517
1518#else
1519 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1520 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1521 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1522#endif
1523 return i;
1524} /* cs_insert_filelist */
1525
1526
1527/*
1528 * PRIVATE: cs_lookup_cmd
1529 *
1530 * find cscope command in command table
1531 */
1532 static cscmd_T *
1533cs_lookup_cmd(eap)
1534 exarg_T *eap;
1535{
1536 cscmd_T *cmdp;
1537 char *stok;
1538 size_t len;
1539
1540 if (eap->arg == NULL)
1541 return NULL;
1542
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001543 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001544 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1547 return NULL;
1548
1549 len = strlen(stok);
1550 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1551 {
1552 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1553 return (cmdp);
1554 }
1555 return NULL;
1556} /* cs_lookup_cmd */
1557
1558
1559/*
1560 * PRIVATE: cs_kill
1561 *
1562 * nuke em
1563 */
1564/* ARGSUSED */
1565 static int
1566cs_kill(eap)
1567 exarg_T *eap;
1568{
1569 char *stok;
1570 short i;
1571
1572 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1573 {
1574 cs_usage_msg(Kill);
1575 return CSCOPE_FAILURE;
1576 }
1577
1578 /* only single digit positive and negative integers are allowed */
1579 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1580 || (strlen(stok) < 3 && stok[0] == '-'
1581 && VIM_ISDIGIT((int)(stok[1]))))
1582 i = atoi(stok);
1583 else
1584 {
1585 /* It must be part of a name. We will try to find a match
1586 * within all the names in the csinfo data structure
1587 */
1588 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1589 {
1590 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1591 break;
1592 }
1593 }
1594
1595 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1596 && i != -1)
1597 {
1598 if (p_csverbose)
1599 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1600 }
1601 else
1602 {
1603 if (i == -1)
1604 {
1605 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1606 {
1607 if (csinfo[i].fname)
1608 cs_kill_execute(i, csinfo[i].fname);
1609 }
1610 }
1611 else
1612 cs_kill_execute(i, stok);
1613 }
1614
1615 return 0;
1616} /* cs_kill */
1617
1618
1619/*
1620 * PRIVATE: cs_kill_execute
1621 *
1622 * Actually kills a specific cscope connection.
1623 */
1624 static void
1625cs_kill_execute(i, cname)
1626 int i; /* cscope table index */
1627 char *cname; /* cscope database name */
1628{
1629 if (p_csverbose)
1630 {
1631 msg_clr_eos();
1632 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1633 (char_u *)_("cscope connection %s closed"), cname);
1634 }
1635 cs_release_csp(i, TRUE);
1636}
1637
1638
1639/*
1640 * PRIVATE: cs_make_vim_style_matches
1641 *
1642 * convert the cscope output into into a ctags style entry (as might be found
1643 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1644 * the type of the tag you are looking for. for example, in Darren Hiebert's
1645 * ctags (the one that comes with vim), #define's use a line number to find the
1646 * tag in a file while function definitions use a regexp search pattern.
1647 *
1648 * i'm going to always use the line number because cscope does something
1649 * quirky (and probably other things i don't know about):
1650 *
1651 * if you have "# define" in your source file, which is
1652 * perfectly legal, cscope thinks you have "#define". this
1653 * will result in a failed regexp search. :(
1654 *
1655 * besides, even if this particular case didn't happen, the search pattern
1656 * would still have to be modified to escape all the special regular expression
1657 * characters to comply with ctags formatting.
1658 */
1659 static char *
1660cs_make_vim_style_matches(fname, slno, search, tagstr)
1661 char *fname;
1662 char *slno;
1663 char *search;
1664 char *tagstr;
1665{
1666 /* vim style is ctags:
1667 *
1668 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1669 *
1670 * but as mentioned above, we'll always use the line number and
1671 * put the search pattern (if one exists) as "extra"
1672 *
1673 * buf is used as part of vim's method of handling tags, and
1674 * (i think) vim frees it when you pop your tags and get replaced
1675 * by new ones on the tag stack.
1676 */
1677 char *buf;
1678 int amt;
1679
1680 if (search != NULL)
1681 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001682 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if ((buf = (char *)alloc(amt)) == NULL)
1684 return NULL;
1685
1686 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1687 }
1688 else
1689 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001690 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if ((buf = (char *)alloc(amt)) == NULL)
1692 return NULL;
1693
1694 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1695 }
1696
1697 return buf;
1698} /* cs_make_vim_style_matches */
1699
1700
1701/*
1702 * PRIVATE: cs_manage_matches
1703 *
1704 * this is kind of hokey, but i don't see an easy way round this..
1705 *
1706 * Store: keep a ptr to the (malloc'd) memory of matches originally
1707 * generated from cs_find(). the matches are originally lines directly
1708 * from cscope output, but transformed to look like something out of a
1709 * ctags. see cs_make_vim_style_matches for more details.
1710 *
1711 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1712 * the next line from the cscope output. it basically keeps track of which
1713 * lines have been "used" and returns the next one.
1714 *
1715 * Free: frees up everything and resets
1716 *
1717 * Print: prints the tags
1718 */
1719 static char *
1720cs_manage_matches(matches, contexts, totmatches, cmd)
1721 char **matches;
1722 char **contexts;
1723 int totmatches;
1724 mcmd_e cmd;
1725{
1726 static char **mp = NULL;
1727 static char **cp = NULL;
1728 static int cnt = -1;
1729 static int next = -1;
1730 char *p = NULL;
1731
1732 switch (cmd)
1733 {
1734 case Store:
1735 assert(matches != NULL);
1736 assert(totmatches > 0);
1737 if (mp != NULL || cp != NULL)
1738 (void)cs_manage_matches(NULL, NULL, -1, Free);
1739 mp = matches;
1740 cp = contexts;
1741 cnt = totmatches;
1742 next = 0;
1743 break;
1744 case Get:
1745 if (next >= cnt)
1746 return NULL;
1747
1748 p = mp[next];
1749 next++;
1750 break;
1751 case Free:
1752 if (mp != NULL)
1753 {
1754 if (cnt > 0)
1755 while (cnt--)
1756 {
1757 vim_free(mp[cnt]);
1758 if (cp != NULL)
1759 vim_free(cp[cnt]);
1760 }
1761 vim_free(mp);
1762 vim_free(cp);
1763 }
1764 mp = NULL;
1765 cp = NULL;
1766 cnt = 0;
1767 next = 0;
1768 break;
1769 case Print:
1770 cs_print_tags_priv(mp, cp, cnt);
1771 break;
1772 default: /* should not reach here */
1773 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1774 return NULL;
1775 }
1776
1777 return p;
1778} /* cs_manage_matches */
1779
1780
1781/*
1782 * PRIVATE: cs_parse_results
1783 *
1784 * parse cscope output
1785 */
1786 static char *
1787cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1788 int cnumber;
1789 char *buf;
1790 int bufsize;
1791 char **context;
1792 char **linenumber;
1793 char **search;
1794{
1795 int ch;
1796 char *p;
1797 char *name;
1798
1799 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1800 {
1801 if (feof(csinfo[cnumber].fr_fp))
1802 errno = EIO;
1803
1804 cs_reading_emsg(cnumber);
1805
1806 return NULL;
1807 }
1808
1809 /* If the line's too long for the buffer, discard it. */
1810 if ((p = strchr(buf, '\n')) == NULL)
1811 {
1812 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1813 ;
1814 return NULL;
1815 }
1816 *p = '\0';
1817
1818 /*
1819 * cscope output is in the following format:
1820 *
1821 * <filename> <context> <line number> <pattern>
1822 */
1823 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1824 return NULL;
1825 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1826 return NULL;
1827 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1828 return NULL;
1829 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1830
1831 /* --- nvi ---
1832 * If the file is older than the cscope database, that is,
1833 * the database was built since the file was last modified,
1834 * or there wasn't a search string, use the line number.
1835 */
1836 if (strcmp(*search, "<unknown>") == 0)
1837 *search = NULL;
1838
1839 name = cs_resolve_file(cnumber, name);
1840 return name;
1841}
1842
Bram Moolenaarc716c302006-01-21 22:12:51 +00001843#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844/*
1845 * PRIVATE: cs_file_results
1846 *
1847 * write cscope find results to file
1848 */
1849 static void
1850cs_file_results(f, nummatches_a)
1851 FILE *f;
1852 int *nummatches_a;
1853{
1854 int i, j;
1855 char *buf;
1856 char *search, *slno;
1857 char *fullname;
1858 char *cntx;
1859 char *context;
1860
1861 buf = (char *)alloc(CSREAD_BUFSIZE);
1862 if (buf == NULL)
1863 return;
1864
1865 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1866 {
1867 if (nummatches_a[i] < 1)
1868 continue;
1869
1870 for (j = 0; j < nummatches_a[i]; j++)
1871 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001872 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1873 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 continue;
1875
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001876 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001877 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 continue;
1879
1880 if (strcmp(cntx, "<global>")==0)
1881 strcpy(context, "<<global>>");
1882 else
1883 sprintf(context, "<<%s>>", cntx);
1884
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001885 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1887 else
1888 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1889
1890 vim_free(context);
1891 vim_free(fullname);
1892 } /* for all matches */
1893
1894 (void)cs_read_prompt(i);
1895
1896 } /* for all cscope connections */
1897 vim_free(buf);
1898}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901/*
1902 * PRIVATE: cs_fill_results
1903 *
1904 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1905 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001906 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 static void
1909cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1910 char *tagstr;
1911 int totmatches;
1912 int *nummatches_a;
1913 char ***matches_p;
1914 char ***cntxts_p;
1915 int *matched;
1916{
1917 int i, j;
1918 char *buf;
1919 char *search, *slno;
1920 int totsofar = 0;
1921 char **matches = NULL;
1922 char **cntxts = NULL;
1923 char *fullname;
1924 char *cntx;
1925
1926 assert(totmatches > 0);
1927
1928 buf = (char *)alloc(CSREAD_BUFSIZE);
1929 if (buf == NULL)
1930 return;
1931
1932 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1933 goto parse_out;
1934 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1935 goto parse_out;
1936
1937 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1938 {
1939 if (nummatches_a[i] < 1)
1940 continue;
1941
1942 for (j = 0; j < nummatches_a[i]; j++)
1943 {
1944 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1945 &slno, &search)) == NULL)
1946 continue;
1947
1948 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1949 search, tagstr);
1950
1951 vim_free(fullname);
1952
1953 if (strcmp(cntx, "<global>") == 0)
1954 cntxts[totsofar] = NULL;
1955 else
1956 /* note: if vim_strsave returns NULL, then the context
1957 * will be "<global>", which is misleading.
1958 */
1959 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1960
1961 if (matches[totsofar] != NULL)
1962 totsofar++;
1963
1964 } /* for all matches */
1965
1966 (void)cs_read_prompt(i);
1967
1968 } /* for all cscope connections */
1969
1970parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001971 if (totsofar == 0)
1972 {
1973 /* No matches, free the arrays and return NULL in "*matches_p". */
1974 vim_free(matches);
1975 matches = NULL;
1976 vim_free(cntxts);
1977 cntxts = NULL;
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 *matched = totsofar;
1980 *matches_p = matches;
1981 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 vim_free(buf);
1984} /* cs_fill_results */
1985
1986
1987/* get the requested path components */
1988 static char *
1989cs_pathcomponents(path)
1990 char *path;
1991{
1992 int i;
1993 char *s;
1994
1995 if (p_cspc == 0)
1996 return path;
1997
1998 s = path + strlen(path) - 1;
1999 for (i = 0; i < p_cspc; ++i)
2000 while (s > path && *--s != '/'
2001#ifdef WIN32
2002 && *--s != '\\'
2003#endif
2004 )
2005 ;
2006 if ((s > path && *s == '/')
2007#ifdef WIN32
2008 || (s > path && *s == '\\')
2009#endif
2010 )
2011 ++s;
2012 return s;
2013}
2014
2015/*
2016 * PRIVATE: cs_print_tags_priv
2017 *
2018 * called from cs_manage_matches()
2019 */
2020 static void
2021cs_print_tags_priv(matches, cntxts, num_matches)
2022 char **matches;
2023 char **cntxts;
2024 int num_matches;
2025{
2026 char *buf = NULL;
2027 int bufsize = 0; /* Track available bufsize */
2028 int newsize = 0;
2029 char *ptag;
2030 char *fname, *lno, *extra, *tbuf;
2031 int i, idx, num;
2032 char *globalcntx = "GLOBAL";
2033 char *cntxformat = " <<%s>>";
2034 char *context;
2035 char *cstag_msg = _("Cscope tag: %s");
2036 char *csfmt_str = "%4d %6s ";
2037
2038 assert (num_matches > 0);
2039
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002040 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return;
2042
2043 strcpy(tbuf, matches[0]);
2044 ptag = strtok(tbuf, "\t");
2045
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002046 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 buf = (char *)alloc(newsize);
2048 if (buf != NULL)
2049 {
2050 bufsize = newsize;
2051 (void)sprintf(buf, cstag_msg, ptag);
2052 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2053 }
2054
2055 vim_free(tbuf);
2056
2057 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2058 msg_advance(msg_col + 2);
2059 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2060
2061 num = 1;
2062 for (i = 0; i < num_matches; i++)
2063 {
2064 idx = i;
2065
2066 /* if we really wanted to, we could avoid this malloc and strcpy
2067 * by parsing matches[i] on the fly and placing stuff into buf
2068 * directly, but that's too much of a hassle
2069 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002070 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 continue;
2072 (void)strcpy(tbuf, matches[idx]);
2073
2074 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
2075 continue;
2076 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2077 continue;
2078 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002079 continue;
2080 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2083
2084 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002085 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (bufsize < newsize)
2087 {
2088 buf = (char *)vim_realloc(buf, newsize);
2089 if (buf == NULL)
2090 bufsize = 0;
2091 else
2092 bufsize = newsize;
2093 }
2094 if (buf != NULL)
2095 {
2096 /* csfmt_str = "%4d %6s "; */
2097 (void)sprintf(buf, csfmt_str, num, lno);
2098 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2099 }
2100 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2101
2102 /* compute the required space for the context */
2103 if (cntxts[idx] != NULL)
2104 context = cntxts[idx];
2105 else
2106 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002107 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
2109 if (bufsize < newsize)
2110 {
2111 buf = (char *)vim_realloc(buf, newsize);
2112 if (buf == NULL)
2113 bufsize = 0;
2114 else
2115 bufsize = newsize;
2116 }
2117 if (buf != NULL)
2118 {
2119 (void)sprintf(buf, cntxformat, context);
2120
2121 /* print the context only if it fits on the same line */
2122 if (msg_col + (int)strlen(buf) >= (int)Columns)
2123 msg_putchar('\n');
2124 msg_advance(12);
2125 MSG_PUTS_LONG(buf);
2126 msg_putchar('\n');
2127 }
2128 if (extra != NULL)
2129 {
2130 msg_advance(13);
2131 MSG_PUTS_LONG(extra);
2132 }
2133
2134 vim_free(tbuf); /* only after printing extra due to strtok use */
2135
2136 if (msg_col)
2137 msg_putchar('\n');
2138
2139 ui_breakcheck();
2140 if (got_int)
2141 {
2142 got_int = FALSE; /* don't print any more matches */
2143 break;
2144 }
2145
2146 num++;
2147 } /* for all matches */
2148
2149 vim_free(buf);
2150} /* cs_print_tags_priv */
2151
2152
2153/*
2154 * PRIVATE: cs_read_prompt
2155 *
2156 * read a cscope prompt (basically, skip over the ">> ")
2157 */
2158 static int
2159cs_read_prompt(i)
2160 int i;
2161{
2162 int ch;
2163 char *buf = NULL; /* buffer for possible error message from cscope */
2164 int bufpos = 0;
2165 char *cs_emsg;
2166 int maxlen;
2167 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002168 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 int n;
2170
2171 cs_emsg = _("E609: Cscope error: %s");
2172 /* compute maximum allowed len for Cscope error message */
2173 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2174
2175 for (;;)
2176 {
2177 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2178 /* if there is room and char is printable */
2179 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2180 {
2181 if (buf == NULL) /* lazy buffer allocation */
2182 buf = (char *)alloc(maxlen);
2183 if (buf != NULL)
2184 {
2185 /* append character to the message */
2186 buf[bufpos++] = ch;
2187 buf[bufpos] = NUL;
2188 if (bufpos >= epromptlen
2189 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2190 {
2191 /* remove eprompt from buf */
2192 buf[bufpos - epromptlen] = NUL;
2193
2194 /* print message to user */
2195 (void)EMSG2(cs_emsg, buf);
2196
2197 /* send RETURN to cscope */
2198 (void)putc('\n', csinfo[i].to_fp);
2199 (void)fflush(csinfo[i].to_fp);
2200
2201 /* clear buf */
2202 bufpos = 0;
2203 buf[bufpos] = NUL;
2204 }
2205 }
2206 }
2207
2208 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2209 {
2210 if (n > 0)
2211 ch = getc(csinfo[i].fr_fp);
2212 if (ch == EOF)
2213 {
2214 PERROR("cs_read_prompt EOF");
2215 if (buf != NULL && buf[0] != NUL)
2216 (void)EMSG2(cs_emsg, buf);
2217 else if (p_csverbose)
2218 cs_reading_emsg(i); /* don't have additional information */
2219 cs_release_csp(i, TRUE);
2220 vim_free(buf);
2221 return CSCOPE_FAILURE;
2222 }
2223
2224 if (ch != CSCOPE_PROMPT[n])
2225 {
2226 ch = EOF;
2227 break;
2228 }
2229 }
2230
2231 if (ch == EOF)
2232 continue; /* didn't find the prompt */
2233 break; /* did find the prompt */
2234 }
2235
2236 vim_free(buf);
2237 return CSCOPE_SUCCESS;
2238}
2239
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002240#if defined(UNIX) && defined(SIGALRM)
2241/*
2242 * Used to catch and ignore SIGALRM below.
2243 */
2244/* ARGSUSED */
2245 static RETSIGTYPE
2246sig_handler SIGDEFARG(sigarg)
2247{
2248 /* do nothing */
2249 SIGRETURN;
2250}
2251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253/*
2254 * PRIVATE: cs_release_csp
2255 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002256 * Does the actual free'ing for the cs ptr with an optional flag of whether
2257 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
2259 static void
2260cs_release_csp(i, freefnpp)
2261 int i;
2262 int freefnpp;
2263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /*
2265 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2266 */
2267 if (csinfo[i].to_fp != NULL)
2268 {
2269 (void)fputs("q\n", csinfo[i].to_fp);
2270 (void)fflush(csinfo[i].to_fp);
2271 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002272#if defined(UNIX)
2273 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002274 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002275 int pstat;
2276 pid_t pid;
2277
2278# if defined(HAVE_SIGACTION)
2279 struct sigaction sa, old;
2280
Bram Moolenaar9701da02008-03-16 12:09:58 +00002281 /* Use sigaction() to limit the waiting time to two seconds. */
2282 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002283 sa.sa_handler = sig_handler;
2284 sa.sa_flags = SA_NODEFER;
2285 sigaction(SIGALRM, &sa, &old);
2286 alarm(2); /* 2 sec timeout */
2287
2288 /* Block until cscope exits or until timer expires */
2289 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002290 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002291
2292 /* cancel pending alarm if still there and restore signal */
2293 alarm(0);
2294 sigaction(SIGALRM, &old, NULL);
2295# else
2296 int waited;
2297
2298 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2299 * to give cscope a chance to exit quickly. */
2300 sleep(0);
2301 for (waited = 0; waited < 40; ++waited)
2302 {
2303 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002304 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002305 if (pid != 0)
2306 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002307 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002308 }
2309# endif
2310 /*
2311 * If the cscope process is still running: kill it.
2312 * Safety check: If the PID would be zero here, the entire X session
2313 * would be killed. -1 and 1 are dangerous as well.
2314 */
2315 if (pid < 0 && csinfo[i].pid > 1)
2316 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002317# ifdef ECHILD
2318 int alive = TRUE;
2319
2320 if (waitpid_errno == ECHILD)
2321 {
2322 /*
2323 * When using 'vim -g', vim is forked and cscope process is
2324 * no longer a child process but a sibling. So waitpid()
2325 * fails with errno being ECHILD (No child processes).
2326 * Don't send SIGKILL to cscope immediately but wait
2327 * (polling) for it to exit normally as result of sending
2328 * the "q" command, hence giving it a chance to clean up
2329 * its temporary files.
2330 */
2331 int waited;
2332
2333 sleep(0);
2334 for (waited = 0; waited < 40; ++waited)
2335 {
2336 /* Check whether cscope process is still alive */
2337 if (kill(csinfo[i].pid, 0) != 0)
2338 {
2339 alive = FALSE; /* cscope process no longer exists */
2340 break;
2341 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002342 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002343 }
2344 }
2345 if (alive)
2346# endif
2347 {
2348 kill(csinfo[i].pid, SIGKILL);
2349 (void)waitpid(csinfo[i].pid, &pstat, 0);
2350 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002351 }
2352 }
2353#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002354 if (csinfo[i].hProc != NULL)
2355 {
2356 /* Give cscope a chance to exit normally */
2357 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2358 TerminateProcess(csinfo[i].hProc, 0);
2359 CloseHandle(csinfo[i].hProc);
2360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#endif
2362
2363 if (csinfo[i].fr_fp != NULL)
2364 (void)fclose(csinfo[i].fr_fp);
2365 if (csinfo[i].to_fp != NULL)
2366 (void)fclose(csinfo[i].to_fp);
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (freefnpp)
2369 {
2370 vim_free(csinfo[i].fname);
2371 vim_free(csinfo[i].ppath);
2372 vim_free(csinfo[i].flags);
2373 }
2374
2375 clear_csinfo(i);
2376} /* cs_release_csp */
2377
2378
2379/*
2380 * PRIVATE: cs_reset
2381 *
2382 * calls cs_kill on all cscope connections then reinits
2383 */
2384/* ARGSUSED */
2385 static int
2386cs_reset(eap)
2387 exarg_T *eap;
2388{
2389 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2390 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002391 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 /* malloc our db and ppath list */
2394 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2395 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2396 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2397 if (dblist == NULL || pplist == NULL || fllist == NULL)
2398 {
2399 vim_free(dblist);
2400 vim_free(pplist);
2401 vim_free(fllist);
2402 return CSCOPE_FAILURE;
2403 }
2404
2405 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2406 {
2407 dblist[i] = csinfo[i].fname;
2408 pplist[i] = csinfo[i].ppath;
2409 fllist[i] = csinfo[i].flags;
2410 if (csinfo[i].fname != NULL)
2411 cs_release_csp(i, FALSE);
2412 }
2413
2414 /* rebuild the cscope connection list */
2415 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2416 {
2417 if (dblist[i] != NULL)
2418 {
2419 cs_add_common(dblist[i], pplist[i], fllist[i]);
2420 if (p_csverbose)
2421 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002422 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 * connection number in the same line as
2424 * "Added cscope database..."
2425 */
2426 sprintf(buf, " (#%d)", i);
2427 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2428 }
2429 }
2430 vim_free(dblist[i]);
2431 vim_free(pplist[i]);
2432 vim_free(fllist[i]);
2433 }
2434 vim_free(dblist);
2435 vim_free(pplist);
2436 vim_free(fllist);
2437
2438 if (p_csverbose)
2439 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2440 return CSCOPE_SUCCESS;
2441} /* cs_reset */
2442
2443
2444/*
2445 * PRIVATE: cs_resolve_file
2446 *
2447 * construct the full pathname to a file found in the cscope database.
2448 * (Prepends ppath, if there is one and if it's not already prepended,
2449 * otherwise just uses the name found.)
2450 *
2451 * we need to prepend the prefix because on some cscope's (e.g., the one that
2452 * ships with Solaris 2.6), the output never has the prefix prepended.
2453 * contrast this with my development system (Digital Unix), which does.
2454 */
2455 static char *
2456cs_resolve_file(i, name)
2457 int i;
2458 char *name;
2459{
2460 char *fullname;
2461 int len;
2462
2463 /*
2464 * ppath is freed when we destroy the cscope connection.
2465 * fullname is freed after cs_make_vim_style_matches, after it's been
2466 * copied into the tag buffer used by vim
2467 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002468 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002470 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if ((fullname = (char *)alloc(len)) == NULL)
2473 return NULL;
2474
2475 /*
2476 * note/example: this won't work if the cscope output already starts
2477 * "../.." and the prefix path is also "../..". if something like this
2478 * happens, you are screwed up and need to fix how you're using cscope.
2479 */
2480 if (csinfo[i].ppath != NULL &&
2481 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2482 (name[0] != '/')
2483#ifdef WIN32
2484 && name[0] != '\\' && name[1] != ':'
2485#endif
2486 )
2487 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2488 else
2489 (void)sprintf(fullname, "%s", name);
2490
2491 return fullname;
2492} /* cs_resolve_file */
2493
2494
2495/*
2496 * PRIVATE: cs_show
2497 *
2498 * show all cscope connections
2499 */
2500/* ARGSUSED */
2501 static int
2502cs_show(eap)
2503 exarg_T *eap;
2504{
2505 short i;
2506 if (cs_cnt_connections() == 0)
2507 MSG_PUTS(_("no cscope connections\n"));
2508 else
2509 {
2510 MSG_PUTS_ATTR(
2511 _(" # pid database name prepend path\n"),
2512 hl_attr(HLF_T));
2513 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2514 {
2515 if (csinfo[i].fname == NULL)
2516 continue;
2517
2518 if (csinfo[i].ppath != NULL)
2519 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2520 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2521 else
2522 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2523 i, (long)csinfo[i].pid, csinfo[i].fname);
2524 }
2525 }
2526
2527 wait_return(TRUE);
2528 return CSCOPE_SUCCESS;
2529} /* cs_show */
2530
Bram Moolenaar02b06312007-09-06 15:39:22 +00002531
2532/*
2533 * PUBLIC: cs_end
2534 *
2535 * Only called when VIM exits to quit any cscope sessions.
2536 */
2537 void
2538cs_end()
2539{
2540 int i;
2541
2542 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2543 cs_release_csp(i, TRUE);
2544}
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#endif /* FEAT_CSCOPE */
2547
2548/* the end */