Debian Codegraph @

main.c

raw symbols SCIP
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"cxx . . $ `<file>/rolldice.h`/
15 #include "version.h"cxx . . $ `<file>/version.h`/
16
17
18 /* The long options for this program in an struct option array */
19 struct optioncxx . . $ option# long_optscxx . . $ long_opts.[] = {{"help", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 'h'},
20 {"version", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 'v'},
21 {"random", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 'r'},
22 {"urandom", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 'u'},
23 {"separate", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 's'},
24 {"interactive", 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 'i'},
25 {NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 0}};
26
27 /* For getopt usage */
28 extern int optindcxx . . $ optind.;
29
30 /* Stores the random number file to use */
31 rand_typecxx . . $ rand_type# rand_filecxx . . $ rand_file. = UNDEFcxx . . $ UNDEF.;
32
33 /* Should we print out the separate results of the dice rolls or not? */
34 static int print_separatecxx . . $ 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_usagecxx . . $ print_usage(d4f767463ce0a6b3).(int exitval) {
43 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Usage: rolldice [options] <dice string>\n");
44 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "For dice string format and options, ");
45 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "see rolldice(6).\n");
46 exitcxx . . $ exit(3ca292c338df9bb3).(exitval);
47 }
48
49 /*
50 * print_version() - Prints the version number
51 *
52 * Parameters: none
53 * Returns: none
54 */
55 static void print_versioncxx . . $ print_version(d878ad36e69247c7).() {
56 printfcxx . . $ printf(581fddd72a8db8fc).("rolldice, v%d.%d\n", MAJOR_VERSIONcxx . . $ MAJOR_VERSION., MINOR_VERSIONcxx . . $ MINOR_VERSION.);
57 printfcxx . . $ printf(581fddd72a8db8fc).("Written by Stevie Strickland ([email protected])\n");
58 exitcxx . . $ exit(3ca292c338df9bb3).(EX_OKcxx . . $ `<external>/5948936369402635282/sysexits.h:92:9`!);
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_rollscxx . . $ print_rolls(11b0e290e57a7e53).(int *dice_nums) {
68 int i, j, k, temp_int, temp_index, temp_total;
69 int* temp_roll;
70
71 if((temp_roll = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*temp_roll) * dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!])) == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
72 perrorcxx . . $ perror(85c52e162fed56f9).("rolldice");
73 exitcxx . . $ exit(3ca292c338df9bb3).(EX_OSERRcxx . . $ `<external>/5948936369402635282/sysexits.h:103:9`!);
74 }
75
76 for(i = 0; i < dice_nums[NUM_ROLLScxx . . $ `rolldice.h:38:9`!]; i++) {
77 temp_total = 0;
78 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("Roll #%d: (", i+1);
79 for(j = 0; j < dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!]; j++) {
80 temp_roll[j] = rolldiecxx . . $ rolldie(7864480464b09eea).(dice_nums[NUM_SIDEScxx . . $ `rolldice.h:40:9`!]);
81 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("%d%s", temp_roll[j], (j < dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!] - 1) ? " " : "");
82 temp_total += temp_roll[j];
83 }
84 for(j = 0; j < dice_nums[NUM_DROPcxx . . $ `rolldice.h:43:9`!]; j++) {
85 temp_int = SHRT_MAXcxx . . $ `<external>/67164224788392828/limits.h:106:9`!;
86 for(k = 0; k < dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!]; k++)
87 if(temp_int > temp_roll[k]) {
88 temp_int = temp_roll[k];
89 temp_index = k;
90 }
91 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("- %d ", temp_int);
92 temp_total -= temp_int;
93 temp_roll[temp_index] = SHRT_MAXcxx . . $ `<external>/67164224788392828/limits.h:106:9`!;
94 }
95 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).(") ");
96 if(dice_nums[MULTIPLIERcxx . . $ `rolldice.h:41:9`!] != 1) {
97 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("* %d ", dice_nums[MULTIPLIERcxx . . $ `rolldice.h:41:9`!]);
98 temp_total *= dice_nums[MULTIPLIERcxx . . $ `rolldice.h:41:9`!];
99 }
100 if(dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!]) {
101 if(print_separatecxx . . $ print_separate.){
102 if (dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!] > 0)
103 printfcxx . . $ printf(581fddd72a8db8fc).("+ %d ", dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!]);
104 else
105 printfcxx . . $ printf(581fddd72a8db8fc).("- %d ", abscxx . . $ abs(7864480464b09eea).(dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!]));
106
107 }
108 temp_total += dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!];
109 }
110 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("= ");
111 printfcxx . . $ printf(581fddd72a8db8fc).("%d ", temp_total);
112 if(print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("\n");
113 }
114 if(!print_separatecxx . . $ print_separate.) printfcxx . . $ printf(581fddd72a8db8fc).("\n");
115 }
116
117 #define DICE_PROMPTcxx . . $ `main.c:117:9`! "> "
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_interactivecxx . . $ roll_from_interactive(d878ad36e69247c7).(){
125 int dice_nums[DICE_ARRAY_SIZEcxx . . $ `rolldice.h:45:9`!] = {};
126 static char *line = (char *)NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
127
128 line = readlinecxx . . $ readline(a8b8e32347182212).(DICE_PROMPTcxx . . $ `main.c:117:9`!);
129 while(line){
130 parse_stringcxx . . $ parse_string(b0a0d720b596a511).( line, dice_nums );
131
132 freecxx . . $ free(5d22bdacc48458e8).(line);
133 print_rollscxx . . $ print_rolls(11b0e290e57a7e53).(dice_nums);
134
135 line = (char *)NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
136 line = readlinecxx . . $ readline(a8b8e32347182212).(DICE_PROMPTcxx . . $ `main.c:117:9`!);
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_stdincxx . . $ roll_from_stdin(d1b40a211a1a4e4c).(){
146 int dice_nums[DICE_ARRAY_SIZEcxx . . $ `rolldice.h:45:9`!] = {};
147 static char *line = (char *)NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
148
149 line = readlinecxx . . $ readline(a8b8e32347182212).("");
150 while(line){
151 int res_int = parse_stringcxx . . $ parse_string(b0a0d720b596a511).( line, dice_nums );
152 if ( res_int ) {
153 return EX_DATAERRcxx . . $ `<external>/5948936369402635282/sysexits.h:97:9`!;
154 }
155 freecxx . . $ free(5d22bdacc48458e8).(line);
156 print_rollscxx . . $ print_rolls(11b0e290e57a7e53).(dice_nums);
157
158 line = (char *)NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
159 line = readlinecxx . . $ readline(a8b8e32347182212).("");
160 }
161 return EX_OKcxx . . $ `<external>/5948936369402635282/sysexits.h:92:9`!;
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_argscxx . . $ roll_from_args(b90f8021b121110e).(char **argv){
170 int dice_nums[DICE_ARRAY_SIZEcxx . . $ `rolldice.h:45:9`!] = {};
171 int index;
172
173 for(index = optindcxx . . $ optind.; argv[index] != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; index++) {
174 int res_int = parse_stringcxx . . $ parse_string(b0a0d720b596a511).( argv[index], dice_nums );
175 if ( res_int ) {
176 return EX_DATAERRcxx . . $ `<external>/5948936369402635282/sysexits.h:97:9`!;
177 }
178 print_rollscxx . . $ print_rolls(11b0e290e57a7e53).(dice_nums);
179 }
180 return EX_OKcxx . . $ `<external>/5948936369402635282/sysexits.h:92:9`!;
181 }
182
183
184 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) {
185 int c;
186 int interactive = 0;
187
188 while((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "hvrusi", long_optscxx . . $ long_opts., NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)) != -1) {
189 switch(c) {
190 case 'h':
191 print_usagecxx . . $ print_usage(d4f767463ce0a6b3).(EX_OKcxx . . $ `<external>/5948936369402635282/sysexits.h:92:9`!); break;
192 case 'v':
193 print_versioncxx . . $ print_version(d878ad36e69247c7).(); break;
194 case 'r':
195 if(rand_filecxx . . $ rand_file. == URANDOMcxx . . $ URANDOM.) {
196 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Choose either '-r' or '-u', please.\n");
197 return EX_USAGEcxx . . $ `<external>/5948936369402635282/sysexits.h:96:9`!;
198 }
199 rand_filecxx . . $ rand_file. = RANDOMcxx . . $ RANDOM.; break;
200 case 'u':
201 if(rand_filecxx . . $ rand_file. == RANDOMcxx . . $ RANDOM.) {
202 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Choose either '-r' or '-u', please.\n");
203 return EX_USAGEcxx . . $ `<external>/5948936369402635282/sysexits.h:96:9`!;
204 }
205 rand_filecxx . . $ rand_file. = URANDOMcxx . . $ URANDOM.; break;
206 case 's':
207 print_separatecxx . . $ print_separate. = 1; break;
208 case 'i':
209 interactive = 1; break;
210 }
211 }
212
213 init_randomcxx . . $ init_random(bc2330f64d909df9).(rand_filecxx . . $ rand_file.);
214
215 if (interactive) {
216 roll_from_interactivecxx . . $ roll_from_interactive(d878ad36e69247c7).();
217 return EX_OKcxx . . $ `<external>/5948936369402635282/sysexits.h:92:9`!;
218 }
219 else if ( optindcxx . . $ optind. == argc ) {
220 return roll_from_stdincxx . . $ roll_from_stdin(d1b40a211a1a4e4c).();
221 }
222 else {
223 return roll_from_argscxx . . $ roll_from_args(b90f8021b121110e).(argv);
224 }
225 }