Annotation of mstools/samples/console/conmode.c, revision 1.1.1.3

1.1.1.3 ! root        1: 
        !             2: /******************************************************************************\
        !             3: *       This is a part of the Microsoft Source Code Samples. 
        !             4: *       Copyright (C) 1993 Microsoft Corporation.
        !             5: *       All rights reserved. 
        !             6: *       This source code is only intended as a supplement to 
        !             7: *       Microsoft Development Tools and/or WinHelp documentation.
        !             8: *       See these sources for detailed information regarding the 
        !             9: *       Microsoft samples programs.
        !            10: \******************************************************************************/
        !            11: 
1.1       root       12: #include <windows.h>
                     13: #include <stdio.h>
                     14: #include <string.h>
                     15: #include "console.h"
                     16: 
                     17: /*********************************************************************
                     18: * FUNCTION: rawOut(HANDLE hConsole, PCHAR s)                         *
                     19: *                                                                    *
                     20: * PURPOSE: output the contents of the input buffer to the console    *
                     21: *          handle in 'raw' mode                                      *
                     22: *                                                                    *
                     23: * INPUT: the console handle to write to, and the string to output in *
                     24: *        'raw' mode                                                  *
                     25: *********************************************************************/
                     26: 
                     27: void rawOut(HANDLE hConsole, PCHAR s)
                     28: {
                     29:   BOOL bSuccess;
                     30:   DWORD cCharsWritten;
                     31:   DWORD dwOutputMode;
                     32: 
                     33:   bSuccess = GetConsoleMode(hConsole, &dwOutputMode);
                     34:   PERR(bSuccess, "GetconsoleMode");
                     35:   /* output the string in raw mode. */
                     36:   /* turn off processed output, output the string, and reset output mode */
                     37:   bSuccess = SetConsoleMode(hConsole, dwOutputMode & ~ENABLE_PROCESSED_OUTPUT);
                     38:   PERR(bSuccess, "SetConsoleMode");
                     39:   bSuccess = WriteFile(hConsole, s, strlen(s), &cCharsWritten, NULL);
                     40:   PERR(bSuccess, "WriteFile");
                     41:   /* now reset output mode */
                     42:   bSuccess = SetConsoleMode(hConsole, dwOutputMode);
                     43:   PERR(bSuccess, "SetConsoleMode");
                     44:   return;
                     45: }
                     46: 
                     47: /******************************************************************
                     48: * FUNCTION: demoConMode(HANDLE hConOut)                           *
                     49: *                                                                 *
                     50: * PURPOSE: demonstrate GetConsoleMode and SetConsoleMode. Display *
                     51: *          the current console input and output modes, then       *
                     52: *          demonstrate each console input and output mode         *
                     53: *                                                                 *
                     54: *                                                                 *
                     55: * INPUT: the console output handle to write to                    *
                     56: ******************************************************************/
                     57: 
                     58: void demoConMode(HANDLE hConOut)
                     59: {
                     60:   BOOL bSuccess;
                     61:   DWORD dwOutputMode, dwInputMode; /* save input & output console modes */
                     62:   CHAR szTemp[128];
                     63:   HANDLE hStdIn; /* standard input handle */
                     64:   /* a string to test 'raw' vs. 'processed' output with */
                     65:   PCHAR szModeTst = "tab:\t backspace:\b linefeed:\n bell:\a cr:\r";
                     66:   DWORD dwRead;
                     67:   CHAR chBuf[256]; /* buffer to read a user string from the console */
                     68:   PCHAR szLong = "This is a line of text that is greater than 80 characters wide. This is a test of ENABLE_WRAP_AT_EOL_OUTPUT.";
                     69: 
                     70:   setConTitle(__FILE__);
                     71:   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.2   root       72:   PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
1.1       root       73:   /* get the input mode and save it so that we can restore it later */
                     74:   bSuccess = GetConsoleMode(hStdIn, &dwInputMode);
                     75:   PERR(bSuccess, "GetconsoleMode");
                     76:   myPuts(hConOut, "Current console input modes, as reported by GetconsoleMode:");
                     77:   sprintf(szTemp, "ENABLE_LINE_INPUT: %s", dwInputMode & ENABLE_LINE_INPUT ?
                     78:       "on" : "off");
                     79:   myPuts(hConOut, szTemp);
                     80:   sprintf(szTemp, "ENABLE_ECHO_INPUT: %s", dwInputMode & ENABLE_ECHO_INPUT ?
                     81:       "on" : "off");
                     82:   myPuts(hConOut, szTemp);
                     83:   sprintf(szTemp, "ENABLE_WINDOW_INPUT: %s", dwInputMode &
                     84:       ENABLE_WINDOW_INPUT ? "on" : "off");
                     85:   myPuts(hConOut, szTemp);
                     86:   sprintf(szTemp, "ENABLE_PROCESSED_INPUT: %s", dwInputMode &
                     87:       ENABLE_PROCESSED_INPUT ? "on" : "off");
                     88:   myPuts(hConOut, szTemp);
                     89:   bSuccess = GetConsoleMode(hConOut, &dwOutputMode);
                     90:   PERR(bSuccess, "GetconsoleMode");
                     91:   myPuts(hConOut, "\nCurrent console output modes:");
                     92:   sprintf(szTemp, "ENABLE_PROCESSED_OUTPUT: %s", dwOutputMode &
                     93:       ENABLE_PROCESSED_OUTPUT ? "on" : "off");
                     94:   myPuts(hConOut, szTemp);
                     95:   sprintf(szTemp, "ENABLE_WRAP_AT_EOL_OUTPUT: %s", dwOutputMode &
                     96:       ENABLE_WRAP_AT_EOL_OUTPUT ? "on" : "off");
                     97:   myPuts(hConOut, szTemp);
                     98:   bSuccess = SetConsoleMode(hConOut, dwOutputMode);  /* back to normal */
                     99:   PERR(bSuccess, "SetConsoleMode");
                    100:   myPuts(hConOut, "\nHit enter to continue...");
                    101:   myGetchar();
                    102:   cls(hConOut);
                    103:   myPuts(hConOut, "\nLet's test the console output modes with SetConsoleMode.\n"
                    104:                   "First we'll output a long line of text with\n"
                    105:                   "ENABLE_WRAP_AT_EOL_OUTPUT enabled, then with it disabled:\n");
                    106:   myPuts(hConOut, szLong);
                    107:   myPuts(hConOut, "\nNow we'll disable ENABLE_WRAP_AT_EOL_OUTPUT and display the same\n"
                    108:                   "string. Note how the wrapped line does NOT get displayed\n"
                    109:                   "on the next line but on the same line:\n");
                    110:   /* turn off EOL wrap */
                    111:   bSuccess = SetConsoleMode(hConOut, dwOutputMode &
                    112:       ~ENABLE_WRAP_AT_EOL_OUTPUT);
                    113:   PERR(bSuccess, "SetConsoleMode");
                    114:   myPuts(hConOut, szLong);
                    115:   /* turn on EOL wrap */
                    116:   bSuccess = SetConsoleMode(hConOut, dwOutputMode |
                    117:       ENABLE_WRAP_AT_EOL_OUTPUT);
                    118:   PERR(bSuccess, "SetConsoleMode");
                    119:   myPuts(hConOut, "\n\nNow let's test processed output. We'll output a string\n"
                    120:                   "with ENABLE_PROCESSED_OUTPUT disabled, then with it enabled.\n"
                    121:                   "Note how the backspace, tab, bell, carriage return, and\n"
                    122:                   "linefeed characters are processed differently:\n");
                    123:   /* turn off processed ("cooked") output - now in "raw" mode */
                    124:   bSuccess = SetConsoleMode(hConOut, dwOutputMode & ~ENABLE_PROCESSED_OUTPUT);
                    125:   PERR(bSuccess, "SetConsoleMode");
                    126:   /* myPuts() appends a \n, so can't use it for this case */
                    127:   bSuccess = WriteFile(hConOut, szModeTst, strlen(szModeTst), &dwRead, NULL);
                    128:   PERR(bSuccess, "WriteFile");
                    129:   /* turn processed ("cooked") mode back on */
                    130:   bSuccess = SetConsoleMode(hConOut, dwOutputMode | ENABLE_PROCESSED_OUTPUT);
                    131:   PERR(bSuccess, "SetConsoleMode");
                    132:   myPuts(hConOut, "\nNow we'll set ENABLE_PROCESSED_OUTPUT and output the same\n"
                    133:                   "string:\n");
                    134:   myPuts(hConOut, szModeTst);
                    135:   myPuts(hConOut, "\nHit enter for the next test...");
                    136:   myGetchar();
                    137:   cls(hConOut);
                    138:   myPuts(hConOut, "Now let's test the input modes. First you'll input\n"
                    139:                   "an input string with ENABLE_PROCESSED_INPUT enabled.\n"
                    140:                   "We'll output exactly what was read in this mode.\n"
                    141:                   "Note how characters such as backspace, tab, ctrl+g,\n"
                    142:                   "carriage return and line feed are interpreted.\n"
                    143:                   "Enter a line of text, end by hitting enter:");
                    144:   /* turn on processed, line, and echo modes. */
                    145:   /* MUST turn on echo mode when turning on line mode */
                    146:   bSuccess = SetConsoleMode(hStdIn, dwInputMode | ENABLE_PROCESSED_INPUT |
                    147:       ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
                    148:   PERR(bSuccess, "SetConsoleMode");
                    149:   memset(chBuf, 0, sizeof(chBuf));
                    150:   bSuccess = ReadFile(hStdIn, chBuf, sizeof(chBuf), &dwRead, NULL);
                    151:   PERR(bSuccess, "ReadFile");
                    152:   /* output the contents of chBuf in raw mode */
                    153:   rawOut(hConOut, chBuf);
                    154:   myPuts(hConOut, "\nNow let's disable ENABLE_PROCESSED_INPUT. Enter another\n"
                    155:                   "input string:");
                    156:   /* turn off processed, line, and echo input. */
                    157:   /* MUST turn off echo input when turning off line input */
                    158:   bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_PROCESSED_INPUT |
                    159:       ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
                    160:   PERR(bSuccess, "SetConsoleMode");
                    161:   memset(chBuf, 0, sizeof(chBuf));
                    162:   bSuccess = ReadFile(hStdIn, chBuf, sizeof(chBuf), &dwRead, NULL);
                    163:   PERR(bSuccess, "ReadFile");    
                    164:   rawOut(hConOut, chBuf);
                    165:   myPuts(hConOut, "\nHit enter to continue...");
                    166:   myGetchar();
                    167:   cls(hConOut);
                    168:   myPuts(hConOut, "\n\nLet's turn off ENABLE_LINE_INPUT - all of our input\n"
                    169:                   "previously has been line-input. We must also disable\n"
                    170:                   "ENABLE_ECHO_INPUT when disabling this flag. Let's read\n"
                    171:                   "a single character from the console and we will return\n"
                    172:                   "immediately with the charater read. Note that the character\n"
                    173:                   "does not echo. Hit any key:");
                    174:   bSuccess = SetConsoleMode(hStdIn, dwInputMode & ~ENABLE_LINE_INPUT &
                    175:       ~ENABLE_ECHO_INPUT);
                    176:   PERR(bSuccess, "SetConsoleMode");
                    177:   memset(chBuf, 0, sizeof(chBuf));
                    178:     bSuccess = ReadFile(hStdIn, chBuf, sizeof(chBuf), &dwRead, NULL);
                    179:   PERR(bSuccess, "ReadFile");    
                    180:   myPuts(hConOut, "\nHere is the character read:");
                    181:   myPuts(hConOut, chBuf);
                    182:   myPuts(hConOut, "\n\nHit enter to continue...");
                    183:   bSuccess = SetConsoleMode(hStdIn, dwInputMode | ENABLE_WINDOW_INPUT);
                    184:   PERR(bSuccess, "SetConsoleMode");
                    185:   myGetchar();
                    186:   return;
                    187: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.