xref: /minix/external/bsd/nvi/dist/vi/v_increment.c (revision 00b67f09)
1 /*	$NetBSD: v_increment.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 static const char sccsid[] = "Id: v_increment.c,v 10.16 2001/06/25 15:19:31 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:31 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: v_increment.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
21 
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 #include <sys/time.h>
25 
26 #include <bitstring.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "../common/common.h"
35 #include "vi.h"
36 
37 static const CHAR_T *fmt[] = {
38 #define	DEC	0
39 	L("%ld"),
40 #define	SDEC	1
41 	L("%+ld"),
42 #define	HEXC	2
43 	L("0X%0*lX"),
44 #define	HEXL	3
45 	L("0x%0*lx"),
46 #define	OCTAL	4
47 	L("%#0*lo"),
48 };
49 
50 static void inc_err __P((SCR *, enum nresult));
51 
52 /*
53  * v_increment -- [count]#[#+-]
54  *	Increment/decrement a keyword number.
55  *
56  * PUBLIC: int v_increment __P((SCR *, VICMD *));
57  */
58 int
59 v_increment(SCR *sp, VICMD *vp)
60 {
61 	enum nresult nret;
62 	u_long ulval, change;
63 	long ltmp, lval;
64 	size_t beg, blen, end, len, nlen, wlen;
65 	int base, isempty, rval;
66 	CHAR_T *bp, *p, *t, nbuf[100];
67 	const CHAR_T *ntype;
68 
69 	/* Validate the operator. */
70 	if (vp->character == L('#'))
71 		vp->character = L('+');
72 	if (vp->character != L('+') && vp->character != L('-')) {
73 		v_emsg(sp, vp->kp->usage, VIM_USAGE);
74 		return (1);
75 	}
76 
77 	/* If new value set, save it off, but it has to fit in a long. */
78 	if (F_ISSET(vp, VC_C1SET)) {
79 		if (vp->count > LONG_MAX) {
80 			inc_err(sp, NUM_OVER);
81 			return (1);
82 		}
83 		change = vp->count;
84 	} else
85 		change = 1;
86 
87 	/* Get the line. */
88 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
89 		if (isempty)
90 			goto nonum;
91 		return (1);
92 	}
93 
94 	/*
95 	 * Skip any leading space before the number.  Getting a cursor word
96 	 * implies moving the cursor to its beginning, if we moved, refresh
97 	 * now.
98 	 */
99 	for (beg = vp->m_start.cno; beg < len && ISSPACE((UCHAR_T)p[beg]); ++beg);
100 	if (beg >= len)
101 		goto nonum;
102 	if (beg != vp->m_start.cno) {
103 		sp->cno = beg;
104 		(void)vs_refresh(sp, 0);
105 	}
106 
107 #undef	isoctal
108 #define	isoctal(c)	(ISDIGIT(c) && (c) != L('8') && (c) != L('9'))
109 
110 	/*
111 	 * Look for 0[Xx], or leading + or - signs, guess at the base.
112 	 * The character after that must be a number.  Wlen is set to
113 	 * the remaining characters in the line that could be part of
114 	 * the number.
115 	 */
116 	wlen = len - beg;
117 	if (p[beg] == L('0') && wlen > 2 &&
118 	    (p[beg + 1] == L('X') || p[beg + 1] == L('x'))) {
119 		base = 16;
120 		end = beg + 2;
121 		if (!ISXDIGIT((UCHAR_T)p[end]))
122 			goto decimal;
123 		ntype = p[beg + 1] == L('X') ? fmt[HEXC] : fmt[HEXL];
124 	} else if (p[beg] == L('0') && wlen > 1) {
125 		base = 8;
126 		end = beg + 1;
127 		if (!isoctal((UCHAR_T)p[end]))
128 			goto decimal;
129 		ntype = fmt[OCTAL];
130 	} else if (wlen >= 1 && (p[beg] == L('+') || p[beg] == L('-'))) {
131 		base = 10;
132 		end = beg + 1;
133 		ntype = fmt[SDEC];
134 		if (!ISDIGIT((UCHAR_T)p[end]))
135 			goto nonum;
136 	} else {
137 decimal:	base = 10;
138 		end = beg;
139 		ntype = fmt[DEC];
140 		if (!ISDIGIT(p[end])) {
141 nonum:			msgq(sp, M_ERR, "181|Cursor not in a number");
142 			return (1);
143 		}
144 	}
145 
146 	/* Find the end of the word, possibly correcting the base. */
147 	while (++end < len) {
148 		switch (base) {
149 		case 8:
150 			if (isoctal((UCHAR_T)p[end]))
151 				continue;
152 			if (p[end] == L('8') || p[end] == L('9')) {
153 				base = 10;
154 				ntype = fmt[DEC];
155 				continue;
156 			}
157 			break;
158 		case 10:
159 			if (ISDIGIT((UCHAR_T)p[end]))
160 				continue;
161 			break;
162 		case 16:
163 			if (ISXDIGIT((UCHAR_T)p[end]))
164 				continue;
165 			break;
166 		default:
167 			abort();
168 			/* NOTREACHED */
169 		}
170 		break;
171 	}
172 	wlen = (end - beg);
173 
174 	/*
175 	 * XXX
176 	 * If the line was at the end of the buffer, we have to copy it
177 	 * so we can guarantee that it's NULL-terminated.  We make the
178 	 * buffer big enough to fit the line changes as well, and only
179 	 * allocate once.
180 	 */
181 	GET_SPACE_RETW(sp, bp, blen, len + 50);
182 	if (end == len) {
183 		MEMMOVEW(bp, &p[beg], wlen);
184 		bp[wlen] = L('\0');
185 		t = bp;
186 	} else
187 		t = &p[beg];
188 
189 	/*
190 	 * Octal or hex deal in unsigned longs, everything else is done
191 	 * in signed longs.
192 	 */
193 	if (base == 10) {
194 		if ((nret = nget_slong(sp, &lval, t, NULL, 10)) != NUM_OK)
195 			goto err;
196 		ltmp = vp->character == L('-') ? -change : change;
197 		if (lval > 0 && ltmp > 0 &&
198 		    !NPFITS(LONG_MAX, (unsigned long)lval, (unsigned long)ltmp)) {
199 			nret = NUM_OVER;
200 			goto err;
201 		}
202 		if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
203 			nret = NUM_UNDER;
204 			goto err;
205 		}
206 		lval += ltmp;
207 		/* If we cross 0, signed numbers lose their sign. */
208 		if (lval == 0 && ntype == fmt[SDEC])
209 			ntype = fmt[DEC];
210 		nlen = SPRINTF(nbuf, SIZE(nbuf), ntype, lval);
211 	} else {
212 		if ((nret = nget_uslong(sp, &ulval, t, NULL, base)) != NUM_OK)
213 			goto err;
214 		if (vp->character == L('+')) {
215 			if (!NPFITS(ULONG_MAX, ulval, change)) {
216 				nret = NUM_OVER;
217 				goto err;
218 			}
219 			ulval += change;
220 		} else {
221 			if (ulval < change) {
222 				nret = NUM_UNDER;
223 				goto err;
224 			}
225 			ulval -= change;
226 		}
227 
228 		/* Correct for literal "0[Xx]" in format. */
229 		if (base == 16)
230 			wlen -= 2;
231 
232 		nlen = SPRINTF(nbuf, SIZE(nbuf), ntype, wlen, ulval);
233 	}
234 
235 	/* Build the new line. */
236 	MEMMOVEW(bp, p, beg);
237 	MEMMOVEW(bp + beg, nbuf, nlen);
238 	MEMMOVEW(bp + beg + nlen, p + end, len - beg - (end - beg));
239 	len = beg + nlen + (len - beg - (end - beg));
240 
241 	nret = NUM_OK;
242 	rval = db_set(sp, vp->m_start.lno, bp, len);
243 
244 	if (0) {
245 err:		rval = 1;
246 		inc_err(sp, nret);
247 	}
248 	if (bp != NULL)
249 		FREE_SPACEW(sp, bp, blen);
250 	return (rval);
251 }
252 
253 static void
254 inc_err(SCR *sp, enum nresult nret)
255 {
256 	switch (nret) {
257 	case NUM_ERR:
258 		break;
259 	case NUM_OK:
260 		abort();
261 		/* NOREACHED */
262 	case NUM_OVER:
263 		msgq(sp, M_ERR, "182|Resulting number too large");
264 		break;
265 	case NUM_UNDER:
266 		msgq(sp, M_ERR, "183|Resulting number too small");
267 		break;
268 	}
269 }
270