interactive.c revision 1.14 1 /* $NetBSD: interactive.c,v 1.14 1998/01/10 08:27:54 enami 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.5 (Berkeley) 5/1/95";
40 #else
41 __RCSID("$NetBSD: interactive.c,v 1.14 1998/01/10 08:27:54 enami 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/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.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%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 or xit - 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 "\tDebug - turn on debugging\n",
211 "If no `arg' is supplied, the current",
212 " directory is used\n");
213 break;
214 /*
215 * List a directory.
216 */
217 case 'l':
218 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
219 goto bad;
220 printlist(name, curdir);
221 break;
222 /*
223 * Print current directory.
224 */
225 case 'p':
226 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
227 goto bad;
228 if (curdir[1] == '\0')
229 fprintf(stderr, "/\n");
230 else
231 fprintf(stderr, "%s\n", &curdir[1]);
232 break;
233 /*
234 * Quit.
235 */
236 case 'q':
237 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
238 goto bad;
239 return;
240 case 'x':
241 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
242 goto bad;
243 return;
244 /*
245 * Toggle verbose mode.
246 */
247 case 'v':
248 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
249 goto bad;
250 if (vflag) {
251 fprintf(stderr, "verbose mode off\n");
252 vflag = 0;
253 break;
254 }
255 fprintf(stderr, "verbose mode on\n");
256 vflag++;
257 break;
258 /*
259 * Just restore requested directory modes.
260 */
261 case 's':
262 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
263 goto bad;
264 setdirmodes(FORCE);
265 break;
266 /*
267 * Print out dump header information.
268 */
269 case 'w':
270 if (strncmp(cmd, "what", strlen(cmd)) != 0)
271 goto bad;
272 printdumpinfo();
273 break;
274 /*
275 * Turn on debugging.
276 */
277 case 'D':
278 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
279 goto bad;
280 if (dflag) {
281 fprintf(stderr, "debugging mode off\n");
282 dflag = 0;
283 break;
284 }
285 fprintf(stderr, "debugging mode on\n");
286 dflag++;
287 break;
288 /*
289 * Unknown command.
290 */
291 default:
292 bad:
293 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
294 break;
295 }
296 goto loop;
297 }
298
299 /*
300 * Read and parse an interactive command.
301 * The first word on the line is assigned to "cmd". If
302 * there are no arguments on the command line, then "curdir"
303 * is returned as the argument. If there are arguments
304 * on the line they are returned one at a time on each
305 * successive call to getcmd. Each argument is first assigned
306 * to "name". If it does not start with "/" the pathname in
307 * "curdir" is prepended to it. Finally "canon" is called to
308 * eliminate any embedded ".." components.
309 */
310 static void
311 getcmd(curdir, cmd, name, ap)
312 char *curdir, *cmd, *name;
313 struct arglist *ap;
314 {
315 extern char *__progname; /* from crt0.o */
316 char *cp;
317 static char input[BUFSIZ];
318 char output[BUFSIZ];
319 # define rawname input /* save space by reusing input buffer */
320
321 /*
322 * Check to see if still processing arguments.
323 */
324 if (ap->argcnt > 0)
325 goto retnext;
326 if (nextarg != NULL)
327 goto getnext;
328 /*
329 * Read a command line and trim off trailing white space.
330 */
331 do {
332 fprintf(stderr, "%s > ", __progname);
333 (void) fflush(stderr);
334 (void) fgets(input, BUFSIZ, terminal);
335 } while (!feof(terminal) && input[0] == '\n');
336 if (feof(terminal)) {
337 (void) strcpy(cmd, "quit");
338 return;
339 }
340 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
341 /* trim off trailing white space and newline */;
342 *++cp = '\0';
343 /*
344 * Copy the command into "cmd".
345 */
346 cp = copynext(input, cmd);
347 ap->cmd = cmd;
348 /*
349 * If no argument, use curdir as the default.
350 */
351 if (*cp == '\0') {
352 (void) strcpy(name, curdir);
353 return;
354 }
355 nextarg = cp;
356 /*
357 * Find the next argument.
358 */
359 getnext:
360 cp = copynext(nextarg, rawname);
361 if (*cp == '\0')
362 nextarg = NULL;
363 else
364 nextarg = cp;
365 /*
366 * If it is an absolute pathname, canonicalize it and return it.
367 */
368 if (rawname[0] == '/') {
369 canon(rawname, name);
370 } else {
371 /*
372 * For relative pathnames, prepend the current directory to
373 * it then canonicalize and return it.
374 */
375 (void) strcpy(output, curdir);
376 (void) strcat(output, "/");
377 (void) strcat(output, rawname);
378 canon(output, name);
379 }
380 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
381 fprintf(stderr, "%s: out of memory\n", ap->cmd);
382 if (ap->glob.gl_pathc == 0)
383 return;
384 ap->freeglob = 1;
385 ap->argcnt = ap->glob.gl_pathc;
386
387 retnext:
388 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
389 if (--ap->argcnt == 0) {
390 ap->freeglob = 0;
391 globfree(&ap->glob);
392 }
393 # undef rawname
394 }
395
396 /*
397 * Strip off the next token of the input.
398 */
399 static char *
400 copynext(input, output)
401 char *input, *output;
402 {
403 char *cp, *bp;
404 char quote;
405
406 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
407 /* skip to argument */;
408 bp = output;
409 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
410 /*
411 * Handle back slashes.
412 */
413 if (*cp == '\\') {
414 if (*++cp == '\0') {
415 fprintf(stderr,
416 "command lines cannot be continued\n");
417 continue;
418 }
419 *bp++ = *cp++;
420 continue;
421 }
422 /*
423 * The usual unquoted case.
424 */
425 if (*cp != '\'' && *cp != '"') {
426 *bp++ = *cp++;
427 continue;
428 }
429 /*
430 * Handle single and double quotes.
431 */
432 quote = *cp++;
433 while (*cp != quote && *cp != '\0')
434 *bp++ = *cp++ | 0200;
435 if (*cp++ == '\0') {
436 fprintf(stderr, "missing %c\n", quote);
437 cp--;
438 continue;
439 }
440 }
441 *bp = '\0';
442 return (cp);
443 }
444
445 /*
446 * Canonicalize file names to always start with ``./'' and
447 * remove any imbedded "." and ".." components.
448 */
449 void
450 canon(rawname, canonname)
451 char *rawname, *canonname;
452 {
453 char *cp, *np;
454
455 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
456 (void) strcpy(canonname, "");
457 else if (rawname[0] == '/')
458 (void) strcpy(canonname, ".");
459 else
460 (void) strcpy(canonname, "./");
461 (void) strcat(canonname, rawname);
462 /*
463 * Eliminate multiple and trailing '/'s
464 */
465 for (cp = np = canonname; *np != '\0'; cp++) {
466 *cp = *np++;
467 while (*cp == '/' && *np == '/')
468 np++;
469 }
470 *cp = '\0';
471 if (*--cp == '/')
472 *cp = '\0';
473 /*
474 * Eliminate extraneous "." and ".." from pathnames.
475 */
476 for (np = canonname; *np != '\0'; ) {
477 np++;
478 cp = np;
479 while (*np != '/' && *np != '\0')
480 np++;
481 if (np - cp == 1 && *cp == '.') {
482 cp--;
483 (void) strcpy(cp, np);
484 np = cp;
485 }
486 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
487 cp--;
488 while (cp > &canonname[1] && *--cp != '/')
489 /* find beginning of name */;
490 (void) strcpy(cp, np);
491 np = cp;
492 }
493 }
494 }
495
496 /*
497 * Do an "ls" style listing of a directory
498 */
499 static void
500 printlist(name, basename)
501 char *name;
502 char *basename;
503 {
504 struct afile *fp, *list, *listp;
505 struct direct *dp;
506 struct afile single;
507 RST_DIR *dirp;
508 int entries, len, namelen;
509 char locname[MAXPATHLEN + 1];
510
511 dp = pathsearch(name);
512 listp = NULL;
513 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
514 (!vflag && dp->d_ino == WINO))
515 return;
516 if ((dirp = rst_opendir(name)) == NULL) {
517 entries = 1;
518 list = &single;
519 mkentry(name, dp, list);
520 len = strlen(basename) + 1;
521 if (strlen(name) - len > single.len) {
522 freename(single.fname);
523 single.fname = savename(&name[len]);
524 single.len = strlen(single.fname);
525 }
526 } else {
527 entries = 0;
528 while ((dp = rst_readdir(dirp)) != NULL)
529 entries++;
530 rst_closedir(dirp);
531 list = (struct afile *)malloc(entries * sizeof(struct afile));
532 if (list == NULL) {
533 fprintf(stderr, "ls: out of memory\n");
534 return;
535 }
536 if ((dirp = rst_opendir(name)) == NULL)
537 panic("directory reopen failed\n");
538 fprintf(stderr, "%s:\n", name);
539 entries = 0;
540 listp = list;
541 (void) strncpy(locname, name, MAXPATHLEN);
542 (void) strncat(locname, "/", MAXPATHLEN);
543 namelen = strlen(locname);
544 while ((dp = rst_readdir(dirp)) != NULL) {
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 memmove(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