Beispiel #1
0
void saffire_do_assign(char *var_name, char *val) {
    //printf("assign(%s => %s)\n", var_name, val);


    char type = VT_STRING;
    char *endptr;
    long num = strtol(val, &endptr, 10);
    if (endptr == val+strlen(val)) {
        type = VT_LONG;
    }


    t_var *var = var_find(var_name);
    if (var == NULL) {
        printf("Initial assign\n");
        var = var_alloc(type, var_name, type == VT_STRING ? (void *)val : (void *)num);
    } else {
        if (var->type != type) {
            printf("We cannot switch types!");
            exit(1);
        }
        if (type == VT_STRING) {
            var->val.str = strdup(val);
        } else if (type = VT_LONG) {
            var->val.num = num;
        }
    }

    //print_var(var);
}
Beispiel #2
0
/*
 * Like var_assign_integer, only this routine copies the
 * supplied "string" into the var named "name". If the var
 * named "name" cannot be found then it is first allocated
 * before the copy. Space for the string in the var comes
 * from interprocess shared memory. If the var "name"
 * cannot be found or allocated, or the memory for the
 * var_string copy of "string" cannot be allocated, the
 * routine returns -1, otherwise it returns 0.
 */
int
var_assign_string(char *name, char *string)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_alloc(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}

	if ((var->var_string = ipc_stralloc(string)) == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}

	filebench_log(LOG_DEBUG_SCRIPT, "Assign string %s=%s", name, string);

	return (0);
}
Beispiel #3
0
/*
 * Searches for the var named "name", and if not found
 * allocates it. The then extracts the var_string from
 * the var named "string" and copies it into the var_string
 * of the var "name", after first allocating a piece of
 * interprocess shared string memory. If the var "name"
 * cannot be found or allocated, or the var "string" cannot
 * be found, the routine returns -1, otherwise it returns 0.
 */
int
var_assign_var(char *name, char *string)
{
	var_t *var;
	var_string_t str;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_alloc(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}

	if ((str = var_ref_string(string)) == NULL)
		return (-1);

	if ((var->var_string = ipc_stralloc(*str)) == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}
	filebench_log(LOG_VERBOSE, "Assign string %s=%s", name, string);
	return (0);
}
Beispiel #4
0
struct bwb_line *
bwb_edit( struct bwb_line *l )
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   char edname[ MAXSTRINGSIZE + 1 ];
   struct bwb_variable *ed;
   FILE *loadfile;

   ed = var_find( DEFVNAME_EDITOR );
   str_btoc( edname, var_getsval( ed ));

   sprintf( tbuf, "%s %s", edname, CURTASK progfile );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#else
   nl();
   endwin(); /* Added by JBV 10/11/97 */
   system( tbuf );

   /*-----------------------*/
   /* Added by JBV 10/11/97 */
   /*-----------------------*/
   fprintf( stderr, "Press RETURN when ready..." );
   fgets( tbuf, MAXREADLINESIZE, stdin );
   refresh();

   nonl();
#endif

   /* open edited file for read */

   if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
      {
      sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
      bwb_error( bwb_ebuf );

      ncu_setpos();
      return bwb_zline( l );
      }

   /* clear current contents */

   bwb_new( l ); /* Relocated by JBV (bug found by DD) */

   /* and (re)load the file into memory */

   bwb_fload( loadfile );


   ncu_setpos();
   return bwb_zline( l );
   }
Beispiel #5
0
/*
 * Searches for the named var, and if found returns the value,
 * of var_integer. If the var is not found, or the var_integer's
 * value is 0, logs an error and returns 0.
 */
vinteger_t
var_to_integer(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if ((var != NULL) && (var->var_integer))
		return (var->var_integer);

	filebench_log(LOG_ERROR,
	    "Variable %s referenced before set", name);

	return (0);
}
Beispiel #6
0
void saffire_do_print(char *str) {
    //printf("print(%s)\n", str);

    if (str[0] == '$') {
        t_var *var = var_find(str);
        if (var == NULL) {
            printf("Cannot find variable %s", str);
            exit(1);
        }
        if (var->type == VT_STRING) {
            printf("%s", var->val.str);
        } else if (var->type = VT_LONG) {
            printf("%ld", var->val.num);
        }
    } else {
        str[strlen(str)-1] = '\0';
        printf("%s", str+1);
    }
}
Beispiel #7
0
struct bwb_line *
bwb_edit( struct bwb_line *l )
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   char edname[ MAXSTRINGSIZE + 1 ];
   struct bwb_variable *ed;
   FILE *loadfile;

   ed = var_find( DEFVNAME_EDITOR );
   str_btoc( edname, var_getsval( ed ));

   sprintf( tbuf, "%s %s", edname, CURTASK progfile );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#else
   system( tbuf );
