Example #1
0
static void
next_token (scanner_t *scanner)
{
	int start, end, type;
	char c;
	skip_spaces (scanner);
	start = scanner->idx;
	while (!is_eof (scanner) && !isspace (CUR_CHAR)) {
		if (scanner->idx == start) {
			if (is_special_char (CUR_CHAR)) {
				++scanner->idx;
				break;
			}
		} else if (is_special_char (CUR_CHAR))
			break;
		++scanner->idx;
	}
	end = scanner->idx;

	c = scanner->input [start];
	if (start >= scanner->size)
		type = TOKEN_EOF;
	else if (isdigit (c) || c == '\'')
		type = TOKEN_NUM;
	else if (ispunct_char (c))
		type = TOKEN_PUNC;
	else
		type = TOKEN_ID;
	scanner->current.start = start;
	scanner->current.end = end;
	scanner->current.type = type;
	scanner->current.line = scanner->line;

	DEBUG_SCANNER (dump_token (scanner, &scanner->current));
}
Example #2
0
HttpParams::HttpParams(char* p): params(p), len(0), n(0) {
	// first, decode any unnecessary encodings
	char* dest = p;
	if(params) {
		for(const char* src = params; *src; src++) {
			if(is_valid_url_char(*src) || is_special_char(*src)) {
				*dest++ = *src;
			} else if('%'==*src) {
				char hex = 0;
				for(int i=0; i<2; i++) {
					hex <<= 4;
					if(!to_hex(*++src,hex))
						goto abort;
				}
				if(is_valid_url_char(hex)) {
					*dest++ = hex;
				} else { // revert it
					*dest++ = '%';
					*dest++ = src[-1];
					*dest++ = *src;
				}
			} else
				goto abort;
		}
	}
	if(!params) {
		static char empty = '\0';
abort:		params=&empty;
		dest=p;
	}
	// put all the counters at the end
	k = v = len = (dest-p);
}
Example #3
0
string sinsp_filter::next_operand(bool expecting_first_operand)
{
	int32_t start;

	//
	// Skip spaces
	//
	if(isblank(m_fltstr[m_scanpos]))
	{
		next();
	}

	//
	// Mark the beginning of the word
	//
	start = m_scanpos;

	for(; m_scanpos < m_scansize; m_scanpos++)
	{
		char curchar = m_fltstr[m_scanpos];
		
		bool is_end_of_word;

		if(expecting_first_operand)
		{
			is_end_of_word = (isblank(curchar) || is_special_char(curchar));
		}
		else
		{
			is_end_of_word = (isblank(curchar) || is_bracket(curchar));
		}

		if(is_end_of_word)
		{
			//
			// End of word
			//
			ASSERT(m_scanpos > start);
			string res = m_fltstr.substr(start, m_scanpos - start);

			if(curchar == '(' || curchar == ')')
			{
				m_scanpos--;
			}

			return res;
		}
	}

	//
	// End of filter
	//
	return m_fltstr.substr(start, m_scansize - 1);
}
// Read the next line and return it as a list of tokens skipping white space and special characters
// The return value indicates success/failure.
bool read_line_as_tokens (istream& is, vector<string>& tokens,
			  bool includeSpecialChars = false) {

  tokens.clear() ;
  
  string line ;
  std::getline (is, line) ;

  while (is && tokens.empty()) {
    
    string token = "" ;

    for (int i=0; i < line.size(); ++i) {
      char currChar = line[i] ;
      bool isSpecialChar = is_special_char(currChar) ;

      if (std::isspace (currChar) || isSpecialChar) {

        if (!token.empty()) {
          // Add the current token to the list of tokens
          tokens.push_back(token) ;
          token.clear() ;
        }

	if (includeSpecialChars && isSpecialChar) {
	  tokens.push_back(string(1, currChar)) ;
	}

      } else {
        // Add the char to the current token
        token.push_back(currChar) ;
      }

    }

    if (!token.empty())
      tokens.push_back(token) ;
    

    if (tokens.empty())
      // Previous line read was empty. Read the next one.
      std::getline (is, line) ;    
  }

  //for (int i=0; i < tokens.size(); ++i)
  //  cout << tokens[i] << " " ;
  //cout << endl ;
  
  return !tokens.empty() ;
}
Example #5
0
vector<char> sinsp_filter::next_operand(bool expecting_first_operand)
{
	vector<char> res;
	bool is_quoted = false;
	int32_t start;
	int32_t nums[2];
	uint32_t num_pos;
	enum ppm_escape_state
	{
		PES_NORMAL,
		PES_SLASH,
		PES_NUMBER,
		PES_ERROR,
	} escape_state;

	//
	// Skip spaces
	//
	if(isblank(m_fltstr[m_scanpos]))
	{
		next();
	}

	//
	// If there are quotes, don't stop on blank
	//
	if(m_scanpos < m_scansize && 
		(m_fltstr[m_scanpos] == '"' || m_fltstr[m_scanpos] == '\''))
	{
		is_quoted = true;
		m_scanpos++;
	}

	//
	// Mark the beginning of the word
	//
	start = m_scanpos;
	escape_state = PES_NORMAL;
	num_pos = 0;

	while(m_scanpos < m_scansize && escape_state != PES_ERROR)
	{
		char curchar = m_fltstr[m_scanpos];
		bool is_end_of_word;

		if(expecting_first_operand)
		{
			is_end_of_word = (isblank(curchar) || is_special_char(curchar));
		}
		else
		{
			is_end_of_word = (!is_quoted && (isblank(curchar) || is_bracket(curchar))) ||
				(is_quoted && escape_state != PES_SLASH && (curchar == '"' || curchar == '\''));
		}

		if(is_end_of_word)
		{
			if(escape_state != PES_NORMAL)
			{
				escape_state = PES_ERROR;
				break;
			}

			//
			// End of word
			//
			ASSERT(m_scanpos > start);

			if(curchar == '(' || curchar == ')')
			{
				m_scanpos--;
			}

			res.push_back('\0');
			return res;
		}

		switch(escape_state)
		{
		case PES_NORMAL:
			if(curchar == '\\' && !expecting_first_operand)
			{
				escape_state = PES_SLASH;
			}
			else
			{
				res.push_back(curchar);
			}
			break;
		case PES_SLASH:
			switch(curchar)
			{
			case '\\':
			case '"':
				escape_state = PES_NORMAL;
				res.push_back(curchar);
				break;
			case 'x':
				escape_state = PES_NUMBER;
				break;
			default:
				escape_state = PES_ERROR;
				break;
			}
			break;
		case PES_NUMBER:
			if(isdigit((int)curchar))
			{
				nums[num_pos++] = curchar - '0';
			}
			else if((curchar >= 'a' && curchar <= 'f') || (curchar >= 'A' && curchar <= 'F'))
			{
				nums[num_pos++] = tolower((int)curchar) - 'a' + 10;
			}
			else
			{
				escape_state = PES_ERROR;
			}

			if(num_pos == 2 && escape_state != PES_ERROR)
			{
				res.push_back((char)(nums[0] * 16 + nums[1]));

				num_pos = 0;
				escape_state = PES_NORMAL;
			}
			break;
		default:
			ASSERT(false);
			escape_state = PES_ERROR;
			break;
		}

		m_scanpos++;
	}

	if(escape_state == PES_ERROR)
	{
		throw sinsp_exception("filter error: unrecognized escape sequence at " + m_fltstr.substr(start, m_scanpos));
	}
	else if(is_quoted)
	{
		throw sinsp_exception("filter error: unclosed quotes");
	}

	//
	// End of filter
	//
	res.push_back('\0');
	return res;
}
Example #6
0
bool ws::http_request_parser::is_valid_header_name_char(char c)
{
	return (!is_special_char(c) && is_ascii_non_ctl(c));		
}
Example #7
0
  command_t
