Subversion Repositories svn1

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
91 - 1
/*************************************************************************
2
*           Copyright (C) 1995 Embedded Solutions
3
*                       All rights reserved
4
*
5
* file:     src\vduW32.c
6
*
7
* purpose:  This module contains all the VDU interface routines
8
*
9
*           This instance of this file implements the functions
10
*           in a Win32 platform.
11
*
12
*           A small number of routines are provided. These can be emulated
13
*           on other machines to provide similar functions
14
*
15
*           A number of general purpose I/O routines are also provided
16
*
17
* functions
18
*       init_screen             - Initialize the screen
19
*       cur                     - Position the cursor on the screen
20
*       clearscreen             - Clears the vdu screen
21
*       beep                    - Generate a bell character
22
*       flush_out               - Flush terminal output
23
*       getinp                  - Get one character of input
24
*       getnum                  - Get a number from the operator
25
*       getstring               - Get and edit a string
26
*       getsex                  - Get a sex type from the operator
27
*       getclass                - Get a class descriptor
28
*       getcountry              - Get a country descriptor
29
*       getfnc                  - Get menu selector
30
*       getyes                  - Prompt for YES / NO
31
*       to_upper                - Convert character to upper case
32
*       to_lower                - Convert character to lower case
33
*       sleep                   - Wait a bit
34
*       printf                  - Text display routines
35
*       putchar                 - Text display routines
36
*       console_clreol          - Clear to end of the line
37
*       console_prompt          - Display a hightlighted prompt
38
*
39
* programmer: David Purdie
40
*
41
* revision  date        by      reason
42
*   00.0    17/09/2002  DDP     Created
43
*
44
**************************************************************************/
45
 
46
#include    <stdio.h>
47
#include    <ctype.h>
48
#include    <windows.h>
49
#include    <wincon.h>
50
 
51
#include    "consts.h"
52
#include    "structs.h"
53
#include    "proto.h"
54
 
55
/* variables used by the routines */
56
 
57
int         n_lines;                             /* Number of lines on the screen */
58
int         n_cols;
59
 
60
int         rubout = '\010';                     /* users rubout character */
61
char        escape = '\033';                     /* Escape character */
62
int         abort_flag = FALSE;                  /* Abort character detected */
63
int         fnc_opr;                             /* Function key pushed */
64
char        *force_cmds = NULL;                  /* Initial commnads */
65
 
66
 
67
HANDLE      h_console;                          /* Output handle */
68
 
69
/*
70
**  Prototypes
71
*/
72
int get_kb_character( void );
73
 
74
/*----------------------------------------------------------------------------
75
** FUNCTION           : set_commands
76
**
77
** DESCRIPTION        :
78
**
79
**
80
** INPUTS             : cptr            - Address of commands to execute
81
**
82
** RETURNS            : Nothing
83
**
84
** PRECONDITION       : 
85
**
86
** EXCEPTIONS         :
87
**
88
** WARNING            :
89
**
90
** SEE ALSO           :
91
----------------------------------------------------------------------------*/
92
 
93
void set_commands ( char * cptr )
94
{
95
    force_cmds = cptr;
96
}
97
 
98
 
99
/*========================================================================
100
 *
101
 *  Initialize the screen
102
 *
103
 *  Purpose:
104
 *      This function is called to Initialize the screen
105
 *
106
 *  Parameters:
107
 *      None
108
 *
109
 *  Returns:
110
 *      Nothing
111
 *
112
 *========================================================================*/
113
 
114
void init_screen( void )
115
{
94 - 116
    clearscreen();
91 - 117
    /*
118
    **  Operate within a virtual console area of 24 * 80
119
    **  Ignore the real console area
120
    */
121
    n_lines = 24;                                /* Fix number of lines for export */
122
    n_cols = 80;                                 /* Number of cols */
123
}
124
 
125
 
126
/*========================================================================
127
 *
128
 *  Restore screen to normal
129
 *
130
 *  Purpose:
131
 *      This function is called to Restore screen to normal
132
 *
133
 *  Parameters:
134
 *      None
135
 *
136
 *  Returns:
137
 *      Nothing
138
 *
139
 *========================================================================*/
140
 
