blob: 9466c34f9d015cfac54623fb69b4c3a3dd117f9b [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if (make_split)
221 {
222 if (!cmdp->cansplit)
223 {
224 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
225 return;
226 }
227 postponed_split = -1;
228 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000229 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232 cmdp->func(eap);
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000235 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236}
237
238/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100239 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
241 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100242ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
244 do_cscope_general(eap, FALSE);
245}
246
247/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100248 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100251ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 do_cscope_general(eap, TRUE);
254}
255
256/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100257 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 */
259 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100260ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261{
262 int ret = FALSE;
263
Bram Moolenaar446cb832008-06-24 21:56:24 +0000264 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 (void)EMSG(_("E562: Usage: cstag <ident>"));
267 return;
268 }
269
270 switch (p_csto)
271 {
272 case 0 :
273 if (cs_check_for_connections())
274 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000275 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200276 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (ret == FALSE)
278 {
279 cs_free_tags();
280 if (msg_col)
281 msg_putchar('\n');
282
283 if (cs_check_for_tags())
284 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
285 }
286 }
287 else if (cs_check_for_tags())
288 {
289 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
290 }
291 break;
292 case 1 :
293 if (cs_check_for_tags())
294 {
295 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
296 if (ret == FALSE)
297 {
298 if (msg_col)
299 msg_putchar('\n');
300
301 if (cs_check_for_connections())
302 {
303 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200304 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (ret == FALSE)
306 cs_free_tags();
307 }
308 }
309 }
310 else if (cs_check_for_connections())
311 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000312 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200313 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (ret == FALSE)
315 cs_free_tags();
316 }
317 break;
318 default :
319 break;
320 }
321
322 if (!ret)
323 {
324 (void)EMSG(_("E257: cstag: tag not found"));
Bram Moolenaar4033c552017-09-16 20:54:51 +0200325#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 g_do_tagpreview = 0;
327#endif
328 }
329
Bram Moolenaard4db7712016-11-12 19:16:46 +0100330}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332
333/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100334 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 * from the cscope output. should only be called from find_tags()
336 *
337 * returns TRUE if eof, FALSE otherwise
338 */
339 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100340cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
342 char *p;
343
344 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
345 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000346 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
348 return FALSE;
349} /* cs_fgets */
350
351
352/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100353 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 */
355 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100356cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
358 cs_manage_matches(NULL, NULL, -1, Free);
359}
360
361
362/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100363 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 */
365 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100366cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367{
368 cs_manage_matches(NULL, NULL, -1, Print);
369}
370
371
372/*
373 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
374 *
375 * Checks for the existence of a |cscope| connection. If no
376 * parameters are specified, then the function returns:
377 *
378 * 0, if cscope was not available (not compiled in), or if there
379 * are no cscope connections; or
380 * 1, if there is at least one cscope connection.
381 *
382 * If parameters are specified, then the value of {num}
383 * determines how existence of a cscope connection is checked:
384 *
385 * {num} Description of existence check
386 * ----- ------------------------------
387 * 0 Same as no parameters (e.g., "cscope_connection()").
388 * 1 Ignore {prepend}, and use partial string matches for
389 * {dbpath}.
390 * 2 Ignore {prepend}, and use exact string matches for
391 * {dbpath}.
392 * 3 Use {prepend}, use partial string matches for both
393 * {dbpath} and {prepend}.
394 * 4 Use {prepend}, use exact string matches for both
395 * {dbpath} and {prepend}.
396 *
397 * Note: All string comparisons are case sensitive!
398 */
399#if defined(FEAT_EVAL) || defined(PROTO)
400 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100401cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
403 int i;
404
405 if (num < 0 || num > 4 || (num > 0 && !dbpath))
406 return FALSE;
407
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000408 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {
410 if (!csinfo[i].fname)
411 continue;
412
413 if (num == 0)
414 return TRUE;
415
416 switch (num)
417 {
418 case 1:
419 if (strstr(csinfo[i].fname, (char *)dbpath))
420 return TRUE;
421 break;
422 case 2:
423 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
424 return TRUE;
425 break;
426 case 3:
427 if (strstr(csinfo[i].fname, (char *)dbpath)
428 && ((!ppath && !csinfo[i].ppath)
429 || (ppath
430 && csinfo[i].ppath
431 && strstr(csinfo[i].ppath, (char *)ppath))))
432 return TRUE;
433 break;
434 case 4:
435 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
436 && ((!ppath && !csinfo[i].ppath)
437 || (ppath
438 && csinfo[i].ppath
439 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
440 return TRUE;
441 break;
442 }
443 }
444
445 return FALSE;
446} /* cs_connection */
447#endif
448
449
450/*
451 * PRIVATE functions
452 ****************************************************************************/
453
454/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100455 * Add cscope database or a directory name (to look for cscope.out)
456 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100459cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 char *fname, *ppath, *flags = NULL;
462
463 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
464 {
465 cs_usage_msg(Add);
466 return CSCOPE_FAILURE;
467 }
468 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
469 flags = strtok((char *)NULL, (const char *)" ");
470
471 return cs_add_common(fname, ppath, flags);
472}
473
474 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100475cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 char *stat_emsg = _("E563: stat(%s) error: %d");
478 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
479
480 if (buf != NULL)
481 {
482 (void)sprintf(buf, stat_emsg, fname, errno);
483 (void)EMSG(buf);
484 vim_free(buf);
485 }
486 else
487 (void)EMSG(_("E563: stat error"));
488}
489
490
491/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100492 * The common routine to add a new cscope connection. Called by
493 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * routine uses a number of goto statements.
495 */
496 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100497cs_add_common(
498 char *arg1, /* filename - may contain environment variables */
499 char *arg2, /* prepend path - may contain environment variables */
500 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200502 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000503 int ret;
504 char *fname = NULL;
505 char *fname2 = NULL;
506 char *ppath = NULL;
507 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200508#ifdef FEAT_MODIFY_FNAME
509 int len;
510 int usedlen = 0;
511 char_u *fbuf = NULL;
512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000515 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 goto add_err;
517
518 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200519#ifdef FEAT_MODIFY_FNAME
520 len = (int)STRLEN(fname);
521 fbuf = (char_u *)fname;
522 (void)modify_fname((char_u *)":p", &usedlen,
523 (char_u **)&fname, &fbuf, &len);
524 if (fname == NULL)
525 goto add_err;
526 fname = (char *)vim_strnsave((char_u *)fname, len);
527 vim_free(fbuf);
528#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200529 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (ret < 0)
531 {
532staterr:
533 if (p_csverbose)
534 cs_stat_emsg(fname);
535 goto add_err;
536 }
537
538 /* get the prepend path (arg2), expand it, and try to stat it */
539 if (arg2 != NULL)
540 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200541 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000543 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 goto add_err;
545
546 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200547 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 if (ret < 0)
549 goto staterr;
550 }
551
552 /* if filename is a directory, append the cscope database name to it */
553 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
554 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000555 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (fname2 == NULL)
557 goto add_err;
558
559 while (fname[strlen(fname)-1] == '/'
560#ifdef WIN32
561 || fname[strlen(fname)-1] == '\\'
562#endif
563 )
564 {
565 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200566 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 break;
568 }
569 if (fname[0] == '\0')
570 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
571 else
572 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
573
Bram Moolenaar8767f522016-07-01 17:17:39 +0200574 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (ret < 0)
576 {
577 if (p_csverbose)
578 cs_stat_emsg(fname2);
579 goto add_err;
580 }
581
582 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
583 }
584#if defined(UNIX)
585 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
586#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000587 /* WIN32 - substitute define S_ISREG from os_unix.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
589#endif
590 {
591 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
592 }
593 else
594 {
595 if (p_csverbose)
596 (void)EMSG2(
597 _("E564: %s is not a directory or a valid cscope database"),
598 fname);
599 goto add_err;
600 }
601
602 if (i != -1)
603 {
604 if (cs_create_connection(i) == CSCOPE_FAILURE
605 || cs_read_prompt(i) == CSCOPE_FAILURE)
606 {
607 cs_release_csp(i, TRUE);
608 goto add_err;
609 }
610
611 if (p_csverbose)
612 {
613 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100614 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 (char_u *)_("Added cscope database %s"),
616 csinfo[i].fname);
617 }
618 }
619
620 vim_free(fname);
621 vim_free(fname2);
622 vim_free(ppath);
623 return CSCOPE_SUCCESS;
624
625add_err:
626 vim_free(fname2);
627 vim_free(fname);
628 vim_free(ppath);
629 return CSCOPE_FAILURE;
630} /* cs_add_common */
631
632
633 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100634cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 return (cs_cnt_connections() > 0);
637} /* cs_check_for_connections */
638
639
640 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100641cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000643 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644} /* cs_check_for_tags */
645
646
647/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100648 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 */
650 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100651cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 short i;
654 short cnt = 0;
655
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000656 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 if (csinfo[i].fname != NULL)
659 cnt++;
660 }
661 return cnt;
662} /* cs_cnt_connections */
663
664 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100665cs_reading_emsg(
666 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 EMSGN(_("E262: error reading cscope connection %ld"), idx);
669}
670
671#define CSREAD_BUFSIZE 2048
672/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100673 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 */
675 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100676cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 char *stok;
679 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100680 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 buf = (char *)alloc(CSREAD_BUFSIZE);
683 if (buf == NULL)
684 return 0;
685 for (;;)
686 {
687 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
688 {
689 if (feof(csinfo[idx].fr_fp))
690 errno = EIO;
691
692 cs_reading_emsg(idx);
693
694 vim_free(buf);
695 return -1;
696 }
697
698 /*
699 * If the database is out of date, or there's some other problem,
700 * cscope will output error messages before the number-of-lines output.
701 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000702 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100703 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 */
Bram Moolenaar1274d332018-01-30 21:47:52 +0100705 if (strstr((const char *)stok, "Unable to search database") != NULL)
706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if ((stok = strtok(buf, (const char *)" ")) == NULL)
708 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000709 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 continue;
711
712 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
713 continue;
714 nlines = atoi(stok);
715 if (nlines < 0)
716 {
717 nlines = 0;
718 break;
719 }
720
721 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
722 continue;
723 if (strncmp((const char *)stok, "lines", 5))
724 continue;
725
726 break;
727 }
728
729 vim_free(buf);
730 return nlines;
731} /* cs_cnt_matches */
732
733
734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 * Creates the actual cscope command query from what the user entered.
736 */
737 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100738cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 char *cmd;
741 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000742 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 switch (csoption[0])
745 {
746 case '0' : case 's' :
747 search = 0;
748 break;
749 case '1' : case 'g' :
750 search = 1;
751 break;
752 case '2' : case 'd' :
753 search = 2;
754 break;
755 case '3' : case 'c' :
756 search = 3;
757 break;
758 case '4' : case 't' :
759 search = 4;
760 break;
761 case '6' : case 'e' :
762 search = 6;
763 break;
764 case '7' : case 'f' :
765 search = 7;
766 break;
767 case '8' : case 'i' :
768 search = 8;
769 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200770 case '9' : case 'a' :
771 search = 9;
772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 default :
774 (void)EMSG(_("E561: unknown cscope search type"));
775 cs_usage_msg(Find);
776 return NULL;
777 }
778
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000779 /* Skip white space before the patter, except for text and pattern search,
780 * they may want to use the leading white space. */
781 pat = pattern;
782 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100783 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000784 ++pat;
785
786 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 return NULL;
788
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000789 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 return cmd;
792} /* cs_create_cmd */
793
794
795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 * This piece of code was taken/adapted from nvi. do we need to add
797 * the BSD license notice?
798 */
799 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100800cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000802#ifdef UNIX
803 int to_cs[2], from_cs[2];
804#endif
805 int len;
806 char *prog, *cmd, *ppath = NULL;
807#ifdef WIN32
808 int fd;
809 SECURITY_ATTRIBUTES sa;
810 PROCESS_INFORMATION pi;
811 STARTUPINFO si;
812 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
813 HANDLE stdin_rd, stdout_rd;
814 HANDLE stdout_wr, stdin_wr;
815 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200816# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
817# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000818# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200819# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822
Bram Moolenaar02b06312007-09-06 15:39:22 +0000823#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 /*
825 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
826 * from_cs[0] and writes to to_cs[1].
827 */
828 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
829 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
830 {
831 (void)EMSG(_("E566: Could not create cscope pipes"));
832err_closing:
833 if (to_cs[0] != -1)
834 (void)close(to_cs[0]);
835 if (to_cs[1] != -1)
836 (void)close(to_cs[1]);
837 if (from_cs[0] != -1)
838 (void)close(from_cs[0]);
839 if (from_cs[1] != -1)
840 (void)close(from_cs[1]);
841 return CSCOPE_FAILURE;
842 }
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 switch (csinfo[i].pid = fork())
845 {
846 case -1:
847 (void)EMSG(_("E622: Could not fork for cscope"));
848 goto err_closing;
849 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (dup2(to_cs[0], STDIN_FILENO) == -1)
851 PERROR("cs_create_connection 1");
852 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
853 PERROR("cs_create_connection 2");
854 if (dup2(from_cs[1], STDERR_FILENO) == -1)
855 PERROR("cs_create_connection 3");
856
857 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 (void)close(to_cs[1]);
859 (void)close(from_cs[0]);
860#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000861 /* WIN32 */
862 /* Create pipes to communicate with cscope */
863 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
864 sa.bInheritHandle = TRUE;
865 sa.lpSecurityDescriptor = NULL;
866
867 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
868 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
869 {
870 (void)EMSG(_("E566: Could not create cscope pipes"));
871err_closing:
872 if (pipe_stdin)
873 {
874 CloseHandle(stdin_rd);
875 CloseHandle(stdin_wr);
876 }
877 if (pipe_stdout)
878 {
879 CloseHandle(stdout_rd);
880 CloseHandle(stdout_wr);
881 }
882 return CSCOPE_FAILURE;
883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
885 /* expand the cscope exec for env var's */
886 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
887 {
888#ifdef UNIX
889 return CSCOPE_FAILURE;
890#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000891 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 goto err_closing;
893#endif
894 }
895 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
896
897 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000898 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (csinfo[i].ppath)
900 {
901 /* expand the prepend path for env var's */
902 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
903 {
904 vim_free(prog);
905#ifdef UNIX
906 return CSCOPE_FAILURE;
907#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000908 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 goto err_closing;
910#endif
911 }
912 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
913
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000914 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
917 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000918 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
920 if ((cmd = (char *)alloc(len)) == NULL)
921 {
922 vim_free(prog);
923 vim_free(ppath);
924#ifdef UNIX
925 return CSCOPE_FAILURE;
926#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000927 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 goto err_closing;
929#endif
930 }
931
932 /* run the cscope command; is there execl for non-unix systems? */
933#if defined(UNIX)
934 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
935#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000936 /* WIN32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
938#endif
939 if (csinfo[i].ppath != NULL)
940 {
941 (void)strcat(cmd, " -P");
942 (void)strcat(cmd, csinfo[i].ppath);
943 }
944 if (csinfo[i].flags != NULL)
945 {
946 (void)strcat(cmd, " ");
947 (void)strcat(cmd, csinfo[i].flags);
948 }
949# ifdef UNIX
950 /* on Win32 we still need prog */
951 vim_free(prog);
952# endif
953 vim_free(ppath);
954
955#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200956# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
957 /* Change our process group to avoid cscope receiving SIGWINCH. */
958# if defined(HAVE_SETSID)
959 (void)setsid();
960# else
961 if (setpgid(0, 0) == -1)
962 PERROR(_("cs_create_connection setpgid failed"));
963# endif
964# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000965 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 PERROR(_("cs_create_connection exec failed"));
967
968 exit(127);
969 /* NOTREACHED */
970 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /*
972 * Save the file descriptors for later duplication, and
973 * reopen as streams.
974 */
975 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
976 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
977 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
978 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /* close unused */
981 (void)close(to_cs[0]);
982 (void)close(from_cs[1]);
983
984 break;
985 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000988 /* WIN32 */
989 /* Create a new process to run cscope and use pipes to talk with it */
990 GetStartupInfo(&si);
991 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
992 si.wShowWindow = SW_HIDE; /* Hide child application window */
993 si.hStdOutput = stdout_wr;
994 si.hStdError = stdout_wr;
995 si.hStdInput = stdin_rd;
996 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
997 NULL, NULL, &si, &pi);
998 vim_free(prog);
999 vim_free(cmd);
1000
1001 if (!created)
1002 {
1003 PERROR(_("cs_create_connection exec failed"));
1004 (void)EMSG(_("E623: Could not spawn cscope process"));
1005 goto err_closing;
1006 }
1007 /* else */
1008 csinfo[i].pid = pi.dwProcessId;
1009 csinfo[i].hProc = pi.hProcess;
1010 CloseHandle(pi.hThread);
1011
1012 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001013 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1014 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001015 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1016 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001017 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1018 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001019 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1020 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1021
1022 /* Close handles for file descriptors inherited by the cscope process */
1023 CloseHandle(stdin_rd);
1024 CloseHandle(stdout_wr);
1025
1026#endif /* !UNIX */
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 return CSCOPE_SUCCESS;
1029} /* cs_create_connection */
1030
1031
1032/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001033 * Query cscope using command line interface. Parse the output and use tselect
1034 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 *
1036 * returns TRUE if we jump to a tag or abort, FALSE if not.
1037 */
1038 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001039cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001042 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044 if (cs_check_for_connections() == FALSE)
1045 {
1046 (void)EMSG(_("E567: no cscope connections"));
1047 return FALSE;
1048 }
1049
1050 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1051 {
1052 cs_usage_msg(Find);
1053 return FALSE;
1054 }
1055
1056 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001057 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059 cs_usage_msg(Find);
1060 return FALSE;
1061 }
1062
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001063 /*
1064 * Let's replace the NULs written by strtok() with spaces - we need the
1065 * spaces to correctly display the quickfix/location list window's title.
1066 */
1067 for (i = 0; i < eap_arg_len; ++i)
1068 if (NUL == eap->arg[i])
1069 eap->arg[i] = ' ';
1070
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001071 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001072 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073} /* cs_find */
1074
1075
1076/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001077 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
1079 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001080cs_find_common(
1081 char *opt,
1082 char *pat,
1083 int forceit,
1084 int verbose,
1085 int use_ll UNUSED,
1086 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 int i;
1089 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001090 int *nummatches;
1091 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#ifdef FEAT_QUICKFIX
1093 char cmdletter;
1094 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001095
1096 /* get cmd letter */
1097 switch (opt[0])
1098 {
1099 case '0' :
1100 cmdletter = 's';
1101 break;
1102 case '1' :
1103 cmdletter = 'g';
1104 break;
1105 case '2' :
1106 cmdletter = 'd';
1107 break;
1108 case '3' :
1109 cmdletter = 'c';
1110 break;
1111 case '4' :
1112 cmdletter = 't';
1113 break;
1114 case '6' :
1115 cmdletter = 'e';
1116 break;
1117 case '7' :
1118 cmdletter = 'f';
1119 break;
1120 case '8' :
1121 cmdletter = 'i';
1122 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001123 case '9' :
1124 cmdletter = 'a';
1125 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001126 default :
1127 cmdletter = opt[0];
1128 }
1129
1130 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1131 if (qfpos != NULL)
1132 {
1133 qfpos++;
1134 /* next symbol must be + or - */
1135 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1136 {
1137 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1138 char *buf = (char *)alloc((unsigned)strlen(nf));
1139
1140 /* strlen will be enough because we use chars */
1141 if (buf != NULL)
1142 {
1143 sprintf(buf, nf, *qfpos, *(qfpos-1));
1144 (void)EMSG(buf);
1145 vim_free(buf);
1146 }
1147 return FALSE;
1148 }
1149
1150# ifdef FEAT_AUTOCMD
Bram Moolenaar21662be2016-11-06 14:46:44 +01001151 if (*qfpos != '0'
1152 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1153 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001154 {
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001155# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001156 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001157 return FALSE;
1158# endif
1159 }
1160# endif
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163
1164 /* create the actual command to send to cscope */
1165 cmd = cs_create_cmd(opt, pat);
1166 if (cmd == NULL)
1167 return FALSE;
1168
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001169 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1170 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001171 {
1172 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001173 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001174 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001175
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001176 /* Send query to all open connections, then count the total number
1177 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001178 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 nummatches[i] = 0;
1180 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001181 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001183 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 continue;
1185
1186 /* send cmd to cscope */
1187 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1188 (void)fflush(csinfo[i].to_fp);
1189
1190 nummatches[i] = cs_cnt_matches(i);
1191
1192 if (nummatches[i] > -1)
1193 totmatches += nummatches[i];
1194
1195 if (nummatches[i] == 0)
1196 (void)cs_read_prompt(i);
1197 }
1198 vim_free(cmd);
1199
1200 if (totmatches == 0)
1201 {
1202 char *nf = _("E259: no matches found for cscope query %s of %s");
1203 char *buf;
1204
1205 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001206 {
1207 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001211 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (buf == NULL)
1213 (void)EMSG(nf);
1214 else
1215 {
1216 sprintf(buf, nf, opt, pat);
1217 (void)EMSG(buf);
1218 vim_free(buf);
1219 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001220 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 return FALSE;
1222 }
1223
1224#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
1227 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001228 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001229 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001230 qf_info_T *qi = NULL;
1231 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001233 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001234 if (f == NULL)
1235 EMSG2(_(e_notopen), tmp);
1236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 cs_file_results(f, nummatches);
1239 fclose(f);
1240 if (use_ll) /* Use location list */
1241 wp = curwin;
1242 /* '-' starts a new error list */
1243 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001244 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001246 if (postponed_split != 0)
1247 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001248 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001250 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001251 postponed_split = 0;
1252 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001253
1254# ifdef FEAT_AUTOCMD
1255 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1256 curbuf->b_fname, TRUE, curbuf);
1257# endif
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001258 if (use_ll)
1259 /*
1260 * In the location list window, use the displayed location
1261 * list. Otherwise, use the location list for the window.
1262 */
1263 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1264 ? wp->w_llist_ref : wp->w_llist;
1265 qf_jump(qi, 0, 0, forceit);
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 mch_remove(tmp);
1269 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001270 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return TRUE;
1272 }
1273 else
1274#endif /* FEAT_QUICKFIX */
1275 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001276 char **matches = NULL, **contexts = NULL;
1277 int matched = 0;
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /* read output */
1280 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1281 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001282 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (matches == NULL)
1284 return FALSE;
1285
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001286 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1289 }
1290
1291} /* cs_find_common */
1292
1293/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001294 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001297cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 cscmd_T *cmdp = cs_cmds;
1300
1301 (void)MSG_PUTS(_("cscope commands:\n"));
1302 while (cmdp->name != NULL)
1303 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001304 char *help = _(cmdp->help);
1305 int space_cnt = 30 - vim_strsize((char_u *)help);
1306
1307 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1308 if (space_cnt < 0)
1309 space_cnt = 0;
1310 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1311 cmdp->name,
1312 help, space_cnt, " ",
1313 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001315 MSG_PUTS(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001316 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001324 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 cmdp++;
1327 }
1328
1329 wait_return(TRUE);
1330 return 0;
1331} /* cs_help */
1332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001335clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 csinfo[i].fname = NULL;
1338 csinfo[i].ppath = NULL;
1339 csinfo[i].flags = NULL;
1340#if defined(UNIX)
1341 csinfo[i].st_dev = (dev_t)0;
1342 csinfo[i].st_ino = (ino_t)0;
1343#else
1344 csinfo[i].nVolume = 0;
1345 csinfo[i].nIndexHigh = 0;
1346 csinfo[i].nIndexLow = 0;
1347#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001348 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 csinfo[i].fr_fp = NULL;
1350 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001351#if defined(WIN32)
1352 csinfo[i].hProc = NULL;
1353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
1356#ifndef UNIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001357static char *GetWin32Error(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001360GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 char *msg = NULL;
1363 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1364 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1365 if (msg != NULL)
1366 {
1367 /* remove trailing \r\n */
1368 char *pcrlf = strstr(msg, "\r\n");
1369 if (pcrlf != NULL)
1370 *pcrlf = '\0';
1371 }
1372 return msg;
1373}
1374#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001377 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001380cs_insert_filelist(
1381 char *fname,
1382 char *ppath,
1383 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001384 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 short i, j;
1387#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 BY_HANDLE_FILE_INFORMATION bhfi;
1389
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001390 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001392 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1393 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (p_csverbose)
1395 {
1396 char *cant_msg = _("E625: cannot open cscope database: %s");
1397 char *winmsg = GetWin32Error();
1398
1399 if (winmsg != NULL)
1400 {
1401 (void)EMSG2(cant_msg, winmsg);
1402 LocalFree(winmsg);
1403 }
1404 else
1405 /* subst filename if can't get error text */
1406 (void)EMSG2(cant_msg, fname);
1407 }
1408 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001409
1410 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (p_csverbose)
1412 (void)EMSG(_("E626: cannot get cscope database information"));
1413 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415#endif
1416
1417 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001418 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
1420 if (csinfo[j].fname != NULL
1421#if defined(UNIX)
1422 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1423#else
1424 /* compare pathnames first */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001425 && ((fullpathcmp((char_u *)csinfo[j].fname,
1426 (char_u *)fname, FALSE) & FPC_SAME)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001427 /* test index file attributes too */
1428 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1430 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1431#endif
1432 )
1433 {
1434 if (p_csverbose)
1435 (void)EMSG(_("E568: duplicate cscope database not added"));
1436 return -1;
1437 }
1438
1439 if (csinfo[j].fname == NULL && i == -1)
1440 i = j; /* remember first empty entry */
1441 }
1442
1443 if (i == -1)
1444 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001445 i = csinfo_size;
1446 if (csinfo_size == 0)
1447 {
1448 /* First time allocation: allocate only 1 connection. It should
1449 * be enough for most users. If more is needed, csinfo will be
1450 * reallocated. */
1451 csinfo_size = 1;
1452 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1453 }
1454 else
1455 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001456 csinfo_T *t_csinfo = csinfo;
1457
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001458 /* Reallocate space for more connections. */
1459 csinfo_size *= 2;
1460 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001461 if (csinfo == NULL)
1462 {
1463 vim_free(t_csinfo);
1464 csinfo_size = 0;
1465 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001466 }
1467 if (csinfo == NULL)
1468 return -1;
1469 for (j = csinfo_size/2; j < csinfo_size; j++)
1470 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001473 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return -1;
1475
1476 (void)strcpy(csinfo[i].fname, (const char *)fname);
1477
1478 if (ppath != NULL)
1479 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001480 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 vim_free(csinfo[i].fname);
1483 csinfo[i].fname = NULL;
1484 return -1;
1485 }
1486 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1487 } else
1488 csinfo[i].ppath = NULL;
1489
1490 if (flags != NULL)
1491 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001492 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 vim_free(csinfo[i].fname);
1495 vim_free(csinfo[i].ppath);
1496 csinfo[i].fname = NULL;
1497 csinfo[i].ppath = NULL;
1498 return -1;
1499 }
1500 (void)strcpy(csinfo[i].flags, (const char *)flags);
1501 } else
1502 csinfo[i].flags = NULL;
1503
1504#if defined(UNIX)
1505 csinfo[i].st_dev = sb->st_dev;
1506 csinfo[i].st_ino = sb->st_ino;
1507
1508#else
1509 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1510 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1511 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1512#endif
1513 return i;
1514} /* cs_insert_filelist */
1515
1516
1517/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001518 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
1520 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001521cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 cscmd_T *cmdp;
1524 char *stok;
1525 size_t len;
1526
1527 if (eap->arg == NULL)
1528 return NULL;
1529
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001530 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001531 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1534 return NULL;
1535
1536 len = strlen(stok);
1537 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1538 {
1539 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1540 return (cmdp);
1541 }
1542 return NULL;
1543} /* cs_lookup_cmd */
1544
1545
1546/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001547 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001550cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 char *stok;
1553 short i;
1554
1555 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1556 {
1557 cs_usage_msg(Kill);
1558 return CSCOPE_FAILURE;
1559 }
1560
1561 /* only single digit positive and negative integers are allowed */
1562 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1563 || (strlen(stok) < 3 && stok[0] == '-'
1564 && VIM_ISDIGIT((int)(stok[1]))))
1565 i = atoi(stok);
1566 else
1567 {
1568 /* It must be part of a name. We will try to find a match
1569 * within all the names in the csinfo data structure
1570 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001571 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1574 break;
1575 }
1576 }
1577
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001578 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 if (p_csverbose)
1581 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1582 }
1583 else
1584 {
1585 if (i == -1)
1586 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001587 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 if (csinfo[i].fname)
1590 cs_kill_execute(i, csinfo[i].fname);
1591 }
1592 }
1593 else
1594 cs_kill_execute(i, stok);
1595 }
1596
1597 return 0;
1598} /* cs_kill */
1599
1600
1601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * Actually kills a specific cscope connection.
1603 */
1604 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001605cs_kill_execute(
1606 int i, /* cscope table index */
1607 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
1609 if (p_csverbose)
1610 {
1611 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001612 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 (char_u *)_("cscope connection %s closed"), cname);
1614 }
1615 cs_release_csp(i, TRUE);
1616}
1617
1618
1619/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001620 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1622 * the type of the tag you are looking for. for example, in Darren Hiebert's
1623 * ctags (the one that comes with vim), #define's use a line number to find the
1624 * tag in a file while function definitions use a regexp search pattern.
1625 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001626 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * quirky (and probably other things i don't know about):
1628 *
1629 * if you have "# define" in your source file, which is
1630 * perfectly legal, cscope thinks you have "#define". this
1631 * will result in a failed regexp search. :(
1632 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001633 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * would still have to be modified to escape all the special regular expression
1635 * characters to comply with ctags formatting.
1636 */
1637 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001638cs_make_vim_style_matches(
1639 char *fname,
1640 char *slno,
1641 char *search,
1642 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 /* vim style is ctags:
1645 *
1646 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1647 *
1648 * but as mentioned above, we'll always use the line number and
1649 * put the search pattern (if one exists) as "extra"
1650 *
1651 * buf is used as part of vim's method of handling tags, and
1652 * (i think) vim frees it when you pop your tags and get replaced
1653 * by new ones on the tag stack.
1654 */
1655 char *buf;
1656 int amt;
1657
1658 if (search != NULL)
1659 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001660 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if ((buf = (char *)alloc(amt)) == NULL)
1662 return NULL;
1663
1664 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1665 }
1666 else
1667 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001668 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if ((buf = (char *)alloc(amt)) == NULL)
1670 return NULL;
1671
1672 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1673 }
1674
1675 return buf;
1676} /* cs_make_vim_style_matches */
1677
1678
1679/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001680 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
1682 * Store: keep a ptr to the (malloc'd) memory of matches originally
1683 * generated from cs_find(). the matches are originally lines directly
1684 * from cscope output, but transformed to look like something out of a
1685 * ctags. see cs_make_vim_style_matches for more details.
1686 *
1687 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1688 * the next line from the cscope output. it basically keeps track of which
1689 * lines have been "used" and returns the next one.
1690 *
1691 * Free: frees up everything and resets
1692 *
1693 * Print: prints the tags
1694 */
1695 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001696cs_manage_matches(
1697 char **matches,
1698 char **contexts,
1699 int totmatches,
1700 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 static char **mp = NULL;
1703 static char **cp = NULL;
1704 static int cnt = -1;
1705 static int next = -1;
1706 char *p = NULL;
1707
1708 switch (cmd)
1709 {
1710 case Store:
1711 assert(matches != NULL);
1712 assert(totmatches > 0);
1713 if (mp != NULL || cp != NULL)
1714 (void)cs_manage_matches(NULL, NULL, -1, Free);
1715 mp = matches;
1716 cp = contexts;
1717 cnt = totmatches;
1718 next = 0;
1719 break;
1720 case Get:
1721 if (next >= cnt)
1722 return NULL;
1723
1724 p = mp[next];
1725 next++;
1726 break;
1727 case Free:
1728 if (mp != NULL)
1729 {
1730 if (cnt > 0)
1731 while (cnt--)
1732 {
1733 vim_free(mp[cnt]);
1734 if (cp != NULL)
1735 vim_free(cp[cnt]);
1736 }
1737 vim_free(mp);
1738 vim_free(cp);
1739 }
1740 mp = NULL;
1741 cp = NULL;
1742 cnt = 0;
1743 next = 0;
1744 break;
1745 case Print:
1746 cs_print_tags_priv(mp, cp, cnt);
1747 break;
1748 default: /* should not reach here */
Bram Moolenaar95f09602016-11-10 20:01:45 +01001749 IEMSG(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 return NULL;
1751 }
1752
1753 return p;
1754} /* cs_manage_matches */
1755
1756
1757/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001758 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 */
1760 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001761cs_parse_results(
1762 int cnumber,
1763 char *buf,
1764 int bufsize,
1765 char **context,
1766 char **linenumber,
1767 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 int ch;
1770 char *p;
1771 char *name;
1772
1773 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1774 {
1775 if (feof(csinfo[cnumber].fr_fp))
1776 errno = EIO;
1777
1778 cs_reading_emsg(cnumber);
1779
1780 return NULL;
1781 }
1782
1783 /* If the line's too long for the buffer, discard it. */
1784 if ((p = strchr(buf, '\n')) == NULL)
1785 {
1786 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1787 ;
1788 return NULL;
1789 }
1790 *p = '\0';
1791
1792 /*
1793 * cscope output is in the following format:
1794 *
1795 * <filename> <context> <line number> <pattern>
1796 */
1797 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1798 return NULL;
1799 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1800 return NULL;
1801 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1802 return NULL;
1803 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1804
1805 /* --- nvi ---
1806 * If the file is older than the cscope database, that is,
1807 * the database was built since the file was last modified,
1808 * or there wasn't a search string, use the line number.
1809 */
1810 if (strcmp(*search, "<unknown>") == 0)
1811 *search = NULL;
1812
1813 name = cs_resolve_file(cnumber, name);
1814 return name;
1815}
1816
Bram Moolenaarc716c302006-01-21 22:12:51 +00001817#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001819 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001822cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int i, j;
1825 char *buf;
1826 char *search, *slno;
1827 char *fullname;
1828 char *cntx;
1829 char *context;
1830
1831 buf = (char *)alloc(CSREAD_BUFSIZE);
1832 if (buf == NULL)
1833 return;
1834
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001835 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 if (nummatches_a[i] < 1)
1838 continue;
1839
1840 for (j = 0; j < nummatches_a[i]; j++)
1841 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001842 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1843 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 continue;
1845
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001846 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001847 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 continue;
1849
1850 if (strcmp(cntx, "<global>")==0)
1851 strcpy(context, "<<global>>");
1852 else
1853 sprintf(context, "<<%s>>", cntx);
1854
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001855 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1857 else
1858 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1859
1860 vim_free(context);
1861 vim_free(fullname);
1862 } /* for all matches */
1863
1864 (void)cs_read_prompt(i);
1865
1866 } /* for all cscope connections */
1867 vim_free(buf);
1868}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001872 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1873 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001874 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 */
1876 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001877cs_fill_results(
1878 char *tagstr,
1879 int totmatches,
1880 int *nummatches_a,
1881 char ***matches_p,
1882 char ***cntxts_p,
1883 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 int i, j;
1886 char *buf;
1887 char *search, *slno;
1888 int totsofar = 0;
1889 char **matches = NULL;
1890 char **cntxts = NULL;
1891 char *fullname;
1892 char *cntx;
1893
1894 assert(totmatches > 0);
1895
1896 buf = (char *)alloc(CSREAD_BUFSIZE);
1897 if (buf == NULL)
1898 return;
1899
1900 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1901 goto parse_out;
1902 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1903 goto parse_out;
1904
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001905 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 if (nummatches_a[i] < 1)
1908 continue;
1909
1910 for (j = 0; j < nummatches_a[i]; j++)
1911 {
1912 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1913 &slno, &search)) == NULL)
1914 continue;
1915
1916 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1917 search, tagstr);
1918
1919 vim_free(fullname);
1920
1921 if (strcmp(cntx, "<global>") == 0)
1922 cntxts[totsofar] = NULL;
1923 else
1924 /* note: if vim_strsave returns NULL, then the context
1925 * will be "<global>", which is misleading.
1926 */
1927 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1928
1929 if (matches[totsofar] != NULL)
1930 totsofar++;
1931
1932 } /* for all matches */
1933
1934 (void)cs_read_prompt(i);
1935
1936 } /* for all cscope connections */
1937
1938parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001939 if (totsofar == 0)
1940 {
1941 /* No matches, free the arrays and return NULL in "*matches_p". */
1942 vim_free(matches);
1943 matches = NULL;
1944 vim_free(cntxts);
1945 cntxts = NULL;
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 *matched = totsofar;
1948 *matches_p = matches;
1949 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 vim_free(buf);
1952} /* cs_fill_results */
1953
1954
1955/* get the requested path components */
1956 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001957cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 int i;
1960 char *s;
1961
1962 if (p_cspc == 0)
1963 return path;
1964
1965 s = path + strlen(path) - 1;
1966 for (i = 0; i < p_cspc; ++i)
1967 while (s > path && *--s != '/'
1968#ifdef WIN32
1969 && *--s != '\\'
1970#endif
1971 )
1972 ;
1973 if ((s > path && *s == '/')
1974#ifdef WIN32
1975 || (s > path && *s == '\\')
1976#endif
1977 )
1978 ++s;
1979 return s;
1980}
1981
1982/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001983 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 */
1985 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001986cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001989 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 int bufsize = 0; /* Track available bufsize */
1991 int newsize = 0;
1992 char *ptag;
1993 char *fname, *lno, *extra, *tbuf;
1994 int i, idx, num;
1995 char *globalcntx = "GLOBAL";
1996 char *cntxformat = " <<%s>>";
1997 char *context;
1998 char *cstag_msg = _("Cscope tag: %s");
1999 char *csfmt_str = "%4d %6s ";
2000
Bram Moolenaar4033c552017-09-16 20:54:51 +02002001 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return;
2005
2006 strcpy(tbuf, matches[0]);
2007 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02002008 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002009 {
2010 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02002011 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002014 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 buf = (char *)alloc(newsize);
2016 if (buf != NULL)
2017 {
2018 bufsize = newsize;
2019 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002020 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022
2023 vim_free(tbuf);
2024
Bram Moolenaar8820b482017-03-16 17:23:31 +01002025 MSG_PUTS_ATTR(_("\n # line"), HL_ATTR(HLF_T)); /* strlen is 7 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 msg_advance(msg_col + 2);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002027 MSG_PUTS_ATTR(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 num = 1;
2030 for (i = 0; i < num_matches; i++)
2031 {
2032 idx = i;
2033
2034 /* if we really wanted to, we could avoid this malloc and strcpy
2035 * by parsing matches[i] on the fly and placing stuff into buf
2036 * directly, but that's too much of a hassle
2037 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002038 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 continue;
2040 (void)strcpy(tbuf, matches[idx]);
2041
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002042 if (strtok(tbuf, (const char *)"\t") == NULL
2043 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2044 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2045 {
2046 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002048 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002049 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2052
2053 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002054 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (bufsize < newsize)
2056 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002057 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 buf = (char *)vim_realloc(buf, newsize);
2059 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002062 vim_free(t_buf);
2063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 else
2065 bufsize = newsize;
2066 }
2067 if (buf != NULL)
2068 {
2069 /* csfmt_str = "%4d %6s "; */
2070 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002071 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
Bram Moolenaar8820b482017-03-16 17:23:31 +01002073 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 /* compute the required space for the context */
2076 if (cntxts[idx] != NULL)
2077 context = cntxts[idx];
2078 else
2079 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002080 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 if (bufsize < newsize)
2083 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002084 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 buf = (char *)vim_realloc(buf, newsize);
2086 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002089 vim_free(t_buf);
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else
2092 bufsize = newsize;
2093 }
2094 if (buf != NULL)
2095 {
2096 (void)sprintf(buf, cntxformat, context);
2097
2098 /* print the context only if it fits on the same line */
2099 if (msg_col + (int)strlen(buf) >= (int)Columns)
2100 msg_putchar('\n');
2101 msg_advance(12);
2102 MSG_PUTS_LONG(buf);
2103 msg_putchar('\n');
2104 }
2105 if (extra != NULL)
2106 {
2107 msg_advance(13);
2108 MSG_PUTS_LONG(extra);
2109 }
2110
2111 vim_free(tbuf); /* only after printing extra due to strtok use */
2112
2113 if (msg_col)
2114 msg_putchar('\n');
2115
2116 ui_breakcheck();
2117 if (got_int)
2118 {
2119 got_int = FALSE; /* don't print any more matches */
2120 break;
2121 }
2122
2123 num++;
2124 } /* for all matches */
2125
2126 vim_free(buf);
2127} /* cs_print_tags_priv */
2128
2129
2130/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002131 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 */
2133 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002134cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 int ch;
2137 char *buf = NULL; /* buffer for possible error message from cscope */
2138 int bufpos = 0;
2139 char *cs_emsg;
2140 int maxlen;
2141 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002142 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 int n;
2144
2145 cs_emsg = _("E609: Cscope error: %s");
2146 /* compute maximum allowed len for Cscope error message */
2147 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2148
2149 for (;;)
2150 {
2151 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2152 /* if there is room and char is printable */
2153 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2154 {
2155 if (buf == NULL) /* lazy buffer allocation */
2156 buf = (char *)alloc(maxlen);
2157 if (buf != NULL)
2158 {
2159 /* append character to the message */
2160 buf[bufpos++] = ch;
2161 buf[bufpos] = NUL;
2162 if (bufpos >= epromptlen
2163 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2164 {
2165 /* remove eprompt from buf */
2166 buf[bufpos - epromptlen] = NUL;
2167
2168 /* print message to user */
2169 (void)EMSG2(cs_emsg, buf);
2170
2171 /* send RETURN to cscope */
2172 (void)putc('\n', csinfo[i].to_fp);
2173 (void)fflush(csinfo[i].to_fp);
2174
2175 /* clear buf */
2176 bufpos = 0;
2177 buf[bufpos] = NUL;
2178 }
2179 }
2180 }
2181
2182 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2183 {
2184 if (n > 0)
2185 ch = getc(csinfo[i].fr_fp);
2186 if (ch == EOF)
2187 {
2188 PERROR("cs_read_prompt EOF");
2189 if (buf != NULL && buf[0] != NUL)
2190 (void)EMSG2(cs_emsg, buf);
2191 else if (p_csverbose)
2192 cs_reading_emsg(i); /* don't have additional information */
2193 cs_release_csp(i, TRUE);
2194 vim_free(buf);
2195 return CSCOPE_FAILURE;
2196 }
2197
2198 if (ch != CSCOPE_PROMPT[n])
2199 {
2200 ch = EOF;
2201 break;
2202 }
2203 }
2204
2205 if (ch == EOF)
2206 continue; /* didn't find the prompt */
2207 break; /* did find the prompt */
2208 }
2209
2210 vim_free(buf);
2211 return CSCOPE_SUCCESS;
2212}
2213
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002214#if defined(UNIX) && defined(SIGALRM)
2215/*
2216 * Used to catch and ignore SIGALRM below.
2217 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002218 static RETSIGTYPE
2219sig_handler SIGDEFARG(sigarg)
2220{
2221 /* do nothing */
2222 SIGRETURN;
2223}
2224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002227 * Does the actual free'ing for the cs ptr with an optional flag of whether
2228 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 */
2230 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002231cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /*
2234 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2235 */
2236 if (csinfo[i].to_fp != NULL)
2237 {
2238 (void)fputs("q\n", csinfo[i].to_fp);
2239 (void)fflush(csinfo[i].to_fp);
2240 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002241#if defined(UNIX)
2242 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002243 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002244 int pstat;
2245 pid_t pid;
2246
2247# if defined(HAVE_SIGACTION)
2248 struct sigaction sa, old;
2249
Bram Moolenaar9701da02008-03-16 12:09:58 +00002250 /* Use sigaction() to limit the waiting time to two seconds. */
2251 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002252 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002253# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002254 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002255# else
2256 sa.sa_flags = 0;
2257# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002258 sigaction(SIGALRM, &sa, &old);
2259 alarm(2); /* 2 sec timeout */
2260
2261 /* Block until cscope exits or until timer expires */
2262 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002263 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002264
2265 /* cancel pending alarm if still there and restore signal */
2266 alarm(0);
2267 sigaction(SIGALRM, &old, NULL);
2268# else
2269 int waited;
2270
2271 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2272 * to give cscope a chance to exit quickly. */
2273 sleep(0);
2274 for (waited = 0; waited < 40; ++waited)
2275 {
2276 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002277 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002278 if (pid != 0)
2279 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002280 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002281 }
2282# endif
2283 /*
2284 * If the cscope process is still running: kill it.
2285 * Safety check: If the PID would be zero here, the entire X session
2286 * would be killed. -1 and 1 are dangerous as well.
2287 */
2288 if (pid < 0 && csinfo[i].pid > 1)
2289 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002290# ifdef ECHILD
2291 int alive = TRUE;
2292
2293 if (waitpid_errno == ECHILD)
2294 {
2295 /*
2296 * When using 'vim -g', vim is forked and cscope process is
2297 * no longer a child process but a sibling. So waitpid()
2298 * fails with errno being ECHILD (No child processes).
2299 * Don't send SIGKILL to cscope immediately but wait
2300 * (polling) for it to exit normally as result of sending
2301 * the "q" command, hence giving it a chance to clean up
2302 * its temporary files.
2303 */
2304 int waited;
2305
2306 sleep(0);
2307 for (waited = 0; waited < 40; ++waited)
2308 {
2309 /* Check whether cscope process is still alive */
2310 if (kill(csinfo[i].pid, 0) != 0)
2311 {
2312 alive = FALSE; /* cscope process no longer exists */
2313 break;
2314 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002315 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002316 }
2317 }
2318 if (alive)
2319# endif
2320 {
2321 kill(csinfo[i].pid, SIGKILL);
2322 (void)waitpid(csinfo[i].pid, &pstat, 0);
2323 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002324 }
2325 }
2326#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002327 if (csinfo[i].hProc != NULL)
2328 {
2329 /* Give cscope a chance to exit normally */
2330 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2331 TerminateProcess(csinfo[i].hProc, 0);
2332 CloseHandle(csinfo[i].hProc);
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#endif
2335
2336 if (csinfo[i].fr_fp != NULL)
2337 (void)fclose(csinfo[i].fr_fp);
2338 if (csinfo[i].to_fp != NULL)
2339 (void)fclose(csinfo[i].to_fp);
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (freefnpp)
2342 {
2343 vim_free(csinfo[i].fname);
2344 vim_free(csinfo[i].ppath);
2345 vim_free(csinfo[i].flags);
2346 }
2347
2348 clear_csinfo(i);
2349} /* cs_release_csp */
2350
2351
2352/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002353 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002356cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2359 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002360 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002362 if (csinfo_size == 0)
2363 return CSCOPE_SUCCESS;
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002366 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2367 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2368 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (dblist == NULL || pplist == NULL || fllist == NULL)
2370 {
2371 vim_free(dblist);
2372 vim_free(pplist);
2373 vim_free(fllist);
2374 return CSCOPE_FAILURE;
2375 }
2376
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002377 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 dblist[i] = csinfo[i].fname;
2380 pplist[i] = csinfo[i].ppath;
2381 fllist[i] = csinfo[i].flags;
2382 if (csinfo[i].fname != NULL)
2383 cs_release_csp(i, FALSE);
2384 }
2385
2386 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002387 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 if (dblist[i] != NULL)
2390 {
2391 cs_add_common(dblist[i], pplist[i], fllist[i]);
2392 if (p_csverbose)
2393 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002394 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * connection number in the same line as
2396 * "Added cscope database..."
2397 */
2398 sprintf(buf, " (#%d)", i);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002399 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 }
2402 vim_free(dblist[i]);
2403 vim_free(pplist[i]);
2404 vim_free(fllist[i]);
2405 }
2406 vim_free(dblist);
2407 vim_free(pplist);
2408 vim_free(fllist);
2409
2410 if (p_csverbose)
Bram Moolenaar8820b482017-03-16 17:23:31 +01002411 MSG_ATTR(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 return CSCOPE_SUCCESS;
2413} /* cs_reset */
2414
2415
2416/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002417 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 * (Prepends ppath, if there is one and if it's not already prepended,
2419 * otherwise just uses the name found.)
2420 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002421 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002423 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002426cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002428 char *fullname;
2429 int len;
2430 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002433 * Ppath is freed when we destroy the cscope connection.
2434 * Fullname is freed after cs_make_vim_style_matches, after it's been
2435 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002437 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002439 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002440 else if (p_csre && csinfo[i].fname != NULL)
2441 {
2442 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2443 * path in path resolution. */
2444 csdir = alloc(MAXPATHL);
2445 if (csdir != NULL)
2446 {
2447 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaar28c21912013-05-29 19:18:00 +02002448 gettail((char_u *)csinfo[i].fname)
2449 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002450 len += (int)STRLEN(csdir);
2451 }
2452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002454 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002456 * happens, you are screwed up and need to fix how you're using cscope. */
2457 if (csinfo[i].ppath != NULL
2458 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2459 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002461 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002463 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002464 {
2465 if ((fullname = (char *)alloc(len)) != NULL)
2466 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2467 }
2468 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002469 {
2470 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002471 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002472 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002475 {
2476 fullname = (char *)vim_strsave((char_u *)name);
2477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002479 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483
2484/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002485 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002488cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
2490 short i;
2491 if (cs_cnt_connections() == 0)
2492 MSG_PUTS(_("no cscope connections\n"));
2493 else
2494 {
2495 MSG_PUTS_ATTR(
2496 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002497 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002498 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 if (csinfo[i].fname == NULL)
2501 continue;
2502
2503 if (csinfo[i].ppath != NULL)
2504 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2505 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2506 else
2507 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2508 i, (long)csinfo[i].pid, csinfo[i].fname);
2509 }
2510 }
2511
2512 wait_return(TRUE);
2513 return CSCOPE_SUCCESS;
2514} /* cs_show */
2515
Bram Moolenaar02b06312007-09-06 15:39:22 +00002516
2517/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002518 * Only called when VIM exits to quit any cscope sessions.
2519 */
2520 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002521cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002522{
2523 int i;
2524
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002525 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002526 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002527 vim_free(csinfo);
2528 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002529}
2530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531#endif /* FEAT_CSCOPE */
2532
2533/* the end */