1 /* $NetBSD: interactive.c,v 1.11 1997/07/06 08:51:30 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1985, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)interactive.c 8.3 (Berkeley) 9/13/94"; 39 #else 40 static char rcsid[] = "$NetBSD: interactive.c,v 1.11 1997/07/06 08:51:30 lukem Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/stat.h> 47 48 #include <ufs/ffs/fs.h> 49 #include <ufs/ufs/dinode.h> 50 #include <ufs/ufs/dir.h> 51 #include <protocols/dumprestore.h> 52 53 #include <setjmp.h> 54 #include <glob.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 59 #include "restore.h" 60 #include "extern.h" 61 62 #define round(a, b) (((a) + (b) - 1) / (b) * (b)) 63 64 /* 65 * Things to handle interruptions. 66 */ 67 static int runshell; 68 static jmp_buf reset; 69 static char *nextarg = NULL; 70 71 /* 72 * Structure and routines associated with listing directories. 73 */ 74 struct afile { 75 ino_t fnum; /* inode number of file */ 76 char *fname; /* file name */ 77 short len; /* name length */ 78 char prefix; /* prefix character */ 79 char postfix; /* postfix character */ 80 }; 81 struct arglist { 82 int freeglob; /* glob structure needs to be freed */ 83 int argcnt; /* next globbed argument to return */ 84 glob_t glob; /* globbing information */ 85 char *cmd; /* the current command */ 86 }; 87 88 static char *copynext __P((char *, char *)); 89 static int fcmp __P((const void *, const void *)); 90 static void formatf __P((struct afile *, int)); 91 static void getcmd __P((char *, char *, char *, struct arglist *)); 92 struct dirent *glob_readdir __P((RST_DIR *dirp)); 93 static int glob_stat __P((const char *, struct stat *)); 94 static void mkentry __P((char *, struct direct *, struct afile *)); 95 static void printlist __P((char *, char *)); 96 97 /* 98 * Read and execute commands from the terminal. 99 */ 100 void 101 runcmdshell() 102 { 103 struct entry *np; 104 ino_t ino; 105 struct arglist arglist; 106 char curdir[MAXPATHLEN]; 107 char name[MAXPATHLEN]; 108 char cmd[BUFSIZ]; 109 110 arglist.freeglob = 0; 111 arglist.argcnt = 0; 112 arglist.glob.gl_flags = GLOB_ALTDIRFUNC; 113 arglist.glob.gl_opendir = (void *)rst_opendir; 114 arglist.glob.gl_readdir = (void *)glob_readdir; 115 arglist.glob.gl_closedir = (void *)rst_closedir; 116 arglist.glob.gl_lstat = glob_stat; 117 arglist.glob.gl_stat = glob_stat; 118 canon("/", curdir); 119 loop: 120 if (setjmp(reset) != 0) { 121 if (arglist.freeglob != 0) { 122 arglist.freeglob = 0; 123 arglist.argcnt = 0; 124 globfree(&arglist.glob); 125 } 126 nextarg = NULL; 127 volno = 0; 128 } 129 runshell = 1; 130 getcmd(curdir, cmd, name, &arglist); 131 switch (cmd[0]) { 132 /* 133 * Add elements to the extraction list. 134 */ 135 case 'a': 136 if (strncmp(cmd, "add", strlen(cmd)) != 0) 137 goto bad; 138 ino = dirlookup(name); 139 if (ino == 0) 140 break; 141 if (mflag) 142 pathcheck(name); 143 treescan(name, ino, addfile); 144 break; 145 /* 146 * Change working directory. 147 */ 148 case 'c': 149 if (strncmp(cmd, "cd", strlen(cmd)) != 0) 150 goto bad; 151 ino = dirlookup(name); 152 if (ino == 0) 153 break; 154 if (inodetype(ino) == LEAF) { 155 fprintf(stderr, "%s: not a directory\n", name); 156 break; 157 } 158 (void) strcpy(curdir, name); 159 break; 160 /* 161 * Delete elements from the extraction list. 162 */ 163 case 'd': 164 if (strncmp(cmd, "delete", strlen(cmd)) != 0) 165 goto bad; 166 np = lookupname(name); 167 if (np == NULL || (np->e_flags & NEW) == 0) { 168 fprintf(stderr, "%s: not on extraction list\n", name); 169 break; 170 } 171 treescan(name, np->e_ino, deletefile); 172 break; 173 /* 174 * Extract the requested list. 175 */ 176 case 'e': 177 if (strncmp(cmd, "extract", strlen(cmd)) != 0) 178 goto bad; 179 createfiles(); 180 createlinks(); 181 setdirmodes(0); 182 if (dflag) 183 checkrestore(); 184 volno = 0; 185 break; 186 /* 187 * List available commands. 188 */ 189 case 'h': 190 if (strncmp(cmd, "help", strlen(cmd)) != 0) 191 goto bad; 192 case '?': 193 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 194 "Available commands are:\n", 195 "\tls [arg] - list directory\n", 196 "\tcd arg - change directory\n", 197 "\tpwd - print current directory\n", 198 "\tadd [arg] - add `arg' to list of", 199 " files to be extracted\n", 200 "\tdelete [arg] - delete `arg' from", 201 " list of files to be extracted\n", 202 "\textract - extract requested files\n", 203 "\tsetmodes - set modes of requested directories\n", 204 "\tquit - immediately exit program\n", 205 "\twhat - list dump header information\n", 206 "\tverbose - toggle verbose flag", 207 " (useful with ``ls'')\n", 208 "\thelp or `?' - print this list\n", 209 "If no `arg' is supplied, the current", 210 " directory is used\n"); 211 break; 212 /* 213 * List a directory. 214 */ 215 case 'l': 216 if (strncmp(cmd, "ls", strlen(cmd)) != 0) 217 goto bad; 218 printlist(name, curdir); 219 break; 220 /* 221 * Print current directory. 222 */ 223 case 'p': 224 if (strncmp(cmd, "pwd", strlen(cmd)) != 0) 225 goto bad; 226 if (curdir[1] == '\0') 227 fprintf(stderr, "/\n"); 228 else 229 fprintf(stderr, "%s\n", &curdir[1]); 230 break; 231 /* 232 * Quit. 233 */ 234 case 'q': 235 if (strncmp(cmd, "quit", strlen(cmd)) != 0) 236 goto bad; 237 return; 238 case 'x': 239 if (strncmp(cmd, "xit", strlen(cmd)) != 0) 240 goto bad; 241 return; 242 /* 243 * Toggle verbose mode. 244 */ 245 case 'v': 246 if (strncmp(cmd, "verbose", strlen(cmd)) != 0) 247 goto bad; 248 if (vflag) { 249 fprintf(stderr, "verbose mode off\n"); 250 vflag = 0; 251 break; 252 } 253 fprintf(stderr, "verbose mode on\n"); 254 vflag++; 255 break; 256 /* 257 * Just restore requested directory modes. 258 */ 259 case 's': 260 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) 261 goto bad; 262 setdirmodes(FORCE); 263 break; 264 /* 265 * Print out dump header information. 266 */ 267 case 'w': 268 if (strncmp(cmd, "what", strlen(cmd)) != 0) 269 goto bad; 270 printdumpinfo(); 271 break; 272 /* 273 * Turn on debugging. 274 */ 275 case 'D': 276 if (strncmp(cmd, "Debug", strlen(cmd)) != 0) 277 goto bad; 278 if (dflag) { 279 fprintf(stderr, "debugging mode off\n"); 280 dflag = 0; 281 break; 282 } 283 fprintf(stderr, "debugging mode on\n"); 284 dflag++; 285 break; 286 /* 287 * Unknown command. 288 */ 289 default: 290 bad: 291 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); 292 break; 293 } 294 goto loop; 295 } 296 297 /* 298 * Read and parse an interactive command. 299 * The first word on the line is assigned to "cmd". If 300 * there are no arguments on the command line, then "curdir" 301 * is returned as the argument. If there are arguments 302 * on the line they are returned one at a time on each 303 * successive call to getcmd. Each argument is first assigned 304 * to "name". If it does not start with "/" the pathname in 305 * "curdir" is prepended to it. Finally "canon" is called to 306 * eliminate any embedded ".." components. 307 */ 308 static void 309 getcmd(curdir, cmd, name, ap) 310 char *curdir, *cmd, *name; 311 struct arglist *ap; 312 { 313 extern char *__progname; /* from crt0.o */ 314 char *cp; 315 static char input[BUFSIZ]; 316 char output[BUFSIZ]; 317 # define rawname input /* save space by reusing input buffer */ 318 319 /* 320 * Check to see if still processing arguments. 321 */ 322 if (ap->argcnt > 0) 323 goto retnext; 324 if (nextarg != NULL) 325 goto getnext; 326 /* 327 * Read a command line and trim off trailing white space. 328 */ 329 do { 330 fprintf(stderr, "%s > ", __progname); 331 (void) fflush(stderr); 332 (void) fgets(input, BUFSIZ, terminal); 333 } while (!feof(terminal) && input[0] == '\n'); 334 if (feof(terminal)) { 335 (void) strcpy(cmd, "quit"); 336 return; 337 } 338 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 339 /* trim off trailing white space and newline */; 340 *++cp = '\0'; 341 /* 342 * Copy the command into "cmd". 343 */ 344 cp = copynext(input, cmd); 345 ap->cmd = cmd; 346 /* 347 * If no argument, use curdir as the default. 348 */ 349 if (*cp == '\0') { 350 (void) strcpy(name, curdir); 351 return; 352 } 353 nextarg = cp; 354 /* 355 * Find the next argument. 356 */ 357 getnext: 358 cp = copynext(nextarg, rawname); 359 if (*cp == '\0') 360 nextarg = NULL; 361 else 362 nextarg = cp; 363 /* 364 * If it is an absolute pathname, canonicalize it and return it. 365 */ 366 if (rawname[0] == '/') { 367 canon(rawname, name); 368 } else { 369 /* 370 * For relative pathnames, prepend the current directory to 371 * it then canonicalize and return it. 372 */ 373 (void) strcpy(output, curdir); 374 (void) strcat(output, "/"); 375 (void) strcat(output, rawname); 376 canon(output, name); 377 } 378 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0) 379 fprintf(stderr, "%s: out of memory\n", ap->cmd); 380 if (ap->glob.gl_pathc == 0) 381 return; 382 ap->freeglob = 1; 383 ap->argcnt = ap->glob.gl_pathc; 384 385 retnext: 386 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]); 387 if (--ap->argcnt == 0) { 388 ap->freeglob = 0; 389 globfree(&ap->glob); 390 } 391 # undef rawname 392 } 393 394 /* 395 * Strip off the next token of the input. 396 */ 397 static char * 398 copynext(input, output) 399 char *input, *output; 400 { 401 char *cp, *bp; 402 char quote; 403 404 for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 405 /* skip to argument */; 406 bp = output; 407 while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 408 /* 409 * Handle back slashes. 410 */ 411 if (*cp == '\\') { 412 if (*++cp == '\0') { 413 fprintf(stderr, 414 "command lines cannot be continued\n"); 415 continue; 416 } 417 *bp++ = *cp++; 418 continue; 419 } 420 /* 421 * The usual unquoted case. 422 */ 423 if (*cp != '\'' && *cp != '"') { 424 *bp++ = *cp++; 425 continue; 426 } 427 /* 428 * Handle single and double quotes. 429 */ 430 quote = *cp++; 431 while (*cp != quote && *cp != '\0') 432 *bp++ = *cp++ | 0200; 433 if (*cp++ == '\0') { 434 fprintf(stderr, "missing %c\n", quote); 435 cp--; 436 continue; 437 } 438 } 439 *bp = '\0'; 440 return (cp); 441 } 442 443 /* 444 * Canonicalize file names to always start with ``./'' and 445 * remove any imbedded "." and ".." components. 446 */ 447 void 448 canon(rawname, canonname) 449 char *rawname, *canonname; 450 { 451 char *cp, *np; 452 453 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 454 (void) strcpy(canonname, ""); 455 else if (rawname[0] == '/') 456 (void) strcpy(canonname, "."); 457 else 458 (void) strcpy(canonname, "./"); 459 (void) strcat(canonname, rawname); 460 /* 461 * Eliminate multiple and trailing '/'s 462 */ 463 for (cp = np = canonname; *np != '\0'; cp++) { 464 *cp = *np++; 465 while (*cp == '/' && *np == '/') 466 np++; 467 } 468 *cp = '\0'; 469 if (*--cp == '/') 470 *cp = '\0'; 471 /* 472 * Eliminate extraneous "." and ".." from pathnames. 473 */ 474 for (np = canonname; *np != '\0'; ) { 475 np++; 476 cp = np; 477 while (*np != '/' && *np != '\0') 478 np++; 479 if (np - cp == 1 && *cp == '.') { 480 cp--; 481 (void) strcpy(cp, np); 482 np = cp; 483 } 484 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 485 cp--; 486 while (cp > &canonname[1] && *--cp != '/') 487 /* find beginning of name */; 488 (void) strcpy(cp, np); 489 np = cp; 490 } 491 } 492 } 493 494 /* 495 * Do an "ls" style listing of a directory 496 */ 497 static void 498 printlist(name, basename) 499 char *name; 500 char *basename; 501 { 502 struct afile *fp, *list, *listp; 503 struct direct *dp; 504 struct afile single; 505 RST_DIR *dirp; 506 int entries, len, namelen; 507 char locname[MAXPATHLEN + 1]; 508 509 dp = pathsearch(name); 510 listp = NULL; 511 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 512 (!vflag && dp->d_ino == WINO)) 513 return; 514 if ((dirp = rst_opendir(name)) == NULL) { 515 entries = 1; 516 list = &single; 517 mkentry(name, dp, list); 518 len = strlen(basename) + 1; 519 if (strlen(name) - len > single.len) { 520 freename(single.fname); 521 single.fname = savename(&name[len]); 522 single.len = strlen(single.fname); 523 } 524 } else { 525 entries = 0; 526 while ((dp = rst_readdir(dirp)) != NULL) 527 entries++; 528 rst_closedir(dirp); 529 list = (struct afile *)malloc(entries * sizeof(struct afile)); 530 if (list == NULL) { 531 fprintf(stderr, "ls: out of memory\n"); 532 return; 533 } 534 if ((dirp = rst_opendir(name)) == NULL) 535 panic("directory reopen failed\n"); 536 fprintf(stderr, "%s:\n", name); 537 entries = 0; 538 listp = list; 539 (void) strncpy(locname, name, MAXPATHLEN); 540 (void) strncat(locname, "/", MAXPATHLEN); 541 namelen = strlen(locname); 542 while ((dp = rst_readdir(dirp)) != NULL) { 543 if (dp == NULL) 544 break; 545 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) 546 continue; 547 if (!vflag && (dp->d_ino == WINO || 548 strcmp(dp->d_name, ".") == 0 || 549 strcmp(dp->d_name, "..") == 0)) 550 continue; 551 locname[namelen] = '\0'; 552 if (namelen + dp->d_namlen >= MAXPATHLEN) { 553 fprintf(stderr, "%s%s: name exceeds %d char\n", 554 locname, dp->d_name, MAXPATHLEN); 555 } else { 556 (void) strncat(locname, dp->d_name, 557 (int)dp->d_namlen); 558 mkentry(locname, dp, listp++); 559 entries++; 560 } 561 } 562 rst_closedir(dirp); 563 if (entries == 0) { 564 fprintf(stderr, "\n"); 565 free(list); 566 return; 567 } 568 qsort((char *)list, entries, sizeof(struct afile), fcmp); 569 } 570 formatf(list, entries); 571 if (dirp != NULL) { 572 for (fp = listp - 1; fp >= list; fp--) 573 freename(fp->fname); 574 fprintf(stderr, "\n"); 575 free(list); 576 } 577 } 578 579 /* 580 * Read the contents of a directory. 581 */ 582 static void 583 mkentry(name, dp, fp) 584 char *name; 585 struct direct *dp; 586 struct afile *fp; 587 { 588 char *cp; 589 struct entry *np; 590 591 fp->fnum = dp->d_ino; 592 fp->fname = savename(dp->d_name); 593 for (cp = fp->fname; *cp; cp++) 594 if (!vflag && (*cp < ' ' || *cp >= 0177)) 595 *cp = '?'; 596 fp->len = cp - fp->fname; 597 if (dflag && TSTINO(fp->fnum, dumpmap) == 0) 598 fp->prefix = '^'; 599 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW)) 600 fp->prefix = '*'; 601 else 602 fp->prefix = ' '; 603 switch(dp->d_type) { 604 605 default: 606 fprintf(stderr, "Warning: undefined file type %d\n", 607 dp->d_type); 608 /* fall through */ 609 case DT_REG: 610 fp->postfix = ' '; 611 break; 612 613 case DT_LNK: 614 fp->postfix = '@'; 615 break; 616 617 case DT_FIFO: 618 case DT_SOCK: 619 fp->postfix = '='; 620 break; 621 622 case DT_CHR: 623 case DT_BLK: 624 fp->postfix = '#'; 625 break; 626 627 case DT_WHT: 628 fp->postfix = '%'; 629 break; 630 631 case DT_UNKNOWN: 632 case DT_DIR: 633 if (inodetype(dp->d_ino) == NODE) 634 fp->postfix = '/'; 635 else 636 fp->postfix = ' '; 637 break; 638 } 639 return; 640 } 641 642 /* 643 * Print out a pretty listing of a directory 644 */ 645 static void 646 formatf(list, nentry) 647 struct afile *list; 648 int nentry; 649 { 650 struct afile *fp, *endlist; 651 int width, bigino, haveprefix, havepostfix; 652 int i, j, w, precision, columns, lines; 653 654 width = 0; 655 haveprefix = 0; 656 havepostfix = 0; 657 precision = 0; 658 bigino = ROOTINO; 659 endlist = &list[nentry]; 660 for (fp = &list[0]; fp < endlist; fp++) { 661 if (bigino < fp->fnum) 662 bigino = fp->fnum; 663 if (width < fp->len) 664 width = fp->len; 665 if (fp->prefix != ' ') 666 haveprefix = 1; 667 if (fp->postfix != ' ') 668 havepostfix = 1; 669 } 670 if (haveprefix) 671 width++; 672 if (havepostfix) 673 width++; 674 if (vflag) { 675 for (precision = 0, i = bigino; i > 0; i /= 10) 676 precision++; 677 width += precision + 1; 678 } 679 width++; 680 columns = 81 / width; 681 if (columns == 0) 682 columns = 1; 683 lines = (nentry + columns - 1) / columns; 684 for (i = 0; i < lines; i++) { 685 for (j = 0; j < columns; j++) { 686 fp = &list[j * lines + i]; 687 if (vflag) { 688 fprintf(stderr, "%*d ", precision, fp->fnum); 689 fp->len += precision + 1; 690 } 691 if (haveprefix) { 692 putc(fp->prefix, stderr); 693 fp->len++; 694 } 695 fprintf(stderr, "%s", fp->fname); 696 if (havepostfix) { 697 putc(fp->postfix, stderr); 698 fp->len++; 699 } 700 if (fp + lines >= endlist) { 701 fprintf(stderr, "\n"); 702 break; 703 } 704 for (w = fp->len; w < width; w++) 705 putc(' ', stderr); 706 } 707 } 708 } 709 710 /* 711 * Skip over directory entries that are not on the tape 712 * 713 * First have to get definition of a dirent. 714 */ 715 #undef DIRBLKSIZ 716 #include <dirent.h> 717 #undef d_ino 718 719 struct dirent * 720 glob_readdir(dirp) 721 RST_DIR *dirp; 722 { 723 struct direct *dp; 724 static struct dirent adirent; 725 726 while ((dp = rst_readdir(dirp)) != NULL) { 727 if (!vflag && dp->d_ino == WINO) 728 continue; 729 if (dflag || TSTINO(dp->d_ino, dumpmap)) 730 break; 731 } 732 if (dp == NULL) 733 return (NULL); 734 adirent.d_fileno = dp->d_ino; 735 adirent.d_namlen = dp->d_namlen; 736 memcpy(adirent.d_name, dp->d_name, dp->d_namlen + 1); 737 return (&adirent); 738 } 739 740 /* 741 * Return st_mode information in response to stat or lstat calls 742 */ 743 static int 744 glob_stat(name, stp) 745 const char *name; 746 struct stat *stp; 747 { 748 struct direct *dp; 749 750 dp = pathsearch(name); 751 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 752 (!vflag && dp->d_ino == WINO)) 753 return (-1); 754 if (inodetype(dp->d_ino) == NODE) 755 stp->st_mode = S_IFDIR; 756 else 757 stp->st_mode = S_IFREG; 758 return (0); 759 } 760 761 /* 762 * Comparison routine for qsort. 763 */ 764 static int 765 fcmp(f1, f2) 766 const void *f1, *f2; 767 { 768 return (strcmp(((struct afile *)f1)->fname, 769 ((struct afile *)f2)->fname)); 770 } 771 772 /* 773 * respond to interrupts 774 */ 775 void 776 onintr(signo) 777 int signo; 778 { 779 if (command == 'i' && runshell) 780 longjmp(reset, 1); 781 if (reply("restore interrupted, continue") == FAIL) 782 exit(1); 783 } 784