141
void fix_screen( void )                          /* Restore screen to normal */
142
{
94 - 143
    //flush_out();
91 - 144
}
145
 
146
 
147
/*========================================================================
148
 *
149
 *  Position the cursor on the screen
150
 *
151
 *  Purpose:
152
 *      This function is called to save the current cursor location
153
 *      A single level save - for debug
154
 *
155
 *  Parameters:
156
 *      None
157
 *
158
 *  Returns:
159
 *      Nothing
160
 *
161
 *========================================================================*/
162
int save_x,save_y;
163
void save_cur(void)
164
{
165
 
166
    CONSOLE_SCREEN_BUFFER_INFO console_info;
167
 
168
    /*
169
    **  Determine the screen size and the current location of the
170
    **  cursor within the screen
171
    */
172
    GetConsoleScreenBufferInfo( h_console, &console_info );
173
    save_x = console_info.dwCursorPosition.X;
174
    save_y = console_info.dwCursorPosition.Y;
175
}
176
 
177
void restore_cur(void)
178
{
179
    cur( save_x, save_y);
180
}
181
 
182
 
183
 
184
 
185
/*========================================================================
186
 *
187
 *  Generate a bell character
188
 *
189
 *  Purpose:
190
 *      This function is called to Generate a bell character
191
 *
192
 *  Parameters:
193
 *      None
194
 *
195
 *  Returns:
196
 *      Nothing
197
 *
198
 *========================================================================*/
199
 
200
void beep( void )
201
{
202
    putchar( '\007' );
203
}
204
 
205
 
206
/*========================================================================
207
 *
208
 *  Flush terminal output
209
 *
210
 *  Purpose:
211
 *      This function is called to Flush terminal output
212
 *
213
 *  Parameters:
214
 *      None
215
 *
216
 *  Returns:
217
 *      Nothing
218
 *
219
 *========================================================================*/
220
 
221
void flush_out( void )
222
{
223
}
224
 
225
 
226
/*========================================================================
227
 *
228
 *  Get one character of input
229
 *
230
 *  Purpose:
231
 *      This function is called to Get one character of input
232
 *
233
 *  Parameters:
234
 *      None
235
 *
236
 *  Returns:
237
 *      Ascii code or internal control code which will be greater than
238
 *      a byte
239
 *
240
 *========================================================================*/
241
 
242
int getinp( void )
243
{
244
 
245
    /*
246
     * General input routine
247
     * Will intercept function keys and set flags
248
     */
249
 
250
    int         c;
251
 
252
    if ( force_cmds && *force_cmds )
253
    {
254
        c = *force_cmds++;
255
    }
256
    else
257
    {
258
        c = get_kb_character();
259
    }
260
    if( c & 0xff )
261
    {
262
        c = c & 0xff;
263
        if( c == '\r' )
264
            c = '\n';
265
        if( c == ABORT_CHAR || c == ABORT_CHAR_1 )
266
        {
267
            abort_flag = TRUE;
268
            c = ABORT_CHAR;
269
        }
270
        return ( c );                            /* Input is ascii char */
271
    }
272
    c >>= 8;                                     /* Upper byte */
273
    if( c >= 59 && c <= 68 )
274
    {
275
        fnc_opr = FNC1 + c - 59;
276
        return ( 0 );
277
    }
278
    switch ( c )
279
    {
280
    case 71:    return ( HOME_KEY );
281
    case 72:    return ( UPARROW );
282
    case 75:    return ( LEFTARROW );
283
    case 77:    return ( RIGHTARROW );
284
    case 80:    return ( DOWNARROW );
285
    case 73:    return ( PAGE_UP );
286
    case 81:    return ( PAGE_DOWN );
287
    case 79:    return ( END_KEY );
288
    case 82:    return ( INSERT_KEY );
289
    case 83:    return ( DELETE_KEY );
290
    case 15:    return ( BACKTAB );
291
    }
292
    return ( 0 );
293
}
294
 
295
 
