Annotation of coherent/e/bin/pax/replace.c, revision 1.1

1.1     ! root        1: /* $Source: /src386/usr/bin/pax/replace.c,v $
        !             2:  *
        !             3:  * $Revision: 1.1 $
        !             4:  *
        !             5:  * replace.c - regular expression pattern replacement functions
        !             6:  *
        !             7:  * DESCRIPTION
        !             8:  *
        !             9:  *     These routines provide for regular expression file name replacement
        !            10:  *     as required by pax.
        !            11:  *
        !            12:  * AUTHORS
        !            13:  *
        !            14:  *     Mark H. Colburn, NAPS International
        !            15:  *
        !            16:  * Sponsored by The USENIX Association for public distribution. 
        !            17:  *
        !            18:  * Copyright (c) 1989 Mark H. Colburn.
        !            19:  * All rights reserved.
        !            20:  *
        !            21:  * Redistribution and use in source and binary forms are permitted
        !            22:  * provided that the above copyright notice is duplicated in all such 
        !            23:  * forms and that any documentation, advertising materials, and other 
        !            24:  * materials related to such distribution and use acknowledge that the 
        !            25:  * software was developed * by Mark H. Colburn and sponsored by The 
        !            26:  * USENIX Association. 
        !            27:  *
        !            28:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            29:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            30:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            31:  *
        !            32:  * $Log:       replace.c,v $
        !            33:  * Revision 1.1  92/08/28  08:03:04  bin
        !            34:  * Initial revision
        !            35:  * 
        !            36:  * Revision 1.1  89/02/14  16:48:45  jep
        !            37:  * Initial revision
        !            38:  * 
        !            39:  * Revision 1.1  88/12/23  18:02:36  mark
        !            40:  * Initial revision
        !            41:  * 
        !            42:  */
        !            43: 
        !            44: #ifndef lint
        !            45: static char *ident = "$Id: replace.c,v 1.1 92/08/28 08:03:04 bin Exp Locker: bin $";
        !            46: static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
        !            47: #endif /* not lint */
        !            48: 
        !            49: /* Headers */
        !            50: 
        !            51: #include "pax.h"
        !            52: 
        !            53: 
        !            54: /* add_replstr - add a replacement string to the replacement string list
        !            55:  *
        !            56:  * DESCRIPTION
        !            57:  *
        !            58:  *     Add_replstr adds a replacement string to the replacement string
        !            59:  *     list which is applied each time a file is about to be processed.
        !            60:  *
        !            61:  * PARAMETERS
        !            62:  *
        !            63:  *     char    *pattern        - A regular expression which is to be parsed
        !            64:  */
        !            65: 
        !            66: #if __STDC__
        !            67: 
        !            68: void add_replstr(char *pattern)
        !            69: 
        !            70: #else
        !            71: 
        !            72: void add_replstr(pattern)
        !            73: char           *pattern;
        !            74: 
        !            75: #endif
        !            76: {
        !            77:     char           *p;
        !            78:     char            sep;
        !            79:     Replstr        *rptr;
        !            80:     int             len;
        !            81: 
        !            82:     if ((len = strlen(pattern)) < 4) {
        !            83:        warn("Replacement string not added",
        !            84:                 "Malformed substitution syntax");
        !            85:        return;
        !            86:     }
        !            87:     if ((rptr = (Replstr *) malloc(sizeof(Replstr))) == (Replstr *)NULL) {
        !            88:        warn("Replacement string not added", "No space");
        !            89:        return;
        !            90:     }
        !            91: 
        !            92:     /* First character is the delimeter... */
        !            93:     sep = *pattern;
        !            94: 
        !            95:     /* Get trailing g and/or p */
        !            96:     p = pattern + len - 1;
        !            97:     while (*p != sep) {
        !            98:        if (*p == 'g') {
        !            99:             rptr->global = 1;
        !           100:        } else if (*p == 'p') {
        !           101:            rptr->print = 1;
        !           102:        } else {
        !           103:            warn(p, "Invalid RE modifier");
        !           104:        }
        !           105:        p--;
        !           106:     }
        !           107: 
        !           108:     if (*p != sep) {
        !           109:        warn("Replacement string not added", "Bad delimeters");
        !           110:        free(rptr);
        !           111:        return;
        !           112:     }
        !           113:     /* strip off leading and trailing delimeter */
        !           114:     *p = '\0';
        !           115:     pattern++;
        !           116: 
        !           117:     /* find the separating '/' in the pattern */
        !           118:     p = pattern;
        !           119:     while (*p) {
        !           120:        if (*p == sep) {
        !           121:            break;
        !           122:        }
        !           123:        if (*p == '\\' && *(p + 1) != '\0') {
        !           124:            p++;
        !           125:        }
        !           126:        p++;
        !           127:     }
        !           128:     if (*p != sep) {
        !           129:        warn("Replacement string not added", "Bad delimeters");
        !           130:        free(rptr);
        !           131:        return;
        !           132:     }
        !           133:     *p++ = '\0';
        !           134: 
        !           135:     /*
        !           136:      * Now pattern points to 'old' and p points to 'new' and both are '\0'
        !           137:      * terminated 
        !           138:      */
        !           139:     if ((rptr->comp = regcomp(pattern)) == (regexp *)NULL) {
        !           140:        warn("Replacement string not added", "Invalid RE");
        !           141:        free(rptr);
        !           142:        return;
        !           143:     }
        !           144:     rptr->replace = p;
        !           145:     rptr->next = (Replstr *)NULL;
        !           146:     if (rplhead == (Replstr *)NULL) {
        !           147:        rplhead = rptr;
        !           148:        rpltail = rptr;
        !           149:     } else {
        !           150:        rpltail->next = rptr;
        !           151:        rpltail = rptr;
        !           152:     }
        !           153: }
        !           154: 
        !           155: 
        !           156: 
        !           157: /* rpl_name - possibly replace a name with a regular expression
        !           158:  *
        !           159:  * DESCRIPTION
        !           160:  *
        !           161:  *     The string name is searched for in the list of regular expression
        !           162:  *     substituions.  If the string matches any of the regular expressions
        !           163:  *     then the string is modified as specified by the user.
        !           164:  *
        !           165:  * PARAMETERS
        !           166:  *
        !           167:  *     char    *name   - name to search for and possibly modify
        !           168:  */
        !           169: 
        !           170: #if __STDC__
        !           171: 
        !           172: void rpl_name(char *name)
        !           173: 
        !           174: #else
        !           175: 
        !           176: void rpl_name(name)
        !           177: char           *name;
        !           178: 
        !           179: #endif
        !           180: {
        !           181:     int             found = 0;
        !           182:     int             ret;
        !           183:     Replstr        *rptr;
        !           184:     char            buff[PATH_MAX + 1];
        !           185:     char            buff1[PATH_MAX + 1];
        !           186:     char            buff2[PATH_MAX + 1];
        !           187:     char           *p;
        !           188:     char           *b;
        !           189: 
        !           190:     strcpy(buff, name);
        !           191:     for (rptr = rplhead; !found && rptr != (Replstr *)NULL; rptr = rptr->next) {
        !           192:        do {
        !           193:            if ((ret = regexec(rptr->comp, buff)) != 0) {
        !           194:                p = buff;
        !           195:                b = buff1;
        !           196:                while (p < rptr->comp->startp[0]) {
        !           197:                    *b++ = *p++;
        !           198:                }
        !           199:                p = rptr->replace;
        !           200:                while (*p) {
        !           201:                    *b++ = *p++;
        !           202:                }
        !           203:                strcpy(b, rptr->comp->endp[0]);
        !           204:                found = 1;
        !           205:                regsub(rptr->comp, buff1, buff2);
        !           206:                strcpy(buff, buff2);
        !           207:            }
        !           208:        } while (ret && rptr->global);
        !           209:        if (found) {
        !           210:            if (rptr->print) {
        !           211:                fprintf(stderr, "%s >> %s\n", name, buff);
        !           212:            }
        !           213:            strcpy(name, buff);
        !           214:        }
        !           215:     }
        !           216: }
        !           217: 
        !           218: 
        !           219: /* get_disposition - get a file disposition
        !           220:  *
        !           221:  * DESCRIPTION
        !           222:  *
        !           223:  *     Get a file disposition from the user.  If the user enters 'y'
        !           224:  *     the the file is processed, anything else and the file is ignored.
        !           225:  *     If the user enters EOF, then the PAX exits with a non-zero return
        !           226:  *     status.
        !           227:  *
        !           228:  * PARAMETERS
        !           229:  *
        !           230:  *     char    *mode   - string signifying the action to be taken on file
        !           231:  *     char    *name   - the name of the file
        !           232:  *
        !           233:  * RETURNS
        !           234:  *
        !           235:  *     Returns 1 if the file should be processed, 0 if it should not.
        !           236:  */
        !           237: 
        !           238: #if __STDC__
        !           239: 
        !           240: int get_disposition(char *mode, char *name)
        !           241: 
        !           242: #else
        !           243: 
        !           244: int get_disposition(mode, name)
        !           245: char   *mode;
        !           246: char   *name;
        !           247: 
        !           248: #endif
        !           249: {
        !           250:     char       ans[2];
        !           251:     char       buf[PATH_MAX + 10];
        !           252: 
        !           253:     if (f_disposition) {
        !           254:        sprintf(buf, "%s %s? ", mode, name);
        !           255:        if (nextask(buf, ans, sizeof(ans)) == -1 || ans[0] == 'q') {
        !           256:            exit(0);
        !           257:        }
        !           258:        if (strlen(ans) == 0 || ans[0] != 'y') {
        !           259:            return(1);
        !           260:        } 
        !           261:     } 
        !           262:     return(0);
        !           263: }
        !           264: 
        !           265: 
        !           266: /* get_newname - prompt the user for a new filename
        !           267:  *
        !           268:  * DESCRIPTION
        !           269:  *
        !           270:  *     The user is prompted with the name of the file which is currently
        !           271:  *     being processed.  The user may choose to rename the file by
        !           272:  *     entering the new file name after the prompt; the user may press
        !           273:  *     carriage-return/newline, which will skip the file or the user may
        !           274:  *     type an 'EOF' character, which will cause the program to stop.
        !           275:  *
        !           276:  * PARAMETERS
        !           277:  *
        !           278:  *     char    *name           - filename, possibly modified by user
        !           279:  *     int     size            - size of allowable new filename
        !           280:  *
        !           281:  * RETURNS
        !           282:  *
        !           283:  *     Returns 0 if successfull, or -1 if an error occurred.
        !           284:  *
        !           285:  */
        !           286: 
        !           287: #if __STDC__
        !           288: 
        !           289: int get_newname(char *name, int size)
        !           290: 
        !           291: #else
        !           292: 
        !           293: int get_newname(name, size)
        !           294: char   *name;
        !           295: int    size;
        !           296: 
        !           297: #endif
        !           298: {
        !           299:     char       buf[PATH_MAX + 10];
        !           300: 
        !           301:     if (f_interactive) {
        !           302:        sprintf(buf, "rename %s? ", name);
        !           303:        if (nextask(buf, name, size) == -1) {
        !           304:            exit(0);
        !           305:        }
        !           306:        if (strlen(name) == 0) {
        !           307:            return(1);
        !           308:        }
        !           309:     }
        !           310:     return(0);
        !           311: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.