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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
/* TODO add support for up and down arrows to browse previous entries in the same session */
USAGE("");
struct die {
intmax_t count;
intmax_t min;
intmax_t max;
};
static intmax_t
random_int(intmax_t min, intmax_t max)
{
uintmax_t res, max0 = (uintmax_t)max - (uintmax_t)min, max1;
int steps;
ssize_t r;
size_t n;
for (max1 = max0, steps = 0; max1 < UINTMAX_MAX && (max1 & (max1 + 1)); steps++)
max1 |= max1 >> steps;
do {
for (n = 0; n < sizeof(res); n += (size_t)r) {
r = getrandom(&((char *)&res)[n], sizeof(res) - n, 0);
if (r < 0)
eprintf("getrandom <buffer> %zu 0:", sizeof(res) - n);
}
res &= max1;
} while (res > max0);
return (intmax_t)res + min;
}
static void
roll_dice(const struct die *dice, size_t n)
{
intmax_t sum = 0, res, j;
size_t i;
for (i = 0; i < n; i++) {
for (j = 0; j < dice[i].count; j++) {
sum += res = random_int(dice[i].min, dice[i].max);
printf("Die roll: %ji\n", res);
}
}
if (n > 1 || (n == 1 && dice[0].count > 1)) {
printf("Sum: %ji\n", sum);
}
}
static int
parse_dice(const char *s, struct die **dicep, size_t *ndicep)
{
struct die *dice = NULL, *die;
size_t ndice = 0;
intmax_t min_sum = 0, max_sum = 0, num, i;
ndice = 0;
for (;;) {
while (*s == ' ' || *s == '\t')
s++;
if (!*s)
break;
if (!isdigit(*s) && *s != '-' && *s != 'd' && *s != 'D')
goto invalid;
dice = ereallocn(dice, ++ndice, sizeof(*dice), 0);
die = &dice[ndice - 1];
die->count = 1;
die->min = 1;
num = 1;
if (*s == 'd' || *s == 'D')
goto at_d;
errno = 0;
num = strtoimax(s, (void *)&s, 0);
if (errno)
goto invalid;
if (*s == 'd' || *s == 'D') {
at_d:
s++;
if (num < 1)
goto invalid;
die->count = (size_t)num;
if (!isdigit(*s) && *s != '-')
goto invalid;
errno = 0;
num = strtoimax(s, (void *)&s, 0);
if (errno)
goto invalid;
}
die->max = num;
if (*s == '-') {
s++;
die->min = num;
errno = 0;
die->max = strtoimax(s, (void *)&s, 0);
if (errno)
goto invalid;
}
if (die->min > die->max)
goto invalid;
for (i = 0; i < die->count; i++) {
if (die->min < 0 && min_sum < INTMAX_MIN - die->min) {
printf("\033[31m%s\033[m\n", "Negative overflow in sum of minimums");
return -1;
}
if (die->min > 0 && min_sum > INTMAX_MAX - die->min) {
printf("\033[31m%s\033[m\n", "Positive overflow in sum of minimums");
return -1;
}
if (die->max < 0 && max_sum < INTMAX_MIN - die->max) {
printf("\033[31m%s\033[m\n", "Negative overflow in sum of maximums");
return -1;
}
if (die->max > 0 && max_sum > INTMAX_MAX - die->max) {
printf("\033[31m%s\033[m\n", "Positive overflow in sum of maximums");
return -1;
}
min_sum += die->min;
max_sum += die->max;
}
if (*s && *s != ',' && *s != ' ' && *s != '\t')
goto invalid;
if (*s == ',')
s++;
}
if (*s || !ndice)
goto invalid;
free(*dicep);
*dicep = dice;
*ndicep = ndice;
return 0;
invalid:
printf("\033[31m%s\033[m\n", "Invalid input");
return -1;
}
int
main(int argc, char *argv[])
{
struct die *dice = NULL;
size_t ndice = 0;
char *line = NULL;
size_t size = 0;
ssize_t len;
int first;
NOFLAGS(argc);
for (first = 1;; first = 0) {
again:
if (first)
printf("\033[1m%s: \033[m", "Enter dice to roll");
else
printf("\033[1m%s\033[m (%s): ", "Enter dice to roll", "Leave empty to reroll");
fflush(stdout);
len = getdelim(&line, &size, '\n', stdin);
if (len < 0) {
printf("\n");
break;
}
if (len && line[len - 1] == '\n')
line[--len] = '\0';
if (!strcasecmp(line, "q") || !strcasecmp(line, "exit") || !strcasecmp(line, "quit"))
break;
if (!*line ? first : parse_dice(line, &dice, &ndice))
goto again;
roll_dice(dice, ndice);
}
if (ferror(stdin))
eprintf("getdelim <stdin>:");
free(dice);
free(line);
return 0;
}
|