blob: ffb1784e1b7071138ee88ad259030bf49ad14c2e [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);
1466 } else
1467 csinfo[i].ppath = NULL;
1468
1469 if (flags != NULL)
1470 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001471 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001473 VIM_CLEAR(csinfo[i].fname);
1474 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return -1;
1476 }
1477 (void)strcpy(csinfo[i].flags, (const char *)flags);
1478 } else
1479 csinfo[i].flags = NULL;
1480
1481#if defined(UNIX)
1482 csinfo[i].st_dev = sb->st_dev;
1483 csinfo[i].st_ino = sb->st_ino;
1484
1485#else
1486 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1487 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1488 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1489#endif
1490 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493
1494/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001495 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001498cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 cscmd_T *cmdp;
1501 char *stok;
1502 size_t len;
1503
1504 if (eap->arg == NULL)
1505 return NULL;
1506
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001507 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001508 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1511 return NULL;
1512
1513 len = strlen(stok);
1514 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1515 {
1516 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1517 return (cmdp);
1518 }
1519 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
1522
1523/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001524 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001527cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 char *stok;
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001530 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1533 {
1534 cs_usage_msg(Kill);
1535 return CSCOPE_FAILURE;
1536 }
1537
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001538 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1540 || (strlen(stok) < 3 && stok[0] == '-'
1541 && VIM_ISDIGIT((int)(stok[1]))))
1542 i = atoi(stok);
1543 else
1544 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001545 // It must be part of a name. We will try to find a match
1546 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001547 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1550 break;
1551 }
1552 }
1553
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001554 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 if (p_csverbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001557 (void)semsg(_(e_cscope_connection_str_not_founc), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559 else
1560 {
1561 if (i == -1)
1562 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001563 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 if (csinfo[i].fname)
1566 cs_kill_execute(i, csinfo[i].fname);
1567 }
1568 }
1569 else
1570 cs_kill_execute(i, stok);
1571 }
1572
1573 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576
1577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 * Actually kills a specific cscope connection.
1579 */
1580 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001581cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001582 int i, // cscope table index
1583 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 if (p_csverbose)
1586 {
1587 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001588 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001589 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 cs_release_csp(i, TRUE);
1592}
1593
1594
1595/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001596 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1598 * the type of the tag you are looking for. for example, in Darren Hiebert's
1599 * ctags (the one that comes with vim), #define's use a line number to find the
1600 * tag in a file while function definitions use a regexp search pattern.
1601 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001602 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * quirky (and probably other things i don't know about):
1604 *
1605 * if you have "# define" in your source file, which is
1606 * perfectly legal, cscope thinks you have "#define". this
1607 * will result in a failed regexp search. :(
1608 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001609 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * would still have to be modified to escape all the special regular expression
1611 * characters to comply with ctags formatting.
1612 */
1613 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001614cs_make_vim_style_matches(
1615 char *fname,
1616 char *slno,
1617 char *search,
1618 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 // vim style is ctags:
1621 //
1622 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1623 //
1624 // but as mentioned above, we'll always use the line number and
1625 // put the search pattern (if one exists) as "extra"
1626 //
1627 // buf is used as part of vim's method of handling tags, and
1628 // (i think) vim frees it when you pop your tags and get replaced
1629 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 char *buf;
1631 int amt;
1632
1633 if (search != NULL)
1634 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001635 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001636 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return NULL;
1638
1639 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1640 }
1641 else
1642 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001643 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001644 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 return NULL;
1646
1647 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1648 }
1649
1650 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001651}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653
1654/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001655 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 *
1657 * Store: keep a ptr to the (malloc'd) memory of matches originally
1658 * generated from cs_find(). the matches are originally lines directly
1659 * from cscope output, but transformed to look like something out of a
1660 * ctags. see cs_make_vim_style_matches for more details.
1661 *
1662 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1663 * the next line from the cscope output. it basically keeps track of which
1664 * lines have been "used" and returns the next one.
1665 *
1666 * Free: frees up everything and resets
1667 *
1668 * Print: prints the tags
1669 */
1670 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001671cs_manage_matches(
1672 char **matches,
1673 char **contexts,
1674 int totmatches,
1675 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 static char **mp = NULL;
1678 static char **cp = NULL;
1679 static int cnt = -1;
1680 static int next = -1;
1681 char *p = NULL;
1682
1683 switch (cmd)
1684 {
1685 case Store:
1686 assert(matches != NULL);
1687 assert(totmatches > 0);
1688 if (mp != NULL || cp != NULL)
1689 (void)cs_manage_matches(NULL, NULL, -1, Free);
1690 mp = matches;
1691 cp = contexts;
1692 cnt = totmatches;
1693 next = 0;
1694 break;
1695 case Get:
1696 if (next >= cnt)
1697 return NULL;
1698
1699 p = mp[next];
1700 next++;
1701 break;
1702 case Free:
1703 if (mp != NULL)
1704 {
1705 if (cnt > 0)
1706 while (cnt--)
1707 {
1708 vim_free(mp[cnt]);
1709 if (cp != NULL)
1710 vim_free(cp[cnt]);
1711 }
1712 vim_free(mp);
1713 vim_free(cp);
1714 }
1715 mp = NULL;
1716 cp = NULL;
1717 cnt = 0;
1718 next = 0;
1719 break;
1720 case Print:
1721 cs_print_tags_priv(mp, cp, cnt);
1722 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001723 default: // should not reach here
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001724 iemsg(_(e_fatal_error_in_cs_manage_matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 return NULL;
1726 }
1727
1728 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731
1732/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001733 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001736cs_parse_results(
1737 int cnumber,
1738 char *buf,
1739 int bufsize,
1740 char **context,
1741 char **linenumber,
1742 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
1744 int ch;
1745 char *p;
1746 char *name;
1747
1748 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1749 {
1750 if (feof(csinfo[cnumber].fr_fp))
1751 errno = EIO;
1752
1753 cs_reading_emsg(cnumber);
1754
1755 return NULL;
1756 }
1757
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001758 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if ((p = strchr(buf, '\n')) == NULL)
1760 {
1761 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1762 ;
1763 return NULL;
1764 }
1765 *p = '\0';
1766
1767 /*
1768 * cscope output is in the following format:
1769 *
1770 * <filename> <context> <line number> <pattern>
1771 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001772 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 return NULL;
1774 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1775 return NULL;
1776 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1777 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001778 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 // --- nvi ---
1781 // If the file is older than the cscope database, that is,
1782 // the database was built since the file was last modified,
1783 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (strcmp(*search, "<unknown>") == 0)
1785 *search = NULL;
1786
1787 name = cs_resolve_file(cnumber, name);
1788 return name;
1789}
1790
Bram Moolenaarc716c302006-01-21 22:12:51 +00001791#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001793 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
1795 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001796cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int i, j;
1799 char *buf;
1800 char *search, *slno;
1801 char *fullname;
1802 char *cntx;
1803 char *context;
1804
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001805 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (buf == NULL)
1807 return;
1808
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001809 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811 if (nummatches_a[i] < 1)
1812 continue;
1813
1814 for (j = 0; j < nummatches_a[i]; j++)
1815 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001816 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1817 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 continue;
1819
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001820 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001821 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001822 {
1823 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827 if (strcmp(cntx, "<global>")==0)
1828 strcpy(context, "<<global>>");
1829 else
1830 sprintf(context, "<<%s>>", cntx);
1831
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001832 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1834 else
1835 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1836
1837 vim_free(context);
1838 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001839 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 (void)cs_read_prompt(i);
1842
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001843 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 vim_free(buf);
1845}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001849 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1850 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001851 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 */
1853 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001854cs_fill_results(
1855 char *tagstr,
1856 int totmatches,
1857 int *nummatches_a,
1858 char ***matches_p,
1859 char ***cntxts_p,
1860 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 int i, j;
1863 char *buf;
1864 char *search, *slno;
1865 int totsofar = 0;
1866 char **matches = NULL;
1867 char **cntxts = NULL;
1868 char *fullname;
1869 char *cntx;
1870
1871 assert(totmatches > 0);
1872
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001873 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (buf == NULL)
1875 return;
1876
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001877 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001879 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 goto parse_out;
1881
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001882 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 if (nummatches_a[i] < 1)
1885 continue;
1886
1887 for (j = 0; j < nummatches_a[i]; j++)
1888 {
1889 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1890 &slno, &search)) == NULL)
1891 continue;
1892
1893 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1894 search, tagstr);
1895
1896 vim_free(fullname);
1897
1898 if (strcmp(cntx, "<global>") == 0)
1899 cntxts[totsofar] = NULL;
1900 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001901 // note: if vim_strsave returns NULL, then the context
1902 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1904
1905 if (matches[totsofar] != NULL)
1906 totsofar++;
1907
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001908 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 (void)cs_read_prompt(i);
1911
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001912 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001915 if (totsofar == 0)
1916 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001917 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001918 VIM_CLEAR(matches);
1919 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 *matched = totsofar;
1922 *matches_p = matches;
1923 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001929/*
1930 * get the requested path components
1931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001933cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 int i;
1936 char *s;
1937
1938 if (p_cspc == 0)
1939 return path;
1940
1941 s = path + strlen(path) - 1;
1942 for (i = 0; i < p_cspc; ++i)
1943 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001944#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 && *--s != '\\'
1946#endif
1947 )
1948 ;
1949 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001950#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 || (s > path && *s == '\\')
1952#endif
1953 )
1954 ++s;
1955 return s;
1956}
1957
1958/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001959 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
1961 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001962cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001965 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001966 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 int newsize = 0;
1968 char *ptag;
1969 char *fname, *lno, *extra, *tbuf;
1970 int i, idx, num;
1971 char *globalcntx = "GLOBAL";
1972 char *cntxformat = " <<%s>>";
1973 char *context;
1974 char *cstag_msg = _("Cscope tag: %s");
1975 char *csfmt_str = "%4d %6s ";
1976
Bram Moolenaar4033c552017-09-16 20:54:51 +02001977 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001979 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 return;
1981
1982 strcpy(tbuf, matches[0]);
1983 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001984 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001985 {
1986 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001987 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001990 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001991 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (buf != NULL)
1993 {
1994 bufsize = newsize;
1995 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001996 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999 vim_free(tbuf);
2000
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002001 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002003 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 num = 1;
2006 for (i = 0; i < num_matches; i++)
2007 {
2008 idx = i;
2009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002010 // if we really wanted to, we could avoid this malloc and strcpy
2011 // by parsing matches[i] on the fly and placing stuff into buf
2012 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002013 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 continue;
2015 (void)strcpy(tbuf, matches[idx]);
2016
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002017 if (strtok(tbuf, (const char *)"\t") == NULL
2018 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2019 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2020 {
2021 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002023 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002024 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002026 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002028 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002029 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (bufsize < newsize)
2031 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002032 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002033 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002037 vim_free(t_buf);
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 else
2040 bufsize = newsize;
2041 }
2042 if (buf != NULL)
2043 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002044 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002046 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002048 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2049 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002051 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (cntxts[idx] != NULL)
2053 context = cntxts[idx];
2054 else
2055 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002056 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 if (bufsize < newsize)
2059 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002060 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002061 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002065 vim_free(t_buf);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 bufsize = newsize;
2069 }
2070 if (buf != NULL)
2071 {
2072 (void)sprintf(buf, cntxformat, context);
2073
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002074 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (msg_col + (int)strlen(buf) >= (int)Columns)
2076 msg_putchar('\n');
2077 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002078 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 msg_putchar('\n');
2080 }
2081 if (extra != NULL)
2082 {
2083 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002084 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002087 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 if (msg_col)
2090 msg_putchar('\n');
2091
2092 ui_breakcheck();
2093 if (got_int)
2094 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002095 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 break;
2097 }
2098
2099 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002100 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105
2106/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002107 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
2109 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002110cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002113 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int bufpos = 0;
2115 char *cs_emsg;
2116 int maxlen;
2117 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002118 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int n;
2120
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002121 cs_emsg = _(e_cscope_error_str);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2124
2125 for (;;)
2126 {
2127 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002128 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2130 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002132 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (buf != NULL)
2134 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002135 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 buf[bufpos++] = ch;
2137 buf[bufpos] = NUL;
2138 if (bufpos >= epromptlen
2139 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2140 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002141 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 buf[bufpos - epromptlen] = NUL;
2143
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002144 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002147 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 (void)putc('\n', csinfo[i].to_fp);
2149 (void)fflush(csinfo[i].to_fp);
2150
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002151 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 bufpos = 0;
2153 buf[bufpos] = NUL;
2154 }
2155 }
2156 }
2157
2158 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2159 {
2160 if (n > 0)
2161 ch = getc(csinfo[i].fr_fp);
2162 if (ch == EOF)
2163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002167 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 cs_release_csp(i, TRUE);
2169 vim_free(buf);
2170 return CSCOPE_FAILURE;
2171 }
2172
2173 if (ch != CSCOPE_PROMPT[n])
2174 {
2175 ch = EOF;
2176 break;
2177 }
2178 }
2179
2180 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002181 continue; // didn't find the prompt
2182 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184
2185 vim_free(buf);
2186 return CSCOPE_SUCCESS;
2187}
2188
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002189#if defined(UNIX) && defined(SIGALRM)
2190/*
2191 * Used to catch and ignore SIGALRM below.
2192 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01002193 static void
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002194sig_handler SIGDEFARG(sigarg)
2195{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002196 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002197}
2198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002201 * Does the actual free'ing for the cs ptr with an optional flag of whether
2202 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 */
2204 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002205cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /*
2208 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2209 */
2210 if (csinfo[i].to_fp != NULL)
2211 {
2212 (void)fputs("q\n", csinfo[i].to_fp);
2213 (void)fflush(csinfo[i].to_fp);
2214 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002215#if defined(UNIX)
2216 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002217 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002218 int pstat;
2219 pid_t pid;
2220
2221# if defined(HAVE_SIGACTION)
2222 struct sigaction sa, old;
2223
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002224 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002225 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002226 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002227# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002228 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002229# else
2230 sa.sa_flags = 0;
2231# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002232 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002233 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002234
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002235 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002236 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002237 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002238
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002239 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002240 alarm(0);
2241 sigaction(SIGALRM, &old, NULL);
2242# else
2243 int waited;
2244
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002245 // Can't use sigaction(), loop for two seconds. First yield the CPU
2246 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002247 sleep(0);
2248 for (waited = 0; waited < 40; ++waited)
2249 {
2250 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002251 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002252 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002253 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002254 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002255 }
2256# endif
2257 /*
2258 * If the cscope process is still running: kill it.
2259 * Safety check: If the PID would be zero here, the entire X session
2260 * would be killed. -1 and 1 are dangerous as well.
2261 */
2262 if (pid < 0 && csinfo[i].pid > 1)
2263 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002264# ifdef ECHILD
2265 int alive = TRUE;
2266
2267 if (waitpid_errno == ECHILD)
2268 {
2269 /*
2270 * When using 'vim -g', vim is forked and cscope process is
2271 * no longer a child process but a sibling. So waitpid()
2272 * fails with errno being ECHILD (No child processes).
2273 * Don't send SIGKILL to cscope immediately but wait
2274 * (polling) for it to exit normally as result of sending
2275 * the "q" command, hence giving it a chance to clean up
2276 * its temporary files.
2277 */
2278 int waited;
2279
2280 sleep(0);
2281 for (waited = 0; waited < 40; ++waited)
2282 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002283 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002284 if (kill(csinfo[i].pid, 0) != 0)
2285 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002286 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002287 break;
2288 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002289 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002290 }
2291 }
2292 if (alive)
2293# endif
2294 {
2295 kill(csinfo[i].pid, SIGKILL);
2296 (void)waitpid(csinfo[i].pid, &pstat, 0);
2297 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002298 }
2299 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002301 if (csinfo[i].hProc != NULL)
2302 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002303 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002304 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2305 TerminateProcess(csinfo[i].hProc, 0);
2306 CloseHandle(csinfo[i].hProc);
2307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309
2310 if (csinfo[i].fr_fp != NULL)
2311 (void)fclose(csinfo[i].fr_fp);
2312 if (csinfo[i].to_fp != NULL)
2313 (void)fclose(csinfo[i].to_fp);
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (freefnpp)
2316 {
2317 vim_free(csinfo[i].fname);
2318 vim_free(csinfo[i].ppath);
2319 vim_free(csinfo[i].flags);
2320 }
2321
2322 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002323}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325
2326/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002327 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002330cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2333 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002334 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002336 if (csinfo_size == 0)
2337 return CSCOPE_SUCCESS;
2338
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002339 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002340 dblist = ALLOC_MULT(char *, csinfo_size);
2341 pplist = ALLOC_MULT(char *, csinfo_size);
2342 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (dblist == NULL || pplist == NULL || fllist == NULL)
2344 {
2345 vim_free(dblist);
2346 vim_free(pplist);
2347 vim_free(fllist);
2348 return CSCOPE_FAILURE;
2349 }
2350
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002351 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 dblist[i] = csinfo[i].fname;
2354 pplist[i] = csinfo[i].ppath;
2355 fllist[i] = csinfo[i].flags;
2356 if (csinfo[i].fname != NULL)
2357 cs_release_csp(i, FALSE);
2358 }
2359
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002360 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002361 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 if (dblist[i] != NULL)
2364 {
2365 cs_add_common(dblist[i], pplist[i], fllist[i]);
2366 if (p_csverbose)
2367 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002368 // don't use smsg_attr() because we want to display the
2369 // connection number in the same line as
2370 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002372 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 }
2375 vim_free(dblist[i]);
2376 vim_free(pplist[i]);
2377 vim_free(fllist[i]);
2378 }
2379 vim_free(dblist);
2380 vim_free(pplist);
2381 vim_free(fllist);
2382
2383 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002384 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388
2389/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002390 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * (Prepends ppath, if there is one and if it's not already prepended,
2392 * otherwise just uses the name found.)
2393 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002394 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002396 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
2398 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002399cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002401 char *fullname;
2402 int len;
2403 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002406 * Ppath is freed when we destroy the cscope connection.
2407 * Fullname is freed after cs_make_vim_style_matches, after it's been
2408 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002410 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002412 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002413 else if (p_csre && csinfo[i].fname != NULL)
2414 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002415 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2416 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002417 csdir = alloc(MAXPATHL);
2418 if (csdir != NULL)
2419 {
2420 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002421 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002422 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002423 len += (int)STRLEN(csdir);
2424 }
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002427 // Note/example: this won't work if the cscope output already starts
2428 // "../.." and the prefix path is also "../..". if something like this
2429 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002430 if (csinfo[i].ppath != NULL
2431 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2432 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002433#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002434 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002436 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002437 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002438 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002439 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2440 }
2441 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002442 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002443 // Check for csdir to be non empty to avoid empty path concatenated to
2444 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002445 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002448 {
2449 fullname = (char *)vim_strsave((char_u *)name);
2450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002452 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002454}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456
2457/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002458 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002461cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00002463 int i;
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002466 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
2468 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002469 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002472 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 if (csinfo[i].fname == NULL)
2475 continue;
2476
2477 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2480 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002481 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 i, (long)csinfo[i].pid, csinfo[i].fname);
2483 }
2484 }
2485
Bram Moolenaar13608d82022-08-29 15:06:50 +01002486 wait_return(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaar02b06312007-09-06 15:39:22 +00002490
2491/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002492 * Only called when VIM exits to quit any cscope sessions.
2493 */
2494 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002495cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002496{
2497 int i;
2498
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002499 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002500 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002501 vim_free(csinfo);
2502 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002503}
2504
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002505#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002507#if defined(FEAT_EVAL) || defined(PROTO)
2508
2509/*
2510 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2511 *
2512 * Checks the existence of a cscope connection.
2513 */
2514 void
2515f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2516{
2517# ifdef FEAT_CSCOPE
2518 int num = 0;
2519 char_u *dbpath = NULL;
2520 char_u *prepend = NULL;
2521 char_u buf[NUMBUFLEN];
2522
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002523 if (in_vim9script()
2524 && (check_for_opt_number_arg(argvars, 0) == FAIL
2525 || (argvars[0].v_type != VAR_UNKNOWN
2526 && (check_for_opt_string_arg(argvars, 1) == FAIL
2527 || (argvars[1].v_type != VAR_UNKNOWN
2528 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2529 return;
2530
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002531 if (argvars[0].v_type != VAR_UNKNOWN
2532 && argvars[1].v_type != VAR_UNKNOWN)
2533 {
2534 num = (int)tv_get_number(&argvars[0]);
2535 dbpath = tv_get_string(&argvars[1]);
2536 if (argvars[2].v_type != VAR_UNKNOWN)
2537 prepend = tv_get_string_buf(&argvars[2], buf);
2538 }
2539
2540 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2541# endif
2542}
2543
2544#endif // FEAT_EVAL