Exemplo n.º 1
0
/* get the next token out of a scanner */
token_t * lexer_nextitem(scanner_t *s) {
  /* try to match longest tokens first */
  static lexcomp_t (*tokenizers[])(scanner_t*) = {
    tokenize_text,
    tokenize_identifier,
    tokenize_number,
    tokenize_bitops,
    tokenize_relops,
    tokenize_mathops,
    tokenize_miscops,
  };

  lexcomp_t lc;
  size_t i;

  /* consume all whitespace */
  while (is_white(scanner_advance(s)));
  scanner_backup(s);
  scanner_ignore(s);

  if (scanner_peek(s) == 0)
    return token_init(tokStackEmpty, "");

  for (i = 0; i < sizeof(tokenizers)/sizeof(tokenizers[0]); i++) {
    if ((lc = tokenizers[i](s)) != tokNoMatch) {
      token_t *t = (token_t*)scanner_accept(s, (acceptfn)tok_maker);
      t->lexcomp = lc;
      return t;
    }
  }

  return token_init(tokNoMatch, "");
}
Exemplo n.º 2
0
void chess_pgn_tokenizer_init(ChessPgnTokenizer* tokenizer, ChessReader* reader)
{
    memset(tokenizer, 0, sizeof(ChessPgnTokenizer));
    tokenizer->reader = reader;
    tokenizer->line = 1;
    tokenizer->col = 0;
    tokenizer->nextc = NOCHAR;
    tokenizer->lastc = NOCHAR;
    token_init(&tokenizer->tokens[0]);
    token_init(&tokenizer->tokens[1]);
    chess_buffer_init(&tokenizer->buffer);
}
Exemplo n.º 3
0
void token(char *lin,char *tok[],int *ntok,char *outbuff) {
int jj;
int tj;
char *cp;
char *p2 = NULL;
	*ntok = 0;
	in_quote = false;
	if (table_loaded==false) token_init();
	cp = lin;
	cp = find_non_space(cp);
	while (*cp!=0) {
		if (*cp==' ' || *cp=='	') {
			*cp = ' ';
			cp = find_non_space(cp);
		}
		if (*cp == '!') goto endofline;
		p2 = find_term(cp);
		jj = p2-cp+1;
		if (jj==0) goto endofline;
		add_tok(cp,jj);
		cp = p2 + 1 ;
		if (*ntok>280) subscript();
	}
endofline:;
	if (*ntok>0) {
		if ( (*tok[*ntok])=='\n' ) (*ntok)--;
		if (strcmp(tok[*ntok]," ")==0) (*ntok)--;
		if (*ntok>0) p2 =  tok[*ntok] + strlen(tok[*ntok])  - 1;
		if (*p2==10) *p2 = 0;
	}
}
Exemplo n.º 4
0
int
ud_init (struct udoc *ud)
{
  bin_zero (ud, sizeof (*ud));
  if (!ud_oht_init (&ud->ud_parts, sizeof (struct ud_part))) goto FAIL;
  if (!ud_oht_init (&ud->ud_link_exts, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_refs, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_ref_names, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_footnotes, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_styles, sizeof (struct ud_ref))) goto FAIL;
  if (!dstack_init (&ud->ud_errors, 16, sizeof (struct ud_err))) goto FAIL;
  if (!token_init (&ud->ud_tok)) goto FAIL;

  ud->ud_dirfd_pwd = open_ro (".");
  if (ud->ud_dirfd_pwd == -1) goto FAIL;
  ud->ud_dirfd_src = -1;
  ud->ud_dirfd_out = -1;
  ud->ud_main_doc = ud;
  ud->ud_cur_doc = ud;

  if (!ht_init (&ud->ud_loopchecks)) goto FAIL;
  if (!ht_init (&ud->ud_documents)) goto FAIL;

  taia_now (&ud->ud_time_start);

  return 1;
  FAIL:
  ud_free (ud);
  return 0;
}
Exemplo n.º 5
0
ssize_t			tokenize(const char *string,
				 struct s_vec *queue)
{
  size_t		i;
  size_t		len;
  struct s_token	*token;

