Lines Matching refs:eval
64 static int substitute(sed_eval_t *eval, sed_reptr_t *ipc,
66 static apr_status_t execute(sed_eval_t *eval);
67 static int match(sed_eval_t *eval, char *expbuf, int gf,
69 static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
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,
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);
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,
149 gspend = &eval->genbuf;
151 grow_buffer(eval->pool, &eval->genbuf, gspend,
152 &eval->gsize, newsize);
153 eval->lcomend = &eval->genbuf[71];
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);
166 eval->lspend += len;
172 static void append_to_linebuf(sed_eval_t *eval, const char* sz)
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)
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);
201 eval->hspend += len;
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)
219 unsigned int reqsize = (*gspend - eval->genbuf) + len + 1;
220 if (eval->gsize < reqsize) {
221 grow_gen_buffer(eval, reqsize, gspend);
231 static void copy_to_genbuf(sed_eval_t *eval, const char* sz)
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));
246 eval->pool = p;
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)
258 eval->errfn = errfn;
259 eval->data = data;
261 eval->commands = commands;
263 eval->lnum = 0;
264 eval->fout = NULL;
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;
289 eval->dolflag = 0;
290 eval->sflag = 0;
291 eval->jflag = 0;
292 eval->delflag = 0;
293 eval->lreadyflag = 0;
294 eval->quitflag = 0;
295 eval->finalflag = 1; /* assume we're evaluating only one file/stream */
296 eval->numpass = 0;
297 eval->nullmatch = 0;
298 eval->col = 0;
302 if (apr_file_open(&eval->fcode[i], filename,
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
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)
342 if (sed_eval_buffer(eval, buf, read_bytes, fout) != APR_SUCCESS)
345 if (eval->quitflag)
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)
359 if (eval->quitflag)
362 if (!sed_canbe_finalized(eval->commands)) {
364 const char* error = sed_get_finalize_error(eval->commands, eval->pool);
366 eval_errf(eval, error);
371 eval->fout = fout;
374 if (bufsz && eval->lreadyflag) {
375 eval->lreadyflag = 0;
376 eval->lspend--;
377 *eval->lspend = '\0';
378 rv = execute(eval);
394 eval->lreadyflag = 1;
398 appendmem_to_linebuf(eval, buf, llen + 1);
399 --eval->lspend;
401 *eval->lspend = '\0';
404 rv = execute(eval);
407 if (eval->quitflag)
413 appendmem_to_linebuf(eval, buf, bufsz);
422 apr_status_t sed_finalize_eval(sed_eval_t *eval, void *fout)
424 if (eval->quitflag)
427 if (eval->finalflag)
428 eval->dolflag = 1;
430 eval->fout = fout;
433 if (eval->lspend > eval->linebuf) {
436 if (eval->lreadyflag) {
437 eval->lreadyflag = 0;
438 eval->lspend--;
444 append_to_linebuf(eval, "");
447 *eval->lspend = '\0';
448 rv = execute(eval);
453 eval->quitflag = 1;
461 static apr_status_t execute(sed_eval_t *eval)
463 sed_reptr_t *ipc = eval->commands->ptrspace;
467 eval->lnum++;
469 eval->sflag = 0;
471 if (eval->pending) {
472 ipc = eval->pending;
473 eval->pending = NULL;
488 if (eval->inar[ipc->nrep]) {
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;
507 if (!eval->dolflag) {
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);
544 if (eval->quitflag)
547 if (eval->pending)
550 if (eval->delflag)
553 if (eval->jflag) {
554 eval->jflag = 0;
556 ipc = eval->commands->ptrspace;
563 if (!eval->commands->nflag && !eval->delflag) {
564 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
569 if (eval->aptr > eval->abuf)
570 rv = arout(eval);
572 eval->delflag = 0;
574 eval->lspend = eval->linebuf;
582 static int match(sed_eval_t *eval, char *expbuf, int gf,
592 p1 = eval->linebuf;
603 static int substitute(sed_eval_t *eval, sed_reptr_t *ipc,
606 if(match(eval, ipc->re1, 0, step_vars) == 0) return(0);
608 eval->numpass = 0;
609 eval->sflag = 0; /* Flags if any substitution was made */
610 if (dosub(eval, ipc->rhs, ipc->gfl, step_vars) != APR_SUCCESS)
615 if(match(eval, ipc->re1, 1, step_vars) == 0) break;
616 if (dosub(eval, ipc->rhs, ipc->gfl, step_vars) != APR_SUCCESS)
620 return(eval->sflag);
626 static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
634 eval->numpass++;
635 if(n != eval->numpass) return APR_SUCCESS;
637 eval->sflag = 1;
638 lp = eval->linebuf;
639 sp = eval->genbuf;
641 sp = place(eval, sp, lp, step_vars->loc1);
644 sp = place(eval, sp, step_vars->loc1, step_vars->loc2);
651 sp = place(eval, sp, step_vars->braslist[c-'1'],
660 if (sp >= eval->genbuf + eval->gsize) {
662 grow_gen_buffer(eval, eval->gsize + 1024, &sp);
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,
704 if(eval->aptr >= &eval->abuf[SED_ABUFSIZE]) {
705 eval_errf(eval, SEDERR_TMAMES, eval->lnum);
707 *eval->aptr++ = ipc;
708 *eval->aptr = NULL;
713 eval->delflag = 1;
714 if(!eval->inar[ipc->nrep] || eval->dolflag) {
717 rv = wline(eval, ipc->re1, p1 - ipc->re1);
721 eval->delflag++;
724 p1 = eval->linebuf;
728 eval->delflag++;
734 copy_to_linebuf(eval, p1);
735 eval->jflag++;
739 length = apr_snprintf(sz, sizeof(sz), "%d", (int) eval->lnum);
740 rv = wline(eval, sz, length);
744 copy_to_linebuf(eval, eval->holdbuf);
748 append_to_linebuf(eval, "\n");
749 append_to_linebuf(eval, eval->holdbuf);
753 copy_to_holdbuf(eval, eval->linebuf);
757 append_to_holdbuf(eval, "\n");
758 append_to_holdbuf(eval, eval->linebuf);
763 rv = wline(eval, ipc->re1, p1 - ipc->re1);
767 eval->jflag = 1;
772 p1 = eval->linebuf;
773 p2 = eval->genbuf;
774 eval->genbuf[72] = 0;
780 if(p2 >= eval->lcomend) {
782 rv = wline(eval, eval->genbuf,
783 strlen(eval->genbuf));
786 p2 = eval->genbuf;
794 if(p2 >= eval->lcomend) {
796 rv = wline(eval, eval->genbuf,
797 strlen(eval->genbuf));
800 p2 = eval->genbuf;
803 if(p2 >= eval->lcomend) {
805 rv = wline(eval, eval->genbuf,
806 strlen(eval->genbuf));
809 p2 = eval->genbuf;
812 if(p2 >= eval->lcomend) {
814 rv = wline(eval, eval->genbuf,
815 strlen(eval->genbuf));
818 p2 = eval->genbuf;
821 if(p2 >= eval->lcomend) {
823 rv = wline(eval, eval->genbuf,
824 strlen(eval->genbuf));
827 p2 = eval->genbuf;
831 if(p2 >= eval->lcomend) {
833 rv = wline(eval, eval->genbuf,
834 strlen(eval->genbuf));
837 p2 = eval->genbuf;
843 if(p2 >= eval->lcomend) {
845 rv = wline(eval, eval->genbuf,
846 strlen(eval->genbuf));
849 p2 = eval->genbuf;
855 rv = wline(eval, eval->genbuf, strlen(eval->genbuf));
859 if(!eval->commands->nflag) {
860 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
865 if(eval->aptr > eval->abuf) {
866 rv = arout(eval);
870 eval->lspend = eval->linebuf;
871 eval->pending = ipc->next;
875 if(eval->aptr > eval->abuf) {
876 rv = arout(eval);
880 append_to_linebuf(eval, "\n");
881 eval->pending = ipc->next;
885 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
888 for (p1 = eval->linebuf; *p1 != '\n' && *p1 != '\0'; p1++);
889 rv = wline(eval, eval->linebuf, p1 - eval->linebuf);
893 if (!eval->commands->nflag) {
894 rv = wline(eval, eval->linebuf, eval->lspend - eval->linebuf);
899 if(eval->aptr > eval->abuf) {
900 rv = arout(eval);
905 eval->quitflag = 1;
908 if(eval->aptr >= &eval->abuf[SED_ABUFSIZE]) {
909 eval_errf(eval, SEDERR_TMRMES, eval->lnum);
911 *eval->aptr++ = ipc;
912 *eval->aptr = NULL;
917 i = substitute(eval, ipc, step_vars);
921 if(ipc->pfl && eval->commands->nflag && i) {
923 rv = wline(eval, eval->linebuf, eval->lspend -
924 eval->linebuf);
928 for (p1 = eval->linebuf; *p1 != '\n' && *p1 != '\0'; p1++);
929 rv = wline(eval, eval->linebuf, p1 - eval->linebuf);
934 if (i && (ipc->findex >= 0) && eval->fcode[ipc->findex])
935 apr_file_printf(eval->fcode[ipc->findex], "%s\n",
936 eval->linebuf);
940 if(eval->sflag == 0) break;
941 eval->sflag = 0;
942 eval->jflag = 1;
947 apr_file_printf(eval->fcode[ipc->findex], "%s\n",
948 eval->linebuf);
951 copy_to_genbuf(eval, eval->linebuf);
952 copy_to_linebuf(eval, eval->holdbuf);
953 copy_to_holdbuf(eval, eval->genbuf);
957 p1 = eval->linebuf;
968 static apr_status_t arout(sed_eval_t *eval)
971 eval->aptr = eval->abuf - 1;
972 while (*++eval->aptr) {
973 if ((*eval->aptr)->command == ACOM) {
976 for (p1 = (*eval->aptr)->re1; *p1; p1++);
977 rv = wline(eval, (*eval->aptr)->re1, p1 - (*eval->aptr)->re1);
985 if (apr_file_open(&fi, (*eval->aptr)->re1, APR_READ, 0, eval->pool)
991 rv = eval->writefn(eval->fout, buf, n);
1001 eval->aptr = eval->abuf;
1002 *eval->aptr = NULL;
1009 static apr_status_t wline(sed_eval_t *eval, char *buf, int sz)
1012 rv = eval->writefn(eval->fout, buf, sz);
1015 rv = eval->writefn(eval->fout, "\n", 1);