interactive.c revision 1.20 1 /* $NetBSD: interactive.c,v 1.20 2005/01/08 14:30:39 fredb 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
36 #else
37 __RCSID("$NetBSD: interactive.c,v 1.20 2005/01/08 14:30:39 fredb Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
48 #include <protocols/dumprestore.h>
49
50 #include <setjmp.h>
51 #include <glob.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "restore.h"
57 #include "extern.h"
58
59 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
60
61 /*
62 * Things to handle interruptions.
63 */
64 static int runshell;
65 static jmp_buf reset;
66 static char *nextarg = NULL;
67
68 /*
69 * Structure and routines associated with listing directories.
70 */
71 struct afile {
72 ino_t fnum; /* inode number of file */
73 char *fname; /* file name */
74 short len; /* name length */
75 char prefix; /* prefix character */
76 char postfix; /* postfix character */
77 };
78 struct arglist {
79 int freeglob; /* glob structure needs to be freed */
80 int argcnt; /* next globbed argument to return */
81 glob_t glob; /* globbing information */
82 char *cmd; /* the current command */
83 };
84
85 static char *copynext __P((char *, char *));
86 static int fcmp __P((const void *, const void *));
87 static void formatf __P((struct afile *, int));
88 static void getcmd __P((char *, char *, char *, struct arglist *));
89 struct dirent *glob_readdir __P((RST_DIR *dirp));
90 static int glob_stat __P((const char *, struct stat *));
91 static void mkentry __P((char *, struct direct *, struct afile *));
92 static void printlist __P((char *, char *));
93
94 /*
95 * Read and execute commands from the terminal.
96 */
97 void
98 runcmdshell()
99 {
100 struct entry *np;
101 ino_t ino;
102 struct arglist arglist;
103 char curdir[MAXPATHLEN];
104 char name[MAXPATHLEN];
105 char cmd[BUFSIZ];
106
107 arglist.freeglob = 0;
108 arglist.argcnt = 0;
109 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
110 arglist.glob.gl_opendir = (void *)rst_opendir;
111 arglist.glob.gl_readdir = (void *)glob_readdir;
112 arglist.glob.gl_closedir = (void *)rst_closedir;
113 arglist.glob.gl_lstat = glob_stat;
114 arglist.glob.gl_stat = glob_stat;
115 canon("/", curdir);
116 loop:
117 if (setjmp(reset) != 0) {
118 if (arglist.freeglob != 0) {
119 arglist.freeglob = 0;
120 arglist.argcnt = 0;
121 globfree(&arglist.glob);
122 }
123 nextarg = NULL;
124 volno = 0;
125 }
126 runshell = 1;
127 getcmd(curdir, cmd, name, &arglist);
128 switch (cmd[0]) {
129 /*
130 * Add elements to the extraction list.
131 */
132 case 'a':
133 if (strncmp(cmd, "add", strlen(cmd)) != 0)
134 goto bad;
135 ino = dirlookup(name);
136 if (ino == 0)
137 break;
138 if (ino == ROOTINO)
139 dotflag = 1;
140 if (mflag)
141 pathcheck(name);
142 treescan(name, ino, addfile);
143 break;
144 /*
145 * Change working directory.
146 */
147 case 'c':
148 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
149 goto bad;
150 ino = dirlookup(name);
151 if (ino == 0)
152 break;
153 if (inodetype(ino) == LEAF) {
154 fprintf(stderr, "%s: not a directory\n", name);
155 break;
156 }
157 (void) strcpy(curdir, name);
158 break;
159 /*
160 * Delete elements from the extraction list.
161 */
162 case 'd':
163 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
164 goto bad;
165 np = lookupname(name);
166 if (np == NULL || (np->e_flags & NEW) == 0) {
167 fprintf(stderr, "%s: not on extraction list\n", name);
168 break;
169 }
170 treescan(name, np->e_ino, deletefile);
171 break;
172 /*
173 * Extract the requested list.
174 */
175 case 'e':
176 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
177 goto bad;
178 createfiles();
179 createlinks();
180 setdirmodes(0);
181 if (dflag)
182 checkrestore();
183 volno = 0;
184 break;
185 /*
186 * List available commands.
187 */
188 case 'h':
189 if (strncmp(cmd, "help", strlen(cmd)) != 0)
190 goto bad;
191 case '?':
192 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
193 "Available commands are:\n",
194 "\tls [arg] - list directory\n",
195 "\tcd arg - change directory\n",
196 "\tpwd - print current directory\n",
197 "\tadd [arg] - add `arg' to list of",
198 " files to be extracted\n",
199 "\tdelete [arg] - delete `arg' from",
200 " list of files to be extracted\n",
201 "\textract - extract requested files\n",
202 "\tsetmodes - set modes of requested directories\n",
203 "\tquit or xit - immediately exit program\n",
204 "\twhat - list dump header information\n",
205 "\tverbose - toggle verbose flag",
206 " (useful with ``ls'')\n",
207 "\thelp or `?' - print this list\n",
208 "\tDebug - turn on debugging\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 char *cp;
314 static char input[BUFSIZ];
315 char output[BUFSIZ];
316 int globretval;
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 > ", getprogname());
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 ((globretval = glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob)) < 0) {
379 fprintf(stderr, "%s: %s: ", ap->cmd, name);
380 switch (globretval) {
381 case GLOB_NOSPACE:
382 fprintf(stderr, "out of memory\n");
383 break;
384 case GLOB_NOMATCH:
385 fprintf(stderr, "no filename match.\n");
386 break;
387 case GLOB_ABORTED:
388 fprintf(stderr, "glob() aborted.\n");
389 break;
390 default:
391 fprintf(stderr, "unknown error!\n");
392 break;
393 }
394 }
395 if (ap->glob.gl_pathc == 0)
396 return;
397 ap->freeglob = 1;
398 ap->argcnt = ap->glob.gl_pathc;
399
400 retnext:
401 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
402 if (--ap->argcnt == 0) {
403 ap->freeglob = 0;
404 globfree(&ap->glob);
405 }
406 # undef rawname
407 }
408
409 /*
410 * Strip off the next token of the input.
411 */
412 static char *
413 copynext(input, output)
414 char *input, *output;
415 {
416 char *cp, *bp;
417 char quote;
418
419 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
420 /* skip to argument */;
421 bp = output;
422 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
423 /*
424 * Handle back slashes.
425 */
426 if (*cp == '\\') {
427 if (*++cp == '\0') {
428 fprintf(stderr,
429 "command lines cannot be continued\n");
430 continue;
431 }
432 *bp++ = *cp++;
433 continue;
434 }
435 /*
436 * The usual unquoted case.
437 */
438 if (*cp != '\'' && *cp != '"') {
439 *bp++ = *cp++;
440 continue;
441 }
442 /*
443 * Handle single and double quotes.
444 */
445 quote = *cp++;
446 while (*cp != quote && *cp != '\0')
447 *bp++ = *cp++;
448 if (*cp++ == '\0') {
449 fprintf(stderr, "missing %c\n", quote);
450 cp--;
451 continue;
452 }
453 }
454 *bp = '\0';
455 return (cp);
456 }
457
458 /*
459 * Canonicalize file names to always start with ``./'' and
460 * remove any imbedded "." and ".." components.
461 */
462 void
463 canon(rawname, canonname)
464 char *rawname, *canonname;
465 {
466 char *cp, *np;
467
468 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
469 (void) strcpy(canonname, "");
470 else if (rawname[0] == '/')
471 (void) strcpy(canonname, ".");
472 else
473 (void) strcpy(canonname, "./");
474 (void) strcat(canonname, rawname);
475 /*
476 * Eliminate multiple and trailing '/'s
477 */
478 for (cp = np = canonname; *np != '\0'; cp++) {
479 *cp = *np++;
480 while (*cp == '/' && *np == '/')
481 np++;
482 }
483 *cp = '\0';
484 if (*--cp == '/')
485 *cp = '\0';
486 /*
487 * Eliminate extraneous "." and ".." from pathnames.
488 */
489 for (np = canonname; *np != '\0'; ) {
490 np++;
491 cp = np;
492 while (*np != '/' && *np != '\0')
493 np++;
494 if (np - cp == 1 && *cp == '.') {
495 cp--;
496 (void) strcpy(cp, np);
497 np = cp;
498 }
499 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
500 cp--;
501 while (cp > &canonname[1] && *--cp != '/')
502 /* find beginning of name */;
503 (void) strcpy(cp, np);
504 np = cp;
505 }
506 }
507 }
508
509 /*
510 * Do an "ls" style listing of a directory
511 */
512 static void
513 printlist(name, basename)
514 char *name;
515 char *basename;
516 {
517 struct afile *fp, *list, *listp;
518 struct direct *dp;
519 struct afile single;
520 RST_DIR *dirp;
521 int entries, len, namelen;
522 char locname[MAXPATHLEN + 1];
523
524 dp = pathsearch(name);
525 listp = NULL;
526 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
527 (!vflag && dp->d_ino == WINO))
528 return;
529 if ((dirp = rst_opendir(name)) == NULL) {
530 entries = 1;
531 list = &single;
532 mkentry(name, dp, list);
533 len = strlen(basename) + 1;
534 if (strlen(name) - len > single.len) {
535 freename(single.fname);
536 single.fname = savename(&name[len]);
537 single.len = strlen(single.fname);
538 }
539 } else {
540 entries = 0;
541 while ((dp = rst_readdir(dirp)) != NULL)
542 entries++;
543 rst_closedir(dirp);
544 list = (struct afile *)malloc(entries * sizeof(struct afile));
545 if (list == NULL) {
546 fprintf(stderr, "ls: out of memory\n");
547 return;
548 }
549 if ((dirp = rst_opendir(name)) == NULL)
550 panic("directory reopen failed\n");
551 fprintf(stderr, "%s:\n", name);
552 entries = 0;
553 listp = list;
554 (void) strncpy(locname, name, MAXPATHLEN);
555 (void) strncat(locname, "/", MAXPATHLEN);
556 namelen = strlen(locname);
557 while ((dp = rst_readdir(dirp)) != NULL) {
558 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
559 continue;
560 if (!vflag && (dp->d_ino == WINO ||
561 strcmp(dp->d_name, ".") == 0 ||
562 strcmp(dp->d_name, "..") == 0))
563 continue;
564 locname[namelen] = '\0';
565 if (namelen + dp->d_namlen >= MAXPATHLEN) {
566 fprintf(stderr, "%s%s: name exceeds %d char\n",
567 locname, dp->d_name, MAXPATHLEN);
568 } else {
569 (void) strncat(locname, dp->d_name,
570 (int)dp->d_namlen);
571 mkentry(locname, dp, listp++);
572 entries++;
573 }
574 }
575 rst_closedir(dirp);
576 if (entries == 0) {
577 fprintf(stderr, "\n");
578 free(list);
579 return;
580 }
581 qsort((char *)list, entries, sizeof(struct afile), fcmp);
582 }
583 formatf(list, entries);
584 if (dirp != NULL) {
585 for (fp = listp - 1; fp >= list; fp--)
586 freename(fp->fname);
587 fprintf(stderr, "\n");
588 free(list);
589 }
590 }
591
592 /*
593 * Read the contents of a directory.
594 */
595 static void
596 mkentry(name, dp, fp)
597 char *name;
598 struct direct *dp;
599 struct afile *fp;
600 {
601 char *cp;
602 struct entry *np;
603
604 fp->fnum = dp->d_ino;
605 fp->fname = savename(dp->d_name);
606 for (cp = fp->fname; *cp; cp++)
607 if (!vflag && (*cp < ' ' || *cp >= 0177))
608 *cp = '?';
609 fp->len = cp - fp->fname;
610 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
611 fp->prefix = '^';
612 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
613 fp->prefix = '*';
614 else
615 fp->prefix = ' ';
616 switch(dp->d_type) {
617
618 default:
619 fprintf(stderr, "Warning: undefined file type %d\n",
620 dp->d_type);
621 /* fall through */
622 case DT_REG:
623 fp->postfix = ' ';
624 break;
625
626 case DT_LNK:
627 fp->postfix = '@';
628 break;
629
630 case DT_FIFO:
631 case DT_SOCK:
632 fp->postfix = '=';
633 break;
634
635 case DT_CHR:
636 case DT_BLK:
637 fp->postfix = '#';
638 break;
639
640 case DT_WHT:
641 fp->postfix = '%';
642 break;
643
644 case DT_UNKNOWN:
645 case DT_DIR:
646 if (inodetype(dp->d_ino) == NODE)
647 fp->postfix = '/';
648 else
649 fp->postfix = ' ';
650 break;
651 }
652 return;
653 }
654
655 /*
656 * Print out a pretty listing of a directory
657 */
658 static void
659 formatf(list, nentry)
660 struct afile *list;
661 int nentry;
662 {
663 struct afile *fp, *endlist;
664 int width, bigino, haveprefix, havepostfix;
665 int i, j, w, precision, columns, lines;
666
667 width = 0;
668 haveprefix = 0;
669 havepostfix = 0;
670 precision = 0;
671 bigino = ROOTINO;
672 endlist = &list[nentry];
673 for (fp = &list[0]; fp < endlist; fp++) {
674 if (bigino < fp->fnum)
675 bigino = fp->fnum;
676 if (width < fp->len)
677 width = fp->len;
678 if (fp->prefix != ' ')
679 haveprefix = 1;
680 if (fp->postfix != ' ')
681 havepostfix = 1;
682 }
683 if (haveprefix)
684 width++;
685 if (havepostfix)
686 width++;
687 if (vflag) {
688 for (precision = 0, i = bigino; i > 0; i /= 10)
689 precision++;
690 width += precision + 1;
691 }
692 width++;
693 columns = 81 / width;
694 if (columns == 0)
695 columns = 1;
696 lines = (nentry + columns - 1) / columns;
697 for (i = 0; i < lines; i++) {
698 for (j = 0; j < columns; j++) {
699 fp = &list[j * lines + i];
700 if (vflag) {
701 fprintf(stderr, "%*d ", precision, fp->fnum);
702 fp->len += precision + 1;
703 }
704 if (haveprefix) {
705 putc(fp->prefix, stderr);
706 fp->len++;
707 }
708 fprintf(stderr, "%s", fp->fname);
709 if (havepostfix) {
710 putc(fp->postfix, stderr);
711 fp->len++;
712 }
713 if (fp + lines >= endlist) {
714 fprintf(stderr, "\n");
715 break;
716 }
717 for (w = fp->len; w < width; w++)
718 putc(' ', stderr);
719 }
720 }
721 }
722
723 /*
724 * Skip over directory entries that are not on the tape
725 *
726 * First have to get definition of a dirent.
727 */
728 #undef DIRBLKSIZ
729 #include <dirent.h>
730 #undef d_ino
731
732 struct dirent *
733 glob_readdir(dirp)
734 RST_DIR *dirp;
735 {
736 struct direct *dp;
737 static struct dirent adirent;
738
739 while ((dp = rst_readdir(dirp)) != NULL) {
740 if (!vflag && dp->d_ino == WINO)
741 continue;
742 if (dflag || TSTINO(dp->d_ino, dumpmap))
743 break;
744 }
745 if (dp == NULL)
746 return (NULL);
747 adirent.d_fileno = dp->d_ino;
748 adirent.d_namlen = dp->d_namlen;
749 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
750 return (&adirent);
751 }
752
753 /*
754 * Return st_mode information in response to stat or lstat calls
755 */
756 static int
757 glob_stat(name, stp)
758 const char *name;
759 struct stat *stp;
760 {
761 struct direct *dp;
762
763 dp = pathsearch(name);
764 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
765 (!vflag && dp->d_ino == WINO))
766 return (-1);
767 if (inodetype(dp->d_ino) == NODE)
768 stp->st_mode = S_IFDIR;
769 else
770 stp->st_mode = S_IFREG;
771 return (0);
772 }
773
774 /*
775 * Comparison routine for qsort.
776 */
777 static int
778 fcmp(f1, f2)
779 const void *f1, *f2;
780 {
781 return (strcmp(((struct afile *)f1)->fname,
782 ((struct afile *)f2)->fname));
783 }
784
785 /*
786 * respond to interrupts
787 */
788 void
789 onintr(signo)
790 int signo;
791 {
792 if (command == 'i' && runshell)
793 longjmp(reset, 1);
794 if (reply("restore interrupted, continue") == FAIL)
795 exit(1);
796 }
797