[7.2.371] Build problems on Tandem NonStop.
[vim_extended.git] / src / if_cscope.c
blobb8fef2887ed9ca40288ceb210c857dadbdbc8de4
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 clear_csinfo __ARGS((int i));
50 static int cs_insert_filelist __ARGS((char *, char *, char *,
51 struct stat *));
52 static int cs_kill __ARGS((exarg_T *eap));
53 static void cs_kill_execute __ARGS((int, char *));
54 static cscmd_T * cs_lookup_cmd __ARGS((exarg_T *eap));
55 static char * cs_make_vim_style_matches __ARGS((char *, char *,
56 char *, char *));
57 static char * cs_manage_matches __ARGS((char **, char **, int, mcmd_e));
58 static char * cs_parse_results __ARGS((int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search));
59 static char * cs_pathcomponents __ARGS((char *path));
60 static void cs_print_tags_priv __ARGS((char **, char **, int));
61 static int cs_read_prompt __ARGS((int));
62 static void cs_release_csp __ARGS((int, int freefnpp));
63 static int cs_reset __ARGS((exarg_T *eap));
64 static char * cs_resolve_file __ARGS((int, char *));
65 static int cs_show __ARGS((exarg_T *eap));
68 static csinfo_T * csinfo = NULL;
69 static int csinfo_size = 0; /* number of items allocated in
70 csinfo[] */
72 static int eap_arg_len; /* length of eap->arg, set in
73 cs_lookup_cmd() */
74 static cscmd_T cs_cmds[] =
76 { "add", cs_add,
77 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
78 { "find", cs_find,
79 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
80 { "help", cs_help,
81 N_("Show this message"), "help", 0 },
82 { "kill", cs_kill,
83 N_("Kill a connection"), "kill #", 0 },
84 { "reset", cs_reset,
85 N_("Reinit all connections"), "reset", 0 },
86 { "show", cs_show,
87 N_("Show connections"), "show", 0 },
88 { NULL, NULL, NULL, NULL, 0 }
91 static void
92 cs_usage_msg(x)
93 csid_e x;
95 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage);
98 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
100 static enum
102 EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */
103 EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */
104 EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */
105 EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */
106 } expand_what;
109 * Function given to ExpandGeneric() to obtain the cscope command
110 * expansion.
112 char_u *
113 get_cscope_name(xp, idx)
114 expand_T *xp UNUSED;
115 int idx;
117 int current_idx;
118 int i;
120 switch (expand_what)
122 case EXP_CSCOPE_SUBCMD:
123 /* Complete with sub-commands of ":cscope":
124 * add, find, help, kill, reset, show */
125 return (char_u *)cs_cmds[idx].name;
126 case EXP_SCSCOPE_SUBCMD:
127 /* Complete with sub-commands of ":scscope": same sub-commands as
128 * ":cscope" but skip commands which don't support split windows */
129 for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++)
130 if (cs_cmds[i].cansplit)
131 if (current_idx++ == idx)
132 break;
133 return (char_u *)cs_cmds[i].name;
134 case EXP_CSCOPE_FIND:
136 const char *query_type[] =
138 "c", "d", "e", "f", "g", "i", "s", "t", NULL
141 /* Complete with query type of ":cscope find {query_type}".
142 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
143 * ..., 8) but only complete with letters, since numbers are
144 * redundant. */
145 return (char_u *)query_type[idx];
147 case EXP_CSCOPE_KILL:
149 static char connection[5];
151 /* ":cscope kill" accepts connection numbers or partial names of
152 * the pathname of the cscope database as argument. Only complete
153 * with connection numbers. -1 can also be used to kill all
154 * connections. */
155 for (i = 0, current_idx = 0; i < csinfo_size; i++)
157 if (csinfo[i].fname == NULL)
158 continue;
159 if (current_idx++ == idx)
161 vim_snprintf(connection, sizeof(connection), "%d", i);
162 return (char_u *)connection;
165 return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL;
167 default:
168 return NULL;
173 * Handle command line completion for :cscope command.
175 void
176 set_context_in_cscope_cmd(xp, arg, cmdidx)
177 expand_T *xp;
178 char_u *arg;
179 cmdidx_T cmdidx;
181 char_u *p;
183 /* Default: expand subcommands */
184 xp->xp_context = EXPAND_CSCOPE;
185 xp->xp_pattern = arg;
186 expand_what = (cmdidx == CMD_scscope)
187 ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD;
189 /* (part of) subcommand already typed */
190 if (*arg != NUL)
192 p = skiptowhite(arg);
193 if (*p != NUL) /* past first word */
195 xp->xp_pattern = skipwhite(p);
196 if (*skiptowhite(xp->xp_pattern) != NUL)
197 xp->xp_context = EXPAND_NOTHING;
198 else if (STRNICMP(arg, "add", p - arg) == 0)
199 xp->xp_context = EXPAND_FILES;
200 else if (STRNICMP(arg, "kill", p - arg) == 0)
201 expand_what = EXP_CSCOPE_KILL;
202 else if (STRNICMP(arg, "find", p - arg) == 0)
203 expand_what = EXP_CSCOPE_FIND;
204 else
205 xp->xp_context = EXPAND_NOTHING;
210 #endif /* FEAT_CMDL_COMPL */
213 * PRIVATE: do_cscope_general
215 * Find the command, print help if invalid, and then call the corresponding
216 * command function.
218 static void
219 do_cscope_general(eap, make_split)
220 exarg_T *eap;
221 int make_split; /* whether to split window */
223 cscmd_T *cmdp;
225 if ((cmdp = cs_lookup_cmd(eap)) == NULL)
227 cs_help(eap);
228 return;
231 #ifdef FEAT_WINDOWS
232 if (make_split)
234 if (!cmdp->cansplit)
236 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
237 return;
239 postponed_split = -1;
240 postponed_split_flags = cmdmod.split;
241 postponed_split_tab = cmdmod.tab;
243 #endif
245 cmdp->func(eap);
247 #ifdef FEAT_WINDOWS
248 postponed_split_flags = 0;
249 postponed_split_tab = 0;
250 #endif
254 * PUBLIC: do_cscope
256 void
257 do_cscope(eap)
258 exarg_T *eap;
260 do_cscope_general(eap, FALSE);
264 * PUBLIC: do_scscope
266 * same as do_cscope, but splits window, too.
268 void
269 do_scscope(eap)
270 exarg_T *eap;
272 do_cscope_general(eap, TRUE);
276 * PUBLIC: do_cstag
279 void
280 do_cstag(eap)
281 exarg_T *eap;
283 int ret = FALSE;
285 if (*eap->arg == NUL)
287 (void)EMSG(_("E562: Usage: cstag <ident>"));
288 return;
291 switch (p_csto)
293 case 0 :
294 if (cs_check_for_connections())
296 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
297 FALSE);
298 if (ret == FALSE)
300 cs_free_tags();
301 if (msg_col)
302 msg_putchar('\n');
304 if (cs_check_for_tags())
305 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
308 else if (cs_check_for_tags())
310 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
312 break;
313 case 1 :
314 if (cs_check_for_tags())
316 ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
317 if (ret == FALSE)
319 if (msg_col)
320 msg_putchar('\n');
322 if (cs_check_for_connections())
324 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
325 FALSE, FALSE);
326 if (ret == FALSE)
327 cs_free_tags();
331 else if (cs_check_for_connections())
333 ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
334 FALSE);
335 if (ret == FALSE)
336 cs_free_tags();
338 break;
339 default :
340 break;
343 if (!ret)
345 (void)EMSG(_("E257: cstag: tag not found"));
346 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
347 g_do_tagpreview = 0;
348 #endif
351 } /* do_cscope */
355 * PUBLIC: cs_find
357 * this simulates a vim_fgets(), but for cscope, returns the next line
358 * from the cscope output. should only be called from find_tags()
360 * returns TRUE if eof, FALSE otherwise
363 cs_fgets(buf, size)
364 char_u *buf;
365 int size;
367 char *p;
369 if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL)
370 return TRUE;
371 vim_strncpy(buf, (char_u *)p, size - 1);
373 return FALSE;
374 } /* cs_fgets */
378 * PUBLIC: cs_free_tags
380 * called only from do_tag(), when popping the tag stack
382 void
383 cs_free_tags()
385 cs_manage_matches(NULL, NULL, -1, Free);
390 * PUBLIC: cs_print_tags
392 * called from do_tag()
394 void
395 cs_print_tags()
397 cs_manage_matches(NULL, NULL, -1, Print);
402 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
404 * Checks for the existence of a |cscope| connection. If no
405 * parameters are specified, then the function returns:
407 * 0, if cscope was not available (not compiled in), or if there
408 * are no cscope connections; or
409 * 1, if there is at least one cscope connection.
411 * If parameters are specified, then the value of {num}
412 * determines how existence of a cscope connection is checked:
414 * {num} Description of existence check
415 * ----- ------------------------------
416 * 0 Same as no parameters (e.g., "cscope_connection()").
417 * 1 Ignore {prepend}, and use partial string matches for
418 * {dbpath}.
419 * 2 Ignore {prepend}, and use exact string matches for
420 * {dbpath}.
421 * 3 Use {prepend}, use partial string matches for both
422 * {dbpath} and {prepend}.
423 * 4 Use {prepend}, use exact string matches for both
424 * {dbpath} and {prepend}.
426 * Note: All string comparisons are case sensitive!
428 #if defined(FEAT_EVAL) || defined(PROTO)
430 cs_connection(num, dbpath, ppath)
431 int num;
432 char_u *dbpath;
433 char_u *ppath;
435 int i;
437 if (num < 0 || num > 4 || (num > 0 && !dbpath))
438 return FALSE;
440 for (i = 0; i < csinfo_size; i++)
442 if (!csinfo[i].fname)
443 continue;
445 if (num == 0)
446 return TRUE;
448 switch (num)
450 case 1:
451 if (strstr(csinfo[i].fname, (char *)dbpath))
452 return TRUE;
453 break;
454 case 2:
455 if (strcmp(csinfo[i].fname, (char *)dbpath) == 0)
456 return TRUE;
457 break;
458 case 3:
459 if (strstr(csinfo[i].fname, (char *)dbpath)
460 && ((!ppath && !csinfo[i].ppath)
461 || (ppath
462 && csinfo[i].ppath
463 && strstr(csinfo[i].ppath, (char *)ppath))))
464 return TRUE;
465 break;
466 case 4:
467 if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0)
468 && ((!ppath && !csinfo[i].ppath)
469 || (ppath
470 && csinfo[i].ppath
471 && (strcmp(csinfo[i].ppath, (char *)ppath) == 0))))
472 return TRUE;
473 break;
477 return FALSE;
478 } /* cs_connection */
479 #endif
483 * PRIVATE functions
484 ****************************************************************************/
487 * PRIVATE: cs_add
489 * add cscope database or a directory name (to look for cscope.out)
490 * to the cscope connection list
492 * MAXPATHL 256
494 static int
495 cs_add(eap)
496 exarg_T *eap UNUSED;
498 char *fname, *ppath, *flags = NULL;
500 if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL)
502 cs_usage_msg(Add);
503 return CSCOPE_FAILURE;
505 if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL)
506 flags = strtok((char *)NULL, (const char *)" ");
508 return cs_add_common(fname, ppath, flags);
511 static void
512 cs_stat_emsg(fname)
513 char *fname;
515 char *stat_emsg = _("E563: stat(%s) error: %d");
516 char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10);
518 if (buf != NULL)
520 (void)sprintf(buf, stat_emsg, fname, errno);
521 (void)EMSG(buf);
522 vim_free(buf);
524 else
525 (void)EMSG(_("E563: stat error"));
530 * PRIVATE: cs_add_common
532 * the common routine to add a new cscope connection. called by
533 * cs_add() and cs_reset(). i really don't like to do this, but this
534 * routine uses a number of goto statements.
536 static int
537 cs_add_common(arg1, arg2, flags)
538 char *arg1; /* filename - may contain environment variables */
539 char *arg2; /* prepend path - may contain environment variables */
540 char *flags;
542 struct stat statbuf;
543 int ret;
544 char *fname = NULL;
545 char *fname2 = NULL;
546 char *ppath = NULL;
547 int i;
549 /* get the filename (arg1), expand it, and try to stat it */
550 if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL)
551 goto add_err;
553 expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
554 ret = stat(fname, &statbuf);
555 if (ret < 0)
557 staterr:
558 if (p_csverbose)
559 cs_stat_emsg(fname);
560 goto add_err;
563 /* get the prepend path (arg2), expand it, and try to stat it */
564 if (arg2 != NULL)
566 struct stat statbuf2;
568 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
569 goto add_err;
571 expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL);
572 ret = stat(ppath, &statbuf2);
573 if (ret < 0)
574 goto staterr;
577 /* if filename is a directory, append the cscope database name to it */
578 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
580 fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2));
581 if (fname2 == NULL)
582 goto add_err;
584 while (fname[strlen(fname)-1] == '/'
585 #ifdef WIN32
586 || fname[strlen(fname)-1] == '\\'
587 #endif
590 fname[strlen(fname)-1] = '\0';
591 if (strlen(fname) == 0)
592 break;
594 if (fname[0] == '\0')
595 (void)sprintf(fname2, "/%s", CSCOPE_DBFILE);
596 else
597 (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE);
599 ret = stat(fname2, &statbuf);
600 if (ret < 0)
602 if (p_csverbose)
603 cs_stat_emsg(fname2);
604 goto add_err;
607 i = cs_insert_filelist(fname2, ppath, flags, &statbuf);
609 #if defined(UNIX)
610 else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
611 #else
612 /* WIN32 - substitute define S_ISREG from os_unix.h */
613 else if (((statbuf.st_mode) & S_IFMT) == S_IFREG)
614 #endif
616 i = cs_insert_filelist(fname, ppath, flags, &statbuf);
618 else
620 if (p_csverbose)
621 (void)EMSG2(
622 _("E564: %s is not a directory or a valid cscope database"),
623 fname);
624 goto add_err;
627 if (i != -1)
629 if (cs_create_connection(i) == CSCOPE_FAILURE
630 || cs_read_prompt(i) == CSCOPE_FAILURE)
632 cs_release_csp(i, TRUE);
633 goto add_err;
636 if (p_csverbose)
638 msg_clr_eos();
639 (void)smsg_attr(hl_attr(HLF_R),
640 (char_u *)_("Added cscope database %s"),
641 csinfo[i].fname);
645 vim_free(fname);
646 vim_free(fname2);
647 vim_free(ppath);
648 return CSCOPE_SUCCESS;
650 add_err:
651 vim_free(fname2);
652 vim_free(fname);
653 vim_free(ppath);
654 return CSCOPE_FAILURE;
655 } /* cs_add_common */
658 static int
659 cs_check_for_connections()
661 return (cs_cnt_connections() > 0);
662 } /* cs_check_for_connections */
665 static int
666 cs_check_for_tags()
668 return (p_tags[0] != NUL && curbuf->b_p_tags != NULL);
669 } /* cs_check_for_tags */
673 * PRIVATE: cs_cnt_connections
675 * count the number of cscope connections
677 static int
678 cs_cnt_connections()
680 short i;
681 short cnt = 0;
683 for (i = 0; i < csinfo_size; i++)
685 if (csinfo[i].fname != NULL)
686 cnt++;
688 return cnt;
689 } /* cs_cnt_connections */
691 static void
692 cs_reading_emsg(idx)
693 int idx; /* connection index */
695 EMSGN(_("E262: error reading cscope connection %ld"), idx);
698 #define CSREAD_BUFSIZE 2048
700 * PRIVATE: cs_cnt_matches
702 * count the number of matches for a given cscope connection.
704 static int
705 cs_cnt_matches(idx)
706 int idx;
708 char *stok;
709 char *buf;
710 int nlines;
712 buf = (char *)alloc(CSREAD_BUFSIZE);
713 if (buf == NULL)
714 return 0;
715 for (;;)
717 if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp))
719 if (feof(csinfo[idx].fr_fp))
720 errno = EIO;
722 cs_reading_emsg(idx);
724 vim_free(buf);
725 return -1;
729 * If the database is out of date, or there's some other problem,
730 * cscope will output error messages before the number-of-lines output.
731 * Display/discard any output that doesn't match what we want.
732 * Accept "\S*cscope: X lines", also matches "mlcscope".
734 if ((stok = strtok(buf, (const char *)" ")) == NULL)
735 continue;
736 if (strstr((const char *)stok, "cscope:") == NULL)
737 continue;
739 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
740 continue;
741 nlines = atoi(stok);
742 if (nlines < 0)
744 nlines = 0;
745 break;
748 if ((stok = strtok(NULL, (const char *)" ")) == NULL)
749 continue;
750 if (strncmp((const char *)stok, "lines", 5))
751 continue;
753 break;
756 vim_free(buf);
757 return nlines;
758 } /* cs_cnt_matches */
762 * PRIVATE: cs_create_cmd
764 * Creates the actual cscope command query from what the user entered.
766 static char *
767 cs_create_cmd(csoption, pattern)
768 char *csoption;
769 char *pattern;
771 char *cmd;
772 short search;
773 char *pat;
775 switch (csoption[0])
777 case '0' : case 's' :
778 search = 0;
779 break;
780 case '1' : case 'g' :
781 search = 1;
782 break;
783 case '2' : case 'd' :
784 search = 2;
785 break;
786 case '3' : case 'c' :
787 search = 3;
788 break;
789 case '4' : case 't' :
790 search = 4;
791 break;
792 case '6' : case 'e' :
793 search = 6;
794 break;
795 case '7' : case 'f' :
796 search = 7;
797 break;
798 case '8' : case 'i' :
799 search = 8;
800 break;
801 default :
802 (void)EMSG(_("E561: unknown cscope search type"));
803 cs_usage_msg(Find);
804 return NULL;
807 /* Skip white space before the patter, except for text and pattern search,
808 * they may want to use the leading white space. */
809 pat = pattern;
810 if (search != 4 && search != 6)
811 while vim_iswhite(*pat)
812 ++pat;
814 if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL)
815 return NULL;
817 (void)sprintf(cmd, "%d%s", search, pat);
819 return cmd;
820 } /* cs_create_cmd */
824 * PRIVATE: cs_create_connection
826 * This piece of code was taken/adapted from nvi. do we need to add
827 * the BSD license notice?
829 static int
830 cs_create_connection(i)
831 int i;
833 #ifdef UNIX
834 int to_cs[2], from_cs[2];
835 #endif
836 int len;
837 char *prog, *cmd, *ppath = NULL;
838 #ifdef WIN32
839 int fd;
840 SECURITY_ATTRIBUTES sa;
841 PROCESS_INFORMATION pi;
842 STARTUPINFO si;
843 BOOL pipe_stdin = FALSE, pipe_stdout = FALSE;
844 HANDLE stdin_rd, stdout_rd;
845 HANDLE stdout_wr, stdin_wr;
846 BOOL created;
847 # ifdef __BORLANDC__
848 # define OPEN_OH_ARGTYPE long
849 # else
850 # if (_MSC_VER >= 1300)
851 # define OPEN_OH_ARGTYPE intptr_t
852 # else
853 # define OPEN_OH_ARGTYPE long
854 # endif
855 # endif
856 #endif
858 #if defined(UNIX)
860 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
861 * from_cs[0] and writes to to_cs[1].
863 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
864 if (pipe(to_cs) < 0 || pipe(from_cs) < 0)
866 (void)EMSG(_("E566: Could not create cscope pipes"));
867 err_closing:
868 if (to_cs[0] != -1)
869 (void)close(to_cs[0]);
870 if (to_cs[1] != -1)
871 (void)close(to_cs[1]);
872 if (from_cs[0] != -1)
873 (void)close(from_cs[0]);
874 if (from_cs[1] != -1)
875 (void)close(from_cs[1]);
876 return CSCOPE_FAILURE;
879 switch (csinfo[i].pid = fork())
881 case -1:
882 (void)EMSG(_("E622: Could not fork for cscope"));
883 goto err_closing;
884 case 0: /* child: run cscope. */
885 if (dup2(to_cs[0], STDIN_FILENO) == -1)
886 PERROR("cs_create_connection 1");
887 if (dup2(from_cs[1], STDOUT_FILENO) == -1)
888 PERROR("cs_create_connection 2");
889 if (dup2(from_cs[1], STDERR_FILENO) == -1)
890 PERROR("cs_create_connection 3");
892 /* close unused */
893 (void)close(to_cs[1]);
894 (void)close(from_cs[0]);
895 #else
896 /* WIN32 */
897 /* Create pipes to communicate with cscope */
898 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
899 sa.bInheritHandle = TRUE;
900 sa.lpSecurityDescriptor = NULL;
902 if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0))
903 || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0)))
905 (void)EMSG(_("E566: Could not create cscope pipes"));
906 err_closing:
907 if (pipe_stdin)
909 CloseHandle(stdin_rd);
910 CloseHandle(stdin_wr);
912 if (pipe_stdout)
914 CloseHandle(stdout_rd);
915 CloseHandle(stdout_wr);
917 return CSCOPE_FAILURE;
919 #endif
920 /* expand the cscope exec for env var's */
921 if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL)
923 #ifdef UNIX
924 return CSCOPE_FAILURE;
925 #else
926 /* WIN32 */
927 goto err_closing;
928 #endif
930 expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL);
932 /* alloc space to hold the cscope command */
933 len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32);
934 if (csinfo[i].ppath)
936 /* expand the prepend path for env var's */
937 if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL)
939 vim_free(prog);
940 #ifdef UNIX
941 return CSCOPE_FAILURE;
942 #else
943 /* WIN32 */
944 goto err_closing;
945 #endif
947 expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL);
949 len += (int)strlen(ppath);
952 if (csinfo[i].flags)
953 len += (int)strlen(csinfo[i].flags);
955 if ((cmd = (char *)alloc(len)) == NULL)
957 vim_free(prog);
958 vim_free(ppath);
959 #ifdef UNIX
960 return CSCOPE_FAILURE;
961 #else
962 /* WIN32 */
963 goto err_closing;
964 #endif
967 /* run the cscope command; is there execl for non-unix systems? */
968 #if defined(UNIX)
969 (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname);
970 #else
971 /* WIN32 */
972 (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname);
973 #endif
974 if (csinfo[i].ppath != NULL)
976 (void)strcat(cmd, " -P");
977 (void)strcat(cmd, csinfo[i].ppath);
979 if (csinfo[i].flags != NULL)
981 (void)strcat(cmd, " ");
982 (void)strcat(cmd, csinfo[i].flags);
984 # ifdef UNIX
985 /* on Win32 we still need prog */
986 vim_free(prog);
987 # endif
988 vim_free(ppath);
990 #if defined(UNIX)
991 if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1)
992 PERROR(_("cs_create_connection exec failed"));
994 exit(127);
995 /* NOTREACHED */
996 default: /* parent. */
998 * Save the file descriptors for later duplication, and
999 * reopen as streams.
1001 if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL)
1002 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1003 if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL)
1004 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1006 /* close unused */
1007 (void)close(to_cs[0]);
1008 (void)close(from_cs[1]);
1010 break;
1013 #else
1014 /* WIN32 */
1015 /* Create a new process to run cscope and use pipes to talk with it */
1016 GetStartupInfo(&si);
1017 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1018 si.wShowWindow = SW_HIDE; /* Hide child application window */
1019 si.hStdOutput = stdout_wr;
1020 si.hStdError = stdout_wr;
1021 si.hStdInput = stdin_rd;
1022 created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE,
1023 NULL, NULL, &si, &pi);
1024 vim_free(prog);
1025 vim_free(cmd);
1027 if (!created)
1029 PERROR(_("cs_create_connection exec failed"));
1030 (void)EMSG(_("E623: Could not spawn cscope process"));
1031 goto err_closing;
1033 /* else */
1034 csinfo[i].pid = pi.dwProcessId;
1035 csinfo[i].hProc = pi.hProcess;
1036 CloseHandle(pi.hThread);
1038 /* TODO - tidy up after failure to create files on pipe handles. */
1039 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr,
1040 _O_TEXT|_O_APPEND)) < 0)
1041 || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL))
1042 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1043 if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd,
1044 _O_TEXT|_O_RDONLY)) < 0)
1045 || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL))
1046 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1048 /* Close handles for file descriptors inherited by the cscope process */
1049 CloseHandle(stdin_rd);
1050 CloseHandle(stdout_wr);
1052 #endif /* !UNIX */
1054 return CSCOPE_SUCCESS;
1055 } /* cs_create_connection */
1059 * PRIVATE: cs_find
1061 * query cscope using command line interface. parse the output and use tselect
1062 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1064 * returns TRUE if we jump to a tag or abort, FALSE if not.
1066 static int
1067 cs_find(eap)
1068 exarg_T *eap;
1070 char *opt, *pat;
1072 if (cs_check_for_connections() == FALSE)
1074 (void)EMSG(_("E567: no cscope connections"));
1075 return FALSE;
1078 if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL)
1080 cs_usage_msg(Find);
1081 return FALSE;
1084 pat = opt + strlen(opt) + 1;
1085 if (pat >= (char *)eap->arg + eap_arg_len)
1087 cs_usage_msg(Find);
1088 return FALSE;
1091 return cs_find_common(opt, pat, eap->forceit, TRUE,
1092 eap->cmdidx == CMD_lcscope);
1093 } /* cs_find */
1097 * PRIVATE: cs_find_common
1099 * common code for cscope find, shared by cs_find() and do_cstag()
1101 static int
1102 cs_find_common(opt, pat, forceit, verbose, use_ll)
1103 char *opt;
1104 char *pat;
1105 int forceit;
1106 int verbose;
1107 int use_ll;
1109 int i;
1110 char *cmd;
1111 int *nummatches;
1112 int totmatches;
1113 #ifdef FEAT_QUICKFIX
1114 char cmdletter;
1115 char *qfpos;
1116 #endif
1118 /* create the actual command to send to cscope */
1119 cmd = cs_create_cmd(opt, pat);
1120 if (cmd == NULL)
1121 return FALSE;
1123 nummatches = (int *)alloc(sizeof(int)*csinfo_size);
1124 if (nummatches == NULL)
1125 return FALSE;
1127 /* send query to all open connections, then count the total number
1128 * of matches so we can alloc matchesp all in one swell foop
1130 for (i = 0; i < csinfo_size; i++)
1131 nummatches[i] = 0;
1132 totmatches = 0;
1133 for (i = 0; i < csinfo_size; i++)
1135 if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL)
1136 continue;
1138 /* send cmd to cscope */
1139 (void)fprintf(csinfo[i].to_fp, "%s\n", cmd);
1140 (void)fflush(csinfo[i].to_fp);
1142 nummatches[i] = cs_cnt_matches(i);
1144 if (nummatches[i] > -1)
1145 totmatches += nummatches[i];
1147 if (nummatches[i] == 0)
1148 (void)cs_read_prompt(i);
1150 vim_free(cmd);
1152 if (totmatches == 0)
1154 char *nf = _("E259: no matches found for cscope query %s of %s");
1155 char *buf;
1157 if (!verbose)
1159 vim_free(nummatches);
1160 return FALSE;
1163 buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf)));
1164 if (buf == NULL)
1165 (void)EMSG(nf);
1166 else
1168 sprintf(buf, nf, opt, pat);
1169 (void)EMSG(buf);
1170 vim_free(buf);
1172 vim_free(nummatches);
1173 return FALSE;
1176 #ifdef FEAT_QUICKFIX
1177 /* get cmd letter */
1178 switch (opt[0])
1180 case '0' :
1181 cmdletter = 's';
1182 break;
1183 case '1' :
1184 cmdletter = 'g';
1185 break;
1186 case '2' :
1187 cmdletter = 'd';
1188 break;
1189 case '3' :
1190 cmdletter = 'c';
1191 break;
1192 case '4' :
1193 cmdletter = 't';
1194 break;
1195 case '6' :
1196 cmdletter = 'e';
1197 break;
1198 case '7' :
1199 cmdletter = 'f';
1200 break;
1201 case '8' :
1202 cmdletter = 'i';
1203 break;
1204 default :
1205 cmdletter = opt[0];
1208 qfpos = (char *)vim_strchr(p_csqf, cmdletter);
1209 if (qfpos != NULL)
1211 qfpos++;
1212 /* next symbol must be + or - */
1213 if (strchr(CSQF_FLAGS, *qfpos) == NULL)
1215 char *nf = _("E469: invalid cscopequickfix flag %c for %c");
1216 char *buf = (char *)alloc((unsigned)strlen(nf));
1218 /* strlen will be enough because we use chars */
1219 if (buf != NULL)
1221 sprintf(buf, nf, *qfpos, *(qfpos-1));
1222 (void)EMSG(buf);
1223 vim_free(buf);
1225 vim_free(nummatches);
1226 return FALSE;
1229 if (qfpos != NULL && *qfpos != '0' && totmatches > 0)
1231 /* fill error list */
1232 FILE *f;
1233 char_u *tmp = vim_tempname('c');
1234 qf_info_T *qi = NULL;
1235 win_T *wp = NULL;
1237 f = mch_fopen((char *)tmp, "w");
1238 if (f == NULL)
1239 EMSG2(_(e_notopen), tmp);
1240 else
1242 cs_file_results(f, nummatches);
1243 fclose(f);
1244 if (use_ll) /* Use location list */
1245 wp = curwin;
1246 /* '-' starts a new error list */
1247 if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m",
1248 *qfpos == '-') > 0)
1250 # ifdef FEAT_WINDOWS
1251 if (postponed_split != 0)
1253 win_split(postponed_split > 0 ? postponed_split : 0,
1254 postponed_split_flags);
1255 # ifdef FEAT_SCROLLBIND
1256 curwin->w_p_scb = FALSE;
1257 # endif
1258 postponed_split = 0;
1260 # endif
1261 if (use_ll)
1263 * In the location list window, use the displayed location
1264 * list. Otherwise, use the location list for the window.
1266 qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
1267 ? wp->w_llist_ref : wp->w_llist;
1268 qf_jump(qi, 0, 0, forceit);
1271 mch_remove(tmp);
1272 vim_free(tmp);
1273 vim_free(nummatches);
1274 return TRUE;
1276 else
1277 #endif /* FEAT_QUICKFIX */
1279 char **matches = NULL, **contexts = NULL;
1280 int matched = 0;
1282 /* read output */
1283 cs_fill_results((char *)pat, totmatches, nummatches, &matches,
1284 &contexts, &matched);
1285 vim_free(nummatches);
1286 if (matches == NULL)
1287 return FALSE;
1289 (void)cs_manage_matches(matches, contexts, matched, Store);
1291 return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose);
1294 } /* cs_find_common */
1297 * PRIVATE: cs_help
1299 * print help
1301 static int
1302 cs_help(eap)
1303 exarg_T *eap UNUSED;
1305 cscmd_T *cmdp = cs_cmds;
1307 (void)MSG_PUTS(_("cscope commands:\n"));
1308 while (cmdp->name != NULL)
1310 char *help = _(cmdp->help);
1311 int space_cnt = 30 - vim_strsize((char_u *)help);
1313 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1314 if (space_cnt < 0)
1315 space_cnt = 0;
1316 (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"),
1317 cmdp->name,
1318 help, space_cnt, " ",
1319 cmdp->usage);
1320 if (strcmp(cmdp->name, "find") == 0)
1321 MSG_PUTS(_("\n"
1322 " c: Find functions calling this function\n"
1323 " d: Find functions called by this function\n"
1324 " e: Find this egrep pattern\n"
1325 " f: Find this file\n"
1326 " g: Find this definition\n"
1327 " i: Find files #including this file\n"
1328 " s: Find this C symbol\n"
1329 " t: Find assignments to\n"));
1331 cmdp++;
1334 wait_return(TRUE);
1335 return 0;
1336 } /* cs_help */
1339 static void
1340 clear_csinfo(i)
1341 int i;
1343 csinfo[i].fname = NULL;
1344 csinfo[i].ppath = NULL;
1345 csinfo[i].flags = NULL;
1346 #if defined(UNIX)
1347 csinfo[i].st_dev = (dev_t)0;
1348 csinfo[i].st_ino = (ino_t)0;
1349 #else
1350 csinfo[i].nVolume = 0;
1351 csinfo[i].nIndexHigh = 0;
1352 csinfo[i].nIndexLow = 0;
1353 #endif
1354 csinfo[i].pid = 0;
1355 csinfo[i].fr_fp = NULL;
1356 csinfo[i].to_fp = NULL;
1357 #if defined(WIN32)
1358 csinfo[i].hProc = NULL;
1359 #endif
1362 #ifndef UNIX
1363 static char *GetWin32Error __ARGS((void));
1365 static char *
1366 GetWin32Error()
1368 char *msg = NULL;
1369 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1370 NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
1371 if (msg != NULL)
1373 /* remove trailing \r\n */
1374 char *pcrlf = strstr(msg, "\r\n");
1375 if (pcrlf != NULL)
1376 *pcrlf = '\0';
1378 return msg;
1380 #endif
1383 * PRIVATE: cs_insert_filelist
1385 * insert a new cscope database filename into the filelist
1387 static int
1388 cs_insert_filelist(fname, ppath, flags, sb)
1389 char *fname;
1390 char *ppath;
1391 char *flags;
1392 struct stat *sb UNUSED;
1394 short i, j;
1395 #ifndef UNIX
1396 HANDLE hFile;
1397 BY_HANDLE_FILE_INFORMATION bhfi;
1399 vim_memset(&bhfi, 0, sizeof(bhfi));
1400 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1401 if (!mch_windows95())
1403 hFile = CreateFile(fname, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
1404 FILE_ATTRIBUTE_NORMAL, NULL);
1405 if (hFile == INVALID_HANDLE_VALUE)
1407 if (p_csverbose)
1409 char *cant_msg = _("E625: cannot open cscope database: %s");
1410 char *winmsg = GetWin32Error();
1412 if (winmsg != NULL)
1414 (void)EMSG2(cant_msg, winmsg);
1415 LocalFree(winmsg);
1417 else
1418 /* subst filename if can't get error text */
1419 (void)EMSG2(cant_msg, fname);
1421 return -1;
1423 if (!GetFileInformationByHandle(hFile, &bhfi))
1425 CloseHandle(hFile);
1426 if (p_csverbose)
1427 (void)EMSG(_("E626: cannot get cscope database information"));
1428 return -1;
1430 CloseHandle(hFile);
1432 #endif
1434 i = -1; /* can be set to the index of an empty item in csinfo */
1435 for (j = 0; j < csinfo_size; j++)
1437 if (csinfo[j].fname != NULL
1438 #if defined(UNIX)
1439 && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino
1440 #else
1441 /* compare pathnames first */
1442 && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME)
1443 /* if not Windows 9x, test index file attributes too */
1444 || (!mch_windows95()
1445 && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber
1446 && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh
1447 && csinfo[j].nIndexLow == bhfi.nFileIndexLow))
1448 #endif
1451 if (p_csverbose)
1452 (void)EMSG(_("E568: duplicate cscope database not added"));
1453 return -1;
1456 if (csinfo[j].fname == NULL && i == -1)
1457 i = j; /* remember first empty entry */
1460 if (i == -1)
1462 i = csinfo_size;
1463 if (csinfo_size == 0)
1465 /* First time allocation: allocate only 1 connection. It should
1466 * be enough for most users. If more is needed, csinfo will be
1467 * reallocated. */
1468 csinfo_size = 1;
1469 csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
1471 else
1473 /* Reallocate space for more connections. */
1474 csinfo_size *= 2;
1475 csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
1477 if (csinfo == NULL)
1478 return -1;
1479 for (j = csinfo_size/2; j < csinfo_size; j++)
1480 clear_csinfo(j);
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 static int
1565 cs_kill(eap)
1566 exarg_T *eap UNUSED;
1568 char *stok;
1569 short i;
1571 if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL)
1573 cs_usage_msg(Kill);
1574 return CSCOPE_FAILURE;
1577 /* only single digit positive and negative integers are allowed */
1578 if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0])))
1579 || (strlen(stok) < 3 && stok[0] == '-'
1580 && VIM_ISDIGIT((int)(stok[1]))))
1581 i = atoi(stok);
1582 else
1584 /* It must be part of a name. We will try to find a match
1585 * within all the names in the csinfo data structure
1587 for (i = 0; i < csinfo_size; i++)
1589 if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok))
1590 break;
1594 if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL))
1596 if (p_csverbose)
1597 (void)EMSG2(_("E261: cscope connection %s not found"), stok);
1599 else
1601 if (i == -1)
1603 for (i = 0; i < csinfo_size; i++)
1605 if (csinfo[i].fname)
1606 cs_kill_execute(i, csinfo[i].fname);
1609 else
1610 cs_kill_execute(i, stok);
1613 return 0;
1614 } /* cs_kill */
1618 * PRIVATE: cs_kill_execute
1620 * Actually kills a specific cscope connection.
1622 static void
1623 cs_kill_execute(i, cname)
1624 int i; /* cscope table index */
1625 char *cname; /* cscope database name */
1627 if (p_csverbose)
1629 msg_clr_eos();
1630 (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST,
1631 (char_u *)_("cscope connection %s closed"), cname);
1633 cs_release_csp(i, TRUE);
1638 * PRIVATE: cs_make_vim_style_matches
1640 * convert the cscope output into into a ctags style entry (as might be found
1641 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1642 * the type of the tag you are looking for. for example, in Darren Hiebert's
1643 * ctags (the one that comes with vim), #define's use a line number to find the
1644 * tag in a file while function definitions use a regexp search pattern.
1646 * i'm going to always use the line number because cscope does something
1647 * quirky (and probably other things i don't know about):
1649 * if you have "# define" in your source file, which is
1650 * perfectly legal, cscope thinks you have "#define". this
1651 * will result in a failed regexp search. :(
1653 * besides, even if this particular case didn't happen, the search pattern
1654 * would still have to be modified to escape all the special regular expression
1655 * characters to comply with ctags formatting.
1657 static char *
1658 cs_make_vim_style_matches(fname, slno, search, tagstr)
1659 char *fname;
1660 char *slno;
1661 char *search;
1662 char *tagstr;
1664 /* vim style is ctags:
1666 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1668 * but as mentioned above, we'll always use the line number and
1669 * put the search pattern (if one exists) as "extra"
1671 * buf is used as part of vim's method of handling tags, and
1672 * (i think) vim frees it when you pop your tags and get replaced
1673 * by new ones on the tag stack.
1675 char *buf;
1676 int amt;
1678 if (search != NULL)
1680 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6);
1681 if ((buf = (char *)alloc(amt)) == NULL)
1682 return NULL;
1684 (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search);
1686 else
1688 amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5);
1689 if ((buf = (char *)alloc(amt)) == NULL)
1690 return NULL;
1692 (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno);
1695 return buf;
1696 } /* cs_make_vim_style_matches */
1700 * PRIVATE: cs_manage_matches
1702 * this is kind of hokey, but i don't see an easy way round this..
1704 * Store: keep a ptr to the (malloc'd) memory of matches originally
1705 * generated from cs_find(). the matches are originally lines directly
1706 * from cscope output, but transformed to look like something out of a
1707 * ctags. see cs_make_vim_style_matches for more details.
1709 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1710 * the next line from the cscope output. it basically keeps track of which
1711 * lines have been "used" and returns the next one.
1713 * Free: frees up everything and resets
1715 * Print: prints the tags
1717 static char *
1718 cs_manage_matches(matches, contexts, totmatches, cmd)
1719 char **matches;
1720 char **contexts;
1721 int totmatches;
1722 mcmd_e cmd;
1724 static char **mp = NULL;
1725 static char **cp = NULL;
1726 static int cnt = -1;
1727 static int next = -1;
1728 char *p = NULL;
1730 switch (cmd)
1732 case Store:
1733 assert(matches != NULL);
1734 assert(totmatches > 0);
1735 if (mp != NULL || cp != NULL)
1736 (void)cs_manage_matches(NULL, NULL, -1, Free);
1737 mp = matches;
1738 cp = contexts;
1739 cnt = totmatches;
1740 next = 0;
1741 break;
1742 case Get:
1743 if (next >= cnt)
1744 return NULL;
1746 p = mp[next];
1747 next++;
1748 break;
1749 case Free:
1750 if (mp != NULL)
1752 if (cnt > 0)
1753 while (cnt--)
1755 vim_free(mp[cnt]);
1756 if (cp != NULL)
1757 vim_free(cp[cnt]);
1759 vim_free(mp);
1760 vim_free(cp);
1762 mp = NULL;
1763 cp = NULL;
1764 cnt = 0;
1765 next = 0;
1766 break;
1767 case Print:
1768 cs_print_tags_priv(mp, cp, cnt);
1769 break;
1770 default: /* should not reach here */
1771 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1772 return NULL;
1775 return p;
1776 } /* cs_manage_matches */
1780 * PRIVATE: cs_parse_results
1782 * parse cscope output
1784 static char *
1785 cs_parse_results(cnumber, buf, bufsize, context, linenumber, search)
1786 int cnumber;
1787 char *buf;
1788 int bufsize;
1789 char **context;
1790 char **linenumber;
1791 char **search;
1793 int ch;
1794 char *p;
1795 char *name;
1797 if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL)
1799 if (feof(csinfo[cnumber].fr_fp))
1800 errno = EIO;
1802 cs_reading_emsg(cnumber);
1804 return NULL;
1807 /* If the line's too long for the buffer, discard it. */
1808 if ((p = strchr(buf, '\n')) == NULL)
1810 while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n')
1812 return NULL;
1814 *p = '\0';
1817 * cscope output is in the following format:
1819 * <filename> <context> <line number> <pattern>
1821 if ((name = strtok((char *)buf, (const char *)" ")) == NULL)
1822 return NULL;
1823 if ((*context = strtok(NULL, (const char *)" ")) == NULL)
1824 return NULL;
1825 if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL)
1826 return NULL;
1827 *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */
1829 /* --- nvi ---
1830 * If the file is older than the cscope database, that is,
1831 * the database was built since the file was last modified,
1832 * or there wasn't a search string, use the line number.
1834 if (strcmp(*search, "<unknown>") == 0)
1835 *search = NULL;
1837 name = cs_resolve_file(cnumber, name);
1838 return name;
1841 #ifdef FEAT_QUICKFIX
1843 * PRIVATE: cs_file_results
1845 * write cscope find results to file
1847 static void
1848 cs_file_results(f, nummatches_a)
1849 FILE *f;
1850 int *nummatches_a;
1852 int i, j;
1853 char *buf;
1854 char *search, *slno;
1855 char *fullname;
1856 char *cntx;
1857 char *context;
1859 buf = (char *)alloc(CSREAD_BUFSIZE);
1860 if (buf == NULL)
1861 return;
1863 for (i = 0; i < csinfo_size; i++)
1865 if (nummatches_a[i] < 1)
1866 continue;
1868 for (j = 0; j < nummatches_a[i]; j++)
1870 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1871 &slno, &search)) == NULL)
1872 continue;
1874 context = (char *)alloc((unsigned)strlen(cntx)+5);
1875 if (context == NULL)
1876 continue;
1878 if (strcmp(cntx, "<global>")==0)
1879 strcpy(context, "<<global>>");
1880 else
1881 sprintf(context, "<<%s>>", cntx);
1883 if (search == NULL)
1884 fprintf(f, "%s\t%s\t%s\n", fullname, slno, context);
1885 else
1886 fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search);
1888 vim_free(context);
1889 vim_free(fullname);
1890 } /* for all matches */
1892 (void)cs_read_prompt(i);
1894 } /* for all cscope connections */
1895 vim_free(buf);
1897 #endif
1900 * PRIVATE: cs_fill_results
1902 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1903 * into ctags format
1904 * When there are no matches sets "*matches_p" to NULL.
1906 static void
1907 cs_fill_results(tagstr, totmatches, nummatches_a, matches_p, cntxts_p, matched)
1908 char *tagstr;
1909 int totmatches;
1910 int *nummatches_a;
1911 char ***matches_p;
1912 char ***cntxts_p;
1913 int *matched;
1915 int i, j;
1916 char *buf;
1917 char *search, *slno;
1918 int totsofar = 0;
1919 char **matches = NULL;
1920 char **cntxts = NULL;
1921 char *fullname;
1922 char *cntx;
1924 assert(totmatches > 0);
1926 buf = (char *)alloc(CSREAD_BUFSIZE);
1927 if (buf == NULL)
1928 return;
1930 if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1931 goto parse_out;
1932 if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL)
1933 goto parse_out;
1935 for (i = 0; i < csinfo_size; i++)
1937 if (nummatches_a[i] < 1)
1938 continue;
1940 for (j = 0; j < nummatches_a[i]; j++)
1942 if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx,
1943 &slno, &search)) == NULL)
1944 continue;
1946 matches[totsofar] = cs_make_vim_style_matches(fullname, slno,
1947 search, tagstr);
1949 vim_free(fullname);
1951 if (strcmp(cntx, "<global>") == 0)
1952 cntxts[totsofar] = NULL;
1953 else
1954 /* note: if vim_strsave returns NULL, then the context
1955 * will be "<global>", which is misleading.
1957 cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx);
1959 if (matches[totsofar] != NULL)
1960 totsofar++;
1962 } /* for all matches */
1964 (void)cs_read_prompt(i);
1966 } /* for all cscope connections */
1968 parse_out:
1969 if (totsofar == 0)
1971 /* No matches, free the arrays and return NULL in "*matches_p". */
1972 vim_free(matches);
1973 matches = NULL;
1974 vim_free(cntxts);
1975 cntxts = NULL;
1977 *matched = totsofar;
1978 *matches_p = matches;
1979 *cntxts_p = cntxts;
1981 vim_free(buf);
1982 } /* cs_fill_results */
1985 /* get the requested path components */
1986 static char *
1987 cs_pathcomponents(path)
1988 char *path;
1990 int i;
1991 char *s;
1993 if (p_cspc == 0)
1994 return path;
1996 s = path + strlen(path) - 1;
1997 for (i = 0; i < p_cspc; ++i)
1998 while (s > path && *--s != '/'
1999 #ifdef WIN32
2000 && *--s != '\\'
2001 #endif
2004 if ((s > path && *s == '/')
2005 #ifdef WIN32
2006 || (s > path && *s == '\\')
2007 #endif
2009 ++s;
2010 return s;
2014 * PRIVATE: cs_print_tags_priv
2016 * called from cs_manage_matches()
2018 static void
2019 cs_print_tags_priv(matches, cntxts, num_matches)
2020 char **matches;
2021 char **cntxts;
2022 int num_matches;
2024 char *buf = NULL;
2025 int bufsize = 0; /* Track available bufsize */
2026 int newsize = 0;
2027 char *ptag;
2028 char *fname, *lno, *extra, *tbuf;
2029 int i, idx, num;
2030 char *globalcntx = "GLOBAL";
2031 char *cntxformat = " <<%s>>";
2032 char *context;
2033 char *cstag_msg = _("Cscope tag: %s");
2034 char *csfmt_str = "%4d %6s ";
2036 assert (num_matches > 0);
2038 if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL)
2039 return;
2041 strcpy(tbuf, matches[0]);
2042 ptag = strtok(tbuf, "\t");
2044 newsize = (int)(strlen(cstag_msg) + strlen(ptag));
2045 buf = (char *)alloc(newsize);
2046 if (buf != NULL)
2048 bufsize = newsize;
2049 (void)sprintf(buf, cstag_msg, ptag);
2050 MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
2053 vim_free(tbuf);
2055 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */
2056 msg_advance(msg_col + 2);
2057 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
2059 num = 1;
2060 for (i = 0; i < num_matches; i++)
2062 idx = i;
2064 /* if we really wanted to, we could avoid this malloc and strcpy
2065 * by parsing matches[i] on the fly and placing stuff into buf
2066 * directly, but that's too much of a hassle
2068 if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL)
2069 continue;
2070 (void)strcpy(tbuf, matches[idx]);
2072 if (strtok(tbuf, (const char *)"\t") == NULL)
2073 continue;
2074 if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
2075 continue;
2076 if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
2077 continue;
2078 extra = strtok(NULL, (const char *)"\t");
2080 lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
2082 /* hopefully 'num' (num of matches) will be less than 10^16 */
2083 newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
2084 if (bufsize < newsize)
2086 buf = (char *)vim_realloc(buf, newsize);
2087 if (buf == NULL)
2088 bufsize = 0;
2089 else
2090 bufsize = newsize;
2092 if (buf != NULL)
2094 /* csfmt_str = "%4d %6s "; */
2095 (void)sprintf(buf, csfmt_str, num, lno);
2096 MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
2098 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
2100 /* compute the required space for the context */
2101 if (cntxts[idx] != NULL)
2102 context = cntxts[idx];
2103 else
2104 context = globalcntx;
2105 newsize = (int)(strlen(context) + strlen(cntxformat));
2107 if (bufsize < newsize)
2109 buf = (char *)vim_realloc(buf, newsize);
2110 if (buf == NULL)
2111 bufsize = 0;
2112 else
2113 bufsize = newsize;
2115 if (buf != NULL)
2117 (void)sprintf(buf, cntxformat, context);
2119 /* print the context only if it fits on the same line */
2120 if (msg_col + (int)strlen(buf) >= (int)Columns)
2121 msg_putchar('\n');
2122 msg_advance(12);
2123 MSG_PUTS_LONG(buf);
2124 msg_putchar('\n');
2126 if (extra != NULL)
2128 msg_advance(13);
2129 MSG_PUTS_LONG(extra);
2132 vim_free(tbuf); /* only after printing extra due to strtok use */
2134 if (msg_col)
2135 msg_putchar('\n');
2137 ui_breakcheck();
2138 if (got_int)
2140 got_int = FALSE; /* don't print any more matches */
2141 break;
2144 num++;
2145 } /* for all matches */
2147 vim_free(buf);
2148 } /* cs_print_tags_priv */
2152 * PRIVATE: cs_read_prompt
2154 * read a cscope prompt (basically, skip over the ">> ")
2156 static int
2157 cs_read_prompt(i)
2158 int i;
2160 int ch;
2161 char *buf = NULL; /* buffer for possible error message from cscope */
2162 int bufpos = 0;
2163 char *cs_emsg;
2164 int maxlen;
2165 static char *eprompt = "Press the RETURN key to continue:";
2166 int epromptlen = (int)strlen(eprompt);
2167 int n;
2169 cs_emsg = _("E609: Cscope error: %s");
2170 /* compute maximum allowed len for Cscope error message */
2171 maxlen = (int)(IOSIZE - strlen(cs_emsg));
2173 for (;;)
2175 while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
2176 /* if there is room and char is printable */
2177 if (bufpos < maxlen - 1 && vim_isprintc(ch))
2179 if (buf == NULL) /* lazy buffer allocation */
2180 buf = (char *)alloc(maxlen);
2181 if (buf != NULL)
2183 /* append character to the message */
2184 buf[bufpos++] = ch;
2185 buf[bufpos] = NUL;
2186 if (bufpos >= epromptlen
2187 && strcmp(&buf[bufpos - epromptlen], eprompt) == 0)
2189 /* remove eprompt from buf */
2190 buf[bufpos - epromptlen] = NUL;
2192 /* print message to user */
2193 (void)EMSG2(cs_emsg, buf);
2195 /* send RETURN to cscope */
2196 (void)putc('\n', csinfo[i].to_fp);
2197 (void)fflush(csinfo[i].to_fp);
2199 /* clear buf */
2200 bufpos = 0;
2201 buf[bufpos] = NUL;
2206 for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n)
2208 if (n > 0)
2209 ch = getc(csinfo[i].fr_fp);
2210 if (ch == EOF)
2212 PERROR("cs_read_prompt EOF");
2213 if (buf != NULL && buf[0] != NUL)
2214 (void)EMSG2(cs_emsg, buf);
2215 else if (p_csverbose)
2216 cs_reading_emsg(i); /* don't have additional information */
2217 cs_release_csp(i, TRUE);
2218 vim_free(buf);
2219 return CSCOPE_FAILURE;
2222 if (ch != CSCOPE_PROMPT[n])
2224 ch = EOF;
2225 break;
2229 if (ch == EOF)
2230 continue; /* didn't find the prompt */
2231 break; /* did find the prompt */
2234 vim_free(buf);
2235 return CSCOPE_SUCCESS;
2238 #if defined(UNIX) && defined(SIGALRM)
2240 * Used to catch and ignore SIGALRM below.
2242 static RETSIGTYPE
2243 sig_handler SIGDEFARG(sigarg)
2245 /* do nothing */
2246 SIGRETURN;
2248 #endif
2251 * PRIVATE: cs_release_csp
2253 * Does the actual free'ing for the cs ptr with an optional flag of whether
2254 * or not to free the filename. Called by cs_kill and cs_reset.
2256 static void
2257 cs_release_csp(i, freefnpp)
2258 int i;
2259 int freefnpp;
2262 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2264 if (csinfo[i].to_fp != NULL)
2266 (void)fputs("q\n", csinfo[i].to_fp);
2267 (void)fflush(csinfo[i].to_fp);
2269 #if defined(UNIX)
2271 int waitpid_errno;
2272 int pstat;
2273 pid_t pid;
2275 # if defined(HAVE_SIGACTION)
2276 struct sigaction sa, old;
2278 /* Use sigaction() to limit the waiting time to two seconds. */
2279 sigemptyset(&sa.sa_mask);
2280 sa.sa_handler = sig_handler;
2281 # ifdef SA_NODEFER
2282 sa.sa_flags = SA_NODEFER;
2283 # else
2284 sa.sa_flags = 0;
2285 # endif
2286 sigaction(SIGALRM, &sa, &old);
2287 alarm(2); /* 2 sec timeout */
2289 /* Block until cscope exits or until timer expires */
2290 pid = waitpid(csinfo[i].pid, &pstat, 0);
2291 waitpid_errno = errno;
2293 /* cancel pending alarm if still there and restore signal */
2294 alarm(0);
2295 sigaction(SIGALRM, &old, NULL);
2296 # else
2297 int waited;
2299 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2300 * to give cscope a chance to exit quickly. */
2301 sleep(0);
2302 for (waited = 0; waited < 40; ++waited)
2304 pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
2305 waitpid_errno = errno;
2306 if (pid != 0)
2307 break; /* break unless the process is still running */
2308 mch_delay(50L, FALSE); /* sleep 50 ms */
2310 # endif
2312 * If the cscope process is still running: kill it.
2313 * Safety check: If the PID would be zero here, the entire X session
2314 * would be killed. -1 and 1 are dangerous as well.
2316 if (pid < 0 && csinfo[i].pid > 1)
2318 # ifdef ECHILD
2319 int alive = TRUE;
2321 if (waitpid_errno == ECHILD)
2324 * When using 'vim -g', vim is forked and cscope process is
2325 * no longer a child process but a sibling. So waitpid()
2326 * fails with errno being ECHILD (No child processes).
2327 * Don't send SIGKILL to cscope immediately but wait
2328 * (polling) for it to exit normally as result of sending
2329 * the "q" command, hence giving it a chance to clean up
2330 * its temporary files.
2332 int waited;
2334 sleep(0);
2335 for (waited = 0; waited < 40; ++waited)
2337 /* Check whether cscope process is still alive */
2338 if (kill(csinfo[i].pid, 0) != 0)
2340 alive = FALSE; /* cscope process no longer exists */
2341 break;
2343 mch_delay(50L, FALSE); /* sleep 50ms */
2346 if (alive)
2347 # endif
2349 kill(csinfo[i].pid, SIGKILL);
2350 (void)waitpid(csinfo[i].pid, &pstat, 0);
2354 #else /* !UNIX */
2355 if (csinfo[i].hProc != NULL)
2357 /* Give cscope a chance to exit normally */
2358 if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT)
2359 TerminateProcess(csinfo[i].hProc, 0);
2360 CloseHandle(csinfo[i].hProc);
2362 #endif
2364 if (csinfo[i].fr_fp != NULL)
2365 (void)fclose(csinfo[i].fr_fp);
2366 if (csinfo[i].to_fp != NULL)
2367 (void)fclose(csinfo[i].to_fp);
2369 if (freefnpp)
2371 vim_free(csinfo[i].fname);
2372 vim_free(csinfo[i].ppath);
2373 vim_free(csinfo[i].flags);
2376 clear_csinfo(i);
2377 } /* cs_release_csp */
2381 * PRIVATE: cs_reset
2383 * calls cs_kill on all cscope connections then reinits
2385 static int
2386 cs_reset(eap)
2387 exarg_T *eap UNUSED;
2389 char **dblist = NULL, **pplist = NULL, **fllist = NULL;
2390 int i;
2391 char buf[20]; /* for sprintf " (#%d)" */
2393 if (csinfo_size == 0)
2394 return CSCOPE_SUCCESS;
2396 /* malloc our db and ppath list */
2397 dblist = (char **)alloc(csinfo_size * sizeof(char *));
2398 pplist = (char **)alloc(csinfo_size * sizeof(char *));
2399 fllist = (char **)alloc(csinfo_size * sizeof(char *));
2400 if (dblist == NULL || pplist == NULL || fllist == NULL)
2402 vim_free(dblist);
2403 vim_free(pplist);
2404 vim_free(fllist);
2405 return CSCOPE_FAILURE;
2408 for (i = 0; i < csinfo_size; i++)
2410 dblist[i] = csinfo[i].fname;
2411 pplist[i] = csinfo[i].ppath;
2412 fllist[i] = csinfo[i].flags;
2413 if (csinfo[i].fname != NULL)
2414 cs_release_csp(i, FALSE);
2417 /* rebuild the cscope connection list */
2418 for (i = 0; i < csinfo_size; i++)
2420 if (dblist[i] != NULL)
2422 cs_add_common(dblist[i], pplist[i], fllist[i]);
2423 if (p_csverbose)
2425 /* don't use smsg_attr() because we want to display the
2426 * connection number in the same line as
2427 * "Added cscope database..."
2429 sprintf(buf, " (#%d)", i);
2430 MSG_PUTS_ATTR(buf, hl_attr(HLF_R));
2433 vim_free(dblist[i]);
2434 vim_free(pplist[i]);
2435 vim_free(fllist[i]);
2437 vim_free(dblist);
2438 vim_free(pplist);
2439 vim_free(fllist);
2441 if (p_csverbose)
2442 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST);
2443 return CSCOPE_SUCCESS;
2444 } /* cs_reset */
2448 * PRIVATE: cs_resolve_file
2450 * construct the full pathname to a file found in the cscope database.
2451 * (Prepends ppath, if there is one and if it's not already prepended,
2452 * otherwise just uses the name found.)
2454 * we need to prepend the prefix because on some cscope's (e.g., the one that
2455 * ships with Solaris 2.6), the output never has the prefix prepended.
2456 * contrast this with my development system (Digital Unix), which does.
2458 static char *
2459 cs_resolve_file(i, name)
2460 int i;
2461 char *name;
2463 char *fullname;
2464 int len;
2467 * ppath is freed when we destroy the cscope connection.
2468 * fullname is freed after cs_make_vim_style_matches, after it's been
2469 * copied into the tag buffer used by vim
2471 len = (int)(strlen(name) + 2);
2472 if (csinfo[i].ppath != NULL)
2473 len += (int)strlen(csinfo[i].ppath);
2475 if ((fullname = (char *)alloc(len)) == NULL)
2476 return NULL;
2479 * note/example: this won't work if the cscope output already starts
2480 * "../.." and the prefix path is also "../..". if something like this
2481 * happens, you are screwed up and need to fix how you're using cscope.
2483 if (csinfo[i].ppath != NULL &&
2484 (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) &&
2485 (name[0] != '/')
2486 #ifdef WIN32
2487 && name[0] != '\\' && name[1] != ':'
2488 #endif
2490 (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
2491 else
2492 (void)sprintf(fullname, "%s", name);
2494 return fullname;
2495 } /* cs_resolve_file */
2499 * PRIVATE: cs_show
2501 * show all cscope connections
2503 static int
2504 cs_show(eap)
2505 exarg_T *eap UNUSED;
2507 short i;
2508 if (cs_cnt_connections() == 0)
2509 MSG_PUTS(_("no cscope connections\n"));
2510 else
2512 MSG_PUTS_ATTR(
2513 _(" # pid database name prepend path\n"),
2514 hl_attr(HLF_T));
2515 for (i = 0; i < csinfo_size; i++)
2517 if (csinfo[i].fname == NULL)
2518 continue;
2520 if (csinfo[i].ppath != NULL)
2521 (void)smsg((char_u *)"%2d %-5ld %-34s %-32s",
2522 i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath);
2523 else
2524 (void)smsg((char_u *)"%2d %-5ld %-34s <none>",
2525 i, (long)csinfo[i].pid, csinfo[i].fname);
2529 wait_return(TRUE);
2530 return CSCOPE_SUCCESS;
2531 } /* cs_show */
2535 * PUBLIC: cs_end
2537 * Only called when VIM exits to quit any cscope sessions.
2539 void
2540 cs_end()
2542 int i;
2544 for (i = 0; i < csinfo_size; i++)
2545 cs_release_csp(i, TRUE);
2546 vim_free(csinfo);
2547 csinfo_size = 0;
2550 #endif /* FEAT_CSCOPE */
2552 /* the end */