blob: ad29fb9f507c5246c56072affeb5bb8699e3609c [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{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +0000678 int i;
679 int cnt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000681 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 if (csinfo[i].fname != NULL)
683 cnt++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 return cnt;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100685}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100688cs_reading_emsg(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100689 int idx) // connection index
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000691 semsg(_(e_error_reading_cscope_connection_nr), idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693
694#define CSREAD_BUFSIZE 2048
695/*
Bram Moolenaard4db7712016-11-12 19:16:46 +0100696 * Count the number of matches for a given cscope connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100699cs_cnt_matches(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 char *stok;
702 char *buf;
Bram Moolenaar1274d332018-01-30 21:47:52 +0100703 int nlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200705 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (buf == NULL)
707 return 0;
708 for (;;)
709 {
710 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
711 {
712 if (feof(csinfo[idx].fr_fp))
713 errno = EIO;
714
715 cs_reading_emsg(idx);
716
717 vim_free(buf);
718 return -1;
719 }
720
721 /*
722 * If the database is out of date, or there's some other problem,
723 * cscope will output error messages before the number-of-lines output.
724 * Display/discard any output that doesn't match what we want.
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000725 * Accept "\S*cscope: X lines", also matches "mlcscope".
Bram Moolenaar1274d332018-01-30 21:47:52 +0100726 * Bail out for the "Unable to search" error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaara172b632018-01-30 22:52:06 +0100728 if (strstr((const char *)buf, "Unable to search database") != NULL)
Bram Moolenaar1274d332018-01-30 21:47:52 +0100729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if ((stok = strtok(buf, (const char *)" ")) == NULL)
731 continue;
Bram Moolenaar84c4d792007-01-16 14:18:41 +0000732 if (strstr((const char *)stok, "cscope:") == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 continue;
734
735 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
736 continue;
737 nlines = atoi(stok);
738 if (nlines < 0)
739 {
740 nlines = 0;
741 break;
742 }
743
744 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
745 continue;
746 if (strncmp((const char *)stok, "lines", 5))
747 continue;
748
749 break;
750 }
751
752 vim_free(buf);
753 return nlines;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100754}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756
757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * Creates the actual cscope command query from what the user entered.
759 */
760 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100761cs_create_cmd(char *csoption, char *pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 char *cmd;
764 short search;
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000765 char *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
767 switch (csoption[0])
768 {
769 case '0' : case 's' :
770 search = 0;
771 break;
772 case '1' : case 'g' :
773 search = 1;
774 break;
775 case '2' : case 'd' :
776 search = 2;
777 break;
778 case '3' : case 'c' :
779 search = 3;
780 break;
781 case '4' : case 't' :
782 search = 4;
783 break;
784 case '6' : case 'e' :
785 search = 6;
786 break;
787 case '7' : case 'f' :
788 search = 7;
789 break;
790 case '8' : case 'i' :
791 search = 8;
792 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +0200793 case '9' : case 'a' :
794 search = 9;
795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 default :
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000797 (void)emsg(_(e_unknown_cscope_search_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 cs_usage_msg(Find);
799 return NULL;
800 }
801
Bram Moolenaardc215522022-09-25 17:03:26 +0100802 // Skip white space before the pattern, except for text and pattern search,
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100803 // they may want to use the leading white space.
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000804 pat = pattern;
805 if (search != 4 && search != 6)
Bram Moolenaar1c465442017-03-12 20:10:05 +0100806 while VIM_ISWHITE(*pat)
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000807 ++pat;
808
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200809 if ((cmd = alloc(strlen(pat) + 2)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return NULL;
811
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000812 (void)sprintf(cmd, "%d%s", search, pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 return cmd;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100815}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817
818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 * This piece of code was taken/adapted from nvi. do we need to add
820 * the BSD license notice?
821 */
822 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100823cs_create_connection(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar02b06312007-09-06 15:39:22 +0000825#ifdef UNIX
826 int to_cs[2], from_cs[2];
827#endif
Bram Moolenaardc215522022-09-25 17:03:26 +0100828 int cmdlen;
Bram Moolenaar02b06312007-09-06 15:39:22 +0000829 int len;
830 char *prog, *cmd, *ppath = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100831#ifdef MSWIN
Bram Moolenaar02b06312007-09-06 15:39:22 +0000832 int fd;
833 SECURITY_ATTRIBUTES sa;
834 PROCESS_INFORMATION pi;
835 STARTUPINFO si;
836 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
837 HANDLE stdin_rd, stdout_rd;
838 HANDLE stdout_wr, stdin_wr;
839 BOOL created;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif
841
Bram Moolenaar02b06312007-09-06 15:39:22 +0000842#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 /*
844 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
845 * from_cs[0] and writes to to_cs[1].
846 */
847 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
848 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
849 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000850 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851err_closing:
852 if (to_cs[0] != -1)
853 (void)close(to_cs[0]);
854 if (to_cs[1] != -1)
855 (void)close(to_cs[1]);
856 if (from_cs[0] != -1)
857 (void)close(from_cs[0]);
858 if (from_cs[1] != -1)
859 (void)close(from_cs[1]);
860 return CSCOPE_FAILURE;
861 }
862
ichizok40503052022-01-13 18:09:11 +0000863 if ((csinfo[i].pid = fork()) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000865 (void)emsg(_(e_could_not_fork_for_cscope));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 goto err_closing;
ichizok40503052022-01-13 18:09:11 +0000867 }
868 else if (csinfo[i].pid == 0) // child: run cscope.
869 {
870 char **argv = NULL;
871 int argc = 0;
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (dup2(to_cs[0], STDIN_FILENO) == -1)
874 PERROR("cs_create_connection 1");
875 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
876 PERROR("cs_create_connection 2");
877 if (dup2(from_cs[1], STDERR_FILENO) == -1)
878 PERROR("cs_create_connection 3");
879
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100880 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 (void)close(to_cs[1]);
882 (void)close(from_cs[0]);
883#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100884 // MSWIN
885 // Create pipes to communicate with cscope
Bram Moolenaar02b06312007-09-06 15:39:22 +0000886 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
887 sa.bInheritHandle = TRUE;
888 sa.lpSecurityDescriptor = NULL;
889
890 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
891 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
892 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000893 (void)emsg(_(e_could_not_create_cscope_pipes));
Bram Moolenaar02b06312007-09-06 15:39:22 +0000894err_closing:
895 if (pipe_stdin)
896 {
897 CloseHandle(stdin_rd);
898 CloseHandle(stdin_wr);
899 }
900 if (pipe_stdout)
901 {
902 CloseHandle(stdout_rd);
903 CloseHandle(stdout_wr);
904 }
905 return CSCOPE_FAILURE;
906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100908 // expand the cscope exec for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200909 if ((prog = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
911#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000912 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100914 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 goto err_closing;
916#endif
917 }
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000918 expand_env(p_csprg, (char_u *)prog, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100920 // alloc space to hold the cscope command
Bram Moolenaardc215522022-09-25 17:03:26 +0100921 cmdlen = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (csinfo[i].ppath)
923 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100924 // expand the prepend path for env var's
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200925 if ((ppath = alloc(MAXPATHL + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 vim_free(prog);
928#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000929 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100931 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 goto err_closing;
933#endif
934 }
935 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
936
Bram Moolenaardc215522022-09-25 17:03:26 +0100937 cmdlen += (int)strlen(ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
939
940 if (csinfo[i].flags)
Bram Moolenaardc215522022-09-25 17:03:26 +0100941 cmdlen += (int)strlen(csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaardc215522022-09-25 17:03:26 +0100943 if ((cmd = alloc(cmdlen)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
945 vim_free(prog);
946 vim_free(ppath);
947#ifdef UNIX
ichizok40503052022-01-13 18:09:11 +0000948 exit(EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100950 // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 goto err_closing;
952#endif
953 }
954
ichizok40503052022-01-13 18:09:11 +0000955 // run the cscope command
Bram Moolenaardc215522022-09-25 17:03:26 +0100956#ifdef UNIX
957 vim_snprintf(cmd, cmdlen, "/bin/sh -c \"exec %s -dl -f %s\"",
958 prog, csinfo[i].fname);
959#else
960 vim_snprintf(cmd, cmdlen, "%s -dl -f %s", prog, csinfo[i].fname);
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (csinfo[i].ppath != NULL)
963 {
Bram Moolenaardc215522022-09-25 17:03:26 +0100964 len = STRLEN(cmd);
965 vim_snprintf(cmd + len, cmdlen - len, " -P%s", csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 if (csinfo[i].flags != NULL)
968 {
Bram Moolenaardc215522022-09-25 17:03:26 +0100969 len = STRLEN(cmd);
970 vim_snprintf(cmd + len, cmdlen - len, " %s", csinfo[i].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972# ifdef UNIX
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100973 // on Win32 we still need prog
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 vim_free(prog);
975# endif
976 vim_free(ppath);
977
978#if defined(UNIX)
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200979# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100980 // Change our process group to avoid cscope receiving SIGWINCH.
Bram Moolenaar85e932f2013-06-30 15:01:22 +0200981# if defined(HAVE_SETSID)
982 (void)setsid();
983# else
984 if (setpgid(0, 0) == -1)
985 PERROR(_("cs_create_connection setpgid failed"));
986# endif
987# endif
ichizok40503052022-01-13 18:09:11 +0000988 if (build_argv_from_string((char_u *)cmd, &argv, &argc) == FAIL)
989 exit(EXIT_FAILURE);
990
991 if (execvp(argv[0], argv) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 PERROR(_("cs_create_connection exec failed"));
993
994 exit(127);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100995 // NOTREACHED
ichizok40503052022-01-13 18:09:11 +0000996 }
997 else // parent.
998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * Save the file descriptors for later duplication, and
1001 * reopen as streams.
1002 */
1003 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1004 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1005 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1007
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001008 // close unused
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 (void)close(to_cs[0]);
1010 (void)close(from_cs[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012#else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001013 // MSWIN
1014 // Create a new process to run cscope and use pipes to talk with it
Bram Moolenaar02b06312007-09-06 15:39:22 +00001015 GetStartupInfo(&si);
1016 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001017 si.wShowWindow = SW_HIDE; // Hide child application window
Bram Moolenaar02b06312007-09-06 15:39:22 +00001018 si.hStdOutput = stdout_wr;
1019 si.hStdError = stdout_wr;
1020 si.hStdInput = stdin_rd;
1021 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1022 NULL, NULL, &si, &pi);
1023 vim_free(prog);
1024 vim_free(cmd);
1025
1026 if (!created)
1027 {
1028 PERROR(_("cs_create_connection exec failed"));
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001029 (void)emsg(_(e_could_not_spawn_cscope_process));
Bram Moolenaar02b06312007-09-06 15:39:22 +00001030 goto err_closing;
1031 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001032 // else
Bram Moolenaar02b06312007-09-06 15:39:22 +00001033 csinfo[i].pid = pi.dwProcessId;
1034 csinfo[i].hProc = pi.hProcess;
1035 CloseHandle(pi.hThread);
1036
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001037 // TODO - tidy up after failure to create files on pipe handles.
K.Takatac351dc12022-01-24 11:24:08 +00001038 if (((fd = _open_osfhandle((intptr_t)stdin_wr,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001039 _O_TEXT|_O_APPEND)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001040 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1041 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
K.Takatac351dc12022-01-24 11:24:08 +00001042 if (((fd = _open_osfhandle((intptr_t)stdout_rd,
Bram Moolenaar5365c4d2007-09-14 17:56:59 +00001043 _O_TEXT|_O_RDONLY)) < 0)
Bram Moolenaar02b06312007-09-06 15:39:22 +00001044 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1045 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1046
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001047 // Close handles for file descriptors inherited by the cscope process
Bram Moolenaar02b06312007-09-06 15:39:22 +00001048 CloseHandle(stdin_rd);
1049 CloseHandle(stdout_wr);
1050
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001051#endif // !UNIX
Bram Moolenaar02b06312007-09-06 15:39:22 +00001052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056
1057/*
Bram Moolenaarcde88542015-08-11 19:14:00 +02001058 * Query cscope using command line interface. Parse the output and use tselect
1059 * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 *
1061 * returns TRUE if we jump to a tag or abort, FALSE if not.
1062 */
1063 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001064cs_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 char *opt, *pat;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001067 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 if (cs_check_for_connections() == FALSE)
1070 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001071 (void)emsg(_(e_no_cscope_connections));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return FALSE;
1073 }
1074
1075 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1076 {
1077 cs_usage_msg(Find);
1078 return FALSE;
1079 }
1080
1081 pat = opt + strlen(opt) + 1;
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001082 if (pat >= (char *)eap->arg + eap_arg_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 cs_usage_msg(Find);
1085 return FALSE;
1086 }
1087
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001088 /*
1089 * Let's replace the NULs written by strtok() with spaces - we need the
1090 * spaces to correctly display the quickfix/location list window's title.
1091 */
1092 for (i = 0; i < eap_arg_len; ++i)
1093 if (NUL == eap->arg[i])
1094 eap->arg[i] = ' ';
1095
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001096 return cs_find_common(opt, pat, eap->forceit, TRUE,
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001097 eap->cmdidx == CMD_lcscope, *eap->cmdlinep);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100
1101/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001102 * Common code for cscope find, shared by cs_find() and ex_cstag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 */
1104 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001105cs_find_common(
1106 char *opt,
1107 char *pat,
1108 int forceit,
1109 int verbose,
1110 int use_ll UNUSED,
1111 char_u *cmdline UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int i;
1114 char *cmd;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001115 int *nummatches;
1116 int totmatches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#ifdef FEAT_QUICKFIX
1118 char cmdletter;
1119 char *qfpos;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001120
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001121 // get cmd letter
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001122 switch (opt[0])
1123 {
1124 case '0' :
1125 cmdletter = 's';
1126 break;
1127 case '1' :
1128 cmdletter = 'g';
1129 break;
1130 case '2' :
1131 cmdletter = 'd';
1132 break;
1133 case '3' :
1134 cmdletter = 'c';
1135 break;
1136 case '4' :
1137 cmdletter = 't';
1138 break;
1139 case '6' :
1140 cmdletter = 'e';
1141 break;
1142 case '7' :
1143 cmdletter = 'f';
1144 break;
1145 case '8' :
1146 cmdletter = 'i';
1147 break;
Bram Moolenaarb12e7ef2016-06-21 23:42:20 +02001148 case '9' :
1149 cmdletter = 'a';
1150 break;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001151 default :
1152 cmdletter = opt[0];
1153 }
1154
1155 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1156 if (qfpos != NULL)
1157 {
1158 qfpos++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001159 // next symbol must be + or -
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001160 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1161 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001162 (void)semsg(_(e_invalid_cscopequickfix_flag_chr_for_chr),
1163 *qfpos, *(qfpos - 1));
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001164 return FALSE;
1165 }
1166
Bram Moolenaar21662be2016-11-06 14:46:44 +01001167 if (*qfpos != '0'
1168 && apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope",
1169 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001170 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001171# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01001172 if (aborting())
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001173 return FALSE;
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001174# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001175 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
1178
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001179 // create the actual command to send to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 cmd = cs_create_cmd(opt, pat);
1181 if (cmd == NULL)
1182 return FALSE;
1183
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001184 nummatches = ALLOC_MULT(int, csinfo_size);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001185 if (nummatches == NULL)
Bram Moolenaarcde88542015-08-11 19:14:00 +02001186 {
1187 vim_free(cmd);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001188 return FALSE;
Bram Moolenaarcde88542015-08-11 19:14:00 +02001189 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001190
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001191 // Send query to all open connections, then count the total number
1192 // of matches so we can alloc all in one swell foop.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001193 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 nummatches[i] = 0;
1195 totmatches = 0;
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001196 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaar508b9e82006-11-21 10:43:23 +00001198 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 continue;
1200
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001201 // send cmd to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1203 (void)fflush(csinfo[i].to_fp);
1204
1205 nummatches[i] = cs_cnt_matches(i);
1206
1207 if (nummatches[i] > -1)
1208 totmatches += nummatches[i];
1209
1210 if (nummatches[i] == 0)
1211 (void)cs_read_prompt(i);
1212 }
1213 vim_free(cmd);
1214
1215 if (totmatches == 0)
1216 {
James McCoy3c5904d2021-10-24 14:50:07 +01001217 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001218 (void)semsg(_(e_no_matches_found_for_cscope_query_str_of_str),
1219 opt, pat);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001220 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 return FALSE;
1222 }
1223
1224#ifdef FEAT_QUICKFIX
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001227 // fill error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001228 FILE *f;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001229 char_u *tmp = vim_tempname('c', TRUE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001230 qf_info_T *qi = NULL;
1231 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001233 f = mch_fopen((char *)tmp, "w");
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001234 if (f == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001235 semsg(_(e_cant_open_file_str), tmp);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001238 cs_file_results(f, nummatches);
1239 fclose(f);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001240 if (use_ll) // Use location list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001241 wp = curwin;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001242 // '-' starts a new error list
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001243 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001244 *qfpos == '-', cmdline, NULL) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001246 if (postponed_split != 0)
1247 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02001248 (void)win_split(postponed_split > 0 ? postponed_split : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 postponed_split_flags);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001250 RESET_BINDING(curwin);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001251 postponed_split = 0;
1252 }
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001253
Bram Moolenaarf1eeae92010-05-14 23:14:42 +02001254 apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope",
1255 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001256 if (use_ll)
1257 /*
1258 * In the location list window, use the displayed location
1259 * list. Otherwise, use the location list for the window.
1260 */
1261 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1262 ? wp->w_llist_ref : wp->w_llist;
1263 qf_jump(qi, 0, 0, forceit);
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266 mch_remove(tmp);
1267 vim_free(tmp);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001268 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return TRUE;
1270 }
1271 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001272#endif // FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001274 char **matches = NULL, **contexts = NULL;
1275 int matched = 0;
1276
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001277 // read output
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001278 cs_fill_results(pat, totmatches, nummatches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 &contexts, &matched);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001280 vim_free(nummatches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (matches == NULL)
1282 return FALSE;
1283
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001284 (void)cs_manage_matches(matches, contexts, matched, Store);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1287 }
1288
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001292 * Print help.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001295cs_help(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 cscmd_T *cmdp = cs_cmds;
1298
Bram Moolenaar32526b32019-01-19 17:43:09 +01001299 (void)msg_puts(_("cscope commands:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 while (cmdp->name != NULL)
1301 {
Bram Moolenaardb867d52009-01-28 15:04:42 +00001302 char *help = _(cmdp->help);
1303 int space_cnt = 30 - vim_strsize((char_u *)help);
1304
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001305 // Use %*s rather than %30s to ensure proper alignment in utf-8
Bram Moolenaardb867d52009-01-28 15:04:42 +00001306 if (space_cnt < 0)
1307 space_cnt = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001308 (void)smsg(_("%-5s: %s%*s (Usage: %s)"),
Bram Moolenaardb867d52009-01-28 15:04:42 +00001309 cmdp->name,
1310 help, space_cnt, " ",
1311 cmdp->usage);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (strcmp(cmdp->name, "find") == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001313 msg_puts(_("\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001314 " a: Find assignments to this symbol\n"
Bram Moolenaar627943d2008-08-25 02:35:59 +00001315 " c: Find functions calling this function\n"
1316 " d: Find functions called by this function\n"
1317 " e: Find this egrep pattern\n"
1318 " f: Find this file\n"
1319 " g: Find this definition\n"
1320 " i: Find files #including this file\n"
1321 " s: Find this C symbol\n"
Bram Moolenaar80632db2016-07-05 22:28:40 +02001322 " t: Find this text string\n"));
Bram Moolenaar627943d2008-08-25 02:35:59 +00001323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 cmdp++;
1325 }
1326
1327 wait_return(TRUE);
1328 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001333clear_csinfo(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 csinfo[i].fname = NULL;
1336 csinfo[i].ppath = NULL;
1337 csinfo[i].flags = NULL;
1338#if defined(UNIX)
1339 csinfo[i].st_dev = (dev_t)0;
1340 csinfo[i].st_ino = (ino_t)0;
1341#else
1342 csinfo[i].nVolume = 0;
1343 csinfo[i].nIndexHigh = 0;
1344 csinfo[i].nIndexLow = 0;
1345#endif
Bram Moolenaar446cb832008-06-24 21:56:24 +00001346 csinfo[i].pid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 csinfo[i].fr_fp = NULL;
1348 csinfo[i].to_fp = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001349#if defined(MSWIN)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001350 csinfo[i].hProc = NULL;
1351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352}
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001355 * Insert a new cscope database filename into the filelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 */
1357 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001358cs_insert_filelist(
1359 char *fname,
1360 char *ppath,
1361 char *flags,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001362 stat_T *sb UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001364 int i;
1365 int j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#ifndef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 BY_HANDLE_FILE_INFORMATION bhfi;
1368
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001369 switch (win32_fileinfo((char_u *)fname, &bhfi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001371 case FILEINFO_ENC_FAIL: // enc_to_utf16() failed
1372 case FILEINFO_READ_FAIL: // CreateFile() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (p_csverbose)
1374 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001375 char *cant_msg = _(e_cannot_open_cscope_database_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char *winmsg = GetWin32Error();
1377
1378 if (winmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001379 (void)semsg(cant_msg, winmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001381 // subst filename if can't get error text
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 (void)semsg(cant_msg, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 return -1;
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02001385
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001386 case FILEINFO_INFO_FAIL: // GetFileInformationByHandle() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (p_csverbose)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001388 (void)emsg(_(e_cannot_get_cscope_database_information));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391#endif
1392
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001393 i = -1; // can be set to the index of an empty item in csinfo
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001394 for (j = 0; j < csinfo_size; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 if (csinfo[j].fname != NULL
1397#if defined(UNIX)
1398 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1399#else
Bram Moolenaar99499b12019-05-23 21:35:48 +02001400 // compare pathnames first
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001401 && ((fullpathcmp((char_u *)csinfo[j].fname,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001402 (char_u *)fname, FALSE, TRUE) & FPC_SAME)
1403 // test index file attributes too
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001404 || (csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1406 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1407#endif
1408 )
1409 {
1410 if (p_csverbose)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001411 (void)emsg(_(e_duplicate_cscope_database_not_added));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 return -1;
1413 }
1414
1415 if (csinfo[j].fname == NULL && i == -1)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001416 i = j; // remember first empty entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418
1419 if (i == -1)
1420 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001421 i = csinfo_size;
1422 if (csinfo_size == 0)
1423 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001424 // First time allocation: allocate only 1 connection. It should
1425 // be enough for most users. If more is needed, csinfo will be
1426 // reallocated.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001427 csinfo_size = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001428 csinfo = ALLOC_CLEAR_ONE(csinfo_T);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001429 }
1430 else
1431 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001432 csinfo_T *t_csinfo = csinfo;
1433
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001434 // Reallocate space for more connections.
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001435 csinfo_size *= 2;
1436 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001437 if (csinfo == NULL)
1438 {
1439 vim_free(t_csinfo);
1440 csinfo_size = 0;
1441 }
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001442 }
1443 if (csinfo == NULL)
1444 return -1;
1445 for (j = csinfo_size/2; j < csinfo_size; j++)
1446 clear_csinfo(j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001449 if ((csinfo[i].fname = alloc(strlen(fname)+1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return -1;
1451
1452 (void)strcpy(csinfo[i].fname, (const char *)fname);
1453
1454 if (ppath != NULL)
1455 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001456 if ((csinfo[i].ppath = alloc(strlen(ppath) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001458 VIM_CLEAR(csinfo[i].fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return -1;
1460 }
1461 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1462 } else
1463 csinfo[i].ppath = NULL;
1464
1465 if (flags != NULL)
1466 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001467 if ((csinfo[i].flags = alloc(strlen(flags) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001469 VIM_CLEAR(csinfo[i].fname);
1470 VIM_CLEAR(csinfo[i].ppath);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return -1;
1472 }
1473 (void)strcpy(csinfo[i].flags, (const char *)flags);
1474 } else
1475 csinfo[i].flags = NULL;
1476
1477#if defined(UNIX)
1478 csinfo[i].st_dev = sb->st_dev;
1479 csinfo[i].st_ino = sb->st_ino;
1480
1481#else
1482 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1483 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1484 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1485#endif
1486 return i;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489
1490/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001491 * Find cscope command in command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 */
1493 static cscmd_T *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001494cs_lookup_cmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 cscmd_T *cmdp;
1497 char *stok;
1498 size_t len;
1499
1500 if (eap->arg == NULL)
1501 return NULL;
1502
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001503 // Store length of eap->arg before it gets modified by strtok().
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001504 eap_arg_len = (int)STRLEN(eap->arg);
Bram Moolenaard2ac9842007-08-21 16:03:51 +00001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1507 return NULL;
1508
1509 len = strlen(stok);
1510 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1511 {
1512 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1513 return (cmdp);
1514 }
1515 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518
1519/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001520 * Nuke em.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001523cs_kill(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 char *stok;
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00001526 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1529 {
1530 cs_usage_msg(Kill);
1531 return CSCOPE_FAILURE;
1532 }
1533
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001534 // only single digit positive and negative integers are allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1536 || (strlen(stok) < 3 && stok[0] == '-'
1537 && VIM_ISDIGIT((int)(stok[1]))))
1538 i = atoi(stok);
1539 else
1540 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001541 // It must be part of a name. We will try to find a match
1542 // within all the names in the csinfo data structure
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001543 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
1545 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1546 break;
1547 }
1548 }
1549
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001550 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 if (p_csverbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001553 (void)semsg(_(e_cscope_connection_str_not_founc), stok);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
1555 else
1556 {
1557 if (i == -1)
1558 {
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001559 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 if (csinfo[i].fname)
1562 cs_kill_execute(i, csinfo[i].fname);
1563 }
1564 }
1565 else
1566 cs_kill_execute(i, stok);
1567 }
1568
1569 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572
1573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 * Actually kills a specific cscope connection.
1575 */
1576 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001577cs_kill_execute(
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001578 int i, // cscope table index
1579 char *cname) // cscope database name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 if (p_csverbose)
1582 {
1583 msg_clr_eos();
Bram Moolenaar8820b482017-03-16 17:23:31 +01001584 (void)smsg_attr(HL_ATTR(HLF_R) | MSG_HIST,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001585 _("cscope connection %s closed"), cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587 cs_release_csp(i, TRUE);
1588}
1589
1590
1591/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001592 * Convert the cscope output into a ctags style entry (as might be found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1594 * the type of the tag you are looking for. for example, in Darren Hiebert's
1595 * ctags (the one that comes with vim), #define's use a line number to find the
1596 * tag in a file while function definitions use a regexp search pattern.
1597 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001598 * I'm going to always use the line number because cscope does something
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 * quirky (and probably other things i don't know about):
1600 *
1601 * if you have "# define" in your source file, which is
1602 * perfectly legal, cscope thinks you have "#define". this
1603 * will result in a failed regexp search. :(
1604 *
Bram Moolenaard4db7712016-11-12 19:16:46 +01001605 * Besides, even if this particular case didn't happen, the search pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 * would still have to be modified to escape all the special regular expression
1607 * characters to comply with ctags formatting.
1608 */
1609 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001610cs_make_vim_style_matches(
1611 char *fname,
1612 char *slno,
1613 char *search,
1614 char *tagstr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001616 // vim style is ctags:
1617 //
1618 // <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1619 //
1620 // but as mentioned above, we'll always use the line number and
1621 // put the search pattern (if one exists) as "extra"
1622 //
1623 // buf is used as part of vim's method of handling tags, and
1624 // (i think) vim frees it when you pop your tags and get replaced
1625 // by new ones on the tag stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 char *buf;
1627 int amt;
1628
1629 if (search != NULL)
1630 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001631 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001632 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 return NULL;
1634
1635 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1636 }
1637 else
1638 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001639 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001640 if ((buf = alloc(amt)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 return NULL;
1642
1643 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1644 }
1645
1646 return buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649
1650/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001651 * This is kind of hokey, but i don't see an easy way round this.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 *
1653 * Store: keep a ptr to the (malloc'd) memory of matches originally
1654 * generated from cs_find(). the matches are originally lines directly
1655 * from cscope output, but transformed to look like something out of a
1656 * ctags. see cs_make_vim_style_matches for more details.
1657 *
1658 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1659 * the next line from the cscope output. it basically keeps track of which
1660 * lines have been "used" and returns the next one.
1661 *
1662 * Free: frees up everything and resets
1663 *
1664 * Print: prints the tags
1665 */
1666 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001667cs_manage_matches(
1668 char **matches,
1669 char **contexts,
1670 int totmatches,
1671 mcmd_e cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 static char **mp = NULL;
1674 static char **cp = NULL;
1675 static int cnt = -1;
1676 static int next = -1;
1677 char *p = NULL;
1678
1679 switch (cmd)
1680 {
1681 case Store:
1682 assert(matches != NULL);
1683 assert(totmatches > 0);
1684 if (mp != NULL || cp != NULL)
1685 (void)cs_manage_matches(NULL, NULL, -1, Free);
1686 mp = matches;
1687 cp = contexts;
1688 cnt = totmatches;
1689 next = 0;
1690 break;
1691 case Get:
1692 if (next >= cnt)
1693 return NULL;
1694
1695 p = mp[next];
1696 next++;
1697 break;
1698 case Free:
1699 if (mp != NULL)
1700 {
1701 if (cnt > 0)
1702 while (cnt--)
1703 {
1704 vim_free(mp[cnt]);
1705 if (cp != NULL)
1706 vim_free(cp[cnt]);
1707 }
1708 vim_free(mp);
1709 vim_free(cp);
1710 }
1711 mp = NULL;
1712 cp = NULL;
1713 cnt = 0;
1714 next = 0;
1715 break;
1716 case Print:
1717 cs_print_tags_priv(mp, cp, cnt);
1718 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001719 default: // should not reach here
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001720 iemsg(_(e_fatal_error_in_cs_manage_matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 return NULL;
1722 }
1723
1724 return p;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001725}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727
1728/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001729 * Parse cscope output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 */
1731 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001732cs_parse_results(
1733 int cnumber,
1734 char *buf,
1735 int bufsize,
1736 char **context,
1737 char **linenumber,
1738 char **search)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 int ch;
1741 char *p;
1742 char *name;
1743
1744 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1745 {
1746 if (feof(csinfo[cnumber].fr_fp))
1747 errno = EIO;
1748
1749 cs_reading_emsg(cnumber);
1750
1751 return NULL;
1752 }
1753
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001754 // If the line's too long for the buffer, discard it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if ((p = strchr(buf, '\n')) == NULL)
1756 {
1757 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1758 ;
1759 return NULL;
1760 }
1761 *p = '\0';
1762
1763 /*
1764 * cscope output is in the following format:
1765 *
1766 * <filename> <context> <line number> <pattern>
1767 */
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001768 if ((name = strtok(buf, (const char *)" ")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 return NULL;
1770 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1771 return NULL;
1772 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1773 return NULL;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001774 *search = *linenumber + strlen(*linenumber) + 1; // +1 to skip \0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 // --- nvi ---
1777 // If the file is older than the cscope database, that is,
1778 // the database was built since the file was last modified,
1779 // or there wasn't a search string, use the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (strcmp(*search, "<unknown>") == 0)
1781 *search = NULL;
1782
1783 name = cs_resolve_file(cnumber, name);
1784 return name;
1785}
1786
Bram Moolenaarc716c302006-01-21 22:12:51 +00001787#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001789 * Write cscope find results to file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001792cs_file_results(FILE *f, int *nummatches_a)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 int i, j;
1795 char *buf;
1796 char *search, *slno;
1797 char *fullname;
1798 char *cntx;
1799 char *context;
1800
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001801 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (buf == NULL)
1803 return;
1804
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001805 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 if (nummatches_a[i] < 1)
1808 continue;
1809
1810 for (j = 0; j < nummatches_a[i]; j++)
1811 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001812 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1813 &slno, &search)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 continue;
1815
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001816 context = alloc(strlen(cntx)+5);
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001817 if (context == NULL)
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001818 {
1819 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 continue;
Bram Moolenaar4dba0422021-02-03 19:35:13 +01001821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 if (strcmp(cntx, "<global>")==0)
1824 strcpy(context, "<<global>>");
1825 else
1826 sprintf(context, "<<%s>>", cntx);
1827
Bram Moolenaar0cae8472006-10-30 21:32:28 +00001828 if (search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1830 else
1831 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1832
1833 vim_free(context);
1834 vim_free(fullname);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001835 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837 (void)cs_read_prompt(i);
1838
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001839 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 vim_free(buf);
1841}
Bram Moolenaarc716c302006-01-21 22:12:51 +00001842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001845 * Get parsed cscope output and calls cs_make_vim_style_matches to convert
1846 * into ctags format.
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001847 * When there are no matches sets "*matches_p" to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001850cs_fill_results(
1851 char *tagstr,
1852 int totmatches,
1853 int *nummatches_a,
1854 char ***matches_p,
1855 char ***cntxts_p,
1856 int *matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int i, j;
1859 char *buf;
1860 char *search, *slno;
1861 int totsofar = 0;
1862 char **matches = NULL;
1863 char **cntxts = NULL;
1864 char *fullname;
1865 char *cntx;
1866
1867 assert(totmatches > 0);
1868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001869 buf = alloc(CSREAD_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (buf == NULL)
1871 return;
1872
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001873 if ((matches = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 goto parse_out;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001875 if ((cntxts = ALLOC_MULT(char *, totmatches)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 goto parse_out;
1877
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00001878 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
1880 if (nummatches_a[i] < 1)
1881 continue;
1882
1883 for (j = 0; j < nummatches_a[i]; j++)
1884 {
1885 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1886 &slno, &search)) == NULL)
1887 continue;
1888
1889 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1890 search, tagstr);
1891
1892 vim_free(fullname);
1893
1894 if (strcmp(cntx, "<global>") == 0)
1895 cntxts[totsofar] = NULL;
1896 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001897 // note: if vim_strsave returns NULL, then the context
1898 // will be "<global>", which is misleading.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1900
1901 if (matches[totsofar] != NULL)
1902 totsofar++;
1903
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001904 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 (void)cs_read_prompt(i);
1907
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001908 } // for all cscope connections
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910parse_out:
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001911 if (totsofar == 0)
1912 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001913 // No matches, free the arrays and return NULL in "*matches_p".
Bram Moolenaard23a8232018-02-10 18:45:26 +01001914 VIM_CLEAR(matches);
1915 VIM_CLEAR(cntxts);
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 *matched = totsofar;
1918 *matches_p = matches;
1919 *cntxts_p = cntxts;
Bram Moolenaard6f676d2005-06-01 21:51:55 +00001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001925/*
1926 * get the requested path components
1927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 static char *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001929cs_pathcomponents(char *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 int i;
1932 char *s;
1933
1934 if (p_cspc == 0)
1935 return path;
1936
1937 s = path + strlen(path) - 1;
1938 for (i = 0; i < p_cspc; ++i)
1939 while (s > path && *--s != '/'
Bram Moolenaar4f974752019-02-17 17:44:42 +01001940#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 && *--s != '\\'
1942#endif
1943 )
1944 ;
1945 if ((s > path && *s == '/')
Bram Moolenaar4f974752019-02-17 17:44:42 +01001946#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 || (s > path && *s == '\\')
1948#endif
1949 )
1950 ++s;
1951 return s;
1952}
1953
1954/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01001955 * Called from cs_manage_matches().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
1957 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001958cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 char *buf = NULL;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001961 char *t_buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001962 int bufsize = 0; // Track available bufsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 int newsize = 0;
1964 char *ptag;
1965 char *fname, *lno, *extra, *tbuf;
1966 int i, idx, num;
1967 char *globalcntx = "GLOBAL";
1968 char *cntxformat = " <<%s>>";
1969 char *context;
1970 char *cstag_msg = _("Cscope tag: %s");
1971 char *csfmt_str = "%4d %6s ";
1972
Bram Moolenaar4033c552017-09-16 20:54:51 +02001973 assert(num_matches > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001975 if ((tbuf = alloc(strlen(matches[0]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 return;
1977
1978 strcpy(tbuf, matches[0]);
1979 ptag = strtok(tbuf, "\t");
Bram Moolenaarcde88542015-08-11 19:14:00 +02001980 if (ptag == NULL)
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001981 {
1982 vim_free(tbuf);
Bram Moolenaarcde88542015-08-11 19:14:00 +02001983 return;
Bram Moolenaar42dd7ae2016-02-23 22:50:12 +01001984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001986 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001987 buf = alloc(newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (buf != NULL)
1989 {
1990 bufsize = newsize;
1991 (void)sprintf(buf, cstag_msg, ptag);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001992 msg_puts_attr(buf, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994
1995 vim_free(tbuf);
1996
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001997 msg_puts_attr(_("\n # line"), HL_ATTR(HLF_T)); // strlen is 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 msg_advance(msg_col + 2);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001999 msg_puts_attr(_("filename / context / line\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 num = 1;
2002 for (i = 0; i < num_matches; i++)
2003 {
2004 idx = i;
2005
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002006 // if we really wanted to, we could avoid this malloc and strcpy
2007 // by parsing matches[i] on the fly and placing stuff into buf
2008 // directly, but that's too much of a hassle
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002009 if ((tbuf = alloc(strlen(matches[idx]) + 1)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 continue;
2011 (void)strcpy(tbuf, matches[idx]);
2012
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002013 if (strtok(tbuf, (const char *)"\t") == NULL
2014 || (fname = strtok(NULL, (const char *)"\t")) == NULL
2015 || (lno = strtok(NULL, (const char *)"\t")) == NULL)
2016 {
2017 vim_free(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 continue;
Bram Moolenaare16e5a92016-02-23 20:44:08 +01002019 }
Bram Moolenaarf2a4e332007-02-27 17:08:16 +00002020 extra = strtok(NULL, (const char *)"\t");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022 lno[strlen(lno)-2] = '\0'; // ignore ;" at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002024 // hopefully 'num' (num of matches) will be less than 10^16
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002025 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (bufsize < newsize)
2027 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002028 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002029 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002033 vim_free(t_buf);
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 else
2036 bufsize = newsize;
2037 }
2038 if (buf != NULL)
2039 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002040 // csfmt_str = "%4d %6s ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 (void)sprintf(buf, csfmt_str, num, lno);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002042 msg_puts_attr(buf, HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002044 msg_outtrans_long_attr((char_u *)cs_pathcomponents(fname),
2045 HL_ATTR(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002047 // compute the required space for the context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (cntxts[idx] != NULL)
2049 context = cntxts[idx];
2050 else
2051 context = globalcntx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002052 newsize = (int)(strlen(context) + strlen(cntxformat));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 if (bufsize < newsize)
2055 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002056 t_buf = buf;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002057 buf = vim_realloc(buf, newsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (buf == NULL)
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 bufsize = 0;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01002061 vim_free(t_buf);
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else
2064 bufsize = newsize;
2065 }
2066 if (buf != NULL)
2067 {
2068 (void)sprintf(buf, cntxformat, context);
2069
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002070 // print the context only if it fits on the same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (msg_col + (int)strlen(buf) >= (int)Columns)
2072 msg_putchar('\n');
2073 msg_advance(12);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002074 msg_outtrans_long_attr((char_u *)buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 msg_putchar('\n');
2076 }
2077 if (extra != NULL)
2078 {
2079 msg_advance(13);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002080 msg_outtrans_long_attr((char_u *)extra, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002083 vim_free(tbuf); // only after printing extra due to strtok use
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 if (msg_col)
2086 msg_putchar('\n');
2087
2088 ui_breakcheck();
2089 if (got_int)
2090 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002091 got_int = FALSE; // don't print any more matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 break;
2093 }
2094
2095 num++;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002096 } // for all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098 vim_free(buf);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
2101
2102/*
Bram Moolenaard4db7712016-11-12 19:16:46 +01002103 * Read a cscope prompt (basically, skip over the ">> ").
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 */
2105 static int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002106cs_read_prompt(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108 int ch;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002109 char *buf = NULL; // buffer for possible error message from cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 int bufpos = 0;
2111 char *cs_emsg;
2112 int maxlen;
2113 static char *eprompt = "Press the RETURN key to continue:";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 int epromptlen = (int)strlen(eprompt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int n;
2116
Bram Moolenaard88be5b2022-01-04 19:57:55 +00002117 cs_emsg = _(e_cscope_error_str);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002118 // compute maximum allowed len for Cscope error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2120
2121 for (;;)
2122 {
2123 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002124 // if there is room and char is printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2126 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002127 if (buf == NULL) // lazy buffer allocation
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002128 buf = alloc(maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (buf != NULL)
2130 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131 // append character to the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 buf[bufpos++] = ch;
2133 buf[bufpos] = NUL;
2134 if (bufpos >= epromptlen
2135 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2136 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002137 // remove eprompt from buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 buf[bufpos - epromptlen] = NUL;
2139
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002140 // print message to user
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002143 // send RETURN to cscope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 (void)putc('\n', csinfo[i].to_fp);
2145 (void)fflush(csinfo[i].to_fp);
2146
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002147 // clear buf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 bufpos = 0;
2149 buf[bufpos] = NUL;
2150 }
2151 }
2152 }
2153
2154 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2155 {
2156 if (n > 0)
2157 ch = getc(csinfo[i].fr_fp);
2158 if (ch == EOF)
2159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (buf != NULL && buf[0] != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 (void)semsg(cs_emsg, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else if (p_csverbose)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002163 cs_reading_emsg(i); // don't have additional information
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 cs_release_csp(i, TRUE);
2165 vim_free(buf);
2166 return CSCOPE_FAILURE;
2167 }
2168
2169 if (ch != CSCOPE_PROMPT[n])
2170 {
2171 ch = EOF;
2172 break;
2173 }
2174 }
2175
2176 if (ch == EOF)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002177 continue; // didn't find the prompt
2178 break; // did find the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180
2181 vim_free(buf);
2182 return CSCOPE_SUCCESS;
2183}
2184
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002185#if defined(UNIX) && defined(SIGALRM)
2186/*
2187 * Used to catch and ignore SIGALRM below.
2188 */
Bram Moolenaar99c48fe2022-06-05 22:05:19 +01002189 static void
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002190sig_handler SIGDEFARG(sigarg)
2191{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002192 // do nothing
Bram Moolenaar7dc767c2008-03-15 11:41:07 +00002193}
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{
=?UTF-8?q?Dundar=20G=C3=B6c?=f12b7812022-01-29 15:12:39 +00002459 int i;
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (cs_cnt_connections() == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002462 msg_puts(_("no cscope connections\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 else
2464 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002465 msg_puts_attr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 _(" # pid database name prepend path\n"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002467 HL_ATTR(HLF_T));
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002468 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 if (csinfo[i].fname == NULL)
2471 continue;
2472
2473 if (csinfo[i].ppath != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 (void)smsg("%2d %-5ld %-34s %-32s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2476 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002477 (void)smsg("%2d %-5ld %-34s <none>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 i, (long)csinfo[i].pid, csinfo[i].fname);
2479 }
2480 }
2481
Bram Moolenaar13608d82022-08-29 15:06:50 +01002482 wait_return(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return CSCOPE_SUCCESS;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaar02b06312007-09-06 15:39:22 +00002486
2487/*
Bram Moolenaar02b06312007-09-06 15:39:22 +00002488 * Only called when VIM exits to quit any cscope sessions.
2489 */
2490 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01002491cs_end(void)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002492{
2493 int i;
2494
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002495 for (i = 0; i < csinfo_size; i++)
Bram Moolenaar02b06312007-09-06 15:39:22 +00002496 cs_release_csp(i, TRUE);
Bram Moolenaar9fa49da2009-07-10 13:11:26 +00002497 vim_free(csinfo);
2498 csinfo_size = 0;
Bram Moolenaar02b06312007-09-06 15:39:22 +00002499}
2500
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002501#endif // FEAT_CSCOPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002503#if defined(FEAT_EVAL) || defined(PROTO)
2504
2505/*
2506 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2507 *
2508 * Checks the existence of a cscope connection.
2509 */
2510 void
2511f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2512{
2513# ifdef FEAT_CSCOPE
2514 int num = 0;
2515 char_u *dbpath = NULL;
2516 char_u *prepend = NULL;
2517 char_u buf[NUMBUFLEN];
2518
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02002519 if (in_vim9script()
2520 && (check_for_opt_number_arg(argvars, 0) == FAIL
2521 || (argvars[0].v_type != VAR_UNKNOWN
2522 && (check_for_opt_string_arg(argvars, 1) == FAIL
2523 || (argvars[1].v_type != VAR_UNKNOWN
2524 && check_for_opt_string_arg(argvars, 2) == FAIL)))))
2525 return;
2526
Bram Moolenaar6f72e902019-09-05 23:04:02 +02002527 if (argvars[0].v_type != VAR_UNKNOWN
2528 && argvars[1].v_type != VAR_UNKNOWN)
2529 {
2530 num = (int)tv_get_number(&argvars[0]);
2531 dbpath = tv_get_string(&argvars[1]);
2532 if (argvars[2].v_type != VAR_UNKNOWN)
2533 prepend = tv_get_string_buf(&argvars[2], buf);
2534 }
2535
2536 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2537# endif
2538}
2539
2540#endif // FEAT_EVAL