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