Blame src/driver/ptyc_amain.c

0efa8c
/*********************************************************/
0efa8c
/*  ptycon: a pty-console bridge                         */
1f983d
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
0efa8c
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
0efa8c
/*********************************************************/
0efa8c
0efa8c
#include <ntapi/ntapi.h>
0efa8c
#include <stdio.h>
0efa8c
#include <unistd.h>
0efa8c
0efa8c
#include <ptycon/ptycon.h>
0efa8c
#include "ptycon_init_impl.h"
0efa8c
#include "ptycon_driver_impl.h"
0efa8c
#include "ptycon_nolibc_impl.h"
0efa8c
0efa8c
#ifndef PTYC_DRIVER_FLAGS
0efa8c
#define PTYC_DRIVER_FLAGS	PTYC_DRIVER_VERBOSITY_ERRORS \
0efa8c
				| PTYC_DRIVER_VERBOSITY_USAGE
0efa8c
#endif
0efa8c
0efa8c
static const char vermsg[] = "%s%s%s (git://midipix.org/ptycon): "
0efa8c
			     "version %s%d.%d.%d%s.\n"
0efa8c
			     "[commit reference: %s%s%s]\n";
0efa8c
0efa8c
static const char * const ptyc_ver_color[6] = {
0efa8c
		"\x1b[1m\x1b[35m","\x1b[0m",
0efa8c
		"\x1b[1m\x1b[32m","\x1b[0m",
0efa8c
		"\x1b[1m\x1b[34m","\x1b[0m"
0efa8c
};
0efa8c
0efa8c
static const char * const ptyc_ver_plain[6] = {
0efa8c
		"","",
0efa8c
		"","",
0efa8c
		"",""
0efa8c
};
0efa8c
0efa8c
static ssize_t ptyc_version(struct ptyc_driver_ctx * dctx)
0efa8c
{
0efa8c
	const struct ptyc_source_version * verinfo;
0efa8c
	const char * const * verclr;
0efa8c
0efa8c
	verinfo = ptyc_source_version();
0efa8c
	verclr  = isatty(STDOUT_FILENO) ? ptyc_ver_color : ptyc_ver_plain;
0efa8c
0efa8c
	return fprintf(stdout,vermsg,
0efa8c
			verclr[0],dctx->program,verclr[1],
0efa8c
			verclr[2],verinfo->major,verinfo->minor,
0efa8c
			verinfo->revision,verclr[3],
0efa8c
			verclr[4],verinfo->commit,verclr[5]);
0efa8c
}
0efa8c
0efa8c
static int ptyc_exit(struct ptyc_driver_ctx * dctx, int nerrors)
0efa8c
{
0efa8c
	ptyc_free_driver_ctx(dctx);
0efa8c
	return nerrors ? 2 : 0;
0efa8c
}
0efa8c
0efa8c
int ptyc_main(int argc, char ** argv, char ** envp)
0efa8c
{
0efa8c
	int				ret;
0efa8c
	struct ptyc_driver_ctx *	dctx;
0efa8c
0efa8c
	if ((ret = ptyc_init()))
0efa8c
		return ret;
0efa8c
0efa8c
	if ((ret = ptyc_get_driver_ctx(argv,envp,PTYC_DRIVER_FLAGS,&dctx)))
0efa8c
		return (ret == PTYC_USAGE) ? !--argc : 2;
0efa8c
0efa8c
	if (dctx->cctx->drvflags & PTYC_DRIVER_VERSION)
0efa8c
		if ((ptyc_version(dctx)) < 0)
0efa8c
			return ptyc_exit(dctx,2);
0efa8c
690c8c
	if (!dctx->cctx->eargv)
690c8c
		if (!(dctx->cctx->drvflags & PTYC_DRIVER_DBG_MODE))
690c8c
			return ptyc_exit(dctx,0);
690c8c
538c8a
	if (ptyc_alloc_pty(dctx))
538c8a
		return ptyc_exit(dctx,2);
538c8a
9cd8e5
	if (ptyc_spawn(dctx))
9cd8e5
		return ptyc_exit(dctx,2);
9cd8e5
1bab02
	if (ptyc_alloc_console(dctx))
1bab02
		return ptyc_exit(dctx,2);
1bab02
1bab02
	if (ptyc_wait_for_console(dctx))
1bab02
		return ptyc_exit(dctx,2);
1bab02
0efa8c
	return ptyc_exit(dctx,ret);
0efa8c
}