|
|
0efa8c |
/*********************************************************/
|
|
|
0efa8c |
/* ptycon: a pty-console bridge */
|
|
|
0efa8c |
/* Copyright (C) 2016 Z. Gilboa */
|
|
|
0efa8c |
/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */
|
|
|
0efa8c |
/*********************************************************/
|
|
|
0efa8c |
|
|
|
0efa8c |
#include <ntapi/ntapi.h>
|
|
|
0efa8c |
#include <stdint.h>
|
|
|
0efa8c |
|
|
|
0efa8c |
#include <ptycon/ptycon.h>
|
|
|
0efa8c |
#include "ptycon_init_impl.h"
|
|
|
0efa8c |
#include "ptycon_nolibc_impl.h"
|
|
|
0efa8c |
|
|
|
0efa8c |
#define ARGV_DRIVER
|
|
|
0efa8c |
|
|
|
0efa8c |
#include "ptycon_version.h"
|
|
|
0efa8c |
#include "ptycon_driver_impl.h"
|
|
|
0efa8c |
#include "argv/argv.h"
|
|
|
0efa8c |
|
|
|
0efa8c |
/* ntapi accessor table */
|
|
|
0efa8c |
const ntapi_vtbl * ptyc_ntapi;
|
|
|
0efa8c |
|
|
|
0efa8c |
/* package info */
|
|
|
0efa8c |
static const struct ptyc_source_version ptyc_src_version = {
|
|
|
0efa8c |
PTYC_TAG_VER_MAJOR,
|
|
|
0efa8c |
PTYC_TAG_VER_MINOR,
|
|
|
0efa8c |
PTYC_TAG_VER_PATCH,
|
|
|
0efa8c |
PTYCON_GIT_VERSION
|
|
|
0efa8c |
};
|
|
|
0efa8c |
|
|
|
0efa8c |
struct ptyc_driver_ctx_alloc {
|
|
|
0efa8c |
struct argv_meta * meta;
|
|
|
0efa8c |
struct ptyc_driver_ctx_impl ctx;
|
|
|
0efa8c |
uint64_t guard;
|
|
|
0efa8c |
const char * units[];
|
|
|
0efa8c |
};
|
|
|
0efa8c |
|
|
|
0efa8c |
static uint32_t ptyc_argv_flags(uint32_t flags)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
uint32_t ret = 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (flags & PTYC_DRIVER_VERBOSITY_NONE)
|
|
|
0efa8c |
ret |= ARGV_VERBOSITY_NONE;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (flags & PTYC_DRIVER_VERBOSITY_ERRORS)
|
|
|
0efa8c |
ret |= ARGV_VERBOSITY_ERRORS;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (flags & PTYC_DRIVER_VERBOSITY_STATUS)
|
|
|
0efa8c |
ret |= ARGV_VERBOSITY_STATUS;
|
|
|
0efa8c |
|
|
|
0efa8c |
return ret;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
static int ptyc_driver_usage(
|
|
|
0efa8c |
const char * program,
|
|
|
0efa8c |
const char * arg,
|
|
|
0efa8c |
const struct argv_option * options,
|
|
|
0efa8c |
struct argv_meta * meta)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
char header[512];
|
|
|
0efa8c |
|
|
|
0efa8c |
snprintf(header,sizeof(header),
|
|
|
0efa8c |
"Usage: %s [options] <file>...\n" "Options:\n",
|
|
|
0efa8c |
program);
|
|
|
0efa8c |
|
|
|
0efa8c |
argv_usage(stdout,header,options,arg);
|
|
|
0efa8c |
argv_free(meta);
|
|
|
0efa8c |
|
|
|
0efa8c |
return PTYC_USAGE;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
static struct ptyc_driver_ctx_impl * ptyc_driver_ctx_alloc(
|
|
|
0efa8c |
struct argv_meta * meta,
|
|
|
0efa8c |
const struct ptyc_common_ctx * cctx,
|
|
|
0efa8c |
size_t nunits)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct ptyc_driver_ctx_alloc * ictx;
|
|
|
0efa8c |
size_t size;
|
|
|
0efa8c |
struct argv_entry * entry;
|
|
|
0efa8c |
const char ** units;
|
|
|
0efa8c |
|
|
|
0efa8c |
size = sizeof(struct ptyc_driver_ctx_alloc);
|
|
|
0efa8c |
size += (nunits+1)*sizeof(const char *);
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!(ictx = calloc(1,size)))
|
|
|
0efa8c |
return 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (cctx)
|
|
|
0efa8c |
memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
|
|
|
0efa8c |
|
|
|
0efa8c |
for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
|
|
|
0efa8c |
if (!entry->fopt)
|
|
|
0efa8c |
*units++ = entry->arg;
|
|
|
0efa8c |
|
|
|
0efa8c |
ictx->meta = meta;
|
|
|
0efa8c |
ictx->ctx.ctx.units = ictx->units;
|
|
|
0efa8c |
return &ictx->ctx;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
static int ptyc_get_driver_ctx_fail(struct argv_meta * meta)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
argv_free(meta);
|
|
|
0efa8c |
return -1;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
int ptyc_get_driver_ctx(
|
|
|
0efa8c |
char ** argv,
|
|
|
0efa8c |
char ** envp,
|
|
|
0efa8c |
uint32_t flags,
|
|
|
0efa8c |
struct ptyc_driver_ctx ** pctx)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct ptyc_driver_ctx_impl * ctx;
|
|
|
0efa8c |
struct ptyc_common_ctx cctx;
|
|
|
0efa8c |
const struct argv_option * options;
|
|
|
0efa8c |
struct argv_meta * meta;
|
|
|
0efa8c |
struct argv_entry * entry;
|
|
|
0efa8c |
size_t nunits;
|
|
|
0efa8c |
const char * program;
|
|
|
0efa8c |
|
|
|
0efa8c |
(void)envp;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (ptyc_init())
|
|
|
0efa8c |
return -1;
|
|
|
0efa8c |
|
|
|
0efa8c |
options = ptyc_default_options;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!(meta = argv_get(argv,options,ptyc_argv_flags(flags))))
|
|
|
0efa8c |
return -1;
|
|
|
0efa8c |
|
|
|
0efa8c |
nunits = 0;
|
|
|
0efa8c |
program = argv_program_name(argv[0]);
|
|
|
0efa8c |
memset(&cctx,0,sizeof(cctx));
|
|
|
0efa8c |
cctx.drvflags = flags;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!argv[1] && (flags & PTYC_DRIVER_VERBOSITY_USAGE))
|
|
|
0efa8c |
return ptyc_driver_usage(program,0,options,meta);
|
|
|
0efa8c |
|
|
|
0efa8c |
/* get options, count units */
|
|
|
0efa8c |
for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
|
|
|
0efa8c |
if (entry->fopt) {
|
|
|
0efa8c |
switch (entry->tag) {
|
|
|
0efa8c |
case TAG_HELP:
|
|
|
0efa8c |
if (flags & PTYC_DRIVER_VERBOSITY_USAGE)
|
|
|
0efa8c |
return ptyc_driver_usage(program,entry->arg,options,meta);
|
|
|
0efa8c |
|
|
|
0efa8c |
case TAG_VERSION:
|
|
|
0efa8c |
cctx.drvflags |= PTYC_DRIVER_VERSION;
|
|
|
0efa8c |
break;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
} else
|
|
|
0efa8c |
nunits++;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!(ctx = ptyc_driver_ctx_alloc(meta,&cctx,nunits)))
|
|
|
0efa8c |
return ptyc_get_driver_ctx_fail(meta);
|
|
|
0efa8c |
|
|
|
0efa8c |
ctx->ctx.program = program;
|
|
|
0efa8c |
ctx->ctx.cctx = &ctx->cctx;
|
|
|
0efa8c |
|
|
|
0efa8c |
*pctx = &ctx->ctx;
|
|
|
0efa8c |
return PTYC_OK;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
int ptyc_create_driver_ctx(
|
|
|
0efa8c |
const struct ptyc_common_ctx * cctx,
|
|
|
0efa8c |
struct ptyc_driver_ctx ** pctx)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct argv_meta * meta;
|
|
|
0efa8c |
struct ptyc_driver_ctx_impl * ctx;
|
|
|
0efa8c |
char * argv[] = {"ptycon_driver",0};
|
|
|
0efa8c |
|
|
|
0efa8c |
if (ptyc_init())
|
|
|
0efa8c |
return -1;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!(meta = argv_get(argv,ptyc_default_options,0)))
|
|
|
0efa8c |
return -1;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!(ctx = ptyc_driver_ctx_alloc(meta,cctx,0)))
|
|
|
0efa8c |
return ptyc_get_driver_ctx_fail(0);
|
|
|
0efa8c |
|
|
|
0efa8c |
ctx->ctx.cctx = &ctx->cctx;
|
|
|
0efa8c |
memcpy(&ctx->cctx,cctx,sizeof(*cctx));
|
|
|
0efa8c |
*pctx = &ctx->ctx;
|
|
|
0efa8c |
return PTYC_OK;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
static void ptyc_free_driver_ctx_impl(struct ptyc_driver_ctx_alloc * ictx)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
argv_free(ictx->meta);
|
|
|
0efa8c |
free(ictx);
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
void ptyc_free_driver_ctx(struct ptyc_driver_ctx * ctx)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct ptyc_driver_ctx_alloc * ictx;
|
|
|
0efa8c |
uintptr_t addr;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (ctx) {
|
|
|
0efa8c |
addr = (uintptr_t)ctx - offsetof(struct ptyc_driver_ctx_alloc,ctx);
|
|
|
0efa8c |
addr = addr - offsetof(struct ptyc_driver_ctx_impl,ctx);
|
|
|
0efa8c |
ictx = (struct ptyc_driver_ctx_alloc *)addr;
|
|
|
0efa8c |
ptyc_free_driver_ctx_impl(ictx);
|
|
|
0efa8c |
}
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
const struct ptyc_source_version * ptyc_source_version(void)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
return &ptyc_src_version;
|
|
|
0efa8c |
}
|