blob: 4e313cebc195871c72be52a1dc53ee93c4950f8d [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{
465 char *stat_emsg = _("E563: stat(%s) error: %d");
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200466 char *buf = alloc(strlen(stat_emsg) + MAXPATHL + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468 if (buf != NULL)
469 {
470 (void)sprintf(buf, stat_emsg, fname, errno);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100471 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 vim_free(buf);
473 }
474 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100475 (void)emsg(_("E563: stat error"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476}
477
478
479/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100480 * The common routine to add a new cscope connection. Called by
481 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * routine uses a number of goto statements.
483 */
484 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100485cs_add_common(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100486 char *arg1, // filename - may contain environment variables
487 char *arg2, // prepend path - may contain environment variables
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100488 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200490 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000491 int ret;
492 char *fname = NULL;
493 char *fname2 = NULL;
494 char *ppath = NULL;
495 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200496 int len;
497 int usedlen = 0;
498 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100500 // get the filename (arg1), expand it, and try to stat it
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200501 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 goto add_err;
503
504 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200505 len = (int)STRLEN(fname);
506 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200507 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200508 (char_u **)&fname, &fbuf, &len);
509 if (fname == NULL)
510 goto add_err;
511 fname = (char *)vim_strnsave((char_u *)fname, len);
512 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200513
Bram Moolenaar8767f522016-07-01 17:17:39 +0200514 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (ret < 0)
516 {
517staterr:
518 if (p_csverbose)
519 cs_stat_emsg(fname);
520 goto add_err;
521 }
522
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100523 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (arg2 != NULL)
525 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200526 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200528 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 goto add_err;
530
531 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200532 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (ret < 0)
534 goto staterr;
535 }
536
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100537 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200538 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200540 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (fname2 == NULL)
542 goto add_err;
543
544 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100545#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 || fname[strlen(fname)-1] == '\\'
547#endif
548 )
549 {
550 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200551 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 break;
553 }
554 if (fname[0] == '\0')
555 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
556 else
557 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
558
Bram Moolenaar8767f522016-07-01 17:17:39 +0200559 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (ret < 0)
561 {
562 if (p_csverbose)
563 cs_stat_emsg(fname2);
564 goto add_err;
565 }
566
567 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
572 }
573 else
574 {
575 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 (void)semsg(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 _("E564: %s is not a directory or a valid cscope database"),
578 fname);
579 goto add_err;
580 }
581
582 if (i != -1)
583 {
584 if (cs_create_connection(i) == CSCOPE_FAILURE
585 || cs_read_prompt(i) == CSCOPE_FAILURE)
586 {
587 cs_release_csp(i, TRUE);
588 goto add_err;
589 }
590
591 if (p_csverbose)
592 {
593 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100594 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100595 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 csinfo[i].fname);
597 }
598 }
599
600 vim_free(fname);
601 vim_free(fname2);
602 vim_free(ppath);
603 return CSCOPE_SUCCESS;
604
605add_err:
606 vim_free(fname2);
607 vim_free(fname);
608 vim_free(ppath);
609 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100610}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612
613 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100614cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100617}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619
620 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100621cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000623 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100624}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626
627/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100628 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 */
630 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100631cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 short i;
634 short cnt = 0;
635
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000636 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 if (csinfo[i].fname != NULL)
639 cnt++;
640 }
641 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100642}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100645cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100646 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100648 semsg(_("E262: error reading cscope connection %d"), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651#define CSREAD_BUFSIZE 2048
652/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100653 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 */
655 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100656cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 char *stok;
659 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100660 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200662 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (buf == NULL)
664 return 0;
665 for (;;)
666 {
667 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
668 {
669 if (feof(csinfo[idx].fr_fp))
670 errno = EIO;
671
672 cs_reading_emsg(idx);
673
674 vim_free(buf);
675 return -1;
676 }
677
678 /*
679 * If the database is out of date, or there's some other problem,
680 * cscope will output error messages before the number-of-lines output.
681 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000682 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100683 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100685 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100686 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if ((stok = strtok(buf, (const char *)" ")) == NULL)
688 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000689 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 continue;
691
692 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
693 continue;
694 nlines = atoi(stok);
695 if (nlines < 0)
696 {
697 nlines = 0;
698 break;
699 }
700
701 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
702 continue;
703 if (strncmp((const char *)stok, "lines", 5))
704 continue;
705
706 break;
707 }
708
709 vim_free(buf);
710 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100711}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
713
714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 * Creates the actual cscope command query from what the user entered.
716 */
717 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100718cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 char *cmd;
721 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000722 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724 switch (csoption[0])
725 {
726 case '0' : case 's' :
727 search = 0;
728 break;
729 case '1' : case 'g' :
730 search = 1;
731 break;
732 case '2' : case 'd' :
733 search = 2;
734 break;
735 case '3' : case 'c' :
736 search = 3;
737 break;
738 case '4' : case 't' :
739 search = 4;
740 break;
741 case '6' : case 'e' :
742 search = 6;
743 break;
744 case '7' : case 'f' :
745 search = 7;
746 break;
747 case '8' : case 'i' :
748 search = 8;
749 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200750 case '9' : case 'a' :
751 search = 9;
752 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 default :
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 (void)emsg(_("E561: unknown cscope search type"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 cs_usage_msg(Find);
756 return NULL;
757 }
758
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100759 // Skip white space before the patter, except for text and pattern search,
760 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000761 pat = pattern;
762 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100763 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000764 ++pat;
765
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200766 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return NULL;
768
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000769 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100772}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774
775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 * This piece of code was taken/adapted from nvi. do we need to add
777 * the BSD license notice?
778 */
779 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100780cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000782#ifdef UNIX
783 int to_cs[2], from_cs[2];
784#endif
785 int len;
786 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100787#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000788 int fd;
789 SECURITY_ATTRIBUTES sa;
790 PROCESS_INFORMATION pi;
791 STARTUPINFO si;
792 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
793 HANDLE stdin_rd, stdout_rd;
794 HANDLE stdout_wr, stdin_wr;
795 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
797# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000798# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#endif
802
Bram Moolenaar02b06312007-09-06 15:39:22 +0000803#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 /*
805 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
806 * from_cs[0] and writes to to_cs[1].
807 */
808 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
809 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100811 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812err_closing:
813 if (to_cs[0] != -1)
814 (void)close(to_cs[0]);
815 if (to_cs[1] != -1)
816 (void)close(to_cs[1]);
817 if (from_cs[0] != -1)
818 (void)close(from_cs[0]);
819 if (from_cs[1] != -1)
820 (void)close(from_cs[1]);
821 return CSCOPE_FAILURE;
822 }
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 switch (csinfo[i].pid = fork())
825 {
826 case -1:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 (void)emsg(_("E622: Could not fork for cscope"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 goto err_closing;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100829 case 0: // child: run cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (dup2(to_cs[0], STDIN_FILENO) == -1)
831 PERROR("cs_create_connection 1");
832 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
833 PERROR("cs_create_connection 2");
834 if (dup2(from_cs[1], STDERR_FILENO) == -1)
835 PERROR("cs_create_connection 3");
836
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100837 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 (void)close(to_cs[1]);
839 (void)close(from_cs[0]);
840#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100841 // MSWIN
842 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000843 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
844 sa.bInheritHandle = TRUE;
845 sa.lpSecurityDescriptor = NULL;
846
847 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
848 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100850 (void)emsg(_("E566: Could not create cscope pipes"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000851err_closing:
852 if (pipe_stdin)
853 {
854 CloseHandle(stdin_rd);
855 CloseHandle(stdin_wr);
856 }
857 if (pipe_stdout)
858 {
859 CloseHandle(stdout_rd);
860 CloseHandle(stdout_wr);
861 }
862 return CSCOPE_FAILURE;
863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100865 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200866 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
868#ifdef UNIX
869 return CSCOPE_FAILURE;
870#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100871 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 goto err_closing;
873#endif
874 }
875 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
876
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100877 // alloc space to hold the cscope command
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000878 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (csinfo[i].ppath)
880 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100881 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200882 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
884 vim_free(prog);
885#ifdef UNIX
886 return CSCOPE_FAILURE;
887#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100888 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 goto err_closing;
890#endif
891 }
892 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000894 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896
897 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000898 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200900 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
902 vim_free(prog);
903 vim_free(ppath);
904#ifdef UNIX
905 return CSCOPE_FAILURE;
906#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100907 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 goto err_closing;
909#endif
910 }
911
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100912 // run the cscope command; is there execl for non-unix systems?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#if defined(UNIX)
914 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
915#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100916 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
918#endif
919 if (csinfo[i].ppath != NULL)
920 {
921 (void)strcat(cmd, " -P");
922 (void)strcat(cmd, csinfo[i].ppath);
923 }
924 if (csinfo[i].flags != NULL)
925 {
926 (void)strcat(cmd, " ");
927 (void)strcat(cmd, csinfo[i].flags);
928 }
929# ifdef UNIX
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100930 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 vim_free(prog);
932# endif
933 vim_free(ppath);
934
935#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200936# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100937 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200938# if defined(HAVE_SETSID)
939 (void)setsid();
940# else
941 if (setpgid(0, 0) == -1)
942 PERROR(_("cs_create_connection setpgid failed"));
943# endif
944# endif
Bram Moolenaar856b9fe2009-05-16 14:16:02 +0000945 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 PERROR(_("cs_create_connection exec failed"));
947
948 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100949 // NOTREACHED
950 default: // parent.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 /*
952 * Save the file descriptors for later duplication, and
953 * reopen as streams.
954 */
955 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
956 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
957 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
958 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
959
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100960 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 (void)close(to_cs[0]);
962 (void)close(from_cs[1]);
963
964 break;
965 }
Bram Moolenaar02b06312007-09-06 15:39:22 +0000966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100968 // MSWIN
969 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +0000970 GetStartupInfo(&si);
971 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100972 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +0000973 si.hStdOutput = stdout_wr;
974 si.hStdError = stdout_wr;
975 si.hStdInput = stdin_rd;
976 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
977 NULL, NULL, &si, &pi);
978 vim_free(prog);
979 vim_free(cmd);
980
981 if (!created)
982 {
983 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 (void)emsg(_("E623: Could not spawn cscope process"));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000985 goto err_closing;
986 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +0000988 csinfo[i].pid = pi.dwProcessId;
989 csinfo[i].hProc = pi.hProcess;
990 CloseHandle(pi.hThread);
991
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100992 // TODO - tidy up after failure to create files on pipe handles.
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000993 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
994 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000995 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
996 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000997 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
998 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +0000999 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1000 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1001
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001002 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001003 CloseHandle(stdin_rd);
1004 CloseHandle(stdout_wr);
1005
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001006#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001009}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011
1012/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001013 * Query cscope using command line interface. Parse the output and use tselect
1014 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 *
1016 * returns TRUE if we jump to a tag or abort, FALSE if not.
1017 */
1018 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001019cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001022 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 if (cs_check_for_connections() == FALSE)
1025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 (void)emsg(_("E567: no cscope connections"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 return FALSE;
1028 }
1029
1030 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1031 {
1032 cs_usage_msg(Find);
1033 return FALSE;
1034 }
1035
1036 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001037 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
1039 cs_usage_msg(Find);
1040 return FALSE;
1041 }
1042
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001043 /*
1044 * Let's replace the NULs written by strtok() with spaces - we need the
1045 * spaces to correctly display the quickfix/location list window's title.
1046 */
1047 for (i = 0; i < eap_arg_len; ++i)
1048 if (NUL == eap->arg[i])
1049 eap->arg[i] = ' ';
1050
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001051 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001052 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055
1056/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001057 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
1059 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001060cs_find_common(
1061 char *opt,
1062 char *pat,
1063 int forceit,
1064 int verbose,
1065 int use_ll UNUSED,
1066 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 int i;
1069 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001070 int *nummatches;
1071 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_QUICKFIX
1073 char cmdletter;
1074 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001075
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001076 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001077 switch (opt[0])
1078 {
1079 case '0' :
1080 cmdletter = 's';
1081 break;
1082 case '1' :
1083 cmdletter = 'g';
1084 break;
1085 case '2' :
1086 cmdletter = 'd';
1087 break;
1088 case '3' :
1089 cmdletter = 'c';
1090 break;
1091 case '4' :
1092 cmdletter = 't';
1093 break;
1094 case '6' :
1095 cmdletter = 'e';
1096 break;
1097 case '7' :
1098 cmdletter = 'f';
1099 break;
1100 case '8' :
1101 cmdletter = 'i';
1102 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001103 case '9' :
1104 cmdletter = 'a';
1105 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001106 default :
1107 cmdletter = opt[0];
1108 }
1109
1110 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1111 if (qfpos != NULL)
1112 {
1113 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001114 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001115 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1116 {
1117 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001118 char *buf = alloc(strlen(nf));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001119
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001120 // strlen will be enough because we use chars
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001121 if (buf != NULL)
1122 {
1123 sprintf(buf, nf, *qfpos, *(qfpos-1));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001124 (void)emsg(buf);
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001125 vim_free(buf);
1126 }
1127 return FALSE;
1128 }
1129
Bram Moolenaar21662be2016-11-06 14:46:44 +01001130 if (*qfpos != '0'
1131 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1132 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001133 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001134# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001135 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001136 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001137# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001138 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#endif
1141
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001142 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 cmd = cs_create_cmd(opt, pat);
1144 if (cmd == NULL)
1145 return FALSE;
1146
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001147 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001148 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001149 {
1150 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001151 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001152 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001153
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001154 // Send query to all open connections, then count the total number
1155 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001156 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 nummatches[i] = 0;
1158 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001159 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001161 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 continue;
1163
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001164 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1166 (void)fflush(csinfo[i].to_fp);
1167
1168 nummatches[i] = cs_cnt_matches(i);
1169
1170 if (nummatches[i] > -1)
1171 totmatches += nummatches[i];
1172
1173 if (nummatches[i] == 0)
1174 (void)cs_read_prompt(i);
1175 }
1176 vim_free(cmd);
1177
1178 if (totmatches == 0)
1179 {
1180 char *nf = _("E259: no matches found for cscope query %s of %s");
1181 char *buf;
1182
1183 if (!verbose)
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001184 {
1185 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 return FALSE;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001189 buf = alloc(strlen(opt) + strlen(pat) + strlen(nf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 (void)emsg(nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 else
1193 {
1194 sprintf(buf, nf, opt, pat);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001195 (void)emsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 vim_free(buf);
1197 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001198 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 return FALSE;
1200 }
1201
1202#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001203 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001205 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001206 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001207 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001208 qf_info_T *qi = NULL;
1209 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001211 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001212 if (f == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 semsg(_(e_notopen), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001216 cs_file_results(f, nummatches);
1217 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001218 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001219 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001220 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001221 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001222 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001224 if (postponed_split != 0)
1225 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001226 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001228 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001229 postponed_split = 0;
1230 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001231
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001232 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1233 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001234 if (use_ll)
1235 /*
1236 * In the location list window, use the displayed location
1237 * list. Otherwise, use the location list for the window.
1238 */
1239 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1240 ? wp->w_llist_ref : wp->w_llist;
1241 qf_jump(qi, 0, 0, forceit);
1242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 mch_remove(tmp);
1245 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001246 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 return TRUE;
1248 }
1249 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001250#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001252 char **matches = NULL, **contexts = NULL;
1253 int matched = 0;
1254
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001255 // read output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1257 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001258 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (matches == NULL)
1260 return FALSE;
1261
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001262 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1265 }
1266
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001267}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001270 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001273cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 cscmd_T *cmdp = cs_cmds;
1276
Bram Moolenaar32526b32019-01-19 17:43:09 +01001277 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 while (cmdp->name != NULL)
1279 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001280 char *help = _(cmdp->help);
1281 int space_cnt = 30 - vim_strsize((char_u *)help);
1282
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001283 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001284 if (space_cnt < 0)
1285 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001286 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001287 cmdp->name,
1288 help, space_cnt, " ",
1289 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001291 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001292 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001293 " c: Find functions calling this function\n"
1294 " d: Find functions called by this function\n"
1295 " e: Find this egrep pattern\n"
1296 " f: Find this file\n"
1297 " g: Find this definition\n"
1298 " i: Find files #including this file\n"
1299 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001300 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 cmdp++;
1303 }
1304
1305 wait_return(TRUE);
1306 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001307}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001311clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 csinfo[i].fname = NULL;
1314 csinfo[i].ppath = NULL;
1315 csinfo[i].flags = NULL;
1316#if defined(UNIX)
1317 csinfo[i].st_dev = (dev_t)0;
1318 csinfo[i].st_ino = (ino_t)0;
1319#else
1320 csinfo[i].nVolume = 0;
1321 csinfo[i].nIndexHigh = 0;
1322 csinfo[i].nIndexLow = 0;
1323#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001324 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 csinfo[i].fr_fp = NULL;
1326 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001327#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001328 csinfo[i].hProc = NULL;
1329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330}
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001333 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001336cs_insert_filelist(
1337 char *fname,
1338 char *ppath,
1339 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001340 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
1342 short i, j;
1343#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 BY_HANDLE_FILE_INFORMATION bhfi;
1345
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001346 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001348 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1349 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (p_csverbose)
1351 {
1352 char *cant_msg = _("E625: cannot open cscope database: %s");
1353 char *winmsg = GetWin32Error();
1354
1355 if (winmsg != NULL)
1356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 LocalFree(winmsg);
1359 }
1360 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001361 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001365
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001366 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 (void)emsg(_("E626: cannot get cscope database information"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371#endif
1372
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001373 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001374 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 if (csinfo[j].fname != NULL
1377#if defined(UNIX)
1378 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1379#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001380 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001381 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001382 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1383 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001384 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1386 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1387#endif
1388 )
1389 {
1390 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001391 (void)emsg(_("E568: duplicate cscope database not added"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 return -1;
1393 }
1394
1395 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001396 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398
1399 if (i == -1)
1400 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001401 i = csinfo_size;
1402 if (csinfo_size == 0)
1403 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404 // First time allocation: allocate only 1 connection. It should
1405 // be enough for most users. If more is needed, csinfo will be
1406 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001407 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001408 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001409 }
1410 else
1411 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001412 csinfo_T *t_csinfo = csinfo;
1413
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001414 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001415 csinfo_size *= 2;
1416 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001417 if (csinfo == NULL)
1418 {
1419 vim_free(t_csinfo);
1420 csinfo_size = 0;
1421 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001422 }
1423 if (csinfo == NULL)
1424 return -1;
1425 for (j = csinfo_size/2; j < csinfo_size; j++)
1426 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001429 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 return -1;
1431
1432 (void)strcpy(csinfo[i].fname, (const char *)fname);
1433
1434 if (ppath != NULL)
1435 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001436 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001438 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return -1;
1440 }
1441 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1442 } else
1443 csinfo[i].ppath = NULL;
1444
1445 if (flags != NULL)
1446 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001447 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001449 VIM_CLEAR(csinfo[i].fname);
1450 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return -1;
1452 }
1453 (void)strcpy(csinfo[i].flags, (const char *)flags);
1454 } else
1455 csinfo[i].flags = NULL;
1456
1457#if defined(UNIX)
1458 csinfo[i].st_dev = sb->st_dev;
1459 csinfo[i].st_ino = sb->st_ino;
1460
1461#else
1462 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1463 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1464 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1465#endif
1466 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
1469
1470/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001471 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 */
1473 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001474cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 cscmd_T *cmdp;
1477 char *stok;
1478 size_t len;
1479
1480 if (eap->arg == NULL)
1481 return NULL;
1482
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001484 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1487 return NULL;
1488
1489 len = strlen(stok);
1490 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1491 {
1492 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1493 return (cmdp);
1494 }
1495 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498
1499/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001500 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001503cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 char *stok;
1506 short i;
1507
1508 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1509 {
1510 cs_usage_msg(Kill);
1511 return CSCOPE_FAILURE;
1512 }
1513
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001514 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1516 || (strlen(stok) < 3 && stok[0] == '-'
1517 && VIM_ISDIGIT((int)(stok[1]))))
1518 i = atoi(stok);
1519 else
1520 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001521 // It must be part of a name. We will try to find a match
1522 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001523 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1526 break;
1527 }
1528 }
1529
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001530 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 if (p_csverbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001533 (void)semsg(_("E261: cscope connection %s not found"), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
1536 {
1537 if (i == -1)
1538 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001539 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 if (csinfo[i].fname)
1542 cs_kill_execute(i, csinfo[i].fname);
1543 }
1544 }
1545 else
1546 cs_kill_execute(i, stok);
1547 }
1548
1549 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552
1553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 * Actually kills a specific cscope connection.
1555 */
1556 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001557cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001558 int i, // cscope table index
1559 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 if (p_csverbose)
1562 {
1563 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001564 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001565 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 cs_release_csp(i, TRUE);
1568}
1569
1570
1571/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001572 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1574 * the type of the tag you are looking for. for example, in Darren Hiebert's
1575 * ctags (the one that comes with vim), #define's use a line number to find the
1576 * tag in a file while function definitions use a regexp search pattern.
1577 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001578 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 * quirky (and probably other things i don't know about):
1580 *
1581 * if you have "# define" in your source file, which is
1582 * perfectly legal, cscope thinks you have "#define". this
1583 * will result in a failed regexp search. :(
1584 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001585 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 * would still have to be modified to escape all the special regular expression
1587 * characters to comply with ctags formatting.
1588 */
1589 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001590cs_make_vim_style_matches(
1591 char *fname,
1592 char *slno,
1593 char *search,
1594 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001596 // vim style is ctags:
1597 //
1598 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1599 //
1600 // but as mentioned above, we'll always use the line number and
1601 // put the search pattern (if one exists) as "extra"
1602 //
1603 // buf is used as part of vim's method of handling tags, and
1604 // (i think) vim frees it when you pop your tags and get replaced
1605 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 char *buf;
1607 int amt;
1608
1609 if (search != NULL)
1610 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001611 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001612 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 return NULL;
1614
1615 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1616 }
1617 else
1618 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001619 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001620 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 return NULL;
1622
1623 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1624 }
1625
1626 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629
1630/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001631 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
1633 * Store: keep a ptr to the (malloc'd) memory of matches originally
1634 * generated from cs_find(). the matches are originally lines directly
1635 * from cscope output, but transformed to look like something out of a
1636 * ctags. see cs_make_vim_style_matches for more details.
1637 *
1638 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1639 * the next line from the cscope output. it basically keeps track of which
1640 * lines have been "used" and returns the next one.
1641 *
1642 * Free: frees up everything and resets
1643 *
1644 * Print: prints the tags
1645 */
1646 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001647cs_manage_matches(
1648 char **matches,
1649 char **contexts,
1650 int totmatches,
1651 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 static char **mp = NULL;
1654 static char **cp = NULL;
1655 static int cnt = -1;
1656 static int next = -1;
1657 char *p = NULL;
1658
1659 switch (cmd)
1660 {
1661 case Store:
1662 assert(matches != NULL);
1663 assert(totmatches > 0);
1664 if (mp != NULL || cp != NULL)
1665 (void)cs_manage_matches(NULL, NULL, -1, Free);
1666 mp = matches;
1667 cp = contexts;
1668 cnt = totmatches;
1669 next = 0;
1670 break;
1671 case Get:
1672 if (next >= cnt)
1673 return NULL;
1674
1675 p = mp[next];
1676 next++;
1677 break;
1678 case Free:
1679 if (mp != NULL)
1680 {
1681 if (cnt > 0)
1682 while (cnt--)
1683 {
1684 vim_free(mp[cnt]);
1685 if (cp != NULL)
1686 vim_free(cp[cnt]);
1687 }
1688 vim_free(mp);
1689 vim_free(cp);
1690 }
1691 mp = NULL;
1692 cp = NULL;
1693 cnt = 0;
1694 next = 0;
1695 break;
1696 case Print:
1697 cs_print_tags_priv(mp, cp, cnt);
1698 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001699 default: // should not reach here
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001700 iemsg(_("E570: fatal error in cs_manage_matches"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 return NULL;
1702 }
1703
1704 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001705}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
1707
1708/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001709 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 */
1711 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001712cs_parse_results(
1713 int cnumber,
1714 char *buf,
1715 int bufsize,
1716 char **context,
1717 char **linenumber,
1718 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 int ch;
1721 char *p;
1722 char *name;
1723
1724 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1725 {
1726 if (feof(csinfo[cnumber].fr_fp))
1727 errno = EIO;
1728
1729 cs_reading_emsg(cnumber);
1730
1731 return NULL;
1732 }
1733
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001734 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if ((p = strchr(buf, '\n')) == NULL)
1736 {
1737 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1738 ;
1739 return NULL;
1740 }
1741 *p = '\0';
1742
1743 /*
1744 * cscope output is in the following format:
1745 *
1746 * <filename> <context> <line number> <pattern>
1747 */
1748 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1749 return NULL;
1750 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1751 return NULL;
1752 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1753 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001754 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001756 // --- nvi ---
1757 // If the file is older than the cscope database, that is,
1758 // the database was built since the file was last modified,
1759 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (strcmp(*search, "<unknown>") == 0)
1761 *search = NULL;
1762
1763 name = cs_resolve_file(cnumber, name);
1764 return name;
1765}
1766
Bram Moolenaarc716c302006-01-21 22:12:51 +00001767#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001769 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 */
1771 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001772cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int i, j;
1775 char *buf;
1776 char *search, *slno;
1777 char *fullname;
1778 char *cntx;
1779 char *context;
1780
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001781 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (buf == NULL)
1783 return;
1784
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001785 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 if (nummatches_a[i] < 1)
1788 continue;
1789
1790 for (j = 0; j < nummatches_a[i]; j++)
1791 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001792 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1793 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 continue;
1795
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001796 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001797 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001798 {
1799 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 if (strcmp(cntx, "<global>")==0)
1804 strcpy(context, "<<global>>");
1805 else
1806 sprintf(context, "<<%s>>", cntx);
1807
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001808 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1810 else
1811 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1812
1813 vim_free(context);
1814 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001815 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 (void)cs_read_prompt(i);
1818
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001819 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 vim_free(buf);
1821}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001825 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1826 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001827 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 */
1829 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001830cs_fill_results(
1831 char *tagstr,
1832 int totmatches,
1833 int *nummatches_a,
1834 char ***matches_p,
1835 char ***cntxts_p,
1836 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int i, j;
1839 char *buf;
1840 char *search, *slno;
1841 int totsofar = 0;
1842 char **matches = NULL;
1843 char **cntxts = NULL;
1844 char *fullname;
1845 char *cntx;
1846
1847 assert(totmatches > 0);
1848
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001849 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (buf == NULL)
1851 return;
1852
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001853 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001855 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 goto parse_out;
1857
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001858 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 if (nummatches_a[i] < 1)
1861 continue;
1862
1863 for (j = 0; j < nummatches_a[i]; j++)
1864 {
1865 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1866 &slno, &search)) == NULL)
1867 continue;
1868
1869 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1870 search, tagstr);
1871
1872 vim_free(fullname);
1873
1874 if (strcmp(cntx, "<global>") == 0)
1875 cntxts[totsofar] = NULL;
1876 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001877 // note: if vim_strsave returns NULL, then the context
1878 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1880
1881 if (matches[totsofar] != NULL)
1882 totsofar++;
1883
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001884 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 (void)cs_read_prompt(i);
1887
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001888 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001891 if (totsofar == 0)
1892 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001893 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001894 VIM_CLEAR(matches);
1895 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 *matched = totsofar;
1898 *matches_p = matches;
1899 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001905/*
1906 * get the requested path components
1907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001909cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 int i;
1912 char *s;
1913
1914 if (p_cspc == 0)
1915 return path;
1916
1917 s = path + strlen(path) - 1;
1918 for (i = 0; i < p_cspc; ++i)
1919 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001920#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 && *--s != '\\'
1922#endif
1923 )
1924 ;
1925 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001926#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 || (s > path && *s == '\\')
1928#endif
1929 )
1930 ++s;
1931 return s;
1932}
1933
1934/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001935 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
1937 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001938cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
1940 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001941 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001942 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int newsize = 0;
1944 char *ptag;
1945 char *fname, *lno, *extra, *tbuf;
1946 int i, idx, num;
1947 char *globalcntx = "GLOBAL";
1948 char *cntxformat = " <<%s>>";
1949 char *context;
1950 char *cstag_msg = _("Cscope tag: %s");
1951 char *csfmt_str = "%4d %6s ";
1952
Bram Moolenaar4033c552017-09-16 20:54:51 +02001953 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001955 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 return;
1957
1958 strcpy(tbuf, matches[0]);
1959 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001960 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001961 {
1962 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001963 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001966 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001967 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (buf != NULL)
1969 {
1970 bufsize = newsize;
1971 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001972 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
1975 vim_free(tbuf);
1976
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001977 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001979 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 num = 1;
1982 for (i = 0; i < num_matches; i++)
1983 {
1984 idx = i;
1985
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001986 // if we really wanted to, we could avoid this malloc and strcpy
1987 // by parsing matches[i] on the fly and placing stuff into buf
1988 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001989 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 continue;
1991 (void)strcpy(tbuf, matches[idx]);
1992
Bram Moolenaare16e5a92016-02-23 20:44:08 +01001993 if (strtok(tbuf, (const char *)"\t") == NULL
1994 || (fname = strtok(NULL, (const char *)"\t")) == NULL
1995 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
1996 {
1997 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01001999 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002000 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002002 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002004 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002005 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (bufsize < newsize)
2007 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002008 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002009 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002013 vim_free(t_buf);
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else
2016 bufsize = newsize;
2017 }
2018 if (buf != NULL)
2019 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002020 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002022 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002024 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2025 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002027 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (cntxts[idx] != NULL)
2029 context = cntxts[idx];
2030 else
2031 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002032 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 if (bufsize < newsize)
2035 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002036 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002037 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002041 vim_free(t_buf);
2042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 else
2044 bufsize = newsize;
2045 }
2046 if (buf != NULL)
2047 {
2048 (void)sprintf(buf, cntxformat, context);
2049
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002050 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (msg_col + (int)strlen(buf) >= (int)Columns)
2052 msg_putchar('\n');
2053 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002054 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 msg_putchar('\n');
2056 }
2057 if (extra != NULL)
2058 {
2059 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
2062
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002063 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 if (msg_col)
2066 msg_putchar('\n');
2067
2068 ui_breakcheck();
2069 if (got_int)
2070 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002071 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 break;
2073 }
2074
2075 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002076 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002079}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081
2082/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002083 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002086cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002089 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 int bufpos = 0;
2091 char *cs_emsg;
2092 int maxlen;
2093 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002094 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 int n;
2096
2097 cs_emsg = _("E609: Cscope error: %s");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002098 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2100
2101 for (;;)
2102 {
2103 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002104 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2106 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002107 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002108 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (buf != NULL)
2110 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002111 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 buf[bufpos++] = ch;
2113 buf[bufpos] = NUL;
2114 if (bufpos >= epromptlen
2115 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2116 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002117 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 buf[bufpos - epromptlen] = NUL;
2119
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002120 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002123 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 (void)putc('\n', csinfo[i].to_fp);
2125 (void)fflush(csinfo[i].to_fp);
2126
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002127 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 bufpos = 0;
2129 buf[bufpos] = NUL;
2130 }
2131 }
2132 }
2133
2134 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2135 {
2136 if (n > 0)
2137 ch = getc(csinfo[i].fr_fp);
2138 if (ch == EOF)
2139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002143 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 cs_release_csp(i, TRUE);
2145 vim_free(buf);
2146 return CSCOPE_FAILURE;
2147 }
2148
2149 if (ch != CSCOPE_PROMPT[n])
2150 {
2151 ch = EOF;
2152 break;
2153 }
2154 }
2155
2156 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002157 continue; // didn't find the prompt
2158 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160
2161 vim_free(buf);
2162 return CSCOPE_SUCCESS;
2163}
2164
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002165#if defined(UNIX) && defined(SIGALRM)
2166/*
2167 * Used to catch and ignore SIGALRM below.
2168 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002169 static RETSIGTYPE
2170sig_handler SIGDEFARG(sigarg)
2171{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002172 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002173 SIGRETURN;
2174}
2175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002178 * Does the actual free'ing for the cs ptr with an optional flag of whether
2179 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002182cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 /*
2185 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2186 */
2187 if (csinfo[i].to_fp != NULL)
2188 {
2189 (void)fputs("q\n", csinfo[i].to_fp);
2190 (void)fflush(csinfo[i].to_fp);
2191 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002192#if defined(UNIX)
2193 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002194 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002195 int pstat;
2196 pid_t pid;
2197
2198# if defined(HAVE_SIGACTION)
2199 struct sigaction sa, old;
2200
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002201 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002202 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002203 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002204# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002205 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002206# else
2207 sa.sa_flags = 0;
2208# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002209 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002210 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002211
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002212 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002213 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002214 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002215
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002216 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002217 alarm(0);
2218 sigaction(SIGALRM, &old, NULL);
2219# else
2220 int waited;
2221
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002222 // Can't use sigaction(), loop for two seconds. First yield the CPU
2223 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002224 sleep(0);
2225 for (waited = 0; waited < 40; ++waited)
2226 {
2227 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002228 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002229 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002230 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002231 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002232 }
2233# endif
2234 /*
2235 * If the cscope process is still running: kill it.
2236 * Safety check: If the PID would be zero here, the entire X session
2237 * would be killed. -1 and 1 are dangerous as well.
2238 */
2239 if (pid < 0 && csinfo[i].pid > 1)
2240 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002241# ifdef ECHILD
2242 int alive = TRUE;
2243
2244 if (waitpid_errno == ECHILD)
2245 {
2246 /*
2247 * When using 'vim -g', vim is forked and cscope process is
2248 * no longer a child process but a sibling. So waitpid()
2249 * fails with errno being ECHILD (No child processes).
2250 * Don't send SIGKILL to cscope immediately but wait
2251 * (polling) for it to exit normally as result of sending
2252 * the "q" command, hence giving it a chance to clean up
2253 * its temporary files.
2254 */
2255 int waited;
2256
2257 sleep(0);
2258 for (waited = 0; waited < 40; ++waited)
2259 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002260 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002261 if (kill(csinfo[i].pid, 0) != 0)
2262 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002263 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002264 break;
2265 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002266 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002267 }
2268 }
2269 if (alive)
2270# endif
2271 {
2272 kill(csinfo[i].pid, SIGKILL);
2273 (void)waitpid(csinfo[i].pid, &pstat, 0);
2274 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002275 }
2276 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002277#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002278 if (csinfo[i].hProc != NULL)
2279 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002280 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002281 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2282 TerminateProcess(csinfo[i].hProc, 0);
2283 CloseHandle(csinfo[i].hProc);
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#endif
2286
2287 if (csinfo[i].fr_fp != NULL)
2288 (void)fclose(csinfo[i].fr_fp);
2289 if (csinfo[i].to_fp != NULL)
2290 (void)fclose(csinfo[i].to_fp);
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (freefnpp)
2293 {
2294 vim_free(csinfo[i].fname);
2295 vim_free(csinfo[i].ppath);
2296 vim_free(csinfo[i].flags);
2297 }
2298
2299 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302
2303/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002304 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002307cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2310 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002311 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002313 if (csinfo_size == 0)
2314 return CSCOPE_SUCCESS;
2315
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002316 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002317 dblist = ALLOC_MULT(char *, csinfo_size);
2318 pplist = ALLOC_MULT(char *, csinfo_size);
2319 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (dblist == NULL || pplist == NULL || fllist == NULL)
2321 {
2322 vim_free(dblist);
2323 vim_free(pplist);
2324 vim_free(fllist);
2325 return CSCOPE_FAILURE;
2326 }
2327
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002328 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 dblist[i] = csinfo[i].fname;
2331 pplist[i] = csinfo[i].ppath;
2332 fllist[i] = csinfo[i].flags;
2333 if (csinfo[i].fname != NULL)
2334 cs_release_csp(i, FALSE);
2335 }
2336
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002337 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002338 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 if (dblist[i] != NULL)
2341 {
2342 cs_add_common(dblist[i], pplist[i], fllist[i]);
2343 if (p_csverbose)
2344 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002345 // don't use smsg_attr() because we want to display the
2346 // connection number in the same line as
2347 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002349 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 }
2352 vim_free(dblist[i]);
2353 vim_free(pplist[i]);
2354 vim_free(fllist[i]);
2355 }
2356 vim_free(dblist);
2357 vim_free(pplist);
2358 vim_free(fllist);
2359
2360 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002361 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365
2366/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002367 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * (Prepends ppath, if there is one and if it's not already prepended,
2369 * otherwise just uses the name found.)
2370 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002371 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002373 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
2375 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002376cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002378 char *fullname;
2379 int len;
2380 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002383 * Ppath is freed when we destroy the cscope connection.
2384 * Fullname is freed after cs_make_vim_style_matches, after it's been
2385 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002387 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002389 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002390 else if (p_csre && csinfo[i].fname != NULL)
2391 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002392 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2393 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002394 csdir = alloc(MAXPATHL);
2395 if (csdir != NULL)
2396 {
2397 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002398 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002399 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002400 len += (int)STRLEN(csdir);
2401 }
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002404 // Note/example: this won't work if the cscope output already starts
2405 // "../.." and the prefix path is also "../..". if something like this
2406 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002407 if (csinfo[i].ppath != NULL
2408 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2409 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002410#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002411 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002413 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002414 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002415 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002416 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2417 }
2418 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002419 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002420 // Check for csdir to be non empty to avoid empty path concatenated to
2421 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002422 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002425 {
2426 fullname = (char *)vim_strsave((char_u *)name);
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002429 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002431}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433
2434/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002435 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002438cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 short i;
2441 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002442 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 else
2444 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002445 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002447 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002448 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 if (csinfo[i].fname == NULL)
2451 continue;
2452
2453 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2456 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002457 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 i, (long)csinfo[i].pid, csinfo[i].fname);
2459 }
2460 }
2461
2462 wait_return(TRUE);
2463 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002464}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
Bram Moolenaar02b06312007-09-06 15:39:22 +00002466
2467/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002468 * Only called when VIM exits to quit any cscope sessions.
2469 */
2470 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002471cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002472{
2473 int i;
2474
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002475 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002476 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002477 vim_free(csinfo);
2478 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002479}
2480
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002481#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002483#if defined(FEAT_EVAL) || defined(PROTO)
2484
2485/*
2486 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2487 *
2488 * Checks the existence of a cscope connection.
2489 */
2490 void
2491f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2492{
2493# ifdef FEAT_CSCOPE
2494 int num = 0;
2495 char_u *dbpath = NULL;
2496 char_u *prepend = NULL;
2497 char_u buf[NUMBUFLEN];
2498
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002499 if (in_vim9script()
2500 && (check_for_opt_number_arg(argvars, 0) == FAIL
2501 || (argvars[0].v_type != VAR_UNKNOWN
2502 && (check_for_opt_string_arg(argvars, 1) == FAIL
2503 || (argvars[1].v_type != VAR_UNKNOWN
2504 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2505 return;
2506
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002507 if (argvars[0].v_type != VAR_UNKNOWN
2508 && argvars[1].v_type != VAR_UNKNOWN)
2509 {
2510 num = (int)tv_get_number(&argvars[0]);
2511 dbpath = tv_get_string(&argvars[1]);
2512 if (argvars[2].v_type != VAR_UNKNOWN)
2513 prepend = tv_get_string_buf(&argvars[2], buf);
2514 }
2515
2516 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2517# endif
2518}
2519
2520#endif // FEAT_EVAL