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