interactive.c revision 1.9 1 /* $NetBSD: interactive.c,v 1.9 1995/03/18 14:59:44 cgd 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.9 1995/03/18 14:59:44 cgd 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 register 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 register char *cp;
314 static char input[BUFSIZ];
315 char output[BUFSIZ];
316 # define rawname input /* save space by reusing input buffer */
317
318 /*
319 * Check to see if still processing arguments.
320 */
321 if (ap->argcnt > 0)
322 goto retnext;
323 if (nextarg != NULL)
324 goto getnext;
325 /*
326 * Read a command line and trim off trailing white space.
327 */
328 do {
329 fprintf(stderr, "restore > ");
330 (void) fflush(stderr);
331 (void) fgets(input, BUFSIZ, terminal);
332 } while (!feof(terminal) && input[0] == '\n');
333 if (feof(terminal)) {
334 (void) strcpy(cmd, "quit");
335 return;
336 }
337 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
338 /* trim off trailing white space and newline */;
339 *++cp = '\0';
340 /*
341 * Copy the command into "cmd".
342 */
343 cp = copynext(input, cmd);
344 ap->cmd = cmd;
345 /*
346 * If no argument, use curdir as the default.
347 */
348 if (*cp == '\0') {
349 (void) strcpy(name, curdir);
350 return;
351 }
352 nextarg = cp;
353 /*
354 * Find the next argument.
355 */
356 getnext:
357 cp = copynext(nextarg, rawname);
358 if (*cp == '\0')
359 nextarg = NULL;
360 else
361 nextarg = cp;
362 /*
363 * If it is an absolute pathname, canonicalize it and return it.
364 */
365 if (rawname[0] == '/') {
366 canon(rawname, name);
367 } else {
368 /*
369 * For relative pathnames, prepend the current directory to
370 * it then canonicalize and return it.
371 */
372 (void) strcpy(output, curdir);
373 (void) strcat(output, "/");
374 (void) strcat(output, rawname);
375 canon(output, name);
376 }
377 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
378 fprintf(stderr, "%s: out of memory\n", ap->cmd);
379 if (ap->glob.gl_pathc == 0)
380 return;
381 ap->freeglob = 1;
382 ap->argcnt = ap->glob.gl_pathc;
383
384 retnext:
385 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
386 if (--ap->argcnt == 0) {
387 ap->freeglob = 0;
388 globfree(&ap->glob);
389 }
390 # undef rawname
391 }
392
393 /*
394 * Strip off the next token of the input.
395 */
396 static char *
397 copynext(input, output)
398 char *input, *output;
399 {
400 register char *cp, *bp;
401 char quote;
402
403 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
404 /* skip to argument */;
405 bp = output;
406 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
407 /*
408 * Handle back slashes.
409 */
410 if (*cp == '\\') {
411 if (*++cp == '\0') {
412 fprintf(stderr,
413 "command lines cannot be continued\n");
414 continue;
415 }
416 *bp++ = *cp++;
417 continue;
418 }
419 /*
420 * The usual unquoted case.
421 */
422 if (*cp != '\'' && *cp != '"') {
423 *bp++ = *cp++;
424 continue;
425 }
426 /*
427 * Handle single and double quotes.
428 */
429 quote = *cp++;
430 while (*cp != quote && *cp != '\0')
431 *bp++ = *cp++ | 0200;
432 if (*cp++ == '\0') {
433 fprintf(stderr, "missing %c\n", quote);
434 cp--;
435 continue;
436 }
437 }
438 *bp = '\0';
439 return (cp);
440 }
441
442 /*
443 * Canonicalize file names to always start with ``./'' and
444 * remove any imbedded "." and ".." components.
445 */
446 void
447 canon(rawname, canonname)
448 char *rawname, *canonname;
449 {
450 register char *cp, *np;
451
452 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
453 (void) strcpy(canonname, "");
454 else if (rawname[0] == '/')
455 (void) strcpy(canonname, ".");
456 else
457 (void) strcpy(canonname, "./");
458 (void) strcat(canonname, rawname);
459 /*
460 * Eliminate multiple and trailing '/'s
461 */
462 for (cp = np = canonname; *np != '\0'; cp++) {
463 *cp = *np++;
464 while (*cp == '/' && *np == '/')
465 np++;
466 }
467 *cp = '\0';
468 if (*--cp == '/')
469 *cp = '\0';
470 /*
471 * Eliminate extraneous "." and ".." from pathnames.
472 */
473 for (np = canonname; *np != '\0'; ) {
474 np++;
475 cp = np;
476 while (*np != '/' && *np != '\0')
477 np++;
478 if (np - cp == 1 && *cp == '.') {
479 cp--;
480 (void) strcpy(cp, np);
481 np = cp;
482 }
483 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
484 cp--;
485 while (cp > &canonname[1] && *--cp != '/')
486 /* find beginning of name */;
487 (void) strcpy(cp, np);
488 np = cp;
489 }
490 }
491 }
492
493 /*
494 * Do an "ls" style listing of a directory
495 */
496 static void
497 printlist(name, basename)
498 char *name;
499 char *basename;
500 {
501 register struct afile *fp, *list, *listp;
502 register struct direct *dp;
503 struct afile single;
504 RST_DIR *dirp;
505 int entries, len, namelen;
506 char locname[MAXPATHLEN + 1];
507
508 dp = pathsearch(name);
509 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
510 (!vflag && dp->d_ino == WINO))
511 return;
512 if ((dirp = rst_opendir(name)) == NULL) {
513 entries = 1;
514 list = &single;
515 mkentry(name, dp, list);
516 len = strlen(basename) + 1;
517 if (strlen(name) - len > single.len) {
518 freename(single.fname);
519 single.fname = savename(&name[len]);
520 single.len = strlen(single.fname);
521 }
522 } else {
523 entries = 0;
524 while (dp = rst_readdir(dirp))
525 entries++;
526 rst_closedir(dirp);
527 list = (struct afile *)malloc(entries * sizeof(struct afile));
528 if (list == NULL) {
529 fprintf(stderr, "ls: out of memory\n");
530 return;
531 }
532 if ((dirp = rst_opendir(name)) == NULL)
533 panic("directory reopen failed\n");
534 fprintf(stderr, "%s:\n", name);
535 entries = 0;
536 listp = list;
537 (void) strncpy(locname, name, MAXPATHLEN);
538 (void) strncat(locname, "/", MAXPATHLEN);
539 namelen = strlen(locname);
540 while (dp = rst_readdir(dirp)) {
541 if (dp == NULL)
542 break;
543 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
544 continue;
545 if (!vflag && (dp->d_ino == WINO ||
546 strcmp(dp->d_name, ".") == 0 ||
547 strcmp(dp->d_name, "..") == 0))
548 continue;
549 locname[namelen] = '\0';
550 if (namelen + dp->d_namlen >= MAXPATHLEN) {
551 fprintf(stderr, "%s%s: name exceeds %d char\n",
552 locname, dp->d_name, MAXPATHLEN);
553 } else {
554 (void) strncat(locname, dp->d_name,
555 (int)dp->d_namlen);
556 mkentry(locname, dp, listp++);
557 entries++;
558 }
559 }
560 rst_closedir(dirp);
561 if (entries == 0) {
562 fprintf(stderr, "\n");
563 free(list);
564 return;
565 }
566 qsort((char *)list, entries, sizeof(struct afile), fcmp);
567 }
568 formatf(list, entries);
569 if (dirp != NULL) {
570 for (fp = listp - 1; fp >= list; fp--)
571 freename(fp->fname);
572 fprintf(stderr, "\n");
573 free(list);
574 }
575 }
576
577 /*
578 * Read the contents of a directory.
579 */
580 static void
581 mkentry(name, dp, fp)
582 char *name;
583 struct direct *dp;
584 register struct afile *fp;
585 {
586 char *cp;
587 struct entry *np;
588
589 fp->fnum = dp->d_ino;
590 fp->fname = savename(dp->d_name);
591 for (cp = fp->fname; *cp; cp++)
592 if (!vflag && (*cp < ' ' || *cp >= 0177))
593 *cp = '?';
594 fp->len = cp - fp->fname;
595 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
596 fp->prefix = '^';
597 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
598 fp->prefix = '*';
599 else
600 fp->prefix = ' ';
601 switch(dp->d_type) {
602
603 default:
604 fprintf(stderr, "Warning: undefined file type %d\n",
605 dp->d_type);
606 /* fall through */
607 case DT_REG:
608 fp->postfix = ' ';
609 break;
610
611 case DT_LNK:
612 fp->postfix = '@';
613 break;
614
615 case DT_FIFO:
616 case DT_SOCK:
617 fp->postfix = '=';
618 break;
619
620 case DT_CHR:
621 case DT_BLK:
622 fp->postfix = '#';
623 break;
624
625 case DT_WHT:
626 fp->postfix = '%';
627 break;
628
629 case DT_UNKNOWN:
630 case DT_DIR:
631 if (inodetype(dp->d_ino) == NODE)
632 fp->postfix = '/';
633 else
634 fp->postfix = ' ';
635 break;
636 }
637 return;
638 }
639
640 /*
641 * Print out a pretty listing of a directory
642 */
643 static void
644 formatf(list, nentry)
645 register struct afile *list;
646 int nentry;
647 {
648 register struct afile *fp, *endlist;
649 int width, bigino, haveprefix, havepostfix;
650 int i, j, w, precision, columns, lines;
651
652 width = 0;
653 haveprefix = 0;
654 havepostfix = 0;
655 bigino = ROOTINO;
656 endlist = &list[nentry];
657 for (fp = &list[0]; fp < endlist; fp++) {
658 if (bigino < fp->fnum)
659 bigino = fp->fnum;
660 if (width < fp->len)
661 width = fp->len;
662 if (fp->prefix != ' ')
663 haveprefix = 1;
664 if (fp->postfix != ' ')
665 havepostfix = 1;
666 }
667 if (haveprefix)
668 width++;
669 if (havepostfix)
670 width++;
671 if (vflag) {
672 for (precision = 0, i = bigino; i > 0; i /= 10)
673 precision++;
674 width += precision + 1;
675 }
676 width++;
677 columns = 81 / width;
678 if (columns == 0)
679 columns = 1;
680 lines = (nentry + columns - 1) / columns;
681 for (i = 0; i < lines; i++) {
682 for (j = 0; j < columns; j++) {
683 fp = &list[j * lines + i];
684 if (vflag) {
685 fprintf(stderr, "%*d ", precision, fp->fnum);
686 fp->len += precision + 1;
687 }
688 if (haveprefix) {
689 putc(fp->prefix, stderr);
690 fp->len++;
691 }
692 fprintf(stderr, "%s", fp->fname);
693 if (havepostfix) {
694 putc(fp->postfix, stderr);
695 fp->len++;
696 }
697 if (fp + lines >= endlist) {
698 fprintf(stderr, "\n");
699 break;
700 }
701 for (w = fp->len; w < width; w++)
702 putc(' ', stderr);
703 }
704 }
705 }
706
707 /*
708 * Skip over directory entries that are not on the tape
709 *
710 * First have to get definition of a dirent.
711 */
712 #undef DIRBLKSIZ
713 #include <dirent.h>
714 #undef d_ino
715
716 struct dirent *
717 glob_readdir(dirp)
718 RST_DIR *dirp;
719 {
720 struct direct *dp;
721 static struct dirent adirent;
722
723 while ((dp = rst_readdir(dirp)) != NULL) {
724 if (!vflag && dp->d_ino == WINO)
725 continue;
726 if (dflag || TSTINO(dp->d_ino, dumpmap))
727 break;
728 }
729 if (dp == NULL)
730 return (NULL);
731 adirent.d_fileno = dp->d_ino;
732 adirent.d_namlen = dp->d_namlen;
733 memcpy(adirent.d_name, dp->d_name, dp->d_namlen + 1);
734 return (&adirent);
735 }
736
737 /*
738 * Return st_mode information in response to stat or lstat calls
739 */
740 static int
741 glob_stat(name, stp)
742 const char *name;
743 struct stat *stp;
744 {
745 register struct direct *dp;
746
747 dp = pathsearch(name);
748 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
749 (!vflag && dp->d_ino == WINO))
750 return (-1);
751 if (inodetype(dp->d_ino) == NODE)
752 stp->st_mode = S_IFDIR;
753 else
754 stp->st_mode = S_IFREG;
755 return (0);
756 }
757
758 /*
759 * Comparison routine for qsort.
760 */
761 static int
762 fcmp(f1, f2)
763 register const void *f1, *f2;
764 {
765 return (strcmp(((struct afile *)f1)->fname,
766 ((struct afile *)f2)->fname));
767 }
768
769 /*
770 * respond to interrupts
771 */
772 void
773 onintr(signo)
774 int signo;
775 {
776 if (command == 'i' && runshell)
777 longjmp(reset, 1);
778 if (reply("restore interrupted, continue") == FAIL)
779 exit(1);
780 }
781