From 531de7353543f24df4f48b11ef58c904ecbaffd6 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 3 Oct 2025 00:21:18 +0900 Subject: [PATCH] removed hawk-std.h and merged the text to hawk.h --- README.md | 34 ++--- bin/cut.c | 1 - bin/hawk.c | 2 +- bin/main.c | 1 - bin/sed.c | 1 - lib/Makefile.am | 1 - lib/Makefile.in | 53 ++++---- lib/Sed.cpp | 1 - lib/Std.cpp | 1 - lib/hawk-cmn.h | 9 ++ lib/hawk-std.h | 322 ----------------------------------------------- lib/hawk.h | 270 +++++++++++++++++++++++++++++++++++++++ lib/std-cut.c | 1 - lib/std-json.c | 2 +- lib/std-sed.c | 1 - lib/std.c | 2 - samples/hawk02.c | 2 +- t/t-004.c | 2 +- t/t-008.c | 2 +- t/t-009.c | 2 +- 20 files changed, 330 insertions(+), 380 deletions(-) delete mode 100644 lib/hawk-std.h diff --git a/README.md b/README.md index 7b1e068f..3dd43f94 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Hawk - Embeddable AWK Interpreter in C/C++ -HAWK is a stable and embeddable **AWK interpreter written in C**. +`Hawk` is a stable and embeddable **AWK interpreter written in C**. It can run AWK scripts inside your own applications or as a standalone AWK engine. The library is stable, portable, and designed for projects that need a scripting engine with a small footprint. @@ -43,13 +43,14 @@ The library is stable, portable, and designed for projects that need a scripting ## Features -- Full AWK interpreter — mostly POSIX AWK compatible, with additional extensions. -- Embeddable library — integrate AWK scripting into C or C++ projects as an execution engine. -- C and C++ APIs — core functions exposed in C, with convenient C++ wrapper classes available. -- Flexible usage — usable as both a standalone command-line interpreter and a library. -- Portable core — the base library depends only on the standard C library. -- Optional extensions — loadable modules (e.g. MySQL access, FFI) can be built in or used via shared objects. -- Mature and stable — developed and maintained for many years with proven reliability. +- Full AWK interpreter - mostly POSIX AWK compatible, with additional extensions. +- Embeddable library - integrate AWK scripting into C or C++ projects as an execution engine. +- C and C++ APIs - core functions exposed in C, with convenient C++ wrapper classes available. +- Flexible usage - usable as both a standalone command-line interpreter and a library. +- Portable core - the base library depends only on the standard C library. +- Optional extensions - loadable modules (e.g. MySQL access, FFI) can be built in or used via shared objects. +- Mature and stable - developed and maintained for many years with proven reliability. +- Embedded sed functionality - includes a sed engine that can be used from C/C++ or invoked via the CLI using --sed # Building Hawk From Source Code @@ -160,7 +161,7 @@ Assuming the above sample code is stored in `hawk02.c` and the built Hawk librar $ gcc -Wall -O2 -o hawk02 hawk02.c -lhawk ``` -The actual command may vary depending on the compiler used and the library `configure` options used. +The actual command may vary depending on the compiler used and the `configure` options used. # Embedding Hawk in C++ Applications @@ -208,13 +209,16 @@ The C++ classes are inferior to the C equivalents in that they don't allow creat # Language -Hawk is an AWK interpreter created by an individual whose name starts with `H`, hence the `H` in the name. It serves a dual purpose: to be an easy-to-embed implementation within other applications and a standalone tool for users. At its core, Hawk largely supports all the fundamental features of AWK, ensuring compatibility with existing AWK programs and scripts. However, it introduces subtle differences in behavior compared to traditional AWK implementations, which will be explained in the [Incompatibility with AWK](#incompatibility-with-awk) section. +`Hawk` is an AWK interpreter created by an individual whose name starts with H, which inspired the H in the name. It serves a dual purpose: as an embeddable AWK engine for integration into other applications, and as a standalone command-line tool for general use. -In Hawk, as in traditional awk, the execution flow follows a specific order: the `BEGIN` block is executed first, followed by the pattern-action blocks, and finally the `END` block. +At its core, `Hawk` supports the fundamental features of AWK, ensuring compatibility with most existing AWK programs and scripts. However, it introduces some subtle differences in behavior compared to traditional AWK implementations, which are detailed in the [Incompatibility with AWK](#incompatibility-with-awk) section. + section. -1. `BEGIN` Block: The `BEGIN` block is executed before any input is processed. It is typically used for initializations, such as setting variable values or defining functions that will be used later in the script. -1. Pattern-Action Blocks: After the `BEGIN` block, Hawk reads the input line by line (or record by record, depending on the record separator `RS`). For each input line or record, Hawk checks if it matches the specified pattern. If a match is found, the associated action block is executed. -1. `END` Block: After processing all input lines or records, the `END` block is executed. It is typically used for performing final operations, such as printing summaries or closing files. +`Hawk` follows the standard AWK execution flow: the BEGIN block is executed first, followed by pattern-action blocks, and finally the END block. + +1. `BEGIN` Block: Executed before any input is processed. It is typically used for initializations, such as setting variable values or defining functions to be used later in the script. +1. Pattern-Action Blocks: After the BEGIN block, `Hawk` reads input line by line (or record by record, based on the record separator RS). For each input line, `Hawk` checks if it matches the specified pattern. If it does, the associated action block is executed. +1. `END` Block: Executed after all input has been processed. It is typically used for final operations, such as printing summaries or closing files. Here's a sample code that demonstrates the basic `BEGIN`, pattern-action, and `END` loop in Hawk: @@ -1351,7 +1355,7 @@ BEGIN { There are subtle differences in handling expressions for positional variables. In Hawk, many of the ambiguity issues can be resolved by enclosing the expression in parentheses. -| Expression | HAWK | AWK | +| Expression | Hawk | AWK | |--------------|---------------|-----------------| | `$++$++i` | syntax error | OK | | `$(++$(++i))`| OK | syntax error | diff --git a/bin/cut.c b/bin/cut.c index a9f68797..bf20ec99 100644 --- a/bin/cut.c +++ b/bin/cut.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #if !defined(_GNU_SOURCE) diff --git a/bin/hawk.c b/bin/hawk.c index 07841f5b..9eaa9816 100644 --- a/bin/hawk.c +++ b/bin/hawk.c @@ -26,7 +26,7 @@ #include "main.h" -#include +#include #include #include #include diff --git a/bin/main.c b/bin/main.c index 3cd47723..66ee6bc9 100644 --- a/bin/main.c +++ b/bin/main.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/bin/sed.c b/bin/sed.c index ef080bf1..246a54c2 100644 --- a/bin/sed.c +++ b/bin/sed.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #if !defined(_GNU_SOURCE) diff --git a/lib/Makefile.am b/lib/Makefile.am index 87de520a..3f4f30af 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -73,7 +73,6 @@ pkginclude_HEADERS = \ hawk-utl.h \ hawk-sed.h \ hawk-sio.h \ - hawk-std.h \ hawk-str.h \ hawk-tio.h \ hawk-tre.h \ diff --git a/lib/Makefile.in b/lib/Makefile.in index 264c0a6d..6c977bdf 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -250,27 +250,27 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cli.h \ hawk-cmn.h hawk-cut.h hawk-dir.h hawk-ecs.h hawk-fio.h \ hawk-fmt.h hawk-gem.h hawk-glob.h hawk-htb.h hawk-json.h \ hawk-map.h hawk-mtx.h hawk-rbt.h hawk-pac1.h hawk-pio.h \ - hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-std.h \ - hawk-str.h hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h \ - Hawk.hpp Hawk-Sed.hpp arr.c chr.c cut-prv.h cut.c dir.c \ - ecs-imp.h ecs.c err-prv.h err.c err-sys.c fmt-imp.h fmt.c \ - fnc-prv.h fnc.c gem.c gem-glob.c gem-nwif.c gem-nwif2.c \ - hawk-prv.h hawk.c htb.c idmap-imp.h json.c json-prv.h mb8.c \ - misc-imp.h misc-prv.h misc.c parse-prv.h parse.c rbt.c rec.c \ - rio-prv.h rio.c run-prv.h run.c sed-prv.h sed.c skad-prv.h \ - skad.c tre-prv.h tre-ast.c tre-ast.h tre-compile.c \ - tre-compile.h tre-match-bt.c tre-match-pa.c tre-match-ut.h \ - tre-mem.c tre-mem.h tre-parse.c tre-parse.h tre-stack.h \ - tre-stack.c tre.c tree-prv.h tree.c uch-prop.h uch-case.h \ - utf16.c utf8.c utl-ass.c utl-cmgr.c utl-rnd.c utl-sort.c \ - utl-str.c utl-sys.c utl-xstr.c utl.c val-prv.h val.c xma.c \ - cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h tio.c std.c \ - std-cut.c std-json.c std-sed.c Hawk.cpp Std.cpp Sed.cpp \ - Std-Sed.cpp mod-hawk.c mod-hawk.h mod-math.c mod-math.h \ - mod-str.c mod-str.h mod-sys.c mod-sys.h ../mod/mod-ffi.c \ - ../mod/mod-ffi.h ../mod/mod-memc.c ../mod/mod-memc.h \ - ../mod/mod-mysql.c ../mod/mod-mysql.h ../mod/mod-sed.c \ - ../mod/mod-sed.h ../mod/mod-uci.c ../mod/mod-uci.h + hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-str.h \ + hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h Hawk.hpp \ + Hawk-Sed.hpp arr.c chr.c cut-prv.h cut.c dir.c ecs-imp.h ecs.c \ + err-prv.h err.c err-sys.c fmt-imp.h fmt.c fnc-prv.h fnc.c \ + gem.c gem-glob.c gem-nwif.c gem-nwif2.c hawk-prv.h hawk.c \ + htb.c idmap-imp.h json.c json-prv.h mb8.c misc-imp.h \ + misc-prv.h misc.c parse-prv.h parse.c rbt.c rec.c rio-prv.h \ + rio.c run-prv.h run.c sed-prv.h sed.c skad-prv.h skad.c \ + tre-prv.h tre-ast.c tre-ast.h tre-compile.c tre-compile.h \ + tre-match-bt.c tre-match-pa.c tre-match-ut.h tre-mem.c \ + tre-mem.h tre-parse.c tre-parse.h tre-stack.h tre-stack.c \ + tre.c tree-prv.h tree.c uch-prop.h uch-case.h utf16.c utf8.c \ + utl-ass.c utl-cmgr.c utl-rnd.c utl-sort.c utl-str.c utl-sys.c \ + utl-xstr.c utl.c val-prv.h val.c xma.c cli-imp.h cli.c fio.c \ + mtx.c pio.c sio.c syscall.h tio.c std.c std-cut.c std-json.c \ + std-sed.c Hawk.cpp Std.cpp Sed.cpp Std-Sed.cpp mod-hawk.c \ + mod-hawk.h mod-math.c mod-math.h mod-str.c mod-str.h mod-sys.c \ + mod-sys.h ../mod/mod-ffi.c ../mod/mod-ffi.h ../mod/mod-memc.c \ + ../mod/mod-memc.h ../mod/mod-mysql.c ../mod/mod-mysql.h \ + ../mod/mod-sed.c ../mod/mod-sed.h ../mod/mod-uci.c \ + ../mod/mod-uci.h am__objects_1 = am__objects_2 = $(am__objects_1) @ENABLE_CXX_TRUE@am__objects_3 = libhawk_la-Hawk.lo libhawk_la-Std.lo \ @@ -442,9 +442,9 @@ am__pkginclude_HEADERS_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cli.h \ hawk-cmn.h hawk-cut.h hawk-dir.h hawk-ecs.h hawk-fio.h \ hawk-fmt.h hawk-gem.h hawk-glob.h hawk-htb.h hawk-json.h \ hawk-map.h hawk-mtx.h hawk-rbt.h hawk-pac1.h hawk-pio.h \ - hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-std.h \ - hawk-str.h hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h \ - Hawk.hpp Hawk-Sed.hpp + hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-str.h \ + hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h Hawk.hpp \ + Hawk-Sed.hpp HEADERS = $(pkginclude_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ hawk-cfg.h.in @@ -673,9 +673,8 @@ pkginclude_HEADERS = hawk.h hawk-arr.h hawk-chr.h hawk-cli.h \ hawk-cmn.h hawk-cut.h hawk-dir.h hawk-ecs.h hawk-fio.h \ hawk-fmt.h hawk-gem.h hawk-glob.h hawk-htb.h hawk-json.h \ hawk-map.h hawk-mtx.h hawk-rbt.h hawk-pac1.h hawk-pio.h \ - hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-std.h \ - hawk-str.h hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h \ - $(am__append_7) + hawk-skad.h hawk-utl.h hawk-sed.h hawk-sio.h hawk-str.h \ + hawk-tio.h hawk-tre.h hawk-upac.h hawk-xma.h $(am__append_7) pkglib_LTLIBRARIES = libhawk.la $(am__append_11) libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c cut-prv.h cut.c \ dir.c ecs-imp.h ecs.c err-prv.h err.c err-sys.c fmt-imp.h \ diff --git a/lib/Sed.cpp b/lib/Sed.cpp index 9a5e224c..0dfaeb4f 100644 --- a/lib/Sed.cpp +++ b/lib/Sed.cpp @@ -63,7 +63,6 @@ void Sed::setCmgr (hawk_cmgr_t* cmgr) int Sed::open () { -// TODO: create this->errinf just like the Hawk class. this->sed = hawk_sed_open(this->getMmgr(), HAWK_SIZEOF(xtn_t), this->getCmgr(), &this->errinf); if (HAWK_UNLIKELY(!this->sed)) return -1; diff --git a/lib/Std.cpp b/lib/Std.cpp index 6079282c..b739ad76 100644 --- a/lib/Std.cpp +++ b/lib/Std.cpp @@ -27,7 +27,6 @@ #include #include #include -#include // for hawk_stdmodXXX() functions #include "hawk-prv.h" #include #include diff --git a/lib/hawk-cmn.h b/lib/hawk-cmn.h index 57b7fe8f..c4bbead0 100644 --- a/lib/hawk-cmn.h +++ b/lib/hawk-cmn.h @@ -1567,5 +1567,14 @@ typedef struct hawk_skad_t hawk_skad_t; #endif +#if defined(__cplusplus) +extern "C" { +#endif + +/* NOTHING HERE */ + +#if defined(__cplusplus) +} +#endif #endif diff --git a/lib/hawk-std.h b/lib/hawk-std.h deleted file mode 100644 index 659cdd04..00000000 --- a/lib/hawk-std.h +++ /dev/null @@ -1,322 +0,0 @@ -/* - Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _HAWK_STD_H_ -#define _HAWK_STD_H_ - -#include - -/** \file - * This file defines functions and data types that help you create - * an hawk interpreter with less effort. It is designed to be as close - * to conventional hawk implementations as possible. - * - * The source script handler does not evaluate a file name of the "var=val" - * form as an assignment expression. Instead, it just treats it as a - * normal file name. - */ - -/** - * The hawk_parsestd_type_t type defines standard source I/O types. - */ -enum hawk_parsestd_type_t -{ - HAWK_PARSESTD_NULL = 0, /**< pseudo-value to indicate no script */ - HAWK_PARSESTD_FILE = 1, /**< file path */ - HAWK_PARSESTD_FILEB = 2, /**< file path */ - HAWK_PARSESTD_FILEU = 3, /**< file path */ - HAWK_PARSESTD_OOCS = 4, /**< length-bounded string */ - HAWK_PARSESTD_BCS = 5, - HAWK_PARSESTD_UCS = 6 - -}; -typedef enum hawk_parsestd_type_t hawk_parsestd_type_t; - -/** - * The hawk_parsestd_t type defines a source I/O. - */ -struct hawk_parsestd_t -{ - hawk_parsestd_type_t type; - - union - { - struct - { - /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ - const hawk_ooch_t* path; - - /** a stream created with the file path is set with this - * cmgr if it is not #HAWK_NULL. */ - hawk_cmgr_t* cmgr; - } file; - - struct - { - /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ - const hawk_bch_t* path; - - /** a stream created with the file path is set with this - * cmgr if it is not #HAWK_NULL. */ - hawk_cmgr_t* cmgr; - } fileb; - - struct - { - /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ - const hawk_uch_t* path; - - /** a stream created with the file path is set with this - * cmgr if it is not #HAWK_NULL. */ - hawk_cmgr_t* cmgr; - } fileu; - - /** - * input string or dynamically allocated output string - * - * For input, the ptr and the len field of str indicates the - * pointer and the length of a string to read. You must set - * these fields before calling hawk_parsestd(). - * - * For output, the ptr and the len field of str indicates the - * pointer and the length of a deparsed source string. The output - * string is dynamically allocated. You don't need to set these - * fields before calling hawk_parsestd() because they are set - * by hawk_parsestd() and valid while the relevant hawk object - * is alive. You must free the memory chunk pointed to by the - * ptr field with hawk_freemem() once you're done with it to - * avoid memory leaks. - */ - hawk_oocs_t oocs; - hawk_bcs_t bcs; - hawk_ucs_t ucs; - } u; -}; - -typedef struct hawk_parsestd_t hawk_parsestd_t; - -#if defined(__cplusplus) -extern "C" { -#endif - -/** - * The hawk_openstd() function creates an hawk object using the default - * memory manager and primitive functions. Besides, it adds a set of - * standard intrinsic functions like atan, system, etc. Use this function - * over hawk_open() if you don't need finer-grained customization. - */ -HAWK_EXPORT hawk_t* hawk_openstd ( - hawk_oow_t xtnsize, /**< extension size in bytes */ - hawk_errinf_t* errinf /**< pointer to an error information variable */ -); - -/** - * The hawk_openstdwithmmgr() function creates an hawk object with a - * user-defined memory manager. It is equivalent to hawk_openstd(), - * except that you can specify your own memory manager. - */ -HAWK_EXPORT hawk_t* hawk_openstdwithmmgr ( - hawk_mmgr_t* mmgr, /**< memory manager */ - hawk_oow_t xtnsize, /**< extension size in bytes */ - hawk_cmgr_t* cmgr, /**< character encoding manager */ - hawk_errinf_t* errinf /**< pointer to an error information variable */ -); - -/** - * The hawk_parsestd() functions parses source script. - * The code below shows how to parse a literal string 'BEGIN { print 10; }' - * and deparses it out to a buffer 'buf'. - * \code - * int n; - * hawk_parsestd_t in[2]; - * hawk_parsestd_t out; - * - * in[0].type = HAWK_PARSESTD_OOCS; - * in[0].u.str.ptr = HAWK_T("BEGIN { print 10; }"); - * in[0].u.str.len = hawk_count_oocstr(in.u.str.ptr); - * in[1].type = HAWK_PARSESTD_NULL; - * out.type = HAWK_PARSESTD_OOCS; - * n = hawk_parsestd(hawk, in, &out); - * if (n >= 0) - * { - * hawk_printf (HAWK_T("%s\n"), out.u.str.ptr); - * HAWK_MMGR_FREE (out.u.str.ptr); - * } - * \endcode - */ -HAWK_EXPORT int hawk_parsestd ( - hawk_t* hawk, - hawk_parsestd_t in[], - hawk_parsestd_t* out -); - -/** - * The hawk_rtx_openstdwithbcstr() function creates a standard runtime context. - * The caller should keep the contents of \a icf and \a ocf valid throughout - * the lifetime of the runtime context created. The \a cmgr is set to the - * streams created with \a icf and \a ocf if it is not #HAWK_NULL. - */ -HAWK_EXPORT hawk_rtx_t* hawk_rtx_openstdwithbcstr ( - hawk_t* hawk, - hawk_oow_t xtnsize, - const hawk_bch_t* id, - hawk_bch_t* icf[], - hawk_bch_t* ocf[], - hawk_cmgr_t* cmgr -); - -/** - * The hawk_rtx_openstdwithucstr() function creates a standard runtime context. - * The caller should keep the contents of \a icf and \a ocf valid throughout - * the lifetime of the runtime context created. The \a cmgr is set to the - * streams created with \a icf and \a ocf if it is not #HAWK_NULL. - */ -HAWK_EXPORT hawk_rtx_t* hawk_rtx_openstdwithucstr ( - hawk_t* hawk, - hawk_oow_t xtnsize, - const hawk_uch_t* id, - hawk_uch_t* icf[], - hawk_uch_t* ocf[], - hawk_cmgr_t* cmgr -); - -#if defined(HAWK_OOCH_IS_BCH) -# define hawk_rtx_openstd hawk_rtx_openstdwithbcstr -#else -# define hawk_rtx_openstd hawk_rtx_openstdwithucstr -#endif - -/** - * The hawk_rtx_getiocmgrstd() function gets the current character - * manager associated with a particular I/O target indicated by the name - * \a ioname if #HAWK_OOCH_IS_UCH is defined. It always returns #HAWK_NULL - * if #HAWK_OOCH_IS_BCH is defined. - */ -HAWK_EXPORT hawk_cmgr_t* hawk_rtx_getiocmgrstd ( - hawk_rtx_t* rtx, - const hawk_ooch_t* ioname -); - - -/* ------------------------------------------------------------------------- */ - - -HAWK_EXPORT hawk_flt_t hawk_stdmathpow ( - hawk_t* hawk, - hawk_flt_t x, - hawk_flt_t y -); - -HAWK_EXPORT hawk_flt_t hawk_stdmathmod ( - hawk_t* hawk, - hawk_flt_t x, - hawk_flt_t y -); - -HAWK_EXPORT int hawk_stdmodstartup ( - hawk_t* hawk -); - -HAWK_EXPORT void hawk_stdmodshutdown ( - hawk_t* hawk -); - -HAWK_EXPORT void* hawk_stdmodopen ( - hawk_t* hawk, - const hawk_mod_spec_t* spec -); - -HAWK_EXPORT void hawk_stdmodclose ( - hawk_t* hawk, - void* handle -); - -HAWK_EXPORT void* hawk_stdmodgetsym ( - hawk_t* hawk, - void* handle, - const hawk_ooch_t* name -); - - -HAWK_EXPORT int hawk_stdplainfileexists ( - hawk_t* hawk, - const hawk_ooch_t* file -); - -HAWK_EXPORT const hawk_ooch_t* hawk_stdgetfileindirs ( - hawk_t* hawk, - const hawk_oocs_t* dirs, - const hawk_ooch_t* file -); - -/* ------------------------------------------------------------------------- */ - -/** - * The hawk_get_sys_mmgr() function returns a pointer to a stock memory manager structure - * that can be used where a custom memory manager is not required. The returned structure - * is managed internally and should not be modified or finalized by the caller. - * - * \return Pointer to the system-wide default memory manager. - */ -HAWK_EXPORT hawk_mmgr_t* hawk_get_sys_mmgr ( - void -); - -/** - * This hawk_init_xma_mmgr() function initializes the memory manager structure pointed - * to by `mmgr` with the specified memory limit `memlimit`. The caller is responsible for - * allocating the memory for `mmgr` prior to calling this function. - * - * \return 0 on success, or a negative value on failure - */ -HAWK_EXPORT int hawk_init_xma_mmgr ( - hawk_mmgr_t* mmgr, - hawk_oow_t memlimit -); - -/** - * The hawk_fini_xma_mmgr() function cleans up resources associated with the memory - * manager structure previously initialized by `hawk_init_xma_mmgr()`. After this call, - * the `mmgr` structure should not be used unless it is re-initialized. - * - */ -HAWK_EXPORT void hawk_fini_xma_mmgr ( - hawk_mmgr_t* mmgr -); - - -HAWK_EXPORT int hawk_init_ama_mmgr ( - hawk_mmgr_t* mmgr -); - -HAWK_EXPORT void hawk_fini_ama_mmgr ( - hawk_mmgr_t* mmgr -); - -#if defined(__cplusplus) -} -#endif - - -#endif diff --git a/lib/hawk.h b/lib/hawk.h index d89fea6e..9c4572f9 100644 --- a/lib/hawk.h +++ b/lib/hawk.h @@ -1532,6 +1532,89 @@ enum hawk_sbuf_id_t }; typedef enum hawk_sbuf_id_t hawk_sbuf_id_t; +/* ----------------------------------------------------------------------- */ + +/** + * The hawk_parsestd_type_t type defines standard source I/O types. + */ +enum hawk_parsestd_type_t +{ + HAWK_PARSESTD_NULL = 0, /**< pseudo-value to indicate no script */ + HAWK_PARSESTD_FILE = 1, /**< file path */ + HAWK_PARSESTD_FILEB = 2, /**< file path */ + HAWK_PARSESTD_FILEU = 3, /**< file path */ + HAWK_PARSESTD_OOCS = 4, /**< length-bounded string */ + HAWK_PARSESTD_BCS = 5, + HAWK_PARSESTD_UCS = 6 + +}; +typedef enum hawk_parsestd_type_t hawk_parsestd_type_t; + +/** + * The hawk_parsestd_t type defines a source I/O. + */ +struct hawk_parsestd_t +{ + hawk_parsestd_type_t type; + + union + { + struct + { + /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ + const hawk_ooch_t* path; + + /** a stream created with the file path is set with this + * cmgr if it is not #HAWK_NULL. */ + hawk_cmgr_t* cmgr; + } file; + + struct + { + /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ + const hawk_bch_t* path; + + /** a stream created with the file path is set with this + * cmgr if it is not #HAWK_NULL. */ + hawk_cmgr_t* cmgr; + } fileb; + + struct + { + /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ + const hawk_uch_t* path; + + /** a stream created with the file path is set with this + * cmgr if it is not #HAWK_NULL. */ + hawk_cmgr_t* cmgr; + } fileu; + + /** + * input string or dynamically allocated output string + * + * For input, the ptr and the len field of str indicates the + * pointer and the length of a string to read. You must set + * these fields before calling hawk_parsestd(). + * + * For output, the ptr and the len field of str indicates the + * pointer and the length of a deparsed source string. The output + * string is dynamically allocated. You don't need to set these + * fields before calling hawk_parsestd() because they are set + * by hawk_parsestd() and valid while the relevant hawk object + * is alive. You must free the memory chunk pointed to by the + * ptr field with hawk_freemem() once you're done with it to + * avoid memory leaks. + */ + hawk_oocs_t oocs; + hawk_bcs_t bcs; + hawk_ucs_t ucs; + } u; +}; + +typedef struct hawk_parsestd_t hawk_parsestd_t; + +/* ----------------------------------------------------------------------- */ + #if defined(__cplusplus) extern "C" { #endif @@ -1573,6 +1656,29 @@ HAWK_EXPORT hawk_t* hawk_open ( hawk_errinf_t* errinf /**< pointer to an error number varaible */ ); +/** + * The hawk_openstd() function creates an hawk object using the default + * memory manager and primitive functions. Besides, it adds a set of + * standard intrinsic functions like atan, system, etc. Use this function + * over hawk_open() if you don't need finer-grained customization. + */ +HAWK_EXPORT hawk_t* hawk_openstd ( + hawk_oow_t xtnsize, /**< extension size in bytes */ + hawk_errinf_t* errinf /**< pointer to an error information variable */ +); + +/** + * The hawk_openstdwithmmgr() function creates an hawk object with a + * user-defined memory manager. It is equivalent to hawk_openstd(), + * except that you can specify your own memory manager. + */ +HAWK_EXPORT hawk_t* hawk_openstdwithmmgr ( + hawk_mmgr_t* mmgr, /**< memory manager */ + hawk_oow_t xtnsize, /**< extension size in bytes */ + hawk_cmgr_t* cmgr, /**< character encoding manager */ + hawk_errinf_t* errinf /**< pointer to an error information variable */ +); + /** * The hawk_close() function destroys a hawk_t object. */ @@ -1961,6 +2067,34 @@ HAWK_EXPORT int hawk_parse ( hawk_sio_cbs_t* sio /**< source script I/O handler */ ); +/** + * The hawk_parsestd() functions parses source script. + * The code below shows how to parse a literal string 'BEGIN { print 10; }' + * and deparses it out to a buffer 'buf'. + * \code + * int n; + * hawk_parsestd_t in[2]; + * hawk_parsestd_t out; + * + * in[0].type = HAWK_PARSESTD_OOCS; + * in[0].u.str.ptr = HAWK_T("BEGIN { print 10; }"); + * in[0].u.str.len = hawk_count_oocstr(in.u.str.ptr); + * in[1].type = HAWK_PARSESTD_NULL; + * out.type = HAWK_PARSESTD_OOCS; + * n = hawk_parsestd(hawk, in, &out); + * if (n >= 0) + * { + * hawk_printf (HAWK_T("%s\n"), out.u.str.ptr); + * HAWK_MMGR_FREE (out.u.str.ptr); + * } + * \endcode + */ +HAWK_EXPORT int hawk_parsestd ( + hawk_t* hawk, + hawk_parsestd_t in[], + hawk_parsestd_t* out +); + /** * The hawk_isvalidident() function determines if a given string is * a valid identifier. @@ -2255,6 +2389,42 @@ HAWK_EXPORT hawk_rtx_t* hawk_rtx_open ( hawk_rio_cbs_t* rio /**< runtime IO handlers */ ); +/** + * The hawk_rtx_openstdwithbcstr() function creates a standard runtime context. + * The caller should keep the contents of \a icf and \a ocf valid throughout + * the lifetime of the runtime context created. The \a cmgr is set to the + * streams created with \a icf and \a ocf if it is not #HAWK_NULL. + */ +HAWK_EXPORT hawk_rtx_t* hawk_rtx_openstdwithbcstr ( + hawk_t* hawk, + hawk_oow_t xtnsize, + const hawk_bch_t* id, + hawk_bch_t* icf[], + hawk_bch_t* ocf[], + hawk_cmgr_t* cmgr +); + +/** + * The hawk_rtx_openstdwithucstr() function creates a standard runtime context. + * The caller should keep the contents of \a icf and \a ocf valid throughout + * the lifetime of the runtime context created. The \a cmgr is set to the + * streams created with \a icf and \a ocf if it is not #HAWK_NULL. + */ +HAWK_EXPORT hawk_rtx_t* hawk_rtx_openstdwithucstr ( + hawk_t* hawk, + hawk_oow_t xtnsize, + const hawk_uch_t* id, + hawk_uch_t* icf[], + hawk_uch_t* ocf[], + hawk_cmgr_t* cmgr +); + +#if defined(HAWK_OOCH_IS_BCH) +# define hawk_rtx_openstd hawk_rtx_openstdwithbcstr +#else +# define hawk_rtx_openstd hawk_rtx_openstdwithucstr +#endif + /** * The hawk_rtx_close() function destroys a runtime context. */ @@ -3713,9 +3883,72 @@ static HAWK_INLINE void hawk_rtx_freerex (hawk_rtx_t* rtx, hawk_tre_t* code, haw #define hawk_rtx_freerex(rtx, code, icode) hawk_gem_freerex(hawk_rtx_getgem(rtx), code, icode) #endif +/* ----------------------------------------------------------------------- */ + +/** + * The hawk_rtx_getiocmgrstd() function gets the current character + * manager associated with a particular I/O target indicated by the name + * \a ioname if #HAWK_OOCH_IS_UCH is defined. It always returns #HAWK_NULL + * if #HAWK_OOCH_IS_BCH is defined. + */ +HAWK_EXPORT hawk_cmgr_t* hawk_rtx_getiocmgrstd ( + hawk_rtx_t* rtx, + const hawk_ooch_t* ioname +); /* ----------------------------------------------------------------------- */ +HAWK_EXPORT hawk_flt_t hawk_stdmathpow ( + hawk_t* hawk, + hawk_flt_t x, + hawk_flt_t y +); + +HAWK_EXPORT hawk_flt_t hawk_stdmathmod ( + hawk_t* hawk, + hawk_flt_t x, + hawk_flt_t y +); + +HAWK_EXPORT int hawk_stdmodstartup ( + hawk_t* hawk +); + +HAWK_EXPORT void hawk_stdmodshutdown ( + hawk_t* hawk +); + +HAWK_EXPORT void* hawk_stdmodopen ( + hawk_t* hawk, + const hawk_mod_spec_t* spec +); + +HAWK_EXPORT void hawk_stdmodclose ( + hawk_t* hawk, + void* handle +); + +HAWK_EXPORT void* hawk_stdmodgetsym ( + hawk_t* hawk, + void* handle, + const hawk_ooch_t* name +); + + +HAWK_EXPORT int hawk_stdplainfileexists ( + hawk_t* hawk, + const hawk_ooch_t* file +); + +HAWK_EXPORT const hawk_ooch_t* hawk_stdgetfileindirs ( + hawk_t* hawk, + const hawk_oocs_t* dirs, + const hawk_ooch_t* file +); + + +/* ------------------------------------------------------------------------- */ + /** * The hawk_get_nil_val() function returns the pointer to the predefined * nil value. you can call this without creating a runtime context. @@ -3732,6 +3965,43 @@ HAWK_EXPORT const hawk_ooch_t* hawk_get_val_type_name ( hawk_val_t* val ); +/* ------------------------------------------------------------------------- */ + +/** + * The hawk_get_sys_mmgr() function returns a pointer to a stock memory manager structure + * that can be used where a custom memory manager is not required. The returned structure + * is managed internally and should not be modified or finalized by the caller. + * + * \return Pointer to the system-wide default memory manager. + */ +HAWK_EXPORT hawk_mmgr_t* hawk_get_sys_mmgr ( + void +); + +/* ------------------------------------------------------------------------- */ + +/** + * This hawk_init_xma_mmgr() function initializes the memory manager structure pointed + * to by `mmgr` with the specified memory limit `memlimit`. The caller is responsible for + * allocating the memory for `mmgr` prior to calling this function. + * + * \return 0 on success, or a negative value on failure + */ +HAWK_EXPORT int hawk_init_xma_mmgr ( + hawk_mmgr_t* mmgr, + hawk_oow_t memlimit +); + +/** + * The hawk_fini_xma_mmgr() function cleans up resources associated with the memory + * manager structure previously initialized by `hawk_init_xma_mmgr()`. After this call, + * the `mmgr` structure should not be used unless it is re-initialized. + * + */ +HAWK_EXPORT void hawk_fini_xma_mmgr ( + hawk_mmgr_t* mmgr +); + #if defined(__cplusplus) } #endif diff --git a/lib/std-cut.c b/lib/std-cut.c index 21c13e56..5023bb4b 100644 --- a/lib/std-cut.c +++ b/lib/std-cut.c @@ -24,7 +24,6 @@ #include "cut-prv.h" #include "hawk-prv.h" -#include "hawk-std.h" typedef struct xtn_in_t xtn_in_t; struct xtn_in_t diff --git a/lib/std-json.c b/lib/std-json.c index 2503f786..dbf56ee7 100644 --- a/lib/std-json.c +++ b/lib/std-json.c @@ -23,7 +23,7 @@ */ #include "json-prv.h" -#include +#include "hawk-prv.h" typedef struct xtn_t xtn_t; struct xtn_t diff --git a/lib/std-sed.c b/lib/std-sed.c index 77f76b27..4bdd12f3 100644 --- a/lib/std-sed.c +++ b/lib/std-sed.c @@ -24,7 +24,6 @@ #include "sed-prv.h" #include "hawk-prv.h" -#include "hawk-std.h" typedef struct xtn_in_t xtn_in_t; struct xtn_in_t diff --git a/lib/std.c b/lib/std.c index 771c00eb..b08ff426 100644 --- a/lib/std.c +++ b/lib/std.c @@ -23,7 +23,6 @@ */ #include "hawk-prv.h" -#include #include #include #include @@ -229,7 +228,6 @@ static hawk_mmgr_t sys_mmgr = HAWK_NULL }; - hawk_mmgr_t* hawk_get_sys_mmgr (void) { return &sys_mmgr; diff --git a/samples/hawk02.c b/samples/hawk02.c index 9acb0925..6544c4b7 100644 --- a/samples/hawk02.c +++ b/samples/hawk02.c @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/t/t-004.c b/t/t-004.c index 4d9a1cc7..4c7f9bca 100644 --- a/t/t-004.c +++ b/t/t-004.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include "tap.h" diff --git a/t/t-008.c b/t/t-008.c index ad7fbb35..6422030d 100644 --- a/t/t-008.c +++ b/t/t-008.c @@ -1,5 +1,5 @@ +#include #include -#include #include #include #include diff --git a/t/t-009.c b/t/t-009.c index e727d816..f086e467 100644 --- a/t/t-009.c +++ b/t/t-009.c @@ -1,5 +1,5 @@ +#include #include -#include #include #include #include