only clean out gs when last window goes
[nvi.git] / vi / v_increment.c
blobe3ab97941c19f2051ea4ba9ebbc8feeb6c663992
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.13 2000/06/27 17:19:07 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:07 $";
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 * const fmt[] = {
32 #define DEC 0
33 "%ld",
34 #define SDEC 1
35 "%+ld",
36 #define HEXC 2
37 "0X%0*lX",
38 #define HEXL 3
39 "0x%0*lx",
40 #define OCTAL 4
41 "%#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(sp, vp)
54 SCR *sp;
55 VICMD *vp;
57 enum nresult nret;
58 u_long ulval;
59 long change, ltmp, lval;
60 size_t beg, blen, end, len, nlen, wlen;
61 int base, isempty, rval;
62 char *bp, *ntype, *t, nbuf[100];
63 CHAR_T *p;
65 /* Validate the operator. */
66 if (vp->character == '#')
67 vp->character = '+';
68 if (vp->character != '+' && vp->character != '-') {
69 v_emsg(sp, vp->kp->usage, VIM_USAGE);
70 return (1);
73 /* If new value set, save it off, but it has to fit in a long. */
74 if (F_ISSET(vp, VC_C1SET)) {
75 if (vp->count > LONG_MAX) {
76 inc_err(sp, NUM_OVER);
77 return (1);
79 change = vp->count;
80 } else
81 change = 1;
83 /* Get the line. */
84 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
85 if (isempty)
86 goto nonum;
87 return (1);
91 * Skip any leading space before the number. Getting a cursor word
92 * implies moving the cursor to its beginning, if we moved, refresh
93 * now.
95 for (beg = vp->m_start.cno; beg < len && isspace(p[beg]); ++beg);
96 if (beg >= len)
97 goto nonum;
98 if (beg != vp->m_start.cno) {
99 sp->cno = beg;
100 (void)vs_refresh(sp, 0);
103 #undef ishex
104 #define ishex(c) (isdigit(c) || strchr("abcdefABCDEF", c))
105 #undef isoctal
106 #define isoctal(c) (isdigit(c) && (c) != '8' && (c) != '9')
109 * Look for 0[Xx], or leading + or - signs, guess at the base.
110 * The character after that must be a number. Wlen is set to
111 * the remaining characters in the line that could be part of
112 * the number.
114 wlen = len - beg;
115 if (p[beg] == '0' && wlen > 2 &&
116 (p[beg + 1] == 'X' || p[beg + 1] == 'x')) {
117 base = 16;
118 end = beg + 2;
119 if (!ishex(p[end]))
120 goto decimal;
121 ntype = p[beg + 1] == 'X' ? fmt[HEXC] : fmt[HEXL];
122 } else if (p[beg] == '0' && wlen > 1) {
123 base = 8;
124 end = beg + 1;
125 if (!isoctal(p[end]))
126 goto decimal;
127 ntype = fmt[OCTAL];
128 } else if (wlen >= 1 && (p[beg] == '+' || p[beg] == '-')) {
129 base = 10;
130 end = beg + 1;
131 ntype = fmt[SDEC];
132 if (!isdigit(p[end]))
133 goto nonum;
134 } else {
135 decimal: base = 10;
136 end = beg;
137 ntype = fmt[DEC];
138 if (!isdigit(p[end])) {
139 nonum: msgq(sp, M_ERR, "181|Cursor not in a number");
140 return (1);
144 /* Find the end of the word, possibly correcting the base. */
145 while (++end < len) {
146 switch (base) {
147 case 8:
148 if (isoctal(p[end]))
149 continue;
150 if (p[end] == '8' || p[end] == '9') {
151 base = 10;
152 ntype = fmt[DEC];
153 continue;
155 break;
156 case 10:
157 if (isdigit(p[end]))
158 continue;
159 break;
160 case 16:
161 if (ishex(p[end]))
162 continue;
163 break;
164 default:
165 abort();
166 /* NOTREACHED */
168 break;
170 wlen = (end - beg);
173 * XXX
174 * If the line was at the end of the buffer, we have to copy it
175 * so we can guarantee that it's NULL-terminated. We make the
176 * buffer big enough to fit the line changes as well, and only
177 * allocate once.
179 GET_SPACE_RET(sp, bp, blen, len + 50);
180 if (end == len) {
181 memmove(bp, &p[beg], wlen);
182 bp[wlen] = '\0';
183 t = bp;
184 } else
185 t = &p[beg];
188 * Octal or hex deal in unsigned longs, everything else is done
189 * in signed longs.
191 if (base == 10) {
192 if ((nret = nget_slong(&lval, t, NULL, 10)) != NUM_OK)
193 goto err;
194 ltmp = vp->character == '-' ? -change : change;
195 if (lval > 0 && ltmp > 0 && !NPFITS(LONG_MAX, lval, ltmp)) {
196 nret = NUM_OVER;
197 goto err;
199 if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
200 nret = NUM_UNDER;
201 goto err;
203 lval += ltmp;
204 /* If we cross 0, signed numbers lose their sign. */
205 if (lval == 0 && ntype == fmt[SDEC])
206 ntype = fmt[DEC];
207 nlen = snprintf(nbuf, sizeof(nbuf), ntype, lval);
208 } else {
209 if ((nret = nget_uslong(&ulval, t, NULL, base)) != NUM_OK)
210 goto err;
211 if (vp->character == '+') {
212 if (!NPFITS(ULONG_MAX, ulval, change)) {
213 nret = NUM_OVER;
214 goto err;
216 ulval += change;
217 } else {
218 if (ulval < change) {
219 nret = NUM_UNDER;
220 goto err;
222 ulval -= change;
225 /* Correct for literal "0[Xx]" in format. */
226 if (base == 16)
227 wlen -= 2;
229 nlen = snprintf(nbuf, sizeof(nbuf), ntype, wlen, ulval);
232 /* Build the new line. */
233 memmove(bp, p, beg);
234 memmove(bp + beg, nbuf, nlen);
235 memmove(bp + beg + nlen, p + end, len - beg - (end - beg));
236 len = beg + nlen + (len - beg - (end - beg));
238 nret = NUM_OK;
239 rval = db_set(sp, vp->m_start.lno, bp, len);
241 if (0) {
242 err: rval = 1;
243 inc_err(sp, nret);
245 if (bp != NULL)
246 FREE_SPACE(sp, bp, blen);
247 return (rval);
250 static void
251 inc_err(sp, nret)
252 SCR *sp;
253 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;