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

Added indent argument to format methods (not used yet).

Rev: src/lsh_keygen.c:1.6
Rev: src/sexp.c:1.10
Rev: src/sexp.h:1.11
Rev: src/sexp_test.c:1.2
parent ae0d08c8
...@@ -180,7 +180,7 @@ int main(int argc, char **argv) ...@@ -180,7 +180,7 @@ int main(int argc, char **argv)
sexp_l(2, sexp_z("g"), sexp_n(public.g), -1), sexp_l(2, sexp_z("g"), sexp_n(public.g), -1),
sexp_l(2, sexp_z("y"), sexp_n(public.y), -1), sexp_l(2, sexp_z("y"), sexp_n(public.y), -1),
sexp_l(2, sexp_z("x"), sexp_n(x), -1), -1), -1), sexp_l(2, sexp_z("x"), sexp_n(x), -1), -1), -1),
style); style, 0);
return LSH_FAILUREP(A_WRITE(output, key)) return LSH_FAILUREP(A_WRITE(output, key))
? 1 : 0; ? 1 : 0;
......
...@@ -54,7 +54,8 @@ ...@@ -54,7 +54,8 @@
/* For advanced format */ /* For advanced format */
static struct lsh_string *do_format_simple_string(struct lsh_string *s, static struct lsh_string *do_format_simple_string(struct lsh_string *s,
int style) int style,
unsigned indent)
{ {
int quote_friendly = ( (~CHAR_control & ~CHAR_international) int quote_friendly = ( (~CHAR_control & ~CHAR_international)
| CHAR_escapable); | CHAR_escapable);
...@@ -140,14 +141,15 @@ static struct lsh_string *do_format_simple_string(struct lsh_string *s, ...@@ -140,14 +141,15 @@ static struct lsh_string *do_format_simple_string(struct lsh_string *s,
return res; return res;
} }
/* Base 64 string */ /* Base 64 string */
return encode_base64(s, "||", 0); return encode_base64(s, "||", indent + 1, 0);
} }
default: default:
fatal("do_format_sexp_string: Unknown output style.\n"); fatal("do_format_sexp_string: Unknown output style.\n");
} }
} }
static struct lsh_string *do_format_sexp_string(struct sexp *s, int style) static struct lsh_string *do_format_sexp_string(struct sexp *s,
int style, unsigned indent)
{ {
CAST(sexp_string, self, s); CAST(sexp_string, self, s);
...@@ -160,11 +162,11 @@ static struct lsh_string *do_format_sexp_string(struct sexp *s, int style) ...@@ -160,11 +162,11 @@ static struct lsh_string *do_format_sexp_string(struct sexp *s, int style)
case SEXP_CANONICAL: case SEXP_CANONICAL:
if (self->display) if (self->display)
return ssh_format("[%lfS]%lfS", return ssh_format("[%lfS]%lfS",
do_format_simple_string(self->display, style), do_format_simple_string(self->display, style, indent + 1),
do_format_simple_string(self->contents, style)); do_format_simple_string(self->contents, style, indent));
else else
return ssh_format("%lfS", return ssh_format("%lfS",
do_format_simple_string(self->contents, style)); do_format_simple_string(self->contents, style, indent));
default: default:
fatal("do_format_sexp_string: Unknown output style.\n"); fatal("do_format_sexp_string: Unknown output style.\n");
} }
...@@ -186,7 +188,8 @@ struct sexp *make_sexp_string(struct lsh_string *d, struct lsh_string *c) ...@@ -186,7 +188,8 @@ struct sexp *make_sexp_string(struct lsh_string *d, struct lsh_string *c)
static struct lsh_string * static struct lsh_string *
do_format_sexp_nil(struct sexp *ignored UNUSED, int style UNUSED) do_format_sexp_nil(struct sexp *ignored UNUSED, int style UNUSED,
unsigned indent UNUSED)
{ {
return ssh_format("()"); return ssh_format("()");
} }
...@@ -241,7 +244,8 @@ static struct sexp_iterator *make_iter_cons(struct sexp *s) ...@@ -241,7 +244,8 @@ static struct sexp_iterator *make_iter_cons(struct sexp *s)
return &iter->super; return &iter->super;
} }
static struct lsh_string *do_format_sexp_tail(struct sexp_cons *c, int style) static struct lsh_string *do_format_sexp_tail(struct sexp_cons *c,
int style, unsigned indent)
{ {
int use_space = 0; int use_space = 0;
...@@ -258,14 +262,23 @@ static struct lsh_string *do_format_sexp_tail(struct sexp_cons *c, int style) ...@@ -258,14 +262,23 @@ static struct lsh_string *do_format_sexp_tail(struct sexp_cons *c, int style)
/* Fall through */ /* Fall through */
case SEXP_CANONICAL: case SEXP_CANONICAL:
return ssh_format(use_space ? " %ls%ls" : "%ls%ls", return ssh_format(use_space ? " %ls%ls" : "%ls%ls",
sexp_format(c->car, style), sexp_format(c->car, style, indent),
do_format_sexp_tail(c->cdr, style)); do_format_sexp_tail(c->cdr, style, indent));
default: default:
fatal("do_format_sexp_tail: Unknown output style.\n"); fatal("do_format_sexp_tail: Unknown output style.\n");
} }
} }
static struct lsh_string *do_format_sexp_cons(struct sexp *s, int style) #if 0
static int is_short(struct lsh_string *s)
{
return ( (s->length < 15)
&& !memchr(s->data, '\n', s->length); )
}
#endif
static struct lsh_string *do_format_sexp_cons(struct sexp *s,
int style, unsigned indent)
{ {
CAST(sexp_cons, self, s); CAST(sexp_cons, self, s);
...@@ -275,8 +288,22 @@ static struct lsh_string *do_format_sexp_cons(struct sexp *s, int style) ...@@ -275,8 +288,22 @@ static struct lsh_string *do_format_sexp_cons(struct sexp *s, int style)
fatal("Internal error!\n"); fatal("Internal error!\n");
case SEXP_ADVANCED: case SEXP_ADVANCED:
case SEXP_INTERNATIONAL: case SEXP_INTERNATIONAL:
#if 0
{
struct lsh_string *head = format_sexp(self->car, style, indent + 1);
if (is_short(head))
return ssh_format("(%lfS%lfS",
head, do_format_sexp_tail(self->cdr, style,
indent + 2 + head->length));
else
return ssh_format("(%lfS\n", do_format_sexp_tail(self->cdr, style,
indent + 1));
}
#endif
/* Fall through */
case SEXP_CANONICAL: case SEXP_CANONICAL:
return ssh_format("(%ls", do_format_sexp_tail(self, style)); return ssh_format("(%ls", do_format_sexp_tail(self, style, indent));
default: default:
fatal("do_format_sexp_tail: Unknown output style.\n"); fatal("do_format_sexp_tail: Unknown output style.\n");
} }
...@@ -355,7 +382,8 @@ static struct sexp_iterator *make_iter_vector(struct sexp *s) ...@@ -355,7 +382,8 @@ static struct sexp_iterator *make_iter_vector(struct sexp *s)
return &iter->super; return &iter->super;
} }
static struct lsh_string *do_format_sexp_vector(struct sexp *e, int style) static struct lsh_string *do_format_sexp_vector(struct sexp *e,
int style, unsigned indent)
{ {
CAST(sexp_vector, v, e); CAST(sexp_vector, v, e);
...@@ -384,7 +412,7 @@ static struct lsh_string *do_format_sexp_vector(struct sexp *e, int style) ...@@ -384,7 +412,7 @@ static struct lsh_string *do_format_sexp_vector(struct sexp *e, int style)
{ {
CAST_SUBTYPE(sexp, o, LIST(v->elements)[i]); CAST_SUBTYPE(sexp, o, LIST(v->elements)[i]);
elements[i] = sexp_format(o, style); elements[i] = sexp_format(o, style, indent + 1);
size += elements[i]->length; size += elements[i]->length;
} }
...@@ -472,17 +500,17 @@ struct sexp *sexp_sn(const mpz_t n) ...@@ -472,17 +500,17 @@ struct sexp *sexp_sn(const mpz_t n)
fatal("sexp_sn: Signed numbers are not supported.\n"); fatal("sexp_sn: Signed numbers are not supported.\n");
} }
struct lsh_string *sexp_format(struct sexp *e, int style) struct lsh_string *sexp_format(struct sexp *e, int style, unsigned indent)
{ {
switch(style) switch(style)
{ {
case SEXP_TRANSPORT: case SEXP_TRANSPORT:
return encode_base64(sexp_format(e, SEXP_CANONICAL), "{}", 1); return encode_base64(sexp_format(e, SEXP_CANONICAL, 0), "{}", indent, 1);
case SEXP_CANONICAL: case SEXP_CANONICAL:
case SEXP_ADVANCED: case SEXP_ADVANCED:
case SEXP_INTERNATIONAL: case SEXP_INTERNATIONAL:
/* NOTE: Check for NULL here? I don't think so. */ /* NOTE: Check for NULL here? I don't think so. */
return SEXP_FORMAT(e, style); return SEXP_FORMAT(e, style, indent);
default: default:
fatal("sexp_format: Unknown output style.\n"); fatal("sexp_format: Unknown output style.\n");
} }
...@@ -504,6 +532,7 @@ static void encode_base64_group(UINT32 n, UINT8 *dest) ...@@ -504,6 +532,7 @@ static void encode_base64_group(UINT32 n, UINT8 *dest)
struct lsh_string *encode_base64(struct lsh_string *s, struct lsh_string *encode_base64(struct lsh_string *s,
const char *delimiters, const char *delimiters,
unsigned indent UNUSED,
int free) int free)
{ {
UINT32 full_groups = (s->length) / 3; UINT32 full_groups = (s->length) / 3;
......
...@@ -44,10 +44,10 @@ struct sexp_iterator; ...@@ -44,10 +44,10 @@ struct sexp_iterator;
(vars (vars
;; NULL for non-lists ;; NULL for non-lists
(iter method "struct sexp_iterator *") (iter method "struct sexp_iterator *")
(format method "struct lsh_string *" "int style"))) (format method "struct lsh_string *" "int style" "unsigned indent")))
*/ */
#define SEXP_FORMAT(e, s) ((e)->format((e), (s))) #define SEXP_FORMAT(e, s, i) ((e)->format((e), (s), (i)))
/* CLASS: /* CLASS:
(class (class
...@@ -89,11 +89,11 @@ struct sexp_iterator; ...@@ -89,11 +89,11 @@ struct sexp_iterator;
/* Like advanced, but allow international characters in quoted strings. */ /* Like advanced, but allow international characters in quoted strings. */
#define SEXP_INTERNATIONAL 3 #define SEXP_INTERNATIONAL 3
struct lsh_string *sexp_format(struct sexp *e, int style); struct lsh_string *sexp_format(struct sexp *e, int style, unsigned indent);
struct lsh_string *encode_base64(struct lsh_string *s, struct lsh_string *encode_base64(struct lsh_string *s,
const char *delimiters, const char *delimiters,
int free); unsigned indent, int free);
/* Creating sexps */ /* Creating sexps */
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include "format.h"
#include "io.h" #include "io.h"
#include "sexp.h" #include "sexp.h"
#include "werror.h" #include "werror.h"
...@@ -45,8 +46,12 @@ ...@@ -45,8 +46,12 @@
static int do_output_sexp(struct sexp_handler *h, struct sexp *e) static int do_output_sexp(struct sexp_handler *h, struct sexp *e)
{ {
CAST(output_sexp, closure, h); CAST(output_sexp, closure, h);
int res = A_WRITE(closure->write, sexp_format(e, closure->style, 0));
return A_WRITE(closure->write, sexp_format(e, closure->style)); if (!LSH_FAILUREP(res))
res |= A_WRITE(closure->write, ssh_format("\n"));
return res;
} }
/* CLASS: /* CLASS:
......
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