Beispiel #1
0
int master(int argc, char **argv) {
	char token[TOKENLEN];
	key  *pkey, *keys = NULL;
	int  gchar;

/* Print help message and exit if arguments were supplied. */
	if (argc > 1) {
		help();
		return 0;
	}

/* Print help and error message if no file was redirected. */
	if (isatty(fileno(stdin))) {
		help();
		error("takes an ascii script file as redirected input");
	}

/* Print help and error message if a redirected file contains binary data. */
	if (isbin(stdin)) {
		help();
		error("takes an ascii script file as input, not a binary file");
	}

/* Decompose script file into a linked list of tokens. */
	while (1) {
		if (scanf("%s",token) < 0) break;

		if (strchr(COMM,(int)token[0]))
			do gchar = getchar();
			while (gchar!=EOL && gchar!=EOF);
		else {
			pkey = keys;
			keys = (key*)malloc(sizeof(key));
			keys->prev = (void*)pkey;
			keys->token = strdup(token);
		}
	}

/* Process token list - do everything */
	doit(keys);

/* Dismantle token list */
	rmkeys(keys);
	
	return 0;
}
Beispiel #2
0
static GEN
int_read(const char **s)
{
  int nb;
  GEN y;
  if (isbin(s)) {
    y = utoi(binnumber(&nb, s));
    if (nb == MAX_BDIGITS) y = bin_read_more(y, s);
  } else
  if (ishex(s)) {
    y = utoi(hexnumber(&nb, s));
    if (nb == MAX_XDIGITS) y = hex_read_more(y, s);
  } else
  {
    y = int_read_dec(s);
  }
  return y;
}
Beispiel #3
0
int
pari_lex(union token_value *yylval, struct node_loc *yylloc, char **lex)
{
  (void) yylval;
  yylloc->start=*lex;
  if (!**lex)
  {
    yylloc->end=*lex;
    return 0;
  }
  if (isalpha((int)**lex))
  {
    while (is_keyword_char(**lex)) ++*lex;
    yylloc->end=*lex;
    return KENTRY;
  }
  if (**lex=='"')
  {
    ++*lex;
    skipstring(lex);
    if (!**lex)
      compile_err("run-away string",*lex-1);
    ++*lex;
    yylloc->end=*lex;
    return KSTRING;
  }
  if (**lex == '.')
  {
    int token;
    if ((*lex)[1]== '.')
    {
      *lex+=2; yylloc->end = *lex; return KDOTDOT;
    }
    token=skipconstante(lex);
    if (token==KREAL)
    {
      yylloc->end = *lex;
      return token;
    }
    ++*lex;
    yylloc->end=*lex;
    return '.';
  }
  if (isbin((const char**)lex))
  {
    while (**lex=='0' || **lex=='1') ++*lex;
    return KINTEGER;
  }
  if (ishex((const char**)lex))
  {
    while (isxdigit((int)**lex)) ++*lex;
    return KINTEGER;
  }
  if (isdigit((int)**lex))
  {
    int token=skipconstante(lex);
    yylloc->end = *lex;
    return token;
  }
  if ((*lex)[1]=='=')
    switch (**lex)
    {
    case '=':
      if ((*lex)[2]=='=')
      { *lex+=3; yylloc->end = *lex; return KID; }
      else
      { *lex+=2; yylloc->end = *lex; return KEQ; }
    case '>':
      *lex+=2; yylloc->end = *lex; return KGE;
    case '<':
      *lex+=2; yylloc->end = *lex; return KLE;
    case '*':
      *lex+=2; yylloc->end = *lex; return KME;
    case '/':
      *lex+=2; yylloc->end = *lex; return KDE;
    case '%':
      if ((*lex)[2]=='=') break;
      *lex+=2; yylloc->end = *lex; return KMODE;
    case '!':
      if ((*lex)[2]=='=') break;
      *lex+=2; yylloc->end = *lex; return KNE;
    case '\\':
      *lex+=2; yylloc->end = *lex; return KEUCE;
    case '+':
      *lex+=2; yylloc->end = *lex; return KPE;
    case '-':
      *lex+=2; yylloc->end = *lex; return KSE;
    }
  if (**lex==')' && (*lex)[1]=='-' && (*lex)[2]=='>')
  {
    *lex+=3; yylloc->end = *lex; return KPARROW;
  }
  if (**lex=='-' && (*lex)[1]=='>')
  {
    *lex+=2; yylloc->end = *lex; return KARROW;
  }
  if (**lex=='<' && (*lex)[1]=='>')
  {
    *lex+=2; yylloc->end = *lex; return KNE;
  }
  if (**lex=='\\' && (*lex)[1]=='/')
    switch((*lex)[2])
    {
    case '=':
      *lex+=3; yylloc->end = *lex; return KDRE;
    default:
      *lex+=2; yylloc->end = *lex; return KDR;
    }
  if ((*lex)[1]==**lex)
    switch (**lex)
    {
    case '&':
      *lex+=2; yylloc->end = *lex; return KAND;
    case '|':
      *lex+=2; yylloc->end = *lex; return KOR;
    case '+':
      *lex+=2; yylloc->end = *lex; return KPP;
    case '-':
      *lex+=2; yylloc->end = *lex; return KSS;
    case '>':
      if ((*lex)[2]=='=') { *lex+=3; yylloc->end = *lex; return KSRE;}
      *lex+=2; yylloc->end = *lex; return KSR;
    case '<':
      if ((*lex)[2]=='=')
      { *lex+=3; yylloc->end = *lex; return KSLE; }
      *lex+=2; yylloc->end = *lex; return KSL;
    }
  yylloc->end = *lex+1;
  return (unsigned char) *(*lex)++;
}
Beispiel #4
0
//static inline
num_result_t parse_number(const char* c, size_t i){
    num_result_t num_result;
    num_result.type = CH_NO_TYPE;
    if( c[i] == '\0'){
        return num_result;
    }

    uint64_t uint_accumulator       = 0;
    int64_t int_accumulator         = 0;
    double float_accumulator        = 0;
    double float_base_accumulator   = 1;
    int64_t sign                    = 1;
    char prefix                     = 0;

    size_t state = STATE_INIT;
    for(    ;
            state != STATE_NONE_FOUND &&
            state != STATE_FINISHED_INT &&
            state != STATE_FINISHED_UINT &&
            state != STATE_FINISHED_FLOAT;
            i++ ){

        switch(state){
            case STATE_INIT:{
                uint_accumulator        = 0;
                float_accumulator       = 0;
                float_base_accumulator  = 1;
                sign                    = 1;

                if( c[i] == '-')    { sign  = -1;
                                      state = STATE_FOUND_SIGN;             continue; }
                if( c[i] == '+')    { state = STATE_FOUND_SIGN;             continue; }
                if( c[i] == '.')    { state = STATE_GET_FLO_DIGITS;         continue; }
                if( c[i] == '0')    { state = STATE_FOUND_INIT_ZERO;        continue; }
                if( isdigit(c[i]) ) { uint_accumulator = (c[i] - '0');
                                      state = STATE_GET_DEC_DIGITS;         continue; }
                if( iswhite(c[i]) ) { state = STATE_INIT;                   continue; }
                else                { state = STATE_NONE_FOUND;             continue; }
            }

            case STATE_FOUND_SIGN:{
                if( c[i] == '0' )    { state = STATE_MUST_BE_ZERO_OR_PERIOD; continue; }
                if( c[i] == '.' )    { state = STATE_GET_FLO_DIGITS;         continue; }
                if( isdigit(c[i]) )  { uint_accumulator = (c[i] - '0');
                                       state = STATE_GET_DEC_DIGITS;         continue; }
                else                 { state = STATE_NONE_FOUND;             continue; }
            }

            case STATE_MUST_BE_ZERO_OR_PERIOD: {
                if( c[i] == '0' )    { state = STATE_MUST_BE_ZERO_OR_PERIOD; continue; }
                if( c[i] == '.' )    { state = STATE_GET_FLO_DIGITS;         continue; }
                else                 { state = STATE_NONE_FOUND;             continue; }

            }

            case STATE_FOUND_INIT_ZERO: {
                if( c[i] == 'x')    { state = STATE_GET_HEX_DIGITS;          continue; }
                if( c[i] == 'X')    { state = STATE_GET_HEX_DIGITS;          continue; }
                if( c[i] == 'b')    { state = STATE_GET_BIN_DIGITS;          continue; }
                if( c[i] == 'B')    { state = STATE_GET_BIN_DIGITS;          continue; }
                if( c[i] == '.')    { state = STATE_GET_FLO_DIGITS;          continue; }
                if( c[i] == '\0')   { state = STATE_FINISHED_UINT;           continue; }
                if( isprefix(c[i])) { prefix = c[i];
                                      state = STATE_END_UINT;                continue; }
                if( isodigit(c[i])) { uint_accumulator = (c[i] - '0');
                                      state = STATE_GET_OCT_DIGITS;          continue; }
                else                { state = STATE_NONE_FOUND;              continue; }
            }

            case STATE_GET_DEC_DIGITS:{
                if( isdigit(c[i]) ) { uint_accumulator *= 10;
                                      uint_accumulator += c[i] - '0';
                                      state = STATE_GET_DEC_DIGITS;         continue; }
                if( c[i] == '.')    { float_accumulator = (double)uint_accumulator;
                                      state = STATE_GET_FLO_DIGITS;         continue; }
                if( isprefix(c[i])) { prefix = c[i];
                                      state = STATE_END_UINT;               continue; }
                if( issci(c[i]) )
                if( iswhite(c[i]) ) { state = STATE_END_UINT;               continue; }
                if( isnull(c[i]) )  { state = STATE_FINISHED_UINT;          continue; }
                else                { state = STATE_NONE_FOUND;             continue; }
            }

            case STATE_GET_BIN_DIGITS: {
                if( isbdigit(c[i]) ) { uint_accumulator <<= 1;
                                       uint_accumulator += c[i] - '0';
                                       state = STATE_GET_BIN_DIGITS;        continue; }
                if( isprefix(c[i]))  { prefix = c[i];
                                       state = STATE_END_UINT;              continue; }
                if( iswhite(c[i]) )  { state = STATE_END_UINT;              continue; }
                if( isnull(c[i]) )   { state = STATE_FINISHED_UINT;         continue; }
                else                 { state = STATE_NONE_FOUND;            continue; }

            }

            case STATE_GET_OCT_DIGITS: {
                if( isodigit(c[i]) ) { uint_accumulator *= 8;
                                       uint_accumulator += c[i] - '0';
                                       state = STATE_GET_OCT_DIGITS;        continue; }
                if( isprefix(c[i]))  { prefix = c[i];
                                       state = STATE_END_UINT;              continue; }
                if( iswhite(c[i]) )  { state = STATE_END_UINT;              continue; }
                if( isnull(c[i]) )   { state = STATE_FINISHED_UINT;         continue; }
                else                 { state = STATE_NONE_FOUND;            continue; }
            }

            case STATE_GET_HEX_DIGITS: {
                if( isdigit(c[i]) )  { uint_accumulator *= 16;
                                       uint_accumulator += c[i] - '0';
                                       state = STATE_GET_HEX_DIGITS;        continue; }
                if( isxdigit(c[i]) ) { uint_accumulator *= 16;
                                       uint_accumulator += getxdigit(c[i]);
                                       state = STATE_GET_HEX_DIGITS;        continue; }
                if( isprefix(c[i]))  { prefix = c[i];
                                       state = STATE_END_UINT;              continue; }
                if( iswhite(c[i]) )  { state = STATE_END_UINT;              continue; }
                if( isnull(c[i]) )   { state = STATE_FINISHED_UINT;         continue; }

                else                 { state = STATE_NONE_FOUND;            continue; }
            }

            case STATE_GET_FLO_DIGITS: {
                if( isdigit(c[i]) )  { float_base_accumulator  *= 10.0;
                                       float_accumulator += (double)(c[i] - '0') / float_base_accumulator;
                                       state = STATE_GET_FLO_DIGITS;         continue; }
                if( isprefix(c[i]))  { prefix = c[i];
                                       state = STATE_END_FLOAT;              continue; }
                if( iswhite(c[i])  ) { state = STATE_END_FLOAT;              continue; }
                if( isnull(c[i]) )   { state = STATE_FINISHED_FLOAT;         continue; }
                else                 { state = STATE_NONE_FOUND;             continue; }
            }

            case STATE_END_UINT:{
                if( iswhite(c[i]) )   { state = STATE_END_UINT;             continue; }
                if( isbin( c[i]) &&
                    isprefix(prefix)) { uint_accumulator *= get_bin_prefix(prefix);
                                        state = STATE_FINISHED_UINT;        continue; }
                if( isprefix(prefix)) { uint_accumulator *= get_prefix(prefix);
                                        state = STATE_FINISHED_UINT;        continue; }
                if( isnull(c[i]) )    { state = STATE_FINISHED_UINT;        continue; }
                else                  { state = STATE_NONE_FOUND;           continue; }
            }

            case STATE_END_INT:  {
                if( iswhite(c[i]) )   { state = STATE_END_INT;               continue; }
                if( isprefix(prefix)) { int_accumulator *= get_prefix(prefix);
                                         state = STATE_FINISHED_INT;         continue; }
                if( isbin(c[i]) &&
                    isprefix(prefix)) { int_accumulator *= get_bin_prefix(prefix);
                                        state = STATE_FINISHED_INT;          continue; }
                if( isnull(c[i]) )    { state = STATE_FINISHED_INT;          continue; }
                else                  { state = STATE_NONE_FOUND;            continue; }
            }

            case STATE_END_FLOAT: {
                if( iswhite(c[i]) )    { state = STATE_END_FLOAT;            continue; }
                if( isprefix(prefix))  { float_accumulator *= get_prefix(prefix);
                                         state = STATE_FINISHED_FLOAT;       continue; }
                if( isbin( c[i] &&
                    isprefix(prefix))) { float_accumulator *= get_bin_prefix(prefix);
                                         state = STATE_FINISHED_FLOAT;       continue; }
                if( isnull(c[i]) )     { state = STATE_FINISHED_FLOAT;       continue; }
                else                   { state = STATE_NONE_FOUND;           continue; }
            }
            default:{
                ch_log_error("Undefined state parsing numeric %lu\n", state);
                num_result.type = CH_NO_TYPE;
                return num_result;
            }

        }
    }


    switch(state){
        case STATE_NONE_FOUND:{
            num_result.type     = CH_NO_TYPE;
            break;
        }

        case STATE_FINISHED_INT: {
            num_result.type     = CH_INT64;
            num_result.val_int  = int_accumulator;
            break;
        }
        case STATE_FINISHED_UINT:{
            if(sign == -1){
                int_accumulator     = uint_accumulator * sign;
                num_result.type     = CH_INT64;
                num_result.val_int  = int_accumulator;
                break;
            }

            num_result.type     = CH_UINT64;
            num_result.val_uint = uint_accumulator;
            break;
        }
        case STATE_FINISHED_FLOAT:{
            num_result.type     = CH_DOUBLE;
            num_result.val_dble = float_accumulator * (double)sign;
            break;
        }
        default:{
            ch_log_error("Undefined state parsing numeric %lu\n", state);
            num_result.type = CH_NO_TYPE;
            return num_result;
        }
    }

    return num_result;


}