Beispiel #1
0
static void _shift_opdr(struct symbols_t *sl, struct token_t *token)
{
    int64_t operand1, operand2;
    enum {
        OPERATION_ADD,
        OPERATION_SUBSTRUCT,
    } operation;
    
    PRINTF("%s" NL, __FUNCTION__);
    if (token_get(token, TOKEN_TYPE_PLUS, TOKEN_NEXT))
        operation = OPERATION_ADD;
    else if (token_get(token, TOKEN_TYPE_MINUS, TOKEN_NEXT))
        operation = OPERATION_SUBSTRUCT;
    else
        return;

    PRINTF("ADD operation %u" NL, operation);

    _add_opd(sl, token);

    operand2 = _exprstack_pop(sl);
    operand1 = _exprstack_pop(sl);

    _exprstack_push(operation == OPERATION_ADD ? operand1 + operand2 : operand1 - operand2);

    _shift_opdr(sl, token);
}
Beispiel #2
0
static void _and_opdr(struct symbols_t *sl, struct token_t *token)
{
    int64_t operand1, operand2;
    enum {
        OPERATION_SHIFT_LEFT,
        OPERATION_SHIFT_RIGHT,
    } operation;
    
    PRINTF("%s" NL, __FUNCTION__);
    if (token_get(token, TOKEN_TYPE_SHIFT_LEFT, TOKEN_NEXT))
        operation = OPERATION_SHIFT_LEFT;
    else if (token_get(token, TOKEN_TYPE_SHIFT_RIGHT, TOKEN_NEXT))
        operation = OPERATION_SHIFT_RIGHT;
    else
        return;

    PRINTF("SHIFT %u" NL, operation);

    _shift_opd(sl, token);

    operand2 = _exprstack_pop(sl);
    operand1 = _exprstack_pop(sl);

    _exprstack_push(operation == OPERATION_SHIFT_LEFT ? operand1 << operand2 : operand1 >> operand2);

    _and_opdr(sl, token);
}
void target_primary_os(void)
{
    if (token_get("CONFIG_RG_OS_LINUX_24"))
	token_set("CONFIG_RG_OS", "LINUX_24");
    else if (token_get("CONFIG_RG_OS_LINUX_26"))
	token_set("CONFIG_RG_OS", "LINUX_26");
}
Beispiel #4
0
static void _add_opdr(struct symbols_t *sl, struct token_t *token)
{
    int64_t operand1, operand2;
    enum {
        OPERATION_MUL,
        OPERATION_DIV,
        OPERATION_MOD,
    } operation;
    
    PRINTF("%s" NL, __FUNCTION__);
    if (token_get(token, TOKEN_TYPE_MUL, TOKEN_NEXT))
        operation = OPERATION_MUL;
    else if (token_get(token, TOKEN_TYPE_DIV, TOKEN_NEXT))
        operation = OPERATION_DIV;
    else if (token_get(token, TOKEN_TYPE_MOD, TOKEN_NEXT))
        operation = OPERATION_MOD;
    else
        return;

    PRINTF("MUL operation %u" NL, operation);

    _mul_opd(sl, token);

    operand2 = _exprstack_pop(sl);
    operand1 = _exprstack_pop(sl);

    if (operation == OPERATION_MUL)
        _exprstack_push(operand1 * operand2);
    else if (operation == OPERATION_DIV)
        _exprstack_push(operand1 / operand2);
    else if (operation == OPERATION_MOD)
        _exprstack_push(operand1 % operand2);

    _add_opdr(sl, token);
}
Beispiel #5
0
/*
 * bestmove
 *   resign
 *   win
 *   <move1> [ponder <move2>]
 */
