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
33 * @(#)interactive.c 8.5 (Berkeley) 5/1/95
34 * $FreeBSD: src/sbin/restore/interactive.c,v 1.8.2.1 2001/01/03 14:36:08 iedowse Exp $
35 * $DragonFly: src/sbin/restore/interactive.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
38 #include <sys/param.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <protocols/dumprestore.h>
54 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
57 * Things to handle interruptions.
61 static char *nextarg = NULL;
64 * Structure and routines associated with listing directories.
67 ino_t fnum; /* inode number of file */
68 char *fname; /* file name */
69 short len; /* name length */
70 char prefix; /* prefix character */
71 char postfix; /* postfix character */
74 int freeglob; /* glob structure needs to be freed */
75 int argcnt; /* next globbed argument to return */
76 glob_t glob; /* globbing information */
77 char *cmd; /* the current command */
80 static char *copynext __P((char *, char *));
81 static int fcmp __P((const void *, const void *));
82 static void formatf __P((struct afile *, int));
83 static void getcmd __P((char *, char *, char *, int, struct arglist *));
84 struct dirent *glob_readdir __P((RST_DIR *dirp));
85 static int glob_stat __P((const char *, struct stat *));
86 static void mkentry __P((char *, struct direct *, struct afile *));
87 static void printlist __P((char *, char *));
90 * Read and execute commands from the terminal.
95 register struct entry *np;
97 struct arglist arglist;
98 char curdir[MAXPATHLEN];
99 char name[MAXPATHLEN];
102 arglist.freeglob = 0;
104 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
105 arglist.glob.gl_opendir = (void *)rst_opendir;
106 arglist.glob.gl_readdir = (void *)glob_readdir;
107 arglist.glob.gl_closedir = (void *)rst_closedir;
108 arglist.glob.gl_lstat = glob_stat;
109 arglist.glob.gl_stat = glob_stat;
110 canon("/", curdir, sizeof(curdir));
112 if (setjmp(reset) != 0) {
113 if (arglist.freeglob != 0) {
114 arglist.freeglob = 0;
116 globfree(&arglist.glob);
122 getcmd(curdir, cmd, name, sizeof(name), &arglist);
125 * Add elements to the extraction list.
128 if (strncmp(cmd, "add", strlen(cmd)) != 0)
130 ino = dirlookup(name);
135 treescan(name, ino, addfile);
138 * Change working directory.
141 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
143 ino = dirlookup(name);
146 if (inodetype(ino) == LEAF) {
147 fprintf(stderr, "%s: not a directory\n", name);
150 (void) strcpy(curdir, name);
153 * Delete elements from the extraction list.
156 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
158 np = lookupname(name);
159 if (np == NULL || (np->e_flags & NEW) == 0) {
160 fprintf(stderr, "%s: not on extraction list\n", name);
163 treescan(name, np->e_ino, deletefile);
166 * Extract the requested list.
169 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
179 * List available commands.
182 if (strncmp(cmd, "help", strlen(cmd)) != 0)
185 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
186 "Available commands are:\n",
187 "\tls [arg] - list directory\n",
188 "\tcd arg - change directory\n",
189 "\tpwd - print current directory\n",
190 "\tadd [arg] - add `arg' to list of",
191 " files to be extracted\n",
192 "\tdelete [arg] - delete `arg' from",
193 " list of files to be extracted\n",
194 "\textract - extract requested files\n",
195 "\tsetmodes - set modes of requested directories\n",
196 "\tquit - immediately exit program\n",
197 "\twhat - list dump header information\n",
198 "\tverbose - toggle verbose flag",
199 " (useful with ``ls'')\n",
200 "\thelp or `?' - print this list\n",
201 "If no `arg' is supplied, the current",
202 " directory is used\n");
208 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
210 printlist(name, curdir);
213 * Print current directory.
216 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
218 if (curdir[1] == '\0')
219 fprintf(stderr, "/\n");
221 fprintf(stderr, "%s\n", &curdir[1]);
227 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
231 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
235 * Toggle verbose mode.
238 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
241 fprintf(stderr, "verbose mode off\n");
245 fprintf(stderr, "verbose mode on\n");
249 * Just restore requested directory modes.
252 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
257 * Print out dump header information.
260 if (strncmp(cmd, "what", strlen(cmd)) != 0)
268 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
271 fprintf(stderr, "debugging mode off\n");
275 fprintf(stderr, "debugging mode on\n");
283 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
290 * Read and parse an interactive command.
291 * The first word on the line is assigned to "cmd". If
292 * there are no arguments on the command line, then "curdir"
293 * is returned as the argument. If there are arguments
294 * on the line they are returned one at a time on each
295 * successive call to getcmd. Each argument is first assigned
296 * to "name". If it does not start with "/" the pathname in
297 * "curdir" is prepended to it. Finally "canon" is called to
298 * eliminate any embedded ".." components.
301 getcmd(curdir, cmd, name, size, ap)
302 char *curdir, *cmd, *name;
307 static char input[BUFSIZ];
309 # define rawname input /* save space by reusing input buffer */
312 * Check to see if still processing arguments.
319 * Read a command line and trim off trailing white space.
322 fprintf(stderr, "restore > ");
323 (void) fflush(stderr);
324 if (fgets(input, BUFSIZ, terminal) == NULL) {
328 } while (input[0] == '\n');
329 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
330 /* trim off trailing white space and newline */;
333 * Copy the command into "cmd".
335 cp = copynext(input, cmd);
338 * If no argument, use curdir as the default.
341 (void) strncpy(name, curdir, size);
342 name[size - 1] = '\0';
347 * Find the next argument.
350 cp = copynext(nextarg, rawname);
356 * If it is an absolute pathname, canonicalize it and return it.
358 if (rawname[0] == '/') {
359 canon(rawname, name, size);
362 * For relative pathnames, prepend the current directory to
363 * it then canonicalize and return it.
365 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
366 canon(output, name, size);
368 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
369 fprintf(stderr, "%s: out of memory\n", ap->cmd);
370 if (ap->glob.gl_pathc == 0)
373 ap->argcnt = ap->glob.gl_pathc;
376 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
377 name[size - 1] = '\0';
378 if (--ap->argcnt == 0) {
386 * Strip off the next token of the input.
389 copynext(input, output)
390 char *input, *output;
392 register char *cp, *bp;
395 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
396 /* skip to argument */;
398 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
400 * Handle back slashes.
405 "command lines cannot be continued\n");
412 * The usual unquoted case.
414 if (*cp != '\'' && *cp != '"') {
419 * Handle single and double quotes.
422 while (*cp != quote && *cp != '\0')
423 *bp++ = *cp++ | 0200;
425 fprintf(stderr, "missing %c\n", quote);
435 * Canonicalize file names to always start with ``./'' and
436 * remove any embedded "." and ".." components.
439 canon(rawname, canonname, len)
440 char *rawname, *canonname;
443 register char *cp, *np;
445 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
446 (void) strcpy(canonname, "");
447 else if (rawname[0] == '/')
448 (void) strcpy(canonname, ".");
450 (void) strcpy(canonname, "./");
451 if (strlen(canonname) + strlen(rawname) >= len) {
452 fprintf(stderr, "canonname: not enough buffer space\n");
456 (void) strcat(canonname, rawname);
458 * Eliminate multiple and trailing '/'s
460 for (cp = np = canonname; *np != '\0'; cp++) {
462 while (*cp == '/' && *np == '/')
469 * Eliminate extraneous "." and ".." from pathnames.
471 for (np = canonname; *np != '\0'; ) {
474 while (*np != '/' && *np != '\0')
476 if (np - cp == 1 && *cp == '.') {
478 (void) strcpy(cp, np);
481 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
483 while (cp > &canonname[1] && *--cp != '/')
484 /* find beginning of name */;
485 (void) strcpy(cp, np);
492 * Do an "ls" style listing of a directory
495 printlist(name, basename)
499 register struct afile *fp, *list, *listp;
500 register struct direct *dp;
503 int entries, len, namelen;
504 char locname[MAXPATHLEN + 1];
506 dp = pathsearch(name);
507 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
508 (!vflag && dp->d_ino == WINO))
510 if ((dirp = rst_opendir(name)) == NULL) {
513 mkentry(name, dp, list);
514 len = strlen(basename) + 1;
515 if (strlen(name) - len > single.len) {
516 freename(single.fname);
517 single.fname = savename(&name[len]);
518 single.len = strlen(single.fname);
522 while ((dp = rst_readdir(dirp)))
525 list = (struct afile *)malloc(entries * sizeof(struct afile));
527 fprintf(stderr, "ls: out of memory\n");
530 if ((dirp = rst_opendir(name)) == NULL)
531 panic("directory reopen failed\n");
532 fprintf(stderr, "%s:\n", name);
535 (void) strncpy(locname, name, MAXPATHLEN);
536 (void) strncat(locname, "/", MAXPATHLEN);
537 namelen = strlen(locname);
538 while ((dp = rst_readdir(dirp))) {
541 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
543 if (!vflag && (dp->d_ino == WINO ||
544 strcmp(dp->d_name, ".") == 0 ||
545 strcmp(dp->d_name, "..") == 0))
547 locname[namelen] = '\0';
548 if (namelen + dp->d_namlen >= MAXPATHLEN) {
549 fprintf(stderr, "%s%s: name exceeds %d char\n",
550 locname, dp->d_name, MAXPATHLEN);
552 (void) strncat(locname, dp->d_name,
554 mkentry(locname, dp, listp++);
560 fprintf(stderr, "\n");
564 qsort((char *)list, entries, sizeof(struct afile), fcmp);
566 formatf(list, entries);
568 for (fp = listp - 1; fp >= list; fp--)
570 fprintf(stderr, "\n");
576 * Read the contents of a directory.
579 mkentry(name, dp, fp)
582 register struct afile *fp;
587 fp->fnum = dp->d_ino;
588 fp->fname = savename(dp->d_name);
589 for (cp = fp->fname; *cp; cp++)
590 if (!vflag && (*cp < ' ' || *cp >= 0177))
592 fp->len = cp - fp->fname;
593 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
595 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
602 fprintf(stderr, "Warning: undefined file type %d\n",
629 if (inodetype(dp->d_ino) == NODE)
639 * Print out a pretty listing of a directory
642 formatf(list, nentry)
643 register struct afile *list;
646 register struct afile *fp, *endlist;
647 int width, bigino, haveprefix, havepostfix;
648 int i, j, w, precision, columns, lines;
654 endlist = &list[nentry];
655 for (fp = &list[0]; fp < endlist; fp++) {
656 if (bigino < fp->fnum)
660 if (fp->prefix != ' ')
662 if (fp->postfix != ' ')
670 for (precision = 0, i = bigino; i > 0; i /= 10)
672 width += precision + 1;
675 columns = 81 / width;
678 lines = (nentry + columns - 1) / columns;
679 for (i = 0; i < lines; i++) {
680 for (j = 0; j < columns; j++) {
681 fp = &list[j * lines + i];
683 fprintf(stderr, "%*d ", precision, fp->fnum);
684 fp->len += precision + 1;
687 putc(fp->prefix, stderr);
690 fprintf(stderr, "%s", fp->fname);
692 putc(fp->postfix, stderr);
695 if (fp + lines >= endlist) {
696 fprintf(stderr, "\n");
699 for (w = fp->len; w < width; w++)
706 * Skip over directory entries that are not on the tape
708 * First have to get definition of a dirent.
719 static struct dirent adirent;
721 while ((dp = rst_readdir(dirp)) != NULL) {
722 if (!vflag && dp->d_ino == WINO)
724 if (dflag || TSTINO(dp->d_ino, dumpmap))
729 adirent.d_fileno = dp->d_ino;
730 adirent.d_namlen = dp->d_namlen;
731 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
736 * Return st_mode information in response to stat or lstat calls
743 register struct direct *dp;
745 dp = pathsearch(name);
746 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
747 (!vflag && dp->d_ino == WINO))
749 if (inodetype(dp->d_ino) == NODE)
750 stp->st_mode = IFDIR;
752 stp->st_mode = IFREG;
757 * Comparison routine for qsort.
761 register const void *f1, *f2;
763 return (strcmp(((struct afile *)f1)->fname,
764 ((struct afile *)f2)->fname));
768 * respond to interrupts
774 if (command == 'i' && runshell)
776 if (reply("restore interrupted, continue") == FAIL)