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

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)
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("x"), sexp_n(x), -1), -1), -1),
style);
style, 0);
return LSH_FAILUREP(A_WRITE(output, key))
? 1 : 0;
......
......@@ -54,7 +54,8 @@
/* For advanced format */
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)
| CHAR_escapable);
......@@ -140,14 +141,15 @@ static struct lsh_string *do_format_simple_string(struct lsh_string *s,
return res;
}
/* Base 64 string */
return encode_base64(s, "||", 0);
return encode_base64(s, "||", indent + 1, 0);
}
default:
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);
......@@ -160,11 +162,11 @@ static struct lsh_string *do_format_sexp_string(struct sexp *s, int style)
case SEXP_CANONICAL:
if (self->display)
return ssh_format("[%lfS]%lfS",
do_format_simple_string(self->display, style),
do_format_simple_string(self->contents, style));
do_format_simple_string(self->display, style, indent + 1),
do_format_simple_string(self->contents, style, indent));
else
return ssh_format("%lfS",
do_format_simple_string(self->contents, style));
do_format_simple_string(self->contents, style, indent));
default:
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)
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("()");
}
......@@ -241,7 +244,8 @@ static struct sexp_iterator *make_iter_cons(struct sexp *s)
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;
......@@ -258,14 +262,23 @@ static struct lsh_string *do_format_sexp_tail(struct sexp_cons *c, int style)
/* Fall through */
case SEXP_CANONICAL:
return ssh_format(use_space ? " %ls%ls" : "%ls%ls",
sexp_format(c->car, style),
do_format_sexp_tail(c->cdr, style));
sexp_format(c->car, style, indent),
do_format_sexp_tail(c->cdr, style, indent));
default:
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);
......@@ -275,8 +288,22 @@ static struct lsh_string *do_format_sexp_cons(struct sexp *s, int style)
fatal("Internal error!\n");
case SEXP_ADVANCED:
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:
return ssh_format("(%ls", do_format_sexp_tail(self, style));
return ssh_format("(%ls", do_format_sexp_tail(self, style, indent));
default:
fatal("do_format_sexp_tail: Unknown output style.\n");
}
......@@ -355,7 +382,8 @@ static struct sexp_iterator *make_iter_vector(struct sexp *s)
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);
......@@ -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]);
elements[i] = sexp_format(o, style);
elements[i] = sexp_format(o, style, indent + 1);
size += elements[i]->length;
}
......@@ -472,17 +500,17 @@ struct sexp *sexp_sn(const mpz_t 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)
{
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_ADVANCED:
case SEXP_INTERNATIONAL:
/* NOTE: Check for NULL here? I don't think so. */
return SEXP_FORMAT(e, style);
return SEXP_FORMAT(e, style, indent);
default:
fatal("sexp_format: Unknown output style.\n");
}
......@@ -504,6 +532,7 @@ static void encode_base64_group(UINT32 n, UINT8 *dest)
struct lsh_string *encode_base64(struct lsh_string *s,
const char *delimiters,
unsigned indent UNUSED,
int free)
{
UINT32 full_groups = (s->length) / 3;
......
......@@ -44,10 +44,10 @@ struct sexp_iterator;
(vars
;; NULL for non-lists
(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
......@@ -89,11 +89,11 @@ struct sexp_iterator;
/* Like advanced, but allow international characters in quoted strings. */
#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,
const char *delimiters,
int free);
unsigned indent, int free);
/* Creating sexps */
......
......@@ -24,6 +24,7 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "format.h"
#include "io.h"
#include "sexp.h"
#include "werror.h"
......@@ -45,8 +46,12 @@
static int do_output_sexp(struct sexp_handler *h, struct sexp *e)
{
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:
......
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