float triatof( const triChar* str ) { if (*str=='\\') return (float)(str[1]); if (*str=='$') { triChar buf[64]; str++; triS32 i = 0, max = varlen(str); if (max>63) max = 63; for (;i<max;i++) buf[i] = str[i]; buf[i] = 0; //printf("%s %.2f\n", buf, triCVarGetf( buf )); return triCVarGetf( buf ); } if (*str=='0' && (str[1]=='x' || str[1]=='X')) { triUInt i; sscanf( &str[2], "%x", &i ); return (float)i; } if (*str>='0' && *str<='9') { float f; sscanf( str, "%f", &f ); return f; } return (TRI_INVALID_VAL); }
void s_rnge(char *varn, int32 offset, char *procn, int32 line) { register int32 i; int32 len; len=varlen(procn); fprintf(stderr, "Subscript out of range on file line %d, procedure ", line); /*for(i = 0 ; i < 2 && (*procn!='_' && *(procn+1) != '\0'); ++i)*/ for(i = 0 ; i < len ; ++i) putc(*procn++, stderr); fprintf(stderr, ".\nAttempt to access the %d-th element of variable ", offset+1); #ifdef sgi for(i = 0; i < 32 && *varn!=' '; ++i) putc(*varn++, stderr); #else for(i = 0 ; i < 6 && *varn!=' ' ; ++i) putc(*varn++, stderr); #endif fprintf(stderr, ".\n"); s_abort(99); }
// Evaluate a term by converting to PRN first, then evaluating the variables and operators // This allows for arithmetic on variable/command arguments, e.g.: // cl_fpsmax "$cl_fpsmin + 10.0" // echo "cvar (cl_fpsmax * 4.0 + 13) / 4 is "(($cl_fpsmax * 4.0 + 13) / 4) static float evaluate( triChar* term ) { triChar* prn = infix2prn( term ); float result[128]; result[0] = 0.0f; triS32 r = 0; while (*prn) { if (*prn == ' ') prn++; else if (*prn == '+') { if (r<2) break; result[r-2] = result[r-2] + result[r-1]; r -= 1; prn++; } else if (*prn == '-') { if (r<2) break; result[r-2] = result[r-2] - result[r-1]; r -= 1; prn++; } else if (*prn == '*') { if (r<2) break; result[r-2] = result[r-2] * result[r-1]; r -= 1; prn++; } else if (*prn == '/') { if (r<2) break; result[r-2] = result[r-2] / result[r-1]; r -= 1; prn++; } else { result[r++] = triatof( prn ); prn += varlen(prn); } } return result[0]; }