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