Subversion Repositories svn1-original

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 root 1
/*************************************************************************
2
*           Copyright (C) 1995 Embedded Solutions
3
*                       All rights reserved
4
*
5
* file:     src\vduibm.c
6
*
7
* purpose:  This module contains all the VDU interface routines
8
*           The functions are implemented ( on the 3b2 ) through low level
9
*           calls to the curses package and the terminfo data base.
10
*
11
*           A small number of routines are provided. These can be emulated
12
*           on other machines to provide similar functions
13
*
14
*           A number of general purpose I/O routines are also provided
15
*
16
* functions
17
*       init_screen             - Initialize the screen
18
*       cur                     - Position the cursor on the screen
19
*       clearscreen             - Clears the vdu screen
20
*       beep                    - Generate a bell character
21
*       flush_out               - Flush terminal output
22
*       getinp                  - Get one character of input
23
*       getnum                  - Get a number from the operator
24
*       getstring               - Get and edit a string
25
*       getsex                  - Get a sex type from the operator
26
*       getclass                - Get a class descriptor
27
*       getcountry              - Get a country descriptor
28
*       getfnc                  - Get menu selector
29
*       getyes                  - Prompt for YES / NO
30
*       to_upper                - Convert character to upper case
31
*       to_lower                - Convert character to lower case
32
*       sleep                   - Wait a bit
33
*
34
* programmer: David Purdie
35
*
36
* revision  date        by      reason
37
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
38
*
39
**************************************************************************/
40
 
41
#include    <stdio.h>
42
#include    <ctype.h>
43
 
44
#include    "consts.h"
45
#include    "structs.h"
46
#include    "proto.h"
47
 
48
 
49
/* variables used by the routines */
50
 
51
int         n_lines;                             /* Number of lines on the screen */
52
int         n_cols;
53
 
54
int         rubout = '\010';                     /* users rubout character */
55
char        escape = '\033';                     /* Escape character */
56
int         abort_flag = FALSE;                  /* Abort character detected */
57
int         fnc_opr;                             /* Function key pushed */
58
 
59
#define Normal 7
60
 
61
 
62
/*========================================================================
63
 *
64
 *  Initialize the screen
65
 *
66
 *  Purpose:
67
 *      This function is called to Initialize the screen
68
 *
69
 *  Parameters:
70
 *      None
71
 *
72
 *  Returns:
73
 *      Nothing
74
 *
75
 *========================================================================*/
76
 
77
void init_screen(void)
78
{
79
    n_lines = 24;                                /* Fix number of lines for export */
80
    n_cols = 80;                                 /* Number of cols */
81
}
82
 
83
 
84
/*========================================================================
85
 *
86
 *  Restore screen to normal
87
 *
88
 *  Purpose:
89
 *      This function is called to Restore screen to normal
90
 *
91
 *  Parameters:
92
 *      None
93
 *
94
 *  Returns:
95
 *      Nothing
96
 *
97
 *========================================================================*/
98
 
99
void fix_screen(void)                          /* Restore screen to normal */
100
{
101
    flush_out();
102
}
103
 
104
/*========================================================================
105
 *
106
 *  Position the cursor on the screen
107
 *
108
 *  Purpose:
109
 *      This function is called to Position the cursor on the screen
110
 *
111
 *  Parameters:
112
 *      x               col number
113
 *      y               line number
114
 *
115
 *  Returns:
116
 *      Nothing
117
 *
118
 *========================================================================*/
119
 
120
void cur( int x, int y )
121
{
122
    gotoxy( x + 1, y + 1 );
123
}
124
 
125
/*========================================================================
126
 *
127
 *  Clears the vdu screen 
128
 *
129
 *  Purpose:
130
 *      This function is called to Clears the vdu screen 
131
 *
132
 *  Parameters:
133
 *      None
134
 *
135
 *  Returns:
136
 *      Nothing
137
 *
138
 *========================================================================*/
139
 
