improved the standard console handler to handle when ARGV has been overridden in a script

This commit is contained in:
hyung-hwan 2009-06-25 02:29:33 +00:00
parent 4b139e0472
commit 393dd9be8d
82 changed files with 593 additions and 101 deletions

View File

@ -4,7 +4,7 @@ AUTOMAKE_OPTION = foreign
# EXTRA_DIST =
SUBDIRS = lib cmd
DIST_SUBDIRS = $(SUBDIRS) include test doc
DIST_SUBDIRS = $(SUBDIRS) include regress samples doc
install-data-hook:
cd include && $(MAKE) install

View File

@ -213,7 +213,7 @@ ACLOCAL_AMFLAGS = -I ac/m4
AUTOMAKE_OPTION = foreign
# EXTRA_DIST =
SUBDIRS = lib cmd
DIST_SUBDIRS = $(SUBDIRS) include test doc
DIST_SUBDIRS = $(SUBDIRS) include regress samples doc
all: all-recursive
.SUFFIXES:

View File

@ -39,11 +39,6 @@ run_init()
run_script_for_init_main "$script"
done
for script in emp-???.awk
do
run_script_for_init "$script" "emp-en.data"
done
for script in cou-???.awk
do
run_script_for_init "$script" "cou-en.data"
@ -153,20 +148,6 @@ run_test()
}
done
for script in emp-???.awk
do
run_script_for_test "$script" "emp-en.data"
[ $? -ne 0 ] && {
echo "###################################"
echo "PROBLEM(S) DETECTED IN $script.".
echo "###################################"
echo "Do you want to abort? [y/n]"
read ans
[ "$ans" = "y" -o "$ans" = "Y" ] && return 1
}
done
for script in cou-???.awk
do
run_script_for_test "$script" "cou-en.data"

12
qse/configure vendored
View File

@ -25648,7 +25648,7 @@ QSE_SIZEOF_DOUBLE=$ac_cv_sizeof_double
QSE_SIZEOF_LONG_DOUBLE=$ac_cv_sizeof_long_double
ac_config_files="$ac_config_files Makefile include/Makefile include/qse/Makefile include/qse/cmn/Makefile include/qse/utl/Makefile include/qse/sed/Makefile include/qse/awk/Makefile include/qse/lsp/Makefile lib/Makefile lib/cmn/Makefile lib/utl/Makefile lib/sed/Makefile lib/awk/Makefile lib/lsp/Makefile cmd/Makefile cmd/utl/Makefile cmd/awk/Makefile cmd/lsp/Makefile test/Makefile test/cmn/Makefile test/utl/Makefile test/sed/Makefile test/awk/Makefile doc/Makefile doc/page/Makefile doc/page.ko/Makefile doc/Doxyfile"
ac_config_files="$ac_config_files Makefile include/Makefile include/qse/Makefile include/qse/cmn/Makefile include/qse/utl/Makefile include/qse/sed/Makefile include/qse/awk/Makefile include/qse/lsp/Makefile lib/Makefile lib/cmn/Makefile lib/utl/Makefile lib/sed/Makefile lib/awk/Makefile lib/lsp/Makefile cmd/Makefile cmd/utl/Makefile cmd/awk/Makefile cmd/lsp/Makefile samples/Makefile samples/cmn/Makefile samples/utl/Makefile samples/sed/Makefile samples/awk/Makefile doc/Makefile doc/page/Makefile doc/page.ko/Makefile doc/Doxyfile"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@ -26684,11 +26684,11 @@ do
"cmd/utl/Makefile") CONFIG_FILES="$CONFIG_FILES cmd/utl/Makefile" ;;
"cmd/awk/Makefile") CONFIG_FILES="$CONFIG_FILES cmd/awk/Makefile" ;;
"cmd/lsp/Makefile") CONFIG_FILES="$CONFIG_FILES cmd/lsp/Makefile" ;;
"test/Makefile") CONFIG_FILES="$CONFIG_FILES test/Makefile" ;;
"test/cmn/Makefile") CONFIG_FILES="$CONFIG_FILES test/cmn/Makefile" ;;
"test/utl/Makefile") CONFIG_FILES="$CONFIG_FILES test/utl/Makefile" ;;
"test/sed/Makefile") CONFIG_FILES="$CONFIG_FILES test/sed/Makefile" ;;
"test/awk/Makefile") CONFIG_FILES="$CONFIG_FILES test/awk/Makefile" ;;
"samples/Makefile") CONFIG_FILES="$CONFIG_FILES samples/Makefile" ;;
"samples/cmn/Makefile") CONFIG_FILES="$CONFIG_FILES samples/cmn/Makefile" ;;
"samples/utl/Makefile") CONFIG_FILES="$CONFIG_FILES samples/utl/Makefile" ;;
"samples/sed/Makefile") CONFIG_FILES="$CONFIG_FILES samples/sed/Makefile" ;;
"samples/awk/Makefile") CONFIG_FILES="$CONFIG_FILES samples/awk/Makefile" ;;
"doc/Makefile") CONFIG_FILES="$CONFIG_FILES doc/Makefile" ;;
"doc/page/Makefile") CONFIG_FILES="$CONFIG_FILES doc/page/Makefile" ;;
"doc/page.ko/Makefile") CONFIG_FILES="$CONFIG_FILES doc/page.ko/Makefile" ;;

