Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * CSCOPE support for Vim added by Andy Kahn <kahn@zk3.dec.com> |
Bram Moolenaar | 2ce06f6 | 2005-01-31 19:19:04 +0000 | [diff] [blame] | 4 | * Ported to Win32 by Sergey Khorev <sergey.khorev@gmail.com> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 8 | * |
| 9 | * See README.txt for an overview of the Vim source code. |
| 10 | */ |
| 11 | |
| 12 | #include "vim.h" |
| 13 | |
| 14 | #if defined(FEAT_CSCOPE) || defined(PROTO) |
| 15 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #if defined(UNIX) |
| 19 | # include <sys/wait.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #include "if_cscope.h" |
| 22 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 23 | static void cs_usage_msg(csid_e x); |
| 24 | static int cs_add(exarg_T *eap); |
| 25 | static void cs_stat_emsg(char *fname); |
| 26 | static int cs_add_common(char *, char *, char *); |
| 27 | static int cs_check_for_connections(void); |
| 28 | static int cs_check_for_tags(void); |
| 29 | static int cs_cnt_connections(void); |
| 30 | static void cs_reading_emsg(int idx); |
| 31 | static int cs_cnt_matches(int idx); |
| 32 | static char * cs_create_cmd(char *csoption, char *pattern); |
| 33 | static int cs_create_connection(int i); |
| 34 | static void do_cscope_general(exarg_T *eap, int make_split); |
Bram Moolenaar | c716c30 | 2006-01-21 22:12:51 +0000 | [diff] [blame] | 35 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 36 | static void cs_file_results(FILE *, int *); |
Bram Moolenaar | c716c30 | 2006-01-21 22:12:51 +0000 | [diff] [blame] | 37 | #endif |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 38 | static void cs_fill_results(char *, int , int *, char ***, |
| 39 | char ***, int *); |
| 40 | static int cs_find(exarg_T *eap); |
| 41 | static int cs_find_common(char *opt, char *pat, int, int, int, char_u *cmdline); |
| 42 | static int cs_help(exarg_T *eap); |
| 43 | static void clear_csinfo(int i); |
| 44 | static int cs_insert_filelist(char *, char *, char *, |
| 45 | struct stat *); |
| 46 | static int cs_kill(exarg_T *eap); |
| 47 | static void cs_kill_execute(int, char *); |
| 48 | static cscmd_T * cs_lookup_cmd(exarg_T *eap); |
| 49 | static char * cs_make_vim_style_matches(char *, char *, |
| 50 | char *, char *); |
| 51 | static char * cs_manage_matches(char **, char **, int, mcmd_e); |
| 52 | static char * cs_parse_results(int cnumber, char *buf, int bufsize, char **context, char **linenumber, char **search); |
| 53 | static char * cs_pathcomponents(char *path); |
| 54 | static void cs_print_tags_priv(char **, char **, int); |
| 55 | static int cs_read_prompt(int); |
| 56 | static void cs_release_csp(int, int freefnpp); |
| 57 | static int cs_reset(exarg_T *eap); |
| 58 | static char * cs_resolve_file(int, char *); |
| 59 | static int cs_show(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | |
| 61 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 62 | static csinfo_T * csinfo = NULL; |
| 63 | static int csinfo_size = 0; /* number of items allocated in |
| 64 | csinfo[] */ |
| 65 | |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 66 | static int eap_arg_len; /* length of eap->arg, set in |
| 67 | cs_lookup_cmd() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | static cscmd_T cs_cmds[] = |
| 69 | { |
| 70 | { "add", cs_add, |
| 71 | N_("Add a new database"), "add file|dir [pre-path] [flags]", 0 }, |
| 72 | { "find", cs_find, |
Bram Moolenaar | 627943d | 2008-08-25 02:35:59 +0000 | [diff] [blame] | 73 | N_("Query for a pattern"), "find c|d|e|f|g|i|s|t name", 1 }, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | { "help", cs_help, |
| 75 | N_("Show this message"), "help", 0 }, |
| 76 | { "kill", cs_kill, |
| 77 | N_("Kill a connection"), "kill #", 0 }, |
| 78 | { "reset", cs_reset, |
| 79 | N_("Reinit all connections"), "reset", 0 }, |
| 80 | { "show", cs_show, |
| 81 | N_("Show connections"), "show", 0 }, |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 82 | { NULL, NULL, NULL, NULL, 0 } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 86 | cs_usage_msg(csid_e x) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | { |
| 88 | (void)EMSG2(_("E560: Usage: cs[cope] %s"), cs_cmds[(int)x].usage); |
| 89 | } |
| 90 | |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 91 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 92 | |
| 93 | static enum |
| 94 | { |
| 95 | EXP_CSCOPE_SUBCMD, /* expand ":cscope" sub-commands */ |
Bram Moolenaar | 7bfef80 | 2009-04-22 14:25:01 +0000 | [diff] [blame] | 96 | EXP_SCSCOPE_SUBCMD, /* expand ":scscope" sub-commands */ |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 97 | EXP_CSCOPE_FIND, /* expand ":cscope find" arguments */ |
| 98 | EXP_CSCOPE_KILL /* expand ":cscope kill" arguments */ |
| 99 | } expand_what; |
| 100 | |
| 101 | /* |
| 102 | * Function given to ExpandGeneric() to obtain the cscope command |
| 103 | * expansion. |
| 104 | */ |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 105 | char_u * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 106 | get_cscope_name(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 107 | { |
Bram Moolenaar | 7bfef80 | 2009-04-22 14:25:01 +0000 | [diff] [blame] | 108 | int current_idx; |
| 109 | int i; |
| 110 | |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 111 | switch (expand_what) |
| 112 | { |
| 113 | case EXP_CSCOPE_SUBCMD: |
| 114 | /* Complete with sub-commands of ":cscope": |
| 115 | * add, find, help, kill, reset, show */ |
| 116 | return (char_u *)cs_cmds[idx].name; |
Bram Moolenaar | 7bfef80 | 2009-04-22 14:25:01 +0000 | [diff] [blame] | 117 | case EXP_SCSCOPE_SUBCMD: |
| 118 | /* Complete with sub-commands of ":scscope": same sub-commands as |
| 119 | * ":cscope" but skip commands which don't support split windows */ |
| 120 | for (i = 0, current_idx = 0; cs_cmds[i].name != NULL; i++) |
| 121 | if (cs_cmds[i].cansplit) |
| 122 | if (current_idx++ == idx) |
| 123 | break; |
| 124 | return (char_u *)cs_cmds[i].name; |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 125 | case EXP_CSCOPE_FIND: |
| 126 | { |
| 127 | const char *query_type[] = |
| 128 | { |
| 129 | "c", "d", "e", "f", "g", "i", "s", "t", NULL |
| 130 | }; |
| 131 | |
| 132 | /* Complete with query type of ":cscope find {query_type}". |
| 133 | * {query_type} can be letters (c, d, ... t) or numbers (0, 1, |
| 134 | * ..., 8) but only complete with letters, since numbers are |
| 135 | * redundant. */ |
| 136 | return (char_u *)query_type[idx]; |
| 137 | } |
| 138 | case EXP_CSCOPE_KILL: |
| 139 | { |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 140 | static char connection[5]; |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 141 | |
| 142 | /* ":cscope kill" accepts connection numbers or partial names of |
| 143 | * the pathname of the cscope database as argument. Only complete |
| 144 | * with connection numbers. -1 can also be used to kill all |
| 145 | * connections. */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 146 | for (i = 0, current_idx = 0; i < csinfo_size; i++) |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 147 | { |
| 148 | if (csinfo[i].fname == NULL) |
| 149 | continue; |
| 150 | if (current_idx++ == idx) |
| 151 | { |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 152 | vim_snprintf(connection, sizeof(connection), "%d", i); |
| 153 | return (char_u *)connection; |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | return (current_idx == idx && idx > 0) ? (char_u *)"-1" : NULL; |
| 157 | } |
| 158 | default: |
| 159 | return NULL; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Handle command line completion for :cscope command. |
| 165 | */ |
| 166 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 167 | set_context_in_cscope_cmd( |
| 168 | expand_T *xp, |
| 169 | char_u *arg, |
| 170 | cmdidx_T cmdidx) |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 171 | { |
| 172 | char_u *p; |
| 173 | |
| 174 | /* Default: expand subcommands */ |
| 175 | xp->xp_context = EXPAND_CSCOPE; |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 176 | xp->xp_pattern = arg; |
Bram Moolenaar | 7bfef80 | 2009-04-22 14:25:01 +0000 | [diff] [blame] | 177 | expand_what = (cmdidx == CMD_scscope) |
| 178 | ? EXP_SCSCOPE_SUBCMD : EXP_CSCOPE_SUBCMD; |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 179 | |
| 180 | /* (part of) subcommand already typed */ |
| 181 | if (*arg != NUL) |
| 182 | { |
| 183 | p = skiptowhite(arg); |
| 184 | if (*p != NUL) /* past first word */ |
| 185 | { |
| 186 | xp->xp_pattern = skipwhite(p); |
| 187 | if (*skiptowhite(xp->xp_pattern) != NUL) |
| 188 | xp->xp_context = EXPAND_NOTHING; |
| 189 | else if (STRNICMP(arg, "add", p - arg) == 0) |
| 190 | xp->xp_context = EXPAND_FILES; |
| 191 | else if (STRNICMP(arg, "kill", p - arg) == 0) |
| 192 | expand_what = EXP_CSCOPE_KILL; |
| 193 | else if (STRNICMP(arg, "find", p - arg) == 0) |
| 194 | expand_what = EXP_CSCOPE_FIND; |
| 195 | else |
| 196 | xp->xp_context = EXPAND_NOTHING; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #endif /* FEAT_CMDL_COMPL */ |
| 202 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | /* |
| 204 | * PRIVATE: do_cscope_general |
| 205 | * |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 206 | * Find the command, print help if invalid, and then call the corresponding |
| 207 | * command function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | */ |
| 209 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 210 | do_cscope_general( |
| 211 | exarg_T *eap, |
| 212 | int make_split) /* whether to split window */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | { |
| 214 | cscmd_T *cmdp; |
| 215 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | if ((cmdp = cs_lookup_cmd(eap)) == NULL) |
| 217 | { |
| 218 | cs_help(eap); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | #ifdef FEAT_WINDOWS |
| 223 | if (make_split) |
| 224 | { |
| 225 | if (!cmdp->cansplit) |
| 226 | { |
| 227 | (void)MSG_PUTS(_("This cscope command does not support splitting the window.\n")); |
| 228 | return; |
| 229 | } |
| 230 | postponed_split = -1; |
| 231 | postponed_split_flags = cmdmod.split; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 232 | postponed_split_tab = cmdmod.tab; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | |
| 236 | cmdp->func(eap); |
| 237 | |
| 238 | #ifdef FEAT_WINDOWS |
| 239 | postponed_split_flags = 0; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 240 | postponed_split_tab = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 241 | #endif |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * PUBLIC: do_cscope |
| 246 | */ |
| 247 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 248 | do_cscope(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | { |
| 250 | do_cscope_general(eap, FALSE); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * PUBLIC: do_scscope |
| 255 | * |
| 256 | * same as do_cscope, but splits window, too. |
| 257 | */ |
| 258 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 259 | do_scscope(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 260 | { |
| 261 | do_cscope_general(eap, TRUE); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * PUBLIC: do_cstag |
| 266 | * |
| 267 | */ |
| 268 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 269 | do_cstag(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | { |
| 271 | int ret = FALSE; |
| 272 | |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 273 | if (*eap->arg == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | { |
| 275 | (void)EMSG(_("E562: Usage: cstag <ident>")); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | switch (p_csto) |
| 280 | { |
| 281 | case 0 : |
| 282 | if (cs_check_for_connections()) |
| 283 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 284 | ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE, |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 285 | FALSE, *eap->cmdlinep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 286 | if (ret == FALSE) |
| 287 | { |
| 288 | cs_free_tags(); |
| 289 | if (msg_col) |
| 290 | msg_putchar('\n'); |
| 291 | |
| 292 | if (cs_check_for_tags()) |
| 293 | ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE); |
| 294 | } |
| 295 | } |
| 296 | else if (cs_check_for_tags()) |
| 297 | { |
| 298 | ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE); |
| 299 | } |
| 300 | break; |
| 301 | case 1 : |
| 302 | if (cs_check_for_tags()) |
| 303 | { |
| 304 | ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE); |
| 305 | if (ret == FALSE) |
| 306 | { |
| 307 | if (msg_col) |
| 308 | msg_putchar('\n'); |
| 309 | |
| 310 | if (cs_check_for_connections()) |
| 311 | { |
| 312 | ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 313 | FALSE, FALSE, *eap->cmdlinep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | if (ret == FALSE) |
| 315 | cs_free_tags(); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | else if (cs_check_for_connections()) |
| 320 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 321 | ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE, |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 322 | FALSE, *eap->cmdlinep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | if (ret == FALSE) |
| 324 | cs_free_tags(); |
| 325 | } |
| 326 | break; |
| 327 | default : |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | if (!ret) |
| 332 | { |
| 333 | (void)EMSG(_("E257: cstag: tag not found")); |
| 334 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 335 | g_do_tagpreview = 0; |
| 336 | #endif |
| 337 | } |
| 338 | |
| 339 | } /* do_cscope */ |
| 340 | |
| 341 | |
| 342 | /* |
| 343 | * PUBLIC: cs_find |
| 344 | * |
| 345 | * this simulates a vim_fgets(), but for cscope, returns the next line |
| 346 | * from the cscope output. should only be called from find_tags() |
| 347 | * |
| 348 | * returns TRUE if eof, FALSE otherwise |
| 349 | */ |
| 350 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 351 | cs_fgets(char_u *buf, int size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | { |
| 353 | char *p; |
| 354 | |
| 355 | if ((p = cs_manage_matches(NULL, NULL, -1, Get)) == NULL) |
| 356 | return TRUE; |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 357 | vim_strncpy(buf, (char_u *)p, size - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | |
| 359 | return FALSE; |
| 360 | } /* cs_fgets */ |
| 361 | |
| 362 | |
| 363 | /* |
| 364 | * PUBLIC: cs_free_tags |
| 365 | * |
| 366 | * called only from do_tag(), when popping the tag stack |
| 367 | */ |
| 368 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 369 | cs_free_tags(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 370 | { |
| 371 | cs_manage_matches(NULL, NULL, -1, Free); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /* |
| 376 | * PUBLIC: cs_print_tags |
| 377 | * |
| 378 | * called from do_tag() |
| 379 | */ |
| 380 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 381 | cs_print_tags(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 382 | { |
| 383 | cs_manage_matches(NULL, NULL, -1, Print); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /* |
| 388 | * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function |
| 389 | * |
| 390 | * Checks for the existence of a |cscope| connection. If no |
| 391 | * parameters are specified, then the function returns: |
| 392 | * |
| 393 | * 0, if cscope was not available (not compiled in), or if there |
| 394 | * are no cscope connections; or |
| 395 | * 1, if there is at least one cscope connection. |
| 396 | * |
| 397 | * If parameters are specified, then the value of {num} |
| 398 | * determines how existence of a cscope connection is checked: |
| 399 | * |
| 400 | * {num} Description of existence check |
| 401 | * ----- ------------------------------ |
| 402 | * 0 Same as no parameters (e.g., "cscope_connection()"). |
| 403 | * 1 Ignore {prepend}, and use partial string matches for |
| 404 | * {dbpath}. |
| 405 | * 2 Ignore {prepend}, and use exact string matches for |
| 406 | * {dbpath}. |
| 407 | * 3 Use {prepend}, use partial string matches for both |
| 408 | * {dbpath} and {prepend}. |
| 409 | * 4 Use {prepend}, use exact string matches for both |
| 410 | * {dbpath} and {prepend}. |
| 411 | * |
| 412 | * Note: All string comparisons are case sensitive! |
| 413 | */ |
| 414 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 415 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 416 | cs_connection(int num, char_u *dbpath, char_u *ppath) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 417 | { |
| 418 | int i; |
| 419 | |
| 420 | if (num < 0 || num > 4 || (num > 0 && !dbpath)) |
| 421 | return FALSE; |
| 422 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 423 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 424 | { |
| 425 | if (!csinfo[i].fname) |
| 426 | continue; |
| 427 | |
| 428 | if (num == 0) |
| 429 | return TRUE; |
| 430 | |
| 431 | switch (num) |
| 432 | { |
| 433 | case 1: |
| 434 | if (strstr(csinfo[i].fname, (char *)dbpath)) |
| 435 | return TRUE; |
| 436 | break; |
| 437 | case 2: |
| 438 | if (strcmp(csinfo[i].fname, (char *)dbpath) == 0) |
| 439 | return TRUE; |
| 440 | break; |
| 441 | case 3: |
| 442 | if (strstr(csinfo[i].fname, (char *)dbpath) |
| 443 | && ((!ppath && !csinfo[i].ppath) |
| 444 | || (ppath |
| 445 | && csinfo[i].ppath |
| 446 | && strstr(csinfo[i].ppath, (char *)ppath)))) |
| 447 | return TRUE; |
| 448 | break; |
| 449 | case 4: |
| 450 | if ((strcmp(csinfo[i].fname, (char *)dbpath) == 0) |
| 451 | && ((!ppath && !csinfo[i].ppath) |
| 452 | || (ppath |
| 453 | && csinfo[i].ppath |
| 454 | && (strcmp(csinfo[i].ppath, (char *)ppath) == 0)))) |
| 455 | return TRUE; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return FALSE; |
| 461 | } /* cs_connection */ |
| 462 | #endif |
| 463 | |
| 464 | |
| 465 | /* |
| 466 | * PRIVATE functions |
| 467 | ****************************************************************************/ |
| 468 | |
| 469 | /* |
| 470 | * PRIVATE: cs_add |
| 471 | * |
| 472 | * add cscope database or a directory name (to look for cscope.out) |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 473 | * to the cscope connection list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 474 | * |
| 475 | * MAXPATHL 256 |
| 476 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 478 | cs_add(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | { |
| 480 | char *fname, *ppath, *flags = NULL; |
| 481 | |
| 482 | if ((fname = strtok((char *)NULL, (const char *)" ")) == NULL) |
| 483 | { |
| 484 | cs_usage_msg(Add); |
| 485 | return CSCOPE_FAILURE; |
| 486 | } |
| 487 | if ((ppath = strtok((char *)NULL, (const char *)" ")) != NULL) |
| 488 | flags = strtok((char *)NULL, (const char *)" "); |
| 489 | |
| 490 | return cs_add_common(fname, ppath, flags); |
| 491 | } |
| 492 | |
| 493 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 494 | cs_stat_emsg(char *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 495 | { |
| 496 | char *stat_emsg = _("E563: stat(%s) error: %d"); |
| 497 | char *buf = (char *)alloc((unsigned)strlen(stat_emsg) + MAXPATHL + 10); |
| 498 | |
| 499 | if (buf != NULL) |
| 500 | { |
| 501 | (void)sprintf(buf, stat_emsg, fname, errno); |
| 502 | (void)EMSG(buf); |
| 503 | vim_free(buf); |
| 504 | } |
| 505 | else |
| 506 | (void)EMSG(_("E563: stat error")); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /* |
| 511 | * PRIVATE: cs_add_common |
| 512 | * |
| 513 | * the common routine to add a new cscope connection. called by |
| 514 | * cs_add() and cs_reset(). i really don't like to do this, but this |
| 515 | * routine uses a number of goto statements. |
| 516 | */ |
| 517 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 518 | cs_add_common( |
| 519 | char *arg1, /* filename - may contain environment variables */ |
| 520 | char *arg2, /* prepend path - may contain environment variables */ |
| 521 | char *flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | { |
| 523 | struct stat statbuf; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 524 | int ret; |
| 525 | char *fname = NULL; |
| 526 | char *fname2 = NULL; |
| 527 | char *ppath = NULL; |
| 528 | int i; |
Bram Moolenaar | cab465a | 2013-06-12 21:25:23 +0200 | [diff] [blame] | 529 | #ifdef FEAT_MODIFY_FNAME |
| 530 | int len; |
| 531 | int usedlen = 0; |
| 532 | char_u *fbuf = NULL; |
| 533 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | |
| 535 | /* get the filename (arg1), expand it, and try to stat it */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 536 | if ((fname = (char *)alloc(MAXPATHL + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | goto add_err; |
| 538 | |
| 539 | expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL); |
Bram Moolenaar | cab465a | 2013-06-12 21:25:23 +0200 | [diff] [blame] | 540 | #ifdef FEAT_MODIFY_FNAME |
| 541 | len = (int)STRLEN(fname); |
| 542 | fbuf = (char_u *)fname; |
| 543 | (void)modify_fname((char_u *)":p", &usedlen, |
| 544 | (char_u **)&fname, &fbuf, &len); |
| 545 | if (fname == NULL) |
| 546 | goto add_err; |
| 547 | fname = (char *)vim_strnsave((char_u *)fname, len); |
| 548 | vim_free(fbuf); |
| 549 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 550 | ret = stat(fname, &statbuf); |
| 551 | if (ret < 0) |
| 552 | { |
| 553 | staterr: |
| 554 | if (p_csverbose) |
| 555 | cs_stat_emsg(fname); |
| 556 | goto add_err; |
| 557 | } |
| 558 | |
| 559 | /* get the prepend path (arg2), expand it, and try to stat it */ |
| 560 | if (arg2 != NULL) |
| 561 | { |
| 562 | struct stat statbuf2; |
| 563 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 564 | if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | goto add_err; |
| 566 | |
| 567 | expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL); |
| 568 | ret = stat(ppath, &statbuf2); |
| 569 | if (ret < 0) |
| 570 | goto staterr; |
| 571 | } |
| 572 | |
| 573 | /* if filename is a directory, append the cscope database name to it */ |
| 574 | if ((statbuf.st_mode & S_IFMT) == S_IFDIR) |
| 575 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 576 | fname2 = (char *)alloc((unsigned)(strlen(CSCOPE_DBFILE) + strlen(fname) + 2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | if (fname2 == NULL) |
| 578 | goto add_err; |
| 579 | |
| 580 | while (fname[strlen(fname)-1] == '/' |
| 581 | #ifdef WIN32 |
| 582 | || fname[strlen(fname)-1] == '\\' |
| 583 | #endif |
| 584 | ) |
| 585 | { |
| 586 | fname[strlen(fname)-1] = '\0'; |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 587 | if (fname[0] == '\0') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | if (fname[0] == '\0') |
| 591 | (void)sprintf(fname2, "/%s", CSCOPE_DBFILE); |
| 592 | else |
| 593 | (void)sprintf(fname2, "%s/%s", fname, CSCOPE_DBFILE); |
| 594 | |
| 595 | ret = stat(fname2, &statbuf); |
| 596 | if (ret < 0) |
| 597 | { |
| 598 | if (p_csverbose) |
| 599 | cs_stat_emsg(fname2); |
| 600 | goto add_err; |
| 601 | } |
| 602 | |
| 603 | i = cs_insert_filelist(fname2, ppath, flags, &statbuf); |
| 604 | } |
| 605 | #if defined(UNIX) |
| 606 | else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) |
| 607 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 608 | /* WIN32 - substitute define S_ISREG from os_unix.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 609 | else if (((statbuf.st_mode) & S_IFMT) == S_IFREG) |
| 610 | #endif |
| 611 | { |
| 612 | i = cs_insert_filelist(fname, ppath, flags, &statbuf); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | if (p_csverbose) |
| 617 | (void)EMSG2( |
| 618 | _("E564: %s is not a directory or a valid cscope database"), |
| 619 | fname); |
| 620 | goto add_err; |
| 621 | } |
| 622 | |
| 623 | if (i != -1) |
| 624 | { |
| 625 | if (cs_create_connection(i) == CSCOPE_FAILURE |
| 626 | || cs_read_prompt(i) == CSCOPE_FAILURE) |
| 627 | { |
| 628 | cs_release_csp(i, TRUE); |
| 629 | goto add_err; |
| 630 | } |
| 631 | |
| 632 | if (p_csverbose) |
| 633 | { |
| 634 | msg_clr_eos(); |
| 635 | (void)smsg_attr(hl_attr(HLF_R), |
| 636 | (char_u *)_("Added cscope database %s"), |
| 637 | csinfo[i].fname); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | vim_free(fname); |
| 642 | vim_free(fname2); |
| 643 | vim_free(ppath); |
| 644 | return CSCOPE_SUCCESS; |
| 645 | |
| 646 | add_err: |
| 647 | vim_free(fname2); |
| 648 | vim_free(fname); |
| 649 | vim_free(ppath); |
| 650 | return CSCOPE_FAILURE; |
| 651 | } /* cs_add_common */ |
| 652 | |
| 653 | |
| 654 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 655 | cs_check_for_connections(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 656 | { |
| 657 | return (cs_cnt_connections() > 0); |
| 658 | } /* cs_check_for_connections */ |
| 659 | |
| 660 | |
| 661 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 662 | cs_check_for_tags(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 664 | return (p_tags[0] != NUL && curbuf->b_p_tags != NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 665 | } /* cs_check_for_tags */ |
| 666 | |
| 667 | |
| 668 | /* |
| 669 | * PRIVATE: cs_cnt_connections |
| 670 | * |
| 671 | * count the number of cscope connections |
| 672 | */ |
| 673 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 674 | cs_cnt_connections(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | { |
| 676 | short i; |
| 677 | short cnt = 0; |
| 678 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 679 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 680 | { |
| 681 | if (csinfo[i].fname != NULL) |
| 682 | cnt++; |
| 683 | } |
| 684 | return cnt; |
| 685 | } /* cs_cnt_connections */ |
| 686 | |
| 687 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 688 | cs_reading_emsg( |
| 689 | int idx) /* connection index */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 690 | { |
| 691 | EMSGN(_("E262: error reading cscope connection %ld"), idx); |
| 692 | } |
| 693 | |
| 694 | #define CSREAD_BUFSIZE 2048 |
| 695 | /* |
| 696 | * PRIVATE: cs_cnt_matches |
| 697 | * |
| 698 | * count the number of matches for a given cscope connection. |
| 699 | */ |
| 700 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 701 | cs_cnt_matches(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | { |
| 703 | char *stok; |
| 704 | char *buf; |
| 705 | int nlines; |
| 706 | |
| 707 | buf = (char *)alloc(CSREAD_BUFSIZE); |
| 708 | if (buf == NULL) |
| 709 | return 0; |
| 710 | for (;;) |
| 711 | { |
| 712 | if (!fgets(buf, CSREAD_BUFSIZE, csinfo[idx].fr_fp)) |
| 713 | { |
| 714 | if (feof(csinfo[idx].fr_fp)) |
| 715 | errno = EIO; |
| 716 | |
| 717 | cs_reading_emsg(idx); |
| 718 | |
| 719 | vim_free(buf); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * If the database is out of date, or there's some other problem, |
| 725 | * cscope will output error messages before the number-of-lines output. |
| 726 | * Display/discard any output that doesn't match what we want. |
Bram Moolenaar | 84c4d79 | 2007-01-16 14:18:41 +0000 | [diff] [blame] | 727 | * Accept "\S*cscope: X lines", also matches "mlcscope". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | */ |
| 729 | if ((stok = strtok(buf, (const char *)" ")) == NULL) |
| 730 | continue; |
Bram Moolenaar | 84c4d79 | 2007-01-16 14:18:41 +0000 | [diff] [blame] | 731 | if (strstr((const char *)stok, "cscope:") == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | continue; |
| 733 | |
| 734 | if ((stok = strtok(NULL, (const char *)" ")) == NULL) |
| 735 | continue; |
| 736 | nlines = atoi(stok); |
| 737 | if (nlines < 0) |
| 738 | { |
| 739 | nlines = 0; |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | if ((stok = strtok(NULL, (const char *)" ")) == NULL) |
| 744 | continue; |
| 745 | if (strncmp((const char *)stok, "lines", 5)) |
| 746 | continue; |
| 747 | |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | vim_free(buf); |
| 752 | return nlines; |
| 753 | } /* cs_cnt_matches */ |
| 754 | |
| 755 | |
| 756 | /* |
| 757 | * PRIVATE: cs_create_cmd |
| 758 | * |
| 759 | * Creates the actual cscope command query from what the user entered. |
| 760 | */ |
| 761 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 762 | cs_create_cmd(char *csoption, char *pattern) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 763 | { |
| 764 | char *cmd; |
| 765 | short search; |
Bram Moolenaar | 80b6a0e | 2009-03-18 13:32:24 +0000 | [diff] [blame] | 766 | char *pat; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 767 | |
| 768 | switch (csoption[0]) |
| 769 | { |
| 770 | case '0' : case 's' : |
| 771 | search = 0; |
| 772 | break; |
| 773 | case '1' : case 'g' : |
| 774 | search = 1; |
| 775 | break; |
| 776 | case '2' : case 'd' : |
| 777 | search = 2; |
| 778 | break; |
| 779 | case '3' : case 'c' : |
| 780 | search = 3; |
| 781 | break; |
| 782 | case '4' : case 't' : |
| 783 | search = 4; |
| 784 | break; |
| 785 | case '6' : case 'e' : |
| 786 | search = 6; |
| 787 | break; |
| 788 | case '7' : case 'f' : |
| 789 | search = 7; |
| 790 | break; |
| 791 | case '8' : case 'i' : |
| 792 | search = 8; |
| 793 | break; |
| 794 | default : |
| 795 | (void)EMSG(_("E561: unknown cscope search type")); |
| 796 | cs_usage_msg(Find); |
| 797 | return NULL; |
| 798 | } |
| 799 | |
Bram Moolenaar | 80b6a0e | 2009-03-18 13:32:24 +0000 | [diff] [blame] | 800 | /* Skip white space before the patter, except for text and pattern search, |
| 801 | * they may want to use the leading white space. */ |
| 802 | pat = pattern; |
| 803 | if (search != 4 && search != 6) |
| 804 | while vim_iswhite(*pat) |
| 805 | ++pat; |
| 806 | |
| 807 | if ((cmd = (char *)alloc((unsigned)(strlen(pat) + 2))) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 808 | return NULL; |
| 809 | |
Bram Moolenaar | 80b6a0e | 2009-03-18 13:32:24 +0000 | [diff] [blame] | 810 | (void)sprintf(cmd, "%d%s", search, pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | |
| 812 | return cmd; |
| 813 | } /* cs_create_cmd */ |
| 814 | |
| 815 | |
| 816 | /* |
| 817 | * PRIVATE: cs_create_connection |
| 818 | * |
| 819 | * This piece of code was taken/adapted from nvi. do we need to add |
| 820 | * the BSD license notice? |
| 821 | */ |
| 822 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 823 | cs_create_connection(int i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | { |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 825 | #ifdef UNIX |
| 826 | int to_cs[2], from_cs[2]; |
| 827 | #endif |
| 828 | int len; |
| 829 | char *prog, *cmd, *ppath = NULL; |
| 830 | #ifdef WIN32 |
| 831 | int fd; |
| 832 | SECURITY_ATTRIBUTES sa; |
| 833 | PROCESS_INFORMATION pi; |
| 834 | STARTUPINFO si; |
| 835 | BOOL pipe_stdin = FALSE, pipe_stdout = FALSE; |
| 836 | HANDLE stdin_rd, stdout_rd; |
| 837 | HANDLE stdout_wr, stdin_wr; |
| 838 | BOOL created; |
Bram Moolenaar | 5365c4d | 2007-09-14 17:56:59 +0000 | [diff] [blame] | 839 | # ifdef __BORLANDC__ |
| 840 | # define OPEN_OH_ARGTYPE long |
| 841 | # else |
| 842 | # if (_MSC_VER >= 1300) |
| 843 | # define OPEN_OH_ARGTYPE intptr_t |
| 844 | # else |
| 845 | # define OPEN_OH_ARGTYPE long |
| 846 | # endif |
| 847 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 848 | #endif |
| 849 | |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 850 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 851 | /* |
| 852 | * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from |
| 853 | * from_cs[0] and writes to to_cs[1]. |
| 854 | */ |
| 855 | to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1; |
| 856 | if (pipe(to_cs) < 0 || pipe(from_cs) < 0) |
| 857 | { |
| 858 | (void)EMSG(_("E566: Could not create cscope pipes")); |
| 859 | err_closing: |
| 860 | if (to_cs[0] != -1) |
| 861 | (void)close(to_cs[0]); |
| 862 | if (to_cs[1] != -1) |
| 863 | (void)close(to_cs[1]); |
| 864 | if (from_cs[0] != -1) |
| 865 | (void)close(from_cs[0]); |
| 866 | if (from_cs[1] != -1) |
| 867 | (void)close(from_cs[1]); |
| 868 | return CSCOPE_FAILURE; |
| 869 | } |
| 870 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | switch (csinfo[i].pid = fork()) |
| 872 | { |
| 873 | case -1: |
| 874 | (void)EMSG(_("E622: Could not fork for cscope")); |
| 875 | goto err_closing; |
| 876 | case 0: /* child: run cscope. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | if (dup2(to_cs[0], STDIN_FILENO) == -1) |
| 878 | PERROR("cs_create_connection 1"); |
| 879 | if (dup2(from_cs[1], STDOUT_FILENO) == -1) |
| 880 | PERROR("cs_create_connection 2"); |
| 881 | if (dup2(from_cs[1], STDERR_FILENO) == -1) |
| 882 | PERROR("cs_create_connection 3"); |
| 883 | |
| 884 | /* close unused */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | (void)close(to_cs[1]); |
| 886 | (void)close(from_cs[0]); |
| 887 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 888 | /* WIN32 */ |
| 889 | /* Create pipes to communicate with cscope */ |
| 890 | sa.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 891 | sa.bInheritHandle = TRUE; |
| 892 | sa.lpSecurityDescriptor = NULL; |
| 893 | |
| 894 | if (!(pipe_stdin = CreatePipe(&stdin_rd, &stdin_wr, &sa, 0)) |
| 895 | || !(pipe_stdout = CreatePipe(&stdout_rd, &stdout_wr, &sa, 0))) |
| 896 | { |
| 897 | (void)EMSG(_("E566: Could not create cscope pipes")); |
| 898 | err_closing: |
| 899 | if (pipe_stdin) |
| 900 | { |
| 901 | CloseHandle(stdin_rd); |
| 902 | CloseHandle(stdin_wr); |
| 903 | } |
| 904 | if (pipe_stdout) |
| 905 | { |
| 906 | CloseHandle(stdout_rd); |
| 907 | CloseHandle(stdout_wr); |
| 908 | } |
| 909 | return CSCOPE_FAILURE; |
| 910 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | #endif |
| 912 | /* expand the cscope exec for env var's */ |
| 913 | if ((prog = (char *)alloc(MAXPATHL + 1)) == NULL) |
| 914 | { |
| 915 | #ifdef UNIX |
| 916 | return CSCOPE_FAILURE; |
| 917 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 918 | /* WIN32 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 919 | goto err_closing; |
| 920 | #endif |
| 921 | } |
| 922 | expand_env((char_u *)p_csprg, (char_u *)prog, MAXPATHL); |
| 923 | |
| 924 | /* alloc space to hold the cscope command */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 925 | len = (int)(strlen(prog) + strlen(csinfo[i].fname) + 32); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | if (csinfo[i].ppath) |
| 927 | { |
| 928 | /* expand the prepend path for env var's */ |
| 929 | if ((ppath = (char *)alloc(MAXPATHL + 1)) == NULL) |
| 930 | { |
| 931 | vim_free(prog); |
| 932 | #ifdef UNIX |
| 933 | return CSCOPE_FAILURE; |
| 934 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 935 | /* WIN32 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 936 | goto err_closing; |
| 937 | #endif |
| 938 | } |
| 939 | expand_env((char_u *)csinfo[i].ppath, (char_u *)ppath, MAXPATHL); |
| 940 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 941 | len += (int)strlen(ppath); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (csinfo[i].flags) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 945 | len += (int)strlen(csinfo[i].flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 946 | |
| 947 | if ((cmd = (char *)alloc(len)) == NULL) |
| 948 | { |
| 949 | vim_free(prog); |
| 950 | vim_free(ppath); |
| 951 | #ifdef UNIX |
| 952 | return CSCOPE_FAILURE; |
| 953 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 954 | /* WIN32 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 955 | goto err_closing; |
| 956 | #endif |
| 957 | } |
| 958 | |
| 959 | /* run the cscope command; is there execl for non-unix systems? */ |
| 960 | #if defined(UNIX) |
| 961 | (void)sprintf(cmd, "exec %s -dl -f %s", prog, csinfo[i].fname); |
| 962 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 963 | /* WIN32 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 964 | (void)sprintf(cmd, "%s -dl -f %s", prog, csinfo[i].fname); |
| 965 | #endif |
| 966 | if (csinfo[i].ppath != NULL) |
| 967 | { |
| 968 | (void)strcat(cmd, " -P"); |
| 969 | (void)strcat(cmd, csinfo[i].ppath); |
| 970 | } |
| 971 | if (csinfo[i].flags != NULL) |
| 972 | { |
| 973 | (void)strcat(cmd, " "); |
| 974 | (void)strcat(cmd, csinfo[i].flags); |
| 975 | } |
| 976 | # ifdef UNIX |
| 977 | /* on Win32 we still need prog */ |
| 978 | vim_free(prog); |
| 979 | # endif |
| 980 | vim_free(ppath); |
| 981 | |
| 982 | #if defined(UNIX) |
Bram Moolenaar | 85e932f | 2013-06-30 15:01:22 +0200 | [diff] [blame] | 983 | # if defined(HAVE_SETSID) || defined(HAVE_SETPGID) |
| 984 | /* Change our process group to avoid cscope receiving SIGWINCH. */ |
| 985 | # if defined(HAVE_SETSID) |
| 986 | (void)setsid(); |
| 987 | # else |
| 988 | if (setpgid(0, 0) == -1) |
| 989 | PERROR(_("cs_create_connection setpgid failed")); |
| 990 | # endif |
| 991 | # endif |
Bram Moolenaar | 856b9fe | 2009-05-16 14:16:02 +0000 | [diff] [blame] | 992 | if (execl("/bin/sh", "sh", "-c", cmd, (char *)NULL) == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 993 | PERROR(_("cs_create_connection exec failed")); |
| 994 | |
| 995 | exit(127); |
| 996 | /* NOTREACHED */ |
| 997 | default: /* parent. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 998 | /* |
| 999 | * Save the file descriptors for later duplication, and |
| 1000 | * reopen as streams. |
| 1001 | */ |
| 1002 | if ((csinfo[i].to_fp = fdopen(to_cs[1], "w")) == NULL) |
| 1003 | PERROR(_("cs_create_connection: fdopen for to_fp failed")); |
| 1004 | if ((csinfo[i].fr_fp = fdopen(from_cs[0], "r")) == NULL) |
| 1005 | PERROR(_("cs_create_connection: fdopen for fr_fp failed")); |
| 1006 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1007 | /* close unused */ |
| 1008 | (void)close(to_cs[0]); |
| 1009 | (void)close(from_cs[1]); |
| 1010 | |
| 1011 | break; |
| 1012 | } |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 1013 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1014 | #else |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 1015 | /* WIN32 */ |
| 1016 | /* Create a new process to run cscope and use pipes to talk with it */ |
| 1017 | GetStartupInfo(&si); |
| 1018 | si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 1019 | si.wShowWindow = SW_HIDE; /* Hide child application window */ |
| 1020 | si.hStdOutput = stdout_wr; |
| 1021 | si.hStdError = stdout_wr; |
| 1022 | si.hStdInput = stdin_rd; |
| 1023 | created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, |
| 1024 | NULL, NULL, &si, &pi); |
| 1025 | vim_free(prog); |
| 1026 | vim_free(cmd); |
| 1027 | |
| 1028 | if (!created) |
| 1029 | { |
| 1030 | PERROR(_("cs_create_connection exec failed")); |
| 1031 | (void)EMSG(_("E623: Could not spawn cscope process")); |
| 1032 | goto err_closing; |
| 1033 | } |
| 1034 | /* else */ |
| 1035 | csinfo[i].pid = pi.dwProcessId; |
| 1036 | csinfo[i].hProc = pi.hProcess; |
| 1037 | CloseHandle(pi.hThread); |
| 1038 | |
| 1039 | /* TODO - tidy up after failure to create files on pipe handles. */ |
Bram Moolenaar | 5365c4d | 2007-09-14 17:56:59 +0000 | [diff] [blame] | 1040 | if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdin_wr, |
| 1041 | _O_TEXT|_O_APPEND)) < 0) |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 1042 | || ((csinfo[i].to_fp = _fdopen(fd, "w")) == NULL)) |
| 1043 | PERROR(_("cs_create_connection: fdopen for to_fp failed")); |
Bram Moolenaar | 5365c4d | 2007-09-14 17:56:59 +0000 | [diff] [blame] | 1044 | if (((fd = _open_osfhandle((OPEN_OH_ARGTYPE)stdout_rd, |
| 1045 | _O_TEXT|_O_RDONLY)) < 0) |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 1046 | || ((csinfo[i].fr_fp = _fdopen(fd, "r")) == NULL)) |
| 1047 | PERROR(_("cs_create_connection: fdopen for fr_fp failed")); |
| 1048 | |
| 1049 | /* Close handles for file descriptors inherited by the cscope process */ |
| 1050 | CloseHandle(stdin_rd); |
| 1051 | CloseHandle(stdout_wr); |
| 1052 | |
| 1053 | #endif /* !UNIX */ |
| 1054 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1055 | return CSCOPE_SUCCESS; |
| 1056 | } /* cs_create_connection */ |
| 1057 | |
| 1058 | |
| 1059 | /* |
| 1060 | * PRIVATE: cs_find |
| 1061 | * |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1062 | * Query cscope using command line interface. Parse the output and use tselect |
| 1063 | * to allow choices. Like Nvi, creates a pipe to send to/from query/cscope. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | * |
| 1065 | * returns TRUE if we jump to a tag or abort, FALSE if not. |
| 1066 | */ |
| 1067 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1068 | cs_find(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | { |
| 1070 | char *opt, *pat; |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 1071 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1072 | |
| 1073 | if (cs_check_for_connections() == FALSE) |
| 1074 | { |
| 1075 | (void)EMSG(_("E567: no cscope connections")); |
| 1076 | return FALSE; |
| 1077 | } |
| 1078 | |
| 1079 | if ((opt = strtok((char *)NULL, (const char *)" ")) == NULL) |
| 1080 | { |
| 1081 | cs_usage_msg(Find); |
| 1082 | return FALSE; |
| 1083 | } |
| 1084 | |
| 1085 | pat = opt + strlen(opt) + 1; |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 1086 | if (pat >= (char *)eap->arg + eap_arg_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | { |
| 1088 | cs_usage_msg(Find); |
| 1089 | return FALSE; |
| 1090 | } |
| 1091 | |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 1092 | /* |
| 1093 | * Let's replace the NULs written by strtok() with spaces - we need the |
| 1094 | * spaces to correctly display the quickfix/location list window's title. |
| 1095 | */ |
| 1096 | for (i = 0; i < eap_arg_len; ++i) |
| 1097 | if (NUL == eap->arg[i]) |
| 1098 | eap->arg[i] = ' '; |
| 1099 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1100 | return cs_find_common(opt, pat, eap->forceit, TRUE, |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 1101 | eap->cmdidx == CMD_lcscope, *eap->cmdlinep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | } /* cs_find */ |
| 1103 | |
| 1104 | |
| 1105 | /* |
| 1106 | * PRIVATE: cs_find_common |
| 1107 | * |
| 1108 | * common code for cscope find, shared by cs_find() and do_cstag() |
| 1109 | */ |
| 1110 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1111 | cs_find_common( |
| 1112 | char *opt, |
| 1113 | char *pat, |
| 1114 | int forceit, |
| 1115 | int verbose, |
| 1116 | int use_ll UNUSED, |
| 1117 | char_u *cmdline UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | { |
| 1119 | int i; |
| 1120 | char *cmd; |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1121 | int *nummatches; |
| 1122 | int totmatches; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1123 | #ifdef FEAT_QUICKFIX |
| 1124 | char cmdletter; |
| 1125 | char *qfpos; |
Bram Moolenaar | f1eeae9 | 2010-05-14 23:14:42 +0200 | [diff] [blame] | 1126 | |
| 1127 | /* get cmd letter */ |
| 1128 | switch (opt[0]) |
| 1129 | { |
| 1130 | case '0' : |
| 1131 | cmdletter = 's'; |
| 1132 | break; |
| 1133 | case '1' : |
| 1134 | cmdletter = 'g'; |
| 1135 | break; |
| 1136 | case '2' : |
| 1137 | cmdletter = 'd'; |
| 1138 | break; |
| 1139 | case '3' : |
| 1140 | cmdletter = 'c'; |
| 1141 | break; |
| 1142 | case '4' : |
| 1143 | cmdletter = 't'; |
| 1144 | break; |
| 1145 | case '6' : |
| 1146 | cmdletter = 'e'; |
| 1147 | break; |
| 1148 | case '7' : |
| 1149 | cmdletter = 'f'; |
| 1150 | break; |
| 1151 | case '8' : |
| 1152 | cmdletter = 'i'; |
| 1153 | break; |
| 1154 | default : |
| 1155 | cmdletter = opt[0]; |
| 1156 | } |
| 1157 | |
| 1158 | qfpos = (char *)vim_strchr(p_csqf, cmdletter); |
| 1159 | if (qfpos != NULL) |
| 1160 | { |
| 1161 | qfpos++; |
| 1162 | /* next symbol must be + or - */ |
| 1163 | if (strchr(CSQF_FLAGS, *qfpos) == NULL) |
| 1164 | { |
| 1165 | char *nf = _("E469: invalid cscopequickfix flag %c for %c"); |
| 1166 | char *buf = (char *)alloc((unsigned)strlen(nf)); |
| 1167 | |
| 1168 | /* strlen will be enough because we use chars */ |
| 1169 | if (buf != NULL) |
| 1170 | { |
| 1171 | sprintf(buf, nf, *qfpos, *(qfpos-1)); |
| 1172 | (void)EMSG(buf); |
| 1173 | vim_free(buf); |
| 1174 | } |
| 1175 | return FALSE; |
| 1176 | } |
| 1177 | |
| 1178 | # ifdef FEAT_AUTOCMD |
| 1179 | if (*qfpos != '0') |
| 1180 | { |
| 1181 | apply_autocmds(EVENT_QUICKFIXCMDPRE, (char_u *)"cscope", |
| 1182 | curbuf->b_fname, TRUE, curbuf); |
| 1183 | # ifdef FEAT_EVAL |
| 1184 | if (did_throw || force_abort) |
| 1185 | return FALSE; |
| 1186 | # endif |
| 1187 | } |
| 1188 | # endif |
| 1189 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | #endif |
| 1191 | |
| 1192 | /* create the actual command to send to cscope */ |
| 1193 | cmd = cs_create_cmd(opt, pat); |
| 1194 | if (cmd == NULL) |
| 1195 | return FALSE; |
| 1196 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1197 | nummatches = (int *)alloc(sizeof(int)*csinfo_size); |
| 1198 | if (nummatches == NULL) |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1199 | { |
| 1200 | vim_free(cmd); |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1201 | return FALSE; |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1202 | } |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1203 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1204 | /* Send query to all open connections, then count the total number |
| 1205 | * of matches so we can alloc all in one swell foop. */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1206 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | nummatches[i] = 0; |
| 1208 | totmatches = 0; |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1209 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1210 | { |
Bram Moolenaar | 508b9e8 | 2006-11-21 10:43:23 +0000 | [diff] [blame] | 1211 | if (csinfo[i].fname == NULL || csinfo[i].to_fp == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1212 | continue; |
| 1213 | |
| 1214 | /* send cmd to cscope */ |
| 1215 | (void)fprintf(csinfo[i].to_fp, "%s\n", cmd); |
| 1216 | (void)fflush(csinfo[i].to_fp); |
| 1217 | |
| 1218 | nummatches[i] = cs_cnt_matches(i); |
| 1219 | |
| 1220 | if (nummatches[i] > -1) |
| 1221 | totmatches += nummatches[i]; |
| 1222 | |
| 1223 | if (nummatches[i] == 0) |
| 1224 | (void)cs_read_prompt(i); |
| 1225 | } |
| 1226 | vim_free(cmd); |
| 1227 | |
| 1228 | if (totmatches == 0) |
| 1229 | { |
| 1230 | char *nf = _("E259: no matches found for cscope query %s of %s"); |
| 1231 | char *buf; |
| 1232 | |
| 1233 | if (!verbose) |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1234 | { |
| 1235 | vim_free(nummatches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1236 | return FALSE; |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1239 | buf = (char *)alloc((unsigned)(strlen(opt) + strlen(pat) + strlen(nf))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | if (buf == NULL) |
| 1241 | (void)EMSG(nf); |
| 1242 | else |
| 1243 | { |
| 1244 | sprintf(buf, nf, opt, pat); |
| 1245 | (void)EMSG(buf); |
| 1246 | vim_free(buf); |
| 1247 | } |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1248 | vim_free(nummatches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1249 | return FALSE; |
| 1250 | } |
| 1251 | |
| 1252 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1253 | if (qfpos != NULL && *qfpos != '0' && totmatches > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | { |
| 1255 | /* fill error list */ |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1256 | FILE *f; |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 1257 | char_u *tmp = vim_tempname('c', TRUE); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1258 | qf_info_T *qi = NULL; |
| 1259 | win_T *wp = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1261 | f = mch_fopen((char *)tmp, "w"); |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1262 | if (f == NULL) |
| 1263 | EMSG2(_(e_notopen), tmp); |
| 1264 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | { |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1266 | cs_file_results(f, nummatches); |
| 1267 | fclose(f); |
| 1268 | if (use_ll) /* Use location list */ |
| 1269 | wp = curwin; |
| 1270 | /* '-' starts a new error list */ |
| 1271 | if (qf_init(wp, tmp, (char_u *)"%f%*\\t%l%*\\t%m", |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 1272 | *qfpos == '-', cmdline) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | { |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1274 | # ifdef FEAT_WINDOWS |
| 1275 | if (postponed_split != 0) |
| 1276 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1277 | (void)win_split(postponed_split > 0 ? postponed_split : 0, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | postponed_split_flags); |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 1279 | RESET_BINDING(curwin); |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1280 | postponed_split = 0; |
| 1281 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1282 | # endif |
Bram Moolenaar | f1eeae9 | 2010-05-14 23:14:42 +0200 | [diff] [blame] | 1283 | |
| 1284 | # ifdef FEAT_AUTOCMD |
| 1285 | apply_autocmds(EVENT_QUICKFIXCMDPOST, (char_u *)"cscope", |
| 1286 | curbuf->b_fname, TRUE, curbuf); |
| 1287 | # endif |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1288 | if (use_ll) |
| 1289 | /* |
| 1290 | * In the location list window, use the displayed location |
| 1291 | * list. Otherwise, use the location list for the window. |
| 1292 | */ |
| 1293 | qi = (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL) |
| 1294 | ? wp->w_llist_ref : wp->w_llist; |
| 1295 | qf_jump(qi, 0, 0, forceit); |
| 1296 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | } |
| 1298 | mch_remove(tmp); |
| 1299 | vim_free(tmp); |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1300 | vim_free(nummatches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | return TRUE; |
| 1302 | } |
| 1303 | else |
| 1304 | #endif /* FEAT_QUICKFIX */ |
| 1305 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1306 | char **matches = NULL, **contexts = NULL; |
| 1307 | int matched = 0; |
| 1308 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1309 | /* read output */ |
| 1310 | cs_fill_results((char *)pat, totmatches, nummatches, &matches, |
| 1311 | &contexts, &matched); |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1312 | vim_free(nummatches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | if (matches == NULL) |
| 1314 | return FALSE; |
| 1315 | |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1316 | (void)cs_manage_matches(matches, contexts, matched, Store); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1317 | |
| 1318 | return do_tag((char_u *)pat, DT_CSCOPE, 0, forceit, verbose); |
| 1319 | } |
| 1320 | |
| 1321 | } /* cs_find_common */ |
| 1322 | |
| 1323 | /* |
| 1324 | * PRIVATE: cs_help |
| 1325 | * |
| 1326 | * print help |
| 1327 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1329 | cs_help(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1330 | { |
| 1331 | cscmd_T *cmdp = cs_cmds; |
| 1332 | |
| 1333 | (void)MSG_PUTS(_("cscope commands:\n")); |
| 1334 | while (cmdp->name != NULL) |
| 1335 | { |
Bram Moolenaar | db867d5 | 2009-01-28 15:04:42 +0000 | [diff] [blame] | 1336 | char *help = _(cmdp->help); |
| 1337 | int space_cnt = 30 - vim_strsize((char_u *)help); |
| 1338 | |
| 1339 | /* Use %*s rather than %30s to ensure proper alignment in utf-8 */ |
| 1340 | if (space_cnt < 0) |
| 1341 | space_cnt = 0; |
| 1342 | (void)smsg((char_u *)_("%-5s: %s%*s (Usage: %s)"), |
| 1343 | cmdp->name, |
| 1344 | help, space_cnt, " ", |
| 1345 | cmdp->usage); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1346 | if (strcmp(cmdp->name, "find") == 0) |
Bram Moolenaar | 627943d | 2008-08-25 02:35:59 +0000 | [diff] [blame] | 1347 | MSG_PUTS(_("\n" |
| 1348 | " c: Find functions calling this function\n" |
| 1349 | " d: Find functions called by this function\n" |
| 1350 | " e: Find this egrep pattern\n" |
| 1351 | " f: Find this file\n" |
| 1352 | " g: Find this definition\n" |
| 1353 | " i: Find files #including this file\n" |
| 1354 | " s: Find this C symbol\n" |
Bram Moolenaar | 657ae0b | 2010-12-30 11:41:09 +0100 | [diff] [blame] | 1355 | " t: Find this text string\n")); |
Bram Moolenaar | 627943d | 2008-08-25 02:35:59 +0000 | [diff] [blame] | 1356 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1357 | cmdp++; |
| 1358 | } |
| 1359 | |
| 1360 | wait_return(TRUE); |
| 1361 | return 0; |
| 1362 | } /* cs_help */ |
| 1363 | |
| 1364 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1365 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1366 | clear_csinfo(int i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | { |
| 1368 | csinfo[i].fname = NULL; |
| 1369 | csinfo[i].ppath = NULL; |
| 1370 | csinfo[i].flags = NULL; |
| 1371 | #if defined(UNIX) |
| 1372 | csinfo[i].st_dev = (dev_t)0; |
| 1373 | csinfo[i].st_ino = (ino_t)0; |
| 1374 | #else |
| 1375 | csinfo[i].nVolume = 0; |
| 1376 | csinfo[i].nIndexHigh = 0; |
| 1377 | csinfo[i].nIndexLow = 0; |
| 1378 | #endif |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1379 | csinfo[i].pid = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1380 | csinfo[i].fr_fp = NULL; |
| 1381 | csinfo[i].to_fp = NULL; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 1382 | #if defined(WIN32) |
| 1383 | csinfo[i].hProc = NULL; |
| 1384 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | #ifndef UNIX |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 1388 | static char *GetWin32Error(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | |
| 1390 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1391 | GetWin32Error(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1392 | { |
| 1393 | char *msg = NULL; |
| 1394 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, |
| 1395 | NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL); |
| 1396 | if (msg != NULL) |
| 1397 | { |
| 1398 | /* remove trailing \r\n */ |
| 1399 | char *pcrlf = strstr(msg, "\r\n"); |
| 1400 | if (pcrlf != NULL) |
| 1401 | *pcrlf = '\0'; |
| 1402 | } |
| 1403 | return msg; |
| 1404 | } |
| 1405 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1406 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1407 | /* |
| 1408 | * PRIVATE: cs_insert_filelist |
| 1409 | * |
| 1410 | * insert a new cscope database filename into the filelist |
| 1411 | */ |
| 1412 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1413 | cs_insert_filelist( |
| 1414 | char *fname, |
| 1415 | char *ppath, |
| 1416 | char *flags, |
| 1417 | struct stat *sb UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | { |
| 1419 | short i, j; |
| 1420 | #ifndef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 1422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1423 | /* On windows 9x GetFileInformationByHandle doesn't work, so skip it */ |
| 1424 | if (!mch_windows95()) |
| 1425 | { |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 1426 | switch (win32_fileinfo(fname, &bhfi)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | { |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 1428 | case FILEINFO_ENC_FAIL: /* enc_to_utf16() failed */ |
| 1429 | case FILEINFO_READ_FAIL: /* CreateFile() failed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1430 | if (p_csverbose) |
| 1431 | { |
| 1432 | char *cant_msg = _("E625: cannot open cscope database: %s"); |
| 1433 | char *winmsg = GetWin32Error(); |
| 1434 | |
| 1435 | if (winmsg != NULL) |
| 1436 | { |
| 1437 | (void)EMSG2(cant_msg, winmsg); |
| 1438 | LocalFree(winmsg); |
| 1439 | } |
| 1440 | else |
| 1441 | /* subst filename if can't get error text */ |
| 1442 | (void)EMSG2(cant_msg, fname); |
| 1443 | } |
| 1444 | return -1; |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 1445 | |
| 1446 | case FILEINFO_INFO_FAIL: /* GetFileInformationByHandle() failed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1447 | if (p_csverbose) |
| 1448 | (void)EMSG(_("E626: cannot get cscope database information")); |
| 1449 | return -1; |
| 1450 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1451 | } |
| 1452 | #endif |
| 1453 | |
| 1454 | i = -1; /* can be set to the index of an empty item in csinfo */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1455 | for (j = 0; j < csinfo_size; j++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1456 | { |
| 1457 | if (csinfo[j].fname != NULL |
| 1458 | #if defined(UNIX) |
| 1459 | && csinfo[j].st_dev == sb->st_dev && csinfo[j].st_ino == sb->st_ino |
| 1460 | #else |
| 1461 | /* compare pathnames first */ |
| 1462 | && ((fullpathcmp(csinfo[j].fname, fname, FALSE) & FPC_SAME) |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 1463 | /* if not Windows 9x, test index file attributes too */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1464 | || (!mch_windows95() |
| 1465 | && csinfo[j].nVolume == bhfi.dwVolumeSerialNumber |
| 1466 | && csinfo[j].nIndexHigh == bhfi.nFileIndexHigh |
| 1467 | && csinfo[j].nIndexLow == bhfi.nFileIndexLow)) |
| 1468 | #endif |
| 1469 | ) |
| 1470 | { |
| 1471 | if (p_csverbose) |
| 1472 | (void)EMSG(_("E568: duplicate cscope database not added")); |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | if (csinfo[j].fname == NULL && i == -1) |
| 1477 | i = j; /* remember first empty entry */ |
| 1478 | } |
| 1479 | |
| 1480 | if (i == -1) |
| 1481 | { |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1482 | i = csinfo_size; |
| 1483 | if (csinfo_size == 0) |
| 1484 | { |
| 1485 | /* First time allocation: allocate only 1 connection. It should |
| 1486 | * be enough for most users. If more is needed, csinfo will be |
| 1487 | * reallocated. */ |
| 1488 | csinfo_size = 1; |
| 1489 | csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T)); |
| 1490 | } |
| 1491 | else |
| 1492 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 1493 | csinfo_T *t_csinfo = csinfo; |
| 1494 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1495 | /* Reallocate space for more connections. */ |
| 1496 | csinfo_size *= 2; |
| 1497 | csinfo = vim_realloc(csinfo, sizeof(csinfo_T)*csinfo_size); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 1498 | if (csinfo == NULL) |
| 1499 | { |
| 1500 | vim_free(t_csinfo); |
| 1501 | csinfo_size = 0; |
| 1502 | } |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1503 | } |
| 1504 | if (csinfo == NULL) |
| 1505 | return -1; |
| 1506 | for (j = csinfo_size/2; j < csinfo_size; j++) |
| 1507 | clear_csinfo(j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1510 | if ((csinfo[i].fname = (char *)alloc((unsigned)strlen(fname)+1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1511 | return -1; |
| 1512 | |
| 1513 | (void)strcpy(csinfo[i].fname, (const char *)fname); |
| 1514 | |
| 1515 | if (ppath != NULL) |
| 1516 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1517 | if ((csinfo[i].ppath = (char *)alloc((unsigned)strlen(ppath) + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1518 | { |
| 1519 | vim_free(csinfo[i].fname); |
| 1520 | csinfo[i].fname = NULL; |
| 1521 | return -1; |
| 1522 | } |
| 1523 | (void)strcpy(csinfo[i].ppath, (const char *)ppath); |
| 1524 | } else |
| 1525 | csinfo[i].ppath = NULL; |
| 1526 | |
| 1527 | if (flags != NULL) |
| 1528 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1529 | if ((csinfo[i].flags = (char *)alloc((unsigned)strlen(flags) + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | { |
| 1531 | vim_free(csinfo[i].fname); |
| 1532 | vim_free(csinfo[i].ppath); |
| 1533 | csinfo[i].fname = NULL; |
| 1534 | csinfo[i].ppath = NULL; |
| 1535 | return -1; |
| 1536 | } |
| 1537 | (void)strcpy(csinfo[i].flags, (const char *)flags); |
| 1538 | } else |
| 1539 | csinfo[i].flags = NULL; |
| 1540 | |
| 1541 | #if defined(UNIX) |
| 1542 | csinfo[i].st_dev = sb->st_dev; |
| 1543 | csinfo[i].st_ino = sb->st_ino; |
| 1544 | |
| 1545 | #else |
| 1546 | csinfo[i].nVolume = bhfi.dwVolumeSerialNumber; |
| 1547 | csinfo[i].nIndexLow = bhfi.nFileIndexLow; |
| 1548 | csinfo[i].nIndexHigh = bhfi.nFileIndexHigh; |
| 1549 | #endif |
| 1550 | return i; |
| 1551 | } /* cs_insert_filelist */ |
| 1552 | |
| 1553 | |
| 1554 | /* |
| 1555 | * PRIVATE: cs_lookup_cmd |
| 1556 | * |
| 1557 | * find cscope command in command table |
| 1558 | */ |
| 1559 | static cscmd_T * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1560 | cs_lookup_cmd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1561 | { |
| 1562 | cscmd_T *cmdp; |
| 1563 | char *stok; |
| 1564 | size_t len; |
| 1565 | |
| 1566 | if (eap->arg == NULL) |
| 1567 | return NULL; |
| 1568 | |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 1569 | /* Store length of eap->arg before it gets modified by strtok(). */ |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 1570 | eap_arg_len = (int)STRLEN(eap->arg); |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 1571 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | if ((stok = strtok((char *)(eap->arg), (const char *)" ")) == NULL) |
| 1573 | return NULL; |
| 1574 | |
| 1575 | len = strlen(stok); |
| 1576 | for (cmdp = cs_cmds; cmdp->name != NULL; ++cmdp) |
| 1577 | { |
| 1578 | if (strncmp((const char *)(stok), cmdp->name, len) == 0) |
| 1579 | return (cmdp); |
| 1580 | } |
| 1581 | return NULL; |
| 1582 | } /* cs_lookup_cmd */ |
| 1583 | |
| 1584 | |
| 1585 | /* |
| 1586 | * PRIVATE: cs_kill |
| 1587 | * |
| 1588 | * nuke em |
| 1589 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1590 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1591 | cs_kill(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1592 | { |
| 1593 | char *stok; |
| 1594 | short i; |
| 1595 | |
| 1596 | if ((stok = strtok((char *)NULL, (const char *)" ")) == NULL) |
| 1597 | { |
| 1598 | cs_usage_msg(Kill); |
| 1599 | return CSCOPE_FAILURE; |
| 1600 | } |
| 1601 | |
| 1602 | /* only single digit positive and negative integers are allowed */ |
| 1603 | if ((strlen(stok) < 2 && VIM_ISDIGIT((int)(stok[0]))) |
| 1604 | || (strlen(stok) < 3 && stok[0] == '-' |
| 1605 | && VIM_ISDIGIT((int)(stok[1])))) |
| 1606 | i = atoi(stok); |
| 1607 | else |
| 1608 | { |
| 1609 | /* It must be part of a name. We will try to find a match |
| 1610 | * within all the names in the csinfo data structure |
| 1611 | */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1612 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | { |
| 1614 | if (csinfo[i].fname != NULL && strstr(csinfo[i].fname, stok)) |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1619 | if ((i != -1) && (i >= csinfo_size || i < -1 || csinfo[i].fname == NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1620 | { |
| 1621 | if (p_csverbose) |
| 1622 | (void)EMSG2(_("E261: cscope connection %s not found"), stok); |
| 1623 | } |
| 1624 | else |
| 1625 | { |
| 1626 | if (i == -1) |
| 1627 | { |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1628 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1629 | { |
| 1630 | if (csinfo[i].fname) |
| 1631 | cs_kill_execute(i, csinfo[i].fname); |
| 1632 | } |
| 1633 | } |
| 1634 | else |
| 1635 | cs_kill_execute(i, stok); |
| 1636 | } |
| 1637 | |
| 1638 | return 0; |
| 1639 | } /* cs_kill */ |
| 1640 | |
| 1641 | |
| 1642 | /* |
| 1643 | * PRIVATE: cs_kill_execute |
| 1644 | * |
| 1645 | * Actually kills a specific cscope connection. |
| 1646 | */ |
| 1647 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1648 | cs_kill_execute( |
| 1649 | int i, /* cscope table index */ |
| 1650 | char *cname) /* cscope database name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1651 | { |
| 1652 | if (p_csverbose) |
| 1653 | { |
| 1654 | msg_clr_eos(); |
| 1655 | (void)smsg_attr(hl_attr(HLF_R) | MSG_HIST, |
| 1656 | (char_u *)_("cscope connection %s closed"), cname); |
| 1657 | } |
| 1658 | cs_release_csp(i, TRUE); |
| 1659 | } |
| 1660 | |
| 1661 | |
| 1662 | /* |
| 1663 | * PRIVATE: cs_make_vim_style_matches |
| 1664 | * |
Bram Moolenaar | 657ae0b | 2010-12-30 11:41:09 +0100 | [diff] [blame] | 1665 | * convert the cscope output into a ctags style entry (as might be found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | * in a ctags tags file). there's one catch though: cscope doesn't tell you |
| 1667 | * the type of the tag you are looking for. for example, in Darren Hiebert's |
| 1668 | * ctags (the one that comes with vim), #define's use a line number to find the |
| 1669 | * tag in a file while function definitions use a regexp search pattern. |
| 1670 | * |
| 1671 | * i'm going to always use the line number because cscope does something |
| 1672 | * quirky (and probably other things i don't know about): |
| 1673 | * |
| 1674 | * if you have "# define" in your source file, which is |
| 1675 | * perfectly legal, cscope thinks you have "#define". this |
| 1676 | * will result in a failed regexp search. :( |
| 1677 | * |
| 1678 | * besides, even if this particular case didn't happen, the search pattern |
| 1679 | * would still have to be modified to escape all the special regular expression |
| 1680 | * characters to comply with ctags formatting. |
| 1681 | */ |
| 1682 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1683 | cs_make_vim_style_matches( |
| 1684 | char *fname, |
| 1685 | char *slno, |
| 1686 | char *search, |
| 1687 | char *tagstr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1688 | { |
| 1689 | /* vim style is ctags: |
| 1690 | * |
| 1691 | * <tagstr>\t<filename>\t<linenum_or_search>"\t<extra> |
| 1692 | * |
| 1693 | * but as mentioned above, we'll always use the line number and |
| 1694 | * put the search pattern (if one exists) as "extra" |
| 1695 | * |
| 1696 | * buf is used as part of vim's method of handling tags, and |
| 1697 | * (i think) vim frees it when you pop your tags and get replaced |
| 1698 | * by new ones on the tag stack. |
| 1699 | */ |
| 1700 | char *buf; |
| 1701 | int amt; |
| 1702 | |
| 1703 | if (search != NULL) |
| 1704 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1705 | amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + strlen(search)+6); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1706 | if ((buf = (char *)alloc(amt)) == NULL) |
| 1707 | return NULL; |
| 1708 | |
| 1709 | (void)sprintf(buf, "%s\t%s\t%s;\"\t%s", tagstr, fname, slno, search); |
| 1710 | } |
| 1711 | else |
| 1712 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1713 | amt = (int)(strlen(fname) + strlen(slno) + strlen(tagstr) + 5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1714 | if ((buf = (char *)alloc(amt)) == NULL) |
| 1715 | return NULL; |
| 1716 | |
| 1717 | (void)sprintf(buf, "%s\t%s\t%s;\"", tagstr, fname, slno); |
| 1718 | } |
| 1719 | |
| 1720 | return buf; |
| 1721 | } /* cs_make_vim_style_matches */ |
| 1722 | |
| 1723 | |
| 1724 | /* |
| 1725 | * PRIVATE: cs_manage_matches |
| 1726 | * |
| 1727 | * this is kind of hokey, but i don't see an easy way round this.. |
| 1728 | * |
| 1729 | * Store: keep a ptr to the (malloc'd) memory of matches originally |
| 1730 | * generated from cs_find(). the matches are originally lines directly |
| 1731 | * from cscope output, but transformed to look like something out of a |
| 1732 | * ctags. see cs_make_vim_style_matches for more details. |
| 1733 | * |
| 1734 | * Get: used only from cs_fgets(), this simulates a vim_fgets() to return |
| 1735 | * the next line from the cscope output. it basically keeps track of which |
| 1736 | * lines have been "used" and returns the next one. |
| 1737 | * |
| 1738 | * Free: frees up everything and resets |
| 1739 | * |
| 1740 | * Print: prints the tags |
| 1741 | */ |
| 1742 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1743 | cs_manage_matches( |
| 1744 | char **matches, |
| 1745 | char **contexts, |
| 1746 | int totmatches, |
| 1747 | mcmd_e cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | { |
| 1749 | static char **mp = NULL; |
| 1750 | static char **cp = NULL; |
| 1751 | static int cnt = -1; |
| 1752 | static int next = -1; |
| 1753 | char *p = NULL; |
| 1754 | |
| 1755 | switch (cmd) |
| 1756 | { |
| 1757 | case Store: |
| 1758 | assert(matches != NULL); |
| 1759 | assert(totmatches > 0); |
| 1760 | if (mp != NULL || cp != NULL) |
| 1761 | (void)cs_manage_matches(NULL, NULL, -1, Free); |
| 1762 | mp = matches; |
| 1763 | cp = contexts; |
| 1764 | cnt = totmatches; |
| 1765 | next = 0; |
| 1766 | break; |
| 1767 | case Get: |
| 1768 | if (next >= cnt) |
| 1769 | return NULL; |
| 1770 | |
| 1771 | p = mp[next]; |
| 1772 | next++; |
| 1773 | break; |
| 1774 | case Free: |
| 1775 | if (mp != NULL) |
| 1776 | { |
| 1777 | if (cnt > 0) |
| 1778 | while (cnt--) |
| 1779 | { |
| 1780 | vim_free(mp[cnt]); |
| 1781 | if (cp != NULL) |
| 1782 | vim_free(cp[cnt]); |
| 1783 | } |
| 1784 | vim_free(mp); |
| 1785 | vim_free(cp); |
| 1786 | } |
| 1787 | mp = NULL; |
| 1788 | cp = NULL; |
| 1789 | cnt = 0; |
| 1790 | next = 0; |
| 1791 | break; |
| 1792 | case Print: |
| 1793 | cs_print_tags_priv(mp, cp, cnt); |
| 1794 | break; |
| 1795 | default: /* should not reach here */ |
| 1796 | (void)EMSG(_("E570: fatal error in cs_manage_matches")); |
| 1797 | return NULL; |
| 1798 | } |
| 1799 | |
| 1800 | return p; |
| 1801 | } /* cs_manage_matches */ |
| 1802 | |
| 1803 | |
| 1804 | /* |
| 1805 | * PRIVATE: cs_parse_results |
| 1806 | * |
| 1807 | * parse cscope output |
| 1808 | */ |
| 1809 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1810 | cs_parse_results( |
| 1811 | int cnumber, |
| 1812 | char *buf, |
| 1813 | int bufsize, |
| 1814 | char **context, |
| 1815 | char **linenumber, |
| 1816 | char **search) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1817 | { |
| 1818 | int ch; |
| 1819 | char *p; |
| 1820 | char *name; |
| 1821 | |
| 1822 | if (fgets(buf, bufsize, csinfo[cnumber].fr_fp) == NULL) |
| 1823 | { |
| 1824 | if (feof(csinfo[cnumber].fr_fp)) |
| 1825 | errno = EIO; |
| 1826 | |
| 1827 | cs_reading_emsg(cnumber); |
| 1828 | |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | |
| 1832 | /* If the line's too long for the buffer, discard it. */ |
| 1833 | if ((p = strchr(buf, '\n')) == NULL) |
| 1834 | { |
| 1835 | while ((ch = getc(csinfo[cnumber].fr_fp)) != EOF && ch != '\n') |
| 1836 | ; |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | *p = '\0'; |
| 1840 | |
| 1841 | /* |
| 1842 | * cscope output is in the following format: |
| 1843 | * |
| 1844 | * <filename> <context> <line number> <pattern> |
| 1845 | */ |
| 1846 | if ((name = strtok((char *)buf, (const char *)" ")) == NULL) |
| 1847 | return NULL; |
| 1848 | if ((*context = strtok(NULL, (const char *)" ")) == NULL) |
| 1849 | return NULL; |
| 1850 | if ((*linenumber = strtok(NULL, (const char *)" ")) == NULL) |
| 1851 | return NULL; |
| 1852 | *search = *linenumber + strlen(*linenumber) + 1; /* +1 to skip \0 */ |
| 1853 | |
| 1854 | /* --- nvi --- |
| 1855 | * If the file is older than the cscope database, that is, |
| 1856 | * the database was built since the file was last modified, |
| 1857 | * or there wasn't a search string, use the line number. |
| 1858 | */ |
| 1859 | if (strcmp(*search, "<unknown>") == 0) |
| 1860 | *search = NULL; |
| 1861 | |
| 1862 | name = cs_resolve_file(cnumber, name); |
| 1863 | return name; |
| 1864 | } |
| 1865 | |
Bram Moolenaar | c716c30 | 2006-01-21 22:12:51 +0000 | [diff] [blame] | 1866 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1867 | /* |
| 1868 | * PRIVATE: cs_file_results |
| 1869 | * |
| 1870 | * write cscope find results to file |
| 1871 | */ |
| 1872 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1873 | cs_file_results(FILE *f, int *nummatches_a) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | { |
| 1875 | int i, j; |
| 1876 | char *buf; |
| 1877 | char *search, *slno; |
| 1878 | char *fullname; |
| 1879 | char *cntx; |
| 1880 | char *context; |
| 1881 | |
| 1882 | buf = (char *)alloc(CSREAD_BUFSIZE); |
| 1883 | if (buf == NULL) |
| 1884 | return; |
| 1885 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1886 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | { |
| 1888 | if (nummatches_a[i] < 1) |
| 1889 | continue; |
| 1890 | |
| 1891 | for (j = 0; j < nummatches_a[i]; j++) |
| 1892 | { |
Bram Moolenaar | 5c06f8b | 2005-05-31 22:14:58 +0000 | [diff] [blame] | 1893 | if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx, |
| 1894 | &slno, &search)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1895 | continue; |
| 1896 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1897 | context = (char *)alloc((unsigned)strlen(cntx)+5); |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1898 | if (context == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | continue; |
| 1900 | |
| 1901 | if (strcmp(cntx, "<global>")==0) |
| 1902 | strcpy(context, "<<global>>"); |
| 1903 | else |
| 1904 | sprintf(context, "<<%s>>", cntx); |
| 1905 | |
Bram Moolenaar | 0cae847 | 2006-10-30 21:32:28 +0000 | [diff] [blame] | 1906 | if (search == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | fprintf(f, "%s\t%s\t%s\n", fullname, slno, context); |
| 1908 | else |
| 1909 | fprintf(f, "%s\t%s\t%s %s\n", fullname, slno, context, search); |
| 1910 | |
| 1911 | vim_free(context); |
| 1912 | vim_free(fullname); |
| 1913 | } /* for all matches */ |
| 1914 | |
| 1915 | (void)cs_read_prompt(i); |
| 1916 | |
| 1917 | } /* for all cscope connections */ |
| 1918 | vim_free(buf); |
| 1919 | } |
Bram Moolenaar | c716c30 | 2006-01-21 22:12:51 +0000 | [diff] [blame] | 1920 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1921 | |
| 1922 | /* |
| 1923 | * PRIVATE: cs_fill_results |
| 1924 | * |
| 1925 | * get parsed cscope output and calls cs_make_vim_style_matches to convert |
| 1926 | * into ctags format |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 1927 | * When there are no matches sets "*matches_p" to NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1928 | */ |
| 1929 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1930 | cs_fill_results( |
| 1931 | char *tagstr, |
| 1932 | int totmatches, |
| 1933 | int *nummatches_a, |
| 1934 | char ***matches_p, |
| 1935 | char ***cntxts_p, |
| 1936 | int *matched) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1937 | { |
| 1938 | int i, j; |
| 1939 | char *buf; |
| 1940 | char *search, *slno; |
| 1941 | int totsofar = 0; |
| 1942 | char **matches = NULL; |
| 1943 | char **cntxts = NULL; |
| 1944 | char *fullname; |
| 1945 | char *cntx; |
| 1946 | |
| 1947 | assert(totmatches > 0); |
| 1948 | |
| 1949 | buf = (char *)alloc(CSREAD_BUFSIZE); |
| 1950 | if (buf == NULL) |
| 1951 | return; |
| 1952 | |
| 1953 | if ((matches = (char **)alloc(sizeof(char *) * totmatches)) == NULL) |
| 1954 | goto parse_out; |
| 1955 | if ((cntxts = (char **)alloc(sizeof(char *) * totmatches)) == NULL) |
| 1956 | goto parse_out; |
| 1957 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 1958 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | { |
| 1960 | if (nummatches_a[i] < 1) |
| 1961 | continue; |
| 1962 | |
| 1963 | for (j = 0; j < nummatches_a[i]; j++) |
| 1964 | { |
| 1965 | if ((fullname = cs_parse_results(i, buf, CSREAD_BUFSIZE, &cntx, |
| 1966 | &slno, &search)) == NULL) |
| 1967 | continue; |
| 1968 | |
| 1969 | matches[totsofar] = cs_make_vim_style_matches(fullname, slno, |
| 1970 | search, tagstr); |
| 1971 | |
| 1972 | vim_free(fullname); |
| 1973 | |
| 1974 | if (strcmp(cntx, "<global>") == 0) |
| 1975 | cntxts[totsofar] = NULL; |
| 1976 | else |
| 1977 | /* note: if vim_strsave returns NULL, then the context |
| 1978 | * will be "<global>", which is misleading. |
| 1979 | */ |
| 1980 | cntxts[totsofar] = (char *)vim_strsave((char_u *)cntx); |
| 1981 | |
| 1982 | if (matches[totsofar] != NULL) |
| 1983 | totsofar++; |
| 1984 | |
| 1985 | } /* for all matches */ |
| 1986 | |
| 1987 | (void)cs_read_prompt(i); |
| 1988 | |
| 1989 | } /* for all cscope connections */ |
| 1990 | |
| 1991 | parse_out: |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 1992 | if (totsofar == 0) |
| 1993 | { |
| 1994 | /* No matches, free the arrays and return NULL in "*matches_p". */ |
| 1995 | vim_free(matches); |
| 1996 | matches = NULL; |
| 1997 | vim_free(cntxts); |
| 1998 | cntxts = NULL; |
| 1999 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2000 | *matched = totsofar; |
| 2001 | *matches_p = matches; |
| 2002 | *cntxts_p = cntxts; |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 2003 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | vim_free(buf); |
| 2005 | } /* cs_fill_results */ |
| 2006 | |
| 2007 | |
| 2008 | /* get the requested path components */ |
| 2009 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2010 | cs_pathcomponents(char *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | { |
| 2012 | int i; |
| 2013 | char *s; |
| 2014 | |
| 2015 | if (p_cspc == 0) |
| 2016 | return path; |
| 2017 | |
| 2018 | s = path + strlen(path) - 1; |
| 2019 | for (i = 0; i < p_cspc; ++i) |
| 2020 | while (s > path && *--s != '/' |
| 2021 | #ifdef WIN32 |
| 2022 | && *--s != '\\' |
| 2023 | #endif |
| 2024 | ) |
| 2025 | ; |
| 2026 | if ((s > path && *s == '/') |
| 2027 | #ifdef WIN32 |
| 2028 | || (s > path && *s == '\\') |
| 2029 | #endif |
| 2030 | ) |
| 2031 | ++s; |
| 2032 | return s; |
| 2033 | } |
| 2034 | |
| 2035 | /* |
| 2036 | * PRIVATE: cs_print_tags_priv |
| 2037 | * |
| 2038 | * called from cs_manage_matches() |
| 2039 | */ |
| 2040 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2041 | cs_print_tags_priv(char **matches, char **cntxts, int num_matches) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | { |
| 2043 | char *buf = NULL; |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2044 | char *t_buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2045 | int bufsize = 0; /* Track available bufsize */ |
| 2046 | int newsize = 0; |
| 2047 | char *ptag; |
| 2048 | char *fname, *lno, *extra, *tbuf; |
| 2049 | int i, idx, num; |
| 2050 | char *globalcntx = "GLOBAL"; |
| 2051 | char *cntxformat = " <<%s>>"; |
| 2052 | char *context; |
| 2053 | char *cstag_msg = _("Cscope tag: %s"); |
| 2054 | char *csfmt_str = "%4d %6s "; |
| 2055 | |
| 2056 | assert (num_matches > 0); |
| 2057 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2058 | if ((tbuf = (char *)alloc((unsigned)strlen(matches[0]) + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | |
| 2061 | strcpy(tbuf, matches[0]); |
| 2062 | ptag = strtok(tbuf, "\t"); |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2063 | if (ptag == NULL) |
| 2064 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2066 | newsize = (int)(strlen(cstag_msg) + strlen(ptag)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | buf = (char *)alloc(newsize); |
| 2068 | if (buf != NULL) |
| 2069 | { |
| 2070 | bufsize = newsize; |
| 2071 | (void)sprintf(buf, cstag_msg, ptag); |
| 2072 | MSG_PUTS_ATTR(buf, hl_attr(HLF_T)); |
| 2073 | } |
| 2074 | |
| 2075 | vim_free(tbuf); |
| 2076 | |
| 2077 | MSG_PUTS_ATTR(_("\n # line"), hl_attr(HLF_T)); /* strlen is 7 */ |
| 2078 | msg_advance(msg_col + 2); |
| 2079 | MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T)); |
| 2080 | |
| 2081 | num = 1; |
| 2082 | for (i = 0; i < num_matches; i++) |
| 2083 | { |
| 2084 | idx = i; |
| 2085 | |
| 2086 | /* if we really wanted to, we could avoid this malloc and strcpy |
| 2087 | * by parsing matches[i] on the fly and placing stuff into buf |
| 2088 | * directly, but that's too much of a hassle |
| 2089 | */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2090 | if ((tbuf = (char *)alloc((unsigned)strlen(matches[idx]) + 1)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | continue; |
| 2092 | (void)strcpy(tbuf, matches[idx]); |
| 2093 | |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 2094 | if (strtok(tbuf, (const char *)"\t") == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2095 | continue; |
| 2096 | if ((fname = strtok(NULL, (const char *)"\t")) == NULL) |
| 2097 | continue; |
| 2098 | if ((lno = strtok(NULL, (const char *)"\t")) == NULL) |
Bram Moolenaar | f2a4e33 | 2007-02-27 17:08:16 +0000 | [diff] [blame] | 2099 | continue; |
| 2100 | extra = strtok(NULL, (const char *)"\t"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2101 | |
| 2102 | lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */ |
| 2103 | |
| 2104 | /* hopefully 'num' (num of matches) will be less than 10^16 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2105 | newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2106 | if (bufsize < newsize) |
| 2107 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2108 | t_buf = buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | buf = (char *)vim_realloc(buf, newsize); |
| 2110 | if (buf == NULL) |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2111 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2112 | bufsize = 0; |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2113 | vim_free(t_buf); |
| 2114 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | else |
| 2116 | bufsize = newsize; |
| 2117 | } |
| 2118 | if (buf != NULL) |
| 2119 | { |
| 2120 | /* csfmt_str = "%4d %6s "; */ |
| 2121 | (void)sprintf(buf, csfmt_str, num, lno); |
| 2122 | MSG_PUTS_ATTR(buf, hl_attr(HLF_CM)); |
| 2123 | } |
| 2124 | MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM)); |
| 2125 | |
| 2126 | /* compute the required space for the context */ |
| 2127 | if (cntxts[idx] != NULL) |
| 2128 | context = cntxts[idx]; |
| 2129 | else |
| 2130 | context = globalcntx; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2131 | newsize = (int)(strlen(context) + strlen(cntxformat)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | |
| 2133 | if (bufsize < newsize) |
| 2134 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2135 | t_buf = buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | buf = (char *)vim_realloc(buf, newsize); |
| 2137 | if (buf == NULL) |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2138 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | bufsize = 0; |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 2140 | vim_free(t_buf); |
| 2141 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | else |
| 2143 | bufsize = newsize; |
| 2144 | } |
| 2145 | if (buf != NULL) |
| 2146 | { |
| 2147 | (void)sprintf(buf, cntxformat, context); |
| 2148 | |
| 2149 | /* print the context only if it fits on the same line */ |
| 2150 | if (msg_col + (int)strlen(buf) >= (int)Columns) |
| 2151 | msg_putchar('\n'); |
| 2152 | msg_advance(12); |
| 2153 | MSG_PUTS_LONG(buf); |
| 2154 | msg_putchar('\n'); |
| 2155 | } |
| 2156 | if (extra != NULL) |
| 2157 | { |
| 2158 | msg_advance(13); |
| 2159 | MSG_PUTS_LONG(extra); |
| 2160 | } |
| 2161 | |
| 2162 | vim_free(tbuf); /* only after printing extra due to strtok use */ |
| 2163 | |
| 2164 | if (msg_col) |
| 2165 | msg_putchar('\n'); |
| 2166 | |
| 2167 | ui_breakcheck(); |
| 2168 | if (got_int) |
| 2169 | { |
| 2170 | got_int = FALSE; /* don't print any more matches */ |
| 2171 | break; |
| 2172 | } |
| 2173 | |
| 2174 | num++; |
| 2175 | } /* for all matches */ |
| 2176 | |
| 2177 | vim_free(buf); |
| 2178 | } /* cs_print_tags_priv */ |
| 2179 | |
| 2180 | |
| 2181 | /* |
| 2182 | * PRIVATE: cs_read_prompt |
| 2183 | * |
| 2184 | * read a cscope prompt (basically, skip over the ">> ") |
| 2185 | */ |
| 2186 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2187 | cs_read_prompt(int i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | { |
| 2189 | int ch; |
| 2190 | char *buf = NULL; /* buffer for possible error message from cscope */ |
| 2191 | int bufpos = 0; |
| 2192 | char *cs_emsg; |
| 2193 | int maxlen; |
| 2194 | static char *eprompt = "Press the RETURN key to continue:"; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2195 | int epromptlen = (int)strlen(eprompt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2196 | int n; |
| 2197 | |
| 2198 | cs_emsg = _("E609: Cscope error: %s"); |
| 2199 | /* compute maximum allowed len for Cscope error message */ |
| 2200 | maxlen = (int)(IOSIZE - strlen(cs_emsg)); |
| 2201 | |
| 2202 | for (;;) |
| 2203 | { |
| 2204 | while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0]) |
| 2205 | /* if there is room and char is printable */ |
| 2206 | if (bufpos < maxlen - 1 && vim_isprintc(ch)) |
| 2207 | { |
| 2208 | if (buf == NULL) /* lazy buffer allocation */ |
| 2209 | buf = (char *)alloc(maxlen); |
| 2210 | if (buf != NULL) |
| 2211 | { |
| 2212 | /* append character to the message */ |
| 2213 | buf[bufpos++] = ch; |
| 2214 | buf[bufpos] = NUL; |
| 2215 | if (bufpos >= epromptlen |
| 2216 | && strcmp(&buf[bufpos - epromptlen], eprompt) == 0) |
| 2217 | { |
| 2218 | /* remove eprompt from buf */ |
| 2219 | buf[bufpos - epromptlen] = NUL; |
| 2220 | |
| 2221 | /* print message to user */ |
| 2222 | (void)EMSG2(cs_emsg, buf); |
| 2223 | |
| 2224 | /* send RETURN to cscope */ |
| 2225 | (void)putc('\n', csinfo[i].to_fp); |
| 2226 | (void)fflush(csinfo[i].to_fp); |
| 2227 | |
| 2228 | /* clear buf */ |
| 2229 | bufpos = 0; |
| 2230 | buf[bufpos] = NUL; |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | for (n = 0; n < (int)strlen(CSCOPE_PROMPT); ++n) |
| 2236 | { |
| 2237 | if (n > 0) |
| 2238 | ch = getc(csinfo[i].fr_fp); |
| 2239 | if (ch == EOF) |
| 2240 | { |
| 2241 | PERROR("cs_read_prompt EOF"); |
| 2242 | if (buf != NULL && buf[0] != NUL) |
| 2243 | (void)EMSG2(cs_emsg, buf); |
| 2244 | else if (p_csverbose) |
| 2245 | cs_reading_emsg(i); /* don't have additional information */ |
| 2246 | cs_release_csp(i, TRUE); |
| 2247 | vim_free(buf); |
| 2248 | return CSCOPE_FAILURE; |
| 2249 | } |
| 2250 | |
| 2251 | if (ch != CSCOPE_PROMPT[n]) |
| 2252 | { |
| 2253 | ch = EOF; |
| 2254 | break; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | if (ch == EOF) |
| 2259 | continue; /* didn't find the prompt */ |
| 2260 | break; /* did find the prompt */ |
| 2261 | } |
| 2262 | |
| 2263 | vim_free(buf); |
| 2264 | return CSCOPE_SUCCESS; |
| 2265 | } |
| 2266 | |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2267 | #if defined(UNIX) && defined(SIGALRM) |
| 2268 | /* |
| 2269 | * Used to catch and ignore SIGALRM below. |
| 2270 | */ |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2271 | static RETSIGTYPE |
| 2272 | sig_handler SIGDEFARG(sigarg) |
| 2273 | { |
| 2274 | /* do nothing */ |
| 2275 | SIGRETURN; |
| 2276 | } |
| 2277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2278 | |
| 2279 | /* |
| 2280 | * PRIVATE: cs_release_csp |
| 2281 | * |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2282 | * Does the actual free'ing for the cs ptr with an optional flag of whether |
| 2283 | * or not to free the filename. Called by cs_kill and cs_reset. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | */ |
| 2285 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2286 | cs_release_csp(int i, int freefnpp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2288 | /* |
| 2289 | * Trying to exit normally (not sure whether it is fit to UNIX cscope |
| 2290 | */ |
| 2291 | if (csinfo[i].to_fp != NULL) |
| 2292 | { |
| 2293 | (void)fputs("q\n", csinfo[i].to_fp); |
| 2294 | (void)fflush(csinfo[i].to_fp); |
| 2295 | } |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2296 | #if defined(UNIX) |
| 2297 | { |
Bram Moolenaar | e9b2884 | 2008-04-01 12:31:14 +0000 | [diff] [blame] | 2298 | int waitpid_errno; |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2299 | int pstat; |
| 2300 | pid_t pid; |
| 2301 | |
| 2302 | # if defined(HAVE_SIGACTION) |
| 2303 | struct sigaction sa, old; |
| 2304 | |
Bram Moolenaar | 9701da0 | 2008-03-16 12:09:58 +0000 | [diff] [blame] | 2305 | /* Use sigaction() to limit the waiting time to two seconds. */ |
| 2306 | sigemptyset(&sa.sa_mask); |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2307 | sa.sa_handler = sig_handler; |
Bram Moolenaar | 25153e1 | 2010-02-24 14:47:08 +0100 | [diff] [blame] | 2308 | # ifdef SA_NODEFER |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2309 | sa.sa_flags = SA_NODEFER; |
Bram Moolenaar | 25153e1 | 2010-02-24 14:47:08 +0100 | [diff] [blame] | 2310 | # else |
| 2311 | sa.sa_flags = 0; |
| 2312 | # endif |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2313 | sigaction(SIGALRM, &sa, &old); |
| 2314 | alarm(2); /* 2 sec timeout */ |
| 2315 | |
| 2316 | /* Block until cscope exits or until timer expires */ |
| 2317 | pid = waitpid(csinfo[i].pid, &pstat, 0); |
Bram Moolenaar | e9b2884 | 2008-04-01 12:31:14 +0000 | [diff] [blame] | 2318 | waitpid_errno = errno; |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2319 | |
| 2320 | /* cancel pending alarm if still there and restore signal */ |
| 2321 | alarm(0); |
| 2322 | sigaction(SIGALRM, &old, NULL); |
| 2323 | # else |
| 2324 | int waited; |
| 2325 | |
| 2326 | /* Can't use sigaction(), loop for two seconds. First yield the CPU |
| 2327 | * to give cscope a chance to exit quickly. */ |
| 2328 | sleep(0); |
| 2329 | for (waited = 0; waited < 40; ++waited) |
| 2330 | { |
| 2331 | pid = waitpid(csinfo[i].pid, &pstat, WNOHANG); |
Bram Moolenaar | e9b2884 | 2008-04-01 12:31:14 +0000 | [diff] [blame] | 2332 | waitpid_errno = errno; |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2333 | if (pid != 0) |
| 2334 | break; /* break unless the process is still running */ |
Bram Moolenaar | 91519e4 | 2008-04-01 18:59:07 +0000 | [diff] [blame] | 2335 | mch_delay(50L, FALSE); /* sleep 50 ms */ |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2336 | } |
| 2337 | # endif |
| 2338 | /* |
| 2339 | * If the cscope process is still running: kill it. |
| 2340 | * Safety check: If the PID would be zero here, the entire X session |
| 2341 | * would be killed. -1 and 1 are dangerous as well. |
| 2342 | */ |
| 2343 | if (pid < 0 && csinfo[i].pid > 1) |
| 2344 | { |
Bram Moolenaar | e9b2884 | 2008-04-01 12:31:14 +0000 | [diff] [blame] | 2345 | # ifdef ECHILD |
| 2346 | int alive = TRUE; |
| 2347 | |
| 2348 | if (waitpid_errno == ECHILD) |
| 2349 | { |
| 2350 | /* |
| 2351 | * When using 'vim -g', vim is forked and cscope process is |
| 2352 | * no longer a child process but a sibling. So waitpid() |
| 2353 | * fails with errno being ECHILD (No child processes). |
| 2354 | * Don't send SIGKILL to cscope immediately but wait |
| 2355 | * (polling) for it to exit normally as result of sending |
| 2356 | * the "q" command, hence giving it a chance to clean up |
| 2357 | * its temporary files. |
| 2358 | */ |
| 2359 | int waited; |
| 2360 | |
| 2361 | sleep(0); |
| 2362 | for (waited = 0; waited < 40; ++waited) |
| 2363 | { |
| 2364 | /* Check whether cscope process is still alive */ |
| 2365 | if (kill(csinfo[i].pid, 0) != 0) |
| 2366 | { |
| 2367 | alive = FALSE; /* cscope process no longer exists */ |
| 2368 | break; |
| 2369 | } |
Bram Moolenaar | 91519e4 | 2008-04-01 18:59:07 +0000 | [diff] [blame] | 2370 | mch_delay(50L, FALSE); /* sleep 50ms */ |
Bram Moolenaar | e9b2884 | 2008-04-01 12:31:14 +0000 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | if (alive) |
| 2374 | # endif |
| 2375 | { |
| 2376 | kill(csinfo[i].pid, SIGKILL); |
| 2377 | (void)waitpid(csinfo[i].pid, &pstat, 0); |
| 2378 | } |
Bram Moolenaar | 7dc767c | 2008-03-15 11:41:07 +0000 | [diff] [blame] | 2379 | } |
| 2380 | } |
| 2381 | #else /* !UNIX */ |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2382 | if (csinfo[i].hProc != NULL) |
| 2383 | { |
| 2384 | /* Give cscope a chance to exit normally */ |
| 2385 | if (WaitForSingleObject(csinfo[i].hProc, 1000) == WAIT_TIMEOUT) |
| 2386 | TerminateProcess(csinfo[i].hProc, 0); |
| 2387 | CloseHandle(csinfo[i].hProc); |
| 2388 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2389 | #endif |
| 2390 | |
| 2391 | if (csinfo[i].fr_fp != NULL) |
| 2392 | (void)fclose(csinfo[i].fr_fp); |
| 2393 | if (csinfo[i].to_fp != NULL) |
| 2394 | (void)fclose(csinfo[i].to_fp); |
| 2395 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | if (freefnpp) |
| 2397 | { |
| 2398 | vim_free(csinfo[i].fname); |
| 2399 | vim_free(csinfo[i].ppath); |
| 2400 | vim_free(csinfo[i].flags); |
| 2401 | } |
| 2402 | |
| 2403 | clear_csinfo(i); |
| 2404 | } /* cs_release_csp */ |
| 2405 | |
| 2406 | |
| 2407 | /* |
| 2408 | * PRIVATE: cs_reset |
| 2409 | * |
| 2410 | * calls cs_kill on all cscope connections then reinits |
| 2411 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2412 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2413 | cs_reset(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | { |
| 2415 | char **dblist = NULL, **pplist = NULL, **fllist = NULL; |
| 2416 | int i; |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 2417 | char buf[20]; /* for sprintf " (#%d)" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2419 | if (csinfo_size == 0) |
| 2420 | return CSCOPE_SUCCESS; |
| 2421 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2422 | /* malloc our db and ppath list */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2423 | dblist = (char **)alloc(csinfo_size * sizeof(char *)); |
| 2424 | pplist = (char **)alloc(csinfo_size * sizeof(char *)); |
| 2425 | fllist = (char **)alloc(csinfo_size * sizeof(char *)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | if (dblist == NULL || pplist == NULL || fllist == NULL) |
| 2427 | { |
| 2428 | vim_free(dblist); |
| 2429 | vim_free(pplist); |
| 2430 | vim_free(fllist); |
| 2431 | return CSCOPE_FAILURE; |
| 2432 | } |
| 2433 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2434 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2435 | { |
| 2436 | dblist[i] = csinfo[i].fname; |
| 2437 | pplist[i] = csinfo[i].ppath; |
| 2438 | fllist[i] = csinfo[i].flags; |
| 2439 | if (csinfo[i].fname != NULL) |
| 2440 | cs_release_csp(i, FALSE); |
| 2441 | } |
| 2442 | |
| 2443 | /* rebuild the cscope connection list */ |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2444 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | { |
| 2446 | if (dblist[i] != NULL) |
| 2447 | { |
| 2448 | cs_add_common(dblist[i], pplist[i], fllist[i]); |
| 2449 | if (p_csverbose) |
| 2450 | { |
Bram Moolenaar | d2ac984 | 2007-08-21 16:03:51 +0000 | [diff] [blame] | 2451 | /* don't use smsg_attr() because we want to display the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2452 | * connection number in the same line as |
| 2453 | * "Added cscope database..." |
| 2454 | */ |
| 2455 | sprintf(buf, " (#%d)", i); |
| 2456 | MSG_PUTS_ATTR(buf, hl_attr(HLF_R)); |
| 2457 | } |
| 2458 | } |
| 2459 | vim_free(dblist[i]); |
| 2460 | vim_free(pplist[i]); |
| 2461 | vim_free(fllist[i]); |
| 2462 | } |
| 2463 | vim_free(dblist); |
| 2464 | vim_free(pplist); |
| 2465 | vim_free(fllist); |
| 2466 | |
| 2467 | if (p_csverbose) |
| 2468 | MSG_ATTR(_("All cscope databases reset"), hl_attr(HLF_R) | MSG_HIST); |
| 2469 | return CSCOPE_SUCCESS; |
| 2470 | } /* cs_reset */ |
| 2471 | |
| 2472 | |
| 2473 | /* |
| 2474 | * PRIVATE: cs_resolve_file |
| 2475 | * |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2476 | * Construct the full pathname to a file found in the cscope database. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | * (Prepends ppath, if there is one and if it's not already prepended, |
| 2478 | * otherwise just uses the name found.) |
| 2479 | * |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2480 | * We need to prepend the prefix because on some cscope's (e.g., the one that |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2481 | * ships with Solaris 2.6), the output never has the prefix prepended. |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2482 | * Contrast this with my development system (Digital Unix), which does. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2483 | */ |
| 2484 | static char * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2485 | cs_resolve_file(int i, char *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | { |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2487 | char *fullname; |
| 2488 | int len; |
| 2489 | char_u *csdir = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2492 | * Ppath is freed when we destroy the cscope connection. |
| 2493 | * Fullname is freed after cs_make_vim_style_matches, after it's been |
| 2494 | * copied into the tag buffer used by Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2495 | */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2496 | len = (int)(strlen(name) + 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | if (csinfo[i].ppath != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2498 | len += (int)strlen(csinfo[i].ppath); |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2499 | else if (p_csre && csinfo[i].fname != NULL) |
| 2500 | { |
| 2501 | /* If 'cscoperelative' is set and ppath is not set, use cscope.out |
| 2502 | * path in path resolution. */ |
| 2503 | csdir = alloc(MAXPATHL); |
| 2504 | if (csdir != NULL) |
| 2505 | { |
| 2506 | vim_strncpy(csdir, (char_u *)csinfo[i].fname, |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2507 | gettail((char_u *)csinfo[i].fname) |
| 2508 | - (char_u *)csinfo[i].fname); |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2509 | len += (int)STRLEN(csdir); |
| 2510 | } |
| 2511 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2512 | |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2513 | /* Note/example: this won't work if the cscope output already starts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2514 | * "../.." and the prefix path is also "../..". if something like this |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2515 | * happens, you are screwed up and need to fix how you're using cscope. */ |
| 2516 | if (csinfo[i].ppath != NULL |
| 2517 | && (strncmp(name, csinfo[i].ppath, strlen(csinfo[i].ppath)) != 0) |
| 2518 | && (name[0] != '/') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2519 | #ifdef WIN32 |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2520 | && name[0] != '\\' && name[1] != ':' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | #endif |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2522 | ) |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2523 | { |
| 2524 | if ((fullname = (char *)alloc(len)) != NULL) |
| 2525 | (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name); |
| 2526 | } |
| 2527 | else if (csdir != NULL && csinfo[i].fname != NULL && *csdir != NUL) |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2528 | { |
| 2529 | /* Check for csdir to be non empty to avoid empty path concatenated to |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2530 | * cscope output. */ |
Bram Moolenaar | 03227ee | 2011-06-12 21:25:00 +0200 | [diff] [blame] | 2531 | fullname = (char *)concat_fnames(csdir, (char_u *)name, TRUE); |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2532 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2533 | else |
Bram Moolenaar | 28c2191 | 2013-05-29 19:18:00 +0200 | [diff] [blame] | 2534 | { |
| 2535 | fullname = (char *)vim_strsave((char_u *)name); |
| 2536 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2537 | |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2538 | vim_free(csdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | return fullname; |
Bram Moolenaar | 2f982e4 | 2011-06-12 20:42:22 +0200 | [diff] [blame] | 2540 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | |
| 2542 | |
| 2543 | /* |
| 2544 | * PRIVATE: cs_show |
| 2545 | * |
| 2546 | * show all cscope connections |
| 2547 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2549 | cs_show(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2550 | { |
| 2551 | short i; |
| 2552 | if (cs_cnt_connections() == 0) |
| 2553 | MSG_PUTS(_("no cscope connections\n")); |
| 2554 | else |
| 2555 | { |
| 2556 | MSG_PUTS_ATTR( |
| 2557 | _(" # pid database name prepend path\n"), |
| 2558 | hl_attr(HLF_T)); |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2559 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2560 | { |
| 2561 | if (csinfo[i].fname == NULL) |
| 2562 | continue; |
| 2563 | |
| 2564 | if (csinfo[i].ppath != NULL) |
| 2565 | (void)smsg((char_u *)"%2d %-5ld %-34s %-32s", |
| 2566 | i, (long)csinfo[i].pid, csinfo[i].fname, csinfo[i].ppath); |
| 2567 | else |
| 2568 | (void)smsg((char_u *)"%2d %-5ld %-34s <none>", |
| 2569 | i, (long)csinfo[i].pid, csinfo[i].fname); |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | wait_return(TRUE); |
| 2574 | return CSCOPE_SUCCESS; |
| 2575 | } /* cs_show */ |
| 2576 | |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2577 | |
| 2578 | /* |
| 2579 | * PUBLIC: cs_end |
| 2580 | * |
| 2581 | * Only called when VIM exits to quit any cscope sessions. |
| 2582 | */ |
| 2583 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 2584 | cs_end(void) |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2585 | { |
| 2586 | int i; |
| 2587 | |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2588 | for (i = 0; i < csinfo_size; i++) |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2589 | cs_release_csp(i, TRUE); |
Bram Moolenaar | 9fa49da | 2009-07-10 13:11:26 +0000 | [diff] [blame] | 2590 | vim_free(csinfo); |
| 2591 | csinfo_size = 0; |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | #endif /* FEAT_CSCOPE */ |
| 2595 | |
| 2596 | /* the end */ |