Module Name:    src
Committed By:   wiz
Date:           Thu Apr 22 14:57:36 UTC 2021

Modified Files:
        src/games/cgram: cgram.6 cgram.c

Log Message:
cgram: allow providing an input file instead of the random fortune


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/games/cgram/cgram.6
cvs rdiff -u -r1.17 -r1.18 src/games/cgram/cgram.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/cgram/cgram.6
diff -u src/games/cgram/cgram.6:1.2 src/games/cgram/cgram.6:1.3
--- src/games/cgram/cgram.6:1.2	Sun Aug  4 07:55:09 2013
+++ src/games/cgram/cgram.6	Thu Apr 22 14:57:36 2021
@@ -1,4 +1,4 @@
-.\" $NetBSD: cgram.6,v 1.2 2013/08/04 07:55:09 wiz Exp $
+.\" $NetBSD: cgram.6,v 1.3 2021/04/22 14:57:36 wiz Exp $
 .\"
 .\" Copyright (c) 2004, 2013 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,21 +27,24 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd August 3, 2013
+.Dd April 22, 2021
 .Dt CGRAM 6
 .Os
 .Sh NAME
 .Nm cgram
 .Nd solve Sunday-paper cryptograms
 .Sh SYNOPSIS
-.Nm
+.Nm Op Ar file
 .Sh DESCRIPTION
 .Nm
 is a curses-based widget for solving Sunday-paper-type cryptograms
 based on substitution ciphers.
-A random cleartext is chosen using
-.Xr fortune 6
-and a random substitution key is generated.
+If
+.Ar file
+is given, it is used as cleartext, otherwise a random cleartext is
+chosen using
+.Xr fortune 6 ;
+either way, a random substitution key is generated.
 .Pp
 The ciphertext is displayed.
 Typing a letter changes the key so that the letter under the cursor

Index: src/games/cgram/cgram.c
diff -u src/games/cgram/cgram.c:1.17 src/games/cgram/cgram.c:1.18
--- src/games/cgram/cgram.c:1.17	Fri Feb 26 15:18:40 2021
+++ src/games/cgram/cgram.c	Thu Apr 22 14:57:36 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: cgram.c,v 1.17 2021/02/26 15:18:40 rillig Exp $ */
+/* $NetBSD: cgram.c,v 1.18 2021/04/22 14:57:36 wiz 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.18 2021/04/22 14:57:36 wiz Exp $");
 #endif
 
 #include <assert.h>
@@ -216,13 +216,7 @@ char_at_cursor(void)
 	return lines.v[cursor_y].s[cursor_x];
 }
 
-static void
-readquote(void)
-{
-	FILE *f = popen(_PATH_FORTUNE, "r");
-	if (f == NULL)
-		err(1, "%s", _PATH_FORTUNE);
-
+static void getquote(FILE *f) {
 	struct string line;
 	string_init(&line);
 
@@ -249,6 +243,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)
+		exit(1);
+}
+
+
+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 +553,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) {
+	    readfile(filename);
+	} else {
+	    readquote();
+	}
 	encode();
 
 	initscr();
@@ -573,9 +595,9 @@ clean_up(void)
 ////////////////////////////////////////////////////////////
 
 int
-main(void)
+main(int argc, char *argv[])
 {
-	init();
+	init(argc > 1 ? argv[1] : NULL);
 	loop();
 	clean_up();
 }

Reply via email to