1+ /*
2+ * Copyright 2009-2021 Peter Kosyh <p.kosyh at gmail.com>
3+ *
4+ * Permission is hereby granted, free of charge, to any person
5+ * obtaining a copy of this software and associated documentation files
6+ * (the "Software"), to deal in the Software without restriction,
7+ * including without limitation the rights to use, copy, modify, merge,
8+ * publish, distribute, sublicense, and/or sell copies of the Software,
9+ * and to permit persons to whom the Software is furnished to do so,
10+ * subject to the following conditions:
11+ *
12+ * The above copyright notice and this permission notice shall be
13+ * included in all copies or substantial portions of the Software.
14+ *
15+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+ *
23+ */
24+
125#include <stdio.h>
226#include <stdlib.h>
327#include <string.h>
@@ -15,6 +39,7 @@ static int opt_log = 0;
1539static int opt_debug = 0 ;
1640static int opt_width = WIDTH ;
1741static char * opt_autoload = NULL ;
42+ static int opt_autosave = 1 ;
1843
1944static int need_restart = 0 ;
2045static int need_load = 0 ;
@@ -170,6 +195,7 @@ int main(int argc, const char **argv)
170195{
171196 int rc , i ;
172197 char * str ;
198+ FILE * log_file = NULL ;
173199 const char * game = NULL ;
174200 char cmd [256 + 64 ];
175201
@@ -180,11 +206,14 @@ int main(int argc, const char **argv)
180206 for (i = 1 ; i < argc ; i ++ ) {
181207 if (!strncmp (argv [i ], "-l" , 2 )) {
182208 opt_log = 1 ;
183- reopen_stderr (argv [i ] + 2 );
209+ log_file = fopen (argv [i ] + 2 , "wb" );
184210#ifdef _WIN32
185211 } else if (!strncmp (argv [i ], "-cp" , 3 )) {
186212 opt_codepage = atoi (argv [i ] + 3 );
187213#endif
214+ } else if (!strcmp (argv [i ], "-a" )) {
215+ opt_autoload = strdup ("autosave" );
216+ opt_autosave = 1 ;
188217 } else if (!strncmp (argv [i ], "-d" , 2 )) {
189218 opt_debug = 1 ;
190219 reopen_stderr (argv [i ] + 2 );
@@ -204,6 +233,7 @@ int main(int argc, const char **argv)
204233 SetConsoleCP (opt_codepage );
205234#endif
206235 if (!game ) {
236+ printf ("instead-cli %s (by Peter Kosyh '2021)\n" , VERSION );
207237 fprintf (stdout , "Usage: %s [-d<file>] [-w<width>] [-i<script>] [-l<log>] <game>\n" , argv [0 ]);
208238 exit (1 );
209239 }
@@ -294,8 +324,12 @@ int main(int argc, const char **argv)
294324 }
295325 if (!parser_mode && !cmd_mode )
296326 footer ();
297- if (opt_log )
298- fprintf (stderr , "%s\n" , p );
327+ if (opt_log ) {
328+ if (log_file )
329+ fprintf (log_file , "%s\n" , p );
330+ else
331+ fprintf (stderr , "%s\n" , p );
332+ }
299333 if (need_restart ) {
300334 instead_done ();
301335 goto restart ;
@@ -331,7 +365,10 @@ int main(int argc, const char **argv)
331365 }
332366 }
333367 }
334- free (instead_cmd ("save autosave" , NULL ));
368+ if (opt_autosave )
369+ free (instead_cmd ("save autosave" , NULL ));
335370 instead_done ();
371+ if (log_file )
372+ fclose (log_file );
336373 exit (0 );
337374}
0 commit comments