View File

@ -261,11 +261,11 @@ AC_CONFIG_FILES([
cmd/utl/Makefile
cmd/awk/Makefile
cmd/lsp/Makefile
test/Makefile
test/cmn/Makefile
test/utl/Makefile
test/sed/Makefile
test/awk/Makefile
samples/Makefile
samples/cmn/Makefile
samples/utl/Makefile
samples/sed/Makefile
samples/awk/Makefile
doc/Makefile
doc/page/Makefile
doc/page.ko/Makefile

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: awk.h 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -418,8 +418,7 @@ enum qse_awk_option_t
* implementations
*/
QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO |
QSE_AWK_NEWLINE | QSE_AWK_PABLOCK |
QSE_AWK_STRIPSPACES
QSE_AWK_NEWLINE | QSE_AWK_PABLOCK
};
/**
@ -1097,6 +1096,7 @@ qse_real_t qse_awk_strxtoreal (
);
qse_size_t qse_awk_longtostr (
qse_awk_t* awk,
qse_long_t value,
int radix,
const qse_char_t* prefix,
@ -1257,23 +1257,19 @@ qse_awk_val_t* qse_awk_rtx_getarg (
);
/******/
/****f* AWK/qse_awk_rtx_getgbl
* NAME
* qse_awk_rtx_getgbl - gets the value of a global variable
* INPUTS
* * rtx - a runtime context
* * id - a global variable ID. It is one of the predefined global
* variable IDs or a value returned by qse_awk_addgbl().
* RETURN
* The pointer to a value is returned. This function never fails
* so long as the ID is valid. Otherwise, you may fall into trouble.
* SYNOPSIS
/**
* The qse_awk_rtx_getgbl() gets the value of a global variable.
* The global variable ID @a id is one of the predefined global
* variable IDs or a value returned by qse_awk_addgbl().
* This function never fails so long as the ID is valid. Otherwise,
* you may get into trouble.
*
* @return a value pointer
*/
qse_awk_val_t* qse_awk_rtx_getgbl (
qse_awk_rtx_t* rtx,
int id
qse_awk_rtx_t* rtx, /**< a runtime context */
int id /**< a global variable ID */
);
/******/
/****f* AWK/qse_awk_rtx_setgbl
* NAME

View File

@ -1,5 +1,5 @@
/*
* $Id: std.h 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: std.h 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -34,6 +34,7 @@
* - StdAwk ARGV and console name handling
* - add RQ and LQ for more powerful record splitting
* - improve performance in qse_awk_rtx_readio() if RS is logner than 2 chars.
* - change the message for EIONMNL
*/
/**

View File

@ -1,5 +1,5 @@
/*
* $Id: err.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: err.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -146,7 +146,7 @@ const qse_char_t* qse_awk_dflerrstr (qse_awk_t* awk, qse_awk_errnum_t errnum)
QSE_T("no such io name found"),
QSE_T("i/o handler returned an error"),
QSE_T("i/o name empty"),
QSE_T("i/o name containing a null character"),
QSE_T("i/o name '${0}' containing a null character"),
QSE_T("not sufficient arguments to formatting sequence"),
QSE_T("recursion detected in format conversion"),
QSE_T("invalid character in CONVFMT"),

View File

@ -1,5 +1,5 @@
/*
* $Id: fnc.c 203 2009-06-17 12:43:50Z hyunghwan.chung $
* $Id: fnc.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -787,7 +787,7 @@ static int fnc_split (
/* put it into the map */
key_len = qse_awk_longtostr (
++nflds, 10, QSE_NULL, key, QSE_COUNTOF(key));
run->awk, ++nflds, 10, QSE_NULL, key, QSE_COUNTOF(key));
QSE_ASSERT (key_len != (qse_size_t)-1);
/* don't forget to update the reference count when you

View File

@ -1,5 +1,5 @@
/*
* $Id: misc.c 195 2009-06-10 13:18:25Z hyunghwan.chung $
* $Id: misc.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -533,8 +533,8 @@ done:
}
qse_size_t qse_awk_longtostr (
qse_long_t value, int radix, const qse_char_t* prefix,
qse_char_t* buf, qse_size_t size)
qse_awk_t* awk, qse_long_t value,
int radix, const qse_char_t* prefix, qse_char_t* buf, qse_size_t size)
{
qse_long_t t, rem;
qse_size_t len, ret, i;

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: parse.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -5697,7 +5697,8 @@ static int deparse (qse_awk_t* awk)
}
else
{
len = qse_awk_longtostr ((qse_long_t)i,
len = qse_awk_longtostr (
awk, (qse_long_t)i,
10, QSE_T("__g"), tmp, QSE_COUNTOF(tmp));
QSE_ASSERT (len != (qse_size_t)-1);
if (qse_awk_putsrcstrx (awk, tmp, len) == -1)
@ -5722,7 +5723,8 @@ static int deparse (qse_awk_t* awk)
}
else
{
len = qse_awk_longtostr ((qse_long_t)i,
len = qse_awk_longtostr (
awk, (qse_long_t)i,
10, QSE_T("__g"), tmp, QSE_COUNTOF(tmp));
QSE_ASSERT (len != (qse_size_t)-1);
if (qse_awk_putsrcstrx (awk, tmp, len) == -1)
@ -5858,7 +5860,8 @@ exit_deparse:
static qse_map_walk_t deparse_func (qse_map_t* map, qse_map_pair_t* pair, void* arg)
static qse_map_walk_t deparse_func (
qse_map_t* map, qse_map_pair_t* pair, void* arg)
{
struct deparse_func_t* df = (struct deparse_func_t*)arg;
/* CHECK: */
@ -5892,7 +5895,8 @@ static qse_map_walk_t deparse_func (qse_map_t* map, qse_map_pair_t* pair, void*
for (i = 0; i < fun->nargs; )
{
n = qse_awk_longtostr (i++, 10,
n = qse_awk_longtostr (
df->awk, i++, 10,
QSE_T("__p"), df->tmp, df->tmp_len);
QSE_ASSERT (n != (qse_size_t)-1);
PUT_SX (df, df->tmp, n);

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 207 2009-06-22 13:01:28Z hyunghwan.chung $
* $Id: run.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1004,7 +1004,8 @@ static int build_runarg (
}
key_len = qse_awk_longtostr (
argc, 10, QSE_NULL, key, QSE_COUNTOF(key));
run->awk, argc, 10,
QSE_NULL, key, QSE_COUNTOF(key));
QSE_ASSERT (key_len != (qse_size_t)-1);
/* increment reference count of v_tmp in advance as if
@ -2820,10 +2821,21 @@ static int run_print (qse_awk_rtx_t* run, qse_awk_nde_print_t* nde)
{
if (out[--len] == QSE_T('\0'))
{
qse_cstr_t errarg;
errarg.ptr = out;
/* provide length up to one character before
* the first null not to contains a null
* in an error message */
errarg.len = qse_strlen(out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL,
nde->line, &errarg
);
/* if so, it skips writing */
QSE_AWK_FREE (run->awk, out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL, nde->line, QSE_NULL);
return -1;
}
}
@ -2978,11 +2990,22 @@ static int run_printf (qse_awk_rtx_t* run, qse_awk_nde_print_t* nde)
{
if (out[--len] == QSE_T('\0'))
{
qse_cstr_t errarg;
errarg.ptr = out;
/* provide length up to one character before
* the first null not to contains a null
* in an error message */
errarg.len = qse_strlen(out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL,
nde->line, &errarg
);
/* the output destination name contains a null
* character. */
QSE_AWK_FREE (run->awk, out);
qse_awk_rtx_seterror (
run, QSE_AWK_EIONMNL, nde->line, QSE_NULL);
return -1;
}
}
@ -6380,7 +6403,9 @@ static qse_awk_val_t* eval_getline (qse_awk_rtx_t* run, qse_awk_nde_t* nde)
if (in[--len] == QSE_T('\0'))
{
/* the input source name contains a null
* character. make getline return -1 */
* character. make getline return -1.
* unlike print & printf, it is not a hard
* error */
QSE_AWK_FREE (run->awk, in);
n = -1;
goto skip_read;

View File

@ -1,5 +1,5 @@
/*
* $Id: std.c 209 2009-06-23 13:29:18Z hyunghwan.chung $
* $Id: std.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -78,6 +78,7 @@ typedef struct rxtn_t
struct {
const qse_char_t*const* files;
qse_size_t index;
qse_size_t count;
} in;
struct
@ -616,15 +617,92 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
* any fields of riod when the open operation fails */
qse_sio_t* sio;
const qse_char_t* file;
qse_awk_val_t* argv;
qse_map_t* map;
qse_map_pair_t* pair;
qse_char_t ibuf[128];
qse_size_t ibuflen;
qse_awk_val_t* v;
qse_awk_rtx_valtostr_out_t out;
nextfile:
file = rxtn->c.in.files[rxtn->c.in.index];
if (file == QSE_NULL)
{
/* no more input file */
if (rxtn->c.in.count == 0)
{
/* all ARGVs are empty strings.
* so no console files were opened.
* open the standard input here.
*
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
riod->handle = qse_sio_in;
rxtn->c.in.count++;
return 1;
}
return 0;
}
/* handle special case when ARGV[x] has been altered.
* so from here down, the file name gotten from
* rxtn->c.in.files is not important and is overridden
* from ARGV.
* 'BEGIN { ARGV[1]="file3"; }
* { print $0; }' file1 file2
*/
argv = qse_awk_rtx_getgbl (rtx, QSE_AWK_GBL_ARGV);
QSE_ASSERT (argv != QSE_NULL);
QSE_ASSERT (argv->type == QSE_AWK_VAL_MAP);
map = ((qse_awk_val_map_t*)argv)->map;
QSE_ASSERT (map != QSE_NULL);
ibuflen = qse_awk_longtostr (
rtx->awk, rxtn->c.in.index + 1, 10, QSE_NULL,
ibuf, QSE_COUNTOF(ibuf));
pair = qse_map_search (map, ibuf, ibuflen);
QSE_ASSERT (pair != QSE_NULL);
v = QSE_MAP_VPTR(pair);
QSE_ASSERT (v != QSE_NULL);
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return -1;
if (out.u.cpldup.len == 0)
{
/* the name is empty */
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
rxtn->c.in.index++;
goto nextfile;
}
if (qse_strlen(out.u.cpldup.ptr) < out.u.cpldup.len)
{
/* the name contains one or more '\0' */
qse_cstr_t errarg;
errarg.ptr = out.u.cpldup.ptr;
/* use this length not to contains '\0'
* in an error message */
errarg.len = qse_strlen(out.u.cpldup.ptr);
qse_awk_rtx_seterror (
rtx, QSE_AWK_EIONMNL, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
file = out.u.cpldup.ptr;
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
{
/* special file name '-' */
@ -643,6 +721,8 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
qse_awk_rtx_seterror (
rtx, QSE_AWK_EOPEN, 0, &errarg);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
}
@ -651,10 +731,15 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
rtx, file, qse_strlen(file)) == -1)
{
if (sio != qse_sio_in) qse_sio_close (sio);
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
return -1;
}
qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
riod->handle = sio;
/* increment the counter of files successfully opened */
rxtn->c.in.count++;
}
rxtn->c.in.index++;
@ -704,7 +789,6 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
}
}
if (qse_awk_rtx_setofilename (
rtx, file, qse_strlen(file)) == -1)
{
@ -932,6 +1016,7 @@ qse_awk_rtx_t* qse_awk_rtx_openstd (
rxtn->c.in.files = icf;
rxtn->c.in.index = 0;
rxtn->c.in.count = 0;
rxtn->c.out.files = ocf;
rxtn->c.out.index = 0;

View File

@ -1,5 +1,5 @@
/*
* $Id: tree.c 75 2009-02-22 14:10:34Z hyunghwan.chung $
* $Id: tree.c 210 2009-06-24 08:29:33Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -320,7 +320,10 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
QSE_ASSERT (px->id.idxa != (qse_size_t)-1);
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10, QSE_NULL, tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTR (awk, QSE_T("__p"));
PUT_SRCSTRX (awk, tmp, n);
@ -338,8 +341,10 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__p"));
n = qse_awk_longtostr (
awk,
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T("["));
PRINT_EXPRESSION_LIST (awk, px->idx);
@ -396,8 +401,12 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__g"));
n = qse_awk_longtostr (
px->id.idxa, 10,
QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10,
QSE_NULL,
tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTRX (awk, tmp, n);
}
}
@ -434,8 +443,12 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
PUT_SRCSTR (awk, QSE_T("__g"));
n = qse_awk_longtostr (
px->id.idxa, 10,
QSE_NULL, tmp, QSE_COUNTOF(tmp));
awk,
px->id.idxa,
10,
QSE_NULL,
tmp, QSE_COUNTOF(tmp)
);
PUT_SRCSTRX (awk, tmp, n);
}
PUT_SRCSTR (awk, QSE_T("["));
@ -460,8 +473,13 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
px->id.idxa,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
}
else
@ -481,8 +499,13 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
px->id.idxa, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
px->id.idxa,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T("["));
}
@ -616,16 +639,26 @@ static int print_statement (qse_awk_t* awk, qse_awk_nde_t* p, int depth)
{
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
i, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
i,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T(", "));
}
PUT_SRCSTR (awk, QSE_T("__l"));
n = qse_awk_longtostr (
i, 10, QSE_NULL,
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt));
awk,
i,
10,
QSE_NULL,
awk->tmp.fmt,
QSE_COUNTOF(awk->tmp.fmt)
);
PUT_SRCSTRX (awk, awk->tmp.fmt, n);
PUT_SRCSTR (awk, QSE_T(";"));
PUT_NEWLINE (awk);

14
qse/regress/awk/adm.d1 Normal file
View File

@ -0,0 +1,14 @@
ld zero # initialize sum to zero
st sum
loop get # read a number
jz done # no more input if number is zero
add sum # add in accumulated sum
st sum # store new value back in sum
j loop # go back and read another number
done ld sum # print sum
put
halt
zero const 0
sum const

51
qse/regress/awk/asm.awk Normal file
View File

@ -0,0 +1,51 @@
#
# $Id: asm.awk,v 1.4 2007/09/27 11:33:45 bacon Exp $
#
# Taken from the book "The AWK Programming Language"
#
BEGIN {
srcfile = ARGV[1];
ARGV[1] = "";
tempfile = "asm.temp";
n = split("const get put ld st add sub jpos jz j halt", x);
for (i = 1; i <= n; i++) op[x[i]] = i - 1;
# PASS 1
FS = "[ \t]+";
while (getline <srcfile > 0) {
sub (/#.*/, "");
symtab[$1] = nextmem;
if ($2 != "") {
print $2 "\t" $3 >tempfile;
nextmem++;
}
}
close (tempfile);
# PASS 2
nextmem = 0;
while (getline <tempfile > 0) {
if ($2 !~ /^[0-9]*$/) $2 = symtab[$2];
mem[nextmem++] = 1000 * op[$1] + $2;
}
# INTERPRETER
for (pc = 0; pc >= 0; ) {
addr = mem[pc] % 1000;
code = int(mem[pc++] / 1000);
if (code == op["get"]) { if (getline acc <= 0) acc = 0; }
else if (code == op["put"]) { print acc; }
else if (code == op["st"]) { mem[addr] = acc; }
else if (code == op["ld"]) { acc = mem[addr]; }
else if (code == op["add"]) { acc += mem[addr]; }
else if (code == op["sub"]) { acc -= mem[addr]; }
else if (code == op["jpos"]) { if (acc > 0) pc = addr; }
else if (code == op["jz"]) { if (acc == 0) pc = addr; }
else if (code == op["j"]) { pc = addr; }
else if (code == op["halt"]) { pc = -1; }
else { pc = -1; }
}
}

