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