Beispiel #1
0
/* Read two ssize_t's, separated by a comma, from str, and store them in
 * *line and *column (if they're not both NULL).  Return FALSE on error,
 * or TRUE otherwise. */
bool parse_line_column(const char *str, ssize_t *line, ssize_t *column)
{
    bool retval = TRUE;
    const char *comma;

    assert(str != NULL);

    comma = strchr(str, ',');

    if (comma != NULL && column != NULL) {
	if (!parse_num(comma + 1, column))
	    retval = FALSE;
    }

    if (line != NULL) {
	if (comma != NULL) {
	    char *str_line = mallocstrncpy(NULL, str, comma - str + 1);
	    str_line[comma - str] = '\0';

	    if (str_line[0] != '\0' && !parse_num(str_line, line))
		retval = FALSE;

	    free(str_line);
	} else if (!parse_num(str, line))
	    retval = FALSE;
    }

    return retval;
}
Beispiel #2
0
int parse_hunk_header(char *line, int len,
		      int *ob, int *on,
		      int *nb, int *nn)
{
	char *cp;
	cp = line + 4;
	if (parse_num(&cp, ob)) {
	bad_line:
		return error("malformed diff output: %s", line);
	}
	if (*cp == ',') {
		cp++;
		if (parse_num(&cp, on))
			goto bad_line;
	}
	else
		*on = 1;
	if (*cp++ != ' ' || *cp++ != '+')
		goto bad_line;
	if (parse_num(&cp, nb))
		goto bad_line;
	if (*cp == ',') {
		cp++;
		if (parse_num(&cp, nn))
			goto bad_line;
	}
	else
		*nn = 1;
	return -!!memcmp(cp, " @@", 3);
}
Beispiel #3
0
uforth_stat uforth_interpret(char *str) {
  uforth_stat stat;
  char *word;
  CELL wd_idx;
  char immediate = 0;
  char primitive = 0;

  uforth_iram->inbufptr = str;
  while(*(word = uforth_next_word()) != 0) {
    wd_idx = find_word(word,uforth_iram->currwordlen,0,&immediate,&primitive);
    switch (uforth_iram->compiling) {
    case 0:                     /* interpret mode */
      if (wd_idx == 0) {        /* number or trash */
        DCELL num = parse_num(word,uforth_uram->base);
        if (num == 0 && word[0] != '0') {
          uforth_abort_request(ABORT_NAW);
          uforth_abort();
          return E_NOT_A_WORD;
        }
        if (abs32(num) > (int32_t)MAX_CELL_NUM){
          dpush32(num);
        } else {
          dpush(num);
        }
      } else {
        stat = exec(wd_idx,primitive,uforth_uram->ridx-1);
        if (stat != OK) {
          uforth_abort();
          uforth_abort_clr();
          return stat;
        }
      }
      break;
    case 1:                     /* in the middle of a colon def */
      if (wd_idx == 0) {        /* number or trash */
        DCELL num = parse_num(word,uforth_uram->base);
        if (num == 0 && word[0] != '0') {
          uforth_abort_request(ABORT_NAW);
          uforth_abort();
          dict_end_def();
          return E_NOT_A_WORD;
        }
        /* OPTIMIZATION: Only DLIT big numbers */
        if (num < 0 || abs32(num) > (int32_t)MAX_CELL_NUM){
          dict_append(DLIT);
          dict_append(((uint32_t)num)>>16);
          dict_append(((uint16_t)num)&0xffff);
        } else {
          dict_append(LIT);
          dict_append(num);
        }
      } else if (word[0] == ';') { /* exit from a colon def */
Beispiel #4
0
/* int parse_real(char *str, double *v)
*   check whether str[] starts with a number, or number/number. If yes,
*   store the value in *v and return the length of the matching part.
*   Return value is the same as for the previous routine.
*/
static int parse_real(char *str, double *v)
{int i,ret; double d;
    i=0; if(*str==' '){ str++; i++; }
    ret=parse_num(str,v);
    if(ret<=0) return ret; // 0 or -1
    i+=ret; str+=ret;
    if(*str=='/'){
        str++; i++; d=*v;
        ret=parse_num(str,v);
        if(ret<=0) return -1; // syntax error
        i+=ret; *v = d/(*v);
    }
    return i;
}
Beispiel #5
0
static void act_width (void) {
    char buffer[80];
    char prompt[80];
    fileoffset_t w;
    fileoffset_t new_top;
    int error;

    sprintf (prompt, "Enter screen width in bytes (now %"OFF"d): ", width);
    if (!get_str (prompt, buffer, FALSE))
	return;
    w = parse_num (buffer, &error);
    if (error) {
	display_beep();
	strcpy (message, "Unable to parse width value");
	return;
    }
    if (w > 0) {
	width = w;
	fix_offset();
	new_top = cur_pos - (scrlines-1) * width;
	new_top = begline(new_top);
	if (top_pos < new_top)
	    top_pos = new_top;
    }
}
Beispiel #6
0
static void act_goto (void) {
    char buffer[80];
    fileoffset_t position, new_top;
    int error;

    if (!get_str("Enter position to go to: ", buffer, FALSE))
	return;			       /* user break */

    position = parse_num (buffer, &error);
    if (error) {
	display_beep();
	strcpy (message, "Unable to parse position value");
	return;
    }

    if (position < 0 || position > file_size) {
	display_beep();
	strcpy (message, "Position is outside bounds of file");
	return;
    }

    cur_pos = position;
    edit_type = !!edit_type;
    new_top = cur_pos - (scrlines-1) * width;
    if (new_top < 0)
	new_top = 0;
    new_top = begline(new_top);
    if (top_pos > cur_pos)
	top_pos = begline(cur_pos);
    if (top_pos < new_top)
	top_pos = new_top;
}
Beispiel #7
0
static int
parse_cryptofunction(ocra_suite * ocra, const char *in)
{
	int ret, l;
	char *token, *string, *tofree;

	if (NULL == (tofree = string = strdup(in)))
		return RFC6287_ERR_POSIX;

	if ((NULL == (token = strsep(&string, "-"))) ||
	    (0 != strcmp(token, "HOTP")) ||
	    (NULL == (token = strsep(&string, "-"))) ||
	    (none == (ocra->hotp_alg = parse_alg(token))) ||
	    (NULL == (token = strsep(&string, "-"))) ||
	    (2 < (l = strlen(token))) ||
	    (-1 == (ocra->hotp_trunc = parse_num(token))) ||
	    ((0 != ocra->hotp_trunc) &&
	    ((4 > ocra->hotp_trunc) || (11 < ocra->hotp_trunc))) ||
	    ((10 > ocra->hotp_trunc) && (2 == l)))
		ret = RFC6287_INVALID_SUITE;
	else
		ret = 0;
	free(tofree);
	return ret;
}
int
get_second_value(longint_t vars[], char *rhsarg,
			longint_t *second_value) {
	char *p;
	int varnum2;
	if (strchr(NUMCHRS, *rhsarg) != NULL ||
				strchr(SGNCHRS, *rhsarg) != NULL) {
		/* first character is a digit or a sign, so RHS 
		 * should be a number
		 */
		p = rhsarg+1;
		while (*p) {
			if (strchr(NUMCHRS, *p) == NULL) {
				/* nope, found an illegal character */
				return ERROR;
			}
			p++;
		}
		*second_value = parse_num(rhsarg);
		return !ERROR;
	} else {
		/* argument is not a number, so might be a variable */
		varnum2 = to_varnum(*rhsarg);
		if (varnum2==ERROR || strlen(rhsarg)!=1) {
			/* nope, not a variable either */
			return ERROR;
		}
		/* is a variable, so can use its value to assign to
		 * second_value
		 */
		*second_value = vars[varnum2];
		return !ERROR;
	}
	return ERROR;
}
Beispiel #9
0
static struct expr *
parse_timecmp()
{
	enum prop prop;
	enum op op;

	if (token("atime"))
		prop = PROP_ATIME;
	else if (token("ctime"))
		prop = PROP_CTIME;
	else if (token("mtime"))
		prop = PROP_MTIME;
	else if (token("date"))
		prop = PROP_DATE;
	else
		return parse_cmp();

	op = parse_op();
	if (!op)
		parse_error("invalid comparison at '%.15s'", pos);

	int64_t n;
	if (parse_num(&n) || parse_dur(&n)) {
		struct expr *e = mkexpr(op);
		e->a.prop = prop;
		e->b.num = n;
		return e;
	}

	return 0;
}
Beispiel #10
0
static void act_offset (void) {
    char buffer[80];
    char prompt[80];
    fileoffset_t o;
    fileoffset_t new_top;
    int error;

    sprintf (prompt, "Enter start-of-file offset in bytes (now %"OFF"d): ",
	     realoffset);
    if (!get_str (prompt, buffer, FALSE))
	return;
    o = parse_num (buffer, &error);
    if (error) {
	display_beep();
	strcpy (message, "Unable to parse offset value");
	return;
    }
    if (o >= 0) {
	realoffset = o;
	fix_offset();
	new_top = cur_pos - (scrlines-1) * width;
	new_top = begline(new_top);
	if (top_pos < new_top)
	    top_pos = new_top;
    }
}
Beispiel #11
0
/* parses a line of the file
 * tries to set the corresponding row in the matrix
 * returns false on error
 */
bool parse_row(char* s, int row, LinearProgram* lp) {
    assert(lp_is_valid(lp));
    assert(row >= 0);
    assert(row < get_rows(lp));

    char* end_ptr;
    int cols = get_cols(lp);

    int i;
    for (i = 0; i < cols; i++) {
        num_t num = parse_num(s, &end_ptr);

        if (!is_num_valid(num, s, end_ptr)) {
            return false;
        }

        set_coef(lp, row, i, num);
        s = end_ptr;
    }


    s = parse_type(s, row, lp);

    if (NULL == s) {
        return false;
    }

    num_t num = parse_num(s, &end_ptr);
    if (!is_num_valid(num, s, end_ptr)) {
        return false;
    }
    s = end_ptr;

    s = skip_spaces(s);

    if ('\0' != *s) {
        return false;
    }

    set_rhs(lp, row, num);

    assert(lp_is_valid(lp));
    return true;
}
static void sendhup(char *str)
{
	int pid;

	if (parse_num(str, "PPPD_PID=", &pid) && pid != getpid()) {
		if (debug)
			dbglog("sending SIGHUP to process %d", pid);
		kill(pid, SIGHUP);
	}
}
Beispiel #13
0
Sophon_Result
sophon_time_parse (Sophon_VM *vm, Sophon_String *str, Sophon_Time *time)
{
	Sophon_Char *ptr;
	Sophon_Date date;
	Sophon_Result r;

	SOPHON_ASSERT(str && time);

	sophon_memset(&date, 0, sizeof(date));

	ptr = sophon_string_chars(vm, str);
	while (*ptr && sophon_isspace(*ptr))
		ptr++;

	if ((r = parse_num(vm, ptr, &date.year)) < 0)
		return r;
	date.year -= 1900;
	ptr += r;
	if (*ptr++ != '-')
		return SOPHON_ERR_PARSE;
	if ((r = parse_num(vm, ptr, &date.mon)) < 0)
		return r;
	date.mon--;
	ptr += r;
	if (*ptr++ != '-')
		return SOPHON_ERR_PARSE;
	if ((r = parse_num(vm, ptr, &date.mday)) < 0)
		return r;
	ptr += r;

	if ((*ptr == 'T') || (*ptr == ' ')) {
		ptr++;
		if ((r = parse_num(vm, ptr, &date.hour)) < 0)
			return r;
		ptr += r;
		if (*ptr++ != ':')
			return SOPHON_ERR_PARSE;
		if ((r = parse_num(vm, ptr, &date.min)) < 0)
			return r;
		ptr += r;
		if (*ptr++ != ':')
			return SOPHON_ERR_PARSE;
		if ((r = parse_num(vm, ptr, &date.sec)) < 0)
			return r;
		ptr += r;
		if (*ptr++ == '.') {
			if ((r = parse_num(vm, ptr, &date.msec)) < 0)
				return r;
			ptr += r;
		}
	} else {
		date.mday = 1;
	}

	return sophon_date_to_time(vm, &date, time);
}
Beispiel #14
0
/* parses a line of the file
 * tries to set the corresponding row in the matrix
 * returns false on error
 */
bool parse_row(char* s, int row, LinearProgram* lp) {
    assert(lp_is_valid(lp));
    assert(row >= 0);
    assert(row < lp->rows);

    int i;
    char* end_ptr;
    for (i = 0; i < lp->cols; i++) {
        num_t num = parse_num(s, &end_ptr);

        if (!is_num_valid(num, s, end_ptr)) {
            return false;
        }

        lp->matrix[row][i] = num;
        s = end_ptr;
    }


    s = parse_type(s, row, lp);

    if (NULL == s) {
        return false;
    }

    num_t num = parse_num(s, &end_ptr);
    if (!is_num_valid(num, s, end_ptr)) {
        return false;
    }
    s = end_ptr;

    s = skip_spaces(s);

    if ('\0' != *s) {
        return false;
    }

    lp->vector[row] = num;

    assert(lp_is_valid(lp));
    return true;
}
Beispiel #15
0
/*----------------------------------------------------------------------
 * Write this byte into current packet
 */
static void
write_byte (const char *str)
{
    guint32 num;

    num = parse_num(str, FALSE);
    packet_buf[curr_offset] = (guint8) num;
    curr_offset ++;
    if (curr_offset >= max_offset) /* packet full */
        start_new_packet();
}
Beispiel #16
0
/*----------------------------------------------------------------------
 * Write this byte into current packet
 */
static void
write_byte (const char *str)
{
    unsigned long num;

    num = parse_num(str, FALSE);
    packet_buf[curr_offset] = (unsigned char) num;
    curr_offset ++;
    if (curr_offset >= max_offset) /* packet full */
        start_new_packet();
}
Beispiel #17
0
static void parse_packet(indexer_dv_context *This, unsigned char *p)
{
	indexer_dv_bitstream b;
	int type = p[0];

	b = bitstream_new(p + 1);

	switch (type) {
		case 0x62: /* Record date */
			parse_num(&b, 8);
			This->rec_curr_day = parse_bcd(&b, 6);
			parse_num(&b, 2);
			This->rec_curr_month = parse_bcd(&b, 5);
			parse_num(&b, 3);
			This->rec_curr_year = parse_bcd(&b, 8);
			if (This->rec_curr_year < 25) {
				This->rec_curr_year += 2000;
			}
			else {
				This->rec_curr_year += 1900;
			}
			This->got_record_date = 1;
			break;
		case 0x63: /* Record time */
			This->rec_curr_frame = parse_bcd(&b, 6);
			parse_num(&b, 2);
			This->rec_curr_second = parse_bcd(&b, 7);
			parse_num(&b, 1);
			This->rec_curr_minute = parse_bcd(&b, 7);
			parse_num(&b, 1);
			This->rec_curr_hour = parse_bcd(&b, 6);
			This->got_record_time = 1;
			break;
	}
}
Beispiel #18
0
bool
parse_baud(char *s, unsigned long *val, char **es, char *delim)
{
        if (!parse_num(s, val, es, delim)) {
            return false;
        }

        if ((*val == 9600) || (*val == 19200) || (*val == 38400) ||
            (*val == 57600) || (*val == 115200))
            return true;
        else
            return false;
}
Beispiel #19
0
//parser
cellpoint read_parse(char *ibuf, char **pscan)
{
	current_state = STATE_INIT;
    stack_init(&parser_stack);
    stack_init(&state_stack);

    while (*pscan == NULL || **pscan == '\0'){
        *pscan = reload_ibuffer(ibuf);
    }

    while (1){
        if (current_state == STATE_INIT && !stack_is_empty(&parser_stack)){
            //parses a object
            return stack_pop(&parser_stack);
        }

		if (current_state == STATE_INIT){
            do_state_init(ibuf, pscan);
		}else if (current_state == STATE_SHARP){
            do_state_sharp(ibuf, pscan);
		}else if (current_state == STATE_DOT){
            do_state_dot(pscan);
		}else if (current_state == STATE_ADD){
            do_state_add(pscan);
		}else if (current_state == STATE_SUB){
            do_state_sub(pscan);
		}else if (current_state == STATE_NUM){
			//parses number object
            parse_num(pscan);
		}else if (current_state == STATE_CHAR){
            //parses character object
            parse_char(pscan);
		}else if (current_state == STATE_STR){
            //parses string object
            parse_string(ibuf, pscan);
		}else if (current_state == STATE_SYM){
			//parses symbol object
            parse_sym(pscan);
		}else if (current_state == STATE_LIST){
			//parses list object
            parse_list(ibuf, pscan);
		}else if (current_state == STATE_VEC){
			//parses vector object
            parse_vector(ibuf, pscan);
		}else {
            perror("Error: Bad state. -- READ\n");
			error_handler();
            return NIL;
        }
    }
}
Beispiel #20
0
Datei: v7.c Projekt: di3online/v7
//  factor  =   number | string_literal | "(" expression ")" |
//              variable | "this" | "null" | "true" | "false" |
//              "{" object_literal "}" |
//              "[" array_literal "]" |
//              function_definition |
//              function_call
static enum v7_err parse_factor(struct v7 *v7) {
  int old_sp = v7_sp(v7);

  if (*v7->cursor == '(') {
    TRY(match(v7, '('));
    TRY(parse_expression(v7));
    TRY(match(v7, ')'));
  } else if (*v7->cursor == '\'' || *v7->cursor == '"') {
    TRY(parse_string_literal(v7));
  } else if (*v7->cursor == '{') {
    TRY(parse_object_literal(v7));
  } else if (is_alpha(*v7->cursor) || *v7->cursor == '_') {
    TRY(parse_identifier(v7));
    if (test_token(v7, "this", 4)) {
      inc_stack(v7, 1);
      v7_top(v7)[-1] = &v7->scopes[v7->current_scope];
    } else if (test_token(v7, "null", 4)) {
      TRY(v7_make_and_push(v7, V7_NULL));
    } else if (test_token(v7, "true", 4)) {
      TRY(v7_make_and_push(v7, V7_BOOL));
      v7_top(v7)[-1]->v.num = 1;
    } else if (test_token(v7, "false", 5)) {
      TRY(v7_make_and_push(v7, V7_BOOL));
      v7_top(v7)[-1]->v.num = 0;
    } else if (test_token(v7, "function", 8)) {
      TRY(parse_function_definition(v7, NULL, 0));
    } else if (test_token(v7, "delete", 6)) {
      TRY(parse_delete(v7));
    } else {
      TRY(parse_variable(v7));
    }
  } else {
    TRY(parse_num(v7));
  }

  if (*v7->cursor == '(') {
    TRY(parse_function_call(v7));
  }

  // Don't leave anything on stack if no execution flag is set
  if (v7->no_exec) {
    inc_stack(v7, old_sp - v7->sp);
  }

  return V7_OK;
}
Beispiel #21
0
object_t *parse_sexp(FILE *fp){
  char buf;
  buf=skip_space_getchar(fp);

  ungetc(buf,fp);

  if(isalpha(buf) || buf=='+' || buf=='*' || buf=='-'){
    return parse_sym(fp);
  }
  else if(isdigit(buf)){
    return parse_num(fp);
  }
  else if(buf=='('){
    return parse_list(fp);
  }
  return (object_t*)NULL;
}
Beispiel #22
0
char *eval (const char *expr)
{
	char result[256], *expr_value;
	int value;

	int session = StartSPASMErrorSession();
	bool fResult = parse_num (expr, &value);
	EndSPASMErrorSession(session);
	if (fResult)
	{
		sprintf (result, "$%0X", value);
		expr_value = strdup (result);
	}
	else
	{
		expr_value = expand_expr (expr);
	}
	return expr_value;
}
Beispiel #23
0
static struct expr *
parse_cmp()
{
	enum prop prop;
	enum op op;

	if (token("depth"))
		prop = PROP_DEPTH;
	else if (token("kept"))
		prop = PROP_KEPT;
	else if (token("index"))
		prop = PROP_INDEX;
	else if (token("replies")) {
		prop = PROP_REPLIES;
		need_thr = 1;
	} else if (token("size"))
		prop = PROP_SIZE;
	else if (token("total"))
		prop = PROP_TOTAL;
	else
		return parse_flag();

	if (!(op = parse_op()))
		parse_error("invalid comparison at '%.15s'", pos);

	int64_t n;
	if (parse_num(&n)) {
		struct expr *e = mkexpr(op);
		e->a.prop = prop;
		e->b.num = n;
		return e;
	} else if (token("cur")) {
		struct expr *e = mkexpr(op);
		e->a.prop = prop;
		e->b.var = VAR_CUR;
		e->extra = 1;
		return e;
	}

	return 0;
}
Beispiel #24
0
static void
do_channel(int argc, char *argv[])
{
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);

    if (argc == 2) { 
#ifdef CYGPKG_REDBOOT_ANY_CONSOLE
        if (strcmp( argv[1], "-1") == 0) {
            console_selected = false;
            console_echo = true;
        } else 
#endif
        {
            unsigned long chan;
            if ( !parse_num( argv[1], &chan, NULL, NULL) ) {
                diag_printf("** Error: invalid channel '%s'\n", argv[1]);
            } else {
                if (chan < CYGNUM_HAL_VIRTUAL_VECTOR_NUM_CHANNELS) {
                    CYGACC_CALL_IF_SET_CONSOLE_COMM(chan);
                    CYGACC_CALL_IF_SET_DEBUG_COMM(chan);
                    if (chan != cur)
                        console_echo = true;
                }
                else {
                    diag_printf("**Error: bad channel number '%s'\n", argv[1]);
                }
            }
        }
    }
    /* else display */ 
    else {
        diag_printf("Current console channel id: ");
#ifdef CYGPKG_REDBOOT_ANY_CONSOLE
        if (!console_selected)
            diag_printf("-1\n");
        else
#endif
            diag_printf("%d\n", cur);
    }
}
static int spmtwam_process_argument(const char *arg, int len)
{
	unsigned int event;

	if (start == 1) {
		reset_driver_stat();
	}

	if (strncmp(arg, "clock:", 6) == 0) {
		if (strncmp(&(arg[6]), "speed", 5) == 0) {
			twam_clock_mode = true;
		} else if (strncmp(&(arg[6]), "normal", 6) == 0){
			twam_clock_mode = false;
		} else {
			return -1;
		}
	} else if (strncmp(arg, "signal:", 7) == 0) {
		if (parse_num(&(arg[7]), &event, len-7) < 0) {
			return -1;
		}
		if (assign_slot(event) < 0) {
			return -1;
		}
	} else if (strncmp(arg, "scpsys:", 7) == 0) { //TODO:
		event = 0;
		if (arg[7] == '0') {//infrasys

		} else if (arg[7] == '1') {//perisys

		} else {
			return -1;
		}
	} else {
		return -1;
	}

	met_spmtwam.mode = 1;
	return 0;
}
Beispiel #26
0
/*
 * Inserts a define into a label list alphabetically (will parse if possible)
 */
void write_defines_callback(define_t *define, list_t *label_list) {
	int value;
	label_t *label;
	list_t *node, *prev;

	if (define->num_args > 0 || define->contents == NULL)
		return;
	
	if (parse_num(define->contents, &value) == false)
		return;
	
	node = label_list->next, prev = label_list;
	while (node != NULL && strcasecmp (define->name, ((label_t *) node->data)->name) > 0) {
		prev = node;
		node = node->next;
	}
	
	label = (label_t *) malloc(sizeof(label_t));
	label->name = define->name;
	label->value = value;
	list_insert(prev, label);
}
Beispiel #27
0
char* reduce_string (char* input) {
	char *output = input;

	int quote_count = 0;
	for (int i = 0; input[i] != '\0'; i++, output++)
	{
		if (input[i] == '\\')
		{
			int value;
			char pbuf[8] = "'\\ '";
			pbuf[2] = input[++i];
			parse_num (pbuf, &value);
			*output = value;
		}
		else if (input[i] == '"')
		{
			quote_count++;
			*output = input[i];
		}
		else
		{
			*output = input[i];
		}
	}

	*output = '\0';

	// Count the quotes
	if (quote_count >= 2)
	{
		if (input[0] == '"' && input[strlen(input) - 1] == '"')
		{
			input[strlen(input) - 1] = '\0';
			memmove(input, input + 1, strlen(input));
		}
	}
	
	return input;
}
static int
NFQUEUE_parse(int c, char **argv, int invert, unsigned int *flags,
              const void *entry, struct xt_entry_target **target)
{
	struct xt_NFQ_info *tinfo
		= (struct xt_NFQ_info *)(*target)->data;

	switch (c) {
	case 'F':
		if (*flags)
			xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
				   "Only use --queue-num ONCE!");
		parse_num(optarg, tinfo);
		break;
	case 'B':
		xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
				   "--queue-balance not supported (kernel too old?)");
	default:
		return 0;
	}

	return 1;
}
Beispiel #29
0
char *eval (const char *expr)
{
	char result[256], *expr_value;
	int value;

	int session = StartSPASMErrorSession();
	bool fResult = parse_num (expr, &value);
	EndSPASMErrorSession(session);
	if (fResult)
	{
		const char *sign = "";
		if (value < 0) {
			sign = "-";
			value = -value;
		}
		snprintf (result, sizeof (result), "%s$%0X", sign, value);
		expr_value = strdup (result);
	}
	else
	{
		expr_value = expand_expr (expr);
	}
	return expr_value;
}
int main(int argc, char **argv)
{
    int wants_wipe = 0;
    int wants_reboot = 0;
    int wants_reboot_bootloader = 0;
    int erase_first = 1;
    void *data;
    unsigned sz;
    int status;
    int c;
    int longindex;

    const struct option longopts[] = {
        {"base", required_argument, 0, 'b'},
        {"kernel_offset", required_argument, 0, 'k'},
        {"page_size", required_argument, 0, 'n'},
        {"ramdisk_offset", required_argument, 0, 'r'},
        {"tags_offset", required_argument, 0, 't'},
        {"help", no_argument, 0, 'h'},
        {"unbuffered", no_argument, 0, 0},
        {"version", no_argument, 0, 0},
        {0, 0, 0, 0}
    };

    serial = getenv("ANDROID_SERIAL");

    while (1) {
        c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
        if (c < 0) {
            break;
        }
        /* Alphabetical cases */
        switch (c) {
        case 'b':
            base_addr = strtoul(optarg, 0, 16);
            break;
        case 'c':
            cmdline = optarg;
            break;
        case 'h':
            usage();
            return 1;
        case 'i': {
                char *endptr = NULL;
                unsigned long val;

                val = strtoul(optarg, &endptr, 0);
                if (!endptr || *endptr != '\0' || (val & ~0xffff))
                    die("invalid vendor id '%s'", optarg);
                vendor_id = (unsigned short)val;
                break;
            }
        case 'k':
            kernel_offset = strtoul(optarg, 0, 16);
            break;
        case 'l':
            long_listing = 1;
            break;
        case 'n':
            page_size = (unsigned)strtoul(optarg, NULL, 0);
            if (!page_size) die("invalid page size");
            break;
        case 'p':
            product = optarg;
            break;
        case 'r':
            ramdisk_offset = strtoul(optarg, 0, 16);
            break;
        case 't':
            tags_offset = strtoul(optarg, 0, 16);
            break;
        case 's':
            serial = optarg;
            break;
        case 'S':
            sparse_limit = parse_num(optarg);
            if (sparse_limit < 0) {
                    die("invalid sparse limit");
            }
            break;
        case 'u':
            erase_first = 0;
            break;
        case 'w':
            wants_wipe = 1;
            break;
        case '?':
            return 1;
        case 0:
            if (strcmp("unbuffered", longopts[longindex].name) == 0) {
                setvbuf(stdout, NULL, _IONBF, 0);
                setvbuf(stderr, NULL, _IONBF, 0);
            } else if (strcmp("version", longopts[longindex].name) == 0) {
                fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
                return 0;
            }
            break;
        default:
            abort();
        }
    }

    argc -= optind;
    argv += optind;

    if (argc == 0 && !wants_wipe) {
        usage();
        return 1;
    }

    if (argc > 0 && !strcmp(*argv, "devices")) {
        skip(1);
        list_devices();
        return 0;
    }

    if (argc > 0 && !strcmp(*argv, "help")) {
        usage();
        return 0;
    }

    usb_handle* usb = open_device();

    while (argc > 0) {
        if(!strcmp(*argv, "getvar")) {
            require(2);
            fb_queue_display(argv[1], argv[1]);
            skip(2);
        } else if(!strcmp(*argv, "erase")) {
            require(2);

            if (fb_format_supported(usb, argv[1], NULL)) {
                fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
            }

            fb_queue_erase(argv[1]);
            skip(2);
        } else if(!strncmp(*argv, "format", strlen("format"))) {
            char *overrides;
            char *type_override = NULL;
            char *size_override = NULL;
            require(2);
            /*
             * Parsing for: "format[:[type][:[size]]]"
             * Some valid things:
             *  - select ontly the size, and leave default fs type:
             *    format::0x4000000 userdata
             *  - default fs type and size:
             *    format userdata
             *    format:: userdata
             */
            overrides = strchr(*argv, ':');
            if (overrides) {
                overrides++;
                size_override = strchr(overrides, ':');
                if (size_override) {
                    size_override[0] = '\0';
                    size_override++;
                }
                type_override = overrides;
            }
            if (type_override && !type_override[0]) type_override = NULL;
            if (size_override && !size_override[0]) size_override = NULL;
            if (erase_first && needs_erase(usb, argv[1])) {
                fb_queue_erase(argv[1]);
            }
            fb_perform_format(usb, argv[1], 0, type_override, size_override);
            skip(2);
        } else if(!strcmp(*argv, "signature")) {
            require(2);
            data = load_file(argv[1], &sz);
            if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
            if (sz != 256) die("signature must be 256 bytes");
            fb_queue_download("signature", data, sz);
            fb_queue_command("signature", "installing signature");
            skip(2);
        } else if(!strcmp(*argv, "reboot")) {
            wants_reboot = 1;
            skip(1);
            if (argc > 0) {
                if (!strcmp(*argv, "bootloader")) {
                    wants_reboot = 0;
                    wants_reboot_bootloader = 1;
                    skip(1);
                }
            }
            require(0);
        } else if(!strcmp(*argv, "reboot-bootloader")) {
            wants_reboot_bootloader = 1;
            skip(1);
        } else if (!strcmp(*argv, "continue")) {
            fb_queue_command("continue", "resuming boot");
            skip(1);
        } else if(!strcmp(*argv, "boot")) {
            char *kname = 0;
            char *rname = 0;
            skip(1);
            if (argc > 0) {
                kname = argv[0];
                skip(1);
            }
            if (argc > 0) {
                rname = argv[0];
                skip(1);
            }
            data = load_bootable_image(kname, rname, &sz, cmdline);
            if (data == 0) return 1;
            fb_queue_download("boot.img", data, sz);
            fb_queue_command("boot", "booting");
        } else if(!strcmp(*argv, "flash")) {
            char *pname = argv[1];
            char *fname = 0;
            require(2);
            if (argc > 2) {
                fname = argv[2];
                skip(3);
            } else {
                fname = find_item(pname, product);
                skip(2);
            }
            if (fname == 0) die("cannot determine image filename for '%s'", pname);
            if (erase_first && needs_erase(usb, pname)) {
                fb_queue_erase(pname);
            }
            do_flash(usb, pname, fname);
        } else if(!strcmp(*argv, "flash:raw")) {
            char *pname = argv[1];
            char *kname = argv[2];
            char *rname = 0;
            require(3);
            if(argc > 3) {
                rname = argv[3];
                skip(4);
            } else {
                skip(3);
            }
            data = load_bootable_image(kname, rname, &sz, cmdline);
            if (data == 0) die("cannot load bootable image");
            fb_queue_flash(pname, data, sz);
        } else if(!strcmp(*argv, "flashall")) {
            skip(1);
            do_flashall(usb, erase_first);
            wants_reboot = 1;
        } else if(!strcmp(*argv, "update")) {
            if (argc > 1) {
                do_update(usb, argv[1], erase_first);
                skip(2);
            } else {
                do_update(usb, "update.zip", erase_first);
                skip(1);
            }
            wants_reboot = 1;
        } else if(!strcmp(*argv, "oem")) {
            argc = do_oem_command(argc, argv);
        } else if(!strcmp(*argv, "flashing") && argc == 2) {
            if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
               || !strcmp(*(argv+1), "unlock_critical")
               || !strcmp(*(argv+1), "lock_critical")
               || !strcmp(*(argv+1), "get_unlock_ability")) {
              argc = do_oem_command(argc, argv);
            } else {
              usage();
              return 1;
            }
        } else {
            usage();
            return 1;
        }
    }

    if (wants_wipe) {
        fb_queue_erase("userdata");
        fb_perform_format(usb, "userdata", 1, NULL, NULL);
        fb_queue_erase("cache");
        fb_perform_format(usb, "cache", 1, NULL, NULL);
    }
    if (wants_reboot) {
        fb_queue_reboot();
        fb_queue_wait_for_disconnect();
    } else if (wants_reboot_bootloader) {
        fb_queue_command("reboot-bootloader", "rebooting into bootloader");
        fb_queue_wait_for_disconnect();
    }

    if (fb_queue_is_empty())
        return 0;

    status = fb_execute_queue(usb);
    return (status) ? 1 : 0;
}