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_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
112 get_cscope_name(xp
, idx
)
121 case EXP_CSCOPE_SUBCMD
:
122 /* Complete with sub-commands of ":cscope":
123 * add, find, help, kill, reset, show */
124 return (char_u
*)cs_cmds
[idx
].name
;
125 case EXP_SCSCOPE_SUBCMD
:
126 /* Complete with sub-commands of ":scscope": same sub-commands as
127 * ":cscope" but skip commands which don't support split windows */
128 for (i
= 0, current_idx
= 0; cs_cmds
[i
].name
!= NULL
; i
++)
129 if (cs_cmds
[i
].cansplit
)
130 if (current_idx
++ == idx
)
132 return (char_u
*)cs_cmds
[i
].name
;
133 case EXP_CSCOPE_FIND
:
135 const char *query_type
[] =
137 "c", "d", "e", "f", "g", "i", "s", "t", NULL
140 /* Complete with query type of ":cscope find {query_type}".
141 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
142 * ..., 8) but only complete with letters, since numbers are
144 return (char_u
*)query_type
[idx
];
146 case EXP_CSCOPE_KILL
:
148 static char_u connection
[2];
150 /* ":cscope kill" accepts connection numbers or partial names of
151 * the pathname of the cscope database as argument. Only complete
152 * with connection numbers. -1 can also be used to kill all
154 for (i
= 0, current_idx
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
156 if (csinfo
[i
].fname
== NULL
)
158 if (current_idx
++ == idx
)
160 /* Connection number fits in one character since
161 * CSCOPE_MAX_CONNECTIONS is < 10 */
162 connection
[0] = i
+ '0';
167 return (current_idx
== idx
&& idx
> 0) ? (char_u
*)"-1" : NULL
;
175 * Handle command line completion for :cscope command.
178 set_context_in_cscope_cmd(xp
, arg
, cmdidx
)
185 /* Default: expand subcommands */
186 xp
->xp_context
= EXPAND_CSCOPE
;
187 xp
->xp_pattern
= arg
;
188 expand_what
= (cmdidx
== CMD_scscope
)
189 ? EXP_SCSCOPE_SUBCMD
: EXP_CSCOPE_SUBCMD
;
191 /* (part of) subcommand already typed */
194 p
= skiptowhite(arg
);
195 if (*p
!= NUL
) /* past first word */
197 xp
->xp_pattern
= skipwhite(p
);
198 if (*skiptowhite(xp
->xp_pattern
) != NUL
)
199 xp
->xp_context
= EXPAND_NOTHING
;
200 else if (STRNICMP(arg
, "add", p
- arg
) == 0)
201 xp
->xp_context
= EXPAND_FILES
;
202 else if (STRNICMP(arg
, "kill", p
- arg
) == 0)
203 expand_what
= EXP_CSCOPE_KILL
;
204 else if (STRNICMP(arg
, "find", p
- arg
) == 0)
205 expand_what
= EXP_CSCOPE_FIND
;
207 xp
->xp_context
= EXPAND_NOTHING
;
212 #endif /* FEAT_CMDL_COMPL */
215 * PRIVATE: do_cscope_general
217 * Find the command, print help if invalid, and then call the corresponding
221 do_cscope_general(eap
, make_split
)
223 int make_split
; /* whether to split window */
228 if ((cmdp
= cs_lookup_cmd(eap
)) == NULL
)
239 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
242 postponed_split
= -1;
243 postponed_split_flags
= cmdmod
.split
;
244 postponed_split_tab
= cmdmod
.tab
;
251 postponed_split_flags
= 0;
252 postponed_split_tab
= 0;
263 do_cscope_general(eap
, FALSE
);
269 * same as do_cscope, but splits window, too.
275 do_cscope_general(eap
, TRUE
);
290 if (*eap
->arg
== NUL
)
292 (void)EMSG(_("E562: Usage: cstag <ident>"));
299 if (cs_check_for_connections())
301 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
309 if (cs_check_for_tags())
310 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
313 else if (cs_check_for_tags())
315 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
319 if (cs_check_for_tags())
321 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
327 if (cs_check_for_connections())
329 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
,
336 else if (cs_check_for_connections())
338 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
350 (void)EMSG(_("E257: cstag: tag not found"));
351 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
362 * this simulates a vim_fgets(), but for cscope, returns the next line
363 * from the cscope output. should only be called from find_tags()
365 * returns TRUE if eof, FALSE otherwise
374 if ((p
= cs_manage_matches(NULL
, NULL
, -1, Get
)) == NULL
)
376 vim_strncpy(buf
, (char_u
*)p
, size
- 1);
383 * PUBLIC: cs_free_tags
385 * called only from do_tag(), when popping the tag stack
390 cs_manage_matches(NULL
, NULL
, -1, Free
);
395 * PUBLIC: cs_print_tags
397 * called from do_tag()
402 cs_manage_matches(NULL
, NULL
, -1, Print
);
407 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
409 * Checks for the existence of a |cscope| connection. If no
410 * parameters are specified, then the function returns:
412 * 0, if cscope was not available (not compiled in), or if there
413 * are no cscope connections; or
414 * 1, if there is at least one cscope connection.
416 * If parameters are specified, then the value of {num}
417 * determines how existence of a cscope connection is checked:
419 * {num} Description of existence check
420 * ----- ------------------------------
421 * 0 Same as no parameters (e.g., "cscope_connection()").
422 * 1 Ignore {prepend}, and use partial string matches for
424 * 2 Ignore {prepend}, and use exact string matches for
426 * 3 Use {prepend}, use partial string matches for both
427 * {dbpath} and {prepend}.
428 * 4 Use {prepend}, use exact string matches for both
429 * {dbpath} and {prepend}.
431 * Note: All string comparisons are case sensitive!
433 #if defined(FEAT_EVAL) || defined(PROTO)
435 cs_connection(num
, dbpath
, ppath
)
442 if (num
< 0 || num
> 4 || (num
> 0 && !dbpath
))
445 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
447 if (!csinfo
[i
].fname
)
456 if (strstr(csinfo
[i
].fname
, (char *)dbpath
))
460 if (strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
464 if (strstr(csinfo
[i
].fname
, (char *)dbpath
)
465 && ((!ppath
&& !csinfo
[i
].ppath
)
468 && strstr(csinfo
[i
].ppath
, (char *)ppath
))))
472 if ((strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
473 && ((!ppath
&& !csinfo
[i
].ppath
)
476 && (strcmp(csinfo
[i
].ppath
, (char *)ppath
) == 0))))
483 } /* cs_connection */
489 ****************************************************************************/
494 * add cscope database or a directory name (to look for cscope.out)
495 * to the cscope connection list
504 char *fname
, *ppath
, *flags
= NULL
;
506 if ((fname
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
509 return CSCOPE_FAILURE
;
511 if ((ppath
= strtok((char *)NULL
, (const char *)" ")) != NULL
)
512 flags
= strtok((char *)NULL
, (const char *)" ");
514 return cs_add_common(fname
, ppath
, flags
);
521 char *stat_emsg
= _("E563: stat(%s) error: %d");
522 char *buf
= (char *)alloc((unsigned)strlen(stat_emsg
) + MAXPATHL
+ 10);
526 (void)sprintf(buf
, stat_emsg
, fname
, errno
);
531 (void)EMSG(_("E563: stat error"));
536 * PRIVATE: cs_add_common
538 * the common routine to add a new cscope connection. called by
539 * cs_add() and cs_reset(). i really don't like to do this, but this
540 * routine uses a number of goto statements.
543 cs_add_common(arg1
, arg2
, flags
)
544 char *arg1
; /* filename - may contain environment variables */
545 char *arg2
; /* prepend path - may contain environment variables */
555 /* get the filename (arg1), expand it, and try to stat it */
556 if ((fname
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
559 expand_env((char_u
*)arg1
, (char_u
*)fname
, MAXPATHL
);
560 ret
= stat(fname
, &statbuf
);
569 /* get the prepend path (arg2), expand it, and try to stat it */
572 struct stat statbuf2
;
574 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
577 expand_env((char_u
*)arg2
, (char_u
*)ppath
, MAXPATHL
);
578 ret
= stat(ppath
, &statbuf2
);
583 /* if filename is a directory, append the cscope database name to it */
584 if ((statbuf
.st_mode
& S_IFMT
) == S_IFDIR
)
586 fname2
= (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE
) + strlen(fname
) + 2));
590 while (fname
[strlen(fname
)-1] == '/'
592 || fname
[strlen(fname
)-1] == '\\'
596 fname
[strlen(fname
)-1] = '\0';
597 if (strlen(fname
) == 0)
600 if (fname
[0] == '\0')
601 (void)sprintf(fname2
, "/%s", CSCOPE_DBFILE
);
603 (void)sprintf(fname2
, "%s/%s", fname
, CSCOPE_DBFILE
);
605 ret
= stat(fname2
, &statbuf
);
609 cs_stat_emsg(fname2
);
613 i
= cs_insert_filelist(fname2
, ppath
, flags
, &statbuf
);
616 else if (S_ISREG(statbuf
.st_mode
) || S_ISLNK(statbuf
.st_mode
))
618 /* WIN32 - substitute define S_ISREG from os_unix.h */
619 else if (((statbuf
.st_mode
) & S_IFMT
) == S_IFREG
)
622 i
= cs_insert_filelist(fname
, ppath
, flags
, &statbuf
);
628 _("E564: %s is not a directory or a valid cscope database"),
635 if (cs_create_connection(i
) == CSCOPE_FAILURE
636 || cs_read_prompt(i
) == CSCOPE_FAILURE
)
638 cs_release_csp(i
, TRUE
);
645 (void)smsg_attr(hl_attr(HLF_R
),
646 (char_u
*)_("Added cscope database %s"),
654 return CSCOPE_SUCCESS
;
660 return CSCOPE_FAILURE
;
661 } /* cs_add_common */
665 cs_check_for_connections()
667 return (cs_cnt_connections() > 0);
668 } /* cs_check_for_connections */
674 return (p_tags
[0] != NUL
&& curbuf
->b_p_tags
!= NULL
);
675 } /* cs_check_for_tags */
679 * PRIVATE: cs_cnt_connections
681 * count the number of cscope connections
689 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
691 if (csinfo
[i
].fname
!= NULL
)
695 } /* cs_cnt_connections */
699 int idx
; /* connection index */
701 EMSGN(_("E262: error reading cscope connection %ld"), idx
);
704 #define CSREAD_BUFSIZE 2048
706 * PRIVATE: cs_cnt_matches
708 * count the number of matches for a given cscope connection.
718 buf
= (char *)alloc(CSREAD_BUFSIZE
);
723 if (!fgets(buf
, CSREAD_BUFSIZE
, csinfo
[idx
].fr_fp
))
725 if (feof(csinfo
[idx
].fr_fp
))
728 cs_reading_emsg(idx
);
735 * If the database is out of date, or there's some other problem,
736 * cscope will output error messages before the number-of-lines output.
737 * Display/discard any output that doesn't match what we want.
738 * Accept "\S*cscope: X lines", also matches "mlcscope".
740 if ((stok
= strtok(buf
, (const char *)" ")) == NULL
)
742 if (strstr((const char *)stok
, "cscope:") == NULL
)
745 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
754 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
756 if (strncmp((const char *)stok
, "lines", 5))
764 } /* cs_cnt_matches */
768 * PRIVATE: cs_create_cmd
770 * Creates the actual cscope command query from what the user entered.
773 cs_create_cmd(csoption
, pattern
)
783 case '0' : case 's' :
786 case '1' : case 'g' :
789 case '2' : case 'd' :
792 case '3' : case 'c' :
795 case '4' : case 't' :
798 case '6' : case 'e' :
801 case '7' : case 'f' :
804 case '8' : case 'i' :
808 (void)EMSG(_("E561: unknown cscope search type"));
813 /* Skip white space before the patter, except for text and pattern search,
814 * they may want to use the leading white space. */
816 if (search
!= 4 && search
!= 6)
817 while vim_iswhite(*pat
)
820 if ((cmd
= (char *)alloc((unsigned)(strlen(pat
) + 2))) == NULL
)
823 (void)sprintf(cmd
, "%d%s", search
, pat
);
826 } /* cs_create_cmd */
830 * PRIVATE: cs_create_connection
832 * This piece of code was taken/adapted from nvi. do we need to add
833 * the BSD license notice?
836 cs_create_connection(i
)
840 int to_cs
[2], from_cs
[2];
843 char *prog
, *cmd
, *ppath
= NULL
;
846 SECURITY_ATTRIBUTES sa
;
847 PROCESS_INFORMATION pi
;
849 BOOL pipe_stdin
= FALSE
, pipe_stdout
= FALSE
;
850 HANDLE stdin_rd
, stdout_rd
;
851 HANDLE stdout_wr
, stdin_wr
;
854 # define OPEN_OH_ARGTYPE long
856 # if (_MSC_VER >= 1300)
857 # define OPEN_OH_ARGTYPE intptr_t
859 # define OPEN_OH_ARGTYPE long
866 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
867 * from_cs[0] and writes to to_cs[1].
869 to_cs
[0] = to_cs
[1] = from_cs
[0] = from_cs
[1] = -1;
870 if (pipe(to_cs
) < 0 || pipe(from_cs
) < 0)
872 (void)EMSG(_("E566: Could not create cscope pipes"));
875 (void)close(to_cs
[0]);
877 (void)close(to_cs
[1]);
878 if (from_cs
[0] != -1)
879 (void)close(from_cs
[0]);
880 if (from_cs
[1] != -1)
881 (void)close(from_cs
[1]);
882 return CSCOPE_FAILURE
;
885 switch (csinfo
[i
].pid
= fork())
888 (void)EMSG(_("E622: Could not fork for cscope"));
890 case 0: /* child: run cscope. */
891 if (dup2(to_cs
[0], STDIN_FILENO
) == -1)
892 PERROR("cs_create_connection 1");
893 if (dup2(from_cs
[1], STDOUT_FILENO
) == -1)
894 PERROR("cs_create_connection 2");
895 if (dup2(from_cs
[1], STDERR_FILENO
) == -1)
896 PERROR("cs_create_connection 3");
899 (void)close(to_cs
[1]);
900 (void)close(from_cs
[0]);
903 /* Create pipes to communicate with cscope */
904 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
905 sa
.bInheritHandle
= TRUE
;
906 sa
.lpSecurityDescriptor
= NULL
;
908 if (!(pipe_stdin
= CreatePipe(&stdin_rd
, &stdin_wr
, &sa
, 0))
909 || !(pipe_stdout
= CreatePipe(&stdout_rd
, &stdout_wr
, &sa
, 0)))
911 (void)EMSG(_("E566: Could not create cscope pipes"));
915 CloseHandle(stdin_rd
);
916 CloseHandle(stdin_wr
);
920 CloseHandle(stdout_rd
);
921 CloseHandle(stdout_wr
);
923 return CSCOPE_FAILURE
;
926 /* expand the cscope exec for env var's */
927 if ((prog
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
930 return CSCOPE_FAILURE
;
936 expand_env((char_u
*)p_csprg
, (char_u
*)prog
, MAXPATHL
);
938 /* alloc space to hold the cscope command */
939 len
= (int)(strlen(prog
) + strlen(csinfo
[i
].fname
) + 32);
942 /* expand the prepend path for env var's */
943 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
947 return CSCOPE_FAILURE
;
953 expand_env((char_u
*)csinfo
[i
].ppath
, (char_u
*)ppath
, MAXPATHL
);
955 len
+= (int)strlen(ppath
);
959 len
+= (int)strlen(csinfo
[i
].flags
);
961 if ((cmd
= (char *)alloc(len
)) == NULL
)
966 return CSCOPE_FAILURE
;
973 /* run the cscope command; is there execl for non-unix systems? */
975 (void)sprintf(cmd
, "exec %s -dl -f %s", prog
, csinfo
[i
].fname
);
978 (void)sprintf(cmd
, "%s -dl -f %s", prog
, csinfo
[i
].fname
);
980 if (csinfo
[i
].ppath
!= NULL
)
982 (void)strcat(cmd
, " -P");
983 (void)strcat(cmd
, csinfo
[i
].ppath
);
985 if (csinfo
[i
].flags
!= NULL
)
987 (void)strcat(cmd
, " ");
988 (void)strcat(cmd
, csinfo
[i
].flags
);
991 /* on Win32 we still need prog */
997 if (execl("/bin/sh", "sh", "-c", cmd
, NULL
) == -1)
998 PERROR(_("cs_create_connection exec failed"));
1002 default: /* parent. */
1004 * Save the file descriptors for later duplication, and
1005 * reopen as streams.
1007 if ((csinfo
[i
].to_fp
= fdopen(to_cs
[1], "w")) == NULL
)
1008 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1009 if ((csinfo
[i
].fr_fp
= fdopen(from_cs
[0], "r")) == NULL
)
1010 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1013 (void)close(to_cs
[0]);
1014 (void)close(from_cs
[1]);
1021 /* Create a new process to run cscope and use pipes to talk with it */
1022 GetStartupInfo(&si
);
1023 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
1024 si
.wShowWindow
= SW_HIDE
; /* Hide child application window */
1025 si
.hStdOutput
= stdout_wr
;
1026 si
.hStdError
= stdout_wr
;
1027 si
.hStdInput
= stdin_rd
;
1028 created
= CreateProcess(NULL
, cmd
, NULL
, NULL
, TRUE
, CREATE_NEW_CONSOLE
,
1029 NULL
, NULL
, &si
, &pi
);
1035 PERROR(_("cs_create_connection exec failed"));
1036 (void)EMSG(_("E623: Could not spawn cscope process"));
1040 csinfo
[i
].pid
= pi
.dwProcessId
;
1041 csinfo
[i
].hProc
= pi
.hProcess
;
1042 CloseHandle(pi
.hThread
);
1044 /* TODO - tidy up after failure to create files on pipe handles. */
1045 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdin_wr
,
1046 _O_TEXT
|_O_APPEND
)) < 0)
1047 || ((csinfo
[i
].to_fp
= _fdopen(fd
, "w")) == NULL
))
1048 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1049 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdout_rd
,
1050 _O_TEXT
|_O_RDONLY
)) < 0)
1051 || ((csinfo
[i
].fr_fp
= _fdopen(fd
, "r")) == NULL
))
1052 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1054 /* Close handles for file descriptors inherited by the cscope process */
1055 CloseHandle(stdin_rd
);
1056 CloseHandle(stdout_wr
);
1060 return CSCOPE_SUCCESS
;
1061 } /* cs_create_connection */
1067 * query cscope using command line interface. parse the output and use tselect
1068 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1070 * returns TRUE if we jump to a tag or abort, FALSE if not.
1078 if (cs_check_for_connections() == FALSE
)
1080 (void)EMSG(_("E567: no cscope connections"));
1084 if ((opt
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1090 pat
= opt
+ strlen(opt
) + 1;
1091 if (pat
>= (char *)eap
->arg
+ eap_arg_len
)
1097 return cs_find_common(opt
, pat
, eap
->forceit
, TRUE
,
1098 eap
->cmdidx
== CMD_lcscope
);
1103 * PRIVATE: cs_find_common
1105 * common code for cscope find, shared by cs_find() and do_cstag()
1108 cs_find_common(opt
, pat
, forceit
, verbose
, use_ll
)
1117 int nummatches
[CSCOPE_MAX_CONNECTIONS
], totmatches
;
1118 #ifdef FEAT_QUICKFIX
1123 /* create the actual command to send to cscope */
1124 cmd
= cs_create_cmd(opt
, pat
);
1128 /* send query to all open connections, then count the total number
1129 * of matches so we can alloc matchesp all in one swell foop
1131 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1134 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1136 if (csinfo
[i
].fname
== NULL
|| csinfo
[i
].to_fp
== NULL
)
1139 /* send cmd to cscope */
1140 (void)fprintf(csinfo
[i
].to_fp
, "%s\n", cmd
);
1141 (void)fflush(csinfo
[i
].to_fp
);
1143 nummatches
[i
] = cs_cnt_matches(i
);
1145 if (nummatches
[i
] > -1)
1146 totmatches
+= nummatches
[i
];
1148 if (nummatches
[i
] == 0)
1149 (void)cs_read_prompt(i
);
1153 if (totmatches
== 0)
1155 char *nf
= _("E259: no matches found for cscope query %s of %s");
1161 buf
= (char *)alloc((unsigned)(strlen(opt
) + strlen(pat
) + strlen(nf
)));
1166 sprintf(buf
, nf
, opt
, pat
);
1173 #ifdef FEAT_QUICKFIX
1174 /* get cmd letter */
1205 qfpos
= (char *)vim_strchr(p_csqf
, cmdletter
);
1209 /* next symbol must be + or - */
1210 if (strchr(CSQF_FLAGS
, *qfpos
) == NULL
)
1212 char *nf
= _("E469: invalid cscopequickfix flag %c for %c");
1213 char *buf
= (char *)alloc((unsigned)strlen(nf
));
1215 /* strlen will be enough because we use chars */
1218 sprintf(buf
, nf
, *qfpos
, *(qfpos
-1));
1225 if (qfpos
!= NULL
&& *qfpos
!= '0' && totmatches
> 0)
1227 /* fill error list */
1229 char_u
*tmp
= vim_tempname('c');
1230 qf_info_T
*qi
= NULL
;
1233 f
= mch_fopen((char *)tmp
, "w");
1235 EMSG2(_(e_notopen
), tmp
);
1238 cs_file_results(f
, nummatches
);
1240 if (use_ll
) /* Use location list */
1242 /* '-' starts a new error list */
1243 if (qf_init(wp
, tmp
, (char_u
*)"%f%*\\t%l%*\\t%m",
1246 # ifdef FEAT_WINDOWS
1247 if (postponed_split
!= 0)
1249 win_split(postponed_split
> 0 ? postponed_split
: 0,
1250 postponed_split_flags
);
1251 # ifdef FEAT_SCROLLBIND
1252 curwin
->w_p_scb
= FALSE
;
1254 postponed_split
= 0;
1259 * In the location list window, use the displayed location
1260 * list. Otherwise, use the location list for the window.
1262 qi
= (bt_quickfix(wp
->w_buffer
) && wp
->w_llist_ref
!= NULL
)
1263 ? wp
->w_llist_ref
: wp
->w_llist
;
1264 qf_jump(qi
, 0, 0, forceit
);
1272 #endif /* FEAT_QUICKFIX */
1274 char **matches
= NULL
, **contexts
= NULL
;
1278 cs_fill_results((char *)pat
, totmatches
, nummatches
, &matches
,
1279 &contexts
, &matched
);
1280 if (matches
== NULL
)
1283 (void)cs_manage_matches(matches
, contexts
, matched
, Store
);
1285 return do_tag((char_u
*)pat
, DT_CSCOPE
, 0, forceit
, verbose
);
1288 } /* cs_find_common */
1300 cscmd_T
*cmdp
= cs_cmds
;
1302 (void)MSG_PUTS(_("cscope commands:\n"));
1303 while (cmdp
->name
!= NULL
)
1305 char *help
= _(cmdp
->help
);
1306 int space_cnt
= 30 - vim_strsize((char_u
*)help
);
1308 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1311 (void)smsg((char_u
*)_("%-5s: %s%*s (Usage: %s)"),
1313 help
, space_cnt
, " ",
1315 if (strcmp(cmdp
->name
, "find") == 0)
1317 " c: Find functions calling this function\n"
1318 " d: Find functions called by this function\n"
1319 " e: Find this egrep pattern\n"
1320 " f: Find this file\n"
1321 " g: Find this definition\n"
1322 " i: Find files #including this file\n"
1323 " s: Find this C symbol\n"
1324 " t: Find assignments to\n"));
1337 * initialize cscope structure if not already
1343 static int init_already
= FALSE
;
1348 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1351 init_already
= TRUE
;
1358 csinfo
[i
].fname
= NULL
;
1359 csinfo
[i
].ppath
= NULL
;
1360 csinfo
[i
].flags
= NULL
;
1362 csinfo
[i
].st_dev
= (dev_t
)0;
1363 csinfo
[i
].st_ino
= (ino_t
)0;
1365 csinfo
[i
].nVolume
= 0;
1366 csinfo
[i
].nIndexHigh
= 0;
1367 csinfo
[i
].nIndexLow
= 0;
1370 csinfo
[i
].fr_fp
= NULL
;
1371 csinfo
[i
].to_fp
= NULL
;
1373 csinfo
[i
].hProc
= NULL
;
1378 static char *GetWin32Error
__ARGS((void));
1384 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
1385 NULL
, GetLastError(), 0, (LPSTR
)&msg
, 0, NULL
);
1388 /* remove trailing \r\n */
1389 char *pcrlf
= strstr(msg
, "\r\n");
1398 * PRIVATE: cs_insert_filelist
1400 * insert a new cscope database filename into the filelist
1404 cs_insert_filelist(fname
, ppath
, flags
, sb
)
1413 BY_HANDLE_FILE_INFORMATION bhfi
;
1415 vim_memset(&bhfi
, 0, sizeof(bhfi
));
1416 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1417 if (!mch_windows95())
1419 hFile
= CreateFile(fname
, FILE_READ_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
1420 FILE_ATTRIBUTE_NORMAL
, NULL
);
1421 if (hFile
== INVALID_HANDLE_VALUE
)
1425 char *cant_msg
= _("E625: cannot open cscope database: %s");
1426 char *winmsg
= GetWin32Error();
1430 (void)EMSG2(cant_msg
, winmsg
);
1434 /* subst filename if can't get error text */
1435 (void)EMSG2(cant_msg
, fname
);
1439 if (!GetFileInformationByHandle(hFile
, &bhfi
))
1443 (void)EMSG(_("E626: cannot get cscope database information"));
1450 i
= -1; /* can be set to the index of an empty item in csinfo */
1451 for (j
= 0; j
< CSCOPE_MAX_CONNECTIONS
; j
++)
1453 if (csinfo
[j
].fname
!= NULL
1455 && csinfo
[j
].st_dev
== sb
->st_dev
&& csinfo
[j
].st_ino
== sb
->st_ino
1457 /* compare pathnames first */
1458 && ((fullpathcmp(csinfo
[j
].fname
, fname
, FALSE
) & FPC_SAME
)
1459 /* if not Windows 9x, test index file attributes too */
1460 || (!mch_windows95()
1461 && csinfo
[j
].nVolume
== bhfi
.dwVolumeSerialNumber
1462 && csinfo
[j
].nIndexHigh
== bhfi
.nFileIndexHigh
1463 && csinfo
[j
].nIndexLow
== bhfi
.nFileIndexLow
))
1468 (void)EMSG(_("E568: duplicate cscope database not added"));
1472 if (csinfo
[j
].fname
== NULL
&& i
== -1)
1473 i
= j
; /* remember first empty entry */
1479 (void)EMSG(_("E569: maximum number of cscope connections reached"));
1483 if ((csinfo
[i
].fname
= (char *)alloc((unsigned)strlen(fname
)+1)) == NULL
)
1486 (void)strcpy(csinfo
[i
].fname
, (const char *)fname
);
1490 if ((csinfo
[i
].ppath
= (char *)alloc((unsigned)strlen(ppath
) + 1)) == NULL
)
1492 vim_free(csinfo
[i
].fname
);
1493 csinfo
[i
].fname
= NULL
;
1496 (void)strcpy(csinfo
[i
].ppath
, (const char *)ppath
);
1498 csinfo
[i
].ppath
= NULL
;
1502 if ((csinfo
[i
].flags
= (char *)alloc((unsigned)strlen(flags
) + 1)) == NULL
)
1504 vim_free(csinfo
[i
].fname
);
1505 vim_free(csinfo
[i
].ppath
);
1506 csinfo
[i
].fname
= NULL
;
1507 csinfo
[i
].ppath
= NULL
;
1510 (void)strcpy(csinfo
[i
].flags
, (const char *)flags
);
1512 csinfo
[i
].flags
= NULL
;
1515 csinfo
[i
].st_dev
= sb
->st_dev
;
1516 csinfo
[i
].st_ino
= sb
->st_ino
;
1519 csinfo
[i
].nVolume
= bhfi
.dwVolumeSerialNumber
;
1520 csinfo
[i
].nIndexLow
= bhfi
.nFileIndexLow
;
1521 csinfo
[i
].nIndexHigh
= bhfi
.nFileIndexHigh
;
1524 } /* cs_insert_filelist */
1528 * PRIVATE: cs_lookup_cmd
1530 * find cscope command in command table
1540 if (eap
->arg
== NULL
)
1543 /* Store length of eap->arg before it gets modified by strtok(). */
1544 eap_arg_len
= (int)STRLEN(eap
->arg
);
1546 if ((stok
= strtok((char *)(eap
->arg
), (const char *)" ")) == NULL
)
1550 for (cmdp
= cs_cmds
; cmdp
->name
!= NULL
; ++cmdp
)
1552 if (strncmp((const char *)(stok
), cmdp
->name
, len
) == 0)
1556 } /* cs_lookup_cmd */
1572 if ((stok
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1575 return CSCOPE_FAILURE
;
1578 /* only single digit positive and negative integers are allowed */
1579 if ((strlen(stok
) < 2 && VIM_ISDIGIT((int)(stok
[0])))
1580 || (strlen(stok
) < 3 && stok
[0] == '-'
1581 && VIM_ISDIGIT((int)(stok
[1]))))
1585 /* It must be part of a name. We will try to find a match
1586 * within all the names in the csinfo data structure
1588 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1590 if (csinfo
[i
].fname
!= NULL
&& strstr(csinfo
[i
].fname
, stok
))
1595 if ((i
>= CSCOPE_MAX_CONNECTIONS
|| i
< -1 || csinfo
[i
].fname
== NULL
)
1599 (void)EMSG2(_("E261: cscope connection %s not found"), stok
);
1605 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1607 if (csinfo
[i
].fname
)
1608 cs_kill_execute(i
, csinfo
[i
].fname
);
1612 cs_kill_execute(i
, stok
);
1620 * PRIVATE: cs_kill_execute
1622 * Actually kills a specific cscope connection.
1625 cs_kill_execute(i
, cname
)
1626 int i
; /* cscope table index */
1627 char *cname
; /* cscope database name */
1632 (void)smsg_attr(hl_attr(HLF_R
) | MSG_HIST
,
1633 (char_u
*)_("cscope connection %s closed"), cname
);
1635 cs_release_csp(i
, TRUE
);
1640 * PRIVATE: cs_make_vim_style_matches
1642 * convert the cscope output into into a ctags style entry (as might be found
1643 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1644 * the type of the tag you are looking for. for example, in Darren Hiebert's
1645 * ctags (the one that comes with vim), #define's use a line number to find the
1646 * tag in a file while function definitions use a regexp search pattern.
1648 * i'm going to always use the line number because cscope does something
1649 * quirky (and probably other things i don't know about):
1651 * if you have "# define" in your source file, which is
1652 * perfectly legal, cscope thinks you have "#define". this
1653 * will result in a failed regexp search. :(
1655 * besides, even if this particular case didn't happen, the search pattern
1656 * would still have to be modified to escape all the special regular expression
1657 * characters to comply with ctags formatting.
1660 cs_make_vim_style_matches(fname
, slno
, search
, tagstr
)
1666 /* vim style is ctags:
1668 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1670 * but as mentioned above, we'll always use the line number and
1671 * put the search pattern (if one exists) as "extra"
1673 * buf is used as part of vim's method of handling tags, and
1674 * (i think) vim frees it when you pop your tags and get replaced
1675 * by new ones on the tag stack.
1682 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + strlen(search
)+6);
1683 if ((buf
= (char *)alloc(amt
)) == NULL
)
1686 (void)sprintf(buf
, "%s\t%s\t%s;\"\t%s", tagstr
, fname
, slno
, search
);
1690 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + 5);
1691 if ((buf
= (char *)alloc(amt
)) == NULL
)
1694 (void)sprintf(buf
, "%s\t%s\t%s;\"", tagstr
, fname
, slno
);
1698 } /* cs_make_vim_style_matches */
1702 * PRIVATE: cs_manage_matches
1704 * this is kind of hokey, but i don't see an easy way round this..
1706 * Store: keep a ptr to the (malloc'd) memory of matches originally
1707 * generated from cs_find(). the matches are originally lines directly
1708 * from cscope output, but transformed to look like something out of a
1709 * ctags. see cs_make_vim_style_matches for more details.
1711 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1712 * the next line from the cscope output. it basically keeps track of which
1713 * lines have been "used" and returns the next one.
1715 * Free: frees up everything and resets
1717 * Print: prints the tags
1720 cs_manage_matches(matches
, contexts
, totmatches
, cmd
)
1726 static char **mp
= NULL
;
1727 static char **cp
= NULL
;
1728 static int cnt
= -1;
1729 static int next
= -1;
1735 assert(matches
!= NULL
);
1736 assert(totmatches
> 0);
1737 if (mp
!= NULL
|| cp
!= NULL
)
1738 (void)cs_manage_matches(NULL
, NULL
, -1, Free
);
1770 cs_print_tags_priv(mp
, cp
, cnt
);
1772 default: /* should not reach here */
1773 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1778 } /* cs_manage_matches */
1782 * PRIVATE: cs_parse_results
1784 * parse cscope output
1787 cs_parse_results(cnumber
, buf
, bufsize
, context
, linenumber
, search
)
1799 if (fgets(buf
, bufsize
, csinfo
[cnumber
].fr_fp
) == NULL
)
1801 if (feof(csinfo
[cnumber
].fr_fp
))
1804 cs_reading_emsg(cnumber
);
1809 /* If the line's too long for the buffer, discard it. */
1810 if ((p
= strchr(buf
, '\n')) == NULL
)
1812 while ((ch
= getc(csinfo
[cnumber
].fr_fp
)) != EOF
&& ch
!= '\n')
1819 * cscope output is in the following format:
1821 * <filename> <context> <line number> <pattern>
1823 if ((name
= strtok((char *)buf
, (const char *)" ")) == NULL
)
1825 if ((*context
= strtok(NULL
, (const char *)" ")) == NULL
)
1827 if ((*linenumber
= strtok(NULL
, (const char *)" ")) == NULL
)
1829 *search
= *linenumber
+ strlen(*linenumber
) + 1; /* +1 to skip \0 */
1832 * If the file is older than the cscope database, that is,
1833 * the database was built since the file was last modified,
1834 * or there wasn't a search string, use the line number.
1836 if (strcmp(*search
, "<unknown>") == 0)
1839 name
= cs_resolve_file(cnumber
, name
);
1843 #ifdef FEAT_QUICKFIX
1845 * PRIVATE: cs_file_results
1847 * write cscope find results to file
1850 cs_file_results(f
, nummatches_a
)
1856 char *search
, *slno
;
1861 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1865 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1867 if (nummatches_a
[i
] < 1)
1870 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1872 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1873 &slno
, &search
)) == NULL
)
1876 context
= (char *)alloc((unsigned)strlen(cntx
)+5);
1877 if (context
== NULL
)
1880 if (strcmp(cntx
, "<global>")==0)
1881 strcpy(context
, "<<global>>");
1883 sprintf(context
, "<<%s>>", cntx
);
1886 fprintf(f
, "%s\t%s\t%s\n", fullname
, slno
, context
);
1888 fprintf(f
, "%s\t%s\t%s %s\n", fullname
, slno
, context
, search
);
1892 } /* for all matches */
1894 (void)cs_read_prompt(i
);
1896 } /* for all cscope connections */
1902 * PRIVATE: cs_fill_results
1904 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1906 * When there are no matches sets "*matches_p" to NULL.
1909 cs_fill_results(tagstr
, totmatches
, nummatches_a
, matches_p
, cntxts_p
, matched
)
1919 char *search
, *slno
;
1921 char **matches
= NULL
;
1922 char **cntxts
= NULL
;
1926 assert(totmatches
> 0);
1928 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1932 if ((matches
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1934 if ((cntxts
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1937 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
1939 if (nummatches_a
[i
] < 1)
1942 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1944 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1945 &slno
, &search
)) == NULL
)
1948 matches
[totsofar
] = cs_make_vim_style_matches(fullname
, slno
,
1953 if (strcmp(cntx
, "<global>") == 0)
1954 cntxts
[totsofar
] = NULL
;
1956 /* note: if vim_strsave returns NULL, then the context
1957 * will be "<global>", which is misleading.
1959 cntxts
[totsofar
] = (char *)vim_strsave((char_u
*)cntx
);
1961 if (matches
[totsofar
] != NULL
)
1964 } /* for all matches */
1966 (void)cs_read_prompt(i
);
1968 } /* for all cscope connections */
1973 /* No matches, free the arrays and return NULL in "*matches_p". */
1979 *matched
= totsofar
;
1980 *matches_p
= matches
;
1984 } /* cs_fill_results */
1987 /* get the requested path components */
1989 cs_pathcomponents(path
)
1998 s
= path
+ strlen(path
) - 1;
1999 for (i
= 0; i
< p_cspc
; ++i
)
2000 while (s
> path
&& *--s
!= '/'
2006 if ((s
> path
&& *s
== '/')
2008 || (s
> path
&& *s
== '\\')
2016 * PRIVATE: cs_print_tags_priv
2018 * called from cs_manage_matches()
2021 cs_print_tags_priv(matches
, cntxts
, num_matches
)
2027 int bufsize
= 0; /* Track available bufsize */
2030 char *fname
, *lno
, *extra
, *tbuf
;
2032 char *globalcntx
= "GLOBAL";
2033 char *cntxformat
= " <<%s>>";
2035 char *cstag_msg
= _("Cscope tag: %s");
2036 char *csfmt_str
= "%4d %6s ";
2038 assert (num_matches
> 0);
2040 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[0]) + 1)) == NULL
)
2043 strcpy(tbuf
, matches
[0]);
2044 ptag
= strtok(tbuf
, "\t");
2046 newsize
= (int)(strlen(cstag_msg
) + strlen(ptag
));
2047 buf
= (char *)alloc(newsize
);
2051 (void)sprintf(buf
, cstag_msg
, ptag
);
2052 MSG_PUTS_ATTR(buf
, hl_attr(HLF_T
));
2057 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T
)); /* strlen is 7 */
2058 msg_advance(msg_col
+ 2);
2059 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T
));
2062 for (i
= 0; i
< num_matches
; i
++)
2066 /* if we really wanted to, we could avoid this malloc and strcpy
2067 * by parsing matches[i] on the fly and placing stuff into buf
2068 * directly, but that's too much of a hassle
2070 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[idx
]) + 1)) == NULL
)
2072 (void)strcpy(tbuf
, matches
[idx
]);
2074 if ((fname
= strtok(tbuf
, (const char *)"\t")) == NULL
)
2076 if ((fname
= strtok(NULL
, (const char *)"\t")) == NULL
)
2078 if ((lno
= strtok(NULL
, (const char *)"\t")) == NULL
)
2080 extra
= strtok(NULL
, (const char *)"\t");
2082 lno
[strlen(lno
)-2] = '\0'; /* ignore ;" at the end */
2084 /* hopefully 'num' (num of matches) will be less than 10^16 */
2085 newsize
= (int)(strlen(csfmt_str
) + 16 + strlen(lno
));
2086 if (bufsize
< newsize
)
2088 buf
= (char *)vim_realloc(buf
, newsize
);
2096 /* csfmt_str = "%4d %6s "; */
2097 (void)sprintf(buf
, csfmt_str
, num
, lno
);
2098 MSG_PUTS_ATTR(buf
, hl_attr(HLF_CM
));
2100 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname
), hl_attr(HLF_CM
));
2102 /* compute the required space for the context */
2103 if (cntxts
[idx
] != NULL
)
2104 context
= cntxts
[idx
];
2106 context
= globalcntx
;
2107 newsize
= (int)(strlen(context
) + strlen(cntxformat
));
2109 if (bufsize
< newsize
)
2111 buf
= (char *)vim_realloc(buf
, newsize
);
2119 (void)sprintf(buf
, cntxformat
, context
);
2121 /* print the context only if it fits on the same line */
2122 if (msg_col
+ (int)strlen(buf
) >= (int)Columns
)
2131 MSG_PUTS_LONG(extra
);
2134 vim_free(tbuf
); /* only after printing extra due to strtok use */
2142 got_int
= FALSE
; /* don't print any more matches */
2147 } /* for all matches */
2150 } /* cs_print_tags_priv */
2154 * PRIVATE: cs_read_prompt
2156 * read a cscope prompt (basically, skip over the ">> ")
2163 char *buf
= NULL
; /* buffer for possible error message from cscope */
2167 static char *eprompt
= "Press the RETURN key to continue:";
2168 int epromptlen
= (int)strlen(eprompt
);
2171 cs_emsg
= _("E609: Cscope error: %s");
2172 /* compute maximum allowed len for Cscope error message */
2173 maxlen
= (int)(IOSIZE
- strlen(cs_emsg
));
2177 while ((ch
= getc(csinfo
[i
].fr_fp
)) != EOF
&& ch
!= CSCOPE_PROMPT
[0])
2178 /* if there is room and char is printable */
2179 if (bufpos
< maxlen
- 1 && vim_isprintc(ch
))
2181 if (buf
== NULL
) /* lazy buffer allocation */
2182 buf
= (char *)alloc(maxlen
);
2185 /* append character to the message */
2188 if (bufpos
>= epromptlen
2189 && strcmp(&buf
[bufpos
- epromptlen
], eprompt
) == 0)
2191 /* remove eprompt from buf */
2192 buf
[bufpos
- epromptlen
] = NUL
;
2194 /* print message to user */
2195 (void)EMSG2(cs_emsg
, buf
);
2197 /* send RETURN to cscope */
2198 (void)putc('\n', csinfo
[i
].to_fp
);
2199 (void)fflush(csinfo
[i
].to_fp
);
2208 for (n
= 0; n
< (int)strlen(CSCOPE_PROMPT
); ++n
)
2211 ch
= getc(csinfo
[i
].fr_fp
);
2214 PERROR("cs_read_prompt EOF");
2215 if (buf
!= NULL
&& buf
[0] != NUL
)
2216 (void)EMSG2(cs_emsg
, buf
);
2217 else if (p_csverbose
)
2218 cs_reading_emsg(i
); /* don't have additional information */
2219 cs_release_csp(i
, TRUE
);
2221 return CSCOPE_FAILURE
;
2224 if (ch
!= CSCOPE_PROMPT
[n
])
2232 continue; /* didn't find the prompt */
2233 break; /* did find the prompt */
2237 return CSCOPE_SUCCESS
;
2240 #if defined(UNIX) && defined(SIGALRM)
2242 * Used to catch and ignore SIGALRM below.
2246 sig_handler
SIGDEFARG(sigarg
)
2254 * PRIVATE: cs_release_csp
2256 * Does the actual free'ing for the cs ptr with an optional flag of whether
2257 * or not to free the filename. Called by cs_kill and cs_reset.
2260 cs_release_csp(i
, freefnpp
)
2265 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2267 if (csinfo
[i
].to_fp
!= NULL
)
2269 (void)fputs("q\n", csinfo
[i
].to_fp
);
2270 (void)fflush(csinfo
[i
].to_fp
);
2278 # if defined(HAVE_SIGACTION)
2279 struct sigaction sa
, old
;
2281 /* Use sigaction() to limit the waiting time to two seconds. */
2282 sigemptyset(&sa
.sa_mask
);
2283 sa
.sa_handler
= sig_handler
;
2284 sa
.sa_flags
= SA_NODEFER
;
2285 sigaction(SIGALRM
, &sa
, &old
);
2286 alarm(2); /* 2 sec timeout */
2288 /* Block until cscope exits or until timer expires */
2289 pid
= waitpid(csinfo
[i
].pid
, &pstat
, 0);
2290 waitpid_errno
= errno
;
2292 /* cancel pending alarm if still there and restore signal */
2294 sigaction(SIGALRM
, &old
, NULL
);
2298 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2299 * to give cscope a chance to exit quickly. */
2301 for (waited
= 0; waited
< 40; ++waited
)
2303 pid
= waitpid(csinfo
[i
].pid
, &pstat
, WNOHANG
);
2304 waitpid_errno
= errno
;
2306 break; /* break unless the process is still running */
2307 mch_delay(50L, FALSE
); /* sleep 50 ms */
2311 * If the cscope process is still running: kill it.
2312 * Safety check: If the PID would be zero here, the entire X session
2313 * would be killed. -1 and 1 are dangerous as well.
2315 if (pid
< 0 && csinfo
[i
].pid
> 1)
2320 if (waitpid_errno
== ECHILD
)
2323 * When using 'vim -g', vim is forked and cscope process is
2324 * no longer a child process but a sibling. So waitpid()
2325 * fails with errno being ECHILD (No child processes).
2326 * Don't send SIGKILL to cscope immediately but wait
2327 * (polling) for it to exit normally as result of sending
2328 * the "q" command, hence giving it a chance to clean up
2329 * its temporary files.
2334 for (waited
= 0; waited
< 40; ++waited
)
2336 /* Check whether cscope process is still alive */
2337 if (kill(csinfo
[i
].pid
, 0) != 0)
2339 alive
= FALSE
; /* cscope process no longer exists */
2342 mch_delay(50L, FALSE
); /* sleep 50ms */
2348 kill(csinfo
[i
].pid
, SIGKILL
);
2349 (void)waitpid(csinfo
[i
].pid
, &pstat
, 0);
2354 if (csinfo
[i
].hProc
!= NULL
)
2356 /* Give cscope a chance to exit normally */
2357 if (WaitForSingleObject(csinfo
[i
].hProc
, 1000) == WAIT_TIMEOUT
)
2358 TerminateProcess(csinfo
[i
].hProc
, 0);
2359 CloseHandle(csinfo
[i
].hProc
);
2363 if (csinfo
[i
].fr_fp
!= NULL
)
2364 (void)fclose(csinfo
[i
].fr_fp
);
2365 if (csinfo
[i
].to_fp
!= NULL
)
2366 (void)fclose(csinfo
[i
].to_fp
);
2370 vim_free(csinfo
[i
].fname
);
2371 vim_free(csinfo
[i
].ppath
);
2372 vim_free(csinfo
[i
].flags
);
2376 } /* cs_release_csp */
2382 * calls cs_kill on all cscope connections then reinits
2389 char **dblist
= NULL
, **pplist
= NULL
, **fllist
= NULL
;
2391 char buf
[20]; /* for sprintf " (#%d)" */
2393 /* malloc our db and ppath list */
2394 dblist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2395 pplist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2396 fllist
= (char **)alloc(CSCOPE_MAX_CONNECTIONS
* sizeof(char *));
2397 if (dblist
== NULL
|| pplist
== NULL
|| fllist
== NULL
)
2402 return CSCOPE_FAILURE
;
2405 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2407 dblist
[i
] = csinfo
[i
].fname
;
2408 pplist
[i
] = csinfo
[i
].ppath
;
2409 fllist
[i
] = csinfo
[i
].flags
;
2410 if (csinfo
[i
].fname
!= NULL
)
2411 cs_release_csp(i
, FALSE
);
2414 /* rebuild the cscope connection list */
2415 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2417 if (dblist
[i
] != NULL
)
2419 cs_add_common(dblist
[i
], pplist
[i
], fllist
[i
]);
2422 /* don't use smsg_attr() because we want to display the
2423 * connection number in the same line as
2424 * "Added cscope database..."
2426 sprintf(buf
, " (#%d)", i
);
2427 MSG_PUTS_ATTR(buf
, hl_attr(HLF_R
));
2430 vim_free(dblist
[i
]);
2431 vim_free(pplist
[i
]);
2432 vim_free(fllist
[i
]);
2439 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R
) | MSG_HIST
);
2440 return CSCOPE_SUCCESS
;
2445 * PRIVATE: cs_resolve_file
2447 * construct the full pathname to a file found in the cscope database.
2448 * (Prepends ppath, if there is one and if it's not already prepended,
2449 * otherwise just uses the name found.)
2451 * we need to prepend the prefix because on some cscope's (e.g., the one that
2452 * ships with Solaris 2.6), the output never has the prefix prepended.
2453 * contrast this with my development system (Digital Unix), which does.
2456 cs_resolve_file(i
, name
)
2464 * ppath is freed when we destroy the cscope connection.
2465 * fullname is freed after cs_make_vim_style_matches, after it's been
2466 * copied into the tag buffer used by vim
2468 len
= (int)(strlen(name
) + 2);
2469 if (csinfo
[i
].ppath
!= NULL
)
2470 len
+= (int)strlen(csinfo
[i
].ppath
);
2472 if ((fullname
= (char *)alloc(len
)) == NULL
)
2476 * note/example: this won't work if the cscope output already starts
2477 * "../.." and the prefix path is also "../..". if something like this
2478 * happens, you are screwed up and need to fix how you're using cscope.
2480 if (csinfo
[i
].ppath
!= NULL
&&
2481 (strncmp(name
, csinfo
[i
].ppath
, strlen(csinfo
[i
].ppath
)) != 0) &&
2484 && name
[0] != '\\' && name
[1] != ':'
2487 (void)sprintf(fullname
, "%s/%s", csinfo
[i
].ppath
, name
);
2489 (void)sprintf(fullname
, "%s", name
);
2492 } /* cs_resolve_file */
2498 * show all cscope connections
2506 if (cs_cnt_connections() == 0)
2507 MSG_PUTS(_("no cscope connections\n"));
2511 _(" # pid database name prepend path\n"),
2513 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2515 if (csinfo
[i
].fname
== NULL
)
2518 if (csinfo
[i
].ppath
!= NULL
)
2519 (void)smsg((char_u
*)"%2d %-5ld %-34s %-32s",
2520 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
, csinfo
[i
].ppath
);
2522 (void)smsg((char_u
*)"%2d %-5ld %-34s <none>",
2523 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
);
2528 return CSCOPE_SUCCESS
;
2535 * Only called when VIM exits to quit any cscope sessions.
2542 for (i
= 0; i
< CSCOPE_MAX_CONNECTIONS
; i
++)
2543 cs_release_csp(i
, TRUE
);
2546 #endif /* FEAT_CSCOPE */