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