[BACK]Return to fep_util.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / fep

Annotation of OpenXM_contrib2/fep/fep_util.c, Revision 1.4

1.1       noro        1: /*     Copyright (c) 1987, 1988 by Software Research Associates, Inc.  */
                      2:
1.4     ! fujimoto    3: #if defined(ANDROID)
        !             4: #include <strings.h>
        !             5: #define index(s,c) strchr(s,c)
        !             6: #define rindex(s,c) strrchr(s,c)
        !             7: #endif
        !             8:
1.1       noro        9: #ifndef lint
                     10: static char rcsid[]=
                     11: "$Header: fep_util.c,v 4.1 88/08/28 18:57:41 utashiro Exp $ (SRA)";
                     12: #endif /* lint */
                     13:
                     14: #ifndef MKARGDEBUG
                     15:
                     16: #include <stdio.h>
1.3       ohara      17: #include <string.h>
1.1       noro       18: #include <pwd.h>
                     19: #include <sys/types.h>
1.4     ! fujimoto   20: #if defined(__CYGWIN__) || defined(sun) || defined(ANDROID)
1.1       noro       21: #include <dirent.h>
                     22: #else
                     23: #include <sys/dir.h>
                     24: #endif
                     25: #include <ctype.h>
                     26: #include "fep_defs.h"
                     27:
                     28: message(messageString)
                     29:     char *messageString;
                     30: {
                     31:     write (2, messageString, strlen (messageString));
                     32: }
                     33:
                     34: errorBell()
                     35: {
                     36:     write (2, "\007", 1);
                     37: }
                     38:
                     39: ctlprint(string)
                     40:     char *string;
                     41: {
                     42:     register char  *cp;
                     43:
                     44:     for (cp = string; *cp; cp++) {
                     45:        if (isctlchar (*cp)) {
                     46:            putchar ('^');
                     47:            putchar (unctl (*cp));
                     48:        }
                     49:        else
                     50:            putchar (*cp);
                     51:     }
                     52:     fputs ("\r\n", stdout);
                     53:     fflush (stdout);
                     54: }
                     55:
                     56: /*
                     57:  * Print string using "^" for control characters
                     58:  */
                     59: printS (string)
                     60:     char *string;
                     61: {
                     62:     char *cp;
                     63:
                     64:     for (cp = string; *cp; cp++)
                     65:        putChar (*cp);
                     66: }
                     67:
                     68: /*
                     69:  * Check the line is empty or not
                     70:  */
                     71: is_empty_line(line)
                     72:     char *line;
                     73: {
                     74:     register char *cp;
                     75:
                     76:     for (cp = line; *cp; cp++) {
                     77:         if (!isspace(*cp)) {
                     78:            return(0);
                     79:        }
                     80:     }
                     81:     return(1);
                     82: }
                     83:
                     84: /*
                     85:  * Put character using "^" for control characters
                     86:  */
                     87: putChar(c)
                     88:     char c;
                     89: {
                     90:     if (isctlchar(c)) {
                     91:        (void) putchar('^');
                     92:        (void) putchar(unctl(c));
                     93:     }
                     94:     else
                     95:        (void) putchar(c);
                     96: }
                     97:
                     98: char *
                     99: x_dirname (dir)
                    100:     char *dir;
                    101: {
                    102:     static char dirname [256];
1.4     ! fujimoto  103: #if !defined(ANDROID)
1.1       noro      104:     char *index();
1.4     ! fujimoto  105: #endif
1.1       noro      106:
                    107:     if (*dir != '~')
                    108:        strcpy (dirname, dir);
                    109:     else {
                    110:        struct passwd *pw;
                    111:
                    112:        if (*(dir+1) == '/' || *(dir+1) == '\0') {
                    113:            pw = getpwuid (getuid ());
                    114:        }
                    115:        else {
                    116:            char user [64], *sp, *dp;
                    117:
                    118:            for (sp = dir+1, dp = user; *sp && *sp != '/'; sp++, dp++)
                    119:                *dp = *sp;
                    120:            *dp = '\0';
                    121:            pw = getpwnam (user);
                    122:        }
                    123:
                    124:        if (pw) {
                    125:            strcpy (dirname, pw->pw_dir);
                    126:            if (any ('/', dir))
                    127:                strcat (dirname, index (dir, '/'));
                    128:        }
                    129:        else {
                    130:            strcpy (dirname, dir);
                    131:        }
                    132:     }
                    133:
                    134:     return (dirname);
                    135: }
                    136:
                    137: DIR *
                    138: x_opendir (dir)
                    139:     char *dir;
                    140: {
                    141:     return (opendir (x_dirname (dir)));
                    142: }
                    143:
                    144: /*
                    145:  * Strring compare for qsort
                    146:  */
                    147: scmp (a, b)
                    148:     char **a, **b;
                    149: {
                    150:
                    151:     return (strcmp (*a, *b));
                    152: }
                    153:
                    154: /*
                    155:  * Return 1 if "str" is prefixed by "sub"
                    156:  */
                    157: prefix (sub, str)
                    158:     register char *sub, *str;
                    159: {
                    160:
                    161:     for (;;) {
                    162:        if (*sub == 0)
                    163:            return (1);
                    164:        if (*str == 0)
                    165:            return (0);
                    166:        if (*sub++ != *str++)
                    167:            return (0);
                    168:     }
                    169: }
                    170:
                    171: /*
                    172:  * Return 1 if s includes character c
                    173:  */
                    174: any (c, s)
                    175:     register int c;
                    176:     register char *s;
                    177: {
                    178:
                    179:     while (*s)
                    180:        if (*s++ == c)
                    181:            return(1);
                    182:     return(0);
                    183: }
                    184:
                    185: #ifndef max
                    186: /*
                    187:  * Return maximum number of d1 and d2
                    188:  */
                    189: max (d1, d2)
                    190:     int d1, d2;
                    191: {
                    192:     return (d1 > d2 ? d1 : d2);
                    193: }
                    194: #endif /* max */
                    195:
                    196: #else /* MKARGDEBUG */
                    197:
                    198: #include <stdio.h>
                    199: #include <ctype.h>
                    200:
                    201: #define MAXARGS        64
                    202:
                    203: main()
                    204: {
                    205:     char s[128];
                    206:     char *argv[MAXARGS];
                    207:
                    208:     while (gets (s)) {
                    209:        register int c;
                    210:
                    211:        showArgs (s);
                    212:     }
                    213: }
                    214: #endif /* MKARGDEBUG */
                    215:
                    216: showArgs (comline)
                    217:     char *comline;
                    218: {
                    219:     char *argv[MAXARGS];
                    220:     register int c;
                    221:     register char **argp;
                    222:     register int i;
                    223:
                    224:     c = mkargv (comline, argv, MAXARGS);
                    225:     if (c < 0) {
                    226:        printf ("%s\n", argv[0]);
                    227:        return;
                    228:     }
                    229:     printf ("argc = %d\n", c);
                    230:     for (i = 0, argp = argv; *argp; argp++, i++) {
                    231:        printf ("\"%s\" ", *argp);
                    232:     }
                    233:     printf ("\n");
                    234: }
                    235:
                    236: mkargv (s, argv, maxarg)
                    237:     char *s;
                    238:     char *argv[];
                    239:     int maxarg;
                    240: {
                    241:     register char *cp;
                    242:     register int argc = 0;
                    243:     int insquot = 0, indquot = 0, ignorenext = 0;
                    244:     enum {STRING, SPACE} status = SPACE;
                    245:     static char buf[1024], *bp;
                    246:
                    247:     for (cp = s, bp = buf; *cp; cp++) {
                    248:
                    249:        if (argc > maxarg)
                    250:            return (argc);
                    251:
                    252:        /*
                    253:         * Found white space
                    254:         */
                    255:        if (isspace (*cp)) {
                    256:            /*
                    257:             * In outside of quotation
                    258:             */
                    259:            if (!ignorenext && !insquot && !indquot) {
                    260:                /*
                    261:                 * If status was in string, go next arg
                    262:                 */
                    263:                if (status == STRING) {
                    264:                    status = SPACE;
                    265:                    *bp++ = '\0';
                    266:                    continue;
                    267:                }
                    268:                else
                    269:                    continue;
                    270:            }
                    271:        }
                    272:
                    273: #      define SPECIALCHARS "\"\'\\"
                    274:        if (!ignorenext && index (SPECIALCHARS, *cp)) {
                    275:            switch (*cp) {
                    276:
                    277:                /*
                    278:                 * Literal next character
                    279:                 */
                    280:                case '\\':
                    281:                    if (indquot || insquot)
                    282:                        goto THROUGH;
                    283:                    else {
                    284:                        ignorenext = 1;
                    285:                        continue;
                    286:                    }
                    287:
                    288:                /*
                    289:                 * Double quotation
                    290:                 */
                    291:                case '\"':
                    292:                    if (insquot)
                    293:                        goto THROUGH;
                    294:                    if (indquot && *(bp-1) == '\\') {
                    295:                        bp--;
                    296:                        goto THROUGH;
                    297:                    }
                    298:                    indquot = !indquot;
                    299:                    break;
                    300:
                    301:                /*
                    302:                 * Single quotation
                    303:                 */
                    304:                case '\'':
                    305:                    if (indquot)
                    306:                        goto THROUGH;
                    307:                    if (insquot && *(bp-1) == '\\') {
                    308:                        bp--;
                    309:                        goto THROUGH;
                    310:                    }
                    311:                    insquot = !insquot;
                    312:                    break;
                    313:            }
                    314:
                    315:            /*
                    316:             * Only in " or ' case.
                    317:             */
                    318:            if (status == SPACE) {
                    319:                status = STRING;
                    320:                argc++;
                    321:                argv[argc-1] = bp;
                    322:                *bp = 0;
                    323:            }
                    324:            continue;
                    325:        }
                    326:
                    327: THROUGH:
                    328:        /*
                    329:         * Found non-space character
                    330:         */
                    331:        ignorenext = 0;
                    332:        if (status == SPACE) {
                    333:            status = STRING;
                    334:            argc++;
                    335:            argv[argc-1] = bp;
                    336:        }
                    337:        *bp++ = *cp;
                    338:     }
                    339:
                    340:     if (indquot || insquot) {
                    341:        argv[0] = indquot ? "Unmatched \"." : "Unmatched \'.";
                    342:        return (-1);
                    343:     }
                    344:
                    345:     *bp = '\0';
                    346:     argv[argc] = (char *)0;
                    347:     return (argc);
                    348: }
                    349:
                    350: reverse_strcpy (to, from)
                    351:     register char *to, *from;
                    352: {
                    353:     register int len;
                    354:
                    355:     for (len = strlen (from); len >= 0; len--)
                    356:        *(to + len) = *(from + len);
                    357: }
                    358:
                    359: char *search_string (s, lookup)
                    360:     char *s, *lookup;
                    361: {
                    362:     int len = strlen (lookup);
                    363:     enum {SQUOTE, DQUOTE, NORMAL} status = NORMAL;
                    364:
                    365:     while (*s) {
                    366:        switch (*s) {
                    367:        case '\'':
                    368:            if (status == DQUOTE) break;
                    369:            status = (status == SQUOTE) ? NORMAL : SQUOTE;
                    370:            break;
                    371:
                    372:        case '\"':
                    373:            if (status == SQUOTE) break;
                    374:            status = (status == DQUOTE) ? NORMAL : DQUOTE;
                    375:            break;
                    376:
                    377:        case '\\':
                    378:            ++s;
                    379:            break;
                    380:        }
                    381:        if (!*s) break;
                    382:        if (status == NORMAL && strncmp (s, lookup, len) == 0)
                    383:            return s;
                    384:        ++s;
                    385:     }
                    386:     return ((char*) 0);
                    387: }
                    388:
                    389: #ifdef KANJI
                    390: /*
                    391:  * Uuuuuuuum. I hate search kanji in Shift-JIS code from the end of string
                    392:  * This function check if i'th character is first byte of KANJI code
                    393:  * in string starting from s.
                    394:  * It is assumed that first byte of strint s can't be second byte of KANJI
                    395:  * code.
                    396:  */
                    397: iskanji_in_string (s, i)
                    398:     char *s;
                    399:     int i;
                    400: {
                    401:     register char *cp = s, *target = s + i;
                    402:
                    403:     if (i < 0)
                    404:        return (0);
                    405:
                    406:     while (cp < target) {
                    407:        if (iskanji (*cp))
                    408:            cp += 2;
                    409:        else
                    410:            cp++;
                    411:     }
                    412:
                    413:     if (cp != target)
                    414:        return (0);
                    415:     else
                    416:        return (iskanji (*cp));
                    417: }
                    418: #endif /* KANJI */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>