Announcements‎ > ‎

AWK/Calling Functions [ Samples ]

posted May 25, 2008, 2:48 AM by Unknown user   [ updated Feb 16, 2009, 7:15 PM ]

[ Top ] [ Samples ]

DESCRIPTION

This program demonstrates how to use qse_awk_rtx_call().

SOURCE

 7 #include <qse/awk/awk.h>
8 #include <qse/utl/stdio.h>
9
10 static const qse_char_t* src = QSE_T(
11 "function init() { a = 20; }"
12 "function main() { a++; }"
13 "function fini() { print a; }"
14 );
15
16 static const qse_char_t* f[] =
17 {
18 QSE_T("init"),
19 QSE_T("main"),
20 QSE_T("main"),
21 QSE_T("main"),
22 QSE_T("main"),
23 QSE_T("fini"),
24 };
25
26 int main ()
27 {
28 qse_awk_t* awk = QSE_NULL;
29 qse_awk_rtx_t* rtx = QSE_NULL;
30 int ret, i;
31
32 /* create a main processor */
33 awk = qse_awk_opensimple ();
34 if (awk == QSE_NULL)
35 {
36 qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
37 ret = -1; goto oops;
38 }
39
40 /* don't allow BEGIN, END, pattern-action blocks */
41 qse_awk_setoption (awk, qse_awk_getoption(awk) & ~QSE_AWK_PABLOCK);
42
43 ret = qse_awk_parsesimple (
44 awk,
45 QSE_AWK_PARSE_STRING, src, /* parse AWK source in a string */
46 QSE_NULL /* no parse output */
47 );
48 if (ret == -1)
49 {
50 qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
51 qse_awk_geterrmsg(awk));
52 goto oops;
53 }
54
55 /* create a runtime context */
56 rtx = qse_awk_rtx_opensimple (
57 awk,
58 QSE_NULL /* no console files */
59 );
60 if (rtx == QSE_NULL)
61 {
62 qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
63 qse_awk_geterrmsg(awk));
64 ret = -1; goto oops;
65 }
66
67 /* invoke functions as indicated in the array f */
68 for (i = 0; i < QSE_COUNTOF(f); i++)
69 {
70 ret = qse_awk_rtx_call (rtx, f[i], QSE_NULL, 0);
71 if (ret == -1)
72 {
73 qse_fprintf (QSE_STDERR, QSE_T("%d error: %s\n"),
74 i, qse_awk_rtx_geterrmsg(rtx));
75 goto oops;
76 }
77 }
78
79 oops:
80 /* destroy a runtime context */
81 if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
82 /* destroy the processor */
83 if (awk != QSE_NULL) qse_awk_close (awk);
84 return ret;
85 }

Comments