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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <protocols/dumprestore.h>
57 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
60 * Things to handle interruptions.
64 static char *nextarg = NULL;
67 * Structure and routines associated with listing directories.
70 ino_t fnum; /* inode number of file */
71 char *fname; /* file name */
72 short len; /* name length */
73 char prefix; /* prefix character */
74 char postfix; /* postfix character */
77 int freeglob; /* glob structure needs to be freed */
78 int argcnt; /* next globbed argument to return */
79 glob_t glob; /* globbing information */
80 char *cmd; /* the current command */
83 static char *copynext(char *, char *);
84 static int fcmp(const void *, const void *);
85 static void formatf(struct afile *, int);
86 static void getcmd(char *, char *, char *, int, struct arglist *);
87 struct dirent *glob_readdir(void *);
88 static int glob_stat(const char *, struct stat *);
89 static void mkentry(char *, struct direct *, struct afile *);
90 static void printlist(char *, char *);
93 * Read and execute commands from the terminal.
100 struct arglist arglist;
101 char curdir[MAXPATHLEN];
102 char name[MAXPATHLEN];
105 arglist.freeglob = 0;
107 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
108 arglist.glob.gl_opendir = rst_opendir;
109 arglist.glob.gl_readdir = glob_readdir;
110 arglist.glob.gl_closedir = rst_closedir;
111 arglist.glob.gl_lstat = glob_stat;
112 arglist.glob.gl_stat = glob_stat;
113 canon("/", curdir, sizeof(curdir));
115 if (setjmp(reset) != 0) {
116 if (arglist.freeglob != 0) {
117 arglist.freeglob = 0;
119 globfree(&arglist.glob);
125 getcmd(curdir, cmd, name, sizeof(name), &arglist);
128 * Add elements to the extraction list.
131 if (strncmp(cmd, "add", strlen(cmd)) != 0)
133 ino = dirlookup(name);
138 treescan(name, ino, addfile);
141 * Change working directory.
144 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
146 ino = dirlookup(name);
149 if (inodetype(ino) == LEAF) {
150 fprintf(stderr, "%s: not a directory\n", name);
153 (void) strcpy(curdir, name);
156 * Delete elements from the extraction list.
159 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
161 np = lookupname(name);
162 if (np == NULL || (np->e_flags & NEW) == 0) {
163 fprintf(stderr, "%s: not on extraction list\n", name);
166 treescan(name, np->e_ino, deletefile);
169 * Extract the requested list.
172 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
182 * List available commands.
185 if (strncmp(cmd, "help", strlen(cmd)) != 0)
188 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
189 "Available commands are:\n",
190 "\tls [arg] - list directory\n",
191 "\tcd arg - change directory\n",
192 "\tpwd - print current directory\n",
193 "\tadd [arg] - add `arg' to list of",
194 " files to be extracted\n",
195 "\tdelete [arg] - delete `arg' from",
196 " list of files to be extracted\n",
197 "\textract - extract requested files\n",
198 "\tsetmodes - set modes of requested directories\n",
199 "\tquit - immediately exit program\n",
200 "\twhat - list dump header information\n",
201 "\tverbose - toggle verbose flag",
202 " (useful with ``ls'')\n",
203 "\thelp or `?' - print this list\n",
204 "If no `arg' is supplied, the current",
205 " directory is used\n");
211 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
213 printlist(name, curdir);
216 * Print current directory.
219 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
221 if (curdir[1] == '\0')
222 fprintf(stderr, "/\n");
224 fprintf(stderr, "%s\n", &curdir[1]);
230 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
234 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
238 * Toggle verbose mode.
241 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
244 fprintf(stderr, "verbose mode off\n");
248 fprintf(stderr, "verbose mode on\n");
252 * Just restore requested directory modes.
255 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
260 * Print out dump header information.
263 if (strncmp(cmd, "what", strlen(cmd)) != 0)
271 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
274 fprintf(stderr, "debugging mode off\n");
278 fprintf(stderr, "debugging mode on\n");
286 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
293 * Read and parse an interactive command.
294 * The first word on the line is assigned to "cmd". If
295 * there are no arguments on the command line, then "curdir"
296 * is returned as the argument. If there are arguments
297 * on the line they are returned one at a time on each
298 * successive call to getcmd. Each argument is first assigned
299 * to "name". If it does not start with "/" the pathname in
300 * "curdir" is prepended to it. Finally "canon" is called to
301 * eliminate any embedded ".." components.
304 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
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(char *input, char *output)
394 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
395 /* skip to argument */;
397 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
399 * Handle back slashes.
404 "command lines cannot be continued\n");
411 * The usual unquoted case.
413 if (*cp != '\'' && *cp != '"') {
418 * Handle single and double quotes.
421 while (*cp != quote && *cp != '\0')
424 fprintf(stderr, "missing %c\n", quote);
434 * Canonicalize file names to always start with ``./'' and
435 * remove any embedded "." and ".." components.
438 canon(char *rawname, char *canonname, int len)
442 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
443 (void) strcpy(canonname, "");
444 else if (rawname[0] == '/')
445 (void) strcpy(canonname, ".");
447 (void) strcpy(canonname, "./");
448 if (strlen(canonname) + strlen(rawname) >= len) {
449 fprintf(stderr, "canonname: not enough buffer space\n");
453 (void) strcat(canonname, rawname);
455 * Eliminate multiple and trailing '/'s
457 for (cp = np = canonname; *np != '\0'; cp++) {
459 while (*cp == '/' && *np == '/')
466 * Eliminate extraneous "." and ".." from pathnames.
468 for (np = canonname; *np != '\0'; ) {
471 while (*np != '/' && *np != '\0')
473 if (np - cp == 1 && *cp == '.') {
475 (void) strcpy(cp, np);
478 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
480 while (cp > &canonname[1] && *--cp != '/')
481 /* find beginning of name */;
482 (void) strcpy(cp, np);
489 * Do an "ls" style listing of a directory
492 printlist(char *name, char *basename)
494 struct afile *fp, *list, *listp;
498 int entries, len, namelen;
499 char locname[MAXPATHLEN + 1];
501 dp = pathsearch(name);
502 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
503 (!vflag && dp->d_ino == WINO))
505 if ((dirp = rst_opendir(name)) == NULL) {
508 mkentry(name, dp, list);
509 len = strlen(basename) + 1;
510 if (strlen(name) - len > single.len) {
511 freename(single.fname);
512 single.fname = savename(&name[len]);
513 single.len = strlen(single.fname);
517 while ((dp = rst_readdir(dirp)))
520 list = (struct afile *)malloc(entries * sizeof(struct afile));
522 fprintf(stderr, "ls: out of memory\n");
525 if ((dirp = rst_opendir(name)) == NULL)
526 panic("directory reopen failed\n");
527 fprintf(stderr, "%s:\n", name);
530 (void) strncpy(locname, name, MAXPATHLEN);
531 (void) strncat(locname, "/", MAXPATHLEN);
532 namelen = strlen(locname);
533 while ((dp = rst_readdir(dirp))) {
536 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
538 if (!vflag && (dp->d_ino == WINO ||
539 strcmp(dp->d_name, ".") == 0 ||
540 strcmp(dp->d_name, "..") == 0))
542 locname[namelen] = '\0';
543 if (namelen + dp->d_namlen >= MAXPATHLEN) {
544 fprintf(stderr, "%s%s: name exceeds %d char\n",
545 locname, dp->d_name, MAXPATHLEN);
547 (void) strncat(locname, dp->d_name,
549 mkentry(locname, dp, listp++);
555 fprintf(stderr, "\n");
559 qsort((char *)list, entries, sizeof(struct afile), fcmp);
561 formatf(list, entries);
563 for (fp = listp - 1; fp >= list; fp--)
565 fprintf(stderr, "\n");
571 * Read the contents of a directory.
574 mkentry(char *name, struct direct *dp, struct afile *fp)
579 fp->fnum = dp->d_ino;
580 fp->fname = savename(dp->d_name);
581 for (cp = fp->fname; *cp; cp++)
582 if (!vflag && !isprint((unsigned char)*cp))
584 fp->len = cp - fp->fname;
585 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
587 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
594 fprintf(stderr, "Warning: undefined file type %d\n",
621 if (inodetype(dp->d_ino) == NODE)
631 * Print out a pretty listing of a directory
634 formatf(struct afile *list, int nentry)
636 struct afile *fp, *endlist;
637 int width, bigino, haveprefix, havepostfix;
638 int i, j, w, precision, columns, lines;
644 endlist = &list[nentry];
645 for (fp = &list[0]; fp < endlist; fp++) {
646 if (bigino < fp->fnum)
650 if (fp->prefix != ' ')
652 if (fp->postfix != ' ')
660 for (precision = 0, i = bigino; i > 0; i /= 10)
662 width += precision + 1;
665 columns = 81 / width;
668 lines = (nentry + columns - 1) / columns;
669 for (i = 0; i < lines; i++) {
670 for (j = 0; j < columns; j++) {
671 fp = &list[j * lines + i];
673 fprintf(stderr, "%*d ", precision, fp->fnum);
674 fp->len += precision + 1;
677 putc(fp->prefix, stderr);
680 fprintf(stderr, "%s", fp->fname);
682 putc(fp->postfix, stderr);
685 if (fp + lines >= endlist) {
686 fprintf(stderr, "\n");
689 for (w = fp->len; w < width; w++)
696 * Skip over directory entries that are not on the tape
698 * First have to get definition of a dirent.
705 glob_readdir(void *dirp)
708 static struct dirent adirent;
710 while ((dp = rst_readdir(dirp)) != NULL) {
711 if (!vflag && dp->d_ino == WINO)
713 if (dflag || TSTINO(dp->d_ino, dumpmap))
718 adirent.d_fileno = dp->d_ino;
719 adirent.d_namlen = dp->d_namlen;
720 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
725 * Return st_mode information in response to stat or lstat calls
728 glob_stat(const char *name, struct stat *stp)
732 dp = pathsearch(name);
733 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
734 (!vflag && dp->d_ino == WINO))
736 if (inodetype(dp->d_ino) == NODE)
737 stp->st_mode = IFDIR;
739 stp->st_mode = IFREG;
744 * Comparison routine for qsort.
747 fcmp(const void *f1, const void *f2)
749 return (strcoll(((struct afile *)f1)->fname,
750 ((struct afile *)f2)->fname));
754 * respond to interrupts
757 onintr(int signo __unused)
759 if (command == 'i' && runshell)
761 if (reply("restore interrupted, continue") == FAIL)