Subversion Repositories svn1-original

Rev

Rev 46 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*************************************************************************
*           Copyright (C) 1995 Embedded Solutions
*                       All rights reserved
*
* file:     src\vduibm.c
*
* purpose:  This module contains all the VDU interface routines
*           The functions are implemented ( on the 3b2 ) through low level
*           calls to the curses package and the terminfo data base.
*
*           A small number of routines are provided. These can be emulated
*           on other machines to provide similar functions
*
*           A number of general purpose I/O routines are also provided
*
* functions
*       init_screen             - Initialize the screen
*       cur                     - Position the cursor on the screen
*       clearscreen             - Clears the vdu screen
*       beep                    - Generate a bell character
*       flush_out               - Flush terminal output
*       getinp                  - Get one character of input
*       getnum                  - Get a number from the operator
*       getstring               - Get and edit a string
*       getsex                  - Get a sex type from the operator
*       getclass                - Get a class descriptor
*       getcountry              - Get a country descriptor
*       getfnc                  - Get menu selector
*       getyes                  - Prompt for YES / NO
*       to_upper                - Convert character to upper case
*       to_lower                - Convert character to lower case
*       sleep                   - Wait a bit
*
* programmer: David Purdie
*
* revision  date        by      reason
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
*
**************************************************************************/

#include    <stdio.h>
#include    <ctype.h>

#include    "consts.h"
#include    "structs.h"
#include    "proto.h"


/* variables used by the routines */

int         n_lines;                             /* Number of lines on the screen */
int         n_cols;

int         rubout = '\010';                     /* users rubout character */
char        escape = '\033';                     /* Escape character */
int         abort_flag = FALSE;                  /* Abort character detected */
int         fnc_opr;                             /* Function key pushed */

#define Normal 7


/*========================================================================
 *
 *  Initialize the screen
 *
 *  Purpose:
 *      This function is called to Initialize the screen
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void init_screen(void)
{
    n_lines = 24;                                /* Fix number of lines for export */
    n_cols = 80;                                 /* Number of cols */
}


/*========================================================================
 *
 *  Restore screen to normal
 *
 *  Purpose:
 *      This function is called to Restore screen to normal
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void fix_screen(void)                          /* Restore screen to normal */
{
    flush_out();
}

/*========================================================================
 *
 *  Position the cursor on the screen
 *
 *  Purpose:
 *      This function is called to Position the cursor on the screen
 *
 *  Parameters:
 *      x               col number
 *      y               line number
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void cur( int x, int y )
{
    gotoxy( x + 1, y + 1 );
}

/*========================================================================
 *
 *  Clears the vdu screen 
 *
 *  Purpose:
 *      This function is called to Clears the vdu screen 
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void clearscreen(void)
{
    clrscr();
    cur( 0, 0 );                                 /* Cursor to top of the screen */
}

/*========================================================================
 *
 *  Generate a bell character
 *
 *  Purpose:
 *      This function is called to Generate a bell character
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void beep(void)
{
    putchar( '\007' );
}


/*========================================================================
 *
 *  Flush terminal output
 *
 *  Purpose:
 *      This function is called to Flush terminal output
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void flush_out(void)
{
    fflush( stdout );
}


/*========================================================================
 *
 *  Get one character of input
 *
 *  Purpose:
 *      This function is called to Get one character of input
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Ascii code or internal control code which will be greater than
 *      a byte
 *
 *========================================================================*/

int getinp(void)
{

    /*
     * General input routine
     * Will intercept function keys and set flags
     */

    int         c;

    c = bioskey( 0 );
    if( c & 0xff )
    {
        c = c & 0xff;
        if( c == '\r' )
            c = '\n';
        if( c == ABORT_CHAR || c == ABORT_CHAR_1 )
        {
            abort_flag = TRUE;
            c = ABORT_CHAR;
        }
        return ( c );                            /* Input is ascii char */
    }
    c >>= 8;                                     /* Upper byte */
    if( c >= 59 && c <= 68 )
    {
        fnc_opr = FNC1 + c - 59;
        return ( 0 );
    }
    switch ( c )
    {
    case 71:    return ( HOME_KEY );
    case 72:    return ( UPARROW );
    case 75:    return ( LEFTARROW );
    case 77:    return ( RIGHTARROW );
    case 80:    return ( DOWNARROW );
    case 73:    return ( PAGE_UP );
    case 81:    return ( PAGE_DOWN );
    case 79:    return ( END_KEY );
    case 82:    return ( INSERT_KEY );
    case 83:    return ( DELETE_KEY );
    case 15:    return ( BACKTAB );
    }
    return ( 0 );
}


