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