296
/*========================================================================
297
 *
298
 *  Get a number from the operator
299
 *
300
 *  Purpose:
301
 *      This function is called to get a number from the operator
302
 *      
303
 *      This routine will accept numeric data entry from the operator
304
 *      the routine will limit the operator input to a limited number
305
 *      of characters. This allowes fixed format input
306
 *      
307
 *  Parameters:
308
 *      max_len         Max field width allowed
309
 *      final_buf       pointer to the final resting place of the number
310
 *
311
 *  Returns:
312
 *      This routine will return an indication of operator input
313
 *          0 - No valid entry
314
 *          1 - data entered and transfered to number.
315
 *
316
 *========================================================================*/
317
 
318
bool getnum( int max_len, int *final_buf )
319
{
320
    char        n_buf[30];                       /* Convert number here */
321
 
322
    n_buf[0] = '\0';
323
    if ( *final_buf )
324
        sprintf( n_buf, "%*.*d", max_len,max_len, *final_buf );
325
    if( getstring( max_len, n_buf, Digit ) )
326
    {
327
        sscanf( n_buf, "%d", final_buf );
328
        return ( TRUE );
329
    }
330
    else
331
        return ( FALSE );
332
}
333
 
334
/*========================================================================
335
 *
336
 *  Get and edit a string
337
 *
338
 *  Purpose:
339
 *      This function is called to Get and edit a string
340
 *
341
 *  Parameters:
342
 *      len                 Max length of the string
343
 *      str                 Address of the string
344
 *      limit               Types of chars allowed
345
 *
346
 *  Returns:
347
 *      Length of the input string
348
 *
349
 *========================================================================*/
350
 
351
int getstring( int len, const char *str, enum class_enum limit )
352
{
353
    char        buf[100];                        /* Internal edit buffer */
354
    int         i;                               /* Current index into buf */
355
    int         ii;                              /* Number of characters in buf */
356
    int         j;                               /* Loooopy variable */
357
    int         ch;                              /* Current input character */
358
 
359
    ( void ) memset( buf, 0, 100 );              /* Init the buffer */
360
    ( void ) strcpy( buf, str );                 /* Copy in the original string */
361
    ii = i = strlen( str );                      /* i is at the end of the string */
362
    printf( "%s", buf );                         /* Display the current string */
363
 
364
    /*
365
     * This routine assumes that the cursor is at the end of
366
     * the string as it is displayed on the screen
367
     */
368
 
369
    while( !abort_flag )
370
    {
371
        ch = getinp();                           /* Get the next character */
372
 
373
        if( ch == '\n' || ch == '\t' || fnc_opr || abort_flag )/* RETURN */
374
            break;                               /* Finish the edit */
375
 
376
        else if( ch == rubout && i )             /* Users rubout character */
377
        {
378
            i--;
379
            putchar( '\010' );
380
            for( j = i; j <= ii; j++ )
381
                buf[j] = buf[j + 1];
382
            printf( "%s ", &buf[i] );            /* Print remainder of the buffer */
383
            ii--;                                /* One less in the buffer */
384
            for( j = i; j <= ii; j++ )
385
                putchar( '\010' );
386
        }
387
 
388
        else if( ch == DELETE_KEY && i != ii )   /* Delete char under cursor */
389
        {
390
            for( j = i; j <= ii; j++ )
391
                buf[j] = buf[j + 1];
392
            printf( "%s ", &buf[i] );
393
            ii--;
394
            for( j = i; j <= ii; j++ )
395
                putchar( '\010' );
396
        }
397
 
398
        else if( ( ch == '\010' || ch == LEFTARROW ) && i ) /* Back-space */
399
        {
400
            i--;
401
            putchar( '\010' );
402
        }
403
 
404
        else if( ch == RIGHTARROW && i < ii )    /* forward */
405
            putchar( buf[i++] );
406
 
407
        else if( ( ii < len ) && ( ( ( limit == Alphanum  || limit == AlphnumUpper) && isprint( ch ) ) || ( ( limit == Digit ) && isascii( ch ) && isdigit( ch ) ) || ( ( limit == Alpha ) && isalpha( ch ) ) ) )    /* Insert the char */
408
        {
409
            if ( limit == AlphnumUpper )
410
                ch = toupper(ch);
411
 
412
            for( j = ii + 1; j > i; j-- )
413
                buf[j] = buf[j - 1];
414
            buf[i] = ch;
415
            ii++;
416
            printf( "%s", &buf[i++] );  /* Print remainder of the buffer */
417
            for( j = i; j < ii; j++ )
418
                putchar( '\010' );
419
        }
420
        else
421
        {
422
            beep();
423
        }
424
    }
425
 
426
    /*
427
     * Edit complete - transfer the data to the user 
428
     */
429
 
430
    printf( "%s", &buf[i] );            /* Cursor to end of the buffer */
431
    if( !abort_flag )
432
        strcpy( (char *)str, buf );             /* Copy buffer to the user */
433
    else
434
        ii = 0;
435
    return ( ii );                               /* Return length of input */
436
}
437
 
