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
36 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
38 static const char rcsid[] =
42 #include <sys/param.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <protocols/dumprestore.h>
59 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
62 * Things to handle interruptions.
66 static char *nextarg = NULL;
69 * Structure and routines associated with listing directories.
72 ino_t fnum; /* inode number of file */
73 char *fname; /* file name */
74 short len; /* name length */
75 char prefix; /* prefix character */
76 char postfix; /* postfix character */
79 int freeglob; /* glob structure needs to be freed */
80 int argcnt; /* next globbed argument to return */
81 glob_t glob; /* globbing information */
82 char *cmd; /* the current command */
85 static char *copynext(char *, char *);
86 static int fcmp(const void *, const void *);
87 static void formatf(struct afile *, int);
88 static void getcmd(char *, char *, char *, int, struct arglist *);
89 struct dirent *glob_readdir(RST_DIR *dirp);
90 static int glob_stat(const char *, struct stat *);
91 static void mkentry(char *, struct direct *, struct afile *);
92 static void printlist(char *, char *);
95 * Read and execute commands from the terminal.
102 struct arglist arglist;
103 char curdir[MAXPATHLEN];
104 char name[MAXPATHLEN];
107 arglist.freeglob = 0;
109 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
110 arglist.glob.gl_opendir = (void *)rst_opendir;
111 arglist.glob.gl_readdir = (void *)glob_readdir;
112 arglist.glob.gl_closedir = (void *)rst_closedir;
113 arglist.glob.gl_lstat = glob_stat;
114 arglist.glob.gl_stat = glob_stat;
115 canon("/", curdir, sizeof(curdir));
117 if (setjmp(reset) != 0) {
118 if (arglist.freeglob != 0) {
119 arglist.freeglob = 0;
121 globfree(&arglist.glob);
127 getcmd(curdir, cmd, name, sizeof(name), &arglist);
130 * Add elements to the extraction list.
133 if (strncmp(cmd, "add", strlen(cmd)) != 0)
135 ino = dirlookup(name);
140 treescan(name, ino, addfile);
143 * Change working directory.
146 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
148 ino = dirlookup(name);
151 if (inodetype(ino) == LEAF) {
152 fprintf(stderr, "%s: not a directory\n", name);
155 (void) strcpy(curdir, name);
158 * Delete elements from the extraction list.
161 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
163 np = lookupname(name);
164 if (np == NULL || (np->e_flags & NEW) == 0) {
165 fprintf(stderr, "%s: not on extraction list\n", name);
168 treescan(name, np->e_ino, deletefile);
171 * Extract the requested list.
174 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
184 * List available commands.
187 if (strncmp(cmd, "help", strlen(cmd)) != 0)
190 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
191 "Available commands are:\n",
192 "\tls [arg] - list directory\n",
193 "\tcd arg - change directory\n",
194 "\tpwd - print current directory\n",
195 "\tadd [arg] - add `arg' to list of",
196 " files to be extracted\n",
197 "\tdelete [arg] - delete `arg' from",
198 " list of files to be extracted\n",
199 "\textract - extract requested files\n",
200 "\tsetmodes - set modes of requested directories\n",
201 "\tquit - immediately exit program\n",
202 "\twhat - list dump header information\n",
203 "\tverbose - toggle verbose flag",
204 " (useful with ``ls'')\n",
205 "\thelp or `?' - print this list\n",
206 "If no `arg' is supplied, the current",
207 " directory is used\n");
213 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
215 printlist(name, curdir);
218 * Print current directory.
221 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
223 if (curdir[1] == '\0')
224 fprintf(stderr, "/\n");
226 fprintf(stderr, "%s\n", &curdir[1]);
232 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
236 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
240 * Toggle verbose mode.
243 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
246 fprintf(stderr, "verbose mode off\n");
250 fprintf(stderr, "verbose mode on\n");
254 * Just restore requested directory modes.
257 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
262 * Print out dump header information.
265 if (strncmp(cmd, "what", strlen(cmd)) != 0)
273 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
276 fprintf(stderr, "debugging mode off\n");
280 fprintf(stderr, "debugging mode on\n");
288 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
295 * Read and parse an interactive command.
296 * The first word on the line is assigned to "cmd". If
297 * there are no arguments on the command line, then "curdir"
298 * is returned as the argument. If there are arguments
299 * on the line they are returned one at a time on each
300 * successive call to getcmd. Each argument is first assigned
301 * to "name". If it does not start with "/" the pathname in
302 * "curdir" is prepended to it. Finally "canon" is called to
303 * eliminate any embedded ".." components.
306 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
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 if (fgets(input, BUFSIZ, terminal) == NULL) {
330 } while (input[0] == '\n');
331 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
332 /* trim off trailing white space and newline */;
335 * Copy the command into "cmd".
337 cp = copynext(input, cmd);
340 * If no argument, use curdir as the default.
343 (void) strncpy(name, curdir, size);
344 name[size - 1] = '\0';
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 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
368 canon(output, name, size);
370 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
371 fprintf(stderr, "%s: out of memory\n", ap->cmd);
372 if (ap->glob.gl_pathc == 0)
375 ap->argcnt = ap->glob.gl_pathc;
378 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
379 name[size - 1] = '\0';
380 if (--ap->argcnt == 0) {
388 * Strip off the next token of the input.
391 copynext(char *input, char *output)
396 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
397 /* skip to argument */;
399 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
401 * Handle back slashes.
406 "command lines cannot be continued\n");
413 * The usual unquoted case.
415 if (*cp != '\'' && *cp != '"') {
420 * Handle single and double quotes.
423 while (*cp != quote && *cp != '\0')
424 *bp++ = *cp++ | 0200;
426 fprintf(stderr, "missing %c\n", quote);
436 * Canonicalize file names to always start with ``./'' and
437 * remove any embedded "." and ".." components.
440 canon(char *rawname, char *canonname, int len)
444 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
445 (void) strcpy(canonname, "");
446 else if (rawname[0] == '/')
447 (void) strcpy(canonname, ".");
449 (void) strcpy(canonname, "./");
450 if (strlen(canonname) + strlen(rawname) >= len) {
451 fprintf(stderr, "canonname: not enough buffer space\n");
455 (void) strcat(canonname, rawname);
457 * Eliminate multiple and trailing '/'s
459 for (cp = np = canonname; *np != '\0'; cp++) {
461 while (*cp == '/' && *np == '/')
468 * Eliminate extraneous "." and ".." from pathnames.
470 for (np = canonname; *np != '\0'; ) {
473 while (*np != '/' && *np != '\0')
475 if (np - cp == 1 && *cp == '.') {
477 (void) strcpy(cp, np);
480 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
482 while (cp > &canonname[1] && *--cp != '/')
483 /* find beginning of name */;
484 (void) strcpy(cp, np);
491 * Do an "ls" style listing of a directory
494 printlist(char *name, char *basename)
496 struct afile *fp, *list, *listp;
500 int entries, len, namelen;
501 char locname[MAXPATHLEN + 1];
503 dp = pathsearch(name);
504 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
505 (!vflag && dp->d_ino == WINO))
507 if ((dirp = rst_opendir(name)) == NULL) {
510 mkentry(name, dp, list);
511 len = strlen(basename) + 1;
512 if (strlen(name) - len > single.len) {
513 freename(single.fname);
514 single.fname = savename(&name[len]);
515 single.len = strlen(single.fname);
519 while ((dp = rst_readdir(dirp)))
522 list = (struct afile *)malloc(entries * sizeof(struct afile));
524 fprintf(stderr, "ls: out of memory\n");
527 if ((dirp = rst_opendir(name)) == NULL)
528 panic("directory reopen failed\n");
529 fprintf(stderr, "%s:\n", name);
532 (void) strncpy(locname, name, MAXPATHLEN);
533 (void) strncat(locname, "/", MAXPATHLEN);
534 namelen = strlen(locname);
535 while ((dp = rst_readdir(dirp))) {
538 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
540 if (!vflag && (dp->d_ino == WINO ||
541 strcmp(dp->d_name, ".") == 0 ||
542 strcmp(dp->d_name, "..") == 0))
544 locname[namelen] = '\0';
545 if (namelen + dp->d_namlen >= MAXPATHLEN) {
546 fprintf(stderr, "%s%s: name exceeds %d char\n",
547 locname, dp->d_name, MAXPATHLEN);
549 (void) strncat(locname, dp->d_name,
551 mkentry(locname, dp, listp++);
557 fprintf(stderr, "\n");
561 qsort((char *)list, entries, sizeof(struct afile), fcmp);
563 formatf(list, entries);
565 for (fp = listp - 1; fp >= list; fp--)
567 fprintf(stderr, "\n");
573 * Read the contents of a directory.
576 mkentry(char *name, struct direct *dp, struct afile *fp)
581 fp->fnum = dp->d_ino;
582 fp->fname = savename(dp->d_name);
583 for (cp = fp->fname; *cp; cp++)
584 if (!vflag && (*cp < ' ' || *cp >= 0177))
586 fp->len = cp - fp->fname;
587 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
589 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
596 fprintf(stderr, "Warning: undefined file type %d\n",
623 if (inodetype(dp->d_ino) == NODE)
633 * Print out a pretty listing of a directory
636 formatf(struct afile *list, int nentry)
638 struct afile *fp, *endlist;
639 int width, bigino, haveprefix, havepostfix;
640 int i, j, w, precision, columns, lines;
646 endlist = &list[nentry];
647 for (fp = &list[0]; fp < endlist; fp++) {
648 if (bigino < fp->fnum)
652 if (fp->prefix != ' ')
654 if (fp->postfix != ' ')
662 for (precision = 0, i = bigino; i > 0; i /= 10)
664 width += precision + 1;
667 columns = 81 / width;
670 lines = (nentry + columns - 1) / columns;
671 for (i = 0; i < lines; i++) {
672 for (j = 0; j < columns; j++) {
673 fp = &list[j * lines + i];
675 fprintf(stderr, "%*d ", precision, fp->fnum);
676 fp->len += precision + 1;
679 putc(fp->prefix, stderr);
682 fprintf(stderr, "%s", fp->fname);
684 putc(fp->postfix, stderr);
687 if (fp + lines >= endlist) {
688 fprintf(stderr, "\n");
691 for (w = fp->len; w < width; w++)
698 * Skip over directory entries that are not on the tape
700 * First have to get definition of a dirent.
707 glob_readdir(RST_DIR *dirp)
710 static struct dirent adirent;
712 while ((dp = rst_readdir(dirp)) != NULL) {
713 if (!vflag && dp->d_ino == WINO)
715 if (dflag || TSTINO(dp->d_ino, dumpmap))
720 adirent.d_fileno = dp->d_ino;
721 adirent.d_namlen = dp->d_namlen;
722 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
727 * Return st_mode information in response to stat or lstat calls
730 glob_stat(const char *name, struct stat *stp)
734 dp = pathsearch(name);
735 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
736 (!vflag && dp->d_ino == WINO))
738 if (inodetype(dp->d_ino) == NODE)
739 stp->st_mode = IFDIR;
741 stp->st_mode = IFREG;
746 * Comparison routine for qsort.
749 fcmp(const void *f1, const void *f2)
751 return (strcmp(((struct afile *)f1)->fname,
752 ((struct afile *)f2)->fname));
756 * respond to interrupts
761 if (command == 'i' && runshell)
763 if (reply("restore interrupted, continue") == FAIL)