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