#endif

   /* open edited file for read */

   if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
      {
      sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
      bwb_error( bwb_ebuf );

      iqc_setpos();
      return bwb_zline( l );
      }

   /* clear current contents */

   bwb_new( l ); /* Relocated by JBV (bug found by DD) */

   /* and (re)load the file into memory */

   bwb_fload( loadfile );


   iqc_setpos();
   return bwb_zline( l );
   }
Beispiel #8
0
void _do_incdec(char *var_name, int inc) {
    t_var *var = var_find(var_name);
    if (var == NULL) {
        printf("Warning: var is not initialized.");
        var = var_alloc(VT_LONG, var_name, 0);
    }

    if (var->type != VT_LONG) {
        printf("Warning: var is not a number");
        return;
    }

    if (inc) {
        var->val.num++;
    } else {
        var->val.num--;
    }

    //print_var(var);
}
Beispiel #9
0
/*
 * Searches for the named var, and if found copies the var_string,
 * if it exists, or a decimal number string representation of
 * var_integer, into a malloc'd bit of memory using fb_stralloc().
 * Returns a pointer to the created string, or NULL on failure.
 */
char *
var_to_string(char *name)
{
	var_t *var;
	char tmp[128];

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var == NULL)
		return (NULL);

	if (var->var_string)
		return (fb_stralloc(var->var_string));

	(void) snprintf(tmp, sizeof (tmp), "%lld", var->var_integer);

	return (fb_stralloc(tmp));
}
Beispiel #10
0
/*
 * Searches for the named var, and if found returns a pointer
 * to the var's var_integer. If not found, attempts to allocate
 * a var named "name" and returns a  pointer to it's (zeroed)
 * var_integer. If the var cannot be found or allocated, an
 * error is logged and the run is terminated.
 */
vinteger_t *
var_ref_integer(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var == NULL)
		var = var_alloc(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Invalid variable $%s",
		    name);
		filebench_shutdown(1);
	}

	return (&var->var_integer);

}
Beispiel #11
0
/*
 * Searches for the named var, and, if found, sets its
 * var_integer's value to that of the supplied integer.
 * If not found, the routine allocates a new var and sets
 * its var_integers's value to that of the supplied
 * integer. If the named var cannot be found or allocated
 * the routine returns -1,	otherwise it returns 0.
 */
