1 /* $NetBSD: seq.c,v 1.7 2010/05/27 08:40:19 dholland Exp $ */
3 * Copyright (c) 2005 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
47 #define MAX(a, b) (((a) < (b))? (b) : (a))
48 #define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
49 #define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
50 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
54 static const char *decimal_point
= "."; /* default */
55 static char default_format
[] = { "%g" }; /* default */
59 static double e_atof(const char *);
61 static int decimal_places(const char *);
62 static int numeric(const char *);
63 static int valid_format(const char *);
65 static char *generate_format(double, double, double, int, char);
66 static char *unescape(char *);
69 * The seq command will print out a numeric sequence from 1, the default,
70 * to a user specified upper limit by 1. The lower bound and increment
71 * maybe indicated by the user on the command line. The sequence can
72 * be either whole, the default, or decimal numbers.
75 main(int argc
, char *argv
[])
77 int c
= 0, errflg
= 0;
84 const char *sep
= "\n";
85 const char *term
= NULL
;
88 /* Determine the locale's decimal point. */
89 locale
= localeconv();
90 if (locale
&& locale
->decimal_point
&& locale
->decimal_point
[0] != '\0')
91 decimal_point
= locale
->decimal_point
;
94 * Process options, but handle negative numbers separately
95 * least they trip up getopt(3).
97 while ((optind
< argc
) && !numeric(argv
[optind
]) &&
98 (c
= getopt(argc
, argv
, "f:hs:t:w")) != -1) {
101 case 'f': /* format (plan9) */
105 case 's': /* separator (GNU) */
106 sep
= unescape(optarg
);
108 case 't': /* terminator (new) */
109 term
= unescape(optarg
);
111 case 'w': /* equal width (plan9) */
116 case 'h': /* help (GNU) */
125 if (argc
< 1 || argc
> 3)
130 "usage: %s [-w] [-f format] [-s string] [-t string] [first [incr]] last\n",
135 last
= e_atof(argv
[argc
- 1]);
138 first
= e_atof(argv
[0]);
141 incr
= e_atof(argv
[1]);
142 /* Plan 9/GNU don't do zero */
144 errx(1, "zero %screment", (first
< last
)? "in" : "de");
147 /* default is one for Plan 9/GNU work alike */
149 incr
= (first
< last
) ? 1.0 : -1.0;
151 if (incr
<= 0.0 && first
< last
)
152 errx(1, "needs positive increment");
154 if (incr
>= 0.0 && first
> last
)
155 errx(1, "needs negative decrement");
158 if (!valid_format(fmt
))
159 errx(1, "invalid format string: `%s'", fmt
);
161 if (!valid_format(fmt
))
162 errx(1, "invalid format string");
164 * XXX to be bug for bug compatible with Plan 9 add a
165 * newline if none found at the end of the format string.
168 fmt
= generate_format(first
, incr
, last
, equalize
, pad
);
171 for (; first
<= last
; first
+= incr
) {
176 for (; first
>= last
; first
+= incr
) {
188 * numeric - verify that string is numeric
191 numeric(const char *s
)
193 int seen_decimal_pt
, decimal_pt_len
;
196 if (ISSIGN((unsigned char)*s
))
200 decimal_pt_len
= strlen(decimal_point
);
202 if (!isdigit((unsigned char)*s
)) {
203 if (!seen_decimal_pt
&&
204 strncmp(s
, decimal_point
, decimal_pt_len
) == 0) {
209 if (ISEXP((unsigned char)*s
)) {
211 if (ISSIGN((unsigned char)*s
) ||
212 isdigit((unsigned char)*s
)) {
225 * valid_format - validate user specified format string
228 valid_format(const char *fmt
)
230 unsigned conversions
= 0;
232 while (*fmt
!= '\0') {
233 /* scan for conversions */
240 /* allow %% but not things like %10% */
247 while (*fmt
!= '\0' && strchr("#0- +'", *fmt
)) {
252 while (*fmt
!= '\0' && strchr("0123456789", *fmt
)) {
259 while (*fmt
!= '\0' && strchr("0123456789", *fmt
)) {
274 /* floating point formats are accepted */
278 /* anything else is not */
283 return (conversions
<= 1);
287 * unescape - handle C escapes in a string
292 char c
, *cp
, *new = orig
;
295 for (cp
= orig
; (*orig
= *cp
); cp
++, orig
++) {
300 case 'a': /* alert (bell) */
303 case 'b': /* backspace */
306 case 'e': /* escape */
309 case 'f': /* formfeed */
312 case 'n': /* newline */
315 case 'r': /* carriage return */
318 case 't': /* horizontal tab */
321 case 'v': /* vertical tab */
324 case '\\': /* backslash */
327 case '\'': /* single quote */
330 case '\"': /* double quote */
336 case '3': /* octal */
340 case '7': /* number */
342 ISODIGIT((unsigned char)*cp
) && i
< 3;
350 case 'x': /* hexadecimal number */
353 isxdigit((unsigned char)*cp
) && i
< 2;
356 if (isdigit((unsigned char)*cp
))
359 c
|= ((toupper((unsigned char)*cp
) -
375 * e_atof - convert an ASCII string to a double
376 * exit if string is not a valid double, or if converted value would
377 * cause overflow or underflow
380 e_atof(const char *num
)
386 dbl
= strtod(num
, &endp
);
389 /* under or overflow */
391 else if (*endp
!= '\0')
392 /* "junk" left in number */
393 errx(2, "invalid floating point argument: %s", num
);
395 /* zero shall have no sign */
402 * decimal_places - count decimal places in a number (string)
405 decimal_places(const char *number
)
410 /* look for a decimal point */
411 if ((dp
= strstr(number
, decimal_point
))) {
412 dp
+= strlen(decimal_point
);
414 while (isdigit((unsigned char)*dp
++))
421 * generate_format - create a format string
423 * XXX to be bug for bug compatible with Plan9 and GNU return "%g"
424 * when "%g" prints as "%e" (this way no width adjustments are made)
427 generate_format(double first
, double incr
, double last
, int equalize
, char pad
)
429 static char buf
[256];
431 int precision
, width1
, width2
, places
;
434 return (default_format
);
436 /* figure out "last" value printed */
438 last
= first
- incr
* floor((first
- last
) / incr
);
440 last
= first
+ incr
* floor((last
- first
) / incr
);
442 sprintf(buf
, "%g", incr
);
443 if (strchr(buf
, 'e'))
445 precision
= decimal_places(buf
);
447 width1
= sprintf(buf
, "%g", first
);
448 if (strchr(buf
, 'e'))
450 if ((places
= decimal_places(buf
)))
451 width1
-= (places
+ strlen(decimal_point
));
453 precision
= MAX(places
, precision
);
455 width2
= sprintf(buf
, "%g", last
);
456 if (strchr(buf
, 'e'))
458 if ((places
= decimal_places(buf
)))
459 width2
-= (places
+ strlen(decimal_point
));
462 sprintf(buf
, "%%%c%d.%d%c", pad
,
463 MAX(width1
, width2
) + (int) strlen(decimal_point
) +
464 precision
, precision
, (cc
) ? cc
: 'f');
466 sprintf(buf
, "%%%c%d%c", pad
, MAX(width1
, width2
),