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