#define	rsts	1
#include <stdio.h>
#include <rsts.h>

extern int	$$rsts;
extern int	$$opsy;
extern int	$$vms;
extern unsigned	$$mend;
extern int	$$tick;

#define	KWORDS	2048

#ifdef rt11
#define	OPSYS	7

#define	HIMEM	(*((int *) 050 ))
#define	RMON	(*((int *) 054 ))
#define	CONFIG	(*((int *) (RMON + 0300) ))
#define	CLOCK50	040

#else
#define	OPSYS	4
#endif
#define	BUFSIZ	128

static	char	err_buf[BUFSIZ];

$$init()
{
    $$opsy = OPSYS;	/* If = 4, RSTS EMTs are legal	*/

    $$rsts = 1;		/* We're RSTS	*/
    $$vms = 0;		/* And not VMS	*/

    stderr->io_flag = (IO_OPN | IO_REC | IO_TTY | 1);
    stderr->io_name = "_TT:";
    stderr->io_bptr =
    stderr->io_bbuf = err_buf;
    stderr->io_bsiz = BUFSIZ;

#ifdef rt11

    if  (CONFIG & CLOCK50)
        $$tick = 50;
    $$mend = HIMEM + 2;

#else

    clrfqb();
    firqb.fqfun = 26;
    firqb.fqsizm = 1;
    rstsys(_UUO);
    $$mend = firqb.fqsiz * KWORDS;

#endif

    $$args();

/*
 *    $$uic =
 *    $$task =
 */
}

extern int	$$argc;
extern char	**$$argv;

static
$$args()
{
    char	*cmd;
    char	*cmn;
    char	*limit;
    char	c;
    int		state;

    $$argc = corcmn[0];
    cmd = alloc($$argc + 1);
    $$argv = calloc($$argc + 1, sizeof (char *));
    cmn = corcmn + 1;
    limit = corcmn + $$argc;
    $$argc = 0;
    state = 0;
    while (cmn <= limit) {
        c = (*cmn++ & 0177);
        switch (state) {
            case 0:
                if (c <= ' ')
                    break;
                else {
                    $$argv[$$argc++] = cmd;
                    state = 1;
                }
            case 1:
                if (c > ' ') {
                    *cmd++ = tolower(c);
                }
                else {
                    *cmd++ = EOS;
                    state = 0;
                }
                break;
        }
    }
    *cmd++ = EOS;
}
