Example #1
0
void pgn_parse_game(const char* filename,
                    struct pgn_game_node* selected_game){
    struct pgn_ply_node size_ply, *first_ply = NULL;
    struct pgn_ply_node *temp_ply = NULL, *curr_node = NULL;
    int fhandler, i;
    char line_buffer[128];
    char token_buffer[10];
    unsigned short pos;
    unsigned short curr_player = white;

    fhandler = rb->open(filename, O_RDONLY);

    /* seek the line where the pgn of the selected game starts */
    for (i=1;i<selected_game->pgn_line;i++){
        rb->read_line(fhandler, line_buffer, sizeof line_buffer);
    }

    loghandler = rb->open(LOG_FILE, O_WRONLY | O_CREAT);

    GNUChess_Initialize();

    while (rb->read_line(fhandler, line_buffer, sizeof line_buffer) > 0){
        if (line_buffer[0] == '\r' || line_buffer[0] == '\n' || line_buffer[0] == '\0'){
            break;
        }
        pos = 0;
        while (pos < rb->strlen(line_buffer)){
            pos = get_next_token(line_buffer, pos, token_buffer);
            if ((token_buffer[0] >= 'A' && token_buffer[0] <= 'Z')
                || (token_buffer[0] >= 'a' && token_buffer[0] <= 'z')
                || (token_buffer[0] == '0' && token_buffer[2] != '1')){
                temp_ply = (struct pgn_ply_node *)pl_malloc(sizeof size_ply);
                temp_ply->player = curr_player;
                curr_player = (curr_player==white)?black:white;
                rb->strcpy(temp_ply->pgn_text, token_buffer);
                pgn_to_coords(temp_ply);
                temp_ply->prev_node = NULL;
                temp_ply->next_node = NULL;
                if (first_ply == NULL) {
                    first_ply = curr_node = temp_ply;
                } else {
                    curr_node->next_node = temp_ply;
                    temp_ply->prev_node = curr_node;
                    curr_node = temp_ply;
                }
                rb->fdprintf(loghandler,
                    "player: %u; pgn: %s; from: %u,%u; to: %u,%u; taken: %u.\n",
                             temp_ply->player, temp_ply->pgn_text, temp_ply->row_from,
                             temp_ply->column_from, temp_ply->row_to,
                             temp_ply->column_to, temp_ply->taken_piece);
            }
        }
    }

    rb->close(loghandler);

    /* additional dummy ply to represent end of file without
     *loosing the previous node's pointer
     */
    if (first_ply != NULL){
        temp_ply = (struct pgn_ply_node *)pl_malloc(sizeof size_ply);
        temp_ply->player = neutral;
        temp_ply->prev_node = curr_node;
        curr_node->next_node = temp_ply;
    }
    selected_game->first_ply = first_ply;
    rb->close(fhandler);
}
Example #2
0
void CJSourceParser::AddMacroNode( wxChar*& cur )
{
    wxChar* start = cur;

    int lineNo = get_line_no();

    skip_preprocessor_dir( cur );

    int tmpLnNo;
    store_line_no( tmpLnNo );

    if ( !mMacrosOn ) return;

    spPreprocessorLine* pPL = new spPreprocessorLine();
    pPL->mSrcLineNo = lineNo;

    AttachComments( *pPL, cur );

    get_string_between( start, cur, &pPL->m_Line );

    ++start; // skip '#'
    get_next_token( start );

    pPL->mDefType = SP_PREP_DEF_OTHER;

    // if we found a definition or redefinition,
    // determine the type exactly and assign
    // a name to the context

    if ( *start == _T('d') )
    {
        if ( cmp_tokens_fast( start, _T("define"), 6 ) )
        {
            char* tok = start+6;

            get_next_token( tok );

            pPL->m_Name = get_token_str( tok );

            skip_token( tok );
            get_next_token( tok);


            if ( tok > cur )
                pPL->mDefType = SP_PREP_DEF_DEFINE_SYMBOL;
            else
                pPL->mDefType = SP_PREP_DEF_REDEFINE_SYMBOL;
        }
    }
    else if ( *start == _T('i') )
    {
        if ( cmp_tokens_fast( start, _T("include"), 7 ) )
        {
            pPL->mDefType = SP_PREP_DEF_INCLUDE_FILE;
        }
        else if ( *++start == _T('f') )
        {
            // either "#if" or "#ifdef"
            cur = start;
            skip_token( cur );
            get_next_token( cur );

            wxString condition = get_token_str( cur );

            // currently, everything except '0' is true
            if ( condition == _T("0") ) {
                // skip until the following else or enif
                while ( cur < _gSrcEnd ) {
                    skip_to_eol( cur );
                    skip_eol( cur );

                    get_next_token( cur );
                    if ( *cur++ == _T('#') && *cur == _T('e') )
                        break;
                }
            }

            // TODO parse the condition...
        }
    }
    else if ( cmp_tokens_fast( start, _T("else"), 4 ) )
    {
        // skip until "#endif"
        while ( cur < _gSrcEnd ) {
            skip_to_eol( cur );
            skip_eol( cur );

            get_next_token( cur );
            if ( *cur++ == _T('#') && cmp_tokens_fast( cur, "endif", 5 ) )
                break;
        }
    }

    mpCurCtx->AddMember( pPL );

    skip_to_eol( cur );
    skip_eol( cur );

    restore_line_no( tmpLnNo );

    clear_commets_queue();
}
Example #3
0
bool CJSourceParser::ParseNameAndRetVal( char*& cur, bool& isAMacro )
{
    isAMacro = false;

    // FOR NOW:: all functions in the global
    //           scope are ignored

    int lineNo = get_line_no();

    char* start = cur;

    bool isVirtual = false;
    while( *cur != '(' )
    {
        if ( get_token_str( cur ) == "virtual" )
            isVirtual = true;

        skip_token( cur );
        if ( !get_next_token( cur ) ) return false;
    }

    char* bracketPos = cur;
    char* savedPos   = cur + 1;

    int tmpLnNo;
    store_line_no( tmpLnNo );

    // skip gap between function name and start of paramters list
    while ( *(cur-1) == ' ' )
        --cur;

    // check if it's not a macro, and let plugin handle it, if so

    if ( mpPlugin )
    {
        skip_token_back( cur );

        char* tmp = cur;

        if ( mpPlugin->CanUnderstandContext( tmp, _gSrcEnd, mpCurCtx ) )
        {
            cur = tmp;

            mpPlugin->ParseContext( _gSrcStart, cur, _gSrcEnd, mpCurCtx );

            isAMacro = true;

            return false;
        }
    }

    spOperation* pOp = new spOperation();

    pOp->mSrcLineNo    = lineNo;
    pOp->mSrcOffset    = int( start - _gSrcStart );
    pOp->mHeaderLength = int( bracketPos - start );
    if ( mpCurCtx->GetContextType() == SP_CTX_CLASS )
        pOp->mScope = mpCurCtx->m_Name;

    mpCurCtx->AddMember( pOp );
    pOp->mVisibility = mCurVis;
    pOp->mIsVirtual = isVirtual;

    // add comments about operation
    AttachComments( *pOp, cur );

    // go backwards to method name
    skip_token_back( cur );

    pOp->m_Name = get_token_str( cur );

    // checker whether it's not an operator
    char chFirst = *pOp->m_Name.c_str();
    if ( !isalpha(chFirst) && chFirst != '_' && chFirst != '~' ) {
        // skip 'operator'
        skip_next_token_back( cur );
        skip_token_back( cur );

        wxString lastToken = get_token_str( cur );
        if ( lastToken == "operator" ) {
            lastToken += pOp->m_Name;
            pOp->m_Name = lastToken;
        }
        else {
            // ok, it wasn't an operator after all
            skip_token( cur );
        }
    }
    else if ( pOp->m_Name == "operator" ) {
        skip_token( cur );
        get_next_token( cur );
        wxString oper = get_token_str( cur );

        pOp->m_Name += oper;
    }

    // go backwards to method return type
    skip_next_token_back( cur );

    if ( cur >= start )
    {
        wxString rettype = wxString( start, size_t( cur-start ) );
        // FIXME just for now...
        wxString::size_type pos = 0;
        wxString toerase("WXDLLEXPORT ");
        while((pos = rettype.find(toerase, pos)) != wxString::npos)
            rettype.erase(pos, toerase.length());
        pOp->m_RetType = rettype;
    }

    arrange_indirection_tokens_between( pOp->m_RetType, pOp->m_Name );

    cur = savedPos;
    restore_line_no( tmpLnNo );

    // now, enter operation context
    mpCurCtx = pOp;

    return true;
}
TCHAR *normalize_path(TCHAR *path)
{
	struct token_t path_tok, *token_ret = NULL;
	TCHAR *write_head = path;
	int len = _tcslen(path);
	int path_type;

	path_tok.begin = path_tok.end = path;
	if(path[0] == _T('\\') ||
			path[0] == _T('/')){	//absolute path without drive
		write_head++;
		path_tok.end++;
		path[0] = _T('\\');		//replace '/'
		path_type = ABSOLUTE_PATH_WITHOUT_DRIVE;
	}else if(len > 0 && path[1] == _T(':')){

		if(len > 1 && 
				path[2] != _T('\\') &&
				path[2] != _T('/')){	//error "c:" without "\"
			return NULL;
		}else if((path[0] >= _T('A') && path[0] <= _T('Z')) ||
				(path[0] >= _T('a') && path[0] <= _T('z'))){	//absolute path with drive
			write_head += 3;
			path_tok.end += 3;
			path[2] = _T('\\');		//replace '/'
			path_type = ABSOLUTE_PATH_WITH_DRIVE;
		}else{	//error "t:" not among a to z or A to Z
			path_type = RELITIVE_PATH;
		}

	}else{
		path_type = RELITIVE_PATH;
	}//start

	while(1){
		token_ret = get_next_token(&path_tok);
		if(token_ret == NULL){
			break;
		}

		switch(path_tok.type){
			case TOKEN_ONE_POINT:
				break;
			case TOKEN_TWO_POINT:
				write_head = go_back_previous_slash(write_head, path, path_type);
				if(!write_head)
					return NULL;
				break;
			case TOKEN_FILE_NAME:
				if(path_tok.begin == write_head){
					write_head = path_tok.end;
				} else {
					if(write_head != path && *(write_head - 1) != _T('\\')){
						*write_head++ = _T('\\');
					}	
					_tcsncpy(write_head, path_tok.begin,
							path_tok.end - path_tok.begin);
					write_head += path_tok.end - path_tok.begin;
				}
				break;
			default:
				return NULL;
		}
	}
	*write_head = _T('\0');

	return path;	
}
Example #5
0
MachineResult *get_next_token(ParserData *parser_data, int options)
{
	// remember where we were last by saving source file pointer
	static FILE *s;

	// save current line
	static char l[MAX_LINE_LENGTH_1];
	static char *f;
	static int i = 0;

	// options for things like NOP
	static int o = 0;
	static MachineResult *r;

	if (o & TOKEN_OPTION_NOP && r != NULL) {
		 o = options;
		return r;
	}

	// grab another line
	if (s != parser_data->source || f - l > MAX_LINE_LENGTH || *f == 0)
	{
		s = parser_data->source;

		char *line = get_next_line(parser_data->source);

		strcpy(l, line);
		f = l;
		i++;

		// output line to listing file
		if (parser_data->listing != NULL)
			fprintf (parser_data->listing, "%-8d%s\n", i, line);
	}

	MachineResult result = machine_omega(f, parser_data->reserved_words, parser_data->symbol_table);

	// do not increment line counter for eof
	if (result.token->type == TOKEN_EOF) i--;

	result.line_no = i;

	MachineResult *resultPtr = (MachineResult *)malloc(sizeof(MachineResult));
	memcpy(resultPtr, &result, sizeof(MachineResult));

	// advance our internal pointer
	f = result.f;

	if (resultPtr->token->type == TOKEN_WHITESPACE)
		return get_next_token(parser_data, options);

	// write token to tokens file
	if (parser_data->tokens != NULL) {
		fprintf (parser_data->tokens, "%-10d%-20s%-20s%-6d(%s)\n", i, resultPtr->lexeme, token_type_to_str(resultPtr->token->type), resultPtr->token->attribute, attribute_to_str(resultPtr->token->attribute));
	}

	// handle lexical errors
	if (resultPtr->token->type == TOKEN_LEXERR) {
		// output errors to listing file
		if (!(options & TOKEN_OPTION_SQUASH_ERRS))
			lexerr(resultPtr, parser_data);

		// do not return lexerr tokens
		return get_next_token(parser_data, options);
	}

	// check for a nop
	if (options & TOKEN_OPTION_NOP)
		r = resultPtr;

	o = options;

	return resultPtr;
}
Example #6
0
int _nodelist_common_string_get_token_common(char *string, char *separators_list,
                                             int *p_token_nb, int token_id,
                                             char **p_token)
{
  int fstatus = -1;

  int i;

  size_t string_length;
  size_t separators_list_length;

  char *working_string;

  char *current_pointer;
  char *best_pointer;
  char *old_pointer;

  size_t copy_length;

  int local_token_nb;
  int end_of_loop;

  /*
     First we check that pointers are not NULL
   */
  if(string != NULL && separators_list != NULL)
    {
      string_length = strlen(string);
      separators_list_length = strlen(separators_list);
      /*
         Then, that their lengths are not null
       */
      if(string_length != 0 && separators_list_length != 0)
        {
          /*
             Then, the separators research loop start
           */
          working_string = string;
          old_pointer = working_string;
          local_token_nb = 1;
          end_of_loop = 0;
          while(!end_of_loop)
            {
              best_pointer = NULL;
              /*
                 Search the first occurence of a separator
               */
              for(i = 0; i < separators_list_length; i++)
                {
                  current_pointer =
                      get_next_token(working_string, *(separators_list + i));
                  if(best_pointer == NULL)
                    {
                      best_pointer = current_pointer;
                    }
                  else if(best_pointer > current_pointer && current_pointer != NULL)
                    {
                      best_pointer = current_pointer;
                    }
                }
              /*
                 If this token must be extracted, extract it
               */
              if(token_id == local_token_nb && (*p_token) == NULL)
                {
                  if(best_pointer == NULL)
                    copy_length = strlen(old_pointer);
                  else
                    copy_length = (size_t) (best_pointer - old_pointer);
                  *p_token = (char *)malloc((copy_length + 1) * sizeof(char));
                  if(*p_token != NULL)
                    {
                      (*p_token)[copy_length] = '\0';
                      strncpy(*p_token, old_pointer, copy_length);
                      fstatus++;
                    }
                  else
                    {
                      fstatus = -2;
                    }
                }
              /*
                 If no more occurences, break the loop
               */
              if(best_pointer == NULL)
                {
                  end_of_loop = 1;
                }
              /*
                 Otherwise, increment token counter and adjust working string
               */
              else
                {
                  local_token_nb++;
                  working_string = best_pointer + 1;
                  old_pointer = working_string;
                }
            }
          *p_token_nb = local_token_nb;
          fstatus++;
        }
    }

  return fstatus;
}
Example #7
0
void PageTemplate::parse(const std::string& data)
{
	std::size_t pos = 0, len = data.length();
	while (pos < len)
		body_.push_back(get_next_token(data, pos));
}
Example #8
0
/*
 * King sized, man-trap for the un-wary. This method is called
 * from a separate AsyncSerial thread; we need to get data
 * synchronisation right.
 */
