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