/*========================================================================
 *
 *  Get a number from the operator
 *
 *  Purpose:
 *      This function is called to get a number from the operator
 *      
 *      This routine will accept numeric data entry from the operator
 *      the routine will limit the operator input to a limited number
 *      of characters. This allowes fixed format input
 *      
 *  Parameters:
 *      max_len         Max field width allowed
 *      final_buf       pointer to the final resting place of the number
 *
 *  Returns:
 *      This routine will return an indication of operator input
 *          0 - No valid entry
 *          1 - data entered and transfered to number.
 *
 *========================================================================*/

bool getnum( int max_len, int *final_buf )
{
    char        n_buf[30];                       /* Convert number here */

    n_buf[0] = '\0';
    if( getstring( max_len, n_buf, Digit ) )
    {
        sscanf( n_buf, "%d", final_buf );
        return ( TRUE );
    }
    else
        return ( FALSE );
}

/*========================================================================
 *
 *  Get and edit a string
 *
 *  Purpose:
 *      This function is called to Get and edit a string
 *
 *  Parameters:
 *      len                 Max length of the string
 *      str                 Address of the string
 *      limit               Types of chars allowed
 *
 *  Returns:
 *      Length of the input string
 *
 *========================================================================*/

int getstring( int len, char *str, enum class_enum limit )
{
    char        buf[100];                        /* Internal edit buffer */
    int         i;                               /* Current index into buf */
    int         ii;                              /* Number of characters in buf */
    int         j;                               /* Loooopy variable */
    int         ch;                              /* Current input character */

    ( void ) memset( buf, 0, 100 );              /* Init the buffer */
    ( void ) strcpy( buf, str );                 /* Copy in the original string */
    ii = i = strlen( str );                      /* i is at the end of the string */
    printf( "%s", buf );                         /* Display the current string */

    /*
     * This routine assumes that the cursor is at the end of
     * the string as it is displayed on the screen
     */

    while( !abort_flag )
    {
        ch = getinp();                           /* Get the next character */

        if( ch == '\n' || fnc_opr )              /* RETURN */
            break;                               /* Finish the edit */

        else if( ch == rubout && i )             /* Users rubout character */
        {
            i--;
            ( void ) putchar( '\010' );
            for( j = i; j <= ii; j++ )
                buf[j] = buf[j + 1];
            ( void ) printf( "%s ", &buf[i] );   /* Print remainder of the buffer */
            ii--;                                /* One less in the buffer */
            for( j = i; j <= ii; j++ )
                ( void ) putchar( '\010' );
        }

        else if( ch == DELETE_KEY && i != ii )   /* Delete char under cursor */
        {
            for( j = i; j <= ii; j++ )
                buf[j] = buf[j + 1];
            ( void ) printf( "%s ", &buf[i] );
            ii--;
            for( j = i; j <= ii; j++ )
                ( void ) putchar( '\010' );
        }

        else if( ( ch == '\010' || ch == LEFTARROW ) && i ) /* Back-space */
        {
            i--;
            ( void ) putchar( '\010' );
        }

        else if( ch == RIGHTARROW && i < ii )    /* forward */
            ( void ) putchar( buf[i++] );

        else if( ( ii < len ) && ( ( ( limit == Alphanum ) && isprint( ch ) ) || ( ( limit == Digit ) && isascii( ch ) && isdigit( ch ) ) || ( ( limit == Alpha ) && isalpha( ch ) ) ) )    /* Insert the char */
        {
            for( j = ii + 1; j > i; j-- )
                buf[j] = buf[j - 1];
            buf[i] = ch;
            ii++;
            ( void ) printf( "%s", &buf[i++] );  /* Print remainder of the buffer */
            for( j = i; j < ii; j++ )
                ( void ) putchar( '\010' );
        }
        else
            beep();
    }

    /*
     * Edit complete - transfer the data to the user 
     */

    ( void ) printf( "%s", &buf[i] );            /* Cursor to end of the buffer */
    if( !abort_flag )
        ( void ) strcpy( str, buf );             /* Copy buffer to the user */
    else
        ii = 0;
    return ( ii );                               /* Return length of input */
}

/*========================================================================
 *
 *  Get a sex type from the operator
 *
 *  Purpose:
 *      This function is called to Get a sex type from the operator
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      A sex type
 *      The unknown sex type is used to signal no change in state
 *
 *========================================================================*/

sex_type getsex(void)
{
    sex_type    curr = unknown;                  /* current sex type in display */
    char        c;

    while( ( c = getinp() ) != ABORT_CHAR )
    {
        if( c == '\n' )
        {
            return ( curr );
        }
        if( c == 'M' )
        {
            printf( "Male  \010\010\010\010\010\010" );
            curr = male;
        }
        else if( c == 'F' )
        {
            printf( "Female\010\010\010\010\010\010" );
            curr = female;
        }
        else
        {
            beep();
        }
    }
    return ( unknown );
}