  assert(string != NULL);
  assert(queue != NULL);
  if (vec_init(queue, 0, &free_token_queue) == -1)
    return (-1);
  len = strlen(string);
  i = 0;
  while (i < len)
    {
      if (string[i] == ' ' || string[i] == '\t')
	{
	  ++i;
	  continue;
	}
      if (token_new(&token) != 0 || token_init(token) != 0)
	return (-1);
      if (!call_tokenizers(string, queue, token, &i))
	i += 1;
    }
  return (queue->size);
}
Exemplo n.º 6
0
static token_t *token_new_with_children(token_id_t id, gpointer data,
                                        clone_func_t data_clone_func,
                                        free_func_t data_free_func,
                                        to_string_func_t data_to_string_func,
                                        GPtrArray *children) {
    token_t *token = g_new_1(token_t);
    token_init(token, id, data, data_clone_func, data_free_func,
               data_to_string_func, children);
    return token;
}
Exemplo n.º 7
0
static token_t _process_identifier(void)
{
    token_t token;

    // current character already belongs to the identifier
    do {
        _move_forward();
    } while (_char_is_alphanum(_cur_char()));

    token_init(&token);
    token_set_identifier(&token, _.begin, _.forward-1);

    return token;
}
Exemplo n.º 8
0
END_TEST

