1 /*- 2 * Copyright (c) 1994, 1996 3 * Rob Mayoff. All rights reserved. 4 * Copyright (c) 1996 5 * Keith Bostic. All rights reserved. 6 * 7 * See the LICENSE file for redistribution information. 8 */ 9 10 #include "config.h" 11 12 #include <sys/types.h> 13 #include <sys/queue.h> 14 #include <sys/stat.h> 15 #include <sys/wait.h> 16 17 #include <bitstring.h> 18 #include <ctype.h> 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <limits.h> 22 #include <signal.h> 23 #include <stddef.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <termios.h> 28 #include <unistd.h> 29 30 #include "../common/common.h" 31 #include "pathnames.h" 32 #include "tag.h" 33 34 #define CSCOPE_DBFILE "cscope.out" 35 #define CSCOPE_PATHS "cscope.tpath" 36 37 /* 38 * 0name find all uses of name 39 * 1name find definition of name 40 * 2name find all function calls made from name 41 * 3name find callers of name 42 * 4string find text string (cscope 12.9) 43 * 4name find assignments to name (cscope 13.3) 44 * 5pattern change pattern -- NOT USED 45 * 6pattern find pattern 46 * 7name find files with name as substring 47 * 8name find files #including name 48 */ 49 #define FINDHELP "\ 50 find c|d|e|f|g|i|s|t buffer|pattern\n\ 51 c: find callers of name\n\ 52 d: find all function calls made from name\n\ 53 e: find pattern\n\ 54 f: find files with name as substring\n\ 55 g: find definition of name\n\ 56 i: find files #including name\n\ 57 s: find all uses of name\n\ 58 t: find assignments to name" 59 60 static int cscope_add(SCR *, EXCMD *, CHAR_T *); 61 static int cscope_find(SCR *, EXCMD*, CHAR_T *); 62 static int cscope_help(SCR *, EXCMD *, CHAR_T *); 63 static int cscope_kill(SCR *, EXCMD *, CHAR_T *); 64 static int cscope_reset(SCR *, EXCMD *, CHAR_T *); 65 66 typedef struct _cc { 67 char *name; 68 int (*function)(SCR *, EXCMD *, CHAR_T *); 69 char *help_msg; 70 char *usage_msg; 71 } CC; 72 73 static CC const cscope_cmds[] = { 74 { "add", cscope_add, 75 "Add a new cscope database", "add file | directory" }, 76 { "find", cscope_find, 77 "Query the databases for a pattern", FINDHELP }, 78 { "help", cscope_help, 79 "Show help for cscope commands", "help [command]" }, 80 { "kill", cscope_kill, 81 "Kill a cscope connection", "kill number" }, 82 { "reset", cscope_reset, 83 "Discard all current cscope connections", "reset" }, 84 { NULL } 85 }; 86 87 static TAGQ *create_cs_cmd(SCR *, char *, size_t *); 88 static int csc_help(SCR *, char *); 89 static void csc_file(SCR *, 90 CSC *, char *, char **, size_t *, int *); 91 static int get_paths(SCR *, CSC *); 92 static CC const *lookup_ccmd(char *); 93 static int parse(SCR *, CSC *, TAGQ *, int *); 94 static int read_prompt(SCR *, CSC *); 95 static int run_cscope(SCR *, CSC *, char *); 96 static int start_cscopes(SCR *, EXCMD *); 97 static int terminate(SCR *, CSC *, int); 98 99 /* 100 * ex_cscope -- 101 * Perform an ex cscope. 102 * 103 * PUBLIC: int ex_cscope(SCR *, EXCMD *); 104 */ 105 int 106 ex_cscope(SCR *sp, EXCMD *cmdp) 107 { 108 CC const *ccp; 109 EX_PRIVATE *exp; 110 int i; 111 CHAR_T *cmd; 112 CHAR_T *p; 113 char *np; 114 size_t nlen; 115 116 /* Initialize the default cscope directories. */ 117 exp = EXP(sp); 118 if (!F_ISSET(exp, EXP_CSCINIT) && start_cscopes(sp, cmdp)) 119 return (1); 120 F_SET(exp, EXP_CSCINIT); 121 122 /* Skip leading whitespace. */ 123 for (p = cmdp->argv[0]->bp, i = cmdp->argv[0]->len; i > 0; --i, ++p) 124 if (!isspace(*p)) 125 break; 126 if (i == 0) 127 goto usage; 128 129 /* Skip the command to any arguments. */ 130 for (cmd = p; i > 0; --i, ++p) 131 if (isspace(*p)) 132 break; 133 if (*p != '\0') { 134 *p++ = '\0'; 135 for (; *p && isspace(*p); ++p); 136 } 137 138 INT2CHAR(sp, cmd, STRLEN(cmd) + 1, np, nlen); 139 if ((ccp = lookup_ccmd(np)) == NULL) { 140 usage: msgq(sp, M_ERR, "309|Use \"cscope help\" for help"); 141 return (1); 142 } 143 144 /* Call the underlying function. */ 145 return (ccp->function(sp, cmdp, p)); 146 } 147 148 /* 149 * start_cscopes -- 150 * Initialize the cscope package. 151 */ 152 static int 153 start_cscopes(SCR *sp, EXCMD *cmdp) 154 { 155 size_t blen, len; 156 char *bp, *cscopes, *p, *t; 157 CHAR_T *wp; 158 size_t wlen; 159 160 /* 161 * EXTENSION #1: 162 * 163 * If the CSCOPE_DIRS environment variable is set, we treat it as a 164 * list of cscope directories that we're using, similar to the tags 165 * edit option. 166 * 167 * XXX 168 * This should probably be an edit option, although that implies that 169 * we start/stop cscope processes periodically, instead of once when 170 * the editor starts. 171 */ 172 if ((cscopes = getenv("CSCOPE_DIRS")) == NULL) 173 return (0); 174 len = strlen(cscopes); 175 GET_SPACE_RETC(sp, bp, blen, len); 176 memcpy(bp, cscopes, len + 1); 177 178 for (cscopes = t = bp; (p = strsep(&t, "\t :")) != NULL;) 179 if (*p != '\0') { 180 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen); 181 (void)cscope_add(sp, cmdp, wp); 182 } 183 184 FREE_SPACE(sp, bp, blen); 185 return (0); 186 } 187 188 /* 189 * cscope_add -- 190 * The cscope add command. 191 */ 192 static int 193 cscope_add(SCR *sp, EXCMD *cmdp, CHAR_T *dname) 194 { 195 struct stat sb; 196 EX_PRIVATE *exp; 197 CSC *csc; 198 size_t len; 199 int cur_argc; 200 char *dbname, *path; 201 char *np = NULL; 202 size_t nlen; 203 204 exp = EXP(sp); 205 206 /* 207 * 0 additional args: usage. 208 * 1 additional args: matched a file. 209 * >1 additional args: object, too many args. 210 */ 211 cur_argc = cmdp->argc; 212 if (argv_exp2(sp, cmdp, dname, STRLEN(dname))) { 213 return (1); 214 } 215 if (cmdp->argc == cur_argc) { 216 (void)csc_help(sp, "add"); 217 return (1); 218 } 219 if (cmdp->argc == cur_argc + 1) 220 dname = cmdp->argv[cur_argc]->bp; 221 else { 222 ex_emsg(sp, np, EXM_FILECOUNT); 223 return (1); 224 } 225 226 INT2CHAR(sp, dname, STRLEN(dname)+1, np, nlen); 227 228 /* 229 * The user can specify a specific file (so they can have multiple 230 * Cscope databases in a single directory) or a directory. If the 231 * file doesn't exist, we're done. If it's a directory, append the 232 * standard database file name and try again. Store the directory 233 * name regardless so that we can use it as a base for searches. 234 */ 235 if (stat(np, &sb)) { 236 msgq(sp, M_SYSERR, "%s", np); 237 return (1); 238 } 239 if (S_ISDIR(sb.st_mode)) { 240 if ((path = join(np, CSCOPE_DBFILE)) == NULL) { 241 msgq(sp, M_SYSERR, NULL); 242 return (1); 243 } 244 if (stat(path, &sb)) { 245 msgq(sp, M_SYSERR, "%s", path); 246 free(path); 247 return (1); 248 } 249 free(path); 250 dbname = CSCOPE_DBFILE; 251 } else if ((dbname = strrchr(np, '/')) != NULL) 252 *dbname++ = '\0'; 253 else { 254 dbname = np; 255 np = "."; 256 } 257 258 /* Allocate a cscope connection structure and initialize its fields. */ 259 len = strlen(np); 260 CALLOC_RET(sp, csc, 1, sizeof(CSC) + len); 261 csc->dname = csc->buf; 262 csc->dlen = len; 263 memcpy(csc->dname, np, len); 264 csc->mtim = sb.st_mtim; 265 266 /* Get the search paths for the cscope. */ 267 if (get_paths(sp, csc)) 268 goto err; 269 270 /* Start the cscope process. */ 271 if (run_cscope(sp, csc, dbname)) 272 goto err; 273 274 /* 275 * Add the cscope connection to the screen's list. From now on, 276 * on error, we have to call terminate, which expects the csc to 277 * be on the chain. 278 */ 279 SLIST_INSERT_HEAD(exp->cscq, csc, q); 280 281 /* Read the initial prompt from the cscope to make sure it's okay. */ 282 return read_prompt(sp, csc); 283 284 err: free(csc); 285 return (1); 286 } 287 288 /* 289 * get_paths -- 290 * Get the directories to search for the files associated with this 291 * cscope database. 292 */ 293 static int 294 get_paths(SCR *sp, CSC *csc) 295 { 296 struct stat sb; 297 int fd, nentries; 298 size_t len; 299 char *p, **pathp, *buf; 300 301 /* 302 * EXTENSION #2: 303 * 304 * If there's a cscope directory with a file named CSCOPE_PATHS, it 305 * contains a colon-separated list of paths in which to search for 306 * files returned by cscope. 307 * 308 * XXX 309 * These paths are absolute paths, and not relative to the cscope 310 * directory. To fix this, rewrite the each path using the cscope 311 * directory as a prefix. 312 */ 313 if ((buf = join(csc->dname, CSCOPE_PATHS)) == NULL) { 314 msgq(sp, M_SYSERR, NULL); 315 return (1); 316 } 317 if (stat(buf, &sb) == 0) { 318 /* Read in the CSCOPE_PATHS file. */ 319 len = sb.st_size; 320 MALLOC_RET(sp, csc->pbuf, len + 1); 321 if ((fd = open(buf, O_RDONLY, 0)) < 0 || 322 read(fd, csc->pbuf, len) != len) { 323 msgq_str(sp, M_SYSERR, buf, "%s"); 324 if (fd >= 0) 325 (void)close(fd); 326 free(buf); 327 return (1); 328 } 329 (void)close(fd); 330 free(buf); 331 csc->pbuf[len] = '\0'; 332 333 /* Count up the entries. */ 334 for (nentries = 0, p = csc->pbuf; *p != '\0'; ++p) 335 if (p[0] == ':' && p[1] != '\0') 336 ++nentries; 337 338 /* Build an array of pointers to the paths. */ 339 CALLOC_GOTO(sp, csc->paths, nentries + 1, sizeof(char **)); 340 for (pathp = csc->paths, p = strtok(csc->pbuf, ":"); 341 p != NULL; p = strtok(NULL, ":")) 342 *pathp++ = p; 343 return (0); 344 } 345 free(buf); 346 347 /* 348 * If the CSCOPE_PATHS file doesn't exist, we look for files 349 * relative to the cscope directory. 350 */ 351 if ((csc->pbuf = strdup(csc->dname)) == NULL) { 352 msgq(sp, M_SYSERR, NULL); 353 return (1); 354 } 355 CALLOC_GOTO(sp, csc->paths, 2, sizeof(char *)); 356 csc->paths[0] = csc->pbuf; 357 return (0); 358 359 alloc_err: 360 free(csc->pbuf); 361 csc->pbuf = NULL; 362 return (1); 363 } 364 365 /* 366 * run_cscope -- 367 * Fork off the cscope process. 368 */ 369 static int 370 run_cscope(SCR *sp, CSC *csc, char *dbname) 371 { 372 int to_cs[2], from_cs[2]; 373 char *cmd; 374 375 /* 376 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from 377 * from_cs[0] and writes to to_cs[1]. 378 */ 379 to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1; 380 if (pipe(to_cs) < 0 || pipe(from_cs) < 0) { 381 msgq(sp, M_SYSERR, "pipe"); 382 goto err; 383 } 384 switch (csc->pid = vfork()) { 385 char *dn, *dbn; 386 case -1: 387 msgq(sp, M_SYSERR, "vfork"); 388 err: if (to_cs[0] != -1) 389 (void)close(to_cs[0]); 390 if (to_cs[1] != -1) 391 (void)close(to_cs[1]); 392 if (from_cs[0] != -1) 393 (void)close(from_cs[0]); 394 if (from_cs[1] != -1) 395 (void)close(from_cs[1]); 396 return (1); 397 case 0: /* child: run cscope. */ 398 (void)dup2(to_cs[0], STDIN_FILENO); 399 (void)dup2(from_cs[1], STDOUT_FILENO); 400 (void)dup2(from_cs[1], STDERR_FILENO); 401 402 /* Close unused file descriptors. */ 403 (void)close(to_cs[1]); 404 (void)close(from_cs[0]); 405 406 /* Run the cscope command. */ 407 #define CSCOPE_CMD_FMT "cd %s && exec cscope -dl -f %s" 408 if ((dn = quote(csc->dname)) == NULL) 409 goto nomem; 410 if ((dbn = quote(dbname)) == NULL) { 411 free(dn); 412 goto nomem; 413 } 414 if (asprintf(&cmd, CSCOPE_CMD_FMT, dn, dbn) == -1) 415 cmd = NULL; 416 free(dbn); 417 free(dn); 418 if (cmd == NULL) { 419 nomem: msgq(sp, M_SYSERR, NULL); 420 _exit (1); 421 } 422 (void)execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL); 423 msgq_str(sp, M_SYSERR, cmd, "execl: %s"); 424 free(cmd); 425 _exit (127); 426 /* NOTREACHED */ 427 default: /* parent. */ 428 /* Close unused file descriptors. */ 429 (void)close(to_cs[0]); 430 (void)close(from_cs[1]); 431 432 /* 433 * Save the file descriptors for later duplication, and 434 * reopen as streams. 435 */ 436 csc->to_fd = to_cs[1]; 437 csc->to_fp = fdopen(to_cs[1], "w"); 438 csc->from_fd = from_cs[0]; 439 csc->from_fp = fdopen(from_cs[0], "r"); 440 break; 441 } 442 return (0); 443 } 444 445 /* 446 * cscope_find -- 447 * The cscope find command. 448 */ 449 static int 450 cscope_find(SCR *sp, EXCMD *cmdp, CHAR_T *pattern) 451 { 452 CSC *csc, *csc_next; 453 EX_PRIVATE *exp; 454 FREF *frp; 455 TAGQ *rtqp, *tqp; 456 TAG *rtp; 457 recno_t lno; 458 size_t cno, search; 459 int force, istmp, matches; 460 char *np = NULL; 461 size_t nlen; 462 463 exp = EXP(sp); 464 465 /* Check for connections. */ 466 if (SLIST_EMPTY(exp->cscq)) { 467 msgq(sp, M_ERR, "310|No cscope connections running"); 468 return (1); 469 } 470 471 /* 472 * Allocate all necessary memory before doing anything hard. If the 473 * tags stack is empty, we'll need the `local context' TAGQ structure 474 * later. 475 */ 476 rtp = NULL; 477 rtqp = NULL; 478 if (TAILQ_EMPTY(exp->tq)) { 479 /* Initialize the `local context' tag queue structure. */ 480 CALLOC_GOTO(sp, rtqp, 1, sizeof(TAGQ)); 481 TAILQ_INIT(rtqp->tagq); 482 483 /* Initialize and link in its tag structure. */ 484 CALLOC_GOTO(sp, rtp, 1, sizeof(TAG)); 485 TAILQ_INSERT_HEAD(rtqp->tagq, rtp, q); 486 rtqp->current = rtp; 487 } 488 489 /* Create the cscope command. */ 490 INT2CHAR(sp, pattern, STRLEN(pattern) + 1, np, nlen); 491 np = strdup(np); 492 if ((tqp = create_cs_cmd(sp, np, &search)) == NULL) 493 goto err; 494 free(np); 495 np = NULL; 496 497 /* 498 * Stick the current context in a convenient place, we'll lose it 499 * when we switch files. 500 */ 501 frp = sp->frp; 502 lno = sp->lno; 503 cno = sp->cno; 504 istmp = F_ISSET(sp->frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN); 505 506 /* Search all open connections for a match. */ 507 matches = 0; 508 /* Copy next connect here in case csc is killed. */ 509 SLIST_FOREACH_MUTABLE(csc, exp->cscq, q, csc_next) { 510 /* 511 * Send the command to the cscope program. (We skip the 512 * first two bytes of the command, because we stored the 513 * search cscope command character and a leading space 514 * there.) 515 */ 516 (void)fprintf(csc->to_fp, "%lu%s\n", search, tqp->tag + 2); 517 (void)fflush(csc->to_fp); 518 519 /* Read the output. */ 520 if (parse(sp, csc, tqp, &matches)) 521 goto nomatch; 522 } 523 524 if (matches == 0) { 525 msgq(sp, M_INFO, "278|No matches for query"); 526 nomatch: free(rtp); 527 free(rtqp); 528 tagq_free(sp, tqp); 529 return (1); 530 } 531 532 /* Try to switch to the first tag. */ 533 force = FL_ISSET(cmdp->iflags, E_C_FORCE); 534 if (F_ISSET(cmdp, E_NEWSCREEN)) { 535 if (ex_tag_Nswitch(sp, tqp->current, force)) 536 goto err; 537 538 /* Everything else gets done in the new screen. */ 539 sp = sp->nextdisp; 540 exp = EXP(sp); 541 } else 542 if (ex_tag_nswitch(sp, tqp->current, force)) 543 goto err; 544 545 /* 546 * If this is the first tag, put a `current location' queue entry 547 * in place, so we can pop all the way back to the current mark. 548 * Note, it doesn't point to much of anything, it's a placeholder. 549 */ 550 if (TAILQ_EMPTY(exp->tq)) { 551 TAILQ_INSERT_HEAD(exp->tq, rtqp, q); 552 } else 553 rtqp = TAILQ_FIRST(exp->tq); 554 555 /* Link the current TAGQ structure into place. */ 556 TAILQ_INSERT_HEAD(exp->tq, tqp, q); 557 558 (void)cscope_search(sp, tqp, tqp->current); 559 560 /* 561 * Move the current context from the temporary save area into the 562 * right structure. 563 * 564 * If we were in a temporary file, we don't have a context to which 565 * we can return, so just make it be the same as what we're moving 566 * to. It will be a little odd that ^T doesn't change anything, but 567 * I don't think it's a big deal. 568 */ 569 if (istmp) { 570 rtqp->current->frp = sp->frp; 571 rtqp->current->lno = sp->lno; 572 rtqp->current->cno = sp->cno; 573 } else { 574 rtqp->current->frp = frp; 575 rtqp->current->lno = lno; 576 rtqp->current->cno = cno; 577 } 578 579 return (0); 580 581 err: 582 alloc_err: 583 free(rtqp); 584 free(rtp); 585 free(np); 586 return (1); 587 } 588 589 /* 590 * create_cs_cmd -- 591 * Build a cscope command, creating and initializing the base TAGQ. 592 */ 593 static TAGQ * 594 create_cs_cmd(SCR *sp, char *pattern, size_t *searchp) 595 { 596 CB *cbp; 597 TAGQ *tqp; 598 size_t tlen; 599 char *p; 600 601 /* 602 * Cscope supports a "change pattern" command which we never use, 603 * cscope command 5. Set CSCOPE_QUERIES[5] to " " since the user 604 * can't pass " " as the first character of pattern. That way the 605 * user can't ask for pattern 5 so we don't need any special-case 606 * code. 607 */ 608 #define CSCOPE_QUERIES "sgdct efi" 609 610 if (pattern == NULL) 611 goto usage; 612 613 /* Skip leading blanks, check for command character. */ 614 for (; cmdskip(pattern[0]); ++pattern); 615 if (pattern[0] == '\0' || !cmdskip(pattern[1])) 616 goto usage; 617 for (*searchp = 0, p = CSCOPE_QUERIES; 618 *p != '\0' && *p != pattern[0]; ++*searchp, ++p); 619 if (*p == '\0') { 620 msgq(sp, M_ERR, 621 "311|%s: unknown search type: use one of %s", 622 KEY_NAME(sp, pattern[0]), CSCOPE_QUERIES); 623 return (NULL); 624 } 625 626 /* Skip <blank> characters to the pattern. */ 627 for (p = pattern + 1; *p != '\0' && cmdskip(*p); ++p); 628 if (*p == '\0') { 629 usage: (void)csc_help(sp, "find"); 630 return (NULL); 631 } 632 633 /* The user can specify the contents of a buffer as the pattern. */ 634 cbp = NULL; 635 if (p[0] == '"' && p[1] != '\0' && p[2] == '\0') 636 CBNAME(sp, cbp, p[1]); 637 if (cbp != NULL) { 638 INT2CHAR(sp, TAILQ_FIRST(cbp->textq)->lb, 639 TAILQ_FIRST(cbp->textq)->len, p, tlen); 640 } else 641 tlen = strlen(p); 642 643 /* Allocate and initialize the TAGQ structure. */ 644 CALLOC(sp, tqp, 1, sizeof(TAGQ) + tlen + 3); 645 if (tqp == NULL) 646 return (NULL); 647 TAILQ_INIT(tqp->tagq); 648 tqp->tag = tqp->buf; 649 tqp->tag[0] = pattern[0]; 650 tqp->tag[1] = ' '; 651 tqp->tlen = tlen + 2; 652 memcpy(tqp->tag + 2, p, tlen); 653 tqp->tag[tlen + 2] = '\0'; 654 F_SET(tqp, TAG_CSCOPE); 655 656 return (tqp); 657 } 658 659 /* 660 * parse -- 661 * Parse the cscope output. 662 */ 663 static int 664 parse(SCR *sp, CSC *csc, TAGQ *tqp, int *matchesp) 665 { 666 TAG *tp; 667 recno_t slno = 0; 668 size_t dlen, nlen = 0, slen = 0; 669 int ch, i, isolder = 0, nlines; 670 char *dname = NULL, *name = NULL, *search, *p, *t, dummy[2], buf[2048]; 671 CHAR_T *wp; 672 size_t wlen; 673 674 for (;;) { 675 if (!fgets(buf, sizeof(buf), csc->from_fp)) 676 goto io_err; 677 678 /* 679 * If the database is out of date, or there's some other 680 * problem, cscope will output error messages before the 681 * number-of-lines output. Display/discard any output 682 * that doesn't match what we want. 683 */ 684 #define CSCOPE_NLINES_FMT "cscope: %d lines%1[\n]" 685 if (sscanf(buf, CSCOPE_NLINES_FMT, &nlines, dummy) == 2) 686 break; 687 if ((p = strchr(buf, '\n')) != NULL) 688 *p = '\0'; 689 msgq(sp, M_ERR, "%s: \"%s\"", csc->dname, buf); 690 } 691 692 while (nlines--) { 693 if (fgets(buf, sizeof(buf), csc->from_fp) == NULL) 694 goto io_err; 695 696 /* If the line's too long for the buffer, discard it. */ 697 if ((p = strchr(buf, '\n')) == NULL) { 698 while ((ch = getc(csc->from_fp)) != EOF && ch != '\n'); 699 continue; 700 } 701 *p = '\0'; 702 703 /* 704 * The cscope output is in the following format: 705 * 706 * <filename> <context> <line number> <pattern> 707 * 708 * Figure out how long everything is so we can allocate in one 709 * swell foop, but discard anything that looks wrong. 710 */ 711 for (p = buf, i = 0; 712 i < 3 && (t = strsep(&p, "\t ")) != NULL; ++i) 713 switch (i) { 714 case 0: /* Filename. */ 715 name = t; 716 nlen = strlen(name); 717 break; 718 case 1: /* Context. */ 719 break; 720 case 2: /* Line number. */ 721 slno = (recno_t)atol(t); 722 break; 723 } 724 if (i != 3 || p == NULL || t == NULL) 725 continue; 726 727 /* The rest of the string is the search pattern. */ 728 search = p; 729 slen = strlen(p); 730 731 /* Resolve the file name. */ 732 csc_file(sp, csc, name, &dname, &dlen, &isolder); 733 734 /* 735 * If the file is older than the cscope database, that is, 736 * the database was built since the file was last modified, 737 * or there wasn't a search string, use the line number. 738 */ 739 if (isolder || strcmp(search, "<unknown>") == 0) { 740 search = NULL; 741 slen = 0; 742 } 743 744 /* 745 * Allocate and initialize a tag structure plus the variable 746 * length cscope information that follows it. 747 */ 748 CALLOC_RET(sp, tp, 1, 749 sizeof(TAG) + dlen + 2 + nlen + 1 + (slen + 1) * sizeof(CHAR_T)); 750 tp->fname = (char *)tp->buf; 751 if (dlen == 1 && *dname == '.') 752 --dlen; 753 else if (dlen != 0) { 754 memcpy(tp->fname, dname, dlen); 755 tp->fname[dlen] = '/'; 756 ++dlen; 757 } 758 memcpy(tp->fname + dlen, name, nlen + 1); 759 tp->fnlen = dlen + nlen; 760 tp->slno = slno; 761 if (slen != 0) { 762 tp->search = (CHAR_T*)(tp->fname + tp->fnlen + 1); 763 CHAR2INT(sp, search, slen + 1, wp, wlen); 764 MEMCPY(tp->search, wp, (tp->slen = slen) + 1); 765 } 766 TAILQ_INSERT_TAIL(tqp->tagq, tp, q); 767 768 /* Try to preset the tag within the current file. */ 769 if (sp->frp != NULL && sp->frp->name != NULL && 770 tqp->current == NULL && !strcmp(tp->fname, sp->frp->name)) 771 tqp->current = tp; 772 773 ++*matchesp; 774 } 775 776 if (tqp->current == NULL) 777 tqp->current = TAILQ_FIRST(tqp->tagq); 778 779 return read_prompt(sp, csc); 780 781 io_err: if (feof(csc->from_fp)) 782 errno = EIO; 783 msgq_str(sp, M_SYSERR, "%s", csc->dname); 784 terminate(sp, csc, 0); 785 return (1); 786 } 787 788 /* 789 * csc_file -- 790 * Search for the right path to this file. 791 */ 792 static void 793 csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolderp) 794 { 795 struct stat sb; 796 char **pp, *buf; 797 798 /* 799 * Check for the file in all of the listed paths. If we don't 800 * find it, we simply return it unchanged. We have to do this 801 * now, even though it's expensive, because if the user changes 802 * directories, we can't change our minds as to where the file 803 * lives. 804 */ 805 for (pp = csc->paths; *pp != NULL; ++pp) { 806 if ((buf = join(*pp, name)) == NULL) { 807 msgq(sp, M_SYSERR, NULL); 808 *dlenp = 0; 809 return; 810 } 811 if (stat(buf, &sb) == 0) { 812 free(buf); 813 *dirp = *pp; 814 *dlenp = strlen(*pp); 815 *isolderp = timespeccmp( 816 &sb.st_mtim, &csc->mtim, <); 817 return; 818 } 819 free(buf); 820 } 821 *dlenp = 0; 822 } 823 824 /* 825 * cscope_help -- 826 * The cscope help command. 827 */ 828 static int 829 cscope_help(SCR *sp, EXCMD *cmdp, CHAR_T *subcmd) 830 { 831 char *np; 832 size_t nlen; 833 834 INT2CHAR(sp, subcmd, STRLEN(subcmd) + 1, np, nlen); 835 return (csc_help(sp, np)); 836 } 837 838 /* 839 * csc_help -- 840 * Display help/usage messages. 841 */ 842 static int 843 csc_help(SCR *sp, char *cmd) 844 { 845 CC const *ccp; 846 847 if (cmd != NULL && *cmd != '\0') 848 if ((ccp = lookup_ccmd(cmd)) == NULL) { 849 ex_printf(sp, 850 "%s doesn't match any cscope command\n", cmd); 851 return (1); 852 } else { 853 ex_printf(sp, 854 "Command: %s (%s)\n", ccp->name, ccp->help_msg); 855 ex_printf(sp, " Usage: %s\n", ccp->usage_msg); 856 return (0); 857 } 858 859 ex_printf(sp, "cscope commands:\n"); 860 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp) 861 ex_printf(sp, " %*s: %s\n", 5, ccp->name, ccp->help_msg); 862 return (0); 863 } 864 865 /* 866 * cscope_kill -- 867 * The cscope kill command. 868 */ 869 static int 870 cscope_kill(SCR *sp, EXCMD *cmdp, CHAR_T *cn) 871 { 872 char *np; 873 size_t nlen; 874 int n = 1; 875 876 if (*cn) { 877 INT2CHAR(sp, cn, STRLEN(cn) + 1, np, nlen); 878 n = atoi(np); 879 } 880 return (terminate(sp, NULL, n)); 881 } 882 883 /* 884 * terminate -- 885 * Detach from a cscope process. 886 */ 887 static int 888 terminate(SCR *sp, CSC *csc, int n) 889 { 890 EX_PRIVATE *exp; 891 int i = 0, pstat; 892 CSC *cp, *pre_cp = NULL; 893 894 exp = EXP(sp); 895 896 /* 897 * We either get a csc structure or a number. Locate and remove 898 * the candidate which matches the structure or the number. 899 */ 900 if (csc == NULL && n < 1) 901 goto badno; 902 SLIST_FOREACH(cp, exp->cscq, q) { 903 ++i; 904 if (csc == NULL ? i != n : cp != csc) { 905 pre_cp = cp; 906 continue; 907 } 908 if (cp == SLIST_FIRST(exp->cscq)) 909 SLIST_REMOVE_HEAD(exp->cscq, q); 910 else 911 SLIST_REMOVE_AFTER(pre_cp, q); 912 csc = cp; 913 break; 914 } 915 if (csc == NULL) { 916 badno: msgq(sp, M_ERR, "312|%d: no such cscope session", n); 917 return (1); 918 } 919 920 /* 921 * XXX 922 * Theoretically, we have the only file descriptors to the process, 923 * so closing them should let it exit gracefully, deleting temporary 924 * files, etc. However, the earlier created cscope processes seems 925 * to refuse to quit unless we send a SIGTERM signal. 926 */ 927 if (csc->from_fp != NULL) 928 (void)fclose(csc->from_fp); 929 if (csc->to_fp != NULL) 930 (void)fclose(csc->to_fp); 931 if (i > 1) 932 (void)kill(csc->pid, SIGTERM); 933 (void)waitpid(csc->pid, &pstat, 0); 934 935 /* Discard cscope connection information. */ 936 free(csc->pbuf); 937 free(csc->paths); 938 free(csc); 939 return (0); 940 } 941 942 /* 943 * cscope_reset -- 944 * The cscope reset command. 945 */ 946 static int 947 cscope_reset(SCR *sp, EXCMD *cmdp, CHAR_T *notusedp) 948 { 949 return cscope_end(sp); 950 } 951 952 /* 953 * cscope_end -- 954 * End all cscope connections. 955 * 956 * PUBLIC: int cscope_end(SCR *); 957 */ 958 int 959 cscope_end(SCR *sp) 960 { 961 EX_PRIVATE *exp; 962 963 for (exp = EXP(sp); !SLIST_EMPTY(exp->cscq);) 964 if (terminate(sp, NULL, 1)) 965 return (1); 966 return (0); 967 } 968 969 /* 970 * cscope_display -- 971 * Display current connections. 972 * 973 * PUBLIC: int cscope_display(SCR *); 974 */ 975 int 976 cscope_display(SCR *sp) 977 { 978 EX_PRIVATE *exp; 979 CSC *csc; 980 int i = 0; 981 982 exp = EXP(sp); 983 if (SLIST_EMPTY(exp->cscq)) { 984 ex_printf(sp, "No cscope connections.\n"); 985 return (0); 986 } 987 SLIST_FOREACH(csc, exp->cscq, q) 988 ex_printf(sp, "%2d %s (process %lu)\n", 989 ++i, csc->dname, (u_long)csc->pid); 990 return (0); 991 } 992 993 /* 994 * cscope_search -- 995 * Search a file for a cscope entry. 996 * 997 * PUBLIC: int cscope_search(SCR *, TAGQ *, TAG *); 998 */ 999 int 1000 cscope_search(SCR *sp, TAGQ *tqp, TAG *tp) 1001 { 1002 MARK m; 1003 1004 /* If we don't have a search pattern, use the line number. */ 1005 if (tp->search == NULL) { 1006 if (!db_exist(sp, tp->slno)) { 1007 tag_msg(sp, TAG_BADLNO, tqp->tag); 1008 return (1); 1009 } 1010 m.lno = tp->slno; 1011 } else { 1012 /* 1013 * Search for the tag; cheap fallback for C functions 1014 * if the name is the same but the arguments have changed. 1015 */ 1016 m.lno = 1; 1017 m.cno = 0; 1018 if (f_search(sp, &m, &m, 1019 tp->search, tp->slen, NULL, SEARCH_CSCOPE | SEARCH_FILE)) { 1020 tag_msg(sp, TAG_SEARCH, tqp->tag); 1021 return (1); 1022 } 1023 1024 /* 1025 * !!! 1026 * Historically, tags set the search direction if it wasn't 1027 * already set. 1028 */ 1029 if (sp->searchdir == NOTSET) 1030 sp->searchdir = FORWARD; 1031 } 1032 1033 /* 1034 * !!! 1035 * Tags move to the first non-blank, NOT the search pattern start. 1036 */ 1037 sp->lno = m.lno; 1038 sp->cno = 0; 1039 (void)nonblank(sp, sp->lno, &sp->cno); 1040 return (0); 1041 } 1042 1043 1044 /* 1045 * lookup_ccmd -- 1046 * Return a pointer to the command structure. 1047 */ 1048 static CC const * 1049 lookup_ccmd(char *name) 1050 { 1051 CC const *ccp; 1052 size_t len; 1053 1054 len = strlen(name); 1055 for (ccp = cscope_cmds; ccp->name != NULL; ++ccp) 1056 if (strncmp(name, ccp->name, len) == 0) 1057 return (ccp); 1058 return (NULL); 1059 } 1060 1061 /* 1062 * read_prompt -- 1063 * Read a prompt from cscope. 1064 */ 1065 static int 1066 read_prompt(SCR *sp, CSC *csc) 1067 { 1068 int ch; 1069 1070 #define CSCOPE_PROMPT ">> " 1071 for (;;) { 1072 while ((ch = 1073 getc(csc->from_fp)) != EOF && ch != CSCOPE_PROMPT[0]); 1074 if (ch == EOF) { 1075 terminate(sp, csc, 0); 1076 return (1); 1077 } 1078 if (getc(csc->from_fp) != CSCOPE_PROMPT[1]) 1079 continue; 1080 if (getc(csc->from_fp) != CSCOPE_PROMPT[2]) 1081 continue; 1082 break; 1083 } 1084 return (0); 1085 } 1086