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