140
void clearscreen(void)
141
{
142
    clrscr();
143
    cur( 0, 0 );                                 /* Cursor to top of the screen */
144
}
145
 
146
/*========================================================================
147
 *
148
 *  Generate a bell character
149
 *
150
 *  Purpose:
151
 *      This function is called to Generate a bell character
152
 *
153
 *  Parameters:
154
 *      None
155
 *
156
 *  Returns:
157
 *      Nothing
158
 *
159
 *========================================================================*/
160
 
161
void beep(void)
162
{
163
    putchar( '\007' );
164
}
165
 
166
 
167
/*========================================================================
168
 *
169
 *  Flush terminal output
170
 *
171
 *  Purpose:
172
 *      This function is called to Flush terminal output
173
 *
174
 *  Parameters:
175
 *      None
176
 *
177
 *  Returns:
178
 *      Nothing
179
 *
180
 *========================================================================*/
181
 
182
void flush_out(void)
183
{
184
    fflush( stdout );
185
}
186
 
187
 
188
/*========================================================================
189
 *
190
 *  Get one character of input
191
 *
192
 *  Purpose:
193
 *      This function is called to Get one character of input
194
 *
195
 *  Parameters:
196
 *      None
197
 *
198
 *  Returns:
199
 *      Ascii code or internal control code which will be greater than
200
 *      a byte
201
 *
202
 *========================================================================*/
203
 
204
int getinp(void)
205
{
206
 
207
    /*
208
     * General input routine
209
     * Will intercept function keys and set flags
210
     */
211
 
212
    int         c;
213
 
214
    c = bioskey( 0 );
215
    if( c & 0xff )
216
    {
217
        c = c & 0xff;
218
        if( c == '\r' )
219
            c = '\n';
220
        if( c == ABORT_CHAR || c == ABORT_CHAR_1 )
221
        {
222
            abort_flag = TRUE;
223
            c = ABORT_CHAR;
224
        }
225
        return ( c );                            /* Input is ascii char */
226
    }
227
    c >>= 8;                                     /* Upper byte */
228
    if( c >= 59 && c <= 68 )
229
    {
230
        fnc_opr = FNC1 + c - 59;
231
        return ( 0 );
232
    }
233
    switch ( c )
234
    {
235
    case 71:    return ( HOME_KEY );
236
    case 72:    return ( UPARROW );
237
    case 75:    return ( LEFTARROW );
238
    case 77:    return ( RIGHTARROW );
239
    case 80:    return ( DOWNARROW );
240
    case 73:    return ( PAGE_UP );
241
    case 81:    return ( PAGE_DOWN );
242
    case 79:    return ( END_KEY );
243
    case 82:    return ( INSERT_KEY );
244
    case 83:    return ( DELETE_KEY );
245
    case 15:    return ( BACKTAB );
246
    }
247
    return ( 0 );
248
}
249
 
250
 
251
/*========================================================================
252
 *
253
 *  Get a number from the operator
254
 *
255
 *  Purpose:
256
 *      This function is called to get a number from the operator
257
 *      
258
 *      This routine will accept numeric data entry from the operator
259
 *      the routine will limit the operator input to a limited number
260
 *      of characters. This allowes fixed format input
261
 *      
262
 *  Parameters:
263
 *      max_len         Max field width allowed
264
 *      final_buf       pointer to the final resting place of the number
265
 *
266
 *  Returns:
267
 *      This routine will return an indication of operator input
268
 *          0 - No valid entry
269
 *          1 - data entered and transfered to number.
270
 *
271
 *========================================================================*/
272
 
273
bool getnum( int max_len, int *final_buf )
274
{
275
    char        n_buf[30];                       /* Convert number here */
276
 
277
    n_buf[0] = '\0';
278
    if( getstring( max_len, n_buf, Digit ) )
279
    {
280
        sscanf( n_buf, "%d", final_buf );
281
        return ( TRUE );
282
    }
283
    else
284
        return ( FALSE );
285
}
286
 
