From eed38537658c25855a1d145921212670a519f681 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 21 Apr 2019 05:44:53 +0000 Subject: [PATCH] added quite some code to handle byte string I/O --- qse/include/qse/awk/Awk.hpp | 13 ++- qse/include/qse/awk/StdAwk.hpp | 3 + qse/include/qse/awk/awk.h | 20 ++-- qse/include/qse/si/nwio.h | 6 ++ qse/include/qse/si/pio.h | 7 ++ qse/lib/awk/Awk.cpp | 106 ++++++++++--------- qse/lib/awk/StdAwk.cpp | 28 +++-- qse/lib/awk/fnc.c | 9 +- qse/lib/awk/parse.c | 32 +++--- qse/lib/awk/rec.c | 2 +- qse/lib/awk/rio.c | 184 ++++++++++++++++++--------------- qse/lib/awk/rio.h | 8 +- qse/lib/awk/run.c | 35 +++---- qse/lib/awk/std.c | 154 +++++++++------------------ qse/lib/awk/tree.c | 2 +- qse/lib/awk/val.c | 3 +- qse/lib/si/nwio.c | 16 ++- qse/lib/si/pio.c | 44 ++++---- 18 files changed, 342 insertions(+), 330 deletions(-) diff --git a/qse/include/qse/awk/Awk.hpp b/qse/include/qse/awk/Awk.hpp index 4d64b527..8bcb5639 100644 --- a/qse/include/qse/awk/Awk.hpp +++ b/qse/include/qse/awk/Awk.hpp @@ -386,6 +386,7 @@ public: virtual int close (Pipe& io) = 0; virtual ssize_t read (Pipe& io, char_t* buf, size_t len) = 0; virtual ssize_t write (Pipe& io, const char_t* buf, size_t len) = 0; + virtual ssize_t writeBytes (Pipe& io, const qse_mchar_t* buf, size_t len) = 0; virtual int flush (Pipe& io) = 0; }; @@ -430,6 +431,7 @@ public: virtual int close (File& io) = 0; virtual ssize_t read (File& io, char_t* buf, size_t len) = 0; virtual ssize_t write (File& io, const char_t* buf, size_t len) = 0; + virtual ssize_t writeBytes (File& io, const qse_mchar_t* buf, size_t len) = 0; virtual int flush (File& io) = 0; }; @@ -493,6 +495,8 @@ public: /// for failure. virtual ssize_t write (Console& io, const char_t* buf, size_t len) = 0; + virtual ssize_t writeBytes (Console& io, const qse_mchar_t* buf, size_t len) = 0; + /// You may choose to buffer the data passed to the write() /// function and perform actual writing when flush() is called. /// It must return 0 for success and -1 for failure. @@ -1282,6 +1286,7 @@ protected: virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len); virtual ssize_t writePipe (Pipe& io, const char_t* buf, size_t len); + virtual ssize_t writePipeBytes (Pipe& io, const qse_mchar_t* buf, size_t len); virtual int flushPipe (Pipe& io); /// \} @@ -1295,6 +1300,7 @@ protected: virtual int closeFile (File& io); virtual ssize_t readFile (File& io, char_t* buf, size_t len); virtual ssize_t writeFile (File& io, const char_t* buf, size_t len); + virtual ssize_t writeFileBytes (File& io, const qse_mchar_t* buf, size_t len); virtual int flushFile (File& io); /// \} @@ -1308,6 +1314,7 @@ protected: virtual int closeConsole (Console& io); virtual ssize_t readConsole (Console& io, char_t* buf, size_t len); virtual ssize_t writeConsole (Console& io, const char_t* buf, size_t len); + virtual ssize_t writeConsoleBytes (Console& io, const qse_mchar_t* buf, size_t len); virtual int flushConsole (Console& io); virtual int nextConsole (Console& io); /// \} @@ -1330,13 +1337,13 @@ protected: static ssize_t pipeHandler ( rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count); + void* data, size_t count); static ssize_t fileHandler ( rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count); + void* data, size_t count); static ssize_t consoleHandler ( rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count); + void* data, size_t count); static int functionHandler (rtx_t* rtx, const fnc_info_t* fi); diff --git a/qse/include/qse/awk/StdAwk.hpp b/qse/include/qse/awk/StdAwk.hpp index edec1893..cffbe575 100644 --- a/qse/include/qse/awk/StdAwk.hpp +++ b/qse/include/qse/awk/StdAwk.hpp @@ -133,6 +133,7 @@ protected: int closePipe (Pipe& io); ssize_t readPipe (Pipe& io, char_t* buf, size_t len); ssize_t writePipe (Pipe& io, const char_t* buf, size_t len); + ssize_t writePipeBytes (Pipe& io, const qse_mchar_t* buf, size_t len); int flushPipe (Pipe& io); // file io handlers @@ -140,6 +141,7 @@ protected: int closeFile (File& io); ssize_t readFile (File& io, char_t* buf, size_t len); ssize_t writeFile (File& io, const char_t* buf, size_t len); + ssize_t writeFileBytes (File& io, const qse_mchar_t* buf, size_t len); int flushFile (File& io); // console io handlers @@ -147,6 +149,7 @@ protected: int closeConsole (Console& io); ssize_t readConsole (Console& io, char_t* buf, size_t len); ssize_t writeConsole (Console& io, const char_t* buf, size_t len); + ssize_t writeConsoleBytes (Console& io, const qse_mchar_t* buf, size_t len); int flushConsole (Console& io); int nextConsole (Console& io); diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index ae747e77..35ee3e37 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -586,13 +586,15 @@ typedef qse_ssize_t (*qse_awk_sio_impl_t) ( */ enum qse_awk_rio_cmd_t { - QSE_AWK_RIO_OPEN = 0, /**< open a stream */ - QSE_AWK_RIO_CLOSE = 1, /**< close a stream */ - QSE_AWK_RIO_READ = 2, /**< read a stream */ - QSE_AWK_RIO_WRITE = 3, /**< write to a stream */ - QSE_AWK_RIO_FLUSH = 4, /**< flush buffered data to a stream */ - QSE_AWK_RIO_NEXT = 5 /**< close the current stream and - open the next stream (only for console) */ + QSE_AWK_RIO_OPEN = 0, /**< open a stream */ + QSE_AWK_RIO_CLOSE = 1, /**< close a stream */ + QSE_AWK_RIO_READ = 2, /**< read a stream */ + QSE_AWK_RIO_WRITE = 3, /**< write to a stream */ + /*QSE_AWK_RIO_READ_BYTES = 4,*/ + QSE_AWK_RIO_WRITE_BYTES = 5, + QSE_AWK_RIO_FLUSH = 6, /**< flush buffered data to a stream */ + QSE_AWK_RIO_NEXT = 7 /**< close the current stream and + open the next stream (only for console) */ }; typedef enum qse_awk_rio_cmd_t qse_awk_rio_cmd_t; @@ -644,7 +646,7 @@ struct qse_awk_rio_arg_t /* read-write. a user handler can do whatever it likes to do with these. */ void* handle; /**< I/O handle set by a handler */ - int uflags; /**< flags set by a handler */ + int uflags; /**< user-flags set by a handler */ /*-- from here down, internal use only --*/ int type; @@ -676,7 +678,7 @@ typedef qse_ssize_t (*qse_awk_rio_impl_t) ( qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* arg, - qse_char_t* data, + void* data, qse_size_t count ); diff --git a/qse/include/qse/si/nwio.h b/qse/include/qse/si/nwio.h index 4a02fd8f..cb93fb96 100644 --- a/qse/include/qse/si/nwio.h +++ b/qse/include/qse/si/nwio.h @@ -214,6 +214,12 @@ QSE_EXPORT qse_ssize_t qse_nwio_write ( qse_size_t size ); +QSE_EXPORT qse_ssize_t qse_nwio_writebytes ( + qse_nwio_t* nwio, + const void* data, + qse_size_t size +); + #if defined(__cplusplus) } #endif diff --git a/qse/include/qse/si/pio.h b/qse/include/qse/si/pio.h index f9a80dc7..8055cdfb 100644 --- a/qse/include/qse/si/pio.h +++ b/qse/include/qse/si/pio.h @@ -365,6 +365,13 @@ QSE_EXPORT qse_ssize_t qse_pio_write ( qse_size_t size /**< data size */ ); +QSE_EXPORT qse_ssize_t qse_pio_writebytes ( + qse_pio_t* pio, /**< pio object */ + qse_pio_hid_t hid, /**< handle ID */ + const void* data, /**< data to write */ + qse_size_t size /**< data size */ +); + /** * The qse_pio_flush() flushes buffered data if #QSE_PIO_TEXT has been * specified to qse_pio_open() and qse_pio_init(). diff --git a/qse/lib/awk/Awk.cpp b/qse/lib/awk/Awk.cpp index 06d5fd5d..5d8b70eb 100644 --- a/qse/lib/awk/Awk.cpp +++ b/qse/lib/awk/Awk.cpp @@ -1800,11 +1800,9 @@ Awk::ssize_t Awk::writeSource ( } } -Awk::ssize_t Awk::pipeHandler ( - rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count) +Awk::ssize_t Awk::pipeHandler (rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, void* data, size_t count) { - rxtn_t* rxtn = (rxtn_t*) QSE_XTN (rtx); + rxtn_t* rxtn = (rxtn_t*)QSE_XTN(rtx); Awk* awk = rxtn->run->awk; QSE_ASSERT ((riod->type & 0xFF) == QSE_AWK_RIO_PIPE); @@ -1812,23 +1810,25 @@ Awk::ssize_t Awk::pipeHandler ( Pipe pipe (rxtn->run, riod); try - { + { if (awk->pipe_handler) { switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->pipe_handler->open (pipe); + return awk->pipe_handler->open(pipe); case QSE_AWK_RIO_CLOSE: - return awk->pipe_handler->close (pipe); - + return awk->pipe_handler->close(pipe); + case QSE_AWK_RIO_READ: - return awk->pipe_handler->read (pipe, data, count); + return awk->pipe_handler->read(pipe, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->pipe_handler->write (pipe, data, count); - + return awk->pipe_handler->write(pipe, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->pipe_handler->writeBytes(pipe, (const qse_mchar_t*)data, count); + case QSE_AWK_RIO_FLUSH: - return awk->pipe_handler->flush (pipe); + return awk->pipe_handler->flush(pipe); default: return -1; @@ -1839,17 +1839,19 @@ Awk::ssize_t Awk::pipeHandler ( switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->openPipe (pipe); + return awk->openPipe(pipe); case QSE_AWK_RIO_CLOSE: - return awk->closePipe (pipe); - + return awk->closePipe(pipe); + case QSE_AWK_RIO_READ: - return awk->readPipe (pipe, data, count); + return awk->readPipe(pipe, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->writePipe (pipe, data, count); + return awk->writePipe(pipe, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->writePipeBytes(pipe, (const qse_mchar_t*)data, count); case QSE_AWK_RIO_FLUSH: - return awk->flushPipe (pipe); + return awk->flushPipe(pipe); default: return -1; @@ -1862,11 +1864,9 @@ Awk::ssize_t Awk::pipeHandler ( } } -Awk::ssize_t Awk::fileHandler ( - rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count) +Awk::ssize_t Awk::fileHandler (rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, void* data, size_t count) { - rxtn_t* rxtn = (rxtn_t*) QSE_XTN (rtx); + rxtn_t* rxtn = (rxtn_t*)QSE_XTN(rtx); Awk* awk = rxtn->run->awk; QSE_ASSERT ((riod->type & 0xFF) == QSE_AWK_RIO_FILE); @@ -1880,17 +1880,19 @@ Awk::ssize_t Awk::fileHandler ( switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->file_handler->open (file); + return awk->file_handler->open(file); case QSE_AWK_RIO_CLOSE: - return awk->file_handler->close (file); - + return awk->file_handler->close(file); + case QSE_AWK_RIO_READ: - return awk->file_handler->read (file, data, count); + return awk->file_handler->read(file, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->file_handler->write (file, data, count); - + return awk->file_handler->write(file, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->file_handler->writeBytes(file, (const qse_mchar_t*)data, count); + case QSE_AWK_RIO_FLUSH: - return awk->file_handler->flush (file); + return awk->file_handler->flush(file); default: return -1; @@ -1901,17 +1903,19 @@ Awk::ssize_t Awk::fileHandler ( switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->openFile (file); + return awk->openFile(file); case QSE_AWK_RIO_CLOSE: - return awk->closeFile (file); + return awk->closeFile(file); case QSE_AWK_RIO_READ: - return awk->readFile (file, data, count); + return awk->readFile(file, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->writeFile (file, data, count); + return awk->writeFile(file, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->writeFileBytes(file, (const qse_mchar_t*)data, count); case QSE_AWK_RIO_FLUSH: - return awk->flushFile (file); + return awk->flushFile(file); default: return -1; @@ -1924,9 +1928,7 @@ Awk::ssize_t Awk::fileHandler ( } } -Awk::ssize_t Awk::consoleHandler ( - rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, - char_t* data, size_t count) +Awk::ssize_t Awk::consoleHandler (rtx_t* rtx, rio_cmd_t cmd, rio_arg_t* riod, void* data, size_t count) { rxtn_t* rxtn = (rxtn_t*) QSE_XTN (rtx); Awk* awk = rxtn->run->awk; @@ -1942,19 +1944,21 @@ Awk::ssize_t Awk::consoleHandler ( switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->console_handler->open (console); + return awk->console_handler->open(console); case QSE_AWK_RIO_CLOSE: - return awk->console_handler->close (console); + return awk->console_handler->close(console); case QSE_AWK_RIO_READ: - return awk->console_handler->read (console, data, count); + return awk->console_handler->read(console, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->console_handler->write (console, data, count); + return awk->console_handler->write(console, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->console_handler->writeBytes(console, (const qse_mchar_t*)data, count); case QSE_AWK_RIO_FLUSH: - return awk->console_handler->flush (console); + return awk->console_handler->flush(console); case QSE_AWK_RIO_NEXT: - return awk->console_handler->next (console); + return awk->console_handler->next(console); default: return -1; @@ -1965,19 +1969,21 @@ Awk::ssize_t Awk::consoleHandler ( switch (cmd) { case QSE_AWK_RIO_OPEN: - return awk->openConsole (console); + return awk->openConsole(console); case QSE_AWK_RIO_CLOSE: - return awk->closeConsole (console); + return awk->closeConsole(console); case QSE_AWK_RIO_READ: - return awk->readConsole (console, data, count); + return awk->readConsole(console, (qse_char_t*)data, count); case QSE_AWK_RIO_WRITE: - return awk->writeConsole (console, data, count); + return awk->writeConsole(console, (const qse_char_t*)data, count); + case QSE_AWK_RIO_WRITE_BYTES: + return awk->writeConsoleBytes(console, (const qse_mchar_t*)data, count); case QSE_AWK_RIO_FLUSH: - return awk->flushConsole (console); + return awk->flushConsole(console); case QSE_AWK_RIO_NEXT: - return awk->nextConsole (console); + return awk->nextConsole(console); default: return -1; @@ -1990,7 +1996,7 @@ Awk::ssize_t Awk::consoleHandler ( } } -int Awk::openPipe (Pipe& io) +int Awk::openPipe (Pipe& io) { ((Run*)io)->setError (QSE_AWK_ENOIMPL); return -1; diff --git a/qse/lib/awk/StdAwk.cpp b/qse/lib/awk/StdAwk.cpp index 73bef9af..af230479 100644 --- a/qse/lib/awk/StdAwk.cpp +++ b/qse/lib/awk/StdAwk.cpp @@ -664,8 +664,15 @@ StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len) StdAwk::ssize_t StdAwk::writePipe (Pipe& io, const char_t* buf, size_t len) { return (io.getUflags() > 0)? - qse_nwio_write ((qse_nwio_t*)io.getHandle(), buf, len): - qse_pio_write ((qse_pio_t*)io.getHandle(), QSE_PIO_IN, buf, len); + qse_nwio_write((qse_nwio_t*)io.getHandle(), buf, len): + qse_pio_write((qse_pio_t*)io.getHandle(), QSE_PIO_IN, buf, len); +} + +StdAwk::ssize_t StdAwk::writePipeBytes (Pipe& io, const qse_mchar_t* buf, size_t len) +{ + return (io.getUflags() > 0)? + qse_nwio_writebytes((qse_nwio_t*)io.getHandle(), buf, len): + qse_pio_writebytes((qse_pio_t*)io.getHandle(), QSE_PIO_IN, buf, len); } int StdAwk::flushPipe (Pipe& io) @@ -724,6 +731,12 @@ StdAwk::ssize_t StdAwk::writeFile (File& io, const char_t* buf, size_t len) return qse_sio_putstrn((qse_sio_t*)io.getHandle(), buf, len); } +StdAwk::ssize_t StdAwk::writeFileBytes (File& io, const qse_mchar_t* buf, size_t len) +{ + return qse_sio_putmbsn((qse_sio_t*)io.getHandle(), buf, len); +} + + int StdAwk::flushFile (File& io) { return qse_sio_flush((qse_sio_t*)io.getHandle()); @@ -1050,11 +1063,12 @@ StdAwk::ssize_t StdAwk::readConsole (Console& io, char_t* data, size_t size) StdAwk::ssize_t StdAwk::writeConsole (Console& io, const char_t* data, size_t size) { - return qse_sio_putstrn ( - (qse_sio_t*)io.getHandle(), - data, - size - ); + return qse_sio_putstrn((qse_sio_t*)io.getHandle(), data, size); +} + +StdAwk::ssize_t StdAwk::writeConsoleBytes (Console& io, const qse_mchar_t* data, size_t size) +{ + return qse_sio_putmbsn((qse_sio_t*)io.getHandle(), data, size); } int StdAwk::flushConsole (Console& io) diff --git a/qse/lib/awk/fnc.c b/qse/lib/awk/fnc.c index 6138967e..dae97275 100644 --- a/qse/lib/awk/fnc.c +++ b/qse/lib/awk/fnc.c @@ -1329,15 +1329,14 @@ int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) fbu_inited = 1; a0 = qse_awk_rtx_getarg (rtx, 0); - cs0.ptr = qse_awk_rtx_getvalstr (rtx, a0, &cs0.len); + cs0.ptr = qse_awk_rtx_getvalstr(rtx, a0, &cs0.len); if (cs0.ptr == QSE_NULL) goto oops; - x.ptr = qse_awk_rtx_format (rtx, - &out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len); + x.ptr = qse_awk_rtx_format(rtx, &out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len); qse_awk_rtx_freevalstr (rtx, a0, cs0.ptr); - if (x.ptr == QSE_NULL) goto oops; + if (!x.ptr) goto oops; - a0 = qse_awk_rtx_makestrvalwithxstr (rtx, &x); + a0 = qse_awk_rtx_makestrvalwithxstr(rtx, &x); if (a0 == QSE_NULL) goto oops; qse_str_fini (&fbu); diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index b1ec2182..72fd8a60 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -5692,8 +5692,10 @@ static int get_string ( return -1; } - #if !defined(QSE_CHAR_IS_MCHAR) - if (byte_only && c != '\\' && !QSE_AWK_BYTE_PRINTABLE(c)) + #if defined(QSE_CHAR_IS_MCHAR) + /* nothing extra to handle byte_only */ + #else + if (byte_only && c != QSE_T('\\') && !QSE_AWK_BYTE_PRINTABLE(c)) { qse_char_t wc = c; SETERR_ARG_LOC (awk, QSE_AWK_EMBSCHR, &wc, 1, &awk->tok.loc); @@ -5906,8 +5908,10 @@ static int get_single_quoted_string (qse_awk_t* awk, int byte_only, qse_awk_tok_ return -1; } - #if !defined(QSE_CHAR_IS_MCHAR) - if (byte_only && c != '\\' && !QSE_AWK_BYTE_PRINTABLE(c)) + #if defined(QSE_CHAR_IS_MCHAR) + /* nothing extra to handle byte_only */ + #else + if (byte_only && c != QSE_T('\\') && !QSE_AWK_BYTE_PRINTABLE(c)) { qse_char_t wc = c; SETERR_ARG_LOC (awk, QSE_AWK_EMBSCHR, &wc, 1, &awk->tok.loc); @@ -5915,7 +5919,6 @@ static int get_single_quoted_string (qse_awk_t* awk, int byte_only, qse_awk_tok_ } #endif - if (c == QSE_T('\'')) { /* terminating quote */ @@ -6195,7 +6198,7 @@ retry: ADD_TOKEN_STR (awk, tok, QSE_T(""), 5); SET_TOKEN_TYPE (awk, tok, TOK_EOF); - } + } else if (c == QSE_T('\n')) { /*ADD_TOKEN_CHAR (awk, tok, QSE_T('\n'));*/ @@ -6214,13 +6217,13 @@ retry: lc = awk->sio.last; GET_CHAR_TO (awk, c); - unget_char (awk, &awk->sio.last); + unget_char (awk, &awk->sio.last); awk->sio.last = lc; if (QSE_AWK_ISDIGIT(awk, c)) { /* for a token such as .123 */ - if (get_number (awk, tok) <= -1) return -1; + if (get_number(awk, tok) <= -1) return -1; } else { @@ -6249,9 +6252,7 @@ retry: ADD_TOKEN_CHAR (awk, tok, c); GET_CHAR_TO (awk, c); } - while (c == QSE_T('_') || - QSE_AWK_ISALPHA(awk, c) || - QSE_AWK_ISDIGIT(awk, c)); + while (c == QSE_T('_') || QSE_AWK_ISALPHA(awk, c) || QSE_AWK_ISDIGIT(awk, c)); type = classify_ident(awk, QSE_STR_XSTR(tok->name)); if (type == TOK_IDENT) @@ -6261,22 +6262,23 @@ retry: } SET_TOKEN_TYPE (awk, tok, type); } - else if (c == 'M') + else if (c == QSE_T('B')) { GET_CHAR_TO (awk, c); - if (c == '\"') + if (c == QSE_T('\"')) { - /* multi-byte string */ + /* multi-byte string/byte array */ SET_TOKEN_TYPE (awk, tok, TOK_MBS); if (get_string(awk, c, QSE_T('\\'), 0, 1, 0, tok) <= -1) return -1; } - else if (c == '\'') + else if (c == QSE_T('\'')) { SET_TOKEN_TYPE (awk, tok, TOK_MBS); if (get_single_quoted_string(awk, 1, tok) <= -1) return -1; } else { + ADD_TOKEN_CHAR (awk, tok, QSE_T('B')); goto process_identifier; } } diff --git a/qse/lib/awk/rec.c b/qse/lib/awk/rec.c index 0730d39c..99cbc6c9 100644 --- a/qse/lib/awk/rec.c +++ b/qse/lib/awk/rec.c @@ -131,7 +131,7 @@ static int split_record (qse_awk_rtx_t* rtx) } else { - fs_ptr = qse_awk_rtx_valtostrdup (rtx, fs, &fs_len); + fs_ptr = qse_awk_rtx_valtostrdup(rtx, fs, &fs_len); if (fs_ptr == QSE_NULL) return -1; fs_free = fs_ptr; } diff --git a/qse/lib/awk/rio.c b/qse/lib/awk/rio.c index c77b1b17..d6c9601e 100644 --- a/qse/lib/awk/rio.c +++ b/qse/lib/awk/rio.c @@ -196,8 +196,7 @@ static int find_rio_in ( return 0; } -static QSE_INLINE int resolve_rs ( - qse_awk_rtx_t* rtx, qse_awk_val_t* rs, qse_cstr_t* rrs) +static QSE_INLINE int resolve_rs (qse_awk_rtx_t* rtx, qse_awk_val_t* rs, qse_cstr_t* rrs) { int ret = 0; qse_awk_val_type_t rs_vtype; @@ -226,8 +225,7 @@ static QSE_INLINE int resolve_rs ( return ret; } -static QSE_INLINE int match_long_rs ( - qse_awk_rtx_t* run, qse_str_t* buf, qse_awk_rio_arg_t* p) +static QSE_INLINE int match_long_rs (qse_awk_rtx_t* run, qse_str_t* buf, qse_awk_rio_arg_t* p) { qse_cstr_t match; qse_awk_errnum_t errnum; @@ -295,9 +293,7 @@ static QSE_INLINE int match_long_rs ( return ret; } -int qse_awk_rtx_readio ( - qse_awk_rtx_t* run, int in_type, - const qse_char_t* name, qse_str_t* buf) +int qse_awk_rtx_readio (qse_awk_rtx_t* run, int in_type, const qse_char_t* name, qse_str_t* buf) { qse_awk_rio_arg_t* p; qse_awk_rio_impl_t handler; @@ -554,7 +550,7 @@ int qse_awk_rtx_readio ( } else line_len++; - if (qse_str_ccat (buf, c) == (qse_size_t)-1) + if (qse_str_ccat(buf, c) == (qse_size_t)-1) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); ret = -1; @@ -640,56 +636,45 @@ int qse_awk_rtx_readio ( return ret; } -int qse_awk_rtx_writeio_val ( - qse_awk_rtx_t* run, int out_type, - const qse_char_t* name, qse_awk_val_t* v) +int qse_awk_rtx_writeioval (qse_awk_rtx_t* run, int out_type, const qse_char_t* name, qse_awk_val_t* v) { - qse_char_t* str; - qse_size_t len; - int n; qse_awk_val_type_t vtype; - - vtype = QSE_AWK_RTX_GETVALTYPE (run, v); - if (vtype == QSE_AWK_VAL_STR) + switch (vtype) { - str = ((qse_awk_val_str_t*)v)->val.ptr; - len = ((qse_awk_val_str_t*)v)->val.len; - } - else - { - qse_awk_rtx_valtostr_out_t out; + case QSE_AWK_VAL_STR: + return qse_awk_rtx_writeiostr(run, out_type, name, ((qse_awk_val_str_t*)v)->val.ptr, ((qse_awk_val_str_t*)v)->val.len); - out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP | - QSE_AWK_RTX_VALTOSTR_PRINT; - if (qse_awk_rtx_valtostr (run, v, &out) <= -1) return -1; + case QSE_AWK_VAL_MBS: + return qse_awk_rtx_writeiobytes(run, out_type, name, ((qse_awk_val_mbs_t*)v)->val.ptr, ((qse_awk_val_mbs_t*)v)->val.len); - str = out.u.cpldup.ptr; - len = out.u.cpldup.len; - } + default: + { + qse_awk_rtx_valtostr_out_t out; + int n; - n = qse_awk_rtx_writeio_str (run, out_type, name, str, len); - - if (vtype == QSE_AWK_VAL_STR) - { - /* nothing to free */ + out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP | QSE_AWK_RTX_VALTOSTR_PRINT; + if (qse_awk_rtx_valtostr(run, v, &out) <= -1) return -1; + n = qse_awk_rtx_writeiostr(run, out_type, name, out.u.cpldup.ptr, out.u.cpldup.len); + QSE_AWK_FREE (run->awk, out.u.cpldup.ptr); + return n; + } } - else - { - QSE_AWK_FREE (run->awk, str); - } - return n; } -int qse_awk_rtx_writeio_str ( - qse_awk_rtx_t* run, int out_type, - const qse_char_t* name, qse_char_t* str, qse_size_t len) +struct write_io_data_t { - qse_awk_rio_arg_t* p = run->rio.chain; + qse_awk_rio_arg_t* p; qse_awk_rio_impl_t handler; - int io_type, io_mode, io_mask; - qse_ssize_t n; +}; +typedef struct write_io_data_t write_io_data_t; + +static int prepare_for_write_io_data (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, write_io_data_t* wid) +{ + qse_awk_rio_arg_t* p = rtx->rio.chain; + qse_awk_rio_impl_t handler; + int io_type, io_mode, io_mask, n; QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_type_map)); QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mode_map)); @@ -700,11 +685,11 @@ int qse_awk_rtx_writeio_str ( io_mode = out_mode_map[out_type]; io_mask = out_mask_map[out_type]; - handler = run->rio.handler[io_type]; + handler = rtx->rio.handler[io_type]; if (handler == QSE_NULL) { /* no I/O handler provided */ - qse_awk_rtx_seterrnum (run, QSE_AWK_EIOUSER, QSE_NULL); + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL); return -1; } @@ -721,29 +706,27 @@ int qse_awk_rtx_writeio_str ( * print "1111" >> "1.tmp" * print "1111" > "1.tmp" */ - if (p->type == (io_type | io_mask) && - qse_strcmp (p->name, name) == 0) break; + if (p->type == (io_type | io_mask) && qse_strcmp(p->name, name) == 0) break; p = p->next; } /* if there is not corresponding rio for name, create one */ if (p == QSE_NULL) { - p = (qse_awk_rio_arg_t*) QSE_AWK_ALLOC ( - run->awk, QSE_SIZEOF(qse_awk_rio_arg_t)); + p = (qse_awk_rio_arg_t*)QSE_AWK_ALLOC(rtx->awk, QSE_SIZEOF(qse_awk_rio_arg_t)); if (p == QSE_NULL) { - qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return -1; } QSE_MEMSET (p, 0, QSE_SIZEOF(*p)); - p->name = QSE_AWK_STRDUP (run->awk, name); + p->name = QSE_AWK_STRDUP(rtx->awk, name); if (p->name == QSE_NULL) { - QSE_AWK_FREE (run->awk, p); - qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); + QSE_AWK_FREE (rtx->awk, p); + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return -1; } @@ -759,52 +742,55 @@ int qse_awk_rtx_writeio_str ( p->out.eos = 0; */ - qse_awk_rtx_seterrnum (run, QSE_AWK_ENOERR, QSE_NULL); - n = handler (run, QSE_AWK_RIO_OPEN, p, QSE_NULL, 0); + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL); + n = handler(rtx, QSE_AWK_RIO_OPEN, p, QSE_NULL, 0); if (n <= -1) { - QSE_AWK_FREE (run->awk, p->name); - QSE_AWK_FREE (run->awk, p); + QSE_AWK_FREE (rtx->awk, p->name); + QSE_AWK_FREE (rtx->awk, p); - if (run->errinf.num == QSE_AWK_ENOERR) - qse_awk_rtx_seterrnum (run, QSE_AWK_EIOIMPL, QSE_NULL); + if (rtx->errinf.num == QSE_AWK_ENOERR) + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL); return -1; } /* chain it */ - p->next = run->rio.chain; - run->rio.chain = p; + p->next = rtx->rio.chain; + rtx->rio.chain = p; } - if (p->out.eos) - { - /* no more streams */ - return 0; - } + if (p->out.eos) return 0; /* no more streams */ + if (p->out.eof) return 0; /* it has reached the end of the stream but this function has been recalled */ - if (p->out.eof) - { - /* it has reached the end of the stream but this function - * has been recalled */ - return 0; - } + wid->handler = handler; + wid->p = p; + return 1; +} + +int qse_awk_rtx_writeiostr (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, qse_char_t* str, qse_size_t len) +{ + int x; + write_io_data_t wid; + + if ((x = prepare_for_write_io_data(rtx, out_type, name, &wid)) <= 0) return x; while (len > 0) { - qse_awk_rtx_seterrnum (run, QSE_AWK_ENOERR, QSE_NULL); - n = handler (run, QSE_AWK_RIO_WRITE, p, str, len); + qse_ssize_t n; + + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL); + n = wid.handler(rtx, QSE_AWK_RIO_WRITE, wid.p, str, len); if (n <= -1) { - if (run->errinf.num == QSE_AWK_ENOERR) - qse_awk_rtx_seterrnum (run, QSE_AWK_EIOIMPL, QSE_NULL); - + if (rtx->errinf.num == QSE_AWK_ENOERR) + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL); return -1; } if (n == 0) { - p->out.eof = 1; + wid.p->out.eof = 1; return 0; } @@ -815,8 +801,40 @@ int qse_awk_rtx_writeio_str ( return 1; } -int qse_awk_rtx_flushio ( - qse_awk_rtx_t* run, int out_type, const qse_char_t* name) +int qse_awk_rtx_writeiobytes (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, qse_mchar_t* str, qse_size_t len) +{ + int x; + write_io_data_t wid; + + if ((x = prepare_for_write_io_data(rtx, out_type, name, &wid)) <= 0) return x; + + while (len > 0) + { + qse_ssize_t n; + + qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL); + n = wid.handler(rtx, QSE_AWK_RIO_WRITE_BYTES, wid.p, str, len); + if (n <= -1) + { + if (rtx->errinf.num == QSE_AWK_ENOERR) + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL); + return -1; + } + + if (n == 0) + { + wid.p->out.eof = 1; + return 0; + } + + len -= n; + str += n; + } + + return 1; +} + +int qse_awk_rtx_flushio (qse_awk_rtx_t* run, int out_type, const qse_char_t* name) { qse_awk_rio_arg_t* p = run->rio.chain; qse_awk_rio_impl_t handler; @@ -852,7 +870,7 @@ int qse_awk_rtx_flushio ( (name == QSE_NULL || qse_strcmp(p->name,name) == 0)) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOERR, QSE_NULL); - n = handler (run, QSE_AWK_RIO_FLUSH, p, QSE_NULL, 0); + n = handler(run, QSE_AWK_RIO_FLUSH, p, QSE_NULL, 0); if (n <= -1) { diff --git a/qse/lib/awk/rio.h b/qse/lib/awk/rio.h index 47cf7177..d0fafc7f 100644 --- a/qse/lib/awk/rio.h +++ b/qse/lib/awk/rio.h @@ -35,14 +35,18 @@ int qse_awk_rtx_readio ( qse_awk_rtx_t* run, int in_type, const qse_char_t* name, qse_str_t* buf); -int qse_awk_rtx_writeio_val ( +int qse_awk_rtx_writeioval ( qse_awk_rtx_t* run, int out_type, const qse_char_t* name, qse_awk_val_t* v); -int qse_awk_rtx_writeio_str ( +int qse_awk_rtx_writeiostr ( qse_awk_rtx_t* run, int out_type, const qse_char_t* name, qse_char_t* str, qse_size_t len); +int qse_awk_rtx_writeiobytes ( + qse_awk_rtx_t* run, int out_type, + const qse_char_t* name, qse_mchar_t* str, qse_size_t len); + int qse_awk_rtx_flushio ( qse_awk_rtx_t* run, int out_type, const qse_char_t* name); diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 3d88ed84..df5505e6 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -1884,7 +1884,7 @@ static int run_block0 (qse_awk_rtx_t* rtx, qse_awk_nde_blk_t* nde) /* blockless pattern - execute print $0*/ qse_awk_rtx_refupval (rtx, rtx->inrec.d0); - n = qse_awk_rtx_writeio_str (rtx, + n = qse_awk_rtx_writeiostr (rtx, QSE_AWK_OUT_CONSOLE, QSE_T(""), QSE_STR_PTR(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line)); @@ -1895,7 +1895,7 @@ static int run_block0 (qse_awk_rtx_t* rtx, qse_awk_nde_blk_t* nde) return -1; } - n = qse_awk_rtx_writeio_str ( + n = qse_awk_rtx_writeiostr ( rtx, QSE_AWK_OUT_CONSOLE, QSE_T(""), rtx->gbl.ors.ptr, rtx->gbl.ors.len); if (n == -1) @@ -2893,12 +2893,12 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) qse_size_t len; /* if so, resolve the destination name */ - out_v = eval_expression (rtx, nde->out); + out_v = eval_expression(rtx, nde->out); if (!out_v) return -1; qse_awk_rtx_refupval (rtx, out_v); - out = qse_awk_rtx_getvalstr (rtx, out_v, &len); + out = qse_awk_rtx_getvalstr(rtx, out_v, &len); if (!out) goto oops; if (len <= 0) @@ -2935,10 +2935,7 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) { /* if it doesn't have any arguments, print the entire * input record */ - n = qse_awk_rtx_writeio_str ( - rtx, nde->out_type, dst, - QSE_STR_PTR(&rtx->inrec.line), - QSE_STR_LEN(&rtx->inrec.line)); + n = qse_awk_rtx_writeiostr(rtx, nde->out_type, dst, QSE_STR_PTR(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line)); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) { if (rtx->awk->opt.trait & QSE_AWK_TOLERANT) @@ -2970,10 +2967,7 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) { if (np != head) { - n = qse_awk_rtx_writeio_str ( - rtx, nde->out_type, dst, - rtx->gbl.ofs.ptr, - rtx->gbl.ofs.len); + n = qse_awk_rtx_writeiostr(rtx, nde->out_type, dst, rtx->gbl.ofs.ptr, rtx->gbl.ofs.len); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) { if (rtx->awk->opt.trait & QSE_AWK_TOLERANT) @@ -2987,11 +2981,11 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) } } - v = eval_expression (rtx, np); + v = eval_expression(rtx, np); if (v == QSE_NULL) goto oops_1; qse_awk_rtx_refupval (rtx, v); - n = qse_awk_rtx_writeio_val (rtx, nde->out_type, dst, v); + n = qse_awk_rtx_writeioval(rtx, nde->out_type, dst, v); qse_awk_rtx_refdownval (rtx, v); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) @@ -3009,9 +3003,7 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) } /* print the value ORS to terminate the operation */ - n = qse_awk_rtx_writeio_str ( - rtx, nde->out_type, dst, - rtx->gbl.ors.ptr, rtx->gbl.ors.len); + n = qse_awk_rtx_writeiostr(rtx, nde->out_type, dst, rtx->gbl.ors.ptr, rtx->gbl.ors.len); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) { if (rtx->awk->opt.trait & QSE_AWK_TOLERANT) @@ -3124,7 +3116,7 @@ static int run_printf (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) { /* the remaining arguments are ignored as the format cannot * contain any % characters. e.g. printf (1, "xxxx") */ - n = qse_awk_rtx_writeio_val (rtx, nde->out_type, dst, v); + n = qse_awk_rtx_writeioval(rtx, nde->out_type, dst, v); qse_awk_rtx_refdownval (rtx, v); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) @@ -3151,7 +3143,7 @@ static int run_printf (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) } } - if (qse_awk_rtx_flushio (rtx, nde->out_type, dst) <= -1) + if (qse_awk_rtx_flushio(rtx, nde->out_type, dst) <= -1) { if (rtx->awk->opt.trait & QSE_AWK_TOLERANT) xret = PRINT_IOERR; else goto oops_1; @@ -3186,11 +3178,10 @@ static int output_formatted ( qse_size_t len; int n; - ptr = qse_awk_rtx_format ( - rtx, QSE_NULL, QSE_NULL, fmt, fmt_len, 0, args, &len); + ptr = qse_awk_rtx_format(rtx, QSE_NULL, QSE_NULL, fmt, fmt_len, 0, args, &len); if (ptr == QSE_NULL) return -1; - n = qse_awk_rtx_writeio_str (rtx, out_type, dst, ptr, len); + n = qse_awk_rtx_writeiostr(rtx, out_type, dst, ptr, len); if (n <= -1 /*&& rtx->errinf.num != QSE_AWK_EIOIMPL*/) { if (rtx->awk->opt.trait & QSE_AWK_TOLERANT) diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 24082386..584802f6 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -987,10 +987,9 @@ static qse_ssize_t sf_out ( ea.len = qse_strlen(ea.ptr); qse_awk_seterrnum (awk, QSE_AWK_EWRITE, &ea); } - return n; } - + case QSE_AWK_PARSESTD_STR: { if (size > QSE_TYPE_MAX(qse_ssize_t)) size = QSE_TYPE_MAX(qse_ssize_t); @@ -1015,8 +1014,7 @@ static qse_ssize_t sf_out ( return -1; } -int qse_awk_parsestd ( - qse_awk_t* awk, qse_awk_parsestd_t in[], qse_awk_parsestd_t* out) +int qse_awk_parsestd (qse_awk_t* awk, qse_awk_parsestd_t in[], qse_awk_parsestd_t* out) { qse_awk_sio_t sio; xtn_t* xtn = (xtn_t*) QSE_XTN (awk); @@ -1064,9 +1062,7 @@ int qse_awk_parsestd ( /*** RTX_OPENSTD ***/ -static qse_ssize_t nwio_handler_open ( - qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod, int flags, - qse_nwad_t* nwad, qse_nwio_tmout_t* tmout) +static qse_ssize_t nwio_handler_open (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod, int flags, qse_nwad_t* nwad, qse_nwio_tmout_t* tmout) { qse_nwio_t* handle; @@ -1090,39 +1086,30 @@ static qse_ssize_t nwio_handler_open ( return 1; } -static qse_ssize_t nwio_handler_rest ( - qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, - qse_char_t* data, qse_size_t size) +static qse_ssize_t nwio_handler_rest (qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, void* data, qse_size_t size) { switch (cmd) { case QSE_AWK_RIO_OPEN: - { qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL); return -1; - } case QSE_AWK_RIO_CLOSE: - { qse_nwio_close ((qse_nwio_t*)riod->handle); return 0; - } case QSE_AWK_RIO_READ: - { - return qse_nwio_read ((qse_nwio_t*)riod->handle, data, size); - } + return qse_nwio_read((qse_nwio_t*)riod->handle, data, size); case QSE_AWK_RIO_WRITE: - { - return qse_nwio_write ((qse_nwio_t*)riod->handle, data, size); - } + return qse_nwio_write((qse_nwio_t*)riod->handle, data, size); + + case QSE_AWK_RIO_WRITE_BYTES: + return qse_nwio_writebytes((qse_nwio_t*)riod->handle, data, size); case QSE_AWK_RIO_FLUSH: - { /*if (riod->mode == QSE_AWK_RIO_PIPE_READ) return -1;*/ - return qse_nwio_flush ((qse_nwio_t*)riod->handle); - } + return qse_nwio_flush((qse_nwio_t*)riod->handle); case QSE_AWK_RIO_NEXT: break; @@ -1162,8 +1149,7 @@ static int parse_rwpipe_uri (const qse_char_t* uri, int* flags, qse_nwad_t* nwad return -1; } -static qse_ssize_t pio_handler_open ( - qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) +static qse_ssize_t pio_handler_open (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) { qse_pio_t* handle; int flags; @@ -1171,8 +1157,7 @@ static qse_ssize_t pio_handler_open ( if (riod->mode == QSE_AWK_RIO_PIPE_READ) { /* TODO: should ERRTOOUT be unset? */ - flags = QSE_PIO_READOUT | - QSE_PIO_ERRTOOUT; + flags = QSE_PIO_READOUT | QSE_PIO_ERRTOOUT; } else if (riod->mode == QSE_AWK_RIO_PIPE_WRITE) { @@ -1180,9 +1165,7 @@ static qse_ssize_t pio_handler_open ( } else if (riod->mode == QSE_AWK_RIO_PIPE_RW) { - flags = QSE_PIO_READOUT | - QSE_PIO_ERRTOOUT | - QSE_PIO_WRITEIN; + flags = QSE_PIO_READOUT | QSE_PIO_ERRTOOUT | QSE_PIO_WRITEIN; } else { @@ -1196,7 +1179,7 @@ static qse_ssize_t pio_handler_open ( 0, riod->name, QSE_NULL, - flags|QSE_PIO_SHELL|QSE_PIO_TEXT|QSE_PIO_IGNOREMBWCERR + flags | QSE_PIO_SHELL | QSE_PIO_TEXT | QSE_PIO_IGNOREMBWCERR ); if (handle == QSE_NULL) return -1; @@ -1217,17 +1200,13 @@ static qse_ssize_t pio_handler_open ( return 1; } -static qse_ssize_t pio_handler_rest ( - qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, - qse_char_t* data, qse_size_t size) +static qse_ssize_t pio_handler_rest (qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, void* data, qse_size_t size) { switch (cmd) { case QSE_AWK_RIO_OPEN: - { qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL); return -1; - } case QSE_AWK_RIO_CLOSE: { @@ -1254,26 +1233,17 @@ static qse_ssize_t pio_handler_rest ( } case QSE_AWK_RIO_READ: - { - return qse_pio_read ( - (qse_pio_t*)riod->handle, - QSE_PIO_OUT, data, size - ); - } + return qse_pio_read ((qse_pio_t*)riod->handle, QSE_PIO_OUT, data, size); case QSE_AWK_RIO_WRITE: - { - return qse_pio_write ( - (qse_pio_t*)riod->handle, - QSE_PIO_IN, data, size - ); - } + return qse_pio_write((qse_pio_t*)riod->handle, QSE_PIO_IN, data, size); + + case QSE_AWK_RIO_WRITE_BYTES: + return qse_pio_writebytes((qse_pio_t*)riod->handle, QSE_PIO_IN, data, size); case QSE_AWK_RIO_FLUSH: - { /*if (riod->mode == QSE_AWK_RIO_PIPE_READ) return -1;*/ return qse_pio_flush ((qse_pio_t*)riod->handle, QSE_PIO_IN); - } case QSE_AWK_RIO_NEXT: break; @@ -1283,9 +1253,7 @@ static qse_ssize_t pio_handler_rest ( return -1; } -static qse_ssize_t awk_rio_pipe ( - qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, - qse_char_t* data, qse_size_t size) +static qse_ssize_t awk_rio_pipe (qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, void* data, qse_size_t size) { if (cmd == QSE_AWK_RIO_OPEN) { @@ -1320,14 +1288,12 @@ static qse_ssize_t awk_rio_pipe ( } } else if (riod->uflags > 0) - return nwio_handler_rest (rtx, cmd, riod, data, size); + return nwio_handler_rest(rtx, cmd, riod, data, size); else - return pio_handler_rest (rtx, cmd, riod, data, size); + return pio_handler_rest(rtx, cmd, riod, data, size); } -static qse_ssize_t awk_rio_file ( - qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, - qse_char_t* data, qse_size_t size) +static qse_ssize_t awk_rio_file (qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, void* data, qse_size_t size) { switch (cmd) { @@ -1342,13 +1308,10 @@ static qse_ssize_t awk_rio_file ( flags |= QSE_SIO_READ; break; case QSE_AWK_RIO_FILE_WRITE: - flags |= QSE_SIO_WRITE | - QSE_SIO_CREATE | - QSE_SIO_TRUNCATE; + flags |= QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE; break; case QSE_AWK_RIO_FILE_APPEND: - flags |= QSE_SIO_APPEND | - QSE_SIO_CREATE; + flags |= QSE_SIO_APPEND | QSE_SIO_CREATE; break; default: /* this must not happen */ @@ -1366,52 +1329,36 @@ static qse_ssize_t awk_rio_file ( return -1; } -#if defined(QSE_CHAR_IS_WCHAR) + #if defined(QSE_CHAR_IS_WCHAR) { qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name); - if (cmgr) qse_sio_setcmgr (handle, cmgr); + if (cmgr) qse_sio_setcmgr(handle, cmgr); } -#endif + #endif riod->handle = (void*)handle; return 1; } case QSE_AWK_RIO_CLOSE: - { qse_sio_close ((qse_sio_t*)riod->handle); riod->handle = QSE_NULL; return 0; - } case QSE_AWK_RIO_READ: - { - return qse_sio_getstrn ( - (qse_sio_t*)riod->handle, - data, - size - ); - } + return qse_sio_getstrn((qse_sio_t*)riod->handle, data, size); case QSE_AWK_RIO_WRITE: - { - return qse_sio_putstrn ( - (qse_sio_t*)riod->handle, - data, - size - ); - } + return qse_sio_putstrn((qse_sio_t*)riod->handle, data, size); + + case QSE_AWK_RIO_WRITE_BYTES: + return qse_sio_putmbsn((qse_sio_t*)riod->handle, data, size); case QSE_AWK_RIO_FLUSH: - { - return qse_sio_flush ((qse_sio_t*)riod->handle); - } + return qse_sio_flush((qse_sio_t*)riod->handle); case QSE_AWK_RIO_NEXT: - { return -1; - } - } return -1; @@ -1424,7 +1371,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) if (riod->mode == QSE_AWK_RIO_CONSOLE_READ) { - xtn_t* xtn = (xtn_t*)QSE_XTN (rtx->awk); + xtn_t* xtn = (xtn_t*)QSE_XTN(rtx->awk); if (rxtn->c.in.files == QSE_NULL) { @@ -1646,9 +1593,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) return -1; } -static qse_ssize_t awk_rio_console ( - qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, - qse_char_t* data, qse_size_t size) +static qse_ssize_t awk_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod, void* data, qse_size_t size) { switch (cmd) { @@ -1663,20 +1608,20 @@ static qse_ssize_t awk_rio_console ( { qse_ssize_t nn; - while ((nn = qse_sio_getstrn((qse_sio_t*)riod->handle,data,size)) == 0) + while ((nn = qse_sio_getstrn((qse_sio_t*)riod->handle, data, size)) == 0) { int n; qse_sio_t* sio = (qse_sio_t*)riod->handle; - - n = open_rio_console (rtx, riod); + + n = open_rio_console(rtx, riod); if (n <= -1) return -1; - + if (n == 0) { /* no more input console */ return 0; } - + if (sio) qse_sio_close (sio); } @@ -1684,23 +1629,22 @@ static qse_ssize_t awk_rio_console ( } case QSE_AWK_RIO_WRITE: - return qse_sio_putstrn ( - (qse_sio_t*)riod->handle, - data, - size - ); + return qse_sio_putstrn((qse_sio_t*)riod->handle, data, size); + + case QSE_AWK_RIO_WRITE_BYTES: + return qse_sio_putmbsn((qse_sio_t*)riod->handle, data, size); case QSE_AWK_RIO_FLUSH: - return qse_sio_flush ((qse_sio_t*)riod->handle); + return qse_sio_flush((qse_sio_t*)riod->handle); case QSE_AWK_RIO_NEXT: { int n; qse_sio_t* sio = (qse_sio_t*)riod->handle; - + n = open_rio_console (rtx, riod); if (n <= -1) return -1; - + if (n == 0) { /* if there is no more file, keep the previous handle */ diff --git a/qse/lib/awk/tree.c b/qse/lib/awk/tree.c index 7999b4f1..0af59bc1 100644 --- a/qse/lib/awk/tree.c +++ b/qse/lib/awk/tree.c @@ -422,7 +422,7 @@ static int print_expr (qse_awk_t* awk, qse_awk_nde_t* nde) qse_mchar_t* ptr; qse_size_t len, i; - PUT_SRCSTR (awk, QSE_T("M\"")); + PUT_SRCSTR (awk, QSE_T("B\"")); ptr = ((qse_awk_nde_mbs_t*)nde)->ptr; len = ((qse_awk_nde_mbs_t*)nde)->len; for (i = 0; i < len; i++) diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 8c00d6f5..ede25fca 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -1303,8 +1303,7 @@ static int val_flt_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_flt_t* v, qse_a } fbu_inited = 1; - tmp = qse_awk_rtx_format(rtx, &buf, &fbu, tmp, tmp_len, - (qse_size_t)-1, (qse_awk_nde_t*)v, &tmp_len); + tmp = qse_awk_rtx_format(rtx, &buf, &fbu, tmp, tmp_len, (qse_size_t)-1, (qse_awk_nde_t*)v, &tmp_len); if (tmp == QSE_NULL) goto oops; switch (type) diff --git a/qse/lib/si/nwio.c b/qse/lib/si/nwio.c index 9e68c107..ded132e9 100644 --- a/qse/lib/si/nwio.c +++ b/qse/lib/si/nwio.c @@ -1599,14 +1599,14 @@ qse_ssize_t qse_nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t size) { if (nwio->tio == QSE_NULL) { - return nwio_write (nwio, data, size); + return nwio_write(nwio, data, size); } else { qse_ssize_t n; - nwio->errnum = QSE_NWIO_ENOERR; - n = qse_tio_write (nwio->tio, data, size); + nwio->errnum = QSE_NWIO_ENOERR; + n = qse_tio_write(nwio->tio, data, size); if (n <= -1 && nwio->errnum == QSE_NWIO_ENOERR) nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio); @@ -1614,6 +1614,16 @@ qse_ssize_t qse_nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t size) } } + +qse_ssize_t qse_nwio_writebytes (qse_nwio_t* nwio, const void* data, qse_size_t size) +{ + if (nwio->tio == QSE_NULL) + return nwio_write(nwio, data, size); + else + return qse_tio_writembs(nwio->tio, data, size); +} + + /* ---------------------------------------------------------- */ static qse_ssize_t socket_input ( diff --git a/qse/lib/si/pio.c b/qse/lib/si/pio.c index 26ffb5ba..6ceb4dd9 100644 --- a/qse/lib/si/pio.c +++ b/qse/lib/si/pio.c @@ -131,8 +131,7 @@ static void free_param (qse_pio_t* pio, param_t* param) if (param->mcmd) QSE_MMGR_FREE (pio->mmgr, param->mcmd); } -static int make_param ( - qse_pio_t* pio, const qse_char_t* cmd, int flags, param_t* param) +static int make_param (qse_pio_t* pio, const qse_char_t* cmd, int flags, param_t* param) { #if defined(QSE_CHAR_IS_MCHAR) qse_mchar_t* mcmd = QSE_NULL; @@ -1876,8 +1875,7 @@ qse_pio_pid_t qse_pio_getchild (const qse_pio_t* pio) return pio->child; } -static qse_ssize_t pio_read ( - qse_pio_t* pio, void* buf, qse_size_t size, qse_pio_hnd_t hnd) +static qse_ssize_t pio_read (qse_pio_t* pio, void* buf, qse_size_t size, qse_pio_hnd_t hnd) { #if defined(_WIN32) DWORD count; @@ -1942,7 +1940,7 @@ static qse_ssize_t pio_read ( size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t); reread: - n = QSE_READ (hnd, buf, size); + n = QSE_READ(hnd, buf, size); if (n <= -1) { if (errno == EINTR) @@ -1961,17 +1959,16 @@ reread: #endif } -qse_ssize_t qse_pio_read ( - qse_pio_t* pio, qse_pio_hid_t hid, void* buf, qse_size_t size) +qse_ssize_t qse_pio_read (qse_pio_t* pio, qse_pio_hid_t hid, void* buf, qse_size_t size) { if (pio->pin[hid].tio == QSE_NULL) - return pio_read (pio, buf, size, pio->pin[hid].handle); + return pio_read(pio, buf, size, pio->pin[hid].handle); else { qse_ssize_t n; pio->errnum = QSE_PIO_ENOERR; - n = qse_tio_read (pio->pin[hid].tio, buf, size); + n = qse_tio_read(pio->pin[hid].tio, buf, size); if (n <= -1 && pio->errnum == QSE_PIO_ENOERR) pio->errnum = tio_errnum_to_pio_errnum (pio->pin[hid].tio); @@ -1979,8 +1976,7 @@ qse_ssize_t qse_pio_read ( } } -static qse_ssize_t pio_write ( - qse_pio_t* pio, const void* data, qse_size_t size, qse_pio_hnd_t hnd) +static qse_ssize_t pio_write (qse_pio_t* pio, const void* data, qse_size_t size, qse_pio_hnd_t hnd) { #if defined(_WIN32) DWORD count; @@ -2059,18 +2055,16 @@ rewrite: #endif } -qse_ssize_t qse_pio_write ( - qse_pio_t* pio, qse_pio_hid_t hid, - const void* data, qse_size_t size) +qse_ssize_t qse_pio_write (qse_pio_t* pio, qse_pio_hid_t hid, const void* data, qse_size_t size) { if (pio->pin[hid].tio == QSE_NULL) - return pio_write (pio, data, size, pio->pin[hid].handle); + return pio_write(pio, data, size, pio->pin[hid].handle); else { qse_ssize_t n; pio->errnum = QSE_PIO_ENOERR; - n = qse_tio_write (pio->pin[hid].tio, data, size); + n = qse_tio_write(pio->pin[hid].tio, data, size); if (n <= -1 && pio->errnum == QSE_PIO_ENOERR) pio->errnum = tio_errnum_to_pio_errnum (pio->pin[hid].tio); @@ -2078,6 +2072,14 @@ qse_ssize_t qse_pio_write ( } } +qse_ssize_t qse_pio_writebytes (qse_pio_t* pio, qse_pio_hid_t hid, const void* data, qse_size_t size) +{ + if (pio->pin[hid].tio == QSE_NULL) + return pio_write(pio, data, size, pio->pin[hid].handle); + else + return qse_tio_writembs(pio->pin[hid].tio, data, size); +} + qse_ssize_t qse_pio_flush (qse_pio_t* pio, qse_pio_hid_t hid) { qse_ssize_t n; @@ -2085,9 +2087,9 @@ qse_ssize_t qse_pio_flush (qse_pio_t* pio, qse_pio_hid_t hid) if (pio->pin[hid].tio == QSE_NULL) return 0; pio->errnum = QSE_PIO_ENOERR; - n = qse_tio_flush (pio->pin[hid].tio); + n = qse_tio_flush(pio->pin[hid].tio); if (n <= -1 && pio->errnum == QSE_PIO_ENOERR) - pio->errnum = tio_errnum_to_pio_errnum (pio->pin[hid].tio); + pio->errnum = tio_errnum_to_pio_errnum(pio->pin[hid].tio); return n; } @@ -2346,8 +2348,7 @@ int qse_pio_kill (qse_pio_t* pio) #endif } -static qse_ssize_t pio_input ( - qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) +static qse_ssize_t pio_input (qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) { if (cmd == QSE_TIO_DATA) { @@ -2363,8 +2364,7 @@ static qse_ssize_t pio_input ( return 0; } -static qse_ssize_t pio_output ( - qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) +static qse_ssize_t pio_output (qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) { if (cmd == QSE_TIO_DATA) {