code reformatted
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-10-03 00:40:25 +09:00
parent 531de73535
commit 203a0660ef
6 changed files with 96 additions and 97 deletions

View File

@ -67,12 +67,12 @@ $ make install
Here's an example of how Hawk can be embedded within a C application:
```c
#include <hawk-std.h>
#include <hawk.h>
#include <stdio.h>
#include <string.h>
static const hawk_bch_t* src =
"BEGIN { print ARGV[0]"
"BEGIN { print ARGV[0];"
" for (i=2;i<=9;i++)"
" {"
" for (j=1;j<=9;j++)"
@ -190,7 +190,7 @@ int main ()
}
HAWK::Hawk::Value vr;
hawk.loop(&vr); // alternatively, hawk.exec (&vr, HAWK_NULL, 0);
hawk.loop(&vr); // alternatively, hawk.exec(&vr, HAWK_NULL, 0);
hawk.close();
return 0;

View File

@ -665,7 +665,7 @@ static int fmt_outv (hawk_fmtout_t* fmtout, va_list ap)
case '5': case '6': case '7': case '8': case '9':
{
if (flagc & FLAGC_LENMOD) goto invalid_format;
for (n = 0;; fmtptr += fmtchsz)
for (n = 0; ; fmtptr += fmtchsz)
{
n = n * 10 + uch - '0';
switch (fmtout->fmt_type)

View File

@ -2239,7 +2239,7 @@ static int run_block (hawk_rtx_t* rtx, hawk_nde_blk_t* nde)
rtx->depth.block >= rtx->hawk->opt.depth.s.block_run)
{
hawk_rtx_seterrnum(rtx, &nde->loc, HAWK_EBLKNST);
return -1;;
return -1;
}
rtx->depth.block++;

View File

@ -21,7 +21,7 @@ int main ()
int ret;
hawk = hawk_openstd(0, HAWK_NULL); /* create a hawk instance */
if (!hawk)
if (!hawk)
{
fprintf (stderr, "ERROR: cannot open hawk\n");
ret = -1; goto oops;
@ -37,13 +37,13 @@ int main ()
ret = hawk_parsestd(hawk, psin, HAWK_NULL); /* parse the script */
if (ret <= -1)
{
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(parse): %js\n", hawk_geterrmsg(hawk));
hawk_logbfmt(hawk, HAWK_LOG_STDERR, "ERROR(parse): %js\n", hawk_geterrmsg(hawk));
ret = -1; goto oops;
}
/* create a runtime context needed for execution */
rtx = hawk_rtx_openstd (
hawk,
rtx = hawk_rtx_openstd(
hawk,
0,
HAWK_T("hawk02"),
HAWK_NULL, /* stdin */
@ -52,7 +52,7 @@ int main ()
);
if (!rtx)
{
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_open): %js\n", hawk_geterrmsg(hawk));
hawk_logbfmt(hawk, HAWK_LOG_STDERR, "ERROR(rtx_open): %js\n", hawk_geterrmsg(hawk));
ret = -1; goto oops;
}
@ -60,16 +60,16 @@ int main ()
retv = hawk_rtx_loop(rtx);
if (!retv)
{
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_loop): %js\n", hawk_geterrmsg(hawk));
hawk_logbfmt(hawk, HAWK_LOG_STDERR, "ERROR(rtx_loop): %js\n", hawk_geterrmsg(hawk));
ret = -1; goto oops;
}
/* lowered the reference count of the returned value */
hawk_rtx_refdownval (rtx, retv);
hawk_rtx_refdownval(rtx, retv);
ret = 0;
oops:
if (rtx) hawk_rtx_close (rtx); /* destroy the runtime context */
if (hawk) hawk_close (hawk); /* destroy the hawk instance */
if (rtx) hawk_rtx_close(rtx); /* destroy the runtime context */
if (hawk) hawk_close(hawk); /* destroy the hawk instance */
return -1;
}

View File

@ -58,7 +58,7 @@ class MyHawk: public HawkStd
{
public:
MyHawk (HAWK::Mmgr* mmgr = HAWK_NULL): HawkStd(mmgr) { }
~MyHawk () { this->close (); }
~MyHawk () { this->close(); }
int open ()
{
@ -67,22 +67,22 @@ public:
idLastSleep = this->addGlobal(HAWK_T("LAST_SLEEP"));
if (idLastSleep <= -1) goto oops;
/* this is for demonstration only.
/* this is for demonstration only.
* you can use sys::sleep() instead */
if (this->addFunction(HAWK_T("sleep"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::sleep) <= -1 ||
this->addFunction (HAWK_T("sumintarray"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::sumintarray) <= -1 ||
this->addFunction (HAWK_T("arrayindices"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::arrayindices) <= -1) goto oops;
this->addFunction(HAWK_T("sumintarray"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::sumintarray) <= -1 ||
this->addFunction(HAWK_T("arrayindices"), 1, 1, HAWK_NULL, (FunctionHandler)&MyHawk::arrayindices) <= -1) goto oops;
return 0;
oops:
HawkStd::close ();
HawkStd::close();
return -1;
}
int sleep (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len)
{
if (args[0].isIndexed())
if (args[0].isIndexed())
{
run.setError (HAWK_EINVAL);
return -1;
@ -92,8 +92,8 @@ public:
/*Value arg;
if (run.getGlobal(idLastSleep, arg) == 0)
hawk_printf (HAWK_T("GOOD: [%d]\n"), (int)arg.toInt());
else { hawk_printf (HAWK_T("BAD:\n")); }
hawk_printf(HAWK_T("GOOD: [%d]\n"), (int)arg.toInt());
else { hawk_printf(HAWK_T("BAD:\n")); }
*/
if (run.setGlobal (idLastSleep, x) <= -1) return -1;
@ -111,13 +111,13 @@ public:
int sumintarray (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len)
{
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// print sumintarray(x);
// }
hawk_int_t x = 0;
if (args[0].isIndexed())
if (args[0].isIndexed())
{
Value val(run);
Value::Index idx;
@ -140,10 +140,10 @@ public:
int arrayindices (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len)
{
// create another array composed of array indices
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// y=arrayindices(x);
// for (i in y) print y[i];
// BEGIN {
// for(i=0;i<=10;i++) x[i]=i;
// y=arrayindices(x);
// for (i in y) print y[i];
// }
if (!args[0].isIndexed()) return 0;
@ -159,10 +159,10 @@ public:
iidx, idx.pointer(), idx.length()) <= -1) return -1;
ii = args[0].getNextIndex (&idx, ii);
}
return 0;
}
private:
int idLastSleep;
};
@ -173,25 +173,25 @@ static void print_error (const hawk_bch_t* fmt, ...)
{
va_list va;
fprintf (stderr, "ERROR: ");
fprintf(stderr, "ERROR: ");
va_start (va, fmt);
vfprintf (stderr, fmt, va);
vfprintf(stderr, fmt, va);
va_end (va);
}
static void print_error (MyHawk& hawk)
static void print_error(MyHawk& hawk)
{
hawk_errnum_t code = hawk.getErrorNumber();
hawk_loc_t loc = hawk.getErrorLocation();
if (loc.file)
{
print_error ("code %d line %lu at %s - %s\n", (int)code, (unsigned long int)loc.line, hawk.getErrorLocationFileB(), hawk.getErrorMessageB());
print_error("code %d line %lu at %s - %s\n", (int)code, (unsigned long int)loc.line, hawk.getErrorLocationFileB(), hawk.getErrorMessageB());
}
else
{
print_error ("code %d line %lu - %s\n", (int)code, (unsigned long int)loc.line, hawk.getErrorMessageB());
print_error("code %d line %lu - %s\n", (int)code, (unsigned long int)loc.line, hawk.getErrorMessageB());
}
}
@ -215,7 +215,7 @@ static int setsignal (int sig, void(*handler)(int), int restart)
sa_int.sa_handler = handler;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
if (restart)
@ -269,15 +269,15 @@ static void unset_signal (void)
static void print_usage (FILE* out, const hawk_bch_t* argv0)
{
fprintf (out, "USAGE: %s [options] -f sourcefile [ -- ] [datafile]*\n", argv0);
fprintf (out, " %s [options] [ -- ] sourcestring [datafile]*\n", argv0);
fprintf (out, "Where options are:\n");
fprintf (out, " -h print this message\n");
fprintf (out, " -f sourcefile set the source script file\n");
fprintf (out, " -d deparsedfile set the deparsing output file\n");
fprintf (out, " -o outputfile set the console output file\n");
fprintf (out, " -F string set a field separator(FS)\n");
fprintf (out, " -I string set include directories\n");
fprintf(out, "USAGE: %s [options] -f sourcefile [ -- ] [datafile]*\n", argv0);
fprintf(out, " %s [options] [ -- ] sourcestring [datafile]*\n", argv0);
fprintf(out, "Where options are:\n");
fprintf(out, " -h print this message\n");
fprintf(out, " -f sourcefile set the source script file\n");
fprintf(out, " -d deparsedfile set the deparsing output file\n");
fprintf(out, " -o outputfile set the console output file\n");
fprintf(out, " -F string set a field separator(FS)\n");
fprintf(out, " -I string set include directories\n");
}
@ -294,7 +294,7 @@ struct cmdline_t
int argc;
};
static int handle_cmdline (MyHawk& awk, int argc, hawk_bch_t* argv[], cmdline_t* cmdline)
static int handle_cmdline (MyHawk& hawk, int argc, hawk_bch_t* argv[], cmdline_t* cmdline)
{
static hawk_bcli_t opt =
{
@ -309,7 +309,7 @@ static int handle_cmdline (MyHawk& awk, int argc, hawk_bch_t* argv[], cmdline_t*
switch (c)
{
case 'h':
print_usage (stdout, argv[0]);
print_usage(stdout, argv[0]);
return 0;
case 'F':
@ -333,15 +333,15 @@ static int handle_cmdline (MyHawk& awk, int argc, hawk_bch_t* argv[], cmdline_t*
break;
case '?':
print_error ("illegal option - '%c'\n", opt.opt);
print_error("illegal option - '%c'\n", opt.opt);
return -1;
case ':':
print_error ("bad argument for '%c'\n", opt.opt);
print_error("bad argument for '%c'\n", opt.opt);
return -1;
default:
print_usage (stderr, argv[0]);
print_usage(stderr, argv[0]);
return -1;
}
}
@ -351,16 +351,16 @@ static int handle_cmdline (MyHawk& awk, int argc, hawk_bch_t* argv[], cmdline_t*
cmdline->argc = 0;
while (opt.ind < argc)
{
if (awk.addArgument(argv[opt.ind++]) <= -1)
if (hawk.addArgument(argv[opt.ind++]) <= -1)
{
print_error (awk);
print_error(hawk);
return -1;
}
}
if (!cmdline->ins && !cmdline->inf)
{
print_usage (stderr, argv[0]);
print_usage(stderr, argv[0]);
return -1;
}
@ -388,7 +388,7 @@ static int make_args_for_exec (cmdline_t* cmdline, MyHawk& hawk, MyHawk::Run* ru
cmdline->argv = new HAWK::Hawk::Value[count - 1];
for (hawk_oow_t i = 1; i < count; i++)
{
if (cmdline->argv[i - 1].setStr(run, hawk.getArgument(i)) <= -1)
if (cmdline->argv[i - 1].setStr(run, hawk.getArgument(i)) <= -1)
{
free_args_for_exec (cmdline);
return -1;
@ -418,8 +418,8 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
// ARGV[0]
if (hawk.addArgument(HAWK_T("hawk51")) <= -1)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
if ((n = handle_cmdline(hawk, argc, argv, &cmdline)) <= 0) return n;
@ -427,45 +427,45 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
if (cmdline.incdirs) hawk.setIncludeDirs (cmdline.incdirs);
MyHawk::Source* in, * out;
MyHawk::SourceString in_str(cmdline.ins);
MyHawk::SourceFile in_file(cmdline.inf);
MyHawk::SourceFile in_file(cmdline.inf);
MyHawk::SourceFile out_file(cmdline.outf);
in = (cmdline.ins)? (MyHawk::Source*)&in_str: (MyHawk::Source*)&in_file;
out = (cmdline.outf)? (MyHawk::Source*)&out_file: &MyHawk::Source::NONE;
run = hawk.parse(*in, *out);
if (!run)
if (!run)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
if (cmdline.inf && run->setGlobal(HAWK_GBL_SCRIPTNAME, cmdline.inf, hawk_count_bcstr(cmdline.inf)) <= -1)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
if (cmdline.fs)
{
MyHawk::Value fs (run);
if (fs.setStr(cmdline.fs) <= -1)
if (fs.setStr(cmdline.fs) <= -1)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
if (hawk.setGlobal(HAWK_GBL_FS, fs) <= -1)
if (hawk.setGlobal(HAWK_GBL_FS, fs) <= -1)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
}
if (cmdline.outc)
if (cmdline.outc)
{
if (hawk.addConsoleOutput(cmdline.outc) <= -1)
{
print_error (hawk);
return -1;
print_error(hawk);
return -1;
}
}
@ -473,18 +473,18 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
if (make_args_for_exec(&cmdline, hawk, run) <= -1) // data made here is not uself if hawk.loop() invoked in hawk.exec().
{
print_error (hawk);
print_error(hawk);
return -1;
}
//if (hawk.loop(&ret) <= -1)
if (hawk.exec(&ret, cmdline.argv, cmdline.argc) <= -1)
{
print_error (hawk);
return -1;
//if (hawk.loop(&ret) <= -1)
if (hawk.exec(&ret, cmdline.argv, cmdline.argc) <= -1)
{
print_error(hawk);
return -1;
}
free_args_for_exec (&cmdline);
free_args_for_exec(&cmdline);
return 0;
}
@ -497,7 +497,7 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
if (hawk.open() <= -1)
{
print_error (hawk);
print_error(hawk);
return -1;
}
app_hawk = &hawk;
@ -507,7 +507,7 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[])
unset_signal ();
app_hawk = HAWK_NULL;
hawk.close ();
hawk.close();
return n;
}

View File

@ -16,14 +16,14 @@ static void print_error (HAWK::SedStd& sed)
{
hawk_errnum_t code = sed.getErrorNumber();
hawk_loc_t loc = sed.getErrorLocation();
print_error ("code %d line %lu column %lu - %s\n", (int)code, (unsigned long int)loc.line, (unsigned long int)loc.colm, sed.getErrorMessageB());
print_error("code %d line %lu column %lu - %s\n", (int)code, (unsigned long int)loc.line, (unsigned long int)loc.colm, sed.getErrorMessageB());
}
int execute_sed (int argc, hawk_bch_t* argv[])
{
if (argc < 2 || argc > 4)
{
print_error ("USAGE: %s command-string [input-file [output-file]]\n", argv[0]);
print_error("USAGE: %s command-string [input-file [output-file]]\n", argv[0]);
return -1;
}
@ -31,15 +31,15 @@ int execute_sed (int argc, hawk_bch_t* argv[])
if (sed.open() <= -1)
{
print_error (sed);
print_error(sed);
return -1;
}
HAWK::SedStd::StringStream sstream (argv[1]);
HAWK::SedStd::StringStream sstream(argv[1]);
if (sed.compile(sstream) <= -1)
{
print_error (sed);
sed.close ();
print_error(sed);
sed.close();
return -1;
}
@ -50,8 +50,8 @@ int execute_sed (int argc, hawk_bch_t* argv[])
if (sed.execute(ifstream, ofstream) <= -1)
{
print_error (sed);
sed.close ();
print_error(sed);
sed.close();
return -1;
}
@ -60,8 +60,8 @@ int execute_sed (int argc, hawk_bch_t* argv[])
HAWK::SedStd::StringStream ostream2;
if (sed.execute(istream2, ostream2) <= -1)
{
print_error (sed);
sed.close ();
print_error(sed);
sed.close();
return -1;
}
hawk_oow_t len;
@ -72,23 +72,22 @@ int execute_sed (int argc, hawk_bch_t* argv[])
HAWK::SedStd::StringStream sstream ("s|CCC|BBBBBBBBBBB|g");
if (sed.compile(sstream) <= -1)
{
print_error (sed);
sed.close ();
print_error(sed);
sed.close();
return -1;
}
HAWK::SedStd::FileStream of("/dev/stdout");
if (sed.execute(ostream2, of) <= -1)
{
print_error (sed);
sed.close ();
print_error(sed);
sed.close();
return -1;
}
}
#endif
sed.close ();
sed.close();
return 0;
}