287
/*========================================================================
288
 *
289
 *  Get and edit a string
290
 *
291
 *  Purpose:
292
 *      This function is called to Get and edit a string
293
 *
294
 *  Parameters:
295
 *      len                 Max length of the string
296
 *      str                 Address of the string
297
 *      limit               Types of chars allowed
298
 *
299
 *  Returns:
300
 *      Length of the input string
301
 *
302
 *========================================================================*/
303
 
304
int getstring( int len, char *str, enum class_enum limit )
305
{
306
    char        buf[100];                        /* Internal edit buffer */
307
    int         i;                               /* Current index into buf */
308
    int         ii;                              /* Number of characters in buf */
309
    int         j;                               /* Loooopy variable */
310
    int         ch;                              /* Current input character */
311
 
312
    ( void ) memset( buf, 0, 100 );              /* Init the buffer */
313
    ( void ) strcpy( buf, str );                 /* Copy in the original string */
314
    ii = i = strlen( str );                      /* i is at the end of the string */
315
    printf( "%s", buf );                         /* Display the current string */
316
 
317
    /*
318
     * This routine assumes that the cursor is at the end of
319
     * the string as it is displayed on the screen
320
     */
321
 
322
    while( !abort_flag )
323
    {
324
        ch = getinp();                           /* Get the next character */
325
 
326
        if( ch == '\n' || fnc_opr )              /* RETURN */
327
            break;                               /* Finish the edit */
328
 
329
        else if( ch == rubout && i )             /* Users rubout character */
330
        {
331
            i--;
332
            ( void ) putchar( '\010' );
333
            for( j = i; j <= ii; j++ )
334
                buf[j] = buf[j + 1];
335
            ( void ) printf( "%s ", &buf[i] );   /* Print remainder of the buffer */
336
            ii--;                                /* One less in the buffer */
337
            for( j = i; j <= ii; j++ )
338
                ( void ) putchar( '\010' );
339
        }
340
 
341
        else if( ch == DELETE_KEY && i != ii )   /* Delete char under cursor */
342
        {
343
            for( j = i; j <= ii; j++ )
344
                buf[j] = buf[j + 1];
345
            ( void ) printf( "%s ", &buf[i] );
346
            ii--;
347
            for( j = i; j <= ii; j++ )
348
                ( void ) putchar( '\010' );
349
        }
350
 
351
        else if( ( ch == '\010' || ch == LEFTARROW ) && i ) /* Back-space */
352
        {
353
            i--;
354
            ( void ) putchar( '\010' );
355
        }
356
 
357
        else if( ch == RIGHTARROW && i < ii )    /* forward */
358
            ( void ) putchar( buf[i++] );
359
 
360
        else if( ( ii < len ) && ( ( ( limit == Alphanum ) && isprint( ch ) ) || ( ( limit == Digit ) && isascii( ch ) && isdigit( ch ) ) || ( ( limit == Alpha ) && isalpha( ch ) ) ) )    /* Insert the char */
361
        {
362
            for( j = ii + 1; j > i; j-- )
363
                buf[j] = buf[j - 1];
364
            buf[i] = ch;
365
            ii++;
366
            ( void ) printf( "%s", &buf[i++] );  /* Print remainder of the buffer */
367
            for( j = i; j < ii; j++ )
368
                ( void ) putchar( '\010' );
369
        }
370
        else
371
            beep();
372
    }
373
 
374
    /*
375
     * Edit complete - transfer the data to the user 
376
     */
377
 
378
    ( void ) printf( "%s", &buf[i] );            /* Cursor to end of the buffer */
379
    if( !abort_flag )
380
        ( void ) strcpy( str, buf );             /* Copy buffer to the user */
381
    else
382
        ii = 0;
383
    return ( ii );                               /* Return length of input */
384
}
385
 
