diff options
author | midipix <writeonce@midipix.org> | 2018-08-05 01:28:31 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-08-05 03:22:21 -0400 |
commit | 27c84717bf617575785dd2dddf4e96f15a5aa1b9 (patch) | |
tree | ea38d3d2de4a4275a518affa7933d400d0088715 | |
parent | 5eaec44974e5414dafcda39c005adcbb6bd752f3 (diff) | |
download | apimagic-27c84717bf617575785dd2dddf4e96f15a5aa1b9.tar.bz2 apimagic-27c84717bf617575785dd2dddf4e96f15a5aa1b9.tar.xz |
driver: amgc_stdin_to_tmp(): re-implemented with pure fdio.
-rw-r--r-- | src/driver/amgc_unit_ctx.c | 92 |
1 files changed, 44 insertions, 48 deletions
diff --git a/src/driver/amgc_unit_ctx.c b/src/driver/amgc_unit_ctx.c index 17568d2..e2d6d0a 100644 --- a/src/driver/amgc_unit_ctx.c +++ b/src/driver/amgc_unit_ctx.c @@ -27,68 +27,72 @@ static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int ret) if (ctx->entities) amgc_free_unit_entities(ctx->entities); - amgc_unmap_input(&ctx->map); free(ctx); } return ret; } -static FILE * amgc_stdin_to_tmp(const struct amgc_driver_ctx * dctx) +static int amgc_stdin_to_tmp(const struct amgc_driver_ctx * dctx) { struct amgc_driver_ctx_impl * ictx; uintptr_t addr; int fdtmp; - FILE * ftmp; + ssize_t ret; + ssize_t cnt; + char * ch; char buf[4096]; - ssize_t nread; - int ret; + char template[] = "/tmp/amgc_stdin_to_tmp_XXXXXX"; addr = (uintptr_t)dctx - offsetof(struct amgc_driver_ctx_impl,ctx); ictx = (struct amgc_driver_ctx_impl *)addr; - if (ictx->fdtmpin >= 0) { - if ((fdtmp = dup(ictx->fdtmpin)) < 0) - return 0; + if (ictx->fdtmpin >= 0) + return dup(ictx->fdtmpin); - if (!(ftmp = fdopen(fdtmp,"r"))) - close(fdtmp); + if ((fdtmp = mkstemp(template)) < 0) + return -1; - return ftmp; + if ((ictx->fdtmpin = dup(fdtmp)) < 0) { + close(fdtmp); + return -1; } - if (!(ftmp = tmpfile())) - return 0; + for (;;) { + ret = read(0,buf,sizeof(buf)-1); - if ((ictx->fdtmpin = dup(fileno(ftmp))) < 0) { - fclose(ftmp); - return 0; - } + while ((ret < 0) && (errno == EINTR)) + ret = read(0,buf,sizeof(buf)-1); - nread = read(0,buf,sizeof(buf)-1); + if (ret < 0) { + close(fdtmp); + return -1; - while (nread) { - if (nread > 0) { - buf[nread] = '\0'; - ret = fputs(buf,ftmp); - } else - ret = (errno == EINTR) ? 0 : -1; + } else if (ret == 0) { + return fdtmp; - if (ret < 0) { - fclose(ftmp); - return 0; - } + } else { + ch = buf; + cnt = ret; + ret = 0; - nread = read(0,buf,sizeof(buf)-1); - } + for (; cnt; ) { + ret = write(fdtmp,ch,cnt); - if (fflush(ftmp)) { - fclose(ftmp); - return 0; - } + while ((ret < 0) && (errno == EINTR)) + ret = write(fdtmp,ch,cnt); - return ftmp; + if (ret < 0) { + close(fdtmp); + return -1; + } + + ch += ret; + cnt -= ret; + } + } + } } static bool amgc_cparser_no_op( @@ -122,7 +126,6 @@ int amgc_get_unit_ctx( struct amgc_unit_ctx ** pctx) { struct amgc_unit_ctx_impl * ctx; - FILE * ftmp; int fd; amgc_init_cparser_unit(); @@ -140,23 +143,17 @@ int amgc_get_unit_ctx( if (strcmp(path,"-")) fd = -1; - else if (!(ftmp = amgc_stdin_to_tmp(dctx))) - return amgc_free_unit_ctx_impl( - ctx,AMGC_SYSTEM_ERROR(dctx)); - - else if ((fd = dup(fileno(ftmp))) < 0) + else if ((fd = amgc_stdin_to_tmp(dctx)) < 0) return amgc_free_unit_ctx_impl( ctx,AMGC_SYSTEM_ERROR(dctx)); - else - ctx->ccunit.input = ftmp; - - if (amgc_map_input(dctx,fd,path,PROT_READ,&ctx->map)) + else if (lseek(fd,0,SEEK_SET < 0)) return amgc_free_unit_ctx_impl( ctx,AMGC_NESTED_ERROR(dctx)); - if (fd > 0) - close(fd); + else if (!(ctx->ccunit.input = fdopen(fd,"r"))) + return amgc_free_unit_ctx_impl( + ctx,AMGC_FILE_ERROR(dctx)); /* compilation unit */ ctx->ccunit.name = path; @@ -178,7 +175,6 @@ int amgc_get_unit_ctx( ctx->dctx = dctx; ctx->path = path; ctx->uctx.path = &ctx->path; - ctx->uctx.map = &ctx->map; ctx->uctx.cctx = &ctx->cctx; ctx->uctx.meta = &ctx->meta; ctx->uctx.ccunit = &ctx->ccunit; |