1 /****************************************************************************
2 * Copyright 2018,2020,2025 Thomas E. Dickey *
3 * Copyright 2016,2017 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
33
34 /*
35 * clear.c -- clears the terminal's screen
36 */
37
38 #define USE_LIBTINFO
39 #include <clear_cmd.h>
40
41 MODULE_ID("$Id: clear_cmd.c,v 1.8 2025/12/06 21:00:26 tom Exp $")
42
43 #ifdef TERMIOS
44 static int
putch(int c)45 putch(int c)
46 {
47 return putchar(c);
48 }
49 #endif
50
51 int
clear_cmd(bool legacy)52 clear_cmd(bool legacy)
53 {
54 int retval;
55 #ifdef TERMIOS
56 retval = tputs(clear_screen, lines > 0 ? lines : 1, putch);
57 if (!legacy) {
58 /* Clear the scrollback buffer if possible. */
59 char *E3 = tigetstr(UserCap(E3));
60 if (VALID_STRING(E3))
61 (void) tputs(E3, lines > 0 ? lines : 1, putch);
62 }
63 #elif defined(_NC_WINDOWS)
64 /*
65 * https://learn.microsoft.com/en-us/windows/console/clearing-the-screen
66 */
67 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
68 COORD coordScreen =
69 {0, 0};
70 #if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
71 DWORD mode = 0;
72
73 retval = ERR;
74
75 if (GetConsoleMode(hConsole, &mode)) {
76 const DWORD originalMode = mode;
77 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
78
79 if (SetConsoleMode(hConsole, mode)) {
80 DWORD written = 0;
81 PCWSTR sequence = legacy ? L"\x1b[2J" : L"\x1b[2J\x1b[3J";
82 if (WriteConsoleW(hConsole, sequence,
83 (DWORD) wcslen(sequence),
84 &written, NULL)) {
85 SetConsoleCursorPosition(hConsole, coordScreen);
86 retval = OK;
87 }
88 SetConsoleMode(hConsole, originalMode);
89 }
90 }
91 #else
92 DWORD cCharsWritten;
93 CONSOLE_SCREEN_BUFFER_INFO csbi;
94 DWORD dwConSize;
95
96 (void) legacy;
97 retval = ERR;
98
99 /* Get the number of character cells in the current buffer,
100 * to fill the entire screen with blanks */
101 if (GetConsoleScreenBufferInfo(hConsole, &csbi)
102 && (dwConSize = csbi.dwSize.X * csbi.dwSize.Y) > 0
103 && FillConsoleOutputCharacter(hConsole,
104 (TCHAR) ' ',
105 dwConSize,
106 coordScreen,
107 &cCharsWritten)
108 && FillConsoleOutputAttribute(hConsole,
109 csbi.wAttributes,
110 dwConSize,
111 coordScreen,
112 &cCharsWritten)) {
113 SetConsoleCursorPosition(hConsole, coordScreen);
114 retval = OK;
115 }
116 #endif
117 #endif
118 return retval;
119 }
120