blob: 2cc25b7439d31ffbc50d3e1e065d854aafc91297 [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)
387 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;
433} /* cs_connection */
434#endif
435
436
437/*
438 * PRIVATE functions
439 ****************************************************************************/
440
441/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100442 * Add cscope database or a directory name (to look for cscope.out)
443 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100446cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
448 char *fname, *ppath, *flags = NULL;
449
450 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
451 {
452 cs_usage_msg(Add);
453 return CSCOPE_FAILURE;
454 }
455 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
456 flags = strtok((char *)NULL, (const char *)" ");
457
458 return cs_add_common(fname, ppath, flags);
459}
460
461 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100462cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463{
464 char *stat_emsg = _("E563: stat(%s) error: %d");
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200465 char *buf = alloc(strlen(stat_emsg) + MAXPATHL + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467 if (buf != NULL)
468 {
469 (void)sprintf(buf, stat_emsg, fname, errno);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100470 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 vim_free(buf);
472 }
473 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100474 (void)emsg(_("E563: stat error"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475}
476
477
478/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100479 * The common routine to add a new cscope connection. Called by
480 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 * routine uses a number of goto statements.
482 */
483 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100484cs_add_common(
485 char *arg1, /* filename - may contain environment variables */
486 char *arg2, /* prepend path - may contain environment variables */
487 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200489 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000490 int ret;
491 char *fname = NULL;
492 char *fname2 = NULL;
493 char *ppath = NULL;
494 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200495 int len;
496 int usedlen = 0;
497 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
499 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200500 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 goto add_err;
502
503 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200504 len = (int)STRLEN(fname);
505 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200506 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200507 (char_u **)&fname, &fbuf, &len);
508 if (fname == NULL)
509 goto add_err;
510 fname = (char *)vim_strnsave((char_u *)fname, len);
511 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200512
Bram Moolenaar8767f522016-07-01 17:17:39 +0200513 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ret < 0)
515 {
516staterr:
517 if (p_csverbose)
518 cs_stat_emsg(fname);
519 goto add_err;
520 }
521
522 /* get the prepend path (arg2), expand it, and try to stat it */
523 if (arg2 != NULL)
524 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200525 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200527 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 goto add_err;
529
530 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200531 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (ret < 0)
533 goto staterr;
534 }
535
536 /* if filename is a directory, append the cscope database name to it */
Bram Moolenaard569bb02018-08-11 13:57:20 +0200537 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200539 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (fname2 == NULL)
541 goto add_err;
542
543 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100544#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 || fname[strlen(fname)-1] == '\\'
546#endif
547 )
548 {
549 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200550 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 break;
552 }
553 if (fname[0] == '\0')
554 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
555 else
556 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
557
Bram Moolenaar8767f522016-07-01 17:17:39 +0200558 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (ret < 0)
560 {
561 if (p_csverbose)
562 cs_stat_emsg(fname2);
563 goto add_err;
564 }
565
566 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
571 }
572 else
573 {
574 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100575 (void)semsg(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 _("E564: %s is not a directory or a valid cscope database"),
577 fname);
578 goto add_err;
579 }
580
581 if (i != -1)
582 {
583 if (cs_create_connection(i) == CSCOPE_FAILURE
584 || cs_read_prompt(i) == CSCOPE_FAILURE)
585 {
586 cs_release_csp(i, TRUE);
587 goto add_err;
588 }
589
590 if (p_csverbose)
591 {
592 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100593 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100594 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 csinfo[i].fname);
596 }
597 }
598
599 vim_free(fname);
600 vim_free(fname2);
601 vim_free(ppath);
602 return CSCOPE_SUCCESS;
603
604add_err:
605 vim_free(fname2);
606 vim_free(fname);
607 vim_free(ppath);
608 return CSCOPE_FAILURE;
609} /* cs_add_common */
610
611
612 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100613cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 return (cs_cnt_connections() > 0);
616} /* cs_check_for_connections */
617
618
619 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100620cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000622 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623} /* cs_check_for_tags */
624
625
626/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100627 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 */
629 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100630cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 short i;
633 short cnt = 0;
634
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000635 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 if (csinfo[i].fname != NULL)
638 cnt++;
639 }
640 return cnt;
641} /* cs_cnt_connections */
642
643 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100644cs_reading_emsg(
645 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100647 semsg(_("E262: error reading cscope connection %d"), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648}
649
650#define CSREAD_BUFSIZE 2048
651/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100652 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
654 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100655cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 char *stok;
658 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100659 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200661 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (buf == NULL)
663 return 0;
664 for (;;)
665 {
666 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
667 {
668 if (feof(csinfo[idx].fr_fp))
669 errno = EIO;
670
671 cs_reading_emsg(idx);
672
673 vim_free(buf);
674 return -1;
675 }
676
677 /*
678 * If the database is out of date, or there's some other problem,
679 * cscope will output error messages before the number-of-lines output.
680 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000681 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100682 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100684 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if ((stok = strtok(buf, (const char *)" ")) == NULL)
687 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000688 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 continue;
690
691 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
692 continue;
693 nlines = atoi(stok);
694 if (nlines < 0)
695 {
696 nlines = 0;
697 break;
698 }
699
700 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
701 continue;
702 if (strncmp((const char *)stok, "lines", 5))
703 continue;
704
705 break;
706 }
707
708 vim_free(buf);
709 return nlines;
710} /* cs_cnt_matches */
711
712
713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 * Creates the actual cscope command query from what the user entered.
715 */
716 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100717cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 char *cmd;
720 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000721 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
723 switch (csoption[0])
724 {
725 case '0' : case 's' :
726 search = 0;
727 break;
728 case '1' : case 'g' :
729 search = 1;
730 break;
731 case '2' : case 'd' :
732 search = 2;
733 break;
734 case '3' : case 'c' :
735 search = 3;
736 break;
737 case '4' : case 't' :
738 search = 4;
739 break;
740 case '6' : case 'e' :
741 search = 6;
742 break;
743 case '7' : case 'f' :
744 search = 7;
745 break;
746 case '8' : case 'i' :
747 search = 8;
748 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200749 case '9' : case 'a' :
750 search = 9;
751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 default :
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 (void)emsg(_("E561: unknown cscope search type"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 cs_usage_msg(Find);
755 return NULL;
756 }
757
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000758 /* Skip white space before the patter, except for text and pattern search,
759 * they may want to use the leading white space. */
760 pat = pattern;
761 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100762 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000763 ++pat;
764
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200765 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 return NULL;
767
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000768 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 return cmd;
771} /* cs_create_cmd */
772
773
774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * This piece of code was taken/adapted from nvi. do we need to add
776 * the BSD license notice?
777 */
778 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100779cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000781#ifdef UNIX
782 int to_cs[2], from_cs[2];
783#endif
784 int len;
785 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100786#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000787 int fd;
788 SECURITY_ATTRIBUTES sa;
789 PROCESS_INFORMATION pi;
790 STARTUPINFO si;
791 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
792 HANDLE stdin_rd, stdout_rd;
793 HANDLE stdout_wr, stdin_wr;
794 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200795# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
796# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000797# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#endif
801
Bram Moolenaar02b06312007-09-06 15:39:22 +0000802#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /*
804 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
805 * from_cs[0] and writes to to_cs[1].
806 */
807 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
808 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100810 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811err_closing:
812 if (to_cs[0] != -1)
813 (void)close(to_cs[0]);
814 if (to_cs[1] != -1)
815 (void)close(to_cs[1]);
816 if (from_cs[0] != -1)
817 (void)close(from_cs[0]);
818 if (from_cs[1] != -1)
819 (void)close(from_cs[1]);
820 return CSCOPE_FAILURE;
821 }
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 switch (csinfo[i].pid = fork())
824 {
825 case -1:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 (void)emsg(_("E622: Could not fork for cscope"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 goto err_closing;
828 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (dup2(to_cs[0], STDIN_FILENO) == -1)
830 PERROR("cs_create_connection 1");
831 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
832 PERROR("cs_create_connection 2");
833 if (dup2(from_cs[1], STDERR_FILENO) == -1)
834 PERROR("cs_create_connection 3");
835
836 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 (void)close(to_cs[1]);
838 (void)close(from_cs[0]);
839#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100840 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000841 /* Create pipes to communicate with cscope */
842 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
843 sa.bInheritHandle = TRUE;
844 sa.lpSecurityDescriptor = NULL;
845
846 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
847 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100849 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000850err_closing:
851 if (pipe_stdin)
852 {
853 CloseHandle(stdin_rd);
854 CloseHandle(stdin_wr);
855 }
856 if (pipe_stdout)
857 {
858 CloseHandle(stdout_rd);
859 CloseHandle(stdout_wr);
860 }
861 return CSCOPE_FAILURE;
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#endif
864 /* expand the cscope exec for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200865 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
867#ifdef UNIX
868 return CSCOPE_FAILURE;
869#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100870 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 goto err_closing;
872#endif
873 }
874 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
875
876 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000877 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (csinfo[i].ppath)
879 {
880 /* expand the prepend path for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200881 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
883 vim_free(prog);
884#ifdef UNIX
885 return CSCOPE_FAILURE;
886#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100887 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 goto err_closing;
889#endif
890 }
891 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
892
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000893 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
896 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000897 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200899 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
901 vim_free(prog);
902 vim_free(ppath);
903#ifdef UNIX
904 return CSCOPE_FAILURE;
905#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100906 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 goto err_closing;
908#endif
909 }
910
911 /* run the cscope command; is there execl for non-unix systems? */
912#if defined(UNIX)
913 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
914#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100915 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
917#endif
918 if (csinfo[i].ppath != NULL)
919 {
920 (void)strcat(cmd, " -P");
921 (void)strcat(cmd, csinfo[i].ppath);
922 }
923 if (csinfo[i].flags != NULL)
924 {
925 (void)strcat(cmd, " ");
926 (void)strcat(cmd, csinfo[i].flags);
927 }
928# ifdef UNIX
929 /* on Win32 we still need prog */
930 vim_free(prog);
931# endif
932 vim_free(ppath);
933
934#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200935# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
936 /* Change our process group to avoid cscope receiving SIGWINCH. */
937# if defined(HAVE_SETSID)
938 (void)setsid();
939# else
940 if (setpgid(0, 0) == -1)
941 PERROR(_("cs_create_connection setpgid failed"));
942# endif
943# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000944 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 PERROR(_("cs_create_connection exec failed"));
946
947 exit(127);
948 /* NOTREACHED */
949 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 /*
951 * Save the file descriptors for later duplication, and
952 * reopen as streams.
953 */
954 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
955 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
956 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
957 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /* close unused */
960 (void)close(to_cs[0]);
961 (void)close(from_cs[1]);
962
963 break;
964 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100967 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000968 /* Create a new process to run cscope and use pipes to talk with it */
969 GetStartupInfo(&si);
970 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
971 si.wShowWindow = SW_HIDE; /* Hide child application window */
972 si.hStdOutput = stdout_wr;
973 si.hStdError = stdout_wr;
974 si.hStdInput = stdin_rd;
975 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
976 NULL, NULL, &si, &pi);
977 vim_free(prog);
978 vim_free(cmd);
979
980 if (!created)
981 {
982 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 (void)emsg(_("E623: Could not spawn cscope process"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000984 goto err_closing;
985 }
986 /* else */
987 csinfo[i].pid = pi.dwProcessId;
988 csinfo[i].hProc = pi.hProcess;
989 CloseHandle(pi.hThread);
990
991 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000992 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
993 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000994 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
995 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000996 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
997 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000998 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
999 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1000
1001 /* Close handles for file descriptors inherited by the cscope process */
1002 CloseHandle(stdin_rd);
1003 CloseHandle(stdout_wr);
1004
1005#endif /* !UNIX */
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return CSCOPE_SUCCESS;
1008} /* cs_create_connection */
1009
1010
1011/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001012 * Query cscope using command line interface. Parse the output and use tselect
1013 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 *
1015 * returns TRUE if we jump to a tag or abort, FALSE if not.
1016 */
1017 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001018cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
1020 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001021 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 if (cs_check_for_connections() == FALSE)
1024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 (void)emsg(_("E567: no cscope connections"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 return FALSE;
1027 }
1028
1029 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1030 {
1031 cs_usage_msg(Find);
1032 return FALSE;
1033 }
1034
1035 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001036 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
1038 cs_usage_msg(Find);
1039 return FALSE;
1040 }
1041
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001042 /*
1043 * Let's replace the NULs written by strtok() with spaces - we need the
1044 * spaces to correctly display the quickfix/location list window's title.
1045 */
1046 for (i = 0; i < eap_arg_len; ++i)
1047 if (NUL == eap->arg[i])
1048 eap->arg[i] = ' ';
1049
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001050 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001051 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052} /* cs_find */
1053
1054
1055/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001056 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
1058 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001059cs_find_common(
1060 char *opt,
1061 char *pat,
1062 int forceit,
1063 int verbose,
1064 int use_ll UNUSED,
1065 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
1067 int i;
1068 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001069 int *nummatches;
1070 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071#ifdef FEAT_QUICKFIX
1072 char cmdletter;
1073 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001074
1075 /* get cmd letter */
1076 switch (opt[0])
1077 {
1078 case '0' :
1079 cmdletter = 's';
1080 break;
1081 case '1' :
1082 cmdletter = 'g';
1083 break;
1084 case '2' :
1085 cmdletter = 'd';
1086 break;
1087 case '3' :
1088 cmdletter = 'c';
1089 break;
1090 case '4' :
1091 cmdletter = 't';
1092 break;
1093 case '6' :
1094 cmdletter = 'e';
1095 break;
1096 case '7' :
1097 cmdletter = 'f';
1098 break;
1099 case '8' :
1100 cmdletter = 'i';
1101 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001102 case '9' :
1103 cmdletter = 'a';
1104 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001105 default :
1106 cmdletter = opt[0];
1107 }
1108
1109 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1110 if (qfpos != NULL)
1111 {
1112 qfpos++;
1113 /* next symbol must be + or - */
1114 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1115 {
1116 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001117 char *buf = alloc(strlen(nf));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001118
1119 /* strlen will be enough because we use chars */
1120 if (buf != NULL)
1121 {
1122 sprintf(buf, nf, *qfpos, *(qfpos-1));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001123 (void)emsg(buf);
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001124 vim_free(buf);
1125 }
1126 return FALSE;
1127 }
1128
Bram Moolenaar21662be2016-11-06 14:46:44 +01001129 if (*qfpos != '0'
1130 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1131 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001132 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001133# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001134 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001135 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001136# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001137 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140
1141 /* create the actual command to send to cscope */
1142 cmd = cs_create_cmd(opt, pat);
1143 if (cmd == NULL)
1144 return FALSE;
1145
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001146 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001147 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001148 {
1149 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001150 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001151 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001152
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001153 /* Send query to all open connections, then count the total number
1154 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001155 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 nummatches[i] = 0;
1157 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001158 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001160 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 continue;
1162
1163 /* send cmd to cscope */
1164 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1165 (void)fflush(csinfo[i].to_fp);
1166
1167 nummatches[i] = cs_cnt_matches(i);
1168
1169 if (nummatches[i] > -1)
1170 totmatches += nummatches[i];
1171
1172 if (nummatches[i] == 0)
1173 (void)cs_read_prompt(i);
1174 }
1175 vim_free(cmd);
1176
1177 if (totmatches == 0)
1178 {
1179 char *nf = _("E259: no matches found for cscope query %s of %s");
1180 char *buf;
1181
1182 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001183 {
1184 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001188 buf = alloc(strlen(opt) + strlen(pat) + strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 (void)emsg(nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 else
1192 {
1193 sprintf(buf, nf, opt, pat);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 vim_free(buf);
1196 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001197 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 return FALSE;
1199 }
1200
1201#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001202 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
1204 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001205 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001206 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001207 qf_info_T *qi = NULL;
1208 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001210 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001211 if (f == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 semsg(_(e_notopen), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001215 cs_file_results(f, nummatches);
1216 fclose(f);
1217 if (use_ll) /* Use location list */
1218 wp = curwin;
1219 /* '-' starts a new error list */
1220 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001221 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001223 if (postponed_split != 0)
1224 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001225 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001227 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001228 postponed_split = 0;
1229 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001230
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001231 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1232 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001233 if (use_ll)
1234 /*
1235 * In the location list window, use the displayed location
1236 * list. Otherwise, use the location list for the window.
1237 */
1238 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1239 ? wp->w_llist_ref : wp->w_llist;
1240 qf_jump(qi, 0, 0, forceit);
1241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 mch_remove(tmp);
1244 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001245 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 return TRUE;
1247 }
1248 else
1249#endif /* FEAT_QUICKFIX */
1250 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001251 char **matches = NULL, **contexts = NULL;
1252 int matched = 0;
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 /* read output */
1255 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1256 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001257 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (matches == NULL)
1259 return FALSE;
1260
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001261 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1264 }
1265
1266} /* cs_find_common */
1267
1268/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001269 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001272cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
1274 cscmd_T *cmdp = cs_cmds;
1275
Bram Moolenaar32526b32019-01-19 17:43:09 +01001276 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 while (cmdp->name != NULL)
1278 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001279 char *help = _(cmdp->help);
1280 int space_cnt = 30 - vim_strsize((char_u *)help);
1281
1282 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1283 if (space_cnt < 0)
1284 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001285 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001286 cmdp->name,
1287 help, space_cnt, " ",
1288 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001290 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001291 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001292 " c: Find functions calling this function\n"
1293 " d: Find functions called by this function\n"
1294 " e: Find this egrep pattern\n"
1295 " f: Find this file\n"
1296 " g: Find this definition\n"
1297 " i: Find files #including this file\n"
1298 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001299 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 cmdp++;
1302 }
1303
1304 wait_return(TRUE);
1305 return 0;
1306} /* cs_help */
1307
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001310clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 csinfo[i].fname = NULL;
1313 csinfo[i].ppath = NULL;
1314 csinfo[i].flags = NULL;
1315#if defined(UNIX)
1316 csinfo[i].st_dev = (dev_t)0;
1317 csinfo[i].st_ino = (ino_t)0;
1318#else
1319 csinfo[i].nVolume = 0;
1320 csinfo[i].nIndexHigh = 0;
1321 csinfo[i].nIndexLow = 0;
1322#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001323 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 csinfo[i].fr_fp = NULL;
1325 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001326#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001327 csinfo[i].hProc = NULL;
1328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329}
1330
1331#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001333GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 char *msg = NULL;
1336 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1337 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1338 if (msg != NULL)
1339 {
1340 /* remove trailing \r\n */
1341 char *pcrlf = strstr(msg, "\r\n");
1342 if (pcrlf != NULL)
1343 *pcrlf = '\0';
1344 }
1345 return msg;
1346}
1347#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001350 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 */
1352 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001353cs_insert_filelist(
1354 char *fname,
1355 char *ppath,
1356 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001357 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 short i, j;
1360#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 BY_HANDLE_FILE_INFORMATION bhfi;
1362
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001363 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001365 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1366 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (p_csverbose)
1368 {
1369 char *cant_msg = _("E625: cannot open cscope database: %s");
1370 char *winmsg = GetWin32Error();
1371
1372 if (winmsg != NULL)
1373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001374 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 LocalFree(winmsg);
1376 }
1377 else
1378 /* subst filename if can't get error text */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001379 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001382
1383 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001385 (void)emsg(_("E626: cannot get cscope database information"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388#endif
1389
1390 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001391 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
1393 if (csinfo[j].fname != NULL
1394#if defined(UNIX)
1395 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1396#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001397 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001398 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001399 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1400 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001401 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1403 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1404#endif
1405 )
1406 {
1407 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001408 (void)emsg(_("E568: duplicate cscope database not added"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 return -1;
1410 }
1411
1412 if (csinfo[j].fname == NULL && i == -1)
1413 i = j; /* remember first empty entry */
1414 }
1415
1416 if (i == -1)
1417 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001418 i = csinfo_size;
1419 if (csinfo_size == 0)
1420 {
1421 /* First time allocation: allocate only 1 connection. It should
1422 * be enough for most users. If more is needed, csinfo will be
1423 * reallocated. */
1424 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001425 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001426 }
1427 else
1428 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001429 csinfo_T *t_csinfo = csinfo;
1430
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001431 /* Reallocate space for more connections. */
1432 csinfo_size *= 2;
1433 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001434 if (csinfo == NULL)
1435 {
1436 vim_free(t_csinfo);
1437 csinfo_size = 0;
1438 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001439 }
1440 if (csinfo == NULL)
1441 return -1;
1442 for (j = csinfo_size/2; j < csinfo_size; j++)
1443 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001446 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 return -1;
1448
1449 (void)strcpy(csinfo[i].fname, (const char *)fname);
1450
1451 if (ppath != NULL)
1452 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001453 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001455 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 return -1;
1457 }
1458 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1459 } else
1460 csinfo[i].ppath = NULL;
1461
1462 if (flags != NULL)
1463 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001464 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001466 VIM_CLEAR(csinfo[i].fname);
1467 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 return -1;
1469 }
1470 (void)strcpy(csinfo[i].flags, (const char *)flags);
1471 } else
1472 csinfo[i].flags = NULL;
1473
1474#if defined(UNIX)
1475 csinfo[i].st_dev = sb->st_dev;
1476 csinfo[i].st_ino = sb->st_ino;
1477
1478#else
1479 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1480 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1481 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1482#endif
1483 return i;
1484} /* cs_insert_filelist */
1485
1486
1487/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001488 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 */
1490 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001491cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 cscmd_T *cmdp;
1494 char *stok;
1495 size_t len;
1496
1497 if (eap->arg == NULL)
1498 return NULL;
1499
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001500 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001501 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1504 return NULL;
1505
1506 len = strlen(stok);
1507 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1508 {
1509 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1510 return (cmdp);
1511 }
1512 return NULL;
1513} /* cs_lookup_cmd */
1514
1515
1516/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001517 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001520cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 char *stok;
1523 short i;
1524
1525 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1526 {
1527 cs_usage_msg(Kill);
1528 return CSCOPE_FAILURE;
1529 }
1530
1531 /* only single digit positive and negative integers are allowed */
1532 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1533 || (strlen(stok) < 3 && stok[0] == '-'
1534 && VIM_ISDIGIT((int)(stok[1]))))
1535 i = atoi(stok);
1536 else
1537 {
1538 /* It must be part of a name. We will try to find a match
1539 * within all the names in the csinfo data structure
1540 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001541 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1544 break;
1545 }
1546 }
1547
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001548 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
1550 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 (void)semsg(_("E261: cscope connection %s not found"), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553 else
1554 {
1555 if (i == -1)
1556 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001557 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559 if (csinfo[i].fname)
1560 cs_kill_execute(i, csinfo[i].fname);
1561 }
1562 }
1563 else
1564 cs_kill_execute(i, stok);
1565 }
1566
1567 return 0;
1568} /* cs_kill */
1569
1570
1571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 * Actually kills a specific cscope connection.
1573 */
1574 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001575cs_kill_execute(
1576 int i, /* cscope table index */
1577 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 if (p_csverbose)
1580 {
1581 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001582 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001583 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 cs_release_csp(i, TRUE);
1586}
1587
1588
1589/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001590 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1592 * the type of the tag you are looking for. for example, in Darren Hiebert's
1593 * ctags (the one that comes with vim), #define's use a line number to find the
1594 * tag in a file while function definitions use a regexp search pattern.
1595 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001596 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 * quirky (and probably other things i don't know about):
1598 *
1599 * if you have "# define" in your source file, which is
1600 * perfectly legal, cscope thinks you have "#define". this
1601 * will result in a failed regexp search. :(
1602 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001603 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 * would still have to be modified to escape all the special regular expression
1605 * characters to comply with ctags formatting.
1606 */
1607 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001608cs_make_vim_style_matches(
1609 char *fname,
1610 char *slno,
1611 char *search,
1612 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 /* vim style is ctags:
1615 *
1616 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1617 *
1618 * but as mentioned above, we'll always use the line number and
1619 * put the search pattern (if one exists) as "extra"
1620 *
1621 * buf is used as part of vim's method of handling tags, and
1622 * (i think) vim frees it when you pop your tags and get replaced
1623 * by new ones on the tag stack.
1624 */
1625 char *buf;
1626 int amt;
1627
1628 if (search != NULL)
1629 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001630 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001631 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 return NULL;
1633
1634 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1635 }
1636 else
1637 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001638 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001639 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return NULL;
1641
1642 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1643 }
1644
1645 return buf;
1646} /* cs_make_vim_style_matches */
1647
1648
1649/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001650 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *
1652 * Store: keep a ptr to the (malloc'd) memory of matches originally
1653 * generated from cs_find(). the matches are originally lines directly
1654 * from cscope output, but transformed to look like something out of a
1655 * ctags. see cs_make_vim_style_matches for more details.
1656 *
1657 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1658 * the next line from the cscope output. it basically keeps track of which
1659 * lines have been "used" and returns the next one.
1660 *
1661 * Free: frees up everything and resets
1662 *
1663 * Print: prints the tags
1664 */
1665 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001666cs_manage_matches(
1667 char **matches,
1668 char **contexts,
1669 int totmatches,
1670 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 static char **mp = NULL;
1673 static char **cp = NULL;
1674 static int cnt = -1;
1675 static int next = -1;
1676 char *p = NULL;
1677
1678 switch (cmd)
1679 {
1680 case Store:
1681 assert(matches != NULL);
1682 assert(totmatches > 0);
1683 if (mp != NULL || cp != NULL)
1684 (void)cs_manage_matches(NULL, NULL, -1, Free);
1685 mp = matches;
1686 cp = contexts;
1687 cnt = totmatches;
1688 next = 0;
1689 break;
1690 case Get:
1691 if (next >= cnt)
1692 return NULL;
1693
1694 p = mp[next];
1695 next++;
1696 break;
1697 case Free:
1698 if (mp != NULL)
1699 {
1700 if (cnt > 0)
1701 while (cnt--)
1702 {
1703 vim_free(mp[cnt]);
1704 if (cp != NULL)
1705 vim_free(cp[cnt]);
1706 }
1707 vim_free(mp);
1708 vim_free(cp);
1709 }
1710 mp = NULL;
1711 cp = NULL;
1712 cnt = 0;
1713 next = 0;
1714 break;
1715 case Print:
1716 cs_print_tags_priv(mp, cp, cnt);
1717 break;
1718 default: /* should not reach here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001719 iemsg(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return NULL;
1721 }
1722
1723 return p;
1724} /* cs_manage_matches */
1725
1726
1727/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001728 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
1730 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001731cs_parse_results(
1732 int cnumber,
1733 char *buf,
1734 int bufsize,
1735 char **context,
1736 char **linenumber,
1737 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 int ch;
1740 char *p;
1741 char *name;
1742
1743 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1744 {
1745 if (feof(csinfo[cnumber].fr_fp))
1746 errno = EIO;
1747
1748 cs_reading_emsg(cnumber);
1749
1750 return NULL;
1751 }
1752
1753 /* If the line's too long for the buffer, discard it. */
1754 if ((p = strchr(buf, '\n')) == NULL)
1755 {
1756 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1757 ;
1758 return NULL;
1759 }
1760 *p = '\0';
1761
1762 /*
1763 * cscope output is in the following format:
1764 *
1765 * <filename> <context> <line number> <pattern>
1766 */
1767 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1768 return NULL;
1769 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1770 return NULL;
1771 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1772 return NULL;
1773 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1774
1775 /* --- nvi ---
1776 * If the file is older than the cscope database, that is,
1777 * the database was built since the file was last modified,
1778 * or there wasn't a search string, use the line number.
1779 */
1780 if (strcmp(*search, "<unknown>") == 0)
1781 *search = NULL;
1782
1783 name = cs_resolve_file(cnumber, name);
1784 return name;
1785}
1786
Bram Moolenaarc716c302006-01-21 22:12:51 +00001787#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001789 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001792cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 int i, j;
1795 char *buf;
1796 char *search, *slno;
1797 char *fullname;
1798 char *cntx;
1799 char *context;
1800
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001801 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (buf == NULL)
1803 return;
1804
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001805 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 if (nummatches_a[i] < 1)
1808 continue;
1809
1810 for (j = 0; j < nummatches_a[i]; j++)
1811 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001812 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1813 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 continue;
1815
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001816 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001817 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 continue;
1819
1820 if (strcmp(cntx, "<global>")==0)
1821 strcpy(context, "<<global>>");
1822 else
1823 sprintf(context, "<<%s>>", cntx);
1824
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001825 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1827 else
1828 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1829
1830 vim_free(context);
1831 vim_free(fullname);
1832 } /* for all matches */
1833
1834 (void)cs_read_prompt(i);
1835
1836 } /* for all cscope connections */
1837 vim_free(buf);
1838}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001842 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1843 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001844 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001847cs_fill_results(
1848 char *tagstr,
1849 int totmatches,
1850 int *nummatches_a,
1851 char ***matches_p,
1852 char ***cntxts_p,
1853 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854{
1855 int i, j;
1856 char *buf;
1857 char *search, *slno;
1858 int totsofar = 0;
1859 char **matches = NULL;
1860 char **cntxts = NULL;
1861 char *fullname;
1862 char *cntx;
1863
1864 assert(totmatches > 0);
1865
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001866 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (buf == NULL)
1868 return;
1869
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001870 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001872 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 goto parse_out;
1874
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001875 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 if (nummatches_a[i] < 1)
1878 continue;
1879
1880 for (j = 0; j < nummatches_a[i]; j++)
1881 {
1882 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1883 &slno, &search)) == NULL)
1884 continue;
1885
1886 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1887 search, tagstr);
1888
1889 vim_free(fullname);
1890
1891 if (strcmp(cntx, "<global>") == 0)
1892 cntxts[totsofar] = NULL;
1893 else
1894 /* note: if vim_strsave returns NULL, then the context
1895 * will be "<global>", which is misleading.
1896 */
1897 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1898
1899 if (matches[totsofar] != NULL)
1900 totsofar++;
1901
1902 } /* for all matches */
1903
1904 (void)cs_read_prompt(i);
1905
1906 } /* for all cscope connections */
1907
1908parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001909 if (totsofar == 0)
1910 {
1911 /* No matches, free the arrays and return NULL in "*matches_p". */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001912 VIM_CLEAR(matches);
1913 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 *matched = totsofar;
1916 *matches_p = matches;
1917 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 vim_free(buf);
1920} /* cs_fill_results */
1921
1922
1923/* get the requested path components */
1924 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001925cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
1927 int i;
1928 char *s;
1929
1930 if (p_cspc == 0)
1931 return path;
1932
1933 s = path + strlen(path) - 1;
1934 for (i = 0; i < p_cspc; ++i)
1935 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001936#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 && *--s != '\\'
1938#endif
1939 )
1940 ;
1941 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001942#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 || (s > path && *s == '\\')
1944#endif
1945 )
1946 ++s;
1947 return s;
1948}
1949
1950/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001951 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
1953 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001954cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001957 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 int bufsize = 0; /* Track available bufsize */
1959 int newsize = 0;
1960 char *ptag;
1961 char *fname, *lno, *extra, *tbuf;
1962 int i, idx, num;
1963 char *globalcntx = "GLOBAL";
1964 char *cntxformat = " <<%s>>";
1965 char *context;
1966 char *cstag_msg = _("Cscope tag: %s");
1967 char *csfmt_str = "%4d %6s ";
1968
Bram Moolenaar4033c552017-09-16 20:54:51 +02001969 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001971 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 return;
1973
1974 strcpy(tbuf, matches[0]);
1975 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001976 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001977 {
1978 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001979 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001982 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001983 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (buf != NULL)
1985 {
1986 bufsize = newsize;
1987 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001988 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990
1991 vim_free(tbuf);
1992
Bram Moolenaar32526b32019-01-19 17:43:09 +01001993 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); /* strlen is 7 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997 num = 1;
1998 for (i = 0; i < num_matches; i++)
1999 {
2000 idx = i;
2001
2002 /* if we really wanted to, we could avoid this malloc and strcpy
2003 * by parsing matches[i] on the fly and placing stuff into buf
2004 * directly, but that's too much of a hassle
2005 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002006 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 continue;
2008 (void)strcpy(tbuf, matches[idx]);
2009
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002010 if (strtok(tbuf, (const char *)"\t") == NULL
2011 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2012 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2013 {
2014 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002016 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002017 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2020
2021 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002022 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (bufsize < newsize)
2024 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002025 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002026 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002030 vim_free(t_buf);
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 else
2033 bufsize = newsize;
2034 }
2035 if (buf != NULL)
2036 {
2037 /* csfmt_str = "%4d %6s "; */
2038 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002039 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002041 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2042 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 /* compute the required space for the context */
2045 if (cntxts[idx] != NULL)
2046 context = cntxts[idx];
2047 else
2048 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002049 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 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 (void)sprintf(buf, cntxformat, context);
2066
2067 /* print the context only if it fits on the same line */
2068 if (msg_col + (int)strlen(buf) >= (int)Columns)
2069 msg_putchar('\n');
2070 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002071 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 msg_putchar('\n');
2073 }
2074 if (extra != NULL)
2075 {
2076 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002077 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080 vim_free(tbuf); /* only after printing extra due to strtok use */
2081
2082 if (msg_col)
2083 msg_putchar('\n');
2084
2085 ui_breakcheck();
2086 if (got_int)
2087 {
2088 got_int = FALSE; /* don't print any more matches */
2089 break;
2090 }
2091
2092 num++;
2093 } /* for all matches */
2094
2095 vim_free(buf);
2096} /* cs_print_tags_priv */
2097
2098
2099/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002100 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 */
2102 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002103cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 int ch;
2106 char *buf = NULL; /* buffer for possible error message from cscope */
2107 int bufpos = 0;
2108 char *cs_emsg;
2109 int maxlen;
2110 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 int n;
2113
2114 cs_emsg = _("E609: Cscope error: %s");
2115 /* compute maximum allowed len for Cscope error message */
2116 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2117
2118 for (;;)
2119 {
2120 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2121 /* if there is room and char is printable */
2122 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2123 {
2124 if (buf == NULL) /* lazy buffer allocation */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002125 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (buf != NULL)
2127 {
2128 /* append character to the message */
2129 buf[bufpos++] = ch;
2130 buf[bufpos] = NUL;
2131 if (bufpos >= epromptlen
2132 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2133 {
2134 /* remove eprompt from buf */
2135 buf[bufpos - epromptlen] = NUL;
2136
2137 /* print message to user */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 /* send RETURN to cscope */
2141 (void)putc('\n', csinfo[i].to_fp);
2142 (void)fflush(csinfo[i].to_fp);
2143
2144 /* clear buf */
2145 bufpos = 0;
2146 buf[bufpos] = NUL;
2147 }
2148 }
2149 }
2150
2151 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2152 {
2153 if (n > 0)
2154 ch = getc(csinfo[i].fr_fp);
2155 if (ch == EOF)
2156 {
2157 PERROR("cs_read_prompt EOF");
2158 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002159 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 else if (p_csverbose)
2161 cs_reading_emsg(i); /* don't have additional information */
2162 cs_release_csp(i, TRUE);
2163 vim_free(buf);
2164 return CSCOPE_FAILURE;
2165 }
2166
2167 if (ch != CSCOPE_PROMPT[n])
2168 {
2169 ch = EOF;
2170 break;
2171 }
2172 }
2173
2174 if (ch == EOF)
2175 continue; /* didn't find the prompt */
2176 break; /* did find the prompt */
2177 }
2178
2179 vim_free(buf);
2180 return CSCOPE_SUCCESS;
2181}
2182
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002183#if defined(UNIX) && defined(SIGALRM)
2184/*
2185 * Used to catch and ignore SIGALRM below.
2186 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002187 static RETSIGTYPE
2188sig_handler SIGDEFARG(sigarg)
2189{
2190 /* do nothing */
2191 SIGRETURN;
2192}
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002196 * Does the actual free'ing for the cs ptr with an optional flag of whether
2197 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 */
2199 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002200cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /*
2203 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2204 */
2205 if (csinfo[i].to_fp != NULL)
2206 {
2207 (void)fputs("q\n", csinfo[i].to_fp);
2208 (void)fflush(csinfo[i].to_fp);
2209 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002210#if defined(UNIX)
2211 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002212 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002213 int pstat;
2214 pid_t pid;
2215
2216# if defined(HAVE_SIGACTION)
2217 struct sigaction sa, old;
2218
Bram Moolenaar9701da02008-03-16 12:09:58 +00002219 /* Use sigaction() to limit the waiting time to two seconds. */
2220 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002221 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002222# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002223 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002224# else
2225 sa.sa_flags = 0;
2226# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002227 sigaction(SIGALRM, &sa, &old);
2228 alarm(2); /* 2 sec timeout */
2229
2230 /* Block until cscope exits or until timer expires */
2231 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002232 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002233
2234 /* cancel pending alarm if still there and restore signal */
2235 alarm(0);
2236 sigaction(SIGALRM, &old, NULL);
2237# else
2238 int waited;
2239
2240 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2241 * to give cscope a chance to exit quickly. */
2242 sleep(0);
2243 for (waited = 0; waited < 40; ++waited)
2244 {
2245 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002246 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002247 if (pid != 0)
2248 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002249 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002250 }
2251# endif
2252 /*
2253 * If the cscope process is still running: kill it.
2254 * Safety check: If the PID would be zero here, the entire X session
2255 * would be killed. -1 and 1 are dangerous as well.
2256 */
2257 if (pid < 0 && csinfo[i].pid > 1)
2258 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002259# ifdef ECHILD
2260 int alive = TRUE;
2261
2262 if (waitpid_errno == ECHILD)
2263 {
2264 /*
2265 * When using 'vim -g', vim is forked and cscope process is
2266 * no longer a child process but a sibling. So waitpid()
2267 * fails with errno being ECHILD (No child processes).
2268 * Don't send SIGKILL to cscope immediately but wait
2269 * (polling) for it to exit normally as result of sending
2270 * the "q" command, hence giving it a chance to clean up
2271 * its temporary files.
2272 */
2273 int waited;
2274
2275 sleep(0);
2276 for (waited = 0; waited < 40; ++waited)
2277 {
2278 /* Check whether cscope process is still alive */
2279 if (kill(csinfo[i].pid, 0) != 0)
2280 {
2281 alive = FALSE; /* cscope process no longer exists */
2282 break;
2283 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002284 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002285 }
2286 }
2287 if (alive)
2288# endif
2289 {
2290 kill(csinfo[i].pid, SIGKILL);
2291 (void)waitpid(csinfo[i].pid, &pstat, 0);
2292 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002293 }
2294 }
2295#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002296 if (csinfo[i].hProc != NULL)
2297 {
2298 /* Give cscope a chance to exit normally */
2299 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2300 TerminateProcess(csinfo[i].hProc, 0);
2301 CloseHandle(csinfo[i].hProc);
2302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304
2305 if (csinfo[i].fr_fp != NULL)
2306 (void)fclose(csinfo[i].fr_fp);
2307 if (csinfo[i].to_fp != NULL)
2308 (void)fclose(csinfo[i].to_fp);
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (freefnpp)
2311 {
2312 vim_free(csinfo[i].fname);
2313 vim_free(csinfo[i].ppath);
2314 vim_free(csinfo[i].flags);
2315 }
2316
2317 clear_csinfo(i);
2318} /* cs_release_csp */
2319
2320
2321/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002322 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002325cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2328 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002329 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002331 if (csinfo_size == 0)
2332 return CSCOPE_SUCCESS;
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 /* malloc our db and ppath list */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002335 dblist = ALLOC_MULT(char *, csinfo_size);
2336 pplist = ALLOC_MULT(char *, csinfo_size);
2337 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (dblist == NULL || pplist == NULL || fllist == NULL)
2339 {
2340 vim_free(dblist);
2341 vim_free(pplist);
2342 vim_free(fllist);
2343 return CSCOPE_FAILURE;
2344 }
2345
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002346 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 dblist[i] = csinfo[i].fname;
2349 pplist[i] = csinfo[i].ppath;
2350 fllist[i] = csinfo[i].flags;
2351 if (csinfo[i].fname != NULL)
2352 cs_release_csp(i, FALSE);
2353 }
2354
2355 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002356 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 if (dblist[i] != NULL)
2359 {
2360 cs_add_common(dblist[i], pplist[i], fllist[i]);
2361 if (p_csverbose)
2362 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002363 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * connection number in the same line as
2365 * "Added cscope database..."
2366 */
2367 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002368 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 }
2371 vim_free(dblist[i]);
2372 vim_free(pplist[i]);
2373 vim_free(fllist[i]);
2374 }
2375 vim_free(dblist);
2376 vim_free(pplist);
2377 vim_free(fllist);
2378
2379 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002380 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return CSCOPE_SUCCESS;
2382} /* cs_reset */
2383
2384
2385/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002386 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 * (Prepends ppath, if there is one and if it's not already prepended,
2388 * otherwise just uses the name found.)
2389 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002390 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002392 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 */
2394 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002395cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002397 char *fullname;
2398 int len;
2399 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002402 * Ppath is freed when we destroy the cscope connection.
2403 * Fullname is freed after cs_make_vim_style_matches, after it's been
2404 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002406 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002408 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002409 else if (p_csre && csinfo[i].fname != NULL)
2410 {
2411 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2412 * path in path resolution. */
2413 csdir = alloc(MAXPATHL);
2414 if (csdir != NULL)
2415 {
2416 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002417 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002418 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002419 len += (int)STRLEN(csdir);
2420 }
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002423 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002425 * happens, you are screwed up and need to fix how you're using cscope. */
2426 if (csinfo[i].ppath != NULL
2427 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2428 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002429#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002430 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002432 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002433 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002434 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002435 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2436 }
2437 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002438 {
2439 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002440 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002441 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002444 {
2445 fullname = (char *)vim_strsave((char_u *)name);
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002448 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002450}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452
2453/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002454 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002457cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 short i;
2460 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002461 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 else
2463 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002464 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002466 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002467 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 if (csinfo[i].fname == NULL)
2470 continue;
2471
2472 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2475 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 i, (long)csinfo[i].pid, csinfo[i].fname);
2478 }
2479 }
2480
2481 wait_return(TRUE);
2482 return CSCOPE_SUCCESS;
2483} /* cs_show */
2484
Bram Moolenaar02b06312007-09-06 15:39:22 +00002485
2486/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002487 * Only called when VIM exits to quit any cscope sessions.
2488 */
2489 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002490cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002491{
2492 int i;
2493
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002494 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002495 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002496 vim_free(csinfo);
2497 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002498}
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500#endif /* FEAT_CSCOPE */
2501
2502/* the end */