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