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