blob: cf308bc64101928c076c2a2039851f29cf0c560a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void cs_usage_msg(csid_e x);
24static int cs_add(exarg_T *eap);
25static void cs_stat_emsg(char *fname);
26static int cs_add_common(char *, char *, char *);
27static int cs_check_for_connections(void);
28static int cs_check_for_tags(void);
29static int cs_cnt_connections(void);
30static void cs_reading_emsg(int idx);
31static int cs_cnt_matches(int idx);
32static char * cs_create_cmd(char *csoption, char *pattern);
33static int cs_create_connection(int i);
34static void do_cscope_general(exarg_T *eap, int make_split);
Bram Moolenaarc716c302006-01-21 22:12:51 +000035#ifdef FEAT_QUICKFIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void cs_file_results(FILE *, int *);
Bram Moolenaarc716c302006-01-21 22:12:51 +000037#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void cs_fill_results(char *, int , int *, char ***,
39 char ***, int *);
40static int cs_find(exarg_T *eap);
41static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
42static int cs_help(exarg_T *eap);
43static void clear_csinfo(int i);
44static int cs_insert_filelist(char *, char *, char *,
Bram Moolenaar8767f522016-07-01 17:17:39 +020045 stat_T *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static int cs_kill(exarg_T *eap);
47static void cs_kill_execute(int, char *);
48static cscmd_T * cs_lookup_cmd(exarg_T *eap);
49static char * cs_make_vim_style_matches(char *, char *,
50 char *, char *);
51static char * cs_manage_matches(char **, char **, int, mcmd_e);
52static char * cs_parse_results(int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search);
53static char * cs_pathcomponents(char *path);
54static void cs_print_tags_priv(char **, char **, int);
55static int cs_read_prompt(int);
56static void cs_release_csp(int, int freefnpp);
57static int cs_reset(exarg_T *eap);
58static char * cs_resolve_file(int, char *);
59static int cs_show(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
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 Moolenaar80632db2016-07-05 22:28:40 +020073 N_("Query for a pattern"), "find a|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
Bram Moolenaar68c2f632016-01-30 17:24:07 +010086cs_usage_msg(csid_e x)
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
89}
90
Bram Moolenaarf4580d82009-03-18 11:52:53 +000091#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
92
93static enum
94{
95 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +000096 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +000097 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
98 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
99} expand_what;
100
101/*
102 * Function given to ExpandGeneric() to obtain the cscope command
103 * expansion.
104 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000105 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100106get_cscope_name(expand_T *xp UNUSED, int idx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000107{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000108 int current_idx;
109 int i;
110
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000111 switch (expand_what)
112 {
113 case EXP_CSCOPE_SUBCMD:
114 /* Complete with sub-commands of ":cscope":
115 * add, find, help, kill, reset, show */
116 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000117 case EXP_SCSCOPE_SUBCMD:
118 /* Complete with sub-commands of ":scscope": same sub-commands as
119 * ":cscope" but skip commands which don't support split windows */
120 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
121 if (cs_cmds[i].cansplit)
122 if (current_idx++ == idx)
123 break;
124 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000125 case EXP_CSCOPE_FIND:
126 {
127 const char *query_type[] =
128 {
Bram Moolenaar80632db2016-07-05 22:28:40 +0200129 "a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000130 };
131
132 /* Complete with query type of ":cscope find {query_type}".
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200133 * {query_type} can be letters (c, d, ... a) or numbers (0, 1,
134 * ..., 9) but only complete with letters, since numbers are
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000135 * redundant. */
136 return (char_u *)query_type[idx];
137 }
138 case EXP_CSCOPE_KILL:
139 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000140 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000141
142 /* ":cscope kill" accepts connection numbers or partial names of
143 * the pathname of the cscope database as argument. Only complete
144 * with connection numbers. -1 can also be used to kill all
145 * connections. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000146 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000147 {
148 if (csinfo[i].fname == NULL)
149 continue;
150 if (current_idx++ == idx)
151 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000152 vim_snprintf(connection, sizeof(connection), "%d", i);
153 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000154 }
155 }
156 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
157 }
158 default:
159 return NULL;
160 }
161}
162
163/*
164 * Handle command line completion for :cscope command.
165 */
166 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100167set_context_in_cscope_cmd(
168 expand_T *xp,
169 char_u *arg,
170 cmdidx_T cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000171{
172 char_u *p;
173
174 /* Default: expand subcommands */
175 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000176 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000177 expand_what = (cmdidx == CMD_scscope)
178 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000179
180 /* (part of) subcommand already typed */
181 if (*arg != NUL)
182 {
183 p = skiptowhite(arg);
184 if (*p != NUL) /* past first word */
185 {
186 xp->xp_pattern = skipwhite(p);
187 if (*skiptowhite(xp->xp_pattern) != NUL)
188 xp->xp_context = EXPAND_NOTHING;
189 else if (STRNICMP(arg, "add", p - arg) == 0)
190 xp->xp_context = EXPAND_FILES;
191 else if (STRNICMP(arg, "kill", p - arg) == 0)
192 expand_what = EXP_CSCOPE_KILL;
193 else if (STRNICMP(arg, "find", p - arg) == 0)
194 expand_what = EXP_CSCOPE_FIND;
195 else
196 xp->xp_context = EXPAND_NOTHING;
197 }
198 }
199}
200
201#endif /* FEAT_CMDL_COMPL */
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203/*
204 * PRIVATE: do_cscope_general
205 *
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000206 * Find the command, print help if invalid, and then call the corresponding
207 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
209 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100210do_cscope_general(
211 exarg_T *eap,
212 int make_split) /* whether to split window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 cscmd_T *cmdp;
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
217 {
218 cs_help(eap);
219 return;
220 }
221
222#ifdef FEAT_WINDOWS
223 if (make_split)
224 {
225 if (!cmdp->cansplit)
226 {
227 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
228 return;
229 }
230 postponed_split = -1;
231 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000232 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234#endif
235
236 cmdp->func(eap);
237
238#ifdef FEAT_WINDOWS
239 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000240 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#endif
242}
243
244/*
245 * PUBLIC: do_cscope
246 */
247 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100248do_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 do_cscope_general(eap, FALSE);
251}
252
253/*
254 * PUBLIC: do_scscope
255 *
256 * same as do_cscope, but splits window, too.
257 */
258 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100259do_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 do_cscope_general(eap, TRUE);
262}
263
264/*
265 * PUBLIC: do_cstag
266 *
267 */
268 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100269do_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270{
271 int ret = FALSE;
272
Bram Moolenaar446cb832008-06-24 21:56:24 +0000273 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
275 (void)EMSG(_("E562: Usage: cstag <ident>"));
276 return;
277 }
278
279 switch (p_csto)
280 {
281 case 0 :
282 if (cs_check_for_connections())
283 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000284 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200285 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (ret == FALSE)
287 {
288 cs_free_tags();
289 if (msg_col)
290 msg_putchar('\n');
291
292 if (cs_check_for_tags())
293 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
294 }
295 }
296 else if (cs_check_for_tags())
297 {
298 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
299 }
300 break;
301 case 1 :
302 if (cs_check_for_tags())
303 {
304 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
305 if (ret == FALSE)
306 {
307 if (msg_col)
308 msg_putchar('\n');
309
310 if (cs_check_for_connections())
311 {
312 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200313 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (ret == FALSE)
315 cs_free_tags();
316 }
317 }
318 }
319 else if (cs_check_for_connections())
320 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000321 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200322 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (ret == FALSE)
324 cs_free_tags();
325 }
326 break;
327 default :
328 break;
329 }
330
331 if (!ret)
332 {
333 (void)EMSG(_("E257: cstag: tag not found"));
334#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
335 g_do_tagpreview = 0;
336#endif
337 }
338
339} /* do_cscope */
340
341
342/*
343 * PUBLIC: cs_find
344 *
345 * this simulates a vim_fgets(), but for cscope, returns the next line
346 * from the cscope output. should only be called from find_tags()
347 *
348 * returns TRUE if eof, FALSE otherwise
349 */
350 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100351cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 char *p;
354
355 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
356 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000357 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return FALSE;
360} /* cs_fgets */
361
362
363/*
364 * PUBLIC: cs_free_tags
365 *
366 * called only from do_tag(), when popping the tag stack
367 */
368 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100369cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
371 cs_manage_matches(NULL, NULL, -1, Free);
372}
373
374
375/*
376 * PUBLIC: cs_print_tags
377 *
378 * called from do_tag()
379 */
380 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100381cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382{
383 cs_manage_matches(NULL, NULL, -1, Print);
384}
385
386
387/*
388 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
389 *
390 * Checks for the existence of a |cscope| connection. If no
391 * parameters are specified, then the function returns:
392 *
393 * 0, if cscope was not available (not compiled in), or if there
394 * are no cscope connections; or
395 * 1, if there is at least one cscope connection.
396 *
397 * If parameters are specified, then the value of {num}
398 * determines how existence of a cscope connection is checked:
399 *
400 * {num} Description of existence check
401 * ----- ------------------------------
402 * 0 Same as no parameters (e.g., "cscope_connection()").
403 * 1 Ignore {prepend}, and use partial string matches for
404 * {dbpath}.
405 * 2 Ignore {prepend}, and use exact string matches for
406 * {dbpath}.
407 * 3 Use {prepend}, use partial string matches for both
408 * {dbpath} and {prepend}.
409 * 4 Use {prepend}, use exact string matches for both
410 * {dbpath} and {prepend}.
411 *
412 * Note: All string comparisons are case sensitive!
413 */
414#if defined(FEAT_EVAL) || defined(PROTO)
415 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100416cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 int i;
419
420 if (num < 0 || num > 4 || (num > 0 && !dbpath))
421 return FALSE;
422
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000423 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
425 if (!csinfo[i].fname)
426 continue;
427
428 if (num == 0)
429 return TRUE;
430
431 switch (num)
432 {
433 case 1:
434 if (strstr(csinfo[i].fname, (char *)dbpath))
435 return TRUE;
436 break;
437 case 2:
438 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
439 return TRUE;
440 break;
441 case 3:
442 if (strstr(csinfo[i].fname, (char *)dbpath)
443 && ((!ppath && !csinfo[i].ppath)
444 || (ppath
445 && csinfo[i].ppath
446 && strstr(csinfo[i].ppath, (char *)ppath))))
447 return TRUE;
448 break;
449 case 4:
450 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
451 && ((!ppath && !csinfo[i].ppath)
452 || (ppath
453 && csinfo[i].ppath
454 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
455 return TRUE;
456 break;
457 }
458 }
459
460 return FALSE;
461} /* cs_connection */
462#endif
463
464
465/*
466 * PRIVATE functions
467 ****************************************************************************/
468
469/*
470 * PRIVATE: cs_add
471 *
472 * add cscope database or a directory name (to look for cscope.out)
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000473 * to the cscope connection list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 *
475 * MAXPATHL 256
476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100478cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 char *fname, *ppath, *flags = NULL;
481
482 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
483 {
484 cs_usage_msg(Add);
485 return CSCOPE_FAILURE;
486 }
487 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
488 flags = strtok((char *)NULL, (const char *)" ");
489
490 return cs_add_common(fname, ppath, flags);
491}
492
493 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100494cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496 char *stat_emsg = _("E563: stat(%s) error: %d");
497 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
498
499 if (buf != NULL)
500 {
501 (void)sprintf(buf, stat_emsg, fname, errno);
502 (void)EMSG(buf);
503 vim_free(buf);
504 }
505 else
506 (void)EMSG(_("E563: stat error"));
507}
508
509
510/*
511 * PRIVATE: cs_add_common
512 *
513 * the common routine to add a new cscope connection. called by
514 * cs_add() and cs_reset(). i really don't like to do this, but this
515 * routine uses a number of goto statements.
516 */
517 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100518cs_add_common(
519 char *arg1, /* filename - may contain environment variables */
520 char *arg2, /* prepend path - may contain environment variables */
521 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200523 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000524 int ret;
525 char *fname = NULL;
526 char *fname2 = NULL;
527 char *ppath = NULL;
528 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200529#ifdef FEAT_MODIFY_FNAME
530 int len;
531 int usedlen = 0;
532 char_u *fbuf = NULL;
533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000536 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 goto add_err;
538
539 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200540#ifdef FEAT_MODIFY_FNAME
541 len = (int)STRLEN(fname);
542 fbuf = (char_u *)fname;
543 (void)modify_fname((char_u *)":p", &usedlen,
544 (char_u **)&fname, &fbuf, &len);
545 if (fname == NULL)
546 goto add_err;
547 fname = (char *)vim_strnsave((char_u *)fname, len);
548 vim_free(fbuf);
549#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200550 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (ret < 0)
552 {
553staterr:
554 if (p_csverbose)
555 cs_stat_emsg(fname);
556 goto add_err;
557 }
558
559 /* get the prepend path (arg2), expand it, and try to stat it */
560 if (arg2 != NULL)
561 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200562 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000564 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 goto add_err;
566
567 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200568 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (ret < 0)
570 goto staterr;
571 }
572
573 /* if filename is a directory, append the cscope database name to it */
574 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
575 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000576 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (fname2 == NULL)
578 goto add_err;
579
580 while (fname[strlen(fname)-1] == '/'
581#ifdef WIN32
582 || fname[strlen(fname)-1] == '\\'
583#endif
584 )
585 {
586 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200587 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 break;
589 }
590 if (fname[0] == '\0')
591 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
592 else
593 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
594
Bram Moolenaar8767f522016-07-01 17:17:39 +0200595 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (ret < 0)
597 {
598 if (p_csverbose)
599 cs_stat_emsg(fname2);
600 goto add_err;
601 }
602
603 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
604 }
605#if defined(UNIX)
606 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
607#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000608 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
610#endif
611 {
612 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
613 }
614 else
615 {
616 if (p_csverbose)
617 (void)EMSG2(
618 _("E564: %s is not a directory or a valid cscope database"),
619 fname);
620 goto add_err;
621 }
622
623 if (i != -1)
624 {
625 if (cs_create_connection(i) == CSCOPE_FAILURE
626 || cs_read_prompt(i) == CSCOPE_FAILURE)
627 {
628 cs_release_csp(i, TRUE);
629 goto add_err;
630 }
631
632 if (p_csverbose)
633 {
634 msg_clr_eos();
635 (void)smsg_attr(hl_attr(HLF_R),
636 (char_u *)_("Added cscope database %s"),
637 csinfo[i].fname);
638 }
639 }
640
641 vim_free(fname);
642 vim_free(fname2);
643 vim_free(ppath);
644 return CSCOPE_SUCCESS;
645
646add_err:
647 vim_free(fname2);
648 vim_free(fname);
649 vim_free(ppath);
650 return CSCOPE_FAILURE;
651} /* cs_add_common */
652
653
654 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100655cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 return (cs_cnt_connections() > 0);
658} /* cs_check_for_connections */
659
660
661 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100662cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000664 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665} /* cs_check_for_tags */
666
667
668/*
669 * PRIVATE: cs_cnt_connections
670 *
671 * count the number of cscope connections
672 */
673 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100674cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 short i;
677 short cnt = 0;
678
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000679 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 if (csinfo[i].fname != NULL)
682 cnt++;
683 }
684 return cnt;
685} /* cs_cnt_connections */
686
687 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100688cs_reading_emsg(
689 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 EMSGN(_("E262: error reading cscope connection %ld"), idx);
692}
693
694#define CSREAD_BUFSIZE 2048
695/*
696 * PRIVATE: cs_cnt_matches
697 *
698 * count the number of matches for a given cscope connection.
699 */
700 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100701cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 char *stok;
704 char *buf;
705 int nlines;
706
707 buf = (char *)alloc(CSREAD_BUFSIZE);
708 if (buf == NULL)
709 return 0;
710 for (;;)
711 {
712 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
713 {
714 if (feof(csinfo[idx].fr_fp))
715 errno = EIO;
716
717 cs_reading_emsg(idx);
718
719 vim_free(buf);
720 return -1;
721 }
722
723 /*
724 * If the database is out of date, or there's some other problem,
725 * cscope will output error messages before the number-of-lines output.
726 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000727 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 if ((stok = strtok(buf, (const char *)" ")) == NULL)
730 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000731 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 continue;
733
734 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
735 continue;
736 nlines = atoi(stok);
737 if (nlines < 0)
738 {
739 nlines = 0;
740 break;
741 }
742
743 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
744 continue;
745 if (strncmp((const char *)stok, "lines", 5))
746 continue;
747
748 break;
749 }
750
751 vim_free(buf);
752 return nlines;
753} /* cs_cnt_matches */
754
755
756/*
757 * PRIVATE: cs_create_cmd
758 *
759 * Creates the actual cscope command query from what the user entered.
760 */
761 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100762cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 char *cmd;
765 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000766 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 switch (csoption[0])
769 {
770 case '0' : case 's' :
771 search = 0;
772 break;
773 case '1' : case 'g' :
774 search = 1;
775 break;
776 case '2' : case 'd' :
777 search = 2;
778 break;
779 case '3' : case 'c' :
780 search = 3;
781 break;
782 case '4' : case 't' :
783 search = 4;
784 break;
785 case '6' : case 'e' :
786 search = 6;
787 break;
788 case '7' : case 'f' :
789 search = 7;
790 break;
791 case '8' : case 'i' :
792 search = 8;
793 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200794 case '9' : case 'a' :
795 search = 9;
796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 default :
798 (void)EMSG(_("E561: unknown cscope search type"));
799 cs_usage_msg(Find);
800 return NULL;
801 }
802
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000803 /* Skip white space before the patter, except for text and pattern search,
804 * they may want to use the leading white space. */
805 pat = pattern;
806 if (search != 4 && search != 6)
807 while vim_iswhite(*pat)
808 ++pat;
809
810 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return NULL;
812
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000813 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 return cmd;
816} /* cs_create_cmd */
817
818
819/*
820 * PRIVATE: cs_create_connection
821 *
822 * This piece of code was taken/adapted from nvi. do we need to add
823 * the BSD license notice?
824 */
825 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100826cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000828#ifdef UNIX
829 int to_cs[2], from_cs[2];
830#endif
831 int len;
832 char *prog, *cmd, *ppath = NULL;
833#ifdef WIN32
834 int fd;
835 SECURITY_ATTRIBUTES sa;
836 PROCESS_INFORMATION pi;
837 STARTUPINFO si;
838 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
839 HANDLE stdin_rd, stdout_rd;
840 HANDLE stdout_wr, stdin_wr;
841 BOOL created;
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000842# ifdef __BORLANDC__
843# define OPEN_OH_ARGTYPE long
844# else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100845# if (_MSC_VER >= 1300) || defined(__MINGW32__)
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000846# define OPEN_OH_ARGTYPE intptr_t
847# else
848# define OPEN_OH_ARGTYPE long
849# endif
850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#endif
852
Bram Moolenaar02b06312007-09-06 15:39:22 +0000853#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /*
855 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
856 * from_cs[0] and writes to to_cs[1].
857 */
858 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
859 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
860 {
861 (void)EMSG(_("E566: Could not create cscope pipes"));
862err_closing:
863 if (to_cs[0] != -1)
864 (void)close(to_cs[0]);
865 if (to_cs[1] != -1)
866 (void)close(to_cs[1]);
867 if (from_cs[0] != -1)
868 (void)close(from_cs[0]);
869 if (from_cs[1] != -1)
870 (void)close(from_cs[1]);
871 return CSCOPE_FAILURE;
872 }
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 switch (csinfo[i].pid = fork())
875 {
876 case -1:
877 (void)EMSG(_("E622: Could not fork for cscope"));
878 goto err_closing;
879 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (dup2(to_cs[0], STDIN_FILENO) == -1)
881 PERROR("cs_create_connection 1");
882 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
883 PERROR("cs_create_connection 2");
884 if (dup2(from_cs[1], STDERR_FILENO) == -1)
885 PERROR("cs_create_connection 3");
886
887 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 (void)close(to_cs[1]);
889 (void)close(from_cs[0]);
890#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000891 /* WIN32 */
892 /* Create pipes to communicate with cscope */
893 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
894 sa.bInheritHandle = TRUE;
895 sa.lpSecurityDescriptor = NULL;
896
897 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
898 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
899 {
900 (void)EMSG(_("E566: Could not create cscope pipes"));
901err_closing:
902 if (pipe_stdin)
903 {
904 CloseHandle(stdin_rd);
905 CloseHandle(stdin_wr);
906 }
907 if (pipe_stdout)
908 {
909 CloseHandle(stdout_rd);
910 CloseHandle(stdout_wr);
911 }
912 return CSCOPE_FAILURE;
913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915 /* expand the cscope exec for env var's */
916 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
917 {
918#ifdef UNIX
919 return CSCOPE_FAILURE;
920#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000921 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 goto err_closing;
923#endif
924 }
925 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
926
927 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000928 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (csinfo[i].ppath)
930 {
931 /* expand the prepend path for env var's */
932 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
933 {
934 vim_free(prog);
935#ifdef UNIX
936 return CSCOPE_FAILURE;
937#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000938 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 goto err_closing;
940#endif
941 }
942 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000944 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946
947 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000948 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 if ((cmd = (char *)alloc(len)) == NULL)
951 {
952 vim_free(prog);
953 vim_free(ppath);
954#ifdef UNIX
955 return CSCOPE_FAILURE;
956#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000957 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 goto err_closing;
959#endif
960 }
961
962 /* run the cscope command; is there execl for non-unix systems? */
963#if defined(UNIX)
964 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
965#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000966 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
968#endif
969 if (csinfo[i].ppath != NULL)
970 {
971 (void)strcat(cmd, " -P");
972 (void)strcat(cmd, csinfo[i].ppath);
973 }
974 if (csinfo[i].flags != NULL)
975 {
976 (void)strcat(cmd, " ");
977 (void)strcat(cmd, csinfo[i].flags);
978 }
979# ifdef UNIX
980 /* on Win32 we still need prog */
981 vim_free(prog);
982# endif
983 vim_free(ppath);
984
985#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200986# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
987 /* Change our process group to avoid cscope receiving SIGWINCH. */
988# if defined(HAVE_SETSID)
989 (void)setsid();
990# else
991 if (setpgid(0, 0) == -1)
992 PERROR(_("cs_create_connection setpgid failed"));
993# endif
994# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000995 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 PERROR(_("cs_create_connection exec failed"));
997
998 exit(127);
999 /* NOTREACHED */
1000 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1004 */
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /* close unused */
1011 (void)close(to_cs[0]);
1012 (void)close(from_cs[1]);
1013
1014 break;
1015 }
Bram Moolenaar02b06312007-09-06 15:39:22 +00001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001018 /* WIN32 */
1019 /* Create a new process to run cscope and use pipes to talk with it */
1020 GetStartupInfo(&si);
1021 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1022 si.wShowWindow = SW_HIDE; /* Hide child application window */
1023 si.hStdOutput = stdout_wr;
1024 si.hStdError = stdout_wr;
1025 si.hStdInput = stdin_rd;
1026 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1027 NULL, NULL, &si, &pi);
1028 vim_free(prog);
1029 vim_free(cmd);
1030
1031 if (!created)
1032 {
1033 PERROR(_("cs_create_connection exec failed"));
1034 (void)EMSG(_("E623: Could not spawn cscope process"));
1035 goto err_closing;
1036 }
1037 /* else */
1038 csinfo[i].pid = pi.dwProcessId;
1039 csinfo[i].hProc = pi.hProcess;
1040 CloseHandle(pi.hThread);
1041
1042 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1044 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001045 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1046 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001047 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1048 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001049 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1050 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1051
1052 /* Close handles for file descriptors inherited by the cscope process */
1053 CloseHandle(stdin_rd);
1054 CloseHandle(stdout_wr);
1055
1056#endif /* !UNIX */
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 return CSCOPE_SUCCESS;
1059} /* cs_create_connection */
1060
1061
1062/*
1063 * PRIVATE: cs_find
1064 *
Bram Moolenaarcde88542015-08-11 19:14:00 +02001065 * Query cscope using command line interface. Parse the output and use tselect
1066 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 *
1068 * returns TRUE if we jump to a tag or abort, FALSE if not.
1069 */
1070 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001071cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001074 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (cs_check_for_connections() == FALSE)
1077 {
1078 (void)EMSG(_("E567: no cscope connections"));
1079 return FALSE;
1080 }
1081
1082 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1083 {
1084 cs_usage_msg(Find);
1085 return FALSE;
1086 }
1087
1088 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001089 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
1091 cs_usage_msg(Find);
1092 return FALSE;
1093 }
1094
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001095 /*
1096 * Let's replace the NULs written by strtok() with spaces - we need the
1097 * spaces to correctly display the quickfix/location list window's title.
1098 */
1099 for (i = 0; i < eap_arg_len; ++i)
1100 if (NUL == eap->arg[i])
1101 eap->arg[i] = ' ';
1102
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001103 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001104 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105} /* cs_find */
1106
1107
1108/*
1109 * PRIVATE: cs_find_common
1110 *
1111 * common code for cscope find, shared by cs_find() and do_cstag()
1112 */
1113 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001114cs_find_common(
1115 char *opt,
1116 char *pat,
1117 int forceit,
1118 int verbose,
1119 int use_ll UNUSED,
1120 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
1122 int i;
1123 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001124 int *nummatches;
1125 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#ifdef FEAT_QUICKFIX
1127 char cmdletter;
1128 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001129
1130 /* get cmd letter */
1131 switch (opt[0])
1132 {
1133 case '0' :
1134 cmdletter = 's';
1135 break;
1136 case '1' :
1137 cmdletter = 'g';
1138 break;
1139 case '2' :
1140 cmdletter = 'd';
1141 break;
1142 case '3' :
1143 cmdletter = 'c';
1144 break;
1145 case '4' :
1146 cmdletter = 't';
1147 break;
1148 case '6' :
1149 cmdletter = 'e';
1150 break;
1151 case '7' :
1152 cmdletter = 'f';
1153 break;
1154 case '8' :
1155 cmdletter = 'i';
1156 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001157 case '9' :
1158 cmdletter = 'a';
1159 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001160 default :
1161 cmdletter = opt[0];
1162 }
1163
1164 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1165 if (qfpos != NULL)
1166 {
1167 qfpos++;
1168 /* next symbol must be + or - */
1169 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1170 {
1171 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1172 char *buf = (char *)alloc((unsigned)strlen(nf));
1173
1174 /* strlen will be enough because we use chars */
1175 if (buf != NULL)
1176 {
1177 sprintf(buf, nf, *qfpos, *(qfpos-1));
1178 (void)EMSG(buf);
1179 vim_free(buf);
1180 }
1181 return FALSE;
1182 }
1183
1184# ifdef FEAT_AUTOCMD
1185 if (*qfpos != '0')
1186 {
1187 apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1188 curbuf->b_fname, TRUE, curbuf);
1189# ifdef FEAT_EVAL
1190 if (did_throw || force_abort)
1191 return FALSE;
1192# endif
1193 }
1194# endif
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#endif
1197
1198 /* create the actual command to send to cscope */
1199 cmd = cs_create_cmd(opt, pat);
1200 if (cmd == NULL)
1201 return FALSE;
1202
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001203 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1204 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001205 {
1206 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001207 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001208 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001209
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;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001263 char_u *tmp = vim_tempname('c', TRUE);
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 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001283 (void)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
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001335cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 cscmd_T *cmdp = cs_cmds;
1338
1339 (void)MSG_PUTS(_("cscope commands:\n"));
1340 while (cmdp->name != NULL)
1341 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001342 char *help = _(cmdp->help);
1343 int space_cnt = 30 - vim_strsize((char_u *)help);
1344
1345 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1346 if (space_cnt < 0)
1347 space_cnt = 0;
1348 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1349 cmdp->name,
1350 help, space_cnt, " ",
1351 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001353 MSG_PUTS(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001354 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001355 " 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 Moolenaar80632db2016-07-05 22:28:40 +02001362 " 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
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001373clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 csinfo[i].fname = NULL;
1376 csinfo[i].ppath = NULL;
1377 csinfo[i].flags = NULL;
1378#if defined(UNIX)
1379 csinfo[i].st_dev = (dev_t)0;
1380 csinfo[i].st_ino = (ino_t)0;
1381#else
1382 csinfo[i].nVolume = 0;
1383 csinfo[i].nIndexHigh = 0;
1384 csinfo[i].nIndexLow = 0;
1385#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001386 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 csinfo[i].fr_fp = NULL;
1388 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001389#if defined(WIN32)
1390 csinfo[i].hProc = NULL;
1391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
1394#ifndef UNIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001395static char *GetWin32Error(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001398GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400 char *msg = NULL;
1401 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1402 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1403 if (msg != NULL)
1404 {
1405 /* remove trailing \r\n */
1406 char *pcrlf = strstr(msg, "\r\n");
1407 if (pcrlf != NULL)
1408 *pcrlf = '\0';
1409 }
1410 return msg;
1411}
1412#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * PRIVATE: cs_insert_filelist
1416 *
1417 * insert a new cscope database filename into the filelist
1418 */
1419 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001420cs_insert_filelist(
1421 char *fname,
1422 char *ppath,
1423 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001424 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 short i, j;
1427#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 BY_HANDLE_FILE_INFORMATION bhfi;
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1431 if (!mch_windows95())
1432 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001433 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001435 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1436 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (p_csverbose)
1438 {
1439 char *cant_msg = _("E625: cannot open cscope database: %s");
1440 char *winmsg = GetWin32Error();
1441
1442 if (winmsg != NULL)
1443 {
1444 (void)EMSG2(cant_msg, winmsg);
1445 LocalFree(winmsg);
1446 }
1447 else
1448 /* subst filename if can't get error text */
1449 (void)EMSG2(cant_msg, fname);
1450 }
1451 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001452
1453 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (p_csverbose)
1455 (void)EMSG(_("E626: cannot get cscope database information"));
1456 return -1;
1457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459#endif
1460
1461 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001462 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 if (csinfo[j].fname != NULL
1465#if defined(UNIX)
1466 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1467#else
1468 /* compare pathnames first */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001469 && ((fullpathcmp((char_u *)csinfo[j].fname,
1470 (char_u *)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 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001501 csinfo_T *t_csinfo = csinfo;
1502
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001503 /* Reallocate space for more connections. */
1504 csinfo_size *= 2;
1505 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001506 if (csinfo == NULL)
1507 {
1508 vim_free(t_csinfo);
1509 csinfo_size = 0;
1510 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001511 }
1512 if (csinfo == NULL)
1513 return -1;
1514 for (j = csinfo_size/2; j < csinfo_size; j++)
1515 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001518 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 return -1;
1520
1521 (void)strcpy(csinfo[i].fname, (const char *)fname);
1522
1523 if (ppath != NULL)
1524 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001525 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 vim_free(csinfo[i].fname);
1528 csinfo[i].fname = NULL;
1529 return -1;
1530 }
1531 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1532 } else
1533 csinfo[i].ppath = NULL;
1534
1535 if (flags != NULL)
1536 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001537 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 vim_free(csinfo[i].fname);
1540 vim_free(csinfo[i].ppath);
1541 csinfo[i].fname = NULL;
1542 csinfo[i].ppath = NULL;
1543 return -1;
1544 }
1545 (void)strcpy(csinfo[i].flags, (const char *)flags);
1546 } else
1547 csinfo[i].flags = NULL;
1548
1549#if defined(UNIX)
1550 csinfo[i].st_dev = sb->st_dev;
1551 csinfo[i].st_ino = sb->st_ino;
1552
1553#else
1554 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1555 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1556 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1557#endif
1558 return i;
1559} /* cs_insert_filelist */
1560
1561
1562/*
1563 * PRIVATE: cs_lookup_cmd
1564 *
1565 * find cscope command in command table
1566 */
1567 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001568cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 cscmd_T *cmdp;
1571 char *stok;
1572 size_t len;
1573
1574 if (eap->arg == NULL)
1575 return NULL;
1576
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001577 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001578 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1581 return NULL;
1582
1583 len = strlen(stok);
1584 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1585 {
1586 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1587 return (cmdp);
1588 }
1589 return NULL;
1590} /* cs_lookup_cmd */
1591
1592
1593/*
1594 * PRIVATE: cs_kill
1595 *
1596 * nuke em
1597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001599cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 char *stok;
1602 short i;
1603
1604 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1605 {
1606 cs_usage_msg(Kill);
1607 return CSCOPE_FAILURE;
1608 }
1609
1610 /* only single digit positive and negative integers are allowed */
1611 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1612 || (strlen(stok) < 3 && stok[0] == '-'
1613 && VIM_ISDIGIT((int)(stok[1]))))
1614 i = atoi(stok);
1615 else
1616 {
1617 /* It must be part of a name. We will try to find a match
1618 * within all the names in the csinfo data structure
1619 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001620 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1623 break;
1624 }
1625 }
1626
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001627 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 if (p_csverbose)
1630 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1631 }
1632 else
1633 {
1634 if (i == -1)
1635 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001636 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 if (csinfo[i].fname)
1639 cs_kill_execute(i, csinfo[i].fname);
1640 }
1641 }
1642 else
1643 cs_kill_execute(i, stok);
1644 }
1645
1646 return 0;
1647} /* cs_kill */
1648
1649
1650/*
1651 * PRIVATE: cs_kill_execute
1652 *
1653 * Actually kills a specific cscope connection.
1654 */
1655 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001656cs_kill_execute(
1657 int i, /* cscope table index */
1658 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 if (p_csverbose)
1661 {
1662 msg_clr_eos();
1663 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1664 (char_u *)_("cscope connection %s closed"), cname);
1665 }
1666 cs_release_csp(i, TRUE);
1667}
1668
1669
1670/*
1671 * PRIVATE: cs_make_vim_style_matches
1672 *
Bram Moolenaar657ae0b2010-12-30 11:41:09 +01001673 * convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1675 * the type of the tag you are looking for. for example, in Darren Hiebert's
1676 * ctags (the one that comes with vim), #define's use a line number to find the
1677 * tag in a file while function definitions use a regexp search pattern.
1678 *
1679 * i'm going to always use the line number because cscope does something
1680 * quirky (and probably other things i don't know about):
1681 *
1682 * if you have "# define" in your source file, which is
1683 * perfectly legal, cscope thinks you have "#define". this
1684 * will result in a failed regexp search. :(
1685 *
1686 * besides, even if this particular case didn't happen, the search pattern
1687 * would still have to be modified to escape all the special regular expression
1688 * characters to comply with ctags formatting.
1689 */
1690 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001691cs_make_vim_style_matches(
1692 char *fname,
1693 char *slno,
1694 char *search,
1695 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 /* vim style is ctags:
1698 *
1699 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1700 *
1701 * but as mentioned above, we'll always use the line number and
1702 * put the search pattern (if one exists) as "extra"
1703 *
1704 * buf is used as part of vim's method of handling tags, and
1705 * (i think) vim frees it when you pop your tags and get replaced
1706 * by new ones on the tag stack.
1707 */
1708 char *buf;
1709 int amt;
1710
1711 if (search != NULL)
1712 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001713 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if ((buf = (char *)alloc(amt)) == NULL)
1715 return NULL;
1716
1717 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1718 }
1719 else
1720 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001721 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if ((buf = (char *)alloc(amt)) == NULL)
1723 return NULL;
1724
1725 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1726 }
1727
1728 return buf;
1729} /* cs_make_vim_style_matches */
1730
1731
1732/*
1733 * PRIVATE: cs_manage_matches
1734 *
1735 * this is kind of hokey, but i don't see an easy way round this..
1736 *
1737 * Store: keep a ptr to the (malloc'd) memory of matches originally
1738 * generated from cs_find(). the matches are originally lines directly
1739 * from cscope output, but transformed to look like something out of a
1740 * ctags. see cs_make_vim_style_matches for more details.
1741 *
1742 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1743 * the next line from the cscope output. it basically keeps track of which
1744 * lines have been "used" and returns the next one.
1745 *
1746 * Free: frees up everything and resets
1747 *
1748 * Print: prints the tags
1749 */
1750 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001751cs_manage_matches(
1752 char **matches,
1753 char **contexts,
1754 int totmatches,
1755 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 static char **mp = NULL;
1758 static char **cp = NULL;
1759 static int cnt = -1;
1760 static int next = -1;
1761 char *p = NULL;
1762
1763 switch (cmd)
1764 {
1765 case Store:
1766 assert(matches != NULL);
1767 assert(totmatches > 0);
1768 if (mp != NULL || cp != NULL)
1769 (void)cs_manage_matches(NULL, NULL, -1, Free);
1770 mp = matches;
1771 cp = contexts;
1772 cnt = totmatches;
1773 next = 0;
1774 break;
1775 case Get:
1776 if (next >= cnt)
1777 return NULL;
1778
1779 p = mp[next];
1780 next++;
1781 break;
1782 case Free:
1783 if (mp != NULL)
1784 {
1785 if (cnt > 0)
1786 while (cnt--)
1787 {
1788 vim_free(mp[cnt]);
1789 if (cp != NULL)
1790 vim_free(cp[cnt]);
1791 }
1792 vim_free(mp);
1793 vim_free(cp);
1794 }
1795 mp = NULL;
1796 cp = NULL;
1797 cnt = 0;
1798 next = 0;
1799 break;
1800 case Print:
1801 cs_print_tags_priv(mp, cp, cnt);
1802 break;
1803 default: /* should not reach here */
1804 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1805 return NULL;
1806 }
1807
1808 return p;
1809} /* cs_manage_matches */
1810
1811
1812/*
1813 * PRIVATE: cs_parse_results
1814 *
1815 * parse cscope output
1816 */
1817 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001818cs_parse_results(
1819 int cnumber,
1820 char *buf,
1821 int bufsize,
1822 char **context,
1823 char **linenumber,
1824 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 int ch;
1827 char *p;
1828 char *name;
1829
1830 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1831 {
1832 if (feof(csinfo[cnumber].fr_fp))
1833 errno = EIO;
1834
1835 cs_reading_emsg(cnumber);
1836
1837 return NULL;
1838 }
1839
1840 /* If the line's too long for the buffer, discard it. */
1841 if ((p = strchr(buf, '\n')) == NULL)
1842 {
1843 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1844 ;
1845 return NULL;
1846 }
1847 *p = '\0';
1848
1849 /*
1850 * cscope output is in the following format:
1851 *
1852 * <filename> <context> <line number> <pattern>
1853 */
1854 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1855 return NULL;
1856 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1857 return NULL;
1858 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1859 return NULL;
1860 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1861
1862 /* --- nvi ---
1863 * If the file is older than the cscope database, that is,
1864 * the database was built since the file was last modified,
1865 * or there wasn't a search string, use the line number.
1866 */
1867 if (strcmp(*search, "<unknown>") == 0)
1868 *search = NULL;
1869
1870 name = cs_resolve_file(cnumber, name);
1871 return name;
1872}
1873
Bram Moolenaarc716c302006-01-21 22:12:51 +00001874#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
1876 * PRIVATE: cs_file_results
1877 *
1878 * write cscope find results to file
1879 */
1880 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001881cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882{
1883 int i, j;
1884 char *buf;
1885 char *search, *slno;
1886 char *fullname;
1887 char *cntx;
1888 char *context;
1889
1890 buf = (char *)alloc(CSREAD_BUFSIZE);
1891 if (buf == NULL)
1892 return;
1893
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001894 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 if (nummatches_a[i] < 1)
1897 continue;
1898
1899 for (j = 0; j < nummatches_a[i]; j++)
1900 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001901 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1902 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 continue;
1904
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001905 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001906 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 continue;
1908
1909 if (strcmp(cntx, "<global>")==0)
1910 strcpy(context, "<<global>>");
1911 else
1912 sprintf(context, "<<%s>>", cntx);
1913
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001914 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1916 else
1917 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1918
1919 vim_free(context);
1920 vim_free(fullname);
1921 } /* for all matches */
1922
1923 (void)cs_read_prompt(i);
1924
1925 } /* for all cscope connections */
1926 vim_free(buf);
1927}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930/*
1931 * PRIVATE: cs_fill_results
1932 *
1933 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1934 * into ctags format
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001935 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
1937 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001938cs_fill_results(
1939 char *tagstr,
1940 int totmatches,
1941 int *nummatches_a,
1942 char ***matches_p,
1943 char ***cntxts_p,
1944 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 int i, j;
1947 char *buf;
1948 char *search, *slno;
1949 int totsofar = 0;
1950 char **matches = NULL;
1951 char **cntxts = NULL;
1952 char *fullname;
1953 char *cntx;
1954
1955 assert(totmatches > 0);
1956
1957 buf = (char *)alloc(CSREAD_BUFSIZE);
1958 if (buf == NULL)
1959 return;
1960
1961 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1962 goto parse_out;
1963 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1964 goto parse_out;
1965
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001966 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 if (nummatches_a[i] < 1)
1969 continue;
1970
1971 for (j = 0; j < nummatches_a[i]; j++)
1972 {
1973 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1974 &slno, &search)) == NULL)
1975 continue;
1976
1977 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1978 search, tagstr);
1979
1980 vim_free(fullname);
1981
1982 if (strcmp(cntx, "<global>") == 0)
1983 cntxts[totsofar] = NULL;
1984 else
1985 /* note: if vim_strsave returns NULL, then the context
1986 * will be "<global>", which is misleading.
1987 */
1988 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1989
1990 if (matches[totsofar] != NULL)
1991 totsofar++;
1992
1993 } /* for all matches */
1994
1995 (void)cs_read_prompt(i);
1996
1997 } /* for all cscope connections */
1998
1999parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002000 if (totsofar == 0)
2001 {
2002 /* No matches, free the arrays and return NULL in "*matches_p". */
2003 vim_free(matches);
2004 matches = NULL;
2005 vim_free(cntxts);
2006 cntxts = NULL;
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 *matched = totsofar;
2009 *matches_p = matches;
2010 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00002011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 vim_free(buf);
2013} /* cs_fill_results */
2014
2015
2016/* get the requested path components */
2017 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002018cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 int i;
2021 char *s;
2022
2023 if (p_cspc == 0)
2024 return path;
2025
2026 s = path + strlen(path) - 1;
2027 for (i = 0; i < p_cspc; ++i)
2028 while (s > path && *--s != '/'
2029#ifdef WIN32
2030 && *--s != '\\'
2031#endif
2032 )
2033 ;
2034 if ((s > path && *s == '/')
2035#ifdef WIN32
2036 || (s > path && *s == '\\')
2037#endif
2038 )
2039 ++s;
2040 return s;
2041}
2042
2043/*
2044 * PRIVATE: cs_print_tags_priv
2045 *
2046 * called from cs_manage_matches()
2047 */
2048 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002049cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002052 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 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");
Bram Moolenaarcde88542015-08-11 19:14:00 +02002071 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002072 {
2073 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02002074 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 buf = (char *)alloc(newsize);
2079 if (buf != NULL)
2080 {
2081 bufsize = newsize;
2082 (void)sprintf(buf, cstag_msg, ptag);
2083 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2084 }
2085
2086 vim_free(tbuf);
2087
2088 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2089 msg_advance(msg_col + 2);
2090 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2091
2092 num = 1;
2093 for (i = 0; i < num_matches; i++)
2094 {
2095 idx = i;
2096
2097 /* if we really wanted to, we could avoid this malloc and strcpy
2098 * by parsing matches[i] on the fly and placing stuff into buf
2099 * directly, but that's too much of a hassle
2100 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 continue;
2103 (void)strcpy(tbuf, matches[idx]);
2104
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002105 if (strtok(tbuf, (const char *)"\t") == NULL
2106 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2107 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2108 {
2109 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002111 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002112 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2115
2116 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (bufsize < newsize)
2119 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002120 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 buf = (char *)vim_realloc(buf, newsize);
2122 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002125 vim_free(t_buf);
2126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 else
2128 bufsize = newsize;
2129 }
2130 if (buf != NULL)
2131 {
2132 /* csfmt_str = "%4d %6s "; */
2133 (void)sprintf(buf, csfmt_str, num, lno);
2134 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2135 }
2136 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2137
2138 /* compute the required space for the context */
2139 if (cntxts[idx] != NULL)
2140 context = cntxts[idx];
2141 else
2142 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002143 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 if (bufsize < newsize)
2146 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002147 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 buf = (char *)vim_realloc(buf, newsize);
2149 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002152 vim_free(t_buf);
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 else
2155 bufsize = newsize;
2156 }
2157 if (buf != NULL)
2158 {
2159 (void)sprintf(buf, cntxformat, context);
2160
2161 /* print the context only if it fits on the same line */
2162 if (msg_col + (int)strlen(buf) >= (int)Columns)
2163 msg_putchar('\n');
2164 msg_advance(12);
2165 MSG_PUTS_LONG(buf);
2166 msg_putchar('\n');
2167 }
2168 if (extra != NULL)
2169 {
2170 msg_advance(13);
2171 MSG_PUTS_LONG(extra);
2172 }
2173
2174 vim_free(tbuf); /* only after printing extra due to strtok use */
2175
2176 if (msg_col)
2177 msg_putchar('\n');
2178
2179 ui_breakcheck();
2180 if (got_int)
2181 {
2182 got_int = FALSE; /* don't print any more matches */
2183 break;
2184 }
2185
2186 num++;
2187 } /* for all matches */
2188
2189 vim_free(buf);
2190} /* cs_print_tags_priv */
2191
2192
2193/*
2194 * PRIVATE: cs_read_prompt
2195 *
2196 * read a cscope prompt (basically, skip over the ">> ")
2197 */
2198 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002199cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int ch;
2202 char *buf = NULL; /* buffer for possible error message from cscope */
2203 int bufpos = 0;
2204 char *cs_emsg;
2205 int maxlen;
2206 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002207 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 int n;
2209
2210 cs_emsg = _("E609: Cscope error: %s");
2211 /* compute maximum allowed len for Cscope error message */
2212 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2213
2214 for (;;)
2215 {
2216 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2217 /* if there is room and char is printable */
2218 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2219 {
2220 if (buf == NULL) /* lazy buffer allocation */
2221 buf = (char *)alloc(maxlen);
2222 if (buf != NULL)
2223 {
2224 /* append character to the message */
2225 buf[bufpos++] = ch;
2226 buf[bufpos] = NUL;
2227 if (bufpos >= epromptlen
2228 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2229 {
2230 /* remove eprompt from buf */
2231 buf[bufpos - epromptlen] = NUL;
2232
2233 /* print message to user */
2234 (void)EMSG2(cs_emsg, buf);
2235
2236 /* send RETURN to cscope */
2237 (void)putc('\n', csinfo[i].to_fp);
2238 (void)fflush(csinfo[i].to_fp);
2239
2240 /* clear buf */
2241 bufpos = 0;
2242 buf[bufpos] = NUL;
2243 }
2244 }
2245 }
2246
2247 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2248 {
2249 if (n > 0)
2250 ch = getc(csinfo[i].fr_fp);
2251 if (ch == EOF)
2252 {
2253 PERROR("cs_read_prompt EOF");
2254 if (buf != NULL && buf[0] != NUL)
2255 (void)EMSG2(cs_emsg, buf);
2256 else if (p_csverbose)
2257 cs_reading_emsg(i); /* don't have additional information */
2258 cs_release_csp(i, TRUE);
2259 vim_free(buf);
2260 return CSCOPE_FAILURE;
2261 }
2262
2263 if (ch != CSCOPE_PROMPT[n])
2264 {
2265 ch = EOF;
2266 break;
2267 }
2268 }
2269
2270 if (ch == EOF)
2271 continue; /* didn't find the prompt */
2272 break; /* did find the prompt */
2273 }
2274
2275 vim_free(buf);
2276 return CSCOPE_SUCCESS;
2277}
2278
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002279#if defined(UNIX) && defined(SIGALRM)
2280/*
2281 * Used to catch and ignore SIGALRM below.
2282 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002283 static RETSIGTYPE
2284sig_handler SIGDEFARG(sigarg)
2285{
2286 /* do nothing */
2287 SIGRETURN;
2288}
2289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291/*
2292 * PRIVATE: cs_release_csp
2293 *
Bram Moolenaar02b06312007-09-06 15:39:22 +00002294 * Does the actual free'ing for the cs ptr with an optional flag of whether
2295 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 */
2297 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002298cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 /*
2301 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2302 */
2303 if (csinfo[i].to_fp != NULL)
2304 {
2305 (void)fputs("q\n", csinfo[i].to_fp);
2306 (void)fflush(csinfo[i].to_fp);
2307 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002308#if defined(UNIX)
2309 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002310 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002311 int pstat;
2312 pid_t pid;
2313
2314# if defined(HAVE_SIGACTION)
2315 struct sigaction sa, old;
2316
Bram Moolenaar9701da02008-03-16 12:09:58 +00002317 /* Use sigaction() to limit the waiting time to two seconds. */
2318 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002319 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002320# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002321 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002322# else
2323 sa.sa_flags = 0;
2324# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002325 sigaction(SIGALRM, &sa, &old);
2326 alarm(2); /* 2 sec timeout */
2327
2328 /* Block until cscope exits or until timer expires */
2329 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002330 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002331
2332 /* cancel pending alarm if still there and restore signal */
2333 alarm(0);
2334 sigaction(SIGALRM, &old, NULL);
2335# else
2336 int waited;
2337
2338 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2339 * to give cscope a chance to exit quickly. */
2340 sleep(0);
2341 for (waited = 0; waited < 40; ++waited)
2342 {
2343 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002344 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002345 if (pid != 0)
2346 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002347 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002348 }
2349# endif
2350 /*
2351 * If the cscope process is still running: kill it.
2352 * Safety check: If the PID would be zero here, the entire X session
2353 * would be killed. -1 and 1 are dangerous as well.
2354 */
2355 if (pid < 0 && csinfo[i].pid > 1)
2356 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002357# ifdef ECHILD
2358 int alive = TRUE;
2359
2360 if (waitpid_errno == ECHILD)
2361 {
2362 /*
2363 * When using 'vim -g', vim is forked and cscope process is
2364 * no longer a child process but a sibling. So waitpid()
2365 * fails with errno being ECHILD (No child processes).
2366 * Don't send SIGKILL to cscope immediately but wait
2367 * (polling) for it to exit normally as result of sending
2368 * the "q" command, hence giving it a chance to clean up
2369 * its temporary files.
2370 */
2371 int waited;
2372
2373 sleep(0);
2374 for (waited = 0; waited < 40; ++waited)
2375 {
2376 /* Check whether cscope process is still alive */
2377 if (kill(csinfo[i].pid, 0) != 0)
2378 {
2379 alive = FALSE; /* cscope process no longer exists */
2380 break;
2381 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002382 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002383 }
2384 }
2385 if (alive)
2386# endif
2387 {
2388 kill(csinfo[i].pid, SIGKILL);
2389 (void)waitpid(csinfo[i].pid, &pstat, 0);
2390 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002391 }
2392 }
2393#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002394 if (csinfo[i].hProc != NULL)
2395 {
2396 /* Give cscope a chance to exit normally */
2397 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2398 TerminateProcess(csinfo[i].hProc, 0);
2399 CloseHandle(csinfo[i].hProc);
2400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401#endif
2402
2403 if (csinfo[i].fr_fp != NULL)
2404 (void)fclose(csinfo[i].fr_fp);
2405 if (csinfo[i].to_fp != NULL)
2406 (void)fclose(csinfo[i].to_fp);
2407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (freefnpp)
2409 {
2410 vim_free(csinfo[i].fname);
2411 vim_free(csinfo[i].ppath);
2412 vim_free(csinfo[i].flags);
2413 }
2414
2415 clear_csinfo(i);
2416} /* cs_release_csp */
2417
2418
2419/*
2420 * PRIVATE: cs_reset
2421 *
2422 * calls cs_kill on all cscope connections then reinits
2423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002425cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2428 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002429 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002431 if (csinfo_size == 0)
2432 return CSCOPE_SUCCESS;
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002435 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2436 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2437 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (dblist == NULL || pplist == NULL || fllist == NULL)
2439 {
2440 vim_free(dblist);
2441 vim_free(pplist);
2442 vim_free(fllist);
2443 return CSCOPE_FAILURE;
2444 }
2445
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002446 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 dblist[i] = csinfo[i].fname;
2449 pplist[i] = csinfo[i].ppath;
2450 fllist[i] = csinfo[i].flags;
2451 if (csinfo[i].fname != NULL)
2452 cs_release_csp(i, FALSE);
2453 }
2454
2455 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002456 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 if (dblist[i] != NULL)
2459 {
2460 cs_add_common(dblist[i], pplist[i], fllist[i]);
2461 if (p_csverbose)
2462 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002463 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 * connection number in the same line as
2465 * "Added cscope database..."
2466 */
2467 sprintf(buf, " (#%d)", i);
2468 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2469 }
2470 }
2471 vim_free(dblist[i]);
2472 vim_free(pplist[i]);
2473 vim_free(fllist[i]);
2474 }
2475 vim_free(dblist);
2476 vim_free(pplist);
2477 vim_free(fllist);
2478
2479 if (p_csverbose)
2480 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2481 return CSCOPE_SUCCESS;
2482} /* cs_reset */
2483
2484
2485/*
2486 * PRIVATE: cs_resolve_file
2487 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002488 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * (Prepends ppath, if there is one and if it's not already prepended,
2490 * otherwise just uses the name found.)
2491 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002492 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002494 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002497cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002499 char *fullname;
2500 int len;
2501 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002504 * Ppath is freed when we destroy the cscope connection.
2505 * Fullname is freed after cs_make_vim_style_matches, after it's been
2506 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002508 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002510 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002511 else if (p_csre && csinfo[i].fname != NULL)
2512 {
2513 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2514 * path in path resolution. */
2515 csdir = alloc(MAXPATHL);
2516 if (csdir != NULL)
2517 {
2518 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaar28c21912013-05-29 19:18:00 +02002519 gettail((char_u *)csinfo[i].fname)
2520 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002521 len += (int)STRLEN(csdir);
2522 }
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002525 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002527 * happens, you are screwed up and need to fix how you're using cscope. */
2528 if (csinfo[i].ppath != NULL
2529 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2530 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002532 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002534 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002535 {
2536 if ((fullname = (char *)alloc(len)) != NULL)
2537 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2538 }
2539 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002540 {
2541 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002542 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002543 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002546 {
2547 fullname = (char *)vim_strsave((char_u *)name);
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002550 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554
2555/*
2556 * PRIVATE: cs_show
2557 *
2558 * show all cscope connections
2559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002561cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 short i;
2564 if (cs_cnt_connections() == 0)
2565 MSG_PUTS(_("no cscope connections\n"));
2566 else
2567 {
2568 MSG_PUTS_ATTR(
2569 _(" # pid database name prepend path\n"),
2570 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002571 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 if (csinfo[i].fname == NULL)
2574 continue;
2575
2576 if (csinfo[i].ppath != NULL)
2577 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2578 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2579 else
2580 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2581 i, (long)csinfo[i].pid, csinfo[i].fname);
2582 }
2583 }
2584
2585 wait_return(TRUE);
2586 return CSCOPE_SUCCESS;
2587} /* cs_show */
2588
Bram Moolenaar02b06312007-09-06 15:39:22 +00002589
2590/*
2591 * PUBLIC: cs_end
2592 *
2593 * Only called when VIM exits to quit any cscope sessions.
2594 */
2595 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002596cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002597{
2598 int i;
2599
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002600 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002601 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002602 vim_free(csinfo);
2603 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002604}
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#endif /* FEAT_CSCOPE */
2607
2608/* the end */