Home | History | Annotate | Download | only in mod_sed
      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 #ifndef PATH_MAX
     32 #define PATH_MAX MAX_PATH
     33 #endif
     34 
     35 #define SED_NLINES 256
     36 #define SED_DEPTH 20
     37 #define SED_LABSIZE 50
     38 #define SED_ABUFSIZE 20
     39 
     40 typedef struct sed_reptr_s sed_reptr_t;
     41 
     42 struct sed_reptr_s {
     43     sed_reptr_t *next;
     44     char        *ad1;
     45     char        *ad2;
     46     char        *re1;
     47     sed_reptr_t *lb1;
     48     char        *rhs;
     49     int         findex;
     50     char        command;
     51     int         gfl;
     52     char        pfl;
     53     char        negfl;
     54     int         nrep;
     55 };
     56 
     57 typedef struct sed_label_s sed_label_t;
     58 
     59 struct sed_label_s {
     60     char        asc[9];
     61     sed_reptr_t *chain;
     62     sed_reptr_t *address;
     63 };
     64 
     65 typedef void (sed_err_fn_t)(void *data, const char *error);
     66 typedef void (sed_write_fn_t)(void *ctx, char *buf, int sz);
     67 
     68 typedef struct sed_commands_s sed_commands_t;
     69 #define NWFILES 11 /* 10 plus one for standard output */
     70 
     71 struct sed_commands_s {
     72     sed_err_fn_t *errfn;
     73     void         *data;
     74 
     75     unsigned     lsize;
     76     char         *linebuf;
     77     char         *lbend;
     78     const char   *saveq;
     79 
     80     char         *cp;
     81     char         *lastre;
     82     char         *respace;
     83     char         sseof;
     84     char         *reend;
     85     const char   *earg;
     86     int          eflag;
     87     int          gflag;
     88     int          nflag;
     89     apr_int64_t  tlno[SED_NLINES];
     90     int          nlno;
     91     int          depth;
     92 
     93     char         *fname[NWFILES];
     94     int          nfiles;
     95 
     96     sed_label_t  ltab[SED_LABSIZE];
     97     sed_label_t  *labtab;
     98     sed_label_t  *lab;
     99     sed_label_t  *labend;
    100 
    101     sed_reptr_t  **cmpend[SED_DEPTH];
    102     sed_reptr_t  *ptrspace;
    103     sed_reptr_t  *ptrend;
    104     sed_reptr_t  *rep;
    105     int          nrep;
    106     apr_pool_t   *pool;
    107     int          canbefinal;
    108 };
    109 
    110 typedef struct sed_eval_s sed_eval_t;
    111 
    112 struct sed_eval_s {
    113     sed_err_fn_t   *errfn;
    114     sed_write_fn_t *writefn;
    115     void           *data;
    116 
    117     sed_commands_t *commands;
    118 
    119     apr_int64_t    lnum;
    120     void           *fout;
    121 
    122     unsigned       lsize;
    123     char           *linebuf;
    124     char           *lspend;
    125 
    126     unsigned       hsize;
    127     char           *holdbuf;
    128     char           *hspend;
    129 
    130     unsigned       gsize;
    131     char           *genbuf;
    132     char           *lcomend;
    133 
    134     apr_file_t    *fcode[NWFILES];
    135     sed_reptr_t    *abuf[SED_ABUFSIZE];
    136     sed_reptr_t    **aptr;
    137     sed_reptr_t    *pending;
    138     unsigned char  *inar;
    139     int            nrep;
    140 
    141     int            dolflag;
    142     int            sflag;
    143     int            jflag;
    144     int            delflag;
    145     int            lreadyflag;
    146     int            quitflag;
    147     int            finalflag;
    148     int            numpass;
    149     int            nullmatch;
    150     int            col;
    151     apr_pool_t     *pool;
    152 };
    153 
    154 apr_status_t sed_init_commands(sed_commands_t *commands, sed_err_fn_t *errfn, void *data,
    155                                apr_pool_t *p);
    156 apr_status_t sed_compile_string(sed_commands_t *commands, const char *s);
    157 apr_status_t sed_compile_file(sed_commands_t *commands, apr_file_t *fin);
    158 char* sed_get_finalize_error(const sed_commands_t *commands, apr_pool_t* pool);
    159 int sed_canbe_finalized(const sed_commands_t *commands);
    160 void sed_destroy_commands(sed_commands_t *commands);
    161 
    162 apr_status_t sed_init_eval(sed_eval_t *eval, sed_commands_t *commands,
    163                            sed_err_fn_t *errfn, void *data,
    164                            sed_write_fn_t *writefn, apr_pool_t *p);
    165 apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_fn_t *errfn, void *data);
    166 apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, int bufsz, void *fout);
    167 apr_status_t sed_eval_file(sed_eval_t *eval, apr_file_t *fin, void *fout);
    168 apr_status_t sed_finalize_eval(sed_eval_t *eval, void *f);
    169 void sed_destroy_eval(sed_eval_t *eval);
    170 
    171 #ifdef __cplusplus
    172 }
    173 #endif
    174 
    175 #endif /* LIBSED_H */
    176