blob: 986663d9516c9233a42ebb5cd9d78cfcc9f04a1a [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 Moolenaara172b632018-01-30 22:52:06 +0100705 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100706 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
Bram Moolenaar21662be2016-11-06 14:46:44 +01001150 if (*qfpos != '0'
1151 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1152 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001153 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001154# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001155 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001156 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001157# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001158 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160#endif
1161
1162 /* create the actual command to send to cscope */
1163 cmd = cs_create_cmd(opt, pat);
1164 if (cmd == NULL)
1165 return FALSE;
1166
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001167 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1168 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001169 {
1170 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001171 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001172 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001173
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001174 /* Send query to all open connections, then count the total number
1175 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001176 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 nummatches[i] = 0;
1178 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001179 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001181 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 continue;
1183
1184 /* send cmd to cscope */
1185 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1186 (void)fflush(csinfo[i].to_fp);
1187
1188 nummatches[i] = cs_cnt_matches(i);
1189
1190 if (nummatches[i] > -1)
1191 totmatches += nummatches[i];
1192
1193 if (nummatches[i] == 0)
1194 (void)cs_read_prompt(i);
1195 }
1196 vim_free(cmd);
1197
1198 if (totmatches == 0)
1199 {
1200 char *nf = _("E259: no matches found for cscope query %s of %s");
1201 char *buf;
1202
1203 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001204 {
1205 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001209 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (buf == NULL)
1211 (void)EMSG(nf);
1212 else
1213 {
1214 sprintf(buf, nf, opt, pat);
1215 (void)EMSG(buf);
1216 vim_free(buf);
1217 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001218 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return FALSE;
1220 }
1221
1222#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001223 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001226 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001227 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001228 qf_info_T *qi = NULL;
1229 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001231 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001232 if (f == NULL)
1233 EMSG2(_(e_notopen), tmp);
1234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 cs_file_results(f, nummatches);
1237 fclose(f);
1238 if (use_ll) /* Use location list */
1239 wp = curwin;
1240 /* '-' starts a new error list */
1241 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001242 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001244 if (postponed_split != 0)
1245 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001246 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001248 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001249 postponed_split = 0;
1250 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001251
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001252 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1253 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001254 if (use_ll)
1255 /*
1256 * In the location list window, use the displayed location
1257 * list. Otherwise, use the location list for the window.
1258 */
1259 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1260 ? wp->w_llist_ref : wp->w_llist;
1261 qf_jump(qi, 0, 0, forceit);
1262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 mch_remove(tmp);
1265 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001266 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 return TRUE;
1268 }
1269 else
1270#endif /* FEAT_QUICKFIX */
1271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001272 char **matches = NULL, **contexts = NULL;
1273 int matched = 0;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /* read output */
1276 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1277 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001278 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (matches == NULL)
1280 return FALSE;
1281
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001282 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1285 }
1286
1287} /* cs_find_common */
1288
1289/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001290 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001293cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 cscmd_T *cmdp = cs_cmds;
1296
1297 (void)MSG_PUTS(_("cscope commands:\n"));
1298 while (cmdp->name != NULL)
1299 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001300 char *help = _(cmdp->help);
1301 int space_cnt = 30 - vim_strsize((char_u *)help);
1302
1303 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1304 if (space_cnt < 0)
1305 space_cnt = 0;
1306 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1307 cmdp->name,
1308 help, space_cnt, " ",
1309 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar627943d2008-08-25 02:35:59 +00001311 MSG_PUTS(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001312 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001313 " c: Find functions calling this function\n"
1314 " d: Find functions called by this function\n"
1315 " e: Find this egrep pattern\n"
1316 " f: Find this file\n"
1317 " g: Find this definition\n"
1318 " i: Find files #including this file\n"
1319 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001320 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 cmdp++;
1323 }
1324
1325 wait_return(TRUE);
1326 return 0;
1327} /* cs_help */
1328
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001331clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 csinfo[i].fname = NULL;
1334 csinfo[i].ppath = NULL;
1335 csinfo[i].flags = NULL;
1336#if defined(UNIX)
1337 csinfo[i].st_dev = (dev_t)0;
1338 csinfo[i].st_ino = (ino_t)0;
1339#else
1340 csinfo[i].nVolume = 0;
1341 csinfo[i].nIndexHigh = 0;
1342 csinfo[i].nIndexLow = 0;
1343#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001344 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 csinfo[i].fr_fp = NULL;
1346 csinfo[i].to_fp = NULL;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001347#if defined(WIN32)
1348 csinfo[i].hProc = NULL;
1349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350}
1351
1352#ifndef UNIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001353static char *GetWin32Error(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
1355 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001356GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 char *msg = NULL;
1359 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1360 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1361 if (msg != NULL)
1362 {
1363 /* remove trailing \r\n */
1364 char *pcrlf = strstr(msg, "\r\n");
1365 if (pcrlf != NULL)
1366 *pcrlf = '\0';
1367 }
1368 return msg;
1369}
1370#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001373 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001376cs_insert_filelist(
1377 char *fname,
1378 char *ppath,
1379 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001380 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 short i, j;
1383#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 BY_HANDLE_FILE_INFORMATION bhfi;
1385
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001386 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001388 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1389 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (p_csverbose)
1391 {
1392 char *cant_msg = _("E625: cannot open cscope database: %s");
1393 char *winmsg = GetWin32Error();
1394
1395 if (winmsg != NULL)
1396 {
1397 (void)EMSG2(cant_msg, winmsg);
1398 LocalFree(winmsg);
1399 }
1400 else
1401 /* subst filename if can't get error text */
1402 (void)EMSG2(cant_msg, fname);
1403 }
1404 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001405
1406 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (p_csverbose)
1408 (void)EMSG(_("E626: cannot get cscope database information"));
1409 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411#endif
1412
1413 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001414 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
1416 if (csinfo[j].fname != NULL
1417#if defined(UNIX)
1418 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1419#else
1420 /* compare pathnames first */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001421 && ((fullpathcmp((char_u *)csinfo[j].fname,
1422 (char_u *)fname, FALSE) & FPC_SAME)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001423 /* test index file attributes too */
1424 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1426 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1427#endif
1428 )
1429 {
1430 if (p_csverbose)
1431 (void)EMSG(_("E568: duplicate cscope database not added"));
1432 return -1;
1433 }
1434
1435 if (csinfo[j].fname == NULL && i == -1)
1436 i = j; /* remember first empty entry */
1437 }
1438
1439 if (i == -1)
1440 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001441 i = csinfo_size;
1442 if (csinfo_size == 0)
1443 {
1444 /* First time allocation: allocate only 1 connection. It should
1445 * be enough for most users. If more is needed, csinfo will be
1446 * reallocated. */
1447 csinfo_size = 1;
1448 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1449 }
1450 else
1451 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001452 csinfo_T *t_csinfo = csinfo;
1453
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001454 /* Reallocate space for more connections. */
1455 csinfo_size *= 2;
1456 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001457 if (csinfo == NULL)
1458 {
1459 vim_free(t_csinfo);
1460 csinfo_size = 0;
1461 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001462 }
1463 if (csinfo == NULL)
1464 return -1;
1465 for (j = csinfo_size/2; j < csinfo_size; j++)
1466 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001469 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return -1;
1471
1472 (void)strcpy(csinfo[i].fname, (const char *)fname);
1473
1474 if (ppath != NULL)
1475 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001476 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001478 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 return -1;
1480 }
1481 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1482 } else
1483 csinfo[i].ppath = NULL;
1484
1485 if (flags != NULL)
1486 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001487 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001489 VIM_CLEAR(csinfo[i].fname);
1490 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return -1;
1492 }
1493 (void)strcpy(csinfo[i].flags, (const char *)flags);
1494 } else
1495 csinfo[i].flags = NULL;
1496
1497#if defined(UNIX)
1498 csinfo[i].st_dev = sb->st_dev;
1499 csinfo[i].st_ino = sb->st_ino;
1500
1501#else
1502 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1503 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1504 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1505#endif
1506 return i;
1507} /* cs_insert_filelist */
1508
1509
1510/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001511 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
1513 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001514cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 cscmd_T *cmdp;
1517 char *stok;
1518 size_t len;
1519
1520 if (eap->arg == NULL)
1521 return NULL;
1522
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001523 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001524 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1527 return NULL;
1528
1529 len = strlen(stok);
1530 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1531 {
1532 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1533 return (cmdp);
1534 }
1535 return NULL;
1536} /* cs_lookup_cmd */
1537
1538
1539/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001540 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001543cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 char *stok;
1546 short i;
1547
1548 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1549 {
1550 cs_usage_msg(Kill);
1551 return CSCOPE_FAILURE;
1552 }
1553
1554 /* only single digit positive and negative integers are allowed */
1555 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1556 || (strlen(stok) < 3 && stok[0] == '-'
1557 && VIM_ISDIGIT((int)(stok[1]))))
1558 i = atoi(stok);
1559 else
1560 {
1561 /* It must be part of a name. We will try to find a match
1562 * within all the names in the csinfo data structure
1563 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001564 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1567 break;
1568 }
1569 }
1570
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001571 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 if (p_csverbose)
1574 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1575 }
1576 else
1577 {
1578 if (i == -1)
1579 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001580 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 if (csinfo[i].fname)
1583 cs_kill_execute(i, csinfo[i].fname);
1584 }
1585 }
1586 else
1587 cs_kill_execute(i, stok);
1588 }
1589
1590 return 0;
1591} /* cs_kill */
1592
1593
1594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Actually kills a specific cscope connection.
1596 */
1597 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001598cs_kill_execute(
1599 int i, /* cscope table index */
1600 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602 if (p_csverbose)
1603 {
1604 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001605 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 (char_u *)_("cscope connection %s closed"), cname);
1607 }
1608 cs_release_csp(i, TRUE);
1609}
1610
1611
1612/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001613 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1615 * the type of the tag you are looking for. for example, in Darren Hiebert's
1616 * ctags (the one that comes with vim), #define's use a line number to find the
1617 * tag in a file while function definitions use a regexp search pattern.
1618 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001619 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 * quirky (and probably other things i don't know about):
1621 *
1622 * if you have "# define" in your source file, which is
1623 * perfectly legal, cscope thinks you have "#define". this
1624 * will result in a failed regexp search. :(
1625 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001626 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * would still have to be modified to escape all the special regular expression
1628 * characters to comply with ctags formatting.
1629 */
1630 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001631cs_make_vim_style_matches(
1632 char *fname,
1633 char *slno,
1634 char *search,
1635 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 /* vim style is ctags:
1638 *
1639 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1640 *
1641 * but as mentioned above, we'll always use the line number and
1642 * put the search pattern (if one exists) as "extra"
1643 *
1644 * buf is used as part of vim's method of handling tags, and
1645 * (i think) vim frees it when you pop your tags and get replaced
1646 * by new ones on the tag stack.
1647 */
1648 char *buf;
1649 int amt;
1650
1651 if (search != NULL)
1652 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001653 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if ((buf = (char *)alloc(amt)) == NULL)
1655 return NULL;
1656
1657 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1658 }
1659 else
1660 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001661 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if ((buf = (char *)alloc(amt)) == NULL)
1663 return NULL;
1664
1665 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1666 }
1667
1668 return buf;
1669} /* cs_make_vim_style_matches */
1670
1671
1672/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001673 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 *
1675 * Store: keep a ptr to the (malloc'd) memory of matches originally
1676 * generated from cs_find(). the matches are originally lines directly
1677 * from cscope output, but transformed to look like something out of a
1678 * ctags. see cs_make_vim_style_matches for more details.
1679 *
1680 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1681 * the next line from the cscope output. it basically keeps track of which
1682 * lines have been "used" and returns the next one.
1683 *
1684 * Free: frees up everything and resets
1685 *
1686 * Print: prints the tags
1687 */
1688 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001689cs_manage_matches(
1690 char **matches,
1691 char **contexts,
1692 int totmatches,
1693 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 static char **mp = NULL;
1696 static char **cp = NULL;
1697 static int cnt = -1;
1698 static int next = -1;
1699 char *p = NULL;
1700
1701 switch (cmd)
1702 {
1703 case Store:
1704 assert(matches != NULL);
1705 assert(totmatches > 0);
1706 if (mp != NULL || cp != NULL)
1707 (void)cs_manage_matches(NULL, NULL, -1, Free);
1708 mp = matches;
1709 cp = contexts;
1710 cnt = totmatches;
1711 next = 0;
1712 break;
1713 case Get:
1714 if (next >= cnt)
1715 return NULL;
1716
1717 p = mp[next];
1718 next++;
1719 break;
1720 case Free:
1721 if (mp != NULL)
1722 {
1723 if (cnt > 0)
1724 while (cnt--)
1725 {
1726 vim_free(mp[cnt]);
1727 if (cp != NULL)
1728 vim_free(cp[cnt]);
1729 }
1730 vim_free(mp);
1731 vim_free(cp);
1732 }
1733 mp = NULL;
1734 cp = NULL;
1735 cnt = 0;
1736 next = 0;
1737 break;
1738 case Print:
1739 cs_print_tags_priv(mp, cp, cnt);
1740 break;
1741 default: /* should not reach here */
Bram Moolenaar95f09602016-11-10 20:01:45 +01001742 IEMSG(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 return NULL;
1744 }
1745
1746 return p;
1747} /* cs_manage_matches */
1748
1749
1750/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001751 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 */
1753 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001754cs_parse_results(
1755 int cnumber,
1756 char *buf,
1757 int bufsize,
1758 char **context,
1759 char **linenumber,
1760 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int ch;
1763 char *p;
1764 char *name;
1765
1766 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1767 {
1768 if (feof(csinfo[cnumber].fr_fp))
1769 errno = EIO;
1770
1771 cs_reading_emsg(cnumber);
1772
1773 return NULL;
1774 }
1775
1776 /* If the line's too long for the buffer, discard it. */
1777 if ((p = strchr(buf, '\n')) == NULL)
1778 {
1779 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1780 ;
1781 return NULL;
1782 }
1783 *p = '\0';
1784
1785 /*
1786 * cscope output is in the following format:
1787 *
1788 * <filename> <context> <line number> <pattern>
1789 */
1790 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1791 return NULL;
1792 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1793 return NULL;
1794 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1795 return NULL;
1796 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1797
1798 /* --- nvi ---
1799 * If the file is older than the cscope database, that is,
1800 * the database was built since the file was last modified,
1801 * or there wasn't a search string, use the line number.
1802 */
1803 if (strcmp(*search, "<unknown>") == 0)
1804 *search = NULL;
1805
1806 name = cs_resolve_file(cnumber, name);
1807 return name;
1808}
1809
Bram Moolenaarc716c302006-01-21 22:12:51 +00001810#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001812 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
1814 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001815cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 int i, j;
1818 char *buf;
1819 char *search, *slno;
1820 char *fullname;
1821 char *cntx;
1822 char *context;
1823
1824 buf = (char *)alloc(CSREAD_BUFSIZE);
1825 if (buf == NULL)
1826 return;
1827
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001828 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 if (nummatches_a[i] < 1)
1831 continue;
1832
1833 for (j = 0; j < nummatches_a[i]; j++)
1834 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001835 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1836 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 continue;
1838
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001839 context = (char *)alloc((unsigned)strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001840 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 continue;
1842
1843 if (strcmp(cntx, "<global>")==0)
1844 strcpy(context, "<<global>>");
1845 else
1846 sprintf(context, "<<%s>>", cntx);
1847
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001848 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1850 else
1851 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1852
1853 vim_free(context);
1854 vim_free(fullname);
1855 } /* for all matches */
1856
1857 (void)cs_read_prompt(i);
1858
1859 } /* for all cscope connections */
1860 vim_free(buf);
1861}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001865 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1866 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001867 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001870cs_fill_results(
1871 char *tagstr,
1872 int totmatches,
1873 int *nummatches_a,
1874 char ***matches_p,
1875 char ***cntxts_p,
1876 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 int i, j;
1879 char *buf;
1880 char *search, *slno;
1881 int totsofar = 0;
1882 char **matches = NULL;
1883 char **cntxts = NULL;
1884 char *fullname;
1885 char *cntx;
1886
1887 assert(totmatches > 0);
1888
1889 buf = (char *)alloc(CSREAD_BUFSIZE);
1890 if (buf == NULL)
1891 return;
1892
1893 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1894 goto parse_out;
1895 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1896 goto parse_out;
1897
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001898 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 if (nummatches_a[i] < 1)
1901 continue;
1902
1903 for (j = 0; j < nummatches_a[i]; j++)
1904 {
1905 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1906 &slno, &search)) == NULL)
1907 continue;
1908
1909 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1910 search, tagstr);
1911
1912 vim_free(fullname);
1913
1914 if (strcmp(cntx, "<global>") == 0)
1915 cntxts[totsofar] = NULL;
1916 else
1917 /* note: if vim_strsave returns NULL, then the context
1918 * will be "<global>", which is misleading.
1919 */
1920 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1921
1922 if (matches[totsofar] != NULL)
1923 totsofar++;
1924
1925 } /* for all matches */
1926
1927 (void)cs_read_prompt(i);
1928
1929 } /* for all cscope connections */
1930
1931parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001932 if (totsofar == 0)
1933 {
1934 /* No matches, free the arrays and return NULL in "*matches_p". */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001935 VIM_CLEAR(matches);
1936 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 *matched = totsofar;
1939 *matches_p = matches;
1940 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 vim_free(buf);
1943} /* cs_fill_results */
1944
1945
1946/* get the requested path components */
1947 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001948cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
1950 int i;
1951 char *s;
1952
1953 if (p_cspc == 0)
1954 return path;
1955
1956 s = path + strlen(path) - 1;
1957 for (i = 0; i < p_cspc; ++i)
1958 while (s > path && *--s != '/'
1959#ifdef WIN32
1960 && *--s != '\\'
1961#endif
1962 )
1963 ;
1964 if ((s > path && *s == '/')
1965#ifdef WIN32
1966 || (s > path && *s == '\\')
1967#endif
1968 )
1969 ++s;
1970 return s;
1971}
1972
1973/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001974 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 */
1976 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001977cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001980 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 int bufsize = 0; /* Track available bufsize */
1982 int newsize = 0;
1983 char *ptag;
1984 char *fname, *lno, *extra, *tbuf;
1985 int i, idx, num;
1986 char *globalcntx = "GLOBAL";
1987 char *cntxformat = " <<%s>>";
1988 char *context;
1989 char *cstag_msg = _("Cscope tag: %s");
1990 char *csfmt_str = "%4d %6s ";
1991
Bram Moolenaar4033c552017-09-16 20:54:51 +02001992 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001994 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return;
1996
1997 strcpy(tbuf, matches[0]);
1998 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001999 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002000 {
2001 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02002002 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002005 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 buf = (char *)alloc(newsize);
2007 if (buf != NULL)
2008 {
2009 bufsize = newsize;
2010 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002011 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013
2014 vim_free(tbuf);
2015
Bram Moolenaar8820b482017-03-16 17:23:31 +01002016 MSG_PUTS_ATTR(_("\n # line"), HL_ATTR(HLF_T)); /* strlen is 7 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 msg_advance(msg_col + 2);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002018 MSG_PUTS_ATTR(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 num = 1;
2021 for (i = 0; i < num_matches; i++)
2022 {
2023 idx = i;
2024
2025 /* if we really wanted to, we could avoid this malloc and strcpy
2026 * by parsing matches[i] on the fly and placing stuff into buf
2027 * directly, but that's too much of a hassle
2028 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002029 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 continue;
2031 (void)strcpy(tbuf, matches[idx]);
2032
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002033 if (strtok(tbuf, (const char *)"\t") == NULL
2034 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2035 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2036 {
2037 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002039 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002040 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2043
2044 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002045 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (bufsize < newsize)
2047 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002048 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 buf = (char *)vim_realloc(buf, newsize);
2050 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002053 vim_free(t_buf);
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 else
2056 bufsize = newsize;
2057 }
2058 if (buf != NULL)
2059 {
2060 /* csfmt_str = "%4d %6s "; */
2061 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002062 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
Bram Moolenaar8820b482017-03-16 17:23:31 +01002064 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 /* compute the required space for the context */
2067 if (cntxts[idx] != NULL)
2068 context = cntxts[idx];
2069 else
2070 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002071 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 if (bufsize < newsize)
2074 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002075 t_buf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 buf = (char *)vim_realloc(buf, newsize);
2077 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002080 vim_free(t_buf);
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 else
2083 bufsize = newsize;
2084 }
2085 if (buf != NULL)
2086 {
2087 (void)sprintf(buf, cntxformat, context);
2088
2089 /* print the context only if it fits on the same line */
2090 if (msg_col + (int)strlen(buf) >= (int)Columns)
2091 msg_putchar('\n');
2092 msg_advance(12);
2093 MSG_PUTS_LONG(buf);
2094 msg_putchar('\n');
2095 }
2096 if (extra != NULL)
2097 {
2098 msg_advance(13);
2099 MSG_PUTS_LONG(extra);
2100 }
2101
2102 vim_free(tbuf); /* only after printing extra due to strtok use */
2103
2104 if (msg_col)
2105 msg_putchar('\n');
2106
2107 ui_breakcheck();
2108 if (got_int)
2109 {
2110 got_int = FALSE; /* don't print any more matches */
2111 break;
2112 }
2113
2114 num++;
2115 } /* for all matches */
2116
2117 vim_free(buf);
2118} /* cs_print_tags_priv */
2119
2120
2121/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002122 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 */
2124 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002125cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int ch;
2128 char *buf = NULL; /* buffer for possible error message from cscope */
2129 int bufpos = 0;
2130 char *cs_emsg;
2131 int maxlen;
2132 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 int n;
2135
2136 cs_emsg = _("E609: Cscope error: %s");
2137 /* compute maximum allowed len for Cscope error message */
2138 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2139
2140 for (;;)
2141 {
2142 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2143 /* if there is room and char is printable */
2144 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2145 {
2146 if (buf == NULL) /* lazy buffer allocation */
2147 buf = (char *)alloc(maxlen);
2148 if (buf != NULL)
2149 {
2150 /* append character to the message */
2151 buf[bufpos++] = ch;
2152 buf[bufpos] = NUL;
2153 if (bufpos >= epromptlen
2154 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2155 {
2156 /* remove eprompt from buf */
2157 buf[bufpos - epromptlen] = NUL;
2158
2159 /* print message to user */
2160 (void)EMSG2(cs_emsg, buf);
2161
2162 /* send RETURN to cscope */
2163 (void)putc('\n', csinfo[i].to_fp);
2164 (void)fflush(csinfo[i].to_fp);
2165
2166 /* clear buf */
2167 bufpos = 0;
2168 buf[bufpos] = NUL;
2169 }
2170 }
2171 }
2172
2173 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2174 {
2175 if (n > 0)
2176 ch = getc(csinfo[i].fr_fp);
2177 if (ch == EOF)
2178 {
2179 PERROR("cs_read_prompt EOF");
2180 if (buf != NULL && buf[0] != NUL)
2181 (void)EMSG2(cs_emsg, buf);
2182 else if (p_csverbose)
2183 cs_reading_emsg(i); /* don't have additional information */
2184 cs_release_csp(i, TRUE);
2185 vim_free(buf);
2186 return CSCOPE_FAILURE;
2187 }
2188
2189 if (ch != CSCOPE_PROMPT[n])
2190 {
2191 ch = EOF;
2192 break;
2193 }
2194 }
2195
2196 if (ch == EOF)
2197 continue; /* didn't find the prompt */
2198 break; /* did find the prompt */
2199 }
2200
2201 vim_free(buf);
2202 return CSCOPE_SUCCESS;
2203}
2204
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002205#if defined(UNIX) && defined(SIGALRM)
2206/*
2207 * Used to catch and ignore SIGALRM below.
2208 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002209 static RETSIGTYPE
2210sig_handler SIGDEFARG(sigarg)
2211{
2212 /* do nothing */
2213 SIGRETURN;
2214}
2215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002218 * Does the actual free'ing for the cs ptr with an optional flag of whether
2219 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 */
2221 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002222cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 /*
2225 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2226 */
2227 if (csinfo[i].to_fp != NULL)
2228 {
2229 (void)fputs("q\n", csinfo[i].to_fp);
2230 (void)fflush(csinfo[i].to_fp);
2231 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002232#if defined(UNIX)
2233 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002234 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002235 int pstat;
2236 pid_t pid;
2237
2238# if defined(HAVE_SIGACTION)
2239 struct sigaction sa, old;
2240
Bram Moolenaar9701da02008-03-16 12:09:58 +00002241 /* Use sigaction() to limit the waiting time to two seconds. */
2242 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002243 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002244# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002245 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002246# else
2247 sa.sa_flags = 0;
2248# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002249 sigaction(SIGALRM, &sa, &old);
2250 alarm(2); /* 2 sec timeout */
2251
2252 /* Block until cscope exits or until timer expires */
2253 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002254 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255
2256 /* cancel pending alarm if still there and restore signal */
2257 alarm(0);
2258 sigaction(SIGALRM, &old, NULL);
2259# else
2260 int waited;
2261
2262 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2263 * to give cscope a chance to exit quickly. */
2264 sleep(0);
2265 for (waited = 0; waited < 40; ++waited)
2266 {
2267 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002268 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002269 if (pid != 0)
2270 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002271 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002272 }
2273# endif
2274 /*
2275 * If the cscope process is still running: kill it.
2276 * Safety check: If the PID would be zero here, the entire X session
2277 * would be killed. -1 and 1 are dangerous as well.
2278 */
2279 if (pid < 0 && csinfo[i].pid > 1)
2280 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002281# ifdef ECHILD
2282 int alive = TRUE;
2283
2284 if (waitpid_errno == ECHILD)
2285 {
2286 /*
2287 * When using 'vim -g', vim is forked and cscope process is
2288 * no longer a child process but a sibling. So waitpid()
2289 * fails with errno being ECHILD (No child processes).
2290 * Don't send SIGKILL to cscope immediately but wait
2291 * (polling) for it to exit normally as result of sending
2292 * the "q" command, hence giving it a chance to clean up
2293 * its temporary files.
2294 */
2295 int waited;
2296
2297 sleep(0);
2298 for (waited = 0; waited < 40; ++waited)
2299 {
2300 /* Check whether cscope process is still alive */
2301 if (kill(csinfo[i].pid, 0) != 0)
2302 {
2303 alive = FALSE; /* cscope process no longer exists */
2304 break;
2305 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002306 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002307 }
2308 }
2309 if (alive)
2310# endif
2311 {
2312 kill(csinfo[i].pid, SIGKILL);
2313 (void)waitpid(csinfo[i].pid, &pstat, 0);
2314 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002315 }
2316 }
2317#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002318 if (csinfo[i].hProc != NULL)
2319 {
2320 /* Give cscope a chance to exit normally */
2321 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2322 TerminateProcess(csinfo[i].hProc, 0);
2323 CloseHandle(csinfo[i].hProc);
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#endif
2326
2327 if (csinfo[i].fr_fp != NULL)
2328 (void)fclose(csinfo[i].fr_fp);
2329 if (csinfo[i].to_fp != NULL)
2330 (void)fclose(csinfo[i].to_fp);
2331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (freefnpp)
2333 {
2334 vim_free(csinfo[i].fname);
2335 vim_free(csinfo[i].ppath);
2336 vim_free(csinfo[i].flags);
2337 }
2338
2339 clear_csinfo(i);
2340} /* cs_release_csp */
2341
2342
2343/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002344 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002347cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2350 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002351 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002353 if (csinfo_size == 0)
2354 return CSCOPE_SUCCESS;
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 /* malloc our db and ppath list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002357 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2358 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2359 fllist = (char **)alloc(csinfo_size * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (dblist == NULL || pplist == NULL || fllist == NULL)
2361 {
2362 vim_free(dblist);
2363 vim_free(pplist);
2364 vim_free(fllist);
2365 return CSCOPE_FAILURE;
2366 }
2367
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002368 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 dblist[i] = csinfo[i].fname;
2371 pplist[i] = csinfo[i].ppath;
2372 fllist[i] = csinfo[i].flags;
2373 if (csinfo[i].fname != NULL)
2374 cs_release_csp(i, FALSE);
2375 }
2376
2377 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002378 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 if (dblist[i] != NULL)
2381 {
2382 cs_add_common(dblist[i], pplist[i], fllist[i]);
2383 if (p_csverbose)
2384 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002385 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 * connection number in the same line as
2387 * "Added cscope database..."
2388 */
2389 sprintf(buf, " (#%d)", i);
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 MSG_PUTS_ATTR(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 }
2393 vim_free(dblist[i]);
2394 vim_free(pplist[i]);
2395 vim_free(fllist[i]);
2396 }
2397 vim_free(dblist);
2398 vim_free(pplist);
2399 vim_free(fllist);
2400
2401 if (p_csverbose)
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 MSG_ATTR(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 return CSCOPE_SUCCESS;
2404} /* cs_reset */
2405
2406
2407/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002408 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * (Prepends ppath, if there is one and if it's not already prepended,
2410 * otherwise just uses the name found.)
2411 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002412 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002414 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 */
2416 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002417cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002419 char *fullname;
2420 int len;
2421 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002424 * Ppath is freed when we destroy the cscope connection.
2425 * Fullname is freed after cs_make_vim_style_matches, after it's been
2426 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002428 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002430 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002431 else if (p_csre && csinfo[i].fname != NULL)
2432 {
2433 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2434 * path in path resolution. */
2435 csdir = alloc(MAXPATHL);
2436 if (csdir != NULL)
2437 {
2438 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002439 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002440 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002441 len += (int)STRLEN(csdir);
2442 }
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002445 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002447 * happens, you are screwed up and need to fix how you're using cscope. */
2448 if (csinfo[i].ppath != NULL
2449 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2450 && (name[0] != '/')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#ifdef WIN32
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002452 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002454 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002455 {
2456 if ((fullname = (char *)alloc(len)) != NULL)
2457 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2458 }
2459 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002460 {
2461 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002462 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002463 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002466 {
2467 fullname = (char *)vim_strsave((char_u *)name);
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002470 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474
2475/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002476 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002479cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 short i;
2482 if (cs_cnt_connections() == 0)
2483 MSG_PUTS(_("no cscope connections\n"));
2484 else
2485 {
2486 MSG_PUTS_ATTR(
2487 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002488 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002489 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 if (csinfo[i].fname == NULL)
2492 continue;
2493
2494 if (csinfo[i].ppath != NULL)
2495 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2496 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2497 else
2498 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2499 i, (long)csinfo[i].pid, csinfo[i].fname);
2500 }
2501 }
2502
2503 wait_return(TRUE);
2504 return CSCOPE_SUCCESS;
2505} /* cs_show */
2506
Bram Moolenaar02b06312007-09-06 15:39:22 +00002507
2508/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002509 * Only called when VIM exits to quit any cscope sessions.
2510 */
2511 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002512cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002513{
2514 int i;
2515
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002516 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002517 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002518 vim_free(csinfo);
2519 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002520}
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522#endif /* FEAT_CSCOPE */
2523
2524/* the end */