/*========================================================================
 *
 *  Get a class descriptor
 *
 *  Purpose:
 *      This function is called to Get a class descriptor
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      An integer which is the index into the  config.team_class array
 *
 *========================================================================*/

int getclass(void)
{
    char        cc[3];                           /* Input character */
    int         i, j;                            /* One those loopy things */


    cc[0] = '\0';
    while( ( j = getstring( 2, cc, Alphanum ) ) != 0 )
    {

        /*
         * Attempt to locate the entered class in the list of defined classes 
         */

        for( i = 0; i < config.num_class; i++ )
        {
            if( toupper( cc[0] ) == config.team_class[i].abr[0] &&
                toupper( cc[1] ) == config.team_class[i].abr[1] )
                return ( ++i );
        }
        beep();
        for( i = 0; i < j; i++ )
            putchar( '\010' );
    }
    return ( 0 );
}

/*========================================================================
 *
 *  Get a country descriptor
 *
 *  Purpose:
 *      This function is called to Get a country descriptor
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      An integer which is the index into the config.country_name array.
 *
 *========================================================================*/

int getcountry(void)
{
    char        cc[5];                           /* Input character */
    int         i, j, k;                         /* One those loopy things */

    cc[0] = '\0';
    while( ( j = getstring( 4, cc, Alphanum ) ) != 0 )
    {

        /*
         * Attempt to locate the entered countryin the list of defined countries 
         */

        for( i = 0; i < MAX_COUNTRY; i++ )
        {
            for( k = 0; k <= j; k++ )
            {
                if( cc[k] == '\000' )
                    return ( ++i );              /* Found it */
                if( toupper( cc[k] ) !=
                    toupper( config.country_name[i].abr[k] ) )
                    break;
            }
        }
        beep();
        for( i = 0; i < j; i++ )
            putchar( '\010' );
    }
    return ( 0 );
}

/*========================================================================
 *
 *  Get menu selector
 *
 *  Purpose:
 *      This function is called to Get menu selector
 *
 *      This function will accept characters until one in
 *      "list" is encountered. This is returned to the user
 *      RETURN is valid if the first character of the list is not a "*"
 *      it will return the first charcater of the list;
 *      ABORT will return a zero
 *
 *  Parameters:
 *      List        A string of valid responses
 *
 *  Returns:
 *      Selected character
 *
 *========================================================================*/

char getfnc( char *list )
{
    char       *copylist;
    char        c;

    copylist = ( *list == '*' ) ? list + 1 : list;
    while( ( c = getinp() ) != ABORT_CHAR )
    {
        c = toupper( c );                        /* Get a character */
        if( strchr( copylist, c ) )
            return ( c );
        if( c == '\n' && *list != '*' )
            return ( *list );
        beep();
    }
    return ( '\000' );
}

/*========================================================================
 *
 *  Prompt for YES / NO
 *
 *  Purpose:
 *      This function is called to Prompt for YES / NO
 *
 *  Parameters:
 *      prompt      Prompt to display to the user
 *
 *  Returns:
 *      TRUE for Yes, FALSE for No
 *
 *========================================================================*/

bool getyes( char *prompt )
{
    char        c;

    printf( "%s ? ", prompt );
    c = getfnc( "*YN" );
    putchar( '\n' );
    return ( c == 'Y' );
}

/*========================================================================
 *
 *  Convert character to upper case
 *
 *  Purpose:
 *      This function is called to convert character to upper case
 *
 *  Parameters:
 *      ch      Character to convert
 *
 *  Returns:
 *      Converted character
 *
 *========================================================================*/

char to_upper( char ch )
{
    if( islower( ch ) )
        return ( ch + 'A' - 'a' );
    return ( ch );
}

/*========================================================================
 *
 *  Convert character to lower case
 *
 *  Purpose:
 *      This function is called to convert character to lower case
 *
 *  Parameters:
 *      ch      Character to convert
 *
 *  Returns:
 *      Converted character
 *
 *========================================================================*/

char to_lower( char ch )
{
    if( isupper( ch ) )
        return ( ch + 'a' - 'A' );
    return ( ch );
}


/*========================================================================
 *
 *  Wait a bit
 *
 *  Purpose:
 *      This function is called to idle and to nothing for a while
 *
 *  Parameters:
 *      t           Time to wait
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/

void sleep( int t )
{
    unsigned int i;

    for( ; t < 0; t-- )
        for( i = 0; i < 50000; i++ );
}


/*========================================================================
 *
 *  Clear to the end of the current display line
 *
 *  Purpose:
 *      Clears to the end of the line
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/
void console_clreol(void)
{
    clreol();
}


/*========================================================================
 *
 *  Display a prompt character in a highlight
 *
 *  Purpose:
 *      USed to highlight menu entries
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/
void console_prompt( char prompt )
{
    highvideo();
    putch( prompt );
    normvideo();
}

/********************************* EOF ***********************************/