static int
bestmove_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;

	if ((p = token_get(&token, s)) == NULL)
		return -1;

	if (token_equal(&token, "resign"))
		return usimsg_pushnil(msg, USIOBJ_RESIGN);
	if (token_equal(&token, "win"))
		return usimsg_pushnil(msg, USIOBJ_WIN);

	if (usimsg_pushstr(msg, USIOBJ_MOVE, token.s, token.len) != 0)
		return -1;

	if ((p = token_get(&token, p)) == NULL)
		return 0;
	if (!token_equal(&token, "ponder"))
		return 0;
	if ((p = token_get(&token, p)) == NULL)
		return -1;
	if (usimsg_pushstr(msg, USIOBJ_MOVE, token.s, token.len) != 0)
		return -1;

	return 0;
}
Beispiel #6
0
int lang_constexpr(struct symbols_t *sl, struct token_t *token, int64_t *value)
{
    if (!token_get(token, TOKEN_TYPE_CURLY_OPEN, TOKEN_NEXT))
        return -1;

    /*
     * Expression syntax.
     *
     *     EXPR      = EXPR, "|", OR_OPD | OR_OPD
     *     OR_OPD    = OR_OPD, "^", XOR_OPD | XOR_OPD
     *     XOR_OPD   = XOR_OPD, "&", AND_OPD | AND_OPD
     *     AND_OPD   = AND_OPD, "<<", SHIFT_OPD | AND_OPD, ">>", SHIFT_OPD | SHIFT_OPD
     *     SHIFT_OPD = SHIFT_OPD, "+", ADD_OPD | SHIFT_OPD, "-", ADD_OPD | ADD_OPD
     *     ADD_OPD   = ADD_OPD, "*", MUL_OPD | ADD_OPD, "/", MUL_OPD | ADD_OPD, "%", MUL_OPD | MUL_OPD
     *     MUL_OPD   = "~", MUL_OPD | NOT_OPD
     *     NOT_OPD   = NUMBER | SYMBOL | "(", EXPR, ")"
     *
     * Eliminate of left recursion.
     *
     *     EXPR       = OR_OPD, EXPRR
     *     EXPRR      = "|", OR_OPD, EXPRR | 0
     *     OR_OPD     = XOR_OPD, OR_OPDR
     *     OR_OPDR    = "^", XOR_OPD, OR_OPDR | 0
     *     XOR_OPD    = AND_OPD, XOR_OPDR
     *     XOR_OPDR   = "&", AND_OPD, XOR_OPDR | 0
     *     AND_OPD    = SHIFT_OPD, AND_OPDR
     *     AND_OPDR   = "<<", SHIFT_OPD, AND_OPDR | ">>", SHIFT_OPD, AND_OPDR | 0
     *     SHIFT_OPD  = ADD_OPD, SHIFT_OPDR
     *     SHIFT_OPDR = "+", ADD_OPD, SHIFT_OPDR | "-", ADD_OPD, SHIFT_OPDR | 0
     *     ADD_OPD    = MUL_OPD, ADD_OPDR
     *     ADD_OPDR   = "*", MUL_OPD, ADD_OPDR | "/", MUL_OPD, ADD_OPDR |"%", MUL_OPD, ADD_OPDR | 0
     *     MUL_OPD    = "!", NOT_OPD | NOT_OPD
     *     NOT_OPD    = NUMBER | SYMBOL | "(", EXPR, ")"
     */

    _exprstack_flush();

    _expr(sl, token);

    *value = _exprstack_pop();

    if (!token_get(token, TOKEN_TYPE_CURLY_CLOSE, TOKEN_NEXT))
    {
        debug_emsg("Missing \"}\" in expr");
        token_print_rollback(token);
        app_close(APP_EXITCODE_ERROR);
    }

    token_drop(token);

    return 0;
}
void target_os_enable_wireless(void)
{
    token_set_y("CONFIG_NET_RADIO");
    token_set_y("CONFIG_NET_WIRELESS");
    if (token_get("CONFIG_RG_OS_LINUX_26"))
	token_set_y("CONFIG_WIRELESS_EXT");
}
Beispiel #8
0
static void read_symtab(symtab_t tab, const char *input, size_t limit) {
  token_t tok;
  const char *inputend = limit ? input + limit : NULL;
  token_init(tok);
  for (;;) {
    input = token_get(tok, input, inputend);
    if (tok->type != token_word) break;
    char *key = pbc_strdup(tok->s);
    input = token_get(tok, input, inputend);
    if (tok->type != token_word) {
      pbc_free(key);
      break;
    }
    symtab_put(tab, pbc_strdup(tok->s), key);
    pbc_free(key);
  }
  token_clear(tok);
}
Beispiel #9
0
static void _not_opd(struct symbols_t *sl, struct token_t *token)
{
    char *tname;
    
    PRINTF("%s" NL, __FUNCTION__);
    if ((tname = token_get(token, TOKEN_TYPE_NUMBER, TOKEN_NEXT)))
    {
        int64_t value;

        PRINTF("NUM %s" NL, tname);

        if (lang_util_str2num(tname, &value) < 0)
        {
            token_print_rollback(token);
            app_close(APP_EXITCODE_ERROR);
        }

        _exprstack_push(value);
    } else if ((tname = token_get(token, TOKEN_TYPE_SYMBOL, TOKEN_NEXT))) {
        struct symbol_t *s;
        int64_t value;

        s = symbol_get_const(sl, tname, &value);
        if (!s)
        {
            debug_emsgf("Symbol not found", "%s" NL, tname);
            token_print_rollback(token);
            app_close(APP_EXITCODE_ERROR);
        }
        _exprstack_push(value);
    } else if (token_get(token, TOKEN_TYPE_ROUND_OPEN, TOKEN_NEXT)) {
        _expr(sl, token);
        if (!token_get(token, TOKEN_TYPE_ROUND_CLOSE, TOKEN_NEXT))
        {
            debug_emsg("Missing \")\" in expr");
            token_print_rollback(token);
            app_close(APP_EXITCODE_ERROR);
        }
    } else {
        debug_emsg("Empty expression");
        token_print_rollback(token);
        app_close(APP_EXITCODE_ERROR);
    }
}
Beispiel #10
0
static void
menu_draw_row(GContext *ctx, const Layer *cell_layer, MenuIndex *cell_index, void *callback_context)
{
  user_data *ud = callback_context;
  bool success;
  token t;

  if (ud->moving.to == cell_index->row)
    success = token_get(ud->moving.from, &t);
  else if (ud->moving.from < ud->moving.to && cell_index->row >= ud->moving.from && cell_index->row < ud->moving.to)
    success = token_get(cell_index->row + 1, &t);
  else if (ud->moving.from > ud->moving.to && cell_index->row > ud->moving.to && cell_index->row <= ud->moving.from)
    success = token_get(cell_index->row - 1, &t);
  else
    success = token_get(cell_index->row, &t);
  
  if (success)
    menu_cell_basic_draw(ctx, cell_layer, t.issuer, t.name, ud->moving.to == cell_index->row ? ud->icon : NULL);
}
Beispiel #11
0
/*
 * Get an expected token.
 */
static bool token_expect(context_t cxt, token_t tok)
{
    char tokstr[TOKEN_MAXLEN+1];
    token_t tok1 = token_get(cxt, NULL, tokstr);
    if (tok1 != tok)
    {
        if (tok1 != TOKEN_ERROR)
            parse_error(cxt, "expected token `%s'; got token `%s'",
                token_getstr(tok), tokstr);
        return false;
    }
    return true;
}
Beispiel #12
0
/*
 * Peek at a token.
 */
static token_t token_peek(context_t cxt, term_t *val, char *tokstr)
{
    if (cxt->peeked_token != TOKEN_NONE)
    {
        if (val != NULL)
            *val = cxt->peeked_val;
        if (tokstr != NULL)
            strcpy(tokstr, cxt->peeked_str);
        return cxt->peeked_token;
    }
    cxt->peeked_token = token_get(cxt, &cxt->peeked_val, cxt->peeked_str);
    if (val != NULL)
        *val = cxt->peeked_val;
    if (tokstr != NULL)
        strcpy(tokstr, cxt->peeked_str);
    return cxt->peeked_token;
}
Beispiel #13
0
/*
 * id
 *   name <program name>
 *   author <program author>
 */
static int
id_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;

	if ((p = token_get(&token, s)) == NULL)
		return -1;

	if (token_equal(&token, "name"))
		return usimsg_pushstr(msg, USIOBJ_PROGNAME, p, strlen(p));

	if (token_equal(&token, "author"))
		return usimsg_pushstr(msg, USIOBJ_AUTHOR, p, strlen(p));

	return -1;
}
Beispiel #14
0
/*
 * checkmate
 *   notimplemented
 *   timeout
 *   nomate
 *   <move1> ... <movei>
 */