386
/*========================================================================
387
 *
388
 *  Get a sex type from the operator
389
 *
390
 *  Purpose:
391
 *      This function is called to Get a sex type from the operator
392
 *
393
 *  Parameters:
394
 *      None
395
 *
396
 *  Returns:
397
 *      A sex type
398
 *      The unknown sex type is used to signal no change in state
399
 *
400
 *========================================================================*/
401
 
402
sex_type getsex(void)
403
{
404
    sex_type    curr = unknown;                  /* current sex type in display */
405
    char        c;
406
 
407
    while( ( c = getinp() ) != ABORT_CHAR )
408
    {
409
        if( c == '\n' )
410
        {
411
            return ( curr );
412
        }
413
        if( c == 'M' )
414
        {
415
            printf( "Male  \010\010\010\010\010\010" );
416
            curr = male;
417
        }
418
        else if( c == 'F' )
419
        {
420
            printf( "Female\010\010\010\010\010\010" );
421
            curr = female;
422
        }
423
        else
424
        {
425
            beep();
426
        }
427
    }
428
    return ( unknown );
429
}
430
 
431
/*========================================================================
432
 *
433
 *  Get a class descriptor
434
 *
435
 *  Purpose:
436
 *      This function is called to Get a class descriptor
437
 *
438
 *  Parameters:
439
 *      None
440
 *
441
 *  Returns:
442
 *      An integer which is the index into the  config.team_class array
443
 *
444
 *========================================================================*/
445
 
446
int getclass(void)
447
{
448
    char        cc[3];                           /* Input character */
449
    int         i, j;                            /* One those loopy things */
450
 
451
 
452
    cc[0] = '\0';
453
    while( ( j = getstring( 2, cc, Alphanum ) ) != 0 )
454
    {
455
 
456
        /*
457
         * Attempt to locate the entered class in the list of defined classes 
458
         */
459
 
460
        for( i = 0; i < config.num_class; i++ )
461
        {
462
            if( toupper( cc[0] ) == config.team_class[i].abr[0] &&
463
                toupper( cc[1] ) == config.team_class[i].abr[1] )
464
                return ( ++i );
465
        }
466
        beep();
467
        for( i = 0; i < j; i++ )
468
            putchar( '\010' );
469
    }
470
    return ( 0 );
471
}
472
 
473
/*========================================================================
474
 *
475
 *  Get a country descriptor
476
 *
477
 *  Purpose:
478
 *      This function is called to Get a country descriptor
479
 *
480
 *  Parameters:
481
 *      None
482
 *
483
 *  Returns:
484
 *      An integer which is the index into the config.country_name array.
485
 *
486
 *========================================================================*/
487
 
488
int getcountry(void)
489
{
490
    char        cc[5];                           /* Input character */
491
    int         i, j, k;                         /* One those loopy things */
492
 
493
    cc[0] = '\0';
494
    while( ( j = getstring( 4, cc, Alphanum ) ) != 0 )
495
    {
496
 
497
        /*
498
         * Attempt to locate the entered countryin the list of defined countries 
499
         */
500
 
501
        for( i = 0; i < MAX_COUNTRY; i++ )
502
        {
503
            for( k = 0; k <= j; k++ )
504
            {
505
                if( cc[k] == '\000' )
506
                    return ( ++i );              /* Found it */
507
                if( toupper( cc[k] ) !=
508
                    toupper( config.country_name[i].abr[k] ) )
509
                    break;
510
            }
511
        }
512
        beep();
513
        for( i = 0; i < j; i++ )
514
            putchar( '\010' );
515
    }
516
    return ( 0 );
517
}
518
 
519
/*========================================================================
520
 *
521
 *  Get menu selector
522
 *
523
 *  Purpose:
524
 *      This function is called to Get menu selector
525
 *
526
 *      This function will accept characters until one in
527
 *      "list" is encountered. This is returned to the user
528
 *      RETURN is valid if the first character of the list is not a "*"
529
 *      it will return the first charcater of the list;
530
 *      ABORT will return a zero
531
 *
532
 *  Parameters:
533
 *      List        A string of valid responses
534
 *
535
 *  Returns:
536
 *      Selected character
537
 *
538
 *========================================================================*/
539
 
540
char getfnc( char *list )
541
{
542
    char       *copylist;
543
    char        c;
544
 
545
    copylist = ( *list == '*' ) ? list + 1 : list;
546
    while( ( c = getinp() ) != ABORT_CHAR )
547
    {
548
        c = toupper( c );                        /* Get a character */
549
        if( strchr( copylist, c ) )
550
            return ( c );
551
        if( c == '\n' && *list != '*' )
552
            return ( *list );
553
        beep();
554
    }
555
    return ( '\000' );
556
}
557
 
558
/*========================================================================
559
 *
560
 *  Prompt for YES / NO
561
 *
562
 *  Purpose:
563
 *      This function is called to Prompt for YES / NO
564
 *
565
 *  Parameters:
566
 *      prompt      Prompt to display to the user
567
 *
568
 *  Returns:
569
 *      TRUE for Yes, FALSE for No
570
 *
571
 *========================================================================*/
572
 
573
bool getyes( char *prompt )
574
{
575
    char        c;
576
 
577
    printf( "%s ? ", prompt );
578
    c = getfnc( "*YN" );
579
    putchar( '\n' );
580
    return ( c == 'Y' );
581
}
582
 
583
/*========================================================================
584
 *
585
 *  Convert character to upper case
586
 *
587
 *  Purpose:
588
 *      This function is called to convert character to upper case
589
 *
590
 *  Parameters:
591
 *      ch      Character to convert
592
 *
593
 *  Returns:
594
 *      Converted character
595
 *
596
 *========================================================================*/
597
 
598
char to_upper( char ch )
599
{
600
    if( islower( ch ) )
601
        return ( ch + 'A' - 'a' );
602
    return ( ch );
603
}
604
 
605
/*========================================================================
606
 *
607
 *  Convert character to lower case
608
 *
609
 *  Purpose:
610
 *      This function is called to convert character to lower case
611
 *
612
 *  Parameters:
613
 *      ch      Character to convert
614
 *
615
 *  Returns:
616
 *      Converted character
617
 *
618
 *========================================================================*/
619
 
620
char to_lower( char ch )
621
{
622
    if( isupper( ch ) )
623
        return ( ch + 'a' - 'A' );
624
    return ( ch );
625
}
626
 
627
 
628
/*========================================================================
629
 *
630
 *  Wait a bit
631
 *
632
 *  Purpose:
633
 *      This function is called to idle and to nothing for a while
634
 *
635
 *  Parameters:
636
 *      t           Time to wait
637
 *
638
 *  Returns:
639
 *      Nothing
640
 *
641
 *========================================================================*/
642
 
643
void sleep( int t )
644
{
645
    unsigned int i;
646
 
647
    for( ; t < 0; t-- )
648
        for( i = 0; i < 50000; i++ );
649
}
650
 
651
 
652
/*========================================================================
653
 *
654
 *  Clear to the end of the current display line
655
 *
656
 *  Purpose:
657
 *      Clears to the end of the line
658
 *
659
 *  Parameters:
660
 *      None
661
 *
662
 *  Returns:
663
 *      Nothing
664
 *
665
 *========================================================================*/
666
void console_clreol(void)
667
{
668
    clreol();
669
}
670
 
671
 
672
/*========================================================================
673
 *
674
 *  Display a prompt character in a highlight
675
 *
676
 *  Purpose:
677
 *      USed to highlight menu entries
678
 *
679
 *  Parameters:
680
 *      None
681
 *
682
 *  Returns:
683
 *      Nothing
684
 *
685
 *========================================================================*/
686
void console_prompt( char prompt )
687
{
688
    highvideo();
689
    putch( prompt );
690
    normvideo();
691
}
692
 
693
/********************************* EOF ***********************************/