int
var_assign_integer(char *name, vinteger_t integer)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_alloc(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}

	var->var_integer = integer;

	filebench_log(LOG_DEBUG_SCRIPT, "Assign integer %s=%lld",
	    name, integer);

	return (0);
}
Beispiel #12
0
static struct bwb_line *
dio_lrset(struct bwb_line * l, int rset)
{
   char            varname[BasicNameLengthMax + 1];
   bstring        *d, *s;
   int            *pp;
   int             n_params;
   int             p;
   register int    n, i;
   int             startpos;
   struct exp_ese *e;
   struct bwb_variable *v;
   int             pos;

   bwx_DEBUG(__FUNCTION__);
   /* find the variable name */

   bwb_getvarname(l->buffer, varname, &(l->position));
   v = var_find(varname);

   if (v == NULL)
   {
      sprintf(bwb_ebuf, "in dio_lrset(): failed to find variable");
      bwb_error(bwb_ebuf);
   }
   if (v->type != STRING)
   {
      sprintf(bwb_ebuf, "in dio_lrset(): assignment must be to string variable");
      bwb_error(bwb_ebuf);
   }
   /* read subscripts */

   pos = 0;
   if ((v->dimensions == 1) && (v->array_sizes[0] == 1))
   {
      n_params = 1;
      pp = &p;
      pp[0] = dim_base;
   }
   else
   {
      dim_getparams(l->buffer, &(l->position), &n_params, &pp);
   }

   CURTASK         exps[CURTASK expsc].pos_adv = pos;
   for (n = 0; n < v->dimensions; ++n)
   {
      v->array_pos[n] = pp[n];
   }

   /* get bstring pointer */

   d = var_findsval(v, pp);

   /* find equals sign */

   adv_ws(l->buffer, &(l->position));
   if (l->buffer[l->position] != '=')
   {
      sprintf(bwb_ebuf, "in dio_lrset(): failed to find equal sign");
      bwb_error(bwb_ebuf);
   }
   ++(l->position);
   adv_ws(l->buffer, &(l->position));

   /* read remainder of line to get value */

   e = bwb_exp(l->buffer, FALSE, &(l->position));
   if (ERROR_PENDING)
   {
      return bwb_zline(l);
   }
   s = exp_getsval(e);

   /* set starting position */

   startpos = 0;
   if (rset == TRUE)
   {
      if (s->length < d->length)
      {
         startpos = d->length - s->length;
      }
   }
   /* write characters to new position */

   i = 0;
   for (n = startpos; (i < (int) s->length) && (n < (int) d->length); ++n)
   {
      d->sbuffer[n] = s->sbuffer[i];
      ++i;
   }

   /* return */

   return bwb_zline(l);

}
Beispiel #13
0
struct bwb_line *
bwb_FIELD(struct bwb_line * l)
{
   int             dev_number;
   struct exp_ese *e;
   int             current_pos;
   char            atbuf[BasicStringLengthMax + 1];
   int             pos;

   bwx_DEBUG(__FUNCTION__);

   current_pos = 0;

   /* first read device number */

   adv_ws(l->buffer, &(l->position));
   if (l->buffer[l->position] == BasicFileNumberPrefix)
   {
      ++(l->position);
      adv_ws(l->buffer, &(l->position));
   }
   adv_element(l->buffer, &(l->position), atbuf);

   pos = 0;
   e = bwb_exp(atbuf, FALSE, &pos);
   if (ERROR_PENDING)
   {
      return bwb_zline(l);
   }
   if (e->type != NUMBER)
   {
      bwb_error("in bwb_field(): Number was expected for device number");
      return bwb_zline(l);
   }
   dev_number = exp_getival(e);


   if (dev_number < 1 || dev_number > BasicFileNumberMax)
   {
      bwb_error("in bwb_field(): Requested device number is not VALID.");
      return bwb_zline(l);
   }
   /* be sure that the requested device is open */

   if (dev_table[dev_number].mode != DEVMODE_RANDOM)
   {
      bwb_error("in bwb_field(): Requested device number is not RANDOM.");
      return bwb_zline(l);
   }
   /* loop to read variables */

   do
   {
      int             length;
      struct bwb_variable *v;
      bstring        *b;


      /* read the comma and advance beyond it */

      adv_ws(l->buffer, &(l->position));
      if (l->buffer[l->position] == ',')
      {
         ++(l->position);
      }
      /* first find the size of the field */

      adv_element(l->buffer, &(l->position), atbuf);  /* get element */

      pos = 0;
      e = bwb_exp(atbuf, FALSE, &pos);
      if (ERROR_PENDING)
      {
         return bwb_zline(l);
      }
      if (e->type != NUMBER)
      {
         bwb_error("in bwb_field(): number value for field size not found");
         return bwb_zline(l);
      }
      length = exp_getival(e);


      /* read the AS */

      adv_element(l->buffer, &(l->position), atbuf);  /* get element */


      if (strcasecmp(atbuf, "AS") != 0)
      {
         bwb_error("in bwb_field(): AS statement not found");
         return bwb_zline(l);
      }
      /* read the string variable name */
#if 0
      adv_element(l->buffer, &(l->position), atbuf);  /* get element */
#endif
      bwb_getvarname(l->buffer, atbuf, &(l->position));
      v = var_find(atbuf);

      if (v->type != STRING)
      {
         bwb_error("in bwb_field(): string variable name not found");
         return bwb_zline(l);
      }
      /* check for overflow of record length */

      if ((current_pos + length) > dev_table[dev_number].width)
      {
         bwb_error("in bwb_field(): record length exceeded");
         return bwb_zline(l);
      }
      /* set buffer */

      b = var_findsval(v, v->array_pos);
      b->sbuffer = dev_table[dev_number].buffer + current_pos;
      b->length = (unsigned int) length;  /* Was unsigned char
                      * (JBV 9/4/97) */
      b->rab = TRUE;

      current_pos += length;


      /* eat up any remaining whitespace */

      adv_ws(l->buffer, &(l->position));

   }

   while (l->buffer[l->position] == ',');

   /* return */

   return bwb_zline(l);

}
Beispiel #14
0
void GLEPolish::internalPolish(GLEPcode& pcode, int *rtype) throw(ParserError) {
	GLESub* sub;
	string uc_token;
	int idx, ret, np, *plist, term_bracket = false;
	int curpri = 0;
	int nstk = 0, stk[50], stkp[50];   /* stack for operators */
	int unary = 1;                     /* binary or unary operation expected */
	bool isa_string = false;
	bool not_string = false;
	if (*rtype==1) not_string = true;
	if (*rtype>0) term_bracket = true;
	pcode.addInt(PCODE_EXPR);   /* Expression follows */
	int savelen = pcode.size(); /* Used to set acutal length at end */
	pcode.addInt(0);	    /* Length of expression */
	while (true) {
		string token = m_tokens.try_next_token();
		int token_col = m_tokens.token_pos_col();
		int token_len = token.length();
		char first_char = token_len > 0 ? token[0] : ' ';
		// cout << "Token: '" << token << "'" << endl;
		// end of stream, or found ',' or ')'
		if (token_len == 0 || (token_len == 1 && (first_char == ',' || (first_char == ')' && curpri == 0) || (first_char == ']' && curpri == 0)))) {
			if (token_len != 0) {
				m_tokens.pushback_token();
			}
			*rtype = 0;
			dbg gprint("Found END OF EXPRESSION \n");
			if (curpri != 0) {
				throw error("unexpected end of expression, missing closing ')' or ']'");
			}
			/* Pop everything off the stack */
			for (int i = nstk; i > 0; i--) {
				dbg gprint("Adding left over operators  I = %d  op=%d \n",i,stk[i]);
				pcode.addInt(stk[i]);
			}
			if (unary == 1) {
				throw error("constant, function, or unary operator expected");
			}
			pcode.setInt(savelen, pcode.size() - savelen - 1);
			#ifdef DEBUG_POLISH
				pcode.show(savelen);
			#endif
			return;
		}
		dbg gprint("First word token via (1=unary %d) cts {%s}\n ", unary, token.c_str());
		switch (unary) {
		case 1:  /* a unary operator, or function, or number or variable */
			if (is_float(token)) {
				dbg gprint("Found number {%s}\n",token.c_str());
				double value = atof(token.c_str());
				pcode.addDouble(value);
				unary = 2;
				break;
			}
			str_to_uppercase(token, uc_token);
			/* NOT a number, is it a built in function? */
			find_un((char*)uc_token.c_str(), &idx, &ret, &np, &plist);
			/* 1,2 = +,- */
			if (idx > 3 && m_tokens.is_next_token("(")) {
				//
				// it is a built in function
				//
				dbg gprint("Found built in function \n");
				get_params(pcode, np, plist, uc_token);
				pcode.addFunction(idx + FN_BUILTIN_MAGIC);
				unary = 2;
				break;
			} else if (idx > 0 && idx <= 3) {
				stack_fn(idx);
				unary = 1;
				break;
			}
			/* Is it a user-defined function, identical code too above. */
			sub = sub_find((char*)uc_token.c_str());
			if (sub != NULL && m_tokens.is_next_token("(")) {
				//
				// it is a user defined function
				//
//				printf("User cts=%s  idx=%d ret=%d np=%d plist=%d\n",cts,idx,ret,np,plist);
				dbg gprint("Found user function \n");
				get_params(pcode, sub->getNbParam(), sub->getParamTypes(), uc_token);
				pcode.addFunction(sub->getIndex()+LOCAL_START_INDEX);
				unary = 2;
				break;
			}
			/* Is it a 'known' variable */
			int v;
			var_find((char*)uc_token.c_str(), &v, &ret);
			if (v >= 0) {
				// cout << "found var: '" << uc_token << "' -> " << v << endl;
				if (ret == 2) pcode.addStrVar(v);
				else pcode.addVar(v);
				unary = 2;
				if (m_vars != NULL && m_vars->try_get(uc_token) == -1) {
					/* Add it to list of vars */
					m_vars->add_item(uc_token, v);
				}
				break;
			}
			/* Is it a string */
			if (first_char == '"' || first_char == '\'') {
				dbg gprint("Found string \n");
				string str_no_quote = token;
				str_remove_quote(str_no_quote);
				pcode.addString(str_no_quote);
				unary = 2;
				break;
			}
			if ((first_char == 'd' || first_char == 'D') && token_len == 1 && m_tokens.is_next_token("[")) {
				get_array_index(pcode);
				pcode.addFunction(FN_DI + FN_BUILTIN_MAGIC);
				unary = 2;
				break;
			}
			if (first_char == '(' && token_len == 1) {
				curpri = curpri + 100;
				break;
			}
			if ((first_char == ')' || first_char == ')') && token_len == 1) {
				throw error("constant, function, or unary operator expected");
			}
			if (m_tokens.is_next_token("(")) {
				throw error(token_col, string("call to undefined function '"+token+"'"));
			}
			/* must be unquoted string, unless a binary operator
			   was found, in which case it is an undelcared variable */
			if (not_string || str_var(token)) {
				/* name that includes '$' is also assumed to be a variable */
				dbg gprint("Found un-initialized variable {%s} /n",token.c_str());
				if (!var_valid_name(uc_token)) {
					throw error(token_col, "illegal variable name '"+uc_token+"'");
				}
				var_findadd((char*)uc_token.c_str(), &v, &ret);
				if (ret == 2) pcode.addStrVar(v);
				else pcode.addVar(v);
				not_string = true;
				unary = 2;
				if (m_vars != NULL && m_vars->try_get(uc_token) == -1) {
					/* Add it to list of vars */
					m_vars->add_item(uc_token, v);
				}
				break;
			}
			// std::cout << "Unquoted string '" << token << "'" << std::endl;
			pcode.addString(token);
			if (!valid_unquoted_string(token)) {
				throw error(token_col, "invalid unquoted string '"+token+"'");
			}
			isa_string = true;
			unary = 2;
			break;
		case 2: /* a binary operator, or space, or end of line */
			/* MIGHT (gives error with a$ = b$+c$) */
			if (first_char != '.') {
				if (isa_string) {
					throw error("left hand side contains unquoted string");
				}
				not_string = true;
			} else {
				not_string = false;
			}
			/* Binary operators, +,-,*,/,^,<,>,<=,>=,.and.,.or. */
			int priority = 0;
			if (token_len == 1) {
				switch (first_char) {
					case '+' : v = BIN_OP_PLUS;  priority = 2; break;
					case '-' : v = BIN_OP_MINUS;  priority = 2; break;
					case '*' : v = BIN_OP_MULTIPLY;  priority = 3; break;
					case '/' : v = BIN_OP_DIVIDE;  priority = 3; break;
					case '%' : v = BIN_OP_MOD; priority = 3; break;
					case '^' : v = BIN_OP_POW;  priority = 4; break;
					case '=' : v = BIN_OP_EQUALS;  priority = 1; break;
					case '&' : v = BIN_OP_AND; priority = 1; break;
					case '|' : v = BIN_OP_OR; priority = 1; break;
					case '<' : v = BIN_OP_LT;  priority = 1; break;
					case '>' : v = BIN_OP_GT;  priority = 1; break;
					case '.' : v = BIN_OP_DOT;  priority = 2; break;
					default  : v = 0;
				}
			} else {
				str_to_uppercase(token, uc_token);
				if (token == "<=") {
					v = BIN_OP_LE; priority = 1;
				} else if (token == "<>") {
					v = BIN_OP_NOT_EQUALS; priority = 1;
				} else if (token == ">=") {
					v = BIN_OP_GE; priority = 1;
				} else if (token == "**") {
					v = BIN_OP_POW;  priority = 4;
				} else if (uc_token == "AND") {
					v = BIN_OP_AND; priority = 1;
				} else if (uc_token == "OR") {
					v = BIN_OP_OR; priority = 1;
				} else {
					v = 0;
				}
			}
			if (v > 0) {
				stack_bin(v, priority);
				dbg gprint("Found binary operator \n");
				unary = 1;
			} else if (first_char == ')' && token_len == 1) {
				if (curpri > 0) {
					curpri = curpri - 100;
					unary = 2;
					break;
				}
				if (!term_bracket) {
					throw error("too many closing ')', expecting binary operator");
				}
			} else {
				throw error(string("unknown binary operator '")+token+"'");
			}
		} // end switch
	} // end for
}