blob: 5b384982e36ae7802185c6a2d823e5aff99fe4c2 [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/*
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000204 * Find the command, print help if invalid, and then call the corresponding
205 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
207 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100208do_cscope_general(
209 exarg_T *eap,
Bram Moolenaar42b8d912017-01-15 17:18:57 +0100210 int make_split UNUSED) /* whether to split window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 cscmd_T *cmdp;
213
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
215 {
216 cs_help(eap);
217 return;
218 }
219
220#ifdef FEAT_WINDOWS
221 if (make_split)
222 {
223 if (!cmdp->cansplit)
224 {
225 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
226 return;
227 }
228 postponed_split = -1;
229 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000230 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
232#endif
233
234 cmdp->func(eap);
235
236#ifdef FEAT_WINDOWS
237 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000238 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#endif
240}
241
242/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100243 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 */
245 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 do_cscope_general(eap, FALSE);
249}
250
251/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100252 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 */
254 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100255ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 do_cscope_general(eap, TRUE);
258}
259
260/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100261 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 */
263 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100264ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 int ret = FALSE;
267
Bram Moolenaar446cb832008-06-24 21:56:24 +0000268 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 (void)EMSG(_("E562: Usage: cstag <ident>"));
271 return;
272 }
273
274 switch (p_csto)
275 {
276 case 0 :
277 if (cs_check_for_connections())
278 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000279 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200280 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (ret == FALSE)
282 {
283 cs_free_tags();
284 if (msg_col)
285 msg_putchar('\n');
286
287 if (cs_check_for_tags())
288 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
289 }
290 }
291 else if (cs_check_for_tags())
292 {
293 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
294 }
295 break;
296 case 1 :
297 if (cs_check_for_tags())
298 {
299 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
300 if (ret == FALSE)
301 {
302 if (msg_col)
303 msg_putchar('\n');
304
305 if (cs_check_for_connections())
306 {
307 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200308 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (ret == FALSE)
310 cs_free_tags();
311 }
312 }
313 }
314 else if (cs_check_for_connections())
315 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000316 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200317 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 if (ret == FALSE)
319 cs_free_tags();
320 }
321 break;
322 default :
323 break;
324 }
325
326 if (!ret)
327 {
328 (void)EMSG(_("E257: cstag: tag not found"));
329#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
330 g_do_tagpreview = 0;
331#endif
332 }
333
Bram Moolenaard4db7712016-11-12 19:16:46 +0100334}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
336
337/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100338 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 * from the cscope output. should only be called from find_tags()
340 *
341 * returns TRUE if eof, FALSE otherwise
342 */
343 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100344cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346 char *p;
347
348 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
349 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000350 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return FALSE;
353} /* cs_fgets */
354
355
356/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100357 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 */
359 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100360cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361{
362 cs_manage_matches(NULL, NULL, -1, Free);
363}
364
365
366/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100367 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 */
369 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100370cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 cs_manage_matches(NULL, NULL, -1, Print);
373}
374
375
376/*
377 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
378 *
379 * Checks for the existence of a |cscope| connection. If no
380 * parameters are specified, then the function returns:
381 *
382 * 0, if cscope was not available (not compiled in), or if there
383 * are no cscope connections; or
384 * 1, if there is at least one cscope connection.
385 *
386 * If parameters are specified, then the value of {num}
387 * determines how existence of a cscope connection is checked:
388 *
389 * {num} Description of existence check
390 * ----- ------------------------------
391 * 0 Same as no parameters (e.g., "cscope_connection()").
392 * 1 Ignore {prepend}, and use partial string matches for
393 * {dbpath}.
394 * 2 Ignore {prepend}, and use exact string matches for
395 * {dbpath}.
396 * 3 Use {prepend}, use partial string matches for both
397 * {dbpath} and {prepend}.
398 * 4 Use {prepend}, use exact string matches for both
399 * {dbpath} and {prepend}.
400 *
401 * Note: All string comparisons are case sensitive!
402 */
403#if defined(FEAT_EVAL) || defined(PROTO)
404 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100405cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
407 int i;
408
409 if (num < 0 || num > 4 || (num > 0 && !dbpath))
410 return FALSE;
411
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000412 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
414 if (!csinfo[i].fname)
415 continue;
416
417 if (num == 0)
418 return TRUE;
419
420 switch (num)
421 {
422 case 1:
423 if (strstr(csinfo[i].fname, (char *)dbpath))
424 return TRUE;
425 break;
426 case 2:
427 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
428 return TRUE;
429 break;
430 case 3:
431 if (strstr(csinfo[i].fname, (char *)dbpath)
432 && ((!ppath && !csinfo[i].ppath)
433 || (ppath
434 && csinfo[i].ppath
435 && strstr(csinfo[i].ppath, (char *)ppath))))
436 return TRUE;
437 break;
438 case 4:
439 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
440 && ((!ppath && !csinfo[i].ppath)
441 || (ppath
442 && csinfo[i].ppath
443 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
444 return TRUE;
445 break;
446 }
447 }
448
449 return FALSE;
450} /* cs_connection */
451#endif
452
453
454/*
455 * PRIVATE functions
456 ****************************************************************************/
457
458/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100459 * Add cscope database or a directory name (to look for cscope.out)
460 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100463cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 char *fname, *ppath, *flags = NULL;
466
467 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
468 {
469 cs_usage_msg(Add);
470 return CSCOPE_FAILURE;
471 }
472 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
473 flags = strtok((char *)NULL, (const char *)" ");
474
475 return cs_add_common(fname, ppath, flags);
476}
477
478 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100479cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 char *stat_emsg = _("E563: stat(%s) error: %d");
482 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
483
484 if (buf != NULL)
485 {
486 (void)sprintf(buf, stat_emsg, fname, errno);
487 (void)EMSG(buf);
488 vim_free(buf);
489 }
490 else
491 (void)EMSG(_("E563: stat error"));
492}
493
494
495/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100496 * The common routine to add a new cscope connection. Called by
497 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * routine uses a number of goto statements.
499 */
500 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100501cs_add_common(
502 char *arg1, /* filename - may contain environment variables */
503 char *arg2, /* prepend path - may contain environment variables */
504 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200506 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000507 int ret;
508 char *fname = NULL;
509 char *fname2 = NULL;
510 char *ppath = NULL;
511 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200512#ifdef FEAT_MODIFY_FNAME
513 int len;
514 int usedlen = 0;
515 char_u *fbuf = NULL;
516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000519 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 goto add_err;
521
522 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200523#ifdef FEAT_MODIFY_FNAME
524 len = (int)STRLEN(fname);
525 fbuf = (char_u *)fname;
526 (void)modify_fname((char_u *)":p", &usedlen,
527 (char_u **)&fname, &fbuf, &len);
528 if (fname == NULL)
529 goto add_err;
530 fname = (char *)vim_strnsave((char_u *)fname, len);
531 vim_free(fbuf);
532#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200533 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (ret < 0)
535 {
536staterr:
537 if (p_csverbose)
538 cs_stat_emsg(fname);
539 goto add_err;
540 }
541
542 /* get the prepend path (arg2), expand it, and try to stat it */
543 if (arg2 != NULL)
544 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200545 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000547 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 goto add_err;
549
550 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200551 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (ret < 0)
553 goto staterr;
554 }
555
556 /* if filename is a directory, append the cscope database name to it */
557 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
558 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000559 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (fname2 == NULL)
561 goto add_err;
562
563 while (fname[strlen(fname)-1] == '/'
564#ifdef WIN32
565 || fname[strlen(fname)-1] == '\\'
566#endif
567 )
568 {
569 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200570 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 break;
572 }
573 if (fname[0] == '\0')
574 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
575 else
576 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
577
Bram Moolenaar8767f522016-07-01 17:17:39 +0200578 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (ret < 0)
580 {
581 if (p_csverbose)
582 cs_stat_emsg(fname2);
583 goto add_err;
584 }
585
586 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
587 }
588#if defined(UNIX)
589 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
590#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000591 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
593#endif
594 {
595 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
596 }
597 else
598 {
599 if (p_csverbose)
600 (void)EMSG2(
601 _("E564: %s is not a directory or a valid cscope database"),
602 fname);
603 goto add_err;
604 }
605
606 if (i != -1)
607 {
608 if (cs_create_connection(i) == CSCOPE_FAILURE
609 || cs_read_prompt(i) == CSCOPE_FAILURE)
610 {
611 cs_release_csp(i, TRUE);
612 goto add_err;
613 }
614
615 if (p_csverbose)
616 {
617 msg_clr_eos();
618 (void)smsg_attr(hl_attr(HLF_R),
619 (char_u *)_("Added cscope database %s"),
620 csinfo[i].fname);
621 }
622 }
623
624 vim_free(fname);
625 vim_free(fname2);
626 vim_free(ppath);
627 return CSCOPE_SUCCESS;
628
629add_err:
630 vim_free(fname2);
631 vim_free(fname);
632 vim_free(ppath);
633 return CSCOPE_FAILURE;
634} /* cs_add_common */
635
636
637 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100638cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
640 return (cs_cnt_connections() > 0);
641} /* cs_check_for_connections */
642
643
644 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100645cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000647 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648} /* cs_check_for_tags */
649
650
651/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100652 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
654 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100655cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 short i;
658 short cnt = 0;
659
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000660 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 if (csinfo[i].fname != NULL)
663 cnt++;
664 }
665 return cnt;
666} /* cs_cnt_connections */
667
668 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100669cs_reading_emsg(
670 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 EMSGN(_("E262: error reading cscope connection %ld"), idx);
673}
674
675#define CSREAD_BUFSIZE 2048
676/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100677 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 */
679 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100680cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 char *stok;
683 char *buf;
684 int nlines;
685
686 buf = (char *)alloc(CSREAD_BUFSIZE);
687 if (buf == NULL)
688 return 0;
689 for (;;)
690 {
691 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
692 {
693 if (feof(csinfo[idx].fr_fp))
694 errno = EIO;
695
696 cs_reading_emsg(idx);
697
698 vim_free(buf);
699 return -1;
700 }
701
702 /*
703 * If the database is out of date, or there's some other problem,
704 * cscope will output error messages before the number-of-lines output.
705 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000706 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
708 if ((stok = strtok(buf, (const char *)" ")) == NULL)
709 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000710 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 continue;
712
713 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
714 continue;
715 nlines = atoi(stok);
716 if (nlines < 0)
717 {
718 nlines = 0;
719 break;
720 }
721
722 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
723 continue;
724 if (strncmp((const char *)stok, "lines", 5))
725 continue;
726
727 break;
728 }
729
730 vim_free(buf);
731 return nlines;
732} /* cs_cnt_matches */
733
734
735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * Creates the actual cscope command query from what the user entered.
737 */
738 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100739cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
741 char *cmd;
742 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000743 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 switch (csoption[0])
746 {
747 case '0' : case 's' :
748 search = 0;
749 break;
750 case '1' : case 'g' :
751 search = 1;
752 break;
753 case '2' : case 'd' :
754 search = 2;
755 break;
756 case '3' : case 'c' :
757 search = 3;
758 break;
759 case '4' : case 't' :
760 search = 4;
761 break;
762 case '6' : case 'e' :
763 search = 6;
764 break;
765 case '7' : case 'f' :
766 search = 7;
767 break;
768 case '8' : case 'i' :
769 search = 8;
770 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200771 case '9' : case 'a' :
772 search = 9;
773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 default :
775 (void)EMSG(_("E561: unknown cscope search type"));
776 cs_usage_msg(Find);
777 return NULL;
778 }
779
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000780 /* Skip white space before the patter, except for text and pattern search,
781 * they may want to use the leading white space. */
782 pat = pattern;
783 if (search != 4 && search != 6)
784 while vim_iswhite(*pat)
785 ++pat;
786
787 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 return NULL;
789
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000790 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 return cmd;
793} /* cs_create_cmd */
794
795
796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 * This piece of code was taken/adapted from nvi. do we need to add
798 * the BSD license notice?
799 */
800 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100801cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000803#ifdef UNIX
804 int to_cs[2], from_cs[2];
805#endif
806 int len;
807 char *prog, *cmd, *ppath = NULL;
808#ifdef WIN32
809 int fd;
810 SECURITY_ATTRIBUTES sa;
811 PROCESS_INFORMATION pi;
812 STARTUPINFO si;
813 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
814 HANDLE stdin_rd, stdout_rd;
815 HANDLE stdout_wr, stdin_wr;
816 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200817# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
818# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000819# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200820# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822#endif
823
Bram Moolenaar02b06312007-09-06 15:39:22 +0000824#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
826 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
827 * from_cs[0] and writes to to_cs[1].
828 */
829 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
830 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
831 {
832 (void)EMSG(_("E566: Could not create cscope pipes"));
833err_closing:
834 if (to_cs[0] != -1)
835 (void)close(to_cs[0]);
836 if (to_cs[1] != -1)
837 (void)close(to_cs[1]);
838 if (from_cs[0] != -1)
839 (void)close(from_cs[0]);
840 if (from_cs[1] != -1)
841 (void)close(from_cs[1]);
842 return CSCOPE_FAILURE;
843 }
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 switch (csinfo[i].pid = fork())
846 {
847 case -1:
848 (void)EMSG(_("E622: Could not fork for cscope"));
849 goto err_closing;
850 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (dup2(to_cs[0], STDIN_FILENO) == -1)
852 PERROR("cs_create_connection 1");
853 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
854 PERROR("cs_create_connection 2");
855 if (dup2(from_cs[1], STDERR_FILENO) == -1)
856 PERROR("cs_create_connection 3");
857
858 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 (void)close(to_cs[1]);
860 (void)close(from_cs[0]);
861#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000862 /* WIN32 */
863 /* Create pipes to communicate with cscope */
864 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
865 sa.bInheritHandle = TRUE;
866 sa.lpSecurityDescriptor = NULL;
867
868 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
869 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
870 {
871 (void)EMSG(_("E566: Could not create cscope pipes"));
872err_closing:
873 if (pipe_stdin)
874 {
875 CloseHandle(stdin_rd);
876 CloseHandle(stdin_wr);
877 }
878 if (pipe_stdout)
879 {
880 CloseHandle(stdout_rd);
881 CloseHandle(stdout_wr);
882 }
883 return CSCOPE_FAILURE;
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#endif
886 /* expand the cscope exec for env var's */
887 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
888 {
889#ifdef UNIX
890 return CSCOPE_FAILURE;
891#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000892 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 goto err_closing;
894#endif
895 }
896 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
897
898 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000899 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (csinfo[i].ppath)
901 {
902 /* expand the prepend path for env var's */
903 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
904 {
905 vim_free(prog);
906#ifdef UNIX
907 return CSCOPE_FAILURE;
908#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000909 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 goto err_closing;
911#endif
912 }
913 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
914
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000915 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000919 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 if ((cmd = (char *)alloc(len)) == NULL)
922 {
923 vim_free(prog);
924 vim_free(ppath);
925#ifdef UNIX
926 return CSCOPE_FAILURE;
927#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000928 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 goto err_closing;
930#endif
931 }
932
933 /* run the cscope command; is there execl for non-unix systems? */
934#if defined(UNIX)
935 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
936#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000937 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
939#endif
940 if (csinfo[i].ppath != NULL)
941 {
942 (void)strcat(cmd, " -P");
943 (void)strcat(cmd, csinfo[i].ppath);
944 }
945 if (csinfo[i].flags != NULL)
946 {
947 (void)strcat(cmd, " ");
948 (void)strcat(cmd, csinfo[i].flags);
949 }
950# ifdef UNIX
951 /* on Win32 we still need prog */
952 vim_free(prog);
953# endif
954 vim_free(ppath);
955
956#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200957# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
958 /* Change our process group to avoid cscope receiving SIGWINCH. */
959# if defined(HAVE_SETSID)
960 (void)setsid();
961# else
962 if (setpgid(0, 0) == -1)
963 PERROR(_("cs_create_connection setpgid failed"));
964# endif
965# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000966 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 PERROR(_("cs_create_connection exec failed"));
968
969 exit(127);
970 /* NOTREACHED */
971 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 /*
973 * Save the file descriptors for later duplication, and
974 * reopen as streams.
975 */
976 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
977 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
978 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
979 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 /* close unused */
982 (void)close(to_cs[0]);
983 (void)close(from_cs[1]);
984
985 break;
986 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000989 /* WIN32 */
990 /* Create a new process to run cscope and use pipes to talk with it */
991 GetStartupInfo(&si);
992 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
993 si.wShowWindow = SW_HIDE; /* Hide child application window */
994 si.hStdOutput = stdout_wr;
995 si.hStdError = stdout_wr;
996 si.hStdInput = stdin_rd;
997 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
998 NULL, NULL, &si, &pi);
999 vim_free(prog);
1000 vim_free(cmd);
1001
1002 if (!created)
1003 {
1004 PERROR(_("cs_create_connection exec failed"));
1005 (void)EMSG(_("E623: Could not spawn cscope process"));
1006 goto err_closing;
1007 }
1008 /* else */
1009 csinfo[i].pid = pi.dwProcessId;
1010 csinfo[i].hProc = pi.hProcess;
1011 CloseHandle(pi.hThread);
1012
1013 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001014 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1015 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001016 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1017 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001018 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1019 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001020 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1021 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1022
1023 /* Close handles for file descriptors inherited by the cscope process */
1024 CloseHandle(stdin_rd);
1025 CloseHandle(stdout_wr);
1026
1027#endif /* !UNIX */
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return CSCOPE_SUCCESS;
1030} /* cs_create_connection */
1031
1032
1033/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001034 * Query cscope using command line interface. Parse the output and use tselect
1035 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 *
1037 * returns TRUE if we jump to a tag or abort, FALSE if not.
1038 */
1039 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001040cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001043 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045 if (cs_check_for_connections() == FALSE)
1046 {
1047 (void)EMSG(_("E567: no cscope connections"));
1048 return FALSE;
1049 }
1050
1051 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1052 {
1053 cs_usage_msg(Find);
1054 return FALSE;
1055 }
1056
1057 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001058 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 cs_usage_msg(Find);
1061 return FALSE;
1062 }
1063
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001064 /*
1065 * Let's replace the NULs written by strtok() with spaces - we need the
1066 * spaces to correctly display the quickfix/location list window's title.
1067 */
1068 for (i = 0; i < eap_arg_len; ++i)
1069 if (NUL == eap->arg[i])
1070 eap->arg[i] = ' ';
1071
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001072 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001073 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074} /* cs_find */
1075
1076
1077/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001078 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
1080 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001081cs_find_common(
1082 char *opt,
1083 char *pat,
1084 int forceit,
1085 int verbose,
1086 int use_ll UNUSED,
1087 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 int i;
1090 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001091 int *nummatches;
1092 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_QUICKFIX
1094 char cmdletter;
1095 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001096
1097 /* get cmd letter */
1098 switch (opt[0])
1099 {
1100 case '0' :
1101 cmdletter = 's';
1102 break;
1103 case '1' :
1104 cmdletter = 'g';
1105 break;
1106 case '2' :
1107 cmdletter = 'd';
1108 break;
1109 case '3' :
1110 cmdletter = 'c';
1111 break;
1112 case '4' :
1113 cmdletter = 't';
1114 break;
1115 case '6' :
1116 cmdletter = 'e';
1117 break;
1118 case '7' :
1119 cmdletter = 'f';
1120 break;
1121 case '8' :
1122 cmdletter = 'i';
1123 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001124 case '9' :
1125 cmdletter = 'a';
1126 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001127 default :
1128 cmdletter = opt[0];
1129 }
1130
1131 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1132 if (qfpos != NULL)
1133 {
1134 qfpos++;
1135 /* next symbol must be + or - */
1136 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1137 {
1138 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1139 char *buf = (char *)alloc((unsigned)strlen(nf));
1140
1141 /* strlen will be enough because we use chars */
1142 if (buf != NULL)
1143 {
1144 sprintf(buf, nf, *qfpos, *(qfpos-1));
1145 (void)EMSG(buf);
1146 vim_free(buf);
1147 }
1148 return FALSE;
1149 }
1150
1151# ifdef FEAT_AUTOCMD
Bram Moolenaar21662be2016-11-06 14:46:44 +01001152 if (*qfpos != '0'
1153 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1154 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001155 {
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001156# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001157 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001158 return FALSE;
1159# endif
1160 }
1161# endif
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163#endif
1164
1165 /* create the actual command to send to cscope */
1166 cmd = cs_create_cmd(opt, pat);
1167 if (cmd == NULL)
1168 return FALSE;
1169
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001170 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1171 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001172 {
1173 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001174 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001175 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001176
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001177 /* Send query to all open connections, then count the total number
1178 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001179 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 nummatches[i] = 0;
1181 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001182 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001184 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 continue;
1186
1187 /* send cmd to cscope */
1188 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1189 (void)fflush(csinfo[i].to_fp);
1190
1191 nummatches[i] = cs_cnt_matches(i);
1192
1193 if (nummatches[i] > -1)
1194 totmatches += nummatches[i];
1195
1196 if (nummatches[i] == 0)
1197 (void)cs_read_prompt(i);
1198 }
1199 vim_free(cmd);
1200
1201 if (totmatches == 0)
1202 {
1203 char *nf = _("E259: no matches found for cscope query %s of %s");
1204 char *buf;
1205
1206 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001207 {
1208 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001212 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (buf == NULL)
1214 (void)EMSG(nf);
1215 else
1216 {
1217 sprintf(buf, nf, opt, pat);
1218 (void)EMSG(buf);
1219 vim_free(buf);
1220 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001221 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 return FALSE;
1223 }
1224
1225#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001226 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
1228 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001229 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001230 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001231 qf_info_T *qi = NULL;
1232 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001234 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001235 if (f == NULL)
1236 EMSG2(_(e_notopen), tmp);
1237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001239 cs_file_results(f, nummatches);
1240 fclose(f);
1241 if (use_ll) /* Use location list */
1242 wp = curwin;
1243 /* '-' starts a new error list */
1244 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001245 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001247# ifdef FEAT_WINDOWS
1248 if (postponed_split != 0)
1249 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001250 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001252 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001253 postponed_split = 0;
1254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255# endif
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001256
1257# ifdef FEAT_AUTOCMD
1258 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1259 curbuf->b_fname, TRUE, curbuf);
1260# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001261 if (use_ll)
1262 /*
1263 * In the location list window, use the displayed location
1264 * list. Otherwise, use the location list for the window.
1265 */
1266 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1267 ? wp->w_llist_ref : wp->w_llist;
1268 qf_jump(qi, 0, 0, forceit);
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 mch_remove(tmp);
1272 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001273 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 return TRUE;
1275 }
1276 else
1277#endif /* FEAT_QUICKFIX */
1278 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001279 char **matches = NULL, **contexts = NULL;
1280 int matched = 0;
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 /* read output */
1283 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1284 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001285 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (matches == NULL)
1287 return FALSE;
1288
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001289 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1292 }
1293
1294} /* cs_find_common */
1295
1296/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001297 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001300cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 cscmd_T *cmdp = cs_cmds;
1303
1304 (void)MSG_PUTS(_("cscope commands:\n"));
1305 while (cmdp->name != NULL)
1306 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001307 char *help = _(cmdp->help);
1308 int space_cnt = 30 - vim_strsize((char_u *)help);
1309
1310 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1311 if (space_cnt < 0)
1312 space_cnt = 0;
1313 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1314 cmdp->name,
1315 help, space_cnt, " ",
1316 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001318 MSG_PUTS(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001319 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001320 " c: Find functions calling this function\n"
1321 " d: Find functions called by this function\n"
1322 " e: Find this egrep pattern\n"
1323 " f: Find this file\n"
1324 " g: Find this definition\n"
1325 " i: Find files #including this file\n"
1326 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001327 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 cmdp++;
1330 }
1331
1332 wait_return(TRUE);
1333 return 0;
1334} /* cs_help */
1335
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001338clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 csinfo[i].fname = NULL;
1341 csinfo[i].ppath = NULL;
1342 csinfo[i].flags = NULL;
1343#if defined(UNIX)
1344 csinfo[i].st_dev = (dev_t)0;
1345 csinfo[i].st_ino = (ino_t)0;
1346#else
1347 csinfo[i].nVolume = 0;
1348 csinfo[i].nIndexHigh = 0;
1349 csinfo[i].nIndexLow = 0;
1350#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001351 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 csinfo[i].fr_fp = NULL;
1353 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001354#if defined(WIN32)
1355 csinfo[i].hProc = NULL;
1356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357}
1358
1359#ifndef UNIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001360static char *GetWin32Error(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001363GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 char *msg = NULL;
1366 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1367 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1368 if (msg != NULL)
1369 {
1370 /* remove trailing \r\n */
1371 char *pcrlf = strstr(msg, "\r\n");
1372 if (pcrlf != NULL)
1373 *pcrlf = '\0';
1374 }
1375 return msg;
1376}
1377#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001380 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001383cs_insert_filelist(
1384 char *fname,
1385 char *ppath,
1386 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001387 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 short i, j;
1390#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 BY_HANDLE_FILE_INFORMATION bhfi;
1392
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001393 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001395 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1396 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (p_csverbose)
1398 {
1399 char *cant_msg = _("E625: cannot open cscope database: %s");
1400 char *winmsg = GetWin32Error();
1401
1402 if (winmsg != NULL)
1403 {
1404 (void)EMSG2(cant_msg, winmsg);
1405 LocalFree(winmsg);
1406 }
1407 else
1408 /* subst filename if can't get error text */
1409 (void)EMSG2(cant_msg, fname);
1410 }
1411 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001412
1413 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (p_csverbose)
1415 (void)EMSG(_("E626: cannot get cscope database information"));
1416 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418#endif
1419
1420 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001421 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
1423 if (csinfo[j].fname != NULL
1424#if defined(UNIX)
1425 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1426#else
1427 /* compare pathnames first */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001428 && ((fullpathcmp((char_u *)csinfo[j].fname,
1429 (char_u *)fname, FALSE) & FPC_SAME)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001430 /* test index file attributes too */
1431 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1433 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1434#endif
1435 )
1436 {
1437 if (p_csverbose)
1438 (void)EMSG(_("E568: duplicate cscope database not added"));
1439 return -1;
1440 }
1441
1442 if (csinfo[j].fname == NULL && i == -1)
1443 i = j; /* remember first empty entry */
1444 }
1445
1446 if (i == -1)
1447 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001448 i = csinfo_size;
1449 if (csinfo_size == 0)
1450 {
1451 /* First time allocation: allocate only 1 connection. It should
1452 * be enough for most users. If more is needed, csinfo will be
1453 * reallocated. */
1454 csinfo_size = 1;
1455 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1456 }
1457 else
1458 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001459 csinfo_T *t_csinfo = csinfo;
1460
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001461 /* Reallocate space for more connections. */
1462 csinfo_size *= 2;
1463 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001464 if (csinfo == NULL)
1465 {
1466 vim_free(t_csinfo);
1467 csinfo_size = 0;
1468 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001469 }
1470 if (csinfo == NULL)
1471 return -1;
1472 for (j = csinfo_size/2; j < csinfo_size; j++)
1473 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 }
1475
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001476 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 return -1;
1478
1479 (void)strcpy(csinfo[i].fname, (const char *)fname);
1480
1481 if (ppath != NULL)
1482 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001483 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 vim_free(csinfo[i].fname);
1486 csinfo[i].fname = NULL;
1487 return -1;
1488 }
1489 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1490 } else
1491 csinfo[i].ppath = NULL;
1492
1493 if (flags != NULL)
1494 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 vim_free(csinfo[i].fname);
1498 vim_free(csinfo[i].ppath);
1499 csinfo[i].fname = NULL;
1500 csinfo[i].ppath = NULL;
1501 return -1;
1502 }
1503 (void)strcpy(csinfo[i].flags, (const char *)flags);
1504 } else
1505 csinfo[i].flags = NULL;
1506
1507#if defined(UNIX)
1508 csinfo[i].st_dev = sb->st_dev;
1509 csinfo[i].st_ino = sb->st_ino;
1510
1511#else
1512 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1513 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1514 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1515#endif
1516 return i;
1517} /* cs_insert_filelist */
1518
1519
1520/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001521 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
1523 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001524cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 cscmd_T *cmdp;
1527 char *stok;
1528 size_t len;
1529
1530 if (eap->arg == NULL)
1531 return NULL;
1532
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001533 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001534 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1537 return NULL;
1538
1539 len = strlen(stok);
1540 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1541 {
1542 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1543 return (cmdp);
1544 }
1545 return NULL;
1546} /* cs_lookup_cmd */
1547
1548
1549/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001550 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001553cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 char *stok;
1556 short i;
1557
1558 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1559 {
1560 cs_usage_msg(Kill);
1561 return CSCOPE_FAILURE;
1562 }
1563
1564 /* only single digit positive and negative integers are allowed */
1565 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1566 || (strlen(stok) < 3 && stok[0] == '-'
1567 && VIM_ISDIGIT((int)(stok[1]))))
1568 i = atoi(stok);
1569 else
1570 {
1571 /* It must be part of a name. We will try to find a match
1572 * within all the names in the csinfo data structure
1573 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001574 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1577 break;
1578 }
1579 }
1580
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001581 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 if (p_csverbose)
1584 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1585 }
1586 else
1587 {
1588 if (i == -1)
1589 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001590 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 if (csinfo[i].fname)
1593 cs_kill_execute(i, csinfo[i].fname);
1594 }
1595 }
1596 else
1597 cs_kill_execute(i, stok);
1598 }
1599
1600 return 0;
1601} /* cs_kill */
1602
1603
1604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * Actually kills a specific cscope connection.
1606 */
1607 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001608cs_kill_execute(
1609 int i, /* cscope table index */
1610 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 if (p_csverbose)
1613 {
1614 msg_clr_eos();
1615 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1616 (char_u *)_("cscope connection %s closed"), cname);
1617 }
1618 cs_release_csp(i, TRUE);
1619}
1620
1621
1622/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001623 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1625 * the type of the tag you are looking for. for example, in Darren Hiebert's
1626 * ctags (the one that comes with vim), #define's use a line number to find the
1627 * tag in a file while function definitions use a regexp search pattern.
1628 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001629 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 * quirky (and probably other things i don't know about):
1631 *
1632 * if you have "# define" in your source file, which is
1633 * perfectly legal, cscope thinks you have "#define". this
1634 * will result in a failed regexp search. :(
1635 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001636 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * would still have to be modified to escape all the special regular expression
1638 * characters to comply with ctags formatting.
1639 */
1640 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001641cs_make_vim_style_matches(
1642 char *fname,
1643 char *slno,
1644 char *search,
1645 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 /* vim style is ctags:
1648 *
1649 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1650 *
1651 * but as mentioned above, we'll always use the line number and
1652 * put the search pattern (if one exists) as "extra"
1653 *
1654 * buf is used as part of vim's method of handling tags, and
1655 * (i think) vim frees it when you pop your tags and get replaced
1656 * by new ones on the tag stack.
1657 */
1658 char *buf;
1659 int amt;
1660
1661 if (search != NULL)
1662 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001663 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if ((buf = (char *)alloc(amt)) == NULL)
1665 return NULL;
1666
1667 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1668 }
1669 else
1670 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001671 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if ((buf = (char *)alloc(amt)) == NULL)
1673 return NULL;
1674
1675 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1676 }
1677
1678 return buf;
1679} /* cs_make_vim_style_matches */
1680
1681
1682/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001683 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 *
1685 * Store: keep a ptr to the (malloc'd) memory of matches originally
1686 * generated from cs_find(). the matches are originally lines directly
1687 * from cscope output, but transformed to look like something out of a
1688 * ctags. see cs_make_vim_style_matches for more details.
1689 *
1690 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1691 * the next line from the cscope output. it basically keeps track of which
1692 * lines have been "used" and returns the next one.
1693 *
1694 * Free: frees up everything and resets
1695 *
1696 * Print: prints the tags
1697 */
1698 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001699cs_manage_matches(
1700 char **matches,
1701 char **contexts,
1702 int totmatches,
1703 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 static char **mp = NULL;
1706 static char **cp = NULL;
1707 static int cnt = -1;
1708 static int next = -1;
1709 char *p = NULL;
1710
1711 switch (cmd)
1712 {
1713 case Store:
1714 assert(matches != NULL);
1715 assert(totmatches > 0);
1716 if (mp != NULL || cp != NULL)
1717 (void)cs_manage_matches(NULL, NULL, -1, Free);
1718 mp = matches;
1719 cp = contexts;
1720 cnt = totmatches;
1721 next = 0;
1722 break;
1723 case Get:
1724 if (next >= cnt)
1725 return NULL;
1726
1727 p = mp[next];
1728 next++;
1729 break;
1730 case Free:
1731 if (mp != NULL)
1732 {
1733 if (cnt > 0)
1734 while (cnt--)
1735 {
1736 vim_free(mp[cnt]);
1737 if (cp != NULL)
1738 vim_free(cp[cnt]);
1739 }
1740 vim_free(mp);
1741 vim_free(cp);
1742 }
1743 mp = NULL;
1744 cp = NULL;
1745 cnt = 0;
1746 next = 0;
1747 break;
1748 case Print:
1749 cs_print_tags_priv(mp, cp, cnt);
1750 break;
1751 default: /* should not reach here */
Bram Moolenaar95f09602016-11-10 20:01:45 +01001752 IEMSG(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 return NULL;
1754 }
1755
1756 return p;
1757} /* cs_manage_matches */
1758
1759
1760/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001761 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 */
1763 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001764cs_parse_results(
1765 int cnumber,
1766 char *buf,
1767 int bufsize,
1768 char **context,
1769 char **linenumber,
1770 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int ch;
1773 char *p;
1774 char *name;
1775
1776 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1777 {
1778 if (feof(csinfo[cnumber].fr_fp))
1779 errno = EIO;
1780
1781 cs_reading_emsg(cnumber);
1782
1783 return NULL;
1784 }
1785
1786 /* If the line's too long for the buffer, discard it. */
1787 if ((p = strchr(buf, '\n')) == NULL)
1788 {
1789 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1790 ;
1791 return NULL;
1792 }
1793 *p = '\0';
1794
1795 /*
1796 * cscope output is in the following format:
1797 *
1798 * <filename> <context> <line number> <pattern>
1799 */
1800 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1801 return NULL;
1802 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1803 return NULL;
1804 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1805 return NULL;
1806 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1807
1808 /* --- nvi ---
1809 * If the file is older than the cscope database, that is,
1810 * the database was built since the file was last modified,
1811 * or there wasn't a search string, use the line number.
1812 */
1813 if (strcmp(*search, "<unknown>") == 0)
1814 *search = NULL;
1815
1816 name = cs_resolve_file(cnumber, name);
1817 return name;
1818}
1819
Bram Moolenaarc716c302006-01-21 22:12:51 +00001820#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001822 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
1824 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001825cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int i, j;
1828 char *buf;
1829 char *search, *slno;
1830 char *fullname;
1831 char *cntx;
1832 char *context;
1833
1834 buf = (char *)alloc(CSREAD_BUFSIZE);
1835 if (buf == NULL)
1836 return;
1837
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001838 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
1840 if (nummatches_a[i] < 1)
1841 continue;
1842
1843 for (j = 0; j < nummatches_a[i]; j++)
1844 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001845 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1846 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 continue;
1848
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001849 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001850 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 continue;
1852
1853 if (strcmp(cntx, "<global>")==0)
1854 strcpy(context, "<<global>>");
1855 else
1856 sprintf(context, "<<%s>>", cntx);
1857
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001858 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1860 else
1861 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1862
1863 vim_free(context);
1864 vim_free(fullname);
1865 } /* for all matches */
1866
1867 (void)cs_read_prompt(i);
1868
1869 } /* for all cscope connections */
1870 vim_free(buf);
1871}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001875 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1876 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001877 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 */
1879 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001880cs_fill_results(
1881 char *tagstr,
1882 int totmatches,
1883 int *nummatches_a,
1884 char ***matches_p,
1885 char ***cntxts_p,
1886 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887{
1888 int i, j;
1889 char *buf;
1890 char *search, *slno;
1891 int totsofar = 0;
1892 char **matches = NULL;
1893 char **cntxts = NULL;
1894 char *fullname;
1895 char *cntx;
1896
1897 assert(totmatches > 0);
1898
1899 buf = (char *)alloc(CSREAD_BUFSIZE);
1900 if (buf == NULL)
1901 return;
1902
1903 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1904 goto parse_out;
1905 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1906 goto parse_out;
1907
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001908 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 if (nummatches_a[i] < 1)
1911 continue;
1912
1913 for (j = 0; j < nummatches_a[i]; j++)
1914 {
1915 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1916 &slno, &search)) == NULL)
1917 continue;
1918
1919 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1920 search, tagstr);
1921
1922 vim_free(fullname);
1923
1924 if (strcmp(cntx, "<global>") == 0)
1925 cntxts[totsofar] = NULL;
1926 else
1927 /* note: if vim_strsave returns NULL, then the context
1928 * will be "<global>", which is misleading.
1929 */
1930 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1931
1932 if (matches[totsofar] != NULL)
1933 totsofar++;
1934
1935 } /* for all matches */
1936
1937 (void)cs_read_prompt(i);
1938
1939 } /* for all cscope connections */
1940
1941parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001942 if (totsofar == 0)
1943 {
1944 /* No matches, free the arrays and return NULL in "*matches_p". */
1945 vim_free(matches);
1946 matches = NULL;
1947 vim_free(cntxts);
1948 cntxts = NULL;
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 *matched = totsofar;
1951 *matches_p = matches;
1952 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 vim_free(buf);
1955} /* cs_fill_results */
1956
1957
1958/* get the requested path components */
1959 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001960cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
1962 int i;
1963 char *s;
1964
1965 if (p_cspc == 0)
1966 return path;
1967
1968 s = path + strlen(path) - 1;
1969 for (i = 0; i < p_cspc; ++i)
1970 while (s > path && *--s != '/'
1971#ifdef WIN32
1972 && *--s != '\\'
1973#endif
1974 )
1975 ;
1976 if ((s > path && *s == '/')
1977#ifdef WIN32
1978 || (s > path && *s == '\\')
1979#endif
1980 )
1981 ++s;
1982 return s;
1983}
1984
1985/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001986 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001989cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001992 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 int bufsize = 0; /* Track available bufsize */
1994 int newsize = 0;
1995 char *ptag;
1996 char *fname, *lno, *extra, *tbuf;
1997 int i, idx, num;
1998 char *globalcntx = "GLOBAL";
1999 char *cntxformat = " <<%s>>";
2000 char *context;
2001 char *cstag_msg = _("Cscope tag: %s");
2002 char *csfmt_str = "%4d %6s ";
2003
2004 assert (num_matches > 0);
2005
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002006 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 return;
2008
2009 strcpy(tbuf, matches[0]);
2010 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02002011 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002012 {
2013 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02002014 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002017 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 buf = (char *)alloc(newsize);
2019 if (buf != NULL)
2020 {
2021 bufsize = newsize;
2022 (void)sprintf(buf, cstag_msg, ptag);
2023 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2024 }
2025
2026 vim_free(tbuf);
2027
2028 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2029 msg_advance(msg_col + 2);
2030 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2031
2032 num = 1;
2033 for (i = 0; i < num_matches; i++)
2034 {
2035 idx = i;
2036
2037 /* if we really wanted to, we could avoid this malloc and strcpy
2038 * by parsing matches[i] on the fly and placing stuff into buf
2039 * directly, but that's too much of a hassle
2040 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002041 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 continue;
2043 (void)strcpy(tbuf, matches[idx]);
2044
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002045 if (strtok(tbuf, (const char *)"\t") == NULL
2046 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2047 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2048 {
2049 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002051 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002052 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2055
2056 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002057 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (bufsize < newsize)
2059 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002060 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 buf = (char *)vim_realloc(buf, newsize);
2062 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002065 vim_free(t_buf);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 bufsize = newsize;
2069 }
2070 if (buf != NULL)
2071 {
2072 /* csfmt_str = "%4d %6s "; */
2073 (void)sprintf(buf, csfmt_str, num, lno);
2074 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2075 }
2076 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2077
2078 /* compute the required space for the context */
2079 if (cntxts[idx] != NULL)
2080 context = cntxts[idx];
2081 else
2082 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 if (bufsize < newsize)
2086 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002087 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 buf = (char *)vim_realloc(buf, newsize);
2089 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002092 vim_free(t_buf);
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else
2095 bufsize = newsize;
2096 }
2097 if (buf != NULL)
2098 {
2099 (void)sprintf(buf, cntxformat, context);
2100
2101 /* print the context only if it fits on the same line */
2102 if (msg_col + (int)strlen(buf) >= (int)Columns)
2103 msg_putchar('\n');
2104 msg_advance(12);
2105 MSG_PUTS_LONG(buf);
2106 msg_putchar('\n');
2107 }
2108 if (extra != NULL)
2109 {
2110 msg_advance(13);
2111 MSG_PUTS_LONG(extra);
2112 }
2113
2114 vim_free(tbuf); /* only after printing extra due to strtok use */
2115
2116 if (msg_col)
2117 msg_putchar('\n');
2118
2119 ui_breakcheck();
2120 if (got_int)
2121 {
2122 got_int = FALSE; /* don't print any more matches */
2123 break;
2124 }
2125
2126 num++;
2127 } /* for all matches */
2128
2129 vim_free(buf);
2130} /* cs_print_tags_priv */
2131
2132
2133/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002134 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
2136 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002137cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 int ch;
2140 char *buf = NULL; /* buffer for possible error message from cscope */
2141 int bufpos = 0;
2142 char *cs_emsg;
2143 int maxlen;
2144 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 int n;
2147
2148 cs_emsg = _("E609: Cscope error: %s");
2149 /* compute maximum allowed len for Cscope error message */
2150 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2151
2152 for (;;)
2153 {
2154 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2155 /* if there is room and char is printable */
2156 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2157 {
2158 if (buf == NULL) /* lazy buffer allocation */
2159 buf = (char *)alloc(maxlen);
2160 if (buf != NULL)
2161 {
2162 /* append character to the message */
2163 buf[bufpos++] = ch;
2164 buf[bufpos] = NUL;
2165 if (bufpos >= epromptlen
2166 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2167 {
2168 /* remove eprompt from buf */
2169 buf[bufpos - epromptlen] = NUL;
2170
2171 /* print message to user */
2172 (void)EMSG2(cs_emsg, buf);
2173
2174 /* send RETURN to cscope */
2175 (void)putc('\n', csinfo[i].to_fp);
2176 (void)fflush(csinfo[i].to_fp);
2177
2178 /* clear buf */
2179 bufpos = 0;
2180 buf[bufpos] = NUL;
2181 }
2182 }
2183 }
2184
2185 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2186 {
2187 if (n > 0)
2188 ch = getc(csinfo[i].fr_fp);
2189 if (ch == EOF)
2190 {
2191 PERROR("cs_read_prompt EOF");
2192 if (buf != NULL && buf[0] != NUL)
2193 (void)EMSG2(cs_emsg, buf);
2194 else if (p_csverbose)
2195 cs_reading_emsg(i); /* don't have additional information */
2196 cs_release_csp(i, TRUE);
2197 vim_free(buf);
2198 return CSCOPE_FAILURE;
2199 }
2200
2201 if (ch != CSCOPE_PROMPT[n])
2202 {
2203 ch = EOF;
2204 break;
2205 }
2206 }
2207
2208 if (ch == EOF)
2209 continue; /* didn't find the prompt */
2210 break; /* did find the prompt */
2211 }
2212
2213 vim_free(buf);
2214 return CSCOPE_SUCCESS;
2215}
2216
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002217#if defined(UNIX) && defined(SIGALRM)
2218/*
2219 * Used to catch and ignore SIGALRM below.
2220 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002221 static RETSIGTYPE
2222sig_handler SIGDEFARG(sigarg)
2223{
2224 /* do nothing */
2225 SIGRETURN;
2226}
2227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002230 * Does the actual free'ing for the cs ptr with an optional flag of whether
2231 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 */
2233 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002234cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /*
2237 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2238 */
2239 if (csinfo[i].to_fp != NULL)
2240 {
2241 (void)fputs("q\n", csinfo[i].to_fp);
2242 (void)fflush(csinfo[i].to_fp);
2243 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002244#if defined(UNIX)
2245 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002246 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002247 int pstat;
2248 pid_t pid;
2249
2250# if defined(HAVE_SIGACTION)
2251 struct sigaction sa, old;
2252
Bram Moolenaar9701da02008-03-16 12:09:58 +00002253 /* Use sigaction() to limit the waiting time to two seconds. */
2254 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002256# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002257 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002258# else
2259 sa.sa_flags = 0;
2260# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002261 sigaction(SIGALRM, &sa, &old);
2262 alarm(2); /* 2 sec timeout */
2263
2264 /* Block until cscope exits or until timer expires */
2265 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002266 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002267
2268 /* cancel pending alarm if still there and restore signal */
2269 alarm(0);
2270 sigaction(SIGALRM, &old, NULL);
2271# else
2272 int waited;
2273
2274 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2275 * to give cscope a chance to exit quickly. */
2276 sleep(0);
2277 for (waited = 0; waited < 40; ++waited)
2278 {
2279 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002280 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002281 if (pid != 0)
2282 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002283 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002284 }
2285# endif
2286 /*
2287 * If the cscope process is still running: kill it.
2288 * Safety check: If the PID would be zero here, the entire X session
2289 * would be killed. -1 and 1 are dangerous as well.
2290 */
2291 if (pid < 0 && csinfo[i].pid > 1)
2292 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002293# ifdef ECHILD
2294 int alive = TRUE;
2295
2296 if (waitpid_errno == ECHILD)
2297 {
2298 /*
2299 * When using 'vim -g', vim is forked and cscope process is
2300 * no longer a child process but a sibling. So waitpid()
2301 * fails with errno being ECHILD (No child processes).
2302 * Don't send SIGKILL to cscope immediately but wait
2303 * (polling) for it to exit normally as result of sending
2304 * the "q" command, hence giving it a chance to clean up
2305 * its temporary files.
2306 */
2307 int waited;
2308
2309 sleep(0);
2310 for (waited = 0; waited < 40; ++waited)
2311 {
2312 /* Check whether cscope process is still alive */
2313 if (kill(csinfo[i].pid, 0) != 0)
2314 {
2315 alive = FALSE; /* cscope process no longer exists */
2316 break;
2317 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002318 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002319 }
2320 }
2321 if (alive)
2322# endif
2323 {
2324 kill(csinfo[i].pid, SIGKILL);
2325 (void)waitpid(csinfo[i].pid, &pstat, 0);
2326 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002327 }
2328 }
2329#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002330 if (csinfo[i].hProc != NULL)
2331 {
2332 /* Give cscope a chance to exit normally */
2333 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2334 TerminateProcess(csinfo[i].hProc, 0);
2335 CloseHandle(csinfo[i].hProc);
2336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#endif
2338
2339 if (csinfo[i].fr_fp != NULL)
2340 (void)fclose(csinfo[i].fr_fp);
2341 if (csinfo[i].to_fp != NULL)
2342 (void)fclose(csinfo[i].to_fp);
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (freefnpp)
2345 {
2346 vim_free(csinfo[i].fname);
2347 vim_free(csinfo[i].ppath);
2348 vim_free(csinfo[i].flags);
2349 }
2350
2351 clear_csinfo(i);
2352} /* cs_release_csp */
2353
2354
2355/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002356 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002359cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2362 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002363 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002365 if (csinfo_size == 0)
2366 return CSCOPE_SUCCESS;
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002369 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2370 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2371 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (dblist == NULL || pplist == NULL || fllist == NULL)
2373 {
2374 vim_free(dblist);
2375 vim_free(pplist);
2376 vim_free(fllist);
2377 return CSCOPE_FAILURE;
2378 }
2379
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002380 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 dblist[i] = csinfo[i].fname;
2383 pplist[i] = csinfo[i].ppath;
2384 fllist[i] = csinfo[i].flags;
2385 if (csinfo[i].fname != NULL)
2386 cs_release_csp(i, FALSE);
2387 }
2388
2389 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002390 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 if (dblist[i] != NULL)
2393 {
2394 cs_add_common(dblist[i], pplist[i], fllist[i]);
2395 if (p_csverbose)
2396 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002397 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * connection number in the same line as
2399 * "Added cscope database..."
2400 */
2401 sprintf(buf, " (#%d)", i);
2402 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2403 }
2404 }
2405 vim_free(dblist[i]);
2406 vim_free(pplist[i]);
2407 vim_free(fllist[i]);
2408 }
2409 vim_free(dblist);
2410 vim_free(pplist);
2411 vim_free(fllist);
2412
2413 if (p_csverbose)
2414 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2415 return CSCOPE_SUCCESS;
2416} /* cs_reset */
2417
2418
2419/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002420 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * (Prepends ppath, if there is one and if it's not already prepended,
2422 * otherwise just uses the name found.)
2423 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002424 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002426 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 */
2428 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002429cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002431 char *fullname;
2432 int len;
2433 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002436 * Ppath is freed when we destroy the cscope connection.
2437 * Fullname is freed after cs_make_vim_style_matches, after it's been
2438 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002440 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002442 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002443 else if (p_csre && csinfo[i].fname != NULL)
2444 {
2445 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2446 * path in path resolution. */
2447 csdir = alloc(MAXPATHL);
2448 if (csdir != NULL)
2449 {
2450 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaar28c21912013-05-29 19:18:00 +02002451 gettail((char_u *)csinfo[i].fname)
2452 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002453 len += (int)STRLEN(csdir);
2454 }
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002457 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002459 * happens, you are screwed up and need to fix how you're using cscope. */
2460 if (csinfo[i].ppath != NULL
2461 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2462 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002464 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002466 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002467 {
2468 if ((fullname = (char *)alloc(len)) != NULL)
2469 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2470 }
2471 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002472 {
2473 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002474 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002475 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002478 {
2479 fullname = (char *)vim_strsave((char_u *)name);
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002482 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486
2487/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002488 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002491cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 short i;
2494 if (cs_cnt_connections() == 0)
2495 MSG_PUTS(_("no cscope connections\n"));
2496 else
2497 {
2498 MSG_PUTS_ATTR(
2499 _(" # pid database name prepend path\n"),
2500 hl_attr(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002501 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 if (csinfo[i].fname == NULL)
2504 continue;
2505
2506 if (csinfo[i].ppath != NULL)
2507 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2508 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2509 else
2510 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2511 i, (long)csinfo[i].pid, csinfo[i].fname);
2512 }
2513 }
2514
2515 wait_return(TRUE);
2516 return CSCOPE_SUCCESS;
2517} /* cs_show */
2518
Bram Moolenaar02b06312007-09-06 15:39:22 +00002519
2520/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002521 * Only called when VIM exits to quit any cscope sessions.
2522 */
2523 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002524cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002525{
2526 int i;
2527
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002528 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002529 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002530 vim_free(csinfo);
2531 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#endif /* FEAT_CSCOPE */
2535
2536/* the end */