blob: edcd4ca684a2871d4b8d7548a531b39d680d532f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005 *
6 * The basic idea/structure of cscope for Vim was borrowed from Nvi. There
7 * might be a few lines of code that look similar to what Nvi has.
8 *
9 * See README.txt for an overview of the Vim source code.
10 */
11
12#include "vim.h"
13
14#if defined(FEAT_CSCOPE) || defined(PROTO)
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include <sys/types.h>
17#include <sys/stat.h>
18#if defined(UNIX)
19# include <sys/wait.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000020#endif
ichizok40503052022-01-13 18:09:11 +000021
22#if defined (MSWIN)
23# ifndef WIN32_LEAN_AND_MEAN
24# define WIN32_LEAN_AND_MEAN
25# endif
26# include <windows.h>
27#endif
28
29#define CSCOPE_SUCCESS 0
30#define CSCOPE_FAILURE -1
31
32#define CSCOPE_DBFILE "cscope.out"
33#define CSCOPE_PROMPT ">> "
34
35/*
36 * See ":help cscope-find" for the possible queries.
37 */
38
39typedef struct {
40 char * name;
41 int (*func)(exarg_T *eap);
42 char * help;
43 char * usage;
44 int cansplit; // if supports splitting window
45} cscmd_T;
46
47typedef struct csi {
48 char * fname; // cscope db name
49 char * ppath; // path to prepend (the -P option)
50 char * flags; // additional cscope flags/options (e.g, -p2)
51#if defined(UNIX)
52 pid_t pid; // PID of the connected cscope process.
53 dev_t st_dev; // ID of dev containing cscope db
54 ino_t st_ino; // inode number of cscope db
55#else
56# if defined(MSWIN)
57 DWORD pid; // PID of the connected cscope process.
58 HANDLE hProc; // cscope process handle
59 DWORD nVolume; // Volume serial number, instead of st_dev
60 DWORD nIndexHigh; // st_ino has no meaning in the Windows
61 DWORD nIndexLow;
62# endif
63#endif
64
65 FILE * fr_fp; // from cscope: FILE.
66 FILE * to_fp; // to cscope: FILE.
67} csinfo_T;
68
69typedef enum { Add, Find, Help, Kill, Reset, Show } csid_e;
70
71typedef enum {
72 Store,
73 Get,
74 Free,
75 Print
76} mcmd_e;
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static int cs_add(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static int cs_add_common(char *, char *, char *);
80static int cs_check_for_connections(void);
81static int cs_check_for_tags(void);
82static int cs_cnt_connections(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010083static int cs_create_connection(int i);
Bram Moolenaarc716c302006-01-21 22:12:51 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010085static void cs_file_results(FILE *, int *);
Bram Moolenaarc716c302006-01-21 22:12:51 +000086#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010087static void cs_fill_results(char *, int , int *, char ***,
88 char ***, int *);
89static int cs_find(exarg_T *eap);
90static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline);
91static int cs_help(exarg_T *eap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static int cs_insert_filelist(char *, char *, char *,
Bram Moolenaar8767f522016-07-01 17:17:39 +020093 stat_T *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static int cs_kill(exarg_T *eap);
95static void cs_kill_execute(int, char *);
96static cscmd_T * cs_lookup_cmd(exarg_T *eap);
97static char * cs_make_vim_style_matches(char *, char *,
98 char *, char *);
99static char * cs_manage_matches(char **, char **, int, mcmd_e);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void cs_print_tags_priv(char **, char **, int);
101static int cs_read_prompt(int);
102static void cs_release_csp(int, int freefnpp);
103static int cs_reset(exarg_T *eap);
104static char * cs_resolve_file(int, char *);
105static int cs_show(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000108static csinfo_T * csinfo = NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100109static int csinfo_size = 0; // number of items allocated in
110 // csinfo[]
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000111
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100112static int eap_arg_len; // length of eap->arg, set in
113 // cs_lookup_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static cscmd_T cs_cmds[] =
115{
116 { "add", cs_add,
117 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
118 { "find", cs_find,
Bram Moolenaar80632db2016-07-05 22:28:40 +0200119 N_("Query for a pattern"), "find a|c|d|e|f|g|i|s|t name", 1 },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 { "help", cs_help,
121 N_("Show this message"), "help", 0 },
122 { "kill", cs_kill,
123 N_("Kill a connection"), "kill #", 0 },
124 { "reset", cs_reset,
125 N_("Reinit all connections"), "reset", 0 },
126 { "show", cs_show,
127 N_("Show connections"), "show", 0 },
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000128 { NULL, NULL, NULL, NULL, 0 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129};
130
131 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100132cs_usage_msg(csid_e x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000134 (void)semsg(_(e_usage_cscope_str), cs_cmds[(int)x].usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135}
136
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000137static enum
138{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100139 EXP_CSCOPE_SUBCMD, // expand ":cscope" sub-commands
140 EXP_SCSCOPE_SUBCMD, // expand ":scscope" sub-commands
141 EXP_CSCOPE_FIND, // expand ":cscope find" arguments
142 EXP_CSCOPE_KILL // expand ":cscope kill" arguments
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000143} expand_what;
144
145/*
146 * Function given to ExpandGeneric() to obtain the cscope command
147 * expansion.
148 */
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000149 char_u *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100150get_cscope_name(expand_T *xp UNUSED, int idx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000151{
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000152 int current_idx;
153 int i;
154
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000155 switch (expand_what)
156 {
157 case EXP_CSCOPE_SUBCMD:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100158 // Complete with sub-commands of ":cscope":
159 // add, find, help, kill, reset, show
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000160 return (char_u *)cs_cmds[idx].name;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000161 case EXP_SCSCOPE_SUBCMD:
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100162 // Complete with sub-commands of ":scscope": same sub-commands as
163 // ":cscope" but skip commands which don't support split windows
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000164 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
165 if (cs_cmds[i].cansplit)
166 if (current_idx++ == idx)
167 break;
168 return (char_u *)cs_cmds[i].name;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000169 case EXP_CSCOPE_FIND:
170 {
171 const char *query_type[] =
172 {
Bram Moolenaar80632db2016-07-05 22:28:40 +0200173 "a", "c", "d", "e", "f", "g", "i", "s", "t", NULL
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000174 };
175
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100176 // Complete with query type of ":cscope find {query_type}".
177 // {query_type} can be letters (c, d, ... a) or numbers (0, 1,
178 // ..., 9) but only complete with letters, since numbers are
179 // redundant.
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000180 return (char_u *)query_type[idx];
181 }
182 case EXP_CSCOPE_KILL:
183 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000184 static char connection[5];
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000185
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100186 // ":cscope kill" accepts connection numbers or partial names of
187 // the pathname of the cscope database as argument. Only complete
188 // with connection numbers. -1 can also be used to kill all
189 // connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000190 for (i = 0, current_idx = 0; i < csinfo_size; i++)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000191 {
192 if (csinfo[i].fname == NULL)
193 continue;
194 if (current_idx++ == idx)
195 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000196 vim_snprintf(connection, sizeof(connection), "%d", i);
197 return (char_u *)connection;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000198 }
199 }
200 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
201 }
202 default:
203 return NULL;
204 }
205}
206
207/*
208 * Handle command line completion for :cscope command.
209 */
210 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100211set_context_in_cscope_cmd(
212 expand_T *xp,
213 char_u *arg,
214 cmdidx_T cmdidx)
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000215{
216 char_u *p;
217
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218 // Default: expand subcommands
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000219 xp->xp_context = EXPAND_CSCOPE;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000220 xp->xp_pattern = arg;
Bram Moolenaar7bfef802009-04-22 14:25:01 +0000221 expand_what = (cmdidx == CMD_scscope)
222 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000223
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100224 // (part of) subcommand already typed
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000225 if (*arg != NUL)
226 {
227 p = skiptowhite(arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100228 if (*p != NUL) // past first word
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000229 {
230 xp->xp_pattern = skipwhite(p);
231 if (*skiptowhite(xp->xp_pattern) != NUL)
232 xp->xp_context = EXPAND_NOTHING;
233 else if (STRNICMP(arg, "add", p - arg) == 0)
234 xp->xp_context = EXPAND_FILES;
235 else if (STRNICMP(arg, "kill", p - arg) == 0)
236 expand_what = EXP_CSCOPE_KILL;
237 else if (STRNICMP(arg, "find", p - arg) == 0)
238 expand_what = EXP_CSCOPE_FIND;
239 else
240 xp->xp_context = EXPAND_NOTHING;
241 }
242 }
243}
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245/*
Bram Moolenaarf4580d82009-03-18 11:52:53 +0000246 * Find the command, print help if invalid, and then call the corresponding
247 * command function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100250do_cscope_general(
251 exarg_T *eap,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100252 int make_split UNUSED) // whether to split window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 cscmd_T *cmdp;
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
257 {
258 cs_help(eap);
259 return;
260 }
261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (make_split)
263 {
264 if (!cmdp->cansplit)
265 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100266 (void)msg_puts(_("This cscope command does not support splitting the window.\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 return;
268 }
269 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +0200270 postponed_split_flags = cmdmod.cmod_split;
271 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274 cmdp->func(eap);
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +0000277 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278}
279
280/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100281 * Implementation of ":cscope" and ":lcscope"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 */
283 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100284ex_cscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
286 do_cscope_general(eap, FALSE);
287}
288
289/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100290 * Implementation of ":scscope". Same as ex_cscope(), but splits window, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 */
292 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100293ex_scscope(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294{
295 do_cscope_general(eap, TRUE);
296}
297
298/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100299 * Implementation of ":cstag"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 */
301 void
Bram Moolenaard4db7712016-11-12 19:16:46 +0100302ex_cstag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303{
304 int ret = FALSE;
305
Bram Moolenaar446cb832008-06-24 21:56:24 +0000306 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000308 (void)emsg(_(e_usage_cstag_ident));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 return;
310 }
311
312 switch (p_csto)
313 {
314 case 0 :
315 if (cs_check_for_connections())
316 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000317 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200318 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 if (ret == FALSE)
320 {
321 cs_free_tags();
322 if (msg_col)
323 msg_putchar('\n');
324
325 if (cs_check_for_tags())
326 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
327 }
328 }
329 else if (cs_check_for_tags())
330 {
331 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
332 }
333 break;
334 case 1 :
335 if (cs_check_for_tags())
336 {
337 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
338 if (ret == FALSE)
339 {
340 if (msg_col)
341 msg_putchar('\n');
342
343 if (cs_check_for_connections())
344 {
345 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200346 FALSE, FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (ret == FALSE)
348 cs_free_tags();
349 }
350 }
351 }
352 else if (cs_check_for_connections())
353 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000354 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200355 FALSE, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 if (ret == FALSE)
357 cs_free_tags();
358 }
359 break;
360 default :
361 break;
362 }
363
364 if (!ret)
365 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000366 (void)emsg(_(e_cstag_tag_not_founc));
Bram Moolenaar4033c552017-09-16 20:54:51 +0200367#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 g_do_tagpreview = 0;
369#endif
370 }
371
Bram Moolenaard4db7712016-11-12 19:16:46 +0100372}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374
375/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100376 * This simulates a vim_fgets(), but for cscope, returns the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * from the cscope output. should only be called from find_tags()
378 *
379 * returns TRUE if eof, FALSE otherwise
380 */
381 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100382cs_fgets(char_u *buf, int size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383{
384 char *p;
385
386 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
387 return TRUE;
Bram Moolenaard2ac9842007-08-21 16:03:51 +0000388 vim_strncpy(buf, (char_u *)p, size - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
390 return FALSE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100391}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
393
394/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100395 * Called only from do_tag(), when popping the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
397 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100398cs_free_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
400 cs_manage_matches(NULL, NULL, -1, Free);
401}
402
403
404/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100405 * Called from do_tag().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 */
407 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100408cs_print_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
410 cs_manage_matches(NULL, NULL, -1, Print);
411}
412
413
414/*
415 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
416 *
417 * Checks for the existence of a |cscope| connection. If no
418 * parameters are specified, then the function returns:
419 *
420 * 0, if cscope was not available (not compiled in), or if there
421 * are no cscope connections; or
422 * 1, if there is at least one cscope connection.
423 *
424 * If parameters are specified, then the value of {num}
425 * determines how existence of a cscope connection is checked:
426 *
427 * {num} Description of existence check
428 * ----- ------------------------------
429 * 0 Same as no parameters (e.g., "cscope_connection()").
430 * 1 Ignore {prepend}, and use partial string matches for
431 * {dbpath}.
432 * 2 Ignore {prepend}, and use exact string matches for
433 * {dbpath}.
434 * 3 Use {prepend}, use partial string matches for both
435 * {dbpath} and {prepend}.
436 * 4 Use {prepend}, use exact string matches for both
437 * {dbpath} and {prepend}.
438 *
439 * Note: All string comparisons are case sensitive!
440 */
441#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200442 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100443cs_connection(int num, char_u *dbpath, char_u *ppath)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
445 int i;
446
447 if (num < 0 || num > 4 || (num > 0 && !dbpath))
448 return FALSE;
449
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000450 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {
452 if (!csinfo[i].fname)
453 continue;
454
455 if (num == 0)
456 return TRUE;
457
458 switch (num)
459 {
460 case 1:
461 if (strstr(csinfo[i].fname, (char *)dbpath))
462 return TRUE;
463 break;
464 case 2:
465 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
466 return TRUE;
467 break;
468 case 3:
469 if (strstr(csinfo[i].fname, (char *)dbpath)
470 && ((!ppath && !csinfo[i].ppath)
471 || (ppath
472 && csinfo[i].ppath
473 && strstr(csinfo[i].ppath, (char *)ppath))))
474 return TRUE;
475 break;
476 case 4:
477 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
478 && ((!ppath && !csinfo[i].ppath)
479 || (ppath
480 && csinfo[i].ppath
481 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
482 return TRUE;
483 break;
484 }
485 }
486
487 return FALSE;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +0200488}
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#endif
491
492
493/*
494 * PRIVATE functions
495 ****************************************************************************/
496
497/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100498 * Add cscope database or a directory name (to look for cscope.out)
499 * to the cscope connection list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100502cs_add(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 char *fname, *ppath, *flags = NULL;
505
506 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
507 {
508 cs_usage_msg(Add);
509 return CSCOPE_FAILURE;
510 }
511 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
512 flags = strtok((char *)NULL, (const char *)" ");
513
514 return cs_add_common(fname, ppath, flags);
515}
516
517 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100518cs_stat_emsg(char *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
James McCoy3c5904d2021-10-24 14:50:07 +0100520 int err = errno;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000521 (void)semsg(_(e_stat_str_error_nr), fname, err);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
524
525/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100526 * The common routine to add a new cscope connection. Called by
527 * cs_add() and cs_reset(). I really don't like to do this, but this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * routine uses a number of goto statements.
529 */
530 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100531cs_add_common(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100532 char *arg1, // filename - may contain environment variables
533 char *arg2, // prepend path - may contain environment variables
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100534 char *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200536 stat_T statbuf;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000537 int ret;
538 char *fname = NULL;
539 char *fname2 = NULL;
540 char *ppath = NULL;
541 int i;
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200542 int len;
543 int usedlen = 0;
544 char_u *fbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100546 // get the filename (arg1), expand it, and try to stat it
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200547 if ((fname = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 goto add_err;
549
550 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200551 len = (int)STRLEN(fname);
552 fbuf = (char_u *)fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +0200553 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaarcab465a2013-06-12 21:25:23 +0200554 (char_u **)&fname, &fbuf, &len);
555 if (fname == NULL)
556 goto add_err;
557 fname = (char *)vim_strnsave((char_u *)fname, len);
558 vim_free(fbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200559
Bram Moolenaar8767f522016-07-01 17:17:39 +0200560 ret = mch_stat(fname, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (ret < 0)
562 {
563staterr:
564 if (p_csverbose)
565 cs_stat_emsg(fname);
566 goto add_err;
567 }
568
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100569 // get the prepend path (arg2), expand it, and try to stat it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (arg2 != NULL)
571 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200572 stat_T statbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200574 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 goto add_err;
576
577 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200578 ret = mch_stat(ppath, &statbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (ret < 0)
580 goto staterr;
581 }
582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100583 // if filename is a directory, append the cscope database name to it
Bram Moolenaard569bb02018-08-11 13:57:20 +0200584 if (S_ISDIR(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200586 fname2 = alloc(strlen(CSCOPE_DBFILE) + strlen(fname) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (fname2 == NULL)
588 goto add_err;
589
590 while (fname[strlen(fname)-1] == '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +0100591#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || fname[strlen(fname)-1] == '\\'
593#endif
594 )
595 {
596 fname[strlen(fname)-1] = '\0';
Bram Moolenaar64404472010-06-26 06:24:45 +0200597 if (fname[0] == '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 break;
599 }
600 if (fname[0] == '\0')
601 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
602 else
603 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
604
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 ret = mch_stat(fname2, &statbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (ret < 0)
607 {
608 if (p_csverbose)
609 cs_stat_emsg(fname2);
610 goto add_err;
611 }
612
613 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
618 }
619 else
620 {
621 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000622 (void)semsg(_(e_str_is_not_directory_or_valid_cscope_database),
623 fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 goto add_err;
625 }
626
627 if (i != -1)
628 {
629 if (cs_create_connection(i) == CSCOPE_FAILURE
630 || cs_read_prompt(i) == CSCOPE_FAILURE)
631 {
632 cs_release_csp(i, TRUE);
633 goto add_err;
634 }
635
636 if (p_csverbose)
637 {
638 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +0100639 (void)smsg_attr(HL_ATTR(HLF_R),
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100640 _("Added cscope database %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 csinfo[i].fname);
642 }
643 }
644
645 vim_free(fname);
646 vim_free(fname2);
647 vim_free(ppath);
648 return CSCOPE_SUCCESS;
649
650add_err:
651 vim_free(fname2);
652 vim_free(fname);
653 vim_free(ppath);
654 return CSCOPE_FAILURE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100655}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657
658 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100659cs_check_for_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 return (cs_cnt_connections() > 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100662}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664
665 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100666cs_check_for_tags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000668 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100669}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671
672/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100673 * Count the number of cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 */
675 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100676cs_cnt_connections(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 short i;
679 short cnt = 0;
680
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000681 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 if (csinfo[i].fname != NULL)
684 cnt++;
685 }
686 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100687}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100690cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100691 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000693 semsg(_(e_error_reading_cscope_connection_nr), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696#define CSREAD_BUFSIZE 2048
697/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100698 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
700 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100701cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 char *stok;
704 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100705 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200707 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (buf == NULL)
709 return 0;
710 for (;;)
711 {
712 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
713 {
714 if (feof(csinfo[idx].fr_fp))
715 errno = EIO;
716
717 cs_reading_emsg(idx);
718
719 vim_free(buf);
720 return -1;
721 }
722
723 /*
724 * If the database is out of date, or there's some other problem,
725 * cscope will output error messages before the number-of-lines output.
726 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000727 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100728 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100730 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if ((stok = strtok(buf, (const char *)" ")) == NULL)
733 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000734 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 continue;
736
737 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
738 continue;
739 nlines = atoi(stok);
740 if (nlines < 0)
741 {
742 nlines = 0;
743 break;
744 }
745
746 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
747 continue;
748 if (strncmp((const char *)stok, "lines", 5))
749 continue;
750
751 break;
752 }
753
754 vim_free(buf);
755 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100756}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758
759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * Creates the actual cscope command query from what the user entered.
761 */
762 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100763cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char *cmd;
766 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000767 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769 switch (csoption[0])
770 {
771 case '0' : case 's' :
772 search = 0;
773 break;
774 case '1' : case 'g' :
775 search = 1;
776 break;
777 case '2' : case 'd' :
778 search = 2;
779 break;
780 case '3' : case 'c' :
781 search = 3;
782 break;
783 case '4' : case 't' :
784 search = 4;
785 break;
786 case '6' : case 'e' :
787 search = 6;
788 break;
789 case '7' : case 'f' :
790 search = 7;
791 break;
792 case '8' : case 'i' :
793 search = 8;
794 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200795 case '9' : case 'a' :
796 search = 9;
797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 default :
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000799 (void)emsg(_(e_unknown_cscope_search_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 cs_usage_msg(Find);
801 return NULL;
802 }
803
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100804 // Skip white space before the patter, except for text and pattern search,
805 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000806 pat = pattern;
807 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100808 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000809 ++pat;
810
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200811 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 return NULL;
813
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000814 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100817}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819
820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 * This piece of code was taken/adapted from nvi. do we need to add
822 * the BSD license notice?
823 */
824 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100825cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000827#ifdef UNIX
828 int to_cs[2], from_cs[2];
829#endif
830 int len;
831 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100832#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000833 int fd;
834 SECURITY_ATTRIBUTES sa;
835 PROCESS_INFORMATION pi;
836 STARTUPINFO si;
837 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
838 HANDLE stdin_rd, stdout_rd;
839 HANDLE stdout_wr, stdin_wr;
840 BOOL created;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#endif
842
Bram Moolenaar02b06312007-09-06 15:39:22 +0000843#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 /*
845 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
846 * from_cs[0] and writes to to_cs[1].
847 */
848 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
849 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
850 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000851 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852err_closing:
853 if (to_cs[0] != -1)
854 (void)close(to_cs[0]);
855 if (to_cs[1] != -1)
856 (void)close(to_cs[1]);
857 if (from_cs[0] != -1)
858 (void)close(from_cs[0]);
859 if (from_cs[1] != -1)
860 (void)close(from_cs[1]);
861 return CSCOPE_FAILURE;
862 }
863
ichizok40503052022-01-13 18:09:11 +0000864 if ((csinfo[i].pid = fork()) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000866 (void)emsg(_(e_could_not_fork_for_cscope));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 goto err_closing;
ichizok40503052022-01-13 18:09:11 +0000868 }
869 else if (csinfo[i].pid == 0) // child: run cscope.
870 {
871 char **argv = NULL;
872 int argc = 0;
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (dup2(to_cs[0], STDIN_FILENO) == -1)
875 PERROR("cs_create_connection 1");
876 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
877 PERROR("cs_create_connection 2");
878 if (dup2(from_cs[1], STDERR_FILENO) == -1)
879 PERROR("cs_create_connection 3");
880
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100881 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 (void)close(to_cs[1]);
883 (void)close(from_cs[0]);
884#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100885 // MSWIN
886 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000887 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
888 sa.bInheritHandle = TRUE;
889 sa.lpSecurityDescriptor = NULL;
890
891 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
892 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
893 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000894 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000895err_closing:
896 if (pipe_stdin)
897 {
898 CloseHandle(stdin_rd);
899 CloseHandle(stdin_wr);
900 }
901 if (pipe_stdout)
902 {
903 CloseHandle(stdout_rd);
904 CloseHandle(stdout_wr);
905 }
906 return CSCOPE_FAILURE;
907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100909 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200910 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000913 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100915 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 goto err_closing;
917#endif
918 }
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000919 expand_env(p_csprg, (char_u *)prog, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100921 // alloc space to hold the cscope command
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000922 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (csinfo[i].ppath)
924 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100925 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200926 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
928 vim_free(prog);
929#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000930 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100932 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 goto err_closing;
934#endif
935 }
936 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000938 len += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940
941 if (csinfo[i].flags)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000942 len += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200944 if ((cmd = alloc(len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 vim_free(prog);
947 vim_free(ppath);
948#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000949 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100951 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 goto err_closing;
953#endif
954 }
955
ichizok40503052022-01-13 18:09:11 +0000956 // run the cscope command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
ichizok40503052022-01-13 18:09:11 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (csinfo[i].ppath != NULL)
960 {
961 (void)strcat(cmd, " -P");
962 (void)strcat(cmd, csinfo[i].ppath);
963 }
964 if (csinfo[i].flags != NULL)
965 {
966 (void)strcat(cmd, " ");
967 (void)strcat(cmd, csinfo[i].flags);
968 }
969# ifdef UNIX
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100970 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 vim_free(prog);
972# endif
973 vim_free(ppath);
974
975#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200976# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100977 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200978# if defined(HAVE_SETSID)
979 (void)setsid();
980# else
981 if (setpgid(0, 0) == -1)
982 PERROR(_("cs_create_connection setpgid failed"));
983# endif
984# endif
ichizok40503052022-01-13 18:09:11 +0000985 if (build_argv_from_string((char_u *)cmd, &argv, &argc) == FAIL)
986 exit(EXIT_FAILURE);
987
988 if (execvp(argv[0], argv) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 PERROR(_("cs_create_connection exec failed"));
990
991 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100992 // NOTREACHED
ichizok40503052022-01-13 18:09:11 +0000993 }
994 else // parent.
995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 /*
997 * Save the file descriptors for later duplication, and
998 * reopen as streams.
999 */
1000 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1001 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1002 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1003 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1004
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001005 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 (void)close(to_cs[0]);
1007 (void)close(from_cs[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001010 // MSWIN
1011 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +00001012 GetStartupInfo(&si);
1013 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001014 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +00001015 si.hStdOutput = stdout_wr;
1016 si.hStdError = stdout_wr;
1017 si.hStdInput = stdin_rd;
1018 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1019 NULL, NULL, &si, &pi);
1020 vim_free(prog);
1021 vim_free(cmd);
1022
1023 if (!created)
1024 {
1025 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001026 (void)emsg(_(e_could_not_spawn_cscope_process));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001027 goto err_closing;
1028 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001029 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001030 csinfo[i].pid = pi.dwProcessId;
1031 csinfo[i].hProc = pi.hProcess;
1032 CloseHandle(pi.hThread);
1033
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 // TODO - tidy up after failure to create files on pipe handles.
K.Takatac351dc12022-01-24 11:24:08 +00001035 if (((fd = _open_osfhandle((intptr_t)stdin_wr,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001036 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001037 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1038 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
K.Takatac351dc12022-01-24 11:24:08 +00001039 if (((fd = _open_osfhandle((intptr_t)stdout_rd,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001040 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001041 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1042 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1043
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001044 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001045 CloseHandle(stdin_rd);
1046 CloseHandle(stdout_wr);
1047
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001048#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053
1054/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001055 * Query cscope using command line interface. Parse the output and use tselect
1056 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 *
1058 * returns TRUE if we jump to a tag or abort, FALSE if not.
1059 */
1060 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001061cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001064 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 if (cs_check_for_connections() == FALSE)
1067 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001068 (void)emsg(_(e_no_cscope_connections));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return FALSE;
1070 }
1071
1072 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1073 {
1074 cs_usage_msg(Find);
1075 return FALSE;
1076 }
1077
1078 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001079 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
1081 cs_usage_msg(Find);
1082 return FALSE;
1083 }
1084
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001085 /*
1086 * Let's replace the NULs written by strtok() with spaces - we need the
1087 * spaces to correctly display the quickfix/location list window's title.
1088 */
1089 for (i = 0; i < eap_arg_len; ++i)
1090 if (NUL == eap->arg[i])
1091 eap->arg[i] = ' ';
1092
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001093 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001094 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097
1098/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001099 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
1101 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001102cs_find_common(
1103 char *opt,
1104 char *pat,
1105 int forceit,
1106 int verbose,
1107 int use_ll UNUSED,
1108 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 int i;
1111 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001112 int *nummatches;
1113 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#ifdef FEAT_QUICKFIX
1115 char cmdletter;
1116 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001117
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001118 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001119 switch (opt[0])
1120 {
1121 case '0' :
1122 cmdletter = 's';
1123 break;
1124 case '1' :
1125 cmdletter = 'g';
1126 break;
1127 case '2' :
1128 cmdletter = 'd';
1129 break;
1130 case '3' :
1131 cmdletter = 'c';
1132 break;
1133 case '4' :
1134 cmdletter = 't';
1135 break;
1136 case '6' :
1137 cmdletter = 'e';
1138 break;
1139 case '7' :
1140 cmdletter = 'f';
1141 break;
1142 case '8' :
1143 cmdletter = 'i';
1144 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001145 case '9' :
1146 cmdletter = 'a';
1147 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001148 default :
1149 cmdletter = opt[0];
1150 }
1151
1152 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1153 if (qfpos != NULL)
1154 {
1155 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001156 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001157 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1158 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001159 (void)semsg(_(e_invalid_cscopequickfix_flag_chr_for_chr),
1160 *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001161 return FALSE;
1162 }
1163
Bram Moolenaar21662be2016-11-06 14:46:44 +01001164 if (*qfpos != '0'
1165 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1166 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001167 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001168# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001169 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001170 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001171# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001172 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#endif
1175
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001176 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 cmd = cs_create_cmd(opt, pat);
1178 if (cmd == NULL)
1179 return FALSE;
1180
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001181 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001182 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001183 {
1184 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001185 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001186 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001187
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001188 // Send query to all open connections, then count the total number
1189 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001190 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 nummatches[i] = 0;
1192 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001193 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001195 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 continue;
1197
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001198 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1200 (void)fflush(csinfo[i].to_fp);
1201
1202 nummatches[i] = cs_cnt_matches(i);
1203
1204 if (nummatches[i] > -1)
1205 totmatches += nummatches[i];
1206
1207 if (nummatches[i] == 0)
1208 (void)cs_read_prompt(i);
1209 }
1210 vim_free(cmd);
1211
1212 if (totmatches == 0)
1213 {
James McCoy3c5904d2021-10-24 14:50:07 +01001214 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001215 (void)semsg(_(e_no_matches_found_for_cscope_query_str_of_str),
1216 opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001217 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 return FALSE;
1219 }
1220
1221#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001222 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001224 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001225 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001226 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001227 qf_info_T *qi = NULL;
1228 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001230 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001231 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001232 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001235 cs_file_results(f, nummatches);
1236 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001237 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001239 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001240 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001241 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001243 if (postponed_split != 0)
1244 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001245 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001247 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001248 postponed_split = 0;
1249 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001250
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001251 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1252 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001253 if (use_ll)
1254 /*
1255 * In the location list window, use the displayed location
1256 * list. Otherwise, use the location list for the window.
1257 */
1258 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1259 ? wp->w_llist_ref : wp->w_llist;
1260 qf_jump(qi, 0, 0, forceit);
1261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 mch_remove(tmp);
1264 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001265 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 return TRUE;
1267 }
1268 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001269#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001271 char **matches = NULL, **contexts = NULL;
1272 int matched = 0;
1273
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001274 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001275 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001277 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (matches == NULL)
1279 return FALSE;
1280
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001281 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1284 }
1285
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001289 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001292cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 cscmd_T *cmdp = cs_cmds;
1295
Bram Moolenaar32526b32019-01-19 17:43:09 +01001296 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 while (cmdp->name != NULL)
1298 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001299 char *help = _(cmdp->help);
1300 int space_cnt = 30 - vim_strsize((char_u *)help);
1301
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001302 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001303 if (space_cnt < 0)
1304 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001305 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001306 cmdp->name,
1307 help, space_cnt, " ",
1308 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001310 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001311 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001312 " c: Find functions calling this function\n"
1313 " d: Find functions called by this function\n"
1314 " e: Find this egrep pattern\n"
1315 " f: Find this file\n"
1316 " g: Find this definition\n"
1317 " i: Find files #including this file\n"
1318 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001319 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 cmdp++;
1322 }
1323
1324 wait_return(TRUE);
1325 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001330clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 csinfo[i].fname = NULL;
1333 csinfo[i].ppath = NULL;
1334 csinfo[i].flags = NULL;
1335#if defined(UNIX)
1336 csinfo[i].st_dev = (dev_t)0;
1337 csinfo[i].st_ino = (ino_t)0;
1338#else
1339 csinfo[i].nVolume = 0;
1340 csinfo[i].nIndexHigh = 0;
1341 csinfo[i].nIndexLow = 0;
1342#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001343 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 csinfo[i].fr_fp = NULL;
1345 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001346#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001347 csinfo[i].hProc = NULL;
1348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349}
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001352 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001355cs_insert_filelist(
1356 char *fname,
1357 char *ppath,
1358 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001359 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 short i, j;
1362#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 BY_HANDLE_FILE_INFORMATION bhfi;
1364
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001365 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001367 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1368 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (p_csverbose)
1370 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001371 char *cant_msg = _(e_cannot_open_cscope_database_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char *winmsg = GetWin32Error();
1373
1374 if (winmsg != NULL)
1375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001376 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 LocalFree(winmsg);
1378 }
1379 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001380 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001381 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001384
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (p_csverbose)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001387 (void)emsg(_(e_cannot_get_cscope_database_information));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390#endif
1391
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001392 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001393 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
1395 if (csinfo[j].fname != NULL
1396#if defined(UNIX)
1397 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1398#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001399 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001400 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001401 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1402 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001403 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1405 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1406#endif
1407 )
1408 {
1409 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001410 (void)emsg(_(e_duplicate_cscope_database_not_added));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 return -1;
1412 }
1413
1414 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001415 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417
1418 if (i == -1)
1419 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001420 i = csinfo_size;
1421 if (csinfo_size == 0)
1422 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001423 // First time allocation: allocate only 1 connection. It should
1424 // be enough for most users. If more is needed, csinfo will be
1425 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001426 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001427 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001428 }
1429 else
1430 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001431 csinfo_T *t_csinfo = csinfo;
1432
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001433 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001434 csinfo_size *= 2;
1435 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001436 if (csinfo == NULL)
1437 {
1438 vim_free(t_csinfo);
1439 csinfo_size = 0;
1440 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001441 }
1442 if (csinfo == NULL)
1443 return -1;
1444 for (j = csinfo_size/2; j < csinfo_size; j++)
1445 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001448 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return -1;
1450
1451 (void)strcpy(csinfo[i].fname, (const char *)fname);
1452
1453 if (ppath != NULL)
1454 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001455 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001457 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return -1;
1459 }
1460 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1461 } else
1462 csinfo[i].ppath = NULL;
1463
1464 if (flags != NULL)
1465 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001466 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001468 VIM_CLEAR(csinfo[i].fname);
1469 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return -1;
1471 }
1472 (void)strcpy(csinfo[i].flags, (const char *)flags);
1473 } else
1474 csinfo[i].flags = NULL;
1475
1476#if defined(UNIX)
1477 csinfo[i].st_dev = sb->st_dev;
1478 csinfo[i].st_ino = sb->st_ino;
1479
1480#else
1481 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1482 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1483 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1484#endif
1485 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488
1489/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001490 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001493cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 cscmd_T *cmdp;
1496 char *stok;
1497 size_t len;
1498
1499 if (eap->arg == NULL)
1500 return NULL;
1501
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001502 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001503 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1506 return NULL;
1507
1508 len = strlen(stok);
1509 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1510 {
1511 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1512 return (cmdp);
1513 }
1514 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
1517
1518/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001519 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001522cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 char *stok;
1525 short i;
1526
1527 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1528 {
1529 cs_usage_msg(Kill);
1530 return CSCOPE_FAILURE;
1531 }
1532
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001533 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1535 || (strlen(stok) < 3 && stok[0] == '-'
1536 && VIM_ISDIGIT((int)(stok[1]))))
1537 i = atoi(stok);
1538 else
1539 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // It must be part of a name. We will try to find a match
1541 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001542 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1545 break;
1546 }
1547 }
1548
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001549 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 if (p_csverbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001552 (void)semsg(_(e_cscope_connection_str_not_founc), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
1554 else
1555 {
1556 if (i == -1)
1557 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001558 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 if (csinfo[i].fname)
1561 cs_kill_execute(i, csinfo[i].fname);
1562 }
1563 }
1564 else
1565 cs_kill_execute(i, stok);
1566 }
1567
1568 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571
1572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 * Actually kills a specific cscope connection.
1574 */
1575 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001576cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001577 int i, // cscope table index
1578 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 if (p_csverbose)
1581 {
1582 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001583 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001584 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 cs_release_csp(i, TRUE);
1587}
1588
1589
1590/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001591 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1593 * the type of the tag you are looking for. for example, in Darren Hiebert's
1594 * ctags (the one that comes with vim), #define's use a line number to find the
1595 * tag in a file while function definitions use a regexp search pattern.
1596 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001597 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * quirky (and probably other things i don't know about):
1599 *
1600 * if you have "# define" in your source file, which is
1601 * perfectly legal, cscope thinks you have "#define". this
1602 * will result in a failed regexp search. :(
1603 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001604 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * would still have to be modified to escape all the special regular expression
1606 * characters to comply with ctags formatting.
1607 */
1608 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001609cs_make_vim_style_matches(
1610 char *fname,
1611 char *slno,
1612 char *search,
1613 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001615 // vim style is ctags:
1616 //
1617 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1618 //
1619 // but as mentioned above, we'll always use the line number and
1620 // put the search pattern (if one exists) as "extra"
1621 //
1622 // buf is used as part of vim's method of handling tags, and
1623 // (i think) vim frees it when you pop your tags and get replaced
1624 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 char *buf;
1626 int amt;
1627
1628 if (search != NULL)
1629 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001630 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001631 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 return NULL;
1633
1634 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1635 }
1636 else
1637 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001638 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001639 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return NULL;
1641
1642 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1643 }
1644
1645 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648
1649/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001650 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *
1652 * Store: keep a ptr to the (malloc'd) memory of matches originally
1653 * generated from cs_find(). the matches are originally lines directly
1654 * from cscope output, but transformed to look like something out of a
1655 * ctags. see cs_make_vim_style_matches for more details.
1656 *
1657 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1658 * the next line from the cscope output. it basically keeps track of which
1659 * lines have been "used" and returns the next one.
1660 *
1661 * Free: frees up everything and resets
1662 *
1663 * Print: prints the tags
1664 */
1665 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001666cs_manage_matches(
1667 char **matches,
1668 char **contexts,
1669 int totmatches,
1670 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 static char **mp = NULL;
1673 static char **cp = NULL;
1674 static int cnt = -1;
1675 static int next = -1;
1676 char *p = NULL;
1677
1678 switch (cmd)
1679 {
1680 case Store:
1681 assert(matches != NULL);
1682 assert(totmatches > 0);
1683 if (mp != NULL || cp != NULL)
1684 (void)cs_manage_matches(NULL, NULL, -1, Free);
1685 mp = matches;
1686 cp = contexts;
1687 cnt = totmatches;
1688 next = 0;
1689 break;
1690 case Get:
1691 if (next >= cnt)
1692 return NULL;
1693
1694 p = mp[next];
1695 next++;
1696 break;
1697 case Free:
1698 if (mp != NULL)
1699 {
1700 if (cnt > 0)
1701 while (cnt--)
1702 {
1703 vim_free(mp[cnt]);
1704 if (cp != NULL)
1705 vim_free(cp[cnt]);
1706 }
1707 vim_free(mp);
1708 vim_free(cp);
1709 }
1710 mp = NULL;
1711 cp = NULL;
1712 cnt = 0;
1713 next = 0;
1714 break;
1715 case Print:
1716 cs_print_tags_priv(mp, cp, cnt);
1717 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001718 default: // should not reach here
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001719 iemsg(_(e_fatal_error_in_cs_manage_matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return NULL;
1721 }
1722
1723 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726
1727/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001728 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
1730 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001731cs_parse_results(
1732 int cnumber,
1733 char *buf,
1734 int bufsize,
1735 char **context,
1736 char **linenumber,
1737 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 int ch;
1740 char *p;
1741 char *name;
1742
1743 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1744 {
1745 if (feof(csinfo[cnumber].fr_fp))
1746 errno = EIO;
1747
1748 cs_reading_emsg(cnumber);
1749
1750 return NULL;
1751 }
1752
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001753 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if ((p = strchr(buf, '\n')) == NULL)
1755 {
1756 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1757 ;
1758 return NULL;
1759 }
1760 *p = '\0';
1761
1762 /*
1763 * cscope output is in the following format:
1764 *
1765 * <filename> <context> <line number> <pattern>
1766 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001767 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 return NULL;
1769 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1770 return NULL;
1771 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1772 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001773 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001775 // --- nvi ---
1776 // If the file is older than the cscope database, that is,
1777 // the database was built since the file was last modified,
1778 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (strcmp(*search, "<unknown>") == 0)
1780 *search = NULL;
1781
1782 name = cs_resolve_file(cnumber, name);
1783 return name;
1784}
1785
Bram Moolenaarc716c302006-01-21 22:12:51 +00001786#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001788 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
1790 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001791cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 int i, j;
1794 char *buf;
1795 char *search, *slno;
1796 char *fullname;
1797 char *cntx;
1798 char *context;
1799
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001800 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (buf == NULL)
1802 return;
1803
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001804 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 if (nummatches_a[i] < 1)
1807 continue;
1808
1809 for (j = 0; j < nummatches_a[i]; j++)
1810 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001811 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1812 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 continue;
1814
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001815 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001816 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001817 {
1818 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 if (strcmp(cntx, "<global>")==0)
1823 strcpy(context, "<<global>>");
1824 else
1825 sprintf(context, "<<%s>>", cntx);
1826
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001827 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1829 else
1830 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1831
1832 vim_free(context);
1833 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001834 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 (void)cs_read_prompt(i);
1837
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001838 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 vim_free(buf);
1840}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001844 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1845 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001846 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 */
1848 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001849cs_fill_results(
1850 char *tagstr,
1851 int totmatches,
1852 int *nummatches_a,
1853 char ***matches_p,
1854 char ***cntxts_p,
1855 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
1857 int i, j;
1858 char *buf;
1859 char *search, *slno;
1860 int totsofar = 0;
1861 char **matches = NULL;
1862 char **cntxts = NULL;
1863 char *fullname;
1864 char *cntx;
1865
1866 assert(totmatches > 0);
1867
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001868 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (buf == NULL)
1870 return;
1871
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001872 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001874 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 goto parse_out;
1876
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001877 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 if (nummatches_a[i] < 1)
1880 continue;
1881
1882 for (j = 0; j < nummatches_a[i]; j++)
1883 {
1884 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1885 &slno, &search)) == NULL)
1886 continue;
1887
1888 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1889 search, tagstr);
1890
1891 vim_free(fullname);
1892
1893 if (strcmp(cntx, "<global>") == 0)
1894 cntxts[totsofar] = NULL;
1895 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001896 // note: if vim_strsave returns NULL, then the context
1897 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1899
1900 if (matches[totsofar] != NULL)
1901 totsofar++;
1902
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001903 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 (void)cs_read_prompt(i);
1906
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001907 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001910 if (totsofar == 0)
1911 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001912 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001913 VIM_CLEAR(matches);
1914 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 *matched = totsofar;
1917 *matches_p = matches;
1918 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001924/*
1925 * get the requested path components
1926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001928cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 int i;
1931 char *s;
1932
1933 if (p_cspc == 0)
1934 return path;
1935
1936 s = path + strlen(path) - 1;
1937 for (i = 0; i < p_cspc; ++i)
1938 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001939#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 && *--s != '\\'
1941#endif
1942 )
1943 ;
1944 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001945#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 || (s > path && *s == '\\')
1947#endif
1948 )
1949 ++s;
1950 return s;
1951}
1952
1953/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001954 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001957cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001960 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001961 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 int newsize = 0;
1963 char *ptag;
1964 char *fname, *lno, *extra, *tbuf;
1965 int i, idx, num;
1966 char *globalcntx = "GLOBAL";
1967 char *cntxformat = " <<%s>>";
1968 char *context;
1969 char *cstag_msg = _("Cscope tag: %s");
1970 char *csfmt_str = "%4d %6s ";
1971
Bram Moolenaar4033c552017-09-16 20:54:51 +02001972 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001974 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 return;
1976
1977 strcpy(tbuf, matches[0]);
1978 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001979 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001980 {
1981 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001982 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001985 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001986 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (buf != NULL)
1988 {
1989 bufsize = newsize;
1990 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001991 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
1994 vim_free(tbuf);
1995
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001996 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 num = 1;
2001 for (i = 0; i < num_matches; i++)
2002 {
2003 idx = i;
2004
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002005 // if we really wanted to, we could avoid this malloc and strcpy
2006 // by parsing matches[i] on the fly and placing stuff into buf
2007 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002008 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 continue;
2010 (void)strcpy(tbuf, matches[idx]);
2011
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002012 if (strtok(tbuf, (const char *)"\t") == NULL
2013 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2014 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2015 {
2016 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002018 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002019 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002021 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002023 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002024 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (bufsize < newsize)
2026 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002027 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002028 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002032 vim_free(t_buf);
2033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 else
2035 bufsize = newsize;
2036 }
2037 if (buf != NULL)
2038 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002039 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002041 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002043 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2044 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002046 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (cntxts[idx] != NULL)
2048 context = cntxts[idx];
2049 else
2050 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002051 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053 if (bufsize < newsize)
2054 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002055 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002056 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002060 vim_free(t_buf);
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else
2063 bufsize = newsize;
2064 }
2065 if (buf != NULL)
2066 {
2067 (void)sprintf(buf, cntxformat, context);
2068
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002069 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (msg_col + (int)strlen(buf) >= (int)Columns)
2071 msg_putchar('\n');
2072 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002073 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 msg_putchar('\n');
2075 }
2076 if (extra != NULL)
2077 {
2078 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002079 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002082 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 if (msg_col)
2085 msg_putchar('\n');
2086
2087 ui_breakcheck();
2088 if (got_int)
2089 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002090 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 break;
2092 }
2093
2094 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002095 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100
2101/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002102 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 */
2104 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002105cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002108 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 int bufpos = 0;
2110 char *cs_emsg;
2111 int maxlen;
2112 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002113 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int n;
2115
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002116 cs_emsg = _(e_cscope_error_str);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002117 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2119
2120 for (;;)
2121 {
2122 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002123 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2125 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002126 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002127 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (buf != NULL)
2129 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002130 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 buf[bufpos++] = ch;
2132 buf[bufpos] = NUL;
2133 if (bufpos >= epromptlen
2134 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2135 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002136 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 buf[bufpos - epromptlen] = NUL;
2138
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002139 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002142 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 (void)putc('\n', csinfo[i].to_fp);
2144 (void)fflush(csinfo[i].to_fp);
2145
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002146 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 bufpos = 0;
2148 buf[bufpos] = NUL;
2149 }
2150 }
2151 }
2152
2153 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2154 {
2155 if (n > 0)
2156 ch = getc(csinfo[i].fr_fp);
2157 if (ch == EOF)
2158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002162 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 cs_release_csp(i, TRUE);
2164 vim_free(buf);
2165 return CSCOPE_FAILURE;
2166 }
2167
2168 if (ch != CSCOPE_PROMPT[n])
2169 {
2170 ch = EOF;
2171 break;
2172 }
2173 }
2174
2175 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002176 continue; // didn't find the prompt
2177 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
2180 vim_free(buf);
2181 return CSCOPE_SUCCESS;
2182}
2183
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002184#if defined(UNIX) && defined(SIGALRM)
2185/*
2186 * Used to catch and ignore SIGALRM below.
2187 */
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002188 static RETSIGTYPE
2189sig_handler SIGDEFARG(sigarg)
2190{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002191 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002192 SIGRETURN;
2193}
2194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002197 * Does the actual free'ing for the cs ptr with an optional flag of whether
2198 * or not to free the filename. Called by cs_kill and cs_reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 */
2200 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002201cs_release_csp(int i, int freefnpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 /*
2204 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2205 */
2206 if (csinfo[i].to_fp != NULL)
2207 {
2208 (void)fputs("q\n", csinfo[i].to_fp);
2209 (void)fflush(csinfo[i].to_fp);
2210 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002211#if defined(UNIX)
2212 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002213 int waitpid_errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002214 int pstat;
2215 pid_t pid;
2216
2217# if defined(HAVE_SIGACTION)
2218 struct sigaction sa, old;
2219
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002220 // Use sigaction() to limit the waiting time to two seconds.
Bram Moolenaar9701da02008-03-16 12:09:58 +00002221 sigemptyset(&sa.sa_mask);
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002222 sa.sa_handler = sig_handler;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002223# ifdef SA_NODEFER
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002224 sa.sa_flags = SA_NODEFER;
Bram Moolenaar25153e12010-02-24 14:47:08 +01002225# else
2226 sa.sa_flags = 0;
2227# endif
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002228 sigaction(SIGALRM, &sa, &old);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002229 alarm(2); // 2 sec timeout
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002230
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002231 // Block until cscope exits or until timer expires
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002232 pid = waitpid(csinfo[i].pid, &pstat, 0);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002233 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002234
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002235 // cancel pending alarm if still there and restore signal
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002236 alarm(0);
2237 sigaction(SIGALRM, &old, NULL);
2238# else
2239 int waited;
2240
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002241 // Can't use sigaction(), loop for two seconds. First yield the CPU
2242 // to give cscope a chance to exit quickly.
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002243 sleep(0);
2244 for (waited = 0; waited < 40; ++waited)
2245 {
2246 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
Bram Moolenaare9b28842008-04-01 12:31:14 +00002247 waitpid_errno = errno;
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002248 if (pid != 0)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002249 break; // break unless the process is still running
Bram Moolenaar0981c872020-08-23 14:28:37 +02002250 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002251 }
2252# endif
2253 /*
2254 * If the cscope process is still running: kill it.
2255 * Safety check: If the PID would be zero here, the entire X session
2256 * would be killed. -1 and 1 are dangerous as well.
2257 */
2258 if (pid < 0 && csinfo[i].pid > 1)
2259 {
Bram Moolenaare9b28842008-04-01 12:31:14 +00002260# ifdef ECHILD
2261 int alive = TRUE;
2262
2263 if (waitpid_errno == ECHILD)
2264 {
2265 /*
2266 * When using 'vim -g', vim is forked and cscope process is
2267 * no longer a child process but a sibling. So waitpid()
2268 * fails with errno being ECHILD (No child processes).
2269 * Don't send SIGKILL to cscope immediately but wait
2270 * (polling) for it to exit normally as result of sending
2271 * the "q" command, hence giving it a chance to clean up
2272 * its temporary files.
2273 */
2274 int waited;
2275
2276 sleep(0);
2277 for (waited = 0; waited < 40; ++waited)
2278 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002279 // Check whether cscope process is still alive
Bram Moolenaare9b28842008-04-01 12:31:14 +00002280 if (kill(csinfo[i].pid, 0) != 0)
2281 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002282 alive = FALSE; // cscope process no longer exists
Bram Moolenaare9b28842008-04-01 12:31:14 +00002283 break;
2284 }
Bram Moolenaar0981c872020-08-23 14:28:37 +02002285 mch_delay(50L, 0); // sleep 50 ms
Bram Moolenaare9b28842008-04-01 12:31:14 +00002286 }
2287 }
2288 if (alive)
2289# endif
2290 {
2291 kill(csinfo[i].pid, SIGKILL);
2292 (void)waitpid(csinfo[i].pid, &pstat, 0);
2293 }
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002294 }
2295 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002296#else // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00002297 if (csinfo[i].hProc != NULL)
2298 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002299 // Give cscope a chance to exit normally
Bram Moolenaar02b06312007-09-06 15:39:22 +00002300 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2301 TerminateProcess(csinfo[i].hProc, 0);
2302 CloseHandle(csinfo[i].hProc);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
2305
2306 if (csinfo[i].fr_fp != NULL)
2307 (void)fclose(csinfo[i].fr_fp);
2308 if (csinfo[i].to_fp != NULL)
2309 (void)fclose(csinfo[i].to_fp);
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (freefnpp)
2312 {
2313 vim_free(csinfo[i].fname);
2314 vim_free(csinfo[i].ppath);
2315 vim_free(csinfo[i].flags);
2316 }
2317
2318 clear_csinfo(i);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002319}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321
2322/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002323 * Calls cs_kill on all cscope connections then reinits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002326cs_reset(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2329 int i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002330 char buf[20]; // for sprintf " (#%d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002332 if (csinfo_size == 0)
2333 return CSCOPE_SUCCESS;
2334
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002335 // malloc our db and ppath list
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002336 dblist = ALLOC_MULT(char *, csinfo_size);
2337 pplist = ALLOC_MULT(char *, csinfo_size);
2338 fllist = ALLOC_MULT(char *, csinfo_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (dblist == NULL || pplist == NULL || fllist == NULL)
2340 {
2341 vim_free(dblist);
2342 vim_free(pplist);
2343 vim_free(fllist);
2344 return CSCOPE_FAILURE;
2345 }
2346
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002347 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 dblist[i] = csinfo[i].fname;
2350 pplist[i] = csinfo[i].ppath;
2351 fllist[i] = csinfo[i].flags;
2352 if (csinfo[i].fname != NULL)
2353 cs_release_csp(i, FALSE);
2354 }
2355
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002356 // rebuild the cscope connection list
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002357 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 if (dblist[i] != NULL)
2360 {
2361 cs_add_common(dblist[i], pplist[i], fllist[i]);
2362 if (p_csverbose)
2363 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002364 // don't use smsg_attr() because we want to display the
2365 // connection number in the same line as
2366 // "Added cscope database..."
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 sprintf(buf, " (#%d)", i);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002368 msg_puts_attr(buf, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 }
2371 vim_free(dblist[i]);
2372 vim_free(pplist[i]);
2373 vim_free(fllist[i]);
2374 }
2375 vim_free(dblist);
2376 vim_free(pplist);
2377 vim_free(fllist);
2378
2379 if (p_csverbose)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002380 msg_attr(_("All cscope databases reset"), HL_ATTR(HLF_R) | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002382}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384
2385/*
Bram Moolenaar28c21912013-05-29 19:18:00 +02002386 * Construct the full pathname to a file found in the cscope database.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 * (Prepends ppath, if there is one and if it's not already prepended,
2388 * otherwise just uses the name found.)
2389 *
Bram Moolenaar28c21912013-05-29 19:18:00 +02002390 * We need to prepend the prefix because on some cscope's (e.g., the one that
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * ships with Solaris 2.6), the output never has the prefix prepended.
Bram Moolenaar28c21912013-05-29 19:18:00 +02002392 * Contrast this with my development system (Digital Unix), which does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 */
2394 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002395cs_resolve_file(int i, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002397 char *fullname;
2398 int len;
2399 char_u *csdir = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 /*
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002402 * Ppath is freed when we destroy the cscope connection.
2403 * Fullname is freed after cs_make_vim_style_matches, after it's been
2404 * copied into the tag buffer used by Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002406 len = (int)(strlen(name) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (csinfo[i].ppath != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002408 len += (int)strlen(csinfo[i].ppath);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002409 else if (p_csre && csinfo[i].fname != NULL)
2410 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002411 // If 'cscoperelative' is set and ppath is not set, use cscope.out
2412 // path in path resolution.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002413 csdir = alloc(MAXPATHL);
2414 if (csdir != NULL)
2415 {
2416 vim_strncpy(csdir, (char_u *)csinfo[i].fname,
Bram Moolenaard23a8232018-02-10 18:45:26 +01002417 gettail((char_u *)csinfo[i].fname)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002418 - (char_u *)csinfo[i].fname);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002419 len += (int)STRLEN(csdir);
2420 }
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002423 // Note/example: this won't work if the cscope output already starts
2424 // "../.." and the prefix path is also "../..". if something like this
2425 // happens, you are screwed up and need to fix how you're using cscope.
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002426 if (csinfo[i].ppath != NULL
2427 && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0)
2428 && (name[0] != '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01002429#ifdef MSWIN
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002430 && name[0] != '\\' && name[1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#endif
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002432 )
Bram Moolenaar28c21912013-05-29 19:18:00 +02002433 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002434 if ((fullname = alloc(len)) != NULL)
Bram Moolenaar28c21912013-05-29 19:18:00 +02002435 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2436 }
2437 else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL)
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002438 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002439 // Check for csdir to be non empty to avoid empty path concatenated to
2440 // cscope output.
Bram Moolenaar03227ee2011-06-12 21:25:00 +02002441 fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE);
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 else
Bram Moolenaar28c21912013-05-29 19:18:00 +02002444 {
2445 fullname = (char *)vim_strsave((char_u *)name);
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002448 vim_free(csdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return fullname;
Bram Moolenaar2f982e42011-06-12 20:42:22 +02002450}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452
2453/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002454 * Show all cscope connections.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002457cs_show(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 short i;
2460 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002461 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 else
2463 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002464 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002466 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002467 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 if (csinfo[i].fname == NULL)
2470 continue;
2471
2472 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2475 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 i, (long)csinfo[i].pid, csinfo[i].fname);
2478 }
2479 }
2480
2481 wait_return(TRUE);
2482 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
Bram Moolenaar02b06312007-09-06 15:39:22 +00002485
2486/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002487 * Only called when VIM exits to quit any cscope sessions.
2488 */
2489 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002490cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002491{
2492 int i;
2493
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002494 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002495 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002496 vim_free(csinfo);
2497 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002498}
2499
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002500#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002502#if defined(FEAT_EVAL) || defined(PROTO)
2503
2504/*
2505 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2506 *
2507 * Checks the existence of a cscope connection.
2508 */
2509 void
2510f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2511{
2512# ifdef FEAT_CSCOPE
2513 int num = 0;
2514 char_u *dbpath = NULL;
2515 char_u *prepend = NULL;
2516 char_u buf[NUMBUFLEN];
2517
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002518 if (in_vim9script()
2519 && (check_for_opt_number_arg(argvars, 0) == FAIL
2520 || (argvars[0].v_type != VAR_UNKNOWN
2521 && (check_for_opt_string_arg(argvars, 1) == FAIL
2522 || (argvars[1].v_type != VAR_UNKNOWN
2523 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2524 return;
2525
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002526 if (argvars[0].v_type != VAR_UNKNOWN
2527 && argvars[1].v_type != VAR_UNKNOWN)
2528 {
2529 num = (int)tv_get_number(&argvars[0]);
2530 dbpath = tv_get_string(&argvars[1]);
2531 if (argvars[2].v_type != VAR_UNKNOWN)
2532 prepend = tv_get_string_buf(&argvars[2], buf);
2533 }
2534
2535 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2536# endif
2537}
2538
2539#endif // FEAT_EVAL