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.
14 #if defined(FEAT_CSCOPE) || defined(PROTO)
19 #include <sys/types.h>
22 # include <sys/wait.h>
24 /* not UNIX, must be WIN32 */
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
));
42 static void cs_file_results
__ARGS((FILE *, int *));
44 static void cs_fill_results
__ARGS((char *, int , int *, char ***,
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 *,
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 *,
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
72 static cscmd_T cs_cmds
[] =
75 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
77 N_("Query for a pattern"), FIND_USAGE
, 1 },
79 N_("Show this message"), "help", 0 },
81 N_("Kill a connection"), "kill #", 0 },
83 N_("Reinit all connections"), "reset", 0 },
85 N_("Show connections"), "show", 0 },
93 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds
[(int)x
].usage
);
97 * PRIVATE: do_cscope_general
99 * find the command, print help if invalid, and the then call the
100 * corresponding command function,
101 * called from do_cscope and do_scscope
104 do_cscope_general(eap
, make_split
)
106 int make_split
; /* whether to split window */
111 if ((cmdp
= cs_lookup_cmd(eap
)) == NULL
)
122 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
125 postponed_split
= -1;
126 postponed_split_flags
= cmdmod
.split
;
127 postponed_split_tab
= cmdmod
.tab
;
134 postponed_split_flags
= 0;
135 postponed_split_tab
= 0;
146 do_cscope_general(eap
, FALSE
);
152 * same as do_cscope, but splits window, too.
158 do_cscope_general(eap
, TRUE
);
173 if (*eap
->arg
== NUL
)
175 (void)EMSG(_("E562: Usage: cstag <ident>"));
182 if (cs_check_for_connections())
184 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
192 if (cs_check_for_tags())
193 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
196 else if (cs_check_for_tags())
198 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
202 if (cs_check_for_tags())
204 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
210 if (cs_check_for_connections())
212 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
,
219 else if (cs_check_for_connections())
221 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
233 (void)EMSG(_("E257: cstag: tag not found"));
234 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
245 * this simulates a vim_fgets(), but for cscope, returns the next line
246 * from the cscope output. should only be called from find_tags()
248 * returns TRUE if eof, FALSE otherwise
257 if ((p
= cs_manage_matches(NULL
, NULL
, -1, Get
)) == NULL
)
259 vim_strncpy(buf
, (char_u
*)p
, size
- 1);
266 * PUBLIC: cs_free_tags
268 * called only from do_tag(), when popping the tag stack
273 cs_manage_matches(NULL
, NULL
, -1, Free
);
278 * PUBLIC: cs_print_tags
280 * called from do_tag()
285 cs_manage_matches(NULL
, NULL
, -1, Print
);
290 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
292 * Checks for the existence of a |cscope| connection. If no
293 * parameters are specified, then the function returns:
295 * 0, if cscope was not available (not compiled in), or if there
296 * are no cscope connections; or
297 * 1, if there is at least one cscope connection.
299 * If parameters are specified, then the value of {num}
300 * determines how existence of a cscope connection is checked:
302 * {num} Description of existence check
303 * ----- ------------------------------
304 * 0 Same as no parameters (e.g., "cscope_connection()").
305 * 1 Ignore {prepend}, and use partial string matches for
307 * 2 Ignore {prepend}, and use exact string matches for
309 * 3 Use {prepend}, use partial string matches for both
310 * {dbpath} and {prepend}.
311 * 4 Use {prepend}, use exact string matches for both
312 * {dbpath} and {prepend}.
314 * Note: All string comparisons are case sensitive!
316 #if defined(FEAT_EVAL) || defined(PROTO)
318 cs_connection(num
, dbpath
, ppath
)
325 if (num
< 0 || num
> 4 || (num
> 0 && !dbpath
))
328 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
330 if (!csinfo
[i
].fname
)
339 if (strstr(csinfo
[i
].fname
, (char *)dbpath
))
343 if (strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
347 if (strstr(csinfo
[i
].fname
, (char *)dbpath
)
348 && ((!ppath
&& !csinfo
[i
].ppath
)
351 && strstr(csinfo
[i
].ppath
, (char *)ppath
))))
355 if ((strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
356 && ((!ppath
&& !csinfo
[i
].ppath
)
359 && (strcmp(csinfo
[i
].ppath
, (char *)ppath
) == 0))))
366 } /* cs_connection */
372 ****************************************************************************/
377 * add cscope database or a directory name (to look for cscope.out)
378 * to the cscope connection list
387 char *fname
, *ppath
, *flags
= NULL
;
389 if ((fname
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
392 return CSCOPE_FAILURE
;
394 if ((ppath
= strtok((char *)NULL
, (const char *)" ")) != NULL
)
395 flags
= strtok((char *)NULL
, (const char *)" ");
397 return cs_add_common(fname
, ppath
, flags
);
404 char *stat_emsg
= _("E563: stat(%s) error: %d");
405 char *buf
= (char *)alloc((unsigned)strlen(stat_emsg
) + MAXPATHL
+ 10);
409 (void)sprintf(buf
, stat_emsg
, fname
, errno
);
414 (void)EMSG(_("E563: stat error"));
419 * PRIVATE: cs_add_common
421 * the common routine to add a new cscope connection. called by
422 * cs_add() and cs_reset(). i really don't like to do this, but this
423 * routine uses a number of goto statements.
426 cs_add_common(arg1
, arg2
, flags
)
427 char *arg1
; /* filename - may contain environment variables */
428 char *arg2
; /* prepend path - may contain environment variables */
438 /* get the filename (arg1), expand it, and try to stat it */
439 if ((fname
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
442 expand_env((char_u
*)arg1
, (char_u
*)fname
, MAXPATHL
);
443 ret
= stat(fname
, &statbuf
);
452 /* get the prepend path (arg2), expand it, and try to stat it */
455 struct stat statbuf2
;
457 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
460 expand_env((char_u
*)arg2
, (char_u
*)ppath
, MAXPATHL
);
461 ret
= stat(ppath
, &statbuf2
);
466 /* if filename is a directory, append the cscope database name to it */
467 if ((statbuf
.st_mode
& S_IFMT
) == S_IFDIR
)
469 fname2
= (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE
) + strlen(fname
) + 2));
473 while (fname
[strlen(fname
)-1] == '/'
475 || fname
[strlen(fname
)-1] == '\\'
479 fname
[strlen(fname
)-1] = '\0';
480 if (strlen(fname
) == 0)
483 if (fname
[0] == '\0')
484 (void)sprintf(fname2
, "/%s", CSCOPE_DBFILE
);
486 (void)sprintf(fname2
, "%s/%s", fname
, CSCOPE_DBFILE
);
488 ret
= stat(fname2
, &statbuf
);
492 cs_stat_emsg(fname2
);
496 i
= cs_insert_filelist(fname2
, ppath
, flags
, &statbuf
);
499 else if (S_ISREG(statbuf
.st_mode
) || S_ISLNK(statbuf
.st_mode
))
501 /* WIN32 - substitute define S_ISREG from os_unix.h */
502 else if (((statbuf
.st_mode
) & S_IFMT
) == S_IFREG
)
505 i
= cs_insert_filelist(fname
, ppath
, flags
, &statbuf
);
511 _("E564: %s is not a directory or a valid cscope database"),
518 if (cs_create_connection(i
) == CSCOPE_FAILURE
519 || cs_read_prompt(i
) == CSCOPE_FAILURE
)
521 cs_release_csp(i
, TRUE
);
528 (void)smsg_attr(hl_attr(HLF_R
),
529 (char_u
*)_("Added cscope database %s"),
537 return CSCOPE_SUCCESS
;
543 return CSCOPE_FAILURE
;
544 } /* cs_add_common */
548 cs_check_for_connections()
550 return (cs_cnt_connections() > 0);
551 } /* cs_check_for_connections */
557 return (p_tags
[0] != NUL
&& curbuf
->b_p_tags
!= NULL
);
558 } /* cs_check_for_tags */
562 * PRIVATE: cs_cnt_connections
564 * count the number of cscope connections
572 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
574 if (csinfo
[i
].fname
!= NULL
)
578 } /* cs_cnt_connections */
582 int idx
; /* connection index */
584 EMSGN(_("E262: error reading cscope connection %ld"), idx
);
587 #define CSREAD_BUFSIZE 2048
589 * PRIVATE: cs_cnt_matches
591 * count the number of matches for a given cscope connection.
601 buf
= (char *)alloc(CSREAD_BUFSIZE
);
606 if (!fgets(buf
, CSREAD_BUFSIZE
, csinfo
[idx
].fr_fp
))
608 if (feof(csinfo
[idx
].fr_fp
))
611 cs_reading_emsg(idx
);
618 * If the database is out of date, or there's some other problem,
619 * cscope will output error messages before the number-of-lines output.
620 * Display/discard any output that doesn't match what we want.
621 * Accept "\S*cscope: X lines", also matches "mlcscope".
623 if ((stok
= strtok(buf
, (const char *)" ")) == NULL
)
625 if (strstr((const char *)stok
, "cscope:") == NULL
)
628 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
637 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
639 if (strncmp((const char *)stok
, "lines", 5))
647 } /* cs_cnt_matches */
651 * PRIVATE: cs_create_cmd
653 * Creates the actual cscope command query from what the user entered.
656 cs_create_cmd(csoption
, pattern
)
665 case '0' : case 's' :
668 case '1' : case 'g' :
671 case '2' : case 'd' :
674 case '3' : case 'c' :
677 case '4' : case 't' :
680 case '6' : case 'e' :
683 case '7' : case 'f' :
686 case '8' : case 'i' :
690 (void)EMSG(_("E561: unknown cscope search type"));
695 if ((cmd
= (char *)alloc((unsigned)(strlen(pattern
) + 2))) == NULL
)
698 (void)sprintf(cmd
, "%d%s", search
, pattern
);
701 } /* cs_create_cmd */
705 * PRIVATE: cs_create_connection
707 * This piece of code was taken/adapted from nvi. do we need to add
708 * the BSD license notice?
711 cs_create_connection(i
)
715 int to_cs
[2], from_cs
[2];
718 char *prog
, *cmd
, *ppath
= NULL
;
721 SECURITY_ATTRIBUTES sa
;
722 PROCESS_INFORMATION pi
;
724 BOOL pipe_stdin
= FALSE
, pipe_stdout
= FALSE
;
725 HANDLE stdin_rd
, stdout_rd
;
726 HANDLE stdout_wr
, stdin_wr
;
729 # define OPEN_OH_ARGTYPE long
731 # if (_MSC_VER >= 1300)
732 # define OPEN_OH_ARGTYPE intptr_t
734 # define OPEN_OH_ARGTYPE long
741 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
742 * from_cs[0] and writes to to_cs[1].
744 to_cs
[0] = to_cs
[1] = from_cs
[0] = from_cs
[1] = -1;
745 if (pipe(to_cs
) < 0 || pipe(from_cs
) < 0)
747 (void)EMSG(_("E566: Could not create cscope pipes"));
750 (void)close(to_cs
[0]);
752 (void)close(to_cs
[1]);
753 if (from_cs
[0] != -1)
754 (void)close(from_cs
[0]);
755 if (from_cs
[1] != -1)
756 (void)close(from_cs
[1]);
757 return CSCOPE_FAILURE
;
760 switch (csinfo
[i
].pid
= fork())
763 (void)EMSG(_("E622: Could not fork for cscope"));
765 case 0: /* child: run cscope. */
766 if (dup2(to_cs
[0], STDIN_FILENO
) == -1)
767 PERROR("cs_create_connection 1");
768 if (dup2(from_cs
[1], STDOUT_FILENO
) == -1)
769 PERROR("cs_create_connection 2");
770 if (dup2(from_cs
[1], STDERR_FILENO
) == -1)
771 PERROR("cs_create_connection 3");
774 (void)close(to_cs
[1]);
775 (void)close(from_cs
[0]);
778 /* Create pipes to communicate with cscope */
779 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
780 sa
.bInheritHandle
= TRUE
;
781 sa
.lpSecurityDescriptor
= NULL
;
783 if (!(pipe_stdin
= CreatePipe(&stdin_rd
, &stdin_wr
, &sa
, 0))
784 || !(pipe_stdout
= CreatePipe(&stdout_rd
, &stdout_wr
, &sa
, 0)))
786 (void)EMSG(_("E566: Could not create cscope pipes"));
790 CloseHandle(stdin_rd
);
791 CloseHandle(stdin_wr
);
795 CloseHandle(stdout_rd
);
796 CloseHandle(stdout_wr
);
798 return CSCOPE_FAILURE
;
801 /* expand the cscope exec for env var's */
802 if ((prog
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
805 return CSCOPE_FAILURE
;
811 expand_env((char_u
*)p_csprg
, (char_u
*)prog
, MAXPATHL
);
813 /* alloc space to hold the cscope command */
814 len
= (int)(strlen(prog
) + strlen(csinfo
[i
].fname
) + 32);
817 /* expand the prepend path for env var's */
818 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
822 return CSCOPE_FAILURE
;
828 expand_env((char_u
*)csinfo
[i
].ppath
, (char_u
*)ppath
, MAXPATHL
);
830 len
+= (int)strlen(ppath
);
834 len
+= (int)strlen(csinfo
[i
].flags
);
836 if ((cmd
= (char *)alloc(len
)) == NULL
)
841 return CSCOPE_FAILURE
;
848 /* run the cscope command; is there execl for non-unix systems? */
850 (void)sprintf(cmd
, "exec %s -dl -f %s", prog
, csinfo
[i
].fname
);
853 (void)sprintf(cmd
, "%s -dl -f %s", prog
, csinfo
[i
].fname
);
855 if (csinfo
[i
].ppath
!= NULL
)
857 (void)strcat(cmd
, " -P");
858 (void)strcat(cmd
, csinfo
[i
].ppath
);
860 if (csinfo
[i
].flags
!= NULL
)
862 (void)strcat(cmd
, " ");
863 (void)strcat(cmd
, csinfo
[i
].flags
);
866 /* on Win32 we still need prog */
872 if (execl("/bin/sh", "sh", "-c", cmd
, NULL
) == -1)
873 PERROR(_("cs_create_connection exec failed"));
877 default: /* parent. */
879 * Save the file descriptors for later duplication, and
882 if ((csinfo
[i
].to_fp
= fdopen(to_cs
[1], "w")) == NULL
)
883 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
884 if ((csinfo
[i
].fr_fp
= fdopen(from_cs
[0], "r")) == NULL
)
885 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
888 (void)close(to_cs
[0]);
889 (void)close(from_cs
[1]);
896 /* Create a new process to run cscope and use pipes to talk with it */
898 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
899 si
.wShowWindow
= SW_HIDE
; /* Hide child application window */
900 si
.hStdOutput
= stdout_wr
;
901 si
.hStdError
= stdout_wr
;
902 si
.hStdInput
= stdin_rd
;
903 created
= CreateProcess(NULL
, cmd
, NULL
, NULL
, TRUE
, CREATE_NEW_CONSOLE
,
904 NULL
, NULL
, &si
, &pi
);
910 PERROR(_("cs_create_connection exec failed"));
911 (void)EMSG(_("E623: Could not spawn cscope process"));
915 csinfo
[i
].pid
= pi
.dwProcessId
;
916 csinfo
[i
].hProc
= pi
.hProcess
;
917 CloseHandle(pi
.hThread
);
919 /* TODO - tidy up after failure to create files on pipe handles. */
920 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdin_wr
,
921 _O_TEXT
|_O_APPEND
)) < 0)
922 || ((csinfo
[i
].to_fp
= _fdopen(fd
, "w")) == NULL
))
923 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
924 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdout_rd
,
925 _O_TEXT
|_O_RDONLY
)) < 0)
926 || ((csinfo
[i
].fr_fp
= _fdopen(fd
, "r")) == NULL
))
927 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
929 /* Close handles for file descriptors inherited by the cscope process */
930 CloseHandle(stdin_rd
);
931 CloseHandle(stdout_wr
);
935 return CSCOPE_SUCCESS
;
936 } /* cs_create_connection */
942 * query cscope using command line interface. parse the output and use tselect
943 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
945 * returns TRUE if we jump to a tag or abort, FALSE if not.
953 if (cs_check_for_connections() == FALSE
)
955 (void)EMSG(_("E567: no cscope connections"));
959 if ((opt
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
965 pat
= opt
+ strlen(opt
) + 1;
966 if (pat
>= (char *)eap
->arg
+ eap_arg_len
)
972 return cs_find_common(opt
, pat
, eap
->forceit
, TRUE
,
973 eap
->cmdidx
== CMD_lcscope
);
978 * PRIVATE: cs_find_common
980 * common code for cscope find, shared by cs_find() and do_cstag()
983 cs_find_common(opt
, pat
, forceit
, verbose
, use_ll
)
992 int nummatches
[CSCOPE_MAX_CONNECTIONS
], totmatches
;
998 /* create the actual command to send to cscope */
999 cmd
= cs_create_cmd(opt
, pat
);
1003 /* send query to all open connections, then count the total number
1004 * of matches so we can alloc matchesp all in one swell foop
1006 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1009 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1011 if (csinfo
[i
].fname
== NULL
|| csinfo
[i
].to_fp
== NULL
)
1014 /* send cmd to cscope */
1015 (void)fprintf(csinfo
[i
].to_fp
, "%s\n", cmd
);
1016 (void)fflush(csinfo
[i
].to_fp
);
1018 nummatches
[i
] = cs_cnt_matches(i
);
1020 if (nummatches
[i
] > -1)
1021 totmatches
+= nummatches
[i
];
1023 if (nummatches
[i
] == 0)
1024 (void)cs_read_prompt(i
);
1028 if (totmatches
== 0)
1030 char *nf
= _("E259: no matches found for cscope query %s of %s");
1036 buf
= (char *)alloc((unsigned)(strlen(opt
) + strlen(pat
) + strlen(nf
)));
1041 sprintf(buf
, nf
, opt
, pat
);
1048 #ifdef FEAT_QUICKFIX
1049 /* get cmd letter */
1080 qfpos
= (char *)vim_strchr(p_csqf
, cmdletter
);
1084 /* next symbol must be + or - */
1085 if (strchr(CSQF_FLAGS
, *qfpos
) == NULL
)
1087 char *nf
= _("E469: invalid cscopequickfix flag %c for %c");
1088 char *buf
= (char *)alloc((unsigned)strlen(nf
));
1090 /* strlen will be enough because we use chars */
1093 sprintf(buf
, nf
, *qfpos
, *(qfpos
-1));
1100 if (qfpos
!= NULL
&& *qfpos
!= '0' && totmatches
> 0)
1102 /* fill error list */
1104 char_u
*tmp
= vim_tempname('c');
1105 qf_info_T
*qi
= NULL
;
1108 f
= mch_fopen((char *)tmp
, "w");
1110 EMSG2(_(e_notopen
), tmp
);
1113 cs_file_results(f
, nummatches
);
1115 if (use_ll
) /* Use location list */
1117 /* '-' starts a new error list */
1118 if (qf_init(wp
, tmp
, (char_u
*)"%f%*\\t%l%*\\t%m",
1121 # ifdef FEAT_WINDOWS
1122 if (postponed_split
!= 0)
1124 win_split(postponed_split
> 0 ? postponed_split
: 0,
1125 postponed_split_flags
);
1126 # ifdef FEAT_SCROLLBIND
1127 curwin
->w_p_scb
= FALSE
;
1129 postponed_split
= 0;
1134 * In the location list window, use the displayed location
1135 * list. Otherwise, use the location list for the window.
1137 qi
= (bt_quickfix(wp
->w_buffer
) && wp
->w_llist_ref
!= NULL
)
1138 ? wp
->w_llist_ref
: wp
->w_llist
;
1139 qf_jump(qi
, 0, 0, forceit
);
1147 #endif /* FEAT_QUICKFIX */
1149 char **matches
= NULL
, **contexts
= NULL
;
1153 cs_fill_results((char *)pat
, totmatches
, nummatches
, &matches
,
1154 &contexts
, &matched
);
1155 if (matches
== NULL
)
1158 (void)cs_manage_matches(matches
, contexts
, matched
, Store
);
1160 return do_tag((char_u
*)pat
, DT_CSCOPE
, 0, forceit
, verbose
);
1163 } /* cs_find_common */
1175 cscmd_T
*cmdp
= cs_cmds
;
1177 (void)MSG_PUTS(_("cscope commands:\n"));
1178 while (cmdp
->name
!= NULL
)
1180 (void)smsg((char_u
*)_("%-5s: %-30s (Usage: %s)"),
1181 cmdp
->name
, _(cmdp
->help
), cmdp
->usage
);
1182 if (strcmp(cmdp
->name
, "find") == 0)
1183 MSG_PUTS(FIND_HELP
);
1195 * initialize cscope structure if not already
1201 static int init_already
= FALSE
;
1206 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1209 init_already
= TRUE
;
1216 csinfo
[i
].fname
= NULL
;
1217 csinfo
[i
].ppath
= NULL
;
1218 csinfo
[i
].flags
= NULL
;
1220 csinfo
[i
].st_dev
= (dev_t
)0;
1221 csinfo
[i
].st_ino
= (ino_t
)0;
1223 csinfo
[i
].nVolume
= 0;
1224 csinfo
[i
].nIndexHigh
= 0;
1225 csinfo
[i
].nIndexLow
= 0;
1228 csinfo
[i
].fr_fp
= NULL
;
1229 csinfo
[i
].to_fp
= NULL
;
1231 csinfo
[i
].hProc
= NULL
;
1236 static char *GetWin32Error
__ARGS((void));
1242 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
1243 NULL
, GetLastError(), 0, (LPSTR
)&msg
, 0, NULL
);
1246 /* remove trailing \r\n */
1247 char *pcrlf
= strstr(msg
, "\r\n");
1256 * PRIVATE: cs_insert_filelist
1258 * insert a new cscope database filename into the filelist
1262 cs_insert_filelist(fname
, ppath
, flags
, sb
)
1271 BY_HANDLE_FILE_INFORMATION bhfi
;
1273 vim_memset(&bhfi
, 0, sizeof(bhfi
));
1274 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1275 if (!mch_windows95())
1277 hFile
= CreateFile(fname
, FILE_READ_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
1278 FILE_ATTRIBUTE_NORMAL
, NULL
);
1279 if (hFile
== INVALID_HANDLE_VALUE
)
1283 char *cant_msg
= _("E625: cannot open cscope database: %s");
1284 char *winmsg
= GetWin32Error();
1288 (void)EMSG2(cant_msg
, winmsg
);
1292 /* subst filename if can't get error text */
1293 (void)EMSG2(cant_msg
, fname
);
1297 if (!GetFileInformationByHandle(hFile
, &bhfi
))
1301 (void)EMSG(_("E626: cannot get cscope database information"));
1308 i
= -1; /* can be set to the index of an empty item in csinfo */
1309 for (j
= 0; j
< CSCOPE_MAX_CONNECTIONS
; j
++)
1311 if (csinfo
[j
].fname
!= NULL
1313 && csinfo
[j
].st_dev
== sb
->st_dev
&& csinfo
[j
].st_ino
== sb
->st_ino
1315 /* compare pathnames first */
1316 && ((fullpathcmp(csinfo
[j
].fname
, fname
, FALSE
) & FPC_SAME
)
1317 /* if not Windows 9x, test index file attributes too */
1318 || (!mch_windows95()
1319 && csinfo
[j
].nVolume
== bhfi
.dwVolumeSerialNumber
1320 && csinfo
[j
].nIndexHigh
== bhfi
.nFileIndexHigh
1321 && csinfo
[j
].nIndexLow
== bhfi
.nFileIndexLow
))
1326 (void)EMSG(_("E568: duplicate cscope database not added"));
1330 if (csinfo
[j
].fname
== NULL
&& i
== -1)
1331 i
= j
; /* remember first empty entry */
1337 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1341 if ((csinfo
[i
].fname
= (char *)alloc((unsigned)strlen(fname
)+1)) == NULL
)
1344 (void)strcpy(csinfo
[i
].fname
, (const char *)fname
);
1348 if ((csinfo
[i
].ppath
= (char *)alloc((unsigned)strlen(ppath
) + 1)) == NULL
)
1350 vim_free(csinfo
[i
].fname
);
1351 csinfo
[i
].fname
= NULL
;
1354 (void)strcpy(csinfo
[i
].ppath
, (const char *)ppath
);
1356 csinfo
[i
].ppath
= NULL
;
1360 if ((csinfo
[i
].flags
= (char *)alloc((unsigned)strlen(flags
) + 1)) == NULL
)
1362 vim_free(csinfo
[i
].fname
);
1363 vim_free(csinfo
[i
].ppath
);
1364 csinfo
[i
].fname
= NULL
;
1365 csinfo
[i
].ppath
= NULL
;
1368 (void)strcpy(csinfo
[i
].flags
, (const char *)flags
);
1370 csinfo
[i
].flags
= NULL
;
1373 csinfo
[i
].st_dev
= sb
->st_dev
;
1374 csinfo
[i
].st_ino
= sb
->st_ino
;
1377 csinfo
[i
].nVolume
= bhfi
.dwVolumeSerialNumber
;
1378 csinfo
[i
].nIndexLow
= bhfi
.nFileIndexLow
;
1379 csinfo
[i
].nIndexHigh
= bhfi
.nFileIndexHigh
;
1382 } /* cs_insert_filelist */
1386 * PRIVATE: cs_lookup_cmd
1388 * find cscope command in command table
1398 if (eap
->arg
== NULL
)
1401 /* Store length of eap->arg before it gets modified by strtok(). */
1402 eap_arg_len
= (int)STRLEN(eap
->arg
);
1404 if ((stok
= strtok((char *)(eap
->arg
), (const char *)" ")) == NULL
)
1408 for (cmdp
= cs_cmds
; cmdp
->name
!= NULL
; ++cmdp
)
1410 if (strncmp((const char *)(stok
), cmdp
->name
, len
) == 0)
1414 } /* cs_lookup_cmd */
1430 if ((stok
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1433 return CSCOPE_FAILURE
;
1436 /* only single digit positive and negative integers are allowed */
1437 if ((strlen(stok
) < 2 && VIM_ISDIGIT((int)(stok
[0])))
1438 || (strlen(stok
) < 3 && stok
[0] == '-'
1439 && VIM_ISDIGIT((int)(stok
[1]))))
1443 /* It must be part of a name. We will try to find a match
1444 * within all the names in the csinfo data structure
1446 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1448 if (csinfo
[i
].fname
!= NULL
&& strstr(csinfo
[i
].fname
, stok
))
1453 if ((i
>= CSCOPE_MAX_CONNECTIONS
|| i
< -1 || csinfo
[i
].fname
== NULL
)
1457 (void)EMSG2(_("E261: cscope connection %s not found"), stok
);
1463 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1465 if (csinfo
[i
].fname
)
1466 cs_kill_execute(i
, csinfo
[i
].fname
);
1470 cs_kill_execute(i
, stok
);
1478 * PRIVATE: cs_kill_execute
1480 * Actually kills a specific cscope connection.
1483 cs_kill_execute(i
, cname
)
1484 int i
; /* cscope table index */
1485 char *cname
; /* cscope database name */
1490 (void)smsg_attr(hl_attr(HLF_R
) | MSG_HIST
,
1491 (char_u
*)_("cscope connection %s closed"), cname
);
1493 cs_release_csp(i
, TRUE
);
1498 * PRIVATE: cs_make_vim_style_matches
1500 * convert the cscope output into into a ctags style entry (as might be found
1501 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1502 * the type of the tag you are looking for. for example, in Darren Hiebert's
1503 * ctags (the one that comes with vim), #define's use a line number to find the
1504 * tag in a file while function definitions use a regexp search pattern.
1506 * i'm going to always use the line number because cscope does something
1507 * quirky (and probably other things i don't know about):
1509 * if you have "# define" in your source file, which is
1510 * perfectly legal, cscope thinks you have "#define". this
1511 * will result in a failed regexp search. :(
1513 * besides, even if this particular case didn't happen, the search pattern
1514 * would still have to be modified to escape all the special regular expression
1515 * characters to comply with ctags formatting.
1518 cs_make_vim_style_matches(fname
, slno
, search
, tagstr
)
1524 /* vim style is ctags:
1526 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1528 * but as mentioned above, we'll always use the line number and
1529 * put the search pattern (if one exists) as "extra"
1531 * buf is used as part of vim's method of handling tags, and
1532 * (i think) vim frees it when you pop your tags and get replaced
1533 * by new ones on the tag stack.
1540 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + strlen(search
)+6);
1541 if ((buf
= (char *)alloc(amt
)) == NULL
)
1544 (void)sprintf(buf
, "%s\t%s\t%s;\"\t%s", tagstr
, fname
, slno
, search
);
1548 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + 5);
1549 if ((buf
= (char *)alloc(amt
)) == NULL
)
1552 (void)sprintf(buf
, "%s\t%s\t%s;\"", tagstr
, fname
, slno
);
1556 } /* cs_make_vim_style_matches */
1560 * PRIVATE: cs_manage_matches
1562 * this is kind of hokey, but i don't see an easy way round this..
1564 * Store: keep a ptr to the (malloc'd) memory of matches originally
1565 * generated from cs_find(). the matches are originally lines directly
1566 * from cscope output, but transformed to look like something out of a
1567 * ctags. see cs_make_vim_style_matches for more details.
1569 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1570 * the next line from the cscope output. it basically keeps track of which
1571 * lines have been "used" and returns the next one.
1573 * Free: frees up everything and resets
1575 * Print: prints the tags
1578 cs_manage_matches(matches
, contexts
, totmatches
, cmd
)
1584 static char **mp
= NULL
;
1585 static char **cp
= NULL
;
1586 static int cnt
= -1;
1587 static int next
= -1;
1593 assert(matches
!= NULL
);
1594 assert(totmatches
> 0);
1595 if (mp
!= NULL
|| cp
!= NULL
)
1596 (void)cs_manage_matches(NULL
, NULL
, -1, Free
);
1628 cs_print_tags_priv(mp
, cp
, cnt
);
1630 default: /* should not reach here */
1631 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1636 } /* cs_manage_matches */
1640 * PRIVATE: cs_parse_results
1642 * parse cscope output
1645 cs_parse_results(cnumber
, buf
, bufsize
, context
, linenumber
, search
)
1657 if (fgets(buf
, bufsize
, csinfo
[cnumber
].fr_fp
) == NULL
)
1659 if (feof(csinfo
[cnumber
].fr_fp
))
1662 cs_reading_emsg(cnumber
);
1667 /* If the line's too long for the buffer, discard it. */
1668 if ((p
= strchr(buf
, '\n')) == NULL
)
1670 while ((ch
= getc(csinfo
[cnumber
].fr_fp
)) != EOF
&& ch
!= '\n')
1677 * cscope output is in the following format:
1679 * <filename> <context> <line number> <pattern>
1681 if ((name
= strtok((char *)buf
, (const char *)" ")) == NULL
)
1683 if ((*context
= strtok(NULL
, (const char *)" ")) == NULL
)
1685 if ((*linenumber
= strtok(NULL
, (const char *)" ")) == NULL
)
1687 *search
= *linenumber
+ strlen(*linenumber
) + 1; /* +1 to skip \0 */
1690 * If the file is older than the cscope database, that is,
1691 * the database was built since the file was last modified,
1692 * or there wasn't a search string, use the line number.
1694 if (strcmp(*search
, "<unknown>") == 0)
1697 name
= cs_resolve_file(cnumber
, name
);
1701 #ifdef FEAT_QUICKFIX
1703 * PRIVATE: cs_file_results
1705 * write cscope find results to file
1708 cs_file_results(f
, nummatches_a
)
1714 char *search
, *slno
;
1719 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1723 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1725 if (nummatches_a
[i
] < 1)
1728 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1730 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1731 &slno
, &search
)) == NULL
)
1734 context
= (char *)alloc((unsigned)strlen(cntx
)+5);
1735 if (context
== NULL
)
1738 if (strcmp(cntx
, "<global>")==0)
1739 strcpy(context
, "<<global>>");
1741 sprintf(context
, "<<%s>>", cntx
);
1744 fprintf(f
, "%s\t%s\t%s\n", fullname
, slno
, context
);
1746 fprintf(f
, "%s\t%s\t%s %s\n", fullname
, slno
, context
, search
);
1750 } /* for all matches */
1752 (void)cs_read_prompt(i
);
1754 } /* for all cscope connections */
1760 * PRIVATE: cs_fill_results
1762 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1764 * When there are no matches sets "*matches_p" to NULL.
1767 cs_fill_results(tagstr
, totmatches
, nummatches_a
, matches_p
, cntxts_p
, matched
)
1777 char *search
, *slno
;
1779 char **matches
= NULL
;
1780 char **cntxts
= NULL
;
1784 assert(totmatches
> 0);
1786 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1790 if ((matches
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1792 if ((cntxts
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1795 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1797 if (nummatches_a
[i
] < 1)
1800 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1802 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1803 &slno
, &search
)) == NULL
)
1806 matches
[totsofar
] = cs_make_vim_style_matches(fullname
, slno
,
1811 if (strcmp(cntx
, "<global>") == 0)
1812 cntxts
[totsofar
] = NULL
;
1814 /* note: if vim_strsave returns NULL, then the context
1815 * will be "<global>", which is misleading.
1817 cntxts
[totsofar
] = (char *)vim_strsave((char_u
*)cntx
);
1819 if (matches
[totsofar
] != NULL
)
1822 } /* for all matches */
1824 (void)cs_read_prompt(i
);
1826 } /* for all cscope connections */
1831 /* No matches, free the arrays and return NULL in "*matches_p". */
1837 *matched
= totsofar
;
1838 *matches_p
= matches
;
1842 } /* cs_fill_results */
1845 /* get the requested path components */
1847 cs_pathcomponents(path
)
1856 s
= path
+ strlen(path
) - 1;
1857 for (i
= 0; i
< p_cspc
; ++i
)
1858 while (s
> path
&& *--s
!= '/'
1864 if ((s
> path
&& *s
== '/')
1866 || (s
> path
&& *s
== '\\')
1874 * PRIVATE: cs_print_tags_priv
1876 * called from cs_manage_matches()
1879 cs_print_tags_priv(matches
, cntxts
, num_matches
)
1885 int bufsize
= 0; /* Track available bufsize */
1888 char *fname
, *lno
, *extra
, *tbuf
;
1890 char *globalcntx
= "GLOBAL";
1891 char *cntxformat
= " <<%s>>";
1893 char *cstag_msg
= _("Cscope tag: %s");
1894 char *csfmt_str
= "%4d %6s ";
1896 assert (num_matches
> 0);
1898 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[0]) + 1)) == NULL
)
1901 strcpy(tbuf
, matches
[0]);
1902 ptag
= strtok(tbuf
, "\t");
1904 newsize
= (int)(strlen(cstag_msg
) + strlen(ptag
));
1905 buf
= (char *)alloc(newsize
);
1909 (void)sprintf(buf
, cstag_msg
, ptag
);
1910 MSG_PUTS_ATTR(buf
, hl_attr(HLF_T
));
1915 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T
)); /* strlen is 7 */
1916 msg_advance(msg_col
+ 2);
1917 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T
));
1920 for (i
= 0; i
< num_matches
; i
++)
1924 /* if we really wanted to, we could avoid this malloc and strcpy
1925 * by parsing matches[i] on the fly and placing stuff into buf
1926 * directly, but that's too much of a hassle
1928 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[idx
]) + 1)) == NULL
)
1930 (void)strcpy(tbuf
, matches
[idx
]);
1932 if ((fname
= strtok(tbuf
, (const char *)"\t")) == NULL
)
1934 if ((fname
= strtok(NULL
, (const char *)"\t")) == NULL
)
1936 if ((lno
= strtok(NULL
, (const char *)"\t")) == NULL
)
1938 extra
= strtok(NULL
, (const char *)"\t");
1940 lno
[strlen(lno
)-2] = '\0'; /* ignore ;" at the end */
1942 /* hopefully 'num' (num of matches) will be less than 10^16 */
1943 newsize
= (int)(strlen(csfmt_str
) + 16 + strlen(lno
));
1944 if (bufsize
< newsize
)
1946 buf
= (char *)vim_realloc(buf
, newsize
);
1954 /* csfmt_str = "%4d %6s "; */
1955 (void)sprintf(buf
, csfmt_str
, num
, lno
);
1956 MSG_PUTS_ATTR(buf
, hl_attr(HLF_CM
));
1958 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname
), hl_attr(HLF_CM
));
1960 /* compute the required space for the context */
1961 if (cntxts
[idx
] != NULL
)
1962 context
= cntxts
[idx
];
1964 context
= globalcntx
;
1965 newsize
= (int)(strlen(context
) + strlen(cntxformat
));
1967 if (bufsize
< newsize
)
1969 buf
= (char *)vim_realloc(buf
, newsize
);
1977 (void)sprintf(buf
, cntxformat
, context
);
1979 /* print the context only if it fits on the same line */
1980 if (msg_col
+ (int)strlen(buf
) >= (int)Columns
)
1989 MSG_PUTS_LONG(extra
);
1992 vim_free(tbuf
); /* only after printing extra due to strtok use */
2000 got_int
= FALSE
; /* don't print any more matches */
2005 } /* for all matches */
2008 } /* cs_print_tags_priv */
2012 * PRIVATE: cs_read_prompt
2014 * read a cscope prompt (basically, skip over the ">> ")
2021 char *buf
= NULL
; /* buffer for possible error message from cscope */
2025 static char *eprompt
= "Press the RETURN key to continue:";
2026 int epromptlen
= (int)strlen(eprompt
);
2029 cs_emsg
= _("E609: Cscope error: %s");
2030 /* compute maximum allowed len for Cscope error message */
2031 maxlen
= (int)(IOSIZE
- strlen(cs_emsg
));
2035 while ((ch
= getc(csinfo
[i
].fr_fp
)) != EOF
&& ch
!= CSCOPE_PROMPT
[0])
2036 /* if there is room and char is printable */
2037 if (bufpos
< maxlen
- 1 && vim_isprintc(ch
))
2039 if (buf
== NULL
) /* lazy buffer allocation */
2040 buf
= (char *)alloc(maxlen
);
2043 /* append character to the message */
2046 if (bufpos
>= epromptlen
2047 && strcmp(&buf
[bufpos
- epromptlen
], eprompt
) == 0)
2049 /* remove eprompt from buf */
2050 buf
[bufpos
- epromptlen
] = NUL
;
2052 /* print message to user */
2053 (void)EMSG2(cs_emsg
, buf
);
2055 /* send RETURN to cscope */
2056 (void)putc('\n', csinfo
[i
].to_fp
);
2057 (void)fflush(csinfo
[i
].to_fp
);
2066 for (n
= 0; n
< (int)strlen(CSCOPE_PROMPT
); ++n
)
2069 ch
= getc(csinfo
[i
].fr_fp
);
2072 PERROR("cs_read_prompt EOF");
2073 if (buf
!= NULL
&& buf
[0] != NUL
)
2074 (void)EMSG2(cs_emsg
, buf
);
2075 else if (p_csverbose
)
2076 cs_reading_emsg(i
); /* don't have additional information */
2077 cs_release_csp(i
, TRUE
);
2079 return CSCOPE_FAILURE
;
2082 if (ch
!= CSCOPE_PROMPT
[n
])
2090 continue; /* didn't find the prompt */
2091 break; /* did find the prompt */
2095 return CSCOPE_SUCCESS
;
2098 #if defined(UNIX) && defined(SIGALRM)
2100 * Used to catch and ignore SIGALRM below.
2104 sig_handler
SIGDEFARG(sigarg
)
2112 * PRIVATE: cs_release_csp
2114 * Does the actual free'ing for the cs ptr with an optional flag of whether
2115 * or not to free the filename. Called by cs_kill and cs_reset.
2118 cs_release_csp(i
, freefnpp
)
2123 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2125 if (csinfo
[i
].to_fp
!= NULL
)
2127 (void)fputs("q\n", csinfo
[i
].to_fp
);
2128 (void)fflush(csinfo
[i
].to_fp
);
2136 # if defined(HAVE_SIGACTION)
2137 struct sigaction sa
, old
;
2139 /* Use sigaction() to limit the waiting time to two seconds. */
2140 sigemptyset(&sa
.sa_mask
);
2141 sa
.sa_handler
= sig_handler
;
2142 sa
.sa_flags
= SA_NODEFER
;
2143 sigaction(SIGALRM
, &sa
, &old
);
2144 alarm(2); /* 2 sec timeout */
2146 /* Block until cscope exits or until timer expires */
2147 pid
= waitpid(csinfo
[i
].pid
, &pstat
, 0);
2148 waitpid_errno
= errno
;
2150 /* cancel pending alarm if still there and restore signal */
2152 sigaction(SIGALRM
, &old
, NULL
);
2156 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2157 * to give cscope a chance to exit quickly. */
2159 for (waited
= 0; waited
< 40; ++waited
)
2161 pid
= waitpid(csinfo
[i
].pid
, &pstat
, WNOHANG
);
2162 waitpid_errno
= errno
;
2164 break; /* break unless the process is still running */
2165 mch_delay(50L, FALSE
); /* sleep 50 ms */
2169 * If the cscope process is still running: kill it.
2170 * Safety check: If the PID would be zero here, the entire X session
2171 * would be killed. -1 and 1 are dangerous as well.
2173 if (pid
< 0 && csinfo
[i
].pid
> 1)
2178 if (waitpid_errno
== ECHILD
)
2181 * When using 'vim -g', vim is forked and cscope process is
2182 * no longer a child process but a sibling. So waitpid()
2183 * fails with errno being ECHILD (No child processes).
2184 * Don't send SIGKILL to cscope immediately but wait
2185 * (polling) for it to exit normally as result of sending
2186 * the "q" command, hence giving it a chance to clean up
2187 * its temporary files.
2192 for (waited
= 0; waited
< 40; ++waited
)
2194 /* Check whether cscope process is still alive */
2195 if (kill(csinfo
[i
].pid
, 0) != 0)
2197 alive
= FALSE
; /* cscope process no longer exists */
2200 mch_delay(50L, FALSE
); /* sleep 50ms */
2206 kill(csinfo
[i
].pid
, SIGKILL
);
2207 (void)waitpid(csinfo
[i
].pid
, &pstat
, 0);
2212 if (csinfo
[i
].hProc
!= NULL
)
2214 /* Give cscope a chance to exit normally */
2215 if (WaitForSingleObject(csinfo
[i
].hProc
, 1000) == WAIT_TIMEOUT
)
2216 TerminateProcess(csinfo
[i
].hProc
, 0);
2217 CloseHandle(csinfo
[i
].hProc
);
2221 if (csinfo
[i
].fr_fp
!= NULL
)
2222 (void)fclose(csinfo
[i
].fr_fp
);
2223 if (csinfo
[i
].to_fp
!= NULL
)
2224 (void)fclose(csinfo
[i
].to_fp
);
2228 vim_free(csinfo
[i
].fname
);
2229 vim_free(csinfo
[i
].ppath
);
2230 vim_free(csinfo
[i
].flags
);
2234 } /* cs_release_csp */
2240 * calls cs_kill on all cscope connections then reinits
2247 char **dblist
= NULL
, **pplist
= NULL
, **fllist
= NULL
;
2249 char buf
[20]; /* for sprintf " (#%d)" */
2251 /* malloc our db and ppath list */
2252 dblist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2253 pplist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2254 fllist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2255 if (dblist
== NULL
|| pplist
== NULL
|| fllist
== NULL
)
2260 return CSCOPE_FAILURE
;
2263 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2265 dblist
[i
] = csinfo
[i
].fname
;
2266 pplist
[i
] = csinfo
[i
].ppath
;
2267 fllist
[i
] = csinfo
[i
].flags
;
2268 if (csinfo
[i
].fname
!= NULL
)
2269 cs_release_csp(i
, FALSE
);
2272 /* rebuild the cscope connection list */
2273 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2275 if (dblist
[i
] != NULL
)
2277 cs_add_common(dblist
[i
], pplist
[i
], fllist
[i
]);
2280 /* don't use smsg_attr() because we want to display the
2281 * connection number in the same line as
2282 * "Added cscope database..."
2284 sprintf(buf
, " (#%d)", i
);
2285 MSG_PUTS_ATTR(buf
, hl_attr(HLF_R
));
2288 vim_free(dblist
[i
]);
2289 vim_free(pplist
[i
]);
2290 vim_free(fllist
[i
]);
2297 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R
) | MSG_HIST
);
2298 return CSCOPE_SUCCESS
;
2303 * PRIVATE: cs_resolve_file
2305 * construct the full pathname to a file found in the cscope database.
2306 * (Prepends ppath, if there is one and if it's not already prepended,
2307 * otherwise just uses the name found.)
2309 * we need to prepend the prefix because on some cscope's (e.g., the one that
2310 * ships with Solaris 2.6), the output never has the prefix prepended.
2311 * contrast this with my development system (Digital Unix), which does.
2314 cs_resolve_file(i
, name
)
2322 * ppath is freed when we destroy the cscope connection.
2323 * fullname is freed after cs_make_vim_style_matches, after it's been
2324 * copied into the tag buffer used by vim
2326 len
= (int)(strlen(name
) + 2);
2327 if (csinfo
[i
].ppath
!= NULL
)
2328 len
+= (int)strlen(csinfo
[i
].ppath
);
2330 if ((fullname
= (char *)alloc(len
)) == NULL
)
2334 * note/example: this won't work if the cscope output already starts
2335 * "../.." and the prefix path is also "../..". if something like this
2336 * happens, you are screwed up and need to fix how you're using cscope.
2338 if (csinfo
[i
].ppath
!= NULL
&&
2339 (strncmp(name
, csinfo
[i
].ppath
, strlen(csinfo
[i
].ppath
)) != 0) &&
2342 && name
[0] != '\\' && name
[1] != ':'
2345 (void)sprintf(fullname
, "%s/%s", csinfo
[i
].ppath
, name
);
2347 (void)sprintf(fullname
, "%s", name
);
2350 } /* cs_resolve_file */
2356 * show all cscope connections
2364 if (cs_cnt_connections() == 0)
2365 MSG_PUTS(_("no cscope connections\n"));
2369 _(" # pid database name prepend path\n"),
2371 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2373 if (csinfo
[i
].fname
== NULL
)
2376 if (csinfo
[i
].ppath
!= NULL
)
2377 (void)smsg((char_u
*)"%2d %-5ld %-34s %-32s",
2378 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
, csinfo
[i
].ppath
);
2380 (void)smsg((char_u
*)"%2d %-5ld %-34s <none>",
2381 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
);
2386 return CSCOPE_SUCCESS
;
2393 * Only called when VIM exits to quit any cscope sessions.
2400 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2401 cs_release_csp(i
, TRUE
);
2404 #endif /* FEAT_CSCOPE */