read_command_stream (command_stream_t s)
{
  /* 
     take a command stream, return a command_t object to be used
     by main.c
     Read a command from STREAM; return it, or NULL on EOF.  If there is`
     an error, report the error and exit instead of returning.  
Given: array of pointers to c strings (command_stream)
Output: singular constructed command objects such that
when read_command_stream is called one command
object is returned, and the next time it is 
called the second command object is returned, etc. command
object is returned, and the next time 
*/

  int index = s->cmd_count;
  int list_size = s->size;

  struct command* cmd_ptr = NULL;
  struct command* special_ptr = NULL;
	struct command* outer_special_ptr = NULL;

  char ** word_list = NULL;
  int word_list_size = 0;
  char dir = 'l';
	char outside_dir = 'l';

	bool found_start_subshell = false;

  if (index > list_size-1)
    return NULL;

  // Skip repeating \n's
  if (strcmp(s->command_list[index], "\n") == 0 && cmd_ptr == NULL) {
    while ( strcmp(s->command_list[index], "\n") == 0 )
    {
      index++;
      s->line_count++;
      if (index > list_size-1)
        return NULL;
    }
  }
	//short loop_iteration = 0;
  while ( strcmp(s->command_list[index], "\n") != 0)
  {
//		printf("Iteration: %d\n", loop_iteration++);
    // Syntax Check
    if (!is_valid_char(s->command_list[index]))
    {
      syn_error(s);
    }

    // For ooerators
    if(is_special_char(s->command_list[index]))
    {
			if(strcmp(s->command_list[index], "(") == 0)
			{
				found_start_subshell = true;
				if(word_list_size !=0 && word_list != NULL && special_ptr==NULL)
					syn_error(s);

				outer_special_ptr = special_ptr;
				special_ptr = NULL;
				outside_dir = dir;
				dir = 'l';


			}
			else if(strcmp(s->command_list[index], ")")==0)
			{
				if(found_start_subshell)
				{
					found_start_subshell = false;
				}
				else
				{
					syn_error(s);
				}

				if(!build_word_command(word_list, &cmd_ptr))
				{
					syn_error(s);
				}
				word_list = NULL;
				word_list_size = 0;

				//if there is a special command

				if(special_ptr != NULL)
				{
					if(!add_cmd_to_special(&cmd_ptr, &special_ptr, 'r'))
						syn_error(s);
					cmd_ptr = special_ptr;
					
				}

				if(!build_sub_command(&cmd_ptr, &special_ptr))
					syn_error(s);
				cmd_ptr = special_ptr;
				special_ptr = outer_special_ptr;
				dir = outside_dir;

		//		printf("I came here!\n");
			}
			
      //check first for redirection special chars
      else if(strcmp(s->command_list[index], "<") == 0)
      {
        if (word_list != NULL)
        {
          if (!build_word_command(word_list, &cmd_ptr))
            syn_error(s);
          word_list = NULL;
          word_list_size = 0;
        }
        //if the next command = special char output an error
        if(index+1 < list_size)
				{
					if(is_special_char(s->command_list[index+1]) ||
            strcmp(s->command_list[index+1], "\n") == 0)
					{
						syn_error(s);
					}
				
					//else output an error
					if(!add_word_to_IO(s->command_list[index+1], &cmd_ptr, 'i'))
					{
						syn_error(s);
					}
				}

        //increment index an additional time because
        //command for index+1 is already constructed
        index++;
      }
      else if(strcmp(s->command_list[index], ">") ==0)
      {
        if (word_list != NULL)
        {
          if (!build_word_command(word_list, &cmd_ptr))
            syn_error(s);
          word_list = NULL;
          word_list_size = 0;
        }
				if(index+1 < list_size)//check condition later
				{
					if(is_special_char(s->command_list[index+1]) ||
						  strcmp(s->command_list[index+1], "\n") == 0)
						syn_error(s);

					if(!add_word_to_IO(s->command_list[index+1], &cmd_ptr, 'o'))
						syn_error(s);
				}
        index++;
      }
      //else condition: not "<" or ">" so implement l and r ptrs
      // Add to left only in first iteration
      else if(dir == 'l')
      {
        if (word_list != NULL)
        {
          if (!build_word_command(word_list, &cmd_ptr))
            syn_error(s);
          word_list = NULL;
          word_list_size = 0;
        }

        if (!build_special_command(s->command_list[index], &special_ptr))
          syn_error(s);
        if (!add_cmd_to_special(&cmd_ptr,&special_ptr, 'l'))
          syn_error(s);
        cmd_ptr = NULL;
        dir = 'r';
      }
      // Add to further special commands
      else
      {
        if (word_list != NULL)
        {
        if (!build_word_command(word_list, &cmd_ptr))
          syn_error(s);
        word_list = NULL;
        word_list_size = 0;
        }
        if (!add_cmd_to_special(&cmd_ptr,&special_ptr,'r'))
          syn_error(s);
        cmd_ptr = special_ptr;
        special_ptr = NULL;
        if (!build_special_command(s->command_list[index], &special_ptr))
          syn_error(s);
        if (!add_cmd_to_special(&cmd_ptr,&special_ptr,'l'))
          syn_error(s);
      }


	//		printf("i made it this far\n");
			//solves "dangling \n" problem
				if ( index+2 < list_size)
				{
					if(strcmp(s->command_list[index+1], "\n") == 0 && special_ptr != NULL)
					{
						index++;
					}
				}
    }
    // For Single Words
    else
    {
      // Append to word_list before making commands
      if(!add_cmd_to_list(s->command_list[index], &word_list, word_list_size))
        syn_error(s);
  //    printf("Added command %s to position %d\n", s->command_list[index], word_list_size);
			word_list_size++;
    }
    index++;
  }
  s->cmd_count = index + 1;
  
  // Fix case for single sided sequence commands
  if (special_ptr != NULL)
  {
    if (special_ptr->type == SEQUENCE_COMMAND && special_ptr->u.command[1] == NULL)
    {
      add_cmd_to_list(" ", &word_list, word_list_size);
    } 
  }

  //  Build remaining list
  if (cmd_ptr == NULL)
  {
    if (!build_word_command(word_list, &cmd_ptr))
      syn_error(s);
   // int i;
	//	for(i = 0; i< word_list_size; i++)
	//	{
//			printf("adding to cmd word %s\n", word_list[i]);

	//	}
		
		word_list_size = 0;
    word_list = NULL;
 }

	if(found_start_subshell)
		syn_error(s);

  // If reaming word_list commands and incomplete tree
  if (special_ptr != NULL)
  {
    if(!add_cmd_to_special(&cmd_ptr, &special_ptr, 'r'))
      syn_error(s);
    cmd_ptr = special_ptr;
  }

	//printf("I made it this far!");
  // Increment line count
  s->line_count++;
  return cmd_ptr;
}
Example #8
0
 void wr_val(char x)
 {
     if(is_special_char(x))
         wr_translated(x);
     else out << x;
 }