
#include <osbind.h>

#define actcol *((int *) 0x291el)
#define actlin *((int *) 0x2920l)

#define ESC '\033'
#define MAXLEN 120

getstr(s)
char *s;
{
    register len, pos;
    int col_1st, lin_1st, new_lin, i;
    register char t[MAXLEN];
    register unsigned char c_in, scan;
    long in;    

    col_1st=actcol;
    lin_1st=actlin;
    len=0;
    pos=0;
    *t='\0';
    Cconout(ESC);
    Cconout('v');                       /* wrap on */
    while (1)
    {
        in=Crawcin();
        c_in=((int) in) % 256;
        scan=((int) (in >> 16)) % 256;
        if (! c_in)
        {
            switch (scan)
            {
            case 0x4b:                  /* left arrow */
                if (pos > 0)
                    --pos;
                break;
            case 0x4d:                  /* right arrow */
                if (pos < len)
                    ++pos;
                break;
            case 0x48:                  /* up arrow */
                if (pos > 79)
                    pos-=80;
                break;
            case 0x50:                  /* down arrow */
                if (len-pos > 79)
                    pos+=80;
                break;
            case 0x47:                  /* home */
                pos=0;
                break;
            case 0x77:                  /* ctrl-home */
                pos=len;
                break;
            case 0x61:                  /* undo */
                pos=0;
                Cconout(ESC);
                Cconout('Y');
                Cconout(32+lin_1st+(pos+col_1st)/80);
                Cconout(32+(col_1st+pos)%80);
                while (len-- > 0)
                    Cconout(' ');
                len++;
                break;
            case 0x52:                  /* insert */
                if ((pos < len) && (len < MAXLEN))
                {
                    i=len-1;
                    while (i >= pos)
                    {
                        t[i+1]=t[i];
                        i--;
                    }
                    t[pos]=' ';
                    t[++len]='\0';
                    Cconws(t+pos);
                }
                break;                      
            }
        }
        else
        {
            if ((c_in >= 0x20) && ((in%0xffffffl) != 0x53007fl))
            {
                if ((len < MAXLEN) || (pos < len))
                {
                    t[pos++]=c_in;
                    Cconout(c_in);
                    if (pos > len)
                        ++len;
                }
                
            }             
            else
                switch (c_in)
            {
            case 0x8:                   /* backspace */
                if (!pos)
                    break;
                pos--;
                Cconout(ESC);
                Cconout('Y');
                Cconout(32+lin_1st+(pos+col_1st)/80);
                Cconout(32+(col_1st+pos)%80);
            case 0x7f:                  /* delete */
                if ((len > 0) && (pos < len))
                {
                    t[len]='\0';
                    i=pos;
                    while (t[i] != '\0')
                    {
                        t[i]=t[i+1];
                        i++;
                    }
                    Cconws(t+pos);
                    Cconout(' ');
                    len--;
                }
                break;
            case 0x14:                  /* ctrl-t */
                len=pos;
                Cconout(ESC);
                Cconout('K');
            case 0xd:                   /* return */
                t[len]='\0';
                strcpy(s,t);
                Cconout(ESC);
                Cconout('Y');
                Cconout(32+lin_1st+(len+col_1st)/80);
                Cconout(32+(col_1st+len)%80);
                Cconout('\r');
                Cconout('\n');
                return len;
            }
        }
        new_lin=lin_1st+(pos+col_1st)/80;
        if (new_lin>24)
        {
            lin_1st--;
            new_lin--;
        }
        Cconout(ESC);
        Cconout('Y');
        Cconout(32+new_lin);
        Cconout(32+(col_1st+pos)%80);
    }
}        
