1 /* $NetBSD: v_increment.c,v 1.3 2009/01/18 03:45:50 lukem Exp $ */
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.
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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
33 static const CHAR_T
* const fmt
[] = {
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 *));
55 v_increment(SCR
*sp
, VICMD
*vp
)
60 size_t beg
, blen
, end
, len
, nlen
, wlen
;
61 int base
, isempty
, rval
;
66 /* Validate the operator. */
67 if (vp
->character
== '#')
69 if (vp
->character
!= '+' && vp
->character
!= '-') {
70 v_emsg(sp
, vp
->kp
->usage
, VIM_USAGE
);
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
);
85 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
92 * Skip any leading space before the number. Getting a cursor word
93 * implies moving the cursor to its beginning, if we moved, refresh
96 for (beg
= vp
->m_start
.cno
; beg
< len
&& isspace(p
[beg
]); ++beg
);
99 if (beg
!= vp
->m_start
.cno
) {
101 (void)vs_refresh(sp
, 0);
105 #define ishex(c) (isdigit(c) || STRCHR(L("abcdefABCDEF"), c))
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
116 if (p
[beg
] == '0' && wlen
> 2 &&
117 (p
[beg
+ 1] == 'X' || p
[beg
+ 1] == 'x')) {
122 ntype
= p
[beg
+ 1] == 'X' ? fmt
[HEXC
] : fmt
[HEXL
];
123 } else if (p
[beg
] == '0' && wlen
> 1) {
126 if (!isoctal(p
[end
]))
129 } else if (wlen
>= 1 && (p
[beg
] == '+' || p
[beg
] == '-')) {
133 if (!isdigit(p
[end
]))
139 if (!isdigit(p
[end
])) {
140 nonum
: msgq(sp
, M_ERR
, "181|Cursor not in a number");
145 /* Find the end of the word, possibly correcting the base. */
146 while (++end
< len
) {
151 if (p
[end
] == '8' || p
[end
] == '9') {
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
180 GET_SPACE_RETW(sp
, bp
, blen
, len
+ 50);
182 MEMMOVEW(bp
, &p
[beg
], wlen
);
189 * Octal or hex deal in unsigned longs, everything else is done
193 if ((nret
= nget_slong(sp
, &lval
, t
, NULL
, 10)) != NUM_OK
)
195 ltmp
= vp
->character
== '-' ? -change
: change
;
196 if (lval
> 0 && ltmp
> 0 &&
197 !NPFITS(LONG_MAX
, (unsigned long)lval
, (unsigned long)ltmp
)) {
201 if (lval
< 0 && ltmp
< 0 && !NNFITS(LONG_MIN
, lval
, ltmp
)) {
206 /* If we cross 0, signed numbers lose their sign. */
207 if (lval
== 0 && ntype
== fmt
[SDEC
])
209 nlen
= SPRINTF(nbuf
, sizeof(nbuf
), ntype
, lval
);
211 if ((nret
= nget_uslong(sp
, &ulval
, t
, NULL
, base
)) != NUM_OK
)
213 if (vp
->character
== '+') {
214 if (!NPFITS(ULONG_MAX
, ulval
, change
)) {
220 if (ulval
< change
) {
227 /* Correct for literal "0[Xx]" in format. */
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
));
241 rval
= db_set(sp
, vp
->m_start
.lno
, bp
, len
);
248 FREE_SPACEW(sp
, bp
, blen
);
253 inc_err(SCR
*sp
, enum nresult nret
)
262 msgq(sp
, M_ERR
, "182|Resulting number too large");
265 msgq(sp
, M_ERR
, "183|Resulting number too small");