2 * Copyright (c) 2005, 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Use is subject to license terms.
5 * Copyright (c) 1984 AT&T
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.
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
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include "apr_strings.h"
28 static const char *const trans[040] = {
61 static const char rub[] = {"\\177"};
63 extern int sed_step(char *p1, char *p2, int circf, step_vars_storage *vars);
64 static int substitute(sed_eval_t *eval, sed_reptr_t *ipc,
65 step_vars_storage *step_vars);
66 static apr_status_t execute(sed_eval_t *eval);
67 static int match(sed_eval_t *eval, char *expbuf, int gf,
68 step_vars_storage *step_vars);
69 static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
70 step_vars_storage *step_vars);
71 static char *place(sed_eval_t *eval, char *asp, char *al1, char *al2);
72 static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
73 step_vars_storage *step_vars);
74 static apr_status_t wline(sed_eval_t *eval, char *buf, int sz);
75 static apr_status_t arout(sed_eval_t *eval);
77 static void eval_errf(sed_eval_t *eval, const char *fmt, ...)
79 if (eval->errfn && eval->pool) {
83 error = apr_pvsprintf(eval->pool, fmt, args);
84 eval->errfn(eval->data, error);
89 #define INIT_BUF_SIZE 1024
94 static void grow_buffer(apr_pool_t *pool, char **buffer,
95 char **spend, unsigned int *cursize,
98 char* newbuffer = NULL;
100 if (*cursize >= newsize)
102 /* Avoid number of times realloc is called. It could cause huge memory
103 * requirement if line size is huge e.g 2 MB */
104 if (newsize < *cursize * 2) {
105 newsize = *cursize * 2;
108 /* Align it to 4 KB boundary */
109 newsize = (newsize + ((1 << 12) - 1)) & ~((1 << 12) -1);
110 newbuffer = apr_pcalloc(pool, newsize);
111 if (*spend && *buffer && (*cursize > 0)) {
112 spendsize = *spend - *buffer;
114 if ((*cursize > 0) && *buffer) {
115 memcpy(newbuffer, *buffer, *cursize);
119 if (spend != buffer) {
120 *spend = *buffer + spendsize;
127 static void grow_line_buffer(sed_eval_t *eval, int newsize)
129 grow_buffer(eval->pool, &eval->linebuf, &eval->lspend,
130 &eval->lsize, newsize);
136 static void grow_hold_buffer(sed_eval_t *eval, int newsize)
138 grow_buffer(eval->pool, &eval->holdbuf, &eval->hspend,
139 &eval->hsize, newsize);
145 static void grow_gen_buffer(sed_eval_t *eval, int newsize,
148 if (gspend == NULL) {
149 gspend = &eval->genbuf;
151 grow_buffer(eval->pool, &eval->genbuf, gspend,
152 &eval->gsize, newsize);
153 eval->lcomend = &eval->genbuf[71];
157 * appendmem_to_linebuf
159 static void appendmem_to_linebuf(sed_eval_t *eval, const char* sz, int len)
161 unsigned int reqsize = (eval->lspend - eval->linebuf) + len;
162 if (eval->lsize < reqsize) {
163 grow_line_buffer(eval, reqsize);
165 memcpy(eval->lspend, sz, len);
172 static void append_to_linebuf(sed_eval_t *eval, const char* sz)
174 int len = strlen(sz);
175 /* Copy string including null character */
176 appendmem_to_linebuf(eval, sz, len + 1);
177 --eval->lspend; /* lspend will now point to NULL character */
183 static void copy_to_linebuf(sed_eval_t *eval, const char* sz)
185 eval->lspend = eval->linebuf;
186 append_to_linebuf(eval, sz);
192 static void append_to_holdbuf(sed_eval_t *eval, const char* sz)
194 int len = strlen(sz);
195 unsigned int reqsize = (eval->hspend - eval->holdbuf) + len + 1;
196 if (eval->hsize <= reqsize) {
197 grow_hold_buffer(eval, reqsize);
199 strcpy(eval->hspend, sz);
200 /* hspend will now point to NULL character */
207 static void copy_to_holdbuf(sed_eval_t *eval, const char* sz)
209 eval->hspend = eval->holdbuf;
210 append_to_holdbuf(eval, sz);
216 static void append_to_genbuf(sed_eval_t *eval, const char* sz, char **gspend)
218 int len = strlen(sz);
219 unsigned int reqsize = (*gspend - eval->genbuf) + len + 1;
220 if (eval->gsize < reqsize) {
221 grow_gen_buffer(eval, reqsize, gspend);
224 /* *gspend will now point to NULL character */
231 static void copy_to_genbuf(sed_eval_t *eval, const char* sz)
233 int len = strlen(sz);
234 unsigned int reqsize = len + 1;
235 if (eval->gsize < reqsize) {
236 grow_gen_buffer(eval, reqsize, NULL);
243 apr_status_t sed_init_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_fn_t *errfn, void *data, sed_write_fn_t *writefn, apr_pool_t* p)
245 memset(eval, 0, sizeof(*eval));
247 eval->writefn = writefn;
248 return sed_reset_eval(eval, commands, errfn, data);
254 apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_fn_t *errfn, void *data)
261 eval->commands = commands;
266 if (eval->linebuf == NULL) {
267 eval->lsize = INIT_BUF_SIZE;
268 eval->linebuf = apr_pcalloc(eval->pool, eval->lsize);
270 if (eval->holdbuf == NULL) {
271 eval->hsize = INIT_BUF_SIZE;
272 eval->holdbuf = apr_pcalloc(eval->pool, eval->hsize);
274 if (eval->genbuf == NULL) {
275 eval->gsize = INIT_BUF_SIZE;
276 eval->genbuf = apr_pcalloc(eval->pool, eval->gsize);
278 eval->lspend = eval->linebuf;
279 eval->hspend = eval->holdbuf;
280 eval->lcomend = &eval->genbuf[71];
282 for (i = 0; i < sizeof(eval->abuf) / sizeof(eval->abuf[0]); i++)
283 eval->abuf[i] = NULL;
284 eval->aptr = eval->abuf;
285 eval->pending = NULL;
286 eval->inar = apr_pcalloc(eval->pool, commands->nrep * sizeof(unsigned char));
287 eval->nrep = commands->nrep;
293 eval->lreadyflag = 0;
295 eval->finalflag = 1; /* assume we're evaluating only one file/stream */
300 for (i = 0; i < commands->nfiles; i++) {
301 const char* filename = commands->fname[i];
302 if (apr_file_open(&eval->fcode[i], filename,
303 APR_WRITE | APR_CREATE, APR_OS_DEFAULT,
304 eval->pool) != APR_SUCCESS) {
305 eval_errf(eval, SEDERR_COMES, filename);
316 void sed_destroy_eval(sed_eval_t *eval)
319 /* eval->linebuf, eval->holdbuf, eval->genbuf and eval->inar are allocated
320 * on pool. It will be freed when pool will be freed */
321 for (i = 0; i < eval->commands->nfiles; i++) {
322 if (eval->fcode[i] != NULL) {
323 apr_file_close(eval->fcode[i]);
324 eval->fcode[i] = NULL;
332 apr_status_t sed_eval_file(sed_eval_t *eval, apr_file_t *fin, void *fout)
336 apr_size_t read_bytes = 0;
338 read_bytes = sizeof(buf);
339 if (apr_file_read(fin, buf, &read_bytes) != APR_SUCCESS)
342 if (sed_eval_buffer(eval, buf, read_bytes, fout) != APR_SUCCESS)
349 return sed_finalize_eval(eval, fout);
355 apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, int bufsz, void *fout)
362 if (!sed_canbe_finalized(eval->commands)) {
363 /* Commands were not finalized properly. */
364 const char* error = sed_get_finalize_error(eval->commands, eval->pool);
366 eval_errf(eval, error);
373 /* Process leftovers */
374 if (bufsz && eval->lreadyflag) {
375 eval->lreadyflag = 0;
377 *eval->lspend = '\0';
379 if (rv != APR_SUCCESS)
387 n = memchr(buf, '\n', bufsz);
392 if (llen == bufsz - 1) {
393 /* This might be the last line; delay its processing */
394 eval->lreadyflag = 1;
398 appendmem_to_linebuf(eval, buf, llen + 1);
400 /* replace new line character with NULL */
401 *eval->lspend = '\0';
405 if (rv != APR_SUCCESS)
411 /* Save the leftovers for later */
413 appendmem_to_linebuf(eval, buf, bufsz);
422 apr_status_t sed_finalize_eval(sed_eval_t *eval, void *fout)
432 /* Process leftovers */
433 if (eval->lspend > eval->linebuf) {
436 if (eval->lreadyflag) {
437 eval->lreadyflag = 0;
440 /* Code can probably reach here when last character in output
441 * buffer is not a newline.
443 /* Assure space for NULL */
444 append_to_linebuf(eval, "");
447 *eval->lspend = '\0';
449 if (rv != APR_SUCCESS)
461 static apr_status_t execute(sed_eval_t *eval)
463 sed_reptr_t *ipc = eval->commands->ptrspace;
464 step_vars_storage step_vars;
465 apr_status_t rv = APR_SUCCESS;
473 eval->pending = NULL;
476 memset(&step_vars, 0, sizeof(step_vars));
478 while (ipc->command) {
488 if (eval->inar[ipc->nrep]) {
491 } else if (*p2 == CLNUM) {
492 c = (unsigned char)p2[1];
493 if (eval->lnum > eval->commands->tlno[c]) {
494 eval->inar[ipc->nrep] = 0;
500 if (eval->lnum == eval->commands->tlno[c]) {
501 eval->inar[ipc->nrep] = 0;
503 } else if (match(eval, p2, 0, &step_vars)) {
504 eval->inar[ipc->nrep] = 0;
506 } else if (*p1 == CEND) {
507 if (!eval->dolflag) {
513 } else if (*p1 == CLNUM) {
514 c = (unsigned char)p1[1];
515 if (eval->lnum != eval->commands->tlno[c]) {
522 eval->inar[ipc->nrep] = 1;
523 } else if (match(eval, p1, 0, &step_vars)) {
525 eval->inar[ipc->nrep] = 1;
540 rv = command(eval, ipc, &step_vars);
541 if (rv != APR_SUCCESS)
555 if ((ipc = ipc->lb1) == 0) {
556 ipc = eval->commands->ptrspace;
563 if (!eval->commands->nflag && !eval->delflag) {
564 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
565 if (rv != APR_SUCCESS)
569 if (eval->aptr > eval->abuf)
574 eval->lspend = eval->linebuf;
582 static int match(sed_eval_t *eval, char *expbuf, int gf,
583 step_vars_storage *step_vars)
589 if(*expbuf) return(0);
590 step_vars->locs = p1 = step_vars->loc2;
597 return(sed_step(p1, expbuf, circf, step_vars));
603 static int substitute(sed_eval_t *eval, sed_reptr_t *ipc,
604 step_vars_storage *step_vars)
606 if(match(eval, ipc->re1, 0, step_vars) == 0) return(0);
609 eval->sflag = 0; /* Flags if any substitution was made */
610 if (dosub(eval, ipc->rhs, ipc->gfl, step_vars) != APR_SUCCESS)
614 while(*step_vars->loc2) {
615 if(match(eval, ipc->re1, 1, step_vars) == 0) break;
616 if (dosub(eval, ipc->rhs, ipc->gfl, step_vars) != APR_SUCCESS)
626 static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
627 step_vars_storage *step_vars)
631 apr_status_t rv = APR_SUCCESS;
633 if(n > 0 && n < 999) {
635 if(n != eval->numpass) return APR_SUCCESS;
641 sp = place(eval, sp, lp, step_vars->loc1);
642 while ((c = *rp++) != 0) {
644 sp = place(eval, sp, step_vars->loc1, step_vars->loc2);
648 else if (c == '\\') {
650 if (c >= '1' && c < NBRA+'1') {
651 sp = place(eval, sp, step_vars->braslist[c-'1'],
652 step_vars->braelist[c-'1']);
660 if (sp >= eval->genbuf + eval->gsize) {
661 /* expand genbuf and set the sp appropriately */
662 grow_gen_buffer(eval, eval->gsize + 1024, &sp);
665 lp = step_vars->loc2;
666 step_vars->loc2 = sp - eval->genbuf + eval->linebuf;
667 append_to_genbuf(eval, lp, &sp);
668 copy_to_linebuf(eval, eval->genbuf);
675 static char *place(sed_eval_t *eval, char *asp, char *al1, char *al2)
679 unsigned int reqsize = (sp - eval->genbuf) + n + 1;
681 if (eval->gsize < reqsize) {
682 grow_gen_buffer(eval, reqsize, &sp);
691 static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
692 step_vars_storage *step_vars)
698 char sz[32]; /* 32 bytes enough to store 64 bit integer in decimal */
699 apr_status_t rv = APR_SUCCESS;
702 switch(ipc->command) {
705 if(eval->aptr >= &eval->abuf[SED_ABUFSIZE]) {
706 eval_errf(eval, SEDERR_TMAMES, eval->lnum);
715 if(!eval->inar[ipc->nrep] || eval->dolflag) {
716 for (p1 = ipc->re1; *p1; p1++)
718 rv = wline(eval, ipc->re1, p1 - ipc->re1);
735 copy_to_linebuf(eval, p1);
740 length = apr_snprintf(sz, sizeof(sz), "%d", (int) eval->lnum);
741 rv = wline(eval, sz, length);
745 copy_to_linebuf(eval, eval->holdbuf);
749 append_to_linebuf(eval, "\n");
750 append_to_linebuf(eval, eval->holdbuf);
754 copy_to_holdbuf(eval, eval->linebuf);
758 append_to_holdbuf(eval, "\n");
759 append_to_holdbuf(eval, eval->linebuf);
763 for (p1 = ipc->re1; *p1; p1++);
764 rv = wline(eval, ipc->re1, p1 - ipc->re1);
775 eval->genbuf[72] = 0;
777 if((unsigned char)*p1 >= 040) {
780 while ((*p2++ = *p3++) != 0)
781 if(p2 >= eval->lcomend) {
783 rv = wline(eval, eval->genbuf,
784 strlen(eval->genbuf));
785 if (rv != APR_SUCCESS)
793 if(!isprint(*p1 & 0377)) {
795 if(p2 >= eval->lcomend) {
797 rv = wline(eval, eval->genbuf,
798 strlen(eval->genbuf));
799 if (rv != APR_SUCCESS)
803 *p2++ = (*p1 >> 6) + '0';
804 if(p2 >= eval->lcomend) {
806 rv = wline(eval, eval->genbuf,
807 strlen(eval->genbuf));
808 if (rv != APR_SUCCESS)
812 *p2++ = ((*p1 >> 3) & 07) + '0';
813 if(p2 >= eval->lcomend) {
815 rv = wline(eval, eval->genbuf,
816 strlen(eval->genbuf));
817 if (rv != APR_SUCCESS)
821 *p2++ = (*p1++ & 07) + '0';
822 if(p2 >= eval->lcomend) {
824 rv = wline(eval, eval->genbuf,
825 strlen(eval->genbuf));
826 if (rv != APR_SUCCESS)
832 if(p2 >= eval->lcomend) {
834 rv = wline(eval, eval->genbuf,
835 strlen(eval->genbuf));
836 if (rv != APR_SUCCESS)
842 p3 = trans[(unsigned char)*p1-1];
843 while ((*p2++ = *p3++) != 0)
844 if(p2 >= eval->lcomend) {
846 rv = wline(eval, eval->genbuf,
847 strlen(eval->genbuf));
848 if (rv != APR_SUCCESS)
856 rv = wline(eval, eval->genbuf, strlen(eval->genbuf));
860 if(!eval->commands->nflag) {
861 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
862 if (rv != APR_SUCCESS)
866 if(eval->aptr > eval->abuf) {
868 if (rv != APR_SUCCESS)
871 eval->lspend = eval->linebuf;
872 eval->pending = ipc->next;
876 if(eval->aptr > eval->abuf) {
878 if (rv != APR_SUCCESS)
881 append_to_linebuf(eval, "\n");
882 eval->pending = ipc->next;
886 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
889 for (p1 = eval->linebuf; *p1 != '\n' && *p1 != '\0'; p1++);
890 rv = wline(eval, eval->linebuf, p1 - eval->linebuf);
894 if (!eval->commands->nflag) {
895 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
896 if (rv != APR_SUCCESS)
900 if(eval->aptr > eval->abuf) {
902 if (rv != APR_SUCCESS)
909 if(eval->aptr >= &eval->abuf[SED_ABUFSIZE]) {
910 eval_errf(eval, SEDERR_TMRMES, eval->lnum);
918 i = substitute(eval, ipc, step_vars);
922 if(ipc->pfl && eval->commands->nflag && i) {
924 rv = wline(eval, eval->linebuf, eval->lspend -
926 if (rv != APR_SUCCESS)
929 for (p1 = eval->linebuf; *p1 != '\n' && *p1 != '\0'; p1++);
930 rv = wline(eval, eval->linebuf, p1 - eval->linebuf);
931 if (rv != APR_SUCCESS)
935 if (i && (ipc->findex >= 0) && eval->fcode[ipc->findex])
936 apr_file_printf(eval->fcode[ipc->findex], "%s\n",
941 if(eval->sflag == 0) break;
947 if (ipc->findex >= 0)
948 apr_file_printf(eval->fcode[ipc->findex], "%s\n",
952 copy_to_genbuf(eval, eval->linebuf);
953 copy_to_linebuf(eval, eval->holdbuf);
954 copy_to_holdbuf(eval, eval->genbuf);
960 while((*p1 = p2[(unsigned char)*p1]) != 0) p1++;
969 static apr_status_t arout(sed_eval_t *eval)
971 apr_status_t rv = APR_SUCCESS;
972 eval->aptr = eval->abuf - 1;
973 while (*++eval->aptr) {
974 if ((*eval->aptr)->command == ACOM) {
977 for (p1 = (*eval->aptr)->re1; *p1; p1++);
978 rv = wline(eval, (*eval->aptr)->re1, p1 - (*eval->aptr)->re1);
979 if (rv != APR_SUCCESS)
982 apr_file_t *fi = NULL;
984 apr_size_t n = sizeof(buf);
986 if (apr_file_open(&fi, (*eval->aptr)->re1, APR_READ, 0, eval->pool)
989 while ((apr_file_read(fi, buf, &n)) == APR_SUCCESS) {
992 rv = eval->writefn(eval->fout, buf, n);
993 if (rv != APR_SUCCESS) {
1002 eval->aptr = eval->abuf;
1010 static apr_status_t wline(sed_eval_t *eval, char *buf, int sz)
1012 apr_status_t rv = APR_SUCCESS;
1013 rv = eval->writefn(eval->fout, buf, sz);
1014 if (rv != APR_SUCCESS)
1016 rv = eval->writefn(eval->fout, "\n", 1);