438
/*========================================================================
439
 *
440
 *  Get a sex type from the operator
441
 *
442
 *  Purpose:
443
 *      This function is called to Get a sex type from the operator
444
 *
445
 *  Parameters:
446
 *      None
447
 *
448
 *  Returns:
449
 *      A sex type
450
 *      The unknown sex type is used to signal no change in state
451
 *
452
 *========================================================================*/
453
 
454
sex_type getsex( void )
455
{
456
    sex_type    curr = unknown;                  /* current sex type in display */
457
    char        c;
458
 
459
    while( ( c = getinp() ) != ABORT_CHAR )
460
    {
461
        if( c == '\n' || c == '\t' )
462
        {
463
            return ( curr );
464
        }
465
        if( c == 'M' || c == 'm')
466
        {
467
            printf( "Male  \010\010\010\010\010\010" );
468
            curr = male;
469
        }
470
        else if( c == 'F' || c == 'f' )
471
        {
472
            printf( "Female\010\010\010\010\010\010" );
473
            curr = female;
474
        }
475
        else
476
        {
477
            beep();
478
        }
479
    }
480
    return ( unknown );
481
}
482
 
483
/*========================================================================
484
 *
485
 *  Get a class descriptor
486
 *
487
 *  Purpose:
488
 *      This function is called to Get a class descriptor
489
 *
490
 *  Parameters:
491
 *      None
492
 *
493
 *  Returns:
494
 *      An integer which is the index into the  config.team_class array
495
 *
496
 *========================================================================*/
497
 
498
int getclass( void )
499
{
500
    char        cc[3];                           /* Input character */
501
    int         i, j;                            /* One those loopy things */
502
 
503
 
504
    cc[0] = '\0';
505
    while( ( j = getstring( 2, cc, AlphnumUpper ) ) != 0 )
506
    {
507
        /*
508
        **  Is this a valid class
509
        */
510
        if ( (i = lookup_class(cc, NULL)) > 0 )
511
            return (i);
512
 
513
        /*
514
        **  Invalid
515
        **  Rubout and try again
516
        */
517
        beep();
518
        for( i = 0; i < j; i++ )
519
            putchar( '\010' );
520
    }
521
    return ( 0 );
522
}
523
 
524
/*========================================================================
525
 *
526
 *  Get a country descriptor
527
 *
528
 *  Purpose:
529
 *      This function is called to Get a country descriptor
530
 *
531
 *  Parameters:
532
 *      None
533
 *
534
 *  Returns:
535
 *      An integer which is the index into the config.country_name array.
536
 *
537
 *========================================================================*/
538
 
539
int getcountry( void )
540
{
541
    char        cc[5];                           /* Input character */
542
    int         i, j, k;                         /* One those loopy things */
543
 
544
    cc[0] = '\0';
545
    while( ( j = getstring( 4, cc, Alphanum ) ) != 0 )
546
    {
547
 
548
        /*
549
         * Attempt to locate the entered country in the list of defined countries
550
         */
551
 
552
        for( i = 0; i < MAX_COUNTRY; i++ )
553
        {
554
            for( k = 0; k <= j; k++ )
555
            {
556
                if( cc[k] == '\000' )
557
                    return ( ++i );              /* Found it */
558
 
559
                if( toupper( cc[k] ) !=
560
                    toupper( config.country_name[i].abr[k] ) )
561
                    break;
562
            }
563
        }
564
        beep();
565
        for( i = 0; i < j; i++ )
566
            putchar( '\010' );
567
    }
568
    return ( 0 );
569
}
570
 