static int
checkmate_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;

	if ((p = token_get(&token, s)) == NULL)
		return -1;

	if (token_equal(&token, "notimplemented"))
		return usimsg_pushnil(msg, USIOBJ_NOTIMPLEMENTED);
	if (token_equal(&token, "timeout"))
		return usimsg_pushnil(msg, USIOBJ_TIMEOUT);
	if (token_equal(&token, "nomate"))
		return usimsg_pushnil(msg, USIOBJ_NOMATE);

	return usimsg_pushstr(msg, USIOBJ_MOVE, token.s, strlen(token.s));
}
Beispiel #15
0
static void _xor_opdr(struct symbols_t *sl, struct token_t *token)
{
    int64_t operand1, operand2;

    PRINTF("%s" NL, __FUNCTION__);
    if (!token_get(token, TOKEN_TYPE_AND, TOKEN_NEXT))
        return;

    PRINTF("&" NL);

    _and_opd(sl, token);

    operand2 = _exprstack_pop(sl);
    operand1 = _exprstack_pop(sl);

    _exprstack_push(operand1 & operand2);

    _xor_opdr(sl, token);
}
Beispiel #16
0
static void _mul_opd(struct symbols_t *sl, struct token_t *token)
{
    int64_t operand;
    
    PRINTF("%s" NL, __FUNCTION__);
    if (token_get(token, TOKEN_TYPE_NEGATE, TOKEN_NEXT))
    {
        PRINTF("NEG" NL);

        _not_opd(sl, token);
        operand = _exprstack_pop(sl);

        PRINTF("~ operand %lld" NL, operand);

        _exprstack_push(~operand);
    } else {
        _not_opd(sl, token);
    }
}
Beispiel #17
0
int assembler(struct asm_context_t *ctx, char *infile)
{
    struct token_t *token;
    int error;

    token = token_new(&ctx->tokens);
    token_prepare(token, infile);

    while (1)
    {
        token_drop(token);
        if (lang_eof(token) == 0)
            break;
        if (lang_comment(token) == 0)
            continue;
        if (lang_label(ctx, token) == 0)
            continue;
        if (ctx->pass)
        {
            if (lang_directive(ctx, token) == 0)
                continue;
            if (lang_instruction(ctx, token) == 0)
                continue;
        } else {
            if (token_get(token, TOKEN_TYPE_LINE, TOKEN_CURRENT))
                continue;
        }

        debug_emsg("Unknown program construction");
        goto error;
    }

    error = 0;
    goto noerror;
error:
    error = -1;
    token_print_rollback(token);
    debug_emsgf("Error in file", "%s" NL, infile);
noerror:
    token_remove(&ctx->tokens, token);
    return error;
}
/* Print a list of the selected/not selected features */
static int print_major_features_by_selection(FILE *f, FILE *cfile, 
    int selected)
{
    option_t *opt;
    int opt_selected;
    char *description;
    
    /* Go through the complete features list and print the major ones */
    for (opt = &openrg_config_options[0] ; opt->token ; opt++)
    {
	/* Skip if not major feature OR if selection is not as required */
	if (!(opt->type & OPT_MAJOR_FEATURE) && !(opt->type & OPT_MODULE) &&
	    !(opt->type & OPT_HARDWARE))
	{
	    continue;
	}

	opt_selected = token_get(opt->token) ? 1 : 0;

	if (!opt_selected != !selected)
	    continue;

	if (selected && !(opt->type & selected))
	    continue;

	/* Use token and warn if no description */
	description = opt->description;

	if (!description)
	{
	    fprintf(stderr, "Warning: no description for major "
		"feature '%s'\n", opt->token);
	    description = opt->token;
	}
	/* Print */
	fprintf(f, "%-40s %s\n", description, opt->token);

	if (cfile && (opt->type & OPT_MAJOR_FEATURE))
	    cfile_feature_print(cfile, description);
    }
    return 0;
}
Beispiel #19
0
static void
menu_select_click(MenuLayer *menu_layer, MenuIndex *cell_index, void *callback_context)
{
  user_data *ud = callback_context;
  token t;
 
  if (ud->moving.from >= 0) {
    if (ud->moving.from != ud->moving.to)
      token_move(ud->moving.from, ud->moving.to);
    ud->moving = (moving) { -1, -1 };
    menu_layer_reload_data(menu_layer);
    return;
  }

  if (token_get(cell_index->row, &t)) {
    ud->code = code_create(&t);
    if (ud->code)
      window_stack_push(ud->code, true);
  }
}
Beispiel #20
0
int
usimsg_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;
	Tbl *tbl;

	if ((p = token_get(&token, s)) == NULL)
		return -1;

	msg->head = NULL;
	msg->tail = NULL;

	if ((tbl = tbl_find(tbl_msg, &token)) == NULL)
		return 0;

	msg->type = tbl->id;
	if (tbl->parse == NULL)
		return 0;

	(*tbl->parse)(msg, p);

	return 0;
}
Beispiel #21
0
int com_area(LEXER *lp, char *arg)		
{
    UNUSED(arg);
    enum {START,NUM1,END} state = START;

    TOKEN token;
    char *word;
    int debug=0;
    int done=0;
    int valid_comp=0;
    size_t i;
    DB_DEFLIST *p_best;
    DB_DEFLIST *p_prev = NULL;
    double x1, y1;
    double area;
    double cum_area=0.0;
    char instname[BUFSIZE];
    char *pinst = (char *) NULL;

    int my_layer=0; 	/* internal working layer */

    int comp=ALL;

    /* check that we are editing a rep */
    if (currep == NULL ) {
	printf("must do \"EDIT <name>\" before AREA\n");
	token_flush_EOL(lp);
	return(1);
    }

/* 
    To create a show set for restricting the pick, we look 
    here for and optional primitive indicator
    concatenated with an optional layer number:

	A[layer_num] ;(arc)
	C[layer_num] ;(circle)
	L[layer_num] ;(line)
	N[layer_num] ;(note)
	O[layer_num] ;(oval)
	P[layer_num] ;(poly)
	R[layer_num] ;(rectangle)
	T[layer_num] ;(text)

    or a instance name.  Instance names can be quoted, which
    allows the user to have instances which overlap the primitive
    namespace.  For example:  N7 is a note on layer seven, but
    "N7" is an instance call.

*/

    while(!done) {
	token = token_look(lp,&word);
	if (debug) printf("got %s: %s\n", tok2str(token), word);
	if (token==CMD) {
	    state=END;
	} 
	switch(state) {	
	case START:		/* get option or first xy pair */
	    if (debug) printf("in START\n");
	    if (token == OPT ) {
		token_get(lp,&word); /* ignore for now */
		state = START;
	    } else if (token == NUMBER) {
		state = NUM1;
	    } else if (token == EOL) {
		token_get(lp,&word); 	/* just eat it up */
		state = START;
	    } else if (token == EOC || token == CMD) {
		state = END;
	    } else if (token == IDENT) {
		token_get(lp,&word);
	    	state = NUM1;
		/* check to see if is a valid comp descriptor */
		valid_comp=0;
		if ((comp = is_comp(toupper((unsigned char)word[0])))) {
		    if (strlen(word) == 1) {
			my_layer = default_layer();
			printf("using default layer=%d\n",my_layer);
			valid_comp++;	/* no layer given */
		    } else {
			valid_comp++;
			/* check for any non-digit characters */
			/* to allow instance names like "L1234b" */
			for (i=0; i<strlen(&word[1]); i++) {
			    if (!isdigit((unsigned char)word[1+i])) {
				valid_comp=0;
			    }
			}
			if (valid_comp) {
			    if(sscanf(&word[1], "%d", &my_layer) == 1) {
				if (debug) printf("given layer=%d\n",my_layer);
			    } else {
				valid_comp=0;
			    }
			} 
			if (valid_comp) {
			    if (my_layer > MAX_LAYER) {
				printf("layer must be less than %d\n",
				    MAX_LAYER);
				valid_comp=0;
				done++;
			    }
			    if (!show_check_modifiable(currep, comp, my_layer)) {
				printf("layer %d is not modifiable!\n",
				    my_layer);
				token_flush_EOL(lp);
				valid_comp=0;
				done++;
			    }
			}
		    }
		} else { 
		    if (db_lookup(word)) {
		        strncpy(instname, word, BUFSIZE);
			pinst = instname;
		    } else {
			printf("not a valid instance name: %s\n", word);
			state = START;
		    }
		}
	    } else {
		token_err("AREA", lp, "expected DESC or NUMBER", token);
		state = END;	/* error */
	    }
	    break;
	case NUM1:		/* get pair of xy coordinates */
	    if (debug) printf("in NUM1\n");
	    if (token==NUMBER) {
		if (getnum(lp, "AREA", &x1, &y1)) {
		    if (debug) printf("got comp %d, layer %d\n", comp, my_layer);

		    if (p_prev != NULL) {
			db_highlight(p_prev);	/* unhighlight it */
			p_prev = NULL;
		    }

		    if ((p_best=db_ident(currep, x1,y1,0,my_layer, comp, pinst)) != NULL) {
			db_notate(p_best);	    /* print out id information */
			db_highlight(p_best);
			area = db_area(p_best);
			cum_area += area;
			if (area >= 0) {
			    printf("   area = %g, total = %g\n", area, cum_area );
			}
			p_prev=p_best;
			state = NUM1;
		    } else {
			printf("nothing here to measure... try SHO command?\n");
			state = START;
		    }
		} else {
		    state = END;
		}
	    } else if (token == EOL) {
		token_get(lp,&word); 	/* just ignore it */
	    } else if (token == EOC || token == CMD) {
		printf("AREA: cancelling POINT\n");
	        state = END;
	    } else {
		token_err("AREA", lp, "expected NUMBER", token);
		state = END; 
	    }
	    break;
	case END:
	default:
	    if (token == EOC || token == CMD) {
		;
	    } else {
		token_flush_EOL(lp);
	    }
	    done++;
	    rubber_clear_callback();
	    break;
	}
    }
    return(1);
}
Beispiel #22
0
int add_inst(LEXER *lp, char *inst_name)
{
    enum {START,NUM1,NUM2,NUM3,NUM4,END} state = START;

    double x1, y1;	// pick value
    double x2, y2;	// pick value
    double x3, y3;	// pick value
    double xold=0.0, yold=0.0;	// previous pick value to suppress double pics
    int numpicks=0;	// number of picks
    int done=0;
    TOKEN token;
    OPTS opts;
    char *word;
    int debug=0;
    struct db_inst *ip;

    DB_TAB *ed_rep;

    XFORM *xp;

    double xx, yy;

    opt_set_defaults(&opts);

    // rl_saveprompt();
    rl_setprompt("ADD_INST> ");

    if (debug) printf("currep = %s\n", currep->name);
    if (debug) printf("adding inst %s\n", inst_name);

    /* don't destroy it if it's already in memory */
    if (debug) printf("calling db_lookup with %s\n", inst_name);

    if (loadrep(inst_name) == 0) {
	printf("warning: you can't add a null instance: %s\n", inst_name);
	token_flush_EOL(lp);
	done++;	
    }

    if ((ed_rep = db_lookup(inst_name)) == 0) {
    	printf("ADD INST: instance not found: %s\n", inst_name );
	return(-1);
    }

    bb_xmin=ed_rep->minx;
    bb_xmax=ed_rep->maxx;
    bb_ymin=ed_rep->miny;
    bb_ymax=ed_rep->maxy;

    if (debug) printf("currep = %s\n", currep->name);

    if (strcmp(currep->name, inst_name) == 0 || db_contains(inst_name, currep->name)) {
    	printf("ADD INST: Sorry, to do that would cause a recursive definition\n" );
	return(-1);
    }

    while (!done) {
	token = token_look(lp, &word);
	if (debug) printf("got %s: %s state: %d\n", tok2str(token), word, state);
	if (token==CMD) {
	    state=END;
	} 
	switch(state) {	
	    case START:		/* get option or first xy pair */
	        db_checkpoint(lp);
		rubber_set_callback(draw_inst_bb);
		if (token == OPT ) {
		    token_get(lp, &word); 
		    if (opt_parse(word, INST_OPTS, &opts) == -1) {
			state = END;
		    } else {
			/* an option may have scaled the bounding box */
			/* clear callback, recompute and then restart */

			rubber_clear_callback(draw_inst_bb);

			xp = matrix_from_opts(&opts);
			bb_xmin = bb_xmax = bb_ymin = bb_ymax = 0.0;

			xx = ed_rep->minx;
			yy = ed_rep->miny;
			xform_point(xp, &xx, &yy); 

			if (xx < bb_xmin) bb_xmin = xx;
			if (yy < bb_ymin) bb_ymin = yy;
			if (xx > bb_xmax) bb_xmax = xx;
			if (yy > bb_ymax) bb_ymax = yy;

			xx = ed_rep->maxx;
			yy = ed_rep->maxy;
			xform_point(xp, &xx, &yy); 

			if (xx < bb_xmin) bb_xmin = xx;
			if (yy < bb_ymin) bb_ymin = yy;
			if (xx > bb_xmax) bb_xmax = xx;
			if (yy > bb_ymax) bb_ymax = yy;

			xx = ed_rep->maxx;
			yy = ed_rep->miny;
			xform_point(xp, &xx, &yy); 

			if (xx < bb_xmin) bb_xmin = xx;
			if (yy < bb_ymin) bb_ymin = yy;
			if (xx > bb_xmax) bb_xmax = xx;
			if (yy > bb_ymax) bb_ymax = yy;

			xx = ed_rep->minx;
			yy = ed_rep->maxy;
			xform_point(xp, &xx, &yy); 

			if (xx < bb_xmin) bb_xmin = xx;
			if (yy < bb_ymin) bb_ymin = yy;
			if (xx > bb_xmax) bb_xmax = xx;
			if (yy > bb_ymax) bb_ymax = yy;

			free(xp);

			state = START;
			rubber_set_callback(draw_inst_bb);
		    	if (opts.stepflag) {
			    rl_setprompt("ARRAY ORIGIN> ");
			    rubber_clear_callback();
			}
		    }
		} else if (token == NUMBER) {
		    if (opts.stepflag) {
			state = NUM2;	
		    } else {
			state = NUM1;	
		    }
		} else if (token == EOL) {
		    token_get(lp, &word); 	/* just eat it up */
		    state = START;
		} else if (token == EOC  || token == CMD) {
		    state = END; 
		} else {
		    token_err("INST", lp, "expected OPT or NUMBER", token);
		    state = END; 
		}
		break;
	    case NUM1:		/* get pair of xy coordinates */
		if (token == NUMBER) {
		    if (getnum(lp, "INST", &x1, &y1)) {
			// supress double clicks
			if (debug) printf("%d %g %g %g %g\n", numpicks, x1, y1, xold, yold);
			if (numpicks==0 || ((xold != x1) || (yold != y1))) {  
			    db_add_inst(currep, ed_rep, opt_copy(&opts), x1, y1);
			    rubber_clear_callback();
			    need_redraw++;
			    rubber_set_callback(draw_inst_bb);
			}
		        numpicks++; xold=x1; yold=y1;
			state = START;
	            } else {
			state = END;
		    }
		} else if (token == EOL) {
		    token_get(lp, &word);
		} else if (token == EOC  || token == CMD) {
		    state = END; 
		} else {
		    token_err("INST", lp, "expected NUMBER", token);
		    state = END; 
		}
		break;
	    case NUM2:		/* get column end coordinate */
		rubber_clear_callback();
		if (token == NUMBER) {
		    if (getnum(lp, "INST", &x1, &y1)) {
    			rl_setprompt("COLUMN EXTENT> ");
			state = NUM3;
	            } else {
			state = END;
		    }
		} else if (token == EOL) {
		    token_get(lp, &word);
		} else if (token == EOC  || token == CMD) {
		    state = END; 
		} else {
		    token_err("INST", lp, "expected NUMBER", token);
		    state = END; 
		}
		break;
	    case NUM3:		/* get column end coordinate */
		if (token == NUMBER) {
		    if (getnum(lp, "INST", &x3, &y3)) {
    			rl_setprompt("ROW EXTENT> ");
			state = NUM4;
	            } else {
			state = END;
		    }
		} else if (token == EOL) {
		    token_get(lp, &word);
		} else if (token == EOC  || token == CMD) {
		    state = END; 
		} else {
		    token_err("INST", lp, "expected NUMBER", token);
		    state = END; 
		}
		break;
	    case NUM4:		/* get column end coordinate */
		if (token == NUMBER) {
		    if (getnum(lp, "INST", &x2, &y2)) {
		        if (debug) printf("%g %g %g %g %g %g\n", x1, y1, x2, y2, x3, y3);
			ip = db_add_inst(currep, ed_rep, opt_copy(&opts), x1, y1);
			ip->colx = x3;
			ip->coly = y3;
			ip->rowx = x2;
			ip->rowy = y2;
			rubber_clear_callback();
			need_redraw++;
			state = END;
	            } else {
			state = END;
		    }
		} else if (token == EOL) {
		    token_get(lp, &word);
		} else if (token == EOC  || token == CMD) {
		    state = END; 
		} else {
		    token_err("INST", lp, "expected NUMBER", token);
		    state = END; 
		}
		break;
	    case END:
	    default:
		if (token == EOC || token == CMD) {
		    ;
		} else {
		    token_flush_EOL(lp);
		}
		done++;
		break;
	}
    }
    rubber_clear_callback();
    rl_restoreprompt();
    return(1);
}
Beispiel #23
0
static int parser_tokenize(struct inifile *inf, token_data *data, parser_entry *entry)
{
#ifdef DEBUG
	static const char *strtoken[] = {
		"NONE",   "BSECT",   "ESECT", "COMMENT", 
		"ASSIGN", "WHITESP", "QUOTE", "CDATA", 
		"EOSTR",  "MLINE"
	};
	static const char *strclass[] = {
		"GLOBAL", "SECTION", "KEYWORD", "VALUE"
	};
#endif
	
	/*
	 * Tokenize input string.
	 */
	while(token_get(inf, data)) {
		
#ifdef DEBUG
		printf("debug: (%d:%d): char='%c', curr=%s, prev=%s, seen=%s, class=%s\n",
		       data->line, data->pos, 
		       isprint(inf->str[data->pos]) ? inf->str[data->pos] : ' ', 
		       strtoken[data->curr],
		       strtoken[data->prev],
		       strtoken[data->seen],
		       strclass[data->cls]);
#endif
		/*
		 * Validate input
		 */
		if(inf->options & INIFILE_CHECK_SYNTAX) {
			if(!lexer_check(inf, data)) {
				return PARSE_ERROR;
			}
		}
		
		switch(data->curr)
		{
		case BSECT:				/* Begin section */
			if(entry->sect) {
				free(entry->sect);
				entry->sect = NULL;
			}
			break;
		case ESECT:				/* End section */
			break;
		case COMMENT:			        /* We are done */
		case EOSTR:
			if(data->seen != ESECT && data->prev != MLINE) {
				/*
				 * Only returns entries where keyword is set.
				 */ 
				token_trim(entry);
				if(entry->key && strlen(entry->key)) {
					return PARSE_DONE;
				}
			}
			/*
			 * Jump to next line.
			 */
			return PARSE_NEXT;
			break;
		case CDATA:
		case WHITESP:
			if(data->curr == WHITESP && data->prev == MLINE) {
				if((inf->options & INIFILE_COMPACT_MLINE) == 0) {
					entry->val = putstr(entry->val, inf->str[data->pos]);
				}
				data->pos++;
				continue;        /* eat whitespace */
			}
			if(data->seen == BSECT) {
				data->cls = SECTION;
				entry->sect = putstr(entry->sect, inf->str[data->pos]);
			} else if(data->seen == ASSIGN ||
				  data->seen == QUOTE) {
				data->cls = VALUE;
				entry->val = putstr(entry->val, inf->str[data->pos]);
			} else {
				data->cls = KEYWORD;
				entry->key = putstr(entry->key, inf->str[data->pos]);
			}
			break;
		case ASSIGN:
			/*
			 * Allow assignment inside values.
			 */
			if(inf->options & INIFILE_ASSIGN_INSIDE) {
				if(data->seen == ASSIGN && data->cls == VALUE) {
					entry->val = putstr(entry->val, inf->str[data->pos]);
				}
			}
			break;
		case NONE:				/* Ignore */
		case QUOTE:				/* Ignore */
		case MLINE:
			break;
		}
		
		/*
		 * Save last seen token thats not CDATA.
		 */
		if(data->curr != CDATA && data->curr != WHITESP && data->curr != MLINE) {
			data->seen = data->curr;
		}
		data->prev = data->curr;
		
		/*
		 * Move to next char.
		 */
		data->pos++;
	}
	
	return PARSE_NEXT;
}
Beispiel #24
0
int com_show(LEXER *lp, char *arg)		/* define which kinds of things to display */
{
    UNUSED(arg);
    TOKEN token;
    int done=0;
    char *word;

    int visible=0;
    int modifiable=0;
    int comp=0;
    int show_layer=0;

    if (lp->mode != EDI) {
    	printf("No cell currently being edited!\n");
	token_flush_EOL(lp);
	return(1);
    }

    while(!done && (token=token_get(lp, &word)) != EOF) {
	switch(token) {
	    case OPT:		/* option */
		switch(toupper((unsigned char)word[0])) {
		    case '-':
		        visible=0;
		        modifiable=0;
		        break;
		    case '+':
		        visible=1;
		        modifiable=0;
		        break;
		    case '#':
		        visible=1;
		        modifiable=1;
		        break;
		    default:
		    	printf("SHOW: options start with one of {+-#}: %s\n", word);
			done++;
			token_flush_EOL(lp);
			break;
		    }

		    if (!done && strlen(word) > 1) {
			switch(toupper((unsigned char)word[1])) {
			   case 'E':
				comp=ALL;
				break;
			   case 'A':
				comp=ARC;
				break;
			   case 'C':
				comp=CIRC;
				break;
			   case 'I':
				comp=INST;
				break;
			   case 'L':
				comp=LINE;
				break;
			   case 'N':
				comp=NOTE;
				break;
			   case 'O':
				comp=OVAL;
				break;
			   case 'P':
				comp=POLY;
				break;
			   case 'R':
				comp=RECT;
				break;
			   case 'T':
				comp=TEXT;
				break;
			   default:
				printf("SHOW: bad component designator: %s\n", word);
				done++;
				token_flush_EOL(lp);
				break;
			}
		    }

		    if (!done) {
			if ((strlen(word) >= 3)) {
			    if(sscanf(&word[2], "%d", &show_layer) != 1 || 
				    show_layer > MAX_LAYER) {
				printf("SHOW: bad layer number: %s\n", word);
				done++;
				token_flush_EOL(lp);
			    } 
			} else {
			    show_layer=0;
			}
		    }
		    if (!done) {
			/* printf("setting comp %d, layer %d, visible %d, modify %d\n", comp,
			show_layer, visible, modifiable); */
			show_set_visible(currep, comp, show_layer, visible);
			show_set_modify(currep, comp, show_layer, modifiable);
		    }
	        break;

	    case CMD:		/* command */
		token_unget(lp, token, word);
		done++;
		break;
	    case EOC:		/* end of command */
		done++;
		break;
	    case EOL:		/* newline or carriage return */
	        break;
	    default:
		printf("SHOW: expected OPT, got %s %s\n", tok2str(token), word);
		done++;
	    	break;
	}
    }
    return (0);
}
Beispiel #25
0
int com_distance(LEXER *lp, char *arg)
{
    UNUSED(arg);
    enum {START,NUM1,NUM2,END} state = START;

    double x2,y2;

    int done=0;
    TOKEN token;
    char *word;
    int debug=0;

    while (!done) {
	token = token_look(lp,&word);
	if (debug) printf("got %s: %s\n", tok2str(token), word); 
	if (token==CMD) {
	    state=END;
	} 
	switch(state) {	
	case START:		/* get option or first xy pair */
	    if (token == OPT ) {
		token_get(lp,&word); /* ignore for now */
		state = START;
	    } else if (token == NUMBER) {
		state = NUM1;
	    } else if (token == EOL) {
		token_get(lp,&word); 	/* just eat it up */
		state = START;
	    } else if (token == EOC || token == CMD) {
		 state = END;
	    } else {
		token_err("DISTANCE", lp, "expected NUMBER", token);
		state = END;	/* error */
	    }
	    break;
	case NUM1:
	    if (token==NUMBER) {
		if (getnum(lp, "DISTANCE", &x1, &yy1)) {
		    rubber_set_callback(draw_dist);
		    state = NUM2;
		} else {
		    state = END;
		}
	    } else if (token == EOL) {	
		token_get(lp,&word); 	/* just ignore it */
	    } else if (token == EOC || token == CMD) {
		printf("DISTANCE: cancelling DISTANCE\n");
	        state = END;
	    } else {
		token_err("DISTANCE", lp, "expected NUMBER", token);
		state = END; 
	    }
	    break;
	case NUM2:
	    if (token==NUMBER) {
		if (getnum(lp, "DISTANCE", &x2, &y2)) {
		    printf("xy1=(%g,%g) xy2=(%g,%g) dx=%g, dy=%g, dxy=%g theta=%g (deg.)\n",
		    x1, yy1, x2, y2, fabs(x1-x2), fabs(yy1-y2),
		    sqrt(pow((x1-x2),2.0)+pow((yy1-y2),2.0)),
		    360.0*atan2(y2-yy1, x2-x1)/(2.0*M_PI));
		    rubber_clear_callback();
		    state = NUM1;
		} else {
		   state = END;
		}
	    } else if (token == EOL) {
		token_get(lp,&word); 	/* just ignore it */
	    } else if (token == EOC || token == CMD) {
		printf("DISTANCE: cancelling DISTANCE\n");
	        state = END;
	    } else {
		token_err("DISTANCE", lp, "expected NUMBER", token);
		state = END; 
	    }
	    break;
	case END:
	default:
	    if (token == EOC || token == CMD) {
		;
	    } else {
		token_flush_EOL(lp);
	    }
	    rubber_clear_callback();
	    done++;
	    break;
	}
    }
    return(1);
}
Beispiel #26
0
/*
 * option
 *   name <optionname> type <optiontype> <parameter...>
 *   <optiontype> =
 *     check
 *     spin
 *     combo
 *     button
 *     string
 *     filename
 *   <parameter> =
 *     default <x>
 *     min <x>
 *     max <x>
 *     var <x1> var <x2> ...
 */
