summaryrefslogtreecommitdiffhomepage
path: root/src
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
parent70c49fc4ed3b2c3db68537f828c286241eeeee98 (diff)
downloadntux-26e14f4143ec690a120e197a3adf547145e4ce59.tar.bz2
ntux-26e14f4143ec690a120e197a3adf547145e4ce59.tar.xz
create free-standing, libpsxscl-based project skeleton.
Diffstat (limited to 'src')
-rw-r--r--src/driver/ntux_amain.c71
-rw-r--r--src/driver/ntux_driver_ctx.c227
-rw-r--r--src/internal/argv/argv.h909
-rw-r--r--src/internal/nolibc/nt32/ntux_compiler.asm20
-rw-r--r--src/internal/nolibc/nt32/ntux_compiler.s20
-rw-r--r--src/internal/nolibc/nt64/ntux_compiler.asm19
-rw-r--r--src/internal/nolibc/nt64/ntux_compiler.s20
-rw-r--r--src/internal/nolibc/stdbool.h11
-rw-r--r--src/internal/nolibc/stddef.h6
-rw-r--r--src/internal/nolibc/stdint.h6
-rw-r--r--src/internal/nolibc/stdio.h8
-rw-r--r--src/internal/nolibc/stdlib.h4
-rw-r--r--src/internal/nolibc/string.h4
-rw-r--r--src/internal/nolibc/unistd.h4
-rw-r--r--src/internal/ntux_driver_impl.h33
-rw-r--r--src/internal/ntux_init_impl.h25
-rw-r--r--src/internal/ntux_memfn_impl.c94
-rw-r--r--src/internal/ntux_memfn_impl.h22
-rw-r--r--src/internal/ntux_nolibc_impl.c73
-rw-r--r--src/internal/ntux_nolibc_impl.h52
-rw-r--r--src/internal/ntux_ntaio_impl.c114
-rw-r--r--src/ntux.c34
-rw-r--r--src/skin/ntux_skin_default.c12
23 files changed, 1788 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;
+}
diff --git a/src/internal/argv/argv.h b/src/internal/argv/argv.h
new file mode 100644
index 0000000..3ddc311
--- /dev/null
+++ b/src/internal/argv/argv.h
@@ -0,0 +1,909 @@
+/****************************************************************************/
+/* argv.h: a thread-safe argument vector parser and usage screen generator */
+/* Copyright (C) 2015--2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/* This file is (also) part of sofort: portable software project template. */
+/****************************************************************************/
+
+#ifndef ARGV_H
+#define ARGV_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#define ARGV_VERBOSITY_NONE 0x00
+#define ARGV_VERBOSITY_ERRORS 0x01
+#define ARGV_VERBOSITY_STATUS 0x02
+#define ARGV_CLONE_VECTOR 0x80
+
+#ifndef ARGV_TAB_WIDTH
+#define ARGV_TAB_WIDTH 8
+#endif
+
+/*******************************************/
+/* */
+/* support of hybrid options */
+/* ------------------------- */
+/* hybrid options are very similar to */
+/* long options, yet are prefixed by */
+/* a single dash rather than two */
+/* (i.e. -std, -isystem). */
+/* hybrid options are supported by this */
+/* driver for compatibility with legacy */
+/* tools; note, however, that the use */
+/* of hybrid options should be strongly */
+/* discouraged due to the limitations */
+/* they impose on short options (for */
+/* example, a driver implementing -std */
+/* may not provide -s as a short option */
+/* that takes an arbitrary value). */
+/* */
+/* SPACE: -hybrid VALUE (i.e. -MF file) */
+/* EQUAL: -hybrid=VALUE (i.e. -std=c99) */
+/* COMMA: -hybrid,VALUE (i.e. -Wl,<arg>) */
+/* ONLY: -opt accepted, --opt rejected */
+/* JOINED: -optVALUE */
+/* */
+/*******************************************/
+
+
+#define ARGV_OPTION_HYBRID_NONE 0x00
+#define ARGV_OPTION_HYBRID_ONLY 0x01
+#define ARGV_OPTION_HYBRID_SPACE 0x02
+#define ARGV_OPTION_HYBRID_EQUAL 0x04
+#define ARGV_OPTION_HYBRID_COMMA 0x08
+#define ARGV_OPTION_HYBRID_JOINED 0x10
+#define ARGV_OPTION_HYBRID_CIRCUS (ARGV_OPTION_HYBRID_SPACE \
+ | ARGV_OPTION_HYBRID_JOINED)
+#define ARGV_OPTION_HYBRID_DUAL (ARGV_OPTION_HYBRID_SPACE \
+ | ARGV_OPTION_HYBRID_EQUAL)
+#define ARGV_OPTION_HYBRID_SWITCH (ARGV_OPTION_HYBRID_ONLY \
+ | ARGV_OPTION_HYBRID_SPACE \
+ | ARGV_OPTION_HYBRID_EQUAL \
+ | ARGV_OPTION_HYBRID_COMMA \
+ | ARGV_OPTION_HYBRID_JOINED)
+
+enum argv_optarg {
+ ARGV_OPTARG_NONE,
+ ARGV_OPTARG_REQUIRED,
+ ARGV_OPTARG_OPTIONAL,
+};
+
+enum argv_mode {
+ ARGV_MODE_SCAN,
+ ARGV_MODE_COPY,
+};
+
+enum argv_error {
+ ARGV_ERROR_OK,
+ ARGV_ERROR_INTERNAL,
+ ARGV_ERROR_SHORT_OPTION,
+ ARGV_ERROR_LONG_OPTION,
+ ARGV_ERROR_OPTARG_NONE,
+ ARGV_ERROR_OPTARG_REQUIRED,
+ ARGV_ERROR_OPTARG_PARADIGM,
+ ARGV_ERROR_HYBRID_NONE,
+ ARGV_ERROR_HYBRID_ONLY,
+ ARGV_ERROR_HYBRID_SPACE,
+ ARGV_ERROR_HYBRID_EQUAL,
+ ARGV_ERROR_HYBRID_COMMA,
+};
+
+struct argv_option {
+ const char * long_name;
+ const char short_name;
+ int tag;
+ enum argv_optarg optarg;
+ int flags;
+ const char * paradigm;
+ const char * argname;
+ const char * description;
+};
+
+struct argv_entry {
+ const char * arg;
+ int tag;
+ bool fopt;
+ bool fval;
+ bool fnoscan;
+ enum argv_error errcode;
+};
+
+struct argv_meta {
+ char ** argv;
+ struct argv_entry * entries;
+};
+
+struct argv_meta_impl {
+ char ** argv;
+ char * strbuf;
+ struct argv_meta meta;
+};
+
+struct argv_ctx {
+ int flags;
+ int mode;
+ int nentries;
+ int unitidx;
+ int erridx;
+ enum argv_error errcode;
+ const char * errch;
+ const struct argv_option * erropt;
+ const char * program;
+};
+
+#ifdef ARGV_DRIVER
+
+static const char * argv_program_name(const char *);
+
+static void argv_usage(
+ FILE *,
+ const char * header,
+ const struct argv_option[],
+ const char * mode);
+
+static struct argv_meta * argv_get(
+ char **,
+ const struct argv_option[],
+ int flags);
+
+static void argv_free(struct argv_meta *);
+
+
+
+
+/*------------------------------------*/
+/* implementation of static functions */
+/*------------------------------------*/
+
+static const struct argv_option * argv_short_option(
+ const char * ch,
+ const struct argv_option options[],
+ struct argv_entry * entry)
+{
+ const struct argv_option * option;
+
+ for (option=options; option->long_name || option->short_name; option++) {
+ if (option->short_name == *ch) {
+ entry->tag = option->tag;
+ entry->fopt = true;
+ return option;
+ }
+ }
+
+ return 0;
+}
+
+static const struct argv_option * argv_long_option(
+ const char * ch,
+ const struct argv_option options[],
+ struct argv_entry * entry)
+{
+ const struct argv_option * option;
+ const char * arg;
+ size_t len;
+
+ for (option=options; option->long_name || option->short_name; option++) {
+ len = option->long_name ? strlen(option->long_name) : 0;
+
+ if (len && !(strncmp(option->long_name,ch,len))) {
+ arg = ch + len;
+
+ if (!*arg
+ || (*arg == '=')
+ || (option->flags & ARGV_OPTION_HYBRID_JOINED)
+ || ((option->flags & ARGV_OPTION_HYBRID_COMMA)
+ && (*arg == ','))) {
+ entry->tag = option->tag;
+ entry->fopt = true;
+ return option;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static inline bool is_short_option(const char * arg)
+{
+ return (arg[0]=='-') && arg[1] && (arg[1]!='-');
+}
+
+static inline bool is_long_option(const char * arg)
+{
+ return (arg[0]=='-') && (arg[1]=='-') && arg[2];
+}
+
+static inline bool is_last_option(const char * arg)
+{
+ return (arg[0]=='-') && (arg[1]=='-') && !arg[2];
+}
+
+static inline bool is_hybrid_option(
+ const char * arg,
+ const struct argv_option options[])
+{
+ const struct argv_option * option;
+ struct argv_entry entry;
+
+ if (!is_short_option(arg))
+ return false;
+
+ if (!(option = argv_long_option(++arg,options,&entry)))
+ return false;
+
+ if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
+ if (argv_short_option(arg,options,&entry))
+ return false;
+
+ return true;
+}
+
+static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm)
+{
+ size_t len;
+ const char * ch;
+
+ for (ch=paradigm,len=strlen(arg); ch; ) {
+ if (!strncmp(arg,ch,len)) {
+ if (!*(ch += len))
+ return true;
+ else if (*ch == '|')
+ return true;
+ }
+
+ if ((ch = strchr(ch,'|')))
+ ch++;
+ }
+
+ return false;
+}
+
+static inline const struct argv_option * option_from_tag(
+ const struct argv_option options[],
+ int tag)
+{
+ const struct argv_option * option;
+
+ for (option=options; option->short_name || option->long_name; option++)
+ if (option->tag == tag)
+ return option;
+ return 0;
+}
+
+static void argv_scan(
+ char ** argv,
+ const struct argv_option options[],
+ struct argv_ctx * ctx,
+ struct argv_meta * meta)
+{
+ char ** parg;
+ const char * ch;
+ const char * val;
+ const struct argv_option * option;
+ struct argv_entry entry;
+ struct argv_entry * mentry;
+ enum argv_error ferr;
+ bool fval;
+ bool fnext;
+ bool fshort;
+ bool fhybrid;
+ bool fnoscan;
+
+ parg = &argv[1];
+ ch = *parg;
+ ferr = ARGV_ERROR_OK;
+ fshort = false;
+ fnoscan = false;
+ fval = false;
+ mentry = meta ? meta->entries : 0;
+
+ ctx->unitidx = 0;
+ ctx->erridx = 0;
+
+ while (ch && (ferr == ARGV_ERROR_OK)) {
+ option = 0;
+ fhybrid = false;
+
+ if (fnoscan)
+ fval = true;
+
+ else if (is_last_option(ch))
+ fnoscan = true;
+
+ else if (!fshort && is_hybrid_option(ch,options))
+ fhybrid = true;
+
+ if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
+ if (!fshort)
+ ch++;
+
+ if ((option = argv_short_option(ch,options,&entry))) {
+ if (ch[1]) {
+ ch++;
+ fnext = false;
+ fshort = (option->optarg == ARGV_OPTARG_NONE);
+ } else {
+ parg++;
+ ch = *parg;
+ fnext = true;
+ fshort = false;
+ }
+
+ if (option->optarg == ARGV_OPTARG_NONE) {
+ if (!fnext && ch && (*ch == '-'))
+ ferr = ARGV_ERROR_OPTARG_NONE;
+ else
+ fval = false;
+ } else if (!fnext)
+ fval = true;
+ else if (option->optarg == ARGV_OPTARG_REQUIRED) {
+ if (ch && is_short_option(ch))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (ch && is_long_option(ch))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (ch && is_last_option(ch))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (ch)
+ fval = true;
+ else
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ } else {
+ /* ARGV_OPTARG_OPTIONAL */
+ if (ch && is_short_option(ch))
+ fval = false;
+ else if (ch && is_long_option(ch))
+ fval = false;
+ else if (ch && is_last_option(ch))
+ fval = false;
+ else
+ fval = ch;
+ }
+ } else
+ ferr = ARGV_ERROR_SHORT_OPTION;
+
+ } else if (!fnoscan && (fhybrid || is_long_option(ch))) {
+ ch += (fhybrid ? 1 : 2);
+
+ if ((option = argv_long_option(ch,options,&entry))) {
+ val = ch + strlen(option->long_name);
+
+ /* val[0] is either '=' (or ',') or '\0' */
+ if (!val[0]) {
+ parg++;
+ ch = *parg;
+ }
+
+ if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH))
+ ferr = ARGV_ERROR_HYBRID_NONE;
+ else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY))
+ ferr = ARGV_ERROR_HYBRID_ONLY;
+ else if (option->optarg == ARGV_OPTARG_NONE) {
+ if (val[0]) {
+ ferr = ARGV_ERROR_OPTARG_NONE;
+ ctx->errch = val + 1;
+ } else
+ fval = false;
+ } else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
+ fval = true;
+ ch = val;
+ } else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE))
+ ferr = ARGV_ERROR_HYBRID_SPACE;
+ else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL))
+ ferr = ARGV_ERROR_HYBRID_EQUAL;
+ else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA))
+ ferr = ARGV_ERROR_HYBRID_COMMA;
+ else if (!fhybrid && (val[0]==','))
+ ferr = ARGV_ERROR_HYBRID_COMMA;
+ else if (val[0] && !val[1])
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (val[0] && val[1]) {
+ fval = true;
+ ch = ++val;
+ } else if (option->optarg == ARGV_OPTARG_REQUIRED) {
+ if (!val[0] && !*parg)
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (*parg && is_short_option(*parg))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (*parg && is_long_option(*parg))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else if (*parg && is_last_option(*parg))
+ ferr = ARGV_ERROR_OPTARG_REQUIRED;
+ else
+ fval = true;
+ } else {
+ /* ARGV_OPTARG_OPTIONAL */
+ fval = val[0];
+ }
+ } else
+ ferr = ARGV_ERROR_LONG_OPTION;
+ }
+
+ if (ferr == ARGV_ERROR_OK)
+ if (option && fval && option->paradigm)
+ if (!is_arg_in_paradigm(ch,option->paradigm))
+ ferr = ARGV_ERROR_OPTARG_PARADIGM;
+
+ if (ferr == ARGV_ERROR_OK)
+ if (!option && !ctx->unitidx)
+ ctx->unitidx = parg - argv;
+
+ if (ferr != ARGV_ERROR_OK) {
+ ctx->errcode = ferr;
+ ctx->errch = ctx->errch ? ctx->errch : ch;
+ ctx->erropt = option;
+ ctx->erridx = parg - argv;
+ return;
+ } else if (ctx->mode == ARGV_MODE_SCAN) {
+ if (!fnoscan)
+ ctx->nentries++;
+ else if (fval)
+ ctx->nentries++;
+
+ if (fval || !option) {
+ parg++;
+ ch = *parg;
+ }
+ } else if (ctx->mode == ARGV_MODE_COPY) {
+ if (fnoscan) {
+ if (fval) {
+ mentry->arg = ch;
+ mentry->fnoscan = true;
+ mentry++;
+ }
+
+ parg++;
+ ch = *parg;
+ } else if (option) {
+ mentry->arg = fval ? ch : 0;
+ mentry->tag = option->tag;
+ mentry->fopt = true;
+ mentry->fval = fval;
+ mentry++;
+
+ if (fval) {
+ parg++;
+ ch = *parg;
+ }
+ } else {
+ mentry->arg = ch;
+ mentry++;
+ parg++;
+ ch = *parg;
+ }
+ }
+ }
+}
+
+static const char * argv_program_name(const char * program_path)
+{
+ const char * ch;
+
+ if (program_path) {
+ if ((ch = strrchr(program_path,'/')))
+ return *(++ch) ? ch : 0;
+
+ if ((ch = strrchr(program_path,'\\')))
+ return *(++ch) ? ch : 0;
+ }
+
+ return program_path;
+}
+
+static void argv_show_error(struct argv_ctx * ctx)
+{
+ char opt_short_name[2] = {0,0};
+
+ if (ctx->erropt && ctx->erropt->short_name)
+ opt_short_name[0] = ctx->erropt->short_name;
+
+ fprintf(stderr,"%s: error: ",ctx->program);
+
+ switch (ctx->errcode) {
+ case ARGV_ERROR_SHORT_OPTION:
+ fprintf(stderr,"'-%c' is not a valid short option\n",*ctx->errch);
+ break;
+
+ case ARGV_ERROR_LONG_OPTION:
+ fprintf(stderr,"'--%s' is not a valid long option\n",ctx->errch);
+ break;
+
+ case ARGV_ERROR_OPTARG_NONE:
+ fprintf(stderr,"'%s' is not a valid option value for [%s%s%s%s%s] "
+ "(option values may not be specified)\n",
+ ctx->errch,
+ opt_short_name[0] ? "-" : "",
+ opt_short_name,
+ opt_short_name[0] ? "," : "",
+ ctx->erropt->long_name ? "--" : "",
+ ctx->erropt->long_name);
+ break;
+
+ case ARGV_ERROR_OPTARG_REQUIRED:
+ fprintf(stderr,"option [%s%s%s%s%s] requires %s %s%s%s\n",
+ opt_short_name[0] ? "-" : "",
+ opt_short_name,
+ opt_short_name[0] ? "," : "",
+ ctx->erropt->long_name ? "--" : "",
+ ctx->erropt->long_name,
+ ctx->erropt->paradigm ? "one of the following values:" : "a value",
+ ctx->erropt->paradigm ? "{" : "",
+ ctx->erropt->paradigm ? ctx->erropt->paradigm : "",
+ ctx->erropt->paradigm ? "}" : "");
+ break;
+
+ case ARGV_ERROR_OPTARG_PARADIGM:
+ fprintf(stderr,"'%s' is not a valid option value for [%s%s%s%s%s]={%s}\n",
+ ctx->errch,
+ opt_short_name[0] ? "-" : "",
+ opt_short_name,
+ opt_short_name[0] ? "," : "",
+ ctx->erropt->long_name ? "--" : "",
+ ctx->erropt->long_name,
+ ctx->erropt->paradigm);
+ break;
+
+ case ARGV_ERROR_HYBRID_NONE:
+ fprintf(stderr,"-%s is not a synonym for --%s\n",
+ ctx->erropt->long_name,
+ ctx->erropt->long_name);
+ break;
+
+ case ARGV_ERROR_HYBRID_ONLY:
+ fprintf(stderr,"--%s is not a synonym for -%s\n",
+ ctx->erropt->long_name,
+ ctx->erropt->long_name);
+ break;
+
+ case ARGV_ERROR_HYBRID_SPACE:
+ case ARGV_ERROR_HYBRID_EQUAL:
+ case ARGV_ERROR_HYBRID_COMMA:
+ fprintf(stderr,"-%s: illegal value assignment; valid syntax is "
+ "-%s%sVAL\n",
+ ctx->erropt->long_name,
+ ctx->erropt->long_name,
+ (ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE)
+ ? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL)
+ ? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA)
+ ? "," : "");
+
+ break;
+
+ case ARGV_ERROR_INTERNAL:
+ fputs("internal error",stderr);
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void argv_show_status(
+ const struct argv_option options[],
+ struct argv_ctx * ctx,
+ struct argv_meta * meta)
+{
+ int argc;
+ char ** argv;
+ struct argv_entry * entry;
+ const struct argv_option * option;
+ char short_name[2] = {0};
+ const char * space = "";
+
+ (void)ctx;
+
+ fputs("\n\nconcatenated command line:\n",stderr);
+ for (argv=meta->argv; *argv; argv++) {
+ fprintf(stderr,"%s%s",space,*argv);
+ space = " ";
+ }
+
+ fputs("\n\nargument vector:\n",stderr);
+ for (argc=0,argv=meta->argv; *argv; argc++,argv++)
+ fprintf(stderr,"argv[%d]: %s\n",argc,*argv);
+
+ fputs("\n\nparsed entries:\n",stderr);
+ for (entry=meta->entries; entry->arg || entry->fopt; entry++)
+ if (entry->fopt) {
+ option = option_from_tag(options,entry->tag);
+ short_name[0] = option->short_name;
+
+ if (entry->fval)
+ fprintf(stderr,"[-%s,--%s] := %s\n",
+ short_name,option->long_name,entry->arg);
+ else
+ fprintf(stderr,"[-%s,--%s]\n",
+ short_name,option->long_name);
+ } else
+ fprintf(stderr,"<program arg> := %s\n",entry->arg);
+
+ fputs("\n\n",stderr);
+}
+
+static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
+{
+ if (imeta->argv)
+ free(imeta->argv);
+
+ if (imeta->strbuf)
+ free(imeta->strbuf);
+
+ if (imeta->meta.entries)
+ free(imeta->meta.entries);
+
+ free(imeta);
+ return 0;
+}
+
+static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx)
+{
+ struct argv_meta_impl * imeta;
+ char ** vector;
+ char * dst;
+ size_t size;
+ int argc;
+ int i;
+
+ if (!(imeta = calloc(1,sizeof(*imeta))))
+ return 0;
+
+ if (ctx->flags & ARGV_CLONE_VECTOR) {
+ for (vector=argv,argc=0,size=0; *vector; vector++) {
+ size += strlen(*vector) + 1;
+ argc++;
+ }
+
+ if (!(imeta->argv = calloc(argc+1,sizeof(char *))))
+ return argv_free_impl(imeta);
+ else if (!(imeta->strbuf = calloc(1,size+1)))
+ return argv_free_impl(imeta);
+
+ for (i=0,dst=imeta->strbuf; i<argc; i++) {
+ strcpy(dst,argv[i]);
+ imeta->argv[i] = dst;
+ dst += strlen(dst)+1;
+ }
+
+ imeta->meta.argv = imeta->argv;
+ } else
+ imeta->meta.argv = argv;
+
+ if (!(imeta->meta.entries = calloc(
+ ctx->nentries+1,
+ sizeof(struct argv_entry))))
+ return argv_free_impl(imeta);
+ else
+ return &imeta->meta;
+}
+
+static struct argv_meta * argv_get(
+ char * argv[],
+ const struct argv_option options[],
+ int flags)
+{
+ struct argv_meta * meta;
+ struct argv_ctx ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0};
+
+ argv_scan(argv,options,&ctx,0);
+
+ if (ctx.errcode != ARGV_ERROR_OK) {
+ ctx.program = argv_program_name(argv[0]);
+
+ if (ctx.flags & ARGV_VERBOSITY_ERRORS)
+ argv_show_error(&ctx);
+
+ return 0;
+ }
+
+ if (!(meta = argv_alloc(argv,&ctx)))
+ return 0;
+
+ ctx.mode = ARGV_MODE_COPY;
+ argv_scan(meta->argv,options,&ctx,meta);
+
+ if (ctx.errcode != ARGV_ERROR_OK) {
+ ctx.program = argv[0];
+ ctx.errcode = ARGV_ERROR_INTERNAL;
+ argv_show_error(&ctx);
+ argv_free(meta);
+
+ return 0;
+ }
+
+ if (ctx.flags & ARGV_VERBOSITY_STATUS)
+ argv_show_status(options,&ctx,meta);
+
+ return meta;
+}
+
+static void argv_free(struct argv_meta * xmeta)
+{
+ struct argv_meta_impl * imeta;
+ uintptr_t addr;
+
+ if (xmeta) {
+ addr = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta);
+ imeta = (struct argv_meta_impl *)addr;
+ argv_free_impl(imeta);
+ }
+}
+
+static void argv_usage(
+ FILE * file,
+ const char * header,
+ const struct argv_option options[],
+ const char * mode)
+{
+ const struct argv_option * option;
+ bool fshort,flong,fboth;
+ size_t len,optlen,desclen;
+ char cache;
+ char * prefix;
+ char * desc;
+ char * mark;
+ char * cap;
+ char description[4096];
+ char optstr [72];
+ const size_t optcap = 64;
+ const size_t width = 80;
+ const char indent[] = " ";
+ const char creset[] = "\x1b[0m";
+ const char cbold [] = "\x1b[1m";
+ const char cgreen[] = "\x1b[32m";
+ const char cblue [] = "\x1b[34m";
+ const char ccyan [] = "\x1b[36m";
+ const char * color = ccyan;
+ bool fcolor;
+
+ fshort = mode ? !strcmp(mode,"short") : 0;
+ flong = fshort ? 0 : mode && !strcmp(mode,"long");
+ fboth = !fshort && !flong;
+ fcolor = isatty(fileno(file));
+
+ if (fcolor)
+ fprintf(file,"%s%s",cbold,cgreen);
+
+ if (header)
+ fprintf(file,"%s",header);
+
+ option = options;
+ optlen = 0;
+
+ for (; option->short_name || option->long_name; option++) {
+ /* indent + comma */
+ len = fboth ? sizeof(indent) + 1 : sizeof(indent);
+
+ /* -o */
+ if (fshort || fboth)
+ len += option->short_name
+ ? 2 : 0;
+
+ /* --option */
+ if (flong || fboth)
+ len += option->long_name
+ ? 2 + strlen(option->long_name) : 0;
+
+ /* optlen */
+ if (len > optlen)
+ optlen = len;
+ }
+
+ if (optlen >= optcap) {
+ fprintf(stderr,
+ "Option strings exceed %zu characters, "
+ "please generate the usage screen manually.\n",
+ optcap);
+ return;
+ }
+
+ optlen += ARGV_TAB_WIDTH;
+ optlen &= (~(ARGV_TAB_WIDTH-1));
+ desclen = (optlen < width / 2) ? width - optlen : optlen;
+
+ for (option=options; option->short_name || option->long_name; option++) {
+ /* color */
+ if (fcolor) {
+ color = (color == ccyan) ? cblue : ccyan;
+ fputs(color,file);
+ }
+
+ /* description, using either paradigm or argname if applicable */
+ snprintf(description,sizeof(description),option->description,
+ option->paradigm
+ ? option->paradigm
+ : option->argname ? option->argname : "");
+ description[sizeof(description)-1] = 0;
+
+ /* long/hybrid option prefix (-/--) */
+ prefix = option->flags & ARGV_OPTION_HYBRID_ONLY
+ ? " -" : "--";
+
+ /* option string */
+ if (fboth && option->short_name && option->long_name)
+ sprintf(optstr,"%s-%c,%s%s",
+ indent,option->short_name,prefix,option->long_name);
+
+ else if ((fshort || fboth) && option->short_name)
+ sprintf(optstr,"%s-%c",indent,option->short_name);
+
+ else if (flong && option->long_name)
+ sprintf(optstr,"%s%s%s",
+ indent,prefix,option->long_name);
+
+ else if (fboth && option->long_name)
+ sprintf(optstr,"%s %s%s",
+ indent,prefix,option->long_name);
+
+ else
+ optstr[0] = 0;
+
+ /* right-indented option buffer */
+ if (description[0]) {
+ len = strlen(optstr);
+ sprintf(&optstr[len],"%-*c",(int)(optlen-len),' ');
+ }
+
+ /* single line? */
+ if (optlen + strlen(description) < width) {
+ fprintf(file,"%s%s\n",optstr,description);
+
+ } else {
+ desc = description;
+ cap = desc + strlen(description);
+
+ while (desc < cap) {
+ mark = (desc + desclen >= cap)
+ ? cap : desc + desclen;
+
+ while (*mark && (mark > desc)
+ && (*mark != ' ')
+ && (*mark != '|')
+ && (*mark != '\t')
+ && (*mark != '\n'))
+ mark--;
+
+ if (mark == desc) {
+ mark = (desc + desclen >= cap)
+ ? cap : desc + desclen;
+ cache = *mark;
+ *mark = 0;
+ } else if (*mark == '|') {
+ cache = *mark;
+ *mark = 0;
+ } else {
+ cache = 0;
+ *mark = 0;
+ }
+
+ /* first line? */
+ if (desc == description)
+ fprintf(file,"%s%s\n",optstr,desc);
+ else
+ fprintf(file,"%-*c %s\n",
+ (*desc == '|')
+ ? (int)(optlen+1)
+ : (int)optlen,
+ ' ',desc);
+
+ if (cache)
+ *mark = cache;
+ else
+ mark++;
+
+ desc = mark;
+ }
+ }
+ }
+
+ if (fcolor)
+ fputs(creset,file);
+}
+
+#endif
+
+#endif
diff --git a/src/internal/nolibc/nt32/ntux_compiler.asm b/src/internal/nolibc/nt32/ntux_compiler.asm
new file mode 100644
index 0000000..fa1e9da
--- /dev/null
+++ b/src/internal/nolibc/nt32/ntux_compiler.asm
@@ -0,0 +1,20 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; ntux: native translation und extension ;;
+;; Copyright (C) 2016 Z. Gilboa ;;
+;; Released under GPLv2 and GPLv3; see COPYING.NTUX. ;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework
+; and are provided here in the form of no-op functions in order to satisfy
+; compiler-generated dependencies.
+
+TITLE __psx_compiler
+
+.model flat
+.code
+
+___chkstk PROC EXPORT
+ ret
+___chkstk ENDP
+
+END
diff --git a/src/internal/nolibc/nt32/ntux_compiler.s b/src/internal/nolibc/nt32/ntux_compiler.s
new file mode 100644
index 0000000..89e2840
--- /dev/null
+++ b/src/internal/nolibc/nt32/ntux_compiler.s
@@ -0,0 +1,20 @@
+############################################################
+## ntux: native translation und extension ##
+## Copyright (C) 2016 Z. Gilboa ##
+## Released under GPLv2 and GPLv3; see COPYING.NTUX. ##
+############################################################
+
+# ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework
+# and are provided here in the form of no-op functions in order to satisfy
+# compiler-generated dependencies.
+
+.section .text
+
+.global _pei386_runtime_relocator
+.global ___chkstk_ms
+
+_pei386_runtime_relocatorr:
+ ret
+
+___chkstk_ms:
+ ret
diff --git a/src/internal/nolibc/nt64/ntux_compiler.asm b/src/internal/nolibc/nt64/ntux_compiler.asm
new file mode 100644
index 0000000..d6011bd
--- /dev/null
+++ b/src/internal/nolibc/nt64/ntux_compiler.asm
@@ -0,0 +1,19 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; ntux: native translation und extension ;;
+;; Copyright (C) 2016 Z. Gilboa ;;
+;; Released under GPLv2 and GPLv3; see COPYING.NTUX. ;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+; ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework
+; and are provided here in the form of no-op functions in order to satisfy
+; compiler-generated dependencies.
+
+TITLE __psx_compiler
+
+.code
+
+__chkstk PROC EXPORT
+ ret
+__chkstk ENDP
+
+END
diff --git a/src/internal/nolibc/nt64/ntux_compiler.s b/src/internal/nolibc/nt64/ntux_compiler.s
new file mode 100644
index 0000000..89e2840
--- /dev/null
+++ b/src/internal/nolibc/nt64/ntux_compiler.s
@@ -0,0 +1,20 @@
+############################################################
+## ntux: native translation und extension ##
+## Copyright (C) 2016 Z. Gilboa ##
+## Released under GPLv2 and GPLv3; see COPYING.NTUX. ##
+############################################################
+
+# ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework
+# and are provided here in the form of no-op functions in order to satisfy
+# compiler-generated dependencies.
+
+.section .text
+
+.global _pei386_runtime_relocator
+.global ___chkstk_ms
+
+_pei386_runtime_relocatorr:
+ ret
+
+___chkstk_ms:
+ ret
diff --git a/src/internal/nolibc/stdbool.h b/src/internal/nolibc/stdbool.h
new file mode 100644
index 0000000..45fc6f9
--- /dev/null
+++ b/src/internal/nolibc/stdbool.h
@@ -0,0 +1,11 @@
+#ifndef _STDBOOL_H
+#define _STDBOOL_H
+
+#ifndef __cplusplus
+
+#define true 1
+#define false 0
+#define bool _Bool
+
+#endif
+#endif
diff --git a/src/internal/nolibc/stddef.h b/src/internal/nolibc/stddef.h
new file mode 100644
index 0000000..8288bae
--- /dev/null
+++ b/src/internal/nolibc/stddef.h
@@ -0,0 +1,6 @@
+#ifndef _STDDEF_H
+#define _STDDEF_H
+
+#define offsetof(type, member) ((size_t)( (char *)&(((type *)0)->member) - (char *)0 ))
+
+#endif
diff --git a/src/internal/nolibc/stdint.h b/src/internal/nolibc/stdint.h
new file mode 100644
index 0000000..831d865
--- /dev/null
+++ b/src/internal/nolibc/stdint.h
@@ -0,0 +1,6 @@
+#ifndef _STDINT_H
+#define _STDINT_H
+
+#include <psxtypes/psxtypes.h>
+
+#endif
diff --git a/src/internal/nolibc/stdio.h b/src/internal/nolibc/stdio.h
new file mode 100644
index 0000000..610533e
--- /dev/null
+++ b/src/internal/nolibc/stdio.h
@@ -0,0 +1,8 @@
+#ifndef _STDIO_H
+#define _STDIO_H
+
+#define STDIN_FILENO 0
+#define STDOUT_FILENO 1
+#define STDERR_FILENO 2
+
+#endif
diff --git a/src/internal/nolibc/stdlib.h b/src/internal/nolibc/stdlib.h
new file mode 100644
index 0000000..8a24bec
--- /dev/null
+++ b/src/internal/nolibc/stdlib.h
@@ -0,0 +1,4 @@
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+#endif
diff --git a/src/internal/nolibc/string.h b/src/internal/nolibc/string.h
new file mode 100644
index 0000000..f8e3b8e
--- /dev/null
+++ b/src/internal/nolibc/string.h
@@ -0,0 +1,4 @@
+#ifndef _STRING_H
+#define _STRING_H
+
+#endif
diff --git a/src/internal/nolibc/unistd.h b/src/internal/nolibc/unistd.h
new file mode 100644
index 0000000..2e39dc8
--- /dev/null
+++ b/src/internal/nolibc/unistd.h
@@ -0,0 +1,4 @@
+#ifndef _UNISTD_H
+#define _UNISTD_H
+
+#endif
diff --git a/src/internal/ntux_driver_impl.h b/src/internal/ntux_driver_impl.h
new file mode 100644
index 0000000..815f3ef
--- /dev/null
+++ b/src/internal/ntux_driver_impl.h
@@ -0,0 +1,33 @@
+#ifndef NTUX_DRIVER_IMPL_H
+#define NTUX_DRIVER_IMPL_H
+
+#include <ntapi/ntapi.h>
+#include <psxscl/psxscl.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include <ntux/ntux.h>
+#include "argv/argv.h"
+
+extern const struct argv_option ntux_default_options[];
+extern const ntapi_vtbl * ntux_ntapi;
+
+#define ntapi ntux_ntapi
+
+enum app_tags {
+ TAG_HELP,
+ TAG_VERSION,
+};
+
+struct ntux_driver_ctx_impl {
+ struct ntux_common_ctx cctx;
+ struct ntux_driver_ctx ctx;
+ struct __psx_context xctx;
+ const char * eunit;
+ struct ntux_error_info ** errinfp;
+ struct ntux_error_info ** erricap;
+ struct ntux_error_info * erriptr[64];
+ struct ntux_error_info erribuf[64];
+};
+
+#endif
diff --git a/src/internal/ntux_init_impl.h b/src/internal/ntux_init_impl.h
new file mode 100644
index 0000000..69d008a
--- /dev/null
+++ b/src/internal/ntux_init_impl.h
@@ -0,0 +1,25 @@
+#ifndef NTUX_INIT_IMPL_H
+#define NTUX_INIT_IMPL_H
+
+#include <psxxfi/xfi_ntapi.h>
+#include <ntapi/ntapi.h>
+#include <ntapi/nt_atomic.h>
+
+extern const ntapi_vtbl * ntux_ntapi;
+
+static int ntux_init(void)
+{
+ int32_t status;
+ ntapi_vtbl * pvtbl;
+
+ if ((status = __xfi_ntapi_init(&pvtbl)))
+ return status;
+
+ at_locked_cas(
+ (intptr_t *)&ntux_ntapi,
+ 0,(intptr_t)pvtbl);
+
+ return 0;
+}
+
+#endif
diff --git a/src/internal/ntux_memfn_impl.c b/src/internal/ntux_memfn_impl.c
new file mode 100644
index 0000000..1bcc753
--- /dev/null
+++ b/src/internal/ntux_memfn_impl.c
@@ -0,0 +1,94 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <stddef.h>
+#include <ntapi/ntapi.h>
+#include "ntux_memfn_impl.h"
+#include "ntux_driver_impl.h"
+
+void * ntux_calloc(size_t n, size_t size)
+{
+ struct ntux_memory_block block;
+
+ if (!n || (size > (size_t)(-1) / n))
+ return 0;
+
+ size *= n;
+ size += 0xf;
+ size &= ~(size_t)0xf;
+
+ block.addr = 0;
+ block.size = size + sizeof(block);
+
+ if (ntapi->zw_allocate_virtual_memory(
+ NT_CURRENT_PROCESS_HANDLE,
+ &block.addr,
+ 0,
+ &block.size,
+ NT_MEM_COMMIT,
+ NT_PAGE_READWRITE))
+ return 0;
+
+ block.used = size;
+ block.avail = block.size - block.used;
+
+ return (char *)block.addr + offsetof(struct ntux_memory_block,buffer);
+}
+
+void * ntux_balloc(
+ struct ntux_memory_block * cache,
+ struct ntux_memory_block * block,
+ size_t n, size_t size)
+{
+ char * baddr;
+ void * addr = 0;
+
+ if (!n || (size > (size_t)(-1) / n))
+ return 0;
+
+ if (!cache || !cache->addr)
+ addr = ntux_calloc(n,size);
+
+ size *= n;
+ size += 0xf;
+ size &= ~(size_t)0xf;
+
+ if (size < block->avail)
+ addr = ntux_calloc(n,size);
+
+ /* newly allocated block? */
+ if (addr) {
+ baddr = addr;
+ baddr -= offsetof(struct ntux_memory_block,buffer);
+
+ ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)block,
+ (uintptr_t *)baddr,
+ sizeof(*block));
+
+ return addr;
+ }
+
+ /* cache */
+ addr = &block->buffer[block->used / sizeof(size_t)];
+ block->used += size;
+ block->avail -= size;
+ return addr;
+}
+
+void ntux_free(void * addr)
+{
+ struct ntux_memory_block block;
+
+ block.addr = addr;
+ block.size = 0;
+
+ ntapi->zw_free_virtual_memory(
+ NT_CURRENT_PROCESS_HANDLE,
+ &block.addr,
+ &block.size,
+ NT_MEM_RELEASE);
+}
diff --git a/src/internal/ntux_memfn_impl.h b/src/internal/ntux_memfn_impl.h
new file mode 100644
index 0000000..bb8e300
--- /dev/null
+++ b/src/internal/ntux_memfn_impl.h
@@ -0,0 +1,22 @@
+#ifndef NTUX_MEMFN_IMPL_H
+#define NTUX_MEMFN_IMPL_H
+
+#include <stdint.h>
+
+struct ntux_memory_block {
+ void * addr;
+ size_t size;
+ size_t used;
+ size_t avail;
+ size_t buffer[];
+};
+
+void * ntux_calloc(size_t n, size_t size);
+void ntux_free(void * addr);
+
+void * ntux_balloc(
+ struct ntux_memory_block * cache,
+ struct ntux_memory_block * block,
+ size_t n, size_t size);
+
+#endif
diff --git a/src/internal/ntux_nolibc_impl.c b/src/internal/ntux_nolibc_impl.c
new file mode 100644
index 0000000..1b5867d
--- /dev/null
+++ b/src/internal/ntux_nolibc_impl.c
@@ -0,0 +1,73 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <ntapi/ntapi.h>
+
+extern const ntapi_vtbl * ntux_ntapi;
+
+void * ntux_memcpy(void * dst, const void * src, size_t n)
+{
+ return ntux_ntapi->tt_generic_memcpy(dst,src,n);
+}
+
+void * ntux_memset(void * ch, int c, size_t n)
+{
+ return ntux_ntapi->tt_generic_memset(ch,c,n);
+}
+
+char * ntux_strcpy(char * dst, const char * src)
+{
+ return ntux_ntapi->tt_generic_memcpy(
+ dst,src,
+ ntux_ntapi->tt_string_null_offset_multibyte(src));
+}
+
+size_t ntux_strlen(const char * ch)
+{
+ return ntux_ntapi->tt_string_null_offset_multibyte(ch);
+}
+
+int ntux_strcmp(const char * a, const char * b)
+{
+ return ntux_ntapi->tt_strcmp_multibyte(a,b);
+}
+
+int ntux_strncmp(const char * a, const char * b, size_t n)
+{
+ return ntux_ntapi->tt_strncmp_multibyte(a,b,n);
+}
+
+char * ntux_strchr(const char * ch, int c)
+{
+ for (; *ch; ch++)
+ if (*ch == c)
+ return (char *)ch;
+ return 0;
+}
+
+char * ntux_strrchr(const char * ch, int c)
+{
+ const char * base;
+
+ base = ch;
+ ch += ntux_ntapi->tt_string_null_offset_multibyte(ch);
+
+ for (; ch >= base; ch--)
+ if (*ch == c)
+ return (char *)ch;
+ return 0;
+}
+
+#ifdef NTUX_EXPORT
+int __stdcall ntux_entry_point(void * hinstance, uint32_t reason, void * reserved)
+{
+ (void)hinstance;
+ (void)reason;
+ (void)reserved;
+
+ return 1;
+}
+#endif
diff --git a/src/internal/ntux_nolibc_impl.h b/src/internal/ntux_nolibc_impl.h
new file mode 100644
index 0000000..d048bd4
--- /dev/null
+++ b/src/internal/ntux_nolibc_impl.h
@@ -0,0 +1,52 @@
+#ifndef NTUX_NOLIBC_IMPL_H
+#define NTUX_NOLIBC_IMPL_H
+
+#define isatty ntux_isatty
+#define fileno ntux_fileno
+
+#define fputs ntux_fputs
+#define fprintf ntux_fprintf
+#define sprintf ntux_sprintf
+#define snprintf ntux_snprintf
+
+#define memcpy ntux_memcpy
+#define memset ntux_memset
+
+#define strcpy ntux_strcpy
+#define strlen ntux_strlen
+#define strcmp ntux_strcmp
+#define strncmp ntux_strncmp
+#define strchr ntux_strchr
+#define strrchr ntux_strrchr
+
+#define calloc ntux_calloc
+#define free ntux_free
+
+#define stdin (void *)0
+#define stdout (void *)1
+#define stderr (void *)2
+
+typedef struct ntux_file FILE;
+
+int ntux_isatty(int fildes);
+int ntux_fileno(void * any);
+
+int ntux_sprintf(char * str, const char * fmt, ...);
+int ntux_snprintf(char * str, size_t n, const char * fmt, ...);
+int ntux_fprintf(FILE *__restrict, const char *__restrict, ...);
+int ntux_fputs(const char * str, FILE * file);
+
+void * ntux_memcpy(void * dst, const void * src, size_t n);
+void * memset(void * ch, int c, size_t n);
+
+char * ntux_strcpy(char * dst, const char * src);
+size_t ntux_strlen(const char * ch);
+int ntux_strcmp(const char * a, const char * b);
+int ntux_strncmp(const char * a, const char * b, size_t n);
+char * ntux_strchr(const char * ch, int c);
+char * ntux_strrchr(const char * ch, int c);
+
+void * ntux_calloc(size_t n, size_t size);
+void ntux_free(void *);
+
+#endif
diff --git a/src/internal/ntux_ntaio_impl.c b/src/internal/ntux_ntaio_impl.c
new file mode 100644
index 0000000..377a833
--- /dev/null
+++ b/src/internal/ntux_ntaio_impl.c
@@ -0,0 +1,114 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <ntapi/ntapi.h>
+#include <psxabi/sys_sysapi.h>
+#include <psxabi/sys_errno.h>
+
+extern const ntapi_vtbl * ntux_ntapi;
+
+typedef struct ntux_file {
+ void * hany;
+} FILE;
+
+int ntux_fputs(const char * str, FILE * file)
+{
+ ssize_t ret;
+ size_t size;
+ size_t nbytes;
+
+ size = ntux_ntapi->tt_string_null_offset_multibyte(str);
+ nbytes = size;
+
+ while (nbytes) {
+ ret = __sys_write(
+ (int)(intptr_t)file,
+ str,nbytes);
+
+ if (ret == -EINTR)
+ (void)0;
+
+ else if (ret < 0)
+ return -1;
+
+ else {
+ str += ret;
+ nbytes -= ret;
+ }
+ }
+
+ /* all done */
+ return (int)size;
+}
+
+int ntux_fprintf(FILE * file, const char * fmt, ...)
+{
+ va_list ap;
+ char * str;
+ size_t buffer[32768/sizeof(size_t)];
+
+ ntux_ntapi->tt_aligned_block_memset(
+ buffer,0,sizeof(buffer));
+
+ str = (char *)buffer;
+
+ va_start(ap, fmt);
+ ntux_ntapi->vsnprintf(str, sizeof(buffer), fmt, ap);
+ va_end(ap);
+
+ str[sizeof(buffer)-1] = 0;
+
+ return ntux_fputs(str,file);
+}
+
+int ntux_sprintf(char * str, const char * fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = ntux_ntapi->vsprintf(str, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+int ntux_snprintf(char * str, size_t n, const char * fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = ntux_ntapi->vsnprintf(str, n, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+int ntux_isatty(int fildes)
+{
+ nt_runtime_data * rtdata;
+
+ if ((ntux_ntapi->tt_get_runtime_data(&rtdata,0)))
+ return 0;
+
+ if (fildes == 0)
+ return (rtdata->stdin_type == NT_FILE_TYPE_PTY);
+
+ else if (fildes == 1)
+ return (rtdata->stdout_type == NT_FILE_TYPE_PTY);
+
+ else if (fildes == 2)
+ return (rtdata->stderr_type == NT_FILE_TYPE_PTY);
+
+ else
+ return 0;
+}
+
+int ntux_fileno(void * any)
+{
+ return (int)(intptr_t)any;
+}
diff --git a/src/ntux.c b/src/ntux.c
new file mode 100644
index 0000000..6c70d0b
--- /dev/null
+++ b/src/ntux.c
@@ -0,0 +1,34 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <psxabi/sys_sysapi.h>
+#include <ntux/ntux.h>
+#include "ntux_driver_impl.h"
+
+int ntux_entry_point(void)
+{
+ int argc;
+ char ** argv;
+ char ** envp;
+ struct __psx_context ctx = {0,0,0,0,0,0,0,0,0,0,0};
+
+ /* ctx */
+ ctx.size = sizeof(ctx);
+
+ /* __psx_init must succeed... */
+ if (__psx_init(&argc,&argv,&envp,&ctx))
+ return -1;
+
+ /* ...and conform */
+ else if (envp != argv + (argc + 1))
+ return -1;
+
+ /* invoke main */
+ __sys_exit_group(
+ ntux_main(argc,argv,envp));
+
+ return NT_STATUS_INTERNAL_ERROR;
+}
diff --git a/src/skin/ntux_skin_default.c b/src/skin/ntux_skin_default.c
new file mode 100644
index 0000000..7bb0045
--- /dev/null
+++ b/src/skin/ntux_skin_default.c
@@ -0,0 +1,12 @@
+#include "ntux_driver_impl.h"
+#include "argv/argv.h"
+
+const struct argv_option ntux_default_options[] = {
+ {"version", 'v',TAG_VERSION,ARGV_OPTARG_NONE,0,0,0,
+ "show version information"},
+
+ {"help", 'h',TAG_HELP,ARGV_OPTARG_OPTIONAL,0,"short|long",0,
+ "show usage information [listing %s options only]"},
+
+ {0,0,0,0,0,0,0,0}
+};