571
/*========================================================================
572
 *
573
 *  Get menu selector
574
 *
575
 *  Purpose:
576
 *      This function is called to Get menu selector
577
 *
578
 *      This function will accept characters until one in
579
 *      "list" is encountered. This is returned to the user
580
 *      RETURN is valid if the first character of the list is not a "*"
581
 *      it will return the first charcater of the list;
582
 *      ABORT will return a zero
583
 *
584
 *  Parameters:
585
 *      List        A string of valid responses
586
 *
587
 *  Returns:
588
 *      Selected character
589
 *
590
 *========================================================================*/
591
 
592
char getfnc( const char *list )
593
{
594
    const char       *copylist;
595
    char        c;
596
    char        result = 0;
597
 
598
    copylist = ( *list == '*' ) ? list + 1 : list;
599
    while( ( c = getinp() ) != ABORT_CHAR )
600
    {
601
        c = toupper( c );                        /* Get a character */
602
        if( strchr( copylist, c ) )
603
        {
604
            result = c;
605
            break;
606
        }
607
 
608
        if( c == '\n' && *list != '*' )
609
        {
610
            result = *list;
611
            break;
612
        }
613
 
614
        beep();
615
    }
616
 
617
    /*
618
    **  Display the option selected
619
    **  Not ready for this yet. Looks a bit ugle in a few places
620
    */
621
    /*    if ( result )
622
    **    putchar(result);
623
    */
624
 
625
    return ( result );
626
}
627
 
628
/*========================================================================
629
 *
630
 *  Prompt for YES / NO
631
 *
632
 *  Purpose:
633
 *      This function is called to Prompt for YES / NO
634
 *
635
 *  Parameters:
636
 *      prompt      Prompt to display to the user
637
 *
638
 *  Returns:
639
 *      TRUE for Yes, FALSE for No
640
 *
641
 *========================================================================*/
642
 
643
bool getyes( const char *prompt )
644
{
645
    char        c;
646
 
647
    printf( "%s ? ", prompt );
648
    c = getfnc( "*YN" );
649
    putchar( '\n' );
650
    return ( c == 'Y' );
651
}
652
 
653
/*========================================================================
654
 *
655
 *  Convert character to upper case
656
 *
657
 *  Purpose:
658
 *      This function is called to convert character to upper case
659
 *
660
 *  Parameters:
661
 *      ch      Character to convert
662
 *
663
 *  Returns:
664
 *      Converted character
665
 *
666
 *========================================================================*/
667
 
668
char to_upper( char ch )
669
{
670
    if( islower( ch ) )
671
        return ( ch + 'A' - 'a' );
672
    return ( ch );
673
}
674
 
675
/*========================================================================
676
 *
677
 *  Convert character to lower case
678
 *
679
 *  Purpose:
680
 *      This function is called to convert character to lower case
681
 *
682
 *  Parameters:
683
 *      ch      Character to convert
684
 *
685
 *  Returns:
686
 *      Converted character
687
 *
688
 *========================================================================*/
689
 
690
char to_lower( char ch )
691
{
692
    if( isupper( ch ) )
693
        return ( ch + 'a' - 'A' );
694
    return ( ch );
695
}
696
 
697
 
698
/*========================================================================
699
 *
700
 *  Wait a bit
701
 *
702
 *  Purpose:
703
 *      This function is called to idle and to nothing for a while
704
 *
705
 *  Parameters:
706
 *      t           Time to wait
707
 *
708
 *  Returns:
709
 *      Nothing
710
 *
711
 *========================================================================*/
712
 
713
void sleep( int t )
714
{
715
    Sleep ( t * 1000 );
716
}
717
 
718
 
719
/*========================================================================
720
 *
721
 *  Clear to the end of the current display line
722
 *
723
 *  Purpose:
724
 *      Clears to the end of the line
725
 *
726
 *  Parameters:
727
 *      None
728
 *
729
 *  Returns:
730
 *      Nothing
731
 *
732
 *========================================================================*/
