1 /* $NetBSD: interactive.c,v 1.25 2006/12/18 20:07:32 christos Exp $ */
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
37 __RCSID("$NetBSD: interactive.c,v 1.25 2006/12/18 20:07:32 christos Exp $");
41 #include <sys/param.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
48 #include <protocols/dumprestore.h>
59 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
62 * Things to handle interruptions.
66 static char *nextarg
= NULL
;
69 * Structure and routines associated with listing directories.
72 ino_t fnum
; /* inode number of file */
73 char *fname
; /* file name */
74 size_t len
; /* name length */
75 char prefix
; /* prefix character */
76 char postfix
; /* postfix character */
79 int freeglob
; /* glob structure needs to be freed */
80 int argcnt
; /* next globbed argument to return */
81 glob_t glob
; /* globbing information */
82 char *cmd
; /* the current command */
85 static char *copynext(char *, char *);
86 static int fcmp(const void *, const void *);
87 static void formatf(struct afile
*, int);
88 static void getcmd(char *, char *, char *, struct arglist
*);
89 struct dirent
*glob_readdir(RST_DIR
*dirp
);
90 static int glob_stat(const char *, struct stat
*);
91 static void mkentry(char *, struct direct
*, struct afile
*);
92 static void printlist(char *, char *);
95 * Read and execute commands from the terminal.
102 struct arglist arglist
;
103 char curdir
[MAXPATHLEN
];
104 char name
[MAXPATHLEN
];
107 arglist
.freeglob
= 0;
109 arglist
.glob
.gl_flags
= GLOB_ALTDIRFUNC
;
110 arglist
.glob
.gl_opendir
= (void *)rst_opendir
;
111 arglist
.glob
.gl_readdir
= (void *)glob_readdir
;
112 arglist
.glob
.gl_closedir
= (void *)rst_closedir
;
113 arglist
.glob
.gl_lstat
= glob_stat
;
114 arglist
.glob
.gl_stat
= glob_stat
;
117 if (setjmp(reset
) != 0) {
118 if (arglist
.freeglob
!= 0) {
119 arglist
.freeglob
= 0;
121 globfree(&arglist
.glob
);
127 getcmd(curdir
, cmd
, name
, &arglist
);
130 * Add elements to the extraction list.
133 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
135 ino
= dirlookup(name
);
142 treescan(name
, ino
, addfile
);
145 * Change working directory.
148 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
150 ino
= dirlookup(name
);
153 if (inodetype(ino
) == LEAF
) {
154 fprintf(stderr
, "%s: not a directory\n", name
);
157 (void) strcpy(curdir
, name
);
160 * Delete elements from the extraction list.
163 if (strncmp(cmd
, "delete", strlen(cmd
)) != 0)
165 np
= lookupname(name
);
166 if (np
== NULL
|| (np
->e_flags
& NEW
) == 0) {
167 fprintf(stderr
, "%s: not on extraction list\n", name
);
170 treescan(name
, np
->e_ino
, deletefile
);
173 * Extract the requested list.
176 if (strncmp(cmd
, "extract", strlen(cmd
)) != 0)
186 * List available commands.
189 if (strncmp(cmd
, "help", strlen(cmd
)) != 0)
192 fprintf(stderr
, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
193 "Available commands are:\n",
194 "\tls [arg] - list directory\n",
195 "\tcd arg - change directory\n",
196 "\tpwd - print current directory\n",
197 "\tadd [arg] - add `arg' to list of",
198 " files to be extracted\n",
199 "\tdelete [arg] - delete `arg' from",
200 " list of files to be extracted\n",
201 "\textract - extract requested files\n",
202 "\tsetmodes - set modes of requested directories\n",
203 "\tquit or xit - immediately exit program\n",
204 "\twhat - list dump header information\n",
205 "\tverbose - toggle verbose flag",
206 " (useful with ``ls'')\n",
207 "\thelp or `?' - print this list\n",
208 "\tDebug - turn on debugging\n",
209 "If no `arg' is supplied, the current",
210 " directory is used\n");
216 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
218 printlist(name
, curdir
);
221 * Print current directory.
224 if (strncmp(cmd
, "pwd", strlen(cmd
)) != 0)
226 if (curdir
[1] == '\0')
227 fprintf(stderr
, "/\n");
229 fprintf(stderr
, "%s\n", &curdir
[1]);
235 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
239 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
243 * Toggle verbose mode.
246 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
249 fprintf(stderr
, "verbose mode off\n");
253 fprintf(stderr
, "verbose mode on\n");
257 * Just restore requested directory modes.
260 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
265 * Print out dump header information.
268 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
276 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
279 fprintf(stderr
, "debugging mode off\n");
283 fprintf(stderr
, "debugging mode on\n");
291 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
298 * Read and parse an interactive command.
299 * The first word on the line is assigned to "cmd". If
300 * there are no arguments on the command line, then "curdir"
301 * is returned as the argument. If there are arguments
302 * on the line they are returned one at a time on each
303 * successive call to getcmd. Each argument is first assigned
304 * to "name". If it does not start with "/" the pathname in
305 * "curdir" is prepended to it. Finally "canon" is called to
306 * eliminate any embedded ".." components.
309 getcmd(char *curdir
, char *cmd
, char *name
, struct arglist
*ap
)
312 static char input
[BUFSIZ
];
315 # define rawname input /* save space by reusing input buffer */
318 * Check to see if still processing arguments.
325 * Read a command line and trim off trailing white space.
328 fprintf(stderr
, "%s > ", getprogname());
329 (void) fflush(stderr
);
330 (void) fgets(input
, BUFSIZ
, terminal
);
331 } while (!feof(terminal
) && input
[0] == '\n');
332 if (feof(terminal
)) {
333 (void) strcpy(cmd
, "quit");
336 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
337 /* trim off trailing white space and newline */;
340 * Copy the command into "cmd".
342 cp
= copynext(input
, cmd
);
345 * If no argument, use curdir as the default.
348 (void) strcpy(name
, curdir
);
353 * Find the next argument.
356 cp
= copynext(nextarg
, rawname
);
362 * If it is an absolute pathname, canonicalize it and return it.
364 if (rawname
[0] == '/') {
365 canon(rawname
, name
);
368 * For relative pathnames, prepend the current directory to
369 * it then canonicalize and return it.
371 (void) strcpy(output
, curdir
);
372 (void) strcat(output
, "/");
373 (void) strcat(output
, rawname
);
376 if ((globretval
= glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
)) < 0) {
377 fprintf(stderr
, "%s: %s: ", ap
->cmd
, name
);
378 switch (globretval
) {
380 fprintf(stderr
, "out of memory\n");
383 fprintf(stderr
, "no filename match.\n");
386 fprintf(stderr
, "glob() aborted.\n");
389 fprintf(stderr
, "unknown error!\n");
393 if (ap
->glob
.gl_pathc
== 0)
396 ap
->argcnt
= ap
->glob
.gl_pathc
;
399 strcpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
]);
400 if (--ap
->argcnt
== 0) {
408 * Strip off the next token of the input.
411 copynext(char *input
, char *output
)
416 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
417 /* skip to argument */;
419 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
421 * Handle back slashes.
426 "command lines cannot be continued\n");
433 * The usual unquoted case.
435 if (*cp
!= '\'' && *cp
!= '"') {
440 * Handle single and double quotes.
443 while (*cp
!= quote
&& *cp
!= '\0')
446 fprintf(stderr
, "missing %c\n", quote
);
456 * Canonicalize file names to always start with ``./'' and
457 * remove any imbedded "." and ".." components.
460 canon(const char *rawname
, char *canonname
)
464 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
465 (void) strcpy(canonname
, "");
466 else if (rawname
[0] == '/')
467 (void) strcpy(canonname
, ".");
469 (void) strcpy(canonname
, "./");
470 (void) strcat(canonname
, rawname
);
472 * Eliminate multiple and trailing '/'s
474 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
476 while (*cp
== '/' && *np
== '/')
483 * Eliminate extraneous "." and ".." from pathnames.
485 for (np
= canonname
; *np
!= '\0'; ) {
488 while (*np
!= '/' && *np
!= '\0')
490 if (np
- cp
== 1 && *cp
== '.') {
492 (void) strcpy(cp
, np
);
495 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
497 while (cp
> &canonname
[1] && *--cp
!= '/')
498 /* find beginning of name */;
499 (void) strcpy(cp
, np
);
506 * Do an "ls" style listing of a directory
509 printlist(char *name
, char *basename
)
511 struct afile
*fp
, *list
, *listp
;
515 int entries
, len
, namelen
;
516 char locname
[MAXPATHLEN
+ 1];
518 dp
= pathsearch(name
);
520 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
521 (!vflag
&& dp
->d_ino
== WINO
))
523 if ((dirp
= rst_opendir(name
)) == NULL
) {
526 mkentry(name
, dp
, list
);
527 len
= strlen(basename
) + 1;
528 if (strlen(name
) - len
> single
.len
) {
529 freename(single
.fname
);
530 single
.fname
= savename(&name
[len
]);
531 single
.len
= strlen(single
.fname
);
535 while ((dp
= rst_readdir(dirp
)) != NULL
)
538 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
540 fprintf(stderr
, "ls: out of memory\n");
543 if ((dirp
= rst_opendir(name
)) == NULL
)
544 panic("directory reopen failed\n");
545 fprintf(stderr
, "%s:\n", name
);
548 (void) strncpy(locname
, name
, MAXPATHLEN
);
549 (void) strncat(locname
, "/", MAXPATHLEN
);
550 namelen
= strlen(locname
);
551 while ((dp
= rst_readdir(dirp
)) != NULL
) {
552 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
554 if (!vflag
&& (dp
->d_ino
== WINO
||
555 strcmp(dp
->d_name
, ".") == 0 ||
556 strcmp(dp
->d_name
, "..") == 0))
558 locname
[namelen
] = '\0';
559 if (namelen
+ dp
->d_namlen
>= MAXPATHLEN
) {
560 fprintf(stderr
, "%s%s: name exceeds %d char\n",
561 locname
, dp
->d_name
, MAXPATHLEN
);
563 (void) strncat(locname
, dp
->d_name
,
565 mkentry(locname
, dp
, listp
++);
571 fprintf(stderr
, "\n");
575 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
577 formatf(list
, entries
);
579 for (fp
= listp
- 1; fp
>= list
; fp
--)
581 fprintf(stderr
, "\n");
587 * Read the contents of a directory.
590 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
595 fp
->fnum
= dp
->d_ino
;
596 fp
->fname
= savename(dp
->d_name
);
597 for (cp
= fp
->fname
; *cp
; cp
++)
598 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
600 fp
->len
= cp
- fp
->fname
;
601 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
603 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
610 fprintf(stderr
, "Warning: undefined file type %d\n",
637 if (inodetype(dp
->d_ino
) == NODE
)
647 * Print out a pretty listing of a directory
650 formatf(struct afile
*list
, int nentry
)
652 struct afile
*fp
, *endlist
;
653 int haveprefix
, havepostfix
;
656 int i
, j
, precision
, columns
, lines
;
663 endlist
= &list
[nentry
];
664 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
665 if (bigino
< fp
->fnum
)
669 if (fp
->prefix
!= ' ')
671 if (fp
->postfix
!= ' ')
679 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
681 width
+= precision
+ 1;
684 columns
= 81 / width
;
687 lines
= (nentry
+ columns
- 1) / columns
;
688 for (i
= 0; i
< lines
; i
++) {
689 for (j
= 0; j
< columns
; j
++) {
690 fp
= &list
[j
* lines
+ i
];
692 fprintf(stderr
, "%*llu ", precision
,
693 (unsigned long long)fp
->fnum
);
694 fp
->len
+= precision
+ 1;
697 putc(fp
->prefix
, stderr
);
700 fprintf(stderr
, "%s", fp
->fname
);
702 putc(fp
->postfix
, stderr
);
705 if (fp
+ lines
>= endlist
) {
706 fprintf(stderr
, "\n");
709 for (w
= fp
->len
; w
< width
; w
++)
716 * Skip over directory entries that are not on the tape
718 * First have to get definition of a dirent.
725 glob_readdir(RST_DIR
*dirp
)
728 static struct dirent adirent
;
730 while ((dp
= rst_readdir(dirp
)) != NULL
) {
731 if (!vflag
&& dp
->d_fileno
== WINO
)
733 if (dflag
|| TSTINO(dp
->d_fileno
, dumpmap
))
738 adirent
.d_fileno
= dp
->d_fileno
;
739 adirent
.d_namlen
= dp
->d_namlen
;
740 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
745 * Return st_mode information in response to stat or lstat calls
748 glob_stat(const char *name
, struct stat
*stp
)
752 dp
= pathsearch(name
);
753 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_fileno
, dumpmap
) == 0) ||
754 (!vflag
&& dp
->d_fileno
== WINO
))
756 if (inodetype(dp
->d_fileno
) == NODE
)
757 stp
->st_mode
= S_IFDIR
;
759 stp
->st_mode
= S_IFREG
;
764 * Comparison routine for qsort.
767 fcmp(const void *f1
, const void *f2
)
769 return (strcmp(((const struct afile
*)f1
)->fname
,
770 ((const struct afile
*)f2
)->fname
));
774 * respond to interrupts
778 onintr(int signo __unused
)
780 if (command
== 'i' && runshell
)
782 if (reply("restore interrupted, continue") == FAIL
)