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