int managesieve_parser_read_args
(struct managesieve_parser *parser, unsigned int count,
	enum managesieve_parser_flags flags, const struct managesieve_arg **args_r)
{
	parser->flags = flags;

	while ( !parser->eol && (count == 0 || IS_UNFINISHED(parser)
		|| array_count(&parser->root_list) < count) ) {
		if ( !managesieve_parser_read_arg(parser) )
			break;

		if ( parser->line_size > parser->max_line_size ) {
			parser->error = "MANAGESIEVE command line too large";
			break;
		}
	}

	if ( parser->error != NULL ) {
		/* error, abort */
		parser->line_size += parser->cur_pos;
		i_stream_skip(parser->input, parser->cur_pos);
		parser->cur_pos = 0;
		*args_r = NULL;
		return -1;
	} else if ( (!IS_UNFINISHED(parser) && count > 0
		&& array_count(&parser->root_list) >= count) || parser->eol ) {
		/* all arguments read / end of line. */
		return finish_line(parser, count, args_r);
	} else {
		/* need more data */
		*args_r = NULL;
		return -2;
	}
}
int managesieve_parser_finish_line
(struct managesieve_parser *parser, unsigned int count,
	enum managesieve_parser_flags flags, const struct managesieve_arg **args_r)
{
	const unsigned char *data;
	size_t data_size;
	int ret;

