Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
bash (5.2.21-2.1deepin1) unstable; urgency=medium

* modify method of clearing terminal scroll buffer

-- luojiahao <[email protected]> Thu, 19 Jun 2025 15:19:12 +0800

bash (5.2.21-2.1) unstable; urgency=medium

* Non-maintainer upload.
Expand Down
64 changes: 6 additions & 58 deletions debian/clear_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,83 +165,31 @@ int is_pseudo_tty(int fd)
return 0;
}

int clear_console(int fd)
int clear_console()
{
int num, tmp_num;
#if defined(__linux__)
struct vt_stat vtstat;
#endif

/* Linux console secure erase (since 2.6.39), this is sufficient there;
other terminals silently ignore this code. If they don't and write junk
instead, well, we're clearing the screen anyway.
*/
write(1, "\e[3J", 4);

/* clear screen */
setupterm((char *) 0, 1, (int *) 0);
if (tputs(clear_screen, lines > 0 ? lines : 1, putch) == ERR)
{
exit(1);
}

if (is_pseudo_tty(STDIN_FILENO))
return 0;

if (!strcmp(getenv("TERM"), "screen"))
return 0;

/* get current vt */
#if defined(__linux__)
if (ioctl(fd, VT_GETSTATE, &vtstat) < 0)
#elif defined(__FreeBSD_kernel__)
if (ioctl(fd, VT_ACTIVATE, &num) < 0)
#endif
{
if (!quiet)
fprintf(stderr, "%s: cannot get VTstate\n", progname);
exit(1);
}
#if defined(__linux__)
num = vtstat.v_active;
#endif
tmp_num = (num == 6 ? 5 : 6);

/* switch vt to clear the scrollback buffer */
if (ioctl(fd, VT_ACTIVATE, tmp_num))
{
if (!quiet)
perror("chvt: VT_ACTIVATE");
exit(1);
}

if (ioctl(fd, VT_WAITACTIVE, tmp_num))
{
if (!quiet)
perror("VT_WAITACTIVE");
exit(1);
}

/* switch back */
if (ioctl(fd, VT_ACTIVATE, num))
{
if (!quiet)
perror("chvt: VT_ACTIVATE");
exit(1);
}

if (ioctl(fd, VT_WAITACTIVE, num))
/* clear screen */
setupterm((char *) 0, 1, (int *) 0);
if (tputs(clear_screen, lines > 0 ? lines : 1, putch) == ERR)
{
if (!quiet)
perror("VT_WAITACTIVE");
exit(1);
}
return 0;
}

int main (int argc, char* argv[])
{
int fd;
int result; /* option handling */
int an_option;

Expand Down Expand Up @@ -278,14 +226,14 @@ int main (int argc, char* argv[])
exit(1);
}

if ((fd = get_console_fd(NULL)) == -1)
if ((get_console_fd(NULL)) == -1)
{
if (!quiet)
fprintf(stderr, "%s: terminal is not a console\n", progname);
exit(1);
}

clear_console(fd);
clear_console();

return 0;
}
Loading