VirtualBox

Changeset 54724 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Mar 11, 2015 8:37:08 PM (10 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
98892
Message:

Runtime/RTStream: Make it possible to disable echoing of typed characters from terminal input streams

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/stream.cpp

    r51770 r54724  
    5959#ifdef RT_OS_WINDOWS
    6060# include <Windows.h>
     61#endif
     62#ifndef RT_OS_WINDOWS
     63# include <termios.h>
     64# include <unistd.h>
    6165#endif
    6266
     
    462466
    463467    return VINF_SUCCESS;
     468}
     469
     470
     471RTR3DECL(int) RTStrmInputGetEchoChars(PRTSTREAM pStream, bool *pfEchoChars)
     472{
     473    int rc;
     474
     475    AssertPtrReturn(pStream, VERR_INVALID_HANDLE);
     476    AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_HANDLE);
     477    AssertPtrReturn(pfEchoChars, VERR_INVALID_POINTER);
     478
     479    int fh = fileno(pStream->pFile);
     480    if (isatty(fh))
     481    {
     482#ifdef RT_OS_WINDOWS
     483        DWORD dwMode;
     484        HANDLE hCon = (HANDLE)_get_osfhandle(fh);
     485        if (GetConsoleMode(hCon, &dwMode))
     486            *pfEchoChars = RT_BOOL(dwMode & ENABLE_ECHO_INPUT);
     487        else
     488            rc = RTErrConvertFromWin32(GetLastError());
     489#else
     490        struct termios Termios;
     491
     492        int rcPosix = tcgetattr(fh, &Termios);
     493        if (!rcPosix)
     494            *pfEchoChars = RT_BOOL(Termios.c_lflag & ECHO);
     495        else
     496            rc = RTErrConvertFromErrno(errno);
     497#endif
     498    }
     499    else
     500        rc = VERR_INVALID_HANDLE;
     501
     502    return rc;
     503}
     504
     505
     506RTR3DECL(int) RTStrmInputSetEchoChars(PRTSTREAM pStream, bool fEchoChars)
     507{
     508    int rc;
     509
     510    AssertPtrReturn(pStream, VERR_INVALID_HANDLE);
     511    AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_HANDLE);
     512
     513    int fh = fileno(pStream->pFile);
     514    if (isatty(fh))
     515    {
     516#ifdef RT_OS_WINDOWS
     517        DWORD dwMode;
     518        HANDLE hCon = (HANDLE)_get_osfhandle(fh);
     519        if (GetConsoleMode(hCon, &dwMode))
     520        {
     521            if (fEchoChars)
     522                dwMode |= ENABLE_ECHO_INPUT;
     523            else
     524                dwMode &= ~ENABLE_ECHO_INPUT;
     525            if (!SetConsoleMode(hCon, dwMode))
     526                rc = RTErrConvertFromWin32(GetLastError());
     527        }
     528        else
     529            rc = RTErrConvertFromWin32(GetLastError());
     530#else
     531        struct termios Termios;
     532
     533        int rcPosix = tcgetattr(fh, &Termios);
     534        if (!rcPosix)
     535        {
     536            if (fEchoChars)
     537                Termios.c_lflag |= ECHO;
     538            else
     539                Termios.c_lflag &= ~ECHO;
     540
     541            rcPosix = tcsetattr(fh, TCSAFLUSH, &Termios);
     542            if (rcPosix != 0)
     543                rc = RTErrConvertFromErrno(errno);
     544        }
     545        else
     546            rc = RTErrConvertFromErrno(errno);
     547#endif
     548    }
     549    else
     550        rc = VERR_INVALID_HANDLE;
     551
     552    return rc;
    464553}
    465554
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette