Commit 3eca0559 authored by Niels Möller's avatar Niels Möller

* testsuite/sexp-conv-test: Updated testcases for improved

handling of comments.

* tools/sexp-conv.c (sexp_convert_item): Use sexp_put_soft_newline
to terminate comments, and modify indentation for the case that a
list starts with a comment.

* tools/output.c (sexp_output_init): Initialize soft_newline.
(sexp_put_raw_char): Clear soft_newline.
(sexp_put_newline): Check and reset soft_newline.
(sexp_put_soft_newline): New function.

* tools/output.h (struct sexp_output): Removed union with single
element, and updated all users. New attribute soft_newline.

Rev: nettle/ChangeLog:1.31
Rev: nettle/testsuite/sexp-conv-test:1.3
Rev: nettle/tools/output.c:1.3
Rev: nettle/tools/output.h:1.3
Rev: nettle/tools/sexp-conv.c:1.4
parent ada5cbfb
2009-02-10 Niels Mller <nisse@lysator.liu.se>
* base16-meta.c (base16_encode_update_wrapper): Mark ctx argument
as UNUSED.
* testsuite/sexp-conv-test: Updated testcases for improved
handling of comments.
* tools/sexp-conv.c (sexp_convert_item): Use sexp_put_soft_newline
to terminate comments, and modify indentation for the case that a
list starts with a comment.
* tools/output.c (sexp_output_init): Initialize soft_newline.
(sexp_put_raw_char): Clear soft_newline.
(sexp_put_newline): Check and reset soft_newline.
(sexp_put_soft_newline): New function.
* tools/output.h (struct sexp_output): Removed union with single
element, and updated all users. New attribute soft_newline.
2008-12-22 Niels Mller <nisse@lysator.liu.se>
* Makefile.in ($(des_headers)): Create files in $(srcdir).
......
......@@ -91,11 +91,9 @@ test_advanced '(foo bar baz)' '(foo bar
baz)'
test_advanced '; comment
()' '; comment
()'
test_advanced '(foo ; gazonk
bar)' '(foo ; gazonk
bar)'
test_advanced '(foo[bar]foo)' '(foo [bar]foo)'
......
......@@ -47,6 +47,7 @@ sexp_output_init(struct sexp_output *output, FILE *f,
output->ctx = NULL;
output->pos = 0;
output->soft_newline = 0;
}
void
......@@ -61,15 +62,21 @@ sexp_output_hash_init(struct sexp_output *output,
static void
sexp_put_raw_char(struct sexp_output *output, uint8_t c)
{
output->pos++;
if (putc(c, output->f) < 0)
die("Write failed: %s\n", strerror(errno));
output->pos++;
output->soft_newline = 0;
}
void
sexp_put_newline(struct sexp_output *output,
unsigned indent)
{
if (output->soft_newline)
output->soft_newline = 0;
else
{
unsigned i;
sexp_put_raw_char(output, '\n');
......@@ -79,6 +86,17 @@ sexp_put_newline(struct sexp_output *output,
sexp_put_raw_char(output, ' ');
output->pos = indent;
}
}
/* Put a newline, but only if it is followed by another newline,
collaps to one newline only. */
void
sexp_put_soft_newline(struct sexp_output *output,
unsigned indent)
{
sexp_put_newline(output, indent);
output->soft_newline = 1;
}
void
......@@ -86,13 +104,13 @@ sexp_put_char(struct sexp_output *output, uint8_t c)
{
if (output->coding)
{
/* Two is enough for both hex and base64. */
/* Two is enough for both base16 and base64. */
uint8_t encoded[2];
unsigned done;
unsigned i;
done = output->coding->encode_update(&output->state, encoded,
done = output->coding->encode_update(&output->base64, encoded,
1, &c);
assert(done <= sizeof(encoded));
......@@ -149,7 +167,7 @@ sexp_put_code_start(struct sexp_output *output,
output->coding_indent = output->pos;
output->coding = coding;
output->coding->encode_init(&output->state);
output->coding->encode_init(&output->base64);
}
void
......@@ -161,7 +179,7 @@ sexp_put_code_end(struct sexp_output *output)
assert(output->coding);
done = output->coding->encode_final(&output->state, encoded);
done = output->coding->encode_final(&output->base64, encoded);
assert(done <= sizeof(encoded));
......
......@@ -45,12 +45,12 @@ struct sexp_output
const struct nettle_hash *hash;
void *ctx;
union {
/* NOTE: There's no context for hex encoding, the state argument to
encode_update is ignored */
struct base64_decode_ctx base64;
/* NOTE: There's no context for hex encoding */
} state;
unsigned pos;
int soft_newline;
};
void
......@@ -65,6 +65,10 @@ void
sexp_put_newline(struct sexp_output *output,
unsigned indent);
void
sexp_put_soft_newline(struct sexp_output *output,
unsigned indent);
void
sexp_put_char(struct sexp_output *output, uint8_t c);
......
......@@ -110,13 +110,26 @@ sexp_convert_item(struct sexp_parser *parser,
{
/* FIXME: Adapt pretty printing to handle a big first
* element. */
if (item == 1)
switch (item)
{
case 0:
if (token->type == SEXP_COMMENT)
{
sexp_put_char(output, ' ');
indent = output->pos;
/* Disable the indentation setup for next item */
item++;
}
else if (item > 1)
break;
case 1:
sexp_put_char(output, ' ');
indent = output->pos;
break;
default:
sexp_put_newline(output, indent);
break;
}
}
sexp_convert_item(parser, token, output, mode_out, indent);
......@@ -141,9 +154,7 @@ sexp_convert_item(struct sexp_parser *parser,
if (mode_out == SEXP_ADVANCED)
{
sexp_put_data(output, token->string.size, token->string.contents);
/* This newline is necessary only if the comment comes first
in a list. It would be nice to supress extra newlines. */
sexp_put_newline(output, indent);
sexp_put_soft_newline(output, indent);
}
break;
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