]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - cgram/cgram.c
cgram: conform to lint's strict bool mode, KNF
[bsdgames-darwin.git] / cgram / cgram.c
index 3c4ca952d065db8cf0fa045b186a5ea6107e184e..77c56a545cdc2307a8a22673f4bcc906b0343c82 100644 (file)
@@ -1,4 +1,4 @@
-/* $NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $ */
+/* $NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $ */
 
 /*-
  * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $");
+__RCSID("$NetBSD: cgram.c,v 1.19 2021/04/25 20:14:29 rillig Exp $");
 #endif
 
 #include <assert.h>
@@ -217,12 +217,8 @@ char_at_cursor(void)
 }
 
 static void
-readquote(void)
+getquote(FILE *f)
 {
-       FILE *f = popen(_PATH_FORTUNE, "r");
-       if (f == NULL)
-               err(1, "%s", _PATH_FORTUNE);
-
        struct string line;
        string_init(&line);
 
@@ -249,6 +245,30 @@ readquote(void)
        extent_y = (int)lines.num;
        for (int i = 0; i < extent_y; i++)
                extent_x = imax(extent_x, (int)lines.v[i].len);
+}
+
+static void
+readfile(const char *name)
+{
+       FILE *f = fopen(name, "r");
+       if (f == NULL)
+               err(1, "%s", name);
+
+       getquote(f);
+
+       if (fclose(f) != 0)
+               err(1, "%s", name);
+}
+
+
+static void
+readquote(void)
+{
+       FILE *f = popen(_PATH_FORTUNE, "r");
+       if (f == NULL)
+               err(1, "%s", _PATH_FORTUNE);
+
+       getquote(f);
 
        if (pclose(f) != 0)
                exit(1); /* error message must come from child process */
@@ -535,12 +555,16 @@ handle_key(void)
 }
 
 static void
-init(void)
+init(const char *filename)
 {
        stringarray_init(&lines);
        stringarray_init(&sollines);
        srandom((unsigned int)time(NULL));
-       readquote();
+       if (filename != NULL) {
+           readfile(filename);
+       } else {
+           readquote();
+       }
        encode();
 
        initscr();
@@ -573,9 +597,9 @@ clean_up(void)
 ////////////////////////////////////////////////////////////
 
 int
-main(void)
+main(int argc, char *argv[])
 {
-       init();
+       init(argc > 1 ? argv[1] : NULL);
        loop();
        clean_up();
 }