2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// types.h - Datatypes used by BP
|
|
|
|
//
|
2020-09-11 01:28:06 -07:00
|
|
|
#ifndef TYPES__H
|
|
|
|
#define TYPES__H
|
2020-09-07 23:22:43 -07:00
|
|
|
|
2020-09-11 01:28:06 -07:00
|
|
|
#include <sys/types.h>
|
2020-09-07 23:22:43 -07:00
|
|
|
|
2020-09-16 19:35:43 -07:00
|
|
|
#include "file_loader.h"
|
|
|
|
|
2020-12-30 19:42:47 -08:00
|
|
|
enum BPFlag {
|
|
|
|
BP_VERBOSE = 1 << 0,
|
|
|
|
BP_IGNORECASE = 1 << 1,
|
|
|
|
BP_EXPLAIN = 1 << 2,
|
|
|
|
BP_JSON = 1 << 3,
|
|
|
|
BP_LISTFILES = 1 << 4,
|
|
|
|
BP_INPLACE = 1 << 5,
|
2020-09-14 12:16:15 -07:00
|
|
|
};
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// BP virtual machine opcodes (these must be kept in sync with the names in vm.c)
|
|
|
|
//
|
2020-09-07 23:34:41 -07:00
|
|
|
enum VMOpcode {
|
|
|
|
VM_ANYCHAR = 1,
|
|
|
|
VM_STRING,
|
|
|
|
VM_RANGE,
|
|
|
|
VM_NOT,
|
2020-09-16 17:57:56 -07:00
|
|
|
VM_UPTO_AND,
|
2020-09-07 23:34:41 -07:00
|
|
|
VM_REPEAT,
|
|
|
|
VM_BEFORE,
|
|
|
|
VM_AFTER,
|
|
|
|
VM_CAPTURE,
|
2020-12-12 16:31:53 -08:00
|
|
|
VM_HIDE,
|
2020-09-07 23:34:41 -07:00
|
|
|
VM_OTHERWISE,
|
|
|
|
VM_CHAIN,
|
2020-09-13 20:33:11 -07:00
|
|
|
VM_EQUAL,
|
2020-09-28 17:56:02 -07:00
|
|
|
VM_NOT_EQUAL,
|
2020-09-07 23:34:41 -07:00
|
|
|
VM_REPLACE,
|
|
|
|
VM_REF,
|
2020-09-12 18:20:13 -07:00
|
|
|
VM_BACKREF,
|
2020-09-14 01:21:49 -07:00
|
|
|
VM_NODENT,
|
2020-09-07 23:34:41 -07:00
|
|
|
};
|
|
|
|
|
2021-01-09 23:43:59 -08:00
|
|
|
struct match_s; // forward declared to resolve circular struct defs
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// A struct reperesenting a BP virtual machine operation
|
|
|
|
//
|
2020-09-07 23:34:41 -07:00
|
|
|
typedef struct vm_op_s {
|
2021-01-12 19:27:57 -08:00
|
|
|
enum VMOpcode type;
|
2020-09-07 23:34:41 -07:00
|
|
|
const char *start, *end;
|
|
|
|
// Length of the match, if constant, otherwise -1
|
|
|
|
ssize_t len;
|
|
|
|
union {
|
|
|
|
const char *s;
|
|
|
|
struct {
|
2020-09-28 21:30:43 -07:00
|
|
|
unsigned char low, high;
|
2020-09-07 23:34:41 -07:00
|
|
|
} range;
|
|
|
|
struct {
|
|
|
|
ssize_t min, max;
|
|
|
|
struct vm_op_s *sep, *repeat_pat;
|
|
|
|
} repetitions;
|
2020-09-09 22:29:09 -07:00
|
|
|
// TODO: use a linked list instead of a binary tree
|
2020-09-07 23:34:41 -07:00
|
|
|
struct {
|
|
|
|
struct vm_op_s *first, *second;
|
|
|
|
} multiple;
|
|
|
|
struct {
|
2020-12-17 19:49:56 -08:00
|
|
|
struct vm_op_s *pat;
|
|
|
|
const char *text;
|
|
|
|
size_t len;
|
2020-09-07 23:34:41 -07:00
|
|
|
} replace;
|
|
|
|
struct {
|
|
|
|
struct vm_op_s *capture_pat;
|
|
|
|
char *name;
|
|
|
|
} capture;
|
2021-01-09 23:43:59 -08:00
|
|
|
struct match_s *backref;
|
2020-09-07 23:34:41 -07:00
|
|
|
struct vm_op_s *pat;
|
|
|
|
} args;
|
|
|
|
} vm_op_t;
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// Pattern matching result object
|
|
|
|
//
|
2020-09-10 22:42:47 -07:00
|
|
|
typedef struct match_s {
|
|
|
|
// Where the match starts and ends (end is after the last character)
|
|
|
|
const char *start, *end;
|
|
|
|
struct match_s *child, *nextsibling;
|
|
|
|
vm_op_t *op;
|
|
|
|
} match_t;
|
2020-09-07 23:34:41 -07:00
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// Pattern matching rule definition(s)
|
|
|
|
//
|
2021-01-10 00:12:09 -08:00
|
|
|
typedef struct def_s {
|
2020-09-07 23:22:43 -07:00
|
|
|
const char *name;
|
2020-09-16 19:35:43 -07:00
|
|
|
file_t *file;
|
2020-09-07 23:22:43 -07:00
|
|
|
vm_op_t *op;
|
2021-01-10 00:12:09 -08:00
|
|
|
struct def_s *next;
|
2020-09-07 23:22:43 -07:00
|
|
|
} def_t;
|
|
|
|
|
2021-01-13 01:48:36 -08:00
|
|
|
//
|
|
|
|
// Structure used for tracking allocated ops, which must be freed when the file
|
|
|
|
// is freed.
|
|
|
|
//
|
|
|
|
typedef struct allocated_op_s {
|
|
|
|
struct allocated_op_s *next;
|
|
|
|
vm_op_t op;
|
|
|
|
} allocated_op_t;
|
|
|
|
|
2020-09-11 01:28:06 -07:00
|
|
|
#endif
|
2020-09-11 01:38:44 -07:00
|
|
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|