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