blob: 86bfbd81b8208fd36464dfee4aad6338c4413d70 [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;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +0100543 size_t len;
Mike Williams51024bb2024-05-30 07:46:30 +0200544 size_t usedlen = 0;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200545 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
John Marriottfff01322025-06-18 18:15:31 +0200551 len = expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200552 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200553 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200554 (char_u **)&fname, &fbuf, &len);
555 if (fname == NULL)
556 goto add_err;
557 fname = (char *)vim_strnsave((char_u *)fname, len);
558 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200559
Bram Moolenaar8767f522016-07-01 17:17:39 +0200560 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (ret < 0)
562 {
563staterr:
564 if (p_csverbose)
565 cs_stat_emsg(fname);
566 goto add_err;
567 }
568
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100569 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (arg2 != NULL)
571 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200572 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200574 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 goto add_err;
576
577 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200578 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (ret < 0)
580 goto staterr;
581 }
582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100583 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200584 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200586 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (fname2 == NULL)
588 goto add_err;
589
590 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100591#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || fname[strlen(fname)-1] == '\\'
593#endif
594 )
595 {
596 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200597 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 break;
599 }
600 if (fname[0] == '\0')
601 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
602 else
603 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
604
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (ret < 0)
607 {
608 if (p_csverbose)
609 cs_stat_emsg(fname2);
610 goto add_err;
611 }
612
613 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
618 }
619 else
620 {
621 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000622 (void)semsg(_(e_str_is_not_directory_or_valid_cscope_database),
623 fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 goto add_err;
625 }
626
627 if (i != -1)
628 {
629 if (cs_create_connection(i) == CSCOPE_FAILURE
630 || cs_read_prompt(i) == CSCOPE_FAILURE)
631 {
632 cs_release_csp(i, TRUE);
633 goto add_err;
634 }
635
636 if (p_csverbose)
637 {
638 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100639 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100640 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 csinfo[i].fname);
642 }
643 }
644
645 vim_free(fname);
646 vim_free(fname2);
647 vim_free(ppath);
648 return CSCOPE_SUCCESS;
649
650add_err:
651 vim_free(fname2);
652 vim_free(fname);
653 vim_free(ppath);
654 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100655}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657
658 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100659cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100662}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664
665 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100666cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000668 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100669}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671
672/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100673 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 */
675 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100676cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +0000678 int i;
679 int cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000681 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 if (csinfo[i].fname != NULL)
683 cnt++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100685}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100688cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100689 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000691 semsg(_(e_error_reading_cscope_connection_nr), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693
694#define CSREAD_BUFSIZE 2048
695/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100696 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100699cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 char *stok;
702 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100703 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200705 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (buf == NULL)
707 return 0;
708 for (;;)
709 {
710 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
711 {
712 if (feof(csinfo[idx].fr_fp))
713 errno = EIO;
714
715 cs_reading_emsg(idx);
716
717 vim_free(buf);
718 return -1;
719 }
720
721 /*
722 * If the database is out of date, or there's some other problem,
723 * cscope will output error messages before the number-of-lines output.
724 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000725 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100726 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100728 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if ((stok = strtok(buf, (const char *)" ")) == NULL)
731 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000732 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 continue;
734
735 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
736 continue;
737 nlines = atoi(stok);
738 if (nlines < 0)
739 {
740 nlines = 0;
741 break;
742 }
743
744 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
745 continue;
746 if (strncmp((const char *)stok, "lines", 5))
747 continue;
748
749 break;
750 }
751
752 vim_free(buf);
753 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100754}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756
757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * Creates the actual cscope command query from what the user entered.
759 */
760 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100761cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 char *cmd;
764 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000765 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
767 switch (csoption[0])
768 {
769 case '0' : case 's' :
770 search = 0;
771 break;
772 case '1' : case 'g' :
773 search = 1;
774 break;
775 case '2' : case 'd' :
776 search = 2;
777 break;
778 case '3' : case 'c' :
779 search = 3;
780 break;
781 case '4' : case 't' :
782 search = 4;
783 break;
784 case '6' : case 'e' :
785 search = 6;
786 break;
787 case '7' : case 'f' :
788 search = 7;
789 break;
790 case '8' : case 'i' :
791 search = 8;
792 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200793 case '9' : case 'a' :
794 search = 9;
795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 default :
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000797 (void)emsg(_(e_unknown_cscope_search_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 cs_usage_msg(Find);
799 return NULL;
800 }
801
Bram Moolenaardc215522022-09-25 17:03:26 +0100802 // Skip white space before the pattern, except for text and pattern search,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100803 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000804 pat = pattern;
805 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100806 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000807 ++pat;
808
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200809 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return NULL;
811
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000812 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100815}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817
818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 * This piece of code was taken/adapted from nvi. do we need to add
820 * the BSD license notice?
821 */
822 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100823cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000825#ifdef UNIX
826 int to_cs[2], from_cs[2];
827#endif
Bram Moolenaardc215522022-09-25 17:03:26 +0100828 int cmdlen;
Bram Moolenaar02b06312007-09-06 15:39:22 +0000829 int len;
830 char *prog, *cmd, *ppath = NULL;
John Marriottfff01322025-06-18 18:15:31 +0200831 size_t proglen;
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 }
John Marriottfff01322025-06-18 18:15:31 +0200919 proglen = 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
John Marriottfff01322025-06-18 18:15:31 +0200922 cmdlen = (int)(proglen + 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 }
John Marriottfff01322025-06-18 18:15:31 +0200936 cmdlen += (int)expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938
939 if (csinfo[i].flags)
Bram Moolenaardc215522022-09-25 17:03:26 +0100940 cmdlen += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
Bram Moolenaardc215522022-09-25 17:03:26 +0100942 if ((cmd = alloc(cmdlen)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
944 vim_free(prog);
945 vim_free(ppath);
946#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000947 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100949 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 goto err_closing;
951#endif
952 }
953
ichizok40503052022-01-13 18:09:11 +0000954 // run the cscope command
Bram Moolenaardc215522022-09-25 17:03:26 +0100955#ifdef UNIX
Bram Moolenaar66b8d2a2022-10-13 16:34:30 +0100956 vim_snprintf(cmd, cmdlen, "/bin/sh -c \"exec %s -dl -f %s",
Bram Moolenaardc215522022-09-25 17:03:26 +0100957 prog, csinfo[i].fname);
958#else
959 vim_snprintf(cmd, cmdlen, "%s -dl -f %s", prog, csinfo[i].fname);
960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (csinfo[i].ppath != NULL)
962 {
Bram Moolenaar3292a222022-10-01 20:17:17 +0100963 len = (int)STRLEN(cmd);
Bram Moolenaardc215522022-09-25 17:03:26 +0100964 vim_snprintf(cmd + len, cmdlen - len, " -P%s", csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 if (csinfo[i].flags != NULL)
967 {
Bram Moolenaar3292a222022-10-01 20:17:17 +0100968 len = (int)STRLEN(cmd);
Bram Moolenaardc215522022-09-25 17:03:26 +0100969 vim_snprintf(cmd + len, cmdlen - len, " %s", csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971# ifdef UNIX
Bram Moolenaar66b8d2a2022-10-13 16:34:30 +0100972 // terminate the -c command argument
973 STRCAT(cmd, "\"");
974
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 vim_free(prog);
977# endif
978 vim_free(ppath);
979
980#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200981# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100982 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200983# if defined(HAVE_SETSID)
984 (void)setsid();
985# else
986 if (setpgid(0, 0) == -1)
987 PERROR(_("cs_create_connection setpgid failed"));
988# endif
989# endif
ichizok40503052022-01-13 18:09:11 +0000990 if (build_argv_from_string((char_u *)cmd, &argv, &argc) == FAIL)
991 exit(EXIT_FAILURE);
992
993 if (execvp(argv[0], argv) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 PERROR(_("cs_create_connection exec failed"));
995
996 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100997 // NOTREACHED
ichizok40503052022-01-13 18:09:11 +0000998 }
999 else // parent.
1000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1004 */
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1009
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001010 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 (void)close(to_cs[0]);
1012 (void)close(from_cs[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001015 // MSWIN
1016 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +00001017 GetStartupInfo(&si);
1018 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001019 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +00001020 si.hStdOutput = stdout_wr;
1021 si.hStdError = stdout_wr;
1022 si.hStdInput = stdin_rd;
1023 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1024 NULL, NULL, &si, &pi);
1025 vim_free(prog);
1026 vim_free(cmd);
1027
1028 if (!created)
1029 {
1030 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001031 (void)emsg(_(e_could_not_spawn_cscope_process));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001032 goto err_closing;
1033 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001035 csinfo[i].pid = pi.dwProcessId;
1036 csinfo[i].hProc = pi.hProcess;
1037 CloseHandle(pi.hThread);
1038
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001039 // TODO - tidy up after failure to create files on pipe handles.
K.Takatac351dc12022-01-24 11:24:08 +00001040 if (((fd = _open_osfhandle((intptr_t)stdin_wr,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001041 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001042 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1043 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
K.Takatac351dc12022-01-24 11:24:08 +00001044 if (((fd = _open_osfhandle((intptr_t)stdout_rd,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001045 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001046 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1047 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1048
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001049 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001050 CloseHandle(stdin_rd);
1051 CloseHandle(stdout_wr);
1052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001053#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058
1059/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001060 * Query cscope using command line interface. Parse the output and use tselect
1061 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 *
1063 * returns TRUE if we jump to a tag or abort, FALSE if not.
1064 */
1065 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001066cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001069 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
1071 if (cs_check_for_connections() == FALSE)
1072 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001073 (void)emsg(_(e_no_cscope_connections));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 return FALSE;
1075 }
1076
1077 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1078 {
1079 cs_usage_msg(Find);
1080 return FALSE;
1081 }
1082
1083 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001084 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 cs_usage_msg(Find);
1087 return FALSE;
1088 }
1089
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001090 /*
1091 * Let's replace the NULs written by strtok() with spaces - we need the
1092 * spaces to correctly display the quickfix/location list window's title.
1093 */
1094 for (i = 0; i < eap_arg_len; ++i)
1095 if (NUL == eap->arg[i])
1096 eap->arg[i] = ' ';
1097
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001098 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001099 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102
1103/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001104 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
1106 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001107cs_find_common(
1108 char *opt,
1109 char *pat,
1110 int forceit,
1111 int verbose,
1112 int use_ll UNUSED,
1113 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int i;
1116 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001117 int *nummatches;
1118 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#ifdef FEAT_QUICKFIX
1120 char cmdletter;
1121 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001122
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001123 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001124 switch (opt[0])
1125 {
1126 case '0' :
1127 cmdletter = 's';
1128 break;
1129 case '1' :
1130 cmdletter = 'g';
1131 break;
1132 case '2' :
1133 cmdletter = 'd';
1134 break;
1135 case '3' :
1136 cmdletter = 'c';
1137 break;
1138 case '4' :
1139 cmdletter = 't';
1140 break;
1141 case '6' :
1142 cmdletter = 'e';
1143 break;
1144 case '7' :
1145 cmdletter = 'f';
1146 break;
1147 case '8' :
1148 cmdletter = 'i';
1149 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001150 case '9' :
1151 cmdletter = 'a';
1152 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001153 default :
1154 cmdletter = opt[0];
1155 }
1156
1157 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1158 if (qfpos != NULL)
1159 {
1160 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001161 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001162 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1163 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001164 (void)semsg(_(e_invalid_cscopequickfix_flag_chr_for_chr),
1165 *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001166 return FALSE;
1167 }
1168
Bram Moolenaar21662be2016-11-06 14:46:44 +01001169 if (*qfpos != '0'
1170 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1171 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001172 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001173# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001174 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001175 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001176# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001177 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001181 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 cmd = cs_create_cmd(opt, pat);
1183 if (cmd == NULL)
1184 return FALSE;
1185
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001186 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001187 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001188 {
1189 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001190 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001191 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001192
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001193 // Send query to all open connections, then count the total number
1194 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001195 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 nummatches[i] = 0;
1197 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001198 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001200 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 continue;
1202
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001203 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1205 (void)fflush(csinfo[i].to_fp);
1206
1207 nummatches[i] = cs_cnt_matches(i);
1208
1209 if (nummatches[i] > -1)
1210 totmatches += nummatches[i];
1211
1212 if (nummatches[i] == 0)
1213 (void)cs_read_prompt(i);
1214 }
1215 vim_free(cmd);
1216
1217 if (totmatches == 0)
1218 {
James McCoy3c5904d2021-10-24 14:50:07 +01001219 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001220 (void)semsg(_(e_no_matches_found_for_cscope_query_str_of_str),
1221 opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001222 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 return FALSE;
1224 }
1225
1226#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001227 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001229 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001230 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001231 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001232 qf_info_T *qi = NULL;
1233 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001235 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001237 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001240 cs_file_results(f, nummatches);
1241 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001242 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001243 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001244 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001245 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001246 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001248 if (postponed_split != 0)
1249 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001250 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001252 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001253 postponed_split = 0;
1254 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001255
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001256 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1257 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001258 if (use_ll)
1259 /*
1260 * In the location list window, use the displayed location
1261 * list. Otherwise, use the location list for the window.
1262 */
1263 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1264 ? wp->w_llist_ref : wp->w_llist;
1265 qf_jump(qi, 0, 0, forceit);
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 mch_remove(tmp);
1269 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001270 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return TRUE;
1272 }
1273 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001274#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001276 char **matches = NULL, **contexts = NULL;
1277 int matched = 0;
1278
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001279 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001280 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001282 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (matches == NULL)
1284 return FALSE;
1285
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001286 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1289 }
1290
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001294 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001297cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 cscmd_T *cmdp = cs_cmds;
1300
Bram Moolenaar32526b32019-01-19 17:43:09 +01001301 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 while (cmdp->name != NULL)
1303 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001304 char *help = _(cmdp->help);
1305 int space_cnt = 30 - vim_strsize((char_u *)help);
1306
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001307 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001308 if (space_cnt < 0)
1309 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001311 cmdp->name,
1312 help, space_cnt, " ",
1313 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001315 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001316 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001324 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 cmdp++;
1327 }
1328
1329 wait_return(TRUE);
1330 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001335clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 csinfo[i].fname = NULL;
1338 csinfo[i].ppath = NULL;
1339 csinfo[i].flags = NULL;
1340#if defined(UNIX)
1341 csinfo[i].st_dev = (dev_t)0;
1342 csinfo[i].st_ino = (ino_t)0;
1343#else
1344 csinfo[i].nVolume = 0;
1345 csinfo[i].nIndexHigh = 0;
1346 csinfo[i].nIndexLow = 0;
1347#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001348 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 csinfo[i].fr_fp = NULL;
1350 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001351#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001352 csinfo[i].hProc = NULL;
1353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001357 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001360cs_insert_filelist(
1361 char *fname,
1362 char *ppath,
1363 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001364 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001366 int i;
1367 int j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 BY_HANDLE_FILE_INFORMATION bhfi;
1370
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001371 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001373 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1374 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (p_csverbose)
1376 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001377 char *cant_msg = _(e_cannot_open_cscope_database_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char *winmsg = GetWin32Error();
1379
1380 if (winmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001381 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001383 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001384 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001387
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001388 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (p_csverbose)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001390 (void)emsg(_(e_cannot_get_cscope_database_information));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393#endif
1394
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001395 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001396 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
1398 if (csinfo[j].fname != NULL
1399#if defined(UNIX)
1400 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1401#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001402 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001403 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001404 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1405 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001406 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1408 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1409#endif
1410 )
1411 {
1412 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001413 (void)emsg(_(e_duplicate_cscope_database_not_added));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return -1;
1415 }
1416
1417 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420
1421 if (i == -1)
1422 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001423 i = csinfo_size;
1424 if (csinfo_size == 0)
1425 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001426 // First time allocation: allocate only 1 connection. It should
1427 // be enough for most users. If more is needed, csinfo will be
1428 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001429 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001430 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001431 }
1432 else
1433 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001434 csinfo_T *t_csinfo = csinfo;
1435
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001436 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001437 csinfo_size *= 2;
1438 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001439 if (csinfo == NULL)
1440 {
1441 vim_free(t_csinfo);
1442 csinfo_size = 0;
1443 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001444 }
1445 if (csinfo == NULL)
1446 return -1;
1447 for (j = csinfo_size/2; j < csinfo_size; j++)
1448 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001451 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 return -1;
1453
1454 (void)strcpy(csinfo[i].fname, (const char *)fname);
1455
1456 if (ppath != NULL)
1457 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001458 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001460 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 return -1;
1462 }
1463 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001464 }
1465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 csinfo[i].ppath = NULL;
1467
1468 if (flags != NULL)
1469 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001470 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001472 VIM_CLEAR(csinfo[i].fname);
1473 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return -1;
1475 }
1476 (void)strcpy(csinfo[i].flags, (const char *)flags);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001477 }
1478 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 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
RestorerZ68ebcee2023-05-31 17:12:14 +01001724 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int maxlen;
2116 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int n;
2119
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002120 // compute maximum allowed len for Cscope error message
RestorerZ68ebcee2023-05-31 17:12:14 +01002121 char *cs_emsg = _(e_cscope_error_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2123
2124 for (;;)
2125 {
2126 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002127 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2129 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002130 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002131 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (buf != NULL)
2133 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002134 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 buf[bufpos++] = ch;
2136 buf[bufpos] = NUL;
2137 if (bufpos >= epromptlen
2138 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2139 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002140 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 buf[bufpos - epromptlen] = NUL;
2142
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002143 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002144 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002146 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 (void)putc('\n', csinfo[i].to_fp);
2148 (void)fflush(csinfo[i].to_fp);
2149
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002150 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 bufpos = 0;
2152 buf[bufpos] = NUL;
2153 }
2154 }
2155 }
2156
2157 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2158 {
2159 if (n > 0)
2160 ch = getc(csinfo[i].fr_fp);
2161 if (ch == EOF)
2162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002166 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 cs_release_csp(i, TRUE);
2168 vim_free(buf);
2169 return CSCOPE_FAILURE;
2170 }
2171
2172 if (ch != CSCOPE_PROMPT[n])
2173 {
2174 ch = EOF;
2175 break;
2176 }
2177 }
2178
2179 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002180 continue; // didn't find the prompt
2181 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183
2184 vim_free(buf);
2185 return CSCOPE_SUCCESS;
2186}
2187
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002188#if defined(UNIX) && defined(SIGALRM)
2189/*
2190 * Used to catch and ignore SIGALRM below.
2191 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01002192 static void
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002193sig_handler SIGDEFARG(sigarg)
2194{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002195 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002196}
2197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002200 * Does the actual free'ing for the cs ptr with an optional flag of whether
2201 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
2203 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002204cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /*
2207 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2208 */
2209 if (csinfo[i].to_fp != NULL)
2210 {
2211 (void)fputs("q\n", csinfo[i].to_fp);
2212 (void)fflush(csinfo[i].to_fp);
2213 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002214#if defined(UNIX)
2215 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002216 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002217 int pstat;
2218 pid_t pid;
2219
2220# if defined(HAVE_SIGACTION)
2221 struct sigaction sa, old;
2222
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002223 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002224 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002225 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002226# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002227 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002228# else
2229 sa.sa_flags = 0;
2230# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002231 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002232 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002233
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002234 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002235 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002236 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002237
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002238 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002239 alarm(0);
2240 sigaction(SIGALRM, &old, NULL);
2241# else
2242 int waited;
2243
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002244 // Can't use sigaction(), loop for two seconds. First yield the CPU
2245 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002246 sleep(0);
2247 for (waited = 0; waited < 40; ++waited)
2248 {
2249 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002250 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002251 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002252 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002253 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002254 }
2255# endif
2256 /*
2257 * If the cscope process is still running: kill it.
2258 * Safety check: If the PID would be zero here, the entire X session
2259 * would be killed. -1 and 1 are dangerous as well.
2260 */
2261 if (pid < 0 && csinfo[i].pid > 1)
2262 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002263# ifdef ECHILD
2264 int alive = TRUE;
2265
2266 if (waitpid_errno == ECHILD)
2267 {
2268 /*
2269 * When using 'vim -g', vim is forked and cscope process is
2270 * no longer a child process but a sibling. So waitpid()
2271 * fails with errno being ECHILD (No child processes).
2272 * Don't send SIGKILL to cscope immediately but wait
2273 * (polling) for it to exit normally as result of sending
2274 * the "q" command, hence giving it a chance to clean up
2275 * its temporary files.
2276 */
2277 int waited;
2278
2279 sleep(0);
2280 for (waited = 0; waited < 40; ++waited)
2281 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002282 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002283 if (kill(csinfo[i].pid, 0) != 0)
2284 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002285 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002286 break;
2287 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002288 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002289 }
2290 }
2291 if (alive)
2292# endif
2293 {
2294 kill(csinfo[i].pid, SIGKILL);
2295 (void)waitpid(csinfo[i].pid, &pstat, 0);
2296 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002297 }
2298 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002299#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002300 if (csinfo[i].hProc != NULL)
2301 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002302 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002303 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2304 TerminateProcess(csinfo[i].hProc, 0);
2305 CloseHandle(csinfo[i].hProc);
2306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309 if (csinfo[i].fr_fp != NULL)
2310 (void)fclose(csinfo[i].fr_fp);
2311 if (csinfo[i].to_fp != NULL)
2312 (void)fclose(csinfo[i].to_fp);
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (freefnpp)
2315 {
2316 vim_free(csinfo[i].fname);
2317 vim_free(csinfo[i].ppath);
2318 vim_free(csinfo[i].flags);
2319 }
2320
2321 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324
2325/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002326 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002329cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2332 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002333 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002335 if (csinfo_size == 0)
2336 return CSCOPE_SUCCESS;
2337
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002338 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002339 dblist = ALLOC_MULT(char *, csinfo_size);
2340 pplist = ALLOC_MULT(char *, csinfo_size);
2341 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (dblist == NULL || pplist == NULL || fllist == NULL)
2343 {
2344 vim_free(dblist);
2345 vim_free(pplist);
2346 vim_free(fllist);
2347 return CSCOPE_FAILURE;
2348 }
2349
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002350 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 dblist[i] = csinfo[i].fname;
2353 pplist[i] = csinfo[i].ppath;
2354 fllist[i] = csinfo[i].flags;
2355 if (csinfo[i].fname != NULL)
2356 cs_release_csp(i, FALSE);
2357 }
2358
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002359 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002360 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 if (dblist[i] != NULL)
2363 {
2364 cs_add_common(dblist[i], pplist[i], fllist[i]);
2365 if (p_csverbose)
2366 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002367 // don't use smsg_attr() because we want to display the
2368 // connection number in the same line as
2369 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002371 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 }
2374 vim_free(dblist[i]);
2375 vim_free(pplist[i]);
2376 vim_free(fllist[i]);
2377 }
2378 vim_free(dblist);
2379 vim_free(pplist);
2380 vim_free(fllist);
2381
2382 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002383 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387
2388/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002389 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 * (Prepends ppath, if there is one and if it's not already prepended,
2391 * otherwise just uses the name found.)
2392 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002393 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002395 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002398cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002400 char *fullname;
2401 int len;
2402 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002405 * Ppath is freed when we destroy the cscope connection.
2406 * Fullname is freed after cs_make_vim_style_matches, after it's been
2407 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002409 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002412 else if (p_csre && csinfo[i].fname != NULL)
2413 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002414 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2415 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002416 csdir = alloc(MAXPATHL);
2417 if (csdir != NULL)
2418 {
2419 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002420 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002421 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002422 len += (int)STRLEN(csdir);
2423 }
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002426 // Note/example: this won't work if the cscope output already starts
2427 // "../.." and the prefix path is also "../..". if something like this
2428 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002429 if (csinfo[i].ppath != NULL
2430 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2431 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002432#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002433 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002435 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002436 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002437 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002438 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2439 }
2440 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002441 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002442 // Check for csdir to be non empty to avoid empty path concatenated to
2443 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002444 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002447 {
2448 fullname = (char *)vim_strsave((char_u *)name);
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002451 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455
2456/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002457 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002460cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00002462 int i;
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002465 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 else
2467 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002468 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002470 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002471 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 if (csinfo[i].fname == NULL)
2474 continue;
2475
2476 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002477 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002480 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 i, (long)csinfo[i].pid, csinfo[i].fname);
2482 }
2483 }
2484
Bram Moolenaar13608d82022-08-29 15:06:50 +01002485 wait_return(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar02b06312007-09-06 15:39:22 +00002489
2490/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002491 * Only called when VIM exits to quit any cscope sessions.
2492 */
2493 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002494cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002495{
2496 int i;
2497
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002498 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002499 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002500 vim_free(csinfo);
2501 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002502}
2503
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002504#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002506#if defined(FEAT_EVAL) || defined(PROTO)
2507
2508/*
2509 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2510 *
2511 * Checks the existence of a cscope connection.
2512 */
2513 void
2514f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2515{
2516# ifdef FEAT_CSCOPE
2517 int num = 0;
2518 char_u *dbpath = NULL;
2519 char_u *prepend = NULL;
2520 char_u buf[NUMBUFLEN];
2521
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002522 if (in_vim9script()
2523 && (check_for_opt_number_arg(argvars, 0) == FAIL
2524 || (argvars[0].v_type != VAR_UNKNOWN
2525 && (check_for_opt_string_arg(argvars, 1) == FAIL
2526 || (argvars[1].v_type != VAR_UNKNOWN
2527 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2528 return;
2529
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002530 if (argvars[0].v_type != VAR_UNKNOWN
2531 && argvars[1].v_type != VAR_UNKNOWN)
2532 {
2533 num = (int)tv_get_number(&argvars[0]);
2534 dbpath = tv_get_string(&argvars[1]);
2535 if (argvars[2].v_type != VAR_UNKNOWN)
2536 prepend = tv_get_string_buf(&argvars[2], buf);
2537 }
2538
2539 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2540# endif
2541}
2542
2543#endif // FEAT_EVAL