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 clear_csinfo
__ARGS((int i
));
50 static int cs_insert_filelist
__ARGS((char *, char *, char *,
52 static int cs_kill
__ARGS((exarg_T
*eap
));
53 static void cs_kill_execute
__ARGS((int, char *));
54 static cscmd_T
* cs_lookup_cmd
__ARGS((exarg_T
*eap
));
55 static char * cs_make_vim_style_matches
__ARGS((char *, char *,
57 static char * cs_manage_matches
__ARGS((char **, char **, int, mcmd_e
));
58 static char * cs_parse_results
__ARGS((int cnumber
, char *buf
, int bufsize
, char **context
, char **linenumber
, char **search
));
59 static char * cs_pathcomponents
__ARGS((char *path
));
60 static void cs_print_tags_priv
__ARGS((char **, char **, int));
61 static int cs_read_prompt
__ARGS((int));
62 static void cs_release_csp
__ARGS((int, int freefnpp
));
63 static int cs_reset
__ARGS((exarg_T
*eap
));
64 static char * cs_resolve_file
__ARGS((int, char *));
65 static int cs_show
__ARGS((exarg_T
*eap
));
68 static csinfo_T
* csinfo
= NULL
;
69 static int csinfo_size
= 0; /* number of items allocated in
72 static int eap_arg_len
; /* length of eap->arg, set in
74 static cscmd_T cs_cmds
[] =
77 N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 },
79 N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 },
81 N_("Show this message"), "help", 0 },
83 N_("Kill a connection"), "kill #", 0 },
85 N_("Reinit all connections"), "reset", 0 },
87 N_("Show connections"), "show", 0 },
88 { NULL
, NULL
, NULL
, NULL
, 0 }
95 (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds
[(int)x
].usage
);
98 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
102 EXP_CSCOPE_SUBCMD
, /* expand ":cscope" sub-commands */
103 EXP_SCSCOPE_SUBCMD
, /* expand ":scscope" sub-commands */
104 EXP_CSCOPE_FIND
, /* expand ":cscope find" arguments */
105 EXP_CSCOPE_KILL
/* expand ":cscope kill" arguments */
109 * Function given to ExpandGeneric() to obtain the cscope command
113 get_cscope_name(xp
, idx
)
122 case EXP_CSCOPE_SUBCMD
:
123 /* Complete with sub-commands of ":cscope":
124 * add, find, help, kill, reset, show */
125 return (char_u
*)cs_cmds
[idx
].name
;
126 case EXP_SCSCOPE_SUBCMD
:
127 /* Complete with sub-commands of ":scscope": same sub-commands as
128 * ":cscope" but skip commands which don't support split windows */
129 for (i
= 0, current_idx
= 0; cs_cmds
[i
].name
!= NULL
; i
++)
130 if (cs_cmds
[i
].cansplit
)
131 if (current_idx
++ == idx
)
133 return (char_u
*)cs_cmds
[i
].name
;
134 case EXP_CSCOPE_FIND
:
136 const char *query_type
[] =
138 "c", "d", "e", "f", "g", "i", "s", "t", NULL
141 /* Complete with query type of ":cscope find {query_type}".
142 * {query_type} can be letters (c, d, ... t) or numbers (0, 1,
143 * ..., 8) but only complete with letters, since numbers are
145 return (char_u
*)query_type
[idx
];
147 case EXP_CSCOPE_KILL
:
149 static char connection
[5];
151 /* ":cscope kill" accepts connection numbers or partial names of
152 * the pathname of the cscope database as argument. Only complete
153 * with connection numbers. -1 can also be used to kill all
155 for (i
= 0, current_idx
= 0; i
< csinfo_size
; i
++)
157 if (csinfo
[i
].fname
== NULL
)
159 if (current_idx
++ == idx
)
161 vim_snprintf(connection
, sizeof(connection
), "%d", i
);
162 return (char_u
*)connection
;
165 return (current_idx
== idx
&& idx
> 0) ? (char_u
*)"-1" : NULL
;
173 * Handle command line completion for :cscope command.
176 set_context_in_cscope_cmd(xp
, arg
, cmdidx
)
183 /* Default: expand subcommands */
184 xp
->xp_context
= EXPAND_CSCOPE
;
185 xp
->xp_pattern
= arg
;
186 expand_what
= (cmdidx
== CMD_scscope
)
187 ? EXP_SCSCOPE_SUBCMD
: EXP_CSCOPE_SUBCMD
;
189 /* (part of) subcommand already typed */
192 p
= skiptowhite(arg
);
193 if (*p
!= NUL
) /* past first word */
195 xp
->xp_pattern
= skipwhite(p
);
196 if (*skiptowhite(xp
->xp_pattern
) != NUL
)
197 xp
->xp_context
= EXPAND_NOTHING
;
198 else if (STRNICMP(arg
, "add", p
- arg
) == 0)
199 xp
->xp_context
= EXPAND_FILES
;
200 else if (STRNICMP(arg
, "kill", p
- arg
) == 0)
201 expand_what
= EXP_CSCOPE_KILL
;
202 else if (STRNICMP(arg
, "find", p
- arg
) == 0)
203 expand_what
= EXP_CSCOPE_FIND
;
205 xp
->xp_context
= EXPAND_NOTHING
;
210 #endif /* FEAT_CMDL_COMPL */
213 * PRIVATE: do_cscope_general
215 * Find the command, print help if invalid, and then call the corresponding
219 do_cscope_general(eap
, make_split
)
221 int make_split
; /* whether to split window */
225 if ((cmdp
= cs_lookup_cmd(eap
)) == NULL
)
236 (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n"));
239 postponed_split
= -1;
240 postponed_split_flags
= cmdmod
.split
;
241 postponed_split_tab
= cmdmod
.tab
;
248 postponed_split_flags
= 0;
249 postponed_split_tab
= 0;
260 do_cscope_general(eap
, FALSE
);
266 * same as do_cscope, but splits window, too.
272 do_cscope_general(eap
, TRUE
);
285 if (*eap
->arg
== NUL
)
287 (void)EMSG(_("E562: Usage: cstag <ident>"));
294 if (cs_check_for_connections())
296 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
304 if (cs_check_for_tags())
305 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
308 else if (cs_check_for_tags())
310 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
314 if (cs_check_for_tags())
316 ret
= do_tag(eap
->arg
, DT_JUMP
, 0, eap
->forceit
, FALSE
);
322 if (cs_check_for_connections())
324 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
,
331 else if (cs_check_for_connections())
333 ret
= cs_find_common("g", (char *)(eap
->arg
), eap
->forceit
, FALSE
,
345 (void)EMSG(_("E257: cstag: tag not found"));
346 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357 * this simulates a vim_fgets(), but for cscope, returns the next line
358 * from the cscope output. should only be called from find_tags()
360 * returns TRUE if eof, FALSE otherwise
369 if ((p
= cs_manage_matches(NULL
, NULL
, -1, Get
)) == NULL
)
371 vim_strncpy(buf
, (char_u
*)p
, size
- 1);
378 * PUBLIC: cs_free_tags
380 * called only from do_tag(), when popping the tag stack
385 cs_manage_matches(NULL
, NULL
, -1, Free
);
390 * PUBLIC: cs_print_tags
392 * called from do_tag()
397 cs_manage_matches(NULL
, NULL
, -1, Print
);
402 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
404 * Checks for the existence of a |cscope| connection. If no
405 * parameters are specified, then the function returns:
407 * 0, if cscope was not available (not compiled in), or if there
408 * are no cscope connections; or
409 * 1, if there is at least one cscope connection.
411 * If parameters are specified, then the value of {num}
412 * determines how existence of a cscope connection is checked:
414 * {num} Description of existence check
415 * ----- ------------------------------
416 * 0 Same as no parameters (e.g., "cscope_connection()").
417 * 1 Ignore {prepend}, and use partial string matches for
419 * 2 Ignore {prepend}, and use exact string matches for
421 * 3 Use {prepend}, use partial string matches for both
422 * {dbpath} and {prepend}.
423 * 4 Use {prepend}, use exact string matches for both
424 * {dbpath} and {prepend}.
426 * Note: All string comparisons are case sensitive!
428 #if defined(FEAT_EVAL) || defined(PROTO)
430 cs_connection(num
, dbpath
, ppath
)
437 if (num
< 0 || num
> 4 || (num
> 0 && !dbpath
))
440 for (i
= 0; i
< csinfo_size
; i
++)
442 if (!csinfo
[i
].fname
)
451 if (strstr(csinfo
[i
].fname
, (char *)dbpath
))
455 if (strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
459 if (strstr(csinfo
[i
].fname
, (char *)dbpath
)
460 && ((!ppath
&& !csinfo
[i
].ppath
)
463 && strstr(csinfo
[i
].ppath
, (char *)ppath
))))
467 if ((strcmp(csinfo
[i
].fname
, (char *)dbpath
) == 0)
468 && ((!ppath
&& !csinfo
[i
].ppath
)
471 && (strcmp(csinfo
[i
].ppath
, (char *)ppath
) == 0))))
478 } /* cs_connection */
484 ****************************************************************************/
489 * add cscope database or a directory name (to look for cscope.out)
490 * to the cscope connection list
498 char *fname
, *ppath
, *flags
= NULL
;
500 if ((fname
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
503 return CSCOPE_FAILURE
;
505 if ((ppath
= strtok((char *)NULL
, (const char *)" ")) != NULL
)
506 flags
= strtok((char *)NULL
, (const char *)" ");
508 return cs_add_common(fname
, ppath
, flags
);
515 char *stat_emsg
= _("E563: stat(%s) error: %d");
516 char *buf
= (char *)alloc((unsigned)strlen(stat_emsg
) + MAXPATHL
+ 10);
520 (void)sprintf(buf
, stat_emsg
, fname
, errno
);
525 (void)EMSG(_("E563: stat error"));
530 * PRIVATE: cs_add_common
532 * the common routine to add a new cscope connection. called by
533 * cs_add() and cs_reset(). i really don't like to do this, but this
534 * routine uses a number of goto statements.
537 cs_add_common(arg1
, arg2
, flags
)
538 char *arg1
; /* filename - may contain environment variables */
539 char *arg2
; /* prepend path - may contain environment variables */
549 /* get the filename (arg1), expand it, and try to stat it */
550 if ((fname
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
553 expand_env((char_u
*)arg1
, (char_u
*)fname
, MAXPATHL
);
554 ret
= stat(fname
, &statbuf
);
563 /* get the prepend path (arg2), expand it, and try to stat it */
566 struct stat statbuf2
;
568 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
571 expand_env((char_u
*)arg2
, (char_u
*)ppath
, MAXPATHL
);
572 ret
= stat(ppath
, &statbuf2
);
577 /* if filename is a directory, append the cscope database name to it */
578 if ((statbuf
.st_mode
& S_IFMT
) == S_IFDIR
)
580 fname2
= (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE
) + strlen(fname
) + 2));
584 while (fname
[strlen(fname
)-1] == '/'
586 || fname
[strlen(fname
)-1] == '\\'
590 fname
[strlen(fname
)-1] = '\0';
591 if (strlen(fname
) == 0)
594 if (fname
[0] == '\0')
595 (void)sprintf(fname2
, "/%s", CSCOPE_DBFILE
);
597 (void)sprintf(fname2
, "%s/%s", fname
, CSCOPE_DBFILE
);
599 ret
= stat(fname2
, &statbuf
);
603 cs_stat_emsg(fname2
);
607 i
= cs_insert_filelist(fname2
, ppath
, flags
, &statbuf
);
610 else if (S_ISREG(statbuf
.st_mode
) || S_ISLNK(statbuf
.st_mode
))
612 /* WIN32 - substitute define S_ISREG from os_unix.h */
613 else if (((statbuf
.st_mode
) & S_IFMT
) == S_IFREG
)
616 i
= cs_insert_filelist(fname
, ppath
, flags
, &statbuf
);
622 _("E564: %s is not a directory or a valid cscope database"),
629 if (cs_create_connection(i
) == CSCOPE_FAILURE
630 || cs_read_prompt(i
) == CSCOPE_FAILURE
)
632 cs_release_csp(i
, TRUE
);
639 (void)smsg_attr(hl_attr(HLF_R
),
640 (char_u
*)_("Added cscope database %s"),
648 return CSCOPE_SUCCESS
;
654 return CSCOPE_FAILURE
;
655 } /* cs_add_common */
659 cs_check_for_connections()
661 return (cs_cnt_connections() > 0);
662 } /* cs_check_for_connections */
668 return (p_tags
[0] != NUL
&& curbuf
->b_p_tags
!= NULL
);
669 } /* cs_check_for_tags */
673 * PRIVATE: cs_cnt_connections
675 * count the number of cscope connections
683 for (i
= 0; i
< csinfo_size
; i
++)
685 if (csinfo
[i
].fname
!= NULL
)
689 } /* cs_cnt_connections */
693 int idx
; /* connection index */
695 EMSGN(_("E262: error reading cscope connection %ld"), idx
);
698 #define CSREAD_BUFSIZE 2048
700 * PRIVATE: cs_cnt_matches
702 * count the number of matches for a given cscope connection.
712 buf
= (char *)alloc(CSREAD_BUFSIZE
);
717 if (!fgets(buf
, CSREAD_BUFSIZE
, csinfo
[idx
].fr_fp
))
719 if (feof(csinfo
[idx
].fr_fp
))
722 cs_reading_emsg(idx
);
729 * If the database is out of date, or there's some other problem,
730 * cscope will output error messages before the number-of-lines output.
731 * Display/discard any output that doesn't match what we want.
732 * Accept "\S*cscope: X lines", also matches "mlcscope".
734 if ((stok
= strtok(buf
, (const char *)" ")) == NULL
)
736 if (strstr((const char *)stok
, "cscope:") == NULL
)
739 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
748 if ((stok
= strtok(NULL
, (const char *)" ")) == NULL
)
750 if (strncmp((const char *)stok
, "lines", 5))
758 } /* cs_cnt_matches */
762 * PRIVATE: cs_create_cmd
764 * Creates the actual cscope command query from what the user entered.
767 cs_create_cmd(csoption
, pattern
)
777 case '0' : case 's' :
780 case '1' : case 'g' :
783 case '2' : case 'd' :
786 case '3' : case 'c' :
789 case '4' : case 't' :
792 case '6' : case 'e' :
795 case '7' : case 'f' :
798 case '8' : case 'i' :
802 (void)EMSG(_("E561: unknown cscope search type"));
807 /* Skip white space before the patter, except for text and pattern search,
808 * they may want to use the leading white space. */
810 if (search
!= 4 && search
!= 6)
811 while vim_iswhite(*pat
)
814 if ((cmd
= (char *)alloc((unsigned)(strlen(pat
) + 2))) == NULL
)
817 (void)sprintf(cmd
, "%d%s", search
, pat
);
820 } /* cs_create_cmd */
824 * PRIVATE: cs_create_connection
826 * This piece of code was taken/adapted from nvi. do we need to add
827 * the BSD license notice?
830 cs_create_connection(i
)
834 int to_cs
[2], from_cs
[2];
837 char *prog
, *cmd
, *ppath
= NULL
;
840 SECURITY_ATTRIBUTES sa
;
841 PROCESS_INFORMATION pi
;
843 BOOL pipe_stdin
= FALSE
, pipe_stdout
= FALSE
;
844 HANDLE stdin_rd
, stdout_rd
;
845 HANDLE stdout_wr
, stdin_wr
;
848 # define OPEN_OH_ARGTYPE long
850 # if (_MSC_VER >= 1300)
851 # define OPEN_OH_ARGTYPE intptr_t
853 # define OPEN_OH_ARGTYPE long
860 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
861 * from_cs[0] and writes to to_cs[1].
863 to_cs
[0] = to_cs
[1] = from_cs
[0] = from_cs
[1] = -1;
864 if (pipe(to_cs
) < 0 || pipe(from_cs
) < 0)
866 (void)EMSG(_("E566: Could not create cscope pipes"));
869 (void)close(to_cs
[0]);
871 (void)close(to_cs
[1]);
872 if (from_cs
[0] != -1)
873 (void)close(from_cs
[0]);
874 if (from_cs
[1] != -1)
875 (void)close(from_cs
[1]);
876 return CSCOPE_FAILURE
;
879 switch (csinfo
[i
].pid
= fork())
882 (void)EMSG(_("E622: Could not fork for cscope"));
884 case 0: /* child: run cscope. */
885 if (dup2(to_cs
[0], STDIN_FILENO
) == -1)
886 PERROR("cs_create_connection 1");
887 if (dup2(from_cs
[1], STDOUT_FILENO
) == -1)
888 PERROR("cs_create_connection 2");
889 if (dup2(from_cs
[1], STDERR_FILENO
) == -1)
890 PERROR("cs_create_connection 3");
893 (void)close(to_cs
[1]);
894 (void)close(from_cs
[0]);
897 /* Create pipes to communicate with cscope */
898 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
899 sa
.bInheritHandle
= TRUE
;
900 sa
.lpSecurityDescriptor
= NULL
;
902 if (!(pipe_stdin
= CreatePipe(&stdin_rd
, &stdin_wr
, &sa
, 0))
903 || !(pipe_stdout
= CreatePipe(&stdout_rd
, &stdout_wr
, &sa
, 0)))
905 (void)EMSG(_("E566: Could not create cscope pipes"));
909 CloseHandle(stdin_rd
);
910 CloseHandle(stdin_wr
);
914 CloseHandle(stdout_rd
);
915 CloseHandle(stdout_wr
);
917 return CSCOPE_FAILURE
;
920 /* expand the cscope exec for env var's */
921 if ((prog
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
924 return CSCOPE_FAILURE
;
930 expand_env((char_u
*)p_csprg
, (char_u
*)prog
, MAXPATHL
);
932 /* alloc space to hold the cscope command */
933 len
= (int)(strlen(prog
) + strlen(csinfo
[i
].fname
) + 32);
936 /* expand the prepend path for env var's */
937 if ((ppath
= (char *)alloc(MAXPATHL
+ 1)) == NULL
)
941 return CSCOPE_FAILURE
;
947 expand_env((char_u
*)csinfo
[i
].ppath
, (char_u
*)ppath
, MAXPATHL
);
949 len
+= (int)strlen(ppath
);
953 len
+= (int)strlen(csinfo
[i
].flags
);
955 if ((cmd
= (char *)alloc(len
)) == NULL
)
960 return CSCOPE_FAILURE
;
967 /* run the cscope command; is there execl for non-unix systems? */
969 (void)sprintf(cmd
, "exec %s -dl -f %s", prog
, csinfo
[i
].fname
);
972 (void)sprintf(cmd
, "%s -dl -f %s", prog
, csinfo
[i
].fname
);
974 if (csinfo
[i
].ppath
!= NULL
)
976 (void)strcat(cmd
, " -P");
977 (void)strcat(cmd
, csinfo
[i
].ppath
);
979 if (csinfo
[i
].flags
!= NULL
)
981 (void)strcat(cmd
, " ");
982 (void)strcat(cmd
, csinfo
[i
].flags
);
985 /* on Win32 we still need prog */
991 if (execl("/bin/sh", "sh", "-c", cmd
, (char *)NULL
) == -1)
992 PERROR(_("cs_create_connection exec failed"));
996 default: /* parent. */
998 * Save the file descriptors for later duplication, and
1001 if ((csinfo
[i
].to_fp
= fdopen(to_cs
[1], "w")) == NULL
)
1002 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1003 if ((csinfo
[i
].fr_fp
= fdopen(from_cs
[0], "r")) == NULL
)
1004 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1007 (void)close(to_cs
[0]);
1008 (void)close(from_cs
[1]);
1015 /* Create a new process to run cscope and use pipes to talk with it */
1016 GetStartupInfo(&si
);
1017 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
1018 si
.wShowWindow
= SW_HIDE
; /* Hide child application window */
1019 si
.hStdOutput
= stdout_wr
;
1020 si
.hStdError
= stdout_wr
;
1021 si
.hStdInput
= stdin_rd
;
1022 created
= CreateProcess(NULL
, cmd
, NULL
, NULL
, TRUE
, CREATE_NEW_CONSOLE
,
1023 NULL
, NULL
, &si
, &pi
);
1029 PERROR(_("cs_create_connection exec failed"));
1030 (void)EMSG(_("E623: Could not spawn cscope process"));
1034 csinfo
[i
].pid
= pi
.dwProcessId
;
1035 csinfo
[i
].hProc
= pi
.hProcess
;
1036 CloseHandle(pi
.hThread
);
1038 /* TODO - tidy up after failure to create files on pipe handles. */
1039 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdin_wr
,
1040 _O_TEXT
|_O_APPEND
)) < 0)
1041 || ((csinfo
[i
].to_fp
= _fdopen(fd
, "w")) == NULL
))
1042 PERROR(_("cs_create_connection: fdopen for to_fp failed"));
1043 if (((fd
= _open_osfhandle((OPEN_OH_ARGTYPE
)stdout_rd
,
1044 _O_TEXT
|_O_RDONLY
)) < 0)
1045 || ((csinfo
[i
].fr_fp
= _fdopen(fd
, "r")) == NULL
))
1046 PERROR(_("cs_create_connection: fdopen for fr_fp failed"));
1048 /* Close handles for file descriptors inherited by the cscope process */
1049 CloseHandle(stdin_rd
);
1050 CloseHandle(stdout_wr
);
1054 return CSCOPE_SUCCESS
;
1055 } /* cs_create_connection */
1061 * query cscope using command line interface. parse the output and use tselect
1062 * to allow choices. like Nvi, creates a pipe to send to/from query/cscope.
1064 * returns TRUE if we jump to a tag or abort, FALSE if not.
1072 if (cs_check_for_connections() == FALSE
)
1074 (void)EMSG(_("E567: no cscope connections"));
1078 if ((opt
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1084 pat
= opt
+ strlen(opt
) + 1;
1085 if (pat
>= (char *)eap
->arg
+ eap_arg_len
)
1091 return cs_find_common(opt
, pat
, eap
->forceit
, TRUE
,
1092 eap
->cmdidx
== CMD_lcscope
);
1097 * PRIVATE: cs_find_common
1099 * common code for cscope find, shared by cs_find() and do_cstag()
1102 cs_find_common(opt
, pat
, forceit
, verbose
, use_ll
)
1113 #ifdef FEAT_QUICKFIX
1118 /* create the actual command to send to cscope */
1119 cmd
= cs_create_cmd(opt
, pat
);
1123 nummatches
= (int *)alloc(sizeof(int)*csinfo_size
);
1124 if (nummatches
== NULL
)
1127 /* send query to all open connections, then count the total number
1128 * of matches so we can alloc matchesp all in one swell foop
1130 for (i
= 0; i
< csinfo_size
; i
++)
1133 for (i
= 0; i
< csinfo_size
; i
++)
1135 if (csinfo
[i
].fname
== NULL
|| csinfo
[i
].to_fp
== NULL
)
1138 /* send cmd to cscope */
1139 (void)fprintf(csinfo
[i
].to_fp
, "%s\n", cmd
);
1140 (void)fflush(csinfo
[i
].to_fp
);
1142 nummatches
[i
] = cs_cnt_matches(i
);
1144 if (nummatches
[i
] > -1)
1145 totmatches
+= nummatches
[i
];
1147 if (nummatches
[i
] == 0)
1148 (void)cs_read_prompt(i
);
1152 if (totmatches
== 0)
1154 char *nf
= _("E259: no matches found for cscope query %s of %s");
1159 vim_free(nummatches
);
1163 buf
= (char *)alloc((unsigned)(strlen(opt
) + strlen(pat
) + strlen(nf
)));
1168 sprintf(buf
, nf
, opt
, pat
);
1172 vim_free(nummatches
);
1176 #ifdef FEAT_QUICKFIX
1177 /* get cmd letter */
1208 qfpos
= (char *)vim_strchr(p_csqf
, cmdletter
);
1212 /* next symbol must be + or - */
1213 if (strchr(CSQF_FLAGS
, *qfpos
) == NULL
)
1215 char *nf
= _("E469: invalid cscopequickfix flag %c for %c");
1216 char *buf
= (char *)alloc((unsigned)strlen(nf
));
1218 /* strlen will be enough because we use chars */
1221 sprintf(buf
, nf
, *qfpos
, *(qfpos
-1));
1225 vim_free(nummatches
);
1229 if (qfpos
!= NULL
&& *qfpos
!= '0' && totmatches
> 0)
1231 /* fill error list */
1233 char_u
*tmp
= vim_tempname('c');
1234 qf_info_T
*qi
= NULL
;
1237 f
= mch_fopen((char *)tmp
, "w");
1239 EMSG2(_(e_notopen
), tmp
);
1242 cs_file_results(f
, nummatches
);
1244 if (use_ll
) /* Use location list */
1246 /* '-' starts a new error list */
1247 if (qf_init(wp
, tmp
, (char_u
*)"%f%*\\t%l%*\\t%m",
1250 # ifdef FEAT_WINDOWS
1251 if (postponed_split
!= 0)
1253 win_split(postponed_split
> 0 ? postponed_split
: 0,
1254 postponed_split_flags
);
1255 # ifdef FEAT_SCROLLBIND
1256 curwin
->w_p_scb
= FALSE
;
1258 postponed_split
= 0;
1263 * In the location list window, use the displayed location
1264 * list. Otherwise, use the location list for the window.
1266 qi
= (bt_quickfix(wp
->w_buffer
) && wp
->w_llist_ref
!= NULL
)
1267 ? wp
->w_llist_ref
: wp
->w_llist
;
1268 qf_jump(qi
, 0, 0, forceit
);
1273 vim_free(nummatches
);
1277 #endif /* FEAT_QUICKFIX */
1279 char **matches
= NULL
, **contexts
= NULL
;
1283 cs_fill_results((char *)pat
, totmatches
, nummatches
, &matches
,
1284 &contexts
, &matched
);
1285 vim_free(nummatches
);
1286 if (matches
== NULL
)
1289 (void)cs_manage_matches(matches
, contexts
, matched
, Store
);
1291 return do_tag((char_u
*)pat
, DT_CSCOPE
, 0, forceit
, verbose
);
1294 } /* cs_find_common */
1303 exarg_T
*eap UNUSED
;
1305 cscmd_T
*cmdp
= cs_cmds
;
1307 (void)MSG_PUTS(_("cscope commands:\n"));
1308 while (cmdp
->name
!= NULL
)
1310 char *help
= _(cmdp
->help
);
1311 int space_cnt
= 30 - vim_strsize((char_u
*)help
);
1313 /* Use %*s rather than %30s to ensure proper alignment in utf-8 */
1316 (void)smsg((char_u
*)_("%-5s: %s%*s (Usage: %s)"),
1318 help
, space_cnt
, " ",
1320 if (strcmp(cmdp
->name
, "find") == 0)
1322 " c: Find functions calling this function\n"
1323 " d: Find functions called by this function\n"
1324 " e: Find this egrep pattern\n"
1325 " f: Find this file\n"
1326 " g: Find this definition\n"
1327 " i: Find files #including this file\n"
1328 " s: Find this C symbol\n"
1329 " t: Find assignments to\n"));
1343 csinfo
[i
].fname
= NULL
;
1344 csinfo
[i
].ppath
= NULL
;
1345 csinfo
[i
].flags
= NULL
;
1347 csinfo
[i
].st_dev
= (dev_t
)0;
1348 csinfo
[i
].st_ino
= (ino_t
)0;
1350 csinfo
[i
].nVolume
= 0;
1351 csinfo
[i
].nIndexHigh
= 0;
1352 csinfo
[i
].nIndexLow
= 0;
1355 csinfo
[i
].fr_fp
= NULL
;
1356 csinfo
[i
].to_fp
= NULL
;
1358 csinfo
[i
].hProc
= NULL
;
1363 static char *GetWin32Error
__ARGS((void));
1369 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
1370 NULL
, GetLastError(), 0, (LPSTR
)&msg
, 0, NULL
);
1373 /* remove trailing \r\n */
1374 char *pcrlf
= strstr(msg
, "\r\n");
1383 * PRIVATE: cs_insert_filelist
1385 * insert a new cscope database filename into the filelist
1388 cs_insert_filelist(fname
, ppath
, flags
, sb
)
1392 struct stat
*sb UNUSED
;
1397 BY_HANDLE_FILE_INFORMATION bhfi
;
1399 vim_memset(&bhfi
, 0, sizeof(bhfi
));
1400 /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */
1401 if (!mch_windows95())
1403 hFile
= CreateFile(fname
, FILE_READ_ATTRIBUTES
, 0, NULL
, OPEN_EXISTING
,
1404 FILE_ATTRIBUTE_NORMAL
, NULL
);
1405 if (hFile
== INVALID_HANDLE_VALUE
)
1409 char *cant_msg
= _("E625: cannot open cscope database: %s");
1410 char *winmsg
= GetWin32Error();
1414 (void)EMSG2(cant_msg
, winmsg
);
1418 /* subst filename if can't get error text */
1419 (void)EMSG2(cant_msg
, fname
);
1423 if (!GetFileInformationByHandle(hFile
, &bhfi
))
1427 (void)EMSG(_("E626: cannot get cscope database information"));
1434 i
= -1; /* can be set to the index of an empty item in csinfo */
1435 for (j
= 0; j
< csinfo_size
; j
++)
1437 if (csinfo
[j
].fname
!= NULL
1439 && csinfo
[j
].st_dev
== sb
->st_dev
&& csinfo
[j
].st_ino
== sb
->st_ino
1441 /* compare pathnames first */
1442 && ((fullpathcmp(csinfo
[j
].fname
, fname
, FALSE
) & FPC_SAME
)
1443 /* if not Windows 9x, test index file attributes too */
1444 || (!mch_windows95()
1445 && csinfo
[j
].nVolume
== bhfi
.dwVolumeSerialNumber
1446 && csinfo
[j
].nIndexHigh
== bhfi
.nFileIndexHigh
1447 && csinfo
[j
].nIndexLow
== bhfi
.nFileIndexLow
))
1452 (void)EMSG(_("E568: duplicate cscope database not added"));
1456 if (csinfo
[j
].fname
== NULL
&& i
== -1)
1457 i
= j
; /* remember first empty entry */
1463 if (csinfo_size
== 0)
1465 /* First time allocation: allocate only 1 connection. It should
1466 * be enough for most users. If more is needed, csinfo will be
1469 csinfo
= (csinfo_T
*)alloc_clear(sizeof(csinfo_T
));
1473 /* Reallocate space for more connections. */
1475 csinfo
= vim_realloc(csinfo
, sizeof(csinfo_T
)*csinfo_size
);
1479 for (j
= csinfo_size
/2; j
< csinfo_size
; j
++)
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 */
1566 exarg_T
*eap UNUSED
;
1571 if ((stok
= strtok((char *)NULL
, (const char *)" ")) == NULL
)
1574 return CSCOPE_FAILURE
;
1577 /* only single digit positive and negative integers are allowed */
1578 if ((strlen(stok
) < 2 && VIM_ISDIGIT((int)(stok
[0])))
1579 || (strlen(stok
) < 3 && stok
[0] == '-'
1580 && VIM_ISDIGIT((int)(stok
[1]))))
1584 /* It must be part of a name. We will try to find a match
1585 * within all the names in the csinfo data structure
1587 for (i
= 0; i
< csinfo_size
; i
++)
1589 if (csinfo
[i
].fname
!= NULL
&& strstr(csinfo
[i
].fname
, stok
))
1594 if ((i
!= -1) && (i
>= csinfo_size
|| i
< -1 || csinfo
[i
].fname
== NULL
))
1597 (void)EMSG2(_("E261: cscope connection %s not found"), stok
);
1603 for (i
= 0; i
< csinfo_size
; i
++)
1605 if (csinfo
[i
].fname
)
1606 cs_kill_execute(i
, csinfo
[i
].fname
);
1610 cs_kill_execute(i
, stok
);
1618 * PRIVATE: cs_kill_execute
1620 * Actually kills a specific cscope connection.
1623 cs_kill_execute(i
, cname
)
1624 int i
; /* cscope table index */
1625 char *cname
; /* cscope database name */
1630 (void)smsg_attr(hl_attr(HLF_R
) | MSG_HIST
,
1631 (char_u
*)_("cscope connection %s closed"), cname
);
1633 cs_release_csp(i
, TRUE
);
1638 * PRIVATE: cs_make_vim_style_matches
1640 * convert the cscope output into into a ctags style entry (as might be found
1641 * in a ctags tags file). there's one catch though: cscope doesn't tell you
1642 * the type of the tag you are looking for. for example, in Darren Hiebert's
1643 * ctags (the one that comes with vim), #define's use a line number to find the
1644 * tag in a file while function definitions use a regexp search pattern.
1646 * i'm going to always use the line number because cscope does something
1647 * quirky (and probably other things i don't know about):
1649 * if you have "# define" in your source file, which is
1650 * perfectly legal, cscope thinks you have "#define". this
1651 * will result in a failed regexp search. :(
1653 * besides, even if this particular case didn't happen, the search pattern
1654 * would still have to be modified to escape all the special regular expression
1655 * characters to comply with ctags formatting.
1658 cs_make_vim_style_matches(fname
, slno
, search
, tagstr
)
1664 /* vim style is ctags:
1666 * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra>
1668 * but as mentioned above, we'll always use the line number and
1669 * put the search pattern (if one exists) as "extra"
1671 * buf is used as part of vim's method of handling tags, and
1672 * (i think) vim frees it when you pop your tags and get replaced
1673 * by new ones on the tag stack.
1680 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + strlen(search
)+6);
1681 if ((buf
= (char *)alloc(amt
)) == NULL
)
1684 (void)sprintf(buf
, "%s\t%s\t%s;\"\t%s", tagstr
, fname
, slno
, search
);
1688 amt
= (int)(strlen(fname
) + strlen(slno
) + strlen(tagstr
) + 5);
1689 if ((buf
= (char *)alloc(amt
)) == NULL
)
1692 (void)sprintf(buf
, "%s\t%s\t%s;\"", tagstr
, fname
, slno
);
1696 } /* cs_make_vim_style_matches */
1700 * PRIVATE: cs_manage_matches
1702 * this is kind of hokey, but i don't see an easy way round this..
1704 * Store: keep a ptr to the (malloc'd) memory of matches originally
1705 * generated from cs_find(). the matches are originally lines directly
1706 * from cscope output, but transformed to look like something out of a
1707 * ctags. see cs_make_vim_style_matches for more details.
1709 * Get: used only from cs_fgets(), this simulates a vim_fgets() to return
1710 * the next line from the cscope output. it basically keeps track of which
1711 * lines have been "used" and returns the next one.
1713 * Free: frees up everything and resets
1715 * Print: prints the tags
1718 cs_manage_matches(matches
, contexts
, totmatches
, cmd
)
1724 static char **mp
= NULL
;
1725 static char **cp
= NULL
;
1726 static int cnt
= -1;
1727 static int next
= -1;
1733 assert(matches
!= NULL
);
1734 assert(totmatches
> 0);
1735 if (mp
!= NULL
|| cp
!= NULL
)
1736 (void)cs_manage_matches(NULL
, NULL
, -1, Free
);
1768 cs_print_tags_priv(mp
, cp
, cnt
);
1770 default: /* should not reach here */
1771 (void)EMSG(_("E570: fatal error in cs_manage_matches"));
1776 } /* cs_manage_matches */
1780 * PRIVATE: cs_parse_results
1782 * parse cscope output
1785 cs_parse_results(cnumber
, buf
, bufsize
, context
, linenumber
, search
)
1797 if (fgets(buf
, bufsize
, csinfo
[cnumber
].fr_fp
) == NULL
)
1799 if (feof(csinfo
[cnumber
].fr_fp
))
1802 cs_reading_emsg(cnumber
);
1807 /* If the line's too long for the buffer, discard it. */
1808 if ((p
= strchr(buf
, '\n')) == NULL
)
1810 while ((ch
= getc(csinfo
[cnumber
].fr_fp
)) != EOF
&& ch
!= '\n')
1817 * cscope output is in the following format:
1819 * <filename> <context> <line number> <pattern>
1821 if ((name
= strtok((char *)buf
, (const char *)" ")) == NULL
)
1823 if ((*context
= strtok(NULL
, (const char *)" ")) == NULL
)
1825 if ((*linenumber
= strtok(NULL
, (const char *)" ")) == NULL
)
1827 *search
= *linenumber
+ strlen(*linenumber
) + 1; /* +1 to skip \0 */
1830 * If the file is older than the cscope database, that is,
1831 * the database was built since the file was last modified,
1832 * or there wasn't a search string, use the line number.
1834 if (strcmp(*search
, "<unknown>") == 0)
1837 name
= cs_resolve_file(cnumber
, name
);
1841 #ifdef FEAT_QUICKFIX
1843 * PRIVATE: cs_file_results
1845 * write cscope find results to file
1848 cs_file_results(f
, nummatches_a
)
1854 char *search
, *slno
;
1859 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1863 for (i
= 0; i
< csinfo_size
; i
++)
1865 if (nummatches_a
[i
] < 1)
1868 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1870 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1871 &slno
, &search
)) == NULL
)
1874 context
= (char *)alloc((unsigned)strlen(cntx
)+5);
1875 if (context
== NULL
)
1878 if (strcmp(cntx
, "<global>")==0)
1879 strcpy(context
, "<<global>>");
1881 sprintf(context
, "<<%s>>", cntx
);
1884 fprintf(f
, "%s\t%s\t%s\n", fullname
, slno
, context
);
1886 fprintf(f
, "%s\t%s\t%s %s\n", fullname
, slno
, context
, search
);
1890 } /* for all matches */
1892 (void)cs_read_prompt(i
);
1894 } /* for all cscope connections */
1900 * PRIVATE: cs_fill_results
1902 * get parsed cscope output and calls cs_make_vim_style_matches to convert
1904 * When there are no matches sets "*matches_p" to NULL.
1907 cs_fill_results(tagstr
, totmatches
, nummatches_a
, matches_p
, cntxts_p
, matched
)
1917 char *search
, *slno
;
1919 char **matches
= NULL
;
1920 char **cntxts
= NULL
;
1924 assert(totmatches
> 0);
1926 buf
= (char *)alloc(CSREAD_BUFSIZE
);
1930 if ((matches
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1932 if ((cntxts
= (char **)alloc(sizeof(char *) * totmatches
)) == NULL
)
1935 for (i
= 0; i
< csinfo_size
; i
++)
1937 if (nummatches_a
[i
] < 1)
1940 for (j
= 0; j
< nummatches_a
[i
]; j
++)
1942 if ((fullname
= cs_parse_results(i
, buf
, CSREAD_BUFSIZE
, &cntx
,
1943 &slno
, &search
)) == NULL
)
1946 matches
[totsofar
] = cs_make_vim_style_matches(fullname
, slno
,
1951 if (strcmp(cntx
, "<global>") == 0)
1952 cntxts
[totsofar
] = NULL
;
1954 /* note: if vim_strsave returns NULL, then the context
1955 * will be "<global>", which is misleading.
1957 cntxts
[totsofar
] = (char *)vim_strsave((char_u
*)cntx
);
1959 if (matches
[totsofar
] != NULL
)
1962 } /* for all matches */
1964 (void)cs_read_prompt(i
);
1966 } /* for all cscope connections */
1971 /* No matches, free the arrays and return NULL in "*matches_p". */
1977 *matched
= totsofar
;
1978 *matches_p
= matches
;
1982 } /* cs_fill_results */
1985 /* get the requested path components */
1987 cs_pathcomponents(path
)
1996 s
= path
+ strlen(path
) - 1;
1997 for (i
= 0; i
< p_cspc
; ++i
)
1998 while (s
> path
&& *--s
!= '/'
2004 if ((s
> path
&& *s
== '/')
2006 || (s
> path
&& *s
== '\\')
2014 * PRIVATE: cs_print_tags_priv
2016 * called from cs_manage_matches()
2019 cs_print_tags_priv(matches
, cntxts
, num_matches
)
2025 int bufsize
= 0; /* Track available bufsize */
2028 char *fname
, *lno
, *extra
, *tbuf
;
2030 char *globalcntx
= "GLOBAL";
2031 char *cntxformat
= " <<%s>>";
2033 char *cstag_msg
= _("Cscope tag: %s");
2034 char *csfmt_str
= "%4d %6s ";
2036 assert (num_matches
> 0);
2038 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[0]) + 1)) == NULL
)
2041 strcpy(tbuf
, matches
[0]);
2042 ptag
= strtok(tbuf
, "\t");
2044 newsize
= (int)(strlen(cstag_msg
) + strlen(ptag
));
2045 buf
= (char *)alloc(newsize
);
2049 (void)sprintf(buf
, cstag_msg
, ptag
);
2050 MSG_PUTS_ATTR(buf
, hl_attr(HLF_T
));
2055 MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T
)); /* strlen is 7 */
2056 msg_advance(msg_col
+ 2);
2057 MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T
));
2060 for (i
= 0; i
< num_matches
; i
++)
2064 /* if we really wanted to, we could avoid this malloc and strcpy
2065 * by parsing matches[i] on the fly and placing stuff into buf
2066 * directly, but that's too much of a hassle
2068 if ((tbuf
= (char *)alloc((unsigned)strlen(matches
[idx
]) + 1)) == NULL
)
2070 (void)strcpy(tbuf
, matches
[idx
]);
2072 if (strtok(tbuf
, (const char *)"\t") == NULL
)
2074 if ((fname
= strtok(NULL
, (const char *)"\t")) == NULL
)
2076 if ((lno
= strtok(NULL
, (const char *)"\t")) == NULL
)
2078 extra
= strtok(NULL
, (const char *)"\t");
2080 lno
[strlen(lno
)-2] = '\0'; /* ignore ;" at the end */
2082 /* hopefully 'num' (num of matches) will be less than 10^16 */
2083 newsize
= (int)(strlen(csfmt_str
) + 16 + strlen(lno
));
2084 if (bufsize
< newsize
)
2086 buf
= (char *)vim_realloc(buf
, newsize
);
2094 /* csfmt_str = "%4d %6s "; */
2095 (void)sprintf(buf
, csfmt_str
, num
, lno
);
2096 MSG_PUTS_ATTR(buf
, hl_attr(HLF_CM
));
2098 MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname
), hl_attr(HLF_CM
));
2100 /* compute the required space for the context */
2101 if (cntxts
[idx
] != NULL
)
2102 context
= cntxts
[idx
];
2104 context
= globalcntx
;
2105 newsize
= (int)(strlen(context
) + strlen(cntxformat
));
2107 if (bufsize
< newsize
)
2109 buf
= (char *)vim_realloc(buf
, newsize
);
2117 (void)sprintf(buf
, cntxformat
, context
);
2119 /* print the context only if it fits on the same line */
2120 if (msg_col
+ (int)strlen(buf
) >= (int)Columns
)
2129 MSG_PUTS_LONG(extra
);
2132 vim_free(tbuf
); /* only after printing extra due to strtok use */
2140 got_int
= FALSE
; /* don't print any more matches */
2145 } /* for all matches */
2148 } /* cs_print_tags_priv */
2152 * PRIVATE: cs_read_prompt
2154 * read a cscope prompt (basically, skip over the ">> ")
2161 char *buf
= NULL
; /* buffer for possible error message from cscope */
2165 static char *eprompt
= "Press the RETURN key to continue:";
2166 int epromptlen
= (int)strlen(eprompt
);
2169 cs_emsg
= _("E609: Cscope error: %s");
2170 /* compute maximum allowed len for Cscope error message */
2171 maxlen
= (int)(IOSIZE
- strlen(cs_emsg
));
2175 while ((ch
= getc(csinfo
[i
].fr_fp
)) != EOF
&& ch
!= CSCOPE_PROMPT
[0])
2176 /* if there is room and char is printable */
2177 if (bufpos
< maxlen
- 1 && vim_isprintc(ch
))
2179 if (buf
== NULL
) /* lazy buffer allocation */
2180 buf
= (char *)alloc(maxlen
);
2183 /* append character to the message */
2186 if (bufpos
>= epromptlen
2187 && strcmp(&buf
[bufpos
- epromptlen
], eprompt
) == 0)
2189 /* remove eprompt from buf */
2190 buf
[bufpos
- epromptlen
] = NUL
;
2192 /* print message to user */
2193 (void)EMSG2(cs_emsg
, buf
);
2195 /* send RETURN to cscope */
2196 (void)putc('\n', csinfo
[i
].to_fp
);
2197 (void)fflush(csinfo
[i
].to_fp
);
2206 for (n
= 0; n
< (int)strlen(CSCOPE_PROMPT
); ++n
)
2209 ch
= getc(csinfo
[i
].fr_fp
);
2212 PERROR("cs_read_prompt EOF");
2213 if (buf
!= NULL
&& buf
[0] != NUL
)
2214 (void)EMSG2(cs_emsg
, buf
);
2215 else if (p_csverbose
)
2216 cs_reading_emsg(i
); /* don't have additional information */
2217 cs_release_csp(i
, TRUE
);
2219 return CSCOPE_FAILURE
;
2222 if (ch
!= CSCOPE_PROMPT
[n
])
2230 continue; /* didn't find the prompt */
2231 break; /* did find the prompt */
2235 return CSCOPE_SUCCESS
;
2238 #if defined(UNIX) && defined(SIGALRM)
2240 * Used to catch and ignore SIGALRM below.
2243 sig_handler
SIGDEFARG(sigarg
)
2251 * PRIVATE: cs_release_csp
2253 * Does the actual free'ing for the cs ptr with an optional flag of whether
2254 * or not to free the filename. Called by cs_kill and cs_reset.
2257 cs_release_csp(i
, freefnpp
)
2262 * Trying to exit normally (not sure whether it is fit to UNIX cscope
2264 if (csinfo
[i
].to_fp
!= NULL
)
2266 (void)fputs("q\n", csinfo
[i
].to_fp
);
2267 (void)fflush(csinfo
[i
].to_fp
);
2275 # if defined(HAVE_SIGACTION)
2276 struct sigaction sa
, old
;
2278 /* Use sigaction() to limit the waiting time to two seconds. */
2279 sigemptyset(&sa
.sa_mask
);
2280 sa
.sa_handler
= sig_handler
;
2282 sa
.sa_flags
= SA_NODEFER
;
2286 sigaction(SIGALRM
, &sa
, &old
);
2287 alarm(2); /* 2 sec timeout */
2289 /* Block until cscope exits or until timer expires */
2290 pid
= waitpid(csinfo
[i
].pid
, &pstat
, 0);
2291 waitpid_errno
= errno
;
2293 /* cancel pending alarm if still there and restore signal */
2295 sigaction(SIGALRM
, &old
, NULL
);
2299 /* Can't use sigaction(), loop for two seconds. First yield the CPU
2300 * to give cscope a chance to exit quickly. */
2302 for (waited
= 0; waited
< 40; ++waited
)
2304 pid
= waitpid(csinfo
[i
].pid
, &pstat
, WNOHANG
);
2305 waitpid_errno
= errno
;
2307 break; /* break unless the process is still running */
2308 mch_delay(50L, FALSE
); /* sleep 50 ms */
2312 * If the cscope process is still running: kill it.
2313 * Safety check: If the PID would be zero here, the entire X session
2314 * would be killed. -1 and 1 are dangerous as well.
2316 if (pid
< 0 && csinfo
[i
].pid
> 1)
2321 if (waitpid_errno
== ECHILD
)
2324 * When using 'vim -g', vim is forked and cscope process is
2325 * no longer a child process but a sibling. So waitpid()
2326 * fails with errno being ECHILD (No child processes).
2327 * Don't send SIGKILL to cscope immediately but wait
2328 * (polling) for it to exit normally as result of sending
2329 * the "q" command, hence giving it a chance to clean up
2330 * its temporary files.
2335 for (waited
= 0; waited
< 40; ++waited
)
2337 /* Check whether cscope process is still alive */
2338 if (kill(csinfo
[i
].pid
, 0) != 0)
2340 alive
= FALSE
; /* cscope process no longer exists */
2343 mch_delay(50L, FALSE
); /* sleep 50ms */
2349 kill(csinfo
[i
].pid
, SIGKILL
);
2350 (void)waitpid(csinfo
[i
].pid
, &pstat
, 0);
2355 if (csinfo
[i
].hProc
!= NULL
)
2357 /* Give cscope a chance to exit normally */
2358 if (WaitForSingleObject(csinfo
[i
].hProc
, 1000) == WAIT_TIMEOUT
)
2359 TerminateProcess(csinfo
[i
].hProc
, 0);
2360 CloseHandle(csinfo
[i
].hProc
);
2364 if (csinfo
[i
].fr_fp
!= NULL
)
2365 (void)fclose(csinfo
[i
].fr_fp
);
2366 if (csinfo
[i
].to_fp
!= NULL
)
2367 (void)fclose(csinfo
[i
].to_fp
);
2371 vim_free(csinfo
[i
].fname
);
2372 vim_free(csinfo
[i
].ppath
);
2373 vim_free(csinfo
[i
].flags
);
2377 } /* cs_release_csp */
2383 * calls cs_kill on all cscope connections then reinits
2387 exarg_T
*eap UNUSED
;
2389 char **dblist
= NULL
, **pplist
= NULL
, **fllist
= NULL
;
2391 char buf
[20]; /* for sprintf " (#%d)" */
2393 if (csinfo_size
== 0)
2394 return CSCOPE_SUCCESS
;
2396 /* malloc our db and ppath list */
2397 dblist
= (char **)alloc(csinfo_size
* sizeof(char *));
2398 pplist
= (char **)alloc(csinfo_size
* sizeof(char *));
2399 fllist
= (char **)alloc(csinfo_size
* sizeof(char *));
2400 if (dblist
== NULL
|| pplist
== NULL
|| fllist
== NULL
)
2405 return CSCOPE_FAILURE
;
2408 for (i
= 0; i
< csinfo_size
; i
++)
2410 dblist
[i
] = csinfo
[i
].fname
;
2411 pplist
[i
] = csinfo
[i
].ppath
;
2412 fllist
[i
] = csinfo
[i
].flags
;
2413 if (csinfo
[i
].fname
!= NULL
)
2414 cs_release_csp(i
, FALSE
);
2417 /* rebuild the cscope connection list */
2418 for (i
= 0; i
< csinfo_size
; i
++)
2420 if (dblist
[i
] != NULL
)
2422 cs_add_common(dblist
[i
], pplist
[i
], fllist
[i
]);
2425 /* don't use smsg_attr() because we want to display the
2426 * connection number in the same line as
2427 * "Added cscope database..."
2429 sprintf(buf
, " (#%d)", i
);
2430 MSG_PUTS_ATTR(buf
, hl_attr(HLF_R
));
2433 vim_free(dblist
[i
]);
2434 vim_free(pplist
[i
]);
2435 vim_free(fllist
[i
]);
2442 MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R
) | MSG_HIST
);
2443 return CSCOPE_SUCCESS
;
2448 * PRIVATE: cs_resolve_file
2450 * construct the full pathname to a file found in the cscope database.
2451 * (Prepends ppath, if there is one and if it's not already prepended,
2452 * otherwise just uses the name found.)
2454 * we need to prepend the prefix because on some cscope's (e.g., the one that
2455 * ships with Solaris 2.6), the output never has the prefix prepended.
2456 * contrast this with my development system (Digital Unix), which does.
2459 cs_resolve_file(i
, name
)
2467 * ppath is freed when we destroy the cscope connection.
2468 * fullname is freed after cs_make_vim_style_matches, after it's been
2469 * copied into the tag buffer used by vim
2471 len
= (int)(strlen(name
) + 2);
2472 if (csinfo
[i
].ppath
!= NULL
)
2473 len
+= (int)strlen(csinfo
[i
].ppath
);
2475 if ((fullname
= (char *)alloc(len
)) == NULL
)
2479 * note/example: this won't work if the cscope output already starts
2480 * "../.." and the prefix path is also "../..". if something like this
2481 * happens, you are screwed up and need to fix how you're using cscope.
2483 if (csinfo
[i
].ppath
!= NULL
&&
2484 (strncmp(name
, csinfo
[i
].ppath
, strlen(csinfo
[i
].ppath
)) != 0) &&
2487 && name
[0] != '\\' && name
[1] != ':'
2490 (void)sprintf(fullname
, "%s/%s", csinfo
[i
].ppath
, name
);
2492 (void)sprintf(fullname
, "%s", name
);
2495 } /* cs_resolve_file */
2501 * show all cscope connections
2505 exarg_T
*eap UNUSED
;
2508 if (cs_cnt_connections() == 0)
2509 MSG_PUTS(_("no cscope connections\n"));
2513 _(" # pid database name prepend path\n"),
2515 for (i
= 0; i
< csinfo_size
; i
++)
2517 if (csinfo
[i
].fname
== NULL
)
2520 if (csinfo
[i
].ppath
!= NULL
)
2521 (void)smsg((char_u
*)"%2d %-5ld %-34s %-32s",
2522 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
, csinfo
[i
].ppath
);
2524 (void)smsg((char_u
*)"%2d %-5ld %-34s <none>",
2525 i
, (long)csinfo
[i
].pid
, csinfo
[i
].fname
);
2530 return CSCOPE_SUCCESS
;
2537 * Only called when VIM exits to quit any cscope sessions.
2544 for (i
= 0; i
< csinfo_size
; i
++)
2545 cs_release_csp(i
, TRUE
);
2550 #endif /* FEAT_CSCOPE */