	ret = managesieve_parser_read_args(parser, count, flags, args_r);
	if (ret == -2) {
		/* we should have noticed end of everything except atom */
		if (parser->cur_type == ARG_PARSE_ATOM) {
			data = i_stream_get_data(parser->input, &data_size);
			managesieve_parser_save_arg(parser, data, data_size);
		}
	}
	return finish_line(parser, count, args_r);
}
Пример #3
0
int imap_parser_read_args(struct imap_parser *parser, unsigned int count,
			  enum imap_parser_flags flags,
			  const struct imap_arg **args_r)
{
	parser->flags = flags;

	if (parser->args_added_extra_eol) {
		/* delete EOL */
		array_delete(&parser->root_list,
			     array_count(&parser->root_list)-1, 1);
		parser->args_added_extra_eol = FALSE;
		parser->literal_size_return = FALSE;
	}

	while (!parser->eol && (count == 0 || IS_UNFINISHED(parser) ||
				array_count(&parser->root_list) < count)) {
		if (!imap_parser_read_arg(parser))
			break;

		if (parser->line_size > parser->max_line_size) {
			parser->error = IMAP_PARSE_ERROR_LINE_TOO_LONG;
			parser->error_msg = "IMAP command line too large";
			break;
		}
	}

	if (parser->error != IMAP_PARSE_ERROR_NONE) {
		/* error, abort */
		parser->line_size += parser->cur_pos;
		i_stream_skip(parser->input, parser->cur_pos);
		parser->cur_pos = 0;
		*args_r = NULL;
		return -1;
	} else if ((!IS_UNFINISHED(parser) && count > 0 &&
		    array_count(&parser->root_list) >= count) ||
		   parser->eol || parser->literal_size_return) {
		/* all arguments read / end of line. */
                return finish_line(parser, count, args_r);
	} else {
		/* need more data */
		*args_r = NULL;
		return -2;
	}
}
Пример #4
0
static int addbyte(grpc_http_parser *parser, uint8_t byte) {
  switch (parser->state) {
    case GRPC_HTTP_FIRST_LINE:
    case GRPC_HTTP_HEADERS:
      if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) {
        if (grpc_http1_trace)
          gpr_log(GPR_ERROR, "HTTP client max line length (%d) exceeded",
                  GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
        return 0;
      }
      parser->cur_line[parser->cur_line_length] = byte;
      parser->cur_line_length++;
      if (check_line(parser)) {
        return finish_line(parser);
      } else {
        return 1;
      }
      GPR_UNREACHABLE_CODE(return 0);
    case GRPC_HTTP_BODY:
      return addbyte_body(parser, byte);
  }
  GPR_UNREACHABLE_CODE(return 0);
}
Пример #5
0
Result parse(
    char **** program_args,
    unsigned * num_program_args,
    unsigned * program_args_size,
    char ** file_in,
    char ** file_out) {

    /* Token variables. */
    int current_token;

    /* Parsing tracking. */
    int expecting_prog_name = 0;
    int file_in_allowed = 1;
    int file_out_allowed = 1;
    int pipe_allowed = 1;

    /* An array of arguments for the current program.
     * This array is added to the array of arrays when a new program is encountered.
    */
    declare_unsigned_ptr(num_current_prog_args);
    declare_unsigned_ptr(current_prog_args_size);
    char ** current_prog_args_r;
    char *** current_prog_args = &current_prog_args_r;

    /* Parse the first token (should be a program name). */
    current_token = yylex();

    /* Allow empty commands. */
    if (current_token == Newline) {
        return Empty;
    }
    /* End of input should always be caught here (CTRL-D or end of script). */
    else if (current_token == EndOfFile) {
        printf("\n");
        return Exit;
    }
    /* Handle the exit command here (should have 0 arguments). */
    else if (strncmp(yytext, "exit", 4) == 0 && yylex() == Newline) {
        return Exit;
    }
    /* Handle cd as well (one argument). */
    else if (strncmp(yytext, "cd", 2) == 0) {
        Result return_val = Empty;
        int first_token;
        int second_token;
        char * directory;

        first_token = yylex();

        if (first_token == Word) {
            directory = strdup(yytext);

            second_token = yylex();

            if (second_token == Newline) {
                int chdir_status = chdir(directory);
                if (chdir_status == -1) {
                    fprintf(stderr, "exec error: unable to cd.\n");
                    return_val = Error;
                }
            } else {
                fprintf(stderr, "parse error: cd only takes one argument.\n");
                finish_line();
                return_val = Error;
            }

            free(directory);
            return return_val;
        }

        if (first_token == Newline) {
            fprintf(stderr, "parse error: cd only takes one argument.\n");
            return_val = Error;
        } else {
            finish_line();
        }

        return return_val;
    }

    *file_in = NULL;
    *file_out = NULL;

    /* Allocate the array of arguments for the current program. */
    *current_prog_args = (char **) new_array(current_prog_args_size);

    /* Add the first program (parsed above) */
    try(current_token == Word, "no initial program to run.");
    try_push(current_prog_args, strdup(yytext));

    while (1) {
        current_token = yylex();

        switch (current_token) {
            case Word: {
                /* Handle the beginning of a new program. */
                if (expecting_prog_name) {
                    /* Add previous program args array to program args array of arrays. */
                    add_to_program_args(current_prog_args);

                    /* Allocate a new array of arguments for this program. */
                    *current_prog_args = (char **) new_array(current_prog_args_size);
                    *num_current_prog_args = 0;

                    expecting_prog_name = 0;
                    pipe_allowed = 1;
                    file_out_allowed = 1;
                }

                /* Push the argument onto the currrent list of arguments. */
                try_push(current_prog_args, strdup(yytext));
                break;
            }

            case FileOut: {
                if (file_out_allowed) {
                    try(yylex() == Word, "no valid filename provided to >");
                    *file_out = strdup(yytext);

                    file_out_allowed = 0;
                    pipe_allowed = 0;

                    break;
                }
                error("inappropriate redirection to a file.");
            }

            case FileIn: {
                if (file_in_allowed) {
                    try(yylex() == Word, "no valid filename provided to <");
                    *file_in = strdup(yytext);

                    file_in_allowed = 0;

                    break;
                }
                error("inappropriate redirection from a file.");
            }

            case Pipe: {
                if (pipe_allowed) {
                    expecting_prog_name = 1;
                    file_in_allowed = 0;
                    break;
                }
                error("inappropriate pipe.");
            }

            /* If a new line is reached, the line has been parsed successfully. */
            case Newline: {
                /* Check for pipes with no destination. */
                if (expecting_prog_name) {
                    print_error_and_free("no program following pipe.");
                    return Error;
                }

                /* Add the last set of program args to the array. */
                add_to_program_args(current_prog_args);

                return Ok;
            }

            case EndOfFile: {
                error("unreachable code executed!");
            }

            default: {
                printf("unhandled token: %s\n", yytext);
            }
        }
    }
}
Пример #6
0
/* Read the current section until 'endsection' is reached. */
void ptrdict_lf_read_section(section_t *self, parser_t *parser)
{
  BOOL section_done = FALSE;
  section_t *s;
  property_t *p;
  char keyword[MAX_KEYWORD+1];
  char value[MAX_VALUE+1];
  char token[MAX_TOKEN+1];
  BOOL is_token;

  self->provided = TRUE;
  if (self->provided_notification)
    *self->provided_notification = TRUE;

  while (!section_done && !at_end_of_file(parser)) {
    read_next_keyword(parser, keyword, MAX_KEYWORD, &is_token);
    
    if (is_token) {
      printf("[ptrdict_lf_read_section] Keyword expected in line %i.\n", parser->row);
      exit(1);
    }

    if (!strcmp(keyword, "endsection")) {
      read_next_value(parser, value, MAX_VALUE);

      if (strcmp(value, self->name)) {
	printf("[ptrdict_lf_read_section] Current open section is '%s', cannot close section '%s' in line %i.\n",
	       self->name, value, parser->row);
	exit(1);
      }

      if (self->kind != SK_SECTION) {
	printf("[ptrdict_lf_read_section] Current open object '%s' is not a section (line %i).\n",
	       self->name, parser->row);
	exit(1);
      }

      finish_line(parser);
      section_done = TRUE;
    } else if (!strcmp(keyword, "endmodule")) {
      read_next_value(parser, value, MAX_VALUE);

      if (strcmp(value, self->name)) {
	printf("[ptrdict_lf_read_section] Current open module is '%s', cannot close module '%s' in line %i.\n",
	       self->name, value, parser->row);
	exit(1);
      }

      if (self->kind != SK_MODULE) {
	printf("[ptrdict_lf_read_section] Current open object '%s' is not a section (line %i).\n",
	       self->name, parser->row);
	exit(1);
      }

      finish_line(parser);
      section_done = TRUE;
    } else if (!strcmp(keyword, "section")) {
      read_next_value(parser, value, MAX_VALUE);
      finish_line(parser);

      s = ptrdict_find_section(self, value);
      if (!s) {
	printf("[ptrdict_lf_read_section] Unknown section '%s' in line %i.\n", value, parser->row);
	
	ptrdict_enum_subsections(self, stdout);

	exit(1);
      } else if (s->kind == SK_MODULE) {
	printf("[ptrdict_lf_read_section] '%s' is a module identifier (line %i).\n", value, parser->row);
	exit(1);
      }

      ptrdict_lf_read_section(s, parser);
    } else if (!strcmp(keyword, "module")) {
      read_next_value(parser, value, MAX_VALUE);
      finish_line(parser);

      s = ptrdict_find_section(self, value);
      if (!s) {
	printf("[ptrdict_lf_read_section] Unknown section '%s' in line %i.\n", value, parser->row);
	
	ptrdict_enum_subsections(self, stdout);

	exit(1);
      } else if (s->kind == SK_SECTION) {
	printf("[ptrdict_lf_read_section] '%s' is a section identifier (line %i).\n", value, parser->row);
	exit(1);
      }

      ptrdict_lf_read_section(s, parser);
    } else {
      p = ptrdict_find_property(self, keyword);

      if (!p) {
	printf("[ptrdict_lf_read_section] Unknown keyword '%s' in line %i.\n"
	       "Possibilities are 'section', 'module' or one of the properties of section '%s', which are:\n",
	       keyword, parser->row, self->name);

	ptrdict_enum_properties(self, stdout);

	exit(1);
      }

      read_next_token(parser, token, MAX_TOKEN);

      if (strcmp(token, "=")) {
	printf("[ptrdict_lf_read_section] '=' expected for assignment of property '%s' in line %i.\n", keyword, parser->row);
	exit(1);
      }

      read_next_value(parser, value, MAX_VALUE);
      finish_line(parser);

      ptrdict_set_property(p, value);
    }
  }

  if (!section_done) {
    printf("[ptrdict_lf_read_section] Error: End-of-file reached, but keyword 'endsection' is missing (line %i).\n", parser->row);
    exit(1);
  }
}