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