blob: ccccb518b3fcce8553501d9efcd1b8ac0b661a6c [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
ichizok40503052022-01-13 18:09:11 +000021
22#if defined (MSWIN)
23# ifndef WIN32_LEAN_AND_MEAN
24# define WIN32_LEAN_AND_MEAN
25# endif
26# include <windows.h>
27#endif
28
29#define CSCOPE_SUCCESS 0
30#define CSCOPE_FAILURE -1
31
32#define CSCOPE_DBFILE "cscope.out"
33#define CSCOPE_PROMPT ">> "
34
35/*
36 * See ":help cscope-find" for the possible queries.
37 */
38
39typedef struct {
40 char * name;
41 int (*func)(exarg_T *eap);
42 char * help;
43 char * usage;
44 int cansplit; // if supports splitting window
45} cscmd_T;
46
47typedef struct csi {
48 char * fname; // cscope db name
49 char * ppath; // path to prepend (the -P option)
50 char * flags; // additional cscope flags/options (e.g, -p2)
51#if defined(UNIX)
52 pid_t pid; // PID of the connected cscope process.
53 dev_t st_dev; // ID of dev containing cscope db
54 ino_t st_ino; // inode number of cscope db
55#else
56# if defined(MSWIN)
57 DWORD pid; // PID of the connected cscope process.
58 HANDLE hProc; // cscope process handle
59 DWORD nVolume; // Volume serial number, instead of st_dev
60 DWORD nIndexHigh; // st_ino has no meaning in the Windows
61 DWORD nIndexLow;
62# endif
63#endif
64
65 FILE * fr_fp; // from cscope: FILE.
66 FILE * to_fp; // to cscope: FILE.
67} csinfo_T;
68
69typedef enum { Add, Find, Help, Kill, Reset, Show } csid_e;
70
71typedef enum {
72 Store,
73 Get,
74 Free,
75 Print
76} mcmd_e;
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static int cs_add(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static int cs_add_common(char *, char *, char *);
80static int cs_check_for_connections(void);
81static int cs_check_for_tags(void);
82static int cs_cnt_connections(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010083static int cs_create_connection(int i);
Bram Moolenaarc716c302006-01-21 22:12:51 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010085static void cs_file_results(FILE *, int *);
Bram Moolenaarc716c302006-01-21 22:12:51 +000086#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010087static void cs_fill_results(char *, int , int *, char ***,
88 char ***, int *);
89static int cs_find(exarg_T *eap);
90static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
91static int cs_help(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static int cs_insert_filelist(char *, char *, char *,
Bram Moolenaar8767f522016-07-01 17:17:39 +020093 stat_T *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static int cs_kill(exarg_T *eap);
95static void cs_kill_execute(int, char *);
96static cscmd_T * cs_lookup_cmd(exarg_T *eap);
97static char * cs_make_vim_style_matches(char *, char *,
98 char *, char *);
99static char * cs_manage_matches(char **, char **, int, mcmd_e);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void cs_print_tags_priv(char **, char **, int);
101static int cs_read_prompt(int);
102static void cs_release_csp(int, int freefnpp);
103static int cs_reset(exarg_T *eap);
104static char * cs_resolve_file(int, char *);
105static int cs_show(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000108static csinfo_T * csinfo = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100109static int csinfo_size = 0; // number of items allocated in
110 // csinfo[]
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000111
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100112static int eap_arg_len; // length of eap->arg, set in
113 // cs_lookup_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static cscmd_T cs_cmds[] =
115{
116 { "add", cs_add,
117 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
118 { "find", cs_find,
Bram Moolenaar80632db2016-07-05 22:28:40 +0200119 N_("Query for a pattern"), "find a|c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 { "help", cs_help,
121 N_("Show this message"), "help", 0 },
122 { "kill", cs_kill,
123 N_("Kill a connection"), "kill #", 0 },
124 { "reset", cs_reset,
125 N_("Reinit all connections"), "reset", 0 },
126 { "show", cs_show,
127 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000128 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129};
130
131 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100132cs_usage_msg(csid_e x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000134 (void)semsg(_(e_usage_cscope_str), cs_cmds[(int)x].usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135}
136
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000137static enum
138{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100139 EXP_CSCOPE_SUBCMD, // expand ":cscope" sub-commands
140 EXP_SCSCOPE_SUBCMD, // expand ":scscope" sub-commands
141 EXP_CSCOPE_FIND, // expand ":cscope find" arguments
142 EXP_CSCOPE_KILL // expand ":cscope kill" arguments
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000143} expand_what;
144
145/*
146 * Function given to ExpandGeneric() to obtain the cscope command
147 * expansion.
148 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000149 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100150get_cscope_name(expand_T *xp UNUSED, int idx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000151{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000152 int current_idx;
153 int i;
154
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000155 switch (expand_what)
156 {
157 case EXP_CSCOPE_SUBCMD:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100158 // Complete with sub-commands of ":cscope":
159 // add, find, help, kill, reset, show
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000160 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000161 case EXP_SCSCOPE_SUBCMD:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100162 // Complete with sub-commands of ":scscope": same sub-commands as
163 // ":cscope" but skip commands which don't support split windows
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000164 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
165 if (cs_cmds[i].cansplit)
166 if (current_idx++ == idx)
167 break;
168 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000169 case EXP_CSCOPE_FIND:
170 {
171 const char *query_type[] =
172 {
Bram Moolenaar80632db2016-07-05 22:28:40 +0200173 "a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000174 };
175
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100176 // Complete with query type of ":cscope find {query_type}".
177 // {query_type} can be letters (c, d, ... a) or numbers (0, 1,
178 // ..., 9) but only complete with letters, since numbers are
179 // redundant.
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000180 return (char_u *)query_type[idx];
181 }
182 case EXP_CSCOPE_KILL:
183 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000184 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000185
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100186 // ":cscope kill" accepts connection numbers or partial names of
187 // the pathname of the cscope database as argument. Only complete
188 // with connection numbers. -1 can also be used to kill all
189 // connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000190 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000191 {
192 if (csinfo[i].fname == NULL)
193 continue;
194 if (current_idx++ == idx)
195 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000196 vim_snprintf(connection, sizeof(connection), "%d", i);
197 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000198 }
199 }
200 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
201 }
202 default:
203 return NULL;
204 }
205}
206
207/*
208 * Handle command line completion for :cscope command.
209 */
210 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100211set_context_in_cscope_cmd(
212 expand_T *xp,
213 char_u *arg,
214 cmdidx_T cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000215{
216 char_u *p;
217
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218 // Default: expand subcommands
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000219 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000220 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000221 expand_what = (cmdidx == CMD_scscope)
222 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000223
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100224 // (part of) subcommand already typed
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000225 if (*arg != NUL)
226 {
227 p = skiptowhite(arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100228 if (*p != NUL) // past first word
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000229 {
230 xp->xp_pattern = skipwhite(p);
231 if (*skiptowhite(xp->xp_pattern) != NUL)
232 xp->xp_context = EXPAND_NOTHING;
233 else if (STRNICMP(arg, "add", p - arg) == 0)
234 xp->xp_context = EXPAND_FILES;
235 else if (STRNICMP(arg, "kill", p - arg) == 0)
236 expand_what = EXP_CSCOPE_KILL;
237 else if (STRNICMP(arg, "find", p - arg) == 0)
238 expand_what = EXP_CSCOPE_FIND;
239 else
240 xp->xp_context = EXPAND_NOTHING;
241 }
242 }
243}
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245/*
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000246 * Find the command, print help if invalid, and then call the corresponding
247 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100250do_cscope_general(
251 exarg_T *eap,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100252 int make_split UNUSED) // whether to split window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 cscmd_T *cmdp;
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
257 {
258 cs_help(eap);
259 return;
260 }
261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (make_split)
263 {
264 if (!cmdp->cansplit)
265 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100266 (void)msg_puts(_("This cscope command does not support splitting the window.\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 return;
268 }
269 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +0200270 postponed_split_flags = cmdmod.cmod_split;
271 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274 cmdp->func(eap);
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000277 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278}
279
280/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100281 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 */
283 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100284ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
286 do_cscope_general(eap, FALSE);
287}
288
289/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100290 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 */
292 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100293ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294{
295 do_cscope_general(eap, TRUE);
296}
297
298/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100299 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 */
301 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100302ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303{
304 int ret = FALSE;
305
Bram Moolenaar446cb832008-06-24 21:56:24 +0000306 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000308 (void)emsg(_(e_usage_cstag_ident));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 return;
310 }
311
312 switch (p_csto)
313 {
314 case 0 :
315 if (cs_check_for_connections())
316 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000317 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200318 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 if (ret == FALSE)
320 {
321 cs_free_tags();
322 if (msg_col)
323 msg_putchar('\n');
324
325 if (cs_check_for_tags())
326 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
327 }
328 }
329 else if (cs_check_for_tags())
330 {
331 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
332 }
333 break;
334 case 1 :
335 if (cs_check_for_tags())
336 {
337 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
338 if (ret == FALSE)
339 {
340 if (msg_col)
341 msg_putchar('\n');
342
343 if (cs_check_for_connections())
344 {
345 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200346 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (ret == FALSE)
348 cs_free_tags();
349 }
350 }
351 }
352 else if (cs_check_for_connections())
353 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000354 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200355 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 if (ret == FALSE)
357 cs_free_tags();
358 }
359 break;
360 default :
361 break;
362 }
363
364 if (!ret)
365 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000366 (void)emsg(_(e_cstag_tag_not_founc));
Bram Moolenaar4033c552017-09-16 20:54:51 +0200367#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 g_do_tagpreview = 0;
369#endif
370 }
371
Bram Moolenaard4db7712016-11-12 19:16:46 +0100372}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374
375/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100376 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * from the cscope output. should only be called from find_tags()
378 *
379 * returns TRUE if eof, FALSE otherwise
380 */
381 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100382cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383{
384 char *p;
385
386 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
387 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000388 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
390 return FALSE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100391}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
393
394/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100395 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
397 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100398cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
400 cs_manage_matches(NULL, NULL, -1, Free);
401}
402
403
404/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100405 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 */
407 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100408cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
410 cs_manage_matches(NULL, NULL, -1, Print);
411}
412
413
414/*
415 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
416 *
417 * Checks for the existence of a |cscope| connection. If no
418 * parameters are specified, then the function returns:
419 *
420 * 0, if cscope was not available (not compiled in), or if there
421 * are no cscope connections; or
422 * 1, if there is at least one cscope connection.
423 *
424 * If parameters are specified, then the value of {num}
425 * determines how existence of a cscope connection is checked:
426 *
427 * {num} Description of existence check
428 * ----- ------------------------------
429 * 0 Same as no parameters (e.g., "cscope_connection()").
430 * 1 Ignore {prepend}, and use partial string matches for
431 * {dbpath}.
432 * 2 Ignore {prepend}, and use exact string matches for
433 * {dbpath}.
434 * 3 Use {prepend}, use partial string matches for both
435 * {dbpath} and {prepend}.
436 * 4 Use {prepend}, use exact string matches for both
437 * {dbpath} and {prepend}.
438 *
439 * Note: All string comparisons are case sensitive!
440 */
441#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200442 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100443cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
445 int i;
446
447 if (num < 0 || num > 4 || (num > 0 && !dbpath))
448 return FALSE;
449
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000450 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {
452 if (!csinfo[i].fname)
453 continue;
454
455 if (num == 0)
456 return TRUE;
457
458 switch (num)
459 {
460 case 1:
461 if (strstr(csinfo[i].fname, (char *)dbpath))
462 return TRUE;
463 break;
464 case 2:
465 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
466 return TRUE;
467 break;
468 case 3:
469 if (strstr(csinfo[i].fname, (char *)dbpath)
470 && ((!ppath && !csinfo[i].ppath)
471 || (ppath
472 && csinfo[i].ppath
473 && strstr(csinfo[i].ppath, (char *)ppath))))
474 return TRUE;
475 break;
476 case 4:
477 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
478 && ((!ppath && !csinfo[i].ppath)
479 || (ppath
480 && csinfo[i].ppath
481 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
482 return TRUE;
483 break;
484 }
485 }
486
487 return FALSE;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200488}
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#endif
491
492
493/*
494 * PRIVATE functions
495 ****************************************************************************/
496
497/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100498 * Add cscope database or a directory name (to look for cscope.out)
499 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100502cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 char *fname, *ppath, *flags = NULL;
505
506 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
507 {
508 cs_usage_msg(Add);
509 return CSCOPE_FAILURE;
510 }
511 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
512 flags = strtok((char *)NULL, (const char *)" ");
513
514 return cs_add_common(fname, ppath, flags);
515}
516
517 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100518cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
James McCoy3c5904d2021-10-24 14:50:07 +0100520 int err = errno;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000521 (void)semsg(_(e_stat_str_error_nr), fname, err);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
524
525/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100526 * The common routine to add a new cscope connection. Called by
527 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * routine uses a number of goto statements.
529 */
530 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100531cs_add_common(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100532 char *arg1, // filename - may contain environment variables
533 char *arg2, // prepend path - may contain environment variables
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100534 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200536 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000537 int ret;
538 char *fname = NULL;
539 char *fname2 = NULL;
540 char *ppath = NULL;
541 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200542 int len;
543 int usedlen = 0;
544 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100546 // get the filename (arg1), expand it, and try to stat it
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200547 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 goto add_err;
549
550 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200551 len = (int)STRLEN(fname);
552 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200553 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200554 (char_u **)&fname, &fbuf, &len);
555 if (fname == NULL)
556 goto add_err;
557 fname = (char *)vim_strnsave((char_u *)fname, len);
558 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200559
Bram Moolenaar8767f522016-07-01 17:17:39 +0200560 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (ret < 0)
562 {
563staterr:
564 if (p_csverbose)
565 cs_stat_emsg(fname);
566 goto add_err;
567 }
568
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100569 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (arg2 != NULL)
571 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200572 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200574 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 goto add_err;
576
577 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200578 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (ret < 0)
580 goto staterr;
581 }
582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100583 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200584 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200586 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (fname2 == NULL)
588 goto add_err;
589
590 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100591#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || fname[strlen(fname)-1] == '\\'
593#endif
594 )
595 {
596 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200597 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 break;
599 }
600 if (fname[0] == '\0')
601 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
602 else
603 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
604
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (ret < 0)
607 {
608 if (p_csverbose)
609 cs_stat_emsg(fname2);
610 goto add_err;
611 }
612
613 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
618 }
619 else
620 {
621 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000622 (void)semsg(_(e_str_is_not_directory_or_valid_cscope_database),
623 fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 goto add_err;
625 }
626
627 if (i != -1)
628 {
629 if (cs_create_connection(i) == CSCOPE_FAILURE
630 || cs_read_prompt(i) == CSCOPE_FAILURE)
631 {
632 cs_release_csp(i, TRUE);
633 goto add_err;
634 }
635
636 if (p_csverbose)
637 {
638 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100639 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100640 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 csinfo[i].fname);
642 }
643 }
644
645 vim_free(fname);
646 vim_free(fname2);
647 vim_free(ppath);
648 return CSCOPE_SUCCESS;
649
650add_err:
651 vim_free(fname2);
652 vim_free(fname);
653 vim_free(ppath);
654 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100655}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657
658 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100659cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100662}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664
665 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100666cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000668 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100669}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671
672/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100673 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 */
675 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100676cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 short i;
679 short cnt = 0;
680
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000681 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 if (csinfo[i].fname != NULL)
684 cnt++;
685 }
686 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100687}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100690cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100691 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000693 semsg(_(e_error_reading_cscope_connection_nr), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696#define CSREAD_BUFSIZE 2048
697/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100698 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
700 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100701cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 char *stok;
704 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100705 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200707 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (buf == NULL)
709 return 0;
710 for (;;)
711 {
712 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
713 {
714 if (feof(csinfo[idx].fr_fp))
715 errno = EIO;
716
717 cs_reading_emsg(idx);
718
719 vim_free(buf);
720 return -1;
721 }
722
723 /*
724 * If the database is out of date, or there's some other problem,
725 * cscope will output error messages before the number-of-lines output.
726 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000727 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100728 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100730 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if ((stok = strtok(buf, (const char *)" ")) == NULL)
733 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000734 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 continue;
736
737 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
738 continue;
739 nlines = atoi(stok);
740 if (nlines < 0)
741 {
742 nlines = 0;
743 break;
744 }
745
746 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
747 continue;
748 if (strncmp((const char *)stok, "lines", 5))
749 continue;
750
751 break;
752 }
753
754 vim_free(buf);
755 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100756}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758
759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * Creates the actual cscope command query from what the user entered.
761 */
762 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100763cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char *cmd;
766 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000767 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769 switch (csoption[0])
770 {
771 case '0' : case 's' :
772 search = 0;
773 break;
774 case '1' : case 'g' :
775 search = 1;
776 break;
777 case '2' : case 'd' :
778 search = 2;
779 break;
780 case '3' : case 'c' :
781 search = 3;
782 break;
783 case '4' : case 't' :
784 search = 4;
785 break;
786 case '6' : case 'e' :
787 search = 6;
788 break;
789 case '7' : case 'f' :
790 search = 7;
791 break;
792 case '8' : case 'i' :
793 search = 8;
794 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200795 case '9' : case 'a' :
796 search = 9;
797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 default :
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000799 (void)emsg(_(e_unknown_cscope_search_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 cs_usage_msg(Find);
801 return NULL;
802 }
803
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100804 // Skip white space before the patter, except for text and pattern search,
805 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000806 pat = pattern;
807 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100808 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000809 ++pat;
810
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200811 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 return NULL;
813
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000814 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100817}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819
820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 * This piece of code was taken/adapted from nvi. do we need to add
822 * the BSD license notice?
823 */
824 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100825cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000827#ifdef UNIX
828 int to_cs[2], from_cs[2];
829#endif
830 int len;
831 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100832#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000833 int fd;
834 SECURITY_ATTRIBUTES sa;
835 PROCESS_INFORMATION pi;
836 STARTUPINFO si;
837 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
838 HANDLE stdin_rd, stdout_rd;
839 HANDLE stdout_wr, stdin_wr;
840 BOOL created;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200841# if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
842# define OPEN_OH_ARGTYPE intptr_t
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000843# else
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200844# define OPEN_OH_ARGTYPE long
Bram Moolenaar5365c4d2007-09-14 17:56:59 +0000845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#endif
847
Bram Moolenaar02b06312007-09-06 15:39:22 +0000848#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 /*
850 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
851 * from_cs[0] and writes to to_cs[1].
852 */
853 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
854 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
855 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000856 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857err_closing:
858 if (to_cs[0] != -1)
859 (void)close(to_cs[0]);
860 if (to_cs[1] != -1)
861 (void)close(to_cs[1]);
862 if (from_cs[0] != -1)
863 (void)close(from_cs[0]);
864 if (from_cs[1] != -1)
865 (void)close(from_cs[1]);
866 return CSCOPE_FAILURE;
867 }
868
ichizok40503052022-01-13 18:09:11 +0000869 if ((csinfo[i].pid = fork()) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000871 (void)emsg(_(e_could_not_fork_for_cscope));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 goto err_closing;
ichizok40503052022-01-13 18:09:11 +0000873 }
874 else if (csinfo[i].pid == 0) // child: run cscope.
875 {
876 char **argv = NULL;
877 int argc = 0;
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (dup2(to_cs[0], STDIN_FILENO) == -1)
880 PERROR("cs_create_connection 1");
881 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
882 PERROR("cs_create_connection 2");
883 if (dup2(from_cs[1], STDERR_FILENO) == -1)
884 PERROR("cs_create_connection 3");
885
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100886 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 (void)close(to_cs[1]);
888 (void)close(from_cs[0]);
889#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100890 // MSWIN
891 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000892 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
893 sa.bInheritHandle = TRUE;
894 sa.lpSecurityDescriptor = NULL;
895
896 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
897 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
898 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000899 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000900err_closing:
901 if (pipe_stdin)
902 {
903 CloseHandle(stdin_rd);
904 CloseHandle(stdin_wr);
905 }
906 if (pipe_stdout)
907 {
908 CloseHandle(stdout_rd);
909 CloseHandle(stdout_wr);
910 }
911 return CSCOPE_FAILURE;
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100914 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200915 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
917#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000918 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100920 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 goto err_closing;
922#endif
923 }
924 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
925
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100926 // alloc space to hold the cscope command
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000927 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (csinfo[i].ppath)
929 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100930 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200931 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
933 vim_free(prog);
934#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000935 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100937 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 goto err_closing;
939#endif
940 }
941 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000943 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945
946 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000947 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200949 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
951 vim_free(prog);
952 vim_free(ppath);
953#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000954 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100956 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 goto err_closing;
958#endif
959 }
960
ichizok40503052022-01-13 18:09:11 +0000961 // run the cscope command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
ichizok40503052022-01-13 18:09:11 +0000963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (csinfo[i].ppath != NULL)
965 {
966 (void)strcat(cmd, " -P");
967 (void)strcat(cmd, csinfo[i].ppath);
968 }
969 if (csinfo[i].flags != NULL)
970 {
971 (void)strcat(cmd, " ");
972 (void)strcat(cmd, csinfo[i].flags);
973 }
974# ifdef UNIX
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 vim_free(prog);
977# endif
978 vim_free(ppath);
979
980#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200981# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100982 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200983# if defined(HAVE_SETSID)
984 (void)setsid();
985# else
986 if (setpgid(0, 0) == -1)
987 PERROR(_("cs_create_connection setpgid failed"));
988# endif
989# endif
ichizok40503052022-01-13 18:09:11 +0000990 if (build_argv_from_string((char_u *)cmd, &argv, &argc) == FAIL)
991 exit(EXIT_FAILURE);
992
993 if (execvp(argv[0], argv) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 PERROR(_("cs_create_connection exec failed"));
995
996 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100997 // NOTREACHED
ichizok40503052022-01-13 18:09:11 +0000998 }
999 else // parent.
1000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1004 */
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001010 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 (void)close(to_cs[0]);
1012 (void)close(from_cs[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001015 // MSWIN
1016 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +00001017 GetStartupInfo(&si);
1018 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001019 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +00001020 si.hStdOutput = stdout_wr;
1021 si.hStdError = stdout_wr;
1022 si.hStdInput = stdin_rd;
1023 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1024 NULL, NULL, &si, &pi);
1025 vim_free(prog);
1026 vim_free(cmd);
1027
1028 if (!created)
1029 {
1030 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001031 (void)emsg(_(e_could_not_spawn_cscope_process));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001032 goto err_closing;
1033 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001035 csinfo[i].pid = pi.dwProcessId;
1036 csinfo[i].hProc = pi.hProcess;
1037 CloseHandle(pi.hThread);
1038
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001039 // TODO - tidy up after failure to create files on pipe handles.
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001040 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1041 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001042 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1043 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001044 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1045 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001046 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1047 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1048
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001049 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001050 CloseHandle(stdin_rd);
1051 CloseHandle(stdout_wr);
1052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001053#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058
1059/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001060 * Query cscope using command line interface. Parse the output and use tselect
1061 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 *
1063 * returns TRUE if we jump to a tag or abort, FALSE if not.
1064 */
1065 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001066cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001069 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
1071 if (cs_check_for_connections() == FALSE)
1072 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001073 (void)emsg(_(e_no_cscope_connections));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 return FALSE;
1075 }
1076
1077 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1078 {
1079 cs_usage_msg(Find);
1080 return FALSE;
1081 }
1082
1083 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001084 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 cs_usage_msg(Find);
1087 return FALSE;
1088 }
1089
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001090 /*
1091 * Let's replace the NULs written by strtok() with spaces - we need the
1092 * spaces to correctly display the quickfix/location list window's title.
1093 */
1094 for (i = 0; i < eap_arg_len; ++i)
1095 if (NUL == eap->arg[i])
1096 eap->arg[i] = ' ';
1097
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001098 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001099 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102
1103/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001104 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
1106 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001107cs_find_common(
1108 char *opt,
1109 char *pat,
1110 int forceit,
1111 int verbose,
1112 int use_ll UNUSED,
1113 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int i;
1116 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001117 int *nummatches;
1118 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#ifdef FEAT_QUICKFIX
1120 char cmdletter;
1121 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001122
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001123 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001124 switch (opt[0])
1125 {
1126 case '0' :
1127 cmdletter = 's';
1128 break;
1129 case '1' :
1130 cmdletter = 'g';
1131 break;
1132 case '2' :
1133 cmdletter = 'd';
1134 break;
1135 case '3' :
1136 cmdletter = 'c';
1137 break;
1138 case '4' :
1139 cmdletter = 't';
1140 break;
1141 case '6' :
1142 cmdletter = 'e';
1143 break;
1144 case '7' :
1145 cmdletter = 'f';
1146 break;
1147 case '8' :
1148 cmdletter = 'i';
1149 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001150 case '9' :
1151 cmdletter = 'a';
1152 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001153 default :
1154 cmdletter = opt[0];
1155 }
1156
1157 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1158 if (qfpos != NULL)
1159 {
1160 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001161 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001162 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1163 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001164 (void)semsg(_(e_invalid_cscopequickfix_flag_chr_for_chr),
1165 *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001166 return FALSE;
1167 }
1168
Bram Moolenaar21662be2016-11-06 14:46:44 +01001169 if (*qfpos != '0'
1170 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1171 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001172 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001173# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001174 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001175 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001176# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001177 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001181 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 cmd = cs_create_cmd(opt, pat);
1183 if (cmd == NULL)
1184 return FALSE;
1185
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001186 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001187 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001188 {
1189 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001190 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001191 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001192
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001193 // Send query to all open connections, then count the total number
1194 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001195 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 nummatches[i] = 0;
1197 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001198 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001200 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 continue;
1202
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001203 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1205 (void)fflush(csinfo[i].to_fp);
1206
1207 nummatches[i] = cs_cnt_matches(i);
1208
1209 if (nummatches[i] > -1)
1210 totmatches += nummatches[i];
1211
1212 if (nummatches[i] == 0)
1213 (void)cs_read_prompt(i);
1214 }
1215 vim_free(cmd);
1216
1217 if (totmatches == 0)
1218 {
James McCoy3c5904d2021-10-24 14:50:07 +01001219 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001220 (void)semsg(_(e_no_matches_found_for_cscope_query_str_of_str),
1221 opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001222 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 return FALSE;
1224 }
1225
1226#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001227 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001229 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001230 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001231 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001232 qf_info_T *qi = NULL;
1233 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001235 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001237 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001240 cs_file_results(f, nummatches);
1241 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001242 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001243 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001244 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001245 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001246 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001248 if (postponed_split != 0)
1249 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001250 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001252 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001253 postponed_split = 0;
1254 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001255
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001256 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1257 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001258 if (use_ll)
1259 /*
1260 * In the location list window, use the displayed location
1261 * list. Otherwise, use the location list for the window.
1262 */
1263 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1264 ? wp->w_llist_ref : wp->w_llist;
1265 qf_jump(qi, 0, 0, forceit);
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 mch_remove(tmp);
1269 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001270 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return TRUE;
1272 }
1273 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001274#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001276 char **matches = NULL, **contexts = NULL;
1277 int matched = 0;
1278
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001279 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001280 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001282 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (matches == NULL)
1284 return FALSE;
1285
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001286 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1289 }
1290
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001294 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001297cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 cscmd_T *cmdp = cs_cmds;
1300
Bram Moolenaar32526b32019-01-19 17:43:09 +01001301 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 while (cmdp->name != NULL)
1303 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001304 char *help = _(cmdp->help);
1305 int space_cnt = 30 - vim_strsize((char_u *)help);
1306
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001307 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001308 if (space_cnt < 0)
1309 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001311 cmdp->name,
1312 help, space_cnt, " ",
1313 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001315 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001316 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001324 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 cmdp++;
1327 }
1328
1329 wait_return(TRUE);
1330 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001335clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 csinfo[i].fname = NULL;
1338 csinfo[i].ppath = NULL;
1339 csinfo[i].flags = NULL;
1340#if defined(UNIX)
1341 csinfo[i].st_dev = (dev_t)0;
1342 csinfo[i].st_ino = (ino_t)0;
1343#else
1344 csinfo[i].nVolume = 0;
1345 csinfo[i].nIndexHigh = 0;
1346 csinfo[i].nIndexLow = 0;
1347#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001348 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 csinfo[i].fr_fp = NULL;
1350 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001351#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001352 csinfo[i].hProc = NULL;
1353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001357 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001360cs_insert_filelist(
1361 char *fname,
1362 char *ppath,
1363 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001364 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 short i, j;
1367#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 BY_HANDLE_FILE_INFORMATION bhfi;
1369
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001370 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001372 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1373 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (p_csverbose)
1375 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001376 char *cant_msg = _(e_cannot_open_cscope_database_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char *winmsg = GetWin32Error();
1378
1379 if (winmsg != NULL)
1380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001381 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 LocalFree(winmsg);
1383 }
1384 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001386 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001389
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (p_csverbose)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001392 (void)emsg(_(e_cannot_get_cscope_database_information));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395#endif
1396
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001397 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001398 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
1400 if (csinfo[j].fname != NULL
1401#if defined(UNIX)
1402 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1403#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001404 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001405 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001406 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1407 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001408 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1410 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1411#endif
1412 )
1413 {
1414 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001415 (void)emsg(_(e_duplicate_cscope_database_not_added));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 return -1;
1417 }
1418
1419 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001420 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422
1423 if (i == -1)
1424 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001425 i = csinfo_size;
1426 if (csinfo_size == 0)
1427 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001428 // First time allocation: allocate only 1 connection. It should
1429 // be enough for most users. If more is needed, csinfo will be
1430 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001431 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001432 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001433 }
1434 else
1435 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001436 csinfo_T *t_csinfo = csinfo;
1437
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001438 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001439 csinfo_size *= 2;
1440 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001441 if (csinfo == NULL)
1442 {
1443 vim_free(t_csinfo);
1444 csinfo_size = 0;
1445 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001446 }
1447 if (csinfo == NULL)
1448 return -1;
1449 for (j = csinfo_size/2; j < csinfo_size; j++)
1450 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001453 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 return -1;
1455
1456 (void)strcpy(csinfo[i].fname, (const char *)fname);
1457
1458 if (ppath != NULL)
1459 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001460 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001462 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return -1;
1464 }
1465 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1466 } else
1467 csinfo[i].ppath = NULL;
1468
1469 if (flags != NULL)
1470 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001471 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001473 VIM_CLEAR(csinfo[i].fname);
1474 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return -1;
1476 }
1477 (void)strcpy(csinfo[i].flags, (const char *)flags);
1478 } else
1479 csinfo[i].flags = NULL;
1480
1481#if defined(UNIX)
1482 csinfo[i].st_dev = sb->st_dev;
1483 csinfo[i].st_ino = sb->st_ino;
1484
1485#else
1486 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1487 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1488 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1489#endif
1490 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493
1494/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001495 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001498cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 cscmd_T *cmdp;
1501 char *stok;
1502 size_t len;
1503
1504 if (eap->arg == NULL)
1505 return NULL;
1506
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001507 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001508 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1511 return NULL;
1512
1513 len = strlen(stok);
1514 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1515 {
1516 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1517 return (cmdp);
1518 }
1519 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
1522
1523/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001524 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001527cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 char *stok;
1530 short i;
1531
1532 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1533 {
1534 cs_usage_msg(Kill);
1535 return CSCOPE_FAILURE;
1536 }
1537
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001538 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1540 || (strlen(stok) < 3 && stok[0] == '-'
1541 && VIM_ISDIGIT((int)(stok[1]))))
1542 i = atoi(stok);
1543 else
1544 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001545 // It must be part of a name. We will try to find a match
1546 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001547 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1550 break;
1551 }
1552 }
1553
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001554 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 if (p_csverbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001557 (void)semsg(_(e_cscope_connection_str_not_founc), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559 else
1560 {
1561 if (i == -1)
1562 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001563 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 if (csinfo[i].fname)
1566 cs_kill_execute(i, csinfo[i].fname);
1567 }
1568 }
1569 else
1570 cs_kill_execute(i, stok);
1571 }
1572
1573 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576
1577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 * Actually kills a specific cscope connection.
1579 */
1580 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001581cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001582 int i, // cscope table index
1583 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 if (p_csverbose)
1586 {
1587 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001588 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001589 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 cs_release_csp(i, TRUE);
1592}
1593
1594
1595/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001596 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1598 * the type of the tag you are looking for. for example, in Darren Hiebert's
1599 * ctags (the one that comes with vim), #define's use a line number to find the
1600 * tag in a file while function definitions use a regexp search pattern.
1601 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001602 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * quirky (and probably other things i don't know about):
1604 *
1605 * if you have "# define" in your source file, which is
1606 * perfectly legal, cscope thinks you have "#define". this
1607 * will result in a failed regexp search. :(
1608 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001609 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * would still have to be modified to escape all the special regular expression
1611 * characters to comply with ctags formatting.
1612 */
1613 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001614cs_make_vim_style_matches(
1615 char *fname,
1616 char *slno,
1617 char *search,
1618 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 // vim style is ctags:
1621 //
1622 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1623 //
1624 // but as mentioned above, we'll always use the line number and
1625 // put the search pattern (if one exists) as "extra"
1626 //
1627 // buf is used as part of vim's method of handling tags, and
1628 // (i think) vim frees it when you pop your tags and get replaced
1629 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 char *buf;
1631 int amt;
1632
1633 if (search != NULL)
1634 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001635 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001636 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return NULL;
1638
1639 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1640 }
1641 else
1642 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001643 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001644 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 return NULL;
1646
1647 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1648 }
1649
1650 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001651}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653
1654/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001655 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 *
1657 * Store: keep a ptr to the (malloc'd) memory of matches originally
1658 * generated from cs_find(). the matches are originally lines directly
1659 * from cscope output, but transformed to look like something out of a
1660 * ctags. see cs_make_vim_style_matches for more details.
1661 *
1662 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1663 * the next line from the cscope output. it basically keeps track of which
1664 * lines have been "used" and returns the next one.
1665 *
1666 * Free: frees up everything and resets
1667 *
1668 * Print: prints the tags
1669 */
1670 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001671cs_manage_matches(
1672 char **matches,
1673 char **contexts,
1674 int totmatches,
1675 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 static char **mp = NULL;
1678 static char **cp = NULL;
1679 static int cnt = -1;
1680 static int next = -1;
1681 char *p = NULL;
1682
1683 switch (cmd)
1684 {
1685 case Store:
1686 assert(matches != NULL);
1687 assert(totmatches > 0);
1688 if (mp != NULL || cp != NULL)
1689 (void)cs_manage_matches(NULL, NULL, -1, Free);
1690 mp = matches;
1691 cp = contexts;
1692 cnt = totmatches;
1693 next = 0;
1694 break;
1695 case Get:
1696 if (next >= cnt)
1697 return NULL;
1698
1699 p = mp[next];
1700 next++;
1701 break;
1702 case Free:
1703 if (mp != NULL)
1704 {
1705 if (cnt > 0)
1706 while (cnt--)
1707 {
1708 vim_free(mp[cnt]);
1709 if (cp != NULL)
1710 vim_free(cp[cnt]);
1711 }
1712 vim_free(mp);
1713 vim_free(cp);
1714 }
1715 mp = NULL;
1716 cp = NULL;
1717 cnt = 0;
1718 next = 0;
1719 break;
1720 case Print:
1721 cs_print_tags_priv(mp, cp, cnt);
1722 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001723 default: // should not reach here
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001724 iemsg(_(e_fatal_error_in_cs_manage_matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 return NULL;
1726 }
1727
1728 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731
1732/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001733 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001736cs_parse_results(
1737 int cnumber,
1738 char *buf,
1739 int bufsize,
1740 char **context,
1741 char **linenumber,
1742 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
1744 int ch;
1745 char *p;
1746 char *name;
1747
1748 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1749 {
1750 if (feof(csinfo[cnumber].fr_fp))
1751 errno = EIO;
1752
1753 cs_reading_emsg(cnumber);
1754
1755 return NULL;
1756 }
1757
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001758 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if ((p = strchr(buf, '\n')) == NULL)
1760 {
1761 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1762 ;
1763 return NULL;
1764 }
1765 *p = '\0';
1766
1767 /*
1768 * cscope output is in the following format:
1769 *
1770 * <filename> <context> <line number> <pattern>
1771 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001772 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 return NULL;
1774 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1775 return NULL;
1776 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1777 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001778 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 // --- nvi ---
1781 // If the file is older than the cscope database, that is,
1782 // the database was built since the file was last modified,
1783 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (strcmp(*search, "<unknown>") == 0)
1785 *search = NULL;
1786
1787 name = cs_resolve_file(cnumber, name);
1788 return name;
1789}
1790
Bram Moolenaarc716c302006-01-21 22:12:51 +00001791#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001793 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
1795 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001796cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int i, j;
1799 char *buf;
1800 char *search, *slno;
1801 char *fullname;
1802 char *cntx;
1803 char *context;
1804
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001805 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (buf == NULL)
1807 return;
1808
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001809 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811 if (nummatches_a[i] < 1)
1812 continue;
1813
1814 for (j = 0; j < nummatches_a[i]; j++)
1815 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001816 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1817 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 continue;
1819
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001820 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001821 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001822 {
1823 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827 if (strcmp(cntx, "<global>")==0)
1828 strcpy(context, "<<global>>");
1829 else
1830 sprintf(context, "<<%s>>", cntx);
1831
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001832 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1834 else
1835 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1836
1837 vim_free(context);
1838 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001839 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 (void)cs_read_prompt(i);
1842
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 vim_free(buf);
1845}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001849 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1850 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001851 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 */
1853 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001854cs_fill_results(
1855 char *tagstr,
1856 int totmatches,
1857 int *nummatches_a,
1858 char ***matches_p,
1859 char ***cntxts_p,
1860 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 int i, j;
1863 char *buf;
1864 char *search, *slno;
1865 int totsofar = 0;
1866 char **matches = NULL;
1867 char **cntxts = NULL;
1868 char *fullname;
1869 char *cntx;
1870
1871 assert(totmatches > 0);
1872
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001873 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (buf == NULL)
1875 return;
1876
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001877 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001879 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 goto parse_out;
1881
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001882 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 if (nummatches_a[i] < 1)
1885 continue;
1886
1887 for (j = 0; j < nummatches_a[i]; j++)
1888 {
1889 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1890 &slno, &search)) == NULL)
1891 continue;
1892
1893 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1894 search, tagstr);
1895
1896 vim_free(fullname);
1897
1898 if (strcmp(cntx, "<global>") == 0)
1899 cntxts[totsofar] = NULL;
1900 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001901 // note: if vim_strsave returns NULL, then the context
1902 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1904
1905 if (matches[totsofar] != NULL)
1906 totsofar++;
1907
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001908 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 (void)cs_read_prompt(i);
1911
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001912 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001915 if (totsofar == 0)
1916 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001917 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001918 VIM_CLEAR(matches);
1919 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 *matched = totsofar;
1922 *matches_p = matches;
1923 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001929/*
1930 * get the requested path components
1931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001933cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 int i;
1936 char *s;
1937
1938 if (p_cspc == 0)
1939 return path;
1940
1941 s = path + strlen(path) - 1;
1942 for (i = 0; i < p_cspc; ++i)
1943 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001944#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 && *--s != '\\'
1946#endif
1947 )
1948 ;
1949 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001950#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 || (s > path && *s == '\\')
1952#endif
1953 )
1954 ++s;
1955 return s;
1956}
1957
1958/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001959 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
1961 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001962cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001965 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001966 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 int newsize = 0;
1968 char *ptag;
1969 char *fname, *lno, *extra, *tbuf;
1970 int i, idx, num;
1971 char *globalcntx = "GLOBAL";
1972 char *cntxformat = " <<%s>>";
1973 char *context;
1974 char *cstag_msg = _("Cscope tag: %s");
1975 char *csfmt_str = "%4d %6s ";
1976
Bram Moolenaar4033c552017-09-16 20:54:51 +02001977 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001979 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 return;
1981
1982 strcpy(tbuf, matches[0]);
1983 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001984 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001985 {
1986 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001987 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001990 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001991 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (buf != NULL)
1993 {
1994 bufsize = newsize;
1995 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001996 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999 vim_free(tbuf);
2000
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002001 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002003 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 num = 1;
2006 for (i = 0; i < num_matches; i++)
2007 {
2008 idx = i;
2009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002010 // if we really wanted to, we could avoid this malloc and strcpy
2011 // by parsing matches[i] on the fly and placing stuff into buf
2012 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002013 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 continue;
2015 (void)strcpy(tbuf, matches[idx]);
2016
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002017 if (strtok(tbuf, (const char *)"\t") == NULL
2018 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2019 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2020 {
2021 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002023 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002024 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002026 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002028 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002029 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (bufsize < newsize)
2031 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002032 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002033 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002037 vim_free(t_buf);
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 else
2040 bufsize = newsize;
2041 }
2042 if (buf != NULL)
2043 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002044 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002046 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002048 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2049 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002051 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (cntxts[idx] != NULL)
2053 context = cntxts[idx];
2054 else
2055 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002056 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 if (bufsize < newsize)
2059 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002060 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002061 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002065 vim_free(t_buf);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 bufsize = newsize;
2069 }
2070 if (buf != NULL)
2071 {
2072 (void)sprintf(buf, cntxformat, context);
2073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002074 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (msg_col + (int)strlen(buf) >= (int)Columns)
2076 msg_putchar('\n');
2077 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002078 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 msg_putchar('\n');
2080 }
2081 if (extra != NULL)
2082 {
2083 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002084 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002087 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 if (msg_col)
2090 msg_putchar('\n');
2091
2092 ui_breakcheck();
2093 if (got_int)
2094 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002095 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 break;
2097 }
2098
2099 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002100 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105
2106/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002107 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
2109 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002110cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002113 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int bufpos = 0;
2115 char *cs_emsg;
2116 int maxlen;
2117 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002118 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int n;
2120
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002121 cs_emsg = _(e_cscope_error_str);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2124
2125 for (;;)
2126 {
2127 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002128 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2130 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002132 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (buf != NULL)
2134 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002135 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 buf[bufpos++] = ch;
2137 buf[bufpos] = NUL;
2138 if (bufpos >= epromptlen
2139 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2140 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002141 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 buf[bufpos - epromptlen] = NUL;
2143
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002144 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002147 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 (void)putc('\n', csinfo[i].to_fp);
2149 (void)fflush(csinfo[i].to_fp);
2150
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002151 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 bufpos = 0;
2153 buf[bufpos] = NUL;
2154 }
2155 }
2156 }
2157
2158 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2159 {
2160 if (n > 0)
2161 ch = getc(csinfo[i].fr_fp);
2162 if (ch == EOF)
2163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002167 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 cs_release_csp(i, TRUE);
2169 vim_free(buf);
2170 return CSCOPE_FAILURE;
2171 }
2172
2173 if (ch != CSCOPE_PROMPT[n])
2174 {
2175 ch = EOF;
2176 break;
2177 }
2178 }
2179
2180 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002181 continue; // didn't find the prompt
2182 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184
2185 vim_free(buf);
2186 return CSCOPE_SUCCESS;
2187}
2188
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002189#if defined(UNIX) && defined(SIGALRM)
2190/*
2191 * Used to catch and ignore SIGALRM below.
2192 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002193 static RETSIGTYPE
2194sig_handler SIGDEFARG(sigarg)
2195{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002196 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002197 SIGRETURN;
2198}
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002202 * Does the actual free'ing for the cs ptr with an optional flag of whether
2203 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
2205 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002206cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /*
2209 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2210 */
2211 if (csinfo[i].to_fp != NULL)
2212 {
2213 (void)fputs("q\n", csinfo[i].to_fp);
2214 (void)fflush(csinfo[i].to_fp);
2215 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002216#if defined(UNIX)
2217 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002218 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002219 int pstat;
2220 pid_t pid;
2221
2222# if defined(HAVE_SIGACTION)
2223 struct sigaction sa, old;
2224
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002225 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002226 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002227 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002228# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002229 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002230# else
2231 sa.sa_flags = 0;
2232# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002233 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002234 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002235
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002236 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002237 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002238 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002239
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002240 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002241 alarm(0);
2242 sigaction(SIGALRM, &old, NULL);
2243# else
2244 int waited;
2245
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002246 // Can't use sigaction(), loop for two seconds. First yield the CPU
2247 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002248 sleep(0);
2249 for (waited = 0; waited < 40; ++waited)
2250 {
2251 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002252 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002253 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002254 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002255 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002256 }
2257# endif
2258 /*
2259 * If the cscope process is still running: kill it.
2260 * Safety check: If the PID would be zero here, the entire X session
2261 * would be killed. -1 and 1 are dangerous as well.
2262 */
2263 if (pid < 0 && csinfo[i].pid > 1)
2264 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002265# ifdef ECHILD
2266 int alive = TRUE;
2267
2268 if (waitpid_errno == ECHILD)
2269 {
2270 /*
2271 * When using 'vim -g', vim is forked and cscope process is
2272 * no longer a child process but a sibling. So waitpid()
2273 * fails with errno being ECHILD (No child processes).
2274 * Don't send SIGKILL to cscope immediately but wait
2275 * (polling) for it to exit normally as result of sending
2276 * the "q" command, hence giving it a chance to clean up
2277 * its temporary files.
2278 */
2279 int waited;
2280
2281 sleep(0);
2282 for (waited = 0; waited < 40; ++waited)
2283 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002284 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002285 if (kill(csinfo[i].pid, 0) != 0)
2286 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002287 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002288 break;
2289 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002290 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002291 }
2292 }
2293 if (alive)
2294# endif
2295 {
2296 kill(csinfo[i].pid, SIGKILL);
2297 (void)waitpid(csinfo[i].pid, &pstat, 0);
2298 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002299 }
2300 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002301#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002302 if (csinfo[i].hProc != NULL)
2303 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002304 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002305 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2306 TerminateProcess(csinfo[i].hProc, 0);
2307 CloseHandle(csinfo[i].hProc);
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#endif
2310
2311 if (csinfo[i].fr_fp != NULL)
2312 (void)fclose(csinfo[i].fr_fp);
2313 if (csinfo[i].to_fp != NULL)
2314 (void)fclose(csinfo[i].to_fp);
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (freefnpp)
2317 {
2318 vim_free(csinfo[i].fname);
2319 vim_free(csinfo[i].ppath);
2320 vim_free(csinfo[i].flags);
2321 }
2322
2323 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326
2327/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002328 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002331cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2334 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002335 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002337 if (csinfo_size == 0)
2338 return CSCOPE_SUCCESS;
2339
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002340 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002341 dblist = ALLOC_MULT(char *, csinfo_size);
2342 pplist = ALLOC_MULT(char *, csinfo_size);
2343 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (dblist == NULL || pplist == NULL || fllist == NULL)
2345 {
2346 vim_free(dblist);
2347 vim_free(pplist);
2348 vim_free(fllist);
2349 return CSCOPE_FAILURE;
2350 }
2351
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002352 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 dblist[i] = csinfo[i].fname;
2355 pplist[i] = csinfo[i].ppath;
2356 fllist[i] = csinfo[i].flags;
2357 if (csinfo[i].fname != NULL)
2358 cs_release_csp(i, FALSE);
2359 }
2360
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002361 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002362 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 if (dblist[i] != NULL)
2365 {
2366 cs_add_common(dblist[i], pplist[i], fllist[i]);
2367 if (p_csverbose)
2368 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002369 // don't use smsg_attr() because we want to display the
2370 // connection number in the same line as
2371 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002373 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375 }
2376 vim_free(dblist[i]);
2377 vim_free(pplist[i]);
2378 vim_free(fllist[i]);
2379 }
2380 vim_free(dblist);
2381 vim_free(pplist);
2382 vim_free(fllist);
2383
2384 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002385 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002387}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389
2390/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002391 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * (Prepends ppath, if there is one and if it's not already prepended,
2393 * otherwise just uses the name found.)
2394 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002395 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002397 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002400cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002402 char *fullname;
2403 int len;
2404 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002407 * Ppath is freed when we destroy the cscope connection.
2408 * Fullname is freed after cs_make_vim_style_matches, after it's been
2409 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002413 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002414 else if (p_csre && csinfo[i].fname != NULL)
2415 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002416 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2417 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002418 csdir = alloc(MAXPATHL);
2419 if (csdir != NULL)
2420 {
2421 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002422 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002423 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002424 len += (int)STRLEN(csdir);
2425 }
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002428 // Note/example: this won't work if the cscope output already starts
2429 // "../.." and the prefix path is also "../..". if something like this
2430 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002431 if (csinfo[i].ppath != NULL
2432 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2433 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002434#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002435 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002437 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002438 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002439 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002440 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2441 }
2442 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002443 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002444 // Check for csdir to be non empty to avoid empty path concatenated to
2445 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002446 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002449 {
2450 fullname = (char *)vim_strsave((char_u *)name);
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002453 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457
2458/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002459 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002462cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 short i;
2465 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002466 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
2468 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002469 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002472 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 if (csinfo[i].fname == NULL)
2475 continue;
2476
2477 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2480 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002481 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 i, (long)csinfo[i].pid, csinfo[i].fname);
2483 }
2484 }
2485
2486 wait_return(TRUE);
2487 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaar02b06312007-09-06 15:39:22 +00002490
2491/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002492 * Only called when VIM exits to quit any cscope sessions.
2493 */
2494 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002495cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002496{
2497 int i;
2498
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002499 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002500 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002501 vim_free(csinfo);
2502 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002503}
2504
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002505#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002507#if defined(FEAT_EVAL) || defined(PROTO)
2508
2509/*
2510 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2511 *
2512 * Checks the existence of a cscope connection.
2513 */
2514 void
2515f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2516{
2517# ifdef FEAT_CSCOPE
2518 int num = 0;
2519 char_u *dbpath = NULL;
2520 char_u *prepend = NULL;
2521 char_u buf[NUMBUFLEN];
2522
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002523 if (in_vim9script()
2524 && (check_for_opt_number_arg(argvars, 0) == FAIL
2525 || (argvars[0].v_type != VAR_UNKNOWN
2526 && (check_for_opt_string_arg(argvars, 1) == FAIL
2527 || (argvars[1].v_type != VAR_UNKNOWN
2528 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2529 return;
2530
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002531 if (argvars[0].v_type != VAR_UNKNOWN
2532 && argvars[1].v_type != VAR_UNKNOWN)
2533 {
2534 num = (int)tv_get_number(&argvars[0]);
2535 dbpath = tv_get_string(&argvars[1]);
2536 if (argvars[2].v_type != VAR_UNKNOWN)
2537 prepend = tv_get_string_buf(&argvars[2], buf);
2538 }
2539
2540 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2541# endif
2542}
2543
2544#endif // FEAT_EVAL