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