Debian Codegraph @

restore/interactive.c

raw symbols SCIP
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1985, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "config.h" // IWYU pragma: keep
39 #include "interactive.h"
40
41 #include <sys/param.h>
42 #include <sys/stat.h>
43
44 #include "compat/include/bsdcompat.h"
45 #include "compat/include/protocols/dumprestore.h"
46
47 #include <setjmp.h>
48 #include "compat/include/compaterr.h" // IWYU pragma: keep
49 #include <errno.h>
50 #include "compat/include/compatglob.h"
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <ctype.h>
55 #include <dirent.h>
56
57 #include "restore.h"
58 #include "dirs.h"
59 #include "utilities.h"
60 #include "symtab.h"
61 #include "tape.h"
62 #include "restore_globals.h"
63
64 extern char * __progname;
65
66 #if HAVE_READLINE
67 #include <readline/readline.h>
68 #include <readline/history.h>
69
70 static const char *rl_gets (char *prompt);
71 static void initialize_readline(void);
72 static char **restore_completion (const char *text, int start, int end);
73 static char *command_generator(const char *text, int state);
74 static char *filename_generator(const char *text, int state);
75 #endif
76
77 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
78
79 /*
80 * Things to handle interruptions.
81 */
82 static int runshell;
83 static jmp_buf reset;
84 static char *nextarg = NULL;
85 static int pflag = 0; /* prompt mode */
86 /*
87 * Structure and routines associated with listing directories.
88 */
89 struct afile {
90 dump_ino_t fnum; /* inode number of file */
91 char *fname; /* file name */
92 short len; /* name length */
93 char prefix; /* prefix character */
94 char postfix; /* postfix character */
95 };
96 struct arglist {
97 int freeglob; /* glob structure needs to be freed */
98 int argcnt; /* next globbed argument to return */
99 glob_t glob; /* globbing information */
100 char *cmd; /* the current command */
101 };
102
103 static char *copynext (char *, char *);
104 static int fcmp (const void *, const void *);
105 static void formatf (struct afile *, int);
106 static void getcmd (char *, char *, char *, int, struct arglist *);
107 static struct dirent *glob_readdir (void *dirp);
108 static int glob_stat (const char *, struct stat *);
109 static void mkentry (char *, struct direct *, struct afile *);
110 static void printlist (char *, char *);
111
112 /*
113 * Read and execute commands from the terminal.
114 */
115 void
116 runcmdshell(void)
117 {
118 struct entry *np;
119 dump_ino_t ino;
120 struct arglist arglist;
121 char curdir[MAXPATHLEN];
122 char name[MAXPATHLEN];
123 char cmd[BUFSIZ];
124
125 #if HAVE_READLINE
126 initialize_readline();
127 #endif
128 arglist.freeglob = 0;
129 arglist.argcnt = 0;
130 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
131 arglist.glob.gl_opendir = rst_opendir;
132 arglist.glob.gl_readdir = glob_readdir;
133 arglist.glob.gl_closedir = rst_closedir;
134 arglist.glob.gl_lstat = glob_stat;
135 arglist.glob.gl_stat = glob_stat;
136 canon("/", curdir, sizeof(curdir));
137 loop:
138 if (setjmp(reset) != 0) {
139 if (arglist.freeglob != 0) {
140 arglist.freeglob = 0;
141 arglist.argcnt = 0;
142 globfree(&arglist.glob);
143 }
144 nextarg = NULL;
145 volno = 0;
146 }
147 runshell = 1;
148 getcmd(curdir, cmd, name, sizeof(name), &arglist);
149 switch (cmd[0]) {
150 /*
151 * Add elements to the extraction list.
152 */
153 case 'a':
154 if (strncmp(cmd, "add", strlen(cmd)) != 0)
155 goto bad;
156 ino = dirlookup(name);
157 if (ino == 0)
158 break;
159 if (mflag)
160 pathcheck(name);
161 treescan(name, ino, addfile);
162 break;
163 /*
164 * Change working directory.
165 */
166 case 'c':
167 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
168 goto bad;
169 ino = dirlookup(name);
170 if (ino == 0)
171 break;
172 if (inodetype(ino) == LEAF) {
173 fprintf(stderr, "%s: not a directory\n", name);
174 break;
175 }
176 (void) strncpy(curdir, name, sizeof(curdir));
177 curdir[sizeof(curdir) - 1] = '\0';
178 break;
179 /*
180 * Delete elements from the extraction list.
181 */
182 case 'd':
183 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
184 goto bad;
185 np = lookupname(name);
186 if (np == NULL || (np->e_flags & NEW) == 0) {
187 fprintf(stderr, "%s: not on extraction list\n", name);
188 break;
189 }
190 treescan(name, np->e_ino, deletefile);
191 break;
192 /*
193 * Extract the requested list.
194 */
195 case 'e':
196 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
197 goto bad;
198 createfiles();
199 createlinks();
200 setdirmodes(oflag);
201 if (dflag)
202 checkrestore();
203 volno = 0;
204 break;
205 /*
206 * List available commands.
207 */
208 case 'h':
209 if (strncmp(cmd, "help", strlen(cmd)) != 0)
210 goto bad;
211 __attribute__((fallthrough));
212 case '?':
213 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
214 "Available commands are:\n",
215 "\tls [arg] - list directory\n",
216 "\tcd arg - change directory\n",
217 "\tpwd - print current directory\n",
218 "\tadd [arg] - add `arg' to list of",
219 " files to be extracted\n",
220 "\tdelete [arg] - delete `arg' from",
221 " list of files to be extracted\n",
222 "\textract - extract requested files\n",
223 "\tsetmodes - set modes of requested directories\n",
224 "\tquit - immediately exit program\n",
225 "\twhat - list dump header information\n",
226 "\tverbose - toggle verbose flag",
227 " (useful with ``ls'')\n",
228 "\tprompt - toggle the prompt display\n",
229 "\thelp or `?' - print this list\n",
230 "If no `arg' is supplied, the current",
231 " directory is used\n");
232 break;
233 /*
234 * List a directory.
235 */
236 case 'l':
237 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
238 goto bad;
239 printlist(name, curdir);
240 break;
241 /*
242 * Print current directory.
243 */
244 case 'p':
245 if (strncmp(cmd, "pwd", strlen(cmd)) == 0) {
246 if (curdir[1] == '\0')
247 fprintf(stderr, "/\n");
248 else
249 fprintf(stderr, "%s\n", &curdir[1]);
250 }
251 /*
252 * Toggle prompt mode.
253 */
254 else if (strncmp(cmd, "prompt", strlen(cmd)) == 0) {
255 if (pflag) {
256 fprintf(stderr, "prompt mode off\n");
257 pflag = 0;
258 break;
259 }
260 fprintf(stderr, "prompt mode on\n");
261 pflag++;
262 break;
263 }
264 else goto bad;
265 break;
266 /*
267 * Quit.
268 */
269 case 'q':
270 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
271 goto bad;
272 return;
273 case 'x':
274 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
275 goto bad;
276 return;
277 /*
278 * Toggle verbose mode.
279 */
280 case 'v':
281 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
282 goto bad;
283 if (vflag) {
284 fprintf(stderr, "verbose mode off\n");
285 vflag = 0;
286 break;
287 }
288 fprintf(stderr, "verbose mode on\n");
289 vflag++;
290 break;
291 /*
292 * Just restore requested directory modes.
293 */
294 case 's':
295 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
296 goto bad;
297 setdirmodes(FORCE);
298 break;
299 /*
300 * Print out dump header information.
301 */
302 case 'w':
303 if (strncmp(cmd, "what", strlen(cmd)) != 0)
304 goto bad;
305 printdumpinfo();
306 printvolinfo();
307 break;
308 /*
309 * Turn on debugging.
310 */
311 case 'D':
312 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
313 goto bad;
314 if (dflag) {
315 fprintf(stderr, "debugging mode off\n");
316 dflag = 0;
317 break;
318 }
319 fprintf(stderr, "debugging mode on\n");
320 dflag = ~0;
321 break;
322 /*
323 * Unknown command.
324 */
325 default:
326 bad:
327 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
328 break;
329 }
330 goto loop;
331 }
332
333 /*
334 * Read and parse an interactive command.
335 * The first word on the line is assigned to "cmd". If
336 * there are no arguments on the command line, then "curdir"
337 * is returned as the argument. If there are arguments
338 * on the line they are returned one at a time on each
339 * successive call to getcmd. Each argument is first assigned
340 * to "name". If it does not start with "/" the pathname in
341 * "curdir" is prepended to it. Finally "canon" is called to
342 * eliminate any embedded ".." components.
343 */
344 static void
345 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
346 {
347 char *cp;
348 static char input[BUFSIZ];
349 char output[BUFSIZ];
350 # define rawname input /* save space by reusing input buffer */
351
352 /*
353 * Check to see if still processing arguments.
354 */
355 if (ap->argcnt > 0)
356 goto retnext;
357 if (nextarg != NULL)
358 goto getnext;
359 /*
360 * Read a command line and trim off trailing white space.
361 */
362 #if HAVE_READLINE
363 snprintf(input, BUFSIZ, "%s\n", rl_gets(curdir));
364 #else
365 do {
366 if (pflag)
367 fprintf(stderr, "%s:%s:%s > ",
368 __progname,
369 spcl.c_filesys,
370 curdir[1] ? &curdir[1] : "/");
371 else
372 fprintf(stderr, "%s > ", __progname);
373 (void) fflush(stderr);
374 (void) fgets(input, BUFSIZ, terminal);
375 } while (!feof(terminal) && input[0] == '\n');
376 if (feof(terminal)) {
377 (void) strcpy(cmd, "quit");
378 return;
379 }
380 #endif
381 for (cp = &input[strlen(input) - 2]; *cp == ' '; cp--)
382 /* trim off trailing white space and newline */;
383 *++cp = '\0';
384 /*
385 * Copy the command into "cmd".
386 */
387 cp = copynext(input, cmd);
388 ap->cmd = cmd;
389 /*
390 * If no argument, use curdir as the default.
391 */
392 if (*cp == '\0') {
393 (void) strncpy(name, curdir, size);
394 name[size - 1] = '\0';
395 return;
396 }
397 nextarg = cp;
398 /*
399 * Find the next argument.
400 */
401 getnext:
402 cp = copynext(nextarg, rawname);
403 if (*cp == '\0')
404 nextarg = NULL;
405 else
406 nextarg = cp;
407 /*
408 * If it is an absolute pathname, canonicalize it and return it.
409 */
410 if (rawname[0] == '/') {
411 canon(rawname, name, size);
412 } else {
413 /*
414 * For relative pathnames, prepend the current directory to
415 * it then canonicalize and return it.
416 */
417 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
418 canon(output, name, size);
419 }
420 if (glob(name, GLOB_ALTDIRFUNC
421 #ifndef HAVE_GLOB
422 |GLOB_QUOTE
423 #endif
424 , NULL, &ap->glob) < 0)
425 fprintf(stderr, "%s: out of memory\n", ap->cmd);
426 if (ap->glob.gl_pathc == 0)
427 return;
428 ap->freeglob = 1;
429 ap->argcnt = ap->glob.gl_pathc;
430
431 retnext:
432 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size - 1);
433 name[size - 1] = '\0';
434 if (--ap->argcnt == 0) {
435 ap->freeglob = 0;
436 globfree(&ap->glob);
437 }
438 # undef rawname
439 }
440
441 static int decode_escape(char* input, char* output)
442 {
443 int len = 1;
444 #define write(v) { if(output) *output++ = v; }
445 switch(*++input) {
446 case 0:
447 return 1;
448 case 'a':
449 write('\a');
450 len++;
451 break;
452 case 'b':
453 write('\b');
454 len++;
455 break;
456 case 'e':
457 case 'E':
458 write('\e');
459 len++;
460 break;
461 case 'f':
462 write('\f');
463 len++;
464 break;
465 case 'n':
466 write('\n');
467 len++;
468 break;
469 case 't':
470 write('\t');
471 len++;
472 break;
473 case 'v':
474 write('\v');
475 len++;
476 break;
477 case '0': case '1': case '2': case '3':
478 case '4': case '5': case '6': case '7': {
479 int c = 0;
480 for (int i = 0; i < 3; ++i) {
481 c = c*8 + *input++ - '0';
482 len++;
483 if (input[0] < '0' || input[0] > '7' || c >= 32)
484 break;
485 }
486 write(c);
487 break;
488 }
489 default:
490 write(*input++);
491 len++;
492 break;
493 }
494 return len;
495 #undef write
496 }
497
498 static int encode_escape_char(char input, char* output)
499 {
500 int len = 0;
501 #define write(v) { if(output) output[len] = v; len++; }
502 switch(input) {
503 case '\a':
504 write('\\');
505 write('a');
506 break;
507 case '\b':
508 write('\\');
509 write('b');
510 break;
511 case '\e':
512 write('\\');
513 write('e');
514 break;
515 case '\f':
516 write('\\');
517 write('f');
518 break;
519 case '\n':
520 write('\\');
521 write('n');
522 break;
523 case '\t':
524 write('\\');
525 write('t');
526 break;
527 case '\v':
528 write('\\');
529 write('v');
530 break;
531 case '?':
532 case '*':
533 case '[':
534 case ']':
535 case '"':
536 case '\'':
537 case '\\':
538 case ' ':
539 write('\\');
540 write(input);
541 break;
542 default:
543 if(isprint(input)) {
544 write(input);
545 } else {
546 write('\\');
547 int c = (unsigned char)input;
548 write((c/64)+'0');
549 c = c%64;
550 write((c/8)+'0');
551 c = c%8;
552 write(c+'0');
553 }
554 break;
555 }
556 return len;
557 #undef write
558 }
559
560 static char* encode_escape(const char* input) {
561 const char* s = input;
562 int len = 0;
563 while(*s)
564 len += encode_escape_char(*s++, NULL);
565 char* outbuf = (char*)malloc(len+1);
566 char* output = outbuf;
567 while(*input)
568 output += encode_escape_char(*input++, output);
569 *output++ = 0;
570 return outbuf;
571 }
572
573 /*
574 * Strip off the next token of the input.
575 */
576 static char *
577 copynext(char *input, char *output)
578 {
579 char *cp;
580 char *bp;
581 char quote = 0;
582
583 #define fix_quoting() { \
584 switch (*(bp-1)) { \
585 case '?': \
586 case '*': \
587 case '[': \
588 case ']': \
589 case '"': \
590 case '\'': \
591 case '\\': \
592 *(bp) = *(bp-1); \
593 *(bp-1) = '\\'; \
594 bp++; \
595 break; \
596 } \
597 }
598
599 for (cp = input; *cp == ' '; cp++)
600 /* skip to argument */;
601 bp = output;
602 while (quote || (*cp != 0 && *cp != ' ')) {
603 if (*cp == 0) {
604 fprintf(stderr, "missing %c\n", quote);
605 break;
606 }
607 /*
608 * Handle back slashes.
609 */
610 if (*cp == '\\') {
611 int r = decode_escape(cp, bp++);
612 cp += r;
613 if(r == 1) {
614 fprintf(stderr,
615 "command lines cannot be continued\n");
616 break;
617 }
618 fix_quoting();
619 }else {
620 /*
621 * The usual unescaped case.
622 */
623 if ((*cp != '\'' && *cp != '"') || (quote && *cp != quote)) {
624 *bp++ = *cp++;
625 if (quote)
626 fix_quoting();
627 continue;
628 }
629 /*
630 * Handle single and double quotes.
631 */
632 if (!quote) {
633 quote = *cp++;
634 } else {
635 // End of quoted part
636 quote = 0;
637 cp++;
638 }
639 }
640 }
641 *bp = '\0';
642 return cp;
643 #undef fix_quoting
644 }
645
646 /*
647 * Canonicalize file names to always start with ``./'' and
648 * remove any embedded "." and ".." components.
649 */
650 void
651 canon(const char *rawname, char *canonname, size_t len)
652 {
653 char *cp, *np;
654
655 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
656 (void) strcpy(canonname, "");
657 else if (rawname[0] == '/')
658 (void) strcpy(canonname, ".");
659 else
660 (void) strcpy(canonname, "./");
661 if (strlen(canonname) + strlen(rawname) >= len)
662 errx(1, "canonname: not enough buffer space");
663
664 (void) strcat(canonname, rawname);
665 /*
666 * Eliminate multiple and trailing '/'s
667 */
668 for (cp = np = canonname; *np != '\0'; cp++) {
669 *cp = *np++;
670 while (*cp == '/' && *np == '/')
671 np++;
672 }
673 *cp = '\0';
674 if (*--cp == '/')
675 *cp = '\0';
676 /*
677 * Eliminate extraneous "." and ".." from pathnames.
678 */
679 for (np = canonname; *np != '\0'; ) {
680 np++;
681 cp = np;
682 while (*np != '/' && *np != '\0')
683 np++;
684 if (np - cp == 1 && *cp == '.') {
685 cp--;
686 (void) strcpy(cp, np);
687 np = cp;
688 }
689 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
690 cp--;
691 while (cp > &canonname[1] && *--cp != '/')
692 /* find beginning of name */;
693 (void) strcpy(cp, np);
694 np = cp;
695 }
696 }
697 }
698
699 /*
700 * Do an "ls" style listing of a directory
701 */
702 static void
703 printlist(char *name, char *basename)
704 {
705 struct afile *fp, *list, *listp = NULL;
706 struct direct *dp;
707 struct afile single;
708 RST_DIR *dirp;
709 int entries, len, namelen;
710 char locname[MAXPATHLEN + 1];
711
712 dp = pathsearch(name);
713 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpinomap) == 0) ||
714 (!vflag && dp->d_ino == WINO))
715 return;
716 if ((dirp = rst_opendir(name)) == NULL) {
717 entries = 1;
718 list = &single;
719 mkentry(name, dp, list);
720 len = strlen(basename) + 1;
721 if (strlen(name) - len > (size_t)single.len) {
722 freename(single.fname);
723 single.fname = savename(&name[len]);
724 single.len = strlen(single.fname);
725 }
726 } else {
727 entries = 0;
728 while ((dp = rst_readdir(dirp)))
729 entries++;
730 rst_closedir(dirp);
731 list = (struct afile *)malloc(entries * sizeof(struct afile));
732 if (list == NULL) {
733 fprintf(stderr, "ls: out of memory\n");
734 return;
735 }
736 if ((dirp = rst_opendir(name)) == NULL)
737 panic("directory reopen failed\n");
738 fprintf(stderr, "%s:\n", name);
739 entries = 0;
740 listp = list;
741 namelen = snprintf(locname, sizeof(locname), "%s/", name);
742 if (namelen >= (int)sizeof(locname))
743 namelen = sizeof(locname) - 1;
744 while ((dp = rst_readdir(dirp))) {
745 if (dp == NULL)
746 break;
747 if (!dflag && TSTINO(dp->d_ino, dumpinomap) == 0)
748 continue;
749 if (!vflag && (dp->d_ino == WINO ||
750 strcmp(dp->d_name, ".") == 0 ||
751 strcmp(dp->d_name, "..") == 0))
752 continue;
753 locname[namelen] = '\0';
754 if (namelen + strlen(dp->d_name) >= MAXPATHLEN) {
755 fprintf(stderr, "%s%s: name exceeds %d char\n",
756 locname, dp->d_name, MAXPATHLEN);
757 } else {
758 (void) strncat(locname, dp->d_name,
759 sizeof(locname)-namelen);
760 mkentry(locname, dp, listp++);
761 entries++;
762 }
763 }
764 rst_closedir(dirp);
765 if (entries == 0) {
766 fprintf(stderr, "\n");
767 free(list);
768 return;
769 }
770 qsort((char *)list, entries, sizeof(struct afile), fcmp);
771 }
772 formatf(list, entries);
773 if (dirp != NULL) {
774 for (fp = listp - 1; fp >= list; fp--)
775 freename(fp->fname);
776 fprintf(stderr, "\n");
777 free(list);
778 }
779 }
780
781 /*
782 * Read the contents of a directory.
783 */
784 static void
785 mkentry(char *name, struct direct *dp, struct afile *fp)
786 {
787 char *cp;
788 struct entry *np;
789
790 fp->fnum = dp->d_ino;
791 cp = encode_escape(dp->d_name);
792 fp->fname = savename(cp);
793 free(cp);
794 fp->len = strlen(fp->fname);
795 if (dflag && TSTINO(fp->fnum, dumpinomap) == 0)
796 fp->prefix = '^';
797 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
798 fp->prefix = '*';
799 else
800 fp->prefix = ' ';
801 switch(dp->d_type) {
802
803 default:
804 fprintf(stderr, "Warning: undefined file type %d\n",
805 dp->d_type);
806 /* fall through */
807 case DT_REG:
808 fp->postfix = ' ';
809 break;
810
811 case DT_LNK:
812 fp->postfix = '@';
813 break;
814
815 case DT_FIFO:
816 case DT_SOCK:
817 fp->postfix = '=';
818 break;
819
820 case DT_CHR:
821 case DT_BLK:
822 fp->postfix = '#';
823 break;
824
825 case DT_UNKNOWN:
826 case DT_DIR:
827 if (inodetype(dp->d_ino) == NODE)
828 fp->postfix = '/';
829 else
830 fp->postfix = ' ';
831 break;
832 }
833 return;
834 }
835
836 /*
837 * Print out a pretty listing of a directory
838 */
839 static void
840 formatf(struct afile *list, int nentry)
841 {
842 struct afile *fp, *endlist;
843 int width, bigino, haveprefix, havepostfix;
844 int i, j, w, precision = 0, columns, lines;
845
846 width = 0;
847 haveprefix = 0;
848 havepostfix = 0;
849 bigino = ROOTINO;
850 endlist = &list[nentry];
851 for (fp = &list[0]; fp < endlist; fp++) {
852 if (bigino < (int)fp->fnum)
853 bigino = fp->fnum;
854 if (width < fp->len)
855 width = fp->len;
856 if (fp->prefix != ' ')
857 haveprefix = 1;
858 if (fp->postfix != ' ')
859 havepostfix = 1;
860 }
861 if (haveprefix)
862 width++;
863 if (havepostfix)
864 width++;
865 if (vflag) {
866 for (precision = 0, i = bigino; i > 0; i /= 10)
867 precision++;
868 width += precision + 1;
869 }
870 width++;
871 columns = 81 / width;
872 if (columns == 0)
873 columns = 1;
874 lines = (nentry + columns - 1) / columns;
875 for (i = 0; i < lines; i++) {
876 for (j = 0; j < columns; j++) {
877 fp = &list[j * lines + i];
878 if (vflag) {
879 fprintf(stderr, "%*u ", precision, fp->fnum);
880 fp->len += precision + 1;
881 }
882 if (haveprefix) {
883 putc(fp->prefix, stderr);
884 fp->len++;
885 }
886 fprintf(stderr, "%s", fp->fname);
887 if (havepostfix) {
888 putc(fp->postfix, stderr);
889 fp->len++;
890 }
891 if (fp + lines >= endlist) {
892 fprintf(stderr, "\n");
893 break;
894 }
895 for (w = fp->len; w < width; w++)
896 putc(' ', stderr);
897 }
898 }
899 }
900
901 struct dirent *
902 glob_readdir(void *argp)
903 {
904 RST_DIR *dirp = argp;
905 struct direct *dp;
906 static struct dirent adirent;
907
908 while ((dp = rst_readdir(dirp)) != NULL) {
909 if (!vflag && dp->d_ino == WINO)
910 continue;
911 if (dflag || TSTINO(dp->d_ino, dumpinomap))
912 break;
913 }
914 if (dp == NULL)
915 return (NULL);
916 adirent.d_ino = dp->d_ino;
917 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
918 return (&adirent);
919 }
920
921 /*
922 * Return st_mode information in response to stat or lstat calls
923 */
924 static int
925 glob_stat(const char *name, struct stat *stp)
926 {
927 struct direct *dp;
928 dp = pathsearch(name);
929 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpinomap) == 0) ||
930 (!vflag && dp->d_ino == WINO))
931 return (-1);
932 if (inodetype(dp->d_ino) == NODE)
933 stp->st_mode = IFDIR;
934 else
935 stp->st_mode = IFREG;
936 return (0);
937 }
938
939 /*
940 * Comparison routine for qsort.
941 */
942 static int
943 fcmp(const void *f1, const void *f2)
944 {
945 return (strcmp(((const struct afile *)f1)->fname,
946 ((const struct afile *)f2)->fname));
947 }
948
949 /*
950 * respond to interrupts
951 */
952 void
953 onintr(UNUSED(int signo))
954 {
955 int save_errno = errno;
956
957 if (command == 'i' && runshell)
958 longjmp(reset, 1);
959 if (qflag)
960 exit(1);
961 if (reply("restore interrupted, continue", 0) == FAIL)
962 exit(1);
963 errno = save_errno;
964 }
965
966
967 #if HAVE_READLINE
968
969 #if !HAVE_READLINE_RLCM
970 #define rl_completion_matches completion_matches
971 #endif
972
973 /* A static variable for holding the line. */
974 static char *line_read = NULL;
975
976 static char completion_curdir[MAXPATHLEN];
977
978 static const char *commands[] = {
979 "add", "cd", "delete", "extract", "help",
980 "?", "ls", "pwd", "prompt", "quit", "xit",
981 "verbose", "setmodes", "what", "Debug",
982 NULL };
983
984 static const char *
985 rl_gets (char *dir)
986 {
987 char *prompt;
988 int sz;
989
990 snprintf(completion_curdir, MAXPATHLEN, "%s", dir);
991 completion_curdir[MAXPATHLEN - 1] = '\0';
992
993 if (pflag) {
994 sz = 6 + strlen(__progname) + strlen(spcl.c_filesys) + strlen((completion_curdir[1] ? completion_curdir + 1 : "/"));
995 prompt = (char *)malloc(sz);
996 if (!prompt)
997 return NULL;
998 snprintf(prompt, sz, "%s:%s:%s > ",
999 __progname,
1000 spcl.c_filesys,
1001 (completion_curdir[1] ? completion_curdir + 1 : "/"));
1002 }
1003 else {
1004 sz = 4 + strlen(__progname);
1005 prompt = (char *)malloc(sz);
1006 if (!prompt)
1007 return NULL;
1008 snprintf(prompt, sz, "%s > ", __progname);
1009 }
1010 prompt[sz - 1] = '\0';
1011
1012 if (line_read) {
1013 free (line_read);
1014 line_read = (char *)NULL;
1015 }
1016
1017 do {
1018 line_read = readline (prompt);
1019 } while (line_read && !*line_read);
1020
1021 free(prompt);
1022
1023 if (!line_read) {
1024 printf("\n");
1025 return "quit";
1026 }
1027
1028 add_history (line_read);
1029
1030 return (line_read);
1031 }
1032
1033 static char *
1034 command_generator(const char *text, int state)
1035 {
1036 static int list_index, len;
1037 const char *name;
1038
1039 if (!state) {
1040 list_index = 0;
1041 len = strlen(text);
1042 }
1043
1044 while ( (name = commands[list_index]) != NULL) {
1045
1046 list_index ++;
1047
1048 if (strncmp(name, text, len) == 0)
1049 return strdup(name);
1050 }
1051
1052 return NULL;
1053 }
1054
1055 static char *
1056 filename_generator(const char *text, int state)
1057 {
1058 rl_filename_quoting_desired = 1;
1059 static int list_index;
1060 static char *files = NULL;
1061
1062 char *name;
1063 RST_DIR *dirp;
1064 struct direct *dp;
1065 static int entries;
1066 char pname[MAXPATHLEN*2+2];
1067 char fname[MAXPATHLEN];
1068 char *slash;
1069 char ppname[MAXPATHLEN];
1070
1071 if (!state) {
1072 list_index = 0;
1073
1074 if (files != NULL) {
1075 free(files);
1076 entries = 0;
1077 files = NULL;
1078 }
1079 if ((slash = strrchr(text, '/')) != NULL) {
1080 int idx = slash - text;
1081 if (idx > MAXPATHLEN - 2)
1082 idx = MAXPATHLEN - 2;
1083 strncpy(ppname, text, MAXPATHLEN);
1084 ppname[MAXPATHLEN - 1] = '\0';
1085 ppname[idx] = '\0';
1086 if (text[0] == '/')
1087 snprintf(pname, sizeof(pname), ".%s", ppname);
1088 else
1089 snprintf(pname, sizeof(pname), "%s/%s", completion_curdir, ppname);
1090 strncpy(fname, ppname + idx + 1, MAXPATHLEN);
1091 ppname[idx] = '/';
1092 ppname[idx + 1] = '\0';
1093 }
1094 else {
1095 strncpy(pname, completion_curdir, MAXPATHLEN);
1096 strncpy(fname, text, MAXPATHLEN - 1);
1097 ppname[0] = '\0';
1098 }
1099 pname[MAXPATHLEN - 1] = '\0';
1100 fname[MAXPATHLEN - 1] = '\0';
1101 if ((dirp = rst_opendir(pname)) == NULL)
1102 return NULL;
1103 entries = 0;
1104 while ((dp = rst_readdir(dirp)))
1105 entries++;
1106 rst_closedir(dirp);
1107 files = (char *)malloc(entries * MAXPATHLEN);
1108 if (files == NULL) {
1109 fprintf(stderr, "Out of memory\n");
1110 entries = 0;
1111 return NULL;
1112 }
1113 if ((dirp = rst_opendir(pname)) == NULL)
1114 panic("directory reopen failed\n");
1115 entries = 0;
1116 while ((dp = rst_readdir(dirp))) {
1117 if (TSTINO(dp->d_ino, dumpinomap) == 0)
1118 continue;
1119 if (strcmp(dp->d_name, ".") == 0 ||
1120 strcmp(dp->d_name, "..") == 0)
1121 continue;
1122 if (strncmp(dp->d_name, fname, strlen(fname)) == 0) {
1123 if (inodetype(dp->d_ino) == NODE)
1124 snprintf(files + entries * MAXPATHLEN, MAXPATHLEN, "%s%s/", ppname, dp->d_name);
1125 else
1126 snprintf(files + entries * MAXPATHLEN, MAXPATHLEN, "%s%s", ppname, dp->d_name);
1127 *(files + (entries + 1) * MAXPATHLEN - 1) = '\0';
1128 ++entries;
1129 }
1130 }
1131 rst_closedir(dirp);
1132 }
1133
1134 if (list_index >= entries)
1135 return NULL;
1136
1137 name = encode_escape(files + list_index * MAXPATHLEN);
1138 list_index ++;
1139
1140 #if HAVE_READLINE_CAC /* compile with readline 2.0 */
1141 if(name[strlen(name)-1] == '/')
1142 rl_completion_append_character = '\0';
1143 #endif
1144 return name;
1145 }
1146
1147 static char **
1148 restore_completion (const char *text, int start, UNUSED(int end))
1149 {
1150 if (start == 0)
1151 return rl_completion_matches (text, command_generator);
1152
1153 int in_dstring = 0;
1154 int in_qstring = 0;
1155 char* line = strdup(rl_line_buffer);
1156
1157 int linelen = 0;
1158
1159 char* inbuf = rl_line_buffer;
1160
1161 while(*inbuf) {
1162 switch (*inbuf) {
1163 case '\\': {
1164 inbuf += decode_escape(inbuf, line+linelen++);
1165 break;
1166 }
1167 case '"':
1168 in_dstring = 1-in_dstring;
1169 inbuf++;
1170 break;
1171 case '\'':
1172 in_qstring = 1-in_qstring;
1173 inbuf++;
1174 break;
1175 case ' ':
1176 if (!in_dstring && !in_qstring) {
1177 linelen = 0;
1178 } else
1179 line[linelen++] = *inbuf;
1180 inbuf++;
1181 break;
1182 default:
1183 line[linelen++] = *inbuf++;
1184 break;
1185 }
1186 }
1187 line[linelen++] = 0;
1188
1189 char* tmp = encode_escape(line);
1190 char* lastsp = strrchr(tmp, ' ');
1191 int lastword = lastsp ? lastsp-tmp + 1: 0;
1192 free(tmp);
1193
1194 char** matches = rl_completion_matches (line, filename_generator);
1195 free(line);
1196 if (!matches)
1197 return NULL;
1198
1199 char* newmatch = (char*)malloc(strlen(matches[0])+3);
1200
1201 int j = 0;
1202
1203
1204 if (!lastword && in_dstring) newmatch[j++] = '"';
1205 else if (!lastword && in_qstring) newmatch[j++] = '\'';
1206
1207 strcpy(newmatch+j, matches[0]+lastword);
1208 j = strlen(newmatch);
1209
1210 if(!matches[1] && j && newmatch[j-1] != '/' ) {
1211 if (in_dstring) newmatch[j++] = '"';
1212 else if (in_qstring) newmatch[j++] = '\'';
1213 }
1214
1215 newmatch[j++] = 0;
1216 free(matches[0]);
1217 matches[0] = newmatch;
1218
1219 return (matches);
1220 }
1221
1222 static void
1223 initialize_readline(void)
1224 {
1225 rl_readline_name = "dump";
1226 rl_completer_word_break_characters = " ";
1227 rl_attempted_completion_over = 1;
1228
1229 rl_attempted_completion_function = restore_completion;
1230 rl_completion_entry_function = NULL;
1231 rl_instream = terminal;
1232 }
1233
1234 #endif /* HAVE_READLINE */