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.
13 static const char sccsid[] = "@(#)v_increment.c 10.12 (Berkeley) 3/19/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
31 static char * const fmt[] = {
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 *));
59 long change, ltmp, lval;
60 size_t beg, blen, end, len, nlen, wlen;
61 int base, isempty, rval;
62 char *bp, *ntype, *p, *t, nbuf[100];
64 /* Validate the operator. */
65 if (vp->character == '#')
67 if (vp->character != '+' && vp->character != '-') {
68 v_emsg(sp, vp->kp->usage, VIM_USAGE);
72 /* If new value set, save it off, but it has to fit in a long. */
73 if (F_ISSET(vp, VC_C1SET)) {
74 if (vp->count > LONG_MAX) {
75 inc_err(sp, NUM_OVER);
83 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
90 * Skip any leading space before the number. Getting a cursor word
91 * implies moving the cursor to its beginning, if we moved, refresh
94 for (beg = vp->m_start.cno; beg < len && isspace(p[beg]); ++beg);
97 if (beg != vp->m_start.cno) {
99 (void)vs_refresh(sp, 0);
103 #define ishex(c) (isdigit(c) || strchr("abcdefABCDEF", c))
105 #define isoctal(c) (isdigit(c) && (c) != '8' && (c) != '9')
108 * Look for 0[Xx], or leading + or - signs, guess at the base.
109 * The character after that must be a number. Wlen is set to
110 * the remaining characters in the line that could be part of
114 if (p[beg] == '0' && wlen > 2 &&
115 (p[beg + 1] == 'X' || p[beg + 1] == 'x')) {
120 ntype = p[beg + 1] == 'X' ? fmt[HEXC] : fmt[HEXL];
121 } else if (p[beg] == '0' && wlen > 1) {
124 if (!isoctal(p[end]))
127 } else if (wlen >= 1 && (p[beg] == '+' || p[beg] == '-')) {
131 if (!isdigit(p[end]))
137 if (!isdigit(p[end])) {
138 nonum: msgq(sp, M_ERR, "181|Cursor not in a number");
143 /* Find the end of the word, possibly correcting the base. */
144 while (++end < len) {
149 if (p[end] == '8' || p[end] == '9') {
173 * If the line was at the end of the buffer, we have to copy it
174 * so we can guarantee that it's NULL-terminated. We make the
175 * buffer big enough to fit the line changes as well, and only
178 GET_SPACE_RET(sp, bp, blen, len + 50);
180 memmove(bp, &p[beg], wlen);
187 * Octal or hex deal in unsigned longs, everything else is done
191 if ((nret = nget_slong(&lval, t, NULL, 10)) != NUM_OK)
193 ltmp = vp->character == '-' ? -change : change;
194 if (lval > 0 && ltmp > 0 && !NPFITS(LONG_MAX, lval, ltmp)) {
198 if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
203 /* If we cross 0, signed numbers lose their sign. */
204 if (lval == 0 && ntype == fmt[SDEC])
206 nlen = snprintf(nbuf, sizeof(nbuf), ntype, lval);
208 if ((nret = nget_uslong(&ulval, t, NULL, base)) != NUM_OK)
210 if (vp->character == '+') {
211 if (!NPFITS(ULONG_MAX, ulval, change)) {
217 if (ulval < change) {
224 /* Correct for literal "0[Xx]" in format. */
228 nlen = snprintf(nbuf, sizeof(nbuf), ntype, wlen, ulval);
231 /* Build the new line. */
233 memmove(bp + beg, nbuf, nlen);
234 memmove(bp + beg + nlen, p + end, len - beg - (end - beg));
235 len = beg + nlen + (len - beg - (end - beg));
238 rval = db_set(sp, vp->m_start.lno, bp, len);
245 FREE_SPACE(sp, bp, blen);
261 msgq(sp, M_ERR, "182|Resulting number too large");
264 msgq(sp, M_ERR, "183|Resulting number too small");