/*****************************************************************************/
/*
                                CgiLib.h

See CGILIB.C for version information.
*/
/*****************************************************************************/

#ifndef CGILIB_H_LOADED
#define CGILIB_H_LOADED 1

#include <descrip.h>
#include <stdio.h>

#define CGILIB_CGIPLUS_VAR_RECORD "CGILIB_CGIPLUS_VAR_RECORD"

#ifndef CGILIB_SOFTWAREID
#define CGILIB_SOFTWAREID CgiLib__SoftwareID
extern char CgiLib__SoftwareID [];
#endif

/* mainly to allow easy use of the __unaligned directive */
#ifndef ULONGPTR
#define ULONGPTR __unaligned unsigned long*
#define USHORTPTR __unaligned unsigned short*
#define INT64PTR __unaligned __int64*
#define UINT64PTR __unaligned unsigned __int64*
#endif

/* Alpha NO MEMBER ALIGNMENT unless otherwise specified */
#ifndef __VAX
#   pragma member_alignment __save
#   pragma nomember_alignment
#endif

/* constants for tracking what environment it's catering for */
#define CGILIB_ENVIRONMENT_WASD      1
#define CGILIB_ENVIRONMENT_CGIPLUS   2
#define CGILIB_ENVIRONMENT_OSU       3
#define CGILIB_ENVIRONMENT_PURVEYOR  4
#define CGILIB_ENVIRONMENT_APACHE    5
#define CGILIB_ENVIRONMENT_CGI       6

#define CGILIB_ENVIRONMENT_STDOUT_BINARY 1
#define CGILIB_ENVIRONMENT_STDOUT_RECORD 2

#ifndef CGILIB_VARLIST_CHUNK
#   define CGILIB_VARLIST_CHUNK 2048
#endif

#ifdef CGILIB_CGIPLUS_LINE_SIZE
    /* for backward compatibility */
#   define CGILIB_CGIPLUS_RECORD_SIZE CGILIB_CGIPLUS_RECORD_SIZE
#endif
#ifndef CGILIB_CGIPLUS_RECORD_SIZE
    /* set fairly large to allow for CGIplus stream variables */
#   define CGILIB_CGIPLUS_RECORD_SIZE 16384
#endif

#ifndef CGILIB_BODY_CHUNK
#   define CGILIB_BODY_CHUNK 4096
#endif
 /* size of memory allocation in CgiLib__VarList() */
#ifndef CGILIB_VARLIST_CHUNK
#   define CGILIB_VARLIST_CHUNK 2048
#endif

#ifndef CGILIB_ENCODED_CHUNK
#   define CGILIB_ENCODED_CHUNK 256
#endif
#ifndef CGILIB_ESCAPED_CHUNK
#   define CGILIB_ESCAPED_CHUNK 256
#endif
 /* bitwise values used in CgiLibAnchorEscapeHtml() */
#define CGILIB_ANCHOR_NONE   0x00000000
#define CGILIB_ANCHOR_WEB    0x00000001
#define CGILIB_ANCHOR_MAIL   0x00000002
#define CGILIB_ANCHOR_ALL2   0x0000ffff  /* to use with _SPLIT */
#define CGILIB_ANCHOR_SPLIT  0x00010000
#define CGILIB_ANCHOR_MASK1  0x0000ffff
#define CGILIB_ANCHOR_MASK2  0xffff0000
#define CGILIB_ANCHOR_ALL    0xffffffff  /* for 'backward' compatibility */

#ifndef CGILIB_RESPONSE_BODY
    /* <BODY...> tag parameters */
#   define CGILIB_RESPONSE_BODY              ""
#endif
#ifndef CGILIB_RESPONSE_CHARSET
    /* script-specified ";charset=xxx" if the content is text */
#   define CGILIB_RESPONSE_CHARSET           NULL
#endif
#ifndef CGILIB_RESPONSE_DEFAULT_CHARSET
    /* default ";charset=xxx" if the content is text */
#   define CGILIB_RESPONSE_DEFAULT_CHARSET   "ISO-8859-1"
#endif
#ifndef CGILIB_RESPONSE_SOFTWAREID
    /* for <META...> tag information */
#   define CGILIB_RESPONSE_SOFTWAREID  CGILIB_SOFTWAREID
#endif
#ifndef CGILIB_RESPONSE_ERROR_MESSAGE
#   define CGILIB_RESPONSE_ERROR_MESSAGE     "Reported by script."
#endif
 
#ifndef CGILIB_RESPONSE_ERROR_STATUS
    /* HTTP status code when reporting errors */
#   define CGILIB_RESPONSE_ERROR_STATUS      404
#endif
#ifndef CGILIB_RESPONSE_ERROR_TITLE
    /* large, bolded error report page title */
#   define CGILIB_RESPONSE_ERROR_TITLE       "ERROR"
#endif
#ifndef CGILIB_RESPONSE_ERROR_INFO
    /* post-error-report text message (site-specific) */
#   define CGILIB_RESPONSE_ERROR_INFO        ""
#endif
 
#ifndef CGILIB_RESPONSE_SUCCESS_STATUS
    /* HTTP status code when reporting success */
#   define CGILIB_RESPONSE_SUCCESS_STATUS      200
#endif
#ifndef CGILIB_RESPONSE_SUCCESS_TITLE
    /* large, bolded success report page title */
#   define CGILIB_RESPONSE_SUCCESS_TITLE     "SUCCESS"
#endif
#ifndef CGILIB_RESPONSE_SUCCESS_MESSAGE
#   define CGILIB_RESPONSE_SUCCESS_MESSAGE   "Reported by script."
#endif
 
#ifndef CGILIB_DEBUG
#  define CGILIB_DEBUG 0
#endif

#ifndef CGILIB_NONE
#   define CGILIB_NONE ""
#endif

#define sys$getmsg SYS$GETMSG

/* elbow-room :-) additional bytes on a memory allocation */
#define CGILIB_VEEMEM_EBROOM 16

/* the size of the VeeMem chunk */
#define CGILIB_VEEMEM_SIZE(vptr) \
(*(unsigned long*)(((char*)vptr)-sizeof(unsigned long)))

/* is the VeeMem chunk exhausted? */
#define CGILIB_VEEMEM_GONE(vptr,mptr) \
(((char*)vptr)+(*(unsigned long*)(((char*)vptr)-sizeof(unsigned long))) > \
((char*)mptr))

/**************/
/* prototypes */
/**************/

char* CgiLibVar (char*);
char* CgiLibVarNull (char*);
char* CgiLibVeeMemVar (char*);
char* CgiLibVeeMemVarNull (char*);
int CgiLibVarByDesc (struct dsc$descriptor*, struct dsc$descriptor*, ...);

/* for backward-compatibility */
char* CgiVar (char*);

char* CgiLibHttpStatusCodeText (int);
int CgiLibUrlDecode (char*);

void CgiLibCgiPlusEOF ();
void CgiLibCgiPlusEOT ();
void CgiLibCgiPlusESC ();
void CgiLibCgiPlusInGets (char*, int);
void CgiLibCgiPlusSetVarRecord ();
void CgiLibCgiPlusSetVarStruct ();
char* CgiLibCgiPlusCallout (char*, ...);

