Commit a9495490 authored by Niels Möller's avatar Niels Möller
Browse files

* tools/sexp-conv.c (struct sexp_input): Deleted string attribute.

Changed all related functions to take a struct nettle_buffer *
argument instead.
(struct sexp_compound_token): New struct.
(sexp_compound_token_init, sexp_compound_token_clear): New
functions.
(struct sexp_parser): Added a struct sexp_compound_token
attribute, as a temporary measure.

Rev: src/nettle/tools/sexp-conv.c:1.11
parent b4476c72
...@@ -136,9 +136,10 @@ struct sexp_input ...@@ -136,9 +136,10 @@ struct sexp_input
/* Type of current token */ /* Type of current token */
enum sexp_token token; enum sexp_token token;
#if 0
/* Current token */ /* Current token */
struct nettle_buffer string; struct nettle_buffer string;
#endif
}; };
static void static void
...@@ -147,7 +148,9 @@ sexp_input_init(struct sexp_input *input, FILE *f) ...@@ -147,7 +148,9 @@ sexp_input_init(struct sexp_input *input, FILE *f)
input->f = f; input->f = f;
input->coding = NULL; input->coding = NULL;
#if 0
nettle_buffer_init(&input->string); nettle_buffer_init(&input->string);
#endif
} }
...@@ -215,11 +218,12 @@ sexp_next_char(struct sexp_input *input) ...@@ -215,11 +218,12 @@ sexp_next_char(struct sexp_input *input)
} }
static void static void
sexp_push_char(struct sexp_input *input) sexp_push_char(struct sexp_input *input,
struct nettle_buffer *string)
{ {
assert(input->ctype == SEXP_NORMAL_CHAR); assert(input->ctype == SEXP_NORMAL_CHAR);
if (!NETTLE_BUFFER_PUTC(&input->string, input->c)) if (!NETTLE_BUFFER_PUTC(string, input->c))
die("Virtual memory exhasuted.\n"); die("Virtual memory exhasuted.\n");
} }
...@@ -314,7 +318,8 @@ token_chars[0x80] = ...@@ -314,7 +318,8 @@ token_chars[0x80] =
#define TOKEN_CHAR(c) ((c) < 0x80 && token_chars[(c)]) #define TOKEN_CHAR(c) ((c) < 0x80 && token_chars[(c)])
static void static void
sexp_get_token_string(struct sexp_input *input) sexp_get_token_string(struct sexp_input *input,
struct nettle_buffer *string)
{ {
assert(!input->coding); assert(!input->coding);
assert(input->ctype == SEXP_NORMAL_CHAR); assert(input->ctype == SEXP_NORMAL_CHAR);
...@@ -324,25 +329,26 @@ sexp_get_token_string(struct sexp_input *input) ...@@ -324,25 +329,26 @@ sexp_get_token_string(struct sexp_input *input)
do do
{ {
sexp_push_char(input); sexp_push_char(input, string);
sexp_get_char(input); sexp_get_char(input);
} }
while (input->ctype == SEXP_NORMAL_CHAR && TOKEN_CHAR(input->c)); while (input->ctype == SEXP_NORMAL_CHAR && TOKEN_CHAR(input->c));
assert (input->string.size); assert (string->size);
} }
static void static void
sexp_get_string(struct sexp_input *input) sexp_get_string(struct sexp_input *input,
struct nettle_buffer *string)
{ {
input->string.size = 0; nettle_buffer_reset(string);
input->token = SEXP_STRING; input->token = SEXP_STRING;
switch (input->c) switch (input->c)
{ {
case '\"': case '\"':
while (sexp_get_quoted_char(input)) while (sexp_get_quoted_char(input))
sexp_push_char(input); sexp_push_char(input, string);
sexp_get_char(input); sexp_get_char(input);
break; break;
...@@ -361,7 +367,7 @@ sexp_get_string(struct sexp_input *input) ...@@ -361,7 +367,7 @@ sexp_get_string(struct sexp_input *input)
switch (input->ctype) switch (input->ctype)
{ {
case SEXP_NORMAL_CHAR: case SEXP_NORMAL_CHAR:
sexp_push_char(input); sexp_push_char(input, string);
break; break;
case SEXP_EOF_CHAR: case SEXP_EOF_CHAR:
die("Unexpected end of file in coded string.\n"); die("Unexpected end of file in coded string.\n");
...@@ -375,17 +381,18 @@ sexp_get_string(struct sexp_input *input) ...@@ -375,17 +381,18 @@ sexp_get_string(struct sexp_input *input)
break; break;
default: default:
sexp_get_token_string(input); sexp_get_token_string(input, string);
break; break;
} }
} }
static void static void
sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode) sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode,
struct nettle_buffer *string)
{ {
unsigned length; unsigned length;
input->string.size = 0; nettle_buffer_reset(string);
input->token = SEXP_STRING; input->token = SEXP_STRING;
length = input->c - '0'; length = input->c - '0';
...@@ -417,7 +424,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode) ...@@ -417,7 +424,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode)
for (; length; length--) for (; length; length--)
{ {
sexp_next_char(input); sexp_next_char(input);
sexp_push_char(input); sexp_push_char(input, string);
} }
break; break;
...@@ -428,7 +435,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode) ...@@ -428,7 +435,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode)
for (; length; length--) for (; length; length--)
if (sexp_get_quoted_char(input)) if (sexp_get_quoted_char(input))
sexp_push_char(input); sexp_push_char(input, string);
else else
die("Unexpected end of string.\n"); die("Unexpected end of string.\n");
...@@ -448,7 +455,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode) ...@@ -448,7 +455,7 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode)
for (; length; length--) for (; length; length--)
{ {
sexp_next_char(input); sexp_next_char(input);
sexp_push_char(input); sexp_push_char(input, string);
} }
sexp_get_char(input); sexp_get_char(input);
if (input->ctype != SEXP_END_CHAR) if (input->ctype != SEXP_END_CHAR)
...@@ -472,7 +479,8 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode) ...@@ -472,7 +479,8 @@ sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode)
* When returning, input->c should be the first character of the next * When returning, input->c should be the first character of the next
* token. */ * token. */
static void static void
sexp_get_token(struct sexp_input *input, enum sexp_mode mode) sexp_get_token(struct sexp_input *input, enum sexp_mode mode,
struct nettle_buffer *string)
{ {
for(;;) for(;;)
switch(input->ctype) switch(input->ctype)
...@@ -492,7 +500,7 @@ sexp_get_token(struct sexp_input *input, enum sexp_mode mode) ...@@ -492,7 +500,7 @@ sexp_get_token(struct sexp_input *input, enum sexp_mode mode)
{ {
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
sexp_get_string_length(input, mode); sexp_get_string_length(input, mode, string);
return; return;
case '(': case '(':
...@@ -555,19 +563,44 @@ sexp_get_token(struct sexp_input *input, enum sexp_mode mode) ...@@ -555,19 +563,44 @@ sexp_get_token(struct sexp_input *input, enum sexp_mode mode)
if (mode != SEXP_ADVANCED) if (mode != SEXP_ADVANCED)
die("Encountered advanced string in canonical mode.\n"); die("Encountered advanced string in canonical mode.\n");
sexp_get_string(input); sexp_get_string(input, string);
return; return;
} }
} }
} }
struct sexp_compound_token
{
enum sexp_token type;
struct nettle_buffer display;
struct nettle_buffer string;
};
static void
sexp_compound_token_init(struct sexp_compound_token *token)
{
token->type = 0;
nettle_buffer_init(&token->display);
nettle_buffer_init(&token->string);
}
static void
sexp_compound_token_clear(struct sexp_compound_token *token)
{
nettle_buffer_clear(&token->display);
nettle_buffer_clear(&token->string);
}
/* Parsing */ /* Parsing */
struct sexp_parser struct sexp_parser
{ {
struct sexp_input *input; struct sexp_input *input;
enum sexp_mode mode; enum sexp_mode mode;
enum sexp_token expected; enum sexp_token expected;
struct sexp_compound_token token;
/* Nesting level of lists. Transport encoding counts as one /* Nesting level of lists. Transport encoding counts as one
* level of nesting. */ * level of nesting. */
...@@ -587,6 +620,7 @@ sexp_parse_init(struct sexp_parser *parser, ...@@ -587,6 +620,7 @@ sexp_parse_init(struct sexp_parser *parser,
parser->mode = mode; parser->mode = mode;
parser->expected = 0; parser->expected = 0;
sexp_compound_token_init(&parser->token);
/* Start counting with 1 for the top level, to make comparisons /* Start counting with 1 for the top level, to make comparisons
* between transport and level simpler. * between transport and level simpler.
* *
...@@ -601,7 +635,8 @@ sexp_check_token(struct sexp_parser *parser, ...@@ -601,7 +635,8 @@ sexp_check_token(struct sexp_parser *parser,
enum sexp_token token) enum sexp_token token)
{ {
sexp_get_token(parser->input, sexp_get_token(parser->input,
parser->transport ? SEXP_CANONICAL : parser->mode); parser->transport ? SEXP_CANONICAL : parser->mode,
&parser->token.string);
if (token && parser->input->token != token) if (token && parser->input->token != token)
die("Syntax error.\n"); die("Syntax error.\n");
...@@ -962,6 +997,28 @@ sexp_put_digest(struct sexp_output *output) ...@@ -962,6 +997,28 @@ sexp_put_digest(struct sexp_output *output)
sexp_put_code_end(output); sexp_put_code_end(output);
} }
#if 0
enum sexp_expr_result
{
sexp_expr_ok,
sexp_expr_eol,
sexp_expr_eof,
};
static enum sexp_token
sexp_put_expression(struct sexp_output *output, enum sexp_mode mode_out,
unsigned indent,
struct sexp_parser *parser)
{
sexp_parse(parser);
if (parser->input->token == SEXP_EOF)
return SEXP_EOF;
if (parser->input->)
;
}
#endif
/* Conversion functions. */ /* Conversion functions. */
...@@ -1001,16 +1058,16 @@ sexp_convert_item(struct sexp_input *input, struct sexp_parser *parser, ...@@ -1001,16 +1058,16 @@ sexp_convert_item(struct sexp_input *input, struct sexp_parser *parser,
break; break;
case SEXP_STRING: case SEXP_STRING:
sexp_put_string(output, mode_out, &input->string); sexp_put_string(output, mode_out, &parser->token.string);
break; break;
case SEXP_DISPLAY: case SEXP_DISPLAY:
sexp_put_char(output, '['); sexp_put_char(output, '[');
sexp_put_string(output, mode_out, &input->string); sexp_put_string(output, mode_out, &parser->token.string);
sexp_put_char(output, ']'); sexp_put_char(output, ']');
sexp_parse(parser); sexp_parse(parser);
assert(input->token == SEXP_STRING); assert(input->token == SEXP_STRING);
sexp_put_string(output, mode_out, &input->string); sexp_put_string(output, mode_out, &parser->token.string);
break; break;
default: default:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment