improved the standard console handler to handle when ARGV has been overridden in a script
This commit is contained in:
86
qse/samples/awk/awk01.c
Normal file
86
qse/samples/awk/awk01.c
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* $Id: awk01.c 195 2009-06-10 13:18:25Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/awk/std.h>
|
||||
#include <qse/cmn/stdio.h>
|
||||
|
||||
const qse_char_t* src = QSE_T(
|
||||
"BEGIN {"
|
||||
" for (i=2;i<=9;i++)"
|
||||
" {"
|
||||
" for (j=1;j<=9;j++)"
|
||||
" print i \"*\" j \"=\" i * j;"
|
||||
" print \"---------------------\";"
|
||||
" }"
|
||||
"}"
|
||||
);
|
||||
|
||||
int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
qse_awk_parsestd_in_t psin;
|
||||
int ret;
|
||||
|
||||
awk = qse_awk_openstd (0);
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
psin.type = QSE_AWK_PARSESTD_CP;
|
||||
psin.u.cp = src;
|
||||
|
||||
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_geterrmsg(awk));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
rtx = qse_awk_rtx_openstd (
|
||||
awk,
|
||||
0,
|
||||
QSE_T("awk01"),
|
||||
QSE_NULL, /* stdin */
|
||||
QSE_NULL /* stdout */
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_geterrmsg(awk));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
ret = qse_awk_rtx_loop (rtx);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_rtx_geterrmsg(rtx));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
oops:
|
||||
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
|
||||
if (awk != QSE_NULL) qse_awk_close (awk);
|
||||
return -1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user