qse/ase/awk/awk.h

414 lines
13 KiB
C
Raw Normal View History

2005-11-05 17:54:00 +00:00
/*
2006-10-13 10:18:39 +00:00
* $Id: awk.h,v 1.127 2006-10-13 10:18:10 bacon Exp $
2005-11-05 17:54:00 +00:00
*/
#ifndef _XP_AWK_AWK_H_
#define _XP_AWK_AWK_H_
#include <xp/types.h>
#include <xp/macros.h>
2006-01-18 15:16:01 +00:00
2006-03-27 11:43:17 +00:00
typedef struct xp_awk_t xp_awk_t;
2006-10-03 14:38:26 +00:00
typedef struct xp_awk_run_t xp_awk_run_t;
2006-06-22 04:25:44 +00:00
typedef struct xp_awk_val_t xp_awk_val_t;
typedef struct xp_awk_extio_t xp_awk_extio_t;
2006-08-04 17:36:40 +00:00
2006-08-31 04:21:04 +00:00
typedef struct xp_awk_syscas_t xp_awk_syscas_t;
2006-08-04 17:54:52 +00:00
typedef struct xp_awk_srcios_t xp_awk_srcios_t;
2006-08-04 17:36:40 +00:00
typedef struct xp_awk_runios_t xp_awk_runios_t;
typedef struct xp_awk_runcbs_t xp_awk_runcbs_t;
2006-10-10 14:09:23 +00:00
typedef struct xp_awk_runarg_t xp_awk_runarg_t;
2006-08-04 17:36:40 +00:00
2006-08-10 16:02:15 +00:00
typedef void (*xp_awk_lk_t) (xp_awk_t* awk, void* arg);
2005-11-05 17:54:00 +00:00
typedef xp_ssize_t (*xp_awk_io_t) (
2006-07-28 10:34:22 +00:00
int cmd, void* arg, xp_char_t* data, xp_size_t count);
2006-06-19 04:38:51 +00:00
2006-06-19 09:10:57 +00:00
struct xp_awk_extio_t
2006-06-19 04:38:51 +00:00
{
2006-10-13 10:18:39 +00:00
int type; /* [IN] console, file, coproc, pipe */
int mode; /* [IN] read, write, etc */
xp_char_t* name; /* [IN] */
void* custom_data; /* [IN] */
void* handle; /* [OUT] */
2006-06-28 14:19:01 +00:00
/* input buffer */
struct
{
xp_char_t buf[2048];
xp_size_t pos;
xp_size_t len;
xp_bool_t eof;
} in;
2006-06-19 09:10:57 +00:00
xp_awk_extio_t* next;
2006-06-19 04:38:51 +00:00
};
2006-08-31 04:21:04 +00:00
struct xp_awk_syscas_t
{
/* memory */
void* (*malloc) (xp_size_t n, void* custom_data);
void* (*realloc) (void* ptr, xp_size_t n, void* custom_data);
void (*free) (void* ptr, void* custom_data);
/* thread lock */
xp_awk_lk_t lock;
xp_awk_lk_t unlock;
2006-09-01 06:23:58 +00:00
/* character class */
xp_bool_t (*is_upper) (xp_cint_t c);
xp_bool_t (*is_lower) (xp_cint_t c);
xp_bool_t (*is_alpha) (xp_cint_t c);
xp_bool_t (*is_digit) (xp_cint_t c);
xp_bool_t (*is_xdigit) (xp_cint_t c);
xp_bool_t (*is_alnum) (xp_cint_t c);
xp_bool_t (*is_space) (xp_cint_t c);
xp_bool_t (*is_print) (xp_cint_t c);
xp_bool_t (*is_graph) (xp_cint_t c);
xp_bool_t (*is_cntrl) (xp_cint_t c);
xp_bool_t (*is_punct) (xp_cint_t c);
xp_cint_t (*to_upper) (xp_cint_t c);
xp_cint_t (*to_lower) (xp_cint_t c);
2006-09-25 06:17:19 +00:00
/* utilities */
void* (*memcpy) (void* dst, const void* src, xp_size_t n);
void* (*memset) (void* dst, int val, xp_size_t n);
2006-10-12 04:17:58 +00:00
2006-09-22 14:05:30 +00:00
int (*sprintf) (xp_char_t* buf, xp_size_t size, xp_char_t* fmt, ...);
2006-10-12 04:17:58 +00:00
int (*dprintf) (xp_char_t* fmt, ...);
void (*abort) (void);
2006-09-22 14:05:30 +00:00
2006-08-31 04:21:04 +00:00
void* custom_data;
};
2006-08-10 16:02:15 +00:00
2006-08-04 17:54:52 +00:00
struct xp_awk_srcios_t
{
xp_awk_io_t in;
xp_awk_io_t out;
void* custom_data;
};
2006-08-04 17:36:40 +00:00
struct xp_awk_runios_t
{
xp_awk_io_t pipe;
xp_awk_io_t coproc;
xp_awk_io_t file;
xp_awk_io_t console;
2006-10-13 10:18:39 +00:00
void* custom_data;
2006-08-04 17:36:40 +00:00
};
struct xp_awk_runcbs_t
2006-08-04 16:31:22 +00:00
{
2006-08-13 05:55:02 +00:00
void (*on_start) (xp_awk_t* awk, void* handle, void* arg);
void (*on_end) (xp_awk_t* awk, void* handle, int errnum, void* arg);
2006-08-04 17:36:40 +00:00
void* custom_data;
2006-08-04 16:31:22 +00:00
};
2006-10-10 14:09:23 +00:00
struct xp_awk_runarg_t
{
const xp_char_t* ptr;
xp_size_t len;
};
2006-08-31 04:21:04 +00:00
2006-01-19 16:28:21 +00:00
/* io function commands */
2005-11-05 17:54:00 +00:00
enum
{
2006-06-22 04:25:44 +00:00
XP_AWK_IO_OPEN = 0,
XP_AWK_IO_CLOSE = 1,
XP_AWK_IO_READ = 2,
XP_AWK_IO_WRITE = 3,
2006-08-22 15:11:13 +00:00
XP_AWK_IO_FLUSH = 4,
XP_AWK_IO_NEXT = 5
2006-06-22 04:25:44 +00:00
};
enum
{
2006-06-25 15:26:57 +00:00
XP_AWK_IO_PIPE_READ = 0,
XP_AWK_IO_PIPE_WRITE = 1,
2006-06-22 04:25:44 +00:00
2006-06-25 15:26:57 +00:00
XP_AWK_IO_FILE_READ = 0,
XP_AWK_IO_FILE_WRITE = 1,
XP_AWK_IO_FILE_APPEND = 2,
XP_AWK_IO_CONSOLE_READ = 0,
XP_AWK_IO_CONSOLE_WRITE = 1
2005-11-05 17:54:00 +00:00
};
2006-08-04 17:36:40 +00:00
/* various options */
2006-09-09 04:52:40 +00:00
enum xp_awk_option_t
2006-07-30 15:53:42 +00:00
{
/* allow undeclared variables */
2006-08-13 16:05:04 +00:00
XP_AWK_IMPLICIT = (1 << 0),
2006-07-30 15:53:42 +00:00
/* variable requires explicit declaration */
2006-08-13 16:05:04 +00:00
XP_AWK_EXPLICIT = (1 << 1),
2006-07-30 15:53:42 +00:00
/* a function name should not coincide to be a variable name */
2006-08-13 16:05:04 +00:00
XP_AWK_UNIQUE = (1 << 2),
2006-07-30 15:53:42 +00:00
/* allow variable shading */
2006-08-13 16:05:04 +00:00
XP_AWK_SHADING = (1 << 3),
2006-07-30 15:53:42 +00:00
/* support shift operators */
2006-08-13 16:05:04 +00:00
XP_AWK_SHIFT = (1 << 4),
2006-07-30 15:53:42 +00:00
/* support comments by a hash sign */
2006-08-13 16:05:04 +00:00
XP_AWK_HASHSIGN = (1 << 5),
2006-07-30 15:53:42 +00:00
/* support comments by double slashes */
2006-08-13 16:05:04 +00:00
XP_AWK_DBLSLASHES = (1 << 6),
2006-07-30 15:53:42 +00:00
/* support string concatenation in tokenization.
* this option can change the behavior of a certain construct.
* getline < "abc" ".def" is treated as if it is getline < "abc.def"
* when this option is on. If this option is off, the same expression
* is treated as if it is (getline < "abc") ".def". */
2006-08-13 16:05:04 +00:00
XP_AWK_STRCONCAT = (1 << 7),
2006-07-30 15:53:42 +00:00
/* support getline and print */
2006-08-13 16:05:04 +00:00
XP_AWK_EXTIO = (1 << 8),
2006-08-03 09:54:16 +00:00
/* support blockless patterns */
2006-08-13 16:05:04 +00:00
XP_AWK_BLOCKLESS = (1 << 9),
2006-01-19 16:28:21 +00:00
2006-08-04 17:36:40 +00:00
/* execution starts from main */
2006-08-13 16:05:04 +00:00
XP_AWK_RUNMAIN = (1 << 10),
/* use 1 as the start index for string operations */
2006-09-08 14:51:15 +00:00
XP_AWK_STRINDEXONE = (1 << 11),
/* strip off leading and trailing spaces when splitting a record
2006-09-08 14:57:43 +00:00
* into fields with a regular expression.
*
* Consider the following program.
* BEGIN { FS="[:[:space:]]+"; }
* {
* print "NF=" NF;
* for (i = 0; i < NF; i++) print i " [" $(i+1) "]";
* }
*
* The program splits " a b c " into [a], [b], [c] when this
* option is on while into [], [a], [b], [c], [] when it is off.
*/
2006-09-08 14:51:15 +00:00
XP_AWK_STRIPSPACES = (1 << 12)
2006-04-09 15:31:13 +00:00
};
2006-04-02 12:45:04 +00:00
/* error code */
2006-09-09 04:52:40 +00:00
enum ant_awk_errnum_t
2006-04-02 12:45:04 +00:00
{
2006-04-17 16:12:02 +00:00
XP_AWK_ENOERR, /* no error */
XP_AWK_ENOMEM, /* out of memory */
2006-06-19 09:10:57 +00:00
XP_AWK_EINVAL, /* invalid parameter */
2006-08-10 16:02:15 +00:00
XP_AWK_ERUNTIME, /* run-time error */
XP_AWK_ERUNNING, /* there are running instances */
XP_AWK_ETOOMANYRUNS, /* too many running instances */
2006-08-16 08:55:43 +00:00
XP_AWK_ERECURSION, /* recursion too deep */
2006-04-02 12:45:04 +00:00
2006-04-22 13:54:53 +00:00
XP_AWK_ESRCINOPEN,
XP_AWK_ESRCINCLOSE,
2006-08-06 12:35:06 +00:00
XP_AWK_ESRCINREAD,
XP_AWK_ESRCOUTOPEN,
XP_AWK_ESRCOUTCLOSE,
XP_AWK_ESRCOUTWRITE,
2006-04-22 13:54:53 +00:00
2006-08-03 09:54:16 +00:00
XP_AWK_ECONINOPEN,
XP_AWK_ECONINCLOSE,
XP_AWK_ECONINNEXT,
XP_AWK_ECONINDATA,
XP_AWK_ECONOUTOPEN,
XP_AWK_ECONOUTCLOSE,
XP_AWK_ECONOUTNEXT,
XP_AWK_ECONOUTDATA,
2006-04-17 16:12:02 +00:00
XP_AWK_ELXCHR, /* lexer came accross an wrong character */
XP_AWK_ELXUNG, /* lexer failed to unget a character */
XP_AWK_EENDSRC, /* unexpected end of source */
2006-08-29 15:01:45 +00:00
XP_AWK_EENDCOMMENT, /* unexpected end of a comment */
2006-04-17 16:12:02 +00:00
XP_AWK_EENDSTR, /* unexpected end of a string */
2006-04-24 11:26:00 +00:00
XP_AWK_EENDREX, /* unexpected end of a regular expression */
2006-04-17 16:12:02 +00:00
XP_AWK_ELBRACE, /* left brace expected */
XP_AWK_ELPAREN, /* left parenthesis expected */
XP_AWK_ERPAREN, /* right parenthesis expected */
XP_AWK_ERBRACK, /* right bracket expected */
XP_AWK_ECOMMA, /* comma expected */
XP_AWK_ESEMICOLON, /* semicolon expected */
XP_AWK_ECOLON, /* colon expected */
2006-04-26 15:53:17 +00:00
XP_AWK_EIN, /* keyword 'in' is expected */
XP_AWK_ENOTVAR, /* not a variable name after 'in' */
2006-04-17 16:12:02 +00:00
XP_AWK_EEXPRESSION, /* expression expected */
XP_AWK_EWHILE, /* keyword 'while' is expected */
XP_AWK_EASSIGNMENT, /* assignment statement expected */
XP_AWK_EIDENT, /* identifier expected */
2006-08-03 15:50:04 +00:00
XP_AWK_EBEGINBLOCK, /* BEGIN requires an action block */
XP_AWK_EENDBLOCK, /* END requires an action block */
2006-04-17 16:12:02 +00:00
XP_AWK_EDUPBEGIN, /* duplicate BEGIN */
XP_AWK_EDUPEND, /* duplicate END */
XP_AWK_EDUPFUNC, /* duplicate function name */
XP_AWK_EDUPPARAM, /* duplicate parameter name */
XP_AWK_EDUPVAR, /* duplicate variable name */
XP_AWK_EDUPNAME, /* duplicate name - function, variable, etc */
XP_AWK_EUNDEF, /* undefined identifier */
XP_AWK_ELVALUE, /* l-value required */
2006-06-21 15:37:51 +00:00
XP_AWK_ETOOFEWARGS, /* too few arguments */
2006-04-18 16:04:59 +00:00
XP_AWK_ETOOMANYARGS, /* too many arguments */
2006-08-06 15:03:42 +00:00
XP_AWK_ETOOMANYGLOBALS, /* too many global variables */
XP_AWK_ETOOMANYLOCALS, /* too many local variables */
XP_AWK_ETOOMANYPARAMS, /* too many parameters */
2006-08-01 15:57:43 +00:00
XP_AWK_EBREAK, /* break outside a loop */
XP_AWK_ECONTINUE, /* continue outside a loop */
XP_AWK_ENEXT, /* next illegal in BEGIN or END block */
XP_AWK_ENEXTFILE, /* nextfile illegal in BEGIN or END block */
2006-06-18 10:53:06 +00:00
XP_AWK_EGETLINE, /* getline expected */
2006-04-05 15:56:20 +00:00
/* run time error */
2006-08-20 15:49:48 +00:00
XP_AWK_EDIVBYZERO, /* divide by zero */
XP_AWK_EOPERAND, /* invalid operand */
XP_AWK_EPOSIDX, /* wrong position index */
XP_AWK_ENOSUCHFUNC, /* no such function */
XP_AWK_ENOTASSIGNABLE, /* value not assignable */
XP_AWK_ENOTINDEXABLE, /* not indexable variable */
XP_AWK_ENOTDELETABLE, /* not deletable variable */
XP_AWK_ENOTREFERENCEABLE, /* not referenceable value */
XP_AWK_EIDXVALASSMAP, /* indexed value cannot be assigned a map */
2006-10-10 07:02:38 +00:00
XP_AWK_EPOSVALASSMAP, /* a positional cannot be assigned a map */
2006-08-20 15:49:48 +00:00
XP_AWK_EMAPTOSCALAR, /* cannot change a map to a scalar value */
XP_AWK_ESCALARTOMAP, /* cannot change a scalar value to a map */
2006-09-14 06:40:06 +00:00
XP_AWK_EMAPNOTALLOWED, /* a map is not allowed */
2006-08-20 15:49:48 +00:00
XP_AWK_EVALTYPE, /* wrong value type */
XP_AWK_EPIPE, /* pipe operation error */
XP_AWK_ENEXTCALL, /* next called from BEGIN or END */
XP_AWK_ENEXTFILECALL, /* nextfile called from BEGIN or END */
XP_AWK_EIOIMPL, /* wrong implementation of user io handler */
2006-08-23 15:42:16 +00:00
XP_AWK_ENOSUCHIO, /* no such io name found */
XP_AWK_EIOHANDLER, /* io handler has returned an error */
2006-08-20 15:49:48 +00:00
XP_AWK_EINTERNAL, /* internal error */
2006-08-10 16:02:15 +00:00
/* regular expression error */
XP_AWK_EREXRPAREN, /* a right parenthesis is expected */
XP_AWK_EREXRBRACKET, /* a right bracket is expected */
XP_AWK_EREXRBRACE, /* a right brace is expected */
XP_AWK_EREXCOLON, /* a colon is expected */
XP_AWK_EREXCRANGE, /* invalid character range */
XP_AWK_EREXCCLASS, /* invalid character class */
XP_AWK_EREXBRANGE, /* invalid boundary range */
XP_AWK_EREXEND, /* unexpected end of the pattern */
XP_AWK_EREXGARBAGE /* garbage after the pattern */
2006-04-02 12:45:04 +00:00
};
2006-06-19 09:10:57 +00:00
/* extio types */
enum
{
/* extio types available */
XP_AWK_EXTIO_PIPE,
XP_AWK_EXTIO_COPROC,
XP_AWK_EXTIO_FILE,
2006-06-25 15:26:57 +00:00
XP_AWK_EXTIO_CONSOLE,
2006-06-19 09:10:57 +00:00
/* reserved for internal use only */
XP_AWK_EXTIO_NUM
};
2005-11-05 17:54:00 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2006-08-31 04:21:04 +00:00
xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas);
2005-11-05 17:54:00 +00:00
int xp_awk_close (xp_awk_t* awk);
2006-08-10 16:02:15 +00:00
int xp_awk_clear (xp_awk_t* awk);
2005-11-05 17:54:00 +00:00
2006-01-31 16:57:45 +00:00
int xp_awk_geterrnum (xp_awk_t* awk);
2006-07-30 15:53:42 +00:00
xp_size_t xp_awk_getsrcline (xp_awk_t* awk);
2006-08-13 16:05:04 +00:00
int xp_awk_getopt (xp_awk_t* awk);
2006-08-10 16:02:15 +00:00
void xp_awk_setopt (xp_awk_t* awk, int opt);
2006-08-04 17:36:40 +00:00
2006-08-04 17:54:52 +00:00
int xp_awk_parse (xp_awk_t* awk, xp_awk_srcios_t* srcios);
2006-08-04 17:36:40 +00:00
2006-08-13 05:55:02 +00:00
/*
* xp_awk_run return 0 on success and -1 on failure, generally speaking.
* A runtime context is required for it to start running the program.
* Once the runtime context is created, the program starts to run.
* The context creation failure is reported by the return value -1 of
* this function. however, the runtime error after the context creation
* is reported differently depending on the use of the callback.
* When no callback is specified (i.e. runcbs is XP_NULL), xp_awk_run
* returns -1 on an error and awk->errnum is set accordingly.
* However, if a callback is specified (i.e. runcbs is not XP_NULL),
* xp_awk_run returns 0 on both success and failure. Instead, the
* on_end handler of the callback is triggered with the relevant
* error number. The third parameter to on_end denotes this error number.
*/
2006-08-04 17:36:40 +00:00
int xp_awk_run (xp_awk_t* awk,
2006-10-10 14:09:23 +00:00
xp_awk_runios_t* runios,
xp_awk_runcbs_t* runcbs,
xp_awk_runarg_t* runarg);
2006-08-04 17:36:40 +00:00
2006-10-03 14:38:26 +00:00
int xp_awk_stop (xp_awk_t* awk, xp_awk_run_t* run);
2006-08-13 05:55:02 +00:00
void xp_awk_stopall (xp_awk_t* awk);
2006-01-09 16:03:56 +00:00
2006-06-20 15:27:50 +00:00
/* functions to access internal stack structure */
2006-10-03 14:38:26 +00:00
xp_size_t xp_awk_getnargs (xp_awk_run_t* run);
xp_awk_val_t* xp_awk_getarg (xp_awk_run_t* run, xp_size_t idx);
xp_awk_val_t* xp_awk_getglobal (xp_awk_run_t* run, xp_size_t idx);
int xp_awk_setglobal (xp_awk_run_t* run, xp_size_t idx, xp_awk_val_t* val);
void xp_awk_setretval (xp_awk_run_t* run, xp_awk_val_t* val);
2006-10-10 07:02:38 +00:00
int xp_awk_getrunerrnum (xp_awk_run_t* run);
void xp_awk_setrunerrnum (xp_awk_run_t* run, int errnum);
2006-10-03 14:38:26 +00:00
/* record and field functions */
int xp_awk_clrrec (xp_awk_run_t* run, xp_bool_t skip_inrec_line);
int xp_awk_setrec (xp_awk_run_t* run, xp_size_t idx, const xp_char_t* str, xp_size_t len);
2006-06-20 15:27:50 +00:00
2006-04-04 16:03:14 +00:00
/* utility functions exported by awk.h */
2006-10-05 14:20:57 +00:00
xp_long_t xp_awk_strxtolong (
xp_awk_t* awk, const xp_char_t* str, xp_size_t len,
2006-09-01 06:23:58 +00:00
int base, const xp_char_t** endptr);
2006-10-06 03:37:40 +00:00
xp_real_t xp_awk_strxtoreal (
xp_awk_t* awk, const xp_char_t* str, xp_size_t len,
2006-10-06 03:41:54 +00:00
const xp_char_t** endptr);
2006-04-04 16:03:14 +00:00
2006-09-01 16:31:13 +00:00
xp_size_t xp_awk_longtostr (
xp_long_t value, int radix, const xp_char_t* prefix,
xp_char_t* buf, xp_size_t size);
2006-09-01 03:44:51 +00:00
/* string functions exported by awk.h */
xp_char_t* xp_awk_strdup (
xp_awk_t* awk, const xp_char_t* str);
xp_char_t* xp_awk_strxdup (
xp_awk_t* awk, const xp_char_t* str, xp_size_t len);
xp_char_t* xp_awk_strxdup2 (
xp_awk_t* awk,
const xp_char_t* str1, xp_size_t len1,
const xp_char_t* str2, xp_size_t len2);
2006-09-01 06:23:58 +00:00
xp_size_t xp_awk_strlen (const xp_char_t* str);
xp_size_t xp_awk_strcpy (xp_char_t* buf, const xp_char_t* str);
xp_size_t xp_awk_strncpy (xp_char_t* buf, const xp_char_t* str, xp_size_t len);
int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2);
int xp_awk_strxncmp (
const xp_char_t* s1, xp_size_t len1,
const xp_char_t* s2, xp_size_t len2);
2006-09-10 15:50:34 +00:00
int xp_awk_strxncasecmp (
xp_awk_t* awk,
const xp_char_t* s1, xp_size_t len1,
const xp_char_t* s2, xp_size_t len2);
2006-09-01 06:23:58 +00:00
xp_char_t* xp_awk_strxnstr (
const xp_char_t* str, xp_size_t strsz,
const xp_char_t* sub, xp_size_t subsz);
2006-08-10 16:02:15 +00:00
/* utility functions to convert an error number ot a string */
const xp_char_t* xp_awk_geterrstr (int errnum);
2005-11-05 17:54:00 +00:00
#ifdef __cplusplus
}
#endif
#endif