xref: /openbsd/usr.bin/sed/defs.h (revision d9a51c35)
1 /*	$OpenBSD: defs.h,v 1.10 2022/12/26 19:16:02 jmc Exp $ */
2 /*-
3  * Copyright (c) 1992 Diomidis Spinellis.
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Diomidis Spinellis of Imperial College, University of London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)defs.h	8.1 (Berkeley) 6/6/93
35  */
36 
37 /*
38  * Types of address specifications
39  */
40 enum e_atype {
41 	AT_RE,					/* Line that match RE */
42 	AT_LINE,				/* Specific line */
43 	AT_LAST,				/* Last line */
44 };
45 
46 /*
47  * Format of an address
48  */
49 struct s_addr {
50 	enum e_atype type;			/* Address type */
51 	union {
52 		u_long l;			/* Line number */
53 		regex_t *r;			/* Regular expression */
54 	} u;
55 };
56 
57 /*
58  * Substitution command
59  */
60 struct s_subst {
61 	int n;					/* Occurrence to subst. */
62 	int p;					/* True if p flag */
63 	char *wfile;				/* NULL if no wfile */
64 	int wfd;				/* Cached file descriptor */
65 	regex_t *re;				/* Regular expression */
66 	u_int maxbref;				/* Largest backreference. */
67 	u_long linenum;				/* Line number. */
68 	char *new;				/* Replacement text */
69 };
70 
71 
72 /*
73  * An internally compiled command.
74  * Initially, label references are stored in t, on a second pass they
75  * are updated to pointers.
76  */
77 struct s_command {
78 	struct s_command *next;			/* Pointer to next command */
79 	struct s_addr *a1, *a2;			/* Start and end address */
80 	char *t;				/* Text for : a c i r w */
81 	union {
82 		struct s_command *c;		/* Command(s) for b t { */
83 		struct s_subst *s;		/* Substitute command */
84 		u_char *y;			/* Replace command array */
85 		int fd;				/* File descriptor for w */
86 	} u;
87 	char code;				/* Command code */
88 	u_int nonsel:1;				/* True if ! */
89 	u_int inrange:1;			/* True if in range */
90 };
91 
92 /*
93  * Types of command arguments recognised by the parser
94  */
95 enum e_args {
96 	EMPTY,			/* d D g G h H l n N p P q x = \0 */
97 	TEXT,			/* a c i */
98 	NONSEL,			/* ! */
99 	GROUP,			/* { */
100 	ENDGROUP,		/* } */
101 	COMMENT,		/* # */
102 	BRANCH,			/* b t */
103 	LABEL,			/* : */
104 	RFILE,			/* r */
105 	WFILE,			/* w */
106 	SUBST,			/* s */
107 	TR			/* y */
108 };
109 
110 /*
111  * Structure containing things to append before a line is read
112  */
113 struct s_appends {
114 	enum {AP_STRING, AP_FILE} type;
115 	char *s;
116 	size_t len;
117 };
118 
119 enum e_spflag {
120 	APPEND,					/* Append to the contents. */
121 	REPLACE,				/* Replace the contents. */
122 };
123 
124 /*
125  * Structure for a space (process, hold, otherwise).
126  */
127 typedef struct {
128 	char *space;		/* Current space pointer. */
129 	size_t len;		/* Current length. */
130 	int deleted;		/* If deleted. */
131 	int append_newline;	/* If originally terminated by \n. */
132 	char *back;		/* Backing memory. */
133 	size_t blen;		/* Backing memory length. */
134 } SPACE;
135 
136 /*
137  * Error severity codes:
138  */
139 #define	FATAL		1	/* Exit immediately with 1 */
140 #define	COMPILE		2	/* Print error, count and finish script */
141 
142 /*
143  * Round up to the nearest multiple of _POSIX2_LINE_MAX
144  */
145 #define ROUNDLEN(x) \
146     (((x) + _POSIX2_LINE_MAX - 1) & ~(_POSIX2_LINE_MAX - 1))
147