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 c|d|e|f|g|i|s|t name", 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
);
96 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
100 EXP_CSCOPE_SUBCMD
, /* expand ":cscope" sub-commands */
101 EXP_CSCOPE_FIND
, /* expand ":cscope find" arguments */
102 EXP_CSCOPE_KILL
/* expand ":cscope kill" arguments */
106 * Function given to ExpandGeneric() to obtain the cscope command
111 get_cscope_name(xp
, idx
)
117 case EXP_CSCOPE_SUBCMD
:
118 /* Complete with sub-commands of ":cscope":
119 * add, find, help, kill, reset, show */
120 return (char_u
*)cs_cmds
[idx
].name
;
121 case EXP_CSCOPE_FIND
:
123 const char *query_type
[] =
125 "c", "d", "e", "f", "g", "i", "s", "t", NULL
128 /* Complete with query type of ":cscope find {query_type}".
129 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
130 * ..., 8) but only complete with letters, since numbers are
132 return (char_u
*)query_type
[idx
];
134 case EXP_CSCOPE_KILL
:
138 static char_u connection
[2];
140 /* ":cscope kill" accepts connection numbers or partial names of
141 * the pathname of the cscope database as argument. Only complete
142 * with connection numbers. -1 can also be used to kill all
144 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
146 if (csinfo
[i
].fname
== NULL
)
148 if (current_idx
++ == idx
)
150 /* Connection number fits in one character since
151 * CSCOPE_MAX_CONNECTIONS is < 10 */
152 connection
[0] = i
+ '0';
157 return (current_idx
== idx
&& idx
> 0) ? (char_u
*)"-1" : NULL
;
165 * Handle command line completion for :cscope command.
168 set_context_in_cscope_cmd(xp
, arg
)
174 /* Default: expand subcommands */
175 xp
->xp_context
= EXPAND_CSCOPE
;
176 expand_what
= EXP_CSCOPE_SUBCMD
;
177 xp
->xp_pattern
= arg
;
179 /* (part of) subcommand already typed */
182 p
= skiptowhite(arg
);
183 if (*p
!= NUL
) /* past first word */
185 xp
->xp_pattern
= skipwhite(p
);
186 if (*skiptowhite(xp
->xp_pattern
) != NUL
)
187 xp
->xp_context
= EXPAND_NOTHING
;
188 else if (STRNICMP(arg
, "add", p
- arg
) == 0)
189 xp
->xp_context
= EXPAND_FILES
;
190 else if (STRNICMP(arg
, "kill", p
- arg
) == 0)
191 expand_what
= EXP_CSCOPE_KILL
;
192 else if (STRNICMP(arg
, "find", p
- arg
) == 0)
193 expand_what
= EXP_CSCOPE_FIND
;
195 xp
->xp_context
= EXPAND_NOTHING
;
200 #endif /* FEAT_CMDL_COMPL */
203 * PRIVATE: do_cscope_general
205 * Find the command, print help if invalid, and then call the corresponding
209 do_cscope_general(eap
, make_split
)
211 int make_split
; /* whether to split window */
216 if ((cmdp
= cs_lookup_cmd(eap
)) == NULL
)
227 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
230 postponed_split
= -1;
231 postponed_split_flags
= cmdmod
.split
;
232 postponed_split_tab
= cmdmod
.tab
;
239 postponed_split_flags
= 0;
240 postponed_split_tab
= 0;
251 do_cscope_general(eap
, FALSE
);
257 * same as do_cscope, but splits window, too.
263 do_cscope_general(eap
, TRUE
);
278 if (*eap
->arg
== NUL
)
280 (void)EMSG(_("E562: Usage: cstag <ident>"));
287 if (cs_check_for_connections())
289 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
297 if (cs_check_for_tags())
298 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
301 else if (cs_check_for_tags())
303 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
307 if (cs_check_for_tags())
309 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
315 if (cs_check_for_connections())
317 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
,
324 else if (cs_check_for_connections())
326 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
338 (void)EMSG(_("E257: cstag: tag not found"));
339 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
350 * this simulates a vim_fgets(), but for cscope, returns the next line
351 * from the cscope output. should only be called from find_tags()
353 * returns TRUE if eof, FALSE otherwise
362 if ((p
= cs_manage_matches(NULL
, NULL
, -1, Get
)) == NULL
)
364 vim_strncpy(buf
, (char_u
*)p
, size
- 1);
371 * PUBLIC: cs_free_tags
373 * called only from do_tag(), when popping the tag stack
378 cs_manage_matches(NULL
, NULL
, -1, Free
);
383 * PUBLIC: cs_print_tags
385 * called from do_tag()
390 cs_manage_matches(NULL
, NULL
, -1, Print
);
395 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
397 * Checks for the existence of a |cscope| connection. If no
398 * parameters are specified, then the function returns:
400 * 0, if cscope was not available (not compiled in), or if there
401 * are no cscope connections; or
402 * 1, if there is at least one cscope connection.
404 * If parameters are specified, then the value of {num}
405 * determines how existence of a cscope connection is checked:
407 * {num} Description of existence check
408 * ----- ------------------------------
409 * 0 Same as no parameters (e.g., "cscope_connection()").
410 * 1 Ignore {prepend}, and use partial string matches for
412 * 2 Ignore {prepend}, and use exact string matches for
414 * 3 Use {prepend}, use partial string matches for both
415 * {dbpath} and {prepend}.
416 * 4 Use {prepend}, use exact string matches for both
417 * {dbpath} and {prepend}.
419 * Note: All string comparisons are case sensitive!
421 #if defined(FEAT_EVAL) || defined(PROTO)
423 cs_connection(num
, dbpath
, ppath
)
430 if (num
< 0 || num
> 4 || (num
> 0 && !dbpath
))
433 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
435 if (!csinfo
[i
].fname
)
444 if (strstr(csinfo
[i
].fname
, (char *)dbpath
))
448 if (strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
452 if (strstr(csinfo
[i
].fname
, (char *)dbpath
)
453 && ((!ppath
&& !csinfo
[i
].ppath
)
456 && strstr(csinfo
[i
].ppath
, (char *)ppath
))))
460 if ((strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
461 && ((!ppath
&& !csinfo
[i
].ppath
)
464 && (strcmp(csinfo
[i
].ppath
, (char *)ppath
) == 0))))
471 } /* cs_connection */
477 ****************************************************************************/
482 * add cscope database or a directory name (to look for cscope.out)
483 * to the cscope connection list
492 char *fname
, *ppath
, *flags
= NULL
;
494 if ((fname
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
497 return CSCOPE_FAILURE
;
499 if ((ppath
= strtok((char *)NULL
, (const char *)" ")) != NULL
)
500 flags
= strtok((char *)NULL
, (const char *)" ");
502 return cs_add_common(fname
, ppath
, flags
);
509 char *stat_emsg
= _("E563: stat(%s) error: %d");
510 char *buf
= (char *)alloc((unsigned)strlen(stat_emsg
) + MAXPATHL
+ 10);
514 (void)sprintf(buf
, stat_emsg
, fname
, errno
);
519 (void)EMSG(_("E563: stat error"));
524 * PRIVATE: cs_add_common
526 * the common routine to add a new cscope connection. called by
527 * cs_add() and cs_reset(). i really don't like to do this, but this
528 * routine uses a number of goto statements.
531 cs_add_common(arg1
, arg2
, flags
)
532 char *arg1
; /* filename - may contain environment variables */
533 char *arg2
; /* prepend path - may contain environment variables */
543 /* get the filename (arg1), expand it, and try to stat it */
544 if ((fname
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
547 expand_env((char_u
*)arg1
, (char_u
*)fname
, MAXPATHL
);
548 ret
= stat(fname
, &statbuf
);
557 /* get the prepend path (arg2), expand it, and try to stat it */
560 struct stat statbuf2
;
562 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
565 expand_env((char_u
*)arg2
, (char_u
*)ppath
, MAXPATHL
);
566 ret
= stat(ppath
, &statbuf2
);
571 /* if filename is a directory, append the cscope database name to it */
572 if ((statbuf
.st_mode
& S_IFMT
) == S_IFDIR
)
574 fname2
= (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE
) + strlen(fname
) + 2));
578 while (fname
[strlen(fname
)-1] == '/'
580 || fname
[strlen(fname
)-1] == '\\'
584 fname
[strlen(fname
)-1] = '\0';
585 if (strlen(fname
) == 0)
588 if (fname
[0] == '\0')
589 (void)sprintf(fname2
, "/%s", CSCOPE_DBFILE
);
591 (void)sprintf(fname2
, "%s/%s", fname
, CSCOPE_DBFILE
);
593 ret
= stat(fname2
, &statbuf
);
597 cs_stat_emsg(fname2
);
601 i
= cs_insert_filelist(fname2
, ppath
, flags
, &statbuf
);
604 else if (S_ISREG(statbuf
.st_mode
) || S_ISLNK(statbuf
.st_mode
))
606 /* WIN32 - substitute define S_ISREG from os_unix.h */
607 else if (((statbuf
.st_mode
) & S_IFMT
) == S_IFREG
)
610 i
= cs_insert_filelist(fname
, ppath
, flags
, &statbuf
);
616 _("E564: %s is not a directory or a valid cscope database"),
623 if (cs_create_connection(i
) == CSCOPE_FAILURE
624 || cs_read_prompt(i
) == CSCOPE_FAILURE
)
626 cs_release_csp(i
, TRUE
);
633 (void)smsg_attr(hl_attr(HLF_R
),
634 (char_u
*)_("Added cscope database %s"),
642 return CSCOPE_SUCCESS
;
648 return CSCOPE_FAILURE
;
649 } /* cs_add_common */
653 cs_check_for_connections()
655 return (cs_cnt_connections() > 0);
656 } /* cs_check_for_connections */
662 return (p_tags
[0] != NUL
&& curbuf
->b_p_tags
!= NULL
);
663 } /* cs_check_for_tags */
667 * PRIVATE: cs_cnt_connections
669 * count the number of cscope connections
677 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
679 if (csinfo
[i
].fname
!= NULL
)
683 } /* cs_cnt_connections */
687 int idx
; /* connection index */
689 EMSGN(_("E262: error reading cscope connection %ld"), idx
);
692 #define CSREAD_BUFSIZE 2048
694 * PRIVATE: cs_cnt_matches
696 * count the number of matches for a given cscope connection.
706 buf
= (char *)alloc(CSREAD_BUFSIZE
);
711 if (!fgets(buf
, CSREAD_BUFSIZE
, csinfo
[idx
].fr_fp
))
713 if (feof(csinfo
[idx
].fr_fp
))
716 cs_reading_emsg(idx
);
723 * If the database is out of date, or there's some other problem,
724 * cscope will output error messages before the number-of-lines output.
725 * Display/discard any output that doesn't match what we want.
726 * Accept "\S*cscope: X lines", also matches "mlcscope".
728 if ((stok
= strtok(buf
, (const char *)" ")) == NULL
)
730 if (strstr((const char *)stok
, "cscope:") == NULL
)
733 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
742 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
744 if (strncmp((const char *)stok
, "lines", 5))
752 } /* cs_cnt_matches */
756 * PRIVATE: cs_create_cmd
758 * Creates the actual cscope command query from what the user entered.
761 cs_create_cmd(csoption
, pattern
)
771 case '0' : case 's' :
774 case '1' : case 'g' :
777 case '2' : case 'd' :
780 case '3' : case 'c' :
783 case '4' : case 't' :
786 case '6' : case 'e' :
789 case '7' : case 'f' :
792 case '8' : case 'i' :
796 (void)EMSG(_("E561: unknown cscope search type"));
801 /* Skip white space before the patter, except for text and pattern search,
802 * they may want to use the leading white space. */
804 if (search
!= 4 && search
!= 6)
805 while vim_iswhite(*pat
)
808 if ((cmd
= (char *)alloc((unsigned)(strlen(pat
) + 2))) == NULL
)
811 (void)sprintf(cmd
, "%d%s", search
, pat
);
814 } /* cs_create_cmd */
818 * PRIVATE: cs_create_connection
820 * This piece of code was taken/adapted from nvi. do we need to add
821 * the BSD license notice?
824 cs_create_connection(i
)
828 int to_cs
[2], from_cs
[2];
831 char *prog
, *cmd
, *ppath
= NULL
;
834 SECURITY_ATTRIBUTES sa
;
835 PROCESS_INFORMATION pi
;
837 BOOL pipe_stdin
= FALSE
, pipe_stdout
= FALSE
;
838 HANDLE stdin_rd
, stdout_rd
;
839 HANDLE stdout_wr
, stdin_wr
;
842 # define OPEN_OH_ARGTYPE long
844 # if (_MSC_VER >= 1300)
845 # define OPEN_OH_ARGTYPE intptr_t
847 # define OPEN_OH_ARGTYPE long
854 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
855 * from_cs[0] and writes to to_cs[1].
857 to_cs
[0] = to_cs
[1] = from_cs
[0] = from_cs
[1] = -1;
858 if (pipe(to_cs
) < 0 || pipe(from_cs
) < 0)
860 (void)EMSG(_("E566: Could not create cscope pipes"));
863 (void)close(to_cs
[0]);
865 (void)close(to_cs
[1]);
866 if (from_cs
[0] != -1)
867 (void)close(from_cs
[0]);
868 if (from_cs
[1] != -1)
869 (void)close(from_cs
[1]);
870 return CSCOPE_FAILURE
;
873 switch (csinfo
[i
].pid
= fork())
876 (void)EMSG(_("E622: Could not fork for cscope"));
878 case 0: /* child: run cscope. */
879 if (dup2(to_cs
[0], STDIN_FILENO
) == -1)
880 PERROR("cs_create_connection 1");
881 if (dup2(from_cs
[1], STDOUT_FILENO
) == -1)
882 PERROR("cs_create_connection 2");
883 if (dup2(from_cs
[1], STDERR_FILENO
) == -1)
884 PERROR("cs_create_connection 3");
887 (void)close(to_cs
[1]);
888 (void)close(from_cs
[0]);
891 /* Create pipes to communicate with cscope */
892 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
893 sa
.bInheritHandle
= TRUE
;
894 sa
.lpSecurityDescriptor
= NULL
;
896 if (!(pipe_stdin
= CreatePipe(&stdin_rd
, &stdin_wr
, &sa
, 0))
897 || !(pipe_stdout
= CreatePipe(&stdout_rd
, &stdout_wr
, &sa
, 0)))
899 (void)EMSG(_("E566: Could not create cscope pipes"));
903 CloseHandle(stdin_rd
);
904 CloseHandle(stdin_wr
);
908 CloseHandle(stdout_rd
);
909 CloseHandle(stdout_wr
);
911 return CSCOPE_FAILURE
;
914 /* expand the cscope exec for env var's */
915 if ((prog
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
918 return CSCOPE_FAILURE
;
924 expand_env((char_u
*)p_csprg
, (char_u
*)prog
, MAXPATHL
);
926 /* alloc space to hold the cscope command */
927 len
= (int)(strlen(prog
) + strlen(csinfo
[i
].fname
) + 32);
930 /* expand the prepend path for env var's */
931 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
935 return CSCOPE_FAILURE
;
941 expand_env((char_u
*)csinfo
[i
].ppath
, (char_u
*)ppath
, MAXPATHL
);
943 len
+= (int)strlen(ppath
);
947 len
+= (int)strlen(csinfo
[i
].flags
);
949 if ((cmd
= (char *)alloc(len
)) == NULL
)
954 return CSCOPE_FAILURE
;
961 /* run the cscope command; is there execl for non-unix systems? */
963 (void)sprintf(cmd
, "exec %s -dl -f %s", prog
, csinfo
[i
].fname
);
966 (void)sprintf(cmd
, "%s -dl -f %s", prog
, csinfo
[i
].fname
);
968 if (csinfo
[i
].ppath
!= NULL
)
970 (void)strcat(cmd
, " -P");
971 (void)strcat(cmd
, csinfo
[i
].ppath
);
973 if (csinfo
[i
].flags
!= NULL
)
975 (void)strcat(cmd
, " ");
976 (void)strcat(cmd
, csinfo
[i
].flags
);
979 /* on Win32 we still need prog */
985 if (execl("/bin/sh", "sh", "-c", cmd
, NULL
) == -1)
986 PERROR(_("cs_create_connection exec failed"));
990 default: /* parent. */
992 * Save the file descriptors for later duplication, and
995 if ((csinfo
[i
].to_fp
= fdopen(to_cs
[1], "w")) == NULL
)
996 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
997 if ((csinfo
[i
].fr_fp
= fdopen(from_cs
[0], "r")) == NULL
)
998 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1001 (void)close(to_cs
[0]);
1002 (void)close(from_cs
[1]);
1009 /* Create a new process to run cscope and use pipes to talk with it */
1010 GetStartupInfo(&si
);
1011 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
1012 si
.wShowWindow
= SW_HIDE
; /* Hide child application window */
1013 si
.hStdOutput
= stdout_wr
;
1014 si
.hStdError
= stdout_wr
;
1015 si
.hStdInput
= stdin_rd
;
1016 created
= CreateProcess(NULL
, cmd
, NULL
, NULL
, TRUE
, CREATE_NEW_CONSOLE
,
1017 NULL
, NULL
, &si
, &pi
);
1023 PERROR(_("cs_create_connection exec failed"));
1024 (void)EMSG(_("E623: Could not spawn cscope process"));
1028 csinfo
[i
].pid
= pi
.dwProcessId
;
1029 csinfo
[i
].hProc
= pi
.hProcess
;
1030 CloseHandle(pi
.hThread
);
1032 /* TODO - tidy up after failure to create files on pipe handles. */
1033 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdin_wr
,
1034 _O_TEXT
|_O_APPEND
)) < 0)
1035 || ((csinfo
[i
].to_fp
= _fdopen(fd
, "w")) == NULL
))
1036 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1037 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdout_rd
,
1038 _O_TEXT
|_O_RDONLY
)) < 0)
1039 || ((csinfo
[i
].fr_fp
= _fdopen(fd
, "r")) == NULL
))
1040 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1042 /* Close handles for file descriptors inherited by the cscope process */
1043 CloseHandle(stdin_rd
);
1044 CloseHandle(stdout_wr
);
1048 return CSCOPE_SUCCESS
;
1049 } /* cs_create_connection */
1055 * query cscope using command line interface. parse the output and use tselect
1056 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1058 * returns TRUE if we jump to a tag or abort, FALSE if not.
1066 if (cs_check_for_connections() == FALSE
)
1068 (void)EMSG(_("E567: no cscope connections"));
1072 if ((opt
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1078 pat
= opt
+ strlen(opt
) + 1;
1079 if (pat
>= (char *)eap
->arg
+ eap_arg_len
)
1085 return cs_find_common(opt
, pat
, eap
->forceit
, TRUE
,
1086 eap
->cmdidx
== CMD_lcscope
);
1091 * PRIVATE: cs_find_common
1093 * common code for cscope find, shared by cs_find() and do_cstag()
1096 cs_find_common(opt
, pat
, forceit
, verbose
, use_ll
)
1105 int nummatches
[CSCOPE_MAX_CONNECTIONS
], totmatches
;
1106 #ifdef FEAT_QUICKFIX
1111 /* create the actual command to send to cscope */
1112 cmd
= cs_create_cmd(opt
, pat
);
1116 /* send query to all open connections, then count the total number
1117 * of matches so we can alloc matchesp all in one swell foop
1119 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1122 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1124 if (csinfo
[i
].fname
== NULL
|| csinfo
[i
].to_fp
== NULL
)
1127 /* send cmd to cscope */
1128 (void)fprintf(csinfo
[i
].to_fp
, "%s\n", cmd
);
1129 (void)fflush(csinfo
[i
].to_fp
);
1131 nummatches
[i
] = cs_cnt_matches(i
);
1133 if (nummatches
[i
] > -1)
1134 totmatches
+= nummatches
[i
];
1136 if (nummatches
[i
] == 0)
1137 (void)cs_read_prompt(i
);
1141 if (totmatches
== 0)
1143 char *nf
= _("E259: no matches found for cscope query %s of %s");
1149 buf
= (char *)alloc((unsigned)(strlen(opt
) + strlen(pat
) + strlen(nf
)));
1154 sprintf(buf
, nf
, opt
, pat
);
1161 #ifdef FEAT_QUICKFIX
1162 /* get cmd letter */
1193 qfpos
= (char *)vim_strchr(p_csqf
, cmdletter
);
1197 /* next symbol must be + or - */
1198 if (strchr(CSQF_FLAGS
, *qfpos
) == NULL
)
1200 char *nf
= _("E469: invalid cscopequickfix flag %c for %c");
1201 char *buf
= (char *)alloc((unsigned)strlen(nf
));
1203 /* strlen will be enough because we use chars */
1206 sprintf(buf
, nf
, *qfpos
, *(qfpos
-1));
1213 if (qfpos
!= NULL
&& *qfpos
!= '0' && totmatches
> 0)
1215 /* fill error list */
1217 char_u
*tmp
= vim_tempname('c');
1218 qf_info_T
*qi
= NULL
;
1221 f
= mch_fopen((char *)tmp
, "w");
1223 EMSG2(_(e_notopen
), tmp
);
1226 cs_file_results(f
, nummatches
);
1228 if (use_ll
) /* Use location list */
1230 /* '-' starts a new error list */
1231 if (qf_init(wp
, tmp
, (char_u
*)"%f%*\\t%l%*\\t%m",
1234 # ifdef FEAT_WINDOWS
1235 if (postponed_split
!= 0)
1237 win_split(postponed_split
> 0 ? postponed_split
: 0,
1238 postponed_split_flags
);
1239 # ifdef FEAT_SCROLLBIND
1240 curwin
->w_p_scb
= FALSE
;
1242 postponed_split
= 0;
1247 * In the location list window, use the displayed location
1248 * list. Otherwise, use the location list for the window.
1250 qi
= (bt_quickfix(wp
->w_buffer
) && wp
->w_llist_ref
!= NULL
)
1251 ? wp
->w_llist_ref
: wp
->w_llist
;
1252 qf_jump(qi
, 0, 0, forceit
);
1260 #endif /* FEAT_QUICKFIX */
1262 char **matches
= NULL
, **contexts
= NULL
;
1266 cs_fill_results((char *)pat
, totmatches
, nummatches
, &matches
,
1267 &contexts
, &matched
);
1268 if (matches
== NULL
)
1271 (void)cs_manage_matches(matches
, contexts
, matched
, Store
);
1273 return do_tag((char_u
*)pat
, DT_CSCOPE
, 0, forceit
, verbose
);
1276 } /* cs_find_common */
1288 cscmd_T
*cmdp
= cs_cmds
;
1290 (void)MSG_PUTS(_("cscope commands:\n"));
1291 while (cmdp
->name
!= NULL
)
1293 char *help
= _(cmdp
->help
);
1294 int space_cnt
= 30 - vim_strsize((char_u
*)help
);
1296 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1299 (void)smsg((char_u
*)_("%-5s: %s%*s (Usage: %s)"),
1301 help
, space_cnt
, " ",
1303 if (strcmp(cmdp
->name
, "find") == 0)
1305 " c: Find functions calling this function\n"
1306 " d: Find functions called by this function\n"
1307 " e: Find this egrep pattern\n"
1308 " f: Find this file\n"
1309 " g: Find this definition\n"
1310 " i: Find files #including this file\n"
1311 " s: Find this C symbol\n"
1312 " t: Find assignments to\n"));
1325 * initialize cscope structure if not already
1331 static int init_already
= FALSE
;
1336 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1339 init_already
= TRUE
;
1346 csinfo
[i
].fname
= NULL
;
1347 csinfo
[i
].ppath
= NULL
;
1348 csinfo
[i
].flags
= NULL
;
1350 csinfo
[i
].st_dev
= (dev_t
)0;
1351 csinfo
[i
].st_ino
= (ino_t
)0;
1353 csinfo
[i
].nVolume
= 0;
1354 csinfo
[i
].nIndexHigh
= 0;
1355 csinfo
[i
].nIndexLow
= 0;
1358 csinfo
[i
].fr_fp
= NULL
;
1359 csinfo
[i
].to_fp
= NULL
;
1361 csinfo
[i
].hProc
= NULL
;
1366 static char *GetWin32Error
__ARGS((void));
1372 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
1373 NULL
, GetLastError(), 0, (LPSTR
)&msg
, 0, NULL
);
1376 /* remove trailing \r\n */
1377 char *pcrlf
= strstr(msg
, "\r\n");
1386 * PRIVATE: cs_insert_filelist
1388 * insert a new cscope database filename into the filelist
1392 cs_insert_filelist(fname
, ppath
, flags
, sb
)
1401 BY_HANDLE_FILE_INFORMATION bhfi
;
1403 vim_memset(&bhfi
, 0, sizeof(bhfi
));
1404 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1405 if (!mch_windows95())
1407 hFile
= CreateFile(fname
, FILE_READ_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
1408 FILE_ATTRIBUTE_NORMAL
, NULL
);
1409 if (hFile
== INVALID_HANDLE_VALUE
)
1413 char *cant_msg
= _("E625: cannot open cscope database: %s");
1414 char *winmsg
= GetWin32Error();
1418 (void)EMSG2(cant_msg
, winmsg
);
1422 /* subst filename if can't get error text */
1423 (void)EMSG2(cant_msg
, fname
);
1427 if (!GetFileInformationByHandle(hFile
, &bhfi
))
1431 (void)EMSG(_("E626: cannot get cscope database information"));
1438 i
= -1; /* can be set to the index of an empty item in csinfo */
1439 for (j
= 0; j
< CSCOPE_MAX_CONNECTIONS
; j
++)
1441 if (csinfo
[j
].fname
!= NULL
1443 && csinfo
[j
].st_dev
== sb
->st_dev
&& csinfo
[j
].st_ino
== sb
->st_ino
1445 /* compare pathnames first */
1446 && ((fullpathcmp(csinfo
[j
].fname
, fname
, FALSE
) & FPC_SAME
)
1447 /* if not Windows 9x, test index file attributes too */
1448 || (!mch_windows95()
1449 && csinfo
[j
].nVolume
== bhfi
.dwVolumeSerialNumber
1450 && csinfo
[j
].nIndexHigh
== bhfi
.nFileIndexHigh
1451 && csinfo
[j
].nIndexLow
== bhfi
.nFileIndexLow
))
1456 (void)EMSG(_("E568: duplicate cscope database not added"));
1460 if (csinfo
[j
].fname
== NULL
&& i
== -1)
1461 i
= j
; /* remember first empty entry */
1467 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1471 if ((csinfo
[i
].fname
= (char *)alloc((unsigned)strlen(fname
)+1)) == NULL
)
1474 (void)strcpy(csinfo
[i
].fname
, (const char *)fname
);
1478 if ((csinfo
[i
].ppath
= (char *)alloc((unsigned)strlen(ppath
) + 1)) == NULL
)
1480 vim_free(csinfo
[i
].fname
);
1481 csinfo
[i
].fname
= NULL
;
1484 (void)strcpy(csinfo
[i
].ppath
, (const char *)ppath
);
1486 csinfo
[i
].ppath
= NULL
;
1490 if ((csinfo
[i
].flags
= (char *)alloc((unsigned)strlen(flags
) + 1)) == NULL
)
1492 vim_free(csinfo
[i
].fname
);
1493 vim_free(csinfo
[i
].ppath
);
1494 csinfo
[i
].fname
= NULL
;
1495 csinfo
[i
].ppath
= NULL
;
1498 (void)strcpy(csinfo
[i
].flags
, (const char *)flags
);
1500 csinfo
[i
].flags
= NULL
;
1503 csinfo
[i
].st_dev
= sb
->st_dev
;
1504 csinfo
[i
].st_ino
= sb
->st_ino
;
1507 csinfo
[i
].nVolume
= bhfi
.dwVolumeSerialNumber
;
1508 csinfo
[i
].nIndexLow
= bhfi
.nFileIndexLow
;
1509 csinfo
[i
].nIndexHigh
= bhfi
.nFileIndexHigh
;
1512 } /* cs_insert_filelist */
1516 * PRIVATE: cs_lookup_cmd
1518 * find cscope command in command table
1528 if (eap
->arg
== NULL
)
1531 /* Store length of eap->arg before it gets modified by strtok(). */
1532 eap_arg_len
= (int)STRLEN(eap
->arg
);
1534 if ((stok
= strtok((char *)(eap
->arg
), (const char *)" ")) == NULL
)
1538 for (cmdp
= cs_cmds
; cmdp
->name
!= NULL
; ++cmdp
)
1540 if (strncmp((const char *)(stok
), cmdp
->name
, len
) == 0)
1544 } /* cs_lookup_cmd */
1560 if ((stok
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1563 return CSCOPE_FAILURE
;
1566 /* only single digit positive and negative integers are allowed */
1567 if ((strlen(stok
) < 2 && VIM_ISDIGIT((int)(stok
[0])))
1568 || (strlen(stok
) < 3 && stok
[0] == '-'
1569 && VIM_ISDIGIT((int)(stok
[1]))))
1573 /* It must be part of a name. We will try to find a match
1574 * within all the names in the csinfo data structure
1576 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1578 if (csinfo
[i
].fname
!= NULL
&& strstr(csinfo
[i
].fname
, stok
))
1583 if ((i
>= CSCOPE_MAX_CONNECTIONS
|| i
< -1 || csinfo
[i
].fname
== NULL
)
1587 (void)EMSG2(_("E261: cscope connection %s not found"), stok
);
1593 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1595 if (csinfo
[i
].fname
)
1596 cs_kill_execute(i
, csinfo
[i
].fname
);
1600 cs_kill_execute(i
, stok
);
1608 * PRIVATE: cs_kill_execute
1610 * Actually kills a specific cscope connection.
1613 cs_kill_execute(i
, cname
)
1614 int i
; /* cscope table index */
1615 char *cname
; /* cscope database name */
1620 (void)smsg_attr(hl_attr(HLF_R
) | MSG_HIST
,
1621 (char_u
*)_("cscope connection %s closed"), cname
);
1623 cs_release_csp(i
, TRUE
);
1628 * PRIVATE: cs_make_vim_style_matches
1630 * convert the cscope output into into a ctags style entry (as might be found
1631 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1632 * the type of the tag you are looking for. for example, in Darren Hiebert's
1633 * ctags (the one that comes with vim), #define's use a line number to find the
1634 * tag in a file while function definitions use a regexp search pattern.
1636 * i'm going to always use the line number because cscope does something
1637 * quirky (and probably other things i don't know about):
1639 * if you have "# define" in your source file, which is
1640 * perfectly legal, cscope thinks you have "#define". this
1641 * will result in a failed regexp search. :(
1643 * besides, even if this particular case didn't happen, the search pattern
1644 * would still have to be modified to escape all the special regular expression
1645 * characters to comply with ctags formatting.
1648 cs_make_vim_style_matches(fname
, slno
, search
, tagstr
)
1654 /* vim style is ctags:
1656 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1658 * but as mentioned above, we'll always use the line number and
1659 * put the search pattern (if one exists) as "extra"
1661 * buf is used as part of vim's method of handling tags, and
1662 * (i think) vim frees it when you pop your tags and get replaced
1663 * by new ones on the tag stack.
1670 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + strlen(search
)+6);
1671 if ((buf
= (char *)alloc(amt
)) == NULL
)
1674 (void)sprintf(buf
, "%s\t%s\t%s;\"\t%s", tagstr
, fname
, slno
, search
);
1678 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + 5);
1679 if ((buf
= (char *)alloc(amt
)) == NULL
)
1682 (void)sprintf(buf
, "%s\t%s\t%s;\"", tagstr
, fname
, slno
);
1686 } /* cs_make_vim_style_matches */
1690 * PRIVATE: cs_manage_matches
1692 * this is kind of hokey, but i don't see an easy way round this..
1694 * Store: keep a ptr to the (malloc'd) memory of matches originally
1695 * generated from cs_find(). the matches are originally lines directly
1696 * from cscope output, but transformed to look like something out of a
1697 * ctags. see cs_make_vim_style_matches for more details.
1699 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1700 * the next line from the cscope output. it basically keeps track of which
1701 * lines have been "used" and returns the next one.
1703 * Free: frees up everything and resets
1705 * Print: prints the tags
1708 cs_manage_matches(matches
, contexts
, totmatches
, cmd
)
1714 static char **mp
= NULL
;
1715 static char **cp
= NULL
;
1716 static int cnt
= -1;
1717 static int next
= -1;
1723 assert(matches
!= NULL
);
1724 assert(totmatches
> 0);
1725 if (mp
!= NULL
|| cp
!= NULL
)
1726 (void)cs_manage_matches(NULL
, NULL
, -1, Free
);
1758 cs_print_tags_priv(mp
, cp
, cnt
);
1760 default: /* should not reach here */
1761 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1766 } /* cs_manage_matches */
1770 * PRIVATE: cs_parse_results
1772 * parse cscope output
1775 cs_parse_results(cnumber
, buf
, bufsize
, context
, linenumber
, search
)
1787 if (fgets(buf
, bufsize
, csinfo
[cnumber
].fr_fp
) == NULL
)
1789 if (feof(csinfo
[cnumber
].fr_fp
))
1792 cs_reading_emsg(cnumber
);
1797 /* If the line's too long for the buffer, discard it. */
1798 if ((p
= strchr(buf
, '\n')) == NULL
)
1800 while ((ch
= getc(csinfo
[cnumber
].fr_fp
)) != EOF
&& ch
!= '\n')
1807 * cscope output is in the following format:
1809 * <filename> <context> <line number> <pattern>
1811 if ((name
= strtok((char *)buf
, (const char *)" ")) == NULL
)
1813 if ((*context
= strtok(NULL
, (const char *)" ")) == NULL
)
1815 if ((*linenumber
= strtok(NULL
, (const char *)" ")) == NULL
)
1817 *search
= *linenumber
+ strlen(*linenumber
) + 1; /* +1 to skip \0 */
1820 * If the file is older than the cscope database, that is,
1821 * the database was built since the file was last modified,
1822 * or there wasn't a search string, use the line number.
1824 if (strcmp(*search
, "<unknown>") == 0)
1827 name
= cs_resolve_file(cnumber
, name
);
1831 #ifdef FEAT_QUICKFIX
1833 * PRIVATE: cs_file_results
1835 * write cscope find results to file
1838 cs_file_results(f
, nummatches_a
)
1844 char *search
, *slno
;
1849 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1853 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1855 if (nummatches_a
[i
] < 1)
1858 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1860 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1861 &slno
, &search
)) == NULL
)
1864 context
= (char *)alloc((unsigned)strlen(cntx
)+5);
1865 if (context
== NULL
)
1868 if (strcmp(cntx
, "<global>")==0)
1869 strcpy(context
, "<<global>>");
1871 sprintf(context
, "<<%s>>", cntx
);
1874 fprintf(f
, "%s\t%s\t%s\n", fullname
, slno
, context
);
1876 fprintf(f
, "%s\t%s\t%s %s\n", fullname
, slno
, context
, search
);
1880 } /* for all matches */
1882 (void)cs_read_prompt(i
);
1884 } /* for all cscope connections */
1890 * PRIVATE: cs_fill_results
1892 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1894 * When there are no matches sets "*matches_p" to NULL.
1897 cs_fill_results(tagstr
, totmatches
, nummatches_a
, matches_p
, cntxts_p
, matched
)
1907 char *search
, *slno
;
1909 char **matches
= NULL
;
1910 char **cntxts
= NULL
;
1914 assert(totmatches
> 0);
1916 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1920 if ((matches
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1922 if ((cntxts
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1925 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1927 if (nummatches_a
[i
] < 1)
1930 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1932 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1933 &slno
, &search
)) == NULL
)
1936 matches
[totsofar
] = cs_make_vim_style_matches(fullname
, slno
,
1941 if (strcmp(cntx
, "<global>") == 0)
1942 cntxts
[totsofar
] = NULL
;
1944 /* note: if vim_strsave returns NULL, then the context
1945 * will be "<global>", which is misleading.
1947 cntxts
[totsofar
] = (char *)vim_strsave((char_u
*)cntx
);
1949 if (matches
[totsofar
] != NULL
)
1952 } /* for all matches */
1954 (void)cs_read_prompt(i
);
1956 } /* for all cscope connections */
1961 /* No matches, free the arrays and return NULL in "*matches_p". */
1967 *matched
= totsofar
;
1968 *matches_p
= matches
;
1972 } /* cs_fill_results */
1975 /* get the requested path components */
1977 cs_pathcomponents(path
)
1986 s
= path
+ strlen(path
) - 1;
1987 for (i
= 0; i
< p_cspc
; ++i
)
1988 while (s
> path
&& *--s
!= '/'
1994 if ((s
> path
&& *s
== '/')
1996 || (s
> path
&& *s
== '\\')
2004 * PRIVATE: cs_print_tags_priv
2006 * called from cs_manage_matches()
2009 cs_print_tags_priv(matches
, cntxts
, num_matches
)
2015 int bufsize
= 0; /* Track available bufsize */
2018 char *fname
, *lno
, *extra
, *tbuf
;
2020 char *globalcntx
= "GLOBAL";
2021 char *cntxformat
= " <<%s>>";
2023 char *cstag_msg
= _("Cscope tag: %s");
2024 char *csfmt_str
= "%4d %6s ";
2026 assert (num_matches
> 0);
2028 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[0]) + 1)) == NULL
)
2031 strcpy(tbuf
, matches
[0]);
2032 ptag
= strtok(tbuf
, "\t");
2034 newsize
= (int)(strlen(cstag_msg
) + strlen(ptag
));
2035 buf
= (char *)alloc(newsize
);
2039 (void)sprintf(buf
, cstag_msg
, ptag
);
2040 MSG_PUTS_ATTR(buf
, hl_attr(HLF_T
));
2045 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T
)); /* strlen is 7 */
2046 msg_advance(msg_col
+ 2);
2047 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T
));
2050 for (i
= 0; i
< num_matches
; i
++)
2054 /* if we really wanted to, we could avoid this malloc and strcpy
2055 * by parsing matches[i] on the fly and placing stuff into buf
2056 * directly, but that's too much of a hassle
2058 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[idx
]) + 1)) == NULL
)
2060 (void)strcpy(tbuf
, matches
[idx
]);
2062 if ((fname
= strtok(tbuf
, (const char *)"\t")) == NULL
)
2064 if ((fname
= strtok(NULL
, (const char *)"\t")) == NULL
)
2066 if ((lno
= strtok(NULL
, (const char *)"\t")) == NULL
)
2068 extra
= strtok(NULL
, (const char *)"\t");
2070 lno
[strlen(lno
)-2] = '\0'; /* ignore ;" at the end */
2072 /* hopefully 'num' (num of matches) will be less than 10^16 */
2073 newsize
= (int)(strlen(csfmt_str
) + 16 + strlen(lno
));
2074 if (bufsize
< newsize
)
2076 buf
= (char *)vim_realloc(buf
, newsize
);
2084 /* csfmt_str = "%4d %6s "; */
2085 (void)sprintf(buf
, csfmt_str
, num
, lno
);
2086 MSG_PUTS_ATTR(buf
, hl_attr(HLF_CM
));
2088 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname
), hl_attr(HLF_CM
));
2090 /* compute the required space for the context */
2091 if (cntxts
[idx
] != NULL
)
2092 context
= cntxts
[idx
];
2094 context
= globalcntx
;
2095 newsize
= (int)(strlen(context
) + strlen(cntxformat
));
2097 if (bufsize
< newsize
)
2099 buf
= (char *)vim_realloc(buf
, newsize
);
2107 (void)sprintf(buf
, cntxformat
, context
);
2109 /* print the context only if it fits on the same line */
2110 if (msg_col
+ (int)strlen(buf
) >= (int)Columns
)
2119 MSG_PUTS_LONG(extra
);
2122 vim_free(tbuf
); /* only after printing extra due to strtok use */
2130 got_int
= FALSE
; /* don't print any more matches */
2135 } /* for all matches */
2138 } /* cs_print_tags_priv */
2142 * PRIVATE: cs_read_prompt
2144 * read a cscope prompt (basically, skip over the ">> ")
2151 char *buf
= NULL
; /* buffer for possible error message from cscope */
2155 static char *eprompt
= "Press the RETURN key to continue:";
2156 int epromptlen
= (int)strlen(eprompt
);
2159 cs_emsg
= _("E609: Cscope error: %s");
2160 /* compute maximum allowed len for Cscope error message */
2161 maxlen
= (int)(IOSIZE
- strlen(cs_emsg
));
2165 while ((ch
= getc(csinfo
[i
].fr_fp
)) != EOF
&& ch
!= CSCOPE_PROMPT
[0])
2166 /* if there is room and char is printable */
2167 if (bufpos
< maxlen
- 1 && vim_isprintc(ch
))
2169 if (buf
== NULL
) /* lazy buffer allocation */
2170 buf
= (char *)alloc(maxlen
);
2173 /* append character to the message */
2176 if (bufpos
>= epromptlen
2177 && strcmp(&buf
[bufpos
- epromptlen
], eprompt
) == 0)
2179 /* remove eprompt from buf */
2180 buf
[bufpos
- epromptlen
] = NUL
;
2182 /* print message to user */
2183 (void)EMSG2(cs_emsg
, buf
);
2185 /* send RETURN to cscope */
2186 (void)putc('\n', csinfo
[i
].to_fp
);
2187 (void)fflush(csinfo
[i
].to_fp
);
2196 for (n
= 0; n
< (int)strlen(CSCOPE_PROMPT
); ++n
)
2199 ch
= getc(csinfo
[i
].fr_fp
);
2202 PERROR("cs_read_prompt EOF");
2203 if (buf
!= NULL
&& buf
[0] != NUL
)
2204 (void)EMSG2(cs_emsg
, buf
);
2205 else if (p_csverbose
)
2206 cs_reading_emsg(i
); /* don't have additional information */
2207 cs_release_csp(i
, TRUE
);
2209 return CSCOPE_FAILURE
;
2212 if (ch
!= CSCOPE_PROMPT
[n
])
2220 continue; /* didn't find the prompt */
2221 break; /* did find the prompt */
2225 return CSCOPE_SUCCESS
;
2228 #if defined(UNIX) && defined(SIGALRM)
2230 * Used to catch and ignore SIGALRM below.
2234 sig_handler
SIGDEFARG(sigarg
)
2242 * PRIVATE: cs_release_csp
2244 * Does the actual free'ing for the cs ptr with an optional flag of whether
2245 * or not to free the filename. Called by cs_kill and cs_reset.
2248 cs_release_csp(i
, freefnpp
)
2253 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2255 if (csinfo
[i
].to_fp
!= NULL
)
2257 (void)fputs("q\n", csinfo
[i
].to_fp
);
2258 (void)fflush(csinfo
[i
].to_fp
);
2266 # if defined(HAVE_SIGACTION)
2267 struct sigaction sa
, old
;
2269 /* Use sigaction() to limit the waiting time to two seconds. */
2270 sigemptyset(&sa
.sa_mask
);
2271 sa
.sa_handler
= sig_handler
;
2272 sa
.sa_flags
= SA_NODEFER
;
2273 sigaction(SIGALRM
, &sa
, &old
);
2274 alarm(2); /* 2 sec timeout */
2276 /* Block until cscope exits or until timer expires */
2277 pid
= waitpid(csinfo
[i
].pid
, &pstat
, 0);
2278 waitpid_errno
= errno
;
2280 /* cancel pending alarm if still there and restore signal */
2282 sigaction(SIGALRM
, &old
, NULL
);
2286 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2287 * to give cscope a chance to exit quickly. */
2289 for (waited
= 0; waited
< 40; ++waited
)
2291 pid
= waitpid(csinfo
[i
].pid
, &pstat
, WNOHANG
);
2292 waitpid_errno
= errno
;
2294 break; /* break unless the process is still running */
2295 mch_delay(50L, FALSE
); /* sleep 50 ms */
2299 * If the cscope process is still running: kill it.
2300 * Safety check: If the PID would be zero here, the entire X session
2301 * would be killed. -1 and 1 are dangerous as well.
2303 if (pid
< 0 && csinfo
[i
].pid
> 1)
2308 if (waitpid_errno
== ECHILD
)
2311 * When using 'vim -g', vim is forked and cscope process is
2312 * no longer a child process but a sibling. So waitpid()
2313 * fails with errno being ECHILD (No child processes).
2314 * Don't send SIGKILL to cscope immediately but wait
2315 * (polling) for it to exit normally as result of sending
2316 * the "q" command, hence giving it a chance to clean up
2317 * its temporary files.
2322 for (waited
= 0; waited
< 40; ++waited
)
2324 /* Check whether cscope process is still alive */
2325 if (kill(csinfo
[i
].pid
, 0) != 0)
2327 alive
= FALSE
; /* cscope process no longer exists */
2330 mch_delay(50L, FALSE
); /* sleep 50ms */
2336 kill(csinfo
[i
].pid
, SIGKILL
);
2337 (void)waitpid(csinfo
[i
].pid
, &pstat
, 0);
2342 if (csinfo
[i
].hProc
!= NULL
)
2344 /* Give cscope a chance to exit normally */
2345 if (WaitForSingleObject(csinfo
[i
].hProc
, 1000) == WAIT_TIMEOUT
)
2346 TerminateProcess(csinfo
[i
].hProc
, 0);
2347 CloseHandle(csinfo
[i
].hProc
);
2351 if (csinfo
[i
].fr_fp
!= NULL
)
2352 (void)fclose(csinfo
[i
].fr_fp
);
2353 if (csinfo
[i
].to_fp
!= NULL
)
2354 (void)fclose(csinfo
[i
].to_fp
);
2358 vim_free(csinfo
[i
].fname
);
2359 vim_free(csinfo
[i
].ppath
);
2360 vim_free(csinfo
[i
].flags
);
2364 } /* cs_release_csp */
2370 * calls cs_kill on all cscope connections then reinits
2377 char **dblist
= NULL
, **pplist
= NULL
, **fllist
= NULL
;
2379 char buf
[20]; /* for sprintf " (#%d)" */
2381 /* malloc our db and ppath list */
2382 dblist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2383 pplist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2384 fllist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2385 if (dblist
== NULL
|| pplist
== NULL
|| fllist
== NULL
)
2390 return CSCOPE_FAILURE
;
2393 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2395 dblist
[i
] = csinfo
[i
].fname
;
2396 pplist
[i
] = csinfo
[i
].ppath
;
2397 fllist
[i
] = csinfo
[i
].flags
;
2398 if (csinfo
[i
].fname
!= NULL
)
2399 cs_release_csp(i
, FALSE
);
2402 /* rebuild the cscope connection list */
2403 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2405 if (dblist
[i
] != NULL
)
2407 cs_add_common(dblist
[i
], pplist
[i
], fllist
[i
]);
2410 /* don't use smsg_attr() because we want to display the
2411 * connection number in the same line as
2412 * "Added cscope database..."
2414 sprintf(buf
, " (#%d)", i
);
2415 MSG_PUTS_ATTR(buf
, hl_attr(HLF_R
));
2418 vim_free(dblist
[i
]);
2419 vim_free(pplist
[i
]);
2420 vim_free(fllist
[i
]);
2427 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R
) | MSG_HIST
);
2428 return CSCOPE_SUCCESS
;
2433 * PRIVATE: cs_resolve_file
2435 * construct the full pathname to a file found in the cscope database.
2436 * (Prepends ppath, if there is one and if it's not already prepended,
2437 * otherwise just uses the name found.)
2439 * we need to prepend the prefix because on some cscope's (e.g., the one that
2440 * ships with Solaris 2.6), the output never has the prefix prepended.
2441 * contrast this with my development system (Digital Unix), which does.
2444 cs_resolve_file(i
, name
)
2452 * ppath is freed when we destroy the cscope connection.
2453 * fullname is freed after cs_make_vim_style_matches, after it's been
2454 * copied into the tag buffer used by vim
2456 len
= (int)(strlen(name
) + 2);
2457 if (csinfo
[i
].ppath
!= NULL
)
2458 len
+= (int)strlen(csinfo
[i
].ppath
);
2460 if ((fullname
= (char *)alloc(len
)) == NULL
)
2464 * note/example: this won't work if the cscope output already starts
2465 * "../.." and the prefix path is also "../..". if something like this
2466 * happens, you are screwed up and need to fix how you're using cscope.
2468 if (csinfo
[i
].ppath
!= NULL
&&
2469 (strncmp(name
, csinfo
[i
].ppath
, strlen(csinfo
[i
].ppath
)) != 0) &&
2472 && name
[0] != '\\' && name
[1] != ':'
2475 (void)sprintf(fullname
, "%s/%s", csinfo
[i
].ppath
, name
);
2477 (void)sprintf(fullname
, "%s", name
);
2480 } /* cs_resolve_file */
2486 * show all cscope connections
2494 if (cs_cnt_connections() == 0)
2495 MSG_PUTS(_("no cscope connections\n"));
2499 _(" # pid database name prepend path\n"),
2501 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2503 if (csinfo
[i
].fname
== NULL
)
2506 if (csinfo
[i
].ppath
!= NULL
)
2507 (void)smsg((char_u
*)"%2d %-5ld %-34s %-32s",
2508 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
, csinfo
[i
].ppath
);
2510 (void)smsg((char_u
*)"%2d %-5ld %-34s <none>",
2511 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
);
2516 return CSCOPE_SUCCESS
;
2523 * Only called when VIM exits to quit any cscope sessions.
2530 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2531 cs_release_csp(i
, TRUE
);
2534 #endif /* FEAT_CSCOPE */