Simplify the signature of savefile().
[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  * SPDX-FileCopyrightText: 1977, 2005 by Will Crowther and Don Woods
8  * SPDX-FileCopyrightText: 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     FILE *fp = NULL;
23
24     // Initialize game variables
25     initialise();
26
27     /* we're generating a saved game, so saved once by default,
28      * unless overridden with command-line options below.
29      */
30     game.saved = 1;
31
32     /*  Options. */
33     const char* opts = "d:l:s:t:v:o:";
34     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n"
35                         "        -d number of deaths. Signed integer.\n"
36                         "        -l lifetime of lamp in turns. Signed integer.\n"
37                         "        -s number of saves. Signed integer.\n"
38                         "        -t number of turns. Signed integer.\n"
39                         "        -v version number of save format.\n"
40                         "        -o required. File name of save game to write.\n";
41
42     while ((ch = getopt(argc, argv, opts)) != EOF) {
43         switch (ch) {
44         case 'd':
45             game.numdie = (turn_t)atoi(optarg);
46             printf("cheat: game.numdie = %d\n", game.numdie);
47             break;
48         case 'l':
49             game.limit = (turn_t)atoi(optarg);
50             printf("cheat: game.limit = %d\n", game.limit);
51             break;
52         case 's':
53             game.saved = (int)atoi(optarg);
54             printf("cheat: game.saved = %d\n", game.saved);
55             break;
56         case 't':
57             game.turns = (turn_t)atoi(optarg);
58             printf("cheat: game.turns = %d\n", game.turns);
59             break;
60         case 'v':
61             save.version = atoi(optarg);
62             printf("cheat: version = %d\n", save.version);
63             break;
64         case 'o':
65             savefilename = optarg;
66             break;
67         default:
68             fprintf(stderr,
69                     usage, argv[0]);
70             exit(EXIT_FAILURE);
71             break;
72         }
73     }
74
75     // Save filename required; the point of cheat is to generate save file
76     if (savefilename == NULL) {
77         fprintf(stderr,
78                 usage, argv[0]);
79         fprintf(stderr,
80                 "ERROR: filename required\n");
81         exit(EXIT_FAILURE);
82     }
83
84     fp = fopen(savefilename, WRITE_MODE);
85     if (fp == NULL) {
86         fprintf(stderr,
87                 "Can't open file %s. Exiting.\n", savefilename);
88         exit(EXIT_FAILURE);
89     }
90
91     savefile(fp);
92
93     fclose(fp);
94
95     printf("cheat: %s created.\n", savefilename);
96
97     return EXIT_SUCCESS;
98 }
99
100 // LCOV_EXCL_START
101 /*
102  * Ugh...unused, but required for linkage.
103  * See the actually useful version of this in main.c
104  */
105
106 char *myreadline(const char *prompt)
107 {
108     return readline(prompt);
109 }
110 // LCOV_EXCL_STOP
111
112 /* end */
113
114