summaryrefslogtreecommitdiff
path: root/lib/rlib.h
blob: b5f8b6887d0e58e3570903df4a4540be876bfc68 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#ifndef __RLIB_H__
#define __RLIB_H__

#ifdef __cplusplus
extern "C" {
#endif

struct internal_state;
struct r_stream;

typedef unsigned char r_byte;
typedef unsigned int r_uint;


/*
 * r_replace:
 *  Represents a replacement which was found. This structure is passed
 *  to the r_match matching callback function below.
 */
typedef struct _r_replace
{
	r_uint offset;	/* The offset from the beginning of the file */
	r_uint from;	/* The offset from the beginning of the current buffer */
	r_uint flen;	/* The length of the data to be replaced */
	r_byte* to;		/* The data to replace */
	r_uint tlen;	/* The length of the replacement data */
}
r_replace;




/*
 * r_match
 *  Callback function which receives replacements made by the script.
 *
 *  stream:		rlib stream
 *  replace:	The replacement.
 *
 *  return 1 to accept this match, or 0 to skip.
 */
typedef int (*r_match)(struct r_stream* stream, r_replace* replace);

/*
 * r_write
 *  Callback used by the rlibRun output processed data.
 *
 *  stream:		rlib stream
 *  data:		The data
 *  len:		The number of bytes to write from data.
 */
typedef int (*r_write)(struct r_stream* stream, r_byte* data, r_uint len);

/*
 * r_message
 *  Callback which is sent messages from the 'message' command
 *  in a rep script.
 *
 *  stream:		rlib stream
 *  message:	Null terminated message
 */
typedef void (*r_message)(struct r_stream* stream, const char* message);






/*
 * r_stream
 *  The basic data interface into rlib.
 */
typedef struct r_stream
{
	/* ------ Data you supply -------------- */
	r_byte* nextIn;		/* The next byte to be read by rlib */
	r_uint availIn;		/* The number of bytes at nextIn */

	r_write fWrite;		/* Callback used for output */
	r_match fMatch;		/* Callback for confirmation or matching */
	r_message fMessage;	/* Callback for messages from script */
	void* arg;			/* Optional data for the above functions */

	/* -------- Data returned -------------- */
	r_uint total;		/* Total replaces */

	/* -------- Internal ------------------- */
	struct internal_state* state;
}
r_stream;





/*
 * r_script
 *  Represents a loaded rep script along with syntax error
 *  information.
 */
typedef struct _r_script
{
	r_byte* ops;		/* Compiled script */
	r_uint len;			/* The length of the script */
	char* error;		/* Syntax error details */
	r_uint errline;		/* Line number of syntax error */
}
r_script;





/*
 * rlibCompile
 *  Call this function to compile a script. It will be compiled into the
 *  r_sript structure. Be sure to zero the script structure passed.
 *
 *  script:		rlib script
 *  data:		rep script text. Must be null terminated.
 */
int rlibCompile(r_script* script, const char* data);



/*
 * rlibDump
 *  Write out byte codes for a script
 *
 *  script:		rlib script
 *  f:			Output stream to write to
 */
void rlibDump(r_script* script, FILE* f);



/*
 * rlibFree
 *  Free internal variables associated with this stream and/or
 *  script structure.
 *
 *  stream:		rlib stream
 *  script:		rlib script
 */
void rlibFree(r_stream* stream, r_script* script);



/*
 * rlibInit
 *  Call this function to initialize a rlib stream.
 *  For any of the following functions to work a stream must have been
 *  initialized.
 *
 *  stream:		Pointer to a zero'd stream structure.
 *  options:	Can be any combination of the following mode values.
 */
int rlibInit(r_stream* stream, long options);

/* Causes rlib to only output replacing text */
#define RLIB_MODE_PARSER	0x00000100

/* Causes rlib not to output anything */
#define RLIB_MODE_MATCHER	0x00000200



/*
 * rlibSetVar
 *  Set a variable for use in the script.
 *
 *  stream:		rlib stream
 *  var:		Variable name
 *  val:		Variable value
 */
int rlibSetVar(r_stream* stream, const char* var, const char* val);



/*
 * rlibRun
 *  Run the script. rlibRun will only "eat" nextIn upto where the
 *  last replace was found or up half whichever is more. Unless
 *  it's the last buffer in which case it finishes up.
 *
 *  stream:		rlib stream
 *  script:		rlib script
 *  done:		flag whether this is the last buffer
 */
int rlibRun(r_stream* stream, r_script* script, int done);



/* rlibClear: -----------------------------------------------------------
 *  Prepare and cleanup a context/stream after being run for a second
 *  run.
 *
 *  stream:		rlib context/stream
 */
void rlibClear(r_stream* stream);





/* ERROR CODES */

/* OK */
#define R_OK		0

/* Need more input */
#define R_IN		1

/* Finished processing script */
#define R_DONE		3

/* Not enough memory */
#define R_NOMEM		-1

/* Syntax error in the script */
#define R_SYNTAX	-2

/* Regular expression error in the script */
#define R_REGEXP	-3

/* Enless loop encountered */
#define R_LOOP		-4

/* User defined error from script */
#define R_USER		-5

/* Read or write error */
#define R_IOERR		-6

/* Invalid argument or stream data member */
#define R_INVARG	-10

#ifdef __cplusplus
}
#endif

#endif /* __RLIB_H__ */