xref: /freebsd/contrib/nvi/common/cut.c (revision 52d19df19ed6455df025f7ac2c6cf5db7df8e5ec)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "common.h"
26 
27 static void	cb_rotate(SCR *);
28 
29 /*
30  * cut --
31  *	Put a range of lines/columns into a TEXT buffer.
32  *
33  * There are two buffer areas, both found in the global structure.  The first
34  * is the linked list of all the buffers the user has named, the second is the
35  * unnamed buffer storage.  There is a pointer, too, which is the current
36  * default buffer, i.e. it may point to the unnamed buffer or a named buffer
37  * depending on into what buffer the last text was cut.  Logically, in both
38  * delete and yank operations, if the user names a buffer, the text is cut
39  * into it.  If it's a delete of information on more than a single line, the
40  * contents of the numbered buffers are rotated up one, the contents of the
41  * buffer named '9' are discarded, and the text is cut into the buffer named
42  * '1'.  The text is always cut into the unnamed buffer.
43  *
44  * In all cases, upper-case buffer names are the same as lower-case names,
45  * with the exception that they cause the buffer to be appended to instead
46  * of replaced.  Note, however, that if text is appended to a buffer, the
47  * default buffer only contains the appended text, not the entire contents
48  * of the buffer.
49  *
50  * !!!
51  * The contents of the default buffer would disappear after most operations
52  * in historic vi.  It's unclear that this is useful, so we don't bother.
53  *
54  * When users explicitly cut text into the numeric buffers, historic vi became
55  * genuinely strange.  I've never been able to figure out what was supposed to
56  * happen.  It behaved differently if you deleted text than if you yanked text,
57  * and, in the latter case, the text was appended to the buffer instead of
58  * replacing the contents.  Hopefully it's not worth getting right, and here
59  * we just treat the numeric buffers like any other named buffer.
60  *
61  * PUBLIC: int cut(SCR *, CHAR_T *, MARK *, MARK *, int);
62  */
63 int
cut(SCR * sp,CHAR_T * namep,MARK * fm,MARK * tm,int flags)64 cut(SCR *sp, CHAR_T *namep, MARK *fm, MARK *tm, int flags)
65 {
66 	CB *cbp;
67 	CHAR_T name = '\0';
68 	recno_t lno;
69 	int append, copy_one, copy_def;
70 
71 	/* Check if the line numbers are out-of-band */
72 	if (fm->lno == OOBLNO || tm->lno == OOBLNO)
73 		return (1);
74 
75 	/*
76 	 * If the user specified a buffer, put it there.  (This may require
77 	 * a copy into the numeric buffers.  We do the copy so that we don't
78 	 * have to reference count and so we don't have to deal with things
79 	 * like appends to buffers that are used multiple times.)
80 	 *
81 	 * Otherwise, if it's supposed to be put in a numeric buffer (usually
82 	 * a delete) put it there.  The rules for putting things in numeric
83 	 * buffers were historically a little strange.  There were three cases.
84 	 *
85 	 *	1: Some motions are always line mode motions, which means
86 	 *	   that the cut always goes into the numeric buffers.
87 	 *	2: Some motions aren't line mode motions, e.g. d10w, but
88 	 *	   can cross line boundaries.  For these commands, if the
89 	 *	   cut crosses a line boundary, it goes into the numeric
90 	 *	   buffers.  This includes most of the commands.
91 	 *	3: Some motions aren't line mode motions, e.g. d`<char>,
92 	 *	   but always go into the numeric buffers, regardless.  This
93 	 *	   was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
94 	 *
95 	 * Otherwise, put it in the unnamed buffer.
96 	 */
97 	append = copy_one = copy_def = 0;
98 	if (namep != NULL) {
99 		name = *namep;
100 		if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
101 		    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
102 			copy_one = 1;
103 			cb_rotate(sp);
104 		}
105 		if ((append = isupper(name))) {
106 			if (!copy_one)
107 				copy_def = 1;
108 			name = tolower(name);
109 		}
110 namecb:		CBNAME(sp, cbp, name);
111 	} else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
112 	    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
113 		name = '1';
114 		cb_rotate(sp);
115 		goto namecb;
116 	} else
117 		cbp = &sp->gp->dcb_store;
118 
119 copyloop:
120 	/*
121 	 * If this is a new buffer, create it and add it into the list.
122 	 * Otherwise, if it's not an append, free its current contents.
123 	 */
124 	if (cbp == NULL) {
125 		CALLOC_RET(sp, cbp, 1, sizeof(CB));
126 		cbp->name = name;
127 		TAILQ_INIT(cbp->textq);
128 		SLIST_INSERT_HEAD(sp->gp->cutq, cbp, q);
129 	} else if (!append) {
130 		text_lfree(cbp->textq);
131 		cbp->len = 0;
132 		cbp->flags = 0;
133 	}
134 
135 
136 	/* In line mode, it's pretty easy, just cut the lines. */
137 	if (LF_ISSET(CUT_LINEMODE)) {
138 		cbp->flags |= CB_LMODE;
139 		for (lno = fm->lno; lno <= tm->lno; ++lno)
140 			if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
141 				goto cut_line_err;
142 	} else {
143 		/*
144 		 * Get the first line.  A length of ENTIRE_LINE causes
145 		 * cut_line to cut from the MARK to the end of the line.
146 		 */
147 		if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
148 		    ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
149 			goto cut_line_err;
150 
151 		/* Get the intermediate lines. */
152 		for (lno = fm->lno; ++lno < tm->lno;)
153 			if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
154 				goto cut_line_err;
155 
156 		/* Get the last line. */
157 		if (tm->lno != fm->lno &&
158 		    cut_line(sp, lno, 0, tm->cno + 1, cbp))
159 			goto cut_line_err;
160 	}
161 
162 	append = 0;		/* Only append to the named buffer. */
163 	sp->gp->dcbp = cbp;	/* Repoint the default buffer on each pass. */
164 
165 	if (copy_one) {		/* Copy into numeric buffer 1. */
166 		name = '1';
167 		CBNAME(sp, cbp, name);
168 		copy_one = 0;
169 		goto copyloop;
170 	}
171 	if (copy_def) {		/* Copy into the default buffer. */
172 		cbp = &sp->gp->dcb_store;
173 		copy_def = 0;
174 		goto copyloop;
175 	}
176 	return (0);
177 
178 cut_line_err:
179 	text_lfree(cbp->textq);
180 	cbp->len = 0;
181 	cbp->flags = 0;
182 	sp->gp->dcbp = NULL;
183 	return (1);
184 }
185 
186 /*
187  * cb_rotate --
188  *	Rotate the numbered buffers up one.
189  */
190 static void
cb_rotate(SCR * sp)191 cb_rotate(SCR *sp)
192 {
193 	CB *cbp, *del_cbp = NULL, *pre_cbp = NULL;
194 
195 	SLIST_FOREACH(cbp, sp->gp->cutq, q) {
196 		switch(cbp->name) {
197 		case '1': case '2': case '3':
198 		case '4': case '5': case '6':
199 		case '7': case '8':
200 			cbp->name += 1;
201 			break;
202 		case '9':
203 			if (cbp == SLIST_FIRST(sp->gp->cutq))
204 				SLIST_REMOVE_HEAD(sp->gp->cutq, q);
205 			else
206 				SLIST_REMOVE_AFTER(pre_cbp, q);
207 			del_cbp = cbp;
208 			break;
209 		}
210 		pre_cbp = cbp;
211 	}
212 	if (del_cbp != NULL) {
213 		text_lfree(del_cbp->textq);
214 		free(del_cbp);
215 	}
216 }
217 
218 /*
219  * cut_line --
220  *	Cut a portion of a single line.
221  *
222  * PUBLIC: int cut_line(SCR *, recno_t, size_t, size_t, CB *);
223  */
224 int
cut_line(SCR * sp,recno_t lno,size_t fcno,size_t clen,CB * cbp)225 cut_line(SCR *sp, recno_t lno, size_t fcno, size_t clen, CB *cbp)
226 {
227 	TEXT *tp;
228 	size_t len;
229 	CHAR_T *p;
230 
231 	/* Get the line. */
232 	if (db_get(sp, lno, DBG_FATAL, &p, &len))
233 		return (1);
234 
235 	/* Create a TEXT structure that can hold the entire line. */
236 	if ((tp = text_init(sp, NULL, 0, len)) == NULL)
237 		return (1);
238 
239 	/*
240 	 * If the line isn't empty and it's not the entire line,
241 	 * copy the portion we want, and reset the TEXT length.
242 	 */
243 	if (len != 0) {
244 		if (clen == ENTIRE_LINE)
245 			clen = len - fcno;
246 		MEMCPY(tp->lb, p + fcno, clen);
247 		tp->len = clen;
248 	}
249 
250 	/* Append to the end of the cut buffer. */
251 	TAILQ_INSERT_TAIL(cbp->textq, tp, q);
252 	cbp->len += tp->len;
253 
254 	return (0);
255 }
256 
257 /*
258  * cut_close --
259  *	Discard all cut buffers.
260  *
261  * PUBLIC: void cut_close(GS *);
262  */
263 void
cut_close(GS * gp)264 cut_close(GS *gp)
265 {
266 	CB *cbp;
267 
268 	/* Free cut buffer list. */
269 	while ((cbp = SLIST_FIRST(gp->cutq)) != NULL) {
270 		if (!TAILQ_EMPTY(cbp->textq))
271 			text_lfree(cbp->textq);
272 		SLIST_REMOVE_HEAD(gp->cutq, q);
273 		free(cbp);
274 	}
275 
276 	/* Free default cut storage. */
277 	cbp = &gp->dcb_store;
278 	if (!TAILQ_EMPTY(cbp->textq))
279 		text_lfree(cbp->textq);
280 }
281 
282 /*
283  * text_init --
284  *	Allocate a new TEXT structure.
285  *
286  * PUBLIC: TEXT *text_init(SCR *, const CHAR_T *, size_t, size_t);
287  */
288 TEXT *
text_init(SCR * sp,const CHAR_T * p,size_t len,size_t total_len)289 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
290 {
291 	TEXT *tp;
292 
293 	CALLOC(sp, tp, 1, sizeof(TEXT));
294 	if (tp == NULL)
295 		return (NULL);
296 	/* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
297 	if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
298 		MALLOC(sp, tp->lb, tp->lb_len);
299 		if (tp->lb == NULL) {
300 			free(tp);
301 			return (NULL);
302 		}
303 		if (p != NULL && len != 0)
304 			MEMCPY(tp->lb, p, len);
305 	}
306 	tp->len = len;
307 	return (tp);
308 }
309 
310 /*
311  * text_lfree --
312  *	Free a chain of text structures.
313  *
314  * PUBLIC: void text_lfree(TEXTH *);
315  */
316 void
text_lfree(TEXTH * headp)317 text_lfree(TEXTH *headp)
318 {
319 	TEXT *tp;
320 
321 	while ((tp = TAILQ_FIRST(headp)) != NULL) {
322 		TAILQ_REMOVE(headp, tp, q);
323 		text_free(tp);
324 	}
325 }
326 
327 /*
328  * text_free --
329  *	Free a text structure.
330  *
331  * PUBLIC: void text_free(TEXT *);
332  */
333 void
text_free(TEXT * tp)334 text_free(TEXT *tp)
335 {
336 	free(tp->lb);
337 	free(tp);
338 }
339