static int
option_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;
	Tbl *tbl;
	int type;
	int num;

	if ((p = token_get(&token, s)) == NULL)
		return -1;
	if (!token_equal(&token, "name"))
		return -1;

	if ((p = token_get(&token, p)) == NULL)
		return -1;
	if (usimsg_pushstr(msg, USIOBJ_OPTNAME, token.s, token.len) != 0)
		return -1;

	if ((p = token_get(&token, p)) == NULL)
		return -1;
	if (!token_equal(&token, "type"))
		return -1;

	if ((p = token_get(&token, p)) == NULL)
		return -1;
	if ((tbl = tbl_find(tbl_option_type, &token)) == NULL)
		return -1;
	if (usimsg_pushnum(msg, USIOBJ_TYPE, tbl->id) != 0)
		return -1;
	type = tbl->id;

	while ((p = token_get(&token, p)) != NULL) {
		if ((tbl = tbl_find(tbl_option, &token)) == NULL)
			continue;
		switch (tbl->id) {
		case USIOBJ_DEFAULT:
			switch (type) {
			case USIOPT_CHECK:
				if ((p = token_get(&token, p)) == NULL)
					return -1;
				if (token_equal(&token, "true"))
					num = 1;
				else if (token_equal(&token, "false"))
					num = 0;
				else
					return -1;
				if (usimsg_pushnum(msg, tbl->id, num) != 0)
					return -1;
				break;
			case USIOPT_SPIN:
				if ((p = token_get(&token, p)) == NULL)
					return -1;
				num = strtol(token.s, NULL, 10);
				if (usimsg_pushnum(msg, tbl->id, num) != 0)
					return -1;
				break;
			case USIOPT_BUTTON:
				break;
			case USIOPT_COMBO:
			case USIOPT_STRING:
			case USIOPT_FILENAME:
				if ((p = token_get(&token, p)) == NULL)
					return -1;
				if (token_equal(&token, "<empty>"))
					token.len = 0;
				if (usimsg_pushstr(msg, tbl->id,
						token.s, token.len) != 0)
					return -1;
				break;
			default:
				break;
			}
			break;
		case USIOBJ_MIN:
		case USIOBJ_MAX:
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			num = strtol(token.s, NULL, 10);
			if (usimsg_pushnum(msg, tbl->id, num) != 0)
				return -1;
			break;
		case USIOBJ_STRING:
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			if (usimsg_pushstr(msg, tbl->id,
					token.s, strlen(token.s)) != 0)
				return -1;
			break;
		default:
			break;
		}
	}

	return 0;
}
void target_os_features(char *os)
{
    /* target OS */
    if (!strcmp(os, "LINUX_24"))
	token_set_y("CONFIG_RG_OS_LINUX_24");
    else if (!strcmp(os, "LINUX_26"))
	token_set_y("CONFIG_RG_OS_LINUX_26");
    else if (!strcmp(os, "ECOS"))
	token_set_y("CONFIG_RG_OS_ECOS");
    else if (!strcmp(os, "VXWORKS"))
	token_set_y("CONFIG_RG_OS_VXWORKS");

    /* VxWorks special behaviour */
    if (token_get("CONFIG_RG_OS_VXWORKS"))
    {
	token_set_y("CONFIG_RG_NOT_UNIX");
	token_set_y("CONFIG_RG_EFSS");
        if (token_get("CONFIG_RG_DEV"))
	    token_set_y("CONFIG_VX_SYMTBL");
	token_set_y("CONFIG_RG_VX_TIMERS_TASK");
    }

    /* Linux Kernel generic configs */
    if (token_get("CONFIG_RG_OS_LINUX_26"))
    {
	token_set_y("CONFIG_RG_OS_LINUX");

	/* Generic kernel */
	
	token_set("CONFIG_INIT_ENV_ARG_LIMIT", "32");
	token_set_y("CONFIG_PRINTK");
	token_set("CONFIG_LOG_BUF_SHIFT", "14");
	token_set_y("CONFIG_BUG");
	if (!token_get("CONFIG_FEROCEON_COMMON"))
	    token_set_y("CONFIG_TINY_SHMEM");
	token_set("CONFIG_BASE_SMALL", "0");
	token_set_y("CONFIG_GENERIC_CALIBRATE_DELAY");
	if (!token_get("CONFIG_COMCERTO_COMMON") &&
	    !token_get("CONFIG_ARCH_SOLOS") &&
	    !token_get("CONFIG_FEROCEON_COMMON"))
	{
	    token_set_y("CONFIG_GENERIC_HARDIRQS");
	}
	token_set_y("CONFIG_SYSCTL");
	token_set_y("CONFIG_BINFMT_ELF");
	token_set_y("CONFIG_CRC32");
	token_set_y("CONFIG_MMU");
	token_set_y("CONFIG_IOSCHED_NOOP");
	token_set_y("CONFIG_IOSCHED_AS");
	token_set_y("CONFIG_IOSCHED_DEADLINE");
	token_set_y("CONFIG_IOSCHED_CFQ");
	token_set_y("CONFIG_SYSVIPC");
	token_set("CONFIG_DEFAULT_IOSCHED", "\"anticipatory\"");
	token_set_y("CONFIG_SLAB");

	/* Pseudo_filesystems */
	
	token_set_y("CONFIG_RAMFS");

	if (!token_get("CONFIG_RG_RGLOADER"))
	{
	    token_set_y("CONFIG_PROC_FS");
	    token_set_y("CONFIG_SYSFS");
	    token_set_m("CONFIG_RG_IPV4");
	}

	/* Modules support */
	
	token_set_y("CONFIG_MODULES");
	if (!token_get("CONFIG_ARCH_SOLOS"))
	    token_set_y("CONFIG_MODULE_UNLOAD");
	token_set_y("CONFIG_OBSOLETE_MODPARM");
	token_set_y("CONFIG_KMOD");

	/* Networking (recheck) */
	
	token_set_y("CONFIG_NET");
	token_set_y("CONFIG_NETDEVICES");
	token_set_y("CONFIG_PACKET");
	token_set_y("CONFIG_UNIX");
	token_set_y("CONFIG_INET");
	token_set_y("CONFIG_IP_MULTICAST");
	token_set_y("CONFIG_IP_FIB_HASH");
	token_set_y("CONFIG_IP_ADVANCED_ROUTER");
	token_set_y("CONFIG_NET_ETHERNET");
	token_set_y("CONFIG_IP_MULTIPLE_TABLES");
	token_set_y("CONFIG_XFRM");
	token_set_y("CONFIG_NET_SCH_CLK_JIFFIES");

	/* PTY */
	token_set_y("CONFIG_LEGACY_PTYS");
	token_set("CONFIG_LEGACY_PTY_COUNT", "256");

	/* Override only if LZMA is not fast enough */
	token_set("CONFIG_RG_KERNEL_COMP_METHOD", "lzma");
    }

    if (token_get("CONFIG_RG_OS_LINUX_24"))
    {
	token_set_y("CONFIG_UID16");
	token_set_y("CONFIG_RWSEM_GENERIC_SPINLOCK");
	token_set_y("CONFIG_IP_MULTICAST");
	token_set_y("CONFIG_IP_ADVANCED_ROUTER");
	token_set_y("CONFIG_UNIX");
	token_set_y("CONFIG_RG_OS_LINUX");
	token_set_y("CONFIG_INET");
	token_set_y("CONFIG_MODULES");
	token_set_y("CONFIG_NET");
	token_set_y("CONFIG_NETDEVICES");
	token_set_y("CONFIG_NETLINK_DEV");
	token_set_y("CONFIG_FILTER");
	token_set_y("CONFIG_NET_ETHERNET");
	token_set_y("CONFIG_PACKET");
	token_set_y("CONFIG_SYSVIPC");

	if (!token_get("CONFIG_RG_RGLOADER"))
	{
	    token_set_y("CONFIG_PROC_FS");
	    token_set_m("CONFIG_RG_IPV4");
	}
	
	/* Override only if LZMA is not fast enough */
	token_set("CONFIG_RG_KERNEL_COMP_METHOD", "lzma");
    }
	
    if (token_get("CONFIG_RG_OS_LINUX"))
	token_set_y("CONFIG_RG_TTYP");

    /* Set CONFIG_RG_TARGET_xxx */
    if (token_get("CONFIG_RG_OS_LINUX"))
	token_set_y("CONFIG_RG_TARGET_LINUX");
    if (token_get("CONFIG_RG_OS_VXWORKS"))
	token_set_y("CONFIG_RG_TARGET_VXWORKS");
}
Beispiel #28
0
/*
 * info
 *   depth <x>
 *   seldepth <x>
 *   time <x>
 *   nodes <x>
 *   pv <move1> ... <movei>
 *   score
 *     cp <x>
 *     mate <y>
 *   currmove <move>
 *   hashfull <x>
 *   nps <x>
 *   string <str>
 */
