Tapify output of cheat.
[open-adventure.git] / cheat.c
1 /*
2  * 'cheat' is a tool for generating save game files to test states that ought
3  * not happen. It leverages chunks of advent, mostly initialize() and
4  * savefile(), so we know we're always outputting save files that advent
5  * can import.
6  *
7  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
8  * Copyright (c) 2017 by Eric S. Raymond
9  * SPDX-License-Identifier: BSD-2-clause
10  */
11 #include <getopt.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <editline/readline.h>
16 #include "advent.h"
17
18 int main(int argc, char *argv[])
19 {
20     int ch;
21     char *savefilename = NULL;
22     int version = 0;
23     FILE *fp = NULL;
24     char *tapmessage = NULL;
25
26     // Initialize game variables
27     initialise();
28
29     /* we're generating a saved game, so saved once by default,
30      * unless overridden with command-line options below.
31      */
32     game.saved = 1;
33
34     /*  Options. */
35     const char* opts = "d:l:s:t:v:o:m:";
36     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n"
37                         "        -d number of deaths. Signed integer.\n"
38                         "        -l lifetime of lamp in turns. Signed integer.\n"
39                         "        -s number of saves. Signed integer.\n"
40                         "        -t number of turns. Signed integer.\n"
41                         "        -v version number of save format.\n"
42                         "        -o required. File name of save game to write.\n"
43                         "        -m specify message for YAP output\n";
44
45     while ((ch = getopt(argc, argv, opts)) != EOF) {
46         switch (ch) {
47         case 'd':
48             game.numdie = (turn_t)atoi(optarg);
49             printf("cheat: game.numdie = %d\n", game.numdie);
50             break;
51         case 'l':
52             game.limit = (turn_t)atoi(optarg);
53             printf("cheat: game.limit = %d\n", game.limit);
54             break;
55         case 's':
56             game.saved = (int)atoi(optarg);
57             printf("cheat: game.saved = %d\n", game.saved);
58             break;
59         case 't':
60             game.turns = (turn_t)atoi(optarg);
61             printf("cheat: game.turns = %d\n", game.turns);
62             break;
63         case 'v':
64             version = atoi(optarg);
65             printf("cheat: version = %d\n", version);
66             break;
67         case 'o':
68             savefilename = optarg;
69             break;
70         case 'm':
71             tapmessage = optarg;
72             break;
73         default:
74             fprintf(stderr,
75                     usage, argv[0]);
76             exit(EXIT_FAILURE);
77             break;
78         }
79     }
80
81     // Save filename required; the point of cheat is to generate save file
82     if (savefilename == NULL) {
83         if (tapmessage != NULL) {
84             printf("not ok - %s: filename required\n", tapmessage);
85         } else {
86             printf("not ok - filename required\n");
87             exit(EXIT_FAILURE);
88         }
89     }
90
91     fp = fopen(savefilename, WRITE_MODE);
92     if (fp == NULL) {
93         if (tapmessage != NULL) {
94             printf("not ok - %s: can't open file %s. Exiting.\n", tapmessage, savefilename);
95         } else {
96             printf("not ok - can't open file %s.\n", savefilename);
97             exit(EXIT_FAILURE);
98         }
99     }
100
101     savefile(fp, version);
102
103     fclose(fp);
104
105     if (tapmessage != NULL) {
106         printf("ok - %s\n", tapmessage);
107     } else {
108         printf("cheat: %s created.\n", savefilename);
109     }
110     return EXIT_SUCCESS;
111 }
112
113 // LCOV_EXCL_START
114 /*
115  * Ugh...unused, but required for linkage.
116  * See the actually useful version of this in main.c
117  */
118
119 char *myreadline(const char *prompt)
120 {
121     return readline(prompt);
122 }
123 // LCOV_EXCL_STOP
124
125 /* end */
126
127