vi/v_txt.c: txt_dent: introduce sequence point between decrement and access
[nvi.git] / common / cut.c
blob3603bb9917463eeac3531630cc6ceb1e6575c5b0
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
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>
28 #include "common.h"
30 static void cb_rotate __P((SCR *));
33 * cut --
34 * Put a range of lines/columns into a TEXT buffer.
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.
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.
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.
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.
64 * PUBLIC: int cut __P((SCR *, CHAR_T *, MARK *, MARK *, int));
66 int
67 cut(SCR *sp, CHAR_T *namep, MARK *fm, MARK *tm, int flags)
69 CB *cbp;
70 CHAR_T name;
71 db_recno_t lno;
72 int append, copy_one, copy_def;
75 * If the user specified a buffer, put it there. (This may require
76 * a copy into the numeric buffers. We do the copy so that we don't
77 * have to reference count and so we don't have to deal with things
78 * like appends to buffers that are used multiple times.)
80 * Otherwise, if it's supposed to be put in a numeric buffer (usually
81 * a delete) put it there. The rules for putting things in numeric
82 * buffers were historically a little strange. There were three cases.
84 * 1: Some motions are always line mode motions, which means
85 * that the cut always goes into the numeric buffers.
86 * 2: Some motions aren't line mode motions, e.g. d10w, but
87 * can cross line boundaries. For these commands, if the
88 * cut crosses a line boundary, it goes into the numeric
89 * buffers. This includes most of the commands.
90 * 3: Some motions aren't line mode motions, e.g. d`<char>,
91 * but always go into the numeric buffers, regardless. This
92 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
94 * Otherwise, put it in the unnamed buffer.
96 append = copy_one = copy_def = 0;
97 if (namep != NULL) {
98 name = *namep;
99 if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
100 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
101 copy_one = 1;
102 cb_rotate(sp);
104 if ((append = isupper(name))) {
105 if (!copy_one)
106 copy_def = 1;
107 name = TOLOWER(name);
109 namecb: CBNAME(sp, cbp, name);
110 } else if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
111 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
112 name = L('1');
113 cb_rotate(sp);
114 goto namecb;
115 } else
116 cbp = &sp->wp->dcb_store;
118 copyloop:
120 * If this is a new buffer, create it and add it into the list.
121 * Otherwise, if it's not an append, free its current contents.
123 if (cbp == NULL) {
124 CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
125 cbp->name = name;
126 CIRCLEQ_INIT(&cbp->textq);
127 LIST_INSERT_HEAD(&sp->wp->cutq, cbp, q);
128 } else if (!append) {
129 text_lfree(&cbp->textq);
130 cbp->len = 0;
131 cbp->flags = 0;
135 #define ENTIRE_LINE 0
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, 0, cbp))
141 goto cut_line_err;
142 } else {
144 * Get the first line. A length of 0 causes cut_line
145 * to cut from the MARK to the end of the line.
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;
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;
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;
162 append = 0; /* Only append to the named buffer. */
163 sp->wp->dcbp = cbp; /* Repoint the default buffer on each pass. */
165 if (copy_one) { /* Copy into numeric buffer 1. */
166 name = L('1');
167 CBNAME(sp, cbp, name);
168 copy_one = 0;
169 goto copyloop;
171 if (copy_def) { /* Copy into the default buffer. */
172 cbp = &sp->wp->dcb_store;
173 copy_def = 0;
174 goto copyloop;
176 return (0);
178 cut_line_err:
179 text_lfree(&cbp->textq);
180 cbp->len = 0;
181 cbp->flags = 0;
182 return (1);
186 * cb_rotate --
187 * Rotate the numbered buffers up one.
189 static void
190 cb_rotate(SCR *sp)
192 CB *cbp, *del_cbp;
194 del_cbp = NULL;
195 for (cbp = sp->wp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
196 switch(cbp->name) {
197 case L('1'):
198 cbp->name = L('2');
199 break;
200 case L('2'):
201 cbp->name = L('3');
202 break;
203 case L('3'):
204 cbp->name = L('4');
205 break;
206 case L('4'):
207 cbp->name = L('5');
208 break;
209 case L('5'):
210 cbp->name = L('6');
211 break;
212 case L('6'):
213 cbp->name = L('7');
214 break;
215 case L('7'):
216 cbp->name = L('8');
217 break;
218 case L('8'):
219 cbp->name = L('9');
220 break;
221 case L('9'):
222 del_cbp = cbp;
223 break;
225 if (del_cbp != NULL) {
226 LIST_REMOVE(del_cbp, q);
227 text_lfree(&del_cbp->textq);
228 free(del_cbp);
233 * cut_line --
234 * Cut a portion of a single line.
236 * PUBLIC: int cut_line __P((SCR *, db_recno_t, size_t, size_t, CB *));
239 cut_line(SCR *sp, db_recno_t lno, size_t fcno, size_t clen, CB *cbp)
241 TEXT *tp;
242 size_t len;
243 CHAR_T *p;
245 /* Get the line. */
246 if (db_get(sp, lno, DBG_FATAL, &p, &len))
247 return (1);
249 /* Create a TEXT structure that can hold the entire line. */
250 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
251 return (1);
254 * If the line isn't empty and it's not the entire line,
255 * copy the portion we want, and reset the TEXT length.
257 if (len != 0) {
258 if (clen == 0)
259 clen = len - fcno;
260 MEMCPYW(tp->lb, p + fcno, clen);
261 tp->len = clen;
264 /* Append to the end of the cut buffer. */
265 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
266 cbp->len += tp->len;
268 return (0);
272 * cut_close --
273 * Discard all cut buffers.
275 * PUBLIC: void cut_close __P((WIN *));
277 void
278 cut_close(WIN *wp)
280 CB *cbp;
282 /* Free cut buffer list. */
283 while ((cbp = wp->cutq.lh_first) != NULL) {
284 if (cbp->textq.cqh_first != (void *)&cbp->textq)
285 text_lfree(&cbp->textq);
286 LIST_REMOVE(cbp, q);
287 free(cbp);
290 /* Free default cut storage. */
291 cbp = &wp->dcb_store;
292 if (cbp->textq.cqh_first != (void *)&cbp->textq)
293 text_lfree(&cbp->textq);
297 * text_init --
298 * Allocate a new TEXT structure.
300 * PUBLIC: TEXT *text_init __P((SCR *, const CHAR_T *, size_t, size_t));
302 TEXT *
303 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
305 TEXT *tp;
307 CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
308 if (tp == NULL)
309 return (NULL);
310 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
311 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
312 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len * sizeof(CHAR_T));
313 if (tp->lb == NULL) {
314 free(tp);
315 return (NULL);
317 if (p != NULL && len != 0)
318 MEMCPYW(tp->lb, p, len);
320 tp->len = len;
321 return (tp);
325 * text_lfree --
326 * Free a chain of text structures.
328 * PUBLIC: void text_lfree __P((TEXTH *));
330 void
331 text_lfree(TEXTH *headp)
333 TEXT *tp;
335 while ((tp = headp->cqh_first) != (void *)headp) {
336 CIRCLEQ_REMOVE(headp, tp, q);
337 text_free(tp);
342 * text_free --
343 * Free a text structure.
345 * PUBLIC: void text_free __P((TEXT *));
347 void
348 text_free(TEXT *tp)
350 if (tp->lb != NULL)
351 free(tp->lb);
352 free(tp);