VirtualBox

Changeset 3188 in kBuild


Ignore:
Timestamp:
Mar 24, 2018 3:32:26 PM (7 years ago)
Author:
bird
Message:

kmk,lib,kWorker: Console output on windows cleanups.

Location:
trunk/src
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kWorker/Makefile.kmk

    r3042 r3188  
    7878       nt/kFsCache.c \
    7979       quote_argv.c \
     80        is_console.c \
    8081        maybe_con_write.c \
    8182        maybe_con_fwrite.c \
  • trunk/src/kWorker/kWorker.c

    r3089 r3188  
    5656#include "quote_argv.h"
    5757#include "md5.h"
     58#include "console.h"
    5859
    5960#include "../kmk/kmkbuiltin.h"
     
    1071610717    char  sz3[64];
    1071710718    char  sz4[64];
    10718     extern size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile);
    1071910719
    1072010720    sprintf(szPrf, "%5d/%u:", getpid(), K_ARCH_BITS);
  • trunk/src/kmk/kmkbuiltin/err.c

    r3065 r3188  
    3636#ifdef KBUILD_OS_WINDOWS
    3737/* This is a trick to speed up console output on windows. */
     38# include "console.h"
    3839# undef fwrite
    3940# define fwrite maybe_con_fwrite
    40 extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
    4141#endif
    4242
  • trunk/src/kmk/kmkbuiltin/mscfakes.c

    r3140 r3188  
    4545#undef lutimes
    4646
    47 extern ssize_t maybe_con_write(int, void const *, size_t);
     47#include "console.h"
     48
    4849
    4950
     
    477478    {
    478479        /* Console output optimization: */
    479         if (cbSrc > 0 && isatty(fd))
     480        if (cbSrc > 0 && is_console(fd))
    480481            return maybe_con_write(fd, pvSrc, cbSrc);
    481482
     
    586587ssize_t writev(int fd, const struct iovec *vector, int count)
    587588{
    588     int size = 0;
    589     int i;
    590     for (i = 0; i < count; i++)
    591     {
    592         int cb = msc_write(fd, vector[i].iov_base, (int)vector[i].iov_len);
    593         if (cb < 0)
    594             return cb;
    595         size += cb;
     589    ssize_t size = 0;
     590    if (count > 0)
     591    {
     592        int i;
     593
     594        /* To get consistent console output, we must try combine the segments
     595           when outputing to the console. */
     596        if (count > 1 && is_console(fd))
     597        {
     598            char   *pbTmp;
     599            ssize_t cbTotal;
     600            if (count == 1)
     601                return maybe_con_write(fd, vector[0].iov_base, (int)vector[0].iov_len);
     602
     603            cbTotal = 0;
     604            for (i = 0; i < count; i++)
     605                cbTotal += vector[i].iov_len;
     606            pbTmp = malloc(cbTotal);
     607            if (pbTmp)
     608            {
     609                char *pbCur = pbTmp;
     610                for (i = 0; i < count; i++)
     611                {
     612                    memcpy(pbCur, vector[i].iov_base, vector[i].iov_len);
     613                    pbCur += vector[i].iov_len;
     614                }
     615                size = maybe_con_write(fd, pbTmp, cbTotal);
     616                free(pbTmp);
     617                return size;
     618            }
     619
     620            /* fall back on segment by segment output. */
     621        }
     622
     623        for (i = 0; i < count; i++)
     624        {
     625            int cb = msc_write(fd, vector[i].iov_base, (int)vector[i].iov_len);
     626            if (cb < 0)
     627                return cb;
     628            size += cb;
     629        }
    596630    }
    597631    return size;
  • trunk/src/kmk/kmkbuiltin/mscfakes.h

    r3140 r3188  
    9090typedef unsigned short gid_t;
    9191#endif
    92 #if defined(_M_AMD64) || defined(_M_X64) || defined(_M_IA64) || defined(_WIN64)
    93 typedef __int64 ssize_t;
    94 #else
    95 typedef long ssize_t;
    96 #endif
     92typedef intptr_t ssize_t;
    9793typedef unsigned long u_long;
    9894typedef unsigned int u_int;
  • trunk/src/kmk/kmkbuiltin/printf.c

    r3145 r3188  
    7878#ifdef KBUILD_OS_WINDOWS
    7979/* This is a trick to speed up console output on windows. */
     80# include "console.h"
    8081# undef fwrite
    8182# define fwrite maybe_con_fwrite
    82 extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
    8383#endif
    8484
  • trunk/src/kmk/output.c

    r3156 r3188  
    4343# endif
    4444#endif /* WINDOWS32 */
     45#ifdef KBUILD_OS_WINDOWS
     46# include "console.h"
     47#endif
    4548
    4649struct output *output_context = NULL;
     
    6669#ifdef KBUILD_OS_WINDOWS
    6770      /** @todo check if fputs is also subject to char-by-char stupidity */
    68       extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
    6971      maybe_con_fwrite(msg, strlen(msg), 1, f);
    7072#else
  • trunk/src/lib/Makefile.kmk

    r3179 r3188  
    4141        maybe_con_write.c \
    4242        maybe_con_fwrite.c \
     43        is_console.c \
    4344       dos2unix.c \
    4445        kbuild_version.c
  • trunk/src/lib/maybe_con_fwrite.c

    r2906 r3188  
    55
    66/*
    7  * Copyright (c) 2016 knut st. osmundsen <[email protected]>
     7 * Copyright (c) 2016-2018 knut st. osmundsen <[email protected]>
    88 *
    99 * Permission is hereby granted, free of charge, to any person obtaining a
     
    3333*   Header Files                                                                                                                 *
    3434*********************************************************************************************************************************/
     35#include "console.h"
    3536#ifdef KBUILD_OS_WINDOWS
    3637# include <windows.h>
    3738#endif
    3839#include <errno.h>
    39 #include <stdio.h>
    4040#ifdef _MSC_VER
    41 # include <io.h>
    4241# include <conio.h>
    4342#endif
    44 
    4543
    4644
     
    6967        if (fd >= 0)
    7068        {
    71             if (isatty(fd))
     69            HANDLE hCon = (HANDLE)_get_osfhandle(fd);
     70            if (   hCon != INVALID_HANDLE_VALUE
     71                && hCon != NULL)
    7272            {
    73                 HANDLE hCon = (HANDLE)_get_osfhandle(fd);
    74                 if (   hCon != INVALID_HANDLE_VALUE
    75                     && hCon != NULL)
     73                if (is_console_handle((intptr_t)hCon))
    7674                {
    7775                    size_t   cbToWrite = cbUnit * cUnits;
     
    8583                            s_uConsoleCp = GetConsoleCP();
    8684
    87                         cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
     85                        cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite,
     86                                                         pawcTmp, (int)(cwcTmp - 1));
    8887                        if (cwcToWrite > 0)
    8988                        {
     
    9291
    9392                            /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    94                                sources indicates _cputws will do the right thing we want.  */
     93                               sources indicates _cputws will do the right thing.  */
    9594                            fflush(pFile);
    9695                            rc = _cputws(pawcTmp);
     
    113112    return fwrite(pvBuf, cbUnit, cUnits, pFile);
    114113}
     114
  • trunk/src/lib/maybe_con_write.c

    r3065 r3188  
    55
    66/*
    7  * Copyright (c) 2016 knut st. osmundsen <[email protected]>
     7 * Copyright (c) 2016-2018 knut st. osmundsen <[email protected]>
    88 *
    99 * Permission is hereby granted, free of charge, to any person obtaining a
     
    3333*   Header Files                                                                                                                 *
    3434*********************************************************************************************************************************/
     35#include "console.h"
    3536#ifdef KBUILD_OS_WINDOWS
    3637# include <windows.h>
     
    3839#include <errno.h>
    3940#ifdef _MSC_VER
    40 # include <io.h>
    4141# include <conio.h>
    42 typedef intptr_t ssize_t;
    4342typedef unsigned int to_write_t;
    4443#else
    45 # include <unistd.h>
    4644typedef size_t to_write_t;
    4745#endif
     
    5654 * @param   cbToWrite           How much to write.
    5755 */
    58 ssize_t maybe_con_write(int fd, void *pvBuf, size_t cbToWrite)
     56ssize_t maybe_con_write(int fd, void const *pvBuf, size_t cbToWrite)
    5957{
    6058    ssize_t cbWritten;
     59
    6160#ifdef KBUILD_OS_WINDOWS
    6261    /*
     
    6463     * call WriteConsoleW directly.
    6564     */
    66     if (cbToWrite > 0 && isatty(fd))
     65    if (cbToWrite > 0)
    6766    {
    6867        HANDLE hCon = (HANDLE)_get_osfhandle(fd);
     
    7069            && hCon != NULL)
    7170        {
    72             size_t   cwcTmp  = cbToWrite * 2 + 16;
    73             wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
    74             if (pawcTmp)
     71            if (is_console_handle((intptr_t)hCon))
    7572            {
    76                 int           cwcToWrite;
    77                 static UINT s_uConsoleCp = 0;
    78                 if (s_uConsoleCp == 0)
    79                     s_uConsoleCp = GetConsoleCP();
     73                size_t   cwcTmp  = cbToWrite * 2 + 16;
     74                wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
     75                if (pawcTmp)
     76                {
     77                    int           cwcToWrite;
     78                    static UINT s_uConsoleCp = 0;
     79                    if (s_uConsoleCp == 0)
     80                        s_uConsoleCp = GetConsoleCP();
    8081
    81                 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
    82                 if (cwcToWrite > 0)
    83                 {
    84                     /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    85                        sources indicates _cputws will do the right thing we want.  */
    86                     pawcTmp[cwcToWrite] = '\0';
    87                     if (_cputws(pawcTmp) >= 0)
    88                         return cbToWrite;
    89                     return -1;
     82                    cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite,
     83                                                     pawcTmp, (int)(cwcTmp - 1));
     84                    if (cwcToWrite > 0)
     85                    {
     86                        /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
     87                           sources indicates _cputws will do the right thing.  */
     88                        pawcTmp[cwcToWrite] = '\0';
     89                        if (_cputws(pawcTmp) >= 0)
     90                            return cbToWrite;
     91                        return -1;
     92                    }
    9093                }
    9194            }
     
    115118    return cbWritten;
    116119}
     120
  • trunk/src/lib/msc_buffered_printf.c

    r2967 r3188  
    3939#include <conio.h>
    4040#include <malloc.h>
     41#include "console.h"
    4142
    4243#undef printf
     
    5354#endif
    5455
    55 extern size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile);
    5656
    5757
     
    9494        if (fd >= 0)
    9595        {
    96             if (isatty(fd))
     96            if (is_console(fd))
    9797            {
    9898                char *pszTmp = (char *)alloca(16384);
     
    135135        if (fd >= 0)
    136136        {
    137             if (isatty(fd))
     137            if (is_console(fd))
    138138            {
    139139                char *pszTmp = (char *)alloca(16384);
     
    182182        if (fd >= 0)
    183183        {
    184             if (isatty(fd))
     184            if (is_console(fd))
    185185            {
    186186                HANDLE hCon = (HANDLE)_get_osfhandle(fd);
  • trunk/src/sed/config.h.win

    r1105 r3188  
    373373
    374374/* Define to `int' if <sys/types.h> does not define. */
    375 #define ssize_t int /* does 14.0 have this? */
     375#define ssize_t intptr_t /* does 14.0 have this? */
    376376
    377377/* Additional defines and includes to make it work with the microsoft compiler */
  • trunk/src/sed/lib/utils.c

    r2909 r3188  
    3939
    4040#ifdef KBUILD_OS_WINDOWS /* bird: Way faster console output! */
    41 extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
     41# include "console.h"
    4242# define fwrite maybe_con_fwrite
    4343#endif
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