Commit Graph

24 Commits

Author SHA1 Message Date
rillig
6a70979a86 cgram: rename local functions
The word 'cleanup' should have been named clean_up all along, but 'done'
is even shorter.

No functional change.
2021-05-01 20:29:23 +00:00
rillig
c9b39ce704 cgram: in hinting mode, show all correct characters in bold face
For almost solved cryptograms, this makes the punctuation stand out
less, helping to focus on the missing letters.
2021-04-29 20:17:20 +00:00
rillig
3e4a0edf9f cgram: use ASCII-only implementation of <ctype.h> functions
The function 'encode' already assumes that all letter characters are
contiguous and that there are only 26 letters of each case.  At the
moment, cgram cannot handle UTF-8 anyway since it reads the input
byte-wise, assuming that each byte is exacty one character.
2021-04-25 20:38:03 +00:00
rillig
0721cc8d68 cgram: provide usage on incorrect invocation 2021-04-25 20:19:19 +00:00
rillig
1f36a02f91 cgram: conform to lint's strict bool mode, KNF 2021-04-25 20:14:29 +00:00
wiz
69305a0d1c cgram: allow providing an input file instead of the random fortune 2021-04-22 14:57:36 +00:00
rillig
32eb951e74 cgram: don't beep if the window is resized 2021-02-26 15:18:40 +00:00
rillig
550a2d894f cgram: place the 'solved' marker nearer to the text
This way, it cannot be overlooked as easily as before.  It also doesn't
change the position of the keyboard help text anymore.  Only if there is
not enough space, the 'solved' marker is put into the bottom line.
2021-02-26 15:15:34 +00:00
rillig
531f30a38a cgram: make the 'solved' stand out more
Suggested by Weitian LI via [1].

[1] https://github.com/NetBSD/src/commit/efec6410b2b5a736
2021-02-26 14:55:37 +00:00
rillig
6ec38e5b23 cgram: fix assertion when substituting at the end of the longest line
Reported by Weitian LI via GitHub.

Contrary to the patch suggested in [1], still allow the cursor to be
placed to the very right of the text in a line since that is the usual
behavior of text editors.

Split the function substitute() into two parts:  one that handles the
curses-specific part of checking whether a substitution is possible at
the current cursor position, and one that performs the actual
substitution.  Only the latter is kept in the code section for the
string manipulation functions, the other is moved to the section for
curses code.  Having all the curses code in one place reduces the places
that call beep().  Previously, as well as now, there is a single beep
per invalid key before, but that was not obvious from the previous code.

[1]: https://github.com/DragonFlyBSD/DragonFlyBSD/commit/18d09f18cf4c
2021-02-22 19:34:07 +00:00
rillig
f7c85ecb67 cgram: add advanced cursor movement with tab, shift+tab, return 2021-02-22 17:36:42 +00:00
rillig
47c6585f93 cgram: properly handle input errors
On both NetBSD and Cygwin, a missing /usr/bin/fortune would previously
continue since popen does not return an error (as /bin/sh is found and
can be executed), so the next chance to catch an error is pclose.  At
that point, the shell has already printed an informative error message
about what happened (or what didn't happen), so that cgram does not need
to print an error by itself.
2021-02-22 16:28:20 +00:00
rillig
9510ff87da cgram: allow navigation with KEY_PPAGE and KEY_NPAGE 2021-02-21 22:21:56 +00:00
rillig
1646b19068 cgram: rewrite completely, fixing bugs and style
Fixed bugs:

Do not consider the puzzle solved if all letters in the visible area are
substituted correctly.  To be properly solved, the whole puzzle must be
solved, even those parts that are currently off-screen.

Never place the cursor at the very right edge of the screen since that
does not work well with some terminals.  The maximum valid x coordinate
is COLS - 1.

Add horizontal scrolling.  Make all coordinate handling symmetric in
regard to the horizontal and vertical axes.  Previously, lines longer
than 80 characters could not be seen on the screen.

Improvements:

Remove the arbitrary limit of 128 characters per line.  Even if
fortune(6) may never generate such long lines, the code is easy enough
to adapt to other sources.

Properly clean up the allocated memory.  Previously, only the string
arrays were freed but not the strings themselves.

Stylistic:

Add RCS ID.

Fix ctype functions in lint's strict bool mode.

Avoid excessive calls to strlen whenever the cursor moves.  Given that
the whole screen is redrawn every time a key is pressed, this is an
unnecessary optimization, but the code smelled nevertheless.
2021-02-21 20:33:42 +00:00
rillig
12c09445ec cgram: WARNS=6, use int for all coordinates
Eliminate some frequently occurring subexpressions during substitution.

No functional change, assuming that fortune(6) always spits out less
than 2 gigacharacters of text.
2021-02-21 17:16:00 +00:00
rillig
f21d40b5e2 cgram: consistently use char for characters
Having to convert back and forth between char, unsigned char and int is
confusing.  Just stay with char, until the support for wide characters
is added.

No functional change.
2021-02-21 16:50:57 +00:00
rillig
c414f53223 cgram: fix undefined behavior when pressing function keys
The key codes for function keys are outside of the "range representable
by an unsigned char".  This resulted in parts of the screen being
cleaned.
2021-02-21 16:37:26 +00:00
rillig
905a01e770 cgram: adjust style to survive lint's strict bool mode
The biggest change is the return type of substitute().  Before, 0 meant
success, now false means failure.

No functional change.
2021-02-21 16:05:44 +00:00
rillig
43892d8404 cgram: sort includes 2021-02-21 15:42:21 +00:00
rillig
b15c3153c9 cgram: indent
With manual corrections afterwards:

- indent removes empty lines between statement and declaration.  This
  had been a syntax error in C90, since C99 this is common style and
  should be accepted by indent.

- indent didn't format the first line of main for whatever reason, did
  that manually.

- indent removed the empty line above the '/////' separators.  It should
  have left these empty lines as-is.
2021-02-21 15:41:04 +00:00
rillig
0aae6846c3 cgram: fix Home key for cursor navigation
Reported by liweitianux via GitHub.
2021-02-21 13:35:37 +00:00
rillig
e8699354fb cgram(6): use standard cursor keys, use standard shuffle algorithm
The previous shuffle algorithm asked for 100 random numbers, on average.
The new algorithm asks exactly for 26 random numbers.

Curses predefines numeric constants for keys, and there is no apparent
reason not to use these standard keys for cursor movement.
2020-07-02 19:11:01 +00:00
wiz
2077b57a6f Add RCS Id. 2013-08-04 07:55:09 +00:00
dholland
0e7c031ea9 Add a curses gizmo for solving Sunday-paper-type cryptograms based on
substitution ciphers. It gets the cleartext from fortune. I wrote this
some years ago for my own amusement; a couple people have suggested
that I should import it.

Approved only by groo, so I'm going to wait a couple days to hook it
to the build in case anyone demands it be removed again...
2013-08-04 05:42:47 +00:00