Home | History | Annotate | Line # | Download | only in pax
pat_rep.c revision 1.1
      1  1.1  jtc /*-
      2  1.1  jtc  * Copyright (c) 1992 Keith Muller.
      3  1.1  jtc  * Copyright (c) 1992, 1993
      4  1.1  jtc  *	The Regents of the University of California.  All rights reserved.
      5  1.1  jtc  *
      6  1.1  jtc  * This code is derived from software contributed to Berkeley by
      7  1.1  jtc  * Keith Muller of the University of California, San Diego.
      8  1.1  jtc  *
      9  1.1  jtc  * Redistribution and use in source and binary forms, with or without
     10  1.1  jtc  * modification, are permitted provided that the following conditions
     11  1.1  jtc  * are met:
     12  1.1  jtc  * 1. Redistributions of source code must retain the above copyright
     13  1.1  jtc  *    notice, this list of conditions and the following disclaimer.
     14  1.1  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  jtc  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  jtc  *    documentation and/or other materials provided with the distribution.
     17  1.1  jtc  * 3. All advertising materials mentioning features or use of this software
     18  1.1  jtc  *    must display the following acknowledgement:
     19  1.1  jtc  *	This product includes software developed by the University of
     20  1.1  jtc  *	California, Berkeley and its contributors.
     21  1.1  jtc  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  jtc  *    may be used to endorse or promote products derived from this software
     23  1.1  jtc  *    without specific prior written permission.
     24  1.1  jtc  *
     25  1.1  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  jtc  * SUCH DAMAGE.
     36  1.1  jtc  */
     37  1.1  jtc 
     38  1.1  jtc #ifndef lint
     39  1.1  jtc static char sccsid[] = "@(#)pat_rep.c	8.2 (Berkeley) 4/18/94";
     40  1.1  jtc #endif /* not lint */
     41  1.1  jtc 
     42  1.1  jtc #include <sys/types.h>
     43  1.1  jtc #include <sys/time.h>
     44  1.1  jtc #include <sys/stat.h>
     45  1.1  jtc #include <sys/param.h>
     46  1.1  jtc #include <stdio.h>
     47  1.1  jtc #include <ctype.h>
     48  1.1  jtc #include <string.h>
     49  1.1  jtc #include <unistd.h>
     50  1.1  jtc #include <stdlib.h>
     51  1.1  jtc #ifdef NET2_REGEX
     52  1.1  jtc #include <regexp.h>
     53  1.1  jtc #else
     54  1.1  jtc #include <regex.h>
     55  1.1  jtc #endif
     56  1.1  jtc #include "pax.h"
     57  1.1  jtc #include "pat_rep.h"
     58  1.1  jtc #include "extern.h"
     59  1.1  jtc 
     60  1.1  jtc /*
     61  1.1  jtc  * routines to handle pattern matching, name modification (regular expression
     62  1.1  jtc  * substitution and interactive renames), and destination name modification for
     63  1.1  jtc  * copy (-rw). Both file name and link names are adjusted as required in these
     64  1.1  jtc  * routines.
     65  1.1  jtc  */
     66  1.1  jtc 
     67  1.1  jtc #define MAXSUBEXP	10		/* max subexpressions, DO NOT CHANGE */
     68  1.1  jtc static PATTERN *pathead = NULL;		/* file pattern match list head */
     69  1.1  jtc static PATTERN *pattail = NULL;		/* file pattern match list tail */
     70  1.1  jtc static REPLACE *rephead = NULL;		/* replacement string list head */
     71  1.1  jtc static REPLACE *reptail = NULL;		/* replacement string list tail */
     72  1.1  jtc 
     73  1.1  jtc static int rep_name __P((char *, int *, int));
     74  1.1  jtc static int tty_rename __P((register ARCHD *));
     75  1.1  jtc static int fix_path __P((char *, int *, char *, int));
     76  1.1  jtc static int fn_match __P((register char *, register char *, char **));
     77  1.1  jtc static char * range_match __P((register char *, register int));
     78  1.1  jtc #ifdef NET2_REGEX
     79  1.1  jtc static int resub __P((regexp *, char *, char *, register char *));
     80  1.1  jtc #else
     81  1.1  jtc static int resub __P((regex_t *, regmatch_t *, char *, char *, char *));
     82  1.1  jtc #endif
     83  1.1  jtc 
     84  1.1  jtc /*
     85  1.1  jtc  * rep_add()
     86  1.1  jtc  *	parses the -s replacement string; compiles the regular expression
     87  1.1  jtc  *	and stores the compiled value and it's replacement string together in
     88  1.1  jtc  *	replacement string list. Input to this function is of the form:
     89  1.1  jtc  *		/old/new/pg
     90  1.1  jtc  *	The first char in the string specifies the delimiter used by this
     91  1.1  jtc  *	replacement string. "Old" is a regular expression in "ed" format which
     92  1.1  jtc  *	is compiled by regcomp() and is applied to filenames. "new" is the
     93  1.1  jtc  *	substitution string; p and g are options flags for printing and global
     94  1.1  jtc  *	replacement (over the single filename)
     95  1.1  jtc  * Return:
     96  1.1  jtc  *	0 if a proper replacement string and regular expression was added to
     97  1.1  jtc  *	the list of replacement patterns; -1 otherwise.
     98  1.1  jtc  */
     99  1.1  jtc 
    100  1.1  jtc #if __STDC__
    101  1.1  jtc int
    102  1.1  jtc rep_add(register char *str)
    103  1.1  jtc #else
    104  1.1  jtc int
    105  1.1  jtc rep_add(str)
    106  1.1  jtc 	register char *str;
    107  1.1  jtc #endif
    108  1.1  jtc {
    109  1.1  jtc 	register char *pt1;
    110  1.1  jtc 	register char *pt2;
    111  1.1  jtc 	register REPLACE *rep;
    112  1.1  jtc #	ifndef NET2_REGEX
    113  1.1  jtc 	register int res;
    114  1.1  jtc 	char rebuf[BUFSIZ];
    115  1.1  jtc #	endif
    116  1.1  jtc 
    117  1.1  jtc 	/*
    118  1.1  jtc 	 * throw out the bad parameters
    119  1.1  jtc 	 */
    120  1.1  jtc 	if ((str == NULL) || (*str == '\0')) {
    121  1.1  jtc 		warn(1, "Empty replacement string");
    122  1.1  jtc 		return(-1);
    123  1.1  jtc 	}
    124  1.1  jtc 
    125  1.1  jtc 	/*
    126  1.1  jtc 	 * first character in the string specifies what the delimiter is for
    127  1.1  jtc 	 * this expression
    128  1.1  jtc 	 */
    129  1.1  jtc 	if ((pt1 = strchr(str+1, *str)) == NULL) {
    130  1.1  jtc 		warn(1, "Invalid replacement string %s", str);
    131  1.1  jtc 		return(-1);
    132  1.1  jtc 	}
    133  1.1  jtc 
    134  1.1  jtc 	/*
    135  1.1  jtc 	 * allocate space for the node that handles this replacement pattern
    136  1.1  jtc 	 * and split out the regular expression and try to compile it
    137  1.1  jtc 	 */
    138  1.1  jtc 	if ((rep = (REPLACE *)malloc(sizeof(REPLACE))) == NULL) {
    139  1.1  jtc 		warn(1, "Unable to allocate memory for replacement string");
    140  1.1  jtc 		return(-1);
    141  1.1  jtc 	}
    142  1.1  jtc 
    143  1.1  jtc 	*pt1 = '\0';
    144  1.1  jtc #	ifdef NET2_REGEX
    145  1.1  jtc 	if ((rep->rcmp = regcomp(str+1)) == NULL) {
    146  1.1  jtc #	else
    147  1.1  jtc 	if ((res = regcomp(&(rep->rcmp), str+1, 0)) != 0) {
    148  1.1  jtc 		regerror(res, &(rep->rcmp), rebuf, sizeof(rebuf));
    149  1.1  jtc 		warn(1, "%s while compiling regular expression %s", rebuf, str);
    150  1.1  jtc #	endif
    151  1.1  jtc 		(void)free((char *)rep);
    152  1.1  jtc 		return(-1);
    153  1.1  jtc 	}
    154  1.1  jtc 
    155  1.1  jtc 	/*
    156  1.1  jtc 	 * put the delimiter back in case we need an error message and
    157  1.1  jtc 	 * locate the delimiter at the end of the replacement string
    158  1.1  jtc 	 * we then point the node at the new substitution string
    159  1.1  jtc 	 */
    160  1.1  jtc 	*pt1++ = *str;
    161  1.1  jtc 	if ((pt2 = strchr(pt1, *str)) == NULL) {
    162  1.1  jtc #		ifdef NET2_REGEX
    163  1.1  jtc 		(void)free((char *)rep->rcmp);
    164  1.1  jtc #		else
    165  1.1  jtc 		regfree(&(rep->rcmp));
    166  1.1  jtc #		endif
    167  1.1  jtc 		(void)free((char *)rep);
    168  1.1  jtc 		warn(1, "Invalid replacement string %s", str);
    169  1.1  jtc 		return(-1);
    170  1.1  jtc 	}
    171  1.1  jtc 
    172  1.1  jtc 	*pt2 = '\0';
    173  1.1  jtc 	rep->nstr = pt1;
    174  1.1  jtc 	pt1 = pt2++;
    175  1.1  jtc 	rep->flgs = 0;
    176  1.1  jtc 
    177  1.1  jtc 	/*
    178  1.1  jtc 	 * set the options if any
    179  1.1  jtc 	 */
    180  1.1  jtc 	while (*pt2 != '\0') {
    181  1.1  jtc 		switch(*pt2) {
    182  1.1  jtc 		case 'g':
    183  1.1  jtc 		case 'G':
    184  1.1  jtc 			rep->flgs  |= GLOB;
    185  1.1  jtc 			break;
    186  1.1  jtc 		case 'p':
    187  1.1  jtc 		case 'P':
    188  1.1  jtc 			rep->flgs  |= PRNT;
    189  1.1  jtc 			break;
    190  1.1  jtc 		default:
    191  1.1  jtc #			ifdef NET2_REGEX
    192  1.1  jtc 			(void)free((char *)rep->rcmp);
    193  1.1  jtc #			else
    194  1.1  jtc 			regfree(&(rep->rcmp));
    195  1.1  jtc #			endif
    196  1.1  jtc 			(void)free((char *)rep);
    197  1.1  jtc 			*pt1 = *str;
    198  1.1  jtc 			warn(1, "Invalid replacement string option %s", str);
    199  1.1  jtc 			return(-1);
    200  1.1  jtc 		}
    201  1.1  jtc 		++pt2;
    202  1.1  jtc 	}
    203  1.1  jtc 
    204  1.1  jtc 	/*
    205  1.1  jtc 	 * all done, link it in at the end
    206  1.1  jtc 	 */
    207  1.1  jtc 	rep->fow = NULL;
    208  1.1  jtc 	if (rephead == NULL) {
    209  1.1  jtc 		reptail = rephead = rep;
    210  1.1  jtc 		return(0);
    211  1.1  jtc 	}
    212  1.1  jtc 	reptail->fow = rep;
    213  1.1  jtc 	reptail = rep;
    214  1.1  jtc 	return(0);
    215  1.1  jtc }
    216  1.1  jtc 
    217  1.1  jtc /*
    218  1.1  jtc  * pat_add()
    219  1.1  jtc  *	add a pattern match to the pattern match list. Pattern matches are used
    220  1.1  jtc  *	to select which archive members are extracted. (They appear as
    221  1.1  jtc  *	arguments to pax in the list and read modes). If no patterns are
    222  1.1  jtc  *	supplied to pax, all members in the archive will be selected (and the
    223  1.1  jtc  *	pattern match list is empty).
    224  1.1  jtc  * Return:
    225  1.1  jtc  *	0 if the pattern was added to the list, -1 otherwise
    226  1.1  jtc  */
    227  1.1  jtc 
    228  1.1  jtc #if __STDC__
    229  1.1  jtc int
    230  1.1  jtc pat_add(char *str)
    231  1.1  jtc #else
    232  1.1  jtc int
    233  1.1  jtc pat_add(str)
    234  1.1  jtc 	char *str;
    235  1.1  jtc #endif
    236  1.1  jtc {
    237  1.1  jtc 	register PATTERN *pt;
    238  1.1  jtc 
    239  1.1  jtc 	/*
    240  1.1  jtc 	 * throw out the junk
    241  1.1  jtc 	 */
    242  1.1  jtc 	if ((str == NULL) || (*str == '\0')) {
    243  1.1  jtc 		warn(1, "Empty pattern string");
    244  1.1  jtc 		return(-1);
    245  1.1  jtc 	}
    246  1.1  jtc 
    247  1.1  jtc 	/*
    248  1.1  jtc 	 * allocate space for the pattern and store the pattern. the pattern is
    249  1.1  jtc 	 * part of argv so do not bother to copy it, just point at it. Add the
    250  1.1  jtc 	 * node to the end of the pattern list
    251  1.1  jtc 	 */
    252  1.1  jtc 	if ((pt = (PATTERN *)malloc(sizeof(PATTERN))) == NULL) {
    253  1.1  jtc 		warn(1, "Unable to allocate memory for pattern string");
    254  1.1  jtc 		return(-1);
    255  1.1  jtc 	}
    256  1.1  jtc 
    257  1.1  jtc 	pt->pstr = str;
    258  1.1  jtc 	pt->pend = NULL;
    259  1.1  jtc 	pt->plen = strlen(str);
    260  1.1  jtc 	pt->fow = NULL;
    261  1.1  jtc 	pt->flgs = 0;
    262  1.1  jtc 	if (pathead == NULL) {
    263  1.1  jtc 		pattail = pathead = pt;
    264  1.1  jtc 		return(0);
    265  1.1  jtc 	}
    266  1.1  jtc 	pattail->fow = pt;
    267  1.1  jtc 	pattail = pt;
    268  1.1  jtc 	return(0);
    269  1.1  jtc }
    270  1.1  jtc 
    271  1.1  jtc /*
    272  1.1  jtc  * pat_chk()
    273  1.1  jtc  *	complain if any the user supplied pattern did not result in a match to
    274  1.1  jtc  *	a selected archive member.
    275  1.1  jtc  */
    276  1.1  jtc 
    277  1.1  jtc #if __STDC__
    278  1.1  jtc void
    279  1.1  jtc pat_chk(void)
    280  1.1  jtc #else
    281  1.1  jtc void
    282  1.1  jtc pat_chk()
    283  1.1  jtc #endif
    284  1.1  jtc {
    285  1.1  jtc 	register PATTERN *pt;
    286  1.1  jtc 	register int wban = 0;
    287  1.1  jtc 
    288  1.1  jtc 	/*
    289  1.1  jtc 	 * walk down the list checking the flags to make sure MTCH was set,
    290  1.1  jtc 	 * if not complain
    291  1.1  jtc 	 */
    292  1.1  jtc 	for (pt = pathead; pt != NULL; pt = pt->fow) {
    293  1.1  jtc 		if (pt->flgs & MTCH)
    294  1.1  jtc 			continue;
    295  1.1  jtc 		if (!wban) {
    296  1.1  jtc 			warn(1, "WARNING! These patterns were not matched:");
    297  1.1  jtc 			++wban;
    298  1.1  jtc 		}
    299  1.1  jtc 		(void)fprintf(stderr, "%s\n", pt->pstr);
    300  1.1  jtc 	}
    301  1.1  jtc }
    302  1.1  jtc 
    303  1.1  jtc /*
    304  1.1  jtc  * pat_sel()
    305  1.1  jtc  *	the archive member which matches a pattern was selected. Mark the
    306  1.1  jtc  *	pattern as having selected an archive member. arcn->pat points at the
    307  1.1  jtc  *	pattern that was matched. arcn->pat is set in pat_match()
    308  1.1  jtc  *
    309  1.1  jtc  *	NOTE: When the -c option is used, we are called when there was no match
    310  1.1  jtc  *	by pat_match() (that means we did match before the inverted sense of
    311  1.1  jtc  *	the logic). Now this seems really strange at first, but with -c  we
    312  1.1  jtc  *	need to keep track of those patterns that cause a archive member to NOT
    313  1.1  jtc  *	be selected (it found an archive member with a specified pattern)
    314  1.1  jtc  * Return:
    315  1.1  jtc  *	0 if the pattern pointed at by arcn->pat was tagged as creating a
    316  1.1  jtc  *	match, -1 otherwise.
    317  1.1  jtc  */
    318  1.1  jtc 
    319  1.1  jtc #if __STDC__
    320  1.1  jtc int
    321  1.1  jtc pat_sel(register ARCHD *arcn)
    322  1.1  jtc #else
    323  1.1  jtc int
    324  1.1  jtc pat_sel(arcn)
    325  1.1  jtc 	register ARCHD *arcn;
    326  1.1  jtc #endif
    327  1.1  jtc {
    328  1.1  jtc 	register PATTERN *pt;
    329  1.1  jtc 	register PATTERN **ppt;
    330  1.1  jtc 	register int len;
    331  1.1  jtc 
    332  1.1  jtc 	/*
    333  1.1  jtc 	 * if no patterns just return
    334  1.1  jtc 	 */
    335  1.1  jtc 	if ((pathead == NULL) || ((pt = arcn->pat) == NULL))
    336  1.1  jtc 		return(0);
    337  1.1  jtc 
    338  1.1  jtc 	/*
    339  1.1  jtc 	 * when we are NOT limited to a single match per pattern mark the
    340  1.1  jtc 	 * pattern and return
    341  1.1  jtc 	 */
    342  1.1  jtc 	if (!nflag) {
    343  1.1  jtc 		pt->flgs |= MTCH;
    344  1.1  jtc 		return(0);
    345  1.1  jtc 	}
    346  1.1  jtc 
    347  1.1  jtc 	/*
    348  1.1  jtc 	 * we reach this point only when we allow a single selected match per
    349  1.1  jtc 	 * pattern, if the pattern matches a directory and we do not have -d
    350  1.1  jtc 	 * (dflag) we are done with this pattern. We may also be handed a file
    351  1.1  jtc 	 * in the subtree of a directory. in that case when we are operating
    352  1.1  jtc 	 * with -d, this pattern was already selected and we are done
    353  1.1  jtc 	 */
    354  1.1  jtc 	if (pt->flgs & DIR_MTCH)
    355  1.1  jtc 		return(0);
    356  1.1  jtc 
    357  1.1  jtc 	if (!dflag && ((pt->pend != NULL) || (arcn->type == PAX_DIR))) {
    358  1.1  jtc 		/*
    359  1.1  jtc 		 * ok we matched a directory and we are allowing
    360  1.1  jtc 		 * subtree matches but because of the -n only its children will
    361  1.1  jtc 		 * match. This is tagged as a DIR_MTCH type.
    362  1.1  jtc 		 * WATCH IT, the code assumes that pt->pend points
    363  1.1  jtc 		 * into arcn->name and arcn->name has not been modified.
    364  1.1  jtc 		 * If not we will have a big mess. Yup this is another kludge
    365  1.1  jtc 		 */
    366  1.1  jtc 
    367  1.1  jtc 		/*
    368  1.1  jtc 		 * if this was a prefix match, remove trailing part of path
    369  1.1  jtc 		 * so we can copy it. Future matches will be exact prefix match
    370  1.1  jtc 		 */
    371  1.1  jtc 		if (pt->pend != NULL)
    372  1.1  jtc 			*pt->pend = '\0';
    373  1.1  jtc 
    374  1.1  jtc 		if ((pt->pstr = strdup(arcn->name)) == NULL) {
    375  1.1  jtc 			warn(1, "Pattern select out of memory");
    376  1.1  jtc 			if (pt->pend != NULL)
    377  1.1  jtc 				*pt->pend = '/';
    378  1.1  jtc 			pt->pend = NULL;
    379  1.1  jtc 			return(-1);
    380  1.1  jtc 		}
    381  1.1  jtc 
    382  1.1  jtc 		/*
    383  1.1  jtc 		 * put the trailing / back in the source string
    384  1.1  jtc 		 */
    385  1.1  jtc 		if (pt->pend != NULL) {
    386  1.1  jtc 			*pt->pend = '/';
    387  1.1  jtc 			pt->pend = NULL;
    388  1.1  jtc 		}
    389  1.1  jtc 		pt->plen = strlen(pt->pstr);
    390  1.1  jtc 
    391  1.1  jtc 		/*
    392  1.1  jtc 		 * strip off any trailing /, this should really never happen
    393  1.1  jtc 		 */
    394  1.1  jtc 		len = pt->plen - 1;
    395  1.1  jtc 		if (*(pt->pstr + len) == '/') {
    396  1.1  jtc 			*(pt->pstr + len) = '\0';
    397  1.1  jtc 			pt->plen = len;
    398  1.1  jtc 		}
    399  1.1  jtc 		pt->flgs = DIR_MTCH | MTCH;
    400  1.1  jtc 		arcn->pat = pt;
    401  1.1  jtc 		return(0);
    402  1.1  jtc 	}
    403  1.1  jtc 
    404  1.1  jtc 	/*
    405  1.1  jtc 	 * we are then done with this pattern, so we delete it from the list
    406  1.1  jtc 	 * because it can never be used for another match.
    407  1.1  jtc 	 * Seems kind of strange to do for a -c, but the pax spec is really
    408  1.1  jtc 	 * vague on the interaction of -c -n and -d. We assume that when -c
    409  1.1  jtc 	 * and the pattern rejects a member (i.e. it matched it) it is done.
    410  1.1  jtc 	 * In effect we place the order of the flags as having -c last.
    411  1.1  jtc 	 */
    412  1.1  jtc 	pt = pathead;
    413  1.1  jtc 	ppt = &pathead;
    414  1.1  jtc 	while ((pt != NULL) && (pt != arcn->pat)) {
    415  1.1  jtc 		ppt = &(pt->fow);
    416  1.1  jtc 		pt = pt->fow;
    417  1.1  jtc 	}
    418  1.1  jtc 
    419  1.1  jtc 	if (pt == NULL) {
    420  1.1  jtc 		/*
    421  1.1  jtc 		 * should never happen....
    422  1.1  jtc 		 */
    423  1.1  jtc 		warn(1, "Pattern list inconsistant");
    424  1.1  jtc 		return(-1);
    425  1.1  jtc 	}
    426  1.1  jtc 	*ppt = pt->fow;
    427  1.1  jtc 	(void)free((char *)pt);
    428  1.1  jtc 	arcn->pat = NULL;
    429  1.1  jtc 	return(0);
    430  1.1  jtc }
    431  1.1  jtc 
    432  1.1  jtc /*
    433  1.1  jtc  * pat_match()
    434  1.1  jtc  *	see if this archive member matches any supplied pattern, if a match
    435  1.1  jtc  *	is found, arcn->pat is set to point at the potential pattern. Later if
    436  1.1  jtc  *	this archive member is "selected" we process and mark the pattern as
    437  1.1  jtc  *	one which matched a selected archive member (see pat_sel())
    438  1.1  jtc  * Return:
    439  1.1  jtc  *	0 if this archive member should be processed, 1 if it should be
    440  1.1  jtc  *	skipped and -1 if we are done with all patterns (and pax should quit
    441  1.1  jtc  *	looking for more members)
    442  1.1  jtc  */
    443  1.1  jtc 
    444  1.1  jtc #if __STDC__
    445  1.1  jtc int
    446  1.1  jtc pat_match(register ARCHD *arcn)
    447  1.1  jtc #else
    448  1.1  jtc int
    449  1.1  jtc pat_match(arcn)
    450  1.1  jtc 	register ARCHD *arcn;
    451  1.1  jtc #endif
    452  1.1  jtc {
    453  1.1  jtc 	register PATTERN *pt;
    454  1.1  jtc 
    455  1.1  jtc 	arcn->pat = NULL;
    456  1.1  jtc 
    457  1.1  jtc 	/*
    458  1.1  jtc 	 * if there are no more patterns and we have -n (and not -c) we are
    459  1.1  jtc 	 * done. otherwise with no patterns to match, matches all
    460  1.1  jtc 	 */
    461  1.1  jtc 	if (pathead == NULL) {
    462  1.1  jtc 		if (nflag && !cflag)
    463  1.1  jtc 			return(-1);
    464  1.1  jtc 		return(0);
    465  1.1  jtc 	}
    466  1.1  jtc 
    467  1.1  jtc 	/*
    468  1.1  jtc 	 * have to search down the list one at a time looking for a match.
    469  1.1  jtc 	 */
    470  1.1  jtc 	pt = pathead;
    471  1.1  jtc 	while (pt != NULL) {
    472  1.1  jtc 		/*
    473  1.1  jtc 		 * check for a file name match unless we have DIR_MTCH set in
    474  1.1  jtc 		 * this pattern then we want a prefix match
    475  1.1  jtc 		 */
    476  1.1  jtc 		if (pt->flgs & DIR_MTCH) {
    477  1.1  jtc 			/*
    478  1.1  jtc 			 * this pattern was matched before to a directory
    479  1.1  jtc 			 * as we must have -n set for this (but not -d). We can
    480  1.1  jtc 			 * only match CHILDREN of that directory so we must use
    481  1.1  jtc 			 * an exact prefix match (no wildcards).
    482  1.1  jtc 			 */
    483  1.1  jtc 			if ((arcn->name[pt->plen] == '/') &&
    484  1.1  jtc 			    (strncmp(pt->pstr, arcn->name, pt->plen) == 0))
    485  1.1  jtc 				break;
    486  1.1  jtc 		} else if (fn_match(pt->pstr, arcn->name, &pt->pend) == 0)
    487  1.1  jtc 			break;
    488  1.1  jtc 		pt = pt->fow;
    489  1.1  jtc 	}
    490  1.1  jtc 
    491  1.1  jtc 	/*
    492  1.1  jtc 	 * return the result, remember that cflag (-c) inverts the sense of a
    493  1.1  jtc 	 * match
    494  1.1  jtc 	 */
    495  1.1  jtc 	if (pt == NULL)
    496  1.1  jtc 		return(cflag ? 0 : 1);
    497  1.1  jtc 
    498  1.1  jtc 	/*
    499  1.1  jtc 	 * we had a match, now when we invert the sense (-c) we reject this
    500  1.1  jtc 	 * member. However we have to tag the pattern a being successful, (in a
    501  1.1  jtc 	 * match, not in selecting a archive member) so we call pat_sel() here.
    502  1.1  jtc 	 */
    503  1.1  jtc 	arcn->pat = pt;
    504  1.1  jtc 	if (!cflag)
    505  1.1  jtc 		return(0);
    506  1.1  jtc 
    507  1.1  jtc 	if (pat_sel(arcn) < 0)
    508  1.1  jtc 		return(-1);
    509  1.1  jtc 	arcn->pat = NULL;
    510  1.1  jtc 	return(1);
    511  1.1  jtc }
    512  1.1  jtc 
    513  1.1  jtc /*
    514  1.1  jtc  * fn_match()
    515  1.1  jtc  * Return:
    516  1.1  jtc  *	0 if this archive member should be processed, 1 if it should be
    517  1.1  jtc  *	skipped and -1 if we are done with all patterns (and pax should quit
    518  1.1  jtc  *	looking for more members)
    519  1.1  jtc  *	Note: *pend may be changed to show where the prefix ends.
    520  1.1  jtc  */
    521  1.1  jtc 
    522  1.1  jtc #if __STDC__
    523  1.1  jtc static int
    524  1.1  jtc fn_match(register char *pattern, register char *string, char **pend)
    525  1.1  jtc #else
    526  1.1  jtc static int
    527  1.1  jtc fn_match(pattern, string, pend)
    528  1.1  jtc 	register char *pattern;
    529  1.1  jtc 	register char *string;
    530  1.1  jtc 	char **pend;
    531  1.1  jtc #endif
    532  1.1  jtc {
    533  1.1  jtc 	register char c;
    534  1.1  jtc 	char test;
    535  1.1  jtc 
    536  1.1  jtc 	*pend = NULL;
    537  1.1  jtc 	for (;;) {
    538  1.1  jtc 		switch (c = *pattern++) {
    539  1.1  jtc 		case '\0':
    540  1.1  jtc 			/*
    541  1.1  jtc 			 * Ok we found an exact match
    542  1.1  jtc 			 */
    543  1.1  jtc 			if (*string == '\0')
    544  1.1  jtc 				return(0);
    545  1.1  jtc 
    546  1.1  jtc 			/*
    547  1.1  jtc 			 * Check if it is a prefix match
    548  1.1  jtc 			 */
    549  1.1  jtc 			if ((dflag == 1) || (*string != '/'))
    550  1.1  jtc 				return(-1);
    551  1.1  jtc 
    552  1.1  jtc 			/*
    553  1.1  jtc 			 * It is a prefix match, remember where the trailing
    554  1.1  jtc 			 * / is located
    555  1.1  jtc 			 */
    556  1.1  jtc 			*pend = string;
    557  1.1  jtc 			return(0);
    558  1.1  jtc 		case '?':
    559  1.1  jtc 			if ((test = *string++) == '\0')
    560  1.1  jtc 				return (-1);
    561  1.1  jtc 			break;
    562  1.1  jtc 		case '*':
    563  1.1  jtc 			c = *pattern;
    564  1.1  jtc 			/*
    565  1.1  jtc 			 * Collapse multiple *'s.
    566  1.1  jtc 			 */
    567  1.1  jtc 			while (c == '*')
    568  1.1  jtc 				c = *++pattern;
    569  1.1  jtc 
    570  1.1  jtc 			/*
    571  1.1  jtc 			 * Optimized hack for pattern with a * at the end
    572  1.1  jtc 			 */
    573  1.1  jtc 			if (c == '\0')
    574  1.1  jtc 				return (0);
    575  1.1  jtc 
    576  1.1  jtc 			/*
    577  1.1  jtc 			 * General case, use recursion.
    578  1.1  jtc 			 */
    579  1.1  jtc 			while ((test = *string) != '\0') {
    580  1.1  jtc 				if (!fn_match(pattern, string, pend))
    581  1.1  jtc 					return (0);
    582  1.1  jtc 				++string;
    583  1.1  jtc 			}
    584  1.1  jtc 			return (-1);
    585  1.1  jtc 		case '[':
    586  1.1  jtc 			/*
    587  1.1  jtc 			 * range match
    588  1.1  jtc 			 */
    589  1.1  jtc 			if (((test = *string++) == '\0') ||
    590  1.1  jtc 			    ((pattern = range_match(pattern, test)) == NULL))
    591  1.1  jtc 				return (-1);
    592  1.1  jtc 			break;
    593  1.1  jtc 		case '\\':
    594  1.1  jtc 		default:
    595  1.1  jtc 			if (c != *string++)
    596  1.1  jtc 				return (-1);
    597  1.1  jtc 			break;
    598  1.1  jtc 		}
    599  1.1  jtc 	}
    600  1.1  jtc 	/* NOTREACHED */
    601  1.1  jtc }
    602  1.1  jtc 
    603  1.1  jtc #ifdef __STDC__
    604  1.1  jtc static char *
    605  1.1  jtc range_match(register char *pattern, register int test)
    606  1.1  jtc #else
    607  1.1  jtc static char *
    608  1.1  jtc range_match(pattern, test)
    609  1.1  jtc 	register char *pattern;
    610  1.1  jtc 	register int test;
    611  1.1  jtc #endif
    612  1.1  jtc {
    613  1.1  jtc 	register char c;
    614  1.1  jtc 	register char c2;
    615  1.1  jtc 	int negate;
    616  1.1  jtc 	int ok = 0;
    617  1.1  jtc 
    618  1.1  jtc 	if (negate = (*pattern == '!'))
    619  1.1  jtc 		++pattern;
    620  1.1  jtc 
    621  1.1  jtc 	while ((c = *pattern++) != ']') {
    622  1.1  jtc 		/*
    623  1.1  jtc 		 * Illegal pattern
    624  1.1  jtc 		 */
    625  1.1  jtc 		if (c == '\0')
    626  1.1  jtc 			return (NULL);
    627  1.1  jtc 
    628  1.1  jtc 		if ((*pattern == '-') && ((c2 = pattern[1]) != '\0') &&
    629  1.1  jtc 		    (c2 != ']')) {
    630  1.1  jtc 			if ((c <= test) && (test <= c2))
    631  1.1  jtc 				ok = 1;
    632  1.1  jtc 			pattern += 2;
    633  1.1  jtc 		} else if (c == test)
    634  1.1  jtc 			ok = 1;
    635  1.1  jtc 	}
    636  1.1  jtc 	return (ok == negate ? NULL : pattern);
    637  1.1  jtc }
    638  1.1  jtc 
    639  1.1  jtc /*
    640  1.1  jtc  * mod_name()
    641  1.1  jtc  *	modify a selected file name. first attempt to apply replacement string
    642  1.1  jtc  *	expressions, then apply interactive file rename. We apply replacement
    643  1.1  jtc  *	string expressions to both filenames and file links (if we didn't the
    644  1.1  jtc  *	links would point to the wrong place, and we could never be able to
    645  1.1  jtc  *	move an archive that has a file link in it). When we rename files
    646  1.1  jtc  *	interactively, we store that mapping (old name to user input name) so
    647  1.1  jtc  *	if we spot any file links to the old file name in the future, we will
    648  1.1  jtc  *	know exactly how to fix the file link.
    649  1.1  jtc  * Return:
    650  1.1  jtc  *	0 continue to  process file, 1 skip this file, -1 pax is finished
    651  1.1  jtc  */
    652  1.1  jtc 
    653  1.1  jtc #if __STDC__
    654  1.1  jtc int
    655  1.1  jtc mod_name(register ARCHD *arcn)
    656  1.1  jtc #else
    657  1.1  jtc int
    658  1.1  jtc mod_name(arcn)
    659  1.1  jtc 	register ARCHD *arcn;
    660  1.1  jtc #endif
    661  1.1  jtc {
    662  1.1  jtc 	register int res = 0;
    663  1.1  jtc 
    664  1.1  jtc 	/*
    665  1.1  jtc 	 * IMPORTANT: We have a problem. what do we do with symlinks?
    666  1.1  jtc 	 * Modifying a hard link name makes sense, as we know the file it
    667  1.1  jtc 	 * points at should have been seen already in the archive (and if it
    668  1.1  jtc 	 * wasn't seen because of a read error or a bad archive, we lose
    669  1.1  jtc 	 * anyway). But there are no such requirements for symlinks. On one
    670  1.1  jtc 	 * hand the symlink that refers to a file in the archive will have to
    671  1.1  jtc 	 * be modified to so it will still work at its new location in the
    672  1.1  jtc 	 * file system. On the other hand a symlink that points elsewhere (and
    673  1.1  jtc 	 * should continue to do so) should not be modified. There is clearly
    674  1.1  jtc 	 * no perfect solution here. So we handle them like hardlinks. Clearly
    675  1.1  jtc 	 * a replacement made by the interactive rename mapping is very likely
    676  1.1  jtc 	 * to be correct since it applies to a single file and is an exact
    677  1.1  jtc 	 * match. The regular expression replacements are a little harder to
    678  1.1  jtc 	 * justify though. We claim that the symlink name is only likely
    679  1.1  jtc 	 * to be replaced when it points within the file tree being moved and
    680  1.1  jtc 	 * in that case it should be modified. what we really need to do is to
    681  1.1  jtc 	 * call an oracle here. :)
    682  1.1  jtc 	 */
    683  1.1  jtc 	if (rephead != NULL) {
    684  1.1  jtc 		/*
    685  1.1  jtc 		 * we have replacement strings, modify the name and the link
    686  1.1  jtc 		 * name if any.
    687  1.1  jtc 		 */
    688  1.1  jtc 		if ((res = rep_name(arcn->name, &(arcn->nlen), 1)) != 0)
    689  1.1  jtc 			return(res);
    690  1.1  jtc 
    691  1.1  jtc 		if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    692  1.1  jtc 		    (arcn->type == PAX_HRG)) &&
    693  1.1  jtc 		    ((res = rep_name(arcn->ln_name, &(arcn->ln_nlen), 0)) != 0))
    694  1.1  jtc 			return(res);
    695  1.1  jtc 	}
    696  1.1  jtc 
    697  1.1  jtc 	if (iflag) {
    698  1.1  jtc 		/*
    699  1.1  jtc 		 * perform interactive file rename, then map the link if any
    700  1.1  jtc 		 */
    701  1.1  jtc 		if ((res = tty_rename(arcn)) != 0)
    702  1.1  jtc 			return(res);
    703  1.1  jtc 		if ((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    704  1.1  jtc 		    (arcn->type == PAX_HRG))
    705  1.1  jtc 			sub_name(arcn->ln_name, &(arcn->ln_nlen));
    706  1.1  jtc 	}
    707  1.1  jtc 	return(res);
    708  1.1  jtc }
    709  1.1  jtc 
    710  1.1  jtc /*
    711  1.1  jtc  * tty_rename()
    712  1.1  jtc  *	Prompt the user for a replacement file name. A "." keeps the old name,
    713  1.1  jtc  *	a empty line skips the file, and an EOF on reading the tty, will cause
    714  1.1  jtc  *	pax to stop processing and exit. Otherwise the file name input, replaces
    715  1.1  jtc  *	the old one.
    716  1.1  jtc  * Return:
    717  1.1  jtc  *	0 process this file, 1 skip this file, -1 we need to exit pax
    718  1.1  jtc  */
    719  1.1  jtc 
    720  1.1  jtc #if __STDC__
    721  1.1  jtc static int
    722  1.1  jtc tty_rename(register ARCHD *arcn)
    723  1.1  jtc #else
    724  1.1  jtc static int
    725  1.1  jtc tty_rename(arcn)
    726  1.1  jtc 	register ARCHD *arcn;
    727  1.1  jtc #endif
    728  1.1  jtc {
    729  1.1  jtc 	char tmpname[PAXPATHLEN+2];
    730  1.1  jtc 	int res;
    731  1.1  jtc 
    732  1.1  jtc 	/*
    733  1.1  jtc 	 * prompt user for the replacement name for a file, keep trying until
    734  1.1  jtc 	 * we get some reasonable input. Archives may have more than one file
    735  1.1  jtc 	 * on them with the same name (from updates etc). We print verbose info
    736  1.1  jtc 	 * on the file so the user knows what is up.
    737  1.1  jtc 	 */
    738  1.1  jtc 	tty_prnt("\nATTENTION: %s interactive file rename operation.\n", argv0);
    739  1.1  jtc 
    740  1.1  jtc 	for (;;) {
    741  1.1  jtc 		ls_tty(arcn);
    742  1.1  jtc 		tty_prnt("Input new name, or a \".\" to keep the old name, ");
    743  1.1  jtc 		tty_prnt("or a \"return\" to skip this file.\n");
    744  1.1  jtc 		tty_prnt("Input > ");
    745  1.1  jtc 		if (tty_read(tmpname, sizeof(tmpname)) < 0)
    746  1.1  jtc 			return(-1);
    747  1.1  jtc 		if (strcmp(tmpname, "..") == 0) {
    748  1.1  jtc 			tty_prnt("Try again, illegal file name: ..\n");
    749  1.1  jtc 			continue;
    750  1.1  jtc 		}
    751  1.1  jtc 		if (strlen(tmpname) > PAXPATHLEN) {
    752  1.1  jtc 			tty_prnt("Try again, file name too long\n");
    753  1.1  jtc 			continue;
    754  1.1  jtc 		}
    755  1.1  jtc 		break;
    756  1.1  jtc 	}
    757  1.1  jtc 
    758  1.1  jtc 	/*
    759  1.1  jtc 	 * empty file name, skips this file. a "." leaves it alone
    760  1.1  jtc 	 */
    761  1.1  jtc 	if (tmpname[0] == '\0') {
    762  1.1  jtc 		tty_prnt("Skipping file.\n");
    763  1.1  jtc 		return(1);
    764  1.1  jtc 	}
    765  1.1  jtc 	if ((tmpname[0] == '.') && (tmpname[1] == '\0')) {
    766  1.1  jtc 		tty_prnt("Processing continues, name unchanged.\n");
    767  1.1  jtc 		return(0);
    768  1.1  jtc 	}
    769  1.1  jtc 
    770  1.1  jtc 	/*
    771  1.1  jtc 	 * ok the name changed. We may run into links that point at this
    772  1.1  jtc 	 * file later. we have to remember where the user sent the file
    773  1.1  jtc 	 * in order to repair any links.
    774  1.1  jtc 	 */
    775  1.1  jtc 	tty_prnt("Processing continues, name changed to: %s\n", tmpname);
    776  1.1  jtc 	res = add_name(arcn->name, arcn->nlen, tmpname);
    777  1.1  jtc 	arcn->nlen = l_strncpy(arcn->name, tmpname, PAXPATHLEN+1);
    778  1.1  jtc 	if (res < 0)
    779  1.1  jtc 		return(-1);
    780  1.1  jtc 	return(0);
    781  1.1  jtc }
    782  1.1  jtc 
    783  1.1  jtc /*
    784  1.1  jtc  * set_dest()
    785  1.1  jtc  *	fix up the file name and the link name (if any) so this file will land
    786  1.1  jtc  *	in the destination directory (used during copy() -rw).
    787  1.1  jtc  * Return:
    788  1.1  jtc  *	0 if ok, -1 if failure (name too long)
    789  1.1  jtc  */
    790  1.1  jtc 
    791  1.1  jtc #if __STDC__
    792  1.1  jtc int
    793  1.1  jtc set_dest(register ARCHD *arcn, char *dest_dir, int dir_len)
    794  1.1  jtc #else
    795  1.1  jtc int
    796  1.1  jtc set_dest(arcn, dest_dir, dir_len)
    797  1.1  jtc 	register ARCHD *arcn;
    798  1.1  jtc 	char *dest_dir;
    799  1.1  jtc 	int dir_len;
    800  1.1  jtc #endif
    801  1.1  jtc {
    802  1.1  jtc 	if (fix_path(arcn->name, &(arcn->nlen), dest_dir, dir_len) < 0)
    803  1.1  jtc 		return(-1);
    804  1.1  jtc 
    805  1.1  jtc 	/*
    806  1.1  jtc 	 * It is really hard to deal with symlinks here, we cannot be sure
    807  1.1  jtc 	 * if the name they point was moved (or will be moved). It is best to
    808  1.1  jtc 	 * leave them alone.
    809  1.1  jtc 	 */
    810  1.1  jtc 	if ((arcn->type != PAX_HLK) && (arcn->type != PAX_HRG))
    811  1.1  jtc 		return(0);
    812  1.1  jtc 
    813  1.1  jtc 	if (fix_path(arcn->ln_name, &(arcn->ln_nlen), dest_dir, dir_len) < 0)
    814  1.1  jtc 		return(-1);
    815  1.1  jtc 	return(0);
    816  1.1  jtc }
    817  1.1  jtc 
    818  1.1  jtc /*
    819  1.1  jtc  * fix_path
    820  1.1  jtc  *	concatenate dir_name and or_name and store the result in or_name (if
    821  1.1  jtc  *	it fits). This is one ugly function.
    822  1.1  jtc  * Return:
    823  1.1  jtc  *	0 if ok, -1 if the final name is too long
    824  1.1  jtc  */
    825  1.1  jtc 
    826  1.1  jtc #if __STDC__
    827  1.1  jtc static int
    828  1.1  jtc fix_path( char *or_name, int *or_len, char *dir_name, int dir_len)
    829  1.1  jtc #else
    830  1.1  jtc static int
    831  1.1  jtc fix_path(or_name, or_len, dir_name, dir_len)
    832  1.1  jtc 	char *or_name;
    833  1.1  jtc 	int *or_len;
    834  1.1  jtc 	char *dir_name;
    835  1.1  jtc 	int dir_len;
    836  1.1  jtc #endif
    837  1.1  jtc {
    838  1.1  jtc 	register char *src;
    839  1.1  jtc 	register char *dest;
    840  1.1  jtc 	register char *start;
    841  1.1  jtc 	int len;
    842  1.1  jtc 
    843  1.1  jtc 	/*
    844  1.1  jtc 	 * we shift the or_name to the right enough to tack in the dir_name
    845  1.1  jtc 	 * at the front. We make sure we have enough space for it all before
    846  1.1  jtc 	 * we start. since dest always ends in a slash, we skip of or_name
    847  1.1  jtc 	 * if it also starts with one.
    848  1.1  jtc 	 */
    849  1.1  jtc 	start = or_name;
    850  1.1  jtc 	src = start + *or_len;
    851  1.1  jtc 	dest = src + dir_len;
    852  1.1  jtc 	if (*start == '/') {
    853  1.1  jtc 		++start;
    854  1.1  jtc 		--dest;
    855  1.1  jtc 	}
    856  1.1  jtc 	if ((len = dest - or_name) > PAXPATHLEN) {
    857  1.1  jtc 		warn(1, "File name %s/%s, too long", dir_name, start);
    858  1.1  jtc 		return(-1);
    859  1.1  jtc 	}
    860  1.1  jtc 	*or_len = len;
    861  1.1  jtc 
    862  1.1  jtc 	/*
    863  1.1  jtc 	 * enough space, shift
    864  1.1  jtc 	 */
    865  1.1  jtc 	while (src >= start)
    866  1.1  jtc 		*dest-- = *src--;
    867  1.1  jtc 	src = dir_name + dir_len - 1;
    868  1.1  jtc 
    869  1.1  jtc 	/*
    870  1.1  jtc 	 * splice in the destination directory name
    871  1.1  jtc 	 */
    872  1.1  jtc 	while (src >= dir_name)
    873  1.1  jtc 		*dest-- = *src--;
    874  1.1  jtc 
    875  1.1  jtc 	*(or_name + len) = '\0';
    876  1.1  jtc 	return(0);
    877  1.1  jtc }
    878  1.1  jtc 
    879  1.1  jtc /*
    880  1.1  jtc  * rep_name()
    881  1.1  jtc  *	walk down the list of replacement strings applying each one in order.
    882  1.1  jtc  *	when we find one with a successful substitution, we modify the name
    883  1.1  jtc  *	as specified. if required, we print the results. if the resulting name
    884  1.1  jtc  *	is empty, we will skip this archive member. We use the regexp(3)
    885  1.1  jtc  *	routines (regexp() ought to win a prize as having the most cryptic
    886  1.1  jtc  *	library function manual page).
    887  1.1  jtc  *	--Parameters--
    888  1.1  jtc  *	name is the file name we are going to apply the regular expressions to
    889  1.1  jtc  *	(and may be modified)
    890  1.1  jtc  *	nlen is the length of this name (and is modified to hold the length of
    891  1.1  jtc  *	the final string).
    892  1.1  jtc  *	prnt is a flag that says whether to print the final result.
    893  1.1  jtc  * Return:
    894  1.1  jtc  *	0 if substitution was successful, 1 if we are to skip the file (the name
    895  1.1  jtc  *	ended up empty)
    896  1.1  jtc  */
    897  1.1  jtc 
    898  1.1  jtc #if __STDC__
    899  1.1  jtc static int
    900  1.1  jtc rep_name(char *name, int *nlen, int prnt)
    901  1.1  jtc #else
    902  1.1  jtc static int
    903  1.1  jtc rep_name(name, nlen, prnt)
    904  1.1  jtc 	char *name;
    905  1.1  jtc 	int *nlen;
    906  1.1  jtc 	int prnt;
    907  1.1  jtc #endif
    908  1.1  jtc {
    909  1.1  jtc 	register REPLACE *pt;
    910  1.1  jtc 	register char *inpt;
    911  1.1  jtc 	register char *outpt;
    912  1.1  jtc 	register char *endpt;
    913  1.1  jtc 	register char *rpt;
    914  1.1  jtc 	register int found = 0;
    915  1.1  jtc 	register int res;
    916  1.1  jtc #	ifndef NET2_REGEX
    917  1.1  jtc 	regmatch_t pm[MAXSUBEXP];
    918  1.1  jtc #	endif
    919  1.1  jtc 	char nname[PAXPATHLEN+1];	/* final result of all replacements */
    920  1.1  jtc 	char buf1[PAXPATHLEN+1];	/* where we work on the name */
    921  1.1  jtc 
    922  1.1  jtc 	/*
    923  1.1  jtc 	 * copy the name into buf1, where we will work on it. We need to keep
    924  1.1  jtc 	 * the orig string around so we can print out the result of the final
    925  1.1  jtc 	 * replacement. We build up the final result in nname. inpt points at
    926  1.1  jtc 	 * the string we apply the regular expression to. prnt is used to
    927  1.1  jtc 	 * suppress printing when we handle replacements on the link field
    928  1.1  jtc 	 * (the user already saw that substitution go by)
    929  1.1  jtc 	 */
    930  1.1  jtc 	pt = rephead;
    931  1.1  jtc 	(void)strcpy(buf1, name);
    932  1.1  jtc 	inpt = buf1;
    933  1.1  jtc 	outpt = nname;
    934  1.1  jtc 	endpt = outpt + PAXPATHLEN;
    935  1.1  jtc 
    936  1.1  jtc 	/*
    937  1.1  jtc 	 * try each replacement string in order
    938  1.1  jtc 	 */
    939  1.1  jtc 	while (pt != NULL) {
    940  1.1  jtc 		do {
    941  1.1  jtc 			/*
    942  1.1  jtc 			 * check for a successful substitution, if not go to
    943  1.1  jtc 			 * the next pattern, or cleanup if we were global
    944  1.1  jtc 			 */
    945  1.1  jtc #			ifdef NET2_REGEX
    946  1.1  jtc 			if (regexec(pt->rcmp, inpt) == 0)
    947  1.1  jtc #			else
    948  1.1  jtc 			if (regexec(&(pt->rcmp), inpt, MAXSUBEXP, pm, 0) != 0)
    949  1.1  jtc #			endif
    950  1.1  jtc 				break;
    951  1.1  jtc 
    952  1.1  jtc 			/*
    953  1.1  jtc 			 * ok we found one. We have three parts, the prefix
    954  1.1  jtc 			 * which did not match, the section that did and the
    955  1.1  jtc 			 * tail (that also did not match). Copy the prefix to
    956  1.1  jtc 			 * the final output buffer (watching to make sure we
    957  1.1  jtc 			 * do not create a string too long).
    958  1.1  jtc 			 */
    959  1.1  jtc 			found = 1;
    960  1.1  jtc #			ifdef NET2_REGEX
    961  1.1  jtc 			rpt = pt->rcmp->startp[0];
    962  1.1  jtc #			else
    963  1.1  jtc 			rpt = inpt + pm[0].rm_so;
    964  1.1  jtc #			endif
    965  1.1  jtc 
    966  1.1  jtc 			while ((inpt < rpt) && (outpt < endpt))
    967  1.1  jtc 				*outpt++ = *inpt++;
    968  1.1  jtc 			if (outpt == endpt)
    969  1.1  jtc 				break;
    970  1.1  jtc 
    971  1.1  jtc 			/*
    972  1.1  jtc 			 * for the second part (which matched the regular
    973  1.1  jtc 			 * expression) apply the substitution using the
    974  1.1  jtc 			 * replacement string and place it the prefix in the
    975  1.1  jtc 			 * final output. If we have problems, skip it.
    976  1.1  jtc 			 */
    977  1.1  jtc #			ifdef NET2_REGEX
    978  1.1  jtc 			if ((res = resub(pt->rcmp,pt->nstr,outpt,endpt)) < 0) {
    979  1.1  jtc #			else
    980  1.1  jtc 			if ((res = resub(&(pt->rcmp),pm,pt->nstr,outpt,endpt))
    981  1.1  jtc 			    < 0) {
    982  1.1  jtc #			endif
    983  1.1  jtc 				if (prnt)
    984  1.1  jtc 					warn(1, "Replacement name error %s",
    985  1.1  jtc 					    name);
    986  1.1  jtc 				return(1);
    987  1.1  jtc 			}
    988  1.1  jtc 			outpt += res;
    989  1.1  jtc 
    990  1.1  jtc 			/*
    991  1.1  jtc 			 * we set up to look again starting at the first
    992  1.1  jtc 			 * character in the tail (of the input string right
    993  1.1  jtc 			 * after the last character matched by the regular
    994  1.1  jtc 			 * expression (inpt always points at the first char in
    995  1.1  jtc 			 * the string to process). If we are not doing a global
    996  1.1  jtc 			 * substitution, we will use inpt to copy the tail to
    997  1.1  jtc 			 * the final result. Make sure we do not overrun the
    998  1.1  jtc 			 * output buffer
    999  1.1  jtc 			 */
   1000  1.1  jtc #			ifdef NET2_REGEX
   1001  1.1  jtc 			inpt = pt->rcmp->endp[0];
   1002  1.1  jtc #			else
   1003  1.1  jtc 			inpt += pm[0].rm_eo;
   1004  1.1  jtc #			endif
   1005  1.1  jtc 
   1006  1.1  jtc 			if ((outpt == endpt) || (*inpt == '\0'))
   1007  1.1  jtc 				break;
   1008  1.1  jtc 
   1009  1.1  jtc 			/*
   1010  1.1  jtc 			 * if the user wants global we keep trying to
   1011  1.1  jtc 			 * substitute until it fails, then we are done.
   1012  1.1  jtc 			 */
   1013  1.1  jtc 		} while (pt->flgs & GLOB);
   1014  1.1  jtc 
   1015  1.1  jtc 		if (found)
   1016  1.1  jtc 			break;
   1017  1.1  jtc 
   1018  1.1  jtc 		/*
   1019  1.1  jtc 		 * a successful substitution did NOT occur, try the next one
   1020  1.1  jtc 		 */
   1021  1.1  jtc 		pt = pt->fow;
   1022  1.1  jtc 	}
   1023  1.1  jtc 
   1024  1.1  jtc 	if (found) {
   1025  1.1  jtc 		/*
   1026  1.1  jtc 		 * we had a substitution, copy the last tail piece (if there is
   1027  1.1  jtc 		 * room) to the final result
   1028  1.1  jtc 		 */
   1029  1.1  jtc 		while ((outpt < endpt) && (*inpt != '\0'))
   1030  1.1  jtc 			*outpt++ = *inpt++;
   1031  1.1  jtc 
   1032  1.1  jtc 		*outpt = '\0';
   1033  1.1  jtc 		if ((outpt == endpt) && (*inpt != '\0')) {
   1034  1.1  jtc 			if (prnt)
   1035  1.1  jtc 				warn(1,"Replacement name too long %s >> %s",
   1036  1.1  jtc 				    name, nname);
   1037  1.1  jtc 			return(1);
   1038  1.1  jtc 		}
   1039  1.1  jtc 
   1040  1.1  jtc 		/*
   1041  1.1  jtc 		 * inform the user of the result if wanted
   1042  1.1  jtc 		 */
   1043  1.1  jtc 		if (prnt && (pt->flgs & PRNT)) {
   1044  1.1  jtc 			if (*nname == '\0')
   1045  1.1  jtc 				(void)fprintf(stderr,"%s >> <empty string>\n",
   1046  1.1  jtc 				    name);
   1047  1.1  jtc 			else
   1048  1.1  jtc 				(void)fprintf(stderr,"%s >> %s\n", name, nname);
   1049  1.1  jtc 		}
   1050  1.1  jtc 
   1051  1.1  jtc 		/*
   1052  1.1  jtc 		 * if empty inform the caller this file is to be skipped
   1053  1.1  jtc 		 * otherwise copy the new name over the orig name and return
   1054  1.1  jtc 		 */
   1055  1.1  jtc 		if (*nname == '\0')
   1056  1.1  jtc 			return(1);
   1057  1.1  jtc 		*nlen = l_strncpy(name, nname, PAXPATHLEN + 1);
   1058  1.1  jtc 	}
   1059  1.1  jtc 	return(0);
   1060  1.1  jtc }
   1061  1.1  jtc 
   1062  1.1  jtc #ifdef NET2_REGEX
   1063  1.1  jtc /*
   1064  1.1  jtc  * resub()
   1065  1.1  jtc  *	apply the replacement to the matched expression. expand out the old
   1066  1.1  jtc  * 	style ed(1) subexpression expansion.
   1067  1.1  jtc  * Return:
   1068  1.1  jtc  *	-1 if error, or the number of characters added to the destination.
   1069  1.1  jtc  */
   1070  1.1  jtc 
   1071  1.1  jtc #if __STDC__
   1072  1.1  jtc static int
   1073  1.1  jtc resub(regexp *prog, char *src, char *dest, register char *destend)
   1074  1.1  jtc #else
   1075  1.1  jtc static int
   1076  1.1  jtc resub(prog, src, dest, destend)
   1077  1.1  jtc 	regexp *prog;
   1078  1.1  jtc 	char *src;
   1079  1.1  jtc 	char *dest;
   1080  1.1  jtc 	register char *destend;
   1081  1.1  jtc #endif
   1082  1.1  jtc {
   1083  1.1  jtc 	register char *spt;
   1084  1.1  jtc 	register char *dpt;
   1085  1.1  jtc 	register char c;
   1086  1.1  jtc 	register int no;
   1087  1.1  jtc 	register int len;
   1088  1.1  jtc 
   1089  1.1  jtc 	spt = src;
   1090  1.1  jtc 	dpt = dest;
   1091  1.1  jtc 	while ((dpt < destend) && ((c = *spt++) != '\0')) {
   1092  1.1  jtc 		if (c == '&')
   1093  1.1  jtc 			no = 0;
   1094  1.1  jtc 		else if ((c == '\\') && (*spt >= '0') && (*spt <= '9'))
   1095  1.1  jtc 			no = *spt++ - '0';
   1096  1.1  jtc 		else {
   1097  1.1  jtc  			if ((c == '\\') && ((*spt == '\\') || (*spt == '&')))
   1098  1.1  jtc  				c = *spt++;
   1099  1.1  jtc  			*dpt++ = c;
   1100  1.1  jtc 			continue;
   1101  1.1  jtc 		}
   1102  1.1  jtc  		if ((prog->startp[no] == NULL) || (prog->endp[no] == NULL) ||
   1103  1.1  jtc 		    ((len = prog->endp[no] - prog->startp[no]) <= 0))
   1104  1.1  jtc 			continue;
   1105  1.1  jtc 
   1106  1.1  jtc 		/*
   1107  1.1  jtc 		 * copy the subexpression to the destination.
   1108  1.1  jtc 		 * fail if we run out of space or the match string is damaged
   1109  1.1  jtc 		 */
   1110  1.1  jtc 		if (len > (destend - dpt))
   1111  1.1  jtc 			len = destend - dpt;
   1112  1.1  jtc 		if (l_strncpy(dpt, prog->startp[no], len) != len)
   1113  1.1  jtc 			return(-1);
   1114  1.1  jtc 		dpt += len;
   1115  1.1  jtc 	}
   1116  1.1  jtc 	return(dpt - dest);
   1117  1.1  jtc }
   1118  1.1  jtc 
   1119  1.1  jtc #else
   1120  1.1  jtc 
   1121  1.1  jtc /*
   1122  1.1  jtc  * resub()
   1123  1.1  jtc  *	apply the replacement to the matched expression. expand out the old
   1124  1.1  jtc  * 	style ed(1) subexpression expansion.
   1125  1.1  jtc  * Return:
   1126  1.1  jtc  *	-1 if error, or the number of characters added to the destination.
   1127  1.1  jtc  */
   1128  1.1  jtc 
   1129  1.1  jtc #if __STDC__
   1130  1.1  jtc static int
   1131  1.1  jtc resub(regex_t *rp, register regmatch_t *pm, char *src, char *dest,
   1132  1.1  jtc 	register char *destend)
   1133  1.1  jtc #else
   1134  1.1  jtc static int
   1135  1.1  jtc resub(rp, pm, src, dest, destend)
   1136  1.1  jtc 	regex_t *rp;
   1137  1.1  jtc 	register regmatch_t *pm;
   1138  1.1  jtc 	char *src;
   1139  1.1  jtc 	char *dest;
   1140  1.1  jtc 	register char *destend;
   1141  1.1  jtc #endif
   1142  1.1  jtc {
   1143  1.1  jtc 	register char *spt;
   1144  1.1  jtc 	register char *dpt;
   1145  1.1  jtc 	register char c;
   1146  1.1  jtc 	register regmatch_t *pmpt;
   1147  1.1  jtc 	register int len;
   1148  1.1  jtc 	int subexcnt;
   1149  1.1  jtc 
   1150  1.1  jtc 	spt =  src;
   1151  1.1  jtc 	dpt = dest;
   1152  1.1  jtc 	subexcnt = rp->re_nsub;
   1153  1.1  jtc 	while ((dpt < destend) && ((c = *spt++) != '\0')) {
   1154  1.1  jtc 		/*
   1155  1.1  jtc 		 * see if we just have an ordinary replacement character
   1156  1.1  jtc 		 * or we refer to a subexpression.
   1157  1.1  jtc 		 */
   1158  1.1  jtc 		if (c == '&') {
   1159  1.1  jtc 			pmpt = pm;
   1160  1.1  jtc 		} else if ((c == '\\') && (*spt >= '0') && (*spt <= '9')) {
   1161  1.1  jtc 			/*
   1162  1.1  jtc 			 * make sure there is a subexpression as specified
   1163  1.1  jtc 			 */
   1164  1.1  jtc 			if ((len = *spt++ - '0') > subexcnt)
   1165  1.1  jtc 				return(-1);
   1166  1.1  jtc 			pmpt = pm + len;
   1167  1.1  jtc 		} else {
   1168  1.1  jtc  			/*
   1169  1.1  jtc 			 * Ordinary character, just copy it
   1170  1.1  jtc 			 */
   1171  1.1  jtc  			if ((c == '\\') && ((*spt == '\\') || (*spt == '&')))
   1172  1.1  jtc  				c = *spt++;
   1173  1.1  jtc  			*dpt++ = c;
   1174  1.1  jtc 			continue;
   1175  1.1  jtc 		}
   1176  1.1  jtc 
   1177  1.1  jtc 		/*
   1178  1.1  jtc 		 * continue if the subexpression is bogus
   1179  1.1  jtc 		 */
   1180  1.1  jtc 		if ((pmpt->rm_so < 0) || (pmpt->rm_eo < 0) ||
   1181  1.1  jtc 		    ((len = pmpt->rm_eo - pmpt->rm_so) <= 0))
   1182  1.1  jtc 			continue;
   1183  1.1  jtc 
   1184  1.1  jtc 		/*
   1185  1.1  jtc 		 * copy the subexpression to the destination.
   1186  1.1  jtc 		 * fail if we run out of space or the match string is damaged
   1187  1.1  jtc 		 */
   1188  1.1  jtc 		if (len > (destend - dpt))
   1189  1.1  jtc 			len = destend - dpt;
   1190  1.1  jtc 		if (l_strncpy(dpt, src + pmpt->rm_so, len) != len)
   1191  1.1  jtc 			return(-1);
   1192  1.1  jtc 		dpt += len;
   1193  1.1  jtc 	}
   1194  1.1  jtc 	return(dpt - dest);
   1195  1.1  jtc }
   1196  1.1  jtc #endif
   1197