cputimer: Add more IDs for VMM cputimers.
[dragonfly.git] / sbin / restore / interactive.c
blobaa4691e3cd853403888b1d91e19576ed90b7289f
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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>
34 #include <sys/stat.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/dir.h>
38 #include <protocols/dumprestore.h>
40 #include <setjmp.h>
41 #include <glob.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include "restore.h"
47 #include "extern.h"
50 * Things to handle interruptions.
52 static int runshell;
53 static jmp_buf reset;
54 static char *nextarg = NULL;
57 * Structure and routines associated with listing directories.
59 struct afile {
60 ufs1_ino_t fnum; /* inode number of file */
61 char *fname; /* file name */
62 short len; /* name length */
63 char prefix; /* prefix character */
64 char postfix; /* postfix character */
66 struct arglist {
67 int freeglob; /* glob structure needs to be freed */
68 int argcnt; /* next globbed argument to return */
69 glob_t glob; /* globbing information */
70 char *cmd; /* the current command */
73 static char *copynext(char *, char *);
74 static int fcmp(const void *, const void *);
75 static void formatf(struct afile *, int);
76 static void getcmd(char *, char *, char *, size_t, struct arglist *);
77 struct dirent *glob_readdir(RST_DIR *dirp);
78 static int glob_stat(const char *, struct stat *);
79 static void mkentry(const char *, struct direct *, struct afile *);
80 static void printlist(const char *, char *);
83 * Read and execute commands from the terminal.
85 void
86 runcmdshell(void)
88 struct entry *np;
89 ufs1_ino_t ino;
90 struct arglist arglist;
91 char curdir[MAXPATHLEN];
92 char name[MAXPATHLEN];
93 char cmd[BUFSIZ];
95 arglist.freeglob = 0;
96 arglist.argcnt = 0;
97 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
98 arglist.glob.gl_opendir = (void *)rst_opendir;
99 arglist.glob.gl_readdir = (void *)glob_readdir;
100 arglist.glob.gl_closedir = (void *)rst_closedir;
101 arglist.glob.gl_lstat = glob_stat;
102 arglist.glob.gl_stat = glob_stat;
103 canon("/", curdir, sizeof(curdir));
104 loop:
105 if (setjmp(reset) != 0) {
106 if (arglist.freeglob != 0) {
107 arglist.freeglob = 0;
108 arglist.argcnt = 0;
109 globfree(&arglist.glob);
111 nextarg = NULL;
112 volno = 0;
114 runshell = 1;
115 getcmd(curdir, cmd, name, sizeof(name), &arglist);
116 switch (cmd[0]) {
118 * Add elements to the extraction list.
120 case 'a':
121 if (strncmp(cmd, "add", strlen(cmd)) != 0)
122 goto bad;
123 ino = dirlookup(name);
124 if (ino == 0)
125 break;
126 if (mflag)
127 pathcheck(name);
128 treescan(name, ino, addfile);
129 break;
131 * Change working directory.
133 case 'c':
134 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
135 goto bad;
136 ino = dirlookup(name);
137 if (ino == 0)
138 break;
139 if (inodetype(ino) == LEAF) {
140 fprintf(stderr, "%s: not a directory\n", name);
141 break;
143 strcpy(curdir, name);
144 break;
146 * Delete elements from the extraction list.
148 case 'd':
149 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
150 goto bad;
151 np = lookupname(name);
152 if (np == NULL || (np->e_flags & NEW) == 0) {
153 fprintf(stderr, "%s: not on extraction list\n", name);
154 break;
156 treescan(name, np->e_ino, deletefile);
157 break;
159 * Extract the requested list.
161 case 'e':
162 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
163 goto bad;
164 createfiles();
165 createlinks();
166 setdirmodes(0);
167 if (dflag)
168 checkrestore();
169 volno = 0;
170 break;
172 * List available commands.
174 case 'h':
175 if (strncmp(cmd, "help", strlen(cmd)) != 0)
176 goto bad;
177 case '?':
178 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
179 "Available commands are:\n",
180 "\tls [arg] - list directory\n",
181 "\tcd arg - change directory\n",
182 "\tpwd - print current directory\n",
183 "\tadd [arg] - add `arg' to list of",
184 " files to be extracted\n",
185 "\tdelete [arg] - delete `arg' from",
186 " list of files to be extracted\n",
187 "\textract - extract requested files\n",
188 "\tsetmodes - set modes of requested directories\n",
189 "\tquit - immediately exit program\n",
190 "\twhat - list dump header information\n",
191 "\tverbose - toggle verbose flag",
192 " (useful with ``ls'')\n",
193 "\thelp or `?' - print this list\n",
194 "If no `arg' is supplied, the current",
195 " directory is used\n");
196 break;
198 * List a directory.
200 case 'l':
201 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
202 goto bad;
203 printlist(name, curdir);
204 break;
206 * Print current directory.
208 case 'p':
209 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
210 goto bad;
211 if (curdir[1] == '\0')
212 fprintf(stderr, "/\n");
213 else
214 fprintf(stderr, "%s\n", &curdir[1]);
215 break;
217 * Quit.
219 case 'q':
220 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
221 goto bad;
222 return;
223 case 'x':
224 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
225 goto bad;
226 return;
228 * Toggle verbose mode.
230 case 'v':
231 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
232 goto bad;
233 if (vflag) {
234 fprintf(stderr, "verbose mode off\n");
235 vflag = 0;
236 break;
238 fprintf(stderr, "verbose mode on\n");
239 vflag++;
240 break;
242 * Just restore requested directory modes.
244 case 's':
245 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
246 goto bad;
247 setdirmodes(FORCE);
248 break;
250 * Print out dump header information.
252 case 'w':
253 if (strncmp(cmd, "what", strlen(cmd)) != 0)
254 goto bad;
255 printdumpinfo();
256 break;
258 * Turn on debugging.
260 case 'D':
261 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
262 goto bad;
263 if (dflag) {
264 fprintf(stderr, "debugging mode off\n");
265 dflag = 0;
266 break;
268 fprintf(stderr, "debugging mode on\n");
269 dflag++;
270 break;
272 * Unknown command.
274 default:
275 bad:
276 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
277 break;
279 goto loop;
283 * Read and parse an interactive command.
284 * The first word on the line is assigned to "cmd". If
285 * there are no arguments on the command line, then "curdir"
286 * is returned as the argument. If there are arguments
287 * on the line they are returned one at a time on each
288 * successive call to getcmd. Each argument is first assigned
289 * to "name". If it does not start with "/" the pathname in
290 * "curdir" is prepended to it. Finally "canon" is called to
291 * eliminate any embedded ".." components.
293 static void
294 getcmd(char *curdir, char *cmd, char *name, size_t size, struct arglist *ap)
296 char *cp;
297 static char input[BUFSIZ];
298 char output[BUFSIZ];
299 # define rawname input /* save space by reusing input buffer */
302 * Check to see if still processing arguments.
304 if (ap->argcnt > 0)
305 goto retnext;
306 if (nextarg != NULL)
307 goto getnext;
309 * Read a command line and trim off trailing white space.
311 do {
312 fprintf(stderr, "restore > ");
313 fflush(stderr);
314 if (fgets(input, BUFSIZ, terminal) == NULL) {
315 strcpy(cmd, "quit");
316 return;
318 } while (input[0] == '\n');
319 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
320 /* trim off trailing white space and newline */;
321 *++cp = '\0';
323 * Copy the command into "cmd".
325 cp = copynext(input, cmd);
326 ap->cmd = cmd;
328 * If no argument, use curdir as the default.
330 if (*cp == '\0') {
331 strncpy(name, curdir, size);
332 name[size - 1] = '\0';
333 return;
335 nextarg = cp;
337 * Find the next argument.
339 getnext:
340 cp = copynext(nextarg, rawname);
341 if (*cp == '\0')
342 nextarg = NULL;
343 else
344 nextarg = cp;
346 * If it is an absolute pathname, canonicalize it and return it.
348 if (rawname[0] == '/') {
349 canon(rawname, name, size);
350 } else {
352 * For relative pathnames, prepend the current directory to
353 * it then canonicalize and return it.
355 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
356 canon(output, name, size);
358 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
359 fprintf(stderr, "%s: out of memory\n", ap->cmd);
360 if (ap->glob.gl_pathc == 0)
361 return;
362 ap->freeglob = 1;
363 ap->argcnt = ap->glob.gl_pathc;
365 retnext:
366 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
367 name[size - 1] = '\0';
368 if (--ap->argcnt == 0) {
369 ap->freeglob = 0;
370 globfree(&ap->glob);
372 # undef rawname
376 * Strip off the next token of the input.
378 static char *
379 copynext(char *input, char *output)
381 char *cp, *bp;
382 char quote;
384 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
385 /* skip to argument */;
386 bp = output;
387 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
389 * Handle back slashes.
391 if (*cp == '\\') {
392 if (*++cp == '\0') {
393 fprintf(stderr,
394 "command lines cannot be continued\n");
395 continue;
397 *bp++ = *cp++;
398 continue;
401 * The usual unquoted case.
403 if (*cp != '\'' && *cp != '"') {
404 *bp++ = *cp++;
405 continue;
408 * Handle single and double quotes.
410 quote = *cp++;
411 while (*cp != quote && *cp != '\0')
412 *bp++ = *cp++ | 0200;
413 if (*cp++ == '\0') {
414 fprintf(stderr, "missing %c\n", quote);
415 cp--;
416 continue;
419 *bp = '\0';
420 return (cp);
424 * Canonicalize file names to always start with ``./'' and
425 * remove any embedded "." and ".." components.
427 void
428 canon(const char *rawname, char *canonname, size_t len)
430 char *cp, *np;
432 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
433 strcpy(canonname, "");
434 else if (rawname[0] == '/')
435 strcpy(canonname, ".");
436 else
437 strcpy(canonname, "./");
438 if (strlen(canonname) + strlen(rawname) >= len) {
439 fprintf(stderr, "canonname: not enough buffer space\n");
440 done(1);
443 strcat(canonname, rawname);
445 * Eliminate multiple and trailing '/'s
447 for (cp = np = canonname; *np != '\0'; cp++) {
448 *cp = *np++;
449 while (*cp == '/' && *np == '/')
450 np++;
452 *cp = '\0';
453 if (*--cp == '/')
454 *cp = '\0';
456 * Eliminate extraneous "." and ".." from pathnames.
458 for (np = canonname; *np != '\0'; ) {
459 np++;
460 cp = np;
461 while (*np != '/' && *np != '\0')
462 np++;
463 if (np - cp == 1 && *cp == '.') {
464 cp--;
465 strcpy(cp, np);
466 np = cp;
468 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
469 cp--;
470 while (cp > &canonname[1] && *--cp != '/')
471 /* find beginning of name */;
472 strcpy(cp, np);
473 np = cp;
479 * Do an "ls" style listing of a directory
481 static void
482 printlist(const char *name, char *basename)
484 struct afile *fp, *list, *listp = NULL;
485 struct direct *dp;
486 struct afile single;
487 RST_DIR *dirp;
488 int entries, len, namelen;
489 char locname[MAXPATHLEN + 1];
491 dp = pathsearch(name);
492 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
493 (!vflag && dp->d_ino == WINO))
494 return;
495 if ((dirp = rst_opendir(name)) == NULL) {
496 entries = 1;
497 list = &single;
498 mkentry(name, dp, list);
499 len = strlen(basename) + 1;
500 if (strlen(name) - len > (unsigned short)single.len) {
501 freename(single.fname);
502 single.fname = savename(&name[len]);
503 single.len = strlen(single.fname);
505 } else {
506 entries = 0;
507 while ((dp = rst_readdir(dirp)))
508 entries++;
509 rst_closedir(dirp);
510 list = (struct afile *)malloc(entries * sizeof(struct afile));
511 if (list == NULL) {
512 fprintf(stderr, "ls: out of memory\n");
513 return;
515 if ((dirp = rst_opendir(name)) == NULL)
516 panic("directory reopen failed\n");
517 fprintf(stderr, "%s:\n", name);
518 entries = 0;
519 listp = list;
520 strncpy(locname, name, MAXPATHLEN);
521 strncat(locname, "/", MAXPATHLEN);
522 namelen = strlen(locname);
523 while ((dp = rst_readdir(dirp))) {
524 if (dp == NULL)
525 break;
526 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
527 continue;
528 if (!vflag && (dp->d_ino == WINO ||
529 strcmp(dp->d_name, ".") == 0 ||
530 strcmp(dp->d_name, "..") == 0))
531 continue;
532 locname[namelen] = '\0';
533 if (namelen + dp->d_namlen >= MAXPATHLEN) {
534 fprintf(stderr, "%s%s: name exceeds %d char\n",
535 locname, dp->d_name, MAXPATHLEN);
536 } else {
537 strncat(locname, dp->d_name, (int)dp->d_namlen);
538 mkentry(locname, dp, listp++);
539 entries++;
542 rst_closedir(dirp);
543 if (entries == 0) {
544 fprintf(stderr, "\n");
545 free(list);
546 return;
548 qsort((char *)list, entries, sizeof(struct afile), fcmp);
550 formatf(list, entries);
551 if (dirp != NULL) {
552 for (fp = listp - 1; fp >= list; fp--)
553 freename(fp->fname);
554 fprintf(stderr, "\n");
555 free(list);
560 * Read the contents of a directory.
562 static void
563 mkentry(const char *name, struct direct *dp, struct afile *fp)
565 char *cp;
566 struct entry *np;
568 fp->fnum = dp->d_ino;
569 fp->fname = savename(dp->d_name);
570 for (cp = fp->fname; *cp; cp++)
571 if (!vflag && (*cp < ' ' || *cp >= 0177))
572 *cp = '?';
573 fp->len = cp - fp->fname;
574 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
575 fp->prefix = '^';
576 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
577 fp->prefix = '*';
578 else
579 fp->prefix = ' ';
580 switch(dp->d_type) {
582 default:
583 fprintf(stderr, "Warning: undefined file type %d\n",
584 dp->d_type);
585 /* fall through */
586 case DT_REG:
587 fp->postfix = ' ';
588 break;
590 case DT_LNK:
591 fp->postfix = '@';
592 break;
594 case DT_FIFO:
595 case DT_SOCK:
596 fp->postfix = '=';
597 break;
599 case DT_CHR:
600 case DT_BLK:
601 fp->postfix = '#';
602 break;
604 case DT_WHT:
605 fp->postfix = '%';
606 break;
608 case DT_UNKNOWN:
609 case DT_DIR:
610 if (inodetype(dp->d_ino) == NODE)
611 fp->postfix = '/';
612 else
613 fp->postfix = ' ';
614 break;
616 return;
620 * Print out a pretty listing of a directory
622 static void
623 formatf(struct afile *list, int nentry)
625 struct afile *fp, *endlist;
626 unsigned int bigino;
627 int width, haveprefix, havepostfix;
628 int i, j, w, precision = 0, columns, lines;
630 width = 0;
631 haveprefix = 0;
632 havepostfix = 0;
633 bigino = ROOTINO;
634 endlist = &list[nentry];
635 for (fp = &list[0]; fp < endlist; fp++) {
636 if (bigino < fp->fnum)
637 bigino = fp->fnum;
638 if (width < fp->len)
639 width = fp->len;
640 if (fp->prefix != ' ')
641 haveprefix = 1;
642 if (fp->postfix != ' ')
643 havepostfix = 1;
645 if (haveprefix)
646 width++;
647 if (havepostfix)
648 width++;
649 if (vflag) {
650 for (precision = 0, i = bigino; i > 0; i /= 10)
651 precision++;
652 width += precision + 1;
654 width++;
655 columns = 81 / width;
656 if (columns == 0)
657 columns = 1;
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];
662 if (vflag) {
663 fprintf(stderr, "%*d ", precision, fp->fnum);
664 fp->len += precision + 1;
666 if (haveprefix) {
667 putc(fp->prefix, stderr);
668 fp->len++;
670 fprintf(stderr, "%s", fp->fname);
671 if (havepostfix) {
672 putc(fp->postfix, stderr);
673 fp->len++;
675 if (fp + lines >= endlist) {
676 fprintf(stderr, "\n");
677 break;
679 for (w = fp->len; w < width; w++)
680 putc(' ', stderr);
686 * Skip over directory entries that are not on the tape
688 * First have to get definition of a dirent.
690 #undef DIRBLKSIZ
691 #include <dirent.h>
692 #undef d_ino
694 struct dirent *
695 glob_readdir(RST_DIR *dirp)
697 enum {
698 ADIRENT_STORAGE_LEN = _DIRENT_RECLEN(NAME_MAX),
700 static union {
701 uint8_t storage[ADIRENT_STORAGE_LEN];
702 struct dirent alignment;
703 } adirent;
704 struct direct *dp;
705 struct dirent *adp;
707 while ((dp = rst_readdir(dirp)) != NULL) {
708 if (!vflag && dp->d_ino == WINO)
709 continue;
710 if (dflag || TSTINO(dp->d_ino, dumpmap))
711 break;
713 if (dp == NULL)
714 return (NULL);
715 adp = (struct dirent *)&adirent;
716 adp->d_fileno = dp->d_ino;
717 adp->d_namlen = dp->d_namlen;
718 strcpy(adp->d_name, dp->d_name);
719 return (adp);
723 * Return st_mode information in response to stat or lstat calls
725 static int
726 glob_stat(const char *name, struct stat *stp)
728 struct direct *dp;
730 dp = pathsearch(name);
731 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
732 (!vflag && dp->d_ino == WINO))
733 return (-1);
734 if (inodetype(dp->d_ino) == NODE)
735 stp->st_mode = IFDIR;
736 else
737 stp->st_mode = IFREG;
738 return (0);
742 * Comparison routine for qsort.
744 static int
745 fcmp(const void *f1, const void *f2)
747 return (strcmp(((const struct afile *)f1)->fname,
748 ((const struct afile *)f2)->fname));
752 * respond to interrupts
754 void
755 onintr(int signo __unused)
757 if (command == 'i' && runshell)
758 longjmp(reset, 1);
759 if (reply("restore interrupted, continue") == FAIL)
760 done(1);