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
[] = "$Id: v_increment.c,v 10.15 2000/07/15 20:26:36 skimo Exp $ (Berkeley) $Date: 2000/07/15 20:26:36 $";
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 *ntype
, nbuf
[100];
65 /* Validate the operator. */
66 if (vp
->character
== '#')
68 if (vp
->character
!= '+' && vp
->character
!= '-') {
69 v_emsg(sp
, vp
->kp
->usage
, VIM_USAGE
);
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
);
84 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
91 * Skip any leading space before the number. Getting a cursor word
92 * implies moving the cursor to its beginning, if we moved, refresh
95 for (beg
= vp
->m_start
.cno
; beg
< len
&& isspace(p
[beg
]); ++beg
);
98 if (beg
!= vp
->m_start
.cno
) {
100 (void)vs_refresh(sp
, 0);
104 #define ishex(c) (isdigit(c) || strchr("abcdefABCDEF", c))
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
115 if (p
[beg
] == '0' && wlen
> 2 &&
116 (p
[beg
+ 1] == 'X' || p
[beg
+ 1] == 'x')) {
121 ntype
= p
[beg
+ 1] == 'X' ? fmt
[HEXC
] : fmt
[HEXL
];
122 } else if (p
[beg
] == '0' && wlen
> 1) {
125 if (!isoctal(p
[end
]))
128 } else if (wlen
>= 1 && (p
[beg
] == '+' || p
[beg
] == '-')) {
132 if (!isdigit(p
[end
]))
138 if (!isdigit(p
[end
])) {
139 nonum
: msgq(sp
, M_ERR
, "181|Cursor not in a number");
144 /* Find the end of the word, possibly correcting the base. */
145 while (++end
< len
) {
150 if (p
[end
] == '8' || p
[end
] == '9') {
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
179 GET_SPACE_RETW(sp
, bp
, blen
, len
+ 50);
181 MEMMOVEW(bp
, &p
[beg
], wlen
);
188 * Octal or hex deal in unsigned longs, everything else is done
192 if ((nret
= nget_slong(sp
, &lval
, t
, NULL
, 10)) != NUM_OK
)
194 ltmp
= vp
->character
== '-' ? -change
: change
;
195 if (lval
> 0 && ltmp
> 0 && !NPFITS(LONG_MAX
, lval
, ltmp
)) {
199 if (lval
< 0 && ltmp
< 0 && !NNFITS(LONG_MIN
, lval
, ltmp
)) {
204 /* If we cross 0, signed numbers lose their sign. */
205 if (lval
== 0 && ntype
== fmt
[SDEC
])
207 nlen
= snprintf(nbuf
, sizeof(nbuf
), ntype
, lval
);
209 if ((nret
= nget_uslong(sp
, &ulval
, t
, NULL
, base
)) != NUM_OK
)
211 if (vp
->character
== '+') {
212 if (!NPFITS(ULONG_MAX
, ulval
, change
)) {
218 if (ulval
< change
) {
225 /* Correct for literal "0[Xx]" in format. */
229 nlen
= snprintf(nbuf
, sizeof(nbuf
), ntype
, wlen
, ulval
);
232 /* Build the new line. */
233 MEMMOVEW(bp
, p
, beg
);
234 MEMMOVEW(bp
+ beg
, nbuf
, nlen
);
235 MEMMOVEW(bp
+ beg
+ nlen
, p
+ end
, len
- beg
- (end
- beg
));
236 len
= beg
+ nlen
+ (len
- beg
- (end
- beg
));
239 rval
= db_set(sp
, vp
->m_start
.lno
, bp
, len
);
246 FREE_SPACEW(sp
, bp
, blen
);
262 msgq(sp
, M_ERR
, "182|Resulting number too large");
265 msgq(sp
, M_ERR
, "183|Resulting number too small");