-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
225 lines (197 loc) · 5.52 KB
/
main.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* main.c - v1.14 - 18 December 2010
* (c) Stevie Strickland, 1999-2012
*
* This program has been placed under the GPL. Any bugfixes or enhancements
* will be greatly appreciated :)
*
* Stevie Strickland - sstrickl@ccs.neu.edu
*/
/* For the abs() function */
#include <stdlib.h>
#include "rolldice.h"
#include "version.h"
/* The long options for this program in an struct option array */
struct option long_opts[] = {{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"random", 0, NULL, 'r'},
{"urandom", 0, NULL, 'u'},
{"separate", 0, NULL, 's'},
{"interactive", 0, NULL, 'i'},
{NULL, 0, NULL, 0}};
/* For getopt usage */
extern int optind;
/* Stores the random number file to use */
rand_type rand_file = UNDEF;
/* Should we print out the separate results of the dice rolls or not? */
static int print_separate;
/*
* print_usage() - Prints the usage for rolldice
*
* Parameters: Value with which to exit program
* Returns: none
*/
static void print_usage(int exitval) {
fprintf(stderr, "Usage: rolldice [options] <dice string>\n");
fprintf(stderr, "For dice string format and options, ");
fprintf(stderr, "see rolldice(6).\n");
exit(exitval);
}
/*
* print_version() - Prints the version number
*
* Parameters: none
* Returns: none
*/
static void print_version() {
printf("rolldice, v%d.%d\n", MAJOR_VERSION, MINOR_VERSION);
printf("Written by Stevie Strickland (sstrickl@gmail.com)\n");
exit(EX_OK);
}
/* print_rolls() - Prints the rolls, either just the totals or the
* separate rolls, also.
*
* Parameters: Dice string with which to calculate dice rolls
* Returns: None
*/
void print_rolls(int *dice_nums) {
int i, j, k, temp_int, temp_index, temp_total;
int* temp_roll;
if((temp_roll = malloc(sizeof(*temp_roll) * dice_nums[NUM_DICE])) == NULL) {
perror("rolldice");
exit(EX_OSERR);
}
for(i = 0; i < dice_nums[NUM_ROLLS]; i++) {
temp_total = 0;
if(print_separate) printf("Roll #%d: (", i+1);
for(j = 0; j < dice_nums[NUM_DICE]; j++) {
temp_roll[j] = rolldie(dice_nums[NUM_SIDES]);
if(print_separate) printf("%d%s", temp_roll[j], (j < dice_nums[NUM_DICE] - 1) ? " " : "");
temp_total += temp_roll[j];
}
for(j = 0; j < dice_nums[NUM_DROP]; j++) {
temp_int = SHRT_MAX;
for(k = 0; k < dice_nums[NUM_DICE]; k++)
if(temp_int > temp_roll[k]) {
temp_int = temp_roll[k];
temp_index = k;
}
if(print_separate) printf("- %d ", temp_int);
temp_total -= temp_int;
temp_roll[temp_index] = SHRT_MAX;
}
if(print_separate) printf(") ");
if(dice_nums[MULTIPLIER] != 1) {
if(print_separate) printf("* %d ", dice_nums[MULTIPLIER]);
temp_total *= dice_nums[MULTIPLIER];
}
if(dice_nums[MODIFIER]) {
if(print_separate){
if (dice_nums[MODIFIER] > 0)
printf("+ %d ", dice_nums[MODIFIER]);
else
printf("- %d ", abs(dice_nums[MODIFIER]));
}
temp_total += dice_nums[MODIFIER];
}
if(print_separate) printf("= ");
printf("%d ", temp_total);
if(print_separate) printf("\n");
}
if(!print_separate) printf("\n");
}
#define DICE_PROMPT "> "
/* roll_from_interactive() - parse stdin, one roll by line, don't exit on errors
*
* Parameters: None
* Returns: None
*/
void roll_from_interactive(){
int dice_nums[DICE_ARRAY_SIZE] = {};
static char *line = (char *)NULL;
line = readline(DICE_PROMPT);
while(line){
parse_string( line, dice_nums );
free(line);
print_rolls(dice_nums);
line = (char *)NULL;
line = readline(DICE_PROMPT);
}
}
/* roll_from_stdin() - parse stdin, one roll by line
*
* Parameters: None
* Returns: EX_OK or EXIT_DATAERR (if bad stdin)
*/
int roll_from_stdin(){
int dice_nums[DICE_ARRAY_SIZE] = {};
static char *line = (char *)NULL;
line = readline("");
while(line){
int res_int = parse_string( line, dice_nums );
if ( res_int ) {
return EX_DATAERR;
}
free(line);
print_rolls(dice_nums);
line = (char *)NULL;
line = readline("");
}
return EX_OK;
}
/* roll_from_args() - parse command line args to roll dices
*
* Parameters: args passed to CLI
* Returns: EX_OK or EXIT_DATAERR (if bad command line)
*/
int roll_from_args(char **argv){
int dice_nums[DICE_ARRAY_SIZE] = {};
int index;
for(index = optind; argv[index] != NULL; index++) {
int res_int = parse_string( argv[index], dice_nums );
if ( res_int ) {
return EX_DATAERR;
}
print_rolls(dice_nums);
}
return EX_OK;
}
int main(int argc, char **argv) {
int c;
int interactive = 0;
while((c = getopt_long(argc, argv, "hvrusi", long_opts, NULL)) != -1) {
switch(c) {
case 'h':
print_usage(EX_OK); break;
case 'v':
print_version(); break;
case 'r':
if(rand_file == URANDOM) {
fprintf(stderr, "Choose either '-r' or '-u', please.\n");
return EX_USAGE;
}
rand_file = RANDOM; break;
case 'u':
if(rand_file == RANDOM) {
fprintf(stderr, "Choose either '-r' or '-u', please.\n");
return EX_USAGE;
}
rand_file = URANDOM; break;
case 's':
print_separate = 1; break;
case 'i':
interactive = 1; break;
}
}
init_random(rand_file);
if (interactive) {
roll_from_interactive();
return EX_OK;
}
else if ( optind == argc ) {
return roll_from_stdin();
}
else {
return roll_from_args(argv);
}
}