733
void console_clreol( void )
734
{
735
 
736
    CONSOLE_SCREEN_BUFFER_INFO console_info;
737
    DWORD                      rlen;
738
 
739
    int ii;
740
    int top;
741
 
742
 
743
    /*
744
    **  Determine the screen size and the current location of the
745
    **  cursor within the screen
746
    */
747
    GetConsoleScreenBufferInfo( h_console, &console_info );
748
 
749
    /*
750
    **  Clear all of it
751
    */
752
    top = console_info.dwSize.X - console_info.dwCursorPosition.X;
753
 
754
    for ( ii = 0; ii < top ; ii++ )
755
    {
756
        WriteConsole( h_console, " ", 1, &rlen, NULL );
757
    }
758
    cur ( console_info.dwCursorPosition.X, console_info.dwCursorPosition.Y );
759
}
760
 
761
/*========================================================================
762
 *
763
 *  printf - Text display routines
764
 *  putchar - Text display routines
765
 *
766
 *
767
 *  Purpose:
768
 *      Intercept standard output calls and direct output to the
769
 *      console.
770
 *
771
 *  Parameters: As per standard function definitions
772
 *      None
773
 *
774
 *  Returns:
775
 *      As per standard function definitions
776
 *
777
 *========================================================================*/
778
 
779
 
780
int putchar(int cc)
781
{
782
    printf( "aaa-%c", cc );
783
    return 1;
784
}
785
 
786
// void gross_error_message(void)
787
// {
788
//     LPVOID lpMsgBuf;
789
//
790
//     FormatMessage(
791
//         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
792
//         NULL,
793
//         GetLastError(),
794
//         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
795
//         (LPTSTR) &lpMsgBuf,
796
//         0,
797
//         NULL
798
//     );
799
//
800
//     // Display the string.
801
//     MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
802
//
803
//     // Free the buffer.
804
//     LocalFree( lpMsgBuf );
805
// }
806
 
807
 
808
/*========================================================================
809
 *
810
 *  Display a prompt character in a highlight
811
 *
812
 *  Purpose:
813
 *      USed to highlight menu entries
814
 *
815
 *  Parameters:
816
 *      None
817
 *
818
 *  Returns:
819
 *      Nothing
820
 *
821
 *========================================================================*/
822
void console_prompt( char prompt )
823
{
824
    SetConsoleTextAttribute( h_console, FOREGROUND_GREEN );
825
    putchar( prompt );
826
    SetConsoleTextAttribute( h_console, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
827
}
828
 
829
/*------------------------------------------------------------------------------
830
Name:           get_kb_character
831
Description:    Extract a keyboard character - including cursor and function
832
                keys.
833
Parameters:     None
834
Global Refs:
835
Returns:        a 16 bit value that encodes ascii character and scan code
836
Side-effects:
837
------------------------------------------------------------------------------*/
838
 
839
int get_kb_character( void )
840
{
841
    HANDLE          hConsoleInput;
842
    INPUT_RECORD    Buffer;
843
    DWORD           NumberOfEventsRead;
844
 
845
    hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
846
 
847
    /*
848
    **  Process console events until we get a suitable keystroke event
849
    */
850
    FlushFileBuffers( h_console );
851
    for (;;)
852
    {
853
        if (ReadConsoleInput( hConsoleInput, &Buffer, 1, &NumberOfEventsRead ))
854
        {
855
            /*
856
            **  Only want keyboard depression - ignore other events
857
            */
858
            if (Buffer.EventType == KEY_EVENT && Buffer.Event.KeyEvent.bKeyDown)
859
            {
860
                /*
861
                **  Ignore several scan codes
862
                **  CNTRL, ALT, SHIFT, CAPLOCK
863
                */
864
                if (Buffer.Event.KeyEvent.wVirtualScanCode == 0x38 ||
865
                    Buffer.Event.KeyEvent.wVirtualScanCode == 0x1D ||
866
                    Buffer.Event.KeyEvent.wVirtualScanCode == 0x2A ||
867
                    Buffer.Event.KeyEvent.wVirtualScanCode == 0x3A)
868
                {
869
                    continue;
870
                }
871
 
872
                return ( (int) (Buffer.Event.KeyEvent.uChar.AsciiChar + (Buffer.Event.KeyEvent.wVirtualScanCode << 8)));
873
            }
874
        }
875
        else
876
        {
877
            sleep (1);
878
        }
879
    }
880
 
881
}
882
 
883
/********************************* EOF ***********************************/
884