Home | History | Annotate | Line # | Download | only in restore
interactive.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1985 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)interactive.c	5.9 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include "restore.h"
     39  1.1  cgd #include <protocols/dumprestore.h>
     40  1.1  cgd #include <setjmp.h>
     41  1.1  cgd #include <ufs/dir.h>
     42  1.1  cgd 
     43  1.1  cgd #define round(a, b) (((a) + (b) - 1) / (b) * (b))
     44  1.1  cgd 
     45  1.1  cgd /*
     46  1.1  cgd  * Things to handle interruptions.
     47  1.1  cgd  */
     48  1.1  cgd static jmp_buf reset;
     49  1.1  cgd static char *nextarg = NULL;
     50  1.1  cgd 
     51  1.1  cgd /*
     52  1.1  cgd  * Structure and routines associated with listing directories.
     53  1.1  cgd  */
     54  1.1  cgd struct afile {
     55  1.1  cgd 	ino_t	fnum;		/* inode number of file */
     56  1.1  cgd 	char	*fname;		/* file name */
     57  1.1  cgd 	short	fflags;		/* extraction flags, if any */
     58  1.1  cgd 	char	ftype;		/* file type, e.g. LEAF or NODE */
     59  1.1  cgd };
     60  1.1  cgd struct arglist {
     61  1.1  cgd 	struct afile	*head;	/* start of argument list */
     62  1.1  cgd 	struct afile	*last;	/* end of argument list */
     63  1.1  cgd 	struct afile	*base;	/* current list arena */
     64  1.1  cgd 	int		nent;	/* maximum size of list */
     65  1.1  cgd 	char		*cmd;	/* the current command */
     66  1.1  cgd };
     67  1.1  cgd extern int fcmp();
     68  1.1  cgd extern char *fmtentry();
     69  1.1  cgd char *copynext();
     70  1.1  cgd 
     71  1.1  cgd /*
     72  1.1  cgd  * Read and execute commands from the terminal.
     73  1.1  cgd  */
     74  1.1  cgd runcmdshell()
     75  1.1  cgd {
     76  1.1  cgd 	register struct entry *np;
     77  1.1  cgd 	ino_t ino;
     78  1.1  cgd 	static struct arglist alist = { 0, 0, 0, 0, 0 };
     79  1.1  cgd 	char curdir[MAXPATHLEN];
     80  1.1  cgd 	char name[MAXPATHLEN];
     81  1.1  cgd 	char cmd[BUFSIZ];
     82  1.1  cgd 
     83  1.1  cgd 	canon("/", curdir);
     84  1.1  cgd loop:
     85  1.1  cgd 	if (setjmp(reset) != 0) {
     86  1.1  cgd 		for (; alist.head < alist.last; alist.head++)
     87  1.1  cgd 			freename(alist.head->fname);
     88  1.1  cgd 		nextarg = NULL;
     89  1.1  cgd 		volno = 0;
     90  1.1  cgd 	}
     91  1.1  cgd 	getcmd(curdir, cmd, name, &alist);
     92  1.1  cgd 	switch (cmd[0]) {
     93  1.1  cgd 	/*
     94  1.1  cgd 	 * Add elements to the extraction list.
     95  1.1  cgd 	 */
     96  1.1  cgd 	case 'a':
     97  1.1  cgd 		if (strncmp(cmd, "add", strlen(cmd)) != 0)
     98  1.1  cgd 			goto bad;
     99  1.1  cgd 		ino = dirlookup(name);
    100  1.1  cgd 		if (ino == 0)
    101  1.1  cgd 			break;
    102  1.1  cgd 		if (mflag)
    103  1.1  cgd 			pathcheck(name);
    104  1.1  cgd 		treescan(name, ino, addfile);
    105  1.1  cgd 		break;
    106  1.1  cgd 	/*
    107  1.1  cgd 	 * Change working directory.
    108  1.1  cgd 	 */
    109  1.1  cgd 	case 'c':
    110  1.1  cgd 		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
    111  1.1  cgd 			goto bad;
    112  1.1  cgd 		ino = dirlookup(name);
    113  1.1  cgd 		if (ino == 0)
    114  1.1  cgd 			break;
    115  1.1  cgd 		if (inodetype(ino) == LEAF) {
    116  1.1  cgd 			fprintf(stderr, "%s: not a directory\n", name);
    117  1.1  cgd 			break;
    118  1.1  cgd 		}
    119  1.1  cgd 		(void) strcpy(curdir, name);
    120  1.1  cgd 		break;
    121  1.1  cgd 	/*
    122  1.1  cgd 	 * Delete elements from the extraction list.
    123  1.1  cgd 	 */
    124  1.1  cgd 	case 'd':
    125  1.1  cgd 		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
    126  1.1  cgd 			goto bad;
    127  1.1  cgd 		np = lookupname(name);
    128  1.1  cgd 		if (np == NIL || (np->e_flags & NEW) == 0) {
    129  1.1  cgd 			fprintf(stderr, "%s: not on extraction list\n", name);
    130  1.1  cgd 			break;
    131  1.1  cgd 		}
    132  1.1  cgd 		treescan(name, np->e_ino, deletefile);
    133  1.1  cgd 		break;
    134  1.1  cgd 	/*
    135  1.1  cgd 	 * Extract the requested list.
    136  1.1  cgd 	 */
    137  1.1  cgd 	case 'e':
    138  1.1  cgd 		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
    139  1.1  cgd 			goto bad;
    140  1.1  cgd 		createfiles();
    141  1.1  cgd 		createlinks();
    142  1.1  cgd 		setdirmodes();
    143  1.1  cgd 		if (dflag)
    144  1.1  cgd 			checkrestore();
    145  1.1  cgd 		volno = 0;
    146  1.1  cgd 		break;
    147  1.1  cgd 	/*
    148  1.1  cgd 	 * List available commands.
    149  1.1  cgd 	 */
    150  1.1  cgd 	case 'h':
    151  1.1  cgd 		if (strncmp(cmd, "help", strlen(cmd)) != 0)
    152  1.1  cgd 			goto bad;
    153  1.1  cgd 	case '?':
    154  1.1  cgd 		fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
    155  1.1  cgd 			"Available commands are:\n",
    156  1.1  cgd 			"\tls [arg] - list directory\n",
    157  1.1  cgd 			"\tcd arg - change directory\n",
    158  1.1  cgd 			"\tpwd - print current directory\n",
    159  1.1  cgd 			"\tadd [arg] - add `arg' to list of",
    160  1.1  cgd 			" files to be extracted\n",
    161  1.1  cgd 			"\tdelete [arg] - delete `arg' from",
    162  1.1  cgd 			" list of files to be extracted\n",
    163  1.1  cgd 			"\textract - extract requested files\n",
    164  1.1  cgd 			"\tsetmodes - set modes of requested directories\n",
    165  1.1  cgd 			"\tquit - immediately exit program\n",
    166  1.1  cgd 			"\twhat - list dump header information\n",
    167  1.1  cgd 			"\tverbose - toggle verbose flag",
    168  1.1  cgd 			" (useful with ``ls'')\n",
    169  1.1  cgd 			"\thelp or `?' - print this list\n",
    170  1.1  cgd 			"If no `arg' is supplied, the current",
    171  1.1  cgd 			" directory is used\n");
    172  1.1  cgd 		break;
    173  1.1  cgd 	/*
    174  1.1  cgd 	 * List a directory.
    175  1.1  cgd 	 */
    176  1.1  cgd 	case 'l':
    177  1.1  cgd 		if (strncmp(cmd, "ls", strlen(cmd)) != 0)
    178  1.1  cgd 			goto bad;
    179  1.1  cgd 		ino = dirlookup(name);
    180  1.1  cgd 		if (ino == 0)
    181  1.1  cgd 			break;
    182  1.1  cgd 		printlist(name, ino, curdir);
    183  1.1  cgd 		break;
    184  1.1  cgd 	/*
    185  1.1  cgd 	 * Print current directory.
    186  1.1  cgd 	 */
    187  1.1  cgd 	case 'p':
    188  1.1  cgd 		if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
    189  1.1  cgd 			goto bad;
    190  1.1  cgd 		if (curdir[1] == '\0')
    191  1.1  cgd 			fprintf(stderr, "/\n");
    192  1.1  cgd 		else
    193  1.1  cgd 			fprintf(stderr, "%s\n", &curdir[1]);
    194  1.1  cgd 		break;
    195  1.1  cgd 	/*
    196  1.1  cgd 	 * Quit.
    197  1.1  cgd 	 */
    198  1.1  cgd 	case 'q':
    199  1.1  cgd 		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
    200  1.1  cgd 			goto bad;
    201  1.1  cgd 		return;
    202  1.1  cgd 	case 'x':
    203  1.1  cgd 		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
    204  1.1  cgd 			goto bad;
    205  1.1  cgd 		return;
    206  1.1  cgd 	/*
    207  1.1  cgd 	 * Toggle verbose mode.
    208  1.1  cgd 	 */
    209  1.1  cgd 	case 'v':
    210  1.1  cgd 		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
    211  1.1  cgd 			goto bad;
    212  1.1  cgd 		if (vflag) {
    213  1.1  cgd 			fprintf(stderr, "verbose mode off\n");
    214  1.1  cgd 			vflag = 0;
    215  1.1  cgd 			break;
    216  1.1  cgd 		}
    217  1.1  cgd 		fprintf(stderr, "verbose mode on\n");
    218  1.1  cgd 		vflag++;
    219  1.1  cgd 		break;
    220  1.1  cgd 	/*
    221  1.1  cgd 	 * Just restore requested directory modes.
    222  1.1  cgd 	 */
    223  1.1  cgd 	case 's':
    224  1.1  cgd 		if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
    225  1.1  cgd 			goto bad;
    226  1.1  cgd 		setdirmodes();
    227  1.1  cgd 		break;
    228  1.1  cgd 	/*
    229  1.1  cgd 	 * Print out dump header information.
    230  1.1  cgd 	 */
    231  1.1  cgd 	case 'w':
    232  1.1  cgd 		if (strncmp(cmd, "what", strlen(cmd)) != 0)
    233  1.1  cgd 			goto bad;
    234  1.1  cgd 		printdumpinfo();
    235  1.1  cgd 		break;
    236  1.1  cgd 	/*
    237  1.1  cgd 	 * Turn on debugging.
    238  1.1  cgd 	 */
    239  1.1  cgd 	case 'D':
    240  1.1  cgd 		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
    241  1.1  cgd 			goto bad;
    242  1.1  cgd 		if (dflag) {
    243  1.1  cgd 			fprintf(stderr, "debugging mode off\n");
    244  1.1  cgd 			dflag = 0;
    245  1.1  cgd 			break;
    246  1.1  cgd 		}
    247  1.1  cgd 		fprintf(stderr, "debugging mode on\n");
    248  1.1  cgd 		dflag++;
    249  1.1  cgd 		break;
    250  1.1  cgd 	/*
    251  1.1  cgd 	 * Unknown command.
    252  1.1  cgd 	 */
    253  1.1  cgd 	default:
    254  1.1  cgd 	bad:
    255  1.1  cgd 		fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
    256  1.1  cgd 		break;
    257  1.1  cgd 	}
    258  1.1  cgd 	goto loop;
    259  1.1  cgd }
    260  1.1  cgd 
    261  1.1  cgd /*
    262  1.1  cgd  * Read and parse an interactive command.
    263  1.1  cgd  * The first word on the line is assigned to "cmd". If
    264  1.1  cgd  * there are no arguments on the command line, then "curdir"
    265  1.1  cgd  * is returned as the argument. If there are arguments
    266  1.1  cgd  * on the line they are returned one at a time on each
    267  1.1  cgd  * successive call to getcmd. Each argument is first assigned
    268  1.1  cgd  * to "name". If it does not start with "/" the pathname in
    269  1.1  cgd  * "curdir" is prepended to it. Finally "canon" is called to
    270  1.1  cgd  * eliminate any embedded ".." components.
    271  1.1  cgd  */
    272  1.1  cgd getcmd(curdir, cmd, name, ap)
    273  1.1  cgd 	char *curdir, *cmd, *name;
    274  1.1  cgd 	struct arglist *ap;
    275  1.1  cgd {
    276  1.1  cgd 	register char *cp;
    277  1.1  cgd 	static char input[BUFSIZ];
    278  1.1  cgd 	char output[BUFSIZ];
    279  1.1  cgd #	define rawname input	/* save space by reusing input buffer */
    280  1.1  cgd 
    281  1.1  cgd 	/*
    282  1.1  cgd 	 * Check to see if still processing arguments.
    283  1.1  cgd 	 */
    284  1.1  cgd 	if (ap->head != ap->last) {
    285  1.1  cgd 		strcpy(name, ap->head->fname);
    286  1.1  cgd 		freename(ap->head->fname);
    287  1.1  cgd 		ap->head++;
    288  1.1  cgd 		return;
    289  1.1  cgd 	}
    290  1.1  cgd 	if (nextarg != NULL)
    291  1.1  cgd 		goto getnext;
    292  1.1  cgd 	/*
    293  1.1  cgd 	 * Read a command line and trim off trailing white space.
    294  1.1  cgd 	 */
    295  1.1  cgd 	do	{
    296  1.1  cgd 		fprintf(stderr, "restore > ");
    297  1.1  cgd 		(void) fflush(stderr);
    298  1.1  cgd 		(void) fgets(input, BUFSIZ, terminal);
    299  1.1  cgd 	} while (!feof(terminal) && input[0] == '\n');
    300  1.1  cgd 	if (feof(terminal)) {
    301  1.1  cgd 		(void) strcpy(cmd, "quit");
    302  1.1  cgd 		return;
    303  1.1  cgd 	}
    304  1.1  cgd 	for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
    305  1.1  cgd 		/* trim off trailing white space and newline */;
    306  1.1  cgd 	*++cp = '\0';
    307  1.1  cgd 	/*
    308  1.1  cgd 	 * Copy the command into "cmd".
    309  1.1  cgd 	 */
    310  1.1  cgd 	cp = copynext(input, cmd);
    311  1.1  cgd 	ap->cmd = cmd;
    312  1.1  cgd 	/*
    313  1.1  cgd 	 * If no argument, use curdir as the default.
    314  1.1  cgd 	 */
    315  1.1  cgd 	if (*cp == '\0') {
    316  1.1  cgd 		(void) strcpy(name, curdir);
    317  1.1  cgd 		return;
    318  1.1  cgd 	}
    319  1.1  cgd 	nextarg = cp;
    320  1.1  cgd 	/*
    321  1.1  cgd 	 * Find the next argument.
    322  1.1  cgd 	 */
    323  1.1  cgd getnext:
    324  1.1  cgd 	cp = copynext(nextarg, rawname);
    325  1.1  cgd 	if (*cp == '\0')
    326  1.1  cgd 		nextarg = NULL;
    327  1.1  cgd 	else
    328  1.1  cgd 		nextarg = cp;
    329  1.1  cgd 	/*
    330  1.1  cgd 	 * If it an absolute pathname, canonicalize it and return it.
    331  1.1  cgd 	 */
    332  1.1  cgd 	if (rawname[0] == '/') {
    333  1.1  cgd 		canon(rawname, name);
    334  1.1  cgd 	} else {
    335  1.1  cgd 		/*
    336  1.1  cgd 		 * For relative pathnames, prepend the current directory to
    337  1.1  cgd 		 * it then canonicalize and return it.
    338  1.1  cgd 		 */
    339  1.1  cgd 		(void) strcpy(output, curdir);
    340  1.1  cgd 		(void) strcat(output, "/");
    341  1.1  cgd 		(void) strcat(output, rawname);
    342  1.1  cgd 		canon(output, name);
    343  1.1  cgd 	}
    344  1.1  cgd 	expandarg(name, ap);
    345  1.1  cgd 	strcpy(name, ap->head->fname);
    346  1.1  cgd 	freename(ap->head->fname);
    347  1.1  cgd 	ap->head++;
    348  1.1  cgd #	undef rawname
    349  1.1  cgd }
    350  1.1  cgd 
    351  1.1  cgd /*
    352  1.1  cgd  * Strip off the next token of the input.
    353  1.1  cgd  */
    354  1.1  cgd char *
    355  1.1  cgd copynext(input, output)
    356  1.1  cgd 	char *input, *output;
    357  1.1  cgd {
    358  1.1  cgd 	register char *cp, *bp;
    359  1.1  cgd 	char quote;
    360  1.1  cgd 
    361  1.1  cgd 	for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
    362  1.1  cgd 		/* skip to argument */;
    363  1.1  cgd 	bp = output;
    364  1.1  cgd 	while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
    365  1.1  cgd 		/*
    366  1.1  cgd 		 * Handle back slashes.
    367  1.1  cgd 		 */
    368  1.1  cgd 		if (*cp == '\\') {
    369  1.1  cgd 			if (*++cp == '\0') {
    370  1.1  cgd 				fprintf(stderr,
    371  1.1  cgd 					"command lines cannot be continued\n");
    372  1.1  cgd 				continue;
    373  1.1  cgd 			}
    374  1.1  cgd 			*bp++ = *cp++;
    375  1.1  cgd 			continue;
    376  1.1  cgd 		}
    377  1.1  cgd 		/*
    378  1.1  cgd 		 * The usual unquoted case.
    379  1.1  cgd 		 */
    380  1.1  cgd 		if (*cp != '\'' && *cp != '"') {
    381  1.1  cgd 			*bp++ = *cp++;
    382  1.1  cgd 			continue;
    383  1.1  cgd 		}
    384  1.1  cgd 		/*
    385  1.1  cgd 		 * Handle single and double quotes.
    386  1.1  cgd 		 */
    387  1.1  cgd 		quote = *cp++;
    388  1.1  cgd 		while (*cp != quote && *cp != '\0')
    389  1.1  cgd 			*bp++ = *cp++ | 0200;
    390  1.1  cgd 		if (*cp++ == '\0') {
    391  1.1  cgd 			fprintf(stderr, "missing %c\n", quote);
    392  1.1  cgd 			cp--;
    393  1.1  cgd 			continue;
    394  1.1  cgd 		}
    395  1.1  cgd 	}
    396  1.1  cgd 	*bp = '\0';
    397  1.1  cgd 	return (cp);
    398  1.1  cgd }
    399  1.1  cgd 
    400  1.1  cgd /*
    401  1.1  cgd  * Canonicalize file names to always start with ``./'' and
    402  1.1  cgd  * remove any imbedded "." and ".." components.
    403  1.1  cgd  */
    404  1.1  cgd canon(rawname, canonname)
    405  1.1  cgd 	char *rawname, *canonname;
    406  1.1  cgd {
    407  1.1  cgd 	register char *cp, *np;
    408  1.1  cgd 	int len;
    409  1.1  cgd 
    410  1.1  cgd 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
    411  1.1  cgd 		(void) strcpy(canonname, "");
    412  1.1  cgd 	else if (rawname[0] == '/')
    413  1.1  cgd 		(void) strcpy(canonname, ".");
    414  1.1  cgd 	else
    415  1.1  cgd 		(void) strcpy(canonname, "./");
    416  1.1  cgd 	(void) strcat(canonname, rawname);
    417  1.1  cgd 	/*
    418  1.1  cgd 	 * Eliminate multiple and trailing '/'s
    419  1.1  cgd 	 */
    420  1.1  cgd 	for (cp = np = canonname; *np != '\0'; cp++) {
    421  1.1  cgd 		*cp = *np++;
    422  1.1  cgd 		while (*cp == '/' && *np == '/')
    423  1.1  cgd 			np++;
    424  1.1  cgd 	}
    425  1.1  cgd 	*cp = '\0';
    426  1.1  cgd 	if (*--cp == '/')
    427  1.1  cgd 		*cp = '\0';
    428  1.1  cgd 	/*
    429  1.1  cgd 	 * Eliminate extraneous "." and ".." from pathnames.
    430  1.1  cgd 	 */
    431  1.1  cgd 	for (np = canonname; *np != '\0'; ) {
    432  1.1  cgd 		np++;
    433  1.1  cgd 		cp = np;
    434  1.1  cgd 		while (*np != '/' && *np != '\0')
    435  1.1  cgd 			np++;
    436  1.1  cgd 		if (np - cp == 1 && *cp == '.') {
    437  1.1  cgd 			cp--;
    438  1.1  cgd 			(void) strcpy(cp, np);
    439  1.1  cgd 			np = cp;
    440  1.1  cgd 		}
    441  1.1  cgd 		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
    442  1.1  cgd 			cp--;
    443  1.1  cgd 			while (cp > &canonname[1] && *--cp != '/')
    444  1.1  cgd 				/* find beginning of name */;
    445  1.1  cgd 			(void) strcpy(cp, np);
    446  1.1  cgd 			np = cp;
    447  1.1  cgd 		}
    448  1.1  cgd 	}
    449  1.1  cgd }
    450  1.1  cgd 
    451  1.1  cgd /*
    452  1.1  cgd  * globals (file name generation)
    453  1.1  cgd  *
    454  1.1  cgd  * "*" in params matches r.e ".*"
    455  1.1  cgd  * "?" in params matches r.e. "."
    456  1.1  cgd  * "[...]" in params matches character class
    457  1.1  cgd  * "[...a-z...]" in params matches a through z.
    458  1.1  cgd  */
    459  1.1  cgd expandarg(arg, ap)
    460  1.1  cgd 	char *arg;
    461  1.1  cgd 	register struct arglist *ap;
    462  1.1  cgd {
    463  1.1  cgd 	static struct afile single;
    464  1.1  cgd 	struct entry *ep;
    465  1.1  cgd 	int size;
    466  1.1  cgd 
    467  1.1  cgd 	ap->head = ap->last = (struct afile *)0;
    468  1.1  cgd 	size = expand(arg, 0, ap);
    469  1.1  cgd 	if (size == 0) {
    470  1.1  cgd 		ep = lookupname(arg);
    471  1.1  cgd 		single.fnum = ep ? ep->e_ino : 0;
    472  1.1  cgd 		single.fname = savename(arg);
    473  1.1  cgd 		ap->head = &single;
    474  1.1  cgd 		ap->last = ap->head + 1;
    475  1.1  cgd 		return;
    476  1.1  cgd 	}
    477  1.1  cgd 	qsort((char *)ap->head, ap->last - ap->head, sizeof *ap->head, fcmp);
    478  1.1  cgd }
    479  1.1  cgd 
    480  1.1  cgd /*
    481  1.1  cgd  * Expand a file name
    482  1.1  cgd  */
    483  1.1  cgd expand(as, rflg, ap)
    484  1.1  cgd 	char *as;
    485  1.1  cgd 	int rflg;
    486  1.1  cgd 	register struct arglist *ap;
    487  1.1  cgd {
    488  1.1  cgd 	int		count, size;
    489  1.1  cgd 	char		dir = 0;
    490  1.1  cgd 	char		*rescan = 0;
    491  1.1  cgd 	DIR		*dirp;
    492  1.1  cgd 	register char	*s, *cs;
    493  1.1  cgd 	int		sindex, rindex, lindex;
    494  1.1  cgd 	struct direct	*dp;
    495  1.1  cgd 	register char	slash;
    496  1.1  cgd 	register char	*rs;
    497  1.1  cgd 	register char	c;
    498  1.1  cgd 
    499  1.1  cgd 	/*
    500  1.1  cgd 	 * check for meta chars
    501  1.1  cgd 	 */
    502  1.1  cgd 	s = cs = as;
    503  1.1  cgd 	slash = 0;
    504  1.1  cgd 	while (*cs != '*' && *cs != '?' && *cs != '[') {
    505  1.1  cgd 		if (*cs++ == 0) {
    506  1.1  cgd 			if (rflg && slash)
    507  1.1  cgd 				break;
    508  1.1  cgd 			else
    509  1.1  cgd 				return (0) ;
    510  1.1  cgd 		} else if (*cs == '/') {
    511  1.1  cgd 			slash++;
    512  1.1  cgd 		}
    513  1.1  cgd 	}
    514  1.1  cgd 	for (;;) {
    515  1.1  cgd 		if (cs == s) {
    516  1.1  cgd 			s = "";
    517  1.1  cgd 			break;
    518  1.1  cgd 		} else if (*--cs == '/') {
    519  1.1  cgd 			*cs = 0;
    520  1.1  cgd 			if (s == cs)
    521  1.1  cgd 				s = "/";
    522  1.1  cgd 			break;
    523  1.1  cgd 		}
    524  1.1  cgd 	}
    525  1.1  cgd 	if ((dirp = rst_opendir(s)) != NULL)
    526  1.1  cgd 		dir++;
    527  1.1  cgd 	count = 0;
    528  1.1  cgd 	if (*cs == 0)
    529  1.1  cgd 		*cs++ = 0200;
    530  1.1  cgd 	if (dir) {
    531  1.1  cgd 		/*
    532  1.1  cgd 		 * check for rescan
    533  1.1  cgd 		 */
    534  1.1  cgd 		rs = cs;
    535  1.1  cgd 		do {
    536  1.1  cgd 			if (*rs == '/') {
    537  1.1  cgd 				rescan = rs;
    538  1.1  cgd 				*rs = 0;
    539  1.1  cgd 			}
    540  1.1  cgd 		} while (*rs++);
    541  1.1  cgd 		sindex = ap->last - ap->head;
    542  1.1  cgd 		while ((dp = rst_readdir(dirp)) != NULL && dp->d_ino != 0) {
    543  1.1  cgd 			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
    544  1.1  cgd 				continue;
    545  1.1  cgd 			if ((*dp->d_name == '.' && *cs != '.'))
    546  1.1  cgd 				continue;
    547  1.1  cgd 			if (gmatch(dp->d_name, cs)) {
    548  1.1  cgd 				if (addg(dp, s, rescan, ap) < 0)
    549  1.1  cgd 					return (-1);
    550  1.1  cgd 				count++;
    551  1.1  cgd 			}
    552  1.1  cgd 		}
    553  1.1  cgd 		if (rescan) {
    554  1.1  cgd 			rindex = sindex;
    555  1.1  cgd 			lindex = ap->last - ap->head;
    556  1.1  cgd 			if (count) {
    557  1.1  cgd 				count = 0;
    558  1.1  cgd 				while (rindex < lindex) {
    559  1.1  cgd 					size = expand(ap->head[rindex].fname,
    560  1.1  cgd 					    1, ap);
    561  1.1  cgd 					if (size < 0)
    562  1.1  cgd 						return (size);
    563  1.1  cgd 					count += size;
    564  1.1  cgd 					rindex++;
    565  1.1  cgd 				}
    566  1.1  cgd 			}
    567  1.1  cgd 			bcopy((char *)&ap->head[lindex],
    568  1.1  cgd 			     (char *)&ap->head[sindex],
    569  1.1  cgd 			     (ap->last - &ap->head[rindex]) * sizeof *ap->head);
    570  1.1  cgd 			ap->last -= lindex - sindex;
    571  1.1  cgd 			*rescan = '/';
    572  1.1  cgd 		}
    573  1.1  cgd 	}
    574  1.1  cgd 	s = as;
    575  1.1  cgd 	while (c = *s)
    576  1.1  cgd 		*s++ = (c&0177 ? c : '/');
    577  1.1  cgd 	return (count);
    578  1.1  cgd }
    579  1.1  cgd 
    580  1.1  cgd /*
    581  1.1  cgd  * Check for a name match
    582  1.1  cgd  */
    583  1.1  cgd gmatch(s, p)
    584  1.1  cgd 	register char	*s, *p;
    585  1.1  cgd {
    586  1.1  cgd 	register int	scc;
    587  1.1  cgd 	char		c;
    588  1.1  cgd 	char		ok;
    589  1.1  cgd 	int		lc;
    590  1.1  cgd 
    591  1.1  cgd 	if (scc = *s++)
    592  1.1  cgd 		if ((scc &= 0177) == 0)
    593  1.1  cgd 			scc = 0200;
    594  1.1  cgd 	switch (c = *p++) {
    595  1.1  cgd 
    596  1.1  cgd 	case '[':
    597  1.1  cgd 		ok = 0;
    598  1.1  cgd 		lc = 077777;
    599  1.1  cgd 		while (c = *p++) {
    600  1.1  cgd 			if (c == ']') {
    601  1.1  cgd 				return (ok ? gmatch(s, p) : 0);
    602  1.1  cgd 			} else if (c == '-') {
    603  1.1  cgd 				if (lc <= scc && scc <= (*p++))
    604  1.1  cgd 					ok++ ;
    605  1.1  cgd 			} else {
    606  1.1  cgd 				if (scc == (lc = (c&0177)))
    607  1.1  cgd 					ok++ ;
    608  1.1  cgd 			}
    609  1.1  cgd 		}
    610  1.1  cgd 		return (0);
    611  1.1  cgd 
    612  1.1  cgd 	default:
    613  1.1  cgd 		if ((c&0177) != scc)
    614  1.1  cgd 			return (0) ;
    615  1.1  cgd 		/* falls through */
    616  1.1  cgd 
    617  1.1  cgd 	case '?':
    618  1.1  cgd 		return (scc ? gmatch(s, p) : 0);
    619  1.1  cgd 
    620  1.1  cgd 	case '*':
    621  1.1  cgd 		if (*p == 0)
    622  1.1  cgd 			return (1) ;
    623  1.1  cgd 		s--;
    624  1.1  cgd 		while (*s) {
    625  1.1  cgd 			if (gmatch(s++, p))
    626  1.1  cgd 				return (1);
    627  1.1  cgd 		}
    628  1.1  cgd 		return (0);
    629  1.1  cgd 
    630  1.1  cgd 	case 0:
    631  1.1  cgd 		return (scc == 0);
    632  1.1  cgd 	}
    633  1.1  cgd }
    634  1.1  cgd 
    635  1.1  cgd /*
    636  1.1  cgd  * Construct a matched name.
    637  1.1  cgd  */
    638  1.1  cgd addg(dp, as1, as3, ap)
    639  1.1  cgd 	struct direct	*dp;
    640  1.1  cgd 	char		*as1, *as3;
    641  1.1  cgd 	struct arglist	*ap;
    642  1.1  cgd {
    643  1.1  cgd 	register char	*s1, *s2;
    644  1.1  cgd 	register int	c;
    645  1.1  cgd 	char		buf[BUFSIZ];
    646  1.1  cgd 
    647  1.1  cgd 	s2 = buf;
    648  1.1  cgd 	s1 = as1;
    649  1.1  cgd 	while (c = *s1++) {
    650  1.1  cgd 		if ((c &= 0177) == 0) {
    651  1.1  cgd 			*s2++ = '/';
    652  1.1  cgd 			break;
    653  1.1  cgd 		}
    654  1.1  cgd 		*s2++ = c;
    655  1.1  cgd 	}
    656  1.1  cgd 	s1 = dp->d_name;
    657  1.1  cgd 	while (*s2 = *s1++)
    658  1.1  cgd 		s2++;
    659  1.1  cgd 	if (s1 = as3) {
    660  1.1  cgd 		*s2++ = '/';
    661  1.1  cgd 		while (*s2++ = *++s1)
    662  1.1  cgd 			/* void */;
    663  1.1  cgd 	}
    664  1.1  cgd 	if (mkentry(buf, dp->d_ino, ap) == FAIL)
    665  1.1  cgd 		return (-1);
    666  1.1  cgd }
    667  1.1  cgd 
    668  1.1  cgd /*
    669  1.1  cgd  * Do an "ls" style listing of a directory
    670  1.1  cgd  */
    671  1.1  cgd printlist(name, ino, basename)
    672  1.1  cgd 	char *name;
    673  1.1  cgd 	ino_t ino;
    674  1.1  cgd 	char *basename;
    675  1.1  cgd {
    676  1.1  cgd 	register struct afile *fp;
    677  1.1  cgd 	register struct direct *dp;
    678  1.1  cgd 	static struct arglist alist = { 0, 0, 0, 0, "ls" };
    679  1.1  cgd 	struct afile single;
    680  1.1  cgd 	DIR *dirp;
    681  1.1  cgd 
    682  1.1  cgd 	if ((dirp = rst_opendir(name)) == NULL) {
    683  1.1  cgd 		single.fnum = ino;
    684  1.1  cgd 		single.fname = savename(name + strlen(basename) + 1);
    685  1.1  cgd 		alist.head = &single;
    686  1.1  cgd 		alist.last = alist.head + 1;
    687  1.1  cgd 	} else {
    688  1.1  cgd 		alist.head = (struct afile *)0;
    689  1.1  cgd 		fprintf(stderr, "%s:\n", name);
    690  1.1  cgd 		while (dp = rst_readdir(dirp)) {
    691  1.1  cgd 			if (dp == NULL || dp->d_ino == 0)
    692  1.1  cgd 				break;
    693  1.1  cgd 			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
    694  1.1  cgd 				continue;
    695  1.1  cgd 			if (vflag == 0 &&
    696  1.1  cgd 			    (strcmp(dp->d_name, ".") == 0 ||
    697  1.1  cgd 			     strcmp(dp->d_name, "..") == 0))
    698  1.1  cgd 				continue;
    699  1.1  cgd 			if (!mkentry(dp->d_name, dp->d_ino, &alist))
    700  1.1  cgd 				return;
    701  1.1  cgd 		}
    702  1.1  cgd 	}
    703  1.1  cgd 	if (alist.head != 0) {
    704  1.1  cgd 		qsort((char *)alist.head, alist.last - alist.head,
    705  1.1  cgd 			sizeof *alist.head, fcmp);
    706  1.1  cgd 		formatf(&alist);
    707  1.1  cgd 		for (fp = alist.head; fp < alist.last; fp++)
    708  1.1  cgd 			freename(fp->fname);
    709  1.1  cgd 	}
    710  1.1  cgd 	if (dirp != NULL)
    711  1.1  cgd 		fprintf(stderr, "\n");
    712  1.1  cgd }
    713  1.1  cgd 
    714  1.1  cgd /*
    715  1.1  cgd  * Read the contents of a directory.
    716  1.1  cgd  */
    717  1.1  cgd mkentry(name, ino, ap)
    718  1.1  cgd 	char *name;
    719  1.1  cgd 	ino_t ino;
    720  1.1  cgd 	register struct arglist *ap;
    721  1.1  cgd {
    722  1.1  cgd 	register struct afile *fp;
    723  1.1  cgd 
    724  1.1  cgd 	if (ap->base == NULL) {
    725  1.1  cgd 		ap->nent = 20;
    726  1.1  cgd 		ap->base = (struct afile *)calloc((unsigned)ap->nent,
    727  1.1  cgd 			sizeof (struct afile));
    728  1.1  cgd 		if (ap->base == NULL) {
    729  1.1  cgd 			fprintf(stderr, "%s: out of memory\n", ap->cmd);
    730  1.1  cgd 			return (FAIL);
    731  1.1  cgd 		}
    732  1.1  cgd 	}
    733  1.1  cgd 	if (ap->head == 0)
    734  1.1  cgd 		ap->head = ap->last = ap->base;
    735  1.1  cgd 	fp = ap->last;
    736  1.1  cgd 	fp->fnum = ino;
    737  1.1  cgd 	fp->fname = savename(name);
    738  1.1  cgd 	fp++;
    739  1.1  cgd 	if (fp == ap->head + ap->nent) {
    740  1.1  cgd 		ap->base = (struct afile *)realloc((char *)ap->base,
    741  1.1  cgd 		    (unsigned)(2 * ap->nent * sizeof (struct afile)));
    742  1.1  cgd 		if (ap->base == 0) {
    743  1.1  cgd 			fprintf(stderr, "%s: out of memory\n", ap->cmd);
    744  1.1  cgd 			return (FAIL);
    745  1.1  cgd 		}
    746  1.1  cgd 		ap->head = ap->base;
    747  1.1  cgd 		fp = ap->head + ap->nent;
    748  1.1  cgd 		ap->nent *= 2;
    749  1.1  cgd 	}
    750  1.1  cgd 	ap->last = fp;
    751  1.1  cgd 	return (GOOD);
    752  1.1  cgd }
    753  1.1  cgd 
    754  1.1  cgd /*
    755  1.1  cgd  * Print out a pretty listing of a directory
    756  1.1  cgd  */
    757  1.1  cgd formatf(ap)
    758  1.1  cgd 	register struct arglist *ap;
    759  1.1  cgd {
    760  1.1  cgd 	register struct afile *fp;
    761  1.1  cgd 	struct entry *np;
    762  1.1  cgd 	int width = 0, w, nentry = ap->last - ap->head;
    763  1.1  cgd 	int i, j, len, columns, lines;
    764  1.1  cgd 	char *cp;
    765  1.1  cgd 
    766  1.1  cgd 	if (ap->head == ap->last)
    767  1.1  cgd 		return;
    768  1.1  cgd 	for (fp = ap->head; fp < ap->last; fp++) {
    769  1.1  cgd 		fp->ftype = inodetype(fp->fnum);
    770  1.1  cgd 		np = lookupino(fp->fnum);
    771  1.1  cgd 		if (np != NIL)
    772  1.1  cgd 			fp->fflags = np->e_flags;
    773  1.1  cgd 		else
    774  1.1  cgd 			fp->fflags = 0;
    775  1.1  cgd 		len = strlen(fmtentry(fp));
    776  1.1  cgd 		if (len > width)
    777  1.1  cgd 			width = len;
    778  1.1  cgd 	}
    779  1.1  cgd 	width += 2;
    780  1.1  cgd 	columns = 80 / width;
    781  1.1  cgd 	if (columns == 0)
    782  1.1  cgd 		columns = 1;
    783  1.1  cgd 	lines = (nentry + columns - 1) / columns;
    784  1.1  cgd 	for (i = 0; i < lines; i++) {
    785  1.1  cgd 		for (j = 0; j < columns; j++) {
    786  1.1  cgd 			fp = ap->head + j * lines + i;
    787  1.1  cgd 			cp = fmtentry(fp);
    788  1.1  cgd 			fprintf(stderr, "%s", cp);
    789  1.1  cgd 			if (fp + lines >= ap->last) {
    790  1.1  cgd 				fprintf(stderr, "\n");
    791  1.1  cgd 				break;
    792  1.1  cgd 			}
    793  1.1  cgd 			w = strlen(cp);
    794  1.1  cgd 			while (w < width) {
    795  1.1  cgd 				w++;
    796  1.1  cgd 				fprintf(stderr, " ");
    797  1.1  cgd 			}
    798  1.1  cgd 		}
    799  1.1  cgd 	}
    800  1.1  cgd }
    801  1.1  cgd 
    802  1.1  cgd /*
    803  1.1  cgd  * Comparison routine for qsort.
    804  1.1  cgd  */
    805  1.1  cgd fcmp(f1, f2)
    806  1.1  cgd 	register struct afile *f1, *f2;
    807  1.1  cgd {
    808  1.1  cgd 
    809  1.1  cgd 	return (strcmp(f1->fname, f2->fname));
    810  1.1  cgd }
    811  1.1  cgd 
    812  1.1  cgd /*
    813  1.1  cgd  * Format a directory entry.
    814  1.1  cgd  */
    815  1.1  cgd char *
    816  1.1  cgd fmtentry(fp)
    817  1.1  cgd 	register struct afile *fp;
    818  1.1  cgd {
    819  1.1  cgd 	static char fmtres[BUFSIZ];
    820  1.1  cgd 	static int precision = 0;
    821  1.1  cgd 	int i;
    822  1.1  cgd 	register char *cp, *dp;
    823  1.1  cgd 
    824  1.1  cgd 	if (!vflag) {
    825  1.1  cgd 		fmtres[0] = '\0';
    826  1.1  cgd 	} else {
    827  1.1  cgd 		if (precision == 0)
    828  1.1  cgd 			for (i = maxino; i > 0; i /= 10)
    829  1.1  cgd 				precision++;
    830  1.1  cgd 		(void) sprintf(fmtres, "%*d ", precision, fp->fnum);
    831  1.1  cgd 	}
    832  1.1  cgd 	dp = &fmtres[strlen(fmtres)];
    833  1.1  cgd 	if (dflag && BIT(fp->fnum, dumpmap) == 0)
    834  1.1  cgd 		*dp++ = '^';
    835  1.1  cgd 	else if ((fp->fflags & NEW) != 0)
    836  1.1  cgd 		*dp++ = '*';
    837  1.1  cgd 	else
    838  1.1  cgd 		*dp++ = ' ';
    839  1.1  cgd 	for (cp = fp->fname; *cp; cp++)
    840  1.1  cgd 		if (!vflag && (*cp < ' ' || *cp >= 0177))
    841  1.1  cgd 			*dp++ = '?';
    842  1.1  cgd 		else
    843  1.1  cgd 			*dp++ = *cp;
    844  1.1  cgd 	if (fp->ftype == NODE)
    845  1.1  cgd 		*dp++ = '/';
    846  1.1  cgd 	*dp++ = 0;
    847  1.1  cgd 	return (fmtres);
    848  1.1  cgd }
    849  1.1  cgd 
    850  1.1  cgd /*
    851  1.1  cgd  * respond to interrupts
    852  1.1  cgd  */
    853  1.1  cgd void
    854  1.1  cgd onintr()
    855  1.1  cgd {
    856  1.1  cgd 	if (command == 'i')
    857  1.1  cgd 		longjmp(reset, 1);
    858  1.1  cgd 	if (reply("restore interrupted, continue") == FAIL)
    859  1.1  cgd 		done(1);
    860  1.1  cgd }
    861