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