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 },
86 { NULL
, NULL
, NULL
, NULL
, 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_SCSCOPE_SUBCMD
, /* expand ":scscope" sub-commands */
102 EXP_CSCOPE_FIND
, /* expand ":cscope find" arguments */
103 EXP_CSCOPE_KILL
/* expand ":cscope kill" arguments */
107 * Function given to ExpandGeneric() to obtain the cscope command
111 get_cscope_name(xp
, idx
)
120 case EXP_CSCOPE_SUBCMD
:
121 /* Complete with sub-commands of ":cscope":
122 * add, find, help, kill, reset, show */
123 return (char_u
*)cs_cmds
[idx
].name
;
124 case EXP_SCSCOPE_SUBCMD
:
125 /* Complete with sub-commands of ":scscope": same sub-commands as
126 * ":cscope" but skip commands which don't support split windows */
127 for (i
= 0, current_idx
= 0; cs_cmds
[i
].name
!= NULL
; i
++)
128 if (cs_cmds
[i
].cansplit
)
129 if (current_idx
++ == idx
)
131 return (char_u
*)cs_cmds
[i
].name
;
132 case EXP_CSCOPE_FIND
:
134 const char *query_type
[] =
136 "c", "d", "e", "f", "g", "i", "s", "t", NULL
139 /* Complete with query type of ":cscope find {query_type}".
140 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
141 * ..., 8) but only complete with letters, since numbers are
143 return (char_u
*)query_type
[idx
];
145 case EXP_CSCOPE_KILL
:
147 static char_u connection
[2];
149 /* ":cscope kill" accepts connection numbers or partial names of
150 * the pathname of the cscope database as argument. Only complete
151 * with connection numbers. -1 can also be used to kill all
153 for (i
= 0, current_idx
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
155 if (csinfo
[i
].fname
== NULL
)
157 if (current_idx
++ == idx
)
159 /* Connection number fits in one character since
160 * CSCOPE_MAX_CONNECTIONS is < 10 */
161 connection
[0] = i
+ '0';
166 return (current_idx
== idx
&& idx
> 0) ? (char_u
*)"-1" : NULL
;
174 * Handle command line completion for :cscope command.
177 set_context_in_cscope_cmd(xp
, arg
, cmdidx
)
184 /* Default: expand subcommands */
185 xp
->xp_context
= EXPAND_CSCOPE
;
186 xp
->xp_pattern
= arg
;
187 expand_what
= (cmdidx
== CMD_scscope
)
188 ? EXP_SCSCOPE_SUBCMD
: EXP_CSCOPE_SUBCMD
;
190 /* (part of) subcommand already typed */
193 p
= skiptowhite(arg
);
194 if (*p
!= NUL
) /* past first word */
196 xp
->xp_pattern
= skipwhite(p
);
197 if (*skiptowhite(xp
->xp_pattern
) != NUL
)
198 xp
->xp_context
= EXPAND_NOTHING
;
199 else if (STRNICMP(arg
, "add", p
- arg
) == 0)
200 xp
->xp_context
= EXPAND_FILES
;
201 else if (STRNICMP(arg
, "kill", p
- arg
) == 0)
202 expand_what
= EXP_CSCOPE_KILL
;
203 else if (STRNICMP(arg
, "find", p
- arg
) == 0)
204 expand_what
= EXP_CSCOPE_FIND
;
206 xp
->xp_context
= EXPAND_NOTHING
;
211 #endif /* FEAT_CMDL_COMPL */
214 * PRIVATE: do_cscope_general
216 * Find the command, print help if invalid, and then call the corresponding
220 do_cscope_general(eap
, make_split
)
222 int make_split
; /* whether to split window */
227 if ((cmdp
= cs_lookup_cmd(eap
)) == NULL
)
238 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
241 postponed_split
= -1;
242 postponed_split_flags
= cmdmod
.split
;
243 postponed_split_tab
= cmdmod
.tab
;
250 postponed_split_flags
= 0;
251 postponed_split_tab
= 0;
262 do_cscope_general(eap
, FALSE
);
268 * same as do_cscope, but splits window, too.
274 do_cscope_general(eap
, TRUE
);
289 if (*eap
->arg
== NUL
)
291 (void)EMSG(_("E562: Usage: cstag <ident>"));
298 if (cs_check_for_connections())
300 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
308 if (cs_check_for_tags())
309 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
312 else if (cs_check_for_tags())
314 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
318 if (cs_check_for_tags())
320 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
326 if (cs_check_for_connections())
328 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
,
335 else if (cs_check_for_connections())
337 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
349 (void)EMSG(_("E257: cstag: tag not found"));
350 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
361 * this simulates a vim_fgets(), but for cscope, returns the next line
362 * from the cscope output. should only be called from find_tags()
364 * returns TRUE if eof, FALSE otherwise
373 if ((p
= cs_manage_matches(NULL
, NULL
, -1, Get
)) == NULL
)
375 vim_strncpy(buf
, (char_u
*)p
, size
- 1);
382 * PUBLIC: cs_free_tags
384 * called only from do_tag(), when popping the tag stack
389 cs_manage_matches(NULL
, NULL
, -1, Free
);
394 * PUBLIC: cs_print_tags
396 * called from do_tag()
401 cs_manage_matches(NULL
, NULL
, -1, Print
);
406 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
408 * Checks for the existence of a |cscope| connection. If no
409 * parameters are specified, then the function returns:
411 * 0, if cscope was not available (not compiled in), or if there
412 * are no cscope connections; or
413 * 1, if there is at least one cscope connection.
415 * If parameters are specified, then the value of {num}
416 * determines how existence of a cscope connection is checked:
418 * {num} Description of existence check
419 * ----- ------------------------------
420 * 0 Same as no parameters (e.g., "cscope_connection()").
421 * 1 Ignore {prepend}, and use partial string matches for
423 * 2 Ignore {prepend}, and use exact string matches for
425 * 3 Use {prepend}, use partial string matches for both
426 * {dbpath} and {prepend}.
427 * 4 Use {prepend}, use exact string matches for both
428 * {dbpath} and {prepend}.
430 * Note: All string comparisons are case sensitive!
432 #if defined(FEAT_EVAL) || defined(PROTO)
434 cs_connection(num
, dbpath
, ppath
)
441 if (num
< 0 || num
> 4 || (num
> 0 && !dbpath
))
444 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
446 if (!csinfo
[i
].fname
)
455 if (strstr(csinfo
[i
].fname
, (char *)dbpath
))
459 if (strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
463 if (strstr(csinfo
[i
].fname
, (char *)dbpath
)
464 && ((!ppath
&& !csinfo
[i
].ppath
)
467 && strstr(csinfo
[i
].ppath
, (char *)ppath
))))
471 if ((strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
472 && ((!ppath
&& !csinfo
[i
].ppath
)
475 && (strcmp(csinfo
[i
].ppath
, (char *)ppath
) == 0))))
482 } /* cs_connection */
488 ****************************************************************************/
493 * add cscope database or a directory name (to look for cscope.out)
494 * to the cscope connection list
502 char *fname
, *ppath
, *flags
= NULL
;
504 if ((fname
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
507 return CSCOPE_FAILURE
;
509 if ((ppath
= strtok((char *)NULL
, (const char *)" ")) != NULL
)
510 flags
= strtok((char *)NULL
, (const char *)" ");
512 return cs_add_common(fname
, ppath
, flags
);
519 char *stat_emsg
= _("E563: stat(%s) error: %d");
520 char *buf
= (char *)alloc((unsigned)strlen(stat_emsg
) + MAXPATHL
+ 10);
524 (void)sprintf(buf
, stat_emsg
, fname
, errno
);
529 (void)EMSG(_("E563: stat error"));
534 * PRIVATE: cs_add_common
536 * the common routine to add a new cscope connection. called by
537 * cs_add() and cs_reset(). i really don't like to do this, but this
538 * routine uses a number of goto statements.
541 cs_add_common(arg1
, arg2
, flags
)
542 char *arg1
; /* filename - may contain environment variables */
543 char *arg2
; /* prepend path - may contain environment variables */
553 /* get the filename (arg1), expand it, and try to stat it */
554 if ((fname
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
557 expand_env((char_u
*)arg1
, (char_u
*)fname
, MAXPATHL
);
558 ret
= stat(fname
, &statbuf
);
567 /* get the prepend path (arg2), expand it, and try to stat it */
570 struct stat statbuf2
;
572 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
575 expand_env((char_u
*)arg2
, (char_u
*)ppath
, MAXPATHL
);
576 ret
= stat(ppath
, &statbuf2
);
581 /* if filename is a directory, append the cscope database name to it */
582 if ((statbuf
.st_mode
& S_IFMT
) == S_IFDIR
)
584 fname2
= (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE
) + strlen(fname
) + 2));
588 while (fname
[strlen(fname
)-1] == '/'
590 || fname
[strlen(fname
)-1] == '\\'
594 fname
[strlen(fname
)-1] = '\0';
595 if (strlen(fname
) == 0)
598 if (fname
[0] == '\0')
599 (void)sprintf(fname2
, "/%s", CSCOPE_DBFILE
);
601 (void)sprintf(fname2
, "%s/%s", fname
, CSCOPE_DBFILE
);
603 ret
= stat(fname2
, &statbuf
);
607 cs_stat_emsg(fname2
);
611 i
= cs_insert_filelist(fname2
, ppath
, flags
, &statbuf
);
614 else if (S_ISREG(statbuf
.st_mode
) || S_ISLNK(statbuf
.st_mode
))
616 /* WIN32 - substitute define S_ISREG from os_unix.h */
617 else if (((statbuf
.st_mode
) & S_IFMT
) == S_IFREG
)
620 i
= cs_insert_filelist(fname
, ppath
, flags
, &statbuf
);
626 _("E564: %s is not a directory or a valid cscope database"),
633 if (cs_create_connection(i
) == CSCOPE_FAILURE
634 || cs_read_prompt(i
) == CSCOPE_FAILURE
)
636 cs_release_csp(i
, TRUE
);
643 (void)smsg_attr(hl_attr(HLF_R
),
644 (char_u
*)_("Added cscope database %s"),
652 return CSCOPE_SUCCESS
;
658 return CSCOPE_FAILURE
;
659 } /* cs_add_common */
663 cs_check_for_connections()
665 return (cs_cnt_connections() > 0);
666 } /* cs_check_for_connections */
672 return (p_tags
[0] != NUL
&& curbuf
->b_p_tags
!= NULL
);
673 } /* cs_check_for_tags */
677 * PRIVATE: cs_cnt_connections
679 * count the number of cscope connections
687 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
689 if (csinfo
[i
].fname
!= NULL
)
693 } /* cs_cnt_connections */
697 int idx
; /* connection index */
699 EMSGN(_("E262: error reading cscope connection %ld"), idx
);
702 #define CSREAD_BUFSIZE 2048
704 * PRIVATE: cs_cnt_matches
706 * count the number of matches for a given cscope connection.
716 buf
= (char *)alloc(CSREAD_BUFSIZE
);
721 if (!fgets(buf
, CSREAD_BUFSIZE
, csinfo
[idx
].fr_fp
))
723 if (feof(csinfo
[idx
].fr_fp
))
726 cs_reading_emsg(idx
);
733 * If the database is out of date, or there's some other problem,
734 * cscope will output error messages before the number-of-lines output.
735 * Display/discard any output that doesn't match what we want.
736 * Accept "\S*cscope: X lines", also matches "mlcscope".
738 if ((stok
= strtok(buf
, (const char *)" ")) == NULL
)
740 if (strstr((const char *)stok
, "cscope:") == NULL
)
743 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
752 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
754 if (strncmp((const char *)stok
, "lines", 5))
762 } /* cs_cnt_matches */
766 * PRIVATE: cs_create_cmd
768 * Creates the actual cscope command query from what the user entered.
771 cs_create_cmd(csoption
, pattern
)
781 case '0' : case 's' :
784 case '1' : case 'g' :
787 case '2' : case 'd' :
790 case '3' : case 'c' :
793 case '4' : case 't' :
796 case '6' : case 'e' :
799 case '7' : case 'f' :
802 case '8' : case 'i' :
806 (void)EMSG(_("E561: unknown cscope search type"));
811 /* Skip white space before the patter, except for text and pattern search,
812 * they may want to use the leading white space. */
814 if (search
!= 4 && search
!= 6)
815 while vim_iswhite(*pat
)
818 if ((cmd
= (char *)alloc((unsigned)(strlen(pat
) + 2))) == NULL
)
821 (void)sprintf(cmd
, "%d%s", search
, pat
);
824 } /* cs_create_cmd */
828 * PRIVATE: cs_create_connection
830 * This piece of code was taken/adapted from nvi. do we need to add
831 * the BSD license notice?
834 cs_create_connection(i
)
838 int to_cs
[2], from_cs
[2];
841 char *prog
, *cmd
, *ppath
= NULL
;
844 SECURITY_ATTRIBUTES sa
;
845 PROCESS_INFORMATION pi
;
847 BOOL pipe_stdin
= FALSE
, pipe_stdout
= FALSE
;
848 HANDLE stdin_rd
, stdout_rd
;
849 HANDLE stdout_wr
, stdin_wr
;
852 # define OPEN_OH_ARGTYPE long
854 # if (_MSC_VER >= 1300)
855 # define OPEN_OH_ARGTYPE intptr_t
857 # define OPEN_OH_ARGTYPE long
864 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
865 * from_cs[0] and writes to to_cs[1].
867 to_cs
[0] = to_cs
[1] = from_cs
[0] = from_cs
[1] = -1;
868 if (pipe(to_cs
) < 0 || pipe(from_cs
) < 0)
870 (void)EMSG(_("E566: Could not create cscope pipes"));
873 (void)close(to_cs
[0]);
875 (void)close(to_cs
[1]);
876 if (from_cs
[0] != -1)
877 (void)close(from_cs
[0]);
878 if (from_cs
[1] != -1)
879 (void)close(from_cs
[1]);
880 return CSCOPE_FAILURE
;
883 switch (csinfo
[i
].pid
= fork())
886 (void)EMSG(_("E622: Could not fork for cscope"));
888 case 0: /* child: run cscope. */
889 if (dup2(to_cs
[0], STDIN_FILENO
) == -1)
890 PERROR("cs_create_connection 1");
891 if (dup2(from_cs
[1], STDOUT_FILENO
) == -1)
892 PERROR("cs_create_connection 2");
893 if (dup2(from_cs
[1], STDERR_FILENO
) == -1)
894 PERROR("cs_create_connection 3");
897 (void)close(to_cs
[1]);
898 (void)close(from_cs
[0]);
901 /* Create pipes to communicate with cscope */
902 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
903 sa
.bInheritHandle
= TRUE
;
904 sa
.lpSecurityDescriptor
= NULL
;
906 if (!(pipe_stdin
= CreatePipe(&stdin_rd
, &stdin_wr
, &sa
, 0))
907 || !(pipe_stdout
= CreatePipe(&stdout_rd
, &stdout_wr
, &sa
, 0)))
909 (void)EMSG(_("E566: Could not create cscope pipes"));
913 CloseHandle(stdin_rd
);
914 CloseHandle(stdin_wr
);
918 CloseHandle(stdout_rd
);
919 CloseHandle(stdout_wr
);
921 return CSCOPE_FAILURE
;
924 /* expand the cscope exec for env var's */
925 if ((prog
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
928 return CSCOPE_FAILURE
;
934 expand_env((char_u
*)p_csprg
, (char_u
*)prog
, MAXPATHL
);
936 /* alloc space to hold the cscope command */
937 len
= (int)(strlen(prog
) + strlen(csinfo
[i
].fname
) + 32);
940 /* expand the prepend path for env var's */
941 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
945 return CSCOPE_FAILURE
;
951 expand_env((char_u
*)csinfo
[i
].ppath
, (char_u
*)ppath
, MAXPATHL
);
953 len
+= (int)strlen(ppath
);
957 len
+= (int)strlen(csinfo
[i
].flags
);
959 if ((cmd
= (char *)alloc(len
)) == NULL
)
964 return CSCOPE_FAILURE
;
971 /* run the cscope command; is there execl for non-unix systems? */
973 (void)sprintf(cmd
, "exec %s -dl -f %s", prog
, csinfo
[i
].fname
);
976 (void)sprintf(cmd
, "%s -dl -f %s", prog
, csinfo
[i
].fname
);
978 if (csinfo
[i
].ppath
!= NULL
)
980 (void)strcat(cmd
, " -P");
981 (void)strcat(cmd
, csinfo
[i
].ppath
);
983 if (csinfo
[i
].flags
!= NULL
)
985 (void)strcat(cmd
, " ");
986 (void)strcat(cmd
, csinfo
[i
].flags
);
989 /* on Win32 we still need prog */
995 if (execl("/bin/sh", "sh", "-c", cmd
, (char *)NULL
) == -1)
996 PERROR(_("cs_create_connection exec failed"));
1000 default: /* parent. */
1002 * Save the file descriptors for later duplication, and
1003 * reopen as streams.
1005 if ((csinfo
[i
].to_fp
= fdopen(to_cs
[1], "w")) == NULL
)
1006 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1007 if ((csinfo
[i
].fr_fp
= fdopen(from_cs
[0], "r")) == NULL
)
1008 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1011 (void)close(to_cs
[0]);
1012 (void)close(from_cs
[1]);
1019 /* Create a new process to run cscope and use pipes to talk with it */
1020 GetStartupInfo(&si
);
1021 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
1022 si
.wShowWindow
= SW_HIDE
; /* Hide child application window */
1023 si
.hStdOutput
= stdout_wr
;
1024 si
.hStdError
= stdout_wr
;
1025 si
.hStdInput
= stdin_rd
;
1026 created
= CreateProcess(NULL
, cmd
, NULL
, NULL
, TRUE
, CREATE_NEW_CONSOLE
,
1027 NULL
, NULL
, &si
, &pi
);
1033 PERROR(_("cs_create_connection exec failed"));
1034 (void)EMSG(_("E623: Could not spawn cscope process"));
1038 csinfo
[i
].pid
= pi
.dwProcessId
;
1039 csinfo
[i
].hProc
= pi
.hProcess
;
1040 CloseHandle(pi
.hThread
);
1042 /* TODO - tidy up after failure to create files on pipe handles. */
1043 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdin_wr
,
1044 _O_TEXT
|_O_APPEND
)) < 0)
1045 || ((csinfo
[i
].to_fp
= _fdopen(fd
, "w")) == NULL
))
1046 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1047 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdout_rd
,
1048 _O_TEXT
|_O_RDONLY
)) < 0)
1049 || ((csinfo
[i
].fr_fp
= _fdopen(fd
, "r")) == NULL
))
1050 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1052 /* Close handles for file descriptors inherited by the cscope process */
1053 CloseHandle(stdin_rd
);
1054 CloseHandle(stdout_wr
);
1058 return CSCOPE_SUCCESS
;
1059 } /* cs_create_connection */
1065 * query cscope using command line interface. parse the output and use tselect
1066 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1068 * returns TRUE if we jump to a tag or abort, FALSE if not.
1076 if (cs_check_for_connections() == FALSE
)
1078 (void)EMSG(_("E567: no cscope connections"));
1082 if ((opt
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1088 pat
= opt
+ strlen(opt
) + 1;
1089 if (pat
>= (char *)eap
->arg
+ eap_arg_len
)
1095 return cs_find_common(opt
, pat
, eap
->forceit
, TRUE
,
1096 eap
->cmdidx
== CMD_lcscope
);
1101 * PRIVATE: cs_find_common
1103 * common code for cscope find, shared by cs_find() and do_cstag()
1106 cs_find_common(opt
, pat
, forceit
, verbose
, use_ll
)
1115 int nummatches
[CSCOPE_MAX_CONNECTIONS
], totmatches
;
1116 #ifdef FEAT_QUICKFIX
1121 /* create the actual command to send to cscope */
1122 cmd
= cs_create_cmd(opt
, pat
);
1126 /* send query to all open connections, then count the total number
1127 * of matches so we can alloc matchesp all in one swell foop
1129 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1132 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1134 if (csinfo
[i
].fname
== NULL
|| csinfo
[i
].to_fp
== NULL
)
1137 /* send cmd to cscope */
1138 (void)fprintf(csinfo
[i
].to_fp
, "%s\n", cmd
);
1139 (void)fflush(csinfo
[i
].to_fp
);
1141 nummatches
[i
] = cs_cnt_matches(i
);
1143 if (nummatches
[i
] > -1)
1144 totmatches
+= nummatches
[i
];
1146 if (nummatches
[i
] == 0)
1147 (void)cs_read_prompt(i
);
1151 if (totmatches
== 0)
1153 char *nf
= _("E259: no matches found for cscope query %s of %s");
1159 buf
= (char *)alloc((unsigned)(strlen(opt
) + strlen(pat
) + strlen(nf
)));
1164 sprintf(buf
, nf
, opt
, pat
);
1171 #ifdef FEAT_QUICKFIX
1172 /* get cmd letter */
1203 qfpos
= (char *)vim_strchr(p_csqf
, cmdletter
);
1207 /* next symbol must be + or - */
1208 if (strchr(CSQF_FLAGS
, *qfpos
) == NULL
)
1210 char *nf
= _("E469: invalid cscopequickfix flag %c for %c");
1211 char *buf
= (char *)alloc((unsigned)strlen(nf
));
1213 /* strlen will be enough because we use chars */
1216 sprintf(buf
, nf
, *qfpos
, *(qfpos
-1));
1223 if (qfpos
!= NULL
&& *qfpos
!= '0' && totmatches
> 0)
1225 /* fill error list */
1227 char_u
*tmp
= vim_tempname('c');
1228 qf_info_T
*qi
= NULL
;
1231 f
= mch_fopen((char *)tmp
, "w");
1233 EMSG2(_(e_notopen
), tmp
);
1236 cs_file_results(f
, nummatches
);
1238 if (use_ll
) /* Use location list */
1240 /* '-' starts a new error list */
1241 if (qf_init(wp
, tmp
, (char_u
*)"%f%*\\t%l%*\\t%m",
1244 # ifdef FEAT_WINDOWS
1245 if (postponed_split
!= 0)
1247 win_split(postponed_split
> 0 ? postponed_split
: 0,
1248 postponed_split_flags
);
1249 # ifdef FEAT_SCROLLBIND
1250 curwin
->w_p_scb
= FALSE
;
1252 postponed_split
= 0;
1257 * In the location list window, use the displayed location
1258 * list. Otherwise, use the location list for the window.
1260 qi
= (bt_quickfix(wp
->w_buffer
) && wp
->w_llist_ref
!= NULL
)
1261 ? wp
->w_llist_ref
: wp
->w_llist
;
1262 qf_jump(qi
, 0, 0, forceit
);
1270 #endif /* FEAT_QUICKFIX */
1272 char **matches
= NULL
, **contexts
= NULL
;
1276 cs_fill_results((char *)pat
, totmatches
, nummatches
, &matches
,
1277 &contexts
, &matched
);
1278 if (matches
== NULL
)
1281 (void)cs_manage_matches(matches
, contexts
, matched
, Store
);
1283 return do_tag((char_u
*)pat
, DT_CSCOPE
, 0, forceit
, verbose
);
1286 } /* cs_find_common */
1295 exarg_T
*eap UNUSED
;
1297 cscmd_T
*cmdp
= cs_cmds
;
1299 (void)MSG_PUTS(_("cscope commands:\n"));
1300 while (cmdp
->name
!= NULL
)
1302 char *help
= _(cmdp
->help
);
1303 int space_cnt
= 30 - vim_strsize((char_u
*)help
);
1305 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1308 (void)smsg((char_u
*)_("%-5s: %s%*s (Usage: %s)"),
1310 help
, space_cnt
, " ",
1312 if (strcmp(cmdp
->name
, "find") == 0)
1314 " c: Find functions calling this function\n"
1315 " d: Find functions called by this function\n"
1316 " e: Find this egrep pattern\n"
1317 " f: Find this file\n"
1318 " g: Find this definition\n"
1319 " i: Find files #including this file\n"
1320 " s: Find this C symbol\n"
1321 " t: Find assignments to\n"));
1334 * initialize cscope structure if not already
1340 static int init_already
= FALSE
;
1345 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1348 init_already
= TRUE
;
1355 csinfo
[i
].fname
= NULL
;
1356 csinfo
[i
].ppath
= NULL
;
1357 csinfo
[i
].flags
= NULL
;
1359 csinfo
[i
].st_dev
= (dev_t
)0;
1360 csinfo
[i
].st_ino
= (ino_t
)0;
1362 csinfo
[i
].nVolume
= 0;
1363 csinfo
[i
].nIndexHigh
= 0;
1364 csinfo
[i
].nIndexLow
= 0;
1367 csinfo
[i
].fr_fp
= NULL
;
1368 csinfo
[i
].to_fp
= NULL
;
1370 csinfo
[i
].hProc
= NULL
;
1375 static char *GetWin32Error
__ARGS((void));
1381 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
1382 NULL
, GetLastError(), 0, (LPSTR
)&msg
, 0, NULL
);
1385 /* remove trailing \r\n */
1386 char *pcrlf
= strstr(msg
, "\r\n");
1395 * PRIVATE: cs_insert_filelist
1397 * insert a new cscope database filename into the filelist
1400 cs_insert_filelist(fname
, ppath
, flags
, sb
)
1404 struct stat
*sb UNUSED
;
1409 BY_HANDLE_FILE_INFORMATION bhfi
;
1411 vim_memset(&bhfi
, 0, sizeof(bhfi
));
1412 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1413 if (!mch_windows95())
1415 hFile
= CreateFile(fname
, FILE_READ_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
1416 FILE_ATTRIBUTE_NORMAL
, NULL
);
1417 if (hFile
== INVALID_HANDLE_VALUE
)
1421 char *cant_msg
= _("E625: cannot open cscope database: %s");
1422 char *winmsg
= GetWin32Error();
1426 (void)EMSG2(cant_msg
, winmsg
);
1430 /* subst filename if can't get error text */
1431 (void)EMSG2(cant_msg
, fname
);
1435 if (!GetFileInformationByHandle(hFile
, &bhfi
))
1439 (void)EMSG(_("E626: cannot get cscope database information"));
1446 i
= -1; /* can be set to the index of an empty item in csinfo */
1447 for (j
= 0; j
< CSCOPE_MAX_CONNECTIONS
; j
++)
1449 if (csinfo
[j
].fname
!= NULL
1451 && csinfo
[j
].st_dev
== sb
->st_dev
&& csinfo
[j
].st_ino
== sb
->st_ino
1453 /* compare pathnames first */
1454 && ((fullpathcmp(csinfo
[j
].fname
, fname
, FALSE
) & FPC_SAME
)
1455 /* if not Windows 9x, test index file attributes too */
1456 || (!mch_windows95()
1457 && csinfo
[j
].nVolume
== bhfi
.dwVolumeSerialNumber
1458 && csinfo
[j
].nIndexHigh
== bhfi
.nFileIndexHigh
1459 && csinfo
[j
].nIndexLow
== bhfi
.nFileIndexLow
))
1464 (void)EMSG(_("E568: duplicate cscope database not added"));
1468 if (csinfo
[j
].fname
== NULL
&& i
== -1)
1469 i
= j
; /* remember first empty entry */
1475 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1479 if ((csinfo
[i
].fname
= (char *)alloc((unsigned)strlen(fname
)+1)) == NULL
)
1482 (void)strcpy(csinfo
[i
].fname
, (const char *)fname
);
1486 if ((csinfo
[i
].ppath
= (char *)alloc((unsigned)strlen(ppath
) + 1)) == NULL
)
1488 vim_free(csinfo
[i
].fname
);
1489 csinfo
[i
].fname
= NULL
;
1492 (void)strcpy(csinfo
[i
].ppath
, (const char *)ppath
);
1494 csinfo
[i
].ppath
= NULL
;
1498 if ((csinfo
[i
].flags
= (char *)alloc((unsigned)strlen(flags
) + 1)) == NULL
)
1500 vim_free(csinfo
[i
].fname
);
1501 vim_free(csinfo
[i
].ppath
);
1502 csinfo
[i
].fname
= NULL
;
1503 csinfo
[i
].ppath
= NULL
;
1506 (void)strcpy(csinfo
[i
].flags
, (const char *)flags
);
1508 csinfo
[i
].flags
= NULL
;
1511 csinfo
[i
].st_dev
= sb
->st_dev
;
1512 csinfo
[i
].st_ino
= sb
->st_ino
;
1515 csinfo
[i
].nVolume
= bhfi
.dwVolumeSerialNumber
;
1516 csinfo
[i
].nIndexLow
= bhfi
.nFileIndexLow
;
1517 csinfo
[i
].nIndexHigh
= bhfi
.nFileIndexHigh
;
1520 } /* cs_insert_filelist */
1524 * PRIVATE: cs_lookup_cmd
1526 * find cscope command in command table
1536 if (eap
->arg
== NULL
)
1539 /* Store length of eap->arg before it gets modified by strtok(). */
1540 eap_arg_len
= (int)STRLEN(eap
->arg
);
1542 if ((stok
= strtok((char *)(eap
->arg
), (const char *)" ")) == NULL
)
1546 for (cmdp
= cs_cmds
; cmdp
->name
!= NULL
; ++cmdp
)
1548 if (strncmp((const char *)(stok
), cmdp
->name
, len
) == 0)
1552 } /* cs_lookup_cmd */
1562 exarg_T
*eap UNUSED
;
1567 if ((stok
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1570 return CSCOPE_FAILURE
;
1573 /* only single digit positive and negative integers are allowed */
1574 if ((strlen(stok
) < 2 && VIM_ISDIGIT((int)(stok
[0])))
1575 || (strlen(stok
) < 3 && stok
[0] == '-'
1576 && VIM_ISDIGIT((int)(stok
[1]))))
1580 /* It must be part of a name. We will try to find a match
1581 * within all the names in the csinfo data structure
1583 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1585 if (csinfo
[i
].fname
!= NULL
&& strstr(csinfo
[i
].fname
, stok
))
1590 if ((i
>= CSCOPE_MAX_CONNECTIONS
|| i
< -1 || csinfo
[i
].fname
== NULL
)
1594 (void)EMSG2(_("E261: cscope connection %s not found"), stok
);
1600 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1602 if (csinfo
[i
].fname
)
1603 cs_kill_execute(i
, csinfo
[i
].fname
);
1607 cs_kill_execute(i
, stok
);
1615 * PRIVATE: cs_kill_execute
1617 * Actually kills a specific cscope connection.
1620 cs_kill_execute(i
, cname
)
1621 int i
; /* cscope table index */
1622 char *cname
; /* cscope database name */
1627 (void)smsg_attr(hl_attr(HLF_R
) | MSG_HIST
,
1628 (char_u
*)_("cscope connection %s closed"), cname
);
1630 cs_release_csp(i
, TRUE
);
1635 * PRIVATE: cs_make_vim_style_matches
1637 * convert the cscope output into into a ctags style entry (as might be found
1638 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1639 * the type of the tag you are looking for. for example, in Darren Hiebert's
1640 * ctags (the one that comes with vim), #define's use a line number to find the
1641 * tag in a file while function definitions use a regexp search pattern.
1643 * i'm going to always use the line number because cscope does something
1644 * quirky (and probably other things i don't know about):
1646 * if you have "# define" in your source file, which is
1647 * perfectly legal, cscope thinks you have "#define". this
1648 * will result in a failed regexp search. :(
1650 * besides, even if this particular case didn't happen, the search pattern
1651 * would still have to be modified to escape all the special regular expression
1652 * characters to comply with ctags formatting.
1655 cs_make_vim_style_matches(fname
, slno
, search
, tagstr
)
1661 /* vim style is ctags:
1663 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1665 * but as mentioned above, we'll always use the line number and
1666 * put the search pattern (if one exists) as "extra"
1668 * buf is used as part of vim's method of handling tags, and
1669 * (i think) vim frees it when you pop your tags and get replaced
1670 * by new ones on the tag stack.
1677 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + strlen(search
)+6);
1678 if ((buf
= (char *)alloc(amt
)) == NULL
)
1681 (void)sprintf(buf
, "%s\t%s\t%s;\"\t%s", tagstr
, fname
, slno
, search
);
1685 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + 5);
1686 if ((buf
= (char *)alloc(amt
)) == NULL
)
1689 (void)sprintf(buf
, "%s\t%s\t%s;\"", tagstr
, fname
, slno
);
1693 } /* cs_make_vim_style_matches */
1697 * PRIVATE: cs_manage_matches
1699 * this is kind of hokey, but i don't see an easy way round this..
1701 * Store: keep a ptr to the (malloc'd) memory of matches originally
1702 * generated from cs_find(). the matches are originally lines directly
1703 * from cscope output, but transformed to look like something out of a
1704 * ctags. see cs_make_vim_style_matches for more details.
1706 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1707 * the next line from the cscope output. it basically keeps track of which
1708 * lines have been "used" and returns the next one.
1710 * Free: frees up everything and resets
1712 * Print: prints the tags
1715 cs_manage_matches(matches
, contexts
, totmatches
, cmd
)
1721 static char **mp
= NULL
;
1722 static char **cp
= NULL
;
1723 static int cnt
= -1;
1724 static int next
= -1;
1730 assert(matches
!= NULL
);
1731 assert(totmatches
> 0);
1732 if (mp
!= NULL
|| cp
!= NULL
)
1733 (void)cs_manage_matches(NULL
, NULL
, -1, Free
);
1765 cs_print_tags_priv(mp
, cp
, cnt
);
1767 default: /* should not reach here */
1768 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1773 } /* cs_manage_matches */
1777 * PRIVATE: cs_parse_results
1779 * parse cscope output
1782 cs_parse_results(cnumber
, buf
, bufsize
, context
, linenumber
, search
)
1794 if (fgets(buf
, bufsize
, csinfo
[cnumber
].fr_fp
) == NULL
)
1796 if (feof(csinfo
[cnumber
].fr_fp
))
1799 cs_reading_emsg(cnumber
);
1804 /* If the line's too long for the buffer, discard it. */
1805 if ((p
= strchr(buf
, '\n')) == NULL
)
1807 while ((ch
= getc(csinfo
[cnumber
].fr_fp
)) != EOF
&& ch
!= '\n')
1814 * cscope output is in the following format:
1816 * <filename> <context> <line number> <pattern>
1818 if ((name
= strtok((char *)buf
, (const char *)" ")) == NULL
)
1820 if ((*context
= strtok(NULL
, (const char *)" ")) == NULL
)
1822 if ((*linenumber
= strtok(NULL
, (const char *)" ")) == NULL
)
1824 *search
= *linenumber
+ strlen(*linenumber
) + 1; /* +1 to skip \0 */
1827 * If the file is older than the cscope database, that is,
1828 * the database was built since the file was last modified,
1829 * or there wasn't a search string, use the line number.
1831 if (strcmp(*search
, "<unknown>") == 0)
1834 name
= cs_resolve_file(cnumber
, name
);
1838 #ifdef FEAT_QUICKFIX
1840 * PRIVATE: cs_file_results
1842 * write cscope find results to file
1845 cs_file_results(f
, nummatches_a
)
1851 char *search
, *slno
;
1856 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1860 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1862 if (nummatches_a
[i
] < 1)
1865 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1867 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1868 &slno
, &search
)) == NULL
)
1871 context
= (char *)alloc((unsigned)strlen(cntx
)+5);
1872 if (context
== NULL
)
1875 if (strcmp(cntx
, "<global>")==0)
1876 strcpy(context
, "<<global>>");
1878 sprintf(context
, "<<%s>>", cntx
);
1881 fprintf(f
, "%s\t%s\t%s\n", fullname
, slno
, context
);
1883 fprintf(f
, "%s\t%s\t%s %s\n", fullname
, slno
, context
, search
);
1887 } /* for all matches */
1889 (void)cs_read_prompt(i
);
1891 } /* for all cscope connections */
1897 * PRIVATE: cs_fill_results
1899 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1901 * When there are no matches sets "*matches_p" to NULL.
1904 cs_fill_results(tagstr
, totmatches
, nummatches_a
, matches_p
, cntxts_p
, matched
)
1914 char *search
, *slno
;
1916 char **matches
= NULL
;
1917 char **cntxts
= NULL
;
1921 assert(totmatches
> 0);
1923 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1927 if ((matches
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1929 if ((cntxts
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1932 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1934 if (nummatches_a
[i
] < 1)
1937 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1939 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1940 &slno
, &search
)) == NULL
)
1943 matches
[totsofar
] = cs_make_vim_style_matches(fullname
, slno
,
1948 if (strcmp(cntx
, "<global>") == 0)
1949 cntxts
[totsofar
] = NULL
;
1951 /* note: if vim_strsave returns NULL, then the context
1952 * will be "<global>", which is misleading.
1954 cntxts
[totsofar
] = (char *)vim_strsave((char_u
*)cntx
);
1956 if (matches
[totsofar
] != NULL
)
1959 } /* for all matches */
1961 (void)cs_read_prompt(i
);
1963 } /* for all cscope connections */
1968 /* No matches, free the arrays and return NULL in "*matches_p". */
1974 *matched
= totsofar
;
1975 *matches_p
= matches
;
1979 } /* cs_fill_results */
1982 /* get the requested path components */
1984 cs_pathcomponents(path
)
1993 s
= path
+ strlen(path
) - 1;
1994 for (i
= 0; i
< p_cspc
; ++i
)
1995 while (s
> path
&& *--s
!= '/'
2001 if ((s
> path
&& *s
== '/')
2003 || (s
> path
&& *s
== '\\')
2011 * PRIVATE: cs_print_tags_priv
2013 * called from cs_manage_matches()
2016 cs_print_tags_priv(matches
, cntxts
, num_matches
)
2022 int bufsize
= 0; /* Track available bufsize */
2025 char *fname
, *lno
, *extra
, *tbuf
;
2027 char *globalcntx
= "GLOBAL";
2028 char *cntxformat
= " <<%s>>";
2030 char *cstag_msg
= _("Cscope tag: %s");
2031 char *csfmt_str
= "%4d %6s ";
2033 assert (num_matches
> 0);
2035 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[0]) + 1)) == NULL
)
2038 strcpy(tbuf
, matches
[0]);
2039 ptag
= strtok(tbuf
, "\t");
2041 newsize
= (int)(strlen(cstag_msg
) + strlen(ptag
));
2042 buf
= (char *)alloc(newsize
);
2046 (void)sprintf(buf
, cstag_msg
, ptag
);
2047 MSG_PUTS_ATTR(buf
, hl_attr(HLF_T
));
2052 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T
)); /* strlen is 7 */
2053 msg_advance(msg_col
+ 2);
2054 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T
));
2057 for (i
= 0; i
< num_matches
; i
++)
2061 /* if we really wanted to, we could avoid this malloc and strcpy
2062 * by parsing matches[i] on the fly and placing stuff into buf
2063 * directly, but that's too much of a hassle
2065 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[idx
]) + 1)) == NULL
)
2067 (void)strcpy(tbuf
, matches
[idx
]);
2069 if ((fname
= strtok(tbuf
, (const char *)"\t")) == NULL
)
2071 if ((fname
= strtok(NULL
, (const char *)"\t")) == NULL
)
2073 if ((lno
= strtok(NULL
, (const char *)"\t")) == NULL
)
2075 extra
= strtok(NULL
, (const char *)"\t");
2077 lno
[strlen(lno
)-2] = '\0'; /* ignore ;" at the end */
2079 /* hopefully 'num' (num of matches) will be less than 10^16 */
2080 newsize
= (int)(strlen(csfmt_str
) + 16 + strlen(lno
));
2081 if (bufsize
< newsize
)
2083 buf
= (char *)vim_realloc(buf
, newsize
);
2091 /* csfmt_str = "%4d %6s "; */
2092 (void)sprintf(buf
, csfmt_str
, num
, lno
);
2093 MSG_PUTS_ATTR(buf
, hl_attr(HLF_CM
));
2095 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname
), hl_attr(HLF_CM
));
2097 /* compute the required space for the context */
2098 if (cntxts
[idx
] != NULL
)
2099 context
= cntxts
[idx
];
2101 context
= globalcntx
;
2102 newsize
= (int)(strlen(context
) + strlen(cntxformat
));
2104 if (bufsize
< newsize
)
2106 buf
= (char *)vim_realloc(buf
, newsize
);
2114 (void)sprintf(buf
, cntxformat
, context
);
2116 /* print the context only if it fits on the same line */
2117 if (msg_col
+ (int)strlen(buf
) >= (int)Columns
)
2126 MSG_PUTS_LONG(extra
);
2129 vim_free(tbuf
); /* only after printing extra due to strtok use */
2137 got_int
= FALSE
; /* don't print any more matches */
2142 } /* for all matches */
2145 } /* cs_print_tags_priv */
2149 * PRIVATE: cs_read_prompt
2151 * read a cscope prompt (basically, skip over the ">> ")
2158 char *buf
= NULL
; /* buffer for possible error message from cscope */
2162 static char *eprompt
= "Press the RETURN key to continue:";
2163 int epromptlen
= (int)strlen(eprompt
);
2166 cs_emsg
= _("E609: Cscope error: %s");
2167 /* compute maximum allowed len for Cscope error message */
2168 maxlen
= (int)(IOSIZE
- strlen(cs_emsg
));
2172 while ((ch
= getc(csinfo
[i
].fr_fp
)) != EOF
&& ch
!= CSCOPE_PROMPT
[0])
2173 /* if there is room and char is printable */
2174 if (bufpos
< maxlen
- 1 && vim_isprintc(ch
))
2176 if (buf
== NULL
) /* lazy buffer allocation */
2177 buf
= (char *)alloc(maxlen
);
2180 /* append character to the message */
2183 if (bufpos
>= epromptlen
2184 && strcmp(&buf
[bufpos
- epromptlen
], eprompt
) == 0)
2186 /* remove eprompt from buf */
2187 buf
[bufpos
- epromptlen
] = NUL
;
2189 /* print message to user */
2190 (void)EMSG2(cs_emsg
, buf
);
2192 /* send RETURN to cscope */
2193 (void)putc('\n', csinfo
[i
].to_fp
);
2194 (void)fflush(csinfo
[i
].to_fp
);
2203 for (n
= 0; n
< (int)strlen(CSCOPE_PROMPT
); ++n
)
2206 ch
= getc(csinfo
[i
].fr_fp
);
2209 PERROR("cs_read_prompt EOF");
2210 if (buf
!= NULL
&& buf
[0] != NUL
)
2211 (void)EMSG2(cs_emsg
, buf
);
2212 else if (p_csverbose
)
2213 cs_reading_emsg(i
); /* don't have additional information */
2214 cs_release_csp(i
, TRUE
);
2216 return CSCOPE_FAILURE
;
2219 if (ch
!= CSCOPE_PROMPT
[n
])
2227 continue; /* didn't find the prompt */
2228 break; /* did find the prompt */
2232 return CSCOPE_SUCCESS
;
2235 #if defined(UNIX) && defined(SIGALRM)
2237 * Used to catch and ignore SIGALRM below.
2240 sig_handler
SIGDEFARG(sigarg
)
2248 * PRIVATE: cs_release_csp
2250 * Does the actual free'ing for the cs ptr with an optional flag of whether
2251 * or not to free the filename. Called by cs_kill and cs_reset.
2254 cs_release_csp(i
, freefnpp
)
2259 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2261 if (csinfo
[i
].to_fp
!= NULL
)
2263 (void)fputs("q\n", csinfo
[i
].to_fp
);
2264 (void)fflush(csinfo
[i
].to_fp
);
2272 # if defined(HAVE_SIGACTION)
2273 struct sigaction sa
, old
;
2275 /* Use sigaction() to limit the waiting time to two seconds. */
2276 sigemptyset(&sa
.sa_mask
);
2277 sa
.sa_handler
= sig_handler
;
2278 sa
.sa_flags
= SA_NODEFER
;
2279 sigaction(SIGALRM
, &sa
, &old
);
2280 alarm(2); /* 2 sec timeout */
2282 /* Block until cscope exits or until timer expires */
2283 pid
= waitpid(csinfo
[i
].pid
, &pstat
, 0);
2284 waitpid_errno
= errno
;
2286 /* cancel pending alarm if still there and restore signal */
2288 sigaction(SIGALRM
, &old
, NULL
);
2292 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2293 * to give cscope a chance to exit quickly. */
2295 for (waited
= 0; waited
< 40; ++waited
)
2297 pid
= waitpid(csinfo
[i
].pid
, &pstat
, WNOHANG
);
2298 waitpid_errno
= errno
;
2300 break; /* break unless the process is still running */
2301 mch_delay(50L, FALSE
); /* sleep 50 ms */
2305 * If the cscope process is still running: kill it.
2306 * Safety check: If the PID would be zero here, the entire X session
2307 * would be killed. -1 and 1 are dangerous as well.
2309 if (pid
< 0 && csinfo
[i
].pid
> 1)
2314 if (waitpid_errno
== ECHILD
)
2317 * When using 'vim -g', vim is forked and cscope process is
2318 * no longer a child process but a sibling. So waitpid()
2319 * fails with errno being ECHILD (No child processes).
2320 * Don't send SIGKILL to cscope immediately but wait
2321 * (polling) for it to exit normally as result of sending
2322 * the "q" command, hence giving it a chance to clean up
2323 * its temporary files.
2328 for (waited
= 0; waited
< 40; ++waited
)
2330 /* Check whether cscope process is still alive */
2331 if (kill(csinfo
[i
].pid
, 0) != 0)
2333 alive
= FALSE
; /* cscope process no longer exists */
2336 mch_delay(50L, FALSE
); /* sleep 50ms */
2342 kill(csinfo
[i
].pid
, SIGKILL
);
2343 (void)waitpid(csinfo
[i
].pid
, &pstat
, 0);
2348 if (csinfo
[i
].hProc
!= NULL
)
2350 /* Give cscope a chance to exit normally */
2351 if (WaitForSingleObject(csinfo
[i
].hProc
, 1000) == WAIT_TIMEOUT
)
2352 TerminateProcess(csinfo
[i
].hProc
, 0);
2353 CloseHandle(csinfo
[i
].hProc
);
2357 if (csinfo
[i
].fr_fp
!= NULL
)
2358 (void)fclose(csinfo
[i
].fr_fp
);
2359 if (csinfo
[i
].to_fp
!= NULL
)
2360 (void)fclose(csinfo
[i
].to_fp
);
2364 vim_free(csinfo
[i
].fname
);
2365 vim_free(csinfo
[i
].ppath
);
2366 vim_free(csinfo
[i
].flags
);
2370 } /* cs_release_csp */
2376 * calls cs_kill on all cscope connections then reinits
2380 exarg_T
*eap UNUSED
;
2382 char **dblist
= NULL
, **pplist
= NULL
, **fllist
= NULL
;
2384 char buf
[20]; /* for sprintf " (#%d)" */
2386 /* malloc our db and ppath list */
2387 dblist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2388 pplist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2389 fllist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2390 if (dblist
== NULL
|| pplist
== NULL
|| fllist
== NULL
)
2395 return CSCOPE_FAILURE
;
2398 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2400 dblist
[i
] = csinfo
[i
].fname
;
2401 pplist
[i
] = csinfo
[i
].ppath
;
2402 fllist
[i
] = csinfo
[i
].flags
;
2403 if (csinfo
[i
].fname
!= NULL
)
2404 cs_release_csp(i
, FALSE
);
2407 /* rebuild the cscope connection list */
2408 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2410 if (dblist
[i
] != NULL
)
2412 cs_add_common(dblist
[i
], pplist
[i
], fllist
[i
]);
2415 /* don't use smsg_attr() because we want to display the
2416 * connection number in the same line as
2417 * "Added cscope database..."
2419 sprintf(buf
, " (#%d)", i
);
2420 MSG_PUTS_ATTR(buf
, hl_attr(HLF_R
));
2423 vim_free(dblist
[i
]);
2424 vim_free(pplist
[i
]);
2425 vim_free(fllist
[i
]);
2432 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R
) | MSG_HIST
);
2433 return CSCOPE_SUCCESS
;
2438 * PRIVATE: cs_resolve_file
2440 * construct the full pathname to a file found in the cscope database.
2441 * (Prepends ppath, if there is one and if it's not already prepended,
2442 * otherwise just uses the name found.)
2444 * we need to prepend the prefix because on some cscope's (e.g., the one that
2445 * ships with Solaris 2.6), the output never has the prefix prepended.
2446 * contrast this with my development system (Digital Unix), which does.
2449 cs_resolve_file(i
, name
)
2457 * ppath is freed when we destroy the cscope connection.
2458 * fullname is freed after cs_make_vim_style_matches, after it's been
2459 * copied into the tag buffer used by vim
2461 len
= (int)(strlen(name
) + 2);
2462 if (csinfo
[i
].ppath
!= NULL
)
2463 len
+= (int)strlen(csinfo
[i
].ppath
);
2465 if ((fullname
= (char *)alloc(len
)) == NULL
)
2469 * note/example: this won't work if the cscope output already starts
2470 * "../.." and the prefix path is also "../..". if something like this
2471 * happens, you are screwed up and need to fix how you're using cscope.
2473 if (csinfo
[i
].ppath
!= NULL
&&
2474 (strncmp(name
, csinfo
[i
].ppath
, strlen(csinfo
[i
].ppath
)) != 0) &&
2477 && name
[0] != '\\' && name
[1] != ':'
2480 (void)sprintf(fullname
, "%s/%s", csinfo
[i
].ppath
, name
);
2482 (void)sprintf(fullname
, "%s", name
);
2485 } /* cs_resolve_file */
2491 * show all cscope connections
2495 exarg_T
*eap UNUSED
;
2498 if (cs_cnt_connections() == 0)
2499 MSG_PUTS(_("no cscope connections\n"));
2503 _(" # pid database name prepend path\n"),
2505 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2507 if (csinfo
[i
].fname
== NULL
)
2510 if (csinfo
[i
].ppath
!= NULL
)
2511 (void)smsg((char_u
*)"%2d %-5ld %-34s %-32s",
2512 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
, csinfo
[i
].ppath
);
2514 (void)smsg((char_u
*)"%2d %-5ld %-34s <none>",
2515 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
);
2520 return CSCOPE_SUCCESS
;
2527 * Only called when VIM exits to quit any cscope sessions.
2534 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2535 cs_release_csp(i
, TRUE
);
2538 #endif /* FEAT_CSCOPE */