--- ray/src/common/words.c 1991/07/30 13:47:29 1.4 +++ ray/src/common/words.c 2021/01/01 02:10:32 2.13 @@ -1,44 +1,58 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: words.c,v 2.13 2021/01/01 02:10:32 greg Exp $"; #endif - /* * Routines for recognizing and moving about words in strings. + * + * External symbols declared in standard.h */ +#include "copyright.h" + #include -#ifdef BSD -#define strchr index -#endif +#include "rtio.h" -#define NULL 0 +char * +atos(char *rs, int nb, char *s) /* get word from string, returning rs */ +{ + char *cp = rs; -extern char *strchr(); + while (isspace(*s)) + s++; + while (--nb > 0 && *s && !isspace(*s)) + *cp++ = *s++; + *cp = '\0'; + return(rs); +} char * -atos(rs, nb, s) /* get next word from string */ -char *rs; -register int nb; -register char *s; +nextword(char *cp, int nb, char *s) /* get (quoted) word, returning new s */ { - register char *cp = rs; + int quote = 0; + if (s == NULL) return(NULL); while (isspace(*s)) s++; - while (--nb > 0 && *s && !isspace(*s)) + switch (*s) { + case '\0': + return(NULL); + case '"': + case '\'': + quote = *s++; + } + while (--nb > 0 && *s && (quote ? *s!=quote : !isspace(*s))) *cp++ = *s++; *cp = '\0'; - return(rs); + if (quote && *s==quote) + s++; + return(s); } char * -sskip(s) /* skip word in string */ -register char *s; +sskip(char *s) /* skip word in string, leaving on space */ { while (isspace(*s)) s++; @@ -49,13 +63,26 @@ register char *s; char * -iskip(s) /* skip integer in string */ -register char *s; +sskip2(char *s, int n) /* skip word(s) in string, leaving on word */ { while (isspace(*s)) s++; - if (*s == '-' || *s == '+') + while (n-- > 0) { + while (*s && !isspace(*s)) + s++; + while (isspace(*s)) + s++; + } + return(s); +} + + +char * +iskip(char *s) /* skip integer in string */ +{ + while (isspace(*s)) s++; + s += (*s == '-') | (*s == '+'); if (!isdigit(*s)) return(NULL); do @@ -66,15 +93,13 @@ register char *s; char * -fskip(s) /* skip float in string */ -register char *s; +fskip(char *s) /* skip float in string */ { - register char *cp; + char *cp; while (isspace(*s)) s++; - if (*s == '-' || *s == '+') - s++; + s += (*s == '-') | (*s == '+'); cp = s; while (isdigit(*cp)) cp++; @@ -85,47 +110,60 @@ register char *s; } if (cp == s) return(NULL); - if (*cp == 'e' || *cp == 'E') - return(iskip(cp+1)); + if ((*cp == 'e') | (*cp == 'E')) + return(isspace(*++cp) ? NULL : iskip(cp)); return(cp); } -isint(s) /* check integer format */ -char *s; +int +isint(char *s) /* check integer format */ { - register char *cp; + char *cp; cp = iskip(s); return(cp != NULL && *cp == '\0'); } -isintd(s, ds) /* check integer format with delimiter set */ -char *s, *ds; +int +isintd(char *s, char *ds) /* check integer format with delimiter set */ { - register char *cp; + char *cp; cp = iskip(s); return(cp != NULL && strchr(ds, *cp) != NULL); } -isflt(s) /* check float format */ -char *s; +int +isflt(char *s) /* check float format */ { - register char *cp; + char *cp; cp = fskip(s); return(cp != NULL && *cp == '\0'); } -isfltd(s, ds) /* check integer format with delimiter set */ -char *s, *ds; +int +isfltd(char *s, char *ds) /* check integer format with delimiter set */ { - register char *cp; + char *cp; cp = fskip(s); return(cp != NULL && strchr(ds, *cp) != NULL); +} + + +int +isname(char *s) /* check for legal identifier name */ +{ + while (*s == '_') /* skip leading underscores */ + s++; + if (!isascii(*s) | !isalpha(*s)) /* start with a letter */ + return(0); + while (isascii(*++s) && isgraph(*s)) /* all visible characters */ + ; + return(*s == '\0'); /* ending in nul */ }