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[] =
39 "$FreeBSD: src/sbin/restore/interactive.c,v 1.8.2.1 2001/01/03 14:36:08 iedowse Exp $";
42 #include <sys/param.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <protocols/dumprestore.h>
58 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
61 * Things to handle interruptions.
65 static char *nextarg = NULL;
68 * Structure and routines associated with listing directories.
71 ino_t fnum; /* inode number of file */
72 char *fname; /* file name */
73 short len; /* name length */
74 char prefix; /* prefix character */
75 char postfix; /* postfix character */
78 int freeglob; /* glob structure needs to be freed */
79 int argcnt; /* next globbed argument to return */
80 glob_t glob; /* globbing information */
81 char *cmd; /* the current command */
84 static char *copynext __P((char *, char *));
85 static int fcmp __P((const void *, const void *));
86 static void formatf __P((struct afile *, int));
87 static void getcmd __P((char *, char *, char *, int, struct arglist *));
88 struct dirent *glob_readdir __P((RST_DIR *dirp));
89 static int glob_stat __P((const char *, struct stat *));
90 static void mkentry __P((char *, struct direct *, struct afile *));
91 static void printlist __P((char *, char *));
94 * Read and execute commands from the terminal.
99 register struct entry *np;
101 struct arglist arglist;
102 char curdir[MAXPATHLEN];
103 char name[MAXPATHLEN];
106 arglist.freeglob = 0;
108 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
109 arglist.glob.gl_opendir = (void *)rst_opendir;
110 arglist.glob.gl_readdir = (void *)glob_readdir;
111 arglist.glob.gl_closedir = (void *)rst_closedir;
112 arglist.glob.gl_lstat = glob_stat;
113 arglist.glob.gl_stat = glob_stat;
114 canon("/", curdir, sizeof(curdir));
116 if (setjmp(reset) != 0) {
117 if (arglist.freeglob != 0) {
118 arglist.freeglob = 0;
120 globfree(&arglist.glob);
126 getcmd(curdir, cmd, name, sizeof(name), &arglist);
129 * Add elements to the extraction list.
132 if (strncmp(cmd, "add", strlen(cmd)) != 0)
134 ino = dirlookup(name);
139 treescan(name, ino, addfile);
142 * Change working directory.
145 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
147 ino = dirlookup(name);
150 if (inodetype(ino) == LEAF) {
151 fprintf(stderr, "%s: not a directory\n", name);
154 (void) strcpy(curdir, name);
157 * Delete elements from the extraction list.
160 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
162 np = lookupname(name);
163 if (np == NULL || (np->e_flags & NEW) == 0) {
164 fprintf(stderr, "%s: not on extraction list\n", name);
167 treescan(name, np->e_ino, deletefile);
170 * Extract the requested list.
173 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
183 * List available commands.
186 if (strncmp(cmd, "help", strlen(cmd)) != 0)
189 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
190 "Available commands are:\n",
191 "\tls [arg] - list directory\n",
192 "\tcd arg - change directory\n",
193 "\tpwd - print current directory\n",
194 "\tadd [arg] - add `arg' to list of",
195 " files to be extracted\n",
196 "\tdelete [arg] - delete `arg' from",
197 " list of files to be extracted\n",
198 "\textract - extract requested files\n",
199 "\tsetmodes - set modes of requested directories\n",
200 "\tquit - immediately exit program\n",
201 "\twhat - list dump header information\n",
202 "\tverbose - toggle verbose flag",
203 " (useful with ``ls'')\n",
204 "\thelp or `?' - print this list\n",
205 "If no `arg' is supplied, the current",
206 " directory is used\n");
212 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
214 printlist(name, curdir);
217 * Print current directory.
220 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
222 if (curdir[1] == '\0')
223 fprintf(stderr, "/\n");
225 fprintf(stderr, "%s\n", &curdir[1]);
231 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
235 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
239 * Toggle verbose mode.
242 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
245 fprintf(stderr, "verbose mode off\n");
249 fprintf(stderr, "verbose mode on\n");
253 * Just restore requested directory modes.
256 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
261 * Print out dump header information.
264 if (strncmp(cmd, "what", strlen(cmd)) != 0)
272 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
275 fprintf(stderr, "debugging mode off\n");
279 fprintf(stderr, "debugging mode on\n");
287 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
294 * Read and parse an interactive command.
295 * The first word on the line is assigned to "cmd". If
296 * there are no arguments on the command line, then "curdir"
297 * is returned as the argument. If there are arguments
298 * on the line they are returned one at a time on each
299 * successive call to getcmd. Each argument is first assigned
300 * to "name". If it does not start with "/" the pathname in
301 * "curdir" is prepended to it. Finally "canon" is called to
302 * eliminate any embedded ".." components.
305 getcmd(curdir, cmd, name, size, ap)
306 char *curdir, *cmd, *name;
311 static char input[BUFSIZ];
313 # define rawname input /* save space by reusing input buffer */
316 * Check to see if still processing arguments.
323 * Read a command line and trim off trailing white space.
326 fprintf(stderr, "restore > ");
327 (void) fflush(stderr);
328 if (fgets(input, BUFSIZ, terminal) == NULL) {
332 } while (input[0] == '\n');
333 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
334 /* trim off trailing white space and newline */;
337 * Copy the command into "cmd".
339 cp = copynext(input, cmd);
342 * If no argument, use curdir as the default.
345 (void) strncpy(name, curdir, size);
346 name[size - 1] = '\0';
351 * Find the next argument.
354 cp = copynext(nextarg, rawname);
360 * If it is an absolute pathname, canonicalize it and return it.
362 if (rawname[0] == '/') {
363 canon(rawname, name, size);
366 * For relative pathnames, prepend the current directory to
367 * it then canonicalize and return it.
369 snprintf(output, sizeof(output), "%s/%s", curdir, 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 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
381 name[size - 1] = '\0';
382 if (--ap->argcnt == 0) {
390 * Strip off the next token of the input.
393 copynext(input, output)
394 char *input, *output;
396 register char *cp, *bp;
399 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
400 /* skip to argument */;
402 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
404 * Handle back slashes.
409 "command lines cannot be continued\n");
416 * The usual unquoted case.
418 if (*cp != '\'' && *cp != '"') {
423 * Handle single and double quotes.
426 while (*cp != quote && *cp != '\0')
427 *bp++ = *cp++ | 0200;
429 fprintf(stderr, "missing %c\n", quote);
439 * Canonicalize file names to always start with ``./'' and
440 * remove any embedded "." and ".." components.
443 canon(rawname, canonname, len)
444 char *rawname, *canonname;
447 register char *cp, *np;
449 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
450 (void) strcpy(canonname, "");
451 else if (rawname[0] == '/')
452 (void) strcpy(canonname, ".");
454 (void) strcpy(canonname, "./");
455 if (strlen(canonname) + strlen(rawname) >= len) {
456 fprintf(stderr, "canonname: not enough buffer space\n");
460 (void) strcat(canonname, rawname);
462 * Eliminate multiple and trailing '/'s
464 for (cp = np = canonname; *np != '\0'; cp++) {
466 while (*cp == '/' && *np == '/')
473 * Eliminate extraneous "." and ".." from pathnames.
475 for (np = canonname; *np != '\0'; ) {
478 while (*np != '/' && *np != '\0')
480 if (np - cp == 1 && *cp == '.') {
482 (void) strcpy(cp, np);
485 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
487 while (cp > &canonname[1] && *--cp != '/')
488 /* find beginning of name */;
489 (void) strcpy(cp, np);
496 * Do an "ls" style listing of a directory
499 printlist(name, basename)
503 register struct afile *fp, *list, *listp;
504 register struct direct *dp;
507 int entries, len, namelen;
508 char locname[MAXPATHLEN + 1];
510 dp = pathsearch(name);
511 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
512 (!vflag && dp->d_ino == WINO))
514 if ((dirp = rst_opendir(name)) == NULL) {
517 mkentry(name, dp, list);
518 len = strlen(basename) + 1;
519 if (strlen(name) - len > single.len) {
520 freename(single.fname);
521 single.fname = savename(&name[len]);
522 single.len = strlen(single.fname);
526 while ((dp = rst_readdir(dirp)))
529 list = (struct afile *)malloc(entries * sizeof(struct afile));
531 fprintf(stderr, "ls: out of memory\n");
534 if ((dirp = rst_opendir(name)) == NULL)
535 panic("directory reopen failed\n");
536 fprintf(stderr, "%s:\n", name);
539 (void) strncpy(locname, name, MAXPATHLEN);
540 (void) strncat(locname, "/", MAXPATHLEN);
541 namelen = strlen(locname);
542 while ((dp = rst_readdir(dirp))) {
545 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
547 if (!vflag && (dp->d_ino == WINO ||
548 strcmp(dp->d_name, ".") == 0 ||
549 strcmp(dp->d_name, "..") == 0))
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);
556 (void) strncat(locname, dp->d_name,
558 mkentry(locname, dp, listp++);
564 fprintf(stderr, "\n");
568 qsort((char *)list, entries, sizeof(struct afile), fcmp);
570 formatf(list, entries);
572 for (fp = listp - 1; fp >= list; fp--)
574 fprintf(stderr, "\n");
580 * Read the contents of a directory.
583 mkentry(name, dp, fp)
586 register struct afile *fp;
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))
596 fp->len = cp - fp->fname;
597 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
599 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
606 fprintf(stderr, "Warning: undefined file type %d\n",
633 if (inodetype(dp->d_ino) == NODE)
643 * Print out a pretty listing of a directory
646 formatf(list, nentry)
647 register struct afile *list;
650 register struct afile *fp, *endlist;
651 int width, bigino, haveprefix, havepostfix;
652 int i, j, w, precision, columns, lines;
658 endlist = &list[nentry];
659 for (fp = &list[0]; fp < endlist; fp++) {
660 if (bigino < fp->fnum)
664 if (fp->prefix != ' ')
666 if (fp->postfix != ' ')
674 for (precision = 0, i = bigino; i > 0; i /= 10)
676 width += precision + 1;
679 columns = 81 / width;
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];
687 fprintf(stderr, "%*d ", precision, fp->fnum);
688 fp->len += precision + 1;
691 putc(fp->prefix, stderr);
694 fprintf(stderr, "%s", fp->fname);
696 putc(fp->postfix, stderr);
699 if (fp + lines >= endlist) {
700 fprintf(stderr, "\n");
703 for (w = fp->len; w < width; w++)
710 * Skip over directory entries that are not on the tape
712 * First have to get definition of a dirent.
723 static struct dirent adirent;
725 while ((dp = rst_readdir(dirp)) != NULL) {
726 if (!vflag && dp->d_ino == WINO)
728 if (dflag || TSTINO(dp->d_ino, dumpmap))
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);
740 * Return st_mode information in response to stat or lstat calls
747 register struct direct *dp;
749 dp = pathsearch(name);
750 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
751 (!vflag && dp->d_ino == WINO))
753 if (inodetype(dp->d_ino) == NODE)
754 stp->st_mode = IFDIR;
756 stp->st_mode = IFREG;
761 * Comparison routine for qsort.
765 register const void *f1, *f2;
767 return (strcmp(((struct afile *)f1)->fname,
768 ((struct afile *)f2)->fname));
772 * respond to interrupts
778 if (command == 'i' && runshell)
780 if (reply("restore interrupted, continue") == FAIL)