1 /*
2  * Copyright (c) 2005, 2008 Sun Microsystems, Inc. All Rights Reserved.
3  * Use is subject to license terms.
4  *
5  *      Copyright (c) 1984 AT&T
6  *        All Rights Reserved
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *  http://www.apache.org/licenses/LICENSE-2.0.
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16  * or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef LIBSED_H
22 #define LIBSED_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <limits.h>
29 
30 #include "apr_file_io.h"
31 
32 #define SED_NLINES 256
33 #define SED_DEPTH 20
34 #define SED_LABSIZE 50
35 #define SED_ABUFSIZE 20
36 
37 typedef struct sed_reptr_s sed_reptr_t;
38 
39 struct sed_reptr_s {
40     sed_reptr_t *next;
41     char        *ad1;
42     char        *ad2;
43     char        *re1;
44     sed_reptr_t *lb1;
45     char        *rhs;
46     int         findex;
47     char        command;
48     int         gfl;
49     char        pfl;
50     char        negfl;
51     int         nrep;
52 };
53 
54 typedef struct sed_label_s sed_label_t;
55 
56 struct sed_label_s {
57     char        asc[9];
58     sed_reptr_t *chain;
59     sed_reptr_t *address;
60 };
61 
62 typedef apr_status_t (sed_err_fn_t)(void *data, const char *error);
63 typedef apr_status_t (sed_write_fn_t)(void *ctx, char *buf, apr_size_t sz);
64 
65 typedef struct sed_commands_s sed_commands_t;
66 #define NWFILES 11 /* 10 plus one for standard output */
67 
68 struct sed_commands_s {
69     sed_err_fn_t *errfn;
70     void         *data;
71 
72     apr_size_t   lsize;
73     char         *linebuf;
74     char         *lbend;
75     const char   *saveq;
76 
77     char         *cp;
78     char         *lastre;
79     char         *respace;
80     char         sseof;
81     char         *reend;
82     const char   *earg;
83     int          eflag;
84     int          gflag;
85     int          nflag;
86     apr_int64_t  tlno[SED_NLINES];
87     int          nlno;
88     int          depth;
89 
90     char         *fname[NWFILES];
91     int          nfiles;
92 
93     sed_label_t  ltab[SED_LABSIZE];
94     sed_label_t  *labtab;
95     sed_label_t  *lab;
96     sed_label_t  *labend;
97 
98     sed_reptr_t  **cmpend[SED_DEPTH];
99     sed_reptr_t  *ptrspace;
100     sed_reptr_t  *ptrend;
101     sed_reptr_t  *rep;
102     int          nrep;
103     apr_pool_t   *pool;
104     int          canbefinal;
105 };
106 
107 typedef struct sed_eval_s sed_eval_t;
108 
109 struct sed_eval_s {
110     sed_err_fn_t   *errfn;
111     sed_write_fn_t *writefn;
112     void           *data;
113 
114     sed_commands_t *commands;
115 
116     apr_int64_t    lnum;
117     void           *fout;
118 
119     apr_size_t     lsize;
120     char           *linebuf;
121     char           *lspend;
122 
123     apr_size_t     hsize;
124     char           *holdbuf;
125     char           *hspend;
126 
127     apr_size_t     gsize;
128     char           *genbuf;
129     char           *lcomend;
130 
131     apr_file_t    *fcode[NWFILES];
132     sed_reptr_t    *abuf[SED_ABUFSIZE];
133     sed_reptr_t    **aptr;
134     sed_reptr_t    *pending;
135     unsigned char  *inar;
136     int            nrep;
137 
138     int            dolflag;
139     int            sflag;
140     int            jflag;
141     int            delflag;
142     int            lreadyflag;
143     int            quitflag;
144     int            finalflag;
145     int            numpass;
146     int            nullmatch;
147     int            col;
148     apr_pool_t     *pool;
149 };
150 
151 apr_status_t sed_init_commands(sed_commands_t *commands, sed_err_fn_t *errfn, void *data,
152                                apr_pool_t *p);
153 apr_status_t sed_compile_string(sed_commands_t *commands, const char *s);
154 apr_status_t sed_compile_file(sed_commands_t *commands, apr_file_t *fin);
155 char* sed_get_finalize_error(const sed_commands_t *commands, apr_pool_t* pool);
156 int sed_canbe_finalized(const sed_commands_t *commands);
157 void sed_destroy_commands(sed_commands_t *commands);
158 
159 apr_status_t sed_init_eval(sed_eval_t *eval, sed_commands_t *commands,
160                            sed_err_fn_t *errfn, void *data,
161                            sed_write_fn_t *writefn, apr_pool_t *p);
162 apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_fn_t *errfn, void *data);
163 apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, apr_size_t bufsz, void *fout);
164 apr_status_t sed_eval_file(sed_eval_t *eval, apr_file_t *fin, void *fout);
165 apr_status_t sed_finalize_eval(sed_eval_t *eval, void *f);
166 void sed_destroy_eval(sed_eval_t *eval);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* LIBSED_H */
173