The '!= 0' is needed for an upcoming fix to lint's strict bool mode.
The additional text in the error messages is necessary because errx only
outputs the given string, with no details from errno. Since ferror does
not set errno, no such details are available, so just output a generic
message, which is still better than just "caesar: <stdout>".
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.
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.
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
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.
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.
Eliminate some frequently occurring subexpressions during substitution.
No functional change, assuming that fortune(6) always spits out less
than 2 gigacharacters of text.
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.