static int
info_parse(Usimsg *msg, const char *s)
{
	const char *p;
	Token token;
	int type;
	int key;
	int num;

	p = s;
	while ((p = token_get(&token, p)) != NULL) {
		if (token_equal(&token, "depth")) {
			type = USITYPE_NUM;
			key = USIOBJ_DEPTH;
		} else if (token_equal(&token, "seldepth")) {
			type = USITYPE_NUM;
			key = USIOBJ_SELDEPTH;
		} else if (token_equal(&token, "time")) {
			type = USITYPE_NUM;
			key = USIOBJ_TIME;
		} else if (token_equal(&token, "nodes")) {
			type = USITYPE_NUM;
			key = USIOBJ_NODES;
		} else if (token_equal(&token, "pv")) {
			type = USITYPE_OCT;
			key = USIOBJ_PV;
		} else if (token_equal(&token, "score")) {
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			if (token_equal(&token, "cp")) {
				type = USITYPE_NUM;
				key = USIOBJ_SCORE_CP;
			} else if (token_equal(&token, "mate")) {
				type = USITYPE_NUM;
				key = USIOBJ_SCORE_MATE;
			} else {
				/* rollback */
				p = token.s;
				continue;
			}
		} else if (token_equal(&token, "currmove")) {
			type = USITYPE_STR;
			key = USIOBJ_CURRMOVE;
		} else if (token_equal(&token, "hashfull")) {
			type = USITYPE_NUM;
			key = USIOBJ_HASHFULL;
		} else if (token_equal(&token, "nps")) {
			type = USITYPE_NUM;
			key = USIOBJ_NPS;
		} else if (token_equal(&token, "string")) {
			type = USITYPE_STR;
			key = USIOBJ_STRING;
		} else {
			continue;
		}

		switch (type) {
		case USITYPE_NUM:
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			if (*token.s == '-' && token.len == 1)
				num = 1 << 31;
			else
				num = strtol(token.s, NULL, 10);
			if (usimsg_pushnum(msg, key, num) != 0)
				return -1;
			break;
		case USITYPE_STR:
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			if (usimsg_pushstr(msg, key, token.s, token.len) != 0)
				return -1;
			break;
		case USITYPE_OCT:
			if ((p = token_get(&token, p)) == NULL)
				return -1;
			if (usimsg_pushstr(msg, key, token.s,
					strlen(token.s)) != 0)
				return -1;
			break;
		default:
			break;
		}
	}

	return 0;
}
Beispiel #29
0
/* Traverse <buffer>, extracting tokens. Build nodes from tokens, and add to <parent> as fit. Recurse. */
static XmlNode * tree_build(XmlNode *parent, const char **buffer, int *complete)
{
	DynStr	*token = NULL;

	if(complete == NULL)
	{
		static int	fake;

		complete = &fake;
	}

	*complete = 1;

	for(; *complete && **buffer;)
	{
		TokenStatus	st;

		*buffer = token_get(*buffer, &token, &st);
		if(st == ERROR)
		{
			LOG_WARN(("XML parse error detected, aborting"));
			*complete = 0;
			return parent;
		}
		if(token != NULL)
		{
			if(st == TAG || st == TAGEMPTY)
			{
				const char	*tag = dynstr_string(token);

				if(tag[0] == '?' || strncmp(tag, "!--", 3) == 0)
					dynstr_truncate(token, 0);
				else if(tag[0] == '/')
				{
					if(node_closes(parent, tag))
					{
						dynstr_destroy(token, 1);
						return parent;
					}
					LOG_ERR(("Element nesting error in XML source, <%s> vs <%s>--aborting", tag, parent->element));
					*complete = 0;
					return parent;
				}
				else
				{
					XmlNode	*child = NULL, *subtree = NULL;

					child = node_new(tag);
					dynstr_destroy(token, 1);
					token = NULL;

					if(st == TAGEMPTY && strcmp(xmlnode_get_name(child), "xi:include") == 0)	/* Use of xi:include? */
					{
						XmlNode	*inc;

						if((inc = do_include(xmlnode_attrib_get_value(child, "href"))) != NULL)
						{
							xmlnode_destroy(child);
							child = inc;
						}
					}
					
					if(st != TAGEMPTY)
						subtree = tree_build(child, buffer, complete);
					else
						subtree = child;
					if(parent != NULL && subtree != NULL)
						node_child_add(parent, subtree);
					else
						parent = child;
				}
			}
			else if(st == TEXT)
			{
				dynstr_trim(token);
				if(dynstr_length(token) > 0)
				{
					if(parent != NULL)
						node_text_add(parent, token);
					else
					{
						LOG_WARN(("Ignoring top-level text"));
						dynstr_destroy(token, 1);
					}
					token = NULL;
				}
				else	/* If the text disappeared, it was all whitespace. Free the token. */
				{
					dynstr_destroy(token, 1);
					token = NULL;
				}
			}
			else if(st == COMMENT)		/* Ignore this comment. */
			{
				dynstr_destroy(token, 1);
				token = NULL;
			}
		}
		else
			break;
	}
	return parent;
}
Beispiel #30
0
/*
 * Parse a term (no operators).
 */
