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.4 2003/09/28 14:39:21 hmp Exp $
38 #include <sys/param.h>
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/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(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
304 static char input[BUFSIZ];
306 # define rawname input /* save space by reusing input buffer */
309 * Check to see if still processing arguments.
316 * Read a command line and trim off trailing white space.
319 fprintf(stderr, "restore > ");
320 (void) fflush(stderr);
321 if (fgets(input, BUFSIZ, terminal) == NULL) {
325 } while (input[0] == '\n');
326 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
327 /* trim off trailing white space and newline */;
330 * Copy the command into "cmd".
332 cp = copynext(input, cmd);
335 * If no argument, use curdir as the default.
338 (void) strncpy(name, curdir, size);
339 name[size - 1] = '\0';
344 * Find the next argument.
347 cp = copynext(nextarg, rawname);
353 * If it is an absolute pathname, canonicalize it and return it.
355 if (rawname[0] == '/') {
356 canon(rawname, name, size);
359 * For relative pathnames, prepend the current directory to
360 * it then canonicalize and return it.
362 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
363 canon(output, name, size);
365 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
366 fprintf(stderr, "%s: out of memory\n", ap->cmd);
367 if (ap->glob.gl_pathc == 0)
370 ap->argcnt = ap->glob.gl_pathc;
373 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
374 name[size - 1] = '\0';
375 if (--ap->argcnt == 0) {
383 * Strip off the next token of the input.
386 copynext(char *input, char *output)
388 register char *cp, *bp;
391 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
392 /* skip to argument */;
394 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
396 * Handle back slashes.
401 "command lines cannot be continued\n");
408 * The usual unquoted case.
410 if (*cp != '\'' && *cp != '"') {
415 * Handle single and double quotes.
418 while (*cp != quote && *cp != '\0')
419 *bp++ = *cp++ | 0200;
421 fprintf(stderr, "missing %c\n", quote);
431 * Canonicalize file names to always start with ``./'' and
432 * remove any embedded "." and ".." components.
435 canon(char *rawname, char *canonname, int len)
437 register char *cp, *np;
439 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
440 (void) strcpy(canonname, "");
441 else if (rawname[0] == '/')
442 (void) strcpy(canonname, ".");
444 (void) strcpy(canonname, "./");
445 if (strlen(canonname) + strlen(rawname) >= len) {
446 fprintf(stderr, "canonname: not enough buffer space\n");
450 (void) strcat(canonname, rawname);
452 * Eliminate multiple and trailing '/'s
454 for (cp = np = canonname; *np != '\0'; cp++) {
456 while (*cp == '/' && *np == '/')
463 * Eliminate extraneous "." and ".." from pathnames.
465 for (np = canonname; *np != '\0'; ) {
468 while (*np != '/' && *np != '\0')
470 if (np - cp == 1 && *cp == '.') {
472 (void) strcpy(cp, np);
475 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
477 while (cp > &canonname[1] && *--cp != '/')
478 /* find beginning of name */;
479 (void) strcpy(cp, np);
486 * Do an "ls" style listing of a directory
489 printlist(char *name, char *basename)
491 register struct afile *fp, *list, *listp;
492 register struct direct *dp;
495 int entries, len, namelen;
496 char locname[MAXPATHLEN + 1];
498 dp = pathsearch(name);
499 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
500 (!vflag && dp->d_ino == WINO))
502 if ((dirp = rst_opendir(name)) == NULL) {
505 mkentry(name, dp, list);
506 len = strlen(basename) + 1;
507 if (strlen(name) - len > single.len) {
508 freename(single.fname);
509 single.fname = savename(&name[len]);
510 single.len = strlen(single.fname);
514 while ((dp = rst_readdir(dirp)))
517 list = (struct afile *)malloc(entries * sizeof(struct afile));
519 fprintf(stderr, "ls: out of memory\n");
522 if ((dirp = rst_opendir(name)) == NULL)
523 panic("directory reopen failed\n");
524 fprintf(stderr, "%s:\n", name);
527 (void) strncpy(locname, name, MAXPATHLEN);
528 (void) strncat(locname, "/", MAXPATHLEN);
529 namelen = strlen(locname);
530 while ((dp = rst_readdir(dirp))) {
533 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
535 if (!vflag && (dp->d_ino == WINO ||
536 strcmp(dp->d_name, ".") == 0 ||
537 strcmp(dp->d_name, "..") == 0))
539 locname[namelen] = '\0';
540 if (namelen + dp->d_namlen >= MAXPATHLEN) {
541 fprintf(stderr, "%s%s: name exceeds %d char\n",
542 locname, dp->d_name, MAXPATHLEN);
544 (void) strncat(locname, dp->d_name,
546 mkentry(locname, dp, listp++);
552 fprintf(stderr, "\n");
556 qsort((char *)list, entries, sizeof(struct afile), fcmp);
558 formatf(list, entries);
560 for (fp = listp - 1; fp >= list; fp--)
562 fprintf(stderr, "\n");
568 * Read the contents of a directory.
571 mkentry(char *name, struct direct *dp, register struct afile *fp)
576 fp->fnum = dp->d_ino;
577 fp->fname = savename(dp->d_name);
578 for (cp = fp->fname; *cp; cp++)
579 if (!vflag && (*cp < ' ' || *cp >= 0177))
581 fp->len = cp - fp->fname;
582 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
584 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
591 fprintf(stderr, "Warning: undefined file type %d\n",
618 if (inodetype(dp->d_ino) == NODE)
628 * Print out a pretty listing of a directory
631 formatf(register struct afile *list, int nentry)
633 register struct afile *fp, *endlist;
634 int width, bigino, haveprefix, havepostfix;
635 int i, j, w, precision, columns, lines;
641 endlist = &list[nentry];
642 for (fp = &list[0]; fp < endlist; fp++) {
643 if (bigino < fp->fnum)
647 if (fp->prefix != ' ')
649 if (fp->postfix != ' ')
657 for (precision = 0, i = bigino; i > 0; i /= 10)
659 width += precision + 1;
662 columns = 81 / width;
665 lines = (nentry + columns - 1) / columns;
666 for (i = 0; i < lines; i++) {
667 for (j = 0; j < columns; j++) {
668 fp = &list[j * lines + i];
670 fprintf(stderr, "%*d ", precision, fp->fnum);
671 fp->len += precision + 1;
674 putc(fp->prefix, stderr);
677 fprintf(stderr, "%s", fp->fname);
679 putc(fp->postfix, stderr);
682 if (fp + lines >= endlist) {
683 fprintf(stderr, "\n");
686 for (w = fp->len; w < width; w++)
693 * Skip over directory entries that are not on the tape
695 * First have to get definition of a dirent.
702 glob_readdir(RST_DIR *dirp)
705 static struct dirent adirent;
707 while ((dp = rst_readdir(dirp)) != NULL) {
708 if (!vflag && dp->d_ino == WINO)
710 if (dflag || TSTINO(dp->d_ino, dumpmap))
715 adirent.d_fileno = dp->d_ino;
716 adirent.d_namlen = dp->d_namlen;
717 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
722 * Return st_mode information in response to stat or lstat calls
725 glob_stat(const char *name, struct stat *stp)
727 register struct direct *dp;
729 dp = pathsearch(name);
730 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
731 (!vflag && dp->d_ino == WINO))
733 if (inodetype(dp->d_ino) == NODE)
734 stp->st_mode = IFDIR;
736 stp->st_mode = IFREG;
741 * Comparison routine for qsort.
744 fcmp(register const void *f1, register const void *f2)
746 return (strcmp(((struct afile *)f1)->fname,
747 ((struct afile *)f2)->fname));
751 * respond to interrupts
756 if (command == 'i' && runshell)
758 if (reply("restore interrupted, continue") == FAIL)