summaryrefslogtreecommitdiffhomepage
path: root/src/driver
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-12-20 20:53:41 +0000
committermidipix <writeonce@midipix.org>2016-12-27 04:40:56 -0500
commit26e14f4143ec690a120e197a3adf547145e4ce59 (patch)
treee9e8b522de5abb14230c5f8e0825452f0c7ccb4b /src/driver
parent70c49fc4ed3b2c3db68537f828c286241eeeee98 (diff)
downloadntux-26e14f4143ec690a120e197a3adf547145e4ce59.tar.bz2
ntux-26e14f4143ec690a120e197a3adf547145e4ce59.tar.xz
create free-standing, libpsxscl-based project skeleton.
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/ntux_amain.c71
-rw-r--r--src/driver/ntux_driver_ctx.c227
2 files changed, 298 insertions, 0 deletions
diff --git a/src/driver/ntux_amain.c b/src/driver/ntux_amain.c
new file mode 100644
index 0000000..709c73b
--- /dev/null
+++ b/src/driver/ntux_amain.c
@@ -0,0 +1,71 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <ntux/ntux.h>
+#include "ntux_driver_impl.h"
+#include "ntux_nolibc_impl.h"
+
+#ifndef NTUX_DRIVER_FLAGS
+#define NTUX_DRIVER_FLAGS NTUX_DRIVER_VERBOSITY_ERRORS \
+ | NTUX_DRIVER_VERBOSITY_USAGE
+#endif
+
+static const char vermsg[] = "%s%s%s (git://midipix.org/ntux): "
+ "version %s%d.%d.%d%s.\n"
+ "[commit reference: %s%s%s]\n";
+
+static const char * const ntux_ver_color[6] = {
+ "\x1b[1m\x1b[35m","\x1b[0m",
+ "\x1b[1m\x1b[32m","\x1b[0m",
+ "\x1b[1m\x1b[34m","\x1b[0m"
+};
+
+static const char * const ntux_ver_plain[6] = {
+ "","",
+ "","",
+ "",""
+};
+
+static ssize_t ntux_version(struct ntux_driver_ctx * dctx)
+{
+ const struct ntux_source_version * verinfo;
+ const char * const * verclr;
+
+ verinfo = ntux_source_version();
+ verclr = isatty(STDOUT_FILENO) ? ntux_ver_color : ntux_ver_plain;
+
+ return fprintf(stdout,vermsg,
+ verclr[0],dctx->program,verclr[1],
+ verclr[2],verinfo->major,verinfo->minor,
+ verinfo->revision,verclr[3],
+ verclr[4],verinfo->commit,verclr[5]);
+}
+
+static int ntux_exit(struct ntux_driver_ctx * dctx, int ret)
+{
+ ntux_free_driver_ctx(dctx);
+ return ret;
+}
+
+int ntux_main(int argc, char ** argv, char ** envp)
+{
+ int ret;
+ struct ntux_driver_ctx * dctx;
+
+ if ((ret = ntux_get_driver_ctx(argv,envp,NTUX_DRIVER_FLAGS,&dctx)))
+ return (ret == NTUX_USAGE)
+ ? !--argc
+ : NTUX_ERROR;
+
+ if (dctx->cctx->drvflags & NTUX_DRIVER_VERSION)
+ if ((ntux_version(dctx)) < 0)
+ return ntux_exit(dctx,NTUX_ERROR);
+
+ return ntux_exit(dctx,dctx->errv[0] ? NTUX_ERROR : NTUX_OK);
+}
diff --git a/src/driver/ntux_driver_ctx.c b/src/driver/ntux_driver_ctx.c
new file mode 100644
index 0000000..2cd3cdf
--- /dev/null
+++ b/src/driver/ntux_driver_ctx.c
@@ -0,0 +1,227 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <ntapi/ntapi.h>
+#include <psxscl/psxscl.h>
+#include <stdint.h>
+
+#include <ntux/ntux.h>
+#include "ntux_init_impl.h"
+#include "ntux_nolibc_impl.h"
+
+#define ARGV_DRIVER
+
+#include "ntux_version.h"
+#include "ntux_driver_impl.h"
+#include "argv/argv.h"
+
+/* framework integration */
+#include <psxtypes/section/midipix.h>
+
+__attr_section_decl__(".midipix")
+static const nt_tty_affiliation tty_affiliation
+ __attr_section__(".midipix")
+ = NT_TTY_AFFILIATION_DEFAULT;
+
+/* ntapi accessor table */
+const ntapi_vtbl * ntux_ntapi;
+
+/* package info */
+static const struct ntux_source_version ntux_src_version = {
+ NTUX_TAG_VER_MAJOR,
+ NTUX_TAG_VER_MINOR,
+ NTUX_TAG_VER_PATCH,
+ NTUX_GIT_VERSION
+};
+
+struct ntux_driver_ctx_alloc {
+ struct argv_meta * meta;
+ struct ntux_driver_ctx_impl ctx;
+ uint64_t guard;
+ const char * units[];
+};
+
+static uint32_t ntux_argv_flags(uint32_t flags)
+{
+ uint32_t ret = 0;
+
+ if (flags & NTUX_DRIVER_VERBOSITY_NONE)
+ ret |= ARGV_VERBOSITY_NONE;
+
+ if (flags & NTUX_DRIVER_VERBOSITY_ERRORS)
+ ret |= ARGV_VERBOSITY_ERRORS;
+
+ if (flags & NTUX_DRIVER_VERBOSITY_STATUS)
+ ret |= ARGV_VERBOSITY_STATUS;
+
+ return ret;
+}
+
+static int ntux_driver_usage(
+ const char * program,
+ const char * arg,
+ const struct argv_option * options,
+ struct argv_meta * meta)
+{
+ char header[512];
+
+ snprintf(header,sizeof(header),
+ "Usage: %s [options] <file>...\n" "Options:\n",
+ program);
+
+ argv_usage(stdout,header,options,arg);
+ argv_free(meta);
+
+ return NTUX_USAGE;
+}
+
+static struct ntux_driver_ctx_impl * ntux_driver_ctx_alloc(
+ struct argv_meta * meta,
+ const struct ntux_common_ctx * cctx,
+ size_t nunits)
+{
+ struct ntux_driver_ctx_alloc * ictx;
+ size_t size;
+ struct argv_entry * entry;
+ const char ** units;
+ int elements;
+
+ (void)tty_affiliation;
+
+ size = sizeof(struct ntux_driver_ctx_alloc);
+ size += (nunits+1)*sizeof(const char *);
+
+ if (!(ictx = calloc(1,size)))
+ return 0;
+
+ elements = sizeof(ictx->ctx.erribuf) / sizeof(*ictx->ctx.erribuf);
+
+ ictx->ctx.errinfp = &ictx->ctx.erriptr[0];
+ ictx->ctx.erricap = &ictx->ctx.erriptr[--elements];
+
+ if (cctx)
+ memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
+
+ for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
+ if (!entry->fopt)
+ *units++ = entry->arg;
+
+ ictx->meta = meta;
+ ictx->ctx.ctx.units = ictx->units;
+ ictx->ctx.ctx.errv = ictx->ctx.errinfp;
+ return &ictx->ctx;
+}
+
+static int ntux_get_driver_ctx_fail(struct argv_meta * meta)
+{
+ argv_free(meta);
+ return -1;
+}
+
+int ntux_get_driver_ctx(
+ char ** argv,
+ char ** envp,
+ uint32_t flags,
+ struct ntux_driver_ctx ** pctx)
+{
+ struct ntux_driver_ctx_impl * ctx;
+ struct ntux_common_ctx cctx;
+ const struct argv_option * options;
+ struct argv_meta * meta;
+ struct argv_entry * entry;
+ size_t nunits;
+ const char * program;
+
+ (void)envp;
+
+ options = ntux_default_options;
+
+ if (ntux_init())
+ return -1;
+
+ if (!(meta = argv_get(argv,options,ntux_argv_flags(flags))))
+ return -1;
+
+ nunits = 0;
+ program = argv_program_name(argv[0]);
+ memset(&cctx,0,sizeof(cctx));
+ cctx.drvflags = flags;
+
+ if (!argv[1] && (flags & NTUX_DRIVER_VERBOSITY_USAGE))
+ return ntux_driver_usage(program,0,options,meta);
+
+ /* get options, count units */
+ for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
+ if (entry->fopt) {
+ switch (entry->tag) {
+ case TAG_HELP:
+ if (flags & NTUX_DRIVER_VERBOSITY_USAGE)
+ return ntux_driver_usage(program,entry->arg,options,meta);
+
+ case TAG_VERSION:
+ cctx.drvflags |= NTUX_DRIVER_VERSION;
+ break;
+ }
+ } else
+ nunits++;
+ }
+
+ if (!(ctx = ntux_driver_ctx_alloc(meta,&cctx,nunits)))
+ return ntux_get_driver_ctx_fail(meta);
+
+ ctx->ctx.program = program;
+ ctx->ctx.cctx = &ctx->cctx;
+
+ *pctx = &ctx->ctx;
+ return NTUX_OK;
+}
+
+int ntux_create_driver_ctx(
+ const struct ntux_common_ctx * cctx,
+ struct ntux_driver_ctx ** pctx)
+{
+ struct argv_meta * meta;
+ struct ntux_driver_ctx_impl * ctx;
+ char * argv[] = {"ntux_driver",0};
+
+ if (ntux_init())
+ return -1;
+
+ if (!(meta = argv_get(argv,ntux_default_options,0)))
+ return -1;
+
+ if (!(ctx = ntux_driver_ctx_alloc(meta,cctx,0)))
+ return ntux_get_driver_ctx_fail(0);
+
+ ctx->ctx.cctx = &ctx->cctx;
+ memcpy(&ctx->cctx,cctx,sizeof(*cctx));
+ *pctx = &ctx->ctx;
+ return NTUX_OK;
+}
+
+static void ntux_free_driver_ctx_impl(struct ntux_driver_ctx_alloc * ictx)
+{
+ argv_free(ictx->meta);
+ free(ictx);
+}
+
+void ntux_free_driver_ctx(struct ntux_driver_ctx * ctx)
+{
+ struct ntux_driver_ctx_alloc * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct ntux_driver_ctx_impl,ctx);
+ addr = addr - offsetof(struct ntux_driver_ctx_alloc,ctx);
+ ictx = (struct ntux_driver_ctx_alloc *)addr;
+ ntux_free_driver_ctx_impl(ictx);
+ }
+}
+
+const struct ntux_source_version * ntux_source_version(void)
+{
+ return &ntux_src_version;
+}