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. 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
29 * @(#)interactive.c 8.5 (Berkeley) 5/1/95
30 * $FreeBSD: src/sbin/restore/interactive.c,v 1.8.2.1 2001/01/03 14:36:08 iedowse Exp $
33 #include <sys/param.h>
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/dir.h>
45 #include <protocols/dumprestore.h>
51 * Things to handle interruptions.
55 static char *nextarg = NULL;
58 * Structure and routines associated with listing directories.
61 ufs1_ino_t fnum; /* inode number of file */
62 char *fname; /* file name */
63 short len; /* name length */
64 char prefix; /* prefix character */
65 char postfix; /* postfix character */
68 int freeglob; /* glob structure needs to be freed */
69 int argcnt; /* next globbed argument to return */
70 glob_t glob; /* globbing information */
71 char *cmd; /* the current command */
74 static char *copynext(char *, char *);
75 static int fcmp(const void *, const void *);
76 static void formatf(struct afile *, int);
77 static void getcmd(char *, char *, char *, int, struct arglist *);
78 struct dirent *glob_readdir(RST_DIR *dirp);
79 static int glob_stat(const char *, struct stat *);
80 static void mkentry(char *, struct direct *, struct afile *);
81 static void printlist(char *, char *);
84 * Read and execute commands from the terminal.
91 struct arglist arglist;
92 char curdir[MAXPATHLEN];
93 char name[MAXPATHLEN];
98 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
99 arglist.glob.gl_opendir = (void *)rst_opendir;
100 arglist.glob.gl_readdir = (void *)glob_readdir;
101 arglist.glob.gl_closedir = (void *)rst_closedir;
102 arglist.glob.gl_lstat = glob_stat;
103 arglist.glob.gl_stat = glob_stat;
104 canon("/", curdir, sizeof(curdir));
106 if (setjmp(reset) != 0) {
107 if (arglist.freeglob != 0) {
108 arglist.freeglob = 0;
110 globfree(&arglist.glob);
116 getcmd(curdir, cmd, name, sizeof(name), &arglist);
119 * Add elements to the extraction list.
122 if (strncmp(cmd, "add", strlen(cmd)) != 0)
124 ino = dirlookup(name);
129 treescan(name, ino, addfile);
132 * Change working directory.
135 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
137 ino = dirlookup(name);
140 if (inodetype(ino) == LEAF) {
141 fprintf(stderr, "%s: not a directory\n", name);
144 strcpy(curdir, name);
147 * Delete elements from the extraction list.
150 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
152 np = lookupname(name);
153 if (np == NULL || (np->e_flags & NEW) == 0) {
154 fprintf(stderr, "%s: not on extraction list\n", name);
157 treescan(name, np->e_ino, deletefile);
160 * Extract the requested list.
163 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
173 * List available commands.
176 if (strncmp(cmd, "help", strlen(cmd)) != 0)
179 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
180 "Available commands are:\n",
181 "\tls [arg] - list directory\n",
182 "\tcd arg - change directory\n",
183 "\tpwd - print current directory\n",
184 "\tadd [arg] - add `arg' to list of",
185 " files to be extracted\n",
186 "\tdelete [arg] - delete `arg' from",
187 " list of files to be extracted\n",
188 "\textract - extract requested files\n",
189 "\tsetmodes - set modes of requested directories\n",
190 "\tquit - immediately exit program\n",
191 "\twhat - list dump header information\n",
192 "\tverbose - toggle verbose flag",
193 " (useful with ``ls'')\n",
194 "\thelp or `?' - print this list\n",
195 "If no `arg' is supplied, the current",
196 " directory is used\n");
202 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
204 printlist(name, curdir);
207 * Print current directory.
210 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
212 if (curdir[1] == '\0')
213 fprintf(stderr, "/\n");
215 fprintf(stderr, "%s\n", &curdir[1]);
221 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
225 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
229 * Toggle verbose mode.
232 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
235 fprintf(stderr, "verbose mode off\n");
239 fprintf(stderr, "verbose mode on\n");
243 * Just restore requested directory modes.
246 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
251 * Print out dump header information.
254 if (strncmp(cmd, "what", strlen(cmd)) != 0)
262 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
265 fprintf(stderr, "debugging mode off\n");
269 fprintf(stderr, "debugging mode on\n");
277 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
284 * Read and parse an interactive command.
285 * The first word on the line is assigned to "cmd". If
286 * there are no arguments on the command line, then "curdir"
287 * is returned as the argument. If there are arguments
288 * on the line they are returned one at a time on each
289 * successive call to getcmd. Each argument is first assigned
290 * to "name". If it does not start with "/" the pathname in
291 * "curdir" is prepended to it. Finally "canon" is called to
292 * eliminate any embedded ".." components.
295 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
298 static char input[BUFSIZ];
300 # define rawname input /* save space by reusing input buffer */
303 * Check to see if still processing arguments.
310 * Read a command line and trim off trailing white space.
313 fprintf(stderr, "restore > ");
315 if (fgets(input, BUFSIZ, terminal) == NULL) {
319 } while (input[0] == '\n');
320 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
321 /* trim off trailing white space and newline */;
324 * Copy the command into "cmd".
326 cp = copynext(input, cmd);
329 * If no argument, use curdir as the default.
332 strncpy(name, curdir, size);
333 name[size - 1] = '\0';
338 * Find the next argument.
341 cp = copynext(nextarg, rawname);
347 * If it is an absolute pathname, canonicalize it and return it.
349 if (rawname[0] == '/') {
350 canon(rawname, name, size);
353 * For relative pathnames, prepend the current directory to
354 * it then canonicalize and return it.
356 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
357 canon(output, name, size);
359 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
360 fprintf(stderr, "%s: out of memory\n", ap->cmd);
361 if (ap->glob.gl_pathc == 0)
364 ap->argcnt = ap->glob.gl_pathc;
367 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
368 name[size - 1] = '\0';
369 if (--ap->argcnt == 0) {
377 * Strip off the next token of the input.
380 copynext(char *input, char *output)
385 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
386 /* skip to argument */;
388 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
390 * Handle back slashes.
395 "command lines cannot be continued\n");
402 * The usual unquoted case.
404 if (*cp != '\'' && *cp != '"') {
409 * Handle single and double quotes.
412 while (*cp != quote && *cp != '\0')
413 *bp++ = *cp++ | 0200;
415 fprintf(stderr, "missing %c\n", quote);
425 * Canonicalize file names to always start with ``./'' and
426 * remove any embedded "." and ".." components.
429 canon(char *rawname, char *canonname, int len)
433 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
434 strcpy(canonname, "");
435 else if (rawname[0] == '/')
436 strcpy(canonname, ".");
438 strcpy(canonname, "./");
439 if (strlen(canonname) + strlen(rawname) >= len) {
440 fprintf(stderr, "canonname: not enough buffer space\n");
444 strcat(canonname, rawname);
446 * Eliminate multiple and trailing '/'s
448 for (cp = np = canonname; *np != '\0'; cp++) {
450 while (*cp == '/' && *np == '/')
457 * Eliminate extraneous "." and ".." from pathnames.
459 for (np = canonname; *np != '\0'; ) {
462 while (*np != '/' && *np != '\0')
464 if (np - cp == 1 && *cp == '.') {
469 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
471 while (cp > &canonname[1] && *--cp != '/')
472 /* find beginning of name */;
480 * Do an "ls" style listing of a directory
483 printlist(char *name, char *basename)
485 struct afile *fp, *list, *listp = NULL;
489 int entries, len, namelen;
490 char locname[MAXPATHLEN + 1];
492 dp = pathsearch(name);
493 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
494 (!vflag && dp->d_ino == WINO))
496 if ((dirp = rst_opendir(name)) == NULL) {
499 mkentry(name, dp, list);
500 len = strlen(basename) + 1;
501 if (strlen(name) - len > single.len) {
502 freename(single.fname);
503 single.fname = savename(&name[len]);
504 single.len = strlen(single.fname);
508 while ((dp = rst_readdir(dirp)))
511 list = (struct afile *)malloc(entries * sizeof(struct afile));
513 fprintf(stderr, "ls: out of memory\n");
516 if ((dirp = rst_opendir(name)) == NULL)
517 panic("directory reopen failed\n");
518 fprintf(stderr, "%s:\n", name);
521 strncpy(locname, name, MAXPATHLEN);
522 strncat(locname, "/", MAXPATHLEN);
523 namelen = strlen(locname);
524 while ((dp = rst_readdir(dirp))) {
527 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
529 if (!vflag && (dp->d_ino == WINO ||
530 strcmp(dp->d_name, ".") == 0 ||
531 strcmp(dp->d_name, "..") == 0))
533 locname[namelen] = '\0';
534 if (namelen + dp->d_namlen >= MAXPATHLEN) {
535 fprintf(stderr, "%s%s: name exceeds %d char\n",
536 locname, dp->d_name, MAXPATHLEN);
538 strncat(locname, dp->d_name, (int)dp->d_namlen);
539 mkentry(locname, dp, listp++);
545 fprintf(stderr, "\n");
549 qsort((char *)list, entries, sizeof(struct afile), fcmp);
551 formatf(list, entries);
553 for (fp = listp - 1; fp >= list; fp--)
555 fprintf(stderr, "\n");
561 * Read the contents of a directory.
564 mkentry(char *name, struct direct *dp, struct afile *fp)
569 fp->fnum = dp->d_ino;
570 fp->fname = savename(dp->d_name);
571 for (cp = fp->fname; *cp; cp++)
572 if (!vflag && (*cp < ' ' || *cp >= 0177))
574 fp->len = cp - fp->fname;
575 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
577 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
584 fprintf(stderr, "Warning: undefined file type %d\n",
611 if (inodetype(dp->d_ino) == NODE)
621 * Print out a pretty listing of a directory
624 formatf(struct afile *list, int nentry)
626 struct afile *fp, *endlist;
627 int width, bigino, haveprefix, havepostfix;
628 int i, j, w, precision = 0, columns, lines;
634 endlist = &list[nentry];
635 for (fp = &list[0]; fp < endlist; fp++) {
636 if (bigino < fp->fnum)
640 if (fp->prefix != ' ')
642 if (fp->postfix != ' ')
650 for (precision = 0, i = bigino; i > 0; i /= 10)
652 width += precision + 1;
655 columns = 81 / width;
658 lines = (nentry + columns - 1) / columns;
659 for (i = 0; i < lines; i++) {
660 for (j = 0; j < columns; j++) {
661 fp = &list[j * lines + i];
663 fprintf(stderr, "%*d ", precision, fp->fnum);
664 fp->len += precision + 1;
667 putc(fp->prefix, stderr);
670 fprintf(stderr, "%s", fp->fname);
672 putc(fp->postfix, stderr);
675 if (fp + lines >= endlist) {
676 fprintf(stderr, "\n");
679 for (w = fp->len; w < width; w++)
686 * Skip over directory entries that are not on the tape
688 * First have to get definition of a dirent.
693 glob_readdir(RST_DIR *dirp)
696 ADIRENT_STORAGE_LEN = _DIRENT_RECLEN(NAME_MAX),
699 uint8_t storage[ADIRENT_STORAGE_LEN];
700 struct dirent alignment;
705 while ((dp = rst_readdir(dirp)) != NULL) {
706 if (!vflag && dp->d_ino == WINO)
708 if (dflag || TSTINO(dp->d_ino, dumpmap))
713 adp = (struct dirent *)&adirent;
714 adp->d_ino = dp->d_ino;
715 adp->d_namlen = dp->d_namlen;
716 strcpy(adp->d_name, dp->d_name);
721 * Return st_mode information in response to stat or lstat calls
724 glob_stat(const char *name, struct stat *stp)
728 dp = pathsearch(name);
729 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
730 (!vflag && dp->d_ino == WINO))
732 if (inodetype(dp->d_ino) == NODE)
733 stp->st_mode = IFDIR;
735 stp->st_mode = IFREG;
740 * Comparison routine for qsort.
743 fcmp(const void *f1, const void *f2)
745 return (strcmp(((struct afile *)f1)->fname,
746 ((struct afile *)f2)->fname));
750 * respond to interrupts
755 if (command == 'i' && runshell)
757 if (reply("restore interrupted, continue") == FAIL)