Copyright (c) 2015, Electric Cloud, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

  - Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(As an historical note, the above license instantiates
the "BSD 2-Clause License" template that was presented
by the Open Source Initiative at

  http://www.opensource.org/licenses/bsd-license.php

on 2012-02-23.)


Introduction
------------

This package defines a binary POSIX-like interface that
you can use to write an application that may be run by
any implementation of that interface.

An implementation might directly delegate to an underlying
POSIX operating system (such as Linux).

But an implementation might be define or delegate to a virtual
or sandboxed environment that simulates a POSIX environment,
even where the host operating system does not adhere to the
POSIX standard.  For example, Cygwin, a trusted browser plug-in,
or a cloud computing application environment.

This package also defines an interface to how the virtual POSIX-like
behavior is embedded (or seems to be embedded) into some underlying
system.  (On a POSIX system that embedding may be rather trivial.)

It also extends POSIX into areas POSIX does not yet cover.
(Such extensions should be clearly noted.)

There can even be multiple implementations of the same interface
on the same operating system.  And an implementation may support
different versions of an interface simultaneously, without conflict.

In addition to the interfaces as such, this package provides an
implementation: one that loads a dynamic library you write, and
provides it with handles and function pointers through which it can
perform tasks such as I/O, thread creation, signal processing, etc.

The implementation does not call a "main" function as such; rather,
it scans a table of entry points provided by your dynamic library
(details in "interface/vrpsx_identified_function.h"), and calls the
first one it supports.  Often that entry point is very similar to
"main", but not the same.

Entry points may ask to receive a pointer to a handle and table of
function pointers that together may be used to perform POSIX-like
system calls.  These calls are always indirect, because by avoiding
linker symbols we allow simultaneous support of multiple versions
of these interfaces.  You will need to pass such pointers to all
portions of of your dynamic library that need them.

See "interface/vrpsx_system.h" for a further discussion
of the rationale, but in summary, the goal is to allow you
to create such dynamic libraries and then use them in the
widest possible range of contexts--or create a new context
in which to run an existing library.


Adding System Calls
-------------------

To support an additional POSIX system call, add it to
"vrpsx_system_vtbl_t" in "interface/vrpsx_system.h", and
also add an implementation to "direct/implementation.c".

The implementation will consist of a function definition (two if you
want to include a traced version of the call, which is recommended),
and inclusion of the address of that function or functions in the
two instances of "vrpsx_system_vtbl_t" (one for when tracing is
enabled, and one for when tracing is disabled).

Depending upon the system call, you might wish to define supporting
interfaces and put some system calls in those.  For example, see the
"vrpsx_mutex" and "vrpsx_mutex_attr" interfaces (that is, the structs
with those name prefixes).  When you ask for a mutex, you obtain an
interface to that particular mutex, and then you use that interface
to lock and unlock the mutex, rather than going back to the general
"vrpsx_system" interface for each lock/unlock.

Then add corresponding C++ wrappers in the "cpp" directory.

If you add new files, then periodically run "check_manifest"
to see which ones need to be added to "MANIFEST.txt".


Portability
-----------

Currently this package supports the following platforms:

  - Cygwin + GCC

  - Linux + GCC

But this package attempts broader support for its interface headers.
Ideally you should be able to use those with most versions of GCC or
Microsoft Visual Studio, even on other operating systems.  Of course
it may become necessary to address portability issues as they arise.

The "vrpsx_preprocessor.h" and "vrpsx_utility.h" headers should also
work broadly, unless you attempt to expand the macros that implement
atomic reference count adjustments, which tend to narrow portability.


Building
--------

Choose the directories which are to receive your architecture-specific
and architecture-neutral outputs, then invoke "make" (or similar) with
those parameters:

  make ARCHDIR=... NEUTDIR=...

The output includes a program that implements interfaces defined
by this package and dynamically loads clients of those interfaces.
In makefile notation, that output program is:

  $(ARCHDIR)/direct/vrpsx_loader$(EXE_EXT)

where EXE_EXT holds the filename extension for executables on your system.


Organization
------------

COPYING        : copyright licensing
MANIFEST.txt   : list of source files in this package
Makefile       : the monolithic makefile that builds this package
README.txt     : this file
check_manifest : finds files omitted from "MANIFEST.txt" & other discrepancies
cpp            : optional C++ utilities; does NOT affect any binary interfaces
direct         : direct implementation on a possibly-embedded POSIX-like system
examples       : example interface clients
interface      : interface definitions
md5            : an MD5 implementation
utility        : optional C utilities for interface clients and implementors

Reading the comments near the start of "interface/vrpsx_system.h" is
a good way to get started in understanding how to call the interfaces.
