Merged from the latest developing branch.
[MacVim.git] / src / if_cscope.c
blobc11fc2acbe799d62f21a6c15b3ed990d2d2734d1
1 /* vi:set ts=8 sts=4 sw=4:
3 * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com>
4 * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com>
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.
9 * See README.txt for an overview of the Vim source code.
12 #include "vim.h"
14 #if defined(FEAT_CSCOPE) || defined(PROTO)
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 "vimio.h"
26 #endif
27 #include "if_cscope.h"
29 static void cs_usage_msg __ARGS((csid_e x));
30 static int cs_add __ARGS((exarg_T *eap));
31 static void cs_stat_emsg __ARGS((char *fname));
32 static int cs_add_common __ARGS((char *, char *, char *));
33 static int cs_check_for_connections __ARGS((void));
34 static int cs_check_for_tags __ARGS((void));
35 static int cs_cnt_connections __ARGS((void));
36 static void cs_reading_emsg __ARGS((int idx));
37 static int cs_cnt_matches __ARGS((int idx));
38 static char * cs_create_cmd __ARGS((char *csoption, char *pattern));
39 static int cs_create_connection __ARGS((int i));
40 static void do_cscope_general __ARGS((exarg_T *eap, int make_split));
41 #ifdef FEAT_QUICKFIX
42 static void cs_file_results __ARGS((FILE *, int *));
43 #endif
44 static void cs_fill_results __ARGS((char *, int , int *, char ***,
45 char ***, int *));
46 static int cs_find __ARGS((exarg_T *eap));
47 static int cs_find_common __ARGS((char *opt, char *pat, int, int, int));
48 static int cs_help __ARGS((exarg_T *eap));
49 static void cs_init __ARGS((void));
50 static void clear_csinfo __ARGS((int i));
51 static int cs_insert_filelist __ARGS((char *, char *, char *,
52 struct stat *));
53 static int cs_kill __ARGS((exarg_T *eap));
54 static void cs_kill_execute __ARGS((int, char *));
55 static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
56 static char * cs_make_vim_style_matches __ARGS((char *, char *,
57 char *, char *));
58 static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
59 static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
60 static char * cs_pathcomponents __ARGS((char *path));
61 static void cs_print_tags_priv __ARGS((char **, char **, int));
62 static int cs_read_prompt __ARGS((int));
63 static void cs_release_csp __ARGS((int, int freefnpp));
64 static int cs_reset __ARGS((exarg_T *eap));
65 static char * cs_resolve_file __ARGS((int, char *));
66 static int cs_show __ARGS((exarg_T *eap));
69 static csinfo_T csinfo[CSCOPE_MAX_CONNECTIONS];
70 static int eap_arg_len; /* length of eap->arg, set in
71 cs_lookup_cmd() */
72 static cscmd_T cs_cmds[] =
74 { "add", cs_add,
75 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
76 { "find", cs_find,
77 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
78 { "help", cs_help,
79 N_("Show this message"), "help", 0 },
80 { "kill", cs_kill,
81 N_("Kill a connection"), "kill #", 0 },
82 { "reset", cs_reset,
83 N_("Reinit all connections"), "reset", 0 },
84 { "show", cs_show,
85 N_("Show connections"), "show", 0 },
86 { NULL, NULL, NULL, NULL, 0 }
89 static void
90 cs_usage_msg(x)
91 csid_e x;
93 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
96 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
98 static enum
100 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
101 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
102 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
103 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
104 } expand_what;
107 * Function given to ExpandGeneric() to obtain the cscope command
108 * expansion.
110 char_u *
111 get_cscope_name(xp, idx)
112 expand_T *xp UNUSED;
113 int idx;
115 int current_idx;
116 int i;
118 switch (expand_what)
120 case EXP_CSCOPE_SUBCMD:
121 /* Complete with sub-commands of ":cscope":
122 * add, find, help, kill, reset, show */
123 return (char_u *)cs_cmds[idx].name;
124 case EXP_SCSCOPE_SUBCMD:
125 /* Complete with sub-commands of ":scscope": same sub-commands as
126 * ":cscope" but skip commands which don't support split windows */
127 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
128 if (cs_cmds[i].cansplit)
129 if (current_idx++ == idx)
130 break;
131 return (char_u *)cs_cmds[i].name;
132 case EXP_CSCOPE_FIND:
134 const char *query_type[] =
136 "c", "d", "e", "f", "g", "i", "s", "t", NULL
139 /* Complete with query type of ":cscope find {query_type}".
140 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
141 * ..., 8) but only complete with letters, since numbers are
142 * redundant. */
143 return (char_u *)query_type[idx];
145 case EXP_CSCOPE_KILL:
147 static char_u connection[2];
149 /* ":cscope kill" accepts connection numbers or partial names of
150 * the pathname of the cscope database as argument. Only complete
151 * with connection numbers. -1 can also be used to kill all
152 * connections. */
153 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
155 if (csinfo[i].fname == NULL)
156 continue;
157 if (current_idx++ == idx)
159 /* Connection number fits in one character since
160 * CSCOPE_MAX_CONNECTIONS is < 10 */
161 connection[0] = i + '0';
162 connection[1] = NUL;
163 return connection;
166 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
168 default:
169 return NULL;
174 * Handle command line completion for :cscope command.
176 void
177 set_context_in_cscope_cmd(xp, arg, cmdidx)
178 expand_T *xp;
179 char_u *arg;
180 cmdidx_T cmdidx;
182 char_u *p;
184 /* Default: expand subcommands */
185 xp->xp_context = EXPAND_CSCOPE;
186 xp->xp_pattern = arg;
187 expand_what = (cmdidx == CMD_scscope)
188 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
190 /* (part of) subcommand already typed */
191 if (*arg != NUL)
193 p = skiptowhite(arg);
194 if (*p != NUL) /* past first word */
196 xp->xp_pattern = skipwhite(p);
197 if (*skiptowhite(xp->xp_pattern) != NUL)
198 xp->xp_context = EXPAND_NOTHING;
199 else if (STRNICMP(arg, "add", p - arg) == 0)
200 xp->xp_context = EXPAND_FILES;
201 else if (STRNICMP(arg, "kill", p - arg) == 0)
202 expand_what = EXP_CSCOPE_KILL;
203 else if (STRNICMP(arg, "find", p - arg) == 0)
204 expand_what = EXP_CSCOPE_FIND;
205 else
206 xp->xp_context = EXPAND_NOTHING;
211 #endif /* FEAT_CMDL_COMPL */
214 * PRIVATE: do_cscope_general
216 * Find the command, print help if invalid, and then call the corresponding
217 * command function.
219 static void
220 do_cscope_general(eap, make_split)
221 exarg_T *eap;
222 int make_split; /* whether to split window */
224 cscmd_T *cmdp;
226 cs_init();
227 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
229 cs_help(eap);
230 return;
233 #ifdef FEAT_WINDOWS
234 if (make_split)
236 if (!cmdp->cansplit)
238 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
239 return;
241 postponed_split = -1;
242 postponed_split_flags = cmdmod.split;
243 postponed_split_tab = cmdmod.tab;
245 #endif
247 cmdp->func(eap);
249 #ifdef FEAT_WINDOWS
250 postponed_split_flags = 0;
251 postponed_split_tab = 0;
252 #endif
256 * PUBLIC: do_cscope
258 void
259 do_cscope(eap)
260 exarg_T *eap;
262 do_cscope_general(eap, FALSE);
266 * PUBLIC: do_scscope
268 * same as do_cscope, but splits window, too.
270 void
271 do_scscope(eap)
272 exarg_T *eap;
274 do_cscope_general(eap, TRUE);
278 * PUBLIC: do_cstag
281 void
282 do_cstag(eap)
283 exarg_T *eap;
285 int ret = FALSE;
287 cs_init();
289 if (*eap->arg == NUL)
291 (void)EMSG(_("E562: Usage: cstag <ident>"));
292 return;
295 switch (p_csto)
297 case 0 :
298 if (cs_check_for_connections())
300 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
301 FALSE);
302 if (ret == FALSE)
304 cs_free_tags();
305 if (msg_col)
306 msg_putchar('\n');
308 if (cs_check_for_tags())
309 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
312 else if (cs_check_for_tags())
314 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
316 break;
317 case 1 :
318 if (cs_check_for_tags())
320 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
321 if (ret == FALSE)
323 if (msg_col)
324 msg_putchar('\n');
326 if (cs_check_for_connections())
328 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
329 FALSE, FALSE);
330 if (ret == FALSE)
331 cs_free_tags();
335 else if (cs_check_for_connections())
337 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
338 FALSE);
339 if (ret == FALSE)
340 cs_free_tags();
342 break;
343 default :
344 break;
347 if (!ret)
349 (void)EMSG(_("E257: cstag: tag not found"));
350 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
351 g_do_tagpreview = 0;
352 #endif
355 } /* do_cscope */
359 * PUBLIC: cs_find
361 * this simulates a vim_fgets(), but for cscope, returns the next line
362 * from the cscope output. should only be called from find_tags()
364 * returns TRUE if eof, FALSE otherwise
367 cs_fgets(buf, size)
368 char_u *buf;
369 int size;
371 char *p;
373 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
374 return TRUE;
375 vim_strncpy(buf, (char_u *)p, size - 1);
377 return FALSE;
378 } /* cs_fgets */
382 * PUBLIC: cs_free_tags
384 * called only from do_tag(), when popping the tag stack
386 void
387 cs_free_tags()
389 cs_manage_matches(NULL, NULL, -1, Free);
394 * PUBLIC: cs_print_tags
396 * called from do_tag()
398 void
399 cs_print_tags()
401 cs_manage_matches(NULL, NULL, -1, Print);
406 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
408 * Checks for the existence of a |cscope| connection. If no
409 * parameters are specified, then the function returns:
411 * 0, if cscope was not available (not compiled in), or if there
412 * are no cscope connections; or
413 * 1, if there is at least one cscope connection.
415 * If parameters are specified, then the value of {num}
416 * determines how existence of a cscope connection is checked:
418 * {num} Description of existence check
419 * ----- ------------------------------
420 * 0 Same as no parameters (e.g., "cscope_connection()").
421 * 1 Ignore {prepend}, and use partial string matches for
422 * {dbpath}.
423 * 2 Ignore {prepend}, and use exact string matches for
424 * {dbpath}.
425 * 3 Use {prepend}, use partial string matches for both
426 * {dbpath} and {prepend}.
427 * 4 Use {prepend}, use exact string matches for both
428 * {dbpath} and {prepend}.
430 * Note: All string comparisons are case sensitive!
432 #if defined(FEAT_EVAL) || defined(PROTO)
434 cs_connection(num, dbpath, ppath)
435 int num;
436 char_u *dbpath;
437 char_u *ppath;
439 int i;
441 if (num < 0 || num > 4 || (num > 0 && !dbpath))
442 return FALSE;
444 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
446 if (!csinfo[i].fname)
447 continue;
449 if (num == 0)
450 return TRUE;
452 switch (num)
454 case 1:
455 if (strstr(csinfo[i].fname, (char *)dbpath))
456 return TRUE;
457 break;
458 case 2:
459 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
460 return TRUE;
461 break;
462 case 3:
463 if (strstr(csinfo[i].fname, (char *)dbpath)
464 && ((!ppath && !csinfo[i].ppath)
465 || (ppath
466 && csinfo[i].ppath
467 && strstr(csinfo[i].ppath, (char *)ppath))))
468 return TRUE;
469 break;
470 case 4:
471 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
472 && ((!ppath && !csinfo[i].ppath)
473 || (ppath
474 && csinfo[i].ppath
475 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
476 return TRUE;
477 break;
481 return FALSE;
482 } /* cs_connection */
483 #endif
487 * PRIVATE functions
488 ****************************************************************************/
491 * PRIVATE: cs_add
493 * add cscope database or a directory name (to look for cscope.out)
494 * to the cscope connection list
496 * MAXPATHL 256
498 static int
499 cs_add(eap)
500 exarg_T *eap UNUSED;
502 char *fname, *ppath, *flags = NULL;
504 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
506 cs_usage_msg(Add);
507 return CSCOPE_FAILURE;
509 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
510 flags = strtok((char *)NULL, (const char *)" ");
512 return cs_add_common(fname, ppath, flags);
515 static void
516 cs_stat_emsg(fname)
517 char *fname;
519 char *stat_emsg = _("E563: stat(%s) error: %d");
520 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
522 if (buf != NULL)
524 (void)sprintf(buf, stat_emsg, fname, errno);
525 (void)EMSG(buf);
526 vim_free(buf);
528 else
529 (void)EMSG(_("E563: stat error"));
534 * PRIVATE: cs_add_common
536 * the common routine to add a new cscope connection. called by
537 * cs_add() and cs_reset(). i really don't like to do this, but this
538 * routine uses a number of goto statements.
540 static int
541 cs_add_common(arg1, arg2, flags)
542 char *arg1; /* filename - may contain environment variables */
543 char *arg2; /* prepend path - may contain environment variables */
544 char *flags;
546 struct stat statbuf;
547 int ret;
548 char *fname = NULL;
549 char *fname2 = NULL;
550 char *ppath = NULL;
551 int i;
553 /* get the filename (arg1), expand it, and try to stat it */
554 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
555 goto add_err;
557 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
558 ret = stat(fname, &statbuf);
559 if (ret < 0)
561 staterr:
562 if (p_csverbose)
563 cs_stat_emsg(fname);
564 goto add_err;
567 /* get the prepend path (arg2), expand it, and try to stat it */
568 if (arg2 != NULL)
570 struct stat statbuf2;
572 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
573 goto add_err;
575 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
576 ret = stat(ppath, &statbuf2);
577 if (ret < 0)
578 goto staterr;
581 /* if filename is a directory, append the cscope database name to it */
582 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
584 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
585 if (fname2 == NULL)
586 goto add_err;
588 while (fname[strlen(fname)-1] == '/'
589 #ifdef WIN32
590 || fname[strlen(fname)-1] == '\\'
591 #endif
594 fname[strlen(fname)-1] = '\0';
595 if (strlen(fname) == 0)
596 break;
598 if (fname[0] == '\0')
599 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
600 else
601 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
603 ret = stat(fname2, &statbuf);
604 if (ret < 0)
606 if (p_csverbose)
607 cs_stat_emsg(fname2);
608 goto add_err;
611 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
613 #if defined(UNIX)
614 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
615 #else
616 /* WIN32 - substitute define S_ISREG from os_unix.h */
617 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
618 #endif
620 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
622 else
624 if (p_csverbose)
625 (void)EMSG2(
626 _("E564: %s is not a directory or a valid cscope database"),
627 fname);
628 goto add_err;
631 if (i != -1)
633 if (cs_create_connection(i) == CSCOPE_FAILURE
634 || cs_read_prompt(i) == CSCOPE_FAILURE)
636 cs_release_csp(i, TRUE);
637 goto add_err;
640 if (p_csverbose)
642 msg_clr_eos();
643 (void)smsg_attr(hl_attr(HLF_R),
644 (char_u *)_("Added cscope database %s"),
645 csinfo[i].fname);
649 vim_free(fname);
650 vim_free(fname2);
651 vim_free(ppath);
652 return CSCOPE_SUCCESS;
654 add_err:
655 vim_free(fname2);
656 vim_free(fname);
657 vim_free(ppath);
658 return CSCOPE_FAILURE;
659 } /* cs_add_common */
662 static int
663 cs_check_for_connections()
665 return (cs_cnt_connections() > 0);
666 } /* cs_check_for_connections */
669 static int
670 cs_check_for_tags()
672 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
673 } /* cs_check_for_tags */
677 * PRIVATE: cs_cnt_connections
679 * count the number of cscope connections
681 static int
682 cs_cnt_connections()
684 short i;
685 short cnt = 0;
687 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
689 if (csinfo[i].fname != NULL)
690 cnt++;
692 return cnt;
693 } /* cs_cnt_connections */
695 static void
696 cs_reading_emsg(idx)
697 int idx; /* connection index */
699 EMSGN(_("E262: error reading cscope connection %ld"), idx);
702 #define CSREAD_BUFSIZE 2048
704 * PRIVATE: cs_cnt_matches
706 * count the number of matches for a given cscope connection.
708 static int
709 cs_cnt_matches(idx)
710 int idx;
712 char *stok;
713 char *buf;
714 int nlines;
716 buf = (char *)alloc(CSREAD_BUFSIZE);
717 if (buf == NULL)
718 return 0;
719 for (;;)
721 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
723 if (feof(csinfo[idx].fr_fp))
724 errno = EIO;
726 cs_reading_emsg(idx);
728 vim_free(buf);
729 return -1;
733 * If the database is out of date, or there's some other problem,
734 * cscope will output error messages before the number-of-lines output.
735 * Display/discard any output that doesn't match what we want.
736 * Accept "\S*cscope: X lines", also matches "mlcscope".
738 if ((stok = strtok(buf, (const char *)" ")) == NULL)
739 continue;
740 if (strstr((const char *)stok, "cscope:") == NULL)
741 continue;
743 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
744 continue;
745 nlines = atoi(stok);
746 if (nlines < 0)
748 nlines = 0;
749 break;
752 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
753 continue;
754 if (strncmp((const char *)stok, "lines", 5))
755 continue;
757 break;
760 vim_free(buf);
761 return nlines;
762 } /* cs_cnt_matches */
766 * PRIVATE: cs_create_cmd
768 * Creates the actual cscope command query from what the user entered.
770 static char *
771 cs_create_cmd(csoption, pattern)
772 char *csoption;
773 char *pattern;
775 char *cmd;
776 short search;
777 char *pat;
779 switch (csoption[0])
781 case '0' : case 's' :
782 search = 0;
783 break;
784 case '1' : case 'g' :
785 search = 1;
786 break;
787 case '2' : case 'd' :
788 search = 2;
789 break;
790 case '3' : case 'c' :
791 search = 3;
792 break;
793 case '4' : case 't' :
794 search = 4;
795 break;
796 case '6' : case 'e' :
797 search = 6;
798 break;
799 case '7' : case 'f' :
800 search = 7;
801 break;
802 case '8' : case 'i' :
803 search = 8;
804 break;
805 default :
806 (void)EMSG(_("E561: unknown cscope search type"));
807 cs_usage_msg(Find);
808 return NULL;
811 /* Skip white space before the patter, except for text and pattern search,
812 * they may want to use the leading white space. */
813 pat = pattern;
814 if (search != 4 && search != 6)
815 while vim_iswhite(*pat)
816 ++pat;
818 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
819 return NULL;
821 (void)sprintf(cmd, "%d%s", search, pat);
823 return cmd;
824 } /* cs_create_cmd */
828 * PRIVATE: cs_create_connection
830 * This piece of code was taken/adapted from nvi. do we need to add
831 * the BSD license notice?
833 static int
834 cs_create_connection(i)
835 int i;
837 #ifdef UNIX
838 int to_cs[2], from_cs[2];
839 #endif
840 int len;
841 char *prog, *cmd, *ppath = NULL;
842 #ifdef WIN32
843 int fd;
844 SECURITY_ATTRIBUTES sa;
845 PROCESS_INFORMATION pi;
846 STARTUPINFO si;
847 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
848 HANDLE stdin_rd, stdout_rd;
849 HANDLE stdout_wr, stdin_wr;
850 BOOL created;
851 # ifdef __BORLANDC__
852 # define OPEN_OH_ARGTYPE long
853 # else
854 # if (_MSC_VER >= 1300)
855 # define OPEN_OH_ARGTYPE intptr_t
856 # else
857 # define OPEN_OH_ARGTYPE long
858 # endif
859 # endif
860 #endif
862 #if defined(UNIX)
864 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
865 * from_cs[0] and writes to to_cs[1].
867 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
868 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
870 (void)EMSG(_("E566: Could not create cscope pipes"));
871 err_closing:
872 if (to_cs[0] != -1)
873 (void)close(to_cs[0]);
874 if (to_cs[1] != -1)
875 (void)close(to_cs[1]);
876 if (from_cs[0] != -1)
877 (void)close(from_cs[0]);
878 if (from_cs[1] != -1)
879 (void)close(from_cs[1]);
880 return CSCOPE_FAILURE;
883 switch (csinfo[i].pid = fork())
885 case -1:
886 (void)EMSG(_("E622: Could not fork for cscope"));
887 goto err_closing;
888 case 0: /* child: run cscope. */
889 if (dup2(to_cs[0], STDIN_FILENO) == -1)
890 PERROR("cs_create_connection 1");
891 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
892 PERROR("cs_create_connection 2");
893 if (dup2(from_cs[1], STDERR_FILENO) == -1)
894 PERROR("cs_create_connection 3");
896 /* close unused */
897 (void)close(to_cs[1]);
898 (void)close(from_cs[0]);
899 #else
900 /* WIN32 */
901 /* Create pipes to communicate with cscope */
902 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
903 sa.bInheritHandle = TRUE;
904 sa.lpSecurityDescriptor = NULL;
906 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
907 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
909 (void)EMSG(_("E566: Could not create cscope pipes"));
910 err_closing:
911 if (pipe_stdin)
913 CloseHandle(stdin_rd);
914 CloseHandle(stdin_wr);
916 if (pipe_stdout)
918 CloseHandle(stdout_rd);
919 CloseHandle(stdout_wr);
921 return CSCOPE_FAILURE;
923 #endif
924 /* expand the cscope exec for env var's */
925 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
927 #ifdef UNIX
928 return CSCOPE_FAILURE;
929 #else
930 /* WIN32 */
931 goto err_closing;
932 #endif
934 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
936 /* alloc space to hold the cscope command */
937 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
938 if (csinfo[i].ppath)
940 /* expand the prepend path for env var's */
941 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
943 vim_free(prog);
944 #ifdef UNIX
945 return CSCOPE_FAILURE;
946 #else
947 /* WIN32 */
948 goto err_closing;
949 #endif
951 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
953 len += (int)strlen(ppath);
956 if (csinfo[i].flags)
957 len += (int)strlen(csinfo[i].flags);
959 if ((cmd = (char *)alloc(len)) == NULL)
961 vim_free(prog);
962 vim_free(ppath);
963 #ifdef UNIX
964 return CSCOPE_FAILURE;
965 #else
966 /* WIN32 */
967 goto err_closing;
968 #endif
971 /* run the cscope command; is there execl for non-unix systems? */
972 #if defined(UNIX)
973 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
974 #else
975 /* WIN32 */
976 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
977 #endif
978 if (csinfo[i].ppath != NULL)
980 (void)strcat(cmd, " -P");
981 (void)strcat(cmd, csinfo[i].ppath);
983 if (csinfo[i].flags != NULL)
985 (void)strcat(cmd, " ");
986 (void)strcat(cmd, csinfo[i].flags);
988 # ifdef UNIX
989 /* on Win32 we still need prog */
990 vim_free(prog);
991 # endif
992 vim_free(ppath);
994 #if defined(UNIX)
995 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
996 PERROR(_("cs_create_connection exec failed"));
998 exit(127);
999 /* NOTREACHED */
1000 default: /* parent. */
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1005 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1010 /* close unused */
1011 (void)close(to_cs[0]);
1012 (void)close(from_cs[1]);
1014 break;
1017 #else
1018 /* WIN32 */
1019 /* Create a new process to run cscope and use pipes to talk with it */
1020 GetStartupInfo(&si);
1021 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1022 si.wShowWindow = SW_HIDE; /* Hide child application window */
1023 si.hStdOutput = stdout_wr;
1024 si.hStdError = stdout_wr;
1025 si.hStdInput = stdin_rd;
1026 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1027 NULL, NULL, &si, &pi);
1028 vim_free(prog);
1029 vim_free(cmd);
1031 if (!created)
1033 PERROR(_("cs_create_connection exec failed"));
1034 (void)EMSG(_("E623: Could not spawn cscope process"));
1035 goto err_closing;
1037 /* else */
1038 csinfo[i].pid = pi.dwProcessId;
1039 csinfo[i].hProc = pi.hProcess;
1040 CloseHandle(pi.hThread);
1042 /* TODO - tidy up after failure to create files on pipe handles. */
1043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1044 _O_TEXT|_O_APPEND)) < 0)
1045 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1046 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1047 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1048 _O_TEXT|_O_RDONLY)) < 0)
1049 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1050 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1052 /* Close handles for file descriptors inherited by the cscope process */
1053 CloseHandle(stdin_rd);
1054 CloseHandle(stdout_wr);
1056 #endif /* !UNIX */
1058 return CSCOPE_SUCCESS;
1059 } /* cs_create_connection */
1063 * PRIVATE: cs_find
1065 * query cscope using command line interface. parse the output and use tselect
1066 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1068 * returns TRUE if we jump to a tag or abort, FALSE if not.
1070 static int
1071 cs_find(eap)
1072 exarg_T *eap;
1074 char *opt, *pat;
1076 if (cs_check_for_connections() == FALSE)
1078 (void)EMSG(_("E567: no cscope connections"));
1079 return FALSE;
1082 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1084 cs_usage_msg(Find);
1085 return FALSE;
1088 pat = opt + strlen(opt) + 1;
1089 if (pat >= (char *)eap->arg + eap_arg_len)
1091 cs_usage_msg(Find);
1092 return FALSE;
1095 return cs_find_common(opt, pat, eap->forceit, TRUE,
1096 eap->cmdidx == CMD_lcscope);
1097 } /* cs_find */
1101 * PRIVATE: cs_find_common
1103 * common code for cscope find, shared by cs_find() and do_cstag()
1105 static int
1106 cs_find_common(opt, pat, forceit, verbose, use_ll)
1107 char *opt;
1108 char *pat;
1109 int forceit;
1110 int verbose;
1111 int use_ll;
1113 int i;
1114 char *cmd;
1115 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
1116 #ifdef FEAT_QUICKFIX
1117 char cmdletter;
1118 char *qfpos;
1119 #endif
1121 /* create the actual command to send to cscope */
1122 cmd = cs_create_cmd(opt, pat);
1123 if (cmd == NULL)
1124 return FALSE;
1126 /* send query to all open connections, then count the total number
1127 * of matches so we can alloc matchesp all in one swell foop
1129 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1130 nummatches[i] = 0;
1131 totmatches = 0;
1132 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1134 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
1135 continue;
1137 /* send cmd to cscope */
1138 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1139 (void)fflush(csinfo[i].to_fp);
1141 nummatches[i] = cs_cnt_matches(i);
1143 if (nummatches[i] > -1)
1144 totmatches += nummatches[i];
1146 if (nummatches[i] == 0)
1147 (void)cs_read_prompt(i);
1149 vim_free(cmd);
1151 if (totmatches == 0)
1153 char *nf = _("E259: no matches found for cscope query %s of %s");
1154 char *buf;
1156 if (!verbose)
1157 return FALSE;
1159 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
1160 if (buf == NULL)
1161 (void)EMSG(nf);
1162 else
1164 sprintf(buf, nf, opt, pat);
1165 (void)EMSG(buf);
1166 vim_free(buf);
1168 return FALSE;
1171 #ifdef FEAT_QUICKFIX
1172 /* get cmd letter */
1173 switch (opt[0])
1175 case '0' :
1176 cmdletter = 's';
1177 break;
1178 case '1' :
1179 cmdletter = 'g';
1180 break;
1181 case '2' :
1182 cmdletter = 'd';
1183 break;
1184 case '3' :
1185 cmdletter = 'c';
1186 break;
1187 case '4' :
1188 cmdletter = 't';
1189 break;
1190 case '6' :
1191 cmdletter = 'e';
1192 break;
1193 case '7' :
1194 cmdletter = 'f';
1195 break;
1196 case '8' :
1197 cmdletter = 'i';
1198 break;
1199 default :
1200 cmdletter = opt[0];
1203 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1204 if (qfpos != NULL)
1206 qfpos++;
1207 /* next symbol must be + or - */
1208 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1210 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1211 char *buf = (char *)alloc((unsigned)strlen(nf));
1213 /* strlen will be enough because we use chars */
1214 if (buf != NULL)
1216 sprintf(buf, nf, *qfpos, *(qfpos-1));
1217 (void)EMSG(buf);
1218 vim_free(buf);
1220 return FALSE;
1223 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
1225 /* fill error list */
1226 FILE *f;
1227 char_u *tmp = vim_tempname('c');
1228 qf_info_T *qi = NULL;
1229 win_T *wp = NULL;
1231 f = mch_fopen((char *)tmp, "w");
1232 if (f == NULL)
1233 EMSG2(_(e_notopen), tmp);
1234 else
1236 cs_file_results(f, nummatches);
1237 fclose(f);
1238 if (use_ll) /* Use location list */
1239 wp = curwin;
1240 /* '-' starts a new error list */
1241 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1242 *qfpos == '-') > 0)
1244 # ifdef FEAT_WINDOWS
1245 if (postponed_split != 0)
1247 win_split(postponed_split > 0 ? postponed_split : 0,
1248 postponed_split_flags);
1249 # ifdef FEAT_SCROLLBIND
1250 curwin->w_p_scb = FALSE;
1251 # endif
1252 postponed_split = 0;
1254 # endif
1255 if (use_ll)
1257 * In the location list window, use the displayed location
1258 * list. Otherwise, use the location list for the window.
1260 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1261 ? wp->w_llist_ref : wp->w_llist;
1262 qf_jump(qi, 0, 0, forceit);
1265 mch_remove(tmp);
1266 vim_free(tmp);
1267 return TRUE;
1269 else
1270 #endif /* FEAT_QUICKFIX */
1272 char **matches = NULL, **contexts = NULL;
1273 int matched = 0;
1275 /* read output */
1276 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1277 &contexts, &matched);
1278 if (matches == NULL)
1279 return FALSE;
1281 (void)cs_manage_matches(matches, contexts, matched, Store);
1283 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1286 } /* cs_find_common */
1289 * PRIVATE: cs_help
1291 * print help
1293 static int
1294 cs_help(eap)
1295 exarg_T *eap UNUSED;
1297 cscmd_T *cmdp = cs_cmds;
1299 (void)MSG_PUTS(_("cscope commands:\n"));
1300 while (cmdp->name != NULL)
1302 char *help = _(cmdp->help);
1303 int space_cnt = 30 - vim_strsize((char_u *)help);
1305 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1306 if (space_cnt < 0)
1307 space_cnt = 0;
1308 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1309 cmdp->name,
1310 help, space_cnt, " ",
1311 cmdp->usage);
1312 if (strcmp(cmdp->name, "find") == 0)
1313 MSG_PUTS(_("\n"
1314 " c: Find functions calling this function\n"
1315 " d: Find functions called by this function\n"
1316 " e: Find this egrep pattern\n"
1317 " f: Find this file\n"
1318 " g: Find this definition\n"
1319 " i: Find files #including this file\n"
1320 " s: Find this C symbol\n"
1321 " t: Find assignments to\n"));
1323 cmdp++;
1326 wait_return(TRUE);
1327 return 0;
1328 } /* cs_help */
1332 * PRIVATE: cs_init
1334 * initialize cscope structure if not already
1336 static void
1337 cs_init()
1339 short i;
1340 static int init_already = FALSE;
1342 if (init_already)
1343 return;
1345 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1346 clear_csinfo(i);
1348 init_already = TRUE;
1349 } /* cs_init */
1351 static void
1352 clear_csinfo(i)
1353 int i;
1355 csinfo[i].fname = NULL;
1356 csinfo[i].ppath = NULL;
1357 csinfo[i].flags = NULL;
1358 #if defined(UNIX)
1359 csinfo[i].st_dev = (dev_t)0;
1360 csinfo[i].st_ino = (ino_t)0;
1361 #else
1362 csinfo[i].nVolume = 0;
1363 csinfo[i].nIndexHigh = 0;
1364 csinfo[i].nIndexLow = 0;
1365 #endif
1366 csinfo[i].pid = 0;
1367 csinfo[i].fr_fp = NULL;
1368 csinfo[i].to_fp = NULL;
1369 #if defined(WIN32)
1370 csinfo[i].hProc = NULL;
1371 #endif
1374 #ifndef UNIX
1375 static char *GetWin32Error __ARGS((void));
1377 static char *
1378 GetWin32Error()
1380 char *msg = NULL;
1381 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1382 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1383 if (msg != NULL)
1385 /* remove trailing \r\n */
1386 char *pcrlf = strstr(msg, "\r\n");
1387 if (pcrlf != NULL)
1388 *pcrlf = '\0';
1390 return msg;
1392 #endif
1395 * PRIVATE: cs_insert_filelist
1397 * insert a new cscope database filename into the filelist
1399 static int
1400 cs_insert_filelist(fname, ppath, flags, sb)
1401 char *fname;
1402 char *ppath;
1403 char *flags;
1404 struct stat *sb UNUSED;
1406 short i, j;
1407 #ifndef UNIX
1408 HANDLE hFile;
1409 BY_HANDLE_FILE_INFORMATION bhfi;
1411 vim_memset(&bhfi, 0, sizeof(bhfi));
1412 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1413 if (!mch_windows95())
1415 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1416 FILE_ATTRIBUTE_NORMAL, NULL);
1417 if (hFile == INVALID_HANDLE_VALUE)
1419 if (p_csverbose)
1421 char *cant_msg = _("E625: cannot open cscope database: %s");
1422 char *winmsg = GetWin32Error();
1424 if (winmsg != NULL)
1426 (void)EMSG2(cant_msg, winmsg);
1427 LocalFree(winmsg);
1429 else
1430 /* subst filename if can't get error text */
1431 (void)EMSG2(cant_msg, fname);
1433 return -1;
1435 if (!GetFileInformationByHandle(hFile, &bhfi))
1437 CloseHandle(hFile);
1438 if (p_csverbose)
1439 (void)EMSG(_("E626: cannot get cscope database information"));
1440 return -1;
1442 CloseHandle(hFile);
1444 #endif
1446 i = -1; /* can be set to the index of an empty item in csinfo */
1447 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1449 if (csinfo[j].fname != NULL
1450 #if defined(UNIX)
1451 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1452 #else
1453 /* compare pathnames first */
1454 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
1455 /* if not Windows 9x, test index file attributes too */
1456 || (!mch_windows95()
1457 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1458 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1459 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1460 #endif
1463 if (p_csverbose)
1464 (void)EMSG(_("E568: duplicate cscope database not added"));
1465 return -1;
1468 if (csinfo[j].fname == NULL && i == -1)
1469 i = j; /* remember first empty entry */
1472 if (i == -1)
1474 if (p_csverbose)
1475 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1476 return -1;
1479 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
1480 return -1;
1482 (void)strcpy(csinfo[i].fname, (const char *)fname);
1484 if (ppath != NULL)
1486 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
1488 vim_free(csinfo[i].fname);
1489 csinfo[i].fname = NULL;
1490 return -1;
1492 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1493 } else
1494 csinfo[i].ppath = NULL;
1496 if (flags != NULL)
1498 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
1500 vim_free(csinfo[i].fname);
1501 vim_free(csinfo[i].ppath);
1502 csinfo[i].fname = NULL;
1503 csinfo[i].ppath = NULL;
1504 return -1;
1506 (void)strcpy(csinfo[i].flags, (const char *)flags);
1507 } else
1508 csinfo[i].flags = NULL;
1510 #if defined(UNIX)
1511 csinfo[i].st_dev = sb->st_dev;
1512 csinfo[i].st_ino = sb->st_ino;
1514 #else
1515 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1516 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1517 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1518 #endif
1519 return i;
1520 } /* cs_insert_filelist */
1524 * PRIVATE: cs_lookup_cmd
1526 * find cscope command in command table
1528 static cscmd_T *
1529 cs_lookup_cmd(eap)
1530 exarg_T *eap;
1532 cscmd_T *cmdp;
1533 char *stok;
1534 size_t len;
1536 if (eap->arg == NULL)
1537 return NULL;
1539 /* Store length of eap->arg before it gets modified by strtok(). */
1540 eap_arg_len = (int)STRLEN(eap->arg);
1542 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1543 return NULL;
1545 len = strlen(stok);
1546 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1548 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1549 return (cmdp);
1551 return NULL;
1552 } /* cs_lookup_cmd */
1556 * PRIVATE: cs_kill
1558 * nuke em
1560 static int
1561 cs_kill(eap)
1562 exarg_T *eap UNUSED;
1564 char *stok;
1565 short i;
1567 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1569 cs_usage_msg(Kill);
1570 return CSCOPE_FAILURE;
1573 /* only single digit positive and negative integers are allowed */
1574 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1575 || (strlen(stok) < 3 && stok[0] == '-'
1576 && VIM_ISDIGIT((int)(stok[1]))))
1577 i = atoi(stok);
1578 else
1580 /* It must be part of a name. We will try to find a match
1581 * within all the names in the csinfo data structure
1583 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1585 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1586 break;
1590 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1591 && i != -1)
1593 if (p_csverbose)
1594 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1596 else
1598 if (i == -1)
1600 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1602 if (csinfo[i].fname)
1603 cs_kill_execute(i, csinfo[i].fname);
1606 else
1607 cs_kill_execute(i, stok);
1610 return 0;
1611 } /* cs_kill */
1615 * PRIVATE: cs_kill_execute
1617 * Actually kills a specific cscope connection.
1619 static void
1620 cs_kill_execute(i, cname)
1621 int i; /* cscope table index */
1622 char *cname; /* cscope database name */
1624 if (p_csverbose)
1626 msg_clr_eos();
1627 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1628 (char_u *)_("cscope connection %s closed"), cname);
1630 cs_release_csp(i, TRUE);
1635 * PRIVATE: cs_make_vim_style_matches
1637 * convert the cscope output into into a ctags style entry (as might be found
1638 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1639 * the type of the tag you are looking for. for example, in Darren Hiebert's
1640 * ctags (the one that comes with vim), #define's use a line number to find the
1641 * tag in a file while function definitions use a regexp search pattern.
1643 * i'm going to always use the line number because cscope does something
1644 * quirky (and probably other things i don't know about):
1646 * if you have "# define" in your source file, which is
1647 * perfectly legal, cscope thinks you have "#define". this
1648 * will result in a failed regexp search. :(
1650 * besides, even if this particular case didn't happen, the search pattern
1651 * would still have to be modified to escape all the special regular expression
1652 * characters to comply with ctags formatting.
1654 static char *
1655 cs_make_vim_style_matches(fname, slno, search, tagstr)
1656 char *fname;
1657 char *slno;
1658 char *search;
1659 char *tagstr;
1661 /* vim style is ctags:
1663 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1665 * but as mentioned above, we'll always use the line number and
1666 * put the search pattern (if one exists) as "extra"
1668 * buf is used as part of vim's method of handling tags, and
1669 * (i think) vim frees it when you pop your tags and get replaced
1670 * by new ones on the tag stack.
1672 char *buf;
1673 int amt;
1675 if (search != NULL)
1677 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
1678 if ((buf = (char *)alloc(amt)) == NULL)
1679 return NULL;
1681 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1683 else
1685 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
1686 if ((buf = (char *)alloc(amt)) == NULL)
1687 return NULL;
1689 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1692 return buf;
1693 } /* cs_make_vim_style_matches */
1697 * PRIVATE: cs_manage_matches
1699 * this is kind of hokey, but i don't see an easy way round this..
1701 * Store: keep a ptr to the (malloc'd) memory of matches originally
1702 * generated from cs_find(). the matches are originally lines directly
1703 * from cscope output, but transformed to look like something out of a
1704 * ctags. see cs_make_vim_style_matches for more details.
1706 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1707 * the next line from the cscope output. it basically keeps track of which
1708 * lines have been "used" and returns the next one.
1710 * Free: frees up everything and resets
1712 * Print: prints the tags
1714 static char *
1715 cs_manage_matches(matches, contexts, totmatches, cmd)
1716 char **matches;
1717 char **contexts;
1718 int totmatches;
1719 mcmd_e cmd;
1721 static char **mp = NULL;
1722 static char **cp = NULL;
1723 static int cnt = -1;
1724 static int next = -1;
1725 char *p = NULL;
1727 switch (cmd)
1729 case Store:
1730 assert(matches != NULL);
1731 assert(totmatches > 0);
1732 if (mp != NULL || cp != NULL)
1733 (void)cs_manage_matches(NULL, NULL, -1, Free);
1734 mp = matches;
1735 cp = contexts;
1736 cnt = totmatches;
1737 next = 0;
1738 break;
1739 case Get:
1740 if (next >= cnt)
1741 return NULL;
1743 p = mp[next];
1744 next++;
1745 break;
1746 case Free:
1747 if (mp != NULL)
1749 if (cnt > 0)
1750 while (cnt--)
1752 vim_free(mp[cnt]);
1753 if (cp != NULL)
1754 vim_free(cp[cnt]);
1756 vim_free(mp);
1757 vim_free(cp);
1759 mp = NULL;
1760 cp = NULL;
1761 cnt = 0;
1762 next = 0;
1763 break;
1764 case Print:
1765 cs_print_tags_priv(mp, cp, cnt);
1766 break;
1767 default: /* should not reach here */
1768 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1769 return NULL;
1772 return p;
1773 } /* cs_manage_matches */
1777 * PRIVATE: cs_parse_results
1779 * parse cscope output
1781 static char *
1782 cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1783 int cnumber;
1784 char *buf;
1785 int bufsize;
1786 char **context;
1787 char **linenumber;
1788 char **search;
1790 int ch;
1791 char *p;
1792 char *name;
1794 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1796 if (feof(csinfo[cnumber].fr_fp))
1797 errno = EIO;
1799 cs_reading_emsg(cnumber);
1801 return NULL;
1804 /* If the line's too long for the buffer, discard it. */
1805 if ((p = strchr(buf, '\n')) == NULL)
1807 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1809 return NULL;
1811 *p = '\0';
1814 * cscope output is in the following format:
1816 * <filename> <context> <line number> <pattern>
1818 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1819 return NULL;
1820 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1821 return NULL;
1822 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1823 return NULL;
1824 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1826 /* --- nvi ---
1827 * If the file is older than the cscope database, that is,
1828 * the database was built since the file was last modified,
1829 * or there wasn't a search string, use the line number.
1831 if (strcmp(*search, "<unknown>") == 0)
1832 *search = NULL;
1834 name = cs_resolve_file(cnumber, name);
1835 return name;
1838 #ifdef FEAT_QUICKFIX
1840 * PRIVATE: cs_file_results
1842 * write cscope find results to file
1844 static void
1845 cs_file_results(f, nummatches_a)
1846 FILE *f;
1847 int *nummatches_a;
1849 int i, j;
1850 char *buf;
1851 char *search, *slno;
1852 char *fullname;
1853 char *cntx;
1854 char *context;
1856 buf = (char *)alloc(CSREAD_BUFSIZE);
1857 if (buf == NULL)
1858 return;
1860 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1862 if (nummatches_a[i] < 1)
1863 continue;
1865 for (j = 0; j < nummatches_a[i]; j++)
1867 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1868 &slno, &search)) == NULL)
1869 continue;
1871 context = (char *)alloc((unsigned)strlen(cntx)+5);
1872 if (context == NULL)
1873 continue;
1875 if (strcmp(cntx, "<global>")==0)
1876 strcpy(context, "<<global>>");
1877 else
1878 sprintf(context, "<<%s>>", cntx);
1880 if (search == NULL)
1881 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1882 else
1883 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1885 vim_free(context);
1886 vim_free(fullname);
1887 } /* for all matches */
1889 (void)cs_read_prompt(i);
1891 } /* for all cscope connections */
1892 vim_free(buf);
1894 #endif
1897 * PRIVATE: cs_fill_results
1899 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1900 * into ctags format
1901 * When there are no matches sets "*matches_p" to NULL.
1903 static void
1904 cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1905 char *tagstr;
1906 int totmatches;
1907 int *nummatches_a;
1908 char ***matches_p;
1909 char ***cntxts_p;
1910 int *matched;
1912 int i, j;
1913 char *buf;
1914 char *search, *slno;
1915 int totsofar = 0;
1916 char **matches = NULL;
1917 char **cntxts = NULL;
1918 char *fullname;
1919 char *cntx;
1921 assert(totmatches > 0);
1923 buf = (char *)alloc(CSREAD_BUFSIZE);
1924 if (buf == NULL)
1925 return;
1927 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1928 goto parse_out;
1929 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1930 goto parse_out;
1932 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1934 if (nummatches_a[i] < 1)
1935 continue;
1937 for (j = 0; j < nummatches_a[i]; j++)
1939 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1940 &slno, &search)) == NULL)
1941 continue;
1943 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1944 search, tagstr);
1946 vim_free(fullname);
1948 if (strcmp(cntx, "<global>") == 0)
1949 cntxts[totsofar] = NULL;
1950 else
1951 /* note: if vim_strsave returns NULL, then the context
1952 * will be "<global>", which is misleading.
1954 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1956 if (matches[totsofar] != NULL)
1957 totsofar++;
1959 } /* for all matches */
1961 (void)cs_read_prompt(i);
1963 } /* for all cscope connections */
1965 parse_out:
1966 if (totsofar == 0)
1968 /* No matches, free the arrays and return NULL in "*matches_p". */
1969 vim_free(matches);
1970 matches = NULL;
1971 vim_free(cntxts);
1972 cntxts = NULL;
1974 *matched = totsofar;
1975 *matches_p = matches;
1976 *cntxts_p = cntxts;
1978 vim_free(buf);
1979 } /* cs_fill_results */
1982 /* get the requested path components */
1983 static char *
1984 cs_pathcomponents(path)
1985 char *path;
1987 int i;
1988 char *s;
1990 if (p_cspc == 0)
1991 return path;
1993 s = path + strlen(path) - 1;
1994 for (i = 0; i < p_cspc; ++i)
1995 while (s > path && *--s != '/'
1996 #ifdef WIN32
1997 && *--s != '\\'
1998 #endif
2001 if ((s > path && *s == '/')
2002 #ifdef WIN32
2003 || (s > path && *s == '\\')
2004 #endif
2006 ++s;
2007 return s;
2011 * PRIVATE: cs_print_tags_priv
2013 * called from cs_manage_matches()
2015 static void
2016 cs_print_tags_priv(matches, cntxts, num_matches)
2017 char **matches;
2018 char **cntxts;
2019 int num_matches;
2021 char *buf = NULL;
2022 int bufsize = 0; /* Track available bufsize */
2023 int newsize = 0;
2024 char *ptag;
2025 char *fname, *lno, *extra, *tbuf;
2026 int i, idx, num;
2027 char *globalcntx = "GLOBAL";
2028 char *cntxformat = " <<%s>>";
2029 char *context;
2030 char *cstag_msg = _("Cscope tag: %s");
2031 char *csfmt_str = "%4d %6s ";
2033 assert (num_matches > 0);
2035 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
2036 return;
2038 strcpy(tbuf, matches[0]);
2039 ptag = strtok(tbuf, "\t");
2041 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
2042 buf = (char *)alloc(newsize);
2043 if (buf != NULL)
2045 bufsize = newsize;
2046 (void)sprintf(buf, cstag_msg, ptag);
2047 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2050 vim_free(tbuf);
2052 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2053 msg_advance(msg_col + 2);
2054 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2056 num = 1;
2057 for (i = 0; i < num_matches; i++)
2059 idx = i;
2061 /* if we really wanted to, we could avoid this malloc and strcpy
2062 * by parsing matches[i] on the fly and placing stuff into buf
2063 * directly, but that's too much of a hassle
2065 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
2066 continue;
2067 (void)strcpy(tbuf, matches[idx]);
2069 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
2070 continue;
2071 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2072 continue;
2073 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
2074 continue;
2075 extra = strtok(NULL, (const char *)"\t");
2077 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2079 /* hopefully 'num' (num of matches) will be less than 10^16 */
2080 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
2081 if (bufsize < newsize)
2083 buf = (char *)vim_realloc(buf, newsize);
2084 if (buf == NULL)
2085 bufsize = 0;
2086 else
2087 bufsize = newsize;
2089 if (buf != NULL)
2091 /* csfmt_str = "%4d %6s "; */
2092 (void)sprintf(buf, csfmt_str, num, lno);
2093 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2095 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2097 /* compute the required space for the context */
2098 if (cntxts[idx] != NULL)
2099 context = cntxts[idx];
2100 else
2101 context = globalcntx;
2102 newsize = (int)(strlen(context) + strlen(cntxformat));
2104 if (bufsize < newsize)
2106 buf = (char *)vim_realloc(buf, newsize);
2107 if (buf == NULL)
2108 bufsize = 0;
2109 else
2110 bufsize = newsize;
2112 if (buf != NULL)
2114 (void)sprintf(buf, cntxformat, context);
2116 /* print the context only if it fits on the same line */
2117 if (msg_col + (int)strlen(buf) >= (int)Columns)
2118 msg_putchar('\n');
2119 msg_advance(12);
2120 MSG_PUTS_LONG(buf);
2121 msg_putchar('\n');
2123 if (extra != NULL)
2125 msg_advance(13);
2126 MSG_PUTS_LONG(extra);
2129 vim_free(tbuf); /* only after printing extra due to strtok use */
2131 if (msg_col)
2132 msg_putchar('\n');
2134 ui_breakcheck();
2135 if (got_int)
2137 got_int = FALSE; /* don't print any more matches */
2138 break;
2141 num++;
2142 } /* for all matches */
2144 vim_free(buf);
2145 } /* cs_print_tags_priv */
2149 * PRIVATE: cs_read_prompt
2151 * read a cscope prompt (basically, skip over the ">> ")
2153 static int
2154 cs_read_prompt(i)
2155 int i;
2157 int ch;
2158 char *buf = NULL; /* buffer for possible error message from cscope */
2159 int bufpos = 0;
2160 char *cs_emsg;
2161 int maxlen;
2162 static char *eprompt = "Press the RETURN key to continue:";
2163 int epromptlen = (int)strlen(eprompt);
2164 int n;
2166 cs_emsg = _("E609: Cscope error: %s");
2167 /* compute maximum allowed len for Cscope error message */
2168 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2170 for (;;)
2172 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2173 /* if there is room and char is printable */
2174 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2176 if (buf == NULL) /* lazy buffer allocation */
2177 buf = (char *)alloc(maxlen);
2178 if (buf != NULL)
2180 /* append character to the message */
2181 buf[bufpos++] = ch;
2182 buf[bufpos] = NUL;
2183 if (bufpos >= epromptlen
2184 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2186 /* remove eprompt from buf */
2187 buf[bufpos - epromptlen] = NUL;
2189 /* print message to user */
2190 (void)EMSG2(cs_emsg, buf);
2192 /* send RETURN to cscope */
2193 (void)putc('\n', csinfo[i].to_fp);
2194 (void)fflush(csinfo[i].to_fp);
2196 /* clear buf */
2197 bufpos = 0;
2198 buf[bufpos] = NUL;
2203 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2205 if (n > 0)
2206 ch = getc(csinfo[i].fr_fp);
2207 if (ch == EOF)
2209 PERROR("cs_read_prompt EOF");
2210 if (buf != NULL && buf[0] != NUL)
2211 (void)EMSG2(cs_emsg, buf);
2212 else if (p_csverbose)
2213 cs_reading_emsg(i); /* don't have additional information */
2214 cs_release_csp(i, TRUE);
2215 vim_free(buf);
2216 return CSCOPE_FAILURE;
2219 if (ch != CSCOPE_PROMPT[n])
2221 ch = EOF;
2222 break;
2226 if (ch == EOF)
2227 continue; /* didn't find the prompt */
2228 break; /* did find the prompt */
2231 vim_free(buf);
2232 return CSCOPE_SUCCESS;
2235 #if defined(UNIX) && defined(SIGALRM)
2237 * Used to catch and ignore SIGALRM below.
2239 static RETSIGTYPE
2240 sig_handler SIGDEFARG(sigarg)
2242 /* do nothing */
2243 SIGRETURN;
2245 #endif
2248 * PRIVATE: cs_release_csp
2250 * Does the actual free'ing for the cs ptr with an optional flag of whether
2251 * or not to free the filename. Called by cs_kill and cs_reset.
2253 static void
2254 cs_release_csp(i, freefnpp)
2255 int i;
2256 int freefnpp;
2259 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2261 if (csinfo[i].to_fp != NULL)
2263 (void)fputs("q\n", csinfo[i].to_fp);
2264 (void)fflush(csinfo[i].to_fp);
2266 #if defined(UNIX)
2268 int waitpid_errno;
2269 int pstat;
2270 pid_t pid;
2272 # if defined(HAVE_SIGACTION)
2273 struct sigaction sa, old;
2275 /* Use sigaction() to limit the waiting time to two seconds. */
2276 sigemptyset(&sa.sa_mask);
2277 sa.sa_handler = sig_handler;
2278 sa.sa_flags = SA_NODEFER;
2279 sigaction(SIGALRM, &sa, &old);
2280 alarm(2); /* 2 sec timeout */
2282 /* Block until cscope exits or until timer expires */
2283 pid = waitpid(csinfo[i].pid, &pstat, 0);
2284 waitpid_errno = errno;
2286 /* cancel pending alarm if still there and restore signal */
2287 alarm(0);
2288 sigaction(SIGALRM, &old, NULL);
2289 # else
2290 int waited;
2292 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2293 * to give cscope a chance to exit quickly. */
2294 sleep(0);
2295 for (waited = 0; waited < 40; ++waited)
2297 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
2298 waitpid_errno = errno;
2299 if (pid != 0)
2300 break; /* break unless the process is still running */
2301 mch_delay(50L, FALSE); /* sleep 50 ms */
2303 # endif
2305 * If the cscope process is still running: kill it.
2306 * Safety check: If the PID would be zero here, the entire X session
2307 * would be killed. -1 and 1 are dangerous as well.
2309 if (pid < 0 && csinfo[i].pid > 1)
2311 # ifdef ECHILD
2312 int alive = TRUE;
2314 if (waitpid_errno == ECHILD)
2317 * When using 'vim -g', vim is forked and cscope process is
2318 * no longer a child process but a sibling. So waitpid()
2319 * fails with errno being ECHILD (No child processes).
2320 * Don't send SIGKILL to cscope immediately but wait
2321 * (polling) for it to exit normally as result of sending
2322 * the "q" command, hence giving it a chance to clean up
2323 * its temporary files.
2325 int waited;
2327 sleep(0);
2328 for (waited = 0; waited < 40; ++waited)
2330 /* Check whether cscope process is still alive */
2331 if (kill(csinfo[i].pid, 0) != 0)
2333 alive = FALSE; /* cscope process no longer exists */
2334 break;
2336 mch_delay(50L, FALSE); /* sleep 50ms */
2339 if (alive)
2340 # endif
2342 kill(csinfo[i].pid, SIGKILL);
2343 (void)waitpid(csinfo[i].pid, &pstat, 0);
2347 #else /* !UNIX */
2348 if (csinfo[i].hProc != NULL)
2350 /* Give cscope a chance to exit normally */
2351 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2352 TerminateProcess(csinfo[i].hProc, 0);
2353 CloseHandle(csinfo[i].hProc);
2355 #endif
2357 if (csinfo[i].fr_fp != NULL)
2358 (void)fclose(csinfo[i].fr_fp);
2359 if (csinfo[i].to_fp != NULL)
2360 (void)fclose(csinfo[i].to_fp);
2362 if (freefnpp)
2364 vim_free(csinfo[i].fname);
2365 vim_free(csinfo[i].ppath);
2366 vim_free(csinfo[i].flags);
2369 clear_csinfo(i);
2370 } /* cs_release_csp */
2374 * PRIVATE: cs_reset
2376 * calls cs_kill on all cscope connections then reinits
2378 static int
2379 cs_reset(eap)
2380 exarg_T *eap UNUSED;
2382 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2383 int i;
2384 char buf[20]; /* for sprintf " (#%d)" */
2386 /* malloc our db and ppath list */
2387 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2388 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2389 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2390 if (dblist == NULL || pplist == NULL || fllist == NULL)
2392 vim_free(dblist);
2393 vim_free(pplist);
2394 vim_free(fllist);
2395 return CSCOPE_FAILURE;
2398 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2400 dblist[i] = csinfo[i].fname;
2401 pplist[i] = csinfo[i].ppath;
2402 fllist[i] = csinfo[i].flags;
2403 if (csinfo[i].fname != NULL)
2404 cs_release_csp(i, FALSE);
2407 /* rebuild the cscope connection list */
2408 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2410 if (dblist[i] != NULL)
2412 cs_add_common(dblist[i], pplist[i], fllist[i]);
2413 if (p_csverbose)
2415 /* don't use smsg_attr() because we want to display the
2416 * connection number in the same line as
2417 * "Added cscope database..."
2419 sprintf(buf, " (#%d)", i);
2420 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2423 vim_free(dblist[i]);
2424 vim_free(pplist[i]);
2425 vim_free(fllist[i]);
2427 vim_free(dblist);
2428 vim_free(pplist);
2429 vim_free(fllist);
2431 if (p_csverbose)
2432 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2433 return CSCOPE_SUCCESS;
2434 } /* cs_reset */
2438 * PRIVATE: cs_resolve_file
2440 * construct the full pathname to a file found in the cscope database.
2441 * (Prepends ppath, if there is one and if it's not already prepended,
2442 * otherwise just uses the name found.)
2444 * we need to prepend the prefix because on some cscope's (e.g., the one that
2445 * ships with Solaris 2.6), the output never has the prefix prepended.
2446 * contrast this with my development system (Digital Unix), which does.
2448 static char *
2449 cs_resolve_file(i, name)
2450 int i;
2451 char *name;
2453 char *fullname;
2454 int len;
2457 * ppath is freed when we destroy the cscope connection.
2458 * fullname is freed after cs_make_vim_style_matches, after it's been
2459 * copied into the tag buffer used by vim
2461 len = (int)(strlen(name) + 2);
2462 if (csinfo[i].ppath != NULL)
2463 len += (int)strlen(csinfo[i].ppath);
2465 if ((fullname = (char *)alloc(len)) == NULL)
2466 return NULL;
2469 * note/example: this won't work if the cscope output already starts
2470 * "../.." and the prefix path is also "../..". if something like this
2471 * happens, you are screwed up and need to fix how you're using cscope.
2473 if (csinfo[i].ppath != NULL &&
2474 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2475 (name[0] != '/')
2476 #ifdef WIN32
2477 && name[0] != '\\' && name[1] != ':'
2478 #endif
2480 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2481 else
2482 (void)sprintf(fullname, "%s", name);
2484 return fullname;
2485 } /* cs_resolve_file */
2489 * PRIVATE: cs_show
2491 * show all cscope connections
2493 static int
2494 cs_show(eap)
2495 exarg_T *eap UNUSED;
2497 short i;
2498 if (cs_cnt_connections() == 0)
2499 MSG_PUTS(_("no cscope connections\n"));
2500 else
2502 MSG_PUTS_ATTR(
2503 _(" # pid database name prepend path\n"),
2504 hl_attr(HLF_T));
2505 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2507 if (csinfo[i].fname == NULL)
2508 continue;
2510 if (csinfo[i].ppath != NULL)
2511 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2512 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2513 else
2514 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2515 i, (long)csinfo[i].pid, csinfo[i].fname);
2519 wait_return(TRUE);
2520 return CSCOPE_SUCCESS;
2521 } /* cs_show */
2525 * PUBLIC: cs_end
2527 * Only called when VIM exits to quit any cscope sessions.
2529 void
2530 cs_end()
2532 int i;
2534 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2535 cs_release_csp(i, TRUE);
2538 #endif /* FEAT_CSCOPE */
2540 /* the end */