blob: 4f359ee55c09d10f3f8e979911418b83b746d3fa [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 int cs_add(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static int cs_add_common(char *, char *, char *);
25static int cs_check_for_connections(void);
26static int cs_check_for_tags(void);
27static int cs_cnt_connections(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static int cs_create_connection(int i);
Bram Moolenaarc716c302006-01-21 22:12:51 +000029#ifdef FEAT_QUICKFIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void cs_file_results(FILE *, int *);
Bram Moolenaarc716c302006-01-21 22:12:51 +000031#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static void cs_fill_results(char *, int , int *, char ***,
33 char ***, int *);
34static int cs_find(exarg_T *eap);
35static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
36static int cs_help(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010037static int cs_insert_filelist(char *, char *, char *,
Bram Moolenaar8767f522016-07-01 17:17:39 +020038 stat_T *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static int cs_kill(exarg_T *eap);
40static void cs_kill_execute(int, char *);
41static cscmd_T * cs_lookup_cmd(exarg_T *eap);
42static char * cs_make_vim_style_matches(char *, char *,
43 char *, char *);
44static char * cs_manage_matches(char **, char **, int, mcmd_e);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void cs_print_tags_priv(char **, char **, int);
46static int cs_read_prompt(int);
47static void cs_release_csp(int, int freefnpp);
48static int cs_reset(exarg_T *eap);
49static char * cs_resolve_file(int, char *);
50static int cs_show(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52
Bram Moolenaar9fa49da2009-07-10 13:11:26 +000053static csinfo_T * csinfo = NULL;
54static int csinfo_size = 0; /* number of items allocated in
55 csinfo[] */
56
Bram Moolenaard2ac9842007-08-21 16:03:51 +000057static int eap_arg_len; /* length of eap->arg, set in
58 cs_lookup_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static cscmd_T cs_cmds[] =
60{
61 { "add", cs_add,
62 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
63 { "find", cs_find,
Bram Moolenaar80632db2016-07-05 22:28:40 +020064 N_("Query for a pattern"), "find a|c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 { "help", cs_help,
66 N_("Show this message"), "help", 0 },
67 { "kill", cs_kill,
68 N_("Kill a connection"), "kill #", 0 },
69 { "reset", cs_reset,
70 N_("Reinit all connections"), "reset", 0 },
71 { "show", cs_show,
72 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000073 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
76 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +010077cs_usage_msg(csid_e x)
Bram Moolenaar071d4272004-06-13 20:20:40 +000078{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010079 (void)semsg(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080}
81
Bram Moolenaarf4580d82009-03-18 11:52:53 +000082static enum
83{
84 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
Bram Moolenaar7bfef802009-04-22 14:25:01 +000085 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
Bram Moolenaarf4580d82009-03-18 11:52:53 +000086 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
87 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
88} expand_what;
89
90/*
91 * Function given to ExpandGeneric() to obtain the cscope command
92 * expansion.
93 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +000094 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +010095get_cscope_name(expand_T *xp UNUSED, int idx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +000096{
Bram Moolenaar7bfef802009-04-22 14:25:01 +000097 int current_idx;
98 int i;
99
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000100 switch (expand_what)
101 {
102 case EXP_CSCOPE_SUBCMD:
103 /* Complete with sub-commands of ":cscope":
104 * add, find, help, kill, reset, show */
105 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000106 case EXP_SCSCOPE_SUBCMD:
107 /* Complete with sub-commands of ":scscope": same sub-commands as
108 * ":cscope" but skip commands which don't support split windows */
109 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
110 if (cs_cmds[i].cansplit)
111 if (current_idx++ == idx)
112 break;
113 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000114 case EXP_CSCOPE_FIND:
115 {
116 const char *query_type[] =
117 {
Bram Moolenaar80632db2016-07-05 22:28:40 +0200118 "a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000119 };
120
121 /* Complete with query type of ":cscope find {query_type}".
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200122 * {query_type} can be letters (c, d, ... a) or numbers (0, 1,
123 * ..., 9) but only complete with letters, since numbers are
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000124 * redundant. */
125 return (char_u *)query_type[idx];
126 }
127 case EXP_CSCOPE_KILL:
128 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000129 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000130
131 /* ":cscope kill" accepts connection numbers or partial names of
132 * the pathname of the cscope database as argument. Only complete
133 * with connection numbers. -1 can also be used to kill all
134 * connections. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000135 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000136 {
137 if (csinfo[i].fname == NULL)
138 continue;
139 if (current_idx++ == idx)
140 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000141 vim_snprintf(connection, sizeof(connection), "%d", i);
142 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000143 }
144 }
145 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
146 }
147 default:
148 return NULL;
149 }
150}
151
152/*
153 * Handle command line completion for :cscope command.
154 */
155 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100156set_context_in_cscope_cmd(
157 expand_T *xp,
158 char_u *arg,
159 cmdidx_T cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000160{
161 char_u *p;
162
163 /* Default: expand subcommands */
164 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000165 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000166 expand_what = (cmdidx == CMD_scscope)
167 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000168
169 /* (part of) subcommand already typed */
170 if (*arg != NUL)
171 {
172 p = skiptowhite(arg);
173 if (*p != NUL) /* past first word */
174 {
175 xp->xp_pattern = skipwhite(p);
176 if (*skiptowhite(xp->xp_pattern) != NUL)
177 xp->xp_context = EXPAND_NOTHING;
178 else if (STRNICMP(arg, "add", p - arg) == 0)
179 xp->xp_context = EXPAND_FILES;
180 else if (STRNICMP(arg, "kill", p - arg) == 0)
181 expand_what = EXP_CSCOPE_KILL;
182 else if (STRNICMP(arg, "find", p - arg) == 0)
183 expand_what = EXP_CSCOPE_FIND;
184 else
185 xp->xp_context = EXPAND_NOTHING;
186 }
187 }
188}
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000191 * Find the command, print help if invalid, and then call the corresponding
192 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 */
194 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100195do_cscope_general(
196 exarg_T *eap,
Bram Moolenaar42b8d912017-01-15 17:18:57 +0100197 int make_split UNUSED) /* whether to split window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 cscmd_T *cmdp;
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
202 {
203 cs_help(eap);
204 return;
205 }
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 if (make_split)
208 {
209 if (!cmdp->cansplit)
210 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100211 (void)msg_puts(_("This cscope command does not support splitting the window.\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 return;
213 }
214 postponed_split = -1;
215 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000216 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219 cmdp->func(eap);
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000222 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223}
224
225/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100226 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 */
228 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100229ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 do_cscope_general(eap, FALSE);
232}
233
234/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100235 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 */
237 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100238ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239{
240 do_cscope_general(eap, TRUE);
241}
242
243/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100244 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100247ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 int ret = FALSE;
250
Bram Moolenaar446cb832008-06-24 21:56:24 +0000251 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100253 (void)emsg(_("E562: Usage: cstag <ident>"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 return;
255 }
256
257 switch (p_csto)
258 {
259 case 0 :
260 if (cs_check_for_connections())
261 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000262 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200263 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 if (ret == FALSE)
265 {
266 cs_free_tags();
267 if (msg_col)
268 msg_putchar('\n');
269
270 if (cs_check_for_tags())
271 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
272 }
273 }
274 else if (cs_check_for_tags())
275 {
276 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
277 }
278 break;
279 case 1 :
280 if (cs_check_for_tags())
281 {
282 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
283 if (ret == FALSE)
284 {
285 if (msg_col)
286 msg_putchar('\n');
287
288 if (cs_check_for_connections())
289 {
290 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200291 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (ret == FALSE)
293 cs_free_tags();
294 }
295 }
296 }
297 else if (cs_check_for_connections())
298 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000299 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200300 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 if (ret == FALSE)
302 cs_free_tags();
303 }
304 break;
305 default :
306 break;
307 }
308
309 if (!ret)
310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100311 (void)emsg(_("E257: cstag: tag not found"));
Bram Moolenaar4033c552017-09-16 20:54:51 +0200312#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 g_do_tagpreview = 0;
314#endif
315 }
316
Bram Moolenaard4db7712016-11-12 19:16:46 +0100317}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
319
320/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100321 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 * from the cscope output. should only be called from find_tags()
323 *
324 * returns TRUE if eof, FALSE otherwise
325 */
326 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100327cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328{
329 char *p;
330
331 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
332 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000333 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335 return FALSE;
336} /* cs_fgets */
337
338
339/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100340 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 */
342 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100343cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
345 cs_manage_matches(NULL, NULL, -1, Free);
346}
347
348
349/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100350 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 */
352 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100353cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 cs_manage_matches(NULL, NULL, -1, Print);
356}
357
358
359/*
360 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
361 *
362 * Checks for the existence of a |cscope| connection. If no
363 * parameters are specified, then the function returns:
364 *
365 * 0, if cscope was not available (not compiled in), or if there
366 * are no cscope connections; or
367 * 1, if there is at least one cscope connection.
368 *
369 * If parameters are specified, then the value of {num}
370 * determines how existence of a cscope connection is checked:
371 *
372 * {num} Description of existence check
373 * ----- ------------------------------
374 * 0 Same as no parameters (e.g., "cscope_connection()").
375 * 1 Ignore {prepend}, and use partial string matches for
376 * {dbpath}.
377 * 2 Ignore {prepend}, and use exact string matches for
378 * {dbpath}.
379 * 3 Use {prepend}, use partial string matches for both
380 * {dbpath} and {prepend}.
381 * 4 Use {prepend}, use exact string matches for both
382 * {dbpath} and {prepend}.
383 *
384 * Note: All string comparisons are case sensitive!
385 */
386#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200387 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100388cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 int i;
391
392 if (num < 0 || num > 4 || (num > 0 && !dbpath))
393 return FALSE;
394
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000395 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
397 if (!csinfo[i].fname)
398 continue;
399
400 if (num == 0)
401 return TRUE;
402
403 switch (num)
404 {
405 case 1:
406 if (strstr(csinfo[i].fname, (char *)dbpath))
407 return TRUE;
408 break;
409 case 2:
410 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
411 return TRUE;
412 break;
413 case 3:
414 if (strstr(csinfo[i].fname, (char *)dbpath)
415 && ((!ppath && !csinfo[i].ppath)
416 || (ppath
417 && csinfo[i].ppath
418 && strstr(csinfo[i].ppath, (char *)ppath))))
419 return TRUE;
420 break;
421 case 4:
422 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
423 && ((!ppath && !csinfo[i].ppath)
424 || (ppath
425 && csinfo[i].ppath
426 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
427 return TRUE;
428 break;
429 }
430 }
431
432 return FALSE;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200433}
434
435/*
436 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
437 *
438 * Checks the existence of a cscope connection.
439 */
440 void
441f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
442{
443#ifdef FEAT_CSCOPE
444 int num = 0;
445 char_u *dbpath = NULL;
446 char_u *prepend = NULL;
447 char_u buf[NUMBUFLEN];
448
449 if (argvars[0].v_type != VAR_UNKNOWN
450 && argvars[1].v_type != VAR_UNKNOWN)
451 {
452 num = (int)tv_get_number(&argvars[0]);
453 dbpath = tv_get_string(&argvars[1]);
454 if (argvars[2].v_type != VAR_UNKNOWN)
455 prepend = tv_get_string_buf(&argvars[2], buf);
456 }
457
458 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
459#endif
460}
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
463
464
465/*
466 * PRIVATE functions
467 ****************************************************************************/
468
469/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100470 * Add cscope database or a directory name (to look for cscope.out)
471 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100474cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 char *fname, *ppath, *flags = NULL;
477
478 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
479 {
480 cs_usage_msg(Add);
481 return CSCOPE_FAILURE;
482 }
483 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
484 flags = strtok((char *)NULL, (const char *)" ");
485
486 return cs_add_common(fname, ppath, flags);
487}
488
489 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100490cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 char *stat_emsg = _("E563: stat(%s) error: %d");
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200493 char *buf = alloc(strlen(stat_emsg) + MAXPATHL + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 if (buf != NULL)
496 {
497 (void)sprintf(buf, stat_emsg, fname, errno);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100498 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 vim_free(buf);
500 }
501 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100502 (void)emsg(_("E563: stat error"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
505
506/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100507 * The common routine to add a new cscope connection. Called by
508 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * routine uses a number of goto statements.
510 */
511 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100512cs_add_common(
513 char *arg1, /* filename - may contain environment variables */
514 char *arg2, /* prepend path - may contain environment variables */
515 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200517 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000518 int ret;
519 char *fname = NULL;
520 char *fname2 = NULL;
521 char *ppath = NULL;
522 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200523 int len;
524 int usedlen = 0;
525 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
527 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200528 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 goto add_err;
530
531 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200532 len = (int)STRLEN(fname);
533 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200534 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200535 (char_u **)&fname, &fbuf, &len);
536 if (fname == NULL)
537 goto add_err;
538 fname = (char *)vim_strnsave((char_u *)fname, len);
539 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200540
Bram Moolenaar8767f522016-07-01 17:17:39 +0200541 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (ret < 0)
543 {
544staterr:
545 if (p_csverbose)
546 cs_stat_emsg(fname);
547 goto add_err;
548 }
549
550 /* get the prepend path (arg2), expand it, and try to stat it */
551 if (arg2 != NULL)
552 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200553 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200555 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 goto add_err;
557
558 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200559 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (ret < 0)
561 goto staterr;
562 }
563
564 /* if filename is a directory, append the cscope database name to it */
Bram Moolenaard569bb02018-08-11 13:57:20 +0200565 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200567 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (fname2 == NULL)
569 goto add_err;
570
571 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100572#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 || fname[strlen(fname)-1] == '\\'
574#endif
575 )
576 {
577 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200578 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 break;
580 }
581 if (fname[0] == '\0')
582 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
583 else
584 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
585
Bram Moolenaar8767f522016-07-01 17:17:39 +0200586 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (ret < 0)
588 {
589 if (p_csverbose)
590 cs_stat_emsg(fname2);
591 goto add_err;
592 }
593
594 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {
598 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
599 }
600 else
601 {
602 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100603 (void)semsg(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 _("E564: %s is not a directory or a valid cscope database"),
605 fname);
606 goto add_err;
607 }
608
609 if (i != -1)
610 {
611 if (cs_create_connection(i) == CSCOPE_FAILURE
612 || cs_read_prompt(i) == CSCOPE_FAILURE)
613 {
614 cs_release_csp(i, TRUE);
615 goto add_err;
616 }
617
618 if (p_csverbose)
619 {
620 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100621 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 csinfo[i].fname);
624 }
625 }
626
627 vim_free(fname);
628 vim_free(fname2);
629 vim_free(ppath);
630 return CSCOPE_SUCCESS;
631
632add_err:
633 vim_free(fname2);
634 vim_free(fname);
635 vim_free(ppath);
636 return CSCOPE_FAILURE;
637} /* cs_add_common */
638
639
640 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100641cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 return (cs_cnt_connections() > 0);
644} /* cs_check_for_connections */
645
646
647 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100648cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000650 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651} /* cs_check_for_tags */
652
653
654/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100655 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 */
657 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100658cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 short i;
661 short cnt = 0;
662
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000663 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 if (csinfo[i].fname != NULL)
666 cnt++;
667 }
668 return cnt;
669} /* cs_cnt_connections */
670
671 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100672cs_reading_emsg(
673 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100675 semsg(_("E262: error reading cscope connection %d"), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677
678#define CSREAD_BUFSIZE 2048
679/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100680 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 */
682 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100683cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 char *stok;
686 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100687 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200689 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (buf == NULL)
691 return 0;
692 for (;;)
693 {
694 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
695 {
696 if (feof(csinfo[idx].fr_fp))
697 errno = EIO;
698
699 cs_reading_emsg(idx);
700
701 vim_free(buf);
702 return -1;
703 }
704
705 /*
706 * If the database is out of date, or there's some other problem,
707 * cscope will output error messages before the number-of-lines output.
708 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000709 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100710 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100712 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if ((stok = strtok(buf, (const char *)" ")) == NULL)
715 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000716 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 continue;
718
719 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
720 continue;
721 nlines = atoi(stok);
722 if (nlines < 0)
723 {
724 nlines = 0;
725 break;
726 }
727
728 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
729 continue;
730 if (strncmp((const char *)stok, "lines", 5))
731 continue;
732
733 break;
734 }
735
736 vim_free(buf);
737 return nlines;
738} /* cs_cnt_matches */
739
740
741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * Creates the actual cscope command query from what the user entered.
743 */
744 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100745cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 char *cmd;
748 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000749 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 switch (csoption[0])
752 {
753 case '0' : case 's' :
754 search = 0;
755 break;
756 case '1' : case 'g' :
757 search = 1;
758 break;
759 case '2' : case 'd' :
760 search = 2;
761 break;
762 case '3' : case 'c' :
763 search = 3;
764 break;
765 case '4' : case 't' :
766 search = 4;
767 break;
768 case '6' : case 'e' :
769 search = 6;
770 break;
771 case '7' : case 'f' :
772 search = 7;
773 break;
774 case '8' : case 'i' :
775 search = 8;
776 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200777 case '9' : case 'a' :
778 search = 9;
779 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 default :
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781 (void)emsg(_("E561: unknown cscope search type"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 cs_usage_msg(Find);
783 return NULL;
784 }
785
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000786 /* Skip white space before the patter, except for text and pattern search,
787 * they may want to use the leading white space. */
788 pat = pattern;
789 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100790 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000791 ++pat;
792
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200793 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 return NULL;
795
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000796 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 return cmd;
799} /* cs_create_cmd */
800
801
802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * This piece of code was taken/adapted from nvi. do we need to add
804 * the BSD license notice?
805 */
806 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100807cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000809#ifdef UNIX
810 int to_cs[2], from_cs[2];
811#endif
812 int len;
813 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100814#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000815 int fd;
816 SECURITY_ATTRIBUTES sa;
817 PROCESS_INFORMATION pi;
818 STARTUPINFO si;
819 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
820 HANDLE stdin_rd, stdout_rd;
821 HANDLE stdout_wr, stdin_wr;
822 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200823# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
824# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000825# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200826# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#endif
829
Bram Moolenaar02b06312007-09-06 15:39:22 +0000830#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 /*
832 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
833 * from_cs[0] and writes to to_cs[1].
834 */
835 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
836 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839err_closing:
840 if (to_cs[0] != -1)
841 (void)close(to_cs[0]);
842 if (to_cs[1] != -1)
843 (void)close(to_cs[1]);
844 if (from_cs[0] != -1)
845 (void)close(from_cs[0]);
846 if (from_cs[1] != -1)
847 (void)close(from_cs[1]);
848 return CSCOPE_FAILURE;
849 }
850
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 switch (csinfo[i].pid = fork())
852 {
853 case -1:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 (void)emsg(_("E622: Could not fork for cscope"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 goto err_closing;
856 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (dup2(to_cs[0], STDIN_FILENO) == -1)
858 PERROR("cs_create_connection 1");
859 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
860 PERROR("cs_create_connection 2");
861 if (dup2(from_cs[1], STDERR_FILENO) == -1)
862 PERROR("cs_create_connection 3");
863
864 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 (void)close(to_cs[1]);
866 (void)close(from_cs[0]);
867#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100868 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000869 /* Create pipes to communicate with cscope */
870 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
871 sa.bInheritHandle = TRUE;
872 sa.lpSecurityDescriptor = NULL;
873
874 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
875 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100877 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000878err_closing:
879 if (pipe_stdin)
880 {
881 CloseHandle(stdin_rd);
882 CloseHandle(stdin_wr);
883 }
884 if (pipe_stdout)
885 {
886 CloseHandle(stdout_rd);
887 CloseHandle(stdout_wr);
888 }
889 return CSCOPE_FAILURE;
890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#endif
892 /* expand the cscope exec for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200893 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
895#ifdef UNIX
896 return CSCOPE_FAILURE;
897#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100898 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 goto err_closing;
900#endif
901 }
902 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
903
904 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000905 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (csinfo[i].ppath)
907 {
908 /* expand the prepend path for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200909 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
911 vim_free(prog);
912#ifdef UNIX
913 return CSCOPE_FAILURE;
914#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100915 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 goto err_closing;
917#endif
918 }
919 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
920
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000921 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923
924 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000925 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200927 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 vim_free(prog);
930 vim_free(ppath);
931#ifdef UNIX
932 return CSCOPE_FAILURE;
933#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100934 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 goto err_closing;
936#endif
937 }
938
939 /* run the cscope command; is there execl for non-unix systems? */
940#if defined(UNIX)
941 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
942#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100943 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
945#endif
946 if (csinfo[i].ppath != NULL)
947 {
948 (void)strcat(cmd, " -P");
949 (void)strcat(cmd, csinfo[i].ppath);
950 }
951 if (csinfo[i].flags != NULL)
952 {
953 (void)strcat(cmd, " ");
954 (void)strcat(cmd, csinfo[i].flags);
955 }
956# ifdef UNIX
957 /* on Win32 we still need prog */
958 vim_free(prog);
959# endif
960 vim_free(ppath);
961
962#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200963# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
964 /* Change our process group to avoid cscope receiving SIGWINCH. */
965# if defined(HAVE_SETSID)
966 (void)setsid();
967# else
968 if (setpgid(0, 0) == -1)
969 PERROR(_("cs_create_connection setpgid failed"));
970# endif
971# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000972 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 PERROR(_("cs_create_connection exec failed"));
974
975 exit(127);
976 /* NOTREACHED */
977 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 /*
979 * Save the file descriptors for later duplication, and
980 * reopen as streams.
981 */
982 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
983 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
984 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
985 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 /* close unused */
988 (void)close(to_cs[0]);
989 (void)close(from_cs[1]);
990
991 break;
992 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100995 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000996 /* Create a new process to run cscope and use pipes to talk with it */
997 GetStartupInfo(&si);
998 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
999 si.wShowWindow = SW_HIDE; /* Hide child application window */
1000 si.hStdOutput = stdout_wr;
1001 si.hStdError = stdout_wr;
1002 si.hStdInput = stdin_rd;
1003 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1004 NULL, NULL, &si, &pi);
1005 vim_free(prog);
1006 vim_free(cmd);
1007
1008 if (!created)
1009 {
1010 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001011 (void)emsg(_("E623: Could not spawn cscope process"));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001012 goto err_closing;
1013 }
1014 /* else */
1015 csinfo[i].pid = pi.dwProcessId;
1016 csinfo[i].hProc = pi.hProcess;
1017 CloseHandle(pi.hThread);
1018
1019 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001020 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1021 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001022 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1023 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001024 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1025 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001026 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1027 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1028
1029 /* Close handles for file descriptors inherited by the cscope process */
1030 CloseHandle(stdin_rd);
1031 CloseHandle(stdout_wr);
1032
1033#endif /* !UNIX */
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 return CSCOPE_SUCCESS;
1036} /* cs_create_connection */
1037
1038
1039/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001040 * Query cscope using command line interface. Parse the output and use tselect
1041 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 *
1043 * returns TRUE if we jump to a tag or abort, FALSE if not.
1044 */
1045 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001046cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001049 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 if (cs_check_for_connections() == FALSE)
1052 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 (void)emsg(_("E567: no cscope connections"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 return FALSE;
1055 }
1056
1057 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1058 {
1059 cs_usage_msg(Find);
1060 return FALSE;
1061 }
1062
1063 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001064 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
1066 cs_usage_msg(Find);
1067 return FALSE;
1068 }
1069
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001070 /*
1071 * Let's replace the NULs written by strtok() with spaces - we need the
1072 * spaces to correctly display the quickfix/location list window's title.
1073 */
1074 for (i = 0; i < eap_arg_len; ++i)
1075 if (NUL == eap->arg[i])
1076 eap->arg[i] = ' ';
1077
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001078 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001079 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080} /* cs_find */
1081
1082
1083/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001084 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
1086 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001087cs_find_common(
1088 char *opt,
1089 char *pat,
1090 int forceit,
1091 int verbose,
1092 int use_ll UNUSED,
1093 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 int i;
1096 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001097 int *nummatches;
1098 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#ifdef FEAT_QUICKFIX
1100 char cmdletter;
1101 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001102
1103 /* get cmd letter */
1104 switch (opt[0])
1105 {
1106 case '0' :
1107 cmdletter = 's';
1108 break;
1109 case '1' :
1110 cmdletter = 'g';
1111 break;
1112 case '2' :
1113 cmdletter = 'd';
1114 break;
1115 case '3' :
1116 cmdletter = 'c';
1117 break;
1118 case '4' :
1119 cmdletter = 't';
1120 break;
1121 case '6' :
1122 cmdletter = 'e';
1123 break;
1124 case '7' :
1125 cmdletter = 'f';
1126 break;
1127 case '8' :
1128 cmdletter = 'i';
1129 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001130 case '9' :
1131 cmdletter = 'a';
1132 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001133 default :
1134 cmdletter = opt[0];
1135 }
1136
1137 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1138 if (qfpos != NULL)
1139 {
1140 qfpos++;
1141 /* next symbol must be + or - */
1142 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1143 {
1144 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001145 char *buf = alloc(strlen(nf));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001146
1147 /* strlen will be enough because we use chars */
1148 if (buf != NULL)
1149 {
1150 sprintf(buf, nf, *qfpos, *(qfpos-1));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001151 (void)emsg(buf);
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001152 vim_free(buf);
1153 }
1154 return FALSE;
1155 }
1156
Bram Moolenaar21662be2016-11-06 14:46:44 +01001157 if (*qfpos != '0'
1158 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1159 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001160 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001161# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001162 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001163 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001164# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001165 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167#endif
1168
1169 /* create the actual command to send to cscope */
1170 cmd = cs_create_cmd(opt, pat);
1171 if (cmd == NULL)
1172 return FALSE;
1173
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001174 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001175 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001176 {
1177 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001178 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001179 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001180
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001181 /* Send query to all open connections, then count the total number
1182 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001183 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 nummatches[i] = 0;
1185 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001186 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001188 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 continue;
1190
1191 /* send cmd to cscope */
1192 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1193 (void)fflush(csinfo[i].to_fp);
1194
1195 nummatches[i] = cs_cnt_matches(i);
1196
1197 if (nummatches[i] > -1)
1198 totmatches += nummatches[i];
1199
1200 if (nummatches[i] == 0)
1201 (void)cs_read_prompt(i);
1202 }
1203 vim_free(cmd);
1204
1205 if (totmatches == 0)
1206 {
1207 char *nf = _("E259: no matches found for cscope query %s of %s");
1208 char *buf;
1209
1210 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001211 {
1212 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001216 buf = alloc(strlen(opt) + strlen(pat) + strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 (void)emsg(nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
1220 {
1221 sprintf(buf, nf, opt, pat);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 vim_free(buf);
1224 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001225 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 return FALSE;
1227 }
1228
1229#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001230 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001233 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001234 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001235 qf_info_T *qi = NULL;
1236 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001238 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001239 if (f == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001240 semsg(_(e_notopen), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001243 cs_file_results(f, nummatches);
1244 fclose(f);
1245 if (use_ll) /* Use location list */
1246 wp = curwin;
1247 /* '-' starts a new error list */
1248 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001249 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001251 if (postponed_split != 0)
1252 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001253 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001255 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001256 postponed_split = 0;
1257 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001258
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001259 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1260 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001261 if (use_ll)
1262 /*
1263 * In the location list window, use the displayed location
1264 * list. Otherwise, use the location list for the window.
1265 */
1266 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1267 ? wp->w_llist_ref : wp->w_llist;
1268 qf_jump(qi, 0, 0, forceit);
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 mch_remove(tmp);
1272 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001273 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 return TRUE;
1275 }
1276 else
1277#endif /* FEAT_QUICKFIX */
1278 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001279 char **matches = NULL, **contexts = NULL;
1280 int matched = 0;
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 /* read output */
1283 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1284 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001285 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (matches == NULL)
1287 return FALSE;
1288
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001289 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1292 }
1293
1294} /* cs_find_common */
1295
1296/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001297 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001300cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 cscmd_T *cmdp = cs_cmds;
1303
Bram Moolenaar32526b32019-01-19 17:43:09 +01001304 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 while (cmdp->name != NULL)
1306 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001307 char *help = _(cmdp->help);
1308 int space_cnt = 30 - vim_strsize((char_u *)help);
1309
1310 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1311 if (space_cnt < 0)
1312 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001313 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001314 cmdp->name,
1315 help, space_cnt, " ",
1316 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001318 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001319 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001320 " c: Find functions calling this function\n"
1321 " d: Find functions called by this function\n"
1322 " e: Find this egrep pattern\n"
1323 " f: Find this file\n"
1324 " g: Find this definition\n"
1325 " i: Find files #including this file\n"
1326 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001327 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 cmdp++;
1330 }
1331
1332 wait_return(TRUE);
1333 return 0;
1334} /* cs_help */
1335
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001338clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 csinfo[i].fname = NULL;
1341 csinfo[i].ppath = NULL;
1342 csinfo[i].flags = NULL;
1343#if defined(UNIX)
1344 csinfo[i].st_dev = (dev_t)0;
1345 csinfo[i].st_ino = (ino_t)0;
1346#else
1347 csinfo[i].nVolume = 0;
1348 csinfo[i].nIndexHigh = 0;
1349 csinfo[i].nIndexLow = 0;
1350#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001351 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 csinfo[i].fr_fp = NULL;
1353 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001354#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001355 csinfo[i].hProc = NULL;
1356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357}
1358
1359#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001361GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 char *msg = NULL;
1364 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1365 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1366 if (msg != NULL)
1367 {
1368 /* remove trailing \r\n */
1369 char *pcrlf = strstr(msg, "\r\n");
1370 if (pcrlf != NULL)
1371 *pcrlf = '\0';
1372 }
1373 return msg;
1374}
1375#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001378 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
1380 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001381cs_insert_filelist(
1382 char *fname,
1383 char *ppath,
1384 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001385 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 short i, j;
1388#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 BY_HANDLE_FILE_INFORMATION bhfi;
1390
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001391 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001393 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1394 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (p_csverbose)
1396 {
1397 char *cant_msg = _("E625: cannot open cscope database: %s");
1398 char *winmsg = GetWin32Error();
1399
1400 if (winmsg != NULL)
1401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 LocalFree(winmsg);
1404 }
1405 else
1406 /* subst filename if can't get error text */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001407 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001410
1411 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001413 (void)emsg(_("E626: cannot get cscope database information"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416#endif
1417
1418 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001419 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
1421 if (csinfo[j].fname != NULL
1422#if defined(UNIX)
1423 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1424#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001425 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001426 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001427 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1428 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001429 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1431 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1432#endif
1433 )
1434 {
1435 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001436 (void)emsg(_("E568: duplicate cscope database not added"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 return -1;
1438 }
1439
1440 if (csinfo[j].fname == NULL && i == -1)
1441 i = j; /* remember first empty entry */
1442 }
1443
1444 if (i == -1)
1445 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001446 i = csinfo_size;
1447 if (csinfo_size == 0)
1448 {
1449 /* First time allocation: allocate only 1 connection. It should
1450 * be enough for most users. If more is needed, csinfo will be
1451 * reallocated. */
1452 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001453 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001454 }
1455 else
1456 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001457 csinfo_T *t_csinfo = csinfo;
1458
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001459 /* Reallocate space for more connections. */
1460 csinfo_size *= 2;
1461 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001462 if (csinfo == NULL)
1463 {
1464 vim_free(t_csinfo);
1465 csinfo_size = 0;
1466 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001467 }
1468 if (csinfo == NULL)
1469 return -1;
1470 for (j = csinfo_size/2; j < csinfo_size; j++)
1471 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001474 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return -1;
1476
1477 (void)strcpy(csinfo[i].fname, (const char *)fname);
1478
1479 if (ppath != NULL)
1480 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001481 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001483 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 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 Moolenaarc799fe22019-05-28 23:08:19 +02001492 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001494 VIM_CLEAR(csinfo[i].fname);
1495 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return -1;
1497 }
1498 (void)strcpy(csinfo[i].flags, (const char *)flags);
1499 } else
1500 csinfo[i].flags = NULL;
1501
1502#if defined(UNIX)
1503 csinfo[i].st_dev = sb->st_dev;
1504 csinfo[i].st_ino = sb->st_ino;
1505
1506#else
1507 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1508 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1509 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1510#endif
1511 return i;
1512} /* cs_insert_filelist */
1513
1514
1515/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001516 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 */
1518 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001519cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 cscmd_T *cmdp;
1522 char *stok;
1523 size_t len;
1524
1525 if (eap->arg == NULL)
1526 return NULL;
1527
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001528 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001529 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1532 return NULL;
1533
1534 len = strlen(stok);
1535 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1536 {
1537 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1538 return (cmdp);
1539 }
1540 return NULL;
1541} /* cs_lookup_cmd */
1542
1543
1544/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001545 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001548cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 char *stok;
1551 short i;
1552
1553 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1554 {
1555 cs_usage_msg(Kill);
1556 return CSCOPE_FAILURE;
1557 }
1558
1559 /* only single digit positive and negative integers are allowed */
1560 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1561 || (strlen(stok) < 3 && stok[0] == '-'
1562 && VIM_ISDIGIT((int)(stok[1]))))
1563 i = atoi(stok);
1564 else
1565 {
1566 /* It must be part of a name. We will try to find a match
1567 * within all the names in the csinfo data structure
1568 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001569 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1572 break;
1573 }
1574 }
1575
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001576 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 (void)semsg(_("E261: cscope connection %s not found"), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 else
1582 {
1583 if (i == -1)
1584 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001585 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 if (csinfo[i].fname)
1588 cs_kill_execute(i, csinfo[i].fname);
1589 }
1590 }
1591 else
1592 cs_kill_execute(i, stok);
1593 }
1594
1595 return 0;
1596} /* cs_kill */
1597
1598
1599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * Actually kills a specific cscope connection.
1601 */
1602 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001603cs_kill_execute(
1604 int i, /* cscope table index */
1605 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 if (p_csverbose)
1608 {
1609 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001610 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001611 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 cs_release_csp(i, TRUE);
1614}
1615
1616
1617/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001618 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1620 * the type of the tag you are looking for. for example, in Darren Hiebert's
1621 * ctags (the one that comes with vim), #define's use a line number to find the
1622 * tag in a file while function definitions use a regexp search pattern.
1623 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001624 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 * quirky (and probably other things i don't know about):
1626 *
1627 * if you have "# define" in your source file, which is
1628 * perfectly legal, cscope thinks you have "#define". this
1629 * will result in a failed regexp search. :(
1630 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001631 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 * would still have to be modified to escape all the special regular expression
1633 * characters to comply with ctags formatting.
1634 */
1635 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001636cs_make_vim_style_matches(
1637 char *fname,
1638 char *slno,
1639 char *search,
1640 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 /* vim style is ctags:
1643 *
1644 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1645 *
1646 * but as mentioned above, we'll always use the line number and
1647 * put the search pattern (if one exists) as "extra"
1648 *
1649 * buf is used as part of vim's method of handling tags, and
1650 * (i think) vim frees it when you pop your tags and get replaced
1651 * by new ones on the tag stack.
1652 */
1653 char *buf;
1654 int amt;
1655
1656 if (search != NULL)
1657 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001658 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001659 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 return NULL;
1661
1662 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1663 }
1664 else
1665 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001666 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001667 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 return NULL;
1669
1670 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1671 }
1672
1673 return buf;
1674} /* cs_make_vim_style_matches */
1675
1676
1677/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001678 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
1680 * Store: keep a ptr to the (malloc'd) memory of matches originally
1681 * generated from cs_find(). the matches are originally lines directly
1682 * from cscope output, but transformed to look like something out of a
1683 * ctags. see cs_make_vim_style_matches for more details.
1684 *
1685 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1686 * the next line from the cscope output. it basically keeps track of which
1687 * lines have been "used" and returns the next one.
1688 *
1689 * Free: frees up everything and resets
1690 *
1691 * Print: prints the tags
1692 */
1693 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001694cs_manage_matches(
1695 char **matches,
1696 char **contexts,
1697 int totmatches,
1698 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 static char **mp = NULL;
1701 static char **cp = NULL;
1702 static int cnt = -1;
1703 static int next = -1;
1704 char *p = NULL;
1705
1706 switch (cmd)
1707 {
1708 case Store:
1709 assert(matches != NULL);
1710 assert(totmatches > 0);
1711 if (mp != NULL || cp != NULL)
1712 (void)cs_manage_matches(NULL, NULL, -1, Free);
1713 mp = matches;
1714 cp = contexts;
1715 cnt = totmatches;
1716 next = 0;
1717 break;
1718 case Get:
1719 if (next >= cnt)
1720 return NULL;
1721
1722 p = mp[next];
1723 next++;
1724 break;
1725 case Free:
1726 if (mp != NULL)
1727 {
1728 if (cnt > 0)
1729 while (cnt--)
1730 {
1731 vim_free(mp[cnt]);
1732 if (cp != NULL)
1733 vim_free(cp[cnt]);
1734 }
1735 vim_free(mp);
1736 vim_free(cp);
1737 }
1738 mp = NULL;
1739 cp = NULL;
1740 cnt = 0;
1741 next = 0;
1742 break;
1743 case Print:
1744 cs_print_tags_priv(mp, cp, cnt);
1745 break;
1746 default: /* should not reach here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001747 iemsg(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 return NULL;
1749 }
1750
1751 return p;
1752} /* cs_manage_matches */
1753
1754
1755/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001756 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
1758 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001759cs_parse_results(
1760 int cnumber,
1761 char *buf,
1762 int bufsize,
1763 char **context,
1764 char **linenumber,
1765 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int ch;
1768 char *p;
1769 char *name;
1770
1771 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1772 {
1773 if (feof(csinfo[cnumber].fr_fp))
1774 errno = EIO;
1775
1776 cs_reading_emsg(cnumber);
1777
1778 return NULL;
1779 }
1780
1781 /* If the line's too long for the buffer, discard it. */
1782 if ((p = strchr(buf, '\n')) == NULL)
1783 {
1784 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1785 ;
1786 return NULL;
1787 }
1788 *p = '\0';
1789
1790 /*
1791 * cscope output is in the following format:
1792 *
1793 * <filename> <context> <line number> <pattern>
1794 */
1795 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1796 return NULL;
1797 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1798 return NULL;
1799 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1800 return NULL;
1801 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1802
1803 /* --- nvi ---
1804 * If the file is older than the cscope database, that is,
1805 * the database was built since the file was last modified,
1806 * or there wasn't a search string, use the line number.
1807 */
1808 if (strcmp(*search, "<unknown>") == 0)
1809 *search = NULL;
1810
1811 name = cs_resolve_file(cnumber, name);
1812 return name;
1813}
1814
Bram Moolenaarc716c302006-01-21 22:12:51 +00001815#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001817 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 */
1819 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001820cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 int i, j;
1823 char *buf;
1824 char *search, *slno;
1825 char *fullname;
1826 char *cntx;
1827 char *context;
1828
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001829 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (buf == NULL)
1831 return;
1832
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001833 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 if (nummatches_a[i] < 1)
1836 continue;
1837
1838 for (j = 0; j < nummatches_a[i]; j++)
1839 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001840 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1841 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 continue;
1843
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001844 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001845 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 continue;
1847
1848 if (strcmp(cntx, "<global>")==0)
1849 strcpy(context, "<<global>>");
1850 else
1851 sprintf(context, "<<%s>>", cntx);
1852
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001853 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1855 else
1856 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1857
1858 vim_free(context);
1859 vim_free(fullname);
1860 } /* for all matches */
1861
1862 (void)cs_read_prompt(i);
1863
1864 } /* for all cscope connections */
1865 vim_free(buf);
1866}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001870 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1871 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001872 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 */
1874 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001875cs_fill_results(
1876 char *tagstr,
1877 int totmatches,
1878 int *nummatches_a,
1879 char ***matches_p,
1880 char ***cntxts_p,
1881 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882{
1883 int i, j;
1884 char *buf;
1885 char *search, *slno;
1886 int totsofar = 0;
1887 char **matches = NULL;
1888 char **cntxts = NULL;
1889 char *fullname;
1890 char *cntx;
1891
1892 assert(totmatches > 0);
1893
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001894 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (buf == NULL)
1896 return;
1897
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001898 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001900 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 goto parse_out;
1902
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001903 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 if (nummatches_a[i] < 1)
1906 continue;
1907
1908 for (j = 0; j < nummatches_a[i]; j++)
1909 {
1910 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1911 &slno, &search)) == NULL)
1912 continue;
1913
1914 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1915 search, tagstr);
1916
1917 vim_free(fullname);
1918
1919 if (strcmp(cntx, "<global>") == 0)
1920 cntxts[totsofar] = NULL;
1921 else
1922 /* note: if vim_strsave returns NULL, then the context
1923 * will be "<global>", which is misleading.
1924 */
1925 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1926
1927 if (matches[totsofar] != NULL)
1928 totsofar++;
1929
1930 } /* for all matches */
1931
1932 (void)cs_read_prompt(i);
1933
1934 } /* for all cscope connections */
1935
1936parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001937 if (totsofar == 0)
1938 {
1939 /* No matches, free the arrays and return NULL in "*matches_p". */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001940 VIM_CLEAR(matches);
1941 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 *matched = totsofar;
1944 *matches_p = matches;
1945 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 vim_free(buf);
1948} /* cs_fill_results */
1949
1950
1951/* get the requested path components */
1952 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001953cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955 int i;
1956 char *s;
1957
1958 if (p_cspc == 0)
1959 return path;
1960
1961 s = path + strlen(path) - 1;
1962 for (i = 0; i < p_cspc; ++i)
1963 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001964#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 && *--s != '\\'
1966#endif
1967 )
1968 ;
1969 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001970#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 || (s > path && *s == '\\')
1972#endif
1973 )
1974 ++s;
1975 return s;
1976}
1977
1978/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001979 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
1981 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001982cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001985 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 int bufsize = 0; /* Track available bufsize */
1987 int newsize = 0;
1988 char *ptag;
1989 char *fname, *lno, *extra, *tbuf;
1990 int i, idx, num;
1991 char *globalcntx = "GLOBAL";
1992 char *cntxformat = " <<%s>>";
1993 char *context;
1994 char *cstag_msg = _("Cscope tag: %s");
1995 char *csfmt_str = "%4d %6s ";
1996
Bram Moolenaar4033c552017-09-16 20:54:51 +02001997 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001999 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return;
2001
2002 strcpy(tbuf, matches[0]);
2003 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02002004 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002005 {
2006 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02002007 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01002008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002010 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002011 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (buf != NULL)
2013 {
2014 bufsize = newsize;
2015 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002016 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
2019 vim_free(tbuf);
2020
Bram Moolenaar32526b32019-01-19 17:43:09 +01002021 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); /* strlen is 7 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002023 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 num = 1;
2026 for (i = 0; i < num_matches; i++)
2027 {
2028 idx = i;
2029
2030 /* if we really wanted to, we could avoid this malloc and strcpy
2031 * by parsing matches[i] on the fly and placing stuff into buf
2032 * directly, but that's too much of a hassle
2033 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002034 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 continue;
2036 (void)strcpy(tbuf, matches[idx]);
2037
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002038 if (strtok(tbuf, (const char *)"\t") == NULL
2039 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2040 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2041 {
2042 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002044 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002045 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2048
2049 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002050 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (bufsize < newsize)
2052 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002053 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002054 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002058 vim_free(t_buf);
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 else
2061 bufsize = newsize;
2062 }
2063 if (buf != NULL)
2064 {
2065 /* csfmt_str = "%4d %6s "; */
2066 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002067 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002069 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2070 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 /* compute the required space for the context */
2073 if (cntxts[idx] != NULL)
2074 context = cntxts[idx];
2075 else
2076 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 if (bufsize < newsize)
2080 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002081 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002082 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002086 vim_free(t_buf);
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 else
2089 bufsize = newsize;
2090 }
2091 if (buf != NULL)
2092 {
2093 (void)sprintf(buf, cntxformat, context);
2094
2095 /* print the context only if it fits on the same line */
2096 if (msg_col + (int)strlen(buf) >= (int)Columns)
2097 msg_putchar('\n');
2098 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002099 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 msg_putchar('\n');
2101 }
2102 if (extra != NULL)
2103 {
2104 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002105 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107
2108 vim_free(tbuf); /* only after printing extra due to strtok use */
2109
2110 if (msg_col)
2111 msg_putchar('\n');
2112
2113 ui_breakcheck();
2114 if (got_int)
2115 {
2116 got_int = FALSE; /* don't print any more matches */
2117 break;
2118 }
2119
2120 num++;
2121 } /* for all matches */
2122
2123 vim_free(buf);
2124} /* cs_print_tags_priv */
2125
2126
2127/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002128 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 */
2130 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002131cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 int ch;
2134 char *buf = NULL; /* buffer for possible error message from cscope */
2135 int bufpos = 0;
2136 char *cs_emsg;
2137 int maxlen;
2138 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002139 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 int n;
2141
2142 cs_emsg = _("E609: Cscope error: %s");
2143 /* compute maximum allowed len for Cscope error message */
2144 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2145
2146 for (;;)
2147 {
2148 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2149 /* if there is room and char is printable */
2150 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2151 {
2152 if (buf == NULL) /* lazy buffer allocation */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002153 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (buf != NULL)
2155 {
2156 /* append character to the message */
2157 buf[bufpos++] = ch;
2158 buf[bufpos] = NUL;
2159 if (bufpos >= epromptlen
2160 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2161 {
2162 /* remove eprompt from buf */
2163 buf[bufpos - epromptlen] = NUL;
2164
2165 /* print message to user */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168 /* send RETURN to cscope */
2169 (void)putc('\n', csinfo[i].to_fp);
2170 (void)fflush(csinfo[i].to_fp);
2171
2172 /* clear buf */
2173 bufpos = 0;
2174 buf[bufpos] = NUL;
2175 }
2176 }
2177 }
2178
2179 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2180 {
2181 if (n > 0)
2182 ch = getc(csinfo[i].fr_fp);
2183 if (ch == EOF)
2184 {
2185 PERROR("cs_read_prompt EOF");
2186 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002187 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 else if (p_csverbose)
2189 cs_reading_emsg(i); /* don't have additional information */
2190 cs_release_csp(i, TRUE);
2191 vim_free(buf);
2192 return CSCOPE_FAILURE;
2193 }
2194
2195 if (ch != CSCOPE_PROMPT[n])
2196 {
2197 ch = EOF;
2198 break;
2199 }
2200 }
2201
2202 if (ch == EOF)
2203 continue; /* didn't find the prompt */
2204 break; /* did find the prompt */
2205 }
2206
2207 vim_free(buf);
2208 return CSCOPE_SUCCESS;
2209}
2210
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002211#if defined(UNIX) && defined(SIGALRM)
2212/*
2213 * Used to catch and ignore SIGALRM below.
2214 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002215 static RETSIGTYPE
2216sig_handler SIGDEFARG(sigarg)
2217{
2218 /* do nothing */
2219 SIGRETURN;
2220}
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002224 * Does the actual free'ing for the cs ptr with an optional flag of whether
2225 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
2227 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002228cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 /*
2231 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2232 */
2233 if (csinfo[i].to_fp != NULL)
2234 {
2235 (void)fputs("q\n", csinfo[i].to_fp);
2236 (void)fflush(csinfo[i].to_fp);
2237 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002238#if defined(UNIX)
2239 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002240 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002241 int pstat;
2242 pid_t pid;
2243
2244# if defined(HAVE_SIGACTION)
2245 struct sigaction sa, old;
2246
Bram Moolenaar9701da02008-03-16 12:09:58 +00002247 /* Use sigaction() to limit the waiting time to two seconds. */
2248 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002249 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002250# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002251 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002252# else
2253 sa.sa_flags = 0;
2254# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255 sigaction(SIGALRM, &sa, &old);
2256 alarm(2); /* 2 sec timeout */
2257
2258 /* Block until cscope exits or until timer expires */
2259 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002260 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002261
2262 /* cancel pending alarm if still there and restore signal */
2263 alarm(0);
2264 sigaction(SIGALRM, &old, NULL);
2265# else
2266 int waited;
2267
2268 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2269 * to give cscope a chance to exit quickly. */
2270 sleep(0);
2271 for (waited = 0; waited < 40; ++waited)
2272 {
2273 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002274 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002275 if (pid != 0)
2276 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002277 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002278 }
2279# endif
2280 /*
2281 * If the cscope process is still running: kill it.
2282 * Safety check: If the PID would be zero here, the entire X session
2283 * would be killed. -1 and 1 are dangerous as well.
2284 */
2285 if (pid < 0 && csinfo[i].pid > 1)
2286 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002287# ifdef ECHILD
2288 int alive = TRUE;
2289
2290 if (waitpid_errno == ECHILD)
2291 {
2292 /*
2293 * When using 'vim -g', vim is forked and cscope process is
2294 * no longer a child process but a sibling. So waitpid()
2295 * fails with errno being ECHILD (No child processes).
2296 * Don't send SIGKILL to cscope immediately but wait
2297 * (polling) for it to exit normally as result of sending
2298 * the "q" command, hence giving it a chance to clean up
2299 * its temporary files.
2300 */
2301 int waited;
2302
2303 sleep(0);
2304 for (waited = 0; waited < 40; ++waited)
2305 {
2306 /* Check whether cscope process is still alive */
2307 if (kill(csinfo[i].pid, 0) != 0)
2308 {
2309 alive = FALSE; /* cscope process no longer exists */
2310 break;
2311 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002312 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002313 }
2314 }
2315 if (alive)
2316# endif
2317 {
2318 kill(csinfo[i].pid, SIGKILL);
2319 (void)waitpid(csinfo[i].pid, &pstat, 0);
2320 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002321 }
2322 }
2323#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002324 if (csinfo[i].hProc != NULL)
2325 {
2326 /* Give cscope a chance to exit normally */
2327 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2328 TerminateProcess(csinfo[i].hProc, 0);
2329 CloseHandle(csinfo[i].hProc);
2330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332
2333 if (csinfo[i].fr_fp != NULL)
2334 (void)fclose(csinfo[i].fr_fp);
2335 if (csinfo[i].to_fp != NULL)
2336 (void)fclose(csinfo[i].to_fp);
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (freefnpp)
2339 {
2340 vim_free(csinfo[i].fname);
2341 vim_free(csinfo[i].ppath);
2342 vim_free(csinfo[i].flags);
2343 }
2344
2345 clear_csinfo(i);
2346} /* cs_release_csp */
2347
2348
2349/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002350 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002353cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2356 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002357 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002359 if (csinfo_size == 0)
2360 return CSCOPE_SUCCESS;
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /* malloc our db and ppath list */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002363 dblist = ALLOC_MULT(char *, csinfo_size);
2364 pplist = ALLOC_MULT(char *, csinfo_size);
2365 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (dblist == NULL || pplist == NULL || fllist == NULL)
2367 {
2368 vim_free(dblist);
2369 vim_free(pplist);
2370 vim_free(fllist);
2371 return CSCOPE_FAILURE;
2372 }
2373
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002374 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 dblist[i] = csinfo[i].fname;
2377 pplist[i] = csinfo[i].ppath;
2378 fllist[i] = csinfo[i].flags;
2379 if (csinfo[i].fname != NULL)
2380 cs_release_csp(i, FALSE);
2381 }
2382
2383 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002384 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 if (dblist[i] != NULL)
2387 {
2388 cs_add_common(dblist[i], pplist[i], fllist[i]);
2389 if (p_csverbose)
2390 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002391 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * connection number in the same line as
2393 * "Added cscope database..."
2394 */
2395 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002396 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398 }
2399 vim_free(dblist[i]);
2400 vim_free(pplist[i]);
2401 vim_free(fllist[i]);
2402 }
2403 vim_free(dblist);
2404 vim_free(pplist);
2405 vim_free(fllist);
2406
2407 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002408 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return CSCOPE_SUCCESS;
2410} /* cs_reset */
2411
2412
2413/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002414 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 * (Prepends ppath, if there is one and if it's not already prepended,
2416 * otherwise just uses the name found.)
2417 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002418 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002420 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 */
2422 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002423cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002425 char *fullname;
2426 int len;
2427 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002430 * Ppath is freed when we destroy the cscope connection.
2431 * Fullname is freed after cs_make_vim_style_matches, after it's been
2432 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002434 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002436 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002437 else if (p_csre && csinfo[i].fname != NULL)
2438 {
2439 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2440 * path in path resolution. */
2441 csdir = alloc(MAXPATHL);
2442 if (csdir != NULL)
2443 {
2444 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002445 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002446 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002447 len += (int)STRLEN(csdir);
2448 }
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002451 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002453 * happens, you are screwed up and need to fix how you're using cscope. */
2454 if (csinfo[i].ppath != NULL
2455 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2456 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002457#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002458 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002460 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002461 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002462 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002463 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2464 }
2465 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002466 {
2467 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002468 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002469 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002472 {
2473 fullname = (char *)vim_strsave((char_u *)name);
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002476 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480
2481/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002482 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002485cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 short i;
2488 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002489 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 else
2491 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002492 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002494 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002495 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 if (csinfo[i].fname == NULL)
2498 continue;
2499
2500 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002501 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2503 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002504 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 i, (long)csinfo[i].pid, csinfo[i].fname);
2506 }
2507 }
2508
2509 wait_return(TRUE);
2510 return CSCOPE_SUCCESS;
2511} /* cs_show */
2512
Bram Moolenaar02b06312007-09-06 15:39:22 +00002513
2514/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002515 * Only called when VIM exits to quit any cscope sessions.
2516 */
2517 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002518cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002519{
2520 int i;
2521
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002522 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002523 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002524 vim_free(csinfo);
2525 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002526}
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#endif /* FEAT_CSCOPE */
2529
2530/* the end */