14
qse/regress/awk/asm.d1 Normal file
View File

@ -0,0 +1,14 @@
ld zero # initialize sum to zero
st sum
loop get # read a number
jz done # no more input if number is zero
add sum # add in accumulated sum
st sum # store new value back in sum
j loop # go back and read another number
done ld sum # print sum
put
halt
zero const 0
sum const

11
qse/regress/awk/asm.d2 Normal file
View File

@ -0,0 +1,11 @@
10
20
30
40
50
60
70
80
90
100
-1

6
qse/regress/awk/emp.d Normal file
View File

@ -0,0 +1,6 @@
Beth 4.00 0
Dan 3.74 0
Kathy 4.00 10
Mark 5.00 20
Mary 5.50 22
Susie 4.25 18

40
qse/regress/awk/hanoi.awk Normal file
View File

@ -0,0 +1,40 @@
BEGIN {
n = arg("-n",5)
for (j=0; j<n; j++) push(0,n-j)
showstacks()
hanoi(n,0,1,2)
}
function hanoi(n,a,b,c) {
if (n==1) {
move(a,b)
} else {
hanoi(n-1,a,c,b)
move(a,b)
hanoi(n-1,c,b,a)
}
}
function move(i,j) {
push(j,pop(i))
showstacks()
}
function showstacks( i,j) {
for (i=0; i<=2; i++) {
printf "%s ", i
for (j=0; j<sp[i]; j++) printf "%s", stack[i,j]
print "" }
print ""
}
function arg(tag,default) {
for(i in ARGV)
if (ARGV[i] ~ tag)
return ARGV[i+1]
return default
}
function push(i,v) { stack[i,sp[i]++]=v }
function pop(i) { return stack[i,--sp[i]] }

View File

@ -0,0 +1,56 @@
#.H1 Quicksort.awk
#.H2 Synopsis
#.P cat numbers | gawk -f quicksort.awk
#.H2 Download
#.P
#Download from
#.URL http://lawker.googlecode.com/svn/fridge/lib/awk/quicksort1.awk LAWKER.
#.H2 Description
#.P
#Some Awk implementations come with built in sort routines (e.g. Gawk's asort and asorti functions). But it
#can be useful to code these yourself, especially in you are doing data structure tricks.
#.P
#Quicksort selects a pivot and divides the data into values above and below the pivot. Sorting then
#recurses on these sub-lists.
#.H2 Code
#.H3 Loading the data
#.PRE
#BEGIN { RS = ""; FS = "\n" }
{ A[NR] = $1 }
END {
qsort(A, 1, NR)
for (i = 1; i <= NR; i++) {
print A[i] #+ 0;
#if (i == NR) break
#print ""
}
}
#./PRE
#.H3 Sorting the data
#.PRE
function qsort(A, left, right, i, last) {
if (left >= right)
return
swap(A, left, left+int((right-left+1)*rand()))
last = left
for (i = left+1; i <= right; i++)
{
# print A[i] "/" A[left] "....." (A[i] < A[left])
if (A[i] < A[left])
{
swap(A, ++last, i)
}
}
swap(A, left, last)
qsort(A, left, last-1)
qsort(A, last+1, right)
}
function swap(A, i, j, t) {
t = A[i]; A[i] = A[j]; A[j] = t
}
#./PRE
#.H2 See also
#.P
#.URL http://awk.info/?quicksort2 quicksort2.awk
#.H2 Authors
#.P Alfred Aho, Peter Weinberger, Brian Kernighan, 1988.