void CgiLibInit (int, char*[], void(*)(void));
void CgiLib__PrepareRequest (void(*)(void));
void CgiLib__ApacheFixBg ();
int CgiLib__ApacheFixBgHandler ();
void CgiLib__ApacheSetSockOpt ();
int CgiLib__ApacheSetSockOptHandler ();
void CgiLibEnvironmentBinaryOut ();
void CgiLibEnvironmentInit (int, char*[], int);
int CgiLibEnvironmentIsApache ();
int CgiLibEnvironmentIsCgi ();
int CgiLibEnvironmentIsCgiPlus ();
int CgiLibEnvironmentIsOsu ();
int CgiLibEnvironmentIsPurveyor ();
int CgiLibEnvironmentIsWasd ();
char* CgiLibEnvironmentName ();
int CgiLibEnvironmentSetDebug (int);
void CgiLibEnvironmentRecordOut ();
/* for backward compatibility */
void CgiLibEnvironmentStandardOut ();
int CgiLibEnvironmentWasdVersion ();
char* CgiLibEnvironmentVersion ();
int CgiLibHttpProtocolVersion ();
char* CgiLibXUACompatible (char*);

char* CgiLibReadRequestBody (char**, int*);
int CgiLibFormRequestBody (char*, int);
int CgiLibSetUnChunkRequestBody (int);
int CgiLibUnChunkRequestBody (char*, int);
int CgiLibBodyIsFormUrlEncoded ();
int CgiLibBodyIsMultipartFormData ();

void CgiLibOsuInit (int, char*[]);
void CgiLibOsuStdoutCgi ();
void CgiLibOsuStdoutRaw ();
void CgiLibOsuStdoutText ();

int CgiLibAnchorHtmlEscape (char*, int, char*, int, int);
char* CgiLibFormEncodedParse (char*, int*);
char* CgiLibFaoPrint (FILE*, char*, ...);
int CgiLibHtmlDeEntify (char*);
int CgiLibHtmlEscape (char*, int, char*, int);
int CgiLibUrlEncode (char*, int, char*, int);
int CgiLibUrlEncodeFileName (char*, char*, int, int, int);

int CgiLibResponseGetStatus ();
int CgiLibResponseError (char*, int, int, char*, ...);
int CgiLibResponseHeader (int, char*, ...);
int CgiLibResponseRedirect (char*, ...);
int CgiLibResponseSuccess (char*, ...);
char* CgiLibResponseSetBody (char*);
char* CgiLibResponseSetCharset (char*);
char* CgiLibResponseSetDefaultCharset (char*);
char* CgiLibResponseSetErrorMessage (char*);
int CgiLibResponseSetErrorStatus (int);
char* CgiLibResponseSetErrorTitle (char*);
char* CgiLibResponseSetErrorInfo (char*);
int CgiLibResponseSetRecordMode (int);
int CgiLibResponseSetStreamMode (int);
char* CgiLibResponseSetSoftwareID (char*);
char* CgiLibResponseSetSuccessMessage (char*);
int CgiLibResponseSetSuccessStatus (int);
char* CgiLibResponseSetSuccessTitle (char*);

void* CgiLibVeeMemCalloc (unsigned long);
int CgiLibVeeMemContinue (int);
void CgiLibVeeMemFree (void*);
int CgiLibVeeMemInit ();
int CgiLibVeeMemInUse (int);
void* CgiLibVeeMemRealloc (void*, unsigned long);
int CgiLibVeeMemShow (void*);

/*****************************************************/
/* these functions are for INTERNAL CGILIB USE only! */
/*****************************************************/

void CgiLib__SetEnvironment ();
char* CgiLib__GetVar (char*, char*);
char* CgiLib__GetKey (char*, char*);
char* CgiLib__GetForm (char*, char*);
char* CgiLib__GetPathTranslated (char*);
char* CgiLib__VarList (char*, char*, char*);
int CgiLib__BodyFormUrlEncoded (char*, int);
int CgiLib__BodyMultipartFormData (char*, int, char*);
void CgiLib__ApacheExitRecordMode ();

void CgiLib__OsuExitCgi ();
void CgiLib__OsuExitRaw ();
void CgiLib__OsuExitText ();
char* CgiLib__OsuQueryNetLink (char*, int);

#ifndef __VAX
#   pragma member_alignment __restore
#endif

#endif /* CGILIB_H_LOADED */

/*****************************************************************************/

