VirtualBox

Changeset 54724 in vbox


Ignore:
Timestamp:
Mar 11, 2015 8:37:08 PM (10 years ago)
Author:
vboxsync
Message:

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/mangling.h

    r54415 r54724  
    14751475# define RTStrmFlush                                    RT_MANGLER(RTStrmFlush)
    14761476# define RTStrmGetCh                                    RT_MANGLER(RTStrmGetCh)
     1477# define RTStrmInputGetEchoChars                        RT_MANGLER(RTStrmInputGetEchoChars)
    14771478# define RTStrmGetLine                                  RT_MANGLER(RTStrmGetLine)
    14781479# define RTStrmOpen                                     RT_MANGLER(RTStrmOpen)
     
    14861487# define RTStrmReadEx                                   RT_MANGLER(RTStrmReadEx)
    14871488# define RTStrmRewind                                   RT_MANGLER(RTStrmRewind)
     1489# define RTStrmInputSetEchoChars                        RT_MANGLER(RTStrmInputSetEchoChars)
    14881490# define RTStrmSetMode                                  RT_MANGLER(RTStrmSetMode)
    14891491# define RTStrmWriteEx                                  RT_MANGLER(RTStrmWriteEx)
  • trunk/include/iprt/stream.h

    r51770 r54724  
    127127
    128128/**
     129 * Returns the current echo mode.
     130 * This works only for standard input streams.
     131 *
     132 * @returns iprt status code.
     133 * @param   pStream         The stream.
     134 * @param   pfEchoChars     Where to store the flag whether typed characters are echoed.
     135 */
     136RTR3DECL(int) RTStrmInputGetEchoChars(PRTSTREAM pStream, bool *pfEchoChars);
     137
     138/**
     139 * Changes the behavior for echoing inpit characters on the command line.
     140 * This works only for standard input streams.
     141 *
     142 * @returns iprt status code.
     143 * @param   pStream         The stream.
     144 * @param   fEchoChars      Flag whether echoing typed characters is wanted.
     145 */
     146RTR3DECL(int) RTStrmInputSetEchoChars(PRTSTREAM pStream, bool fEchoChars);
     147
     148/**
    129149 * Rewinds the stream.
    130150 *
  • 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.

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