blob: 8e3a9bfa10458e5c4909e8ad68430cfae8d56a92 [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#ifdef FEAT_MODIFY_FNAME
496 int len;
497 int usedlen = 0;
498 char_u *fbuf = NULL;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501 /* get the filename (arg1), expand it, and try to stat it */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200502 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 goto add_err;
504
505 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200506#ifdef FEAT_MODIFY_FNAME
507 len = (int)STRLEN(fname);
508 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200509 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200510 (char_u **)&fname, &fbuf, &len);
511 if (fname == NULL)
512 goto add_err;
513 fname = (char *)vim_strnsave((char_u *)fname, len);
514 vim_free(fbuf);
515#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200516 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (ret < 0)
518 {
519staterr:
520 if (p_csverbose)
521 cs_stat_emsg(fname);
522 goto add_err;
523 }
524
525 /* get the prepend path (arg2), expand it, and try to stat it */
526 if (arg2 != NULL)
527 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200528 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200530 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 goto add_err;
532
533 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200534 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (ret < 0)
536 goto staterr;
537 }
538
539 /* if filename is a directory, append the cscope database name to it */
Bram Moolenaard569bb02018-08-11 13:57:20 +0200540 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200542 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (fname2 == NULL)
544 goto add_err;
545
546 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100547#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 || fname[strlen(fname)-1] == '\\'
549#endif
550 )
551 {
552 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200553 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 break;
555 }
556 if (fname[0] == '\0')
557 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
558 else
559 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
560
Bram Moolenaar8767f522016-07-01 17:17:39 +0200561 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (ret < 0)
563 {
564 if (p_csverbose)
565 cs_stat_emsg(fname2);
566 goto add_err;
567 }
568
569 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
574 }
575 else
576 {
577 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100578 (void)semsg(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 _("E564: %s is not a directory or a valid cscope database"),
580 fname);
581 goto add_err;
582 }
583
584 if (i != -1)
585 {
586 if (cs_create_connection(i) == CSCOPE_FAILURE
587 || cs_read_prompt(i) == CSCOPE_FAILURE)
588 {
589 cs_release_csp(i, TRUE);
590 goto add_err;
591 }
592
593 if (p_csverbose)
594 {
595 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100596 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100597 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 csinfo[i].fname);
599 }
600 }
601
602 vim_free(fname);
603 vim_free(fname2);
604 vim_free(ppath);
605 return CSCOPE_SUCCESS;
606
607add_err:
608 vim_free(fname2);
609 vim_free(fname);
610 vim_free(ppath);
611 return CSCOPE_FAILURE;
612} /* cs_add_common */
613
614
615 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100616cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 return (cs_cnt_connections() > 0);
619} /* cs_check_for_connections */
620
621
622 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100623cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000625 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626} /* cs_check_for_tags */
627
628
629/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100630 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 */
632 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100633cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 short i;
636 short cnt = 0;
637
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000638 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 if (csinfo[i].fname != NULL)
641 cnt++;
642 }
643 return cnt;
644} /* cs_cnt_connections */
645
646 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100647cs_reading_emsg(
648 int idx) /* connection index */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100650 semsg(_("E262: error reading cscope connection %d"), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651}
652
653#define CSREAD_BUFSIZE 2048
654/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100655 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 */
657 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100658cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 char *stok;
661 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100662 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200664 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (buf == NULL)
666 return 0;
667 for (;;)
668 {
669 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
670 {
671 if (feof(csinfo[idx].fr_fp))
672 errno = EIO;
673
674 cs_reading_emsg(idx);
675
676 vim_free(buf);
677 return -1;
678 }
679
680 /*
681 * If the database is out of date, or there's some other problem,
682 * cscope will output error messages before the number-of-lines output.
683 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000684 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100685 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100687 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if ((stok = strtok(buf, (const char *)" ")) == NULL)
690 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000691 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 continue;
693
694 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
695 continue;
696 nlines = atoi(stok);
697 if (nlines < 0)
698 {
699 nlines = 0;
700 break;
701 }
702
703 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
704 continue;
705 if (strncmp((const char *)stok, "lines", 5))
706 continue;
707
708 break;
709 }
710
711 vim_free(buf);
712 return nlines;
713} /* cs_cnt_matches */
714
715
716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 * Creates the actual cscope command query from what the user entered.
718 */
719 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100720cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 char *cmd;
723 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000724 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
726 switch (csoption[0])
727 {
728 case '0' : case 's' :
729 search = 0;
730 break;
731 case '1' : case 'g' :
732 search = 1;
733 break;
734 case '2' : case 'd' :
735 search = 2;
736 break;
737 case '3' : case 'c' :
738 search = 3;
739 break;
740 case '4' : case 't' :
741 search = 4;
742 break;
743 case '6' : case 'e' :
744 search = 6;
745 break;
746 case '7' : case 'f' :
747 search = 7;
748 break;
749 case '8' : case 'i' :
750 search = 8;
751 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200752 case '9' : case 'a' :
753 search = 9;
754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 default :
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100756 (void)emsg(_("E561: unknown cscope search type"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 cs_usage_msg(Find);
758 return NULL;
759 }
760
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000761 /* Skip white space before the patter, except for text and pattern search,
762 * they may want to use the leading white space. */
763 pat = pattern;
764 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100765 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000766 ++pat;
767
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200768 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 return NULL;
770
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000771 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773 return cmd;
774} /* cs_create_cmd */
775
776
777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * This piece of code was taken/adapted from nvi. do we need to add
779 * the BSD license notice?
780 */
781 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100782cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000784#ifdef UNIX
785 int to_cs[2], from_cs[2];
786#endif
787 int len;
788 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100789#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000790 int fd;
791 SECURITY_ATTRIBUTES sa;
792 PROCESS_INFORMATION pi;
793 STARTUPINFO si;
794 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
795 HANDLE stdin_rd, stdout_rd;
796 HANDLE stdout_wr, stdin_wr;
797 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
799# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000800# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200801# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803#endif
804
Bram Moolenaar02b06312007-09-06 15:39:22 +0000805#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 /*
807 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
808 * from_cs[0] and writes to to_cs[1].
809 */
810 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
811 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814err_closing:
815 if (to_cs[0] != -1)
816 (void)close(to_cs[0]);
817 if (to_cs[1] != -1)
818 (void)close(to_cs[1]);
819 if (from_cs[0] != -1)
820 (void)close(from_cs[0]);
821 if (from_cs[1] != -1)
822 (void)close(from_cs[1]);
823 return CSCOPE_FAILURE;
824 }
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 switch (csinfo[i].pid = fork())
827 {
828 case -1:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 (void)emsg(_("E622: Could not fork for cscope"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 goto err_closing;
831 case 0: /* child: run cscope. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (dup2(to_cs[0], STDIN_FILENO) == -1)
833 PERROR("cs_create_connection 1");
834 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
835 PERROR("cs_create_connection 2");
836 if (dup2(from_cs[1], STDERR_FILENO) == -1)
837 PERROR("cs_create_connection 3");
838
839 /* close unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 (void)close(to_cs[1]);
841 (void)close(from_cs[0]);
842#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000844 /* Create pipes to communicate with cscope */
845 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
846 sa.bInheritHandle = TRUE;
847 sa.lpSecurityDescriptor = NULL;
848
849 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
850 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100852 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000853err_closing:
854 if (pipe_stdin)
855 {
856 CloseHandle(stdin_rd);
857 CloseHandle(stdin_wr);
858 }
859 if (pipe_stdout)
860 {
861 CloseHandle(stdout_rd);
862 CloseHandle(stdout_wr);
863 }
864 return CSCOPE_FAILURE;
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867 /* expand the cscope exec for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200868 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
870#ifdef UNIX
871 return CSCOPE_FAILURE;
872#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100873 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 goto err_closing;
875#endif
876 }
877 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
878
879 /* alloc space to hold the cscope command */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000880 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (csinfo[i].ppath)
882 {
883 /* expand the prepend path for env var's */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200884 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
886 vim_free(prog);
887#ifdef UNIX
888 return CSCOPE_FAILURE;
889#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100890 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 goto err_closing;
892#endif
893 }
894 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000896 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
899 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000900 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200902 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 vim_free(prog);
905 vim_free(ppath);
906#ifdef UNIX
907 return CSCOPE_FAILURE;
908#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100909 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 goto err_closing;
911#endif
912 }
913
914 /* run the cscope command; is there execl for non-unix systems? */
915#if defined(UNIX)
916 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
917#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100918 /* MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
920#endif
921 if (csinfo[i].ppath != NULL)
922 {
923 (void)strcat(cmd, " -P");
924 (void)strcat(cmd, csinfo[i].ppath);
925 }
926 if (csinfo[i].flags != NULL)
927 {
928 (void)strcat(cmd, " ");
929 (void)strcat(cmd, csinfo[i].flags);
930 }
931# ifdef UNIX
932 /* on Win32 we still need prog */
933 vim_free(prog);
934# endif
935 vim_free(ppath);
936
937#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200938# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
939 /* Change our process group to avoid cscope receiving SIGWINCH. */
940# if defined(HAVE_SETSID)
941 (void)setsid();
942# else
943 if (setpgid(0, 0) == -1)
944 PERROR(_("cs_create_connection setpgid failed"));
945# endif
946# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000947 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 PERROR(_("cs_create_connection exec failed"));
949
950 exit(127);
951 /* NOTREACHED */
952 default: /* parent. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /*
954 * Save the file descriptors for later duplication, and
955 * reopen as streams.
956 */
957 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
958 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
959 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
960 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 /* close unused */
963 (void)close(to_cs[0]);
964 (void)close(from_cs[1]);
965
966 break;
967 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970 /* MSWIN */
Bram Moolenaar02b06312007-09-06 15:39:22 +0000971 /* Create a new process to run cscope and use pipes to talk with it */
972 GetStartupInfo(&si);
973 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
974 si.wShowWindow = SW_HIDE; /* Hide child application window */
975 si.hStdOutput = stdout_wr;
976 si.hStdError = stdout_wr;
977 si.hStdInput = stdin_rd;
978 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
979 NULL, NULL, &si, &pi);
980 vim_free(prog);
981 vim_free(cmd);
982
983 if (!created)
984 {
985 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 (void)emsg(_("E623: Could not spawn cscope process"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000987 goto err_closing;
988 }
989 /* else */
990 csinfo[i].pid = pi.dwProcessId;
991 csinfo[i].hProc = pi.hProcess;
992 CloseHandle(pi.hThread);
993
994 /* TODO - tidy up after failure to create files on pipe handles. */
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000995 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
996 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000997 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
998 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000999 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1000 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001001 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1002 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1003
1004 /* Close handles for file descriptors inherited by the cscope process */
1005 CloseHandle(stdin_rd);
1006 CloseHandle(stdout_wr);
1007
1008#endif /* !UNIX */
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return CSCOPE_SUCCESS;
1011} /* cs_create_connection */
1012
1013
1014/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001015 * Query cscope using command line interface. Parse the output and use tselect
1016 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 *
1018 * returns TRUE if we jump to a tag or abort, FALSE if not.
1019 */
1020 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001021cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001024 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 if (cs_check_for_connections() == FALSE)
1027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001028 (void)emsg(_("E567: no cscope connections"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return FALSE;
1030 }
1031
1032 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1033 {
1034 cs_usage_msg(Find);
1035 return FALSE;
1036 }
1037
1038 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001039 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 cs_usage_msg(Find);
1042 return FALSE;
1043 }
1044
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001045 /*
1046 * Let's replace the NULs written by strtok() with spaces - we need the
1047 * spaces to correctly display the quickfix/location list window's title.
1048 */
1049 for (i = 0; i < eap_arg_len; ++i)
1050 if (NUL == eap->arg[i])
1051 eap->arg[i] = ' ';
1052
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001053 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001054 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055} /* cs_find */
1056
1057
1058/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001059 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 */
1061 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001062cs_find_common(
1063 char *opt,
1064 char *pat,
1065 int forceit,
1066 int verbose,
1067 int use_ll UNUSED,
1068 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
1070 int i;
1071 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001072 int *nummatches;
1073 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_QUICKFIX
1075 char cmdletter;
1076 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001077
1078 /* get cmd letter */
1079 switch (opt[0])
1080 {
1081 case '0' :
1082 cmdletter = 's';
1083 break;
1084 case '1' :
1085 cmdletter = 'g';
1086 break;
1087 case '2' :
1088 cmdletter = 'd';
1089 break;
1090 case '3' :
1091 cmdletter = 'c';
1092 break;
1093 case '4' :
1094 cmdletter = 't';
1095 break;
1096 case '6' :
1097 cmdletter = 'e';
1098 break;
1099 case '7' :
1100 cmdletter = 'f';
1101 break;
1102 case '8' :
1103 cmdletter = 'i';
1104 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001105 case '9' :
1106 cmdletter = 'a';
1107 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001108 default :
1109 cmdletter = opt[0];
1110 }
1111
1112 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1113 if (qfpos != NULL)
1114 {
1115 qfpos++;
1116 /* next symbol must be + or - */
1117 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1118 {
1119 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001120 char *buf = alloc(strlen(nf));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001121
1122 /* strlen will be enough because we use chars */
1123 if (buf != NULL)
1124 {
1125 sprintf(buf, nf, *qfpos, *(qfpos-1));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001126 (void)emsg(buf);
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001127 vim_free(buf);
1128 }
1129 return FALSE;
1130 }
1131
Bram Moolenaar21662be2016-11-06 14:46:44 +01001132 if (*qfpos != '0'
1133 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1134 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001135 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001136# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001137 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001138 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001139# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001140 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 /* create the actual command to send to cscope */
1145 cmd = cs_create_cmd(opt, pat);
1146 if (cmd == NULL)
1147 return FALSE;
1148
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001149 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001150 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001151 {
1152 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001153 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001154 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001155
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001156 /* Send query to all open connections, then count the total number
1157 * of matches so we can alloc all in one swell foop. */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001158 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 nummatches[i] = 0;
1160 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001161 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001163 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 continue;
1165
1166 /* send cmd to cscope */
1167 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1168 (void)fflush(csinfo[i].to_fp);
1169
1170 nummatches[i] = cs_cnt_matches(i);
1171
1172 if (nummatches[i] > -1)
1173 totmatches += nummatches[i];
1174
1175 if (nummatches[i] == 0)
1176 (void)cs_read_prompt(i);
1177 }
1178 vim_free(cmd);
1179
1180 if (totmatches == 0)
1181 {
1182 char *nf = _("E259: no matches found for cscope query %s of %s");
1183 char *buf;
1184
1185 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001186 {
1187 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001191 buf = alloc(strlen(opt) + strlen(pat) + strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001193 (void)emsg(nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 else
1195 {
1196 sprintf(buf, nf, opt, pat);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 vim_free(buf);
1199 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001200 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 return FALSE;
1202 }
1203
1204#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001205 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 /* fill error list */
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001208 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001209 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001210 qf_info_T *qi = NULL;
1211 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001213 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001214 if (f == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 semsg(_(e_notopen), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001218 cs_file_results(f, nummatches);
1219 fclose(f);
1220 if (use_ll) /* Use location list */
1221 wp = curwin;
1222 /* '-' starts a new error list */
1223 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001224 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001226 if (postponed_split != 0)
1227 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001228 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001230 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001231 postponed_split = 0;
1232 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001233
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001234 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1235 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 if (use_ll)
1237 /*
1238 * In the location list window, use the displayed location
1239 * list. Otherwise, use the location list for the window.
1240 */
1241 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1242 ? wp->w_llist_ref : wp->w_llist;
1243 qf_jump(qi, 0, 0, forceit);
1244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246 mch_remove(tmp);
1247 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001248 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 return TRUE;
1250 }
1251 else
1252#endif /* FEAT_QUICKFIX */
1253 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001254 char **matches = NULL, **contexts = NULL;
1255 int matched = 0;
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /* read output */
1258 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1259 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001260 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (matches == NULL)
1262 return FALSE;
1263
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001264 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1267 }
1268
1269} /* cs_find_common */
1270
1271/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001272 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001275cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 cscmd_T *cmdp = cs_cmds;
1278
Bram Moolenaar32526b32019-01-19 17:43:09 +01001279 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 while (cmdp->name != NULL)
1281 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001282 char *help = _(cmdp->help);
1283 int space_cnt = 30 - vim_strsize((char_u *)help);
1284
1285 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1286 if (space_cnt < 0)
1287 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001288 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001289 cmdp->name,
1290 help, space_cnt, " ",
1291 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001293 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001294 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001295 " c: Find functions calling this function\n"
1296 " d: Find functions called by this function\n"
1297 " e: Find this egrep pattern\n"
1298 " f: Find this file\n"
1299 " g: Find this definition\n"
1300 " i: Find files #including this file\n"
1301 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001302 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 cmdp++;
1305 }
1306
1307 wait_return(TRUE);
1308 return 0;
1309} /* cs_help */
1310
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001313clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 csinfo[i].fname = NULL;
1316 csinfo[i].ppath = NULL;
1317 csinfo[i].flags = NULL;
1318#if defined(UNIX)
1319 csinfo[i].st_dev = (dev_t)0;
1320 csinfo[i].st_ino = (ino_t)0;
1321#else
1322 csinfo[i].nVolume = 0;
1323 csinfo[i].nIndexHigh = 0;
1324 csinfo[i].nIndexLow = 0;
1325#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001326 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 csinfo[i].fr_fp = NULL;
1328 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001329#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001330 csinfo[i].hProc = NULL;
1331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332}
1333
1334#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001336GetWin32Error(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 char *msg = NULL;
1339 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1340 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1341 if (msg != NULL)
1342 {
1343 /* remove trailing \r\n */
1344 char *pcrlf = strstr(msg, "\r\n");
1345 if (pcrlf != NULL)
1346 *pcrlf = '\0';
1347 }
1348 return msg;
1349}
1350#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001353 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
1355 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001356cs_insert_filelist(
1357 char *fname,
1358 char *ppath,
1359 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001360 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 short i, j;
1363#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 BY_HANDLE_FILE_INFORMATION bhfi;
1365
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001366 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001368 case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */
1369 case FILEINFO_READ_FAIL: /* CreateFile() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (p_csverbose)
1371 {
1372 char *cant_msg = _("E625: cannot open cscope database: %s");
1373 char *winmsg = GetWin32Error();
1374
1375 if (winmsg != NULL)
1376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001377 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 LocalFree(winmsg);
1379 }
1380 else
1381 /* subst filename if can't get error text */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001385
1386 case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001388 (void)emsg(_("E626: cannot get cscope database information"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391#endif
1392
1393 i = -1; /* can be set to the index of an empty item in csinfo */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001394 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 if (csinfo[j].fname != NULL
1397#if defined(UNIX)
1398 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1399#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001400 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001401 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001402 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1403 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001404 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1406 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1407#endif
1408 )
1409 {
1410 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001411 (void)emsg(_("E568: duplicate cscope database not added"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 return -1;
1413 }
1414
1415 if (csinfo[j].fname == NULL && i == -1)
1416 i = j; /* remember first empty entry */
1417 }
1418
1419 if (i == -1)
1420 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001421 i = csinfo_size;
1422 if (csinfo_size == 0)
1423 {
1424 /* First time allocation: allocate only 1 connection. It should
1425 * be enough for most users. If more is needed, csinfo will be
1426 * reallocated. */
1427 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001428 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001429 }
1430 else
1431 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001432 csinfo_T *t_csinfo = csinfo;
1433
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001434 /* Reallocate space for more connections. */
1435 csinfo_size *= 2;
1436 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001437 if (csinfo == NULL)
1438 {
1439 vim_free(t_csinfo);
1440 csinfo_size = 0;
1441 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001442 }
1443 if (csinfo == NULL)
1444 return -1;
1445 for (j = csinfo_size/2; j < csinfo_size; j++)
1446 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001449 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return -1;
1451
1452 (void)strcpy(csinfo[i].fname, (const char *)fname);
1453
1454 if (ppath != NULL)
1455 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001456 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001458 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return -1;
1460 }
1461 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1462 } else
1463 csinfo[i].ppath = NULL;
1464
1465 if (flags != NULL)
1466 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001467 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001469 VIM_CLEAR(csinfo[i].fname);
1470 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return -1;
1472 }
1473 (void)strcpy(csinfo[i].flags, (const char *)flags);
1474 } else
1475 csinfo[i].flags = NULL;
1476
1477#if defined(UNIX)
1478 csinfo[i].st_dev = sb->st_dev;
1479 csinfo[i].st_ino = sb->st_ino;
1480
1481#else
1482 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1483 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1484 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1485#endif
1486 return i;
1487} /* cs_insert_filelist */
1488
1489
1490/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001491 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 */
1493 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001494cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 cscmd_T *cmdp;
1497 char *stok;
1498 size_t len;
1499
1500 if (eap->arg == NULL)
1501 return NULL;
1502
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001503 /* Store length of eap->arg before it gets modified by strtok(). */
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001504 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1507 return NULL;
1508
1509 len = strlen(stok);
1510 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1511 {
1512 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1513 return (cmdp);
1514 }
1515 return NULL;
1516} /* cs_lookup_cmd */
1517
1518
1519/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001520 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001523cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 char *stok;
1526 short i;
1527
1528 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1529 {
1530 cs_usage_msg(Kill);
1531 return CSCOPE_FAILURE;
1532 }
1533
1534 /* only single digit positive and negative integers are allowed */
1535 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1536 || (strlen(stok) < 3 && stok[0] == '-'
1537 && VIM_ISDIGIT((int)(stok[1]))))
1538 i = atoi(stok);
1539 else
1540 {
1541 /* It must be part of a name. We will try to find a match
1542 * within all the names in the csinfo data structure
1543 */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001544 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
1546 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1547 break;
1548 }
1549 }
1550
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001551 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001554 (void)semsg(_("E261: cscope connection %s not found"), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556 else
1557 {
1558 if (i == -1)
1559 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001560 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 if (csinfo[i].fname)
1563 cs_kill_execute(i, csinfo[i].fname);
1564 }
1565 }
1566 else
1567 cs_kill_execute(i, stok);
1568 }
1569
1570 return 0;
1571} /* cs_kill */
1572
1573
1574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * Actually kills a specific cscope connection.
1576 */
1577 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001578cs_kill_execute(
1579 int i, /* cscope table index */
1580 char *cname) /* cscope database name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 if (p_csverbose)
1583 {
1584 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001585 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001586 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 cs_release_csp(i, TRUE);
1589}
1590
1591
1592/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001593 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1595 * the type of the tag you are looking for. for example, in Darren Hiebert's
1596 * ctags (the one that comes with vim), #define's use a line number to find the
1597 * tag in a file while function definitions use a regexp search pattern.
1598 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001599 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * quirky (and probably other things i don't know about):
1601 *
1602 * if you have "# define" in your source file, which is
1603 * perfectly legal, cscope thinks you have "#define". this
1604 * will result in a failed regexp search. :(
1605 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001606 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * would still have to be modified to escape all the special regular expression
1608 * characters to comply with ctags formatting.
1609 */
1610 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001611cs_make_vim_style_matches(
1612 char *fname,
1613 char *slno,
1614 char *search,
1615 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 /* vim style is ctags:
1618 *
1619 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1620 *
1621 * but as mentioned above, we'll always use the line number and
1622 * put the search pattern (if one exists) as "extra"
1623 *
1624 * buf is used as part of vim's method of handling tags, and
1625 * (i think) vim frees it when you pop your tags and get replaced
1626 * by new ones on the tag stack.
1627 */
1628 char *buf;
1629 int amt;
1630
1631 if (search != NULL)
1632 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001633 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001634 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return NULL;
1636
1637 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1638 }
1639 else
1640 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001641 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001642 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 return NULL;
1644
1645 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1646 }
1647
1648 return buf;
1649} /* cs_make_vim_style_matches */
1650
1651
1652/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001653 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 *
1655 * Store: keep a ptr to the (malloc'd) memory of matches originally
1656 * generated from cs_find(). the matches are originally lines directly
1657 * from cscope output, but transformed to look like something out of a
1658 * ctags. see cs_make_vim_style_matches for more details.
1659 *
1660 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1661 * the next line from the cscope output. it basically keeps track of which
1662 * lines have been "used" and returns the next one.
1663 *
1664 * Free: frees up everything and resets
1665 *
1666 * Print: prints the tags
1667 */
1668 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001669cs_manage_matches(
1670 char **matches,
1671 char **contexts,
1672 int totmatches,
1673 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 static char **mp = NULL;
1676 static char **cp = NULL;
1677 static int cnt = -1;
1678 static int next = -1;
1679 char *p = NULL;
1680
1681 switch (cmd)
1682 {
1683 case Store:
1684 assert(matches != NULL);
1685 assert(totmatches > 0);
1686 if (mp != NULL || cp != NULL)
1687 (void)cs_manage_matches(NULL, NULL, -1, Free);
1688 mp = matches;
1689 cp = contexts;
1690 cnt = totmatches;
1691 next = 0;
1692 break;
1693 case Get:
1694 if (next >= cnt)
1695 return NULL;
1696
1697 p = mp[next];
1698 next++;
1699 break;
1700 case Free:
1701 if (mp != NULL)
1702 {
1703 if (cnt > 0)
1704 while (cnt--)
1705 {
1706 vim_free(mp[cnt]);
1707 if (cp != NULL)
1708 vim_free(cp[cnt]);
1709 }
1710 vim_free(mp);
1711 vim_free(cp);
1712 }
1713 mp = NULL;
1714 cp = NULL;
1715 cnt = 0;
1716 next = 0;
1717 break;
1718 case Print:
1719 cs_print_tags_priv(mp, cp, cnt);
1720 break;
1721 default: /* should not reach here */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001722 iemsg(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 return NULL;
1724 }
1725
1726 return p;
1727} /* cs_manage_matches */
1728
1729
1730/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001731 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 */
1733 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001734cs_parse_results(
1735 int cnumber,
1736 char *buf,
1737 int bufsize,
1738 char **context,
1739 char **linenumber,
1740 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 int ch;
1743 char *p;
1744 char *name;
1745
1746 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1747 {
1748 if (feof(csinfo[cnumber].fr_fp))
1749 errno = EIO;
1750
1751 cs_reading_emsg(cnumber);
1752
1753 return NULL;
1754 }
1755
1756 /* If the line's too long for the buffer, discard it. */
1757 if ((p = strchr(buf, '\n')) == NULL)
1758 {
1759 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1760 ;
1761 return NULL;
1762 }
1763 *p = '\0';
1764
1765 /*
1766 * cscope output is in the following format:
1767 *
1768 * <filename> <context> <line number> <pattern>
1769 */
1770 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1771 return NULL;
1772 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1773 return NULL;
1774 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1775 return NULL;
1776 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1777
1778 /* --- nvi ---
1779 * If the file is older than the cscope database, that is,
1780 * the database was built since the file was last modified,
1781 * or there wasn't a search string, use the line number.
1782 */
1783 if (strcmp(*search, "<unknown>") == 0)
1784 *search = NULL;
1785
1786 name = cs_resolve_file(cnumber, name);
1787 return name;
1788}
1789
Bram Moolenaarc716c302006-01-21 22:12:51 +00001790#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001792 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 */
1794 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001795cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
1797 int i, j;
1798 char *buf;
1799 char *search, *slno;
1800 char *fullname;
1801 char *cntx;
1802 char *context;
1803
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001804 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (buf == NULL)
1806 return;
1807
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001808 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 if (nummatches_a[i] < 1)
1811 continue;
1812
1813 for (j = 0; j < nummatches_a[i]; j++)
1814 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001815 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1816 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 continue;
1818
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001819 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001820 if (context == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 continue;
1822
1823 if (strcmp(cntx, "<global>")==0)
1824 strcpy(context, "<<global>>");
1825 else
1826 sprintf(context, "<<%s>>", cntx);
1827
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001828 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1830 else
1831 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1832
1833 vim_free(context);
1834 vim_free(fullname);
1835 } /* for all matches */
1836
1837 (void)cs_read_prompt(i);
1838
1839 } /* for all cscope connections */
1840 vim_free(buf);
1841}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001845 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1846 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001847 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001850cs_fill_results(
1851 char *tagstr,
1852 int totmatches,
1853 int *nummatches_a,
1854 char ***matches_p,
1855 char ***cntxts_p,
1856 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int i, j;
1859 char *buf;
1860 char *search, *slno;
1861 int totsofar = 0;
1862 char **matches = NULL;
1863 char **cntxts = NULL;
1864 char *fullname;
1865 char *cntx;
1866
1867 assert(totmatches > 0);
1868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001869 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (buf == NULL)
1871 return;
1872
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001873 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001875 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 goto parse_out;
1877
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001878 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
1880 if (nummatches_a[i] < 1)
1881 continue;
1882
1883 for (j = 0; j < nummatches_a[i]; j++)
1884 {
1885 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1886 &slno, &search)) == NULL)
1887 continue;
1888
1889 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1890 search, tagstr);
1891
1892 vim_free(fullname);
1893
1894 if (strcmp(cntx, "<global>") == 0)
1895 cntxts[totsofar] = NULL;
1896 else
1897 /* note: if vim_strsave returns NULL, then the context
1898 * will be "<global>", which is misleading.
1899 */
1900 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1901
1902 if (matches[totsofar] != NULL)
1903 totsofar++;
1904
1905 } /* for all matches */
1906
1907 (void)cs_read_prompt(i);
1908
1909 } /* for all cscope connections */
1910
1911parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001912 if (totsofar == 0)
1913 {
1914 /* No matches, free the arrays and return NULL in "*matches_p". */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001915 VIM_CLEAR(matches);
1916 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 *matched = totsofar;
1919 *matches_p = matches;
1920 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 vim_free(buf);
1923} /* cs_fill_results */
1924
1925
1926/* get the requested path components */
1927 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001928cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 int i;
1931 char *s;
1932
1933 if (p_cspc == 0)
1934 return path;
1935
1936 s = path + strlen(path) - 1;
1937 for (i = 0; i < p_cspc; ++i)
1938 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001939#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 && *--s != '\\'
1941#endif
1942 )
1943 ;
1944 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001945#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 || (s > path && *s == '\\')
1947#endif
1948 )
1949 ++s;
1950 return s;
1951}
1952
1953/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001954 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001957cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001960 char *t_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 int bufsize = 0; /* Track available bufsize */
1962 int newsize = 0;
1963 char *ptag;
1964 char *fname, *lno, *extra, *tbuf;
1965 int i, idx, num;
1966 char *globalcntx = "GLOBAL";
1967 char *cntxformat = " <<%s>>";
1968 char *context;
1969 char *cstag_msg = _("Cscope tag: %s");
1970 char *csfmt_str = "%4d %6s ";
1971
Bram Moolenaar4033c552017-09-16 20:54:51 +02001972 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001974 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 return;
1976
1977 strcpy(tbuf, matches[0]);
1978 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001979 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001980 {
1981 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001982 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001985 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001986 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (buf != NULL)
1988 {
1989 bufsize = newsize;
1990 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001991 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
1994 vim_free(tbuf);
1995
Bram Moolenaar32526b32019-01-19 17:43:09 +01001996 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); /* strlen is 7 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 num = 1;
2001 for (i = 0; i < num_matches; i++)
2002 {
2003 idx = i;
2004
2005 /* if we really wanted to, we could avoid this malloc and strcpy
2006 * by parsing matches[i] on the fly and placing stuff into buf
2007 * directly, but that's too much of a hassle
2008 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002009 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 continue;
2011 (void)strcpy(tbuf, matches[idx]);
2012
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002013 if (strtok(tbuf, (const char *)"\t") == NULL
2014 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2015 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2016 {
2017 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002019 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002020 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2023
2024 /* hopefully 'num' (num of matches) will be less than 10^16 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002025 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (bufsize < newsize)
2027 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002028 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002029 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002033 vim_free(t_buf);
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 else
2036 bufsize = newsize;
2037 }
2038 if (buf != NULL)
2039 {
2040 /* csfmt_str = "%4d %6s "; */
2041 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002042 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002044 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2045 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 /* compute the required space for the context */
2048 if (cntxts[idx] != NULL)
2049 context = cntxts[idx];
2050 else
2051 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002052 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 if (bufsize < newsize)
2055 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002056 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002057 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002061 vim_free(t_buf);
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else
2064 bufsize = newsize;
2065 }
2066 if (buf != NULL)
2067 {
2068 (void)sprintf(buf, cntxformat, context);
2069
2070 /* print the context only if it fits on the same line */
2071 if (msg_col + (int)strlen(buf) >= (int)Columns)
2072 msg_putchar('\n');
2073 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002074 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 msg_putchar('\n');
2076 }
2077 if (extra != NULL)
2078 {
2079 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002080 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083 vim_free(tbuf); /* only after printing extra due to strtok use */
2084
2085 if (msg_col)
2086 msg_putchar('\n');
2087
2088 ui_breakcheck();
2089 if (got_int)
2090 {
2091 got_int = FALSE; /* don't print any more matches */
2092 break;
2093 }
2094
2095 num++;
2096 } /* for all matches */
2097
2098 vim_free(buf);
2099} /* cs_print_tags_priv */
2100
2101
2102/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002103 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 */
2105 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002106cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108 int ch;
2109 char *buf = NULL; /* buffer for possible error message from cscope */
2110 int bufpos = 0;
2111 char *cs_emsg;
2112 int maxlen;
2113 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int n;
2116
2117 cs_emsg = _("E609: Cscope error: %s");
2118 /* compute maximum allowed len for Cscope error message */
2119 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2120
2121 for (;;)
2122 {
2123 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2124 /* if there is room and char is printable */
2125 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2126 {
2127 if (buf == NULL) /* lazy buffer allocation */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002128 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (buf != NULL)
2130 {
2131 /* append character to the message */
2132 buf[bufpos++] = ch;
2133 buf[bufpos] = NUL;
2134 if (bufpos >= epromptlen
2135 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2136 {
2137 /* remove eprompt from buf */
2138 buf[bufpos - epromptlen] = NUL;
2139
2140 /* print message to user */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /* send RETURN to cscope */
2144 (void)putc('\n', csinfo[i].to_fp);
2145 (void)fflush(csinfo[i].to_fp);
2146
2147 /* clear buf */
2148 bufpos = 0;
2149 buf[bufpos] = NUL;
2150 }
2151 }
2152 }
2153
2154 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2155 {
2156 if (n > 0)
2157 ch = getc(csinfo[i].fr_fp);
2158 if (ch == EOF)
2159 {
2160 PERROR("cs_read_prompt EOF");
2161 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002162 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 else if (p_csverbose)
2164 cs_reading_emsg(i); /* don't have additional information */
2165 cs_release_csp(i, TRUE);
2166 vim_free(buf);
2167 return CSCOPE_FAILURE;
2168 }
2169
2170 if (ch != CSCOPE_PROMPT[n])
2171 {
2172 ch = EOF;
2173 break;
2174 }
2175 }
2176
2177 if (ch == EOF)
2178 continue; /* didn't find the prompt */
2179 break; /* did find the prompt */
2180 }
2181
2182 vim_free(buf);
2183 return CSCOPE_SUCCESS;
2184}
2185
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002186#if defined(UNIX) && defined(SIGALRM)
2187/*
2188 * Used to catch and ignore SIGALRM below.
2189 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002190 static RETSIGTYPE
2191sig_handler SIGDEFARG(sigarg)
2192{
2193 /* do nothing */
2194 SIGRETURN;
2195}
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002199 * Does the actual free'ing for the cs ptr with an optional flag of whether
2200 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
2202 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002203cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2207 */
2208 if (csinfo[i].to_fp != NULL)
2209 {
2210 (void)fputs("q\n", csinfo[i].to_fp);
2211 (void)fflush(csinfo[i].to_fp);
2212 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002213#if defined(UNIX)
2214 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002215 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002216 int pstat;
2217 pid_t pid;
2218
2219# if defined(HAVE_SIGACTION)
2220 struct sigaction sa, old;
2221
Bram Moolenaar9701da02008-03-16 12:09:58 +00002222 /* Use sigaction() to limit the waiting time to two seconds. */
2223 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002224 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002225# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002226 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002227# else
2228 sa.sa_flags = 0;
2229# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002230 sigaction(SIGALRM, &sa, &old);
2231 alarm(2); /* 2 sec timeout */
2232
2233 /* Block until cscope exits or until timer expires */
2234 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002235 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002236
2237 /* cancel pending alarm if still there and restore signal */
2238 alarm(0);
2239 sigaction(SIGALRM, &old, NULL);
2240# else
2241 int waited;
2242
2243 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2244 * to give cscope a chance to exit quickly. */
2245 sleep(0);
2246 for (waited = 0; waited < 40; ++waited)
2247 {
2248 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002249 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002250 if (pid != 0)
2251 break; /* break unless the process is still running */
Bram Moolenaar91519e42008-04-01 18:59:07 +00002252 mch_delay(50L, FALSE); /* sleep 50 ms */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002253 }
2254# endif
2255 /*
2256 * If the cscope process is still running: kill it.
2257 * Safety check: If the PID would be zero here, the entire X session
2258 * would be killed. -1 and 1 are dangerous as well.
2259 */
2260 if (pid < 0 && csinfo[i].pid > 1)
2261 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002262# ifdef ECHILD
2263 int alive = TRUE;
2264
2265 if (waitpid_errno == ECHILD)
2266 {
2267 /*
2268 * When using 'vim -g', vim is forked and cscope process is
2269 * no longer a child process but a sibling. So waitpid()
2270 * fails with errno being ECHILD (No child processes).
2271 * Don't send SIGKILL to cscope immediately but wait
2272 * (polling) for it to exit normally as result of sending
2273 * the "q" command, hence giving it a chance to clean up
2274 * its temporary files.
2275 */
2276 int waited;
2277
2278 sleep(0);
2279 for (waited = 0; waited < 40; ++waited)
2280 {
2281 /* Check whether cscope process is still alive */
2282 if (kill(csinfo[i].pid, 0) != 0)
2283 {
2284 alive = FALSE; /* cscope process no longer exists */
2285 break;
2286 }
Bram Moolenaar91519e42008-04-01 18:59:07 +00002287 mch_delay(50L, FALSE); /* sleep 50ms */
Bram Moolenaare9b28842008-04-01 12:31:14 +00002288 }
2289 }
2290 if (alive)
2291# endif
2292 {
2293 kill(csinfo[i].pid, SIGKILL);
2294 (void)waitpid(csinfo[i].pid, &pstat, 0);
2295 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002296 }
2297 }
2298#else /* !UNIX */
Bram Moolenaar02b06312007-09-06 15:39:22 +00002299 if (csinfo[i].hProc != NULL)
2300 {
2301 /* Give cscope a chance to exit normally */
2302 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2303 TerminateProcess(csinfo[i].hProc, 0);
2304 CloseHandle(csinfo[i].hProc);
2305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307
2308 if (csinfo[i].fr_fp != NULL)
2309 (void)fclose(csinfo[i].fr_fp);
2310 if (csinfo[i].to_fp != NULL)
2311 (void)fclose(csinfo[i].to_fp);
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (freefnpp)
2314 {
2315 vim_free(csinfo[i].fname);
2316 vim_free(csinfo[i].ppath);
2317 vim_free(csinfo[i].flags);
2318 }
2319
2320 clear_csinfo(i);
2321} /* cs_release_csp */
2322
2323
2324/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002325 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002328cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2331 int i;
Bram Moolenaar051b7822005-05-19 21:00:46 +00002332 char buf[20]; /* for sprintf " (#%d)" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002334 if (csinfo_size == 0)
2335 return CSCOPE_SUCCESS;
2336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 /* malloc our db and ppath list */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002338 dblist = ALLOC_MULT(char *, csinfo_size);
2339 pplist = ALLOC_MULT(char *, csinfo_size);
2340 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (dblist == NULL || pplist == NULL || fllist == NULL)
2342 {
2343 vim_free(dblist);
2344 vim_free(pplist);
2345 vim_free(fllist);
2346 return CSCOPE_FAILURE;
2347 }
2348
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002349 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 dblist[i] = csinfo[i].fname;
2352 pplist[i] = csinfo[i].ppath;
2353 fllist[i] = csinfo[i].flags;
2354 if (csinfo[i].fname != NULL)
2355 cs_release_csp(i, FALSE);
2356 }
2357
2358 /* rebuild the cscope connection list */
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002359 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 if (dblist[i] != NULL)
2362 {
2363 cs_add_common(dblist[i], pplist[i], fllist[i]);
2364 if (p_csverbose)
2365 {
Bram Moolenaard2ac9842007-08-21 16:03:51 +00002366 /* don't use smsg_attr() because we want to display the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 * connection number in the same line as
2368 * "Added cscope database..."
2369 */
2370 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002371 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 }
2374 vim_free(dblist[i]);
2375 vim_free(pplist[i]);
2376 vim_free(fllist[i]);
2377 }
2378 vim_free(dblist);
2379 vim_free(pplist);
2380 vim_free(fllist);
2381
2382 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002383 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return CSCOPE_SUCCESS;
2385} /* cs_reset */
2386
2387
2388/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002389 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 * (Prepends ppath, if there is one and if it's not already prepended,
2391 * otherwise just uses the name found.)
2392 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002393 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002395 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002398cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002400 char *fullname;
2401 int len;
2402 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002405 * Ppath is freed when we destroy the cscope connection.
2406 * Fullname is freed after cs_make_vim_style_matches, after it's been
2407 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002409 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002412 else if (p_csre && csinfo[i].fname != NULL)
2413 {
2414 /* If 'cscoperelative' is set and ppath is not set, use cscope.out
2415 * path in path resolution. */
2416 csdir = alloc(MAXPATHL);
2417 if (csdir != NULL)
2418 {
2419 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002420 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002421 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002422 len += (int)STRLEN(csdir);
2423 }
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002426 /* Note/example: this won't work if the cscope output already starts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 * "../.." and the prefix path is also "../..". if something like this
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002428 * happens, you are screwed up and need to fix how you're using cscope. */
2429 if (csinfo[i].ppath != NULL
2430 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2431 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002432#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002433 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002435 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002436 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002437 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002438 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2439 }
2440 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002441 {
2442 /* Check for csdir to be non empty to avoid empty path concatenated to
Bram Moolenaar28c21912013-05-29 19:18:00 +02002443 * cscope output. */
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002444 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002447 {
2448 fullname = (char *)vim_strsave((char_u *)name);
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002451 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455
2456/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002457 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002460cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 short i;
2463 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002464 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else
2466 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002467 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002469 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002470 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 if (csinfo[i].fname == NULL)
2473 continue;
2474
2475 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2478 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 i, (long)csinfo[i].pid, csinfo[i].fname);
2481 }
2482 }
2483
2484 wait_return(TRUE);
2485 return CSCOPE_SUCCESS;
2486} /* cs_show */
2487
Bram Moolenaar02b06312007-09-06 15:39:22 +00002488
2489/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002490 * Only called when VIM exits to quit any cscope sessions.
2491 */
2492 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002493cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002494{
2495 int i;
2496
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002497 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002498 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002499 vim_free(csinfo);
2500 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002501}
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#endif /* FEAT_CSCOPE */
2504
2505/* the end */