7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
|
148
|
4 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
|
7
|
5 *
|
|
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
|
|
16 #include <string.h>
|
|
17 #include <errno.h>
|
|
18 #include <assert.h>
|
|
19 #include <sys/types.h>
|
|
20 #include <sys/stat.h>
|
|
21 #if defined(UNIX)
|
|
22 # include <sys/wait.h>
|
|
23 #else
|
|
24 /* not UNIX, must be WIN32 */
|
714
|
25 # include "vimio.h"
|
7
|
26 #endif
|
|
27 #include "if_cscope.h"
|
|
28
|
|
29 static void cs_usage_msg __ARGS((csid_e x));
|
|
30 static int cs_add __ARGS((exarg_T *eap));
|
|
31 static void cs_stat_emsg __ARGS((char *fname));
|
|
32 static int cs_add_common __ARGS((char *, char *, char *));
|
|
33 static int cs_check_for_connections __ARGS((void));
|
|
34 static int cs_check_for_tags __ARGS((void));
|
|
35 static int cs_cnt_connections __ARGS((void));
|
|
36 static void cs_reading_emsg __ARGS((int idx));
|
|
37 static int cs_cnt_matches __ARGS((int idx));
|
|
38 static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
|
|
39 static int cs_create_connection __ARGS((int i));
|
|
40 static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
|
636
|
41 #ifdef FEAT_QUICKFIX
|
7
|
42 static void cs_file_results __ARGS((FILE *, int *));
|
636
|
43 #endif
|
7
|
44 static void cs_fill_results __ARGS((char *, int , int *, char ***,
|
|
45 char ***, int *));
|
|
46 static int cs_find __ARGS((exarg_T *eap));
|
665
|
47 static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
|
7
|
48 static int cs_help __ARGS((exarg_T *eap));
|
|
49 static void cs_init __ARGS((void));
|
|
50 static void clear_csinfo __ARGS((int i));
|
|
51 static int cs_insert_filelist __ARGS((char *, char *, char *,
|
|
52 struct stat *));
|
|
53 static int cs_kill __ARGS((exarg_T *eap));
|
|
54 static void cs_kill_execute __ARGS((int, char *));
|
|
55 static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
|
|
56 static char * cs_make_vim_style_matches __ARGS((char *, char *,
|
|
57 char *, char *));
|
|
58 static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
|
|
59 static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
|
|
60 static char * cs_pathcomponents __ARGS((char *path));
|
|
61 static void cs_print_tags_priv __ARGS((char **, char **, int));
|
1385
|
62 static int cs_read_prompt __ARGS((int));
|
7
|
63 static void cs_release_csp __ARGS((int, int freefnpp));
|
|
64 static int cs_reset __ARGS((exarg_T *eap));
|
|
65 static char * cs_resolve_file __ARGS((int, char *));
|
|
66 static int cs_show __ARGS((exarg_T *eap));
|
|
67
|
|
68
|
|
69 static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS];
|
1372
|
70 static int eap_arg_len; /* length of eap->arg, set in
|
|
71 cs_lookup_cmd() */
|
7
|
72 static cscmd_T cs_cmds[] =
|
|
73 {
|
|
74 { "add", cs_add,
|
|
75 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
|
|
76 { "find", cs_find,
|
1706
|
77 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
|
7
|
78 { "help", cs_help,
|
|
79 N_("Show this message"), "help", 0 },
|
|
80 { "kill", cs_kill,
|
|
81 N_("Kill a connection"), "kill #", 0 },
|
|
82 { "reset", cs_reset,
|
|
83 N_("Reinit all connections"), "reset", 0 },
|
|
84 { "show", cs_show,
|
|
85 N_("Show connections"), "show", 0 },
|
1880
|
86 { NULL, NULL, NULL, NULL, 0 }
|
7
|
87 };
|
|
88
|
|
89 static void
|
|
90 cs_usage_msg(x)
|
|
91 csid_e x;
|
|
92 {
|
|
93 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
|
|
94 }
|
|
95
|
1845
|
96 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
|
|
97
|
|
98 static enum
|
|
99 {
|
|
100 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
|
1858
|
101 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
|
1845
|
102 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
|
|
103 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
|
|
104 } expand_what;
|
|
105
|
|
106 /*
|
|
107 * Function given to ExpandGeneric() to obtain the cscope command
|
|
108 * expansion.
|
|
109 */
|
|
110 char_u *
|
|
111 get_cscope_name(xp, idx)
|
1880
|
112 expand_T *xp UNUSED;
|
1845
|
113 int idx;
|
|
114 {
|
1858
|
115 int current_idx;
|
|
116 int i;
|
|
117
|
1845
|
118 switch (expand_what)
|
|
119 {
|
|
120 case EXP_CSCOPE_SUBCMD:
|
|
121 /* Complete with sub-commands of ":cscope":
|
|
122 * add, find, help, kill, reset, show */
|
|
123 return (char_u *)cs_cmds[idx].name;
|
1858
|
124 case EXP_SCSCOPE_SUBCMD:
|
|
125 /* Complete with sub-commands of ":scscope": same sub-commands as
|
|
126 * ":cscope" but skip commands which don't support split windows */
|
|
127 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
|
|
128 if (cs_cmds[i].cansplit)
|
|
129 if (current_idx++ == idx)
|
|
130 break;
|
|
131 return (char_u *)cs_cmds[i].name;
|
1845
|
132 case EXP_CSCOPE_FIND:
|
|
133 {
|
|
134 const char *query_type[] =
|
|
135 {
|
|
136 "c", "d", "e", "f", "g", "i", "s", "t", NULL
|
|
137 };
|
|
138
|
|
139 /* Complete with query type of ":cscope find {query_type}".
|
|
140 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
|
|
141 * ..., 8) but only complete with letters, since numbers are
|
|
142 * redundant. */
|
|
143 return (char_u *)query_type[idx];
|
|
144 }
|
|
145 case EXP_CSCOPE_KILL:
|
|
146 {
|
|
147 static char_u connection[2];
|
|
148
|
|
149 /* ":cscope kill" accepts connection numbers or partial names of
|
|
150 * the pathname of the cscope database as argument. Only complete
|
|
151 * with connection numbers. -1 can also be used to kill all
|
|
152 * connections. */
|
1858
|
153 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
1845
|
154 {
|
|
155 if (csinfo[i].fname == NULL)
|
|
156 continue;
|
|
157 if (current_idx++ == idx)
|
|
158 {
|
|
159 /* Connection number fits in one character since
|
|
160 * CSCOPE_MAX_CONNECTIONS is < 10 */
|
|
161 connection[0] = i + '0';
|
|
162 connection[1] = NUL;
|
|
163 return connection;
|
|
164 }
|
|
165 }
|
|
166 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
|
|
167 }
|
|
168 default:
|
|
169 return NULL;
|
|
170 }
|
|
171 }
|
|
172
|
|
173 /*
|
|
174 * Handle command line completion for :cscope command.
|
|
175 */
|
|
176 void
|
1858
|
177 set_context_in_cscope_cmd(xp, arg, cmdidx)
|
1845
|
178 expand_T *xp;
|
|
179 char_u *arg;
|
1858
|
180 cmdidx_T cmdidx;
|
1845
|
181 {
|
|
182 char_u *p;
|
|
183
|
|
184 /* Default: expand subcommands */
|
|
185 xp->xp_context = EXPAND_CSCOPE;
|
|
186 xp->xp_pattern = arg;
|
1858
|
187 expand_what = (cmdidx == CMD_scscope)
|
|
188 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
|
1845
|
189
|
|
190 /* (part of) subcommand already typed */
|
|
191 if (*arg != NUL)
|
|
192 {
|
|
193 p = skiptowhite(arg);
|
|
194 if (*p != NUL) /* past first word */
|
|
195 {
|
|
196 xp->xp_pattern = skipwhite(p);
|
|
197 if (*skiptowhite(xp->xp_pattern) != NUL)
|
|
198 xp->xp_context = EXPAND_NOTHING;
|
|
199 else if (STRNICMP(arg, "add", p - arg) == 0)
|
|
200 xp->xp_context = EXPAND_FILES;
|
|
201 else if (STRNICMP(arg, "kill", p - arg) == 0)
|
|
202 expand_what = EXP_CSCOPE_KILL;
|
|
203 else if (STRNICMP(arg, "find", p - arg) == 0)
|
|
204 expand_what = EXP_CSCOPE_FIND;
|
|
205 else
|
|
206 xp->xp_context = EXPAND_NOTHING;
|
|
207 }
|
|
208 }
|
|
209 }
|
|
210
|
|
211 #endif /* FEAT_CMDL_COMPL */
|
|
212
|
7
|
213 /*
|
|
214 * PRIVATE: do_cscope_general
|
|
215 *
|
1845
|
216 * Find the command, print help if invalid, and then call the corresponding
|
|
217 * command function.
|
7
|
218 */
|
|
219 static void
|
|
220 do_cscope_general(eap, make_split)
|
|
221 exarg_T *eap;
|
|
222 int make_split; /* whether to split window */
|
|
223 {
|
|
224 cscmd_T *cmdp;
|
|
225
|
|
226 cs_init();
|
|
227 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
|
|
228 {
|
|
229 cs_help(eap);
|
|
230 return;
|
|
231 }
|
|
232
|
|
233 #ifdef FEAT_WINDOWS
|
|
234 if (make_split)
|
|
235 {
|
|
236 if (!cmdp->cansplit)
|
|
237 {
|
|
238 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
|
|
239 return;
|
|
240 }
|
|
241 postponed_split = -1;
|
|
242 postponed_split_flags = cmdmod.split;
|
1090
|
243 postponed_split_tab = cmdmod.tab;
|
7
|
244 }
|
|
245 #endif
|
|
246
|
|
247 cmdp->func(eap);
|
|
248
|
|
249 #ifdef FEAT_WINDOWS
|
|
250 postponed_split_flags = 0;
|
1090
|
251 postponed_split_tab = 0;
|
7
|
252 #endif
|
|
253 }
|
|
254
|
|
255 /*
|
|
256 * PUBLIC: do_cscope
|
|
257 */
|
|
258 void
|
|
259 do_cscope(eap)
|
|
260 exarg_T *eap;
|
|
261 {
|
|
262 do_cscope_general(eap, FALSE);
|
|
263 }
|
|
264
|
|
265 /*
|
|
266 * PUBLIC: do_scscope
|
|
267 *
|
|
268 * same as do_cscope, but splits window, too.
|
|
269 */
|
|
270 void
|
|
271 do_scscope(eap)
|
|
272 exarg_T *eap;
|
|
273 {
|
|
274 do_cscope_general(eap, TRUE);
|
|
275 }
|
|
276
|
|
277 /*
|
|
278 * PUBLIC: do_cstag
|
|
279 *
|
|
280 */
|
|
281 void
|
|
282 do_cstag(eap)
|
|
283 exarg_T *eap;
|
|
284 {
|
|
285 int ret = FALSE;
|
|
286
|
|
287 cs_init();
|
|
288
|
1621
|
289 if (*eap->arg == NUL)
|
7
|
290 {
|
|
291 (void)EMSG(_("E562: Usage: cstag <ident>"));
|
|
292 return;
|
|
293 }
|
|
294
|
|
295 switch (p_csto)
|
|
296 {
|
|
297 case 0 :
|
|
298 if (cs_check_for_connections())
|
|
299 {
|
665
|
300 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
|
|
301 FALSE);
|
7
|
302 if (ret == FALSE)
|
|
303 {
|
|
304 cs_free_tags();
|
|
305 if (msg_col)
|
|
306 msg_putchar('\n');
|
|
307
|
|
308 if (cs_check_for_tags())
|
|
309 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
|
|
310 }
|
|
311 }
|
|
312 else if (cs_check_for_tags())
|
|
313 {
|
|
314 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
|
|
315 }
|
|
316 break;
|
|
317 case 1 :
|
|
318 if (cs_check_for_tags())
|
|
319 {
|
|
320 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
|
|
321 if (ret == FALSE)
|
|
322 {
|
|
323 if (msg_col)
|
|
324 msg_putchar('\n');
|
|
325
|
|
326 if (cs_check_for_connections())
|
|
327 {
|
|
328 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
|
665
|
329 FALSE, FALSE);
|
7
|
330 if (ret == FALSE)
|
|
331 cs_free_tags();
|
|
332 }
|
|
333 }
|
|
334 }
|
|
335 else if (cs_check_for_connections())
|
|
336 {
|
665
|
337 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
|
|
338 FALSE);
|
7
|
339 if (ret == FALSE)
|
|
340 cs_free_tags();
|
|
341 }
|
|
342 break;
|
|
343 default :
|
|
344 break;
|
|
345 }
|
|
346
|
|
347 if (!ret)
|
|
348 {
|
|
349 (void)EMSG(_("E257: cstag: tag not found"));
|
|
350 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
|
|
351 g_do_tagpreview = 0;
|
|
352 #endif
|
|
353 }
|
|
354
|
|
355 } /* do_cscope */
|
|
356
|
|
357
|
|
358 /*
|
|
359 * PUBLIC: cs_find
|
|
360 *
|
|
361 * this simulates a vim_fgets(), but for cscope, returns the next line
|
|
362 * from the cscope output. should only be called from find_tags()
|
|
363 *
|
|
364 * returns TRUE if eof, FALSE otherwise
|
|
365 */
|
|
366 int
|
|
367 cs_fgets(buf, size)
|
|
368 char_u *buf;
|
|
369 int size;
|
|
370 {
|
|
371 char *p;
|
|
372
|
|
373 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
|
|
374 return TRUE;
|
1372
|
375 vim_strncpy(buf, (char_u *)p, size - 1);
|
7
|
376
|
|
377 return FALSE;
|
|
378 } /* cs_fgets */
|
|
379
|
|
380
|
|
381 /*
|
|
382 * PUBLIC: cs_free_tags
|
|
383 *
|
|
384 * called only from do_tag(), when popping the tag stack
|
|
385 */
|
|
386 void
|
|
387 cs_free_tags()
|
|
388 {
|
|
389 cs_manage_matches(NULL, NULL, -1, Free);
|
|
390 }
|
|
391
|
|
392
|
|
393 /*
|
|
394 * PUBLIC: cs_print_tags
|
|
395 *
|
|
396 * called from do_tag()
|
|
397 */
|
|
398 void
|
|
399 cs_print_tags()
|
|
400 {
|
|
401 cs_manage_matches(NULL, NULL, -1, Print);
|
|
402 }
|
|
403
|
|
404
|
|
405 /*
|
|
406 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
|
|
407 *
|
|
408 * Checks for the existence of a |cscope| connection. If no
|
|
409 * parameters are specified, then the function returns:
|
|
410 *
|
|
411 * 0, if cscope was not available (not compiled in), or if there
|
|
412 * are no cscope connections; or
|
|
413 * 1, if there is at least one cscope connection.
|
|
414 *
|
|
415 * If parameters are specified, then the value of {num}
|
|
416 * determines how existence of a cscope connection is checked:
|
|
417 *
|
|
418 * {num} Description of existence check
|
|
419 * ----- ------------------------------
|
|
420 * 0 Same as no parameters (e.g., "cscope_connection()").
|
|
421 * 1 Ignore {prepend}, and use partial string matches for
|
|
422 * {dbpath}.
|
|
423 * 2 Ignore {prepend}, and use exact string matches for
|
|
424 * {dbpath}.
|
|
425 * 3 Use {prepend}, use partial string matches for both
|
|
426 * {dbpath} and {prepend}.
|
|
427 * 4 Use {prepend}, use exact string matches for both
|
|
428 * {dbpath} and {prepend}.
|
|
429 *
|
|
430 * Note: All string comparisons are case sensitive!
|
|
431 */
|
|
432 #if defined(FEAT_EVAL) || defined(PROTO)
|
|
433 int
|
|
434 cs_connection(num, dbpath, ppath)
|
|
435 int num;
|
|
436 char_u *dbpath;
|
|
437 char_u *ppath;
|
|
438 {
|
|
439 int i;
|
|
440
|
|
441 if (num < 0 || num > 4 || (num > 0 && !dbpath))
|
|
442 return FALSE;
|
|
443
|
|
444 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
445 {
|
|
446 if (!csinfo[i].fname)
|
|
447 continue;
|
|
448
|
|
449 if (num == 0)
|
|
450 return TRUE;
|
|
451
|
|
452 switch (num)
|
|
453 {
|
|
454 case 1:
|
|
455 if (strstr(csinfo[i].fname, (char *)dbpath))
|
|
456 return TRUE;
|
|
457 break;
|
|
458 case 2:
|
|
459 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
|
|
460 return TRUE;
|
|
461 break;
|
|
462 case 3:
|
|
463 if (strstr(csinfo[i].fname, (char *)dbpath)
|
|
464 && ((!ppath && !csinfo[i].ppath)
|
|
465 || (ppath
|
|
466 && csinfo[i].ppath
|
|
467 && strstr(csinfo[i].ppath, (char *)ppath))))
|
|
468 return TRUE;
|
|
469 break;
|
|
470 case 4:
|
|
471 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
|
|
472 && ((!ppath && !csinfo[i].ppath)
|
|
473 || (ppath
|
|
474 && csinfo[i].ppath
|
|
475 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
|
|
476 return TRUE;
|
|
477 break;
|
|
478 }
|
|
479 }
|
|
480
|
|
481 return FALSE;
|
|
482 } /* cs_connection */
|
|
483 #endif
|
|
484
|
|
485
|
|
486 /*
|
|
487 * PRIVATE functions
|
|
488 ****************************************************************************/
|
|
489
|
|
490 /*
|
|
491 * PRIVATE: cs_add
|
|
492 *
|
|
493 * add cscope database or a directory name (to look for cscope.out)
|
1372
|
494 * to the cscope connection list
|
7
|
495 *
|
|
496 * MAXPATHL 256
|
|
497 */
|
|
498 static int
|
|
499 cs_add(eap)
|
1880
|
500 exarg_T *eap UNUSED;
|
7
|
501 {
|
|
502 char *fname, *ppath, *flags = NULL;
|
|
503
|
|
504 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
|
|
505 {
|
|
506 cs_usage_msg(Add);
|
|
507 return CSCOPE_FAILURE;
|
|
508 }
|
|
509 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
|
|
510 flags = strtok((char *)NULL, (const char *)" ");
|
|
511
|
|
512 return cs_add_common(fname, ppath, flags);
|
|
513 }
|
|
514
|
|
515 static void
|
|
516 cs_stat_emsg(fname)
|
|
517 char *fname;
|
|
518 {
|
|
519 char *stat_emsg = _("E563: stat(%s) error: %d");
|
|
520 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
|
|
521
|
|
522 if (buf != NULL)
|
|
523 {
|
|
524 (void)sprintf(buf, stat_emsg, fname, errno);
|
|
525 (void)EMSG(buf);
|
|
526 vim_free(buf);
|
|
527 }
|
|
528 else
|
|
529 (void)EMSG(_("E563: stat error"));
|
|
530 }
|
|
531
|
|
532
|
|
533 /*
|
|
534 * PRIVATE: cs_add_common
|
|
535 *
|
|
536 * the common routine to add a new cscope connection. called by
|
|
537 * cs_add() and cs_reset(). i really don't like to do this, but this
|
|
538 * routine uses a number of goto statements.
|
|
539 */
|
|
540 static int
|
|
541 cs_add_common(arg1, arg2, flags)
|
|
542 char *arg1; /* filename - may contain environment variables */
|
|
543 char *arg2; /* prepend path - may contain environment variables */
|
|
544 char *flags;
|
|
545 {
|
|
546 struct stat statbuf;
|
12
|
547 int ret;
|
|
548 char *fname = NULL;
|
|
549 char *fname2 = NULL;
|
|
550 char *ppath = NULL;
|
|
551 int i;
|
7
|
552
|
|
553 /* get the filename (arg1), expand it, and try to stat it */
|
12
|
554 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
|
7
|
555 goto add_err;
|
|
556
|
|
557 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
|
|
558 ret = stat(fname, &statbuf);
|
|
559 if (ret < 0)
|
|
560 {
|
|
561 staterr:
|
|
562 if (p_csverbose)
|
|
563 cs_stat_emsg(fname);
|
|
564 goto add_err;
|
|
565 }
|
|
566
|
|
567 /* get the prepend path (arg2), expand it, and try to stat it */
|
|
568 if (arg2 != NULL)
|
|
569 {
|
|
570 struct stat statbuf2;
|
|
571
|
12
|
572 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
|
7
|
573 goto add_err;
|
|
574
|
|
575 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
|
|
576 ret = stat(ppath, &statbuf2);
|
|
577 if (ret < 0)
|
|
578 goto staterr;
|
|
579 }
|
|
580
|
|
581 /* if filename is a directory, append the cscope database name to it */
|
|
582 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
|
|
583 {
|
835
|
584 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
|
7
|
585 if (fname2 == NULL)
|
|
586 goto add_err;
|
|
587
|
|
588 while (fname[strlen(fname)-1] == '/'
|
|
589 #ifdef WIN32
|
|
590 || fname[strlen(fname)-1] == '\\'
|
|
591 #endif
|
|
592 )
|
|
593 {
|
|
594 fname[strlen(fname)-1] = '\0';
|
|
595 if (strlen(fname) == 0)
|
|
596 break;
|
|
597 }
|
|
598 if (fname[0] == '\0')
|
|
599 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
|
|
600 else
|
|
601 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
|
|
602
|
|
603 ret = stat(fname2, &statbuf);
|
|
604 if (ret < 0)
|
|
605 {
|
|
606 if (p_csverbose)
|
|
607 cs_stat_emsg(fname2);
|
|
608 goto add_err;
|
|
609 }
|
|
610
|
|
611 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
|
|
612 }
|
|
613 #if defined(UNIX)
|
|
614 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
|
|
615 #else
|
1385
|
616 /* WIN32 - substitute define S_ISREG from os_unix.h */
|
7
|
617 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
|
|
618 #endif
|
|
619 {
|
|
620 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
|
|
621 }
|
|
622 else
|
|
623 {
|
|
624 if (p_csverbose)
|
|
625 (void)EMSG2(
|
|
626 _("E564: %s is not a directory or a valid cscope database"),
|
|
627 fname);
|
|
628 goto add_err;
|
|
629 }
|
|
630
|
|
631 if (i != -1)
|
|
632 {
|
|
633 if (cs_create_connection(i) == CSCOPE_FAILURE
|
|
634 || cs_read_prompt(i) == CSCOPE_FAILURE)
|
|
635 {
|
|
636 cs_release_csp(i, TRUE);
|
|
637 goto add_err;
|
|
638 }
|
|
639
|
|
640 if (p_csverbose)
|
|
641 {
|
|
642 msg_clr_eos();
|
|
643 (void)smsg_attr(hl_attr(HLF_R),
|
|
644 (char_u *)_("Added cscope database %s"),
|
|
645 csinfo[i].fname);
|
|
646 }
|
|
647 }
|
|
648
|
|
649 vim_free(fname);
|
|
650 vim_free(fname2);
|
|
651 vim_free(ppath);
|
|
652 return CSCOPE_SUCCESS;
|
|
653
|
|
654 add_err:
|
|
655 vim_free(fname2);
|
|
656 vim_free(fname);
|
|
657 vim_free(ppath);
|
|
658 return CSCOPE_FAILURE;
|
|
659 } /* cs_add_common */
|
|
660
|
|
661
|
|
662 static int
|
|
663 cs_check_for_connections()
|
|
664 {
|
|
665 return (cs_cnt_connections() > 0);
|
|
666 } /* cs_check_for_connections */
|
|
667
|
|
668
|
|
669 static int
|
|
670 cs_check_for_tags()
|
|
671 {
|
301
|
672 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
|
7
|
673 } /* cs_check_for_tags */
|
|
674
|
|
675
|
|
676 /*
|
|
677 * PRIVATE: cs_cnt_connections
|
|
678 *
|
|
679 * count the number of cscope connections
|
|
680 */
|
|
681 static int
|
|
682 cs_cnt_connections()
|
|
683 {
|
|
684 short i;
|
|
685 short cnt = 0;
|
|
686
|
|
687 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
688 {
|
|
689 if (csinfo[i].fname != NULL)
|
|
690 cnt++;
|
|
691 }
|
|
692 return cnt;
|
|
693 } /* cs_cnt_connections */
|
|
694
|
|
695 static void
|
|
696 cs_reading_emsg(idx)
|
|
697 int idx; /* connection index */
|
|
698 {
|
|
699 EMSGN(_("E262: error reading cscope connection %ld"), idx);
|
|
700 }
|
|
701
|
|
702 #define CSREAD_BUFSIZE 2048
|
|
703 /*
|
|
704 * PRIVATE: cs_cnt_matches
|
|
705 *
|
|
706 * count the number of matches for a given cscope connection.
|
|
707 */
|
|
708 static int
|
|
709 cs_cnt_matches(idx)
|
|
710 int idx;
|
|
711 {
|
|
712 char *stok;
|
|
713 char *buf;
|
|
714 int nlines;
|
|
715
|
|
716 buf = (char *)alloc(CSREAD_BUFSIZE);
|
|
717 if (buf == NULL)
|
|
718 return 0;
|
|
719 for (;;)
|
|
720 {
|
|
721 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
|
|
722 {
|
|
723 if (feof(csinfo[idx].fr_fp))
|
|
724 errno = EIO;
|
|
725
|
|
726 cs_reading_emsg(idx);
|
|
727
|
|
728 vim_free(buf);
|
|
729 return -1;
|
|
730 }
|
|
731
|
|
732 /*
|
|
733 * If the database is out of date, or there's some other problem,
|
|
734 * cscope will output error messages before the number-of-lines output.
|
|
735 * Display/discard any output that doesn't match what we want.
|
1058
|
736 * Accept "\S*cscope: X lines", also matches "mlcscope".
|
7
|
737 */
|
|
738 if ((stok = strtok(buf, (const char *)" ")) == NULL)
|
|
739 continue;
|
1058
|
740 if (strstr((const char *)stok, "cscope:") == NULL)
|
7
|
741 continue;
|
|
742
|
|
743 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
|
|
744 continue;
|
|
745 nlines = atoi(stok);
|
|
746 if (nlines < 0)
|
|
747 {
|
|
748 nlines = 0;
|
|
749 break;
|
|
750 }
|
|
751
|
|
752 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
|
|
753 continue;
|
|
754 if (strncmp((const char *)stok, "lines", 5))
|
|
755 continue;
|
|
756
|
|
757 break;
|
|
758 }
|
|
759
|
|
760 vim_free(buf);
|
|
761 return nlines;
|
|
762 } /* cs_cnt_matches */
|
|
763
|
|
764
|
|
765 /*
|
|
766 * PRIVATE: cs_create_cmd
|
|
767 *
|
|
768 * Creates the actual cscope command query from what the user entered.
|
|
769 */
|
|
770 static char *
|
|
771 cs_create_cmd(csoption, pattern)
|
|
772 char *csoption;
|
|
773 char *pattern;
|
|
774 {
|
|
775 char *cmd;
|
|
776 short search;
|
1847
|
777 char *pat;
|
7
|
778
|
|
779 switch (csoption[0])
|
|
780 {
|
|
781 case '0' : case 's' :
|
|
782 search = 0;
|
|
783 break;
|
|
784 case '1' : case 'g' :
|
|
785 search = 1;
|
|
786 break;
|
|
787 case '2' : case 'd' :
|
|
788 search = 2;
|
|
789 break;
|
|
790 case '3' : case 'c' :
|
|
791 search = 3;
|
|
792 break;
|
|
793 case '4' : case 't' :
|
|
794 search = 4;
|
|
795 break;
|
|
796 case '6' : case 'e' :
|
|
797 search = 6;
|
|
798 break;
|
|
799 case '7' : case 'f' :
|
|
800 search = 7;
|
|
801 break;
|
|
802 case '8' : case 'i' :
|
|
803 search = 8;
|
|
804 break;
|
|
805 default :
|
|
806 (void)EMSG(_("E561: unknown cscope search type"));
|
|
807 cs_usage_msg(Find);
|
|
808 return NULL;
|
|
809 }
|
|
810
|
1847
|
811 /* Skip white space before the patter, except for text and pattern search,
|
|
812 * they may want to use the leading white space. */
|
|
813 pat = pattern;
|
|
814 if (search != 4 && search != 6)
|
|
815 while vim_iswhite(*pat)
|
|
816 ++pat;
|
|
817
|
|
818 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
|
7
|
819 return NULL;
|
|
820
|
1847
|
821 (void)sprintf(cmd, "%d%s", search, pat);
|
7
|
822
|
|
823 return cmd;
|
|
824 } /* cs_create_cmd */
|
|
825
|
|
826
|
|
827 /*
|
|
828 * PRIVATE: cs_create_connection
|
|
829 *
|
|
830 * This piece of code was taken/adapted from nvi. do we need to add
|
|
831 * the BSD license notice?
|
|
832 */
|
|
833 static int
|
|
834 cs_create_connection(i)
|
|
835 int i;
|
|
836 {
|
1385
|
837 #ifdef UNIX
|
|
838 int to_cs[2], from_cs[2];
|
|
839 #endif
|
|
840 int len;
|
|
841 char *prog, *cmd, *ppath = NULL;
|
|
842 #ifdef WIN32
|
|
843 int fd;
|
|
844 SECURITY_ATTRIBUTES sa;
|
|
845 PROCESS_INFORMATION pi;
|
|
846 STARTUPINFO si;
|
|
847 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
|
|
848 HANDLE stdin_rd, stdout_rd;
|
|
849 HANDLE stdout_wr, stdin_wr;
|
|
850 BOOL created;
|
1393
|
851 # ifdef __BORLANDC__
|
|
852 # define OPEN_OH_ARGTYPE long
|
|
853 # else
|
|
854 # if (_MSC_VER >= 1300)
|
|
855 # define OPEN_OH_ARGTYPE intptr_t
|
|
856 # else
|
|
857 # define OPEN_OH_ARGTYPE long
|
|
858 # endif
|
|
859 # endif
|
7
|
860 #endif
|
|
861
|
1385
|
862 #if defined(UNIX)
|
7
|
863 /*
|
|
864 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
|
|
865 * from_cs[0] and writes to to_cs[1].
|
|
866 */
|
|
867 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
|
|
868 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
|
|
869 {
|
|
870 (void)EMSG(_("E566: Could not create cscope pipes"));
|
|
871 err_closing:
|
|
872 if (to_cs[0] != -1)
|
|
873 (void)close(to_cs[0]);
|
|
874 if (to_cs[1] != -1)
|
|
875 (void)close(to_cs[1]);
|
|
876 if (from_cs[0] != -1)
|
|
877 (void)close(from_cs[0]);
|
|
878 if (from_cs[1] != -1)
|
|
879 (void)close(from_cs[1]);
|
|
880 return CSCOPE_FAILURE;
|
|
881 }
|
|
882
|
|
883 switch (csinfo[i].pid = fork())
|
|
884 {
|
|
885 case -1:
|
|
886 (void)EMSG(_("E622: Could not fork for cscope"));
|
|
887 goto err_closing;
|
|
888 case 0: /* child: run cscope. */
|
|
889 if (dup2(to_cs[0], STDIN_FILENO) == -1)
|
|
890 PERROR("cs_create_connection 1");
|
|
891 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
|
|
892 PERROR("cs_create_connection 2");
|
|
893 if (dup2(from_cs[1], STDERR_FILENO) == -1)
|
|
894 PERROR("cs_create_connection 3");
|
|
895
|
|
896 /* close unused */
|
|
897 (void)close(to_cs[1]);
|
|
898 (void)close(from_cs[0]);
|
|
899 #else
|
1385
|
900 /* WIN32 */
|
|
901 /* Create pipes to communicate with cscope */
|
|
902 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
903 sa.bInheritHandle = TRUE;
|
|
904 sa.lpSecurityDescriptor = NULL;
|
|
905
|
|
906 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
|
|
907 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
|
|
908 {
|
|
909 (void)EMSG(_("E566: Could not create cscope pipes"));
|
|
910 err_closing:
|
|
911 if (pipe_stdin)
|
|
912 {
|
|
913 CloseHandle(stdin_rd);
|
|
914 CloseHandle(stdin_wr);
|
|
915 }
|
|
916 if (pipe_stdout)
|
|
917 {
|
|
918 CloseHandle(stdout_rd);
|
|
919 CloseHandle(stdout_wr);
|
|
920 }
|
|
921 return CSCOPE_FAILURE;
|
|
922 }
|
7
|
923 #endif
|
|
924 /* expand the cscope exec for env var's */
|
|
925 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
|
|
926 {
|
|
927 #ifdef UNIX
|
|
928 return CSCOPE_FAILURE;
|
|
929 #else
|
1385
|
930 /* WIN32 */
|
7
|
931 goto err_closing;
|
|
932 #endif
|
|
933 }
|
|
934 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
|
|
935
|
|
936 /* alloc space to hold the cscope command */
|
835
|
937 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
|
7
|
938 if (csinfo[i].ppath)
|
|
939 {
|
|
940 /* expand the prepend path for env var's */
|
|
941 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
|
|
942 {
|
|
943 vim_free(prog);
|
|
944 #ifdef UNIX
|
|
945 return CSCOPE_FAILURE;
|
|
946 #else
|
1385
|
947 /* WIN32 */
|
7
|
948 goto err_closing;
|
|
949 #endif
|
|
950 }
|
|
951 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
|
|
952
|
835
|
953 len += (int)strlen(ppath);
|
7
|
954 }
|
|
955
|
|
956 if (csinfo[i].flags)
|
835
|
957 len += (int)strlen(csinfo[i].flags);
|
7
|
958
|
|
959 if ((cmd = (char *)alloc(len)) == NULL)
|
|
960 {
|
|
961 vim_free(prog);
|
|
962 vim_free(ppath);
|
|
963 #ifdef UNIX
|
|
964 return CSCOPE_FAILURE;
|
|
965 #else
|
1385
|
966 /* WIN32 */
|
7
|
967 goto err_closing;
|
|
968 #endif
|
|
969 }
|
|
970
|
|
971 /* run the cscope command; is there execl for non-unix systems? */
|
|
972 #if defined(UNIX)
|
|
973 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
|
|
974 #else
|
1385
|
975 /* WIN32 */
|
7
|
976 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
|
|
977 #endif
|
|
978 if (csinfo[i].ppath != NULL)
|
|
979 {
|
|
980 (void)strcat(cmd, " -P");
|
|
981 (void)strcat(cmd, csinfo[i].ppath);
|
|
982 }
|
|
983 if (csinfo[i].flags != NULL)
|
|
984 {
|
|
985 (void)strcat(cmd, " ");
|
|
986 (void)strcat(cmd, csinfo[i].flags);
|
|
987 }
|
|
988 # ifdef UNIX
|
|
989 /* on Win32 we still need prog */
|
|
990 vim_free(prog);
|
|
991 # endif
|
|
992 vim_free(ppath);
|
|
993
|
|
994 #if defined(UNIX)
|
1878
|
995 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
|
7
|
996 PERROR(_("cs_create_connection exec failed"));
|
|
997
|
|
998 exit(127);
|
|
999 /* NOTREACHED */
|
|
1000 default: /* parent. */
|
|
1001 /*
|
|
1002 * Save the file descriptors for later duplication, and
|
|
1003 * reopen as streams.
|
|
1004 */
|
|
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
|
|
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
|
|
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
|
|
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
|
|
1009
|
|
1010 /* close unused */
|
|
1011 (void)close(to_cs[0]);
|
|
1012 (void)close(from_cs[1]);
|
|
1013
|
|
1014 break;
|
|
1015 }
|
1385
|
1016
|
7
|
1017 #else
|
1385
|
1018 /* WIN32 */
|
|
1019 /* Create a new process to run cscope and use pipes to talk with it */
|
|
1020 GetStartupInfo(&si);
|
|
1021 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
1022 si.wShowWindow = SW_HIDE; /* Hide child application window */
|
|
1023 si.hStdOutput = stdout_wr;
|
|
1024 si.hStdError = stdout_wr;
|
|
1025 si.hStdInput = stdin_rd;
|
|
1026 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
|
|
1027 NULL, NULL, &si, &pi);
|
|
1028 vim_free(prog);
|
|
1029 vim_free(cmd);
|
|
1030
|
|
1031 if (!created)
|
|
1032 {
|
|
1033 PERROR(_("cs_create_connection exec failed"));
|
|
1034 (void)EMSG(_("E623: Could not spawn cscope process"));
|
|
1035 goto err_closing;
|
|
1036 }
|
|
1037 /* else */
|
|
1038 csinfo[i].pid = pi.dwProcessId;
|
|
1039 csinfo[i].hProc = pi.hProcess;
|
|
1040 CloseHandle(pi.hThread);
|
|
1041
|
|
1042 /* TODO - tidy up after failure to create files on pipe handles. */
|
1393
|
1043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
|
|
1044 _O_TEXT|_O_APPEND)) < 0)
|
1385
|
1045 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
|
|
1046 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
|
1393
|
1047 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
|
|
1048 _O_TEXT|_O_RDONLY)) < 0)
|
1385
|
1049 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
|
|
1050 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
|
|
1051
|
|
1052 /* Close handles for file descriptors inherited by the cscope process */
|
|
1053 CloseHandle(stdin_rd);
|
|
1054 CloseHandle(stdout_wr);
|
|
1055
|
|
1056 #endif /* !UNIX */
|
|
1057
|
7
|
1058 return CSCOPE_SUCCESS;
|
|
1059 } /* cs_create_connection */
|
|
1060
|
|
1061
|
|
1062 /*
|
|
1063 * PRIVATE: cs_find
|
|
1064 *
|
|
1065 * query cscope using command line interface. parse the output and use tselect
|
|
1066 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
|
|
1067 *
|
|
1068 * returns TRUE if we jump to a tag or abort, FALSE if not.
|
|
1069 */
|
|
1070 static int
|
|
1071 cs_find(eap)
|
|
1072 exarg_T *eap;
|
|
1073 {
|
|
1074 char *opt, *pat;
|
|
1075
|
|
1076 if (cs_check_for_connections() == FALSE)
|
|
1077 {
|
|
1078 (void)EMSG(_("E567: no cscope connections"));
|
|
1079 return FALSE;
|
|
1080 }
|
|
1081
|
|
1082 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
|
|
1083 {
|
|
1084 cs_usage_msg(Find);
|
|
1085 return FALSE;
|
|
1086 }
|
|
1087
|
|
1088 pat = opt + strlen(opt) + 1;
|
1372
|
1089 if (pat >= (char *)eap->arg + eap_arg_len)
|
7
|
1090 {
|
|
1091 cs_usage_msg(Find);
|
|
1092 return FALSE;
|
|
1093 }
|
|
1094
|
665
|
1095 return cs_find_common(opt, pat, eap->forceit, TRUE,
|
|
1096 eap->cmdidx == CMD_lcscope);
|
7
|
1097 } /* cs_find */
|
|
1098
|
|
1099
|
|
1100 /*
|
|
1101 * PRIVATE: cs_find_common
|
|
1102 *
|
|
1103 * common code for cscope find, shared by cs_find() and do_cstag()
|
|
1104 */
|
|
1105 static int
|
665
|
1106 cs_find_common(opt, pat, forceit, verbose, use_ll)
|
7
|
1107 char *opt;
|
|
1108 char *pat;
|
|
1109 int forceit;
|
|
1110 int verbose;
|
665
|
1111 int use_ll;
|
7
|
1112 {
|
|
1113 int i;
|
|
1114 char *cmd;
|
944
|
1115 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
|
7
|
1116 #ifdef FEAT_QUICKFIX
|
|
1117 char cmdletter;
|
|
1118 char *qfpos;
|
|
1119 #endif
|
|
1120
|
|
1121 /* create the actual command to send to cscope */
|
|
1122 cmd = cs_create_cmd(opt, pat);
|
|
1123 if (cmd == NULL)
|
|
1124 return FALSE;
|
|
1125
|
|
1126 /* send query to all open connections, then count the total number
|
|
1127 * of matches so we can alloc matchesp all in one swell foop
|
|
1128 */
|
|
1129 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1130 nummatches[i] = 0;
|
|
1131 totmatches = 0;
|
|
1132 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1133 {
|
1040
|
1134 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
|
7
|
1135 continue;
|
|
1136
|
|
1137 /* send cmd to cscope */
|
|
1138 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
|
|
1139 (void)fflush(csinfo[i].to_fp);
|
|
1140
|
|
1141 nummatches[i] = cs_cnt_matches(i);
|
|
1142
|
|
1143 if (nummatches[i] > -1)
|
|
1144 totmatches += nummatches[i];
|
|
1145
|
|
1146 if (nummatches[i] == 0)
|
|
1147 (void)cs_read_prompt(i);
|
|
1148 }
|
|
1149 vim_free(cmd);
|
|
1150
|
|
1151 if (totmatches == 0)
|
|
1152 {
|
|
1153 char *nf = _("E259: no matches found for cscope query %s of %s");
|
|
1154 char *buf;
|
|
1155
|
|
1156 if (!verbose)
|
|
1157 return FALSE;
|
|
1158
|
835
|
1159 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
|
7
|
1160 if (buf == NULL)
|
|
1161 (void)EMSG(nf);
|
|
1162 else
|
|
1163 {
|
|
1164 sprintf(buf, nf, opt, pat);
|
|
1165 (void)EMSG(buf);
|
|
1166 vim_free(buf);
|
|
1167 }
|
|
1168 return FALSE;
|
|
1169 }
|
|
1170
|
|
1171 #ifdef FEAT_QUICKFIX
|
|
1172 /* get cmd letter */
|
|
1173 switch (opt[0])
|
|
1174 {
|
|
1175 case '0' :
|
|
1176 cmdletter = 's';
|
|
1177 break;
|
|
1178 case '1' :
|
|
1179 cmdletter = 'g';
|
|
1180 break;
|
|
1181 case '2' :
|
|
1182 cmdletter = 'd';
|
|
1183 break;
|
|
1184 case '3' :
|
|
1185 cmdletter = 'c';
|
|
1186 break;
|
|
1187 case '4' :
|
|
1188 cmdletter = 't';
|
|
1189 break;
|
|
1190 case '6' :
|
|
1191 cmdletter = 'e';
|
|
1192 break;
|
|
1193 case '7' :
|
|
1194 cmdletter = 'f';
|
|
1195 break;
|
|
1196 case '8' :
|
|
1197 cmdletter = 'i';
|
|
1198 break;
|
|
1199 default :
|
|
1200 cmdletter = opt[0];
|
|
1201 }
|
|
1202
|
|
1203 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
|
|
1204 if (qfpos != NULL)
|
|
1205 {
|
|
1206 qfpos++;
|
|
1207 /* next symbol must be + or - */
|
|
1208 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
|
|
1209 {
|
|
1210 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
|
835
|
1211 char *buf = (char *)alloc((unsigned)strlen(nf));
|
7
|
1212
|
|
1213 /* strlen will be enough because we use chars */
|
|
1214 if (buf != NULL)
|
|
1215 {
|
|
1216 sprintf(buf, nf, *qfpos, *(qfpos-1));
|
|
1217 (void)EMSG(buf);
|
|
1218 vim_free(buf);
|
|
1219 }
|
|
1220 return FALSE;
|
|
1221 }
|
|
1222 }
|
36
|
1223 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
|
7
|
1224 {
|
|
1225 /* fill error list */
|
1027
|
1226 FILE *f;
|
|
1227 char_u *tmp = vim_tempname('c');
|
665
|
1228 qf_info_T *qi = NULL;
|
|
1229 win_T *wp = NULL;
|
7
|
1230
|
531
|
1231 f = mch_fopen((char *)tmp, "w");
|
1027
|
1232 if (f == NULL)
|
|
1233 EMSG2(_(e_notopen), tmp);
|
|
1234 else
|
7
|
1235 {
|
1027
|
1236 cs_file_results(f, nummatches);
|
|
1237 fclose(f);
|
|
1238 if (use_ll) /* Use location list */
|
|
1239 wp = curwin;
|
|
1240 /* '-' starts a new error list */
|
|
1241 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
|
|
1242 *qfpos == '-') > 0)
|
|
1243 {
|
7
|
1244 # ifdef FEAT_WINDOWS
|
1027
|
1245 if (postponed_split != 0)
|
|
1246 {
|
|
1247 win_split(postponed_split > 0 ? postponed_split : 0,
|
7
|
1248 postponed_split_flags);
|
|
1249 # ifdef FEAT_SCROLLBIND
|
1027
|
1250 curwin->w_p_scb = FALSE;
|
7
|
1251 # endif
|
1027
|
1252 postponed_split = 0;
|
|
1253 }
|
7
|
1254 # endif
|
1027
|
1255 if (use_ll)
|
|
1256 /*
|
|
1257 * In the location list window, use the displayed location
|
|
1258 * list. Otherwise, use the location list for the window.
|
|
1259 */
|
|
1260 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
|
|
1261 ? wp->w_llist_ref : wp->w_llist;
|
|
1262 qf_jump(qi, 0, 0, forceit);
|
|
1263 }
|
7
|
1264 }
|
|
1265 mch_remove(tmp);
|
|
1266 vim_free(tmp);
|
|
1267 return TRUE;
|
|
1268 }
|
|
1269 else
|
|
1270 #endif /* FEAT_QUICKFIX */
|
|
1271 {
|
944
|
1272 char **matches = NULL, **contexts = NULL;
|
|
1273 int matched = 0;
|
|
1274
|
7
|
1275 /* read output */
|
|
1276 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
|
|
1277 &contexts, &matched);
|
|
1278 if (matches == NULL)
|
|
1279 return FALSE;
|
|
1280
|
293
|
1281 (void)cs_manage_matches(matches, contexts, matched, Store);
|
7
|
1282
|
|
1283 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
|
|
1284 }
|
|
1285
|
|
1286 } /* cs_find_common */
|
|
1287
|
|
1288 /*
|
|
1289 * PRIVATE: cs_help
|
|
1290 *
|
|
1291 * print help
|
|
1292 */
|
|
1293 static int
|
|
1294 cs_help(eap)
|
1880
|
1295 exarg_T *eap UNUSED;
|
7
|
1296 {
|
|
1297 cscmd_T *cmdp = cs_cmds;
|
|
1298
|
|
1299 (void)MSG_PUTS(_("cscope commands:\n"));
|
|
1300 while (cmdp->name != NULL)
|
|
1301 {
|
1793
|
1302 char *help = _(cmdp->help);
|
|
1303 int space_cnt = 30 - vim_strsize((char_u *)help);
|
|
1304
|
|
1305 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
|
|
1306 if (space_cnt < 0)
|
|
1307 space_cnt = 0;
|
|
1308 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
|
|
1309 cmdp->name,
|
|
1310 help, space_cnt, " ",
|
|
1311 cmdp->usage);
|
7
|
1312 if (strcmp(cmdp->name, "find") == 0)
|
1706
|
1313 MSG_PUTS(_("\n"
|
|
1314 " c: Find functions calling this function\n"
|
|
1315 " d: Find functions called by this function\n"
|
|
1316 " e: Find this egrep pattern\n"
|
|
1317 " f: Find this file\n"
|
|
1318 " g: Find this definition\n"
|
|
1319 " i: Find files #including this file\n"
|
|
1320 " s: Find this C symbol\n"
|
|
1321 " t: Find assignments to\n"));
|
|
1322
|
7
|
1323 cmdp++;
|
|
1324 }
|
|
1325
|
|
1326 wait_return(TRUE);
|
|
1327 return 0;
|
|
1328 } /* cs_help */
|
|
1329
|
|
1330
|
|
1331 /*
|
|
1332 * PRIVATE: cs_init
|
|
1333 *
|
|
1334 * initialize cscope structure if not already
|
|
1335 */
|
|
1336 static void
|
|
1337 cs_init()
|
|
1338 {
|
|
1339 short i;
|
|
1340 static int init_already = FALSE;
|
|
1341
|
|
1342 if (init_already)
|
|
1343 return;
|
|
1344
|
|
1345 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1346 clear_csinfo(i);
|
|
1347
|
|
1348 init_already = TRUE;
|
|
1349 } /* cs_init */
|
|
1350
|
|
1351 static void
|
|
1352 clear_csinfo(i)
|
|
1353 int i;
|
|
1354 {
|
|
1355 csinfo[i].fname = NULL;
|
|
1356 csinfo[i].ppath = NULL;
|
|
1357 csinfo[i].flags = NULL;
|
|
1358 #if defined(UNIX)
|
|
1359 csinfo[i].st_dev = (dev_t)0;
|
|
1360 csinfo[i].st_ino = (ino_t)0;
|
|
1361 #else
|
|
1362 csinfo[i].nVolume = 0;
|
|
1363 csinfo[i].nIndexHigh = 0;
|
|
1364 csinfo[i].nIndexLow = 0;
|
|
1365 #endif
|
1621
|
1366 csinfo[i].pid = 0;
|
7
|
1367 csinfo[i].fr_fp = NULL;
|
|
1368 csinfo[i].to_fp = NULL;
|
301
|
1369 #if defined(WIN32)
|
|
1370 csinfo[i].hProc = NULL;
|
|
1371 #endif
|
7
|
1372 }
|
|
1373
|
|
1374 #ifndef UNIX
|
|
1375 static char *GetWin32Error __ARGS((void));
|
|
1376
|
|
1377 static char *
|
|
1378 GetWin32Error()
|
|
1379 {
|
|
1380 char *msg = NULL;
|
|
1381 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
|
1382 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
|
|
1383 if (msg != NULL)
|
|
1384 {
|
|
1385 /* remove trailing \r\n */
|
|
1386 char *pcrlf = strstr(msg, "\r\n");
|
|
1387 if (pcrlf != NULL)
|
|
1388 *pcrlf = '\0';
|
|
1389 }
|
|
1390 return msg;
|
|
1391 }
|
|
1392 #endif
|
323
|
1393
|
7
|
1394 /*
|
|
1395 * PRIVATE: cs_insert_filelist
|
|
1396 *
|
|
1397 * insert a new cscope database filename into the filelist
|
|
1398 */
|
|
1399 static int
|
|
1400 cs_insert_filelist(fname, ppath, flags, sb)
|
|
1401 char *fname;
|
|
1402 char *ppath;
|
|
1403 char *flags;
|
1880
|
1404 struct stat *sb UNUSED;
|
7
|
1405 {
|
|
1406 short i, j;
|
|
1407 #ifndef UNIX
|
|
1408 HANDLE hFile;
|
|
1409 BY_HANDLE_FILE_INFORMATION bhfi;
|
|
1410
|
|
1411 vim_memset(&bhfi, 0, sizeof(bhfi));
|
|
1412 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
|
|
1413 if (!mch_windows95())
|
|
1414 {
|
|
1415 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
|
|
1416 FILE_ATTRIBUTE_NORMAL, NULL);
|
|
1417 if (hFile == INVALID_HANDLE_VALUE)
|
|
1418 {
|
|
1419 if (p_csverbose)
|
|
1420 {
|
|
1421 char *cant_msg = _("E625: cannot open cscope database: %s");
|
|
1422 char *winmsg = GetWin32Error();
|
|
1423
|
|
1424 if (winmsg != NULL)
|
|
1425 {
|
|
1426 (void)EMSG2(cant_msg, winmsg);
|
|
1427 LocalFree(winmsg);
|
|
1428 }
|
|
1429 else
|
|
1430 /* subst filename if can't get error text */
|
|
1431 (void)EMSG2(cant_msg, fname);
|
|
1432 }
|
|
1433 return -1;
|
|
1434 }
|
|
1435 if (!GetFileInformationByHandle(hFile, &bhfi))
|
|
1436 {
|
|
1437 CloseHandle(hFile);
|
|
1438 if (p_csverbose)
|
|
1439 (void)EMSG(_("E626: cannot get cscope database information"));
|
|
1440 return -1;
|
|
1441 }
|
|
1442 CloseHandle(hFile);
|
|
1443 }
|
|
1444 #endif
|
|
1445
|
|
1446 i = -1; /* can be set to the index of an empty item in csinfo */
|
|
1447 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
|
|
1448 {
|
|
1449 if (csinfo[j].fname != NULL
|
|
1450 #if defined(UNIX)
|
|
1451 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
|
|
1452 #else
|
|
1453 /* compare pathnames first */
|
|
1454 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
|
1372
|
1455 /* if not Windows 9x, test index file attributes too */
|
7
|
1456 || (!mch_windows95()
|
|
1457 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
|
|
1458 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
|
|
1459 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
|
|
1460 #endif
|
|
1461 )
|
|
1462 {
|
|
1463 if (p_csverbose)
|
|
1464 (void)EMSG(_("E568: duplicate cscope database not added"));
|
|
1465 return -1;
|
|
1466 }
|
|
1467
|
|
1468 if (csinfo[j].fname == NULL && i == -1)
|
|
1469 i = j; /* remember first empty entry */
|
|
1470 }
|
|
1471
|
|
1472 if (i == -1)
|
|
1473 {
|
|
1474 if (p_csverbose)
|
|
1475 (void)EMSG(_("E569: maximum number of cscope connections reached"));
|
|
1476 return -1;
|
|
1477 }
|
|
1478
|
835
|
1479 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
|
7
|
1480 return -1;
|
|
1481
|
|
1482 (void)strcpy(csinfo[i].fname, (const char *)fname);
|
|
1483
|
|
1484 if (ppath != NULL)
|
|
1485 {
|
835
|
1486 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
|
7
|
1487 {
|
|
1488 vim_free(csinfo[i].fname);
|
|
1489 csinfo[i].fname = NULL;
|
|
1490 return -1;
|
|
1491 }
|
|
1492 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
|
|
1493 } else
|
|
1494 csinfo[i].ppath = NULL;
|
|
1495
|
|
1496 if (flags != NULL)
|
|
1497 {
|
835
|
1498 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
|
7
|
1499 {
|
|
1500 vim_free(csinfo[i].fname);
|
|
1501 vim_free(csinfo[i].ppath);
|
|
1502 csinfo[i].fname = NULL;
|
|
1503 csinfo[i].ppath = NULL;
|
|
1504 return -1;
|
|
1505 }
|
|
1506 (void)strcpy(csinfo[i].flags, (const char *)flags);
|
|
1507 } else
|
|
1508 csinfo[i].flags = NULL;
|
|
1509
|
|
1510 #if defined(UNIX)
|
|
1511 csinfo[i].st_dev = sb->st_dev;
|
|
1512 csinfo[i].st_ino = sb->st_ino;
|
|
1513
|
|
1514 #else
|
|
1515 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
|
|
1516 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
|
|
1517 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
|
|
1518 #endif
|
|
1519 return i;
|
|
1520 } /* cs_insert_filelist */
|
|
1521
|
|
1522
|
|
1523 /*
|
|
1524 * PRIVATE: cs_lookup_cmd
|
|
1525 *
|
|
1526 * find cscope command in command table
|
|
1527 */
|
|
1528 static cscmd_T *
|
|
1529 cs_lookup_cmd(eap)
|
|
1530 exarg_T *eap;
|
|
1531 {
|
|
1532 cscmd_T *cmdp;
|
|
1533 char *stok;
|
|
1534 size_t len;
|
|
1535
|
|
1536 if (eap->arg == NULL)
|
|
1537 return NULL;
|
|
1538
|
1372
|
1539 /* Store length of eap->arg before it gets modified by strtok(). */
|
1570
|
1540 eap_arg_len = (int)STRLEN(eap->arg);
|
1372
|
1541
|
7
|
1542 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
|
|
1543 return NULL;
|
|
1544
|
|
1545 len = strlen(stok);
|
|
1546 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
|
|
1547 {
|
|
1548 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
|
|
1549 return (cmdp);
|
|
1550 }
|
|
1551 return NULL;
|
|
1552 } /* cs_lookup_cmd */
|
|
1553
|
|
1554
|
|
1555 /*
|
|
1556 * PRIVATE: cs_kill
|
|
1557 *
|
|
1558 * nuke em
|
|
1559 */
|
|
1560 static int
|
|
1561 cs_kill(eap)
|
1880
|
1562 exarg_T *eap UNUSED;
|
7
|
1563 {
|
|
1564 char *stok;
|
|
1565 short i;
|
|
1566
|
|
1567 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
|
|
1568 {
|
|
1569 cs_usage_msg(Kill);
|
|
1570 return CSCOPE_FAILURE;
|
|
1571 }
|
|
1572
|
|
1573 /* only single digit positive and negative integers are allowed */
|
|
1574 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
|
|
1575 || (strlen(stok) < 3 && stok[0] == '-'
|
|
1576 && VIM_ISDIGIT((int)(stok[1]))))
|
|
1577 i = atoi(stok);
|
|
1578 else
|
|
1579 {
|
|
1580 /* It must be part of a name. We will try to find a match
|
|
1581 * within all the names in the csinfo data structure
|
|
1582 */
|
|
1583 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1584 {
|
|
1585 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
|
|
1586 break;
|
|
1587 }
|
|
1588 }
|
|
1589
|
|
1590 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
|
|
1591 && i != -1)
|
|
1592 {
|
|
1593 if (p_csverbose)
|
|
1594 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
|
|
1595 }
|
|
1596 else
|
|
1597 {
|
|
1598 if (i == -1)
|
|
1599 {
|
|
1600 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1601 {
|
|
1602 if (csinfo[i].fname)
|
|
1603 cs_kill_execute(i, csinfo[i].fname);
|
|
1604 }
|
|
1605 }
|
|
1606 else
|
|
1607 cs_kill_execute(i, stok);
|
|
1608 }
|
|
1609
|
|
1610 return 0;
|
|
1611 } /* cs_kill */
|
|
1612
|
|
1613
|
|
1614 /*
|
|
1615 * PRIVATE: cs_kill_execute
|
|
1616 *
|
|
1617 * Actually kills a specific cscope connection.
|
|
1618 */
|
|
1619 static void
|
|
1620 cs_kill_execute(i, cname)
|
|
1621 int i; /* cscope table index */
|
|
1622 char *cname; /* cscope database name */
|
|
1623 {
|
|
1624 if (p_csverbose)
|
|
1625 {
|
|
1626 msg_clr_eos();
|
|
1627 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
|
|
1628 (char_u *)_("cscope connection %s closed"), cname);
|
|
1629 }
|
|
1630 cs_release_csp(i, TRUE);
|
|
1631 }
|
|
1632
|
|
1633
|
|
1634 /*
|
|
1635 * PRIVATE: cs_make_vim_style_matches
|
|
1636 *
|
|
1637 * convert the cscope output into into a ctags style entry (as might be found
|
|
1638 * in a ctags tags file). there's one catch though: cscope doesn't tell you
|
|
1639 * the type of the tag you are looking for. for example, in Darren Hiebert's
|
|
1640 * ctags (the one that comes with vim), #define's use a line number to find the
|
|
1641 * tag in a file while function definitions use a regexp search pattern.
|
|
1642 *
|
|
1643 * i'm going to always use the line number because cscope does something
|
|
1644 * quirky (and probably other things i don't know about):
|
|
1645 *
|
|
1646 * if you have "# define" in your source file, which is
|
|
1647 * perfectly legal, cscope thinks you have "#define". this
|
|
1648 * will result in a failed regexp search. :(
|
|
1649 *
|
|
1650 * besides, even if this particular case didn't happen, the search pattern
|
|
1651 * would still have to be modified to escape all the special regular expression
|
|
1652 * characters to comply with ctags formatting.
|
|
1653 */
|
|
1654 static char *
|
|
1655 cs_make_vim_style_matches(fname, slno, search, tagstr)
|
|
1656 char *fname;
|
|
1657 char *slno;
|
|
1658 char *search;
|
|
1659 char *tagstr;
|
|
1660 {
|
|
1661 /* vim style is ctags:
|
|
1662 *
|
|
1663 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
|
|
1664 *
|
|
1665 * but as mentioned above, we'll always use the line number and
|
|
1666 * put the search pattern (if one exists) as "extra"
|
|
1667 *
|
|
1668 * buf is used as part of vim's method of handling tags, and
|
|
1669 * (i think) vim frees it when you pop your tags and get replaced
|
|
1670 * by new ones on the tag stack.
|
|
1671 */
|
|
1672 char *buf;
|
|
1673 int amt;
|
|
1674
|
|
1675 if (search != NULL)
|
|
1676 {
|
835
|
1677 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
|
7
|
1678 if ((buf = (char *)alloc(amt)) == NULL)
|
|
1679 return NULL;
|
|
1680
|
|
1681 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
|
|
1682 }
|
|
1683 else
|
|
1684 {
|
835
|
1685 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
|
7
|
1686 if ((buf = (char *)alloc(amt)) == NULL)
|
|
1687 return NULL;
|
|
1688
|
|
1689 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
|
|
1690 }
|
|
1691
|
|
1692 return buf;
|
|
1693 } /* cs_make_vim_style_matches */
|
|
1694
|
|
1695
|
|
1696 /*
|
|
1697 * PRIVATE: cs_manage_matches
|
|
1698 *
|
|
1699 * this is kind of hokey, but i don't see an easy way round this..
|
|
1700 *
|
|
1701 * Store: keep a ptr to the (malloc'd) memory of matches originally
|
|
1702 * generated from cs_find(). the matches are originally lines directly
|
|
1703 * from cscope output, but transformed to look like something out of a
|
|
1704 * ctags. see cs_make_vim_style_matches for more details.
|
|
1705 *
|
|
1706 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
|
|
1707 * the next line from the cscope output. it basically keeps track of which
|
|
1708 * lines have been "used" and returns the next one.
|
|
1709 *
|
|
1710 * Free: frees up everything and resets
|
|
1711 *
|
|
1712 * Print: prints the tags
|
|
1713 */
|
|
1714 static char *
|
|
1715 cs_manage_matches(matches, contexts, totmatches, cmd)
|
|
1716 char **matches;
|
|
1717 char **contexts;
|
|
1718 int totmatches;
|
|
1719 mcmd_e cmd;
|
|
1720 {
|
|
1721 static char **mp = NULL;
|
|
1722 static char **cp = NULL;
|
|
1723 static int cnt = -1;
|
|
1724 static int next = -1;
|
|
1725 char *p = NULL;
|
|
1726
|
|
1727 switch (cmd)
|
|
1728 {
|
|
1729 case Store:
|
|
1730 assert(matches != NULL);
|
|
1731 assert(totmatches > 0);
|
|
1732 if (mp != NULL || cp != NULL)
|
|
1733 (void)cs_manage_matches(NULL, NULL, -1, Free);
|
|
1734 mp = matches;
|
|
1735 cp = contexts;
|
|
1736 cnt = totmatches;
|
|
1737 next = 0;
|
|
1738 break;
|
|
1739 case Get:
|
|
1740 if (next >= cnt)
|
|
1741 return NULL;
|
|
1742
|
|
1743 p = mp[next];
|
|
1744 next++;
|
|
1745 break;
|
|
1746 case Free:
|
|
1747 if (mp != NULL)
|
|
1748 {
|
|
1749 if (cnt > 0)
|
|
1750 while (cnt--)
|
|
1751 {
|
|
1752 vim_free(mp[cnt]);
|
|
1753 if (cp != NULL)
|
|
1754 vim_free(cp[cnt]);
|
|
1755 }
|
|
1756 vim_free(mp);
|
|
1757 vim_free(cp);
|
|
1758 }
|
|
1759 mp = NULL;
|
|
1760 cp = NULL;
|
|
1761 cnt = 0;
|
|
1762 next = 0;
|
|
1763 break;
|
|
1764 case Print:
|
|
1765 cs_print_tags_priv(mp, cp, cnt);
|
|
1766 break;
|
|
1767 default: /* should not reach here */
|
|
1768 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
|
|
1769 return NULL;
|
|
1770 }
|
|
1771
|
|
1772 return p;
|
|
1773 } /* cs_manage_matches */
|
|
1774
|
|
1775
|
|
1776 /*
|
|
1777 * PRIVATE: cs_parse_results
|
|
1778 *
|
|
1779 * parse cscope output
|
|
1780 */
|
|
1781 static char *
|
|
1782 cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
|
|
1783 int cnumber;
|
|
1784 char *buf;
|
|
1785 int bufsize;
|
|
1786 char **context;
|
|
1787 char **linenumber;
|
|
1788 char **search;
|
|
1789 {
|
|
1790 int ch;
|
|
1791 char *p;
|
|
1792 char *name;
|
|
1793
|
|
1794 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
|
|
1795 {
|
|
1796 if (feof(csinfo[cnumber].fr_fp))
|
|
1797 errno = EIO;
|
|
1798
|
|
1799 cs_reading_emsg(cnumber);
|
|
1800
|
|
1801 return NULL;
|
|
1802 }
|
|
1803
|
|
1804 /* If the line's too long for the buffer, discard it. */
|
|
1805 if ((p = strchr(buf, '\n')) == NULL)
|
|
1806 {
|
|
1807 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
|
|
1808 ;
|
|
1809 return NULL;
|
|
1810 }
|
|
1811 *p = '\0';
|
|
1812
|
|
1813 /*
|
|
1814 * cscope output is in the following format:
|
|
1815 *
|
|
1816 * <filename> <context> <line number> <pattern>
|
|
1817 */
|
|
1818 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
|
|
1819 return NULL;
|
|
1820 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
|
|
1821 return NULL;
|
|
1822 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
|
|
1823 return NULL;
|
|
1824 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
|
|
1825
|
|
1826 /* --- nvi ---
|
|
1827 * If the file is older than the cscope database, that is,
|
|
1828 * the database was built since the file was last modified,
|
|
1829 * or there wasn't a search string, use the line number.
|
|
1830 */
|
|
1831 if (strcmp(*search, "<unknown>") == 0)
|
|
1832 *search = NULL;
|
|
1833
|
|
1834 name = cs_resolve_file(cnumber, name);
|
|
1835 return name;
|
|
1836 }
|
|
1837
|
636
|
1838 #ifdef FEAT_QUICKFIX
|
7
|
1839 /*
|
|
1840 * PRIVATE: cs_file_results
|
|
1841 *
|
|
1842 * write cscope find results to file
|
|
1843 */
|
|
1844 static void
|
|
1845 cs_file_results(f, nummatches_a)
|
|
1846 FILE *f;
|
|
1847 int *nummatches_a;
|
|
1848 {
|
|
1849 int i, j;
|
|
1850 char *buf;
|
|
1851 char *search, *slno;
|
|
1852 char *fullname;
|
|
1853 char *cntx;
|
|
1854 char *context;
|
|
1855
|
|
1856 buf = (char *)alloc(CSREAD_BUFSIZE);
|
|
1857 if (buf == NULL)
|
|
1858 return;
|
|
1859
|
|
1860 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1861 {
|
|
1862 if (nummatches_a[i] < 1)
|
|
1863 continue;
|
|
1864
|
|
1865 for (j = 0; j < nummatches_a[i]; j++)
|
|
1866 {
|
293
|
1867 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
|
|
1868 &slno, &search)) == NULL)
|
7
|
1869 continue;
|
|
1870
|
835
|
1871 context = (char *)alloc((unsigned)strlen(cntx)+5);
|
1027
|
1872 if (context == NULL)
|
7
|
1873 continue;
|
|
1874
|
|
1875 if (strcmp(cntx, "<global>")==0)
|
|
1876 strcpy(context, "<<global>>");
|
|
1877 else
|
|
1878 sprintf(context, "<<%s>>", cntx);
|
|
1879
|
1027
|
1880 if (search == NULL)
|
7
|
1881 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
|
|
1882 else
|
|
1883 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
|
|
1884
|
|
1885 vim_free(context);
|
|
1886 vim_free(fullname);
|
|
1887 } /* for all matches */
|
|
1888
|
|
1889 (void)cs_read_prompt(i);
|
|
1890
|
|
1891 } /* for all cscope connections */
|
|
1892 vim_free(buf);
|
|
1893 }
|
636
|
1894 #endif
|
7
|
1895
|
|
1896 /*
|
|
1897 * PRIVATE: cs_fill_results
|
|
1898 *
|
|
1899 * get parsed cscope output and calls cs_make_vim_style_matches to convert
|
|
1900 * into ctags format
|
297
|
1901 * When there are no matches sets "*matches_p" to NULL.
|
7
|
1902 */
|
|
1903 static void
|
|
1904 cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
|
|
1905 char *tagstr;
|
|
1906 int totmatches;
|
|
1907 int *nummatches_a;
|
|
1908 char ***matches_p;
|
|
1909 char ***cntxts_p;
|
|
1910 int *matched;
|
|
1911 {
|
|
1912 int i, j;
|
|
1913 char *buf;
|
|
1914 char *search, *slno;
|
|
1915 int totsofar = 0;
|
|
1916 char **matches = NULL;
|
|
1917 char **cntxts = NULL;
|
|
1918 char *fullname;
|
|
1919 char *cntx;
|
|
1920
|
|
1921 assert(totmatches > 0);
|
|
1922
|
|
1923 buf = (char *)alloc(CSREAD_BUFSIZE);
|
|
1924 if (buf == NULL)
|
|
1925 return;
|
|
1926
|
|
1927 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
|
|
1928 goto parse_out;
|
|
1929 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
|
|
1930 goto parse_out;
|
|
1931
|
|
1932 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
1933 {
|
|
1934 if (nummatches_a[i] < 1)
|
|
1935 continue;
|
|
1936
|
|
1937 for (j = 0; j < nummatches_a[i]; j++)
|
|
1938 {
|
|
1939 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
|
|
1940 &slno, &search)) == NULL)
|
|
1941 continue;
|
|
1942
|
|
1943 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
|
|
1944 search, tagstr);
|
|
1945
|
|
1946 vim_free(fullname);
|
|
1947
|
|
1948 if (strcmp(cntx, "<global>") == 0)
|
|
1949 cntxts[totsofar] = NULL;
|
|
1950 else
|
|
1951 /* note: if vim_strsave returns NULL, then the context
|
|
1952 * will be "<global>", which is misleading.
|
|
1953 */
|
|
1954 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
|
|
1955
|
|
1956 if (matches[totsofar] != NULL)
|
|
1957 totsofar++;
|
|
1958
|
|
1959 } /* for all matches */
|
|
1960
|
|
1961 (void)cs_read_prompt(i);
|
|
1962
|
|
1963 } /* for all cscope connections */
|
|
1964
|
|
1965 parse_out:
|
297
|
1966 if (totsofar == 0)
|
|
1967 {
|
|
1968 /* No matches, free the arrays and return NULL in "*matches_p". */
|
|
1969 vim_free(matches);
|
|
1970 matches = NULL;
|
|
1971 vim_free(cntxts);
|
|
1972 cntxts = NULL;
|
|
1973 }
|
7
|
1974 *matched = totsofar;
|
|
1975 *matches_p = matches;
|
|
1976 *cntxts_p = cntxts;
|
297
|
1977
|
7
|
1978 vim_free(buf);
|
|
1979 } /* cs_fill_results */
|
|
1980
|
|
1981
|
|
1982 /* get the requested path components */
|
|
1983 static char *
|
|
1984 cs_pathcomponents(path)
|
|
1985 char *path;
|
|
1986 {
|
|
1987 int i;
|
|
1988 char *s;
|
|
1989
|
|
1990 if (p_cspc == 0)
|
|
1991 return path;
|
|
1992
|
|
1993 s = path + strlen(path) - 1;
|
|
1994 for (i = 0; i < p_cspc; ++i)
|
|
1995 while (s > path && *--s != '/'
|
|
1996 #ifdef WIN32
|
|
1997 && *--s != '\\'
|
|
1998 #endif
|
|
1999 )
|
|
2000 ;
|
|
2001 if ((s > path && *s == '/')
|
|
2002 #ifdef WIN32
|
|
2003 || (s > path && *s == '\\')
|
|
2004 #endif
|
|
2005 )
|
|
2006 ++s;
|
|
2007 return s;
|
|
2008 }
|
|
2009
|
|
2010 /*
|
|
2011 * PRIVATE: cs_print_tags_priv
|
|
2012 *
|
|
2013 * called from cs_manage_matches()
|
|
2014 */
|
|
2015 static void
|
|
2016 cs_print_tags_priv(matches, cntxts, num_matches)
|
|
2017 char **matches;
|
|
2018 char **cntxts;
|
|
2019 int num_matches;
|
|
2020 {
|
|
2021 char *buf = NULL;
|
|
2022 int bufsize = 0; /* Track available bufsize */
|
|
2023 int newsize = 0;
|
|
2024 char *ptag;
|
|
2025 char *fname, *lno, *extra, *tbuf;
|
|
2026 int i, idx, num;
|
|
2027 char *globalcntx = "GLOBAL";
|
|
2028 char *cntxformat = " <<%s>>";
|
|
2029 char *context;
|
|
2030 char *cstag_msg = _("Cscope tag: %s");
|
|
2031 char *csfmt_str = "%4d %6s ";
|
|
2032
|
|
2033 assert (num_matches > 0);
|
|
2034
|
835
|
2035 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
|
7
|
2036 return;
|
|
2037
|
|
2038 strcpy(tbuf, matches[0]);
|
|
2039 ptag = strtok(tbuf, "\t");
|
|
2040
|
835
|
2041 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
|
7
|
2042 buf = (char *)alloc(newsize);
|
|
2043 if (buf != NULL)
|
|
2044 {
|
|
2045 bufsize = newsize;
|
|
2046 (void)sprintf(buf, cstag_msg, ptag);
|
|
2047 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
|
|
2048 }
|
|
2049
|
|
2050 vim_free(tbuf);
|
|
2051
|
|
2052 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
|
|
2053 msg_advance(msg_col + 2);
|
|
2054 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
|
|
2055
|
|
2056 num = 1;
|
|
2057 for (i = 0; i < num_matches; i++)
|
|
2058 {
|
|
2059 idx = i;
|
|
2060
|
|
2061 /* if we really wanted to, we could avoid this malloc and strcpy
|
|
2062 * by parsing matches[i] on the fly and placing stuff into buf
|
|
2063 * directly, but that's too much of a hassle
|
|
2064 */
|
835
|
2065 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
|
7
|
2066 continue;
|
|
2067 (void)strcpy(tbuf, matches[idx]);
|
|
2068
|
|
2069 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
|
|
2070 continue;
|
|
2071 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
|
|
2072 continue;
|
|
2073 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
|
1078
|
2074 continue;
|
|
2075 extra = strtok(NULL, (const char *)"\t");
|
7
|
2076
|
|
2077 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
|
|
2078
|
|
2079 /* hopefully 'num' (num of matches) will be less than 10^16 */
|
835
|
2080 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
|
7
|
2081 if (bufsize < newsize)
|
|
2082 {
|
|
2083 buf = (char *)vim_realloc(buf, newsize);
|
|
2084 if (buf == NULL)
|
|
2085 bufsize = 0;
|
|
2086 else
|
|
2087 bufsize = newsize;
|
|
2088 }
|
|
2089 if (buf != NULL)
|
|
2090 {
|
|
2091 /* csfmt_str = "%4d %6s "; */
|
|
2092 (void)sprintf(buf, csfmt_str, num, lno);
|
|
2093 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
|
|
2094 }
|
|
2095 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
|
|
2096
|
|
2097 /* compute the required space for the context */
|
|
2098 if (cntxts[idx] != NULL)
|
|
2099 context = cntxts[idx];
|
|
2100 else
|
|
2101 context = globalcntx;
|
835
|
2102 newsize = (int)(strlen(context) + strlen(cntxformat));
|
7
|
2103
|
|
2104 if (bufsize < newsize)
|
|
2105 {
|
|
2106 buf = (char *)vim_realloc(buf, newsize);
|
|
2107 if (buf == NULL)
|
|
2108 bufsize = 0;
|
|
2109 else
|
|
2110 bufsize = newsize;
|
|
2111 }
|
|
2112 if (buf != NULL)
|
|
2113 {
|
|
2114 (void)sprintf(buf, cntxformat, context);
|
|
2115
|
|
2116 /* print the context only if it fits on the same line */
|
|
2117 if (msg_col + (int)strlen(buf) >= (int)Columns)
|
|
2118 msg_putchar('\n');
|
|
2119 msg_advance(12);
|
|
2120 MSG_PUTS_LONG(buf);
|
|
2121 msg_putchar('\n');
|
|
2122 }
|
|
2123 if (extra != NULL)
|
|
2124 {
|
|
2125 msg_advance(13);
|
|
2126 MSG_PUTS_LONG(extra);
|
|
2127 }
|
|
2128
|
|
2129 vim_free(tbuf); /* only after printing extra due to strtok use */
|
|
2130
|
|
2131 if (msg_col)
|
|
2132 msg_putchar('\n');
|
|
2133
|
|
2134 ui_breakcheck();
|
|
2135 if (got_int)
|
|
2136 {
|
|
2137 got_int = FALSE; /* don't print any more matches */
|
|
2138 break;
|
|
2139 }
|
|
2140
|
|
2141 num++;
|
|
2142 } /* for all matches */
|
|
2143
|
|
2144 vim_free(buf);
|
|
2145 } /* cs_print_tags_priv */
|
|
2146
|
|
2147
|
|
2148 /*
|
|
2149 * PRIVATE: cs_read_prompt
|
|
2150 *
|
|
2151 * read a cscope prompt (basically, skip over the ">> ")
|
|
2152 */
|
|
2153 static int
|
|
2154 cs_read_prompt(i)
|
|
2155 int i;
|
|
2156 {
|
|
2157 int ch;
|
|
2158 char *buf = NULL; /* buffer for possible error message from cscope */
|
|
2159 int bufpos = 0;
|
|
2160 char *cs_emsg;
|
|
2161 int maxlen;
|
|
2162 static char *eprompt = "Press the RETURN key to continue:";
|
835
|
2163 int epromptlen = (int)strlen(eprompt);
|
7
|
2164 int n;
|
|
2165
|
|
2166 cs_emsg = _("E609: Cscope error: %s");
|
|
2167 /* compute maximum allowed len for Cscope error message */
|
|
2168 maxlen = (int)(IOSIZE - strlen(cs_emsg));
|
|
2169
|
|
2170 for (;;)
|
|
2171 {
|
|
2172 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
|
|
2173 /* if there is room and char is printable */
|
|
2174 if (bufpos < maxlen - 1 && vim_isprintc(ch))
|
|
2175 {
|
|
2176 if (buf == NULL) /* lazy buffer allocation */
|
|
2177 buf = (char *)alloc(maxlen);
|
|
2178 if (buf != NULL)
|
|
2179 {
|
|
2180 /* append character to the message */
|
|
2181 buf[bufpos++] = ch;
|
|
2182 buf[bufpos] = NUL;
|
|
2183 if (bufpos >= epromptlen
|
|
2184 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
|
|
2185 {
|
|
2186 /* remove eprompt from buf */
|
|
2187 buf[bufpos - epromptlen] = NUL;
|
|
2188
|
|
2189 /* print message to user */
|
|
2190 (void)EMSG2(cs_emsg, buf);
|
|
2191
|
|
2192 /* send RETURN to cscope */
|
|
2193 (void)putc('\n', csinfo[i].to_fp);
|
|
2194 (void)fflush(csinfo[i].to_fp);
|
|
2195
|
|
2196 /* clear buf */
|
|
2197 bufpos = 0;
|
|
2198 buf[bufpos] = NUL;
|
|
2199 }
|
|
2200 }
|
|
2201 }
|
|
2202
|
|
2203 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
|
|
2204 {
|
|
2205 if (n > 0)
|
|
2206 ch = getc(csinfo[i].fr_fp);
|
|
2207 if (ch == EOF)
|
|
2208 {
|
|
2209 PERROR("cs_read_prompt EOF");
|
|
2210 if (buf != NULL && buf[0] != NUL)
|
|
2211 (void)EMSG2(cs_emsg, buf);
|
|
2212 else if (p_csverbose)
|
|
2213 cs_reading_emsg(i); /* don't have additional information */
|
|
2214 cs_release_csp(i, TRUE);
|
|
2215 vim_free(buf);
|
|
2216 return CSCOPE_FAILURE;
|
|
2217 }
|
|
2218
|
|
2219 if (ch != CSCOPE_PROMPT[n])
|
|
2220 {
|
|
2221 ch = EOF;
|
|
2222 break;
|
|
2223 }
|
|
2224 }
|
|
2225
|
|
2226 if (ch == EOF)
|
|
2227 continue; /* didn't find the prompt */
|
|
2228 break; /* did find the prompt */
|
|
2229 }
|
|
2230
|
|
2231 vim_free(buf);
|
|
2232 return CSCOPE_SUCCESS;
|
|
2233 }
|
|
2234
|
1566
|
2235 #if defined(UNIX) && defined(SIGALRM)
|
|
2236 /*
|
|
2237 * Used to catch and ignore SIGALRM below.
|
|
2238 */
|
|
2239 static RETSIGTYPE
|
|
2240 sig_handler SIGDEFARG(sigarg)
|
|
2241 {
|
|
2242 /* do nothing */
|
|
2243 SIGRETURN;
|
|
2244 }
|
|
2245 #endif
|
7
|
2246
|
|
2247 /*
|
|
2248 * PRIVATE: cs_release_csp
|
|
2249 *
|
1385
|
2250 * Does the actual free'ing for the cs ptr with an optional flag of whether
|
|
2251 * or not to free the filename. Called by cs_kill and cs_reset.
|
7
|
2252 */
|
|
2253 static void
|
|
2254 cs_release_csp(i, freefnpp)
|
|
2255 int i;
|
|
2256 int freefnpp;
|
|
2257 {
|
|
2258 /*
|
|
2259 * Trying to exit normally (not sure whether it is fit to UNIX cscope
|
|
2260 */
|
|
2261 if (csinfo[i].to_fp != NULL)
|
|
2262 {
|
|
2263 (void)fputs("q\n", csinfo[i].to_fp);
|
|
2264 (void)fflush(csinfo[i].to_fp);
|
|
2265 }
|
1566
|
2266 #if defined(UNIX)
|
|
2267 {
|
1575
|
2268 int waitpid_errno;
|
1566
|
2269 int pstat;
|
|
2270 pid_t pid;
|
|
2271
|
|
2272 # if defined(HAVE_SIGACTION)
|
|
2273 struct sigaction sa, old;
|
|
2274
|
1568
|
2275 /* Use sigaction() to limit the waiting time to two seconds. */
|
|
2276 sigemptyset(&sa.sa_mask);
|
1566
|
2277 sa.sa_handler = sig_handler;
|
|
2278 sa.sa_flags = SA_NODEFER;
|
|
2279 sigaction(SIGALRM, &sa, &old);
|
|
2280 alarm(2); /* 2 sec timeout */
|
|
2281
|
|
2282 /* Block until cscope exits or until timer expires */
|
|
2283 pid = waitpid(csinfo[i].pid, &pstat, 0);
|
1575
|
2284 waitpid_errno = errno;
|
1566
|
2285
|
|
2286 /* cancel pending alarm if still there and restore signal */
|
|
2287 alarm(0);
|
|
2288 sigaction(SIGALRM, &old, NULL);
|
|
2289 # else
|
|
2290 int waited;
|
|
2291
|
|
2292 /* Can't use sigaction(), loop for two seconds. First yield the CPU
|
|
2293 * to give cscope a chance to exit quickly. */
|
|
2294 sleep(0);
|
|
2295 for (waited = 0; waited < 40; ++waited)
|
|
2296 {
|
|
2297 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
|
1575
|
2298 waitpid_errno = errno;
|
1566
|
2299 if (pid != 0)
|
|
2300 break; /* break unless the process is still running */
|
1578
|
2301 mch_delay(50L, FALSE); /* sleep 50 ms */
|
1566
|
2302 }
|
|
2303 # endif
|
|
2304 /*
|
|
2305 * If the cscope process is still running: kill it.
|
|
2306 * Safety check: If the PID would be zero here, the entire X session
|
|
2307 * would be killed. -1 and 1 are dangerous as well.
|
|
2308 */
|
|
2309 if (pid < 0 && csinfo[i].pid > 1)
|
|
2310 {
|
1575
|
2311 # ifdef ECHILD
|
|
2312 int alive = TRUE;
|
|
2313
|
|
2314 if (waitpid_errno == ECHILD)
|
|
2315 {
|
|
2316 /*
|
|
2317 * When using 'vim -g', vim is forked and cscope process is
|
|
2318 * no longer a child process but a sibling. So waitpid()
|
|
2319 * fails with errno being ECHILD (No child processes).
|
|
2320 * Don't send SIGKILL to cscope immediately but wait
|
|
2321 * (polling) for it to exit normally as result of sending
|
|
2322 * the "q" command, hence giving it a chance to clean up
|
|
2323 * its temporary files.
|
|
2324 */
|
|
2325 int waited;
|
|
2326
|
|
2327 sleep(0);
|
|
2328 for (waited = 0; waited < 40; ++waited)
|
|
2329 {
|
|
2330 /* Check whether cscope process is still alive */
|
|
2331 if (kill(csinfo[i].pid, 0) != 0)
|
|
2332 {
|
|
2333 alive = FALSE; /* cscope process no longer exists */
|
|
2334 break;
|
|
2335 }
|
1578
|
2336 mch_delay(50L, FALSE); /* sleep 50ms */
|
1575
|
2337 }
|
|
2338 }
|
|
2339 if (alive)
|
|
2340 # endif
|
|
2341 {
|
|
2342 kill(csinfo[i].pid, SIGKILL);
|
|
2343 (void)waitpid(csinfo[i].pid, &pstat, 0);
|
|
2344 }
|
1566
|
2345 }
|
|
2346 }
|
|
2347 #else /* !UNIX */
|
1385
|
2348 if (csinfo[i].hProc != NULL)
|
|
2349 {
|
|
2350 /* Give cscope a chance to exit normally */
|
|
2351 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
|
|
2352 TerminateProcess(csinfo[i].hProc, 0);
|
|
2353 CloseHandle(csinfo[i].hProc);
|
|
2354 }
|
7
|
2355 #endif
|
|
2356
|
|
2357 if (csinfo[i].fr_fp != NULL)
|
|
2358 (void)fclose(csinfo[i].fr_fp);
|
|
2359 if (csinfo[i].to_fp != NULL)
|
|
2360 (void)fclose(csinfo[i].to_fp);
|
|
2361
|
|
2362 if (freefnpp)
|
|
2363 {
|
|
2364 vim_free(csinfo[i].fname);
|
|
2365 vim_free(csinfo[i].ppath);
|
|
2366 vim_free(csinfo[i].flags);
|
|
2367 }
|
|
2368
|
|
2369 clear_csinfo(i);
|
|
2370 } /* cs_release_csp */
|
|
2371
|
|
2372
|
|
2373 /*
|
|
2374 * PRIVATE: cs_reset
|
|
2375 *
|
|
2376 * calls cs_kill on all cscope connections then reinits
|
|
2377 */
|
|
2378 static int
|
|
2379 cs_reset(eap)
|
1880
|
2380 exarg_T *eap UNUSED;
|
7
|
2381 {
|
|
2382 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
|
|
2383 int i;
|
273
|
2384 char buf[20]; /* for sprintf " (#%d)" */
|
7
|
2385
|
|
2386 /* malloc our db and ppath list */
|
|
2387 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
|
|
2388 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
|
|
2389 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
|
|
2390 if (dblist == NULL || pplist == NULL || fllist == NULL)
|
|
2391 {
|
|
2392 vim_free(dblist);
|
|
2393 vim_free(pplist);
|
|
2394 vim_free(fllist);
|
|
2395 return CSCOPE_FAILURE;
|
|
2396 }
|
|
2397
|
|
2398 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
2399 {
|
|
2400 dblist[i] = csinfo[i].fname;
|
|
2401 pplist[i] = csinfo[i].ppath;
|
|
2402 fllist[i] = csinfo[i].flags;
|
|
2403 if (csinfo[i].fname != NULL)
|
|
2404 cs_release_csp(i, FALSE);
|
|
2405 }
|
|
2406
|
|
2407 /* rebuild the cscope connection list */
|
|
2408 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
2409 {
|
|
2410 if (dblist[i] != NULL)
|
|
2411 {
|
|
2412 cs_add_common(dblist[i], pplist[i], fllist[i]);
|
|
2413 if (p_csverbose)
|
|
2414 {
|
1372
|
2415 /* don't use smsg_attr() because we want to display the
|
7
|
2416 * connection number in the same line as
|
|
2417 * "Added cscope database..."
|
|
2418 */
|
|
2419 sprintf(buf, " (#%d)", i);
|
|
2420 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
|
|
2421 }
|
|
2422 }
|
|
2423 vim_free(dblist[i]);
|
|
2424 vim_free(pplist[i]);
|
|
2425 vim_free(fllist[i]);
|
|
2426 }
|
|
2427 vim_free(dblist);
|
|
2428 vim_free(pplist);
|
|
2429 vim_free(fllist);
|
|
2430
|
|
2431 if (p_csverbose)
|
|
2432 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
|
|
2433 return CSCOPE_SUCCESS;
|
|
2434 } /* cs_reset */
|
|
2435
|
|
2436
|
|
2437 /*
|
|
2438 * PRIVATE: cs_resolve_file
|
|
2439 *
|
|
2440 * construct the full pathname to a file found in the cscope database.
|
|
2441 * (Prepends ppath, if there is one and if it's not already prepended,
|
|
2442 * otherwise just uses the name found.)
|
|
2443 *
|
|
2444 * we need to prepend the prefix because on some cscope's (e.g., the one that
|
|
2445 * ships with Solaris 2.6), the output never has the prefix prepended.
|
|
2446 * contrast this with my development system (Digital Unix), which does.
|
|
2447 */
|
|
2448 static char *
|
|
2449 cs_resolve_file(i, name)
|
|
2450 int i;
|
|
2451 char *name;
|
|
2452 {
|
|
2453 char *fullname;
|
|
2454 int len;
|
|
2455
|
|
2456 /*
|
|
2457 * ppath is freed when we destroy the cscope connection.
|
|
2458 * fullname is freed after cs_make_vim_style_matches, after it's been
|
|
2459 * copied into the tag buffer used by vim
|
|
2460 */
|
835
|
2461 len = (int)(strlen(name) + 2);
|
7
|
2462 if (csinfo[i].ppath != NULL)
|
835
|
2463 len += (int)strlen(csinfo[i].ppath);
|
7
|
2464
|
|
2465 if ((fullname = (char *)alloc(len)) == NULL)
|
|
2466 return NULL;
|
|
2467
|
|
2468 /*
|
|
2469 * note/example: this won't work if the cscope output already starts
|
|
2470 * "../.." and the prefix path is also "../..". if something like this
|
|
2471 * happens, you are screwed up and need to fix how you're using cscope.
|
|
2472 */
|
|
2473 if (csinfo[i].ppath != NULL &&
|
|
2474 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
|
|
2475 (name[0] != '/')
|
|
2476 #ifdef WIN32
|
|
2477 && name[0] != '\\' && name[1] != ':'
|
|
2478 #endif
|
|
2479 )
|
|
2480 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
|
|
2481 else
|
|
2482 (void)sprintf(fullname, "%s", name);
|
|
2483
|
|
2484 return fullname;
|
|
2485 } /* cs_resolve_file */
|
|
2486
|
|
2487
|
|
2488 /*
|
|
2489 * PRIVATE: cs_show
|
|
2490 *
|
|
2491 * show all cscope connections
|
|
2492 */
|
|
2493 static int
|
|
2494 cs_show(eap)
|
1880
|
2495 exarg_T *eap UNUSED;
|
7
|
2496 {
|
|
2497 short i;
|
|
2498 if (cs_cnt_connections() == 0)
|
|
2499 MSG_PUTS(_("no cscope connections\n"));
|
|
2500 else
|
|
2501 {
|
|
2502 MSG_PUTS_ATTR(
|
|
2503 _(" # pid database name prepend path\n"),
|
|
2504 hl_attr(HLF_T));
|
|
2505 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
2506 {
|
|
2507 if (csinfo[i].fname == NULL)
|
|
2508 continue;
|
|
2509
|
|
2510 if (csinfo[i].ppath != NULL)
|
|
2511 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
|
|
2512 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
|
|
2513 else
|
|
2514 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
|
|
2515 i, (long)csinfo[i].pid, csinfo[i].fname);
|
|
2516 }
|
|
2517 }
|
|
2518
|
|
2519 wait_return(TRUE);
|
|
2520 return CSCOPE_SUCCESS;
|
|
2521 } /* cs_show */
|
|
2522
|
1385
|
2523
|
|
2524 /*
|
|
2525 * PUBLIC: cs_end
|
|
2526 *
|
|
2527 * Only called when VIM exits to quit any cscope sessions.
|
|
2528 */
|
|
2529 void
|
|
2530 cs_end()
|
|
2531 {
|
|
2532 int i;
|
|
2533
|
|
2534 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
|
|
2535 cs_release_csp(i, TRUE);
|
|
2536 }
|
|
2537
|
7
|
2538 #endif /* FEAT_CSCOPE */
|
|
2539
|
|
2540 /* the end */
|