sexp.h 3.63 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
#include "list.h"
Niels Möller's avatar
Niels Möller committed
31

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

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

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

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

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

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

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

/* 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
81
82
83
84
/* Output styles */

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

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

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

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

Niels Möller's avatar
Niels Möller committed
96
/* Creating sexps */
Niels Möller's avatar
Niels Möller committed
97
98
99
100

/* Consumes its args (display may be NULL) */
struct sexp *make_sexp_string(struct lsh_string *d, struct lsh_string *c);

Niels Möller's avatar
Niels Möller committed
101
/* atom->sexp */
Niels Möller's avatar
Niels Möller committed
102
struct sexp *sexp_a(const int a);
Niels Möller's avatar
Niels Möller committed
103
104

/* cstring->sexp */
Niels Möller's avatar
Niels Möller committed
105
struct sexp *sexp_z(const char *s);
Niels Möller's avatar
Niels Möller committed
106
107

/* mpz->atom */
Niels Möller's avatar
Niels Möller committed
108
109
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
110
111
112
113
114
115
116

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

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

Niels Möller's avatar
Niels Möller committed
117
118
119
/* vector */
struct sexp *sexp_v(struct object_list *l);

120
#if 0
Niels Möller's avatar
Niels Möller committed
121
122
123
/* 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
124
int sexp_consp(struct sexp *e);
Niels Möller's avatar
Niels Möller committed
125
126

/* For lists */
Niels Möller's avatar
Niels Möller committed
127
128
struct sexp *sexp_car(const struct sexp *e);
struct sexp *sexp_cdr(const struct sexp *e);
129
130
#endif

Niels Möller's avatar
Niels Möller committed
131
132
int sexp_nullp(const struct sexp *e);
int sexp_atomp(const struct sexp *e);
Niels Möller's avatar
Niels Möller committed
133

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

Niels Möller's avatar
Niels Möller committed
136
137
138
139
140
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
141

Niels Möller's avatar
Niels Möller committed
142
extern int sexp_char_classes[];
Niels Möller's avatar
Niels Möller committed
143
144
145

/* Parsing sexp */

Niels Möller's avatar
Niels Möller committed
146
/* CLASS:
Niels Möller's avatar
Niels Möller committed
147
148
149
   (class
     (name sexp_handler)
     (vars
Niels Möller's avatar
Niels Möller committed
150
       ;; Called when a complete sexpression has been read.
151
       (handler method int "struct sexp *e")))
Niels Möller's avatar
Niels Möller committed
152
153
*/

154
#define HANDLE_SEXP(h, s) ((h)->handler((h), (s)))
155
     
Niels Möller's avatar
Niels Möller committed
156
157
158
#endif /* LSH_SEXP_H_INCLUDED */