sexp.h 3.48 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* sexp.h
 *
 * An implementation of Ron Rivest's S-expressions, used in spki.
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef LSH_SEXP_H_INCLUDED
#define LSH_SEXP_H_INCLUDED

29
#include "bignum.h"
Niels Möller's avatar
Niels Möller committed
30

31
32
33
/* Forward declaration */
struct sexp_iterator;

Niels Möller's avatar
Niels Möller committed
34
#define CLASS_DECLARE
35
#include "sexp.h.x"
Niels Möller's avatar
Niels Möller committed
36
#undef CLASS_DECLARE
Niels Möller's avatar
Niels Möller committed
37
38
39
40
41

/* CLASS:
   (class
     (name sexp)
     (vars
42
       ;; NULL for non-lists
Niels Möller's avatar
Niels Möller committed
43
       (iter method "struct sexp_iterator *")
44
       (format method "struct lsh_string *" "int style")))
Niels Möller's avatar
Niels Möller committed
45
46
*/

Niels Möller's avatar
Niels Möller committed
47
48
#define SEXP_FORMAT(e, s) ((e)->format((e), (s)))

Niels Möller's avatar
Niels Möller committed
49
50
/* CLASS:
   (class
51
     (name sexp_cons)
Niels Möller's avatar
Niels Möller committed
52
53
     (super sexp)
     (vars
54
55
       (car object sexp)
       (cdr object sexp_cons)))
Niels Möller's avatar
Niels Möller committed
56
57
*/

58
/* ;; CLASS:
Niels Möller's avatar
Niels Möller committed
59
60
61
62
63
64
65
   (class
     (name sexp_atom)
     (super sexp)
     (vars
       (atom . int)))
*/

66
67
68
69
70
71
72
73
74
75
76
77
78
79

/* Iterator abstraction idea taken from Ron's code */
/* CLASS:
   (class
     (name sexp_iterator)
     (vars
       (get method "struct sexp *")
       (set method void "struct sexp *")
       (next method void)))
*/

#define SEXP_CURRENT(i) ((i)->current((i)))
#define SEXP_NEXT(i) ((i)->next((i)))

Niels Möller's avatar
Niels Möller committed
80
81
82
83
/* Output styles */

#define SEXP_CANONICAL 0
#define SEXP_TRANSPORT 1
84
85
#define SEXP_ADVANCED 2

Niels Möller's avatar
Niels Möller committed
86
87
88
/* Like advanced, but allow international characters in quoted strings. */
#define SEXP_INTERNATIONAL 3

89
90
struct lsh_string *sexp_format(struct sexp *e, int style);

Niels Möller's avatar
Niels Möller committed
91
struct lsh_string *encode_base64(struct lsh_string *s,
Niels Möller's avatar
Niels Möller committed
92
				 const char *delimiters,
Niels Möller's avatar
Niels Möller committed
93
				 int free);
Niels Möller's avatar
Niels Möller committed
94

Niels Möller's avatar
Niels Möller committed
95
96
/* Creating sexps */
/* atom->sexp */
Niels Möller's avatar
Niels Möller committed
97
struct sexp *sexp_a(const int a);
Niels Möller's avatar
Niels Möller committed
98
99

/* cstring->sexp */
Niels Möller's avatar
Niels Möller committed
100
struct sexp *sexp_z(const char *s);
Niels Möller's avatar
Niels Möller committed
101
102

/* mpz->atom */
Niels Möller's avatar
Niels Möller committed
103
104
struct sexp *sexp_n(const mpz_t n);
struct sexp *sexp_sn(const mpz_t n);
Niels Möller's avatar
Niels Möller committed
105
106
107
108
109
110
111

/* cons */
struct sexp *sexp_c(struct sexp *car, struct sexp_cons *cdr);

/* list */
struct sexp *sexp_l(unsigned n, ...);

112
#if 0
Niels Möller's avatar
Niels Möller committed
113
114
115
/* Extracting information from sexp. These functions accept NULL
 * arguments, and return NULL if the conversion is not possible */

Niels Möller's avatar
Niels Möller committed
116
int sexp_consp(struct sexp *e);
Niels Möller's avatar
Niels Möller committed
117
118

/* For lists */
Niels Möller's avatar
Niels Möller committed
119
120
struct sexp *sexp_car(const struct sexp *e);
struct sexp *sexp_cdr(const struct sexp *e);
121
122
#endif

Niels Möller's avatar
Niels Möller committed
123
124
int sexp_nullp(const struct sexp *e);
int sexp_atomp(const struct sexp *e);
Niels Möller's avatar
Niels Möller committed
125

Niels Möller's avatar
Niels Möller committed
126
/* int sexp_null_cdr(struct sexp *e); */
Niels Möller's avatar
Niels Möller committed
127

Niels Möller's avatar
Niels Möller committed
128
129
130
131
132
struct lsh_string *sexp_contents(const struct sexp *e);
struct lsh_string *sexp_display(const struct sexp *e);
int sexp_atom(const struct sexp *e);
int sexp_bignum_u(const struct sexp *e, mpz_t n);
int sexp_bignum_s(const struct sexp *e, mpz_t n);
Niels Möller's avatar
Niels Möller committed
133
134
135
136


/* Parsing sexp */

137
/* ;;CLASS:
Niels Möller's avatar
Niels Möller committed
138
139
140
   (class
     (name sexp_handler)
     (vars
Niels Möller's avatar
Niels Möller committed
141
       ;; Called when a complete sexpression has been read.
142
       (handler method int "struct sexp *e")))
Niels Möller's avatar
Niels Möller committed
143
144
*/

145
146
#define HANDLE_SEXP(h, s) ((h)->handler((h), (s)))

147
148
149
150
#if 0
struct read_handler make_read_sexp(struct sexp_handler *h);
#endif
     
Niels Möller's avatar
Niels Möller committed
151
152
153
#endif /* LSH_SEXP_H_INCLUDED */