blob: c7bd9e33ed849cb9b3b79d60b920cf58341fb5b0 [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;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010054static int csinfo_size = 0; // number of items allocated in
55 // csinfo[]
Bram Moolenaar9fa49da2009-07-10 13:11:26 +000056
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010057static 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{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010084 EXP_CSCOPE_SUBCMD, // expand ":cscope" sub-commands
85 EXP_SCSCOPE_SUBCMD, // expand ":scscope" sub-commands
86 EXP_CSCOPE_FIND, // expand ":cscope find" arguments
87 EXP_CSCOPE_KILL // expand ":cscope kill" arguments
Bram Moolenaarf4580d82009-03-18 11:52:53 +000088} 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:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100103 // Complete with sub-commands of ":cscope":
104 // add, find, help, kill, reset, show
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000105 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000106 case EXP_SCSCOPE_SUBCMD:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100107 // Complete with sub-commands of ":scscope": same sub-commands as
108 // ":cscope" but skip commands which don't support split windows
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000109 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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100121 // Complete with query type of ":cscope find {query_type}".
122 // {query_type} can be letters (c, d, ... a) or numbers (0, 1,
123 // ..., 9) but only complete with letters, since numbers are
124 // redundant.
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000125 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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100131 // ":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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100163 // Default: expand subcommands
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000164 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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100169 // (part of) subcommand already typed
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000170 if (*arg != NUL)
171 {
172 p = skiptowhite(arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100173 if (*p != NUL) // past first word
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000174 {
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 Moolenaar2ab2e862019-12-04 21:24:53 +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;
Bram Moolenaare1004402020-10-24 20:49:43 +0200215 postponed_split_flags = cmdmod.cmod_split;
216 postponed_split_tab = cmdmod.cmod_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;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100336}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
338
339/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100340 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 */
342 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100343cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
345 cs_manage_matches(NULL, NULL, -1, Free);
346}
347
348
349/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100350 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 */
352 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100353cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 cs_manage_matches(NULL, NULL, -1, Print);
356}
357
358
359/*
360 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
361 *
362 * Checks for the existence of a |cscope| connection. If no
363 * parameters are specified, then the function returns:
364 *
365 * 0, if cscope was not available (not compiled in), or if there
366 * are no cscope connections; or
367 * 1, if there is at least one cscope connection.
368 *
369 * If parameters are specified, then the value of {num}
370 * determines how existence of a cscope connection is checked:
371 *
372 * {num} Description of existence check
373 * ----- ------------------------------
374 * 0 Same as no parameters (e.g., "cscope_connection()").
375 * 1 Ignore {prepend}, and use partial string matches for
376 * {dbpath}.
377 * 2 Ignore {prepend}, and use exact string matches for
378 * {dbpath}.
379 * 3 Use {prepend}, use partial string matches for both
380 * {dbpath} and {prepend}.
381 * 4 Use {prepend}, use exact string matches for both
382 * {dbpath} and {prepend}.
383 *
384 * Note: All string comparisons are case sensitive!
385 */
386#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200387 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100388cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 int i;
391
392 if (num < 0 || num > 4 || (num > 0 && !dbpath))
393 return FALSE;
394
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000395 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
397 if (!csinfo[i].fname)
398 continue;
399
400 if (num == 0)
401 return TRUE;
402
403 switch (num)
404 {
405 case 1:
406 if (strstr(csinfo[i].fname, (char *)dbpath))
407 return TRUE;
408 break;
409 case 2:
410 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
411 return TRUE;
412 break;
413 case 3:
414 if (strstr(csinfo[i].fname, (char *)dbpath)
415 && ((!ppath && !csinfo[i].ppath)
416 || (ppath
417 && csinfo[i].ppath
418 && strstr(csinfo[i].ppath, (char *)ppath))))
419 return TRUE;
420 break;
421 case 4:
422 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
423 && ((!ppath && !csinfo[i].ppath)
424 || (ppath
425 && csinfo[i].ppath
426 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
427 return TRUE;
428 break;
429 }
430 }
431
432 return FALSE;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200433}
434
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#endif
436
437
438/*
439 * PRIVATE functions
440 ****************************************************************************/
441
442/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100443 * Add cscope database or a directory name (to look for cscope.out)
444 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100447cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 char *fname, *ppath, *flags = NULL;
450
451 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
452 {
453 cs_usage_msg(Add);
454 return CSCOPE_FAILURE;
455 }
456 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
457 flags = strtok((char *)NULL, (const char *)" ");
458
459 return cs_add_common(fname, ppath, flags);
460}
461
462 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100463cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
James McCoy3c5904d2021-10-24 14:50:07 +0100465 int err = errno;
466 (void)semsg(_("E563: stat(%s) error: %d"), fname, err);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467}
468
469
470/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100471 * The common routine to add a new cscope connection. Called by
472 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 * routine uses a number of goto statements.
474 */
475 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100476cs_add_common(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100477 char *arg1, // filename - may contain environment variables
478 char *arg2, // prepend path - may contain environment variables
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100479 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200481 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000482 int ret;
483 char *fname = NULL;
484 char *fname2 = NULL;
485 char *ppath = NULL;
486 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200487 int len;
488 int usedlen = 0;
489 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100491 // get the filename (arg1), expand it, and try to stat it
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200492 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 goto add_err;
494
495 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200496 len = (int)STRLEN(fname);
497 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200498 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200499 (char_u **)&fname, &fbuf, &len);
500 if (fname == NULL)
501 goto add_err;
502 fname = (char *)vim_strnsave((char_u *)fname, len);
503 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200504
Bram Moolenaar8767f522016-07-01 17:17:39 +0200505 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (ret < 0)
507 {
508staterr:
509 if (p_csverbose)
510 cs_stat_emsg(fname);
511 goto add_err;
512 }
513
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100514 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (arg2 != NULL)
516 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200517 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200519 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 goto add_err;
521
522 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200523 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (ret < 0)
525 goto staterr;
526 }
527
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100528 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200529 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200531 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (fname2 == NULL)
533 goto add_err;
534
535 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100536#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 || fname[strlen(fname)-1] == '\\'
538#endif
539 )
540 {
541 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200542 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 break;
544 }
545 if (fname[0] == '\0')
546 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
547 else
548 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
549
Bram Moolenaar8767f522016-07-01 17:17:39 +0200550 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (ret < 0)
552 {
553 if (p_csverbose)
554 cs_stat_emsg(fname2);
555 goto add_err;
556 }
557
558 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
562 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
563 }
564 else
565 {
566 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 (void)semsg(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 _("E564: %s is not a directory or a valid cscope database"),
569 fname);
570 goto add_err;
571 }
572
573 if (i != -1)
574 {
575 if (cs_create_connection(i) == CSCOPE_FAILURE
576 || cs_read_prompt(i) == CSCOPE_FAILURE)
577 {
578 cs_release_csp(i, TRUE);
579 goto add_err;
580 }
581
582 if (p_csverbose)
583 {
584 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100585 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100586 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 csinfo[i].fname);
588 }
589 }
590
591 vim_free(fname);
592 vim_free(fname2);
593 vim_free(ppath);
594 return CSCOPE_SUCCESS;
595
596add_err:
597 vim_free(fname2);
598 vim_free(fname);
599 vim_free(ppath);
600 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100601}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603
604 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100605cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100608}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610
611 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100612cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000614 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100615}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617
618/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100619 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 */
621 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100622cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 short i;
625 short cnt = 0;
626
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000627 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 if (csinfo[i].fname != NULL)
630 cnt++;
631 }
632 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100633}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100636cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100637 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100639 semsg(_("E262: error reading cscope connection %d"), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640}
641
642#define CSREAD_BUFSIZE 2048
643/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100644 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100647cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 char *stok;
650 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100651 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200653 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (buf == NULL)
655 return 0;
656 for (;;)
657 {
658 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
659 {
660 if (feof(csinfo[idx].fr_fp))
661 errno = EIO;
662
663 cs_reading_emsg(idx);
664
665 vim_free(buf);
666 return -1;
667 }
668
669 /*
670 * If the database is out of date, or there's some other problem,
671 * cscope will output error messages before the number-of-lines output.
672 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000673 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100674 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100676 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100677 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if ((stok = strtok(buf, (const char *)" ")) == NULL)
679 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000680 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 continue;
682
683 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
684 continue;
685 nlines = atoi(stok);
686 if (nlines < 0)
687 {
688 nlines = 0;
689 break;
690 }
691
692 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
693 continue;
694 if (strncmp((const char *)stok, "lines", 5))
695 continue;
696
697 break;
698 }
699
700 vim_free(buf);
701 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100702}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704
705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 * Creates the actual cscope command query from what the user entered.
707 */
708 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100709cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 char *cmd;
712 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000713 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
715 switch (csoption[0])
716 {
717 case '0' : case 's' :
718 search = 0;
719 break;
720 case '1' : case 'g' :
721 search = 1;
722 break;
723 case '2' : case 'd' :
724 search = 2;
725 break;
726 case '3' : case 'c' :
727 search = 3;
728 break;
729 case '4' : case 't' :
730 search = 4;
731 break;
732 case '6' : case 'e' :
733 search = 6;
734 break;
735 case '7' : case 'f' :
736 search = 7;
737 break;
738 case '8' : case 'i' :
739 search = 8;
740 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200741 case '9' : case 'a' :
742 search = 9;
743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 default :
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100745 (void)emsg(_("E561: unknown cscope search type"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 cs_usage_msg(Find);
747 return NULL;
748 }
749
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100750 // Skip white space before the patter, except for text and pattern search,
751 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000752 pat = pattern;
753 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100754 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000755 ++pat;
756
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200757 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 return NULL;
759
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000760 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
762 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100763}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765
766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 * This piece of code was taken/adapted from nvi. do we need to add
768 * the BSD license notice?
769 */
770 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100771cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000773#ifdef UNIX
774 int to_cs[2], from_cs[2];
775#endif
776 int len;
777 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100778#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000779 int fd;
780 SECURITY_ATTRIBUTES sa;
781 PROCESS_INFORMATION pi;
782 STARTUPINFO si;
783 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
784 HANDLE stdin_rd, stdout_rd;
785 HANDLE stdout_wr, stdin_wr;
786 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200787# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
788# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000789# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#endif
793
Bram Moolenaar02b06312007-09-06 15:39:22 +0000794#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /*
796 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
797 * from_cs[0] and writes to to_cs[1].
798 */
799 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
800 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100802 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803err_closing:
804 if (to_cs[0] != -1)
805 (void)close(to_cs[0]);
806 if (to_cs[1] != -1)
807 (void)close(to_cs[1]);
808 if (from_cs[0] != -1)
809 (void)close(from_cs[0]);
810 if (from_cs[1] != -1)
811 (void)close(from_cs[1]);
812 return CSCOPE_FAILURE;
813 }
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 switch (csinfo[i].pid = fork())
816 {
817 case -1:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 (void)emsg(_("E622: Could not fork for cscope"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 goto err_closing;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100820 case 0: // child: run cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (dup2(to_cs[0], STDIN_FILENO) == -1)
822 PERROR("cs_create_connection 1");
823 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
824 PERROR("cs_create_connection 2");
825 if (dup2(from_cs[1], STDERR_FILENO) == -1)
826 PERROR("cs_create_connection 3");
827
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100828 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 (void)close(to_cs[1]);
830 (void)close(from_cs[0]);
831#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100832 // MSWIN
833 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000834 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
835 sa.bInheritHandle = TRUE;
836 sa.lpSecurityDescriptor = NULL;
837
838 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
839 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100841 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000842err_closing:
843 if (pipe_stdin)
844 {
845 CloseHandle(stdin_rd);
846 CloseHandle(stdin_wr);
847 }
848 if (pipe_stdout)
849 {
850 CloseHandle(stdout_rd);
851 CloseHandle(stdout_wr);
852 }
853 return CSCOPE_FAILURE;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100856 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200857 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859#ifdef UNIX
860 return CSCOPE_FAILURE;
861#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100862 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 goto err_closing;
864#endif
865 }
866 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
867
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100868 // alloc space to hold the cscope command
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000869 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (csinfo[i].ppath)
871 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100872 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200873 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
875 vim_free(prog);
876#ifdef UNIX
877 return CSCOPE_FAILURE;
878#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100879 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 goto err_closing;
881#endif
882 }
883 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
884
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000885 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887
888 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000889 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200891 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 vim_free(prog);
894 vim_free(ppath);
895#ifdef UNIX
896 return CSCOPE_FAILURE;
897#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100898 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 goto err_closing;
900#endif
901 }
902
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100903 // run the cscope command; is there execl for non-unix systems?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#if defined(UNIX)
905 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
906#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100907 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
909#endif
910 if (csinfo[i].ppath != NULL)
911 {
912 (void)strcat(cmd, " -P");
913 (void)strcat(cmd, csinfo[i].ppath);
914 }
915 if (csinfo[i].flags != NULL)
916 {
917 (void)strcat(cmd, " ");
918 (void)strcat(cmd, csinfo[i].flags);
919 }
920# ifdef UNIX
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100921 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 vim_free(prog);
923# endif
924 vim_free(ppath);
925
926#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200927# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100928 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200929# if defined(HAVE_SETSID)
930 (void)setsid();
931# else
932 if (setpgid(0, 0) == -1)
933 PERROR(_("cs_create_connection setpgid failed"));
934# endif
935# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000936 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 PERROR(_("cs_create_connection exec failed"));
938
939 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100940 // NOTREACHED
941 default: // parent.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 /*
943 * Save the file descriptors for later duplication, and
944 * reopen as streams.
945 */
946 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
947 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
948 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
949 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
950
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100951 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 (void)close(to_cs[0]);
953 (void)close(from_cs[1]);
954
955 break;
956 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100959 // MSWIN
960 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +0000961 GetStartupInfo(&si);
962 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100963 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +0000964 si.hStdOutput = stdout_wr;
965 si.hStdError = stdout_wr;
966 si.hStdInput = stdin_rd;
967 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
968 NULL, NULL, &si, &pi);
969 vim_free(prog);
970 vim_free(cmd);
971
972 if (!created)
973 {
974 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100975 (void)emsg(_("E623: Could not spawn cscope process"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000976 goto err_closing;
977 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100978 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000979 csinfo[i].pid = pi.dwProcessId;
980 csinfo[i].hProc = pi.hProcess;
981 CloseHandle(pi.hThread);
982
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100983 // TODO - tidy up after failure to create files on pipe handles.
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000984 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
985 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000986 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
987 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000988 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
989 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000990 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
991 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
992
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100993 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +0000994 CloseHandle(stdin_rd);
995 CloseHandle(stdout_wr);
996
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100997#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002
1003/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001004 * Query cscope using command line interface. Parse the output and use tselect
1005 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 *
1007 * returns TRUE if we jump to a tag or abort, FALSE if not.
1008 */
1009 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001010cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
1012 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001013 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if (cs_check_for_connections() == FALSE)
1016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 (void)emsg(_("E567: no cscope connections"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 return FALSE;
1019 }
1020
1021 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1022 {
1023 cs_usage_msg(Find);
1024 return FALSE;
1025 }
1026
1027 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001028 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 cs_usage_msg(Find);
1031 return FALSE;
1032 }
1033
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001034 /*
1035 * Let's replace the NULs written by strtok() with spaces - we need the
1036 * spaces to correctly display the quickfix/location list window's title.
1037 */
1038 for (i = 0; i < eap_arg_len; ++i)
1039 if (NUL == eap->arg[i])
1040 eap->arg[i] = ' ';
1041
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001042 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001043 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046
1047/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001048 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 */
1050 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001051cs_find_common(
1052 char *opt,
1053 char *pat,
1054 int forceit,
1055 int verbose,
1056 int use_ll UNUSED,
1057 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
1059 int i;
1060 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001061 int *nummatches;
1062 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef FEAT_QUICKFIX
1064 char cmdletter;
1065 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001066
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001067 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001068 switch (opt[0])
1069 {
1070 case '0' :
1071 cmdletter = 's';
1072 break;
1073 case '1' :
1074 cmdletter = 'g';
1075 break;
1076 case '2' :
1077 cmdletter = 'd';
1078 break;
1079 case '3' :
1080 cmdletter = 'c';
1081 break;
1082 case '4' :
1083 cmdletter = 't';
1084 break;
1085 case '6' :
1086 cmdletter = 'e';
1087 break;
1088 case '7' :
1089 cmdletter = 'f';
1090 break;
1091 case '8' :
1092 cmdletter = 'i';
1093 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001094 case '9' :
1095 cmdletter = 'a';
1096 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001097 default :
1098 cmdletter = opt[0];
1099 }
1100
1101 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1102 if (qfpos != NULL)
1103 {
1104 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001105 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001106 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1107 {
James McCoy3c5904d2021-10-24 14:50:07 +01001108 (void)semsg(_("E469: invalid cscopequickfix flag %c for %c"), *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001109 return FALSE;
1110 }
1111
Bram Moolenaar21662be2016-11-06 14:46:44 +01001112 if (*qfpos != '0'
1113 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1114 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001115 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001116# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001117 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001118 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001119# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001120 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001124 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 cmd = cs_create_cmd(opt, pat);
1126 if (cmd == NULL)
1127 return FALSE;
1128
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001129 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001130 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001131 {
1132 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001133 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001134 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001135
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001136 // Send query to all open connections, then count the total number
1137 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001138 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 nummatches[i] = 0;
1140 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001141 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001143 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 continue;
1145
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001146 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1148 (void)fflush(csinfo[i].to_fp);
1149
1150 nummatches[i] = cs_cnt_matches(i);
1151
1152 if (nummatches[i] > -1)
1153 totmatches += nummatches[i];
1154
1155 if (nummatches[i] == 0)
1156 (void)cs_read_prompt(i);
1157 }
1158 vim_free(cmd);
1159
1160 if (totmatches == 0)
1161 {
James McCoy3c5904d2021-10-24 14:50:07 +01001162 if (verbose)
1163 (void)semsg(_("E259: no matches found for cscope query %s of %s"), opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001164 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 return FALSE;
1166 }
1167
1168#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001169 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001171 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001172 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001173 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001174 qf_info_T *qi = NULL;
1175 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001177 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001178 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001179 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001182 cs_file_results(f, nummatches);
1183 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001184 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001185 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001186 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001187 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001188 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001190 if (postponed_split != 0)
1191 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001192 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001194 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001195 postponed_split = 0;
1196 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001197
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001198 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1199 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001200 if (use_ll)
1201 /*
1202 * In the location list window, use the displayed location
1203 * list. Otherwise, use the location list for the window.
1204 */
1205 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1206 ? wp->w_llist_ref : wp->w_llist;
1207 qf_jump(qi, 0, 0, forceit);
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 mch_remove(tmp);
1211 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001212 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 return TRUE;
1214 }
1215 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001216#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001218 char **matches = NULL, **contexts = NULL;
1219 int matched = 0;
1220
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001221 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001222 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001224 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (matches == NULL)
1226 return FALSE;
1227
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001228 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
1230 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1231 }
1232
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001236 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001239cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240{
1241 cscmd_T *cmdp = cs_cmds;
1242
Bram Moolenaar32526b32019-01-19 17:43:09 +01001243 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 while (cmdp->name != NULL)
1245 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001246 char *help = _(cmdp->help);
1247 int space_cnt = 30 - vim_strsize((char_u *)help);
1248
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001249 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001250 if (space_cnt < 0)
1251 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001253 cmdp->name,
1254 help, space_cnt, " ",
1255 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001257 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001258 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001259 " c: Find functions calling this function\n"
1260 " d: Find functions called by this function\n"
1261 " e: Find this egrep pattern\n"
1262 " f: Find this file\n"
1263 " g: Find this definition\n"
1264 " i: Find files #including this file\n"
1265 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001266 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 cmdp++;
1269 }
1270
1271 wait_return(TRUE);
1272 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001277clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 csinfo[i].fname = NULL;
1280 csinfo[i].ppath = NULL;
1281 csinfo[i].flags = NULL;
1282#if defined(UNIX)
1283 csinfo[i].st_dev = (dev_t)0;
1284 csinfo[i].st_ino = (ino_t)0;
1285#else
1286 csinfo[i].nVolume = 0;
1287 csinfo[i].nIndexHigh = 0;
1288 csinfo[i].nIndexLow = 0;
1289#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001290 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 csinfo[i].fr_fp = NULL;
1292 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001293#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001294 csinfo[i].hProc = NULL;
1295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296}
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001299 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 */
1301 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001302cs_insert_filelist(
1303 char *fname,
1304 char *ppath,
1305 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001306 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 short i, j;
1309#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 BY_HANDLE_FILE_INFORMATION bhfi;
1311
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001312 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001314 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1315 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (p_csverbose)
1317 {
1318 char *cant_msg = _("E625: cannot open cscope database: %s");
1319 char *winmsg = GetWin32Error();
1320
1321 if (winmsg != NULL)
1322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001323 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 LocalFree(winmsg);
1325 }
1326 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001327 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001328 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001331
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001332 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 (void)emsg(_("E626: cannot get cscope database information"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337#endif
1338
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001339 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001340 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 if (csinfo[j].fname != NULL
1343#if defined(UNIX)
1344 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1345#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001346 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001347 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001348 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1349 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001350 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1352 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1353#endif
1354 )
1355 {
1356 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 (void)emsg(_("E568: duplicate cscope database not added"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 return -1;
1359 }
1360
1361 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001362 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364
1365 if (i == -1)
1366 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001367 i = csinfo_size;
1368 if (csinfo_size == 0)
1369 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001370 // First time allocation: allocate only 1 connection. It should
1371 // be enough for most users. If more is needed, csinfo will be
1372 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001373 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001374 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001375 }
1376 else
1377 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001378 csinfo_T *t_csinfo = csinfo;
1379
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001380 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001381 csinfo_size *= 2;
1382 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001383 if (csinfo == NULL)
1384 {
1385 vim_free(t_csinfo);
1386 csinfo_size = 0;
1387 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001388 }
1389 if (csinfo == NULL)
1390 return -1;
1391 for (j = csinfo_size/2; j < csinfo_size; j++)
1392 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001395 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 return -1;
1397
1398 (void)strcpy(csinfo[i].fname, (const char *)fname);
1399
1400 if (ppath != NULL)
1401 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001402 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001404 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 return -1;
1406 }
1407 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1408 } else
1409 csinfo[i].ppath = NULL;
1410
1411 if (flags != NULL)
1412 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001413 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001415 VIM_CLEAR(csinfo[i].fname);
1416 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 return -1;
1418 }
1419 (void)strcpy(csinfo[i].flags, (const char *)flags);
1420 } else
1421 csinfo[i].flags = NULL;
1422
1423#if defined(UNIX)
1424 csinfo[i].st_dev = sb->st_dev;
1425 csinfo[i].st_ino = sb->st_ino;
1426
1427#else
1428 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1429 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1430 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1431#endif
1432 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
1435
1436/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001437 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 */
1439 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001440cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 cscmd_T *cmdp;
1443 char *stok;
1444 size_t len;
1445
1446 if (eap->arg == NULL)
1447 return NULL;
1448
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001449 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001450 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1453 return NULL;
1454
1455 len = strlen(stok);
1456 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1457 {
1458 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1459 return (cmdp);
1460 }
1461 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464
1465/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001466 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001469cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 char *stok;
1472 short i;
1473
1474 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1475 {
1476 cs_usage_msg(Kill);
1477 return CSCOPE_FAILURE;
1478 }
1479
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001480 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1482 || (strlen(stok) < 3 && stok[0] == '-'
1483 && VIM_ISDIGIT((int)(stok[1]))))
1484 i = atoi(stok);
1485 else
1486 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001487 // It must be part of a name. We will try to find a match
1488 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001489 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
1491 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1492 break;
1493 }
1494 }
1495
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001496 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001499 (void)semsg(_("E261: cscope connection %s not found"), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 else
1502 {
1503 if (i == -1)
1504 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001505 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 if (csinfo[i].fname)
1508 cs_kill_execute(i, csinfo[i].fname);
1509 }
1510 }
1511 else
1512 cs_kill_execute(i, stok);
1513 }
1514
1515 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518
1519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 * Actually kills a specific cscope connection.
1521 */
1522 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001523cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001524 int i, // cscope table index
1525 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 if (p_csverbose)
1528 {
1529 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001530 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001531 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 cs_release_csp(i, TRUE);
1534}
1535
1536
1537/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001538 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1540 * the type of the tag you are looking for. for example, in Darren Hiebert's
1541 * ctags (the one that comes with vim), #define's use a line number to find the
1542 * tag in a file while function definitions use a regexp search pattern.
1543 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001544 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * quirky (and probably other things i don't know about):
1546 *
1547 * if you have "# define" in your source file, which is
1548 * perfectly legal, cscope thinks you have "#define". this
1549 * will result in a failed regexp search. :(
1550 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001551 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 * would still have to be modified to escape all the special regular expression
1553 * characters to comply with ctags formatting.
1554 */
1555 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001556cs_make_vim_style_matches(
1557 char *fname,
1558 char *slno,
1559 char *search,
1560 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001562 // vim style is ctags:
1563 //
1564 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1565 //
1566 // but as mentioned above, we'll always use the line number and
1567 // put the search pattern (if one exists) as "extra"
1568 //
1569 // buf is used as part of vim's method of handling tags, and
1570 // (i think) vim frees it when you pop your tags and get replaced
1571 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 char *buf;
1573 int amt;
1574
1575 if (search != NULL)
1576 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001577 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001578 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return NULL;
1580
1581 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1582 }
1583 else
1584 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001585 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001586 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return NULL;
1588
1589 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1590 }
1591
1592 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001593}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595
1596/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001597 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 *
1599 * Store: keep a ptr to the (malloc'd) memory of matches originally
1600 * generated from cs_find(). the matches are originally lines directly
1601 * from cscope output, but transformed to look like something out of a
1602 * ctags. see cs_make_vim_style_matches for more details.
1603 *
1604 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1605 * the next line from the cscope output. it basically keeps track of which
1606 * lines have been "used" and returns the next one.
1607 *
1608 * Free: frees up everything and resets
1609 *
1610 * Print: prints the tags
1611 */
1612 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001613cs_manage_matches(
1614 char **matches,
1615 char **contexts,
1616 int totmatches,
1617 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 static char **mp = NULL;
1620 static char **cp = NULL;
1621 static int cnt = -1;
1622 static int next = -1;
1623 char *p = NULL;
1624
1625 switch (cmd)
1626 {
1627 case Store:
1628 assert(matches != NULL);
1629 assert(totmatches > 0);
1630 if (mp != NULL || cp != NULL)
1631 (void)cs_manage_matches(NULL, NULL, -1, Free);
1632 mp = matches;
1633 cp = contexts;
1634 cnt = totmatches;
1635 next = 0;
1636 break;
1637 case Get:
1638 if (next >= cnt)
1639 return NULL;
1640
1641 p = mp[next];
1642 next++;
1643 break;
1644 case Free:
1645 if (mp != NULL)
1646 {
1647 if (cnt > 0)
1648 while (cnt--)
1649 {
1650 vim_free(mp[cnt]);
1651 if (cp != NULL)
1652 vim_free(cp[cnt]);
1653 }
1654 vim_free(mp);
1655 vim_free(cp);
1656 }
1657 mp = NULL;
1658 cp = NULL;
1659 cnt = 0;
1660 next = 0;
1661 break;
1662 case Print:
1663 cs_print_tags_priv(mp, cp, cnt);
1664 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001665 default: // should not reach here
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001666 iemsg(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 return NULL;
1668 }
1669
1670 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
1673
1674/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001675 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 */
1677 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001678cs_parse_results(
1679 int cnumber,
1680 char *buf,
1681 int bufsize,
1682 char **context,
1683 char **linenumber,
1684 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 int ch;
1687 char *p;
1688 char *name;
1689
1690 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1691 {
1692 if (feof(csinfo[cnumber].fr_fp))
1693 errno = EIO;
1694
1695 cs_reading_emsg(cnumber);
1696
1697 return NULL;
1698 }
1699
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001700 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if ((p = strchr(buf, '\n')) == NULL)
1702 {
1703 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1704 ;
1705 return NULL;
1706 }
1707 *p = '\0';
1708
1709 /*
1710 * cscope output is in the following format:
1711 *
1712 * <filename> <context> <line number> <pattern>
1713 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001714 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return NULL;
1716 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1717 return NULL;
1718 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1719 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001720 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001722 // --- nvi ---
1723 // If the file is older than the cscope database, that is,
1724 // the database was built since the file was last modified,
1725 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (strcmp(*search, "<unknown>") == 0)
1727 *search = NULL;
1728
1729 name = cs_resolve_file(cnumber, name);
1730 return name;
1731}
1732
Bram Moolenaarc716c302006-01-21 22:12:51 +00001733#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001735 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
1737 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001738cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 int i, j;
1741 char *buf;
1742 char *search, *slno;
1743 char *fullname;
1744 char *cntx;
1745 char *context;
1746
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001747 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (buf == NULL)
1749 return;
1750
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001751 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 if (nummatches_a[i] < 1)
1754 continue;
1755
1756 for (j = 0; j < nummatches_a[i]; j++)
1757 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001758 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1759 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 continue;
1761
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001762 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001763 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001764 {
1765 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 if (strcmp(cntx, "<global>")==0)
1770 strcpy(context, "<<global>>");
1771 else
1772 sprintf(context, "<<%s>>", cntx);
1773
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001774 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1776 else
1777 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1778
1779 vim_free(context);
1780 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001781 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 (void)cs_read_prompt(i);
1784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 vim_free(buf);
1787}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001791 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1792 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001793 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
1795 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001796cs_fill_results(
1797 char *tagstr,
1798 int totmatches,
1799 int *nummatches_a,
1800 char ***matches_p,
1801 char ***cntxts_p,
1802 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 int i, j;
1805 char *buf;
1806 char *search, *slno;
1807 int totsofar = 0;
1808 char **matches = NULL;
1809 char **cntxts = NULL;
1810 char *fullname;
1811 char *cntx;
1812
1813 assert(totmatches > 0);
1814
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001815 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (buf == NULL)
1817 return;
1818
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001819 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001821 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 goto parse_out;
1823
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001824 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 if (nummatches_a[i] < 1)
1827 continue;
1828
1829 for (j = 0; j < nummatches_a[i]; j++)
1830 {
1831 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1832 &slno, &search)) == NULL)
1833 continue;
1834
1835 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1836 search, tagstr);
1837
1838 vim_free(fullname);
1839
1840 if (strcmp(cntx, "<global>") == 0)
1841 cntxts[totsofar] = NULL;
1842 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843 // note: if vim_strsave returns NULL, then the context
1844 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1846
1847 if (matches[totsofar] != NULL)
1848 totsofar++;
1849
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001850 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 (void)cs_read_prompt(i);
1853
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001854 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001857 if (totsofar == 0)
1858 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001859 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001860 VIM_CLEAR(matches);
1861 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 *matched = totsofar;
1864 *matches_p = matches;
1865 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001868}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001871/*
1872 * get the requested path components
1873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001875cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877 int i;
1878 char *s;
1879
1880 if (p_cspc == 0)
1881 return path;
1882
1883 s = path + strlen(path) - 1;
1884 for (i = 0; i < p_cspc; ++i)
1885 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001886#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 && *--s != '\\'
1888#endif
1889 )
1890 ;
1891 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001892#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 || (s > path && *s == '\\')
1894#endif
1895 )
1896 ++s;
1897 return s;
1898}
1899
1900/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001901 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 */
1903 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001904cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001907 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001908 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 int newsize = 0;
1910 char *ptag;
1911 char *fname, *lno, *extra, *tbuf;
1912 int i, idx, num;
1913 char *globalcntx = "GLOBAL";
1914 char *cntxformat = " <<%s>>";
1915 char *context;
1916 char *cstag_msg = _("Cscope tag: %s");
1917 char *csfmt_str = "%4d %6s ";
1918
Bram Moolenaar4033c552017-09-16 20:54:51 +02001919 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001921 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 return;
1923
1924 strcpy(tbuf, matches[0]);
1925 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001926 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001927 {
1928 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001929 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001932 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001933 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (buf != NULL)
1935 {
1936 bufsize = newsize;
1937 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001938 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940
1941 vim_free(tbuf);
1942
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001943 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001945 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 num = 1;
1948 for (i = 0; i < num_matches; i++)
1949 {
1950 idx = i;
1951
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001952 // if we really wanted to, we could avoid this malloc and strcpy
1953 // by parsing matches[i] on the fly and placing stuff into buf
1954 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001955 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 continue;
1957 (void)strcpy(tbuf, matches[idx]);
1958
Bram Moolenaare16e5a92016-02-23 20:44:08 +01001959 if (strtok(tbuf, (const char *)"\t") == NULL
1960 || (fname = strtok(NULL, (const char *)"\t")) == NULL
1961 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
1962 {
1963 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01001965 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00001966 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001968 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001970 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001971 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (bufsize < newsize)
1973 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001974 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001975 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001979 vim_free(t_buf);
1980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 else
1982 bufsize = newsize;
1983 }
1984 if (buf != NULL)
1985 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001986 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001988 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001990 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
1991 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001993 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (cntxts[idx] != NULL)
1995 context = cntxts[idx];
1996 else
1997 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001998 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 if (bufsize < newsize)
2001 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002002 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002003 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002007 vim_free(t_buf);
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 else
2010 bufsize = newsize;
2011 }
2012 if (buf != NULL)
2013 {
2014 (void)sprintf(buf, cntxformat, context);
2015
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002016 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (msg_col + (int)strlen(buf) >= (int)Columns)
2018 msg_putchar('\n');
2019 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002020 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 msg_putchar('\n');
2022 }
2023 if (extra != NULL)
2024 {
2025 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002026 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002029 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 if (msg_col)
2032 msg_putchar('\n');
2033
2034 ui_breakcheck();
2035 if (got_int)
2036 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002037 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 break;
2039 }
2040
2041 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002042 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047
2048/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002049 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
2051 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002052cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002055 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 int bufpos = 0;
2057 char *cs_emsg;
2058 int maxlen;
2059 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002060 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 int n;
2062
2063 cs_emsg = _("E609: Cscope error: %s");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002064 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2066
2067 for (;;)
2068 {
2069 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002070 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2072 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002074 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (buf != NULL)
2076 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002077 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 buf[bufpos++] = ch;
2079 buf[bufpos] = NUL;
2080 if (bufpos >= epromptlen
2081 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2082 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002083 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 buf[bufpos - epromptlen] = NUL;
2085
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002086 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002087 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002089 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 (void)putc('\n', csinfo[i].to_fp);
2091 (void)fflush(csinfo[i].to_fp);
2092
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002093 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 bufpos = 0;
2095 buf[bufpos] = NUL;
2096 }
2097 }
2098 }
2099
2100 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2101 {
2102 if (n > 0)
2103 ch = getc(csinfo[i].fr_fp);
2104 if (ch == EOF)
2105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002109 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 cs_release_csp(i, TRUE);
2111 vim_free(buf);
2112 return CSCOPE_FAILURE;
2113 }
2114
2115 if (ch != CSCOPE_PROMPT[n])
2116 {
2117 ch = EOF;
2118 break;
2119 }
2120 }
2121
2122 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002123 continue; // didn't find the prompt
2124 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126
2127 vim_free(buf);
2128 return CSCOPE_SUCCESS;
2129}
2130
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002131#if defined(UNIX) && defined(SIGALRM)
2132/*
2133 * Used to catch and ignore SIGALRM below.
2134 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002135 static RETSIGTYPE
2136sig_handler SIGDEFARG(sigarg)
2137{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002138 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002139 SIGRETURN;
2140}
2141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002144 * Does the actual free'ing for the cs ptr with an optional flag of whether
2145 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
2147 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002148cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 /*
2151 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2152 */
2153 if (csinfo[i].to_fp != NULL)
2154 {
2155 (void)fputs("q\n", csinfo[i].to_fp);
2156 (void)fflush(csinfo[i].to_fp);
2157 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002158#if defined(UNIX)
2159 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002160 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002161 int pstat;
2162 pid_t pid;
2163
2164# if defined(HAVE_SIGACTION)
2165 struct sigaction sa, old;
2166
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002167 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002168 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002169 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002170# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002171 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002172# else
2173 sa.sa_flags = 0;
2174# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002175 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002176 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002177
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002178 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002179 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002180 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002181
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002182 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002183 alarm(0);
2184 sigaction(SIGALRM, &old, NULL);
2185# else
2186 int waited;
2187
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002188 // Can't use sigaction(), loop for two seconds. First yield the CPU
2189 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002190 sleep(0);
2191 for (waited = 0; waited < 40; ++waited)
2192 {
2193 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002194 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002195 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002196 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002197 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002198 }
2199# endif
2200 /*
2201 * If the cscope process is still running: kill it.
2202 * Safety check: If the PID would be zero here, the entire X session
2203 * would be killed. -1 and 1 are dangerous as well.
2204 */
2205 if (pid < 0 && csinfo[i].pid > 1)
2206 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002207# ifdef ECHILD
2208 int alive = TRUE;
2209
2210 if (waitpid_errno == ECHILD)
2211 {
2212 /*
2213 * When using 'vim -g', vim is forked and cscope process is
2214 * no longer a child process but a sibling. So waitpid()
2215 * fails with errno being ECHILD (No child processes).
2216 * Don't send SIGKILL to cscope immediately but wait
2217 * (polling) for it to exit normally as result of sending
2218 * the "q" command, hence giving it a chance to clean up
2219 * its temporary files.
2220 */
2221 int waited;
2222
2223 sleep(0);
2224 for (waited = 0; waited < 40; ++waited)
2225 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002226 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002227 if (kill(csinfo[i].pid, 0) != 0)
2228 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002229 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002230 break;
2231 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002232 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002233 }
2234 }
2235 if (alive)
2236# endif
2237 {
2238 kill(csinfo[i].pid, SIGKILL);
2239 (void)waitpid(csinfo[i].pid, &pstat, 0);
2240 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002241 }
2242 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002243#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002244 if (csinfo[i].hProc != NULL)
2245 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002246 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002247 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2248 TerminateProcess(csinfo[i].hProc, 0);
2249 CloseHandle(csinfo[i].hProc);
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252
2253 if (csinfo[i].fr_fp != NULL)
2254 (void)fclose(csinfo[i].fr_fp);
2255 if (csinfo[i].to_fp != NULL)
2256 (void)fclose(csinfo[i].to_fp);
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (freefnpp)
2259 {
2260 vim_free(csinfo[i].fname);
2261 vim_free(csinfo[i].ppath);
2262 vim_free(csinfo[i].flags);
2263 }
2264
2265 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268
2269/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002270 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002273cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2276 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002277 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002279 if (csinfo_size == 0)
2280 return CSCOPE_SUCCESS;
2281
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002282 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002283 dblist = ALLOC_MULT(char *, csinfo_size);
2284 pplist = ALLOC_MULT(char *, csinfo_size);
2285 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (dblist == NULL || pplist == NULL || fllist == NULL)
2287 {
2288 vim_free(dblist);
2289 vim_free(pplist);
2290 vim_free(fllist);
2291 return CSCOPE_FAILURE;
2292 }
2293
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002294 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 dblist[i] = csinfo[i].fname;
2297 pplist[i] = csinfo[i].ppath;
2298 fllist[i] = csinfo[i].flags;
2299 if (csinfo[i].fname != NULL)
2300 cs_release_csp(i, FALSE);
2301 }
2302
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002303 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002304 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 if (dblist[i] != NULL)
2307 {
2308 cs_add_common(dblist[i], pplist[i], fllist[i]);
2309 if (p_csverbose)
2310 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002311 // don't use smsg_attr() because we want to display the
2312 // connection number in the same line as
2313 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002315 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 }
2318 vim_free(dblist[i]);
2319 vim_free(pplist[i]);
2320 vim_free(fllist[i]);
2321 }
2322 vim_free(dblist);
2323 vim_free(pplist);
2324 vim_free(fllist);
2325
2326 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002327 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331
2332/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002333 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * (Prepends ppath, if there is one and if it's not already prepended,
2335 * otherwise just uses the name found.)
2336 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002337 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002339 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 */
2341 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002342cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002344 char *fullname;
2345 int len;
2346 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002349 * Ppath is freed when we destroy the cscope connection.
2350 * Fullname is freed after cs_make_vim_style_matches, after it's been
2351 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002353 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002355 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002356 else if (p_csre && csinfo[i].fname != NULL)
2357 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002358 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2359 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002360 csdir = alloc(MAXPATHL);
2361 if (csdir != NULL)
2362 {
2363 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002364 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002365 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002366 len += (int)STRLEN(csdir);
2367 }
2368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002370 // Note/example: this won't work if the cscope output already starts
2371 // "../.." and the prefix path is also "../..". if something like this
2372 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002373 if (csinfo[i].ppath != NULL
2374 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2375 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002376#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002377 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002379 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002380 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002381 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002382 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2383 }
2384 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002385 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002386 // Check for csdir to be non empty to avoid empty path concatenated to
2387 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002388 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002391 {
2392 fullname = (char *)vim_strsave((char_u *)name);
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002395 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399
2400/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002401 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002404cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 short i;
2407 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002408 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 else
2410 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002411 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002414 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 if (csinfo[i].fname == NULL)
2417 continue;
2418
2419 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2422 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002423 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 i, (long)csinfo[i].pid, csinfo[i].fname);
2425 }
2426 }
2427
2428 wait_return(TRUE);
2429 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002430}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaar02b06312007-09-06 15:39:22 +00002432
2433/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002434 * Only called when VIM exits to quit any cscope sessions.
2435 */
2436 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002437cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002438{
2439 int i;
2440
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002441 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002442 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002443 vim_free(csinfo);
2444 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002445}
2446
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002447#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002449#if defined(FEAT_EVAL) || defined(PROTO)
2450
2451/*
2452 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2453 *
2454 * Checks the existence of a cscope connection.
2455 */
2456 void
2457f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2458{
2459# ifdef FEAT_CSCOPE
2460 int num = 0;
2461 char_u *dbpath = NULL;
2462 char_u *prepend = NULL;
2463 char_u buf[NUMBUFLEN];
2464
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002465 if (in_vim9script()
2466 && (check_for_opt_number_arg(argvars, 0) == FAIL
2467 || (argvars[0].v_type != VAR_UNKNOWN
2468 && (check_for_opt_string_arg(argvars, 1) == FAIL
2469 || (argvars[1].v_type != VAR_UNKNOWN
2470 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2471 return;
2472
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002473 if (argvars[0].v_type != VAR_UNKNOWN
2474 && argvars[1].v_type != VAR_UNKNOWN)
2475 {
2476 num = (int)tv_get_number(&argvars[0]);
2477 dbpath = tv_get_string(&argvars[1]);
2478 if (argvars[2].v_type != VAR_UNKNOWN)
2479 prepend = tv_get_string_buf(&argvars[2], buf);
2480 }
2481
2482 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2483# endif
2484}
2485
2486#endif // FEAT_EVAL