blob: d9982ef1644cb645e0618320f92114c75d5ec221 [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
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000224 if (*arg == NUL)
225 return;
226
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100227 // (part of) subcommand already typed
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000228 p = skiptowhite(arg);
229 if (*p == NUL)
230 return;
231
232 // past first word
233 xp->xp_pattern = skipwhite(p);
234 if (*skiptowhite(xp->xp_pattern) != NUL)
235 xp->xp_context = EXPAND_NOTHING;
236 else if (STRNICMP(arg, "add", p - arg) == 0)
237 xp->xp_context = EXPAND_FILES;
238 else if (STRNICMP(arg, "kill", p - arg) == 0)
239 expand_what = EXP_CSCOPE_KILL;
240 else if (STRNICMP(arg, "find", p - arg) == 0)
241 expand_what = EXP_CSCOPE_FIND;
242 else
243 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000244}
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/*
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000247 * Find the command, print help if invalid, and then call the corresponding
248 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100251do_cscope_general(
252 exarg_T *eap,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100253 int make_split UNUSED) // whether to split window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 cscmd_T *cmdp;
256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
258 {
259 cs_help(eap);
260 return;
261 }
262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (make_split)
264 {
265 if (!cmdp->cansplit)
266 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100267 (void)msg_puts(_("This cscope command does not support splitting the window.\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 return;
269 }
270 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +0200271 postponed_split_flags = cmdmod.cmod_split;
272 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 cmdp->func(eap);
276
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000278 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279}
280
281/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100282 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 */
284 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100285ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
287 do_cscope_general(eap, FALSE);
288}
289
290/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100291 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 */
293 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100294ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295{
296 do_cscope_general(eap, TRUE);
297}
298
299/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100300 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 */
302 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100303ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304{
305 int ret = FALSE;
306
Bram Moolenaar446cb832008-06-24 21:56:24 +0000307 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000309 (void)emsg(_(e_usage_cstag_ident));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 return;
311 }
312
313 switch (p_csto)
314 {
315 case 0 :
316 if (cs_check_for_connections())
317 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000318 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200319 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 if (ret == FALSE)
321 {
322 cs_free_tags();
323 if (msg_col)
324 msg_putchar('\n');
325
326 if (cs_check_for_tags())
327 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
328 }
329 }
330 else if (cs_check_for_tags())
331 {
332 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
333 }
334 break;
335 case 1 :
336 if (cs_check_for_tags())
337 {
338 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
339 if (ret == FALSE)
340 {
341 if (msg_col)
342 msg_putchar('\n');
343
344 if (cs_check_for_connections())
345 {
346 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200347 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (ret == FALSE)
349 cs_free_tags();
350 }
351 }
352 }
353 else if (cs_check_for_connections())
354 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000355 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200356 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (ret == FALSE)
358 cs_free_tags();
359 }
360 break;
361 default :
362 break;
363 }
364
365 if (!ret)
366 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000367 (void)emsg(_(e_cstag_tag_not_founc));
Bram Moolenaar4033c552017-09-16 20:54:51 +0200368#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 g_do_tagpreview = 0;
370#endif
371 }
372
Bram Moolenaard4db7712016-11-12 19:16:46 +0100373}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375
376/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100377 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 * from the cscope output. should only be called from find_tags()
379 *
380 * returns TRUE if eof, FALSE otherwise
381 */
382 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100383cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
385 char *p;
386
387 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
388 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000389 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
391 return FALSE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100392}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
394
395/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100396 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100399cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 cs_manage_matches(NULL, NULL, -1, Free);
402}
403
404
405/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100406 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
408 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100409cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
411 cs_manage_matches(NULL, NULL, -1, Print);
412}
413
414
415/*
416 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
417 *
418 * Checks for the existence of a |cscope| connection. If no
419 * parameters are specified, then the function returns:
420 *
421 * 0, if cscope was not available (not compiled in), or if there
422 * are no cscope connections; or
423 * 1, if there is at least one cscope connection.
424 *
425 * If parameters are specified, then the value of {num}
426 * determines how existence of a cscope connection is checked:
427 *
428 * {num} Description of existence check
429 * ----- ------------------------------
430 * 0 Same as no parameters (e.g., "cscope_connection()").
431 * 1 Ignore {prepend}, and use partial string matches for
432 * {dbpath}.
433 * 2 Ignore {prepend}, and use exact string matches for
434 * {dbpath}.
435 * 3 Use {prepend}, use partial string matches for both
436 * {dbpath} and {prepend}.
437 * 4 Use {prepend}, use exact string matches for both
438 * {dbpath} and {prepend}.
439 *
440 * Note: All string comparisons are case sensitive!
441 */
442#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200443 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100444cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
446 int i;
447
448 if (num < 0 || num > 4 || (num > 0 && !dbpath))
449 return FALSE;
450
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000451 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {
453 if (!csinfo[i].fname)
454 continue;
455
456 if (num == 0)
457 return TRUE;
458
459 switch (num)
460 {
461 case 1:
462 if (strstr(csinfo[i].fname, (char *)dbpath))
463 return TRUE;
464 break;
465 case 2:
466 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
467 return TRUE;
468 break;
469 case 3:
470 if (strstr(csinfo[i].fname, (char *)dbpath)
471 && ((!ppath && !csinfo[i].ppath)
472 || (ppath
473 && csinfo[i].ppath
474 && strstr(csinfo[i].ppath, (char *)ppath))))
475 return TRUE;
476 break;
477 case 4:
478 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
479 && ((!ppath && !csinfo[i].ppath)
480 || (ppath
481 && csinfo[i].ppath
482 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
483 return TRUE;
484 break;
485 }
486 }
487
488 return FALSE;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200489}
490
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#endif
492
493
494/*
495 * PRIVATE functions
496 ****************************************************************************/
497
498/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100499 * Add cscope database or a directory name (to look for cscope.out)
500 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100503cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 char *fname, *ppath, *flags = NULL;
506
507 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
508 {
509 cs_usage_msg(Add);
510 return CSCOPE_FAILURE;
511 }
512 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
513 flags = strtok((char *)NULL, (const char *)" ");
514
515 return cs_add_common(fname, ppath, flags);
516}
517
518 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100519cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
James McCoy3c5904d2021-10-24 14:50:07 +0100521 int err = errno;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000522 (void)semsg(_(e_stat_str_error_nr), fname, err);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523}
524
525
526/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100527 * The common routine to add a new cscope connection. Called by
528 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * routine uses a number of goto statements.
530 */
531 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100532cs_add_common(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100533 char *arg1, // filename - may contain environment variables
534 char *arg2, // prepend path - may contain environment variables
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100535 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200537 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000538 int ret;
539 char *fname = NULL;
540 char *fname2 = NULL;
541 char *ppath = NULL;
542 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200543 int len;
544 int usedlen = 0;
545 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100547 // get the filename (arg1), expand it, and try to stat it
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200548 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 goto add_err;
550
551 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200552 len = (int)STRLEN(fname);
553 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200554 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200555 (char_u **)&fname, &fbuf, &len);
556 if (fname == NULL)
557 goto add_err;
558 fname = (char *)vim_strnsave((char_u *)fname, len);
559 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200560
Bram Moolenaar8767f522016-07-01 17:17:39 +0200561 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (ret < 0)
563 {
564staterr:
565 if (p_csverbose)
566 cs_stat_emsg(fname);
567 goto add_err;
568 }
569
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100570 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (arg2 != NULL)
572 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200573 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200575 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 goto add_err;
577
578 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200579 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (ret < 0)
581 goto staterr;
582 }
583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100584 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200585 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200587 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (fname2 == NULL)
589 goto add_err;
590
591 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100592#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 || fname[strlen(fname)-1] == '\\'
594#endif
595 )
596 {
597 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200598 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 break;
600 }
601 if (fname[0] == '\0')
602 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
603 else
604 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
605
Bram Moolenaar8767f522016-07-01 17:17:39 +0200606 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (ret < 0)
608 {
609 if (p_csverbose)
610 cs_stat_emsg(fname2);
611 goto add_err;
612 }
613
614 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {
618 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
619 }
620 else
621 {
622 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000623 (void)semsg(_(e_str_is_not_directory_or_valid_cscope_database),
624 fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 goto add_err;
626 }
627
628 if (i != -1)
629 {
630 if (cs_create_connection(i) == CSCOPE_FAILURE
631 || cs_read_prompt(i) == CSCOPE_FAILURE)
632 {
633 cs_release_csp(i, TRUE);
634 goto add_err;
635 }
636
637 if (p_csverbose)
638 {
639 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100640 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100641 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 csinfo[i].fname);
643 }
644 }
645
646 vim_free(fname);
647 vim_free(fname2);
648 vim_free(ppath);
649 return CSCOPE_SUCCESS;
650
651add_err:
652 vim_free(fname2);
653 vim_free(fname);
654 vim_free(ppath);
655 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100656}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658
659 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100660cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100663}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665
666 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100667cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000669 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100670}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672
673/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100674 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 */
676 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100677cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +0000679 int i;
680 int cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000682 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if (csinfo[i].fname != NULL)
684 cnt++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100686}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100689cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100690 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000692 semsg(_(e_error_reading_cscope_connection_nr), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695#define CSREAD_BUFSIZE 2048
696/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100697 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 */
699 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100700cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 char *stok;
703 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100704 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200706 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (buf == NULL)
708 return 0;
709 for (;;)
710 {
711 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
712 {
713 if (feof(csinfo[idx].fr_fp))
714 errno = EIO;
715
716 cs_reading_emsg(idx);
717
718 vim_free(buf);
719 return -1;
720 }
721
722 /*
723 * If the database is out of date, or there's some other problem,
724 * cscope will output error messages before the number-of-lines output.
725 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000726 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100727 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100729 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if ((stok = strtok(buf, (const char *)" ")) == NULL)
732 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000733 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 continue;
735
736 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
737 continue;
738 nlines = atoi(stok);
739 if (nlines < 0)
740 {
741 nlines = 0;
742 break;
743 }
744
745 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
746 continue;
747 if (strncmp((const char *)stok, "lines", 5))
748 continue;
749
750 break;
751 }
752
753 vim_free(buf);
754 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100755}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757
758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * Creates the actual cscope command query from what the user entered.
760 */
761 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100762cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 char *cmd;
765 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000766 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 switch (csoption[0])
769 {
770 case '0' : case 's' :
771 search = 0;
772 break;
773 case '1' : case 'g' :
774 search = 1;
775 break;
776 case '2' : case 'd' :
777 search = 2;
778 break;
779 case '3' : case 'c' :
780 search = 3;
781 break;
782 case '4' : case 't' :
783 search = 4;
784 break;
785 case '6' : case 'e' :
786 search = 6;
787 break;
788 case '7' : case 'f' :
789 search = 7;
790 break;
791 case '8' : case 'i' :
792 search = 8;
793 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200794 case '9' : case 'a' :
795 search = 9;
796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 default :
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000798 (void)emsg(_(e_unknown_cscope_search_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 cs_usage_msg(Find);
800 return NULL;
801 }
802
Bram Moolenaardc215522022-09-25 17:03:26 +0100803 // Skip white space before the pattern, except for text and pattern search,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100804 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000805 pat = pattern;
806 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100807 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000808 ++pat;
809
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200810 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return NULL;
812
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000813 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100816}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818
819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 * This piece of code was taken/adapted from nvi. do we need to add
821 * the BSD license notice?
822 */
823 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100824cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000826#ifdef UNIX
827 int to_cs[2], from_cs[2];
828#endif
Bram Moolenaardc215522022-09-25 17:03:26 +0100829 int cmdlen;
Bram Moolenaar02b06312007-09-06 15:39:22 +0000830 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 Moolenaar071d4272004-06-13 20:20:40 +0000841#endif
842
Bram Moolenaar02b06312007-09-06 15:39:22 +0000843#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 /*
845 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
846 * from_cs[0] and writes to to_cs[1].
847 */
848 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
849 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
850 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000851 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852err_closing:
853 if (to_cs[0] != -1)
854 (void)close(to_cs[0]);
855 if (to_cs[1] != -1)
856 (void)close(to_cs[1]);
857 if (from_cs[0] != -1)
858 (void)close(from_cs[0]);
859 if (from_cs[1] != -1)
860 (void)close(from_cs[1]);
861 return CSCOPE_FAILURE;
862 }
863
ichizok40503052022-01-13 18:09:11 +0000864 if ((csinfo[i].pid = fork()) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000866 (void)emsg(_(e_could_not_fork_for_cscope));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 goto err_closing;
ichizok40503052022-01-13 18:09:11 +0000868 }
869 else if (csinfo[i].pid == 0) // child: run cscope.
870 {
871 char **argv = NULL;
872 int argc = 0;
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (dup2(to_cs[0], STDIN_FILENO) == -1)
875 PERROR("cs_create_connection 1");
876 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
877 PERROR("cs_create_connection 2");
878 if (dup2(from_cs[1], STDERR_FILENO) == -1)
879 PERROR("cs_create_connection 3");
880
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100881 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 (void)close(to_cs[1]);
883 (void)close(from_cs[0]);
884#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100885 // MSWIN
886 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000887 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
888 sa.bInheritHandle = TRUE;
889 sa.lpSecurityDescriptor = NULL;
890
891 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
892 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
893 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000894 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000895err_closing:
896 if (pipe_stdin)
897 {
898 CloseHandle(stdin_rd);
899 CloseHandle(stdin_wr);
900 }
901 if (pipe_stdout)
902 {
903 CloseHandle(stdout_rd);
904 CloseHandle(stdout_wr);
905 }
906 return CSCOPE_FAILURE;
907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100909 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200910 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000913 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100915 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 goto err_closing;
917#endif
918 }
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000919 expand_env(p_csprg, (char_u *)prog, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100921 // alloc space to hold the cscope command
Bram Moolenaardc215522022-09-25 17:03:26 +0100922 cmdlen = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (csinfo[i].ppath)
924 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100925 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200926 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
928 vim_free(prog);
929#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000930 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100932 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 goto err_closing;
934#endif
935 }
936 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
937
Bram Moolenaardc215522022-09-25 17:03:26 +0100938 cmdlen += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940
941 if (csinfo[i].flags)
Bram Moolenaardc215522022-09-25 17:03:26 +0100942 cmdlen += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Bram Moolenaardc215522022-09-25 17:03:26 +0100944 if ((cmd = alloc(cmdlen)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 vim_free(prog);
947 vim_free(ppath);
948#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000949 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100951 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 goto err_closing;
953#endif
954 }
955
ichizok40503052022-01-13 18:09:11 +0000956 // run the cscope command
Bram Moolenaardc215522022-09-25 17:03:26 +0100957#ifdef UNIX
Bram Moolenaar66b8d2a2022-10-13 16:34:30 +0100958 vim_snprintf(cmd, cmdlen, "/bin/sh -c \"exec %s -dl -f %s",
Bram Moolenaardc215522022-09-25 17:03:26 +0100959 prog, csinfo[i].fname);
960#else
961 vim_snprintf(cmd, cmdlen, "%s -dl -f %s", prog, csinfo[i].fname);
962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (csinfo[i].ppath != NULL)
964 {
Bram Moolenaar3292a222022-10-01 20:17:17 +0100965 len = (int)STRLEN(cmd);
Bram Moolenaardc215522022-09-25 17:03:26 +0100966 vim_snprintf(cmd + len, cmdlen - len, " -P%s", csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 if (csinfo[i].flags != NULL)
969 {
Bram Moolenaar3292a222022-10-01 20:17:17 +0100970 len = (int)STRLEN(cmd);
Bram Moolenaardc215522022-09-25 17:03:26 +0100971 vim_snprintf(cmd + len, cmdlen - len, " %s", csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973# ifdef UNIX
Bram Moolenaar66b8d2a2022-10-13 16:34:30 +0100974 // terminate the -c command argument
975 STRCAT(cmd, "\"");
976
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100977 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 vim_free(prog);
979# endif
980 vim_free(ppath);
981
982#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200983# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100984 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200985# if defined(HAVE_SETSID)
986 (void)setsid();
987# else
988 if (setpgid(0, 0) == -1)
989 PERROR(_("cs_create_connection setpgid failed"));
990# endif
991# endif
ichizok40503052022-01-13 18:09:11 +0000992 if (build_argv_from_string((char_u *)cmd, &argv, &argc) == FAIL)
993 exit(EXIT_FAILURE);
994
995 if (execvp(argv[0], argv) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 PERROR(_("cs_create_connection exec failed"));
997
998 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100999 // NOTREACHED
ichizok40503052022-01-13 18:09:11 +00001000 }
1001 else // parent.
1002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * Save the file descriptors for later duplication, and
1005 * reopen as streams.
1006 */
1007 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1009 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1010 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1011
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001012 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 (void)close(to_cs[0]);
1014 (void)close(from_cs[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001017 // MSWIN
1018 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +00001019 GetStartupInfo(&si);
1020 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001021 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +00001022 si.hStdOutput = stdout_wr;
1023 si.hStdError = stdout_wr;
1024 si.hStdInput = stdin_rd;
1025 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1026 NULL, NULL, &si, &pi);
1027 vim_free(prog);
1028 vim_free(cmd);
1029
1030 if (!created)
1031 {
1032 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001033 (void)emsg(_(e_could_not_spawn_cscope_process));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001034 goto err_closing;
1035 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001036 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001037 csinfo[i].pid = pi.dwProcessId;
1038 csinfo[i].hProc = pi.hProcess;
1039 CloseHandle(pi.hThread);
1040
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001041 // TODO - tidy up after failure to create files on pipe handles.
K.Takatac351dc12022-01-24 11:24:08 +00001042 if (((fd = _open_osfhandle((intptr_t)stdin_wr,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001043 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001044 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1045 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
K.Takatac351dc12022-01-24 11:24:08 +00001046 if (((fd = _open_osfhandle((intptr_t)stdout_rd,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001047 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001048 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1049 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1050
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001051 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001052 CloseHandle(stdin_rd);
1053 CloseHandle(stdout_wr);
1054
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001055#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060
1061/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001062 * Query cscope using command line interface. Parse the output and use tselect
1063 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 *
1065 * returns TRUE if we jump to a tag or abort, FALSE if not.
1066 */
1067 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001068cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
1070 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001071 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
1073 if (cs_check_for_connections() == FALSE)
1074 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001075 (void)emsg(_(e_no_cscope_connections));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 return FALSE;
1077 }
1078
1079 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1080 {
1081 cs_usage_msg(Find);
1082 return FALSE;
1083 }
1084
1085 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001086 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
1088 cs_usage_msg(Find);
1089 return FALSE;
1090 }
1091
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001092 /*
1093 * Let's replace the NULs written by strtok() with spaces - we need the
1094 * spaces to correctly display the quickfix/location list window's title.
1095 */
1096 for (i = 0; i < eap_arg_len; ++i)
1097 if (NUL == eap->arg[i])
1098 eap->arg[i] = ' ';
1099
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001100 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001101 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
1104
1105/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001106 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
1108 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001109cs_find_common(
1110 char *opt,
1111 char *pat,
1112 int forceit,
1113 int verbose,
1114 int use_ll UNUSED,
1115 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int i;
1118 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001119 int *nummatches;
1120 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#ifdef FEAT_QUICKFIX
1122 char cmdletter;
1123 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001124
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001125 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001126 switch (opt[0])
1127 {
1128 case '0' :
1129 cmdletter = 's';
1130 break;
1131 case '1' :
1132 cmdletter = 'g';
1133 break;
1134 case '2' :
1135 cmdletter = 'd';
1136 break;
1137 case '3' :
1138 cmdletter = 'c';
1139 break;
1140 case '4' :
1141 cmdletter = 't';
1142 break;
1143 case '6' :
1144 cmdletter = 'e';
1145 break;
1146 case '7' :
1147 cmdletter = 'f';
1148 break;
1149 case '8' :
1150 cmdletter = 'i';
1151 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001152 case '9' :
1153 cmdletter = 'a';
1154 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001155 default :
1156 cmdletter = opt[0];
1157 }
1158
1159 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1160 if (qfpos != NULL)
1161 {
1162 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001163 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001164 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1165 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001166 (void)semsg(_(e_invalid_cscopequickfix_flag_chr_for_chr),
1167 *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001168 return FALSE;
1169 }
1170
Bram Moolenaar21662be2016-11-06 14:46:44 +01001171 if (*qfpos != '0'
1172 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1173 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001174 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001175# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001176 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001177 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001178# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001179 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001183 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 cmd = cs_create_cmd(opt, pat);
1185 if (cmd == NULL)
1186 return FALSE;
1187
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001188 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001189 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001190 {
1191 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001192 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001193 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001194
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001195 // Send query to all open connections, then count the total number
1196 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001197 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 nummatches[i] = 0;
1199 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001200 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001202 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 continue;
1204
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001205 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1207 (void)fflush(csinfo[i].to_fp);
1208
1209 nummatches[i] = cs_cnt_matches(i);
1210
1211 if (nummatches[i] > -1)
1212 totmatches += nummatches[i];
1213
1214 if (nummatches[i] == 0)
1215 (void)cs_read_prompt(i);
1216 }
1217 vim_free(cmd);
1218
1219 if (totmatches == 0)
1220 {
James McCoy3c5904d2021-10-24 14:50:07 +01001221 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001222 (void)semsg(_(e_no_matches_found_for_cscope_query_str_of_str),
1223 opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001224 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 return FALSE;
1226 }
1227
1228#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001229 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001231 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001232 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001233 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001234 qf_info_T *qi = NULL;
1235 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001237 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001239 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001242 cs_file_results(f, nummatches);
1243 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001244 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001245 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001246 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001247 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001248 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001250 if (postponed_split != 0)
1251 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001252 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001254 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001255 postponed_split = 0;
1256 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001257
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001258 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1259 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001260 if (use_ll)
1261 /*
1262 * In the location list window, use the displayed location
1263 * list. Otherwise, use the location list for the window.
1264 */
1265 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1266 ? wp->w_llist_ref : wp->w_llist;
1267 qf_jump(qi, 0, 0, forceit);
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 mch_remove(tmp);
1271 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001272 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 return TRUE;
1274 }
1275 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001276#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001278 char **matches = NULL, **contexts = NULL;
1279 int matched = 0;
1280
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001281 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001282 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001284 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (matches == NULL)
1286 return FALSE;
1287
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001288 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1291 }
1292
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001296 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001299cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 cscmd_T *cmdp = cs_cmds;
1302
Bram Moolenaar32526b32019-01-19 17:43:09 +01001303 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 while (cmdp->name != NULL)
1305 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001306 char *help = _(cmdp->help);
1307 int space_cnt = 30 - vim_strsize((char_u *)help);
1308
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001309 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001310 if (space_cnt < 0)
1311 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001313 cmdp->name,
1314 help, space_cnt, " ",
1315 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001317 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001318 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001319 " c: Find functions calling this function\n"
1320 " d: Find functions called by this function\n"
1321 " e: Find this egrep pattern\n"
1322 " f: Find this file\n"
1323 " g: Find this definition\n"
1324 " i: Find files #including this file\n"
1325 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001326 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 cmdp++;
1329 }
1330
1331 wait_return(TRUE);
1332 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001337clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 csinfo[i].fname = NULL;
1340 csinfo[i].ppath = NULL;
1341 csinfo[i].flags = NULL;
1342#if defined(UNIX)
1343 csinfo[i].st_dev = (dev_t)0;
1344 csinfo[i].st_ino = (ino_t)0;
1345#else
1346 csinfo[i].nVolume = 0;
1347 csinfo[i].nIndexHigh = 0;
1348 csinfo[i].nIndexLow = 0;
1349#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001350 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 csinfo[i].fr_fp = NULL;
1352 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001353#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001354 csinfo[i].hProc = NULL;
1355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001359 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 */
1361 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001362cs_insert_filelist(
1363 char *fname,
1364 char *ppath,
1365 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001366 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001368 int i;
1369 int j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 BY_HANDLE_FILE_INFORMATION bhfi;
1372
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001373 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001375 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1376 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (p_csverbose)
1378 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001379 char *cant_msg = _(e_cannot_open_cscope_database_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char *winmsg = GetWin32Error();
1381
1382 if (winmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001383 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 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);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001466 }
1467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 csinfo[i].ppath = NULL;
1469
1470 if (flags != NULL)
1471 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001472 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001474 VIM_CLEAR(csinfo[i].fname);
1475 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return -1;
1477 }
1478 (void)strcpy(csinfo[i].flags, (const char *)flags);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001479 }
1480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 csinfo[i].flags = NULL;
1482
1483#if defined(UNIX)
1484 csinfo[i].st_dev = sb->st_dev;
1485 csinfo[i].st_ino = sb->st_ino;
1486
1487#else
1488 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1489 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1490 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1491#endif
1492 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001493}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495
1496/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001497 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 */
1499 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001500cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 cscmd_T *cmdp;
1503 char *stok;
1504 size_t len;
1505
1506 if (eap->arg == NULL)
1507 return NULL;
1508
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001509 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001510 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1513 return NULL;
1514
1515 len = strlen(stok);
1516 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1517 {
1518 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1519 return (cmdp);
1520 }
1521 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524
1525/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001526 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001529cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 char *stok;
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001532 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1535 {
1536 cs_usage_msg(Kill);
1537 return CSCOPE_FAILURE;
1538 }
1539
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1542 || (strlen(stok) < 3 && stok[0] == '-'
1543 && VIM_ISDIGIT((int)(stok[1]))))
1544 i = atoi(stok);
1545 else
1546 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001547 // It must be part of a name. We will try to find a match
1548 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001549 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1552 break;
1553 }
1554 }
1555
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001556 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 if (p_csverbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001559 (void)semsg(_(e_cscope_connection_str_not_founc), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 }
1561 else
1562 {
1563 if (i == -1)
1564 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001565 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 if (csinfo[i].fname)
1568 cs_kill_execute(i, csinfo[i].fname);
1569 }
1570 }
1571 else
1572 cs_kill_execute(i, stok);
1573 }
1574
1575 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578
1579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 * Actually kills a specific cscope connection.
1581 */
1582 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001583cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584 int i, // cscope table index
1585 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 if (p_csverbose)
1588 {
1589 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001590 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001591 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 cs_release_csp(i, TRUE);
1594}
1595
1596
1597/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001598 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1600 * the type of the tag you are looking for. for example, in Darren Hiebert's
1601 * ctags (the one that comes with vim), #define's use a line number to find the
1602 * tag in a file while function definitions use a regexp search pattern.
1603 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001604 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * quirky (and probably other things i don't know about):
1606 *
1607 * if you have "# define" in your source file, which is
1608 * perfectly legal, cscope thinks you have "#define". this
1609 * will result in a failed regexp search. :(
1610 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001611 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 * would still have to be modified to escape all the special regular expression
1613 * characters to comply with ctags formatting.
1614 */
1615 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001616cs_make_vim_style_matches(
1617 char *fname,
1618 char *slno,
1619 char *search,
1620 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001622 // vim style is ctags:
1623 //
1624 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1625 //
1626 // but as mentioned above, we'll always use the line number and
1627 // put the search pattern (if one exists) as "extra"
1628 //
1629 // buf is used as part of vim's method of handling tags, and
1630 // (i think) vim frees it when you pop your tags and get replaced
1631 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 char *buf;
1633 int amt;
1634
1635 if (search != NULL)
1636 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001637 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001638 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 return NULL;
1640
1641 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1642 }
1643 else
1644 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001645 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001646 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 return NULL;
1648
1649 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1650 }
1651
1652 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655
1656/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001657 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 *
1659 * Store: keep a ptr to the (malloc'd) memory of matches originally
1660 * generated from cs_find(). the matches are originally lines directly
1661 * from cscope output, but transformed to look like something out of a
1662 * ctags. see cs_make_vim_style_matches for more details.
1663 *
1664 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1665 * the next line from the cscope output. it basically keeps track of which
1666 * lines have been "used" and returns the next one.
1667 *
1668 * Free: frees up everything and resets
1669 *
1670 * Print: prints the tags
1671 */
1672 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001673cs_manage_matches(
1674 char **matches,
1675 char **contexts,
1676 int totmatches,
1677 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 static char **mp = NULL;
1680 static char **cp = NULL;
1681 static int cnt = -1;
1682 static int next = -1;
1683 char *p = NULL;
1684
1685 switch (cmd)
1686 {
1687 case Store:
1688 assert(matches != NULL);
1689 assert(totmatches > 0);
1690 if (mp != NULL || cp != NULL)
1691 (void)cs_manage_matches(NULL, NULL, -1, Free);
1692 mp = matches;
1693 cp = contexts;
1694 cnt = totmatches;
1695 next = 0;
1696 break;
1697 case Get:
1698 if (next >= cnt)
1699 return NULL;
1700
1701 p = mp[next];
1702 next++;
1703 break;
1704 case Free:
1705 if (mp != NULL)
1706 {
1707 if (cnt > 0)
1708 while (cnt--)
1709 {
1710 vim_free(mp[cnt]);
1711 if (cp != NULL)
1712 vim_free(cp[cnt]);
1713 }
1714 vim_free(mp);
1715 vim_free(cp);
1716 }
1717 mp = NULL;
1718 cp = NULL;
1719 cnt = 0;
1720 next = 0;
1721 break;
1722 case Print:
1723 cs_print_tags_priv(mp, cp, cnt);
1724 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001725 default: // should not reach here
RestorerZ68ebcee2023-05-31 17:12:14 +01001726 iemsg(e_fatal_error_in_cs_manage_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 return NULL;
1728 }
1729
1730 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001731}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733
1734/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001735 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
1737 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001738cs_parse_results(
1739 int cnumber,
1740 char *buf,
1741 int bufsize,
1742 char **context,
1743 char **linenumber,
1744 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int ch;
1747 char *p;
1748 char *name;
1749
1750 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1751 {
1752 if (feof(csinfo[cnumber].fr_fp))
1753 errno = EIO;
1754
1755 cs_reading_emsg(cnumber);
1756
1757 return NULL;
1758 }
1759
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001760 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if ((p = strchr(buf, '\n')) == NULL)
1762 {
1763 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1764 ;
1765 return NULL;
1766 }
1767 *p = '\0';
1768
1769 /*
1770 * cscope output is in the following format:
1771 *
1772 * <filename> <context> <line number> <pattern>
1773 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001774 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 return NULL;
1776 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1777 return NULL;
1778 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1779 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001782 // --- nvi ---
1783 // If the file is older than the cscope database, that is,
1784 // the database was built since the file was last modified,
1785 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 if (strcmp(*search, "<unknown>") == 0)
1787 *search = NULL;
1788
1789 name = cs_resolve_file(cnumber, name);
1790 return name;
1791}
1792
Bram Moolenaarc716c302006-01-21 22:12:51 +00001793#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001795 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001798cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 int i, j;
1801 char *buf;
1802 char *search, *slno;
1803 char *fullname;
1804 char *cntx;
1805 char *context;
1806
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001807 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (buf == NULL)
1809 return;
1810
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001811 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 if (nummatches_a[i] < 1)
1814 continue;
1815
1816 for (j = 0; j < nummatches_a[i]; j++)
1817 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001818 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1819 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 continue;
1821
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001822 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001823 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001824 {
1825 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 if (strcmp(cntx, "<global>")==0)
1830 strcpy(context, "<<global>>");
1831 else
1832 sprintf(context, "<<%s>>", cntx);
1833
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001834 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1836 else
1837 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1838
1839 vim_free(context);
1840 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001841 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 (void)cs_read_prompt(i);
1844
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001845 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 vim_free(buf);
1847}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001851 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1852 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001853 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001856cs_fill_results(
1857 char *tagstr,
1858 int totmatches,
1859 int *nummatches_a,
1860 char ***matches_p,
1861 char ***cntxts_p,
1862 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 int i, j;
1865 char *buf;
1866 char *search, *slno;
1867 int totsofar = 0;
1868 char **matches = NULL;
1869 char **cntxts = NULL;
1870 char *fullname;
1871 char *cntx;
1872
1873 assert(totmatches > 0);
1874
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001875 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (buf == NULL)
1877 return;
1878
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001879 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001881 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 goto parse_out;
1883
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001884 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
1886 if (nummatches_a[i] < 1)
1887 continue;
1888
1889 for (j = 0; j < nummatches_a[i]; j++)
1890 {
1891 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1892 &slno, &search)) == NULL)
1893 continue;
1894
1895 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1896 search, tagstr);
1897
1898 vim_free(fullname);
1899
1900 if (strcmp(cntx, "<global>") == 0)
1901 cntxts[totsofar] = NULL;
1902 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001903 // note: if vim_strsave returns NULL, then the context
1904 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1906
1907 if (matches[totsofar] != NULL)
1908 totsofar++;
1909
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001910 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912 (void)cs_read_prompt(i);
1913
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001914 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001917 if (totsofar == 0)
1918 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001919 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001920 VIM_CLEAR(matches);
1921 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 *matched = totsofar;
1924 *matches_p = matches;
1925 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001928}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001931/*
1932 * get the requested path components
1933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001935cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937 int i;
1938 char *s;
1939
1940 if (p_cspc == 0)
1941 return path;
1942
1943 s = path + strlen(path) - 1;
1944 for (i = 0; i < p_cspc; ++i)
1945 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001946#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 && *--s != '\\'
1948#endif
1949 )
1950 ;
1951 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001952#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 || (s > path && *s == '\\')
1954#endif
1955 )
1956 ++s;
1957 return s;
1958}
1959
1960/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001961 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 */
1963 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001964cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
1966 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001967 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001968 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 int newsize = 0;
1970 char *ptag;
1971 char *fname, *lno, *extra, *tbuf;
1972 int i, idx, num;
1973 char *globalcntx = "GLOBAL";
1974 char *cntxformat = " <<%s>>";
1975 char *context;
1976 char *cstag_msg = _("Cscope tag: %s");
1977 char *csfmt_str = "%4d %6s ";
1978
Bram Moolenaar4033c552017-09-16 20:54:51 +02001979 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001981 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 return;
1983
1984 strcpy(tbuf, matches[0]);
1985 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001986 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001987 {
1988 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001989 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001992 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001993 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (buf != NULL)
1995 {
1996 bufsize = newsize;
1997 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000
2001 vim_free(tbuf);
2002
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002003 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002005 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 num = 1;
2008 for (i = 0; i < num_matches; i++)
2009 {
2010 idx = i;
2011
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002012 // if we really wanted to, we could avoid this malloc and strcpy
2013 // by parsing matches[i] on the fly and placing stuff into buf
2014 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002015 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 continue;
2017 (void)strcpy(tbuf, matches[idx]);
2018
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002019 if (strtok(tbuf, (const char *)"\t") == NULL
2020 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2021 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2022 {
2023 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002025 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002026 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002028 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002030 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002031 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (bufsize < newsize)
2033 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002034 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002035 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002039 vim_free(t_buf);
2040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 else
2042 bufsize = newsize;
2043 }
2044 if (buf != NULL)
2045 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002046 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002048 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002050 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2051 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002053 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if (cntxts[idx] != NULL)
2055 context = cntxts[idx];
2056 else
2057 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002058 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 if (bufsize < newsize)
2061 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002062 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002063 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002067 vim_free(t_buf);
2068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 bufsize = newsize;
2071 }
2072 if (buf != NULL)
2073 {
2074 (void)sprintf(buf, cntxformat, context);
2075
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002076 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (msg_col + (int)strlen(buf) >= (int)Columns)
2078 msg_putchar('\n');
2079 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002080 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 msg_putchar('\n');
2082 }
2083 if (extra != NULL)
2084 {
2085 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002086 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002089 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 if (msg_col)
2092 msg_putchar('\n');
2093
2094 ui_breakcheck();
2095 if (got_int)
2096 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002097 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 break;
2099 }
2100
2101 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002102 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107
2108/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002109 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
2111 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002112cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002115 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 int bufpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 int maxlen;
2118 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 int n;
2121
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 // compute maximum allowed len for Cscope error message
RestorerZ68ebcee2023-05-31 17:12:14 +01002123 char *cs_emsg = _(e_cscope_error_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2125
2126 for (;;)
2127 {
2128 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002129 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2131 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002132 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002133 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (buf != NULL)
2135 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002136 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 buf[bufpos++] = ch;
2138 buf[bufpos] = NUL;
2139 if (bufpos >= epromptlen
2140 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2141 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002142 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 buf[bufpos - epromptlen] = NUL;
2144
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002145 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002146 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002148 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 (void)putc('\n', csinfo[i].to_fp);
2150 (void)fflush(csinfo[i].to_fp);
2151
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002152 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 bufpos = 0;
2154 buf[bufpos] = NUL;
2155 }
2156 }
2157 }
2158
2159 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2160 {
2161 if (n > 0)
2162 ch = getc(csinfo[i].fr_fp);
2163 if (ch == EOF)
2164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002168 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 cs_release_csp(i, TRUE);
2170 vim_free(buf);
2171 return CSCOPE_FAILURE;
2172 }
2173
2174 if (ch != CSCOPE_PROMPT[n])
2175 {
2176 ch = EOF;
2177 break;
2178 }
2179 }
2180
2181 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002182 continue; // didn't find the prompt
2183 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
2186 vim_free(buf);
2187 return CSCOPE_SUCCESS;
2188}
2189
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002190#if defined(UNIX) && defined(SIGALRM)
2191/*
2192 * Used to catch and ignore SIGALRM below.
2193 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01002194 static void
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002195sig_handler SIGDEFARG(sigarg)
2196{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002197 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002198}
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{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00002464 int i;
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002467 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 else
2469 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002470 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002472 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002473 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 if (csinfo[i].fname == NULL)
2476 continue;
2477
2478 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2481 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002482 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 i, (long)csinfo[i].pid, csinfo[i].fname);
2484 }
2485 }
2486
Bram Moolenaar13608d82022-08-29 15:06:50 +01002487 wait_return(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar02b06312007-09-06 15:39:22 +00002491
2492/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002493 * Only called when VIM exits to quit any cscope sessions.
2494 */
2495 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002496cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002497{
2498 int i;
2499
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002500 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002501 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002502 vim_free(csinfo);
2503 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002504}
2505
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002506#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002508#if defined(FEAT_EVAL) || defined(PROTO)
2509
2510/*
2511 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2512 *
2513 * Checks the existence of a cscope connection.
2514 */
2515 void
2516f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2517{
2518# ifdef FEAT_CSCOPE
2519 int num = 0;
2520 char_u *dbpath = NULL;
2521 char_u *prepend = NULL;
2522 char_u buf[NUMBUFLEN];
2523
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002524 if (in_vim9script()
2525 && (check_for_opt_number_arg(argvars, 0) == FAIL
2526 || (argvars[0].v_type != VAR_UNKNOWN
2527 && (check_for_opt_string_arg(argvars, 1) == FAIL
2528 || (argvars[1].v_type != VAR_UNKNOWN
2529 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2530 return;
2531
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002532 if (argvars[0].v_type != VAR_UNKNOWN
2533 && argvars[1].v_type != VAR_UNKNOWN)
2534 {
2535 num = (int)tv_get_number(&argvars[0]);
2536 dbpath = tv_get_string(&argvars[1]);
2537 if (argvars[2].v_type != VAR_UNKNOWN)
2538 prepend = tv_get_string_buf(&argvars[2], buf);
2539 }
2540
2541 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2542# endif
2543}
2544
2545#endif // FEAT_EVAL