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