/*      SetCon
 *        by
 *    Phil Dietz -- NCEMRSoft 1993
 *
 *    USAGE:    SetCon FORE/N,BACK/N,CLEAR/S,BOLD/S,FAINT/S,ITALIC/S,
 *                     UNDER/S,REVERSE/S,CONCEAL/S,RESTORE/S,RESET/S
 *
 *              FORE #    - sets forground color from color0 to color9
 *              BACK #    - sets background color from color0 to color9
 *              CLEAR     - clears the screen
 *              BOLD      - sets bold
 *              FAINT     - sets faint
 *              ITALIC    - sets italic
 *              UNDER     - sets underline
 *              REVERSE   - sets reverse
 *              CONCEAL   - sets conceal (warning invisible!)
 *              RESTORE   - (DEFAULT) restores to the last style/SGR
 *                          'SetCon RESTORE' is the same as just 'SetCon'.
 *              RESET     - resets the style to the bootup style
 *
 *    SUMMARY:  Sets the style for the current console/shell.
 *              In WB3.0, SetCon will also store your custom style as the
 *              default SGR/shell style so clears and resets
 *              won't erase your fav. settings!!
 *
 *    REQUIREMENTS:  WB2.0 version 37
 *                   WB3.0 to use SGR setting option
 *
 *    NOTES:    This program is PURE so it can be made RESIDENT.
 *              If you cant see what you are typing, try 'setcon reset'
 *              or just type ESCc
 *
 *    $VER: SetCon 2.0  (25-Sep-93)  Setcon Source
 *
 *    Changes since 1.0 -
 *        Chopped the executable from 1700 bytes to only 688 bytes by
 *        getting rid of the startup code.  Quite nice!
 *
 *    SC SetCon  NOLINK
 *    SLINK from SetCon.o to SetCon SC SD ND
 */

#include <stdio.h>
#include <exec/execbase.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/conunit.h>
#include <proto/dos.h>
#include <proto/exec.h>

#define RESTORE    "\33[0m"
#define TEMPLATE   "FORE/N,BACK/N,CLEAR/S,BOLD/S,FAINT/S,ITALIC/S,UNDER/S,REVERSE/S,CONCEAL/S,RESTORE/S,RESET/S\n"
#define NUM        11

static char ver[]="$VER: SetCon (25.9.93) by Phil Dietz";

static char *style[NUM]={
   "\33[3xm",   /* Initial foreground string */
   "\33[4xm",   /* Initial background string */
   "\14",       /* Clear  */
   "\33[1m",    /* Bold   */
   "\33[2m",    /* Faint  */
   "\33[3m",    /* Italic */
   "\33[4m",    /* Underline */
   "\33[7m",    /* Reverse video */
   "\33[8m",    /* Concealed mode */
   RESTORE,     /* Restore from last SGR */
   "\33c"       /* Reset all style and colors */
   };

void __saveds mymain(char *cmd)
   {

	struct ExecBase		*SysBase = (*((struct ExecBase **) 4));
	struct Library		*DOSBase;
	struct WBStartup	*wbMsg = NULL;
	struct Process		*process;
	struct RDArgs		*rd;

	LONG	*args;
	int	rc=1;
	int	i=0;

	process = (struct Process *) SysBase->ThisTask;
	if (!(process->pr_CLI)) {
		WaitPort (&process->pr_MsgPort);
		wbMsg = (struct WBStartup *) GetMsg (&process->pr_MsgPort);
	}

	if (SysBase->LibNode.lib_Version < 37) goto xit;

	DOSBase = OpenLibrary ("dos.library", 37);
	if(DOSBase) {
		args=AllocVec(NUM*4,MEMF_PUBLIC | MEMF_CLEAR);	/* Get array  */
		if(args) {
			rd=ReadArgs(TEMPLATE,args,NULL);	/* Parse args */
			PutStr(RESTORE);	/* RESTORE to default style   */
			for(;i<NUM;i++)		/* Scan args and print strings*/
				if(args[i]) {
					if(i<2)		/* Replace 'x' character in string with ASCII number */
						style[i][3]=*((LONG *)args[i])+48;
					PutStr(style[i]);
				}

			PutStr("\33[ s");	/* Set default style/SGR in WB3.0 */
						/*   which is harmless in WB2.0 */
			Flush(Output());	/* Flush the output to the CON: */
			FreeArgs(rd);		/* Free the readargs structure  */
			FreeVec(args);		/* Free memory for flag stuff   */
			rc=0;			/* return good */
		}
		CloseLibrary(DOSBase);		/* Close library 		*/
	}

xit:	if (wbMsg) {
		Forbid ();				/* Very important! */
		ReplyMsg ((struct Message *) wbMsg);	/* Must reply Workbench Msg */
	}
	process->pr_Result2=rc;
}
