This commit is contained in:
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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++;
|
||||
|
@ -37,12 +37,12 @@ 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 (
|
||||
rtx = hawk_rtx_openstd(
|
||||
hawk,
|
||||
0,
|
||||
HAWK_T("hawk02"),
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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 ()
|
||||
{
|
||||
@ -70,13 +70,13 @@ public:
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -418,7 +418,7 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
|
||||
// ARGV[0]
|
||||
if (hawk.addArgument(HAWK_T("hawk51")) <= -1)
|
||||
{
|
||||
print_error (hawk);
|
||||
print_error(hawk);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -435,13 +435,13 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
|
||||
run = hawk.parse(*in, *out);
|
||||
if (!run)
|
||||
{
|
||||
print_error (hawk);
|
||||
print_error(hawk);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cmdline.inf && run->setGlobal(HAWK_GBL_SCRIPTNAME, cmdline.inf, hawk_count_bcstr(cmdline.inf)) <= -1)
|
||||
{
|
||||
print_error (hawk);
|
||||
print_error(hawk);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -450,12 +450,12 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
|
||||
MyHawk::Value fs (run);
|
||||
if (fs.setStr(cmdline.fs) <= -1)
|
||||
{
|
||||
print_error (hawk);
|
||||
print_error(hawk);
|
||||
return -1;
|
||||
}
|
||||
if (hawk.setGlobal(HAWK_GBL_FS, fs) <= -1)
|
||||
{
|
||||
print_error (hawk);
|
||||
print_error(hawk);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -464,7 +464,7 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[])
|
||||
{
|
||||
if (hawk.addConsoleOutput(cmdline.outc) <= -1)
|
||||
{
|
||||
print_error (hawk);
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user