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