START_TEST (test_dlredir)
{
  const char		*s = "<<";
  struct s_token	t;

  assert(token_init(&t) == 0);

  ck_assert_int_eq(parse_token_dlredir(s, &t), 2);

  ck_assert_str_eq(t.string._string, "<<");
  ck_assert_int_eq(t.string.len, 2);
  ck_assert_int_eq(t.type, TT_DLREDIR);
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
int load_file( char * filename )
{
    long   size;
    file * fp = file_open( filename, "rb0" );
    char * source;

    if ( n_files == MAX_SOURCES ) compile_error( MSG_TOO_MANY_FILES );
    strcpy( files[n_files++], filename );

    if ( !fp ) compile_error( MSG_FILE_NOT_FOUND, filename );

    size = file_size( fp );
    source = ( char * ) calloc( size + 1, sizeof( char ) );
    if ( !source ) compile_error( MSG_FILE_TOO_BIG, filename );
    if ( size == 0 ) compile_error( MSG_FILE_EMPTY, filename );
    if ( !file_read( fp, source, size ) ) compile_error( MSG_READ_ERROR, filename );

    source[size] = 0;
    file_close( fp );

    token_init( source, n_files - 1 );
    return n_files -1;
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
	plan_lazy();

	dnssec_crypto_init();

	// PKCS #11 initialization

	dnssec_keystore_t *store = NULL;
	int r = dnssec_keystore_init_pkcs11(&store);
	if (r == DNSSEC_NOT_IMPLEMENTED_ERROR) {
		skip_all("not supported");
		goto done;
	}
	ok(r == DNSSEC_EOK && store, "dnssec_keystore_init_pkcs11()");

	char *dso_name = libsofthsm_dso();
	if (!dso_name) {
		skip_all("%s not found, set %s environment variable",
			 SOFTHSM_DSO, ENV_SOFTHSM_DSO);
		goto done;
	}
	ok(dso_name != NULL, "find token DSO");

	bool success = token_init();
	if (!success) {
		skip_all("failed to configure and initialize the token");
		goto done;
	}
	ok(success, "initialize the token");

	char config[4096] = { 0 };
	r = snprintf(config, sizeof(config), "pkcs11:token=%s;pin-value=%s %s",
	                                     TOKEN_LABEL, TOKEN_PIN, dso_name);
	free(dso_name);
	ok(r > 0 && r < sizeof(config), "build configuration");

	// key store access

	r = dnssec_keystore_init(store, config);
	ok(r == DNSSEC_NOT_IMPLEMENTED_ERROR, "dnssec_keystore_init(), not implemented");

	r = dnssec_keystore_open(store, config);
	ok(r == DNSSEC_EOK, "dnssec_keystore_open()");

	dnssec_list_t *keys = NULL;
	r = dnssec_keystore_list_keys(store, &keys);
	ok(r == DNSSEC_EOK && dnssec_list_size(keys) == 0, "dnssec_keystore_list_keys(), empty");
	dnssec_list_free_full(keys, NULL, NULL);

	// key manipulation

	static const int KEYS_COUNT = 3;
	static const key_parameters_t *KEYS[] = {
		&SAMPLE_RSA_KEY,
		&SAMPLE_ECDSA_KEY,
		&SAMPLE_DSA_KEY,
	};
	assert(KEYS_COUNT == sizeof(KEYS) / sizeof(*KEYS));

	for (int i = 0; i < KEYS_COUNT; i++) {
		test_algorithm(store, KEYS[i]);
	}

	test_key_listing(store, KEYS, KEYS_COUNT);

	r = dnssec_keystore_close(store);
	ok(r == DNSSEC_EOK, "dnssec_keystore_close()");
done:
	dnssec_keystore_deinit(store);
	dnssec_crypto_cleanup();
	token_cleanup();

	return 0;
}
Exemplo n.º 12
0
void token_equal() {
	if (table_loaded==false) token_init();
	term_table = &term_table3[0];
	spmode = false;
}
Exemplo n.º 13
0
void token_space() {
	if (table_loaded==false) token_init();
	term_table = &term_table2[0];
	spmode = true;
}
Exemplo n.º 14
0
void token_norm() {
	if (table_loaded==false) token_init();
	term_table = &term_table1[0];
	spmode = false;
}
Exemplo n.º 15
0
void preprocessor_expand( DEFINE * def )
{
    const char * param_left[MAX_MACRO_PARAMS];
    const char * param_right[MAX_MACRO_PARAMS];
    const char * begin = NULL;
    const char * old_source = NULL;
    char * text;
    int i, count, depth, allocated, size, part, actual_line_count;

    /* No params - easy case */

    if ( def->param_count == -1 )
    {
        int line = line_count - 1;
        token_init( def->text, current_file );
        line_count = line;
        return;
    }

    /* Find left parenthesis */

    disable_expand_defines++;
    token_next();
    disable_expand_defines--;
    if ( token.type != IDENTIFIER || token.code != identifier_leftp ) compile_error( MSG_EXPECTED, "(" );

    /* Mark parameters' starting and ending positions */

    if ( def->param_count > 0 )
    {
        for ( count = 0; count < def->param_count; count++ )
        {
            depth = 0;
            param_left[count] = source_ptr;

            while ( *source_ptr && ( depth > 0 || ( *source_ptr != ')' && *source_ptr != ',' ) ) )
            {
                if ( *source_ptr == '"' || *source_ptr == '\'' )
                {
                    begin = source_ptr++;
                    while ( *source_ptr && *source_ptr != *begin ) source_ptr++;
                    if ( !*source_ptr ) compile_error( MSG_EXPECTED, "\"" );
                    source_ptr++;
                    continue;
                }
                if ( *source_ptr == '(' ) depth++;
                if ( *source_ptr == ')' ) depth--;
                source_ptr++;
            }
            param_right[count] = source_ptr;
            if ( !*source_ptr ) compile_error( MSG_EXPECTED, ")" );
            if ( *source_ptr == ')' ) break;
            source_ptr++;
        }

        if ( count != def->param_count - 1 || *source_ptr != ')' )
            compile_error( MSG_INCORRECT_PARAMC, identifier_name( def->code ), def->param_count - 1 );
    }
    else
    {
        if ( *source_ptr != ')' )
            compile_error( MSG_INCORRECT_PARAMC, identifier_name( def->code ), def->param_count );
    }

    source_ptr++;

    /* Expand the macro */

    allocated = 128;
    size = 0;
    text = ( char * )calloc( allocated, sizeof( char ) );
    old_source = source_ptr;
    source_ptr = def->text;
    actual_line_count = line_count;

    while ( *source_ptr )
    {
        SKIP_SPACES_UNTIL_LF;
        if ( *source_ptr == '\n' ) break;

        begin = source_ptr;
        SKIP_SPACES_UNTIL_LF;
        if ( *source_ptr )
        {
            SKIP_SPACES_UNTIL_LF;
            if ( !*source_ptr ) break;
            if ( *source_ptr != '\n' )
            {
                disable_expand_defines++;
                token_next();
                disable_expand_defines--;
                if ( token.type == NOTOKEN ) break;
                if ( token.type == IDENTIFIER )
                {
                    /* Next token is an identifier. Search for parameter */

                    for ( i = 0; i < def->param_count; i++ )
                        if ( def->param_id[i] == token.code ) break;

                    if ( i != def->param_count )   /* Parameter found - expand it */
                    {

                        part = param_right[i] - param_left[i];
                        if ( size + part + 1 >= allocated )
                        {
                            allocated += (( part + 256 ) & ~ 127 );
                            text = ( char * )realloc( text, allocated );
                        }
                        text[size++] = ' ';
                        memcpy( text + size, param_left[i], part );
                        size += part;
                        continue;
                    }
                }

                /* No parameter found - copy the token */

                part = source_ptr - begin;
                if ( size + part + 1 >= allocated )
                {
                    allocated += (( part + 256 ) & ~127 );
                    text = ( char * )realloc( text, allocated );
                }
                memcpy( text + size, begin, part );
                size += part;
            }
            else
            {
                line_count++;
                source_ptr++;
            }
        }
    }

    text[size] = 0;
    source_ptr = old_source;
    line_count = actual_line_count;

    /* Now "include" the expanded text "file" */

    token_init( text, current_file );
    line_count = actual_line_count - 1;

    free( text );
}
Exemplo n.º 16
0
void preprocessor()
{
    int i, ifdef;
    char * ptr;
    int actual_line_count;

    static int initialized = 0;

    if ( !initialized )
    {
        id_define  = identifier_search_or_add( "DEFINE" );
        id_undef   = identifier_search_or_add( "UNDEF" );
        id_ifdef   = identifier_search_or_add( "IFDEF" );
        id_ifndef  = identifier_search_or_add( "IFNDEF" );
        id_else    = identifier_search_or_add( "ELSE" );
        id_endif   = identifier_search_or_add( "ENDIF" );
        id_if      = identifier_search_or_add( "IF" );
        initialized = 1;
    }

    token_next();

    if ( token.type != IDENTIFIER ) compile_error( MSG_UNKNOWN_PREP );

    /* #define TEXT value */

    if ( token.code == id_define )
    {
        disable_expand_defines++;

        token_next();
        if ( token.type != IDENTIFIER ) compile_error( MSG_INVALID_IDENTIFIER );

        if ( find_define( token.code ) != -1 ) compile_error( MSG_MACRO_ERROR, identifier_name( token.code ) );

        /* Allocate the macro */

        if ( defines_allocated == defines_count )
        {
            defines_allocated += 8;
            defines = ( DEFINE * ) realloc( defines, sizeof( DEFINE ) * defines_allocated );
        }

        defines[defines_count].code = token.code;

        /* Check for parameters: no space allowed between name and ( */

        if ( *source_ptr == '(' )
        {
            source_ptr++;
            for ( defines[defines_count].param_count = i = 0; *source_ptr != ')'; )
            {
                if ( !*source_ptr ) compile_error( MSG_EXPECTED, ")" );
                if ( i == MAX_MACRO_PARAMS ) compile_error( MSG_TOO_MANY_PARAMS );
                token_next();

                if ( token.type != IDENTIFIER || token.code < reserved_words ) compile_error( MSG_INVALID_IDENTIFIER );

                defines[defines_count].param_id[i++] = token.code;
                defines[defines_count].param_count++;

                SKIP_SPACES;
                if ( *source_ptr == ',' ) source_ptr++;
            }
            source_ptr++;
        }
        else
        {
            /* No parameters and no parenthesis */
            defines[defines_count].param_count = -1;
        }

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;

        ptr = ( char * ) source_ptr;

        while ( *ptr && *ptr != '\n' )
            if ( *ptr == '\\' && *( ptr + 1 ) == '\n' )
            {
                *ptr = ' ';
                ptr++;
                *ptr = ' ';
                ptr++;
                line_count++;
            }
            else
                ptr++;

        while ( ptr > source_ptr && ( !*ptr || ISSPACE( *ptr ) ) ) ptr--;

        defines[defines_count].text = ( char * )calloc( ptr - source_ptr + 2, sizeof( char ) );
        strncpy( defines[defines_count].text, source_ptr, ptr - source_ptr + 1 );
        defines[defines_count].text[ptr - source_ptr + 1] = 0;

        defines_count++;

        source_ptr = ptr + 1;

        disable_expand_defines--;

        return;
    }

    /* #undef TEXT */

    if ( token.code == id_undef )
    {
        disable_expand_defines++;

        token_next();
        if ( token.type != IDENTIFIER ) compile_error( MSG_INVALID_IDENTIFIER );

        if (( i = find_define( token.code ) ) != -1 )
        {
            defines_count--;
            if ( defines[i].text ) free( defines[i].text );
            memmove( &defines[i], &defines[i+1], ( defines_count - i ) * sizeof( DEFINE ) );
        }

        disable_expand_defines--;

        return;
    }

    /* #ifdef CONST / #ifndef CONST*/

    if ( token.code == id_ifdef || token.code == id_ifndef )
    {
        ifdef = token.code == id_ifdef;

        prepro_stack[prepro_sp++] = token.code;

        disable_expand_defines++;
        token_next();
        disable_expand_defines--;

        if ( token.type != IDENTIFIER ) compile_error( MSG_INVALID_IDENTIFIER );

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr && *source_ptr != '\n' )
        {
            if ( ifdef ) compile_warning( "extra tokens at end of #ifdef directive" );
            else compile_warning( "extra tokens at end of #ifndef directive" );
            SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
        }

        if ( *source_ptr == '\n' ) line_count--;

        for ( i = 0; i < defines_count; i++ )
        {
            if ( defines[i].code == token.code )
            {
                if ( ifdef ) return;
                break;
            }
        }
        if ( !ifdef && i == defines_count ) return;


        preprocessor_jumpto( id_else, id_endif );

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr && *source_ptr != '\n' )
        {
            if ( token.code == id_else ) compile_warning( "extra tokens at end of #else directive" );
            else if ( token.code == id_endif ) compile_warning( "extra tokens at end of #endif directive" );
            SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
        }
        if ( *source_ptr == '\n' ) line_count--;
        return;
    }

    /* #if */

    if ( token.code == id_if )
    {
        int actual_sources;
        expresion_result res;
        char c;

        prepro_stack[prepro_sp++] = token.code;

        ptr = ( char * ) source_ptr;

        while ( *ptr && *ptr != '\n' && *ptr != ';' )
            if ( *ptr == '\\' && *( ptr + 1 ) == '\n' )
            {
                *ptr = ' ';
                ptr++;
                *ptr = ' ';
                ptr++;
                line_count++;
            }
            else
                ptr++;

        c = *ptr;
        *ptr = '\0';

        actual_line_count = line_count;
        actual_sources = sources;

        token_init( source_ptr, current_file );

        identifiers_as_strings = 1;
        res = compile_expresion( 0, 0, 1, TYPE_DWORD );
        identifiers_as_strings = 0;
        /*
        printf ("exp: asignation: [%d] call: [%d] lvalue: [%d] constant: [%d] value: [%d] lvalue: [%f] type: [%d]\n",
                      res.asignation,
                      res.call,
                      res.lvalue,
                      res.constant,
                      res.value,
                      res.fvalue,
                      typedef_base(res.type));
        */
        if ( sources != actual_sources ) token_endfile();
        *ptr = c;
        source_ptr = ptr;
        line_count = actual_line_count;

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr && *source_ptr != '\n' )
        {
            compile_warning( "extra tokens at end of #if directive" );
            SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
        }
        if ( *source_ptr == '\n' ) line_count--;

        use_saved = 0;
        if ( !res.constant ) compile_error( MSG_CONSTANT_EXP );
        if ( !res.value )
        {
            preprocessor_jumpto( id_else, id_endif );

            SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
            if ( *source_ptr && *source_ptr != '\n' )
            {
                if ( token.code == id_else ) compile_warning( "extra tokens at end of #else directive" );
                else if ( token.code == id_endif ) compile_warning( "extra tokens at end of #endif directive" );
                SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
            }
            if ( *source_ptr == '\n' ) line_count--;
        }

        return;
    }

    /* #else */

    if ( token.code == id_else )
    {
        if ( !prepro_sp ) compile_error( "#else without #if" );

        int stck_code = prepro_stack[--prepro_sp];

        if ( prepro_sp < 0 ||
                (
                    stck_code != id_if &&
                    stck_code != id_ifdef &&
                    stck_code != id_ifndef
                )
           )
        {
            if ( stck_code == id_else )
            {
                compile_error( "#else after #else" );
            }
            else
            {
                compile_error( "#else without #if" );
            }
        }
        prepro_stack[prepro_sp++] = token.code;

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr && *source_ptr != '\n' )
        {
            compile_warning( "extra tokens at end of #else directive" );
            SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
        }
        if ( *source_ptr == '\n' ) line_count--;

        preprocessor_jumpto( id_endif, 0 );

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr && *source_ptr != '\n' )
        {
            compile_warning( "extra tokens at end of #endif directive" );
            SKIP_ALL_UNTIL_LF_AND_COUNT_LINES;
        }
        if ( *source_ptr == '\n' ) line_count--;
        return;
    }

    /* #endif */

    if ( token.code == id_endif )
    {
        int stck_code = prepro_stack[--prepro_sp];

        if ( prepro_sp < 0 || (
                    stck_code != id_else &&
                    stck_code != id_if &&
                    stck_code != id_ifdef &&
                    stck_code != id_ifndef ) ) compile_error( "#endif without #if" );

        SKIP_SPACES_UNTIL_LF_AND_COUNT_LINES;
        if ( *source_ptr == '\n' ) line_count--;
        return;
    }

    /* Unknown preprocessor directive */
    compile_error( MSG_UNKNOWN_PREP );
}
Exemplo n.º 17
0
token_t* _lexer_read_token(lexer_state_t* lex) {
    while ( (lex->source[lex->sourceIndex] == ' '  || lex->source[lex->sourceIndex] == '\t' ||
            lex->source[lex->sourceIndex] == '\n') && lex->sourceIndex < lex->_sourceLen ) {
        if ( lex->source[lex->sourceIndex] == '\n' ) {
            lex->lineNumber++;
            lex->lineIndex = 0;
        }
        else {
            lex->lineIndex++;
        }

        lex->sourceIndex++;
    }

    if ( lex->sourceIndex >= lex->_sourceLen ) {
        return token_init(eof_token, lex->lineNumber, lex->lineIndex, NULL);
    }

    char current = lex->source[lex->sourceIndex];

    if ( current >= '0' && current <= '9' ) {
        unsigned int currAlloc = MALLOC_CHUNK;
        unsigned int index = 0;
        char* str = (char*) malloc(currAlloc * sizeof(char));

        while ( (current >= '0' && current <= '9') && (lex->sourceIndex + index) < lex->_sourceLen ) {
            if ( index >= (currAlloc - 1) ) {
                currAlloc += MALLOC_CHUNK;
                char* temp = (char*) realloc(str, currAlloc);

                if ( !temp ) {
                    lex->sourceIndex += index;
                    lex->lineIndex += index;
                    lexer_error(lex, "Ran out of memory.\n");

                    free(str);
                    return NULL;
                }

                str = temp;
            }

            str[index] = current;
            str[index + 1] = 0;

            current = lex->source[lex->sourceIndex + (++index)];
        }

        if ( current == '.') {
            currAlloc += MALLOC_CHUNK;

            char* temp = (char*) realloc(str, currAlloc);

            if ( !temp ) {
                lex->sourceIndex += index;
                lex->lineIndex += index;
                lexer_error(lex, "Ran out of memory.\n");

                free(str);
                return NULL;
            }

            str = temp;

            str[index] = '.';
            str[index + 1] = 0;

            current = lex->source[lex->sourceIndex + (++index)];

            while ( (current >= '0' && current <= '9') && (lex->sourceIndex + index) < lex->_sourceLen ) {
                if ( index >= (currAlloc - 1) ) {
                    currAlloc += MALLOC_CHUNK;
                    char* temp = (char*) realloc(str, currAlloc);

                    if ( !temp ) {
                        lex->sourceIndex += index;
                        lex->lineIndex += index;
                        lexer_error(lex, "Ran out of memory.\n");

                        free(str);
                        return NULL;
                    }

                    str = temp;
                }

                str[index] = current;
                str[index + 1] = 0;

                current = lex->source[lex->sourceIndex + (++index)];
            }

            lex->sourceIndex += index;
            lex->lineIndex += index;

            double* v = (double*) malloc(sizeof(double));
            *v = atof(str);

            token_t* token = token_init(double_token, lex->lineNumber, lex->lineIndex, (void*) v);

            free(str);
            return token;
        }
        else {
            lex->sourceIndex += index;
            lex->lineIndex += index;

            int* v = (int*) malloc(sizeof(int));
            *v = atol(str);

            token_t* token = token_init(int_token, lex->lineNumber, lex->lineIndex, (void*) v);

            free(str);
            return token;
        }
    }
    else if ( (current >= 'a' && current <= 'z') || (current >= 'A' && current <= 'Z') ||
              (current >= '0' && current <= '9') || (current == '_')) {
        unsigned int currAlloc = MALLOC_CHUNK;
        char* str = (char*) malloc(currAlloc * sizeof(char));
        str[0] = current;
        str[1] = 0;

        unsigned int index = 1;
        current = lex->source[lex->sourceIndex + index];

        while ( ((current >= 'a' && current <= 'z') || (current >= 'A' && current <= 'Z') ||
                 (current >= '0' && current <= '9') || (current == '_')) &&
                 (lex->sourceIndex + index) < lex->_sourceLen ) {
            if ( index >= (currAlloc - 1) ) {
                currAlloc += MALLOC_CHUNK;
                char* temp = (char*) realloc(str, currAlloc);

                if ( !temp ) {
                    lex->sourceIndex += index;
                    lex->lineIndex += index;
                    lexer_error(lex, "Ran out of memory.\n");

                    free(str);
                    return NULL;
                }

                str = temp;
            }

            str[index] = current;
            str[index + 1] = 0;

            current = lex->source[lex->sourceIndex + (++index)];
        }

        lex->sourceIndex += index;
        lex->lineIndex += index;

        char* temp = (char*) realloc(str, strlen(str) + 1);

        if ( !temp ) {
            lexer_error(lex, "Ran out of memory.\n");

            free(str);
            return NULL;
        }

        bool special = false;

        for ( int i = 0; i < lex->specialTokenLength; i++ ) {
            if ( strcmp(temp, lex->specialTokens[i]) == 0 ) {
                special = true;
                break;
            }
        }

        token_t* token = token_init((special ? special_token : name_token), lex->lineNumber, lex->lineIndex, (void*) temp);

        return token;
    }
    else if ( current == '\"' || current == '\'' ) {
        char initial = current;

        lex->sourceIndex += 1;
        lex->lineIndex += 1;

        current = lex->source[lex->sourceIndex];

        unsigned int currAlloc = MALLOC_CHUNK;
        char* str = (char*) malloc(currAlloc * sizeof(char));
        str[0] = current;
        str[1] = 0;

        unsigned int index = 1;
        current = lex->source[lex->sourceIndex + index];

        while ( !(current == initial && lex->source[lex->sourceIndex + index - 1] != '\\') &&
                 (lex->sourceIndex + index) <= lex->_sourceLen ) {
            if ( index >= (currAlloc - 1) ) {
                currAlloc += MALLOC_CHUNK;
                char* temp = (char*) realloc(str, currAlloc);

                if ( !temp ) {
                    lex->sourceIndex += index;
                    lex->lineIndex += index;
                    lexer_error(lex, "Ran out of memory.\n");

                    free(str);
                    return NULL;
                }

                str = temp;
            }

            if ( lex->source[lex->sourceIndex + index] == '\\' ) {
                if ( lex->source[lex->sourceIndex + index - 1] != '\\' ) {
                    current = lex->source[lex->sourceIndex + (++index)];
                    continue;
                }
            }

            int t = strlen(str);
            str[t] = current;
            str[t + 1] = 0;

            current = lex->source[lex->sourceIndex + (++index)];
        }

        lex->sourceIndex += index;
        lex->lineIndex += index;

        char* temp = (char*) realloc(str, strlen(str) + 1);

        if ( !temp ) {
            lexer_error(lex, "Ran out of memory.\n");

            free(str);
            return NULL;
        }

        token_t* token = token_init(string_token, lex->lineNumber, lex->lineIndex, (void*) temp);

        lex->sourceIndex++;

        return token;
    }
    else {
        for ( int i = 0; i < lex->specialTokenLength; i++ ) {
            bool good = true;

            if ( lex->specialTokens[i] != NULL && lex->specialTokens[i][0] == current ) {
                for ( int j = 0; j < strlen(lex->specialTokens[i]); j++ ) {
                    if ( lex->specialTokens[i][j] != lex->source[j + lex->sourceIndex] ) {
                        good = false;
                        break;
                    }
                }
            }
            else {
                good = false;
            }

            if ( good ) {
                char* copy = (char*) malloc((strlen(lex->specialTokens[i]) + 1) * sizeof(char));;
                strcpy(copy, lex->specialTokens[i]);

                lex->sourceIndex += strlen(copy);
                lex->lineIndex += strlen(copy);

                token_t* tok = token_init(special_token, lex->lineNumber, lex->lineIndex, (void*) copy);

                return tok;
            }
        }

        lexer_error(lex, "Unexpected token %c\n", current);
    }

    return NULL;
}
Exemplo n.º 18
0
int
main(int argc, char *argv[])
{
	FILE *back = NULL;
	char *username = 0;
	char *instance;
	char challenge[1024];
	char response[1024];
	char *pp = 0;
	int c;
	int mode = 0;
	struct rlimit cds;
	sigset_t blockset;

	(void)setpriority(PRIO_PROCESS, 0, 0);

	/* We block keyboard-generated signals during database accesses. */
	sigemptyset(&blockset);
	sigaddset(&blockset, SIGINT);
	sigaddset(&blockset, SIGQUIT);
	sigaddset(&blockset, SIGTSTP);

	openlog(NULL, LOG_ODELAY, LOG_AUTH);

	cds.rlim_cur = 0;
	cds.rlim_max = 0;
	if (setrlimit(RLIMIT_CORE, &cds) < 0)
		syslog(LOG_ERR, "couldn't set core dump size to 0: %m");

	(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
	if (token_init(argv[0]) < 0) {
		syslog(LOG_ERR, "unknown token type");
		errx(1, "unknown token type");
	}
	(void)sigprocmask(SIG_UNBLOCK, &blockset, NULL);

	while ((c = getopt(argc, argv, "ds:v:")) != -1)
		switch (c) {
		case 'd':		/* to remain undocumented */
			back = stdout;
			break;
		case 'v':
			break;
		case 's':	/* service */
			if (strcmp(optarg, "login") == 0)
				mode = 0;
			else if (strcmp(optarg, "challenge") == 0)
				mode = 1;
			else if (strcmp(optarg, "response") == 0)
				mode = 2;
			else {
				syslog(LOG_ERR, "%s: invalid service", optarg);
				exit(1);
			}
			break;
		default:
			syslog(LOG_ERR, "usage error");
			exit(1);
		}

	switch (argc - optind) {
	case 2:
	case 1:
		username = argv[optind];
		break;
	default:
		syslog(LOG_ERR, "usage error");
		exit(1);
	}


	if (back == NULL && (back = fdopen(3, "r+")) == NULL)  {
		syslog(LOG_ERR, "reopening back channel");
		exit(1);
	}
	if (mode == 2) {
		mode = 0;
		c = -1;
		while (++c < sizeof(challenge) &&
		    read(3, &challenge[c], 1) == 1) {
			if (challenge[c] == '\0' && ++mode == 2)
				break;
			if (challenge[c] == '\0' && mode == 1)
				pp = challenge + c + 1;
		}
		if (mode < 2) {
			syslog(LOG_ERR, "protocol error on back channel");
			exit(1);
		}
	} else {
		(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
		tokenchallenge(username, challenge, sizeof(challenge),
		    tt->proper);
		(void)sigprocmask(SIG_UNBLOCK, &blockset, NULL);
		if (mode == 1) {
			fprintf(back, BI_VALUE " challenge %s\n",
			    auth_mkvalue(challenge));
			fprintf(back, BI_CHALLENGE "\n");
			exit(0);
		}

		pp = readpassphrase(challenge, response, sizeof(response), 0);
		if (pp == NULL)
			exit(1);
		if (*pp == '\0') {
			char buf[64];
			snprintf(buf, sizeof(buf), "%s Response [echo on]: ",
			    tt->proper);
			pp = readpassphrase(buf, response, sizeof(response),
			    RPP_ECHO_ON);
			if (pp == NULL)
				exit(1);
		}
	}

	(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
	if (tokenverify(username, challenge, pp) == 0) {
		fprintf(back, BI_AUTH "\n");

		if ((instance = strchr(username, '.'))) {
			*instance++ = 0;
			if (strcmp(instance, "root") == 0)
				fprintf(back, BI_ROOTOKAY "\n");
		}
		fprintf(back, BI_SECURE "\n");
		exit(0);
	}

	fprintf(back, BI_REJECT "\n");
	exit(1);
}