View File

@ -0,0 +1,16 @@
34
0.11111111111111111111111111111
0.0000000000
0b11111
0xA
1
35
92
19123
29
1.E12
11.2839091
12
99X
301
493

View File

@ -0,0 +1,17 @@
BEGIN {
recurse1 = "../../cmd/awk/qseawk -f quicksort2.awk #" rand()
recurse2 = "../../cmd/awk/qseawk -f quicksort2.awk #" rand()
}
NR == 1 {
pivot=$0;
next
}
NR > 1 { if($0 < pivot) { print | recurse1 }
if($0 > pivot) { print | recurse2 }
}
END {
close(recurse1)
if(NR > 0) print pivot
close(recurse2)
}

View File

@ -0,0 +1,16 @@
34
0.11111111111111111111111111111
0.0000000000
0b11111
0xA
1
35
92
19123
29
1.E12
11.2839091
12
99X
301
493

85
qse/regress/awk/regress.sh Executable file
View File

@ -0,0 +1,85 @@
#!/bin/sh
echo_so()
{
tput smso
while [ $# -gt 0 ]
do
echo -n "$1 "
shift
done
echo
tput rmso
}
print_usage()
{
echo "Usage: $0 init"
echo " $0 test"
}
###################
# MAIN #
###################
QSEAWK="../../cmd/awk/qseawk"
PROGS="
emp-001.awk/emp.d//
emp-002.awk/emp.d//
emp-003.awk/emp.d//
emp-004.awk/emp.d//
emp-005.awk/emp.d//
emp-006.awk/emp.d//
emp-007.awk/emp.d//
emp-008.awk/emp.d//
emp-009.awk/emp.d//
emp-010.awk/emp.d//
emp-011.awk/emp.d//
emp-012.awk/emp.d//
emp-013.awk/emp.d//
emp-014.awk/emp.d//
emp-015.awk/emp.d//
emp-016.awk/emp.d//
emp-017.awk/emp.d//
emp-018.awk/emp.d//
emp-019.awk/emp.d//
emp-020.awk/emp.d//
emp-021.awk/emp.d//
emp-022.awk/emp.d//
emp-023.awk/emp.d//
emp-024.awk/emp.d//
emp-025.awk/emp.d//
emp-026.awk/emp.d//
emp-027.awk/emp.d//
quicksort.awk/quicksort.d//
quicksort2.awk/quicksort2.d//
asm.awk/asm.d1/asm.d2/
stripcomment.awk/stripcomment.d//
wordfreq.awk/wordfreq.awk//
hanoi.awk//
"
[ -x "${QSEAWK}" ] || {
echo "ERROR: ${QSEAWK} not found"
exit 1;
}
for prog in ${PROGS}
do
script="`echo ${prog} | cut -d/ -f1`"
datafile="`echo ${prog} | cut -d/ -f2`"
redinfile="`echo ${prog} | cut -d/ -f3`"
awkopts="`echo ${prog} | cut -d/ -f4`"
if [ -n "${redinfile}" ]
then
echo_so "${QSEAWK} ${awkopts} -f ${script} ${datafile} < ${redinfile}"
${QSEAWK} ${awkopts} -f ${script} ${datafile} < ${redinfile}
else
echo_so "${QSEAWK} ${awkopts} -f ${script} ${datafile}"
${QSEAWK} ${awkopts} -f ${script} ${datafile}
fi
done
exit 0

View File

@ -0,0 +1,11 @@
BEGIN {
RS = "/\\*([^*]|\\*+[^/*])*\\*+/"
# comment is record separator
ORS = " "
getline hold
}
{ print hold ; hold = $0 }
END { printf "%s" , hold }

View File

@ -0,0 +1,10 @@
/* sample C program */
#include <stdio.h>
int main ()
{
/* print hello world
* with printf */
printf ("hello, world\n");
return 0;
}

View File

@ -0,0 +1,20 @@
# wordfreq.awk --- print list of word frequencies
{
$0 = tolower($0); # remove case distinctions
# remove punctuation
a=0;
gsub(/[^[:alnum:]_[:blank:]]/, " ", $a);
#gsub(/[^[:alnum:]_[:blank:]]/, " ");
for (i = 1; i <= NF; i++) freq[$i]++;
}
#/[^kkka-bcx-dd-y]|abc|def/
END {
for (word in freq)
print word, freq[word];
}

View File

@ -31,7 +31,7 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
subdir = test
subdir = samples
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
@ -208,9 +208,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign test/Makefile'; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign samples/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign test/Makefile
$(AUTOMAKE) --foreign samples/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \

View File

@ -35,7 +35,7 @@ host_triplet = @host@
bin_PROGRAMS = awk01$(EXEEXT) awk02$(EXEEXT) awk03$(EXEEXT) \
awk04$(EXEEXT) $(am__EXEEXT_1)
@ENABLE_CXX_TRUE@am__append_1 = awk05
subdir = test/awk
subdir = samples/awk
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
@ -259,9 +259,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign test/awk/Makefile'; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign samples/awk/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign test/awk/Makefile
$(AUTOMAKE) --foreign samples/awk/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \

View File

@ -35,7 +35,7 @@ host_triplet = @host@
bin_PROGRAMS = chr$(EXEEXT) str$(EXEEXT) sll$(EXEEXT) map$(EXEEXT) \
lda$(EXEEXT) fio$(EXEEXT) pio$(EXEEXT) sio$(EXEEXT) \
time$(EXEEXT)
subdir = test/cmn
subdir = samples/cmn
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
@ -268,9 +268,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign test/cmn/Makefile'; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign samples/cmn/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign test/cmn/Makefile
$(AUTOMAKE) --foreign samples/cmn/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \

View File

@ -34,7 +34,7 @@ build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS = sed01$(EXEEXT) $(am__EXEEXT_1)
@ENABLE_CXX_TRUE@am__append_1 = sed02 sed03
subdir = test/sed
subdir = samples/sed
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
@ -248,9 +248,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign test/sed/Makefile'; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign samples/sed/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign test/sed/Makefile
$(AUTOMAKE) --foreign samples/sed/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \

View File

@ -33,7 +33,7 @@ POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS =
subdir = test/utl
subdir = samples/utl
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
@ -202,9 +202,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign test/utl/Makefile'; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign samples/utl/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign test/utl/Makefile
$(AUTOMAKE) --foreign samples/utl/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \