interactive.c revision 1.13 1 /* $NetBSD: interactive.c,v 1.13 1997/09/16 13:44:13 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.5 (Berkeley) 5/1/95";
40 #else
41 __RCSID("$NetBSD: interactive.c,v 1.13 1997/09/16 13:44:13 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/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",
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 (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
545 continue;
546 if (!vflag && (dp->d_ino == WINO ||
547 strcmp(dp->d_name, ".") == 0 ||
548 strcmp(dp->d_name, "..") == 0))
549 continue;
550 locname[namelen] = '\0';
551 if (namelen + dp->d_namlen >= MAXPATHLEN) {
552 fprintf(stderr, "%s%s: name exceeds %d char\n",
553 locname, dp->d_name, MAXPATHLEN);
554 } else {
555 (void) strncat(locname, dp->d_name,
556 (int)dp->d_namlen);
557 mkentry(locname, dp, listp++);
558 entries++;
559 }
560 }
561 rst_closedir(dirp);
562 if (entries == 0) {
563 fprintf(stderr, "\n");
564 free(list);
565 return;
566 }
567 qsort((char *)list, entries, sizeof(struct afile), fcmp);
568 }
569 formatf(list, entries);
570 if (dirp != NULL) {
571 for (fp = listp - 1; fp >= list; fp--)
572 freename(fp->fname);
573 fprintf(stderr, "\n");
574 free(list);
575 }
576 }
577
578 /*
579 * Read the contents of a directory.
580 */
581 static void
582 mkentry(name, dp, fp)
583 char *name;
584 struct direct *dp;
585 struct afile *fp;
586 {
587 char *cp;
588 struct entry *np;
589
590 fp->fnum = dp->d_ino;
591 fp->fname = savename(dp->d_name);
592 for (cp = fp->fname; *cp; cp++)
593 if (!vflag && (*cp < ' ' || *cp >= 0177))
594 *cp = '?';
595 fp->len = cp - fp->fname;
596 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
597 fp->prefix = '^';
598 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
599 fp->prefix = '*';
600 else
601 fp->prefix = ' ';
602 switch(dp->d_type) {
603
604 default:
605 fprintf(stderr, "Warning: undefined file type %d\n",
606 dp->d_type);
607 /* fall through */
608 case DT_REG:
609 fp->postfix = ' ';
610 break;
611
612 case DT_LNK:
613 fp->postfix = '@';
614 break;
615
616 case DT_FIFO:
617 case DT_SOCK:
618 fp->postfix = '=';
619 break;
620
621 case DT_CHR:
622 case DT_BLK:
623 fp->postfix = '#';
624 break;
625
626 case DT_WHT:
627 fp->postfix = '%';
628 break;
629
630 case DT_UNKNOWN:
631 case DT_DIR:
632 if (inodetype(dp->d_ino) == NODE)
633 fp->postfix = '/';
634 else
635 fp->postfix = ' ';
636 break;
637 }
638 return;
639 }
640
641 /*
642 * Print out a pretty listing of a directory
643 */
644 static void
645 formatf(list, nentry)
646 struct afile *list;
647 int nentry;
648 {
649 struct afile *fp, *endlist;
650 int width, bigino, haveprefix, havepostfix;
651 int i, j, w, precision, columns, lines;
652
653 width = 0;
654 haveprefix = 0;
655 havepostfix = 0;
656 precision = 0;
657 bigino = ROOTINO;
658 endlist = &list[nentry];
659 for (fp = &list[0]; fp < endlist; fp++) {
660 if (bigino < fp->fnum)
661 bigino = fp->fnum;
662 if (width < fp->len)
663 width = fp->len;
664 if (fp->prefix != ' ')
665 haveprefix = 1;
666 if (fp->postfix != ' ')
667 havepostfix = 1;
668 }
669 if (haveprefix)
670 width++;
671 if (havepostfix)
672 width++;
673 if (vflag) {
674 for (precision = 0, i = bigino; i > 0; i /= 10)
675 precision++;
676 width += precision + 1;
677 }
678 width++;
679 columns = 81 / width;
680 if (columns == 0)
681 columns = 1;
682 lines = (nentry + columns - 1) / columns;
683 for (i = 0; i < lines; i++) {
684 for (j = 0; j < columns; j++) {
685 fp = &list[j * lines + i];
686 if (vflag) {
687 fprintf(stderr, "%*d ", precision, fp->fnum);
688 fp->len += precision + 1;
689 }
690 if (haveprefix) {
691 putc(fp->prefix, stderr);
692 fp->len++;
693 }
694 fprintf(stderr, "%s", fp->fname);
695 if (havepostfix) {
696 putc(fp->postfix, stderr);
697 fp->len++;
698 }
699 if (fp + lines >= endlist) {
700 fprintf(stderr, "\n");
701 break;
702 }
703 for (w = fp->len; w < width; w++)
704 putc(' ', stderr);
705 }
706 }
707 }
708
709 /*
710 * Skip over directory entries that are not on the tape
711 *
712 * First have to get definition of a dirent.
713 */
714 #undef DIRBLKSIZ
715 #include <dirent.h>
716 #undef d_ino
717
718 struct dirent *
719 glob_readdir(dirp)
720 RST_DIR *dirp;
721 {
722 struct direct *dp;
723 static struct dirent adirent;
724
725 while ((dp = rst_readdir(dirp)) != NULL) {
726 if (!vflag && dp->d_ino == WINO)
727 continue;
728 if (dflag || TSTINO(dp->d_ino, dumpmap))
729 break;
730 }
731 if (dp == NULL)
732 return (NULL);
733 adirent.d_fileno = dp->d_ino;
734 adirent.d_namlen = dp->d_namlen;
735 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
736 return (&adirent);
737 }
738
739 /*
740 * Return st_mode information in response to stat or lstat calls
741 */
742 static int
743 glob_stat(name, stp)
744 const char *name;
745 struct stat *stp;
746 {
747 struct direct *dp;
748
749 dp = pathsearch(name);
750 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
751 (!vflag && dp->d_ino == WINO))
752 return (-1);
753 if (inodetype(dp->d_ino) == NODE)
754 stp->st_mode = S_IFDIR;
755 else
756 stp->st_mode = S_IFREG;
757 return (0);
758 }
759
760 /*
761 * Comparison routine for qsort.
762 */
763 static int
764 fcmp(f1, f2)
765 const void *f1, *f2;
766 {
767 return (strcmp(((struct afile *)f1)->fname,
768 ((struct afile *)f2)->fname));
769 }
770
771 /*
772 * respond to interrupts
773 */
774 void
775 onintr(signo)
776 int signo;
777 {
778 if (command == 'i' && runshell)
779 longjmp(reset, 1);
780 if (reply("restore interrupted, continue") == FAIL)
781 exit(1);
782 }
783