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