+ if (x < 0) {
+ return(-1);
+ }
+ if (x > ('z' - ARRAYBASE)) {
+ debug(F101,"xarray x out of range","",x);
+ return(-1);
+ }
+ if (*(s+3)) {
+ debug(F110,"xarray syntax",s,0);
+ return(-1);
+ }
+ return(x);
+}
+
+/*
+ boundspair() -- parses blah[n:m]
+
+ For use with array segment specifiers and compact substring notation.
+ Ignores the "blah" part, gets the values of n and m, which can be
+ numbers, variables, or arithmetic expressions; anything that resolves
+ to a number.
+
+ Call with:
+ s - string to parse
+ sep - array of permissible bounds separator chars
+ lo - pointer to low-bound result (or -1)
+ hi - pointer to hi-bound result (or -1)
+ zz - pointer to separator char that was encountered (or NUL)
+ Returns:
+ -1 on failure
+ 0 on success
+*/
+
+int
+#ifdef CK_ANSIC
+boundspair(char *s, char *sep, int *lo, int *hi, char *zz)
+#else
+boundspair(s,sep,lo,hi,zz) char *s, *sep, *zz; int *lo, *hi;
+#endif /* CK_ANSIC */
+{
+ int i, x, y, range[2], bc = 0;
+ char c = NUL, *s2 = NULL, buf[256], *p, *q, *r, *e[2], *tmp = NULL;
+
+ debug(F110,"boundspair s",s,0);
+ debug(F110,"boundspair sep",sep,0);
+
+ *lo = -1; /* Default bounds */
+ *hi = -1;
+ *zz = 0; /* Default bounds separator */
+
+ range[0] = -1; /* It's OK -- get contents */
+ range[1] = -1; /* of subscript brackets */
+ if (!s) s = "";
+ if (!*s)
+ return(-1);
+ makestr(&tmp,s); /* Make a pokeable copy */
+ p = tmp;
+ q = NULL;
+ r = NULL;
+ for (p = s; *p; p++) { /* Get the two elements */
+ if (*p == '[') {
+ bc++; /* Bracket counter */
+ if (bc == 1 && !q) q = p+1;
+ } else if (*p == ']') {
+ bc--;
+ if (bc == 0 && q) *p = NUL;
+ } else if (bc == 1) { /* If within brackers */
+ s2 = ckstrchr(sep,*p); /* Check for separator */
+ if (s2) {
+ debug(F000,"boundspair *s2","",*s2);
+ if (c) {
+ debug(F000,"boundspair","Too many separators",*s2);
+ makestr(&tmp,NULL);
+ return(-1);
+ }
+ c = *s2; /* Separator character */
+ *p = NUL;
+ r = p+1;
+ }
+ }
+ }
+ if (bc == 0 && !q) {
+ /* This allows such constructions as "show array a" */
+ debug(F110,"boundspair","no brackets",0);
+ makestr(&tmp,NULL);
+ return(0);
+ }
+ if (bc != 0 || !q) {
+ debug(F110,"boundspair","unbalanced or missing brackets",0);
+ makestr(&tmp,NULL);
+ return(-1);
+ }
+ if (!q) q = "";
+ if (!*q) q = "-1";
+ if (!r) r = "";
+ if (!*r) r = "-1";
+
+ e[0] = q;
+ e[1] = r;
+
+ debug(F000,"boundspair c","",c);
+ debug(F110,"boundspair q",q,0);
+ debug(F110,"boundspair r",r,0);
+
+ for (i = 0; i < 2 && e[i]; i++) {
+ y = 255; /* Expand variables, etc. */
+ s = buf;
+ zzstring(e[i],&s,&y);
+ s = evalx(buf); /* Evaluate it arithmetically */
+ if (s) if (*s)
+ ckstrncpy(buf,s,256);
+ if (!chknum(buf)) { /* Did we get a number? */
+ debug(F110,"boundspair element not numeric",buf,0);
+ makestr(&tmp,NULL); /* No, fail. */
+ return(-1);
+ }
+ range[i] = atoi(buf);
+ }
+ makestr(&tmp,NULL); /* Free temporary poked string */
+ *lo = range[0]; /* Return what we got */
+ *hi = range[1];
+ *zz = c;
+ debug(F101,"boundspair lo","",*lo);
+ debug(F101,"boundspair hi","",*hi);
+ return(0);