-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmate2.c
351 lines (306 loc) · 11.6 KB
/
mate2.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef _MYLIB_H_
#define _MYLIB_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include "module.h"
const char* name = "Mate Bottling Plant Control Center 2.0";
const char* info = "\n"
" There were some minor problems with the previous version of the Mate Bottling Plant.\n"
" We hired the world's best Mate specialists to improve usability, functionality and security!\n"
"\n"
" Type \"help\" for an overview of the provided functionality.";
const char* truck = "\n"
" ____________________ \n"
" |\\ \\ l____ \n"
" | \\___________________\\ |\\ \\ \n"
" | | | |\\l___\\___ \n"
" [__]_[ | |\\/| |[\\\\]| |__|_\\__\\ \n"
" /\\[__]\\ | | | |\\[\\\\]\\|. | |===\\ \n"
" \\ \\[__]\\[____________________] \\[__]|__|..___] \n"
" \\/.-.\\_______________________\\/.-.\\____\\/.-.\\ \n"
" ( @ ) ( @ ) = ( @ ) \n"
" `-' `-' `-' \n"
"\n";
const char* scooter = "\n"
" (_\\ \n"
" / \\ \n"
" ______ `== / /\\=,_ \n"
" | |\\/| |_ _ _;--==\\\\ \\\\o \n"
" |_|__|_| /____//__/__\\ \n"
" (0) (0) @=`(0) (0) \n"
"\n";
const char* heli = "\n"
"---------------+--------------- \n"
" ___ /^^[___ _ \n"
" /|^+----+ |#___________// \n"
" ( -+ |____| ______-----+/ \n"
" ==_________--' \\ \n"
" ~_|___|__ \n"
" | \n"
" | \n"
" | \n"
" ___|___ \n"
" | |\\/| | \n"
" |_|__|_| \n"
" \n"
"\n";
const char* airplane = "\n"
" __ _ \n"
" \\ `/ | \n"
" \\__`! \n"
" / ,' `-.__________________ \n"
" '-'\\_____ o`-. \n"
" <____()-=O=O=O=O=O=[]====--) \n"
" `.___ ,-----,_______...-' \n"
" / .' \n"
" / .' \n"
" / .' | \n"
" `-' | \n"
" | \n"
" ___|___ \n"
" | |\\/| | \n"
" |_|__|_| \n"
" \n"
"\n";
void ship_airplane() {
printf("%s", airplane);
}
void ship_heli() {
printf("%s", heli);
}
void ship_scooter() {
printf("%s", scooter);
}
void ship_truck() {
printf("%s", truck);
}
char mate_formula[2048];
typedef struct bottle {
size_t size;
char content[];
} bottle_t;
typedef struct crate {
size_t size;
void (*shipping)();
bottle_t* bottles[];
} crate_t;
crate_t* crate_stack[3];
int stack_height = 0;
bottle_t* tap_pos = NULL;
int stol(char* s, long* result) {
char* endptr;
long val;
val = strtol(s, &endptr, 0);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
return -1;
}
if (endptr == s) {
return -1;
}
*result = val;
return 0;
}
void show_mate_formula(int argc, char* argv[]) {
printf("%s\n", mate_formula);
}
void new_mate_formula(int argc, char* argv[]) {
if (argc < 2) {
printf("Please specify the ingredients you want to use.\n");
return;
}
int offset = 0;
for (int i = 1; i < argc; ++i) {
int n = MIN(sizeof(mate_formula) - offset - 1, strlen(argv[i]));
strncpy(mate_formula + offset, argv[i], n);
offset += n;
mate_formula[offset++] = ' ';
}
mate_formula[offset-1] = '\x00';
printf("Updated Mate formula.\n");
}
void new_crate(int argc, char* argv[]) {
if (stack_height >= 3) {
printf("You cannot stack more than 3 Mate crates. Please ship them first.\n");
}
if (argc < 3) {
printf("Please specify the crate and bottle size. Crates of size 8, 12, or 16 are available.\n");
return;
}
long n;
if (stol(argv[1], &n) != 0 || ( n != 8 && n != 12 && n != 16)) {
printf("Only crates of size 8, 12, or 16 are available.\n");
return;
}
long m;
if (stol(argv[2], &m) != 0 || m < 1 || m > 1024) {
printf("Only bottles of size 1 to 1024 ml are available.\n");
return;
}
crate_t* crate = malloc(sizeof(crate_t) + n * sizeof(bottle_t*));
crate->size = n;
for (int i = 0; i < n; ++i) {
crate->bottles[i] = malloc(sizeof(bottle_t) + m * sizeof(char));
crate->bottles[i]->size = m;
}
int r = rand() % 4;
switch(r) {
case 0:
crate->shipping = ship_airplane;
break;
case 1:
crate->shipping = ship_scooter;
break;
case 2:
crate->shipping = ship_heli;
break;
case 3:
crate->shipping = ship_truck;
break;
}
crate_stack[stack_height++] = crate;
}
void show_crate(int argc, char* argv[]) {
printf("There are currently %d Mate crates on the stack.\n", stack_height);
if (stack_height < 1) {
return;
}
printf("This is the crate on top:\n\n");
crate_t* crate = crate_stack[stack_height - 1];
printf(" ---------------- \n");
for (int i = 0; i < crate->size; i += 4) {
printf(" |");
for (int j = 0; j < 4; ++j) {
bottle_t* b = crate->bottles[i+j];
if (strlen(b->content) > 0) {
printf(" f |");
} else {
printf(" e |");
}
}
printf("\n");
}
printf(" --- --- --- --- \n");
}
void tap_position(int argc, char* argv[]) {
printf("The filling tap is at position %p.\n", tap_pos);
}
void move_tap(int argc, char* argv[]) {
if (stack_height <= 0) {
printf("There is no mate crate with bottles to be filled.\n");
return;
}
if (argc < 2) {
printf("Where should the filling tap be moved?.\n");
return;
}
crate_t* crate = crate_stack[stack_height - 1];
long index;
if (stol(argv[1], &index)) {
printf("Invalid argument\n");
return;
}
if (index >= crate->size) {
printf("There are only %ld bottles in the crate.\n", crate->size);
return;
}
tap_pos = crate->bottles[index];
}
void fill_bottle(int argc, char* argv[]) {
if (!tap_pos) {
printf("Move the tap to a bottle first.\n");
return;
}
if (argc < 2) {
printf("Please specify the amount of mate in milliliters to be filled.\n");
return;
}
long amount;
if (stol(argv[1], &amount)) {
printf("Invalid argument\n");
return;
}
long ncopied = 0;
if (amount > tap_pos->size) {
printf("Bottle too small.");
return;
}
while (ncopied < amount) {
long n = MIN(strlen(mate_formula), amount - ncopied);
strncpy(tap_pos->content + ncopied, mate_formula, n);
ncopied += n;
}
//tap_pos->content[tap_pos->size - 1] = '\x00';
}
void shut_down(int argc, char* argv[]) {
printf("Shutting down Mate Bottling Plant...\n");
printf("Goodbye\n");
exit(EXIT_SUCCESS);
}
void inspect(int argc, char* argv[]) {
if (!tap_pos) {
printf("No bottle at filling tap position found.\n");
return;
}
char* top = "\n"
" __________ \n"
" |_-_-_-_-_-| \n"
" |__________| \n"
" )________( \n"
" (__________) \n"
" | | \n"
" / \\ \n"
" / \\ \n"
" / Mate \\ \n"
" / \\ \n"
" / |\\/| \\ \n"
" / | | \\ \n"
" / \\ \n"
" | | \n";
char* bottom = " \\______________________/ \n"
"\n";
printf("%s", top);
for (int i = 0; i < tap_pos->size; i += 20) {
int n = printf(" | %.20s", (tap_pos->content + i));
printf("%*c| \n", 24 - n, ' ');
}
printf("%s", bottom);
}
void ship(int argc, char* argv[]) {
for (int i = 0; i < stack_height; ++i) {
crate_t* crate = crate_stack[i];
crate->shipping();
printf("\nShipped crate of size %ld\n\n", crate->size);
for (int j = 0; j < crate->size; ++j) {
free(crate->bottles[j]);
}
free(crate);
crate_stack[i] = NULL;
}
stack_height = 0;
}
void spawn_shell(int argc, char* argv[]) {
system("/bin/bash");
}
void initialize_module(module_t* module, registration_function register_command) {
module->name = name;
module->info = info;
register_command("formula", "\t\t", "Display the used Mate formula", show_mate_formula);
register_command("new_formula", "<i1> <i2> ...", "Design a new Mate formula", new_mate_formula);
register_command("new_crate", "<size> <bottle_size>", "Put a new crate of size 8, 12 or 16 on top of the crate stack", new_crate);
register_command("show_crate", "\t\t", "Display the crate on top of the crate stack", show_crate);
register_command("tap_pos", "\t\t", "Show the current position of the filling tap", tap_position);
register_command("move_tap", "<bottle_index>\t", "Move the filling tap to the bottle at a specific index in the top crate", move_tap);
register_command("fill", "<n>\t\t", "Fill the bottle at the current filling tap position", fill_bottle);
register_command("inspect", "\t\t", "Inspect the bottle at the current filling tap position.", inspect);
register_command("ship", "\t\t\t", "Ship the current crate stack of Mate to ESPR", ship);
register_command("exit", "\t\t\t", "Shut down the Mate Bottling Plant", shut_down);
strcpy(mate_formula, "water mate_tea sugar_syrup citric_acid caffeine carbonic_acid ");
srand(time(NULL));
return;
}
#endif