/**********************************************************/ /* apimagic: cparser-based API normalization utility */ /* Copyright (C) 2015--2016 Z. Gilboa */ /* Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */ /**********************************************************/ #include #include #include #include #include #include #include #include #include "apimagic_driver_impl.h" static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int status) { if (ctx) { amgc_unmap_input(&ctx->map); free(ctx); } return status; } static FILE * amgc_stdin_to_tmp(const struct amgc_driver_ctx * dctx) { struct amgc_driver_ctx_impl * ictx; uintptr_t addr; int fdtmp; FILE * ftmp; char buf[4096]; ssize_t nread; int ret; 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 (!(ftmp = fdopen(fdtmp,"r"))) close(fdtmp); return ftmp; } if (!(ftmp = tmpfile())) return 0; if ((ictx->fdtmpin = dup(fileno(ftmp))) < 0) { fclose(ftmp); return 0; } nread = read(0,buf,sizeof(buf)-1); while (nread) { if (nread > 0) { buf[nread] = '\0'; ret = fputs(buf,ftmp); } else ret = (errno == EINTR) ? 0 : -1; if (ret < 0) { fclose(ftmp); return 0; } nread = read(0,buf,sizeof(buf)-1); } return ftmp; } int amgc_get_unit_ctx( const struct amgc_driver_ctx * dctx, const char * path, struct amgc_unit_ctx ** pctx) { struct amgc_unit_ctx_impl * ctx; FILE * ftmp; int fd; if (!dctx || !(ctx = calloc(sizeof(*ctx),1))) return -1; if (strcmp(path,"-")) fd = -1; else if (!(ftmp = amgc_stdin_to_tmp(dctx))) return amgc_free_unit_ctx_impl(ctx,-1); else if ((fd = dup(fileno(ftmp))) < 0) return amgc_free_unit_ctx_impl(ctx,-1); else fclose(ftmp); if (amgc_map_input(fd,path,PROT_READ,&ctx->map)) return amgc_free_unit_ctx_impl(ctx,-1); if (fd > 0) close(fd); memcpy(&ctx->cctx,dctx->cctx, sizeof(ctx->cctx)); ctx->path = path; ctx->uctx.path = &ctx->path; ctx->uctx.map = &ctx->map; ctx->uctx.cctx = &ctx->cctx; *pctx = &ctx->uctx; return 0; } void amgc_free_unit_ctx(struct amgc_unit_ctx * ctx) { struct amgc_unit_ctx_impl * ictx; uintptr_t addr; if (ctx) { addr = (uintptr_t)ctx - offsetof(struct amgc_unit_ctx_impl,uctx); ictx = (struct amgc_unit_ctx_impl *)addr; amgc_free_unit_ctx_impl(ictx,0); } }