static bool parse_term_head(context_t cxt, term_t *val)
{
    char *tokstr = (char *)gc_malloc(TOKEN_MAXLEN+1);
    term_t tokval;
    token_t tok = token_get(cxt, &tokval, tokstr);

    unsigned priority;
    if (parse_maybe_op(tok) &&
        unop_lookup(cxt->opinfo, tokstr, &priority, NULL))
    {
        term_t lval;
        if (!parse_term_op(cxt, &lval, priority))
        {
            gc_free(tokstr);
            return false;
        }
        atom_t atom = make_atom(gc_strdup(tokstr), 1);
        func_t f = make_func(atom, lval);
        tokval = term_func(f);
        *val = tokval;
        gc_free(tokstr);
        return true;
    }

    bool ok = true;
    term_t *args = NULL;
    switch ((int)tok)
    {
        case '(':
            if (!parse_term_op(cxt, val, UINT32_MAX) || !token_expect(cxt, ')'))
                ok = false;
            break;
        case TOKEN_NIL: case TOKEN_BOOLEAN: case TOKEN_ATOM: case TOKEN_STRING:
        case TOKEN_NUMBER:
            *val = tokval;
            break;
        case TOKEN_VARIABLE:
        {
            if (token_peek(cxt, NULL, NULL) != '(')
            {
                // Really is a variable:
                *val = tokval;
                break;
            }
            // Otherwise this is a functor:
            var_t x = var(tokval);
            atom_t atom = make_atom(x->name, 0);
            if (!token_expect(cxt, '('))
            {
                ok = false;
                break;
            }
            args = (term_t *)gc_malloc(MAX_ARGS * sizeof(term_t));
            uint_t a = 0;
            if (token_peek(cxt, NULL, NULL) == ')')
                token_get(cxt, NULL, NULL);
            else
            {
                while (true)
                {
                    ok = parse_term_op(cxt, &tokval, UINT32_MAX);
                    if (!ok)
                        break;
                    if (a >= MAX_ARGS)
                    {
                        parse_error(cxt, "too many arguments; maximum is %zu",
                            MAX_ARGS);
                        ok = false;
                        break;
                    }
                    args[a++] = tokval;
                    tok = token_get(cxt, NULL, tokstr);
                    if (tok == ',')
                        continue;
                    if (tok == ')')
                        break;
                    parse_error(cxt, "expected token `,' or `)'; got token "
                        "`%s'", tokstr);
                    ok = false;
                    break;
                }
                if (!ok)
                    break;
            }
            atom = atom_set_arity(atom, a);
            func_t f = make_func_a(atom, args);
            tokval = term_func(f);
            *val = tokval;
            break;
        }
        default:
            if (tok != TOKEN_ERROR)
                parse_error(cxt, "unexpected token `%s'", tokstr);
            ok = false;
            break;
    }
    gc_free(tokstr);
    gc_free(args);
    return ok;
}