2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
38 #include <sys/param.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ffs/fs.h>
45 #include <protocols/dumprestore.h>
56 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
59 * Things to handle interruptions.
63 static char *nextarg = NULL;
66 * Structure and routines associated with listing directories.
69 ino_t fnum; /* inode number of file */
70 char *fname; /* file name */
71 short len; /* name length */
72 char prefix; /* prefix character */
73 char postfix; /* postfix character */
76 int freeglob; /* glob structure needs to be freed */
77 int argcnt; /* next globbed argument to return */
78 glob_t glob; /* globbing information */
79 char *cmd; /* the current command */
82 static char *copynext __P((char *, char *));
83 static int fcmp __P((const void *, const void *));
84 static void formatf __P((struct afile *, int));
85 static void getcmd __P((char *, char *, char *, int, struct arglist *));
86 struct dirent *glob_readdir __P((RST_DIR *dirp));
87 static int glob_stat __P((const char *, struct stat *));
88 static void mkentry __P((char *, struct direct *, struct afile *));
89 static void printlist __P((char *, char *));
92 * Read and execute commands from the terminal.
97 register struct entry *np;
99 struct arglist arglist;
100 char curdir[MAXPATHLEN];
101 char name[MAXPATHLEN];
104 arglist.freeglob = 0;
106 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
107 arglist.glob.gl_opendir = (void *)rst_opendir;
108 arglist.glob.gl_readdir = (void *)glob_readdir;
109 arglist.glob.gl_closedir = (void *)rst_closedir;
110 arglist.glob.gl_lstat = glob_stat;
111 arglist.glob.gl_stat = glob_stat;
112 canon("/", curdir, sizeof(curdir));
114 if (setjmp(reset) != 0) {
115 if (arglist.freeglob != 0) {
116 arglist.freeglob = 0;
118 globfree(&arglist.glob);
124 getcmd(curdir, cmd, name, sizeof(name), &arglist);
127 * Add elements to the extraction list.
130 if (strncmp(cmd, "add", strlen(cmd)) != 0)
132 ino = dirlookup(name);
137 treescan(name, ino, addfile);
140 * Change working directory.
143 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
145 ino = dirlookup(name);
148 if (inodetype(ino) == LEAF) {
149 fprintf(stderr, "%s: not a directory\n", name);
152 (void) strcpy(curdir, name);
155 * Delete elements from the extraction list.
158 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
160 np = lookupname(name);
161 if (np == NULL || (np->e_flags & NEW) == 0) {
162 fprintf(stderr, "%s: not on extraction list\n", name);
165 treescan(name, np->e_ino, deletefile);
168 * Extract the requested list.
171 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
181 * List available commands.
184 if (strncmp(cmd, "help", strlen(cmd)) != 0)
187 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
188 "Available commands are:\n",
189 "\tls [arg] - list directory\n",
190 "\tcd arg - change directory\n",
191 "\tpwd - print current directory\n",
192 "\tadd [arg] - add `arg' to list of",
193 " files to be extracted\n",
194 "\tdelete [arg] - delete `arg' from",
195 " list of files to be extracted\n",
196 "\textract - extract requested files\n",
197 "\tsetmodes - set modes of requested directories\n",
198 "\tquit - immediately exit program\n",
199 "\twhat - list dump header information\n",
200 "\tverbose - toggle verbose flag",
201 " (useful with ``ls'')\n",
202 "\thelp or `?' - print this list\n",
203 "If no `arg' is supplied, the current",
204 " directory is used\n");
210 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
212 printlist(name, curdir);
215 * Print current directory.
218 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
220 if (curdir[1] == '\0')
221 fprintf(stderr, "/\n");
223 fprintf(stderr, "%s\n", &curdir[1]);
229 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
233 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
237 * Toggle verbose mode.
240 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
243 fprintf(stderr, "verbose mode off\n");
247 fprintf(stderr, "verbose mode on\n");
251 * Just restore requested directory modes.
254 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
259 * Print out dump header information.
262 if (strncmp(cmd, "what", strlen(cmd)) != 0)
270 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
273 fprintf(stderr, "debugging mode off\n");
277 fprintf(stderr, "debugging mode on\n");
285 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
292 * Read and parse an interactive command.
293 * The first word on the line is assigned to "cmd". If
294 * there are no arguments on the command line, then "curdir"
295 * is returned as the argument. If there are arguments
296 * on the line they are returned one at a time on each
297 * successive call to getcmd. Each argument is first assigned
298 * to "name". If it does not start with "/" the pathname in
299 * "curdir" is prepended to it. Finally "canon" is called to
300 * eliminate any embedded ".." components.
303 getcmd(curdir, cmd, name, size, ap)
304 char *curdir, *cmd, *name;
309 static char input[BUFSIZ];
311 # define rawname input /* save space by reusing input buffer */
314 * Check to see if still processing arguments.
321 * Read a command line and trim off trailing white space.
324 fprintf(stderr, "restore > ");
325 (void) fflush(stderr);
326 (void) fgets(input, BUFSIZ, terminal);
327 } while (!feof(terminal) && input[0] == '\n');
328 if (feof(terminal)) {
329 (void) strcpy(cmd, "quit");
332 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
333 /* trim off trailing white space and newline */;
336 * Copy the command into "cmd".
338 cp = copynext(input, cmd);
341 * If no argument, use curdir as the default.
344 (void) strcpy(name, curdir);
349 * Find the next argument.
352 cp = copynext(nextarg, rawname);
358 * If it is an absolute pathname, canonicalize it and return it.
360 if (rawname[0] == '/') {
361 canon(rawname, name, size);
364 * For relative pathnames, prepend the current directory to
365 * it then canonicalize and return it.
367 (void) strcpy(output, curdir);
368 (void) strcat(output, "/");
369 (void) strcat(output, rawname);
370 canon(output, name, size);
372 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
373 fprintf(stderr, "%s: out of memory\n", ap->cmd);
374 if (ap->glob.gl_pathc == 0)
377 ap->argcnt = ap->glob.gl_pathc;
380 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
381 if (--ap->argcnt == 0) {
389 * Strip off the next token of the input.
392 copynext(input, output)
393 char *input, *output;
395 register char *cp, *bp;
398 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
399 /* skip to argument */;
401 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
403 * Handle back slashes.
408 "command lines cannot be continued\n");
415 * The usual unquoted case.
417 if (*cp != '\'' && *cp != '"') {
422 * Handle single and double quotes.
425 while (*cp != quote && *cp != '\0')
426 *bp++ = *cp++ | 0200;
428 fprintf(stderr, "missing %c\n", quote);
438 * Canonicalize file names to always start with ``./'' and
439 * remove any imbedded "." and ".." components.
442 canon(rawname, canonname, len)
443 char *rawname, *canonname;
446 register char *cp, *np;
448 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
449 (void) strcpy(canonname, "");
450 else if (rawname[0] == '/')
451 (void) strcpy(canonname, ".");
453 (void) strcpy(canonname, "./");
454 if (strlen(canonname) + strlen(rawname) >= len) {
455 fprintf(stderr, "canonname: not enough bufferspace\n");
459 (void) strcat(canonname, rawname);
461 * Eliminate multiple and trailing '/'s
463 for (cp = np = canonname; *np != '\0'; cp++) {
465 while (*cp == '/' && *np == '/')
472 * Eliminate extraneous "." and ".." from pathnames.
474 for (np = canonname; *np != '\0'; ) {
477 while (*np != '/' && *np != '\0')
479 if (np - cp == 1 && *cp == '.') {
481 (void) strcpy(cp, np);
484 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
486 while (cp > &canonname[1] && *--cp != '/')
487 /* find beginning of name */;
488 (void) strcpy(cp, np);
495 * Do an "ls" style listing of a directory
498 printlist(name, basename)
502 register struct afile *fp, *list, *listp;
503 register struct direct *dp;
506 int entries, len, namelen;
507 char locname[MAXPATHLEN + 1];
509 dp = pathsearch(name);
510 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
511 (!vflag && dp->d_ino == WINO))
513 if ((dirp = rst_opendir(name)) == NULL) {
516 mkentry(name, dp, list);
517 len = strlen(basename) + 1;
518 if (strlen(name) - len > single.len) {
519 freename(single.fname);
520 single.fname = savename(&name[len]);
521 single.len = strlen(single.fname);
525 while (dp = rst_readdir(dirp))
528 list = (struct afile *)malloc(entries * sizeof(struct afile));
530 fprintf(stderr, "ls: out of memory\n");
533 if ((dirp = rst_opendir(name)) == NULL)
534 panic("directory reopen failed\n");
535 fprintf(stderr, "%s:\n", name);
538 (void) strncpy(locname, name, MAXPATHLEN);
539 (void) strncat(locname, "/", MAXPATHLEN);
540 namelen = strlen(locname);
541 while (dp = rst_readdir(dirp)) {
544 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
546 if (!vflag && (dp->d_ino == WINO ||
547 strcmp(dp->d_name, ".") == 0 ||
548 strcmp(dp->d_name, "..") == 0))
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);
555 (void) strncat(locname, dp->d_name,
557 mkentry(locname, dp, listp++);
563 fprintf(stderr, "\n");
567 qsort((char *)list, entries, sizeof(struct afile), fcmp);
569 formatf(list, entries);
571 for (fp = listp - 1; fp >= list; fp--)
573 fprintf(stderr, "\n");
579 * Read the contents of a directory.
582 mkentry(name, dp, fp)
585 register struct afile *fp;
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))
595 fp->len = cp - fp->fname;
596 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
598 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
605 fprintf(stderr, "Warning: undefined file type %d\n",
632 if (inodetype(dp->d_ino) == NODE)
642 * Print out a pretty listing of a directory
645 formatf(list, nentry)
646 register struct afile *list;
649 register struct afile *fp, *endlist;
650 int width, bigino, haveprefix, havepostfix;
651 int i, j, w, precision, columns, lines;
657 endlist = &list[nentry];
658 for (fp = &list[0]; fp < endlist; fp++) {
659 if (bigino < fp->fnum)
663 if (fp->prefix != ' ')
665 if (fp->postfix != ' ')
673 for (precision = 0, i = bigino; i > 0; i /= 10)
675 width += precision + 1;
678 columns = 81 / width;
681 lines = (nentry + columns - 1) / columns;
682 for (i = 0; i < lines; i++) {
683 for (j = 0; j < columns; j++) {
684 fp = &list[j * lines + i];
686 fprintf(stderr, "%*d ", precision, fp->fnum);
687 fp->len += precision + 1;
690 putc(fp->prefix, stderr);
693 fprintf(stderr, "%s", fp->fname);
695 putc(fp->postfix, stderr);
698 if (fp + lines >= endlist) {
699 fprintf(stderr, "\n");
702 for (w = fp->len; w < width; w++)
709 * Skip over directory entries that are not on the tape
711 * First have to get definition of a dirent.
722 static struct dirent adirent;
724 while ((dp = rst_readdir(dirp)) != NULL) {
725 if (!vflag && dp->d_ino == WINO)
727 if (dflag || TSTINO(dp->d_ino, dumpmap))
732 adirent.d_fileno = dp->d_ino;
733 adirent.d_namlen = dp->d_namlen;
734 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
739 * Return st_mode information in response to stat or lstat calls
746 register struct direct *dp;
748 dp = pathsearch(name);
749 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
750 (!vflag && dp->d_ino == WINO))
752 if (inodetype(dp->d_ino) == NODE)
753 stp->st_mode = IFDIR;
755 stp->st_mode = IFREG;
760 * Comparison routine for qsort.
764 register const void *f1, *f2;
766 return (strcmp(((struct afile *)f1)->fname,
767 ((struct afile *)f2)->fname));
771 * respond to interrupts
777 if (command == 'i' && runshell)
779 if (reply("restore interrupted, continue") == FAIL)