void RepRapSerial::OnEvent(char* data, size_t dwBytesRead)
{
	// Read data, until there is nothing left
	data[dwBytesRead] = '\0';
	InBuffer += data;		// Buffer data for later analysis

	// Endchars = \r\n

	//		debugPrint( string("Received:\"") + InBuffer +"\" (" + stringify(dwBytesRead));
	{
		// Check inbuffer for good stuff

		// remove leading \n, \r, and spaces (also added \t for good measure)
		while(InBuffer.length() > 0 && (InBuffer.substr(0,1) == "\n" ||  InBuffer.substr(0,1) == "\r" || InBuffer.substr(0,1) == " " || InBuffer.substr(0,1) == "\t"))
			InBuffer = InBuffer.substr(1, InBuffer.length()-1);

		if(InBuffer[0] == 1)	// Ctrl
		{
			InBuffer = InBuffer.substr(2, InBuffer.length()-2);
			debugPrint("Recieved a Ctrl character", true);
		}
		if(InBuffer.size() == 0)
			return;

		size_t found;
		size_t found2;
		found=InBuffer.find_first_of("\r");
		found2=InBuffer.find_first_of("\n");
		if (found!=string::npos)
		{
			if (found2!=string::npos)
			{
				found = found < found2 && found ? found : found2;
			}
		}
		else
			found = found2;


		while (found!=string::npos && found != 0)
		{
			bool knownCommand = true;
			string command = InBuffer.substr(0,found);

			if(0)
			{
				stringstream oss;
				oss << "Command:" << command;
				debugPrint(oss.str(), true);
			}

			if (command == "ok")	// most common, first
			{
				if(m_bPrinting)
					SendNextLine();
			}
			else if(command.substr(0,5) == "Echo:") // search, there's a parameter int (temperature)
			{
				string parameter = command.substr(5,command.length()-5);
				echo( string("Echo:") + parameter);
				// Check parameter
			}
			else if(command.substr(0,2) == "T:") // search, there's a parameter int (temperature)
			{
				string parameter = command.substr(2,command.length()-2);
				debugPrint( string("Received:") + command+ " with parameter " + parameter);

				ToolkitLock guard;

				// Reduce re-draws by only updating the GUI on a real change
				const char *old_value = gui->CurrentTempText->value();
				if (!old_value || strcmp (parameter.c_str(), old_value))
					gui->CurrentTempText->value(parameter.c_str());
			}
			else if(command == "start")
			{
				debugPrint( string("Received: start"));
			}
			else if(command.substr(0,3) == "E: ") // search, there's a parameter int (temperature_error, wait_till_hot)
			{
				string parameter = command.substr(3,command.length()-3);
				debugPrint( string("Received:") + command+ " with parameter " + parameter);
				// Check parameter
			}
			// this is the common case for the modern 5D reprap firmware
			else if(command.substr(0,3) == "ok ") // search, there's a parameter string (debugstring)
			{

				//starting from the "ok", we'll parse the rest as "tokens"
				string s = get_next_token(command.substr(0,command.length()));
				uint l = s.length();
				while ( s.length() > 0 ) {
					string remainder = command.substr(l,command.length());
					uint ws = count_leading_whitespace(remainder);

					// s = the current token , l = the offset of this token in command
					//cout << "s:" << s << endl;

					// we already know this is how the line starts:
				        if ( s == "ok" ) {
					// do nothing more

					// temperature token:
					} else if ( s.substr(0,2) == "T:" ) {
						temp_param = s.substr(2,s.length());

						// Reduce re-draws by only updating the GUI on a real change
						const char *old_value = gui->CurrentTempText->value();
						if (!old_value || strcmp (temp_param.c_str(), old_value))
						gui->CurrentTempText->value(temp_param.c_str());


					// bed temperature token:
					} else if ( s.substr(0,2) == "B:" ) {
						bedtemp_param = s.substr(2,s.length());

						// Reduce re-draws by only updating the GUI on a real change
						const char *old_value = gui->CurrentBedTempText->value();
						if (!old_value || strcmp (bedtemp_param.c_str(), old_value))
						gui->CurrentBedTempText->value(bedtemp_param.c_str());

					// a token we don't yet understand , dump to stdout for now
					} else  {

						cout << "unknown token:" << s << endl;

					}

					s = get_next_token(remainder);
					l += s.length() + ws;
				}

				string parameter = command.substr(3,command.length()-3);
				debugPrint( string("Received:") + command+ " with parameter " + parameter + "**************************************", true);
				if(m_bPrinting)
					SendNextLine();
			}
			else if(command.substr(0,5) == "huh? ") // search, there's a parameter string (unknown command)
			{
				string parameter = command.substr(6,command.length()-5);
				debugPrint( string("Received:") + command+ " with parameter " + parameter, true);

				if(m_bPrinting)
					SendNextLine();
			}
			else if(command.substr(0,7) == "Resend:") // search, there's a parameter string (unknown command)
			{
				string parameter = command.substr(7,command.length()-7);
				debugPrint( string("Received:") + command+ " with parameter " + parameter, true);

				std::stringstream iss(parameter);
				iss >> m_iLineNr;	// Rewind to requested line

				if(m_bPrinting)
					SendNextLine();
			}
			else if(command.substr(0,3) == "rs ") // search, there's a parameter string (unknown command)
			{
				string parameter = command.substr(3,command.length()-3);
				debugPrint( string("Received:") + command+ " with parameter " + parameter, true);
				std::stringstream iss(parameter);
				iss >> m_iLineNr; // Rewind to requested line
				if(m_bPrinting)
					SendNextLine();
			}
Example #9
0
expression_t parse_expr() {
    expression_t parsed_expr;
    stack_sym_t top;
    stack_sym_t next;
    vector_char_t* stack = vector_init(VI_CHAR);
    vector_token_t* token_buffer = vector_init(VI_TOKEN);
    vector_expr_t* expr_buffer = vector_init(VI_EXPR);
    vector_push(stack, S_END);

    bool use_cached = cached_identificator.type != TT_NONE;
    token_t last_token;
    token_t expr_token = use_cached ? cached_identificator : next_token ;
    next = token_to_sym(&expr_token);

    do {
        top = vector_find(stack, top_term_cmp);
        switch (prec_table[top][next]) {
            case TAB_SP:
                vector_insert_after(stack, S_SEP, top_term_cmp);
            case TAB_P:
                vector_push(stack, next);
                //next is now top terminal on stack
                top = next;
                //store last token
                vector_push(token_buffer, expr_token);
                //choose between cached_identificator and next_token
                if (use_cached) {
                    use_cached = false;
                    cached_identificator.type = TT_NONE;
                    expr_token = next_token;
                } else {
                    expr_token = next_token = get_next_token(token_stream);
                }
                //convert next token to table symbol
                next = token_to_sym(&expr_token);
                break;
            case TAB_R:
                last_token = vector_pop(token_buffer);
                switch (reduce_sequence(stack)) {
                    case RULE_REL:
                        check_rule_rel(last_token.op_rel, expr_buffer);
                        break;
                    case RULE_ADDSUB:
                        check_rule_arith(last_token.op_arith, expr_buffer);
                        break;
                    case RULE_MULDIV:
                        check_rule_arith(last_token.op_arith, expr_buffer);
                        break;
                    case RULE_PAR:
                        //only pop next token(TT_PARENTHESES_OPEN) from token buffer
                        vector_pop(token_buffer);
                        break;
                    case RULE_ID:
                        check_rule_id(&last_token, expr_buffer);
                        break;
                    default:
                        error("Syntactic error: Failed to parse the expression", ERROR_SYN);
                }
                vector_push(stack, S_EXPR);
                break;
            case TAB_END:
                if (vector_pop(stack) != S_EXPR) {
                    error("Syntactic error: Failed to parse the expression", ERROR_SYN);
                }
                parsed_expr = vector_top(expr_buffer);
                break;
            case TAB_ERR:
            default:
                error("Syntactic error: Failed to parse the expression", ERROR_SYN);
        }
    } while (top != S_END || next != S_END);

    ifj15_free(stack);
    ifj15_free(token_buffer);
    ifj15_free(expr_buffer);
    return parsed_expr;
}
Example #10
0
spFile* CJSourceParser::Parse( char* start, char* end )
{
    // set up state variables
    mCurVis       = SP_VIS_PRIVATE;

    spFile* pTopCtx = new spFile();
    mpCurCtx        = pTopCtx;

    mIsVirtual    = 0;
    mIsTemplate   = 0;
    mNestingLevel = 0;

    m_cur = start;

    mpStart = start;
    mpEnd   = end;

    _gSrcEnd   = mpEnd; // let all the C-functions "smell" the end of file
    _gSrcStart = start;

    _gLineNo   = 0;

    clear_commets_queue();

    // main parsing loop

    do
    {
        if ( !get_next_token( m_cur ) )
            // end of source reached
            return pTopCtx;

        if ( memcmp( m_cur, "ScriptSection( const string&",
                     strlen( "ScriptSection( const string&" )
                   ) == 0
            )
        {
            // int o = 0;
            // ++o;
        }

        switch (*m_cur)
        {
            case '#' :
                {
                    AddMacroNode( m_cur );
                    continue;
                }

            case ':' :
                {
                    skip_token( m_cur );
                    continue;
                }

            case ';' :
                {
                    skip_token( m_cur );
                    continue;
                }

            case ')' :
                {
                    skip_token( m_cur );
                    continue;
                }

            case '=' :
                {
                    skip_token( m_cur );
                    continue;
                }

            default: break;
        }

        // 'const' is a part of the return type, not a keyword here
        if ( strncmp(m_cur, "const", 5) != 0 && is_keyword( m_cur ) )
        {
            // parses, token, if token identifies
            // the container context (e.g. class/namespace)
            // the corresponding context object is created
            // and set as current context

            ParseKeyword( m_cur );
            continue;
        }

        if ( *m_cur >= _T('0') && *m_cur <= _T('9') )
        {
            skip_token( m_cur );
            continue;
        }

        if ( *m_cur == _T('}') )
        {
            if ( mCurCtxType != SP_CTX_CLASS )
            {
                // FOR NOW:: disable the below assertion

                // DBG:: unexpected closing-bracket found
                //ASSERT(0);

                skip_token( m_cur ); // just skip it
                continue;
            }

            if ( mpCurCtx->GetType() == SP_CTX_CLASS )
            {
                int curOfs = ( (m_cur+1) - _gSrcStart );

                mpCurCtx->mContextLength = ( curOfs - mpCurCtx->mSrcOffset );
            }

            --mNestingLevel;

            // terminate operation/class/namespace context
            // TBD:: check if it's really this type of context

            wxASSERT( mpCurCtx );
            mpCurCtx = mpCurCtx->GetOutterContext();
            wxASSERT( mpCurCtx );

            if ( mNestingLevel == 0 )
            {

                mCurCtxType = SP_CTX_FILE;

                // not-nested class delclaration finished,
                // rest template flag in any case
                mIsTemplate = 0;
            }

            skip_token( m_cur );
            continue;
        }

        bool isAMacro = false;

        if ( is_function( m_cur, isAMacro ) )
        {
            if ( isAMacro )
            {
                skip_token( m_cur );
                continue;
            }

            char* savedPos = m_cur;

            int tmpLnNo;
            store_line_no( tmpLnNo );
            wxUnusedVar( tmpLnNo );

            isAMacro = false;

            if ( !ParseNameAndRetVal( m_cur, isAMacro ) )
            {
                if ( !isAMacro )
                {
                    m_cur = savedPos;
                    SkipFunction( m_cur );
                }
                continue;
            }

            if ( !ParseArguments( m_cur ) )
            {
                // failure while parsing arguments,
                // remove enclosing operation context

                spContext* pFailed = mpCurCtx;
                mpCurCtx = mpCurCtx->GetOutterContext();
                mpCurCtx->RemoveChild( pFailed );

                skip_to_eol( m_cur );
                //m_cur = savedPos;
            }
            else
            {
                // otherwise, successfully close operation context:

                clear_commets_queue();

                SkipFunctionBody( m_cur );

                mpCurCtx = mpCurCtx->GetOutterContext();

                // DBG::
                wxASSERT( mpCurCtx );

            }
        }
        else // otherwise it's declaration of a variable;
        {
            // now, the cursor point to the end of statement (';' token)

            if ( mCurCtxType != SP_CTX_CLASS )
            {
                // non-class members are ignored

                skip_token( m_cur ); // skip the end of statement
                continue;
            }

            ParseMemberVar( m_cur );
        }

    } while( 1 );
}
Example #11
0
int
cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
{
	struct cmdline_token_string *tk2;
	struct cmdline_token_string_data *sd;
	unsigned int token_len;
	const char *str;

	if (!tk || !buf || ! *buf)
		return -1;

	tk2 = (struct cmdline_token_string *)tk;

	sd = &tk2->string_data;

	/* fixed string */
	if (sd->str) {
		str = sd->str;
		do {
			token_len = get_token_len(str);

			/* if token is too big... */
			if (token_len >= STR_TOKEN_SIZE - 1) {
				continue;
			}

			if ( strncmp(buf, str, token_len) ) {
				continue;
			}

			if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
				continue;
			}

			break;
		} while ( (str = get_next_token(str)) != NULL );

		if (!str)
			return -1;
	}
	/* unspecified string */
	else {
		token_len = 0;
		while(!cmdline_isendoftoken(buf[token_len]) &&
		      token_len < (STR_TOKEN_SIZE-1))
			token_len++;

		/* return if token too long */
		if (token_len >= STR_TOKEN_SIZE - 1) {
			return -1;
		}
	}

	if (res) {
		/* we are sure that token_len is < STR_TOKEN_SIZE-1 */
		rte_snprintf(res, STR_TOKEN_SIZE, "%s", buf);
		*((char *)res + token_len) = 0;
	}


	return token_len;
}
Example #12
0
static size_t skip_block( char*& cur )
{
    size_t level = 0; // nesting level

    char* start = cur;

    // NOTE:: assumed that block not necessarely starts
    //        with bracket rightaway

    if ( *cur == '(' )
    {
        ++level;
    }

    do
    {
        skip_token( cur );

        char* savedPos = cur;
        int tmpLnNo;
        store_line_no( tmpLnNo );

        get_next_token( cur );

        if ( cur >= _gSrcEnd ) return 0;

        if ( *cur == '(' )
        {
            ++level;
        }
        else
        if ( *cur == ')' )
        {
            if ( level == 0 )
            {
                cur = savedPos;
                restore_line_no( tmpLnNo );

                return size_t(cur-start);
            }

            --level;

            if ( level == 0 )
            {
                ++cur;

                // QUICK-HACK::to easily handle function prototypes ,
                // it works, besause theoretically there should
                // be no cast-expressions in non-implementation
                // scope (e.g. "time( (long*)(ptr+1) )" should not
                // appear in the declarations, thus it is most likelly
                // for the ")(" fragment to be within a function
                // prototype in the declarations scope

                if ( *cur == '(' )
                {
                    ++level;
                    continue;
                }

                else return size_t(cur-start);
            }
        }
        else
        {
            if ( level == 0 )
            {
                cur = savedPos;
                restore_line_no( tmpLnNo );

                return size_t(cur-start);
            }
        }

    } while(1);
}
Example #13
0
void CJSourceParser::AddTypeDefNode( wxChar*& cur )
{
    // now the cursor at the token next to "typedef" keyword

    if ( !get_next_token(cur) ) return;

    wxChar* start = cur;

    spTypeDef* pTDef = new spTypeDef();
    mpCurCtx->AddMember( pTDef );

    pTDef->mSrcLineNo = get_line_no();

    AttachComments( *pTDef, cur );

    skip_statement( cur );

    int tmpLnNo;
    store_line_no( tmpLnNo );

    wxChar* tok = cur-1;
    skip_next_token_back( tok );

    wxChar* nameEnd = tok;

    skip_token_back( tok );

    wxChar* nameStart = tok;

    skip_next_token_back( tok );

    wxChar* typeEnd = tok;

    // check if it's function prototype
    if ( *nameStart == ')' )
    {
        typeEnd = nameStart+1;

        // skip argument list
        while ( *nameStart != '(' ) --nameStart;

        // skip to function type definition
        while ( *nameStart != ')' ) --nameStart;

        skip_next_token_back( nameStart );

        nameEnd = nameStart;

        skip_token_back( nameStart );

        if ( *nameStart == '*' ) ++nameStart;
    }

    get_string_between( start, typeEnd, &pTDef->m_OriginalType );

    get_string_between( nameStart, nameEnd, &pTDef->m_Name );

    clear_commets_queue();

    restore_line_no( tmpLnNo );
}
Example #14
0
void CJSourceParser::AddClassNode( char*& cur )
{
    char* ctxStart = cur;

    wxString classkeyword = get_token_str( cur );

    skip_token( cur ); // skip 'class' keyword
    if ( !get_next_token( cur ) ) return;

    // in C++
    if ( *cur == ':' )
    {
        skip_token( cur );
        get_next_token( cur );
    }

    // by default all class members are private
    mCurVis = SP_VIS_PRIVATE;

    spClass* pClass = new spClass();
    if ( classkeyword == "class" )
        pClass->mClassSubType = SP_CLTYPE_CLASS;
    else if ( classkeyword == "struct" ) {
        pClass->mClassSubType = SP_CLTYPE_STRUCTURE;

        mCurVis = SP_VIS_PUBLIC;
    }
    else if ( classkeyword == "union" ) {
        pClass->mClassSubType = SP_CLTYPE_UNION;

        mCurVis = SP_VIS_PUBLIC;
    }
    else if ( classkeyword == "interface" )
        pClass->mClassSubType = SP_CLTYPE_INTERFACE;
    else {
        pClass->mClassSubType = SP_CLTYPE_INVALID;

        wxFAIL_MSG("unknown class keyword");
    }

    mpCurCtx->AddMember( pClass );

    // attach comments about the class
    AttachComments( *pClass, cur );

    pClass->mSrcLineNo = get_line_no();

    pClass->mSrcOffset = int( ctxStart - _gSrcStart );

    char* nameTok = cur;
    pClass->m_Name = get_token_str( cur );

    bool isDerived = 0;

    // DANGER-MACROS::

    do
    {
        skip_token( cur );
        if ( !get_next_token( cur ) ) return;

        if ( *cur == ':' )
        {
            isDerived = 1;

            char* tok = cur;

            int tmpLn;
            store_line_no( tmpLn );

            skip_next_token_back( tok );
            skip_token_back( tok );

            restore_line_no( tmpLn );

            // class name should precend ':' colon, thus
            // the one which was captured before was
            // proablty something else (like __dllexport MyClass : ... )

            if ( nameTok != tok )
            {
                pClass->m_Name = get_token_str( tok );
            }

        }

        if ( *cur == '{' )
            break;

        if ( *cur == ',' )
            continue;

        size_t len = get_token_len( cur );

        // skip neglectable C++ modifieres
        if ( cmp_tokens_fast( cur, "public", len ) )
            continue;

        if ( cmp_tokens_fast( cur, "protected", len ) )
            continue;

        if ( cmp_tokens_fast( cur, "private", len ) )
            continue;

        if ( cmp_tokens_fast( cur, "virtual", len ) )
            continue;

        // skip neglectable JAVA modifieres

        if ( cmp_tokens_fast( cur, "extends", len ) )
        {
            isDerived = 1;
            continue;
        }

        if ( cmp_tokens_fast( cur, "implements", len ) )
        {
            isDerived = 1;
            continue;
        }

        // all we need to know is superclass or interface

        char* tok = cur;
        int tmpLn;
        store_line_no( tmpLn );

        skip_token(tok);
        get_next_token(tok);

        restore_line_no( tmpLn );

        if ( *tok != ':' && *cur != ':' )

            pClass->m_SuperClassNames.push_back( wxString( cur, len ) );

    } while(1);

    if ( !isDerived )
    {
        int tmpLn;
        store_line_no( tmpLn );

        while ( pClass->m_SuperClassNames.size() )

            pClass->m_SuperClassNames.erase( &pClass->m_SuperClassNames[0] );

        char* tok = cur;

        // some non-obviouse token was following "class" keyword -
        // we've confused it with class name - thus now we're reverting this mistake

        skip_next_token_back( tok );
        skip_token_back( tok );

        pClass->m_Name = get_token_str( tok );

        restore_line_no( tmpLn );
    }


    ++cur; // skip opening curly brace

    pClass->mHeaderLength = ( cur - ctxStart );

    // now, enter the class context
    mpCurCtx = pClass;

    clear_commets_queue();
}
Example #15
0
/** Read all tokens from a string between <b>start</b> and <b>end</b>, and add
 * them to <b>out</b>.  Parse according to the token rules in <b>table</b>.
 * Caller must free tokens in <b>out</b>.  If <b>end</b> is NULL, use the
 * entire string.
 */
int
tokenize_string(memarea_t *area,
                const char *start, const char *end, smartlist_t *out,
                token_rule_t *table, int flags)
{
  const char **s;
  directory_token_t *tok = NULL;
  int counts[NIL_];
  int i;
  int first_nonannotation;
  int prev_len = smartlist_len(out);
  tor_assert(area);

  s = &start;
  if (!end) {
    end = start+strlen(start);
  } else {
    /* it's only meaningful to check for nuls if we got an end-of-string ptr */
    if (memchr(start, '\0', end-start)) {
      log_warn(LD_DIR, "parse error: internal NUL character.");
      return -1;
    }
  }
  for (i = 0; i < NIL_; ++i)
    counts[i] = 0;

  SMARTLIST_FOREACH(out, const directory_token_t *, t, ++counts[t->tp]);

  while (*s < end && (!tok || tok->tp != EOF_)) {
    tok = get_next_token(area, s, end, table);
    if (tok->tp == ERR_) {
      log_warn(LD_DIR, "parse error: %s", tok->error);
      token_clear(tok);
      return -1;
    }
    ++counts[tok->tp];
    smartlist_add(out, tok);
    *s = eat_whitespace_eos(*s, end);
  }

  if (flags & TS_NOCHECK)
    return 0;

  if ((flags & TS_ANNOTATIONS_OK)) {
    first_nonannotation = -1;
    for (i = 0; i < smartlist_len(out); ++i) {
      tok = smartlist_get(out, i);
      if (tok->tp < MIN_ANNOTATION || tok->tp > MAX_ANNOTATION) {
        first_nonannotation = i;
        break;
      }
    }
    if (first_nonannotation < 0) {
      log_warn(LD_DIR, "parse error: item contains only annotations");
      return -1;
    }
    for (i=first_nonannotation;  i < smartlist_len(out); ++i) {
      tok = smartlist_get(out, i);
      if (tok->tp >= MIN_ANNOTATION && tok->tp <= MAX_ANNOTATION) {
        log_warn(LD_DIR, "parse error: Annotations mixed with keywords");
        return -1;
      }
    }
    if ((flags & TS_NO_NEW_ANNOTATIONS)) {
      if (first_nonannotation != prev_len) {
        log_warn(LD_DIR, "parse error: Unexpected annotations.");
        return -1;
      }
    }
  } else {
    for (i=0;  i < smartlist_len(out); ++i) {
      tok = smartlist_get(out, i);
      if (tok->tp >= MIN_ANNOTATION && tok->tp <= MAX_ANNOTATION) {
        log_warn(LD_DIR, "parse error: no annotations allowed.");
        return -1;
      }
    }
    first_nonannotation = 0;
  }
  for (i = 0; table[i].t; ++i) {
    if (counts[table[i].v] < table[i].min_cnt) {
      log_warn(LD_DIR, "Parse error: missing %s element.", table[i].t);
      return -1;
    }
    if (counts[table[i].v] > table[i].max_cnt) {
      log_warn(LD_DIR, "Parse error: too many %s elements.", table[i].t);
      return -1;
    }
    if (table[i].pos & AT_START) {
      if (smartlist_len(out) < 1 ||
          (tok = smartlist_get(out, first_nonannotation))->tp != table[i].v) {
        log_warn(LD_DIR, "Parse error: first item is not %s.", table[i].t);
        return -1;
      }
    }
    if (table[i].pos & AT_END) {
      if (smartlist_len(out) < 1 ||
          (tok = smartlist_get(out, smartlist_len(out)-1))->tp != table[i].v) {
        log_warn(LD_DIR, "Parse error: last item is not %s.", table[i].t);
        return -1;
      }
    }
  }
  return 0;
}
Example #16
0
//entry point function
void parse_program(scanner_t* s) {
    token_stream = s;
    next_token = get_next_token(token_stream);
    cached_identificator.type = TT_NONE;
    parse_funcs();
}
Example #17
0
int to_RPN(char *src) {
	char buf0[1024];
	int i;
	int f;
	int top;
	int type;
	int value;
	double d_value;

	init_stack();

	strncpy(buf0, src, sizeof(buf0));
	if(!strncmp(src, "$", 1)) {
		f = 0;
		for(i = 1; src[i] != '\0'; i++) {
			if(src[i] == '=') f = 1;
			if(f == 0) variable_name[i - 1] = src[i];
			if(src[i] == '=') {
				strncpy(buf0, src + i + 1, sizeof(buf0));
				break;
			}
		}
		if(f == 0) {
			variable_name[0] = '\0';
		} else {
			variable_name[i - 1] = '\0';
		}
	}
	for( ;; ) {
		type = get_next_token(buf0, &value, &d_value);
		switch(type) {
		case 0: // End of String
			goto end_of_formula;
		case 1: // NUMBER
			append_value(d_value);
			break;
		case 2: // OPERATOR
		case 3: // SPLIT
		case 4: // SYMBOL
			if(pop(&top)) {
				/* empty stack */
				if(value == ')') {
					fprintf(stderr, "Error: Invalid syntax\n");
				}
				push(value);
			} else if(value == '(') {
				push(top);
				push(value);
			} else if(value == ')') {
				for( ;; ) {
					append_operator(top);
					if(pop(&top)) break;
					if(top == '(') break;
				}
			} else {
				if(order(value) == -1) {
					fprintf(stderr, "Error: Syntax error\n");
					break;
				}
				while(order(top) >= order(value)) {
					append_operator(top);
					if(pop(&top)) goto end_loop;
				}
				push(top);
			end_loop:
				push(value);
			}
			break;
		default:
			break;
		}
	}
end_of_formula:

	while(!pop(&top)) {
		if(top == '(') {
			fprintf(stderr, "Error: Invalid syntax\n");
			return -1;
		}
		append_operator(top);
	}

	term_stack();
	return 0;
}
Example #18
0
void match(enum e_token_t token) {
    if (next_token.type != token)
        error("Syntactic error: Failed to parse the program", ERROR_SYN);
    curr_token = next_token;
    next_token = get_next_token(token_stream);
}
Example #19
0
/*---------------------------------------------------------------------------*/
void
tokenizer_init(char *program)
{
	ptr = program;
	current_token = get_next_token();
}
Example #20
0
void match_deduction(enum e_token_t token) {
    if (next_token.type != token)
        error("Unitinitialized auto variable", ERROR_TYPE_DEDUCTION);
    curr_token = next_token;
    next_token = get_next_token(token_stream);
}
Example #21
0
int
main (int argc, char ** argv)
{
  int rc;
  int need_shell;
  char * cmdline;
  char * progname;
  int envsize;
  char **pass_through_args;
  int num_pass_through_args;
  char modname[MAX_PATH];
  char path[MAX_PATH];
  char dir[MAX_PATH];
  int status;

  interactive = TRUE;

  SetConsoleCtrlHandler ((PHANDLER_ROUTINE) console_event_handler, TRUE);

  if (!GetCurrentDirectory (sizeof (dir), dir))
    fail ("error: GetCurrentDirectory failed\n");

  /* We serve double duty: we can be called either as a proxy for the
     real shell (that is, because we are defined to be the user shell),
     or in our role as a helper application for running DOS programs.
     In the former case, we interpret the command line options as if we
     were a Unix shell, but in the latter case we simply pass our
     command line to CreateProcess.  We know which case we are dealing
     with by whether argv[0] refers to ourself or to some other program.
     (This relies on an arcane feature of CreateProcess, where we can
     specify cmdproxy as the module to run, but specify a different
     program in the command line - the MSVC startup code sets argv[0]
     from the command line.)  */

  if (!GetModuleFileName (NULL, modname, sizeof (modname)))
    fail ("error: GetModuleFileName failed\n");

  /* Change directory to location of .exe so startup directory can be
     deleted.  */
  progname = strrchr (modname, '\\');
  *progname = '\0';
  SetCurrentDirectory (modname);
  *progname = '\\';

  /* Due to problems with interaction between API functions that use "OEM"
     codepage vs API functions that use the "ANSI" codepage, we need to
     make things consistent by choosing one and sticking with it.  */
  SetConsoleCP (GetACP ());
  SetConsoleOutputCP (GetACP ());

  /* Although Emacs always sets argv[0] to an absolute pathname, we
     might get run in other ways as well, so convert argv[0] to an
     absolute name before comparing to the module name.  */
  path[0] = '\0';
  /* The call to SearchPath will find argv[0] in the current
     directory, append ".exe" to it if needed, and also canonicalize
     it, to resolve references to ".", "..", etc.  */
  status = SearchPath (NULL, argv[0], ".exe", sizeof (path), path,
				  &progname);
  if (!(status > 0 && stricmp (modname, path) == 0))
    {
      if (status <= 0)
	{
	  char *s;

	  /* Make sure we have argv[0] in path[], as the failed
	     SearchPath might not have copied it there.  */
	  strcpy (path, argv[0]);
	  /* argv[0] could include forward slashes; convert them all
	     to backslashes, for strrchr calls below to DTRT.  */
	  for (s = path; *s; s++)
	    if (*s == '/')
	      *s = '\\';
	}
      /* Perhaps MODNAME and PATH use mixed short and long file names.  */
      if (!(GetShortPathName (modname, modname, sizeof (modname))
	    && GetShortPathName (path, path, sizeof (path))
	    && stricmp (modname, path) == 0))
	{
	  /* Sometimes GetShortPathName fails because one or more
	     directories leading to argv[0] have issues with access
	     rights.  In that case, at least we can compare the
	     basenames.  Note: this disregards the improbable case of
	     invoking a program of the same name from another
	     directory, since the chances of that other executable to
	     be both our namesake and a 16-bit DOS application are nil.  */
	  char *p = strrchr (path, '\\');
	  char *q = strrchr (modname, '\\');
	  char *pdot, *qdot;

	  if (!p)
	    p = strchr (path, ':');
	  if (!p)
	    p = path;
	  else
	    p++;
	  if (!q)
	    q = strchr (modname, ':');
	  if (!q)
	    q = modname;
	  else
	    q++;

	  pdot = strrchr (p, '.');
	  if (!pdot || stricmp (pdot, ".exe") != 0)
	    pdot = p + strlen (p);
	  qdot = strrchr (q, '.');
	  if (!qdot || stricmp (qdot, ".exe") != 0)
	    qdot = q + strlen (q);
	  if (pdot - p != qdot - q || strnicmp (p, q, pdot - p) != 0)
	    {
	      /* We are being used as a helper to run a DOS app; just
		 pass command line to DOS app without change.  */
	      /* TODO: fill in progname.  */
	      if (spawn (NULL, GetCommandLine (), dir, &rc))
		return rc;
	      fail ("Could not run %s\n", GetCommandLine ());
	    }
	}
    }

  /* Process command line.  If running interactively (-c or /c not
     specified) then spawn a real command shell, passing it the command
     line arguments.

     If not running interactively, then attempt to execute the specified
     command directly.  If necessary, spawn a real shell to execute the
     command.

  */

  progname = NULL;
  cmdline = NULL;
  /* If no args, spawn real shell for interactive use.  */
  need_shell = TRUE;
  interactive = TRUE;
  /* Ask command.com to create an environment block with a reasonable
     amount of free space.  */
  envsize = get_env_size () + 300;
  pass_through_args = (char **) alloca (argc * sizeof (char *));
  num_pass_through_args = 0;

  while (--argc > 0)
    {
      ++argv;
      /* Act on switches we recognize (mostly single letter switches,
	 except for -e); all unrecognized switches and extra args are
	 passed on to real shell if used (only really of benefit for
	 interactive use, but allow for batch use as well).  Accept / as
	 switch char for compatibility with cmd.exe.  */
      if (((*argv)[0] == '-' || (*argv)[0] == '/') && (*argv)[1] != '\0')
	{
	  if (((*argv)[1] == 'c' || (*argv)[1] == 'C') && ((*argv)[2] == '\0'))
	    {
	      if (--argc == 0)
		fail ("error: expecting arg for %s\n", *argv);
	      cmdline = *(++argv);
	      interactive = FALSE;
	    }
	  else if (((*argv)[1] == 'i' || (*argv)[1] == 'I') && ((*argv)[2] == '\0'))
	    {
	      if (cmdline)
		warn ("warning: %s ignored because of -c\n", *argv);
	    }
	  else if (((*argv)[1] == 'e' || (*argv)[1] == 'E') && ((*argv)[2] == ':'))
	    {
	      int requested_envsize = atoi (*argv + 3);
	      /* Enforce a reasonable minimum size, as above.  */
	      if (requested_envsize > envsize)
		envsize = requested_envsize;
	      /* For sanity, enforce a reasonable maximum.  */
	      if (envsize > 32768)
		envsize = 32768;
	    }
	  else
	    {
	      /* warn ("warning: unknown option %s ignored", *argv); */
	      pass_through_args[num_pass_through_args++] = *argv;
	    }
	}
      else
	break;
    }

#if 0
  /* I think this is probably not useful - cmd.exe ignores extra
     (non-switch) args in interactive mode, and they cannot be passed on
     when -c was given.  */

  /* Collect any remaining args after (initial) switches.  */
  while (argc-- > 0)
    {
      pass_through_args[num_pass_through_args++] = *argv++;
    }
#else
  /* Probably a mistake for there to be extra args; not fatal.  */
  if (argc > 0)
    warn ("warning: extra args ignored after '%s'\n", argv[-1]);
#endif

  pass_through_args[num_pass_through_args] = NULL;

  /* If -c option, determine if we must spawn a real shell, or if we can
     execute the command directly ourself.  */
  if (cmdline)
    {
      const char *args;

      /* The program name is the first token of cmdline.  Since
         filenames cannot legally contain embedded quotes, the value
         of escape_char doesn't matter.  */
      args = cmdline;
      if (!get_next_token (path, &args))
        fail ("error: no program name specified.\n");

      canon_filename (path);
      progname = make_absolute (path);

      /* If we found the program and the rest of the command line does
         not contain unquoted shell metacharacters, run the program
         directly (if not found it might be an internal shell command,
         so don't fail).  */
      if (progname != NULL && try_dequote_cmdline (cmdline))
        need_shell = FALSE;
      else
        progname = NULL;
    }

 pass_to_shell:
  if (need_shell)
    {
      char * p;
      int    extra_arg_space = 0;
      int    maxlen, remlen;
      int    run_command_dot_com;

      progname = getenv ("COMSPEC");
      if (!progname)
	fail ("error: COMSPEC is not set\n");

      canon_filename (progname);
      progname = make_absolute (progname);

      if (progname == NULL || strchr (progname, '\\') == NULL)
	fail ("error: the program %s could not be found.\n", getenv ("COMSPEC"));

      /* Need to set environment size when running command.com.  */
      run_command_dot_com =
	(stricmp (strrchr (progname, '\\'), "command.com") == 0);

      /* Work out how much extra space is required for
         pass_through_args.  */
      for (argv = pass_through_args; *argv != NULL; ++argv)
	/* We don't expect to have to quote switches.  */
	extra_arg_space += strlen (*argv) + 2;

      if (cmdline)
	{
	  char * buf;

	  /* Convert to syntax expected by cmd.exe/command.com for
	     running non-interactively.  Always quote program name in
	     case path contains spaces (fortunately it can't contain
	     quotes, since they are illegal in path names).  */

	  remlen = maxlen =
	    strlen (progname) + extra_arg_space + strlen (cmdline) + 16 + 2;
	  buf = p = alloca (maxlen + 1);

	  /* Quote progname in case it contains spaces.  */
	  p += _snprintf (p, remlen, "\"%s\"", progname);
	  remlen = maxlen - (p - buf);

	  /* Include pass_through_args verbatim; these are just switches
             so should not need quoting.  */
	  for (argv = pass_through_args; *argv != NULL; ++argv)
	    {
	      p += _snprintf (p, remlen, " %s", *argv);
	      remlen = maxlen - (p - buf);
	    }

	  /* Now that we know we will be invoking the shell, quote the
	     command line after the "/c" switch as the shell expects:
	     a single pair of quotes enclosing the entire command
	     tail, no matter whether quotes are used in the command
	     line, and how many of them are there.  See the output of
	     "cmd /?" for how cmd.exe treats quotes.  */
	  if (run_command_dot_com)
	    _snprintf (p, remlen, " /e:%d /c \"%s\"", envsize, cmdline);
	  else
	    _snprintf (p, remlen, " /c \"%s\"", cmdline);
	  cmdline = buf;
	}
      else
	{
	  if (run_command_dot_com)
	    {
	      /* Provide dir arg expected by command.com when first
		 started interactively (the "command search path").  To
		 avoid potential problems with spaces in command dir
		 (which cannot be quoted - command.com doesn't like it),
		 we always use the 8.3 form.  */
	      GetShortPathName (progname, path, sizeof (path));
	      p = strrchr (path, '\\');
	      /* Trailing slash is acceptable, so always leave it.  */
	      *(++p) = '\0';
	    }
	  else
	    path[0] = '\0';

	  remlen = maxlen =
	    strlen (progname) + extra_arg_space + strlen (path) + 13;
	  cmdline = p = alloca (maxlen + 1);

	  /* Quote progname in case it contains spaces.  */
	  p += _snprintf (p, remlen, "\"%s\" %s", progname, path);
	  remlen = maxlen - (p - cmdline);

	  /* Include pass_through_args verbatim; these are just switches
             so should not need quoting.  */
	  for (argv = pass_through_args; *argv != NULL; ++argv)
	    {
	      p += _snprintf (p, remlen, " %s", *argv);
	      remlen = maxlen - (p - cmdline);
	    }

	  if (run_command_dot_com)
	    _snprintf (p, remlen, " /e:%d", envsize);
	}
    }

  if (!progname)
    fail ("Internal error: program name not defined\n");

  if (!cmdline)
    cmdline = progname;

  if (spawn (progname, cmdline, dir, &rc))
    return rc;

  if (!need_shell)
    {
      need_shell = TRUE;
      goto pass_to_shell;
    }

  fail ("Could not run %s\n", progname);

  return 0;
}
Example #22
0
/*
 * lbone_getDepot2DepotResolution
 *      contact lbone server to discover the resolution (either NWS or
 *      distance) from each depot in srcList to each in targetList.  These
 *      results are returned in a two dimentional array.
 *
 *      values of src to target, can be indexed as follows 
 *
 *      double res = lbone_getDepot2DepotResolution(...)
 *      bw = res[0][1]       // indicates the resolution from src depot 0 
 *                           // to target depot 1
 *
 *      To obtain both to and from results of all depots, simply make srcList
 *      and targetList identical.  All entries where the src and target are
 *      equal, the resolution will be zero.
 *
 */
int lbone_getDepot2DepotResolution(double ***resArray, 
                                   Depot lboneServer, Depot *srcList, Depot *targetList,
                                   int type, unsigned int timeout)
{
    int         si, ti;
    struct timeval        timer;
    struct itimerval        int_timer;
    char                    version[10], header[10];
    char                   *temp;
    int           retval;
    int           i, j;
    int           fd;
    int           srcCount;
    int           targetCount;
    double    **resolution;

  int           fd_ready;

  char          message[LBONE_MESSAGE_LENGTH];
  Depot        *depots;
  fd_set        read_fds;

  /* error check the Depot struct */

  if ((srcList == NULL) || (targetList == NULL) || type < 0 )
  {
    fprintf(stderr, "lbone_client: NULL depot lists\n");
    return 1;
  }

  if (timeout > 0)
    memset(&timer, 0, sizeof(struct timeval));

  memset(&int_timer, 0, sizeof(struct itimerval));
  int_timer.it_value.tv_sec = (long) timeout;

  /* count size of src and target lists */
  for ( i=0; srcList[i] != NULL; i++);
  si = i;
  for ( i=0; targetList[i] != NULL; i++);
  ti = i;
  if ( si == 0 || ti == 0 )
  {
      return 1;
  }

  /* build string to send over socket */

  memset(version, 0, 10);
  strcpy(version, LBONE_CLIENT_VERSION);

    temp = (char *) malloc(LBONE_MESSAGE_LENGTH);
    memset(temp, 0, LBONE_MESSAGE_LENGTH);

    sprintf(temp, "%-10s%-10d%-10d%-10d%-10d", version, DEPOT2DEPOT_RESOLUTION, type, si, ti);

    memset(message, 0, LBONE_MESSAGE_LENGTH);
    strncpy(message, temp, LBONE_MESSAGE_LENGTH);
    free(temp);

    /* start signal handler and then start timer */

    signal(SIGALRM, sigalarm_handler);

    setitimer(ITIMER_REAL, &int_timer, NULL);

    /* request_connection() and write message */

    fd = request_connection(lboneServer->host, lboneServer->port);
    if (fd < 0) {
        perror("lbone_client: request_connection failed");
        return 1;
        /* exit(1); */
    }

    getitimer(ITIMER_REAL, &int_timer);
    timer.tv_sec = int_timer.it_value.tv_sec;
    int_timer.it_value.tv_sec = 0;
    int_timer.it_value.tv_usec = 0;
    setitimer(ITIMER_REAL, &int_timer, NULL);

    retval = write(fd, message, strlen(message));
    if (retval != strlen(message)) {
        fprintf(stderr, "lbone_client: write to socket failed. ");
        fprintf(stderr, "Only %d characters written.\n", retval);
        return 1;
    }
    temp = (char *) calloc( LBONE_MESSAGE_LENGTH, 1);
    for ( i=0; i < si; i++)
    {
        snprintf(temp, 266, "%-256s%-10d", srcList[i]->host, srcList[i]->port);
        retval = write(fd, temp, 266);
        if (retval != 266) 
        {
            fprintf(stderr, "lbone_client: write to socket failed. ");
            fprintf(stderr, "Only %d characters written.\n", retval);
            return 1;
        }
    }
    for ( i=0; i < ti; i++)
    {
        snprintf(temp, 266, "%-256s%-10d", targetList[i]->host, targetList[i]->port);
        retval = write(fd, temp, 266);
        if (retval != 266) 
        {
            fprintf(stderr, "lbone_client: write to socket failed. ");
            fprintf(stderr, "Only %d characters written.\n", retval);
            return 1;
        }
    }
    free(temp);

    /* get select() ready */

    FD_ZERO(&read_fds);
    FD_SET(fd, &read_fds);

    /* receive reply header */

    if (timeout > 0) {
        fd_ready = select(fd+1, &read_fds, NULL, NULL, &timer);
    } else {
        fd_ready = select(fd+1, &read_fds, NULL, NULL, NULL);
    }

    if (fd_ready < 0)
    {
        perror("lbone_client: select returned an error");
        return 1;
    }

    if (fd_ready > 0)
    {
        memset(header, 0, 10);
        retval = get_next_token(fd, header, 10);
        header[9] = '\0';

        if (atoi(header) == SUCCESS)  /* get count */
        {
            memset(header, 0, 10);
            retval = get_next_token(fd, header, 10);
            header[9] = '\0';
            srcCount = atoi(header);

            memset(header, 0, 10);
            retval = get_next_token(fd, header, 10);
            header[9] = '\0';
            targetCount = atoi(header);

            /*fprintf(stderr, "srccnt: %d  targetCount: %d\n", srcCount, targetCount);*/
        }
        else
        {                          /* get errno */
            memset(header, 0, 10);
            retval = get_next_token(fd, header, 10);
            header[9] = '\0';
            srcCount = atoi(header);

            fprintf(stderr, "lbone_getDepots: server returned error %d\n", srcCount);
            return 1;
        }

        /* receive depots and build array */

        resolution = (double **) calloc(sizeof(double *) * (srcCount), 1);
        for (i=0; i < srcCount; i++)
        {
            resolution[i] = (double *) calloc(sizeof(double) * (targetCount), 1);
        }

        for (i = 0; i < srcCount; i++)
        {
            for (j=0; j < targetCount; j++)
            {
                memset(header, 0, 10);
                retval = get_next_token(fd, header, 10);
                header[9] = '\0';
                resolution[i][j] = atof(header);
            }
        } 
    } else
    {               /* function timed out */
        fprintf(stderr, "lbone_client: message header did not arrive before timeout.\n");
        //depots = NULL;  /* SA 1-22-03  why dset this to nNULL? */
        close(fd);
        return -1;
    }

    close(fd);

  /* return depots */
    *resArray = resolution;
    return 0 ;
}
Example #23
0
int incbin_file(char *name, int *id, int *swap, int *skip, int *read, struct macro_static **macro) {

    struct incbin_file_data *ifd;
    char *in_tmp, *n, *tmp_c;
    int file_size, q;
    FILE *f;


    /* create the full output file name */
    if (use_incdir == YES)
        tmp_c = ext_incdir;
    else
        tmp_c = include_dir;

    if (create_full_name(tmp_c, name) == FAILED)
        return FAILED;

    f = fopen(full_name, "rb");
    q = 0;

    if (f == NULL && (tmp_c == NULL || tmp_c[0] == 0)) {
        sprintf(emsg, "Error opening file \"%s\".\n", name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* if not found in ext_incdir silently try the include directory */
    if (f == NULL && use_incdir == YES) {
        if (create_full_name(include_dir, name) == FAILED)
            return FAILED;

        f = fopen(full_name, "rb");
        q = 0;

        if (f == NULL && (include_dir == NULL || include_dir[0] == 0)) {
            sprintf(emsg, "Error opening file \"%s\".\n", name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* if failed try to find the file in the current directory */
    if (f == NULL) {
        fprintf(stderr, "%s:%d: ", get_file_name(active_file_info_last->filename_id), active_file_info_last->line_current);
        fprintf(stderr, "INCBIN_FILE: Could not open \"%s\", trying \"%s\"... ", full_name, name);
        f = fopen(name, "rb");
        q = 1;
    }

    if (f == NULL) {
        fprintf(stderr, "not found.\n");
        sprintf(emsg, "Error opening file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    if (q == 1) {
        fprintf(stderr, "found.\n");
        strcpy(full_name, name);
    }

    fseek(f, 0, SEEK_END);
    file_size = ftell(f);
    fseek(f, 0, SEEK_SET);

    ifd = (struct incbin_file_data *)malloc(sizeof(struct incbin_file_data));
    n = malloc(sizeof(char) * (strlen(full_name)+1));
    in_tmp = (char *)malloc(sizeof(char) * file_size);
    if (ifd == NULL || n == NULL || in_tmp == NULL) {
        if (ifd != NULL)
            free(ifd);
        if (n != NULL)
            free(n);
        if (in_tmp != NULL)
            free(in_tmp);
        sprintf(emsg, "Out of memory while allocating data structure for \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* read the whole file into a buffer */
    fread(in_tmp, 1, file_size, f);
    fclose(f);

    /* complete the structure */
    ifd->next = NULL;
    ifd->size = file_size;
    strcpy(n, full_name);
    ifd->name = n;
    ifd->data = in_tmp;

    /* find the index */
    q = 0;
    if (incbin_file_data_first != NULL) {
        ifd_tmp = incbin_file_data_first;
        while (ifd_tmp->next != NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp = ifd_tmp->next;
            q++;
        }
        if (ifd_tmp->next == NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp->next = ifd;
            q++;
        }
    }
    else
        incbin_file_data_first = ifd;

    *id = q;

    /* SKIP bytes? */
    if (compare_next_token("SKIP", 4) == FAILED)
        *skip = 0;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of skipped bytes.\n", ERROR_DIR);
            return FAILED;
        }

        *skip = d;

        if (d >= file_size) {
            sprintf(emsg, "SKIP value (%d) is more than the size (%d) of file \"%s\".\n", d, file_size, full_name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* READ bytes? */
    if (compare_next_token("READ", 4) == FAILED)
        *read = file_size - *skip;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of bytes for reading.\n", ERROR_DIR);
            return FAILED;
        }

        *read = d;

        if (*skip + *read > file_size) {
            sprintf(emsg, "Overreading file \"%s\" by %d bytes.\n", full_name, *skip + *read - file_size);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* SWAP bytes? */
    if (compare_next_token("SWAP", 4) == FAILED)
        *swap = 0;
    else {
        if ((*read & 1) == 1) {
            sprintf(emsg, "The read size of file \"%s\" is odd (%d)! Cannot perform SWAP.\n", full_name, *read);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
        *swap = 1;
        skip_next_token();
    }

    /* FSIZE? */
    if (compare_next_token("FSIZE", 5) == SUCCEEDED) {
        skip_next_token();

        /* get the definition label */
        if (get_next_token() == FAILED)
            return FAILED;

        add_a_new_definition(tmp, (double)file_size, NULL, DEFINITION_TYPE_VALUE, 0);
    }

    /* FILTER? */
    if (compare_next_token("FILTER", 6) == SUCCEEDED) {
        skip_next_token();

        /* get the filter macro name */
        if (get_next_token() == FAILED)
            return FAILED;

        *macro = macro_get(tmp);

        if (*macro == NULL) {
            sprintf(emsg, "No MACRO \"%s\" defined.\n", tmp);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }
    else
        *macro = NULL;

    return SUCCEEDED;
}
Example #24
0
static s32_t cpiofs_readdir(struct vnode_t * node, struct file_t * fp, struct dirent_t * dir)
{
	struct block_t * dev = (struct block_t *)node->v_mount->m_dev;
	struct cpio_newc_header header;
	char path[MAX_PATH];
	char name[MAX_NAME];
	u32_t size, name_size, mode;
	loff_t off = 0;
	char buf[9];
	s32_t i = 0;

	if(fp->f_offset == 0)
	{
		dir->d_type = DT_DIR;
		strlcpy((char *)&dir->d_name, (const char *)".", sizeof(dir->d_name));
	}
	else if(fp->f_offset == 1)
	{
		dir->d_type = DT_DIR;
		strlcpy((char *)&dir->d_name, (const char *)"..", sizeof(dir->d_name));
	}
	else
	{
		while(1)
		{
			block_read(dev, (u8_t *)(&header), off, sizeof(struct cpio_newc_header));

			if(strncmp((const char *)(header.c_magic), (const char *)"070701", 6) != 0)
				return ENOENT;

			buf[8] = '\0';

			memcpy(buf, (const s8_t *)(header.c_filesize), 8);
			size = strtoul((const char *)buf, NULL, 16);

			memcpy(buf, (const s8_t *)(header.c_namesize), 8);
			name_size = strtoul((const char *)buf, NULL, 16);

			memcpy(buf, (const s8_t *)(header.c_mode), 8);
			mode = strtoul((const char *)buf, NULL, 16);

			block_read(dev, (u8_t *)path, off + sizeof(struct cpio_newc_header), (loff_t)name_size);

			if( (size == 0) && (mode == 0) && (name_size == 11) && (strncmp(path, (const char *)"TRAILER!!!", 10) == 0) )
				return ENOENT;

			off = off + sizeof(struct cpio_newc_header) + (((name_size + 1) & ~3) + 2) + size;
			off = (off + 3) & ~3;

			if(!get_next_token(path, (const char *)node->v_path, name))
				continue;

			if(i++ == fp->f_offset - 2)
			{
				off = 0;
				break;
			}
		}

		if(mode & 0040000)
			dir->d_type = DT_DIR;
		else
			dir->d_type = DT_REG;
		strlcpy((char *)&dir->d_name, name, sizeof(name));
	}

	dir->d_fileno = (u32_t)fp->f_offset;
	dir->d_namlen = (u16_t)strlen((const char *)dir->d_name);
	fp->f_offset++;

	return 0;
}
Example #25
0
int
cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
	unsigned ressize)
{
	struct cmdline_token_string *tk2;
	struct cmdline_token_string_data *sd;
	unsigned int token_len;
	const char *str;

	if (res && ressize < STR_TOKEN_SIZE)
		return -1;

	if (!tk || !buf || ! *buf)
		return -1;

	tk2 = (struct cmdline_token_string *)tk;

	sd = &tk2->string_data;

	/* fixed string (known single token) */
	if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) != 0)) {
		str = sd->str;
		do {
			token_len = get_token_len(str);

			/* if token is too big... */
			if (token_len >= STR_TOKEN_SIZE - 1) {
				continue;
			}

			if ( strncmp(buf, str, token_len) ) {
				continue;
			}

			if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
				continue;
			}

			break;
		} while ( (str = get_next_token(str)) != NULL );

		if (!str)
			return -1;
	}
	/* multi string */
	else if (sd->str != NULL) {
		if (ressize < STR_MULTI_TOKEN_SIZE)
			return -1;

		token_len = 0;
		while (!cmdline_isendofcommand(buf[token_len]) &&
		      token_len < (STR_MULTI_TOKEN_SIZE - 1))
			token_len++;

		/* return if token too long */
		if (token_len >= (STR_MULTI_TOKEN_SIZE - 1))
			return -1;
	}
	/* unspecified string (unknown single token) */
	else {
		token_len = 0;
		while(!cmdline_isendoftoken(buf[token_len]) &&
		      token_len < (STR_TOKEN_SIZE-1))
			token_len++;

		/* return if token too long */
		if (token_len >= STR_TOKEN_SIZE - 1) {
			return -1;
		}
	}

	if (res) {
		if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
			/* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */
			snprintf(res, STR_MULTI_TOKEN_SIZE, "%s", buf);
		else
			/* we are sure that token_len is < STR_TOKEN_SIZE-1 */
			snprintf(res, STR_TOKEN_SIZE, "%s", buf);

		*((char *)res + token_len) = 0;
	}

	return token_len;
}
Example #26
0
uint32_t opp_parse(token_node * lookback_ptr, 
		      token_node * lookahead_ptr, 
		      token_node * list_begin, 
		      token_node * chunk_end, 
		      parsing_ctx * ctx)
{
    uint32_t parse_result = 0;
    reduction_list *main_reduction_list, *temp_reduction_list;	/* reduction_list represents where we are inside the reduction tree. */
    uint32_t end_chunk_parse = __MORE_INPUT;
    uint32_t reduction_error = 0;
    uint32_t node = 0;
    token_node * current_list_pos = list_begin;
    token_node * prev_symbol_ptr    = lookback_ptr;
    token_node * list_itr         = NULL;
    vect_stack yields_prec_stack,parsing_stack;
    token_node * last_terminal = NULL;
    uint32_t prec=0;
    vect_stack stack;
    
    init_vect_stack(&stack, ctx->NODE_ALLOC_SIZE);
    init_vect_stack(&yields_prec_stack, ctx->PREC_ALLOC_SIZE);
    init_vect_stack(&parsing_stack,     ctx->PREC_ALLOC_SIZE);

    main_reduction_list = (reduction_list *) malloc(sizeof(reduction_list));
    init_reduction_list(main_reduction_list);

    temp_reduction_list = (reduction_list *) malloc(sizeof(reduction_list));
    init_reduction_list(temp_reduction_list);

    /* set correctly last_terminal and current_pos */
    while ( !( is_terminal(current_list_pos->token) || end_chunk_parse ) ) {
	    end_chunk_parse = get_next_token(&current_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr); 
	}
    last_terminal= is_terminal(lookback_ptr->token) ? lookback_ptr : current_list_pos;
    /* Start the parsing process. */
    while (current_list_pos != NULL) {
	/* lookup precedence between last stack terminal token and new token. */
        prec = get_precedence(current_list_pos,last_terminal);
        /*this was inserted by the guys: add the string terminator to the precedence table to remove this cruft */
        if (lookback_ptr->token == __TERM && prec == __EQ && yields_prec_stack.top_of_stack == 0) {
	     prec = __GT;
        }
	/* Invalid precedence value fetched from precedence table, abort parsing */
	if (prec == __NOP) {
	    parse_result = __PARSE_ERROR;
	    break;
	}
	/* if precedence is __EQ or __LT, we should shift */
	if (prec != __GT || yields_prec_stack.top_of_stack == 0) {
	    if (end_chunk_parse == __END_OF_INPUT) {
		parse_result = __PARSE_IN_PROGRESS;
		break;
	    }
	    /* Push token_node on top of yields_prec_stack. */
	    if (prec == __LT) {
               vect_stack_push(&yields_prec_stack, last_terminal, ctx->PREC_REALLOC_SIZE);
	    }
	    /* Get next token. */
	    assert(is_terminal(current_list_pos->token));
	    vect_stack_push(&parsing_stack, last_terminal, ctx->NODE_REALLOC_SIZE);
	    last_terminal=current_list_pos;
	    end_chunk_parse = get_next_token(&current_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr);
	} else {
		/*clear reduction list */
	        main_reduction_list->idx_last = 0;
		/* node is the offset of the root of the vectorized reduction trie. */
		node = vect_reduction_tree[0];
		/* obtain the position of the previous yields precedence */
		prev_symbol_ptr = vect_stack_pop(&yields_prec_stack);
	        /* add pop to parsing stack to remove all the terminals up to the one 
		 which should be reduced */
		while (last_terminal!= prev_symbol_ptr && last_terminal!=NULL){
		  last_terminal=vect_stack_pop(&parsing_stack);
		}
		/* the stack pop should always be successful, as TOS !=0 
		 * it should_never get here*/
		assert(prev_symbol_ptr !=NULL);
		list_itr = prev_symbol_ptr->next;
		/* Set the first element of the reduction list to the root. */
		append_position_on_reduction_list(main_reduction_list, node);
		reduction_error = 0;

		/* starting from the previous yields precedence scan the candidate rhs and 
		 *match it against the reduction trie */
		while (list_itr != NULL && list_itr != current_list_pos && list_itr != chunk_end) {
		    /* For each available position in the reduction tree. */
		    uint32_t i;
		    for (i = 0; i < main_reduction_list->idx_last; i++) {
			reduction_step(temp_reduction_list, 
				       get_reduction_position_at_index(main_reduction_list, i), 
				       list_itr, 
		                       ctx);
		    }
		    swap_reduction_lists(&temp_reduction_list, &main_reduction_list);
		    /* erase temp_reduction_list */
		    temp_reduction_list->idx_last = 0;
		    list_itr = list_itr->next;
		    /* If there is at least one available position in the reduction tree after having considered current token, go on. */
		    if (main_reduction_list->idx_last <= 0) {
			reduction_error = 1;
			break;
		    }
		}
		
		/* Finished handle identification. */
		if (!reduction_error && vect_reduction_tree[get_reduction_position_at_index(main_reduction_list, 0)] == __GRAMMAR_SIZE) {
		    DEBUG_PRINT("Not in reducible position.\n")
			reduction_error = 1;
		}
		if (!reduction_error) {
		    call_semantics(main_reduction_list, prev_symbol_ptr, &stack, ctx);
		} else {
		    parse_result = __PARSE_NOT_RECOGNIZED;
		    break;
		}
		/* if the axiom is reached and only two terminators are left reduce and exit */
		if (lookback_ptr->token == __TERM && ctx->token_list->next == NULL && current_list_pos->token == __TERM &&
		    !is_terminal(ctx->token_list->token) && rewrite_to_axiom(ctx->token_list->token)) {
		    perform_rewrite(lookback_ptr, ctx->token_list->token, __S, &stack, ctx);
		    parse_result = __PARSE_SUCCESS;
		    break;
		}
	    }
	/* If next token is a nonterminal just move on. Eventually you might hit the terminator */
	while ( !( is_terminal(current_list_pos->token) || end_chunk_parse ) ) {
	    end_chunk_parse = get_next_token(&current_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr);
	}
    }/*end of the parsing loop */
    cleanup_stack_and_lists(&yields_prec_stack, main_reduction_list, temp_reduction_list);
    return parse_result;
}
Example #27
0
void CJSourceParser::ParseKeyword( char*& cur )
{
    // analyze token, which identifies the begining of a new context

    if ( CheckVisibilty( cur ) )
    {
        skip_token( cur );
        return;
    }

    if ( is_class_token( cur ) )
    {
        if ( is_forward_decl( cur ) )
        {
            // forward declarations are ignored;
            skip_token( cur );
            return;
        }

        if ( mNestingLevel == 0 )
        {
            // change context form global class context
            mCurCtxType = SP_CTX_CLASS;
        }

        ++mNestingLevel;

        // add information about new class (name, inheritance, etc)
        AddClassNode( cur );

        // the default visiblity for class members is 'private'
        mCurVis = SP_VIS_PRIVATE;

        return;
    }

    size_t len = get_token_len( cur );

    if ( cmp_tokens_fast( cur, "typedef", len  ) )
    {
        skip_token(cur);
        get_next_token(cur);

        if ( cmp_tokens_fast( cur, "struct", len ) ||
             cmp_tokens_fast( cur, "union",  len ) ||
             cmp_tokens_fast( cur, "class",  len )
           )
        {
            if ( mNestingLevel == 0 )
            {
                // change context form global class context
                mCurCtxType = SP_CTX_CLASS;
            }

            ++mNestingLevel;

            // add information about new class (name, inheritance, etc)
            AddClassNode( cur );

            // the default visiblity for class members is 'private'
            mCurVis = SP_VIS_PRIVATE;

            return;

            // FOR NOW:: typedef struct, etc are also ignored
            //skip_scope_block( cur );
        }

        if ( cmp_tokens_fast( cur, "enum", len  ) )
        {
            AddEnumNode( cur );
            return;
        }

        AddTypeDefNode( cur );

        return;
    }

    if ( cmp_tokens_fast( cur, "enum", len ) )
    {
        AddEnumNode( cur );
        return;
    }

    if ( cmp_tokens_fast( cur, "extern", len ) )
    {
        // extern's are ignored (both extern "C" and extern vars)
        while ( *cur != '{' &&
                *cur != ';' )
        {
            skip_token( cur );
            get_next_token( cur );
        }
        return;

    }
    if ( cmp_tokens_fast( cur, "enum", len ) )
    {
        // enumeration blocks are ignored

        skip_scope_block( cur );

        get_next_token( cur );
        skip_token( cur ); // skip ';' token;
        return;
    }

    if ( cmp_tokens_fast( cur, "package", len  ) )
    {
        // packages are ignored
        skip_statement( cur );
        return;
    };

    if ( cmp_tokens_fast( cur, "import", len  ) )
    {
        // import statements are ignored
        skip_statement( cur );
        return;
    }

    if ( cmp_tokens_fast( cur, "virtual", len  ) )
    {
        // probably the virtual method is in front of us;
        mIsVirtual = 1;
        skip_token( cur );
        return;
    }

    if ( cmp_tokens_fast( cur, "template", len  ) )
    {
        mIsTemplate = 1;
        skip_tempalate_statement( cur );
        return;
    }

    if ( cmp_tokens_fast( cur, "friend", len  ) )
    {
        skip_statement( cur );
        return;
    }

    // ingnore "unsigificant" tokens (i.e. which do not
    // affect the current parsing context)

    skip_token( cur );
}
void om_cfg_entry_list_callback(char * cfg)
{
	char * domain, * ip, * port, * prefix;
	char * p;
	int len;
	om_cfg_t * pcl;

        if(cfg==NULL) {
		// This is mostly called at initialization time.
		return;
        }

	pcl = (om_cfg_t *)nkn_malloc_type( sizeof(om_cfg_t), mod_mgmta_charbuf );
	if( !pcl ) return;

	// Format of the string will be "server port prefix"
	p = cfg;

	// get domain
	domain = get_next_token (p, &len);
	if(!domain) return; //error
	p+=len;

        // get ip
	ip = get_next_token (p, &len);
	if(!ip) return; //error
	p+=len;

        // get port
	port = get_next_token (p, &len);
	if(!port) return; //error
	p+=len;

	// get proxy
	prefix = get_next_token (p, &len);
	if(!prefix) return; //error
	p+=len;

	pcl->domain = nkn_strdup_type(domain, mod_mgmta_charbuf);
	pcl->ip = nkn_strdup_type(ip, mod_mgmta_charbuf);
	pcl->port = htons(atoi(port));
	pcl->prefix = nkn_strdup_type(prefix, mod_mgmta_charbuf);
	pcl->prefix_len = strlen(prefix);
	pcl->next = NULL;

	/*
	printf("domain=%s port=%d prefix=%s\n",
		omcfg_list[ glob_om_tot_cfg_policy ].domain,
		ntohs(omcfg_list[ glob_om_tot_cfg_policy ].port),
		omcfg_list[ glob_om_tot_cfg_policy ].prefix);
	*/
	if( omcfg_list == NULL ) {
		omcfg_list=pcl;
	}
	else {
		om_cfg_t * ptmp;
		ptmp=omcfg_list;
		while(ptmp->next) { ptmp=ptmp->next; }
		ptmp->next=pcl;
	}
	glob_om_tot_cfg_policy ++;
}
Example #29
0
bool CJSourceParser::ParseArguments( char*& cur )
{
    // DANGER-MACROS::

    // now cursor position is right after the first opening bracket
    // of the function declaration

    char* blocks    [16]; // used exclusivelly for iterative "lean out"
                          // of macros and misc. not-obviouse grammar
                          // (dirty,, but we cannot do it very nice,
                          //  we're not preprocessor-free C/C++ code)
    int   blockSizes[16];

    do
    {
        size_t blocksSkipped = 0;

        get_next_token( cur );

        bool first_blk = true;

        while( *cur != ')' && *cur != ',' )
        {
            blocks[blocksSkipped] = cur;

            if ( first_blk )
            {
                char* prev = cur;
                skip_token( cur );

                blockSizes[blocksSkipped] = size_t(cur-prev);

                first_blk = 0;
            }
            else
                blockSizes[blocksSkipped] = skip_block( cur );

            get_next_token( cur );
            ++blocksSkipped;
        }


        if ( blocksSkipped == 1 )
        {
            // check if the empty arg. list stressed with "void" inside
            if ( cmp_tokens_fast( blocks[0] , "void", 4 ) )
            {
                cur++;  // skip ')'

                break;
            }

            // FIXME:: TBD:: K&R-style function declarations!

            // if only one block enclosed, than it's probably
            // some macro, there should be at least two blocks,
            // one for argument type and another for it's identifier
            return false;
        }

        if ( blocksSkipped == 0 )
        {
            if ( *cur == 10 ) ++_gLineNo;
            ++cur; // skip ')'

            break; // function without paramters
        }

        // we should be in the operation context now
        spOperation* pOp = (spOperation*)mpCurCtx;

        spParameter* pPar = new spParameter();

        pOp->AddMember( pPar );
        // FOR NOW:: line number is not exact if argument list is mutiline
        pPar->mSrcLineNo = get_line_no();

        size_t nameBlock = blocksSkipped - 1;
        size_t typeBlock = nameBlock - 1;

        // check if default values present
        if ( *blocks[typeBlock] == '=' )
        {
            // expressions like "int = 5" are ignored,
            // since name for paramters is required
            if ( blocksSkipped == 3 )
            {
                if ( *cur == ')' )
                {
                    ++cur;
                    break;
                }
            else
                continue;
            }

            pPar->m_InitVal = wxString( blocks[nameBlock], blockSizes[nameBlock] );

            nameBlock = nameBlock - 2; // skip '=' token and default value block
            typeBlock = nameBlock - 1;
        }

        // attach comments about the parameter
        AttachComments( *pPar, blocks[nameBlock] );

        // retrieve argument name
        pPar->m_Name = wxString( blocks[nameBlock], blockSizes[nameBlock] );

        // retreive argument type

        size_t len = blockSizes[ typeBlock ];
        len = size_t ( (blocks[ typeBlock ] + len) - blocks[ 0 ] );

        pPar->m_Type = wxString( blocks[0], len );

        arrange_indirection_tokens_between( pPar->m_Type, pPar->m_Name );

        if ( *cur == ')' )
        {
            ++cur;
            break;
        }

        ++cur; // skip comma
        get_next_token(cur);

    } while(1);

    // skip possible whitespace between ')' and following "const"
    while ( isspace(*cur) )
        cur++;

    // check if it was really a function not a macro,
    // if so, than it should be terminated with semicolon ';'
    // or opening implemenetaton bracket '{'

    char* tok = cur;

    int tmpLnNo;
    store_line_no( tmpLnNo );

    bool result = true;

    do
    {
        if ( *tok == '{' || *tok == ';' )
        {
            restore_line_no(tmpLnNo);
            break;
        }

        // check for unexpected tokens
        if ( *tok == '=' || *tok == '0' )
        {
            skip_token(tok);
            if ( !get_next_token(tok) ) return false;
            continue;
        }

        if ( *tok == '}' ) return false;

        // if initialization list found
        if ( *tok == ':' )
        {
            restore_line_no(tmpLnNo);
            break;
        }

        if ( cmp_tokens_fast( tok, "const", 5 ) )
        {
            ((spOperation*)mpCurCtx)->mIsConstant = true;

            skip_token(tok);
            if ( !get_next_token(tok) ) return false;
            continue;
        }

        if ( CheckVisibilty( tok ) ) return false;

        // if next context found
        if ( is_keyword( tok ) ) return false;

        skip_token(tok);
        if ( !get_next_token(tok) ) return false;

    } while(1);

    return result;
}
int get_next_entry(char *log_file, char *uri, uint64_t *content_len, int *resp,
		   uint64_t *start_offset, uint64_t *end_offset, int *provider,
		   int *cacheable, time_t *acs_time, int *partial)
{
    char line[20480];
    char *linep = NULL;
    char *tmp_string = NULL;
    int parse_offset = 0;
    char req_file[20480], method[32], http_id[32];
    memset(req_file,0,sizeof(req_file));
    memset(line,0,sizeof(line));
    memset(method, 0, sizeof(method));
    memset(http_id, 0, sizeof(http_id));
    static char running_char = '|';
    struct tm time_val = { 0 };

    *content_len = 0;
    *resp = 0;
    *start_offset = 0;
    *end_offset = 0;
    *provider = 0;
    *cacheable = 1;
    *acs_time = 0;

    if(!fp) {
	fp = fopen(log_file,"r");
	printf("#|");
	if(!fp)
	    return errno;
	rewind(fp);
    }
next_line:;
    if(feof(fp))
	return -1;
    linep = fgets(line, 20480, fp);
    if(!linep)
	return -1;

    //*uri = malloc(strlen(linep));
    line_no++;
    if(!(line_no % 10000)) {
	printf("");
	if(!(line_no % 1000000))
	    printf("#");
	printf("%c", running_char);
	if(running_char == '|')
	    running_char = '/';
	else if(running_char == '/')
	    running_char = '-';
	else if(running_char == '-')
	    running_char = '\\';
	else if(running_char == '\\')
	    running_char = '|';
	fflush(stdout);
    }

    if(*linep == '#') {
	if(!(memcmp(linep, "#Fields", 7))) {
	    //printf("Format line present\n");
	}
        goto next_line;
    }

    tmp_string = get_next_token(linep);
    parse_offset = 0;
    while(tmp_string) {
	if(parse_offset == provider_offset) {
//	    printf("provider = %s\n", tmp_string);
	    if(strstr(tmp_string, "Tunnel")) {
		*cacheable = 0;
	    }
	    if(tmp_string[0] != 'T' && tmp_string[0] != 'B' &&
		    tmp_string[0] != 'O' && tmp_string[0] != 'N' &&
		    tmp_string[0] != 'S' && tmp_string[0] != 'i')
		return 0;
	} else if(parse_offset == domain_offset) {
//	    printf("uri = %s\n", tmp_string);
	    sprintf(uri, "%s:", tmp_string);
	} else if(parse_offset == time_offset) {
	    if(tmp_string[0] < '0' || tmp_string[0] > '9')
		return 0;
	    strptime(tmp_string, "%d/%b/%Y:%H:%M:%S %z", &time_val);
	    *acs_time = mktime(&time_val);
//	    printf("time = %s\n", tmp_string);
	} else if(parse_offset == uri_offset) {
	    if(tmp_string) {
	//	sscanf(tmp_string, "%s %s %s", method, req_file, http_id);
			sscanf(tmp_string, "%s",req_file);
//	    printf("method = %s\n", method);
//	    printf("req_file = %s\n", req_file);
//	    printf("http_id = %s\n", http_id);
		strcat(uri, req_file);
//	    printf("uri %s\n", *uri);
	    }
	} else if(parse_offset == resp_offset) {
	    *resp = atoi(tmp_string);
//	    printf("resp = %s\n", tmp_string);
	} else if(parse_offset == range_offset) {
	    if(!memcmp(tmp_string, "bytes=", 6)) {
		sscanf(tmp_string, "bytes=%ld-%ld", start_offset, end_offset);
		*partial = 1;
	    }
//	    printf("range = %s\n", tmp_string);
	} else if(parse_offset == resp_size_offset) {
	    *content_len = atoi(tmp_string);
//	    printf("content_length = %s\n", tmp_string);
	} else if(parse_offset == cache_control_offset) {
	    if(strstr(tmp_string, "no_cache") || strstr(tmp_string, "private")) {
		*cacheable = 0;
	    }
//	    printf("cache_control = %s\n", tmp_string);
	}
	tmp_string = get_next_token(NULL);
	if(!tmp_string)
	    return 0;
	parse_offset ++;
    }
    return 0;

}