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