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