Merged from the latest developing branch.
[MacVim.git] / src / if_cscope.c
blob1b0a8cdad2134c011137224e17e9a5aa5bfbf830
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 }
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 /*ARGSUSED*/
111 char_u *
112 get_cscope_name(xp, idx)
113 expand_T *xp;
114 int idx;
116 int current_idx;
117 int i;
119 switch (expand_what)
121 case EXP_CSCOPE_SUBCMD:
122 /* Complete with sub-commands of ":cscope":
123 * add, find, help, kill, reset, show */
124 return (char_u *)cs_cmds[idx].name;
125 case EXP_SCSCOPE_SUBCMD:
126 /* Complete with sub-commands of ":scscope": same sub-commands as
127 * ":cscope" but skip commands which don't support split windows */
128 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
129 if (cs_cmds[i].cansplit)
130 if (current_idx++ == idx)
131 break;
132 return (char_u *)cs_cmds[i].name;
133 case EXP_CSCOPE_FIND:
135 const char *query_type[] =
137 "c", "d", "e", "f", "g", "i", "s", "t", NULL
140 /* Complete with query type of ":cscope find {query_type}".
141 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
142 * ..., 8) but only complete with letters, since numbers are
143 * redundant. */
144 return (char_u *)query_type[idx];
146 case EXP_CSCOPE_KILL:
148 static char_u connection[2];
150 /* ":cscope kill" accepts connection numbers or partial names of
151 * the pathname of the cscope database as argument. Only complete
152 * with connection numbers. -1 can also be used to kill all
153 * connections. */
154 for (i = 0, current_idx = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
156 if (csinfo[i].fname == NULL)
157 continue;
158 if (current_idx++ == idx)
160 /* Connection number fits in one character since
161 * CSCOPE_MAX_CONNECTIONS is < 10 */
162 connection[0] = i + '0';
163 connection[1] = NUL;
164 return connection;
167 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
169 default:
170 return NULL;
175 * Handle command line completion for :cscope command.
177 void
178 set_context_in_cscope_cmd(xp, arg, cmdidx)
179 expand_T *xp;
180 char_u *arg;
181 cmdidx_T cmdidx;
183 char_u *p;
185 /* Default: expand subcommands */
186 xp->xp_context = EXPAND_CSCOPE;
187 xp->xp_pattern = arg;
188 expand_what = (cmdidx == CMD_scscope)
189 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
191 /* (part of) subcommand already typed */
192 if (*arg != NUL)
194 p = skiptowhite(arg);
195 if (*p != NUL) /* past first word */
197 xp->xp_pattern = skipwhite(p);
198 if (*skiptowhite(xp->xp_pattern) != NUL)
199 xp->xp_context = EXPAND_NOTHING;
200 else if (STRNICMP(arg, "add", p - arg) == 0)
201 xp->xp_context = EXPAND_FILES;
202 else if (STRNICMP(arg, "kill", p - arg) == 0)
203 expand_what = EXP_CSCOPE_KILL;
204 else if (STRNICMP(arg, "find", p - arg) == 0)
205 expand_what = EXP_CSCOPE_FIND;
206 else
207 xp->xp_context = EXPAND_NOTHING;
212 #endif /* FEAT_CMDL_COMPL */
215 * PRIVATE: do_cscope_general
217 * Find the command, print help if invalid, and then call the corresponding
218 * command function.
220 static void
221 do_cscope_general(eap, make_split)
222 exarg_T *eap;
223 int make_split; /* whether to split window */
225 cscmd_T *cmdp;
227 cs_init();
228 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
230 cs_help(eap);
231 return;
234 #ifdef FEAT_WINDOWS
235 if (make_split)
237 if (!cmdp->cansplit)
239 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
240 return;
242 postponed_split = -1;
243 postponed_split_flags = cmdmod.split;
244 postponed_split_tab = cmdmod.tab;
246 #endif
248 cmdp->func(eap);
250 #ifdef FEAT_WINDOWS
251 postponed_split_flags = 0;
252 postponed_split_tab = 0;
253 #endif
257 * PUBLIC: do_cscope
259 void
260 do_cscope(eap)
261 exarg_T *eap;
263 do_cscope_general(eap, FALSE);
267 * PUBLIC: do_scscope
269 * same as do_cscope, but splits window, too.
271 void
272 do_scscope(eap)
273 exarg_T *eap;
275 do_cscope_general(eap, TRUE);
279 * PUBLIC: do_cstag
282 void
283 do_cstag(eap)
284 exarg_T *eap;
286 int ret = FALSE;
288 cs_init();
290 if (*eap->arg == NUL)
292 (void)EMSG(_("E562: Usage: cstag <ident>"));
293 return;
296 switch (p_csto)
298 case 0 :
299 if (cs_check_for_connections())
301 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
302 FALSE);
303 if (ret == FALSE)
305 cs_free_tags();
306 if (msg_col)
307 msg_putchar('\n');
309 if (cs_check_for_tags())
310 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
313 else if (cs_check_for_tags())
315 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
317 break;
318 case 1 :
319 if (cs_check_for_tags())
321 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
322 if (ret == FALSE)
324 if (msg_col)
325 msg_putchar('\n');
327 if (cs_check_for_connections())
329 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
330 FALSE, FALSE);
331 if (ret == FALSE)
332 cs_free_tags();
336 else if (cs_check_for_connections())
338 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
339 FALSE);
340 if (ret == FALSE)
341 cs_free_tags();
343 break;
344 default :
345 break;
348 if (!ret)
350 (void)EMSG(_("E257: cstag: tag not found"));
351 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
352 g_do_tagpreview = 0;
353 #endif
356 } /* do_cscope */
360 * PUBLIC: cs_find
362 * this simulates a vim_fgets(), but for cscope, returns the next line
363 * from the cscope output. should only be called from find_tags()
365 * returns TRUE if eof, FALSE otherwise
368 cs_fgets(buf, size)
369 char_u *buf;
370 int size;
372 char *p;
374 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
375 return TRUE;
376 vim_strncpy(buf, (char_u *)p, size - 1);
378 return FALSE;
379 } /* cs_fgets */
383 * PUBLIC: cs_free_tags
385 * called only from do_tag(), when popping the tag stack
387 void
388 cs_free_tags()
390 cs_manage_matches(NULL, NULL, -1, Free);
395 * PUBLIC: cs_print_tags
397 * called from do_tag()
399 void
400 cs_print_tags()
402 cs_manage_matches(NULL, NULL, -1, Print);
407 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
409 * Checks for the existence of a |cscope| connection. If no
410 * parameters are specified, then the function returns:
412 * 0, if cscope was not available (not compiled in), or if there
413 * are no cscope connections; or
414 * 1, if there is at least one cscope connection.
416 * If parameters are specified, then the value of {num}
417 * determines how existence of a cscope connection is checked:
419 * {num} Description of existence check
420 * ----- ------------------------------
421 * 0 Same as no parameters (e.g., "cscope_connection()").
422 * 1 Ignore {prepend}, and use partial string matches for
423 * {dbpath}.
424 * 2 Ignore {prepend}, and use exact string matches for
425 * {dbpath}.
426 * 3 Use {prepend}, use partial string matches for both
427 * {dbpath} and {prepend}.
428 * 4 Use {prepend}, use exact string matches for both
429 * {dbpath} and {prepend}.
431 * Note: All string comparisons are case sensitive!
433 #if defined(FEAT_EVAL) || defined(PROTO)
435 cs_connection(num, dbpath, ppath)
436 int num;
437 char_u *dbpath;
438 char_u *ppath;
440 int i;
442 if (num < 0 || num > 4 || (num > 0 && !dbpath))
443 return FALSE;
445 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
447 if (!csinfo[i].fname)
448 continue;
450 if (num == 0)
451 return TRUE;
453 switch (num)
455 case 1:
456 if (strstr(csinfo[i].fname, (char *)dbpath))
457 return TRUE;
458 break;
459 case 2:
460 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
461 return TRUE;
462 break;
463 case 3:
464 if (strstr(csinfo[i].fname, (char *)dbpath)
465 && ((!ppath && !csinfo[i].ppath)
466 || (ppath
467 && csinfo[i].ppath
468 && strstr(csinfo[i].ppath, (char *)ppath))))
469 return TRUE;
470 break;
471 case 4:
472 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
473 && ((!ppath && !csinfo[i].ppath)
474 || (ppath
475 && csinfo[i].ppath
476 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
477 return TRUE;
478 break;
482 return FALSE;
483 } /* cs_connection */
484 #endif
488 * PRIVATE functions
489 ****************************************************************************/
492 * PRIVATE: cs_add
494 * add cscope database or a directory name (to look for cscope.out)
495 * to the cscope connection list
497 * MAXPATHL 256
499 /* ARGSUSED */
500 static int
501 cs_add(eap)
502 exarg_T *eap;
504 char *fname, *ppath, *flags = NULL;
506 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
508 cs_usage_msg(Add);
509 return CSCOPE_FAILURE;
511 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
512 flags = strtok((char *)NULL, (const char *)" ");
514 return cs_add_common(fname, ppath, flags);
517 static void
518 cs_stat_emsg(fname)
519 char *fname;
521 char *stat_emsg = _("E563: stat(%s) error: %d");
522 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
524 if (buf != NULL)
526 (void)sprintf(buf, stat_emsg, fname, errno);
527 (void)EMSG(buf);
528 vim_free(buf);
530 else
531 (void)EMSG(_("E563: stat error"));
536 * PRIVATE: cs_add_common
538 * the common routine to add a new cscope connection. called by
539 * cs_add() and cs_reset(). i really don't like to do this, but this
540 * routine uses a number of goto statements.
542 static int
543 cs_add_common(arg1, arg2, flags)
544 char *arg1; /* filename - may contain environment variables */
545 char *arg2; /* prepend path - may contain environment variables */
546 char *flags;
548 struct stat statbuf;
549 int ret;
550 char *fname = NULL;
551 char *fname2 = NULL;
552 char *ppath = NULL;
553 int i;
555 /* get the filename (arg1), expand it, and try to stat it */
556 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
557 goto add_err;
559 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
560 ret = stat(fname, &statbuf);
561 if (ret < 0)
563 staterr:
564 if (p_csverbose)
565 cs_stat_emsg(fname);
566 goto add_err;
569 /* get the prepend path (arg2), expand it, and try to stat it */
570 if (arg2 != NULL)
572 struct stat statbuf2;
574 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
575 goto add_err;
577 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
578 ret = stat(ppath, &statbuf2);
579 if (ret < 0)
580 goto staterr;
583 /* if filename is a directory, append the cscope database name to it */
584 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
586 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
587 if (fname2 == NULL)
588 goto add_err;
590 while (fname[strlen(fname)-1] == '/'
591 #ifdef WIN32
592 || fname[strlen(fname)-1] == '\\'
593 #endif
596 fname[strlen(fname)-1] = '\0';
597 if (strlen(fname) == 0)
598 break;
600 if (fname[0] == '\0')
601 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
602 else
603 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
605 ret = stat(fname2, &statbuf);
606 if (ret < 0)
608 if (p_csverbose)
609 cs_stat_emsg(fname2);
610 goto add_err;
613 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
615 #if defined(UNIX)
616 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
617 #else
618 /* WIN32 - substitute define S_ISREG from os_unix.h */
619 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
620 #endif
622 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
624 else
626 if (p_csverbose)
627 (void)EMSG2(
628 _("E564: %s is not a directory or a valid cscope database"),
629 fname);
630 goto add_err;
633 if (i != -1)
635 if (cs_create_connection(i) == CSCOPE_FAILURE
636 || cs_read_prompt(i) == CSCOPE_FAILURE)
638 cs_release_csp(i, TRUE);
639 goto add_err;
642 if (p_csverbose)
644 msg_clr_eos();
645 (void)smsg_attr(hl_attr(HLF_R),
646 (char_u *)_("Added cscope database %s"),
647 csinfo[i].fname);
651 vim_free(fname);
652 vim_free(fname2);
653 vim_free(ppath);
654 return CSCOPE_SUCCESS;
656 add_err:
657 vim_free(fname2);
658 vim_free(fname);
659 vim_free(ppath);
660 return CSCOPE_FAILURE;
661 } /* cs_add_common */
664 static int
665 cs_check_for_connections()
667 return (cs_cnt_connections() > 0);
668 } /* cs_check_for_connections */
671 static int
672 cs_check_for_tags()
674 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
675 } /* cs_check_for_tags */
679 * PRIVATE: cs_cnt_connections
681 * count the number of cscope connections
683 static int
684 cs_cnt_connections()
686 short i;
687 short cnt = 0;
689 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
691 if (csinfo[i].fname != NULL)
692 cnt++;
694 return cnt;
695 } /* cs_cnt_connections */
697 static void
698 cs_reading_emsg(idx)
699 int idx; /* connection index */
701 EMSGN(_("E262: error reading cscope connection %ld"), idx);
704 #define CSREAD_BUFSIZE 2048
706 * PRIVATE: cs_cnt_matches
708 * count the number of matches for a given cscope connection.
710 static int
711 cs_cnt_matches(idx)
712 int idx;
714 char *stok;
715 char *buf;
716 int nlines;
718 buf = (char *)alloc(CSREAD_BUFSIZE);
719 if (buf == NULL)
720 return 0;
721 for (;;)
723 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
725 if (feof(csinfo[idx].fr_fp))
726 errno = EIO;
728 cs_reading_emsg(idx);
730 vim_free(buf);
731 return -1;
735 * If the database is out of date, or there's some other problem,
736 * cscope will output error messages before the number-of-lines output.
737 * Display/discard any output that doesn't match what we want.
738 * Accept "\S*cscope: X lines", also matches "mlcscope".
740 if ((stok = strtok(buf, (const char *)" ")) == NULL)
741 continue;
742 if (strstr((const char *)stok, "cscope:") == NULL)
743 continue;
745 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
746 continue;
747 nlines = atoi(stok);
748 if (nlines < 0)
750 nlines = 0;
751 break;
754 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
755 continue;
756 if (strncmp((const char *)stok, "lines", 5))
757 continue;
759 break;
762 vim_free(buf);
763 return nlines;
764 } /* cs_cnt_matches */
768 * PRIVATE: cs_create_cmd
770 * Creates the actual cscope command query from what the user entered.
772 static char *
773 cs_create_cmd(csoption, pattern)
774 char *csoption;
775 char *pattern;
777 char *cmd;
778 short search;
779 char *pat;
781 switch (csoption[0])
783 case '0' : case 's' :
784 search = 0;
785 break;
786 case '1' : case 'g' :
787 search = 1;
788 break;
789 case '2' : case 'd' :
790 search = 2;
791 break;
792 case '3' : case 'c' :
793 search = 3;
794 break;
795 case '4' : case 't' :
796 search = 4;
797 break;
798 case '6' : case 'e' :
799 search = 6;
800 break;
801 case '7' : case 'f' :
802 search = 7;
803 break;
804 case '8' : case 'i' :
805 search = 8;
806 break;
807 default :
808 (void)EMSG(_("E561: unknown cscope search type"));
809 cs_usage_msg(Find);
810 return NULL;
813 /* Skip white space before the patter, except for text and pattern search,
814 * they may want to use the leading white space. */
815 pat = pattern;
816 if (search != 4 && search != 6)
817 while vim_iswhite(*pat)
818 ++pat;
820 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
821 return NULL;
823 (void)sprintf(cmd, "%d%s", search, pat);
825 return cmd;
826 } /* cs_create_cmd */
830 * PRIVATE: cs_create_connection
832 * This piece of code was taken/adapted from nvi. do we need to add
833 * the BSD license notice?
835 static int
836 cs_create_connection(i)
837 int i;
839 #ifdef UNIX
840 int to_cs[2], from_cs[2];
841 #endif
842 int len;
843 char *prog, *cmd, *ppath = NULL;
844 #ifdef WIN32
845 int fd;
846 SECURITY_ATTRIBUTES sa;
847 PROCESS_INFORMATION pi;
848 STARTUPINFO si;
849 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
850 HANDLE stdin_rd, stdout_rd;
851 HANDLE stdout_wr, stdin_wr;
852 BOOL created;
853 # ifdef __BORLANDC__
854 # define OPEN_OH_ARGTYPE long
855 # else
856 # if (_MSC_VER >= 1300)
857 # define OPEN_OH_ARGTYPE intptr_t
858 # else
859 # define OPEN_OH_ARGTYPE long
860 # endif
861 # endif
862 #endif
864 #if defined(UNIX)
866 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
867 * from_cs[0] and writes to to_cs[1].
869 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
870 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
872 (void)EMSG(_("E566: Could not create cscope pipes"));
873 err_closing:
874 if (to_cs[0] != -1)
875 (void)close(to_cs[0]);
876 if (to_cs[1] != -1)
877 (void)close(to_cs[1]);
878 if (from_cs[0] != -1)
879 (void)close(from_cs[0]);
880 if (from_cs[1] != -1)
881 (void)close(from_cs[1]);
882 return CSCOPE_FAILURE;
885 switch (csinfo[i].pid = fork())
887 case -1:
888 (void)EMSG(_("E622: Could not fork for cscope"));
889 goto err_closing;
890 case 0: /* child: run cscope. */
891 if (dup2(to_cs[0], STDIN_FILENO) == -1)
892 PERROR("cs_create_connection 1");
893 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
894 PERROR("cs_create_connection 2");
895 if (dup2(from_cs[1], STDERR_FILENO) == -1)
896 PERROR("cs_create_connection 3");
898 /* close unused */
899 (void)close(to_cs[1]);
900 (void)close(from_cs[0]);
901 #else
902 /* WIN32 */
903 /* Create pipes to communicate with cscope */
904 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
905 sa.bInheritHandle = TRUE;
906 sa.lpSecurityDescriptor = NULL;
908 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
909 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
911 (void)EMSG(_("E566: Could not create cscope pipes"));
912 err_closing:
913 if (pipe_stdin)
915 CloseHandle(stdin_rd);
916 CloseHandle(stdin_wr);
918 if (pipe_stdout)
920 CloseHandle(stdout_rd);
921 CloseHandle(stdout_wr);
923 return CSCOPE_FAILURE;
925 #endif
926 /* expand the cscope exec for env var's */
927 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
929 #ifdef UNIX
930 return CSCOPE_FAILURE;
931 #else
932 /* WIN32 */
933 goto err_closing;
934 #endif
936 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
938 /* alloc space to hold the cscope command */
939 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
940 if (csinfo[i].ppath)
942 /* expand the prepend path for env var's */
943 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
945 vim_free(prog);
946 #ifdef UNIX
947 return CSCOPE_FAILURE;
948 #else
949 /* WIN32 */
950 goto err_closing;
951 #endif
953 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
955 len += (int)strlen(ppath);
958 if (csinfo[i].flags)
959 len += (int)strlen(csinfo[i].flags);
961 if ((cmd = (char *)alloc(len)) == NULL)
963 vim_free(prog);
964 vim_free(ppath);
965 #ifdef UNIX
966 return CSCOPE_FAILURE;
967 #else
968 /* WIN32 */
969 goto err_closing;
970 #endif
973 /* run the cscope command; is there execl for non-unix systems? */
974 #if defined(UNIX)
975 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
976 #else
977 /* WIN32 */
978 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
979 #endif
980 if (csinfo[i].ppath != NULL)
982 (void)strcat(cmd, " -P");
983 (void)strcat(cmd, csinfo[i].ppath);
985 if (csinfo[i].flags != NULL)
987 (void)strcat(cmd, " ");
988 (void)strcat(cmd, csinfo[i].flags);
990 # ifdef UNIX
991 /* on Win32 we still need prog */
992 vim_free(prog);
993 # endif
994 vim_free(ppath);
996 #if defined(UNIX)
997 if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
998 PERROR(_("cs_create_connection exec failed"));
1000 exit(127);
1001 /* NOTREACHED */
1002 default: /* parent. */
1004 * Save the file descriptors for later duplication, and
1005 * reopen as streams.
1007 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1008 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1009 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1010 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1012 /* close unused */
1013 (void)close(to_cs[0]);
1014 (void)close(from_cs[1]);
1016 break;
1019 #else
1020 /* WIN32 */
1021 /* Create a new process to run cscope and use pipes to talk with it */
1022 GetStartupInfo(&si);
1023 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1024 si.wShowWindow = SW_HIDE; /* Hide child application window */
1025 si.hStdOutput = stdout_wr;
1026 si.hStdError = stdout_wr;
1027 si.hStdInput = stdin_rd;
1028 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1029 NULL, NULL, &si, &pi);
1030 vim_free(prog);
1031 vim_free(cmd);
1033 if (!created)
1035 PERROR(_("cs_create_connection exec failed"));
1036 (void)EMSG(_("E623: Could not spawn cscope process"));
1037 goto err_closing;
1039 /* else */
1040 csinfo[i].pid = pi.dwProcessId;
1041 csinfo[i].hProc = pi.hProcess;
1042 CloseHandle(pi.hThread);
1044 /* TODO - tidy up after failure to create files on pipe handles. */
1045 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1046 _O_TEXT|_O_APPEND)) < 0)
1047 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1048 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1049 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1050 _O_TEXT|_O_RDONLY)) < 0)
1051 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1052 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1054 /* Close handles for file descriptors inherited by the cscope process */
1055 CloseHandle(stdin_rd);
1056 CloseHandle(stdout_wr);
1058 #endif /* !UNIX */
1060 return CSCOPE_SUCCESS;
1061 } /* cs_create_connection */
1065 * PRIVATE: cs_find
1067 * query cscope using command line interface. parse the output and use tselect
1068 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1070 * returns TRUE if we jump to a tag or abort, FALSE if not.
1072 static int
1073 cs_find(eap)
1074 exarg_T *eap;
1076 char *opt, *pat;
1078 if (cs_check_for_connections() == FALSE)
1080 (void)EMSG(_("E567: no cscope connections"));
1081 return FALSE;
1084 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1086 cs_usage_msg(Find);
1087 return FALSE;
1090 pat = opt + strlen(opt) + 1;
1091 if (pat >= (char *)eap->arg + eap_arg_len)
1093 cs_usage_msg(Find);
1094 return FALSE;
1097 return cs_find_common(opt, pat, eap->forceit, TRUE,
1098 eap->cmdidx == CMD_lcscope);
1099 } /* cs_find */
1103 * PRIVATE: cs_find_common
1105 * common code for cscope find, shared by cs_find() and do_cstag()
1107 static int
1108 cs_find_common(opt, pat, forceit, verbose, use_ll)
1109 char *opt;
1110 char *pat;
1111 int forceit;
1112 int verbose;
1113 int use_ll;
1115 int i;
1116 char *cmd;
1117 int nummatches[CSCOPE_MAX_CONNECTIONS], totmatches;
1118 #ifdef FEAT_QUICKFIX
1119 char cmdletter;
1120 char *qfpos;
1121 #endif
1123 /* create the actual command to send to cscope */
1124 cmd = cs_create_cmd(opt, pat);
1125 if (cmd == NULL)
1126 return FALSE;
1128 /* send query to all open connections, then count the total number
1129 * of matches so we can alloc matchesp all in one swell foop
1131 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1132 nummatches[i] = 0;
1133 totmatches = 0;
1134 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1136 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
1137 continue;
1139 /* send cmd to cscope */
1140 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1141 (void)fflush(csinfo[i].to_fp);
1143 nummatches[i] = cs_cnt_matches(i);
1145 if (nummatches[i] > -1)
1146 totmatches += nummatches[i];
1148 if (nummatches[i] == 0)
1149 (void)cs_read_prompt(i);
1151 vim_free(cmd);
1153 if (totmatches == 0)
1155 char *nf = _("E259: no matches found for cscope query %s of %s");
1156 char *buf;
1158 if (!verbose)
1159 return FALSE;
1161 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
1162 if (buf == NULL)
1163 (void)EMSG(nf);
1164 else
1166 sprintf(buf, nf, opt, pat);
1167 (void)EMSG(buf);
1168 vim_free(buf);
1170 return FALSE;
1173 #ifdef FEAT_QUICKFIX
1174 /* get cmd letter */
1175 switch (opt[0])
1177 case '0' :
1178 cmdletter = 's';
1179 break;
1180 case '1' :
1181 cmdletter = 'g';
1182 break;
1183 case '2' :
1184 cmdletter = 'd';
1185 break;
1186 case '3' :
1187 cmdletter = 'c';
1188 break;
1189 case '4' :
1190 cmdletter = 't';
1191 break;
1192 case '6' :
1193 cmdletter = 'e';
1194 break;
1195 case '7' :
1196 cmdletter = 'f';
1197 break;
1198 case '8' :
1199 cmdletter = 'i';
1200 break;
1201 default :
1202 cmdletter = opt[0];
1205 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1206 if (qfpos != NULL)
1208 qfpos++;
1209 /* next symbol must be + or - */
1210 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1212 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1213 char *buf = (char *)alloc((unsigned)strlen(nf));
1215 /* strlen will be enough because we use chars */
1216 if (buf != NULL)
1218 sprintf(buf, nf, *qfpos, *(qfpos-1));
1219 (void)EMSG(buf);
1220 vim_free(buf);
1222 return FALSE;
1225 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
1227 /* fill error list */
1228 FILE *f;
1229 char_u *tmp = vim_tempname('c');
1230 qf_info_T *qi = NULL;
1231 win_T *wp = NULL;
1233 f = mch_fopen((char *)tmp, "w");
1234 if (f == NULL)
1235 EMSG2(_(e_notopen), tmp);
1236 else
1238 cs_file_results(f, nummatches);
1239 fclose(f);
1240 if (use_ll) /* Use location list */
1241 wp = curwin;
1242 /* '-' starts a new error list */
1243 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1244 *qfpos == '-') > 0)
1246 # ifdef FEAT_WINDOWS
1247 if (postponed_split != 0)
1249 win_split(postponed_split > 0 ? postponed_split : 0,
1250 postponed_split_flags);
1251 # ifdef FEAT_SCROLLBIND
1252 curwin->w_p_scb = FALSE;
1253 # endif
1254 postponed_split = 0;
1256 # endif
1257 if (use_ll)
1259 * In the location list window, use the displayed location
1260 * list. Otherwise, use the location list for the window.
1262 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1263 ? wp->w_llist_ref : wp->w_llist;
1264 qf_jump(qi, 0, 0, forceit);
1267 mch_remove(tmp);
1268 vim_free(tmp);
1269 return TRUE;
1271 else
1272 #endif /* FEAT_QUICKFIX */
1274 char **matches = NULL, **contexts = NULL;
1275 int matched = 0;
1277 /* read output */
1278 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1279 &contexts, &matched);
1280 if (matches == NULL)
1281 return FALSE;
1283 (void)cs_manage_matches(matches, contexts, matched, Store);
1285 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1288 } /* cs_find_common */
1291 * PRIVATE: cs_help
1293 * print help
1295 /* ARGSUSED */
1296 static int
1297 cs_help(eap)
1298 exarg_T *eap;
1300 cscmd_T *cmdp = cs_cmds;
1302 (void)MSG_PUTS(_("cscope commands:\n"));
1303 while (cmdp->name != NULL)
1305 char *help = _(cmdp->help);
1306 int space_cnt = 30 - vim_strsize((char_u *)help);
1308 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1309 if (space_cnt < 0)
1310 space_cnt = 0;
1311 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1312 cmdp->name,
1313 help, space_cnt, " ",
1314 cmdp->usage);
1315 if (strcmp(cmdp->name, "find") == 0)
1316 MSG_PUTS(_("\n"
1317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
1324 " t: Find assignments to\n"));
1326 cmdp++;
1329 wait_return(TRUE);
1330 return 0;
1331 } /* cs_help */
1335 * PRIVATE: cs_init
1337 * initialize cscope structure if not already
1339 static void
1340 cs_init()
1342 short i;
1343 static int init_already = FALSE;
1345 if (init_already)
1346 return;
1348 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1349 clear_csinfo(i);
1351 init_already = TRUE;
1352 } /* cs_init */
1354 static void
1355 clear_csinfo(i)
1356 int i;
1358 csinfo[i].fname = NULL;
1359 csinfo[i].ppath = NULL;
1360 csinfo[i].flags = NULL;
1361 #if defined(UNIX)
1362 csinfo[i].st_dev = (dev_t)0;
1363 csinfo[i].st_ino = (ino_t)0;
1364 #else
1365 csinfo[i].nVolume = 0;
1366 csinfo[i].nIndexHigh = 0;
1367 csinfo[i].nIndexLow = 0;
1368 #endif
1369 csinfo[i].pid = 0;
1370 csinfo[i].fr_fp = NULL;
1371 csinfo[i].to_fp = NULL;
1372 #if defined(WIN32)
1373 csinfo[i].hProc = NULL;
1374 #endif
1377 #ifndef UNIX
1378 static char *GetWin32Error __ARGS((void));
1380 static char *
1381 GetWin32Error()
1383 char *msg = NULL;
1384 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1385 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1386 if (msg != NULL)
1388 /* remove trailing \r\n */
1389 char *pcrlf = strstr(msg, "\r\n");
1390 if (pcrlf != NULL)
1391 *pcrlf = '\0';
1393 return msg;
1395 #endif
1398 * PRIVATE: cs_insert_filelist
1400 * insert a new cscope database filename into the filelist
1402 /*ARGSUSED*/
1403 static int
1404 cs_insert_filelist(fname, ppath, flags, sb)
1405 char *fname;
1406 char *ppath;
1407 char *flags;
1408 struct stat *sb;
1410 short i, j;
1411 #ifndef UNIX
1412 HANDLE hFile;
1413 BY_HANDLE_FILE_INFORMATION bhfi;
1415 vim_memset(&bhfi, 0, sizeof(bhfi));
1416 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1417 if (!mch_windows95())
1419 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1420 FILE_ATTRIBUTE_NORMAL, NULL);
1421 if (hFile == INVALID_HANDLE_VALUE)
1423 if (p_csverbose)
1425 char *cant_msg = _("E625: cannot open cscope database: %s");
1426 char *winmsg = GetWin32Error();
1428 if (winmsg != NULL)
1430 (void)EMSG2(cant_msg, winmsg);
1431 LocalFree(winmsg);
1433 else
1434 /* subst filename if can't get error text */
1435 (void)EMSG2(cant_msg, fname);
1437 return -1;
1439 if (!GetFileInformationByHandle(hFile, &bhfi))
1441 CloseHandle(hFile);
1442 if (p_csverbose)
1443 (void)EMSG(_("E626: cannot get cscope database information"));
1444 return -1;
1446 CloseHandle(hFile);
1448 #endif
1450 i = -1; /* can be set to the index of an empty item in csinfo */
1451 for (j = 0; j < CSCOPE_MAX_CONNECTIONS; j++)
1453 if (csinfo[j].fname != NULL
1454 #if defined(UNIX)
1455 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1456 #else
1457 /* compare pathnames first */
1458 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
1459 /* if not Windows 9x, test index file attributes too */
1460 || (!mch_windows95()
1461 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1462 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1463 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1464 #endif
1467 if (p_csverbose)
1468 (void)EMSG(_("E568: duplicate cscope database not added"));
1469 return -1;
1472 if (csinfo[j].fname == NULL && i == -1)
1473 i = j; /* remember first empty entry */
1476 if (i == -1)
1478 if (p_csverbose)
1479 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1480 return -1;
1483 if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL)
1484 return -1;
1486 (void)strcpy(csinfo[i].fname, (const char *)fname);
1488 if (ppath != NULL)
1490 if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL)
1492 vim_free(csinfo[i].fname);
1493 csinfo[i].fname = NULL;
1494 return -1;
1496 (void)strcpy(csinfo[i].ppath, (const char *)ppath);
1497 } else
1498 csinfo[i].ppath = NULL;
1500 if (flags != NULL)
1502 if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL)
1504 vim_free(csinfo[i].fname);
1505 vim_free(csinfo[i].ppath);
1506 csinfo[i].fname = NULL;
1507 csinfo[i].ppath = NULL;
1508 return -1;
1510 (void)strcpy(csinfo[i].flags, (const char *)flags);
1511 } else
1512 csinfo[i].flags = NULL;
1514 #if defined(UNIX)
1515 csinfo[i].st_dev = sb->st_dev;
1516 csinfo[i].st_ino = sb->st_ino;
1518 #else
1519 csinfo[i].nVolume = bhfi.dwVolumeSerialNumber;
1520 csinfo[i].nIndexLow = bhfi.nFileIndexLow;
1521 csinfo[i].nIndexHigh = bhfi.nFileIndexHigh;
1522 #endif
1523 return i;
1524 } /* cs_insert_filelist */
1528 * PRIVATE: cs_lookup_cmd
1530 * find cscope command in command table
1532 static cscmd_T *
1533 cs_lookup_cmd(eap)
1534 exarg_T *eap;
1536 cscmd_T *cmdp;
1537 char *stok;
1538 size_t len;
1540 if (eap->arg == NULL)
1541 return NULL;
1543 /* Store length of eap->arg before it gets modified by strtok(). */
1544 eap_arg_len = (int)STRLEN(eap->arg);
1546 if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL)
1547 return NULL;
1549 len = strlen(stok);
1550 for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp)
1552 if (strncmp((const char *)(stok), cmdp->name, len) == 0)
1553 return (cmdp);
1555 return NULL;
1556 } /* cs_lookup_cmd */
1560 * PRIVATE: cs_kill
1562 * nuke em
1564 /* ARGSUSED */
1565 static int
1566 cs_kill(eap)
1567 exarg_T *eap;
1569 char *stok;
1570 short i;
1572 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1574 cs_usage_msg(Kill);
1575 return CSCOPE_FAILURE;
1578 /* only single digit positive and negative integers are allowed */
1579 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1580 || (strlen(stok) < 3 && stok[0] == '-'
1581 && VIM_ISDIGIT((int)(stok[1]))))
1582 i = atoi(stok);
1583 else
1585 /* It must be part of a name. We will try to find a match
1586 * within all the names in the csinfo data structure
1588 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1590 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1591 break;
1595 if ((i >= CSCOPE_MAX_CONNECTIONS || i < -1 || csinfo[i].fname == NULL)
1596 && i != -1)
1598 if (p_csverbose)
1599 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1601 else
1603 if (i == -1)
1605 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1607 if (csinfo[i].fname)
1608 cs_kill_execute(i, csinfo[i].fname);
1611 else
1612 cs_kill_execute(i, stok);
1615 return 0;
1616 } /* cs_kill */
1620 * PRIVATE: cs_kill_execute
1622 * Actually kills a specific cscope connection.
1624 static void
1625 cs_kill_execute(i, cname)
1626 int i; /* cscope table index */
1627 char *cname; /* cscope database name */
1629 if (p_csverbose)
1631 msg_clr_eos();
1632 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1633 (char_u *)_("cscope connection %s closed"), cname);
1635 cs_release_csp(i, TRUE);
1640 * PRIVATE: cs_make_vim_style_matches
1642 * convert the cscope output into into a ctags style entry (as might be found
1643 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1644 * the type of the tag you are looking for. for example, in Darren Hiebert's
1645 * ctags (the one that comes with vim), #define's use a line number to find the
1646 * tag in a file while function definitions use a regexp search pattern.
1648 * i'm going to always use the line number because cscope does something
1649 * quirky (and probably other things i don't know about):
1651 * if you have "# define" in your source file, which is
1652 * perfectly legal, cscope thinks you have "#define". this
1653 * will result in a failed regexp search. :(
1655 * besides, even if this particular case didn't happen, the search pattern
1656 * would still have to be modified to escape all the special regular expression
1657 * characters to comply with ctags formatting.
1659 static char *
1660 cs_make_vim_style_matches(fname, slno, search, tagstr)
1661 char *fname;
1662 char *slno;
1663 char *search;
1664 char *tagstr;
1666 /* vim style is ctags:
1668 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1670 * but as mentioned above, we'll always use the line number and
1671 * put the search pattern (if one exists) as "extra"
1673 * buf is used as part of vim's method of handling tags, and
1674 * (i think) vim frees it when you pop your tags and get replaced
1675 * by new ones on the tag stack.
1677 char *buf;
1678 int amt;
1680 if (search != NULL)
1682 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
1683 if ((buf = (char *)alloc(amt)) == NULL)
1684 return NULL;
1686 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1688 else
1690 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
1691 if ((buf = (char *)alloc(amt)) == NULL)
1692 return NULL;
1694 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1697 return buf;
1698 } /* cs_make_vim_style_matches */
1702 * PRIVATE: cs_manage_matches
1704 * this is kind of hokey, but i don't see an easy way round this..
1706 * Store: keep a ptr to the (malloc'd) memory of matches originally
1707 * generated from cs_find(). the matches are originally lines directly
1708 * from cscope output, but transformed to look like something out of a
1709 * ctags. see cs_make_vim_style_matches for more details.
1711 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1712 * the next line from the cscope output. it basically keeps track of which
1713 * lines have been "used" and returns the next one.
1715 * Free: frees up everything and resets
1717 * Print: prints the tags
1719 static char *
1720 cs_manage_matches(matches, contexts, totmatches, cmd)
1721 char **matches;
1722 char **contexts;
1723 int totmatches;
1724 mcmd_e cmd;
1726 static char **mp = NULL;
1727 static char **cp = NULL;
1728 static int cnt = -1;
1729 static int next = -1;
1730 char *p = NULL;
1732 switch (cmd)
1734 case Store:
1735 assert(matches != NULL);
1736 assert(totmatches > 0);
1737 if (mp != NULL || cp != NULL)
1738 (void)cs_manage_matches(NULL, NULL, -1, Free);
1739 mp = matches;
1740 cp = contexts;
1741 cnt = totmatches;
1742 next = 0;
1743 break;
1744 case Get:
1745 if (next >= cnt)
1746 return NULL;
1748 p = mp[next];
1749 next++;
1750 break;
1751 case Free:
1752 if (mp != NULL)
1754 if (cnt > 0)
1755 while (cnt--)
1757 vim_free(mp[cnt]);
1758 if (cp != NULL)
1759 vim_free(cp[cnt]);
1761 vim_free(mp);
1762 vim_free(cp);
1764 mp = NULL;
1765 cp = NULL;
1766 cnt = 0;
1767 next = 0;
1768 break;
1769 case Print:
1770 cs_print_tags_priv(mp, cp, cnt);
1771 break;
1772 default: /* should not reach here */
1773 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1774 return NULL;
1777 return p;
1778 } /* cs_manage_matches */
1782 * PRIVATE: cs_parse_results
1784 * parse cscope output
1786 static char *
1787 cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1788 int cnumber;
1789 char *buf;
1790 int bufsize;
1791 char **context;
1792 char **linenumber;
1793 char **search;
1795 int ch;
1796 char *p;
1797 char *name;
1799 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1801 if (feof(csinfo[cnumber].fr_fp))
1802 errno = EIO;
1804 cs_reading_emsg(cnumber);
1806 return NULL;
1809 /* If the line's too long for the buffer, discard it. */
1810 if ((p = strchr(buf, '\n')) == NULL)
1812 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1814 return NULL;
1816 *p = '\0';
1819 * cscope output is in the following format:
1821 * <filename> <context> <line number> <pattern>
1823 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1824 return NULL;
1825 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1826 return NULL;
1827 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1828 return NULL;
1829 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1831 /* --- nvi ---
1832 * If the file is older than the cscope database, that is,
1833 * the database was built since the file was last modified,
1834 * or there wasn't a search string, use the line number.
1836 if (strcmp(*search, "<unknown>") == 0)
1837 *search = NULL;
1839 name = cs_resolve_file(cnumber, name);
1840 return name;
1843 #ifdef FEAT_QUICKFIX
1845 * PRIVATE: cs_file_results
1847 * write cscope find results to file
1849 static void
1850 cs_file_results(f, nummatches_a)
1851 FILE *f;
1852 int *nummatches_a;
1854 int i, j;
1855 char *buf;
1856 char *search, *slno;
1857 char *fullname;
1858 char *cntx;
1859 char *context;
1861 buf = (char *)alloc(CSREAD_BUFSIZE);
1862 if (buf == NULL)
1863 return;
1865 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1867 if (nummatches_a[i] < 1)
1868 continue;
1870 for (j = 0; j < nummatches_a[i]; j++)
1872 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1873 &slno, &search)) == NULL)
1874 continue;
1876 context = (char *)alloc((unsigned)strlen(cntx)+5);
1877 if (context == NULL)
1878 continue;
1880 if (strcmp(cntx, "<global>")==0)
1881 strcpy(context, "<<global>>");
1882 else
1883 sprintf(context, "<<%s>>", cntx);
1885 if (search == NULL)
1886 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1887 else
1888 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1890 vim_free(context);
1891 vim_free(fullname);
1892 } /* for all matches */
1894 (void)cs_read_prompt(i);
1896 } /* for all cscope connections */
1897 vim_free(buf);
1899 #endif
1902 * PRIVATE: cs_fill_results
1904 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1905 * into ctags format
1906 * When there are no matches sets "*matches_p" to NULL.
1908 static void
1909 cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1910 char *tagstr;
1911 int totmatches;
1912 int *nummatches_a;
1913 char ***matches_p;
1914 char ***cntxts_p;
1915 int *matched;
1917 int i, j;
1918 char *buf;
1919 char *search, *slno;
1920 int totsofar = 0;
1921 char **matches = NULL;
1922 char **cntxts = NULL;
1923 char *fullname;
1924 char *cntx;
1926 assert(totmatches > 0);
1928 buf = (char *)alloc(CSREAD_BUFSIZE);
1929 if (buf == NULL)
1930 return;
1932 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1933 goto parse_out;
1934 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1935 goto parse_out;
1937 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
1939 if (nummatches_a[i] < 1)
1940 continue;
1942 for (j = 0; j < nummatches_a[i]; j++)
1944 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1945 &slno, &search)) == NULL)
1946 continue;
1948 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1949 search, tagstr);
1951 vim_free(fullname);
1953 if (strcmp(cntx, "<global>") == 0)
1954 cntxts[totsofar] = NULL;
1955 else
1956 /* note: if vim_strsave returns NULL, then the context
1957 * will be "<global>", which is misleading.
1959 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1961 if (matches[totsofar] != NULL)
1962 totsofar++;
1964 } /* for all matches */
1966 (void)cs_read_prompt(i);
1968 } /* for all cscope connections */
1970 parse_out:
1971 if (totsofar == 0)
1973 /* No matches, free the arrays and return NULL in "*matches_p". */
1974 vim_free(matches);
1975 matches = NULL;
1976 vim_free(cntxts);
1977 cntxts = NULL;
1979 *matched = totsofar;
1980 *matches_p = matches;
1981 *cntxts_p = cntxts;
1983 vim_free(buf);
1984 } /* cs_fill_results */
1987 /* get the requested path components */
1988 static char *
1989 cs_pathcomponents(path)
1990 char *path;
1992 int i;
1993 char *s;
1995 if (p_cspc == 0)
1996 return path;
1998 s = path + strlen(path) - 1;
1999 for (i = 0; i < p_cspc; ++i)
2000 while (s > path && *--s != '/'
2001 #ifdef WIN32
2002 && *--s != '\\'
2003 #endif
2006 if ((s > path && *s == '/')
2007 #ifdef WIN32
2008 || (s > path && *s == '\\')
2009 #endif
2011 ++s;
2012 return s;
2016 * PRIVATE: cs_print_tags_priv
2018 * called from cs_manage_matches()
2020 static void
2021 cs_print_tags_priv(matches, cntxts, num_matches)
2022 char **matches;
2023 char **cntxts;
2024 int num_matches;
2026 char *buf = NULL;
2027 int bufsize = 0; /* Track available bufsize */
2028 int newsize = 0;
2029 char *ptag;
2030 char *fname, *lno, *extra, *tbuf;
2031 int i, idx, num;
2032 char *globalcntx = "GLOBAL";
2033 char *cntxformat = " <<%s>>";
2034 char *context;
2035 char *cstag_msg = _("Cscope tag: %s");
2036 char *csfmt_str = "%4d %6s ";
2038 assert (num_matches > 0);
2040 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
2041 return;
2043 strcpy(tbuf, matches[0]);
2044 ptag = strtok(tbuf, "\t");
2046 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
2047 buf = (char *)alloc(newsize);
2048 if (buf != NULL)
2050 bufsize = newsize;
2051 (void)sprintf(buf, cstag_msg, ptag);
2052 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2055 vim_free(tbuf);
2057 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2058 msg_advance(msg_col + 2);
2059 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2061 num = 1;
2062 for (i = 0; i < num_matches; i++)
2064 idx = i;
2066 /* if we really wanted to, we could avoid this malloc and strcpy
2067 * by parsing matches[i] on the fly and placing stuff into buf
2068 * directly, but that's too much of a hassle
2070 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
2071 continue;
2072 (void)strcpy(tbuf, matches[idx]);
2074 if ((fname = strtok(tbuf, (const char *)"\t")) == NULL)
2075 continue;
2076 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2077 continue;
2078 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
2079 continue;
2080 extra = strtok(NULL, (const char *)"\t");
2082 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2084 /* hopefully 'num' (num of matches) will be less than 10^16 */
2085 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
2086 if (bufsize < newsize)
2088 buf = (char *)vim_realloc(buf, newsize);
2089 if (buf == NULL)
2090 bufsize = 0;
2091 else
2092 bufsize = newsize;
2094 if (buf != NULL)
2096 /* csfmt_str = "%4d %6s "; */
2097 (void)sprintf(buf, csfmt_str, num, lno);
2098 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2100 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2102 /* compute the required space for the context */
2103 if (cntxts[idx] != NULL)
2104 context = cntxts[idx];
2105 else
2106 context = globalcntx;
2107 newsize = (int)(strlen(context) + strlen(cntxformat));
2109 if (bufsize < newsize)
2111 buf = (char *)vim_realloc(buf, newsize);
2112 if (buf == NULL)
2113 bufsize = 0;
2114 else
2115 bufsize = newsize;
2117 if (buf != NULL)
2119 (void)sprintf(buf, cntxformat, context);
2121 /* print the context only if it fits on the same line */
2122 if (msg_col + (int)strlen(buf) >= (int)Columns)
2123 msg_putchar('\n');
2124 msg_advance(12);
2125 MSG_PUTS_LONG(buf);
2126 msg_putchar('\n');
2128 if (extra != NULL)
2130 msg_advance(13);
2131 MSG_PUTS_LONG(extra);
2134 vim_free(tbuf); /* only after printing extra due to strtok use */
2136 if (msg_col)
2137 msg_putchar('\n');
2139 ui_breakcheck();
2140 if (got_int)
2142 got_int = FALSE; /* don't print any more matches */
2143 break;
2146 num++;
2147 } /* for all matches */
2149 vim_free(buf);
2150 } /* cs_print_tags_priv */
2154 * PRIVATE: cs_read_prompt
2156 * read a cscope prompt (basically, skip over the ">> ")
2158 static int
2159 cs_read_prompt(i)
2160 int i;
2162 int ch;
2163 char *buf = NULL; /* buffer for possible error message from cscope */
2164 int bufpos = 0;
2165 char *cs_emsg;
2166 int maxlen;
2167 static char *eprompt = "Press the RETURN key to continue:";
2168 int epromptlen = (int)strlen(eprompt);
2169 int n;
2171 cs_emsg = _("E609: Cscope error: %s");
2172 /* compute maximum allowed len for Cscope error message */
2173 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2175 for (;;)
2177 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2178 /* if there is room and char is printable */
2179 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2181 if (buf == NULL) /* lazy buffer allocation */
2182 buf = (char *)alloc(maxlen);
2183 if (buf != NULL)
2185 /* append character to the message */
2186 buf[bufpos++] = ch;
2187 buf[bufpos] = NUL;
2188 if (bufpos >= epromptlen
2189 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2191 /* remove eprompt from buf */
2192 buf[bufpos - epromptlen] = NUL;
2194 /* print message to user */
2195 (void)EMSG2(cs_emsg, buf);
2197 /* send RETURN to cscope */
2198 (void)putc('\n', csinfo[i].to_fp);
2199 (void)fflush(csinfo[i].to_fp);
2201 /* clear buf */
2202 bufpos = 0;
2203 buf[bufpos] = NUL;
2208 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2210 if (n > 0)
2211 ch = getc(csinfo[i].fr_fp);
2212 if (ch == EOF)
2214 PERROR("cs_read_prompt EOF");
2215 if (buf != NULL && buf[0] != NUL)
2216 (void)EMSG2(cs_emsg, buf);
2217 else if (p_csverbose)
2218 cs_reading_emsg(i); /* don't have additional information */
2219 cs_release_csp(i, TRUE);
2220 vim_free(buf);
2221 return CSCOPE_FAILURE;
2224 if (ch != CSCOPE_PROMPT[n])
2226 ch = EOF;
2227 break;
2231 if (ch == EOF)
2232 continue; /* didn't find the prompt */
2233 break; /* did find the prompt */
2236 vim_free(buf);
2237 return CSCOPE_SUCCESS;
2240 #if defined(UNIX) && defined(SIGALRM)
2242 * Used to catch and ignore SIGALRM below.
2244 /* ARGSUSED */
2245 static RETSIGTYPE
2246 sig_handler SIGDEFARG(sigarg)
2248 /* do nothing */
2249 SIGRETURN;
2251 #endif
2254 * PRIVATE: cs_release_csp
2256 * Does the actual free'ing for the cs ptr with an optional flag of whether
2257 * or not to free the filename. Called by cs_kill and cs_reset.
2259 static void
2260 cs_release_csp(i, freefnpp)
2261 int i;
2262 int freefnpp;
2265 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2267 if (csinfo[i].to_fp != NULL)
2269 (void)fputs("q\n", csinfo[i].to_fp);
2270 (void)fflush(csinfo[i].to_fp);
2272 #if defined(UNIX)
2274 int waitpid_errno;
2275 int pstat;
2276 pid_t pid;
2278 # if defined(HAVE_SIGACTION)
2279 struct sigaction sa, old;
2281 /* Use sigaction() to limit the waiting time to two seconds. */
2282 sigemptyset(&sa.sa_mask);
2283 sa.sa_handler = sig_handler;
2284 sa.sa_flags = SA_NODEFER;
2285 sigaction(SIGALRM, &sa, &old);
2286 alarm(2); /* 2 sec timeout */
2288 /* Block until cscope exits or until timer expires */
2289 pid = waitpid(csinfo[i].pid, &pstat, 0);
2290 waitpid_errno = errno;
2292 /* cancel pending alarm if still there and restore signal */
2293 alarm(0);
2294 sigaction(SIGALRM, &old, NULL);
2295 # else
2296 int waited;
2298 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2299 * to give cscope a chance to exit quickly. */
2300 sleep(0);
2301 for (waited = 0; waited < 40; ++waited)
2303 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
2304 waitpid_errno = errno;
2305 if (pid != 0)
2306 break; /* break unless the process is still running */
2307 mch_delay(50L, FALSE); /* sleep 50 ms */
2309 # endif
2311 * If the cscope process is still running: kill it.
2312 * Safety check: If the PID would be zero here, the entire X session
2313 * would be killed. -1 and 1 are dangerous as well.
2315 if (pid < 0 && csinfo[i].pid > 1)
2317 # ifdef ECHILD
2318 int alive = TRUE;
2320 if (waitpid_errno == ECHILD)
2323 * When using 'vim -g', vim is forked and cscope process is
2324 * no longer a child process but a sibling. So waitpid()
2325 * fails with errno being ECHILD (No child processes).
2326 * Don't send SIGKILL to cscope immediately but wait
2327 * (polling) for it to exit normally as result of sending
2328 * the "q" command, hence giving it a chance to clean up
2329 * its temporary files.
2331 int waited;
2333 sleep(0);
2334 for (waited = 0; waited < 40; ++waited)
2336 /* Check whether cscope process is still alive */
2337 if (kill(csinfo[i].pid, 0) != 0)
2339 alive = FALSE; /* cscope process no longer exists */
2340 break;
2342 mch_delay(50L, FALSE); /* sleep 50ms */
2345 if (alive)
2346 # endif
2348 kill(csinfo[i].pid, SIGKILL);
2349 (void)waitpid(csinfo[i].pid, &pstat, 0);
2353 #else /* !UNIX */
2354 if (csinfo[i].hProc != NULL)
2356 /* Give cscope a chance to exit normally */
2357 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2358 TerminateProcess(csinfo[i].hProc, 0);
2359 CloseHandle(csinfo[i].hProc);
2361 #endif
2363 if (csinfo[i].fr_fp != NULL)
2364 (void)fclose(csinfo[i].fr_fp);
2365 if (csinfo[i].to_fp != NULL)
2366 (void)fclose(csinfo[i].to_fp);
2368 if (freefnpp)
2370 vim_free(csinfo[i].fname);
2371 vim_free(csinfo[i].ppath);
2372 vim_free(csinfo[i].flags);
2375 clear_csinfo(i);
2376 } /* cs_release_csp */
2380 * PRIVATE: cs_reset
2382 * calls cs_kill on all cscope connections then reinits
2384 /* ARGSUSED */
2385 static int
2386 cs_reset(eap)
2387 exarg_T *eap;
2389 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2390 int i;
2391 char buf[20]; /* for sprintf " (#%d)" */
2393 /* malloc our db and ppath list */
2394 dblist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2395 pplist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2396 fllist = (char **)alloc(CSCOPE_MAX_CONNECTIONS * sizeof(char *));
2397 if (dblist == NULL || pplist == NULL || fllist == NULL)
2399 vim_free(dblist);
2400 vim_free(pplist);
2401 vim_free(fllist);
2402 return CSCOPE_FAILURE;
2405 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2407 dblist[i] = csinfo[i].fname;
2408 pplist[i] = csinfo[i].ppath;
2409 fllist[i] = csinfo[i].flags;
2410 if (csinfo[i].fname != NULL)
2411 cs_release_csp(i, FALSE);
2414 /* rebuild the cscope connection list */
2415 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2417 if (dblist[i] != NULL)
2419 cs_add_common(dblist[i], pplist[i], fllist[i]);
2420 if (p_csverbose)
2422 /* don't use smsg_attr() because we want to display the
2423 * connection number in the same line as
2424 * "Added cscope database..."
2426 sprintf(buf, " (#%d)", i);
2427 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2430 vim_free(dblist[i]);
2431 vim_free(pplist[i]);
2432 vim_free(fllist[i]);
2434 vim_free(dblist);
2435 vim_free(pplist);
2436 vim_free(fllist);
2438 if (p_csverbose)
2439 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2440 return CSCOPE_SUCCESS;
2441 } /* cs_reset */
2445 * PRIVATE: cs_resolve_file
2447 * construct the full pathname to a file found in the cscope database.
2448 * (Prepends ppath, if there is one and if it's not already prepended,
2449 * otherwise just uses the name found.)
2451 * we need to prepend the prefix because on some cscope's (e.g., the one that
2452 * ships with Solaris 2.6), the output never has the prefix prepended.
2453 * contrast this with my development system (Digital Unix), which does.
2455 static char *
2456 cs_resolve_file(i, name)
2457 int i;
2458 char *name;
2460 char *fullname;
2461 int len;
2464 * ppath is freed when we destroy the cscope connection.
2465 * fullname is freed after cs_make_vim_style_matches, after it's been
2466 * copied into the tag buffer used by vim
2468 len = (int)(strlen(name) + 2);
2469 if (csinfo[i].ppath != NULL)
2470 len += (int)strlen(csinfo[i].ppath);
2472 if ((fullname = (char *)alloc(len)) == NULL)
2473 return NULL;
2476 * note/example: this won't work if the cscope output already starts
2477 * "../.." and the prefix path is also "../..". if something like this
2478 * happens, you are screwed up and need to fix how you're using cscope.
2480 if (csinfo[i].ppath != NULL &&
2481 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2482 (name[0] != '/')
2483 #ifdef WIN32
2484 && name[0] != '\\' && name[1] != ':'
2485 #endif
2487 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2488 else
2489 (void)sprintf(fullname, "%s", name);
2491 return fullname;
2492 } /* cs_resolve_file */
2496 * PRIVATE: cs_show
2498 * show all cscope connections
2500 /* ARGSUSED */
2501 static int
2502 cs_show(eap)
2503 exarg_T *eap;
2505 short i;
2506 if (cs_cnt_connections() == 0)
2507 MSG_PUTS(_("no cscope connections\n"));
2508 else
2510 MSG_PUTS_ATTR(
2511 _(" # pid database name prepend path\n"),
2512 hl_attr(HLF_T));
2513 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2515 if (csinfo[i].fname == NULL)
2516 continue;
2518 if (csinfo[i].ppath != NULL)
2519 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2520 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2521 else
2522 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2523 i, (long)csinfo[i].pid, csinfo[i].fname);
2527 wait_return(TRUE);
2528 return CSCOPE_SUCCESS;
2529 } /* cs_show */
2533 * PUBLIC: cs_end
2535 * Only called when VIM exits to quit any cscope sessions.
2537 void
2538 cs_end()
2540 int i;
2542 for (i = 0; i < CSCOPE_MAX_CONNECTIONS; i++)
2543 cs_release_csp(i, TRUE);
2546 #endif /* FEAT_CSCOPE */
2548 /* the end */