1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define FOR_seq
26#include "toys.h"
27
28GLOBALS(
29 char *sep;
30 char *fmt;
31
32 int precision;
33)
34
35
36static void insanitize(char *f)
37{
38 char *s = next_printf(f, 0);
39
40 if (!s) error_exit("bad -f no %%f");
41 if (-1 == stridx("aAeEfFgG", *s) || (s = next_printf(s, 0))) {
42
43 error_exit("bad -f '%s'@%d", f, (int)(s-f+1));
44 }
45}
46
47
48
49static double parsef(char *s)
50{
51 char *dp = strchr(s, '.');
52
53 if (dp++) TT.precision = maxof(TT.precision, strcspn(dp, "eE"));
54
55 return xstrtod(s);
56}
57
58void seq_main(void)
59{
60 double first = 1, increment = 1, last, dd;
61 int i;
62
63 if (!TT.sep) TT.sep = "\n";
64 switch (toys.optc) {
65 case 3: increment = parsef(toys.optargs[1]);
66 case 2: first = parsef(*toys.optargs);
67 default: last = parsef(toys.optargs[toys.optc-1]);
68 }
69
70
71 if (toys.optflags & FLAG_f) insanitize(TT.fmt);
72 else sprintf(TT.fmt = toybuf, "%%.%df", TT.precision);
73
74
75 if (toys.optflags & FLAG_w) {
76 int len = 0;
77
78 for (i=0; i<3; i++) {
79 dd = (double []){first, increment, last}[i];
80 len = maxof(len, snprintf(0, 0, TT.fmt, dd));
81 }
82 sprintf(TT.fmt = toybuf, "%%0%d.%df", len, TT.precision);
83 }
84
85
86
87
88 if (!increment) return;
89
90 i = 0;
91 for (;;) {
92
93 dd = first+i*increment;
94 if ((increment<0 && dd<last) || (increment>0 && dd>last)) break;
95 if (i++) printf("%s", TT.sep);
96 printf(TT.fmt, dd);
97 }
98
99 if (i) printf("\n");
100}
101