| 1 | /* |
| 2 | * main.c - v1.14 - 18 December 2010 |
| 3 | * (c) Stevie Strickland, 1999-2012 |
| 4 | * |
| 5 | * This program has been placed under the GPL. Any bugfixes or enhancements |
| 6 | * will be greatly appreciated :) |
| 7 | * |
| 8 | * Stevie Strickland - [email protected] |
| 9 | */ |
| 10 | |
| 11 | /* For the abs() function */ |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include "rolldice.h" |
| 15 | #include "version.h" |
| 16 | |
| 17 | |
| 18 | /* The long options for this program in an struct option array */ |
| 19 | struct option long_opts[] = {{"help", 0, NULL, 'h'}, |
| 20 | {"version", 0, NULL, 'v'}, |
| 21 | {"random", 0, NULL, 'r'}, |
| 22 | {"urandom", 0, NULL, 'u'}, |
| 23 | {"separate", 0, NULL, 's'}, |
| 24 | {"interactive", 0, NULL, 'i'}, |
| 25 | {NULL, 0, NULL, 0}}; |
| 26 | |
| 27 | /* For getopt usage */ |
| 28 | extern int optind; |
| 29 | |
| 30 | /* Stores the random number file to use */ |
| 31 | rand_type rand_file = UNDEF; |
| 32 | |
| 33 | /* Should we print out the separate results of the dice rolls or not? */ |
| 34 | static int print_separate; |
| 35 | |
| 36 | /* |
| 37 | * print_usage() - Prints the usage for rolldice |
| 38 | * |
| 39 | * Parameters: Value with which to exit program |
| 40 | * Returns: none |
| 41 | */ |
| 42 | static void print_usage(int exitval) { |
| 43 | fprintf(stderr, "Usage: rolldice [options] <dice string>\n"); |
| 44 | fprintf(stderr, "For dice string format and options, "); |
| 45 | fprintf(stderr, "see rolldice(6).\n"); |
| 46 | exit(exitval); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * print_version() - Prints the version number |
| 51 | * |
| 52 | * Parameters: none |
| 53 | * Returns: none |
| 54 | */ |
| 55 | static void print_version() { |
| 56 | printf("rolldice, v%d.%d\n", MAJOR_VERSION, MINOR_VERSION); |
| 57 | printf("Written by Stevie Strickland ([email protected])\n"); |
| 58 | exit(EX_OK); |
| 59 | } |
| 60 | |
| 61 | /* print_rolls() - Prints the rolls, either just the totals or the |
| 62 | * separate rolls, also. |
| 63 | * |
| 64 | * Parameters: Dice string with which to calculate dice rolls |
| 65 | * Returns: None |
| 66 | */ |
| 67 | void print_rolls(int *dice_nums) { |
| 68 | int i, j, k, temp_int, temp_index, temp_total; |
| 69 | int* temp_roll; |
| 70 | |
| 71 | if((temp_roll = malloc(sizeof(*temp_roll) * dice_nums[NUM_DICE])) == NULL) { |
| 72 | perror("rolldice"); |
| 73 | exit(EX_OSERR); |
| 74 | } |
| 75 | |
| 76 | for(i = 0; i < dice_nums[NUM_ROLLS]; i++) { |
| 77 | temp_total = 0; |
| 78 | if(print_separate) printf("Roll #%d: (", i+1); |
| 79 | for(j = 0; j < dice_nums[NUM_DICE]; j++) { |
| 80 | temp_roll[j] = rolldie(dice_nums[NUM_SIDES]); |
| 81 | if(print_separate) printf("%d%s", temp_roll[j], (j < dice_nums[NUM_DICE] - 1) ? " " : ""); |
| 82 | temp_total += temp_roll[j]; |
| 83 | } |
| 84 | for(j = 0; j < dice_nums[NUM_DROP]; j++) { |
| 85 | temp_int = SHRT_MAX; |
| 86 | for(k = 0; k < dice_nums[NUM_DICE]; k++) |
| 87 | if(temp_int > temp_roll[k]) { |
| 88 | temp_int = temp_roll[k]; |
| 89 | temp_index = k; |
| 90 | } |
| 91 | if(print_separate) printf("- %d ", temp_int); |
| 92 | temp_total -= temp_int; |
| 93 | temp_roll[temp_index] = SHRT_MAX; |
| 94 | } |
| 95 | if(print_separate) printf(") "); |
| 96 | if(dice_nums[MULTIPLIER] != 1) { |
| 97 | if(print_separate) printf("* %d ", dice_nums[MULTIPLIER]); |
| 98 | temp_total *= dice_nums[MULTIPLIER]; |
| 99 | } |
| 100 | if(dice_nums[MODIFIER]) { |
| 101 | if(print_separate){ |
| 102 | if (dice_nums[MODIFIER] > 0) |
| 103 | printf("+ %d ", dice_nums[MODIFIER]); |
| 104 | else |
| 105 | printf("- %d ", abs(dice_nums[MODIFIER])); |
| 106 | |
| 107 | } |
| 108 | temp_total += dice_nums[MODIFIER]; |
| 109 | } |
| 110 | if(print_separate) printf("= "); |
| 111 | printf("%d ", temp_total); |
| 112 | if(print_separate) printf("\n"); |
| 113 | } |
| 114 | if(!print_separate) printf("\n"); |
| 115 | } |
| 116 | |
| 117 | #define DICE_PROMPT "> " |
| 118 | |
| 119 | /* roll_from_interactive() - parse stdin, one roll by line, don't exit on errors |
| 120 | * |
| 121 | * Parameters: None |
| 122 | * Returns: None |
| 123 | */ |
| 124 | void roll_from_interactive(){ |
| 125 | int dice_nums[DICE_ARRAY_SIZE] = {}; |
| 126 | static char *line = (char *)NULL; |
| 127 | |
| 128 | line = readline(DICE_PROMPT); |
| 129 | while(line){ |
| 130 | parse_string( line, dice_nums ); |
| 131 | |
| 132 | free(line); |
| 133 | print_rolls(dice_nums); |
| 134 | |
| 135 | line = (char *)NULL; |
| 136 | line = readline(DICE_PROMPT); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* roll_from_stdin() - parse stdin, one roll by line |
| 141 | * |
| 142 | * Parameters: None |
| 143 | * Returns: EX_OK or EXIT_DATAERR (if bad stdin) |
| 144 | */ |
| 145 | int roll_from_stdin(){ |
| 146 | int dice_nums[DICE_ARRAY_SIZE] = {}; |
| 147 | static char *line = (char *)NULL; |
| 148 | |
| 149 | line = readline(""); |
| 150 | while(line){ |
| 151 | int res_int = parse_string( line, dice_nums ); |
| 152 | if ( res_int ) { |
| 153 | return EX_DATAERR; |
| 154 | } |
| 155 | free(line); |
| 156 | print_rolls(dice_nums); |
| 157 | |
| 158 | line = (char *)NULL; |
| 159 | line = readline(""); |
| 160 | } |
| 161 | return EX_OK; |
| 162 | } |
| 163 | |
| 164 | /* roll_from_args() - parse command line args to roll dices |
| 165 | * |
| 166 | * Parameters: args passed to CLI |
| 167 | * Returns: EX_OK or EXIT_DATAERR (if bad command line) |
| 168 | */ |
| 169 | int roll_from_args(char **argv){ |
| 170 | int dice_nums[DICE_ARRAY_SIZE] = {}; |
| 171 | int index; |
| 172 | |
| 173 | for(index = optind; argv[index] != NULL; index++) { |
| 174 | int res_int = parse_string( argv[index], dice_nums ); |
| 175 | if ( res_int ) { |
| 176 | return EX_DATAERR; |
| 177 | } |
| 178 | print_rolls(dice_nums); |
| 179 | } |
| 180 | return EX_OK; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | int main(int argc, char **argv) { |
| 185 | int c; |
| 186 | int interactive = 0; |
| 187 | |
| 188 | while((c = getopt_long(argc, argv, "hvrusi", long_opts, NULL)) != -1) { |
| 189 | switch(c) { |
| 190 | case 'h': |
| 191 | print_usage(EX_OK); break; |
| 192 | case 'v': |
| 193 | print_version(); break; |
| 194 | case 'r': |
| 195 | if(rand_file == URANDOM) { |
| 196 | fprintf(stderr, "Choose either '-r' or '-u', please.\n"); |
| 197 | return EX_USAGE; |
| 198 | } |
| 199 | rand_file = RANDOM; break; |
| 200 | case 'u': |
| 201 | if(rand_file == RANDOM) { |
| 202 | fprintf(stderr, "Choose either '-r' or '-u', please.\n"); |
| 203 | return EX_USAGE; |
| 204 | } |
| 205 | rand_file = URANDOM; break; |
| 206 | case 's': |
| 207 | print_separate = 1; break; |
| 208 | case 'i': |
| 209 | interactive = 1; break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | init_random(rand_file); |
| 214 | |
| 215 | if (interactive) { |
| 216 | roll_from_interactive(); |
| 217 | return EX_OK; |
| 218 | } |
| 219 | else if ( optind == argc ) { |
| 220 | return roll_from_stdin(); |
| 221 | } |
| 222 | else { |
| 223 | return roll_from_args(argv); |
| 224 | } |
| 225 | } |