Make 1992/westley whereami code scripts

It is not actually necessary to have a program that uses curses to get
the number of columns. One can use tput cols or stty size to get that.
Thus there is no need to have a C program that links in the curses
library.
This commit is contained in:
Cody Boone Ferguson committed 2023-11-16 04:12:41 -08:00
1 parent 6285bdee1a
commit 76efe07785
7 files changed
+147 -151

No files matched your search

-2
View File
@@ -1,6 +1,4 @@
westley
whereami
whereami.alt
westley.alt
westley.orig
prog.orig
+2 -8
View File
@@ -121,7 +121,7 @@ ALT_TARGET= ${PROG}.alt
# build the entry
#################
#
all: data ${TARGET} whereami whereami.alt
all: data ${TARGET}
@${TRUE}
.PHONY: all alt data everything diff_orig_prog diff_prog_orig \
@@ -133,12 +133,9 @@ ${PROG}: ${PROG}.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS}
@echo "NOTE: your terminal must be at least 80 columns wide for this to work right"
whereami: whereami.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} -lcurses
# alternative executable
#
alt: data ${ALT_TARGET} whereami.alt
alt: data ${ALT_TARGET}
@${TRUE}
${PROG}.alt: ${PROG}.alt.c
@@ -146,9 +143,6 @@ ${PROG}.alt: ${PROG}.alt.c
@echo "NOTE: your terminal must be at least 80 columns wide for this to work right"
whereami.alt: whereami.alt.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} -lcurses
# data files
#
data: ${DATA}
+42 -49
View File
@@ -7,42 +7,27 @@
#
# This script will first try compiling the code and alt code (alt = US only with
# higher resolution, based on the author's remarks) and then if all is OK it
# tries to make the whereami and whereami.alt programs. If that fails the
# programs used will be westley and westley.alt; otherwise the programs will be
# whereami and whereami.alt. One can pass CC=foo to set the compiler, defaulting
# to cc.
# will continue.
#
# The difference between whereami and westley is that the former first checks
# that the number of columns is at least 80. The programs whereami and
# whereami.alt can be deceived by way of doing:
# The whereami.sh and whereami.alt.sh scripts correspond to westley.c and
# westley.alt.c but they check that the number of columns is at least 80. If
# it's not at least 80 then it's an error. The scripts can be deceived by doing
# something like:
#
# COLUMNS=100 ./whereami lat long
# COLUMNS=100 ./whereami.sh lat long
#
# even if the number of columns is < 80 but there's nothing that can be done
# about that. This script does not have that problem, however, so it's nice to
# use for that purpose if nothing else.
# about that.
#
# If westley or westley.alt cannot compile then it is an error and the script
# will exit 4, corresponding to the same code that whereami.c and whereami.alt.c
# exit with if westley.c or westley.alt.c cannot be compiled.
# will exit.
#
# If whereami or whereami.alt cannot compile or link, say because libcurses is
# not installed, then it is not an error as such but westley and westley.alt
# will be used instead unless of course there exists an executable of one or
# both called whereami or whereami.alt or westley.c or westley.alt.c cannot be
# compiled. It is an error in the script if either of westley.c or westley.alt.c
# cannot be compiled.
#
# This is the best way to protect against showing poor output. We can't get the
# environmental variable COLUMNS from a program or a script so it has to be done
# this way. Note that in curses the COLUMNS is called COLS.
# If by chance westley.sh or westley.alt.sh are not executable files then it
# will try chmod +x on them and then try again. Otherwise it'll just use westley
# and westley.alt.
#
# This script knows which version, the entry or alt code, to use based on what
# city is being shown.
#
# If whereami and whereami.alt can be built it is run once, ahead of time, and
# if it exits non-zero then we exit to prevent repeatedly showing the error
# message. Otherwise we will call westley or westley.alt directly.
# city is being shown (based on the author's remarks).
#
# Usage: ./try.sh
#
@@ -56,72 +41,80 @@ if [[ -z "$CC" ]]; then
fi
make CC="$CC" westley westley.alt >/dev/null || exit 4
make CC="$CC" whereami whereami.alt >/dev/null || exit 4
# get the path to the right programs
if [[ -x "whereami" ]]; then
WESTLEY="whereami"
CC="$CC" ./"$WESTLEY" 0 0 >/dev/null || exit 1
if [[ -x "whereami.sh" ]]; then
WESTLEY="whereami.sh"
else
WESTLEY="westley"
fi
if [[ -x "whereami.alt" ]]; then
WESTLEY_ALT="whereami.alt"
CC="$CC" ./"$WESTLEY_ALT" 0 0 >/dev/null || exit 1
else
WESTLEY_ALT="westley.alt"
chmod +x whereami.sh
if [[ -x "whereami.sh" ]]; then
WESTLEY="whereami.sh"
else
WESTLEY="westley"
fi
fi
echo "New York:" 1>&2
if [[ -x "whereami.alt.sh" ]]; then
WESTLEY_ALT="whereami.alt.sh"
else
chmod +x whereami.alt.sh
if [[ -x "whereami.alt.sh" ]]; then
WESTLEY_ALT="whereami.alt.sh"
else
WESTLEY_ALT="westley.alt"
fi
fi
read -r -n 1 -p "Press any key to show New York: "
echo 1>&2
./"$WESTLEY_ALT" 41 -74
echo 1>&2
echo "London:" 1>&2
read -r -n 1 -p "Press any key to show London: "
echo 1>&2
./"$WESTLEY" 52 0
echo 1>&2
echo "Moscow:" 1>&2
read -r -n 1 -p "Press any key to show Moscow: "
echo 1>&2
./"$WESTLEY" 56 38
echo 1>&2
echo "New Delhi:" 1>&2
read -r -n 1 -p "Press any key to show New Delhi: "
echo 1>&2
./"$WESTLEY" 29 77
echo 1>&2
echo "Sydney:" 1>&2
read -r -n 1 -p "Press any key to show Sydney: "
echo 1>&2
./"$WESTLEY" -34 151
echo 1>&2
echo "Los Angeles:" 1>&2
read -r -n 1 -p "Press any key to show Los Angeles: "
echo 1>&2
./"$WESTLEY_ALT" 34 -118
echo 1>&2
echo "Paris:" 1>&2
read -r -n 1 -p "Press any key to show Paris: "
echo 1>&2
./"$WESTLEY" 45 2
echo 1>&2
echo "Rio de Janeiro:" 1>&2
read -r -n 1 -p "Press any key to show Rio de Janeiro: "
echo 1>&2
./"$WESTLEY" -23 -43
echo 1>&2
echo "Beijing:" 1>&2
read -r -n 1 -p "Press any key to show Beijing: "
echo 1>&2
./"$WESTLEY" 40 116
echo 1>&2
echo "Tokyo:" 1>&2
read -r -n 1 -p "Press any key to show Tokyo: "
echo 1>&2
./"$WESTLEY" 36 140
echo 1>&2
echo "Approximate judging location:" 1>&2
read -r -n 1 -p "Press any key to show approximate judging location: "
echo 1>&2
./"$WESTLEY_ALT" 37 -122
-79
View File
@@ -1,79 +0,0 @@
/*
* whereami.alt.c: wrapper program to westley.alt.c that checks first the
* columns of the terminal, exiting if < 80 and otherwise try compiling
* westley.alt.c (with make) and then if is already compiled or ends up being
* compiled it will be run with the specified args to this program.
*
* It is an error to not specify two args but it is not an error to specify args
* that are not numbers. The atoi(3) function typically returns 0 in that case
* which westley.alt.c uses.
*
* The reason to use this wrapper is because westley.alt.c needs at least 80
* columns to show the output right. Previously it required 80 column wrapping
* but in 2023 this limitation was removed, allowing it to work as long as there
* are at least 80 columns.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
int main(int argc, char **argv)
{
int cols;
char *cc = NULL; /* for if the user passes in CC=foo */
char *buf = NULL;
if (argc < 3)
exit(fprintf(stderr, "usage: %s <lat> <long>\n", argv[0]));
/* try and get compiler to use */
cc = getenv("CC");
if (!cc)
cc = "cc";
buf = calloc(1, strlen(cc) + strlen("CC=") + 1);
if (buf != NULL)
sprintf(buf, "CC=%s", cc);
else
buf = "CC=cc";
/* init curses so we can get the number of columns */
if (!initscr())
exit(2);
/* save columns */
cols = COLS;
/*
* end curses so that we can either report an error or compile and run
* westley.alt
*/
endwin();
if (cols < 80)
{
fprintf(stderr, "your terminal must be at\nleast 80 columns wide\nbut is only %d, sorry.\n", COLS);
exit(3);
}
/*
* compile westley.alt.c and run if successful
*/
buf = calloc(1, strlen("make CC=") + strlen(cc) + strlen("westley.alt") + strlen(">/dev/null") + 1);
if (buf)
sprintf(buf, "make CC=%s westley.alt >/dev/null", cc);
else
buf = "make westley.alt >/dev/null";
/* compile source file */
if (system(buf))
{
fprintf(stderr, "couldn't compile westley.alt.c, sorry.\n");
exit(4);
}
/* if we get here we compiled the code or it was already compiled so run it */
execl("./westley.alt", "./westley.alt", argv[1], argv[2], NULL);
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
#
# whereami.alt - a wrapper script to westley.alt.c that checks first the columns
# of the terminal, exiting if < 80 and otherwise try compiling westley.alt.c
# (with make) and then if is already compiled or ends up being compiled it will
# be run with the specified args to this program.
#
# It is an error to not specify two args but it is not an error to specify args
# that are not numbers. The atoi(3) function typically returns 0 in that case
# which westley.alt.c uses.
#
# The reason to use this wrapper is because westley.alt.c needs at least 80
# columns to show the output right. Previously it required 80 column wrapping
# but in 2023 this limitation was removed, allowing it to work as long as there
# are at least 80 columns.
# make sure there are at least two args
if [[ "$#" -lt 2 ]]; then
echo "usage: $0 lat long" 1>&2
exit 1
fi
# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it
# this way allows us to have the user specify a different compiler in an easy
# way.
if [[ -z "$CC" ]]; then
CC="cc"
fi
# try getting the columns the simple way first: tput cols
COLUMNS="$(tput cols)"
if [[ "$?" -ne 0 ]]; then
# if tput failed for any reason try stty(1) with awk(1):
COLUMNS="$(stty size|awkf '{print $NF}')"
fi
if [[ "$COLUMNS" -lt 80 ]]; then
echo "Your terminal must be at least 80" 1>&2
echo "columns wide echo but yours is only" 1>&2
echo "$COLUMNS, sorry." 1>&2
exit 2
fi
make CC="$CC" everything >/dev/null || exit 1
./westley.alt "$1" "$2"
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
#
# whereami - a wrapper script to westley.c that checks first the
# columns of the terminal, exiting if < 80 and otherwise try compiling
# westley.c (with make) and then if is already compiled or ends up being
# compiled it will be run with the specified args to this program.
#
# It is an error to not specify two args but it is not an error to specify args
# that are not numbers. The atoi(3) function typically returns 0 in that case
# which westley.c uses.
#
# The reason to use this wrapper is because westley.c needs at least 80 columns
# to show the output right. Previously it required 80 column wrapping but in
# 2023 this limitation was removed, allowing it to work as long as there are at
# least 80 columns.
# make sure there are at least two args
if [[ "$#" -lt 2 ]]; then
echo "usage: $0 lat long" 1>&2
exit 1
fi
# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it
# this way allows us to have the user specify a different compiler in an easy
# way.
if [[ -z "$CC" ]]; then
CC="cc"
fi
# try getting the columns the simple way first: tput cols
COLUMNS="$(tput cols)"
if [[ "$?" -ne 0 ]]; then
# if tput failed for any reason try stty(1) with awk(1):
COLUMNS="$(stty size|awkf '{print $NF}')"
fi
if [[ "$COLUMNS" -lt 80 ]]; then
echo "Your terminal must be at least 80" 1>&2
echo "columns wide echo but yours is only" 1>&2
echo "$COLUMNS, sorry." 1>&2
exit 1
fi
make CC="$CC" all >/dev/null || exit 1
./westley "$1" "$2"
+10 -13
View File
@@ -1627,22 +1627,19 @@ the final loop it prints another newline. This fix has another bonus in that
resizing the terminal after running it should not mess up the display either,
unless of course it becomes too small.
Cody added the programs [whereami.c](1992/westley/whereami.c) and
[whereami.alt](1992/westley/whereami.alt.c) which correspond to the entry and
the alt code but which, by way of curses, checks for the number of columns; if
all is good it will try compiling the program or alt program, respectively, and
then if successful run the program. Otherwise, if there are not enough columns
it is an error. It is always an error if the compilation of the program itself
(westley.c, westley.alt.c) fails.
Cody added the scripts [whereami.sh](1992/westley/whereami.sh) and
[whereami.alt.sh](1992/westley/whereami.alt.sh) which correspond to the entry and
the alt code but first check that the number of columns is at least 80 and if
not it is an error.
Cody added the [try.sh](1992/westley/try.sh) script that shows the different
cities that the author recommended one try as well as the one recommended by the
judges (approximate judging location), labelling each city and printing a
newline before the next city. The try.sh script uses the `whereami` code, if it
can be compiled and linked, but otherwise it uses `westley` code instead, either
the entry or alt code. The try.sh cannot be deceived by way of `COLUMNS=81
./try.sh` but the `whereami`/`whereami.alt` code can be deceived if directly
called. This is a feature, not a bug: or maybe a limitation of curses.
newline before the next city. The try.sh script uses the `whereami.sh` and
`whereami.alt.sh` scripts, as long as they're executable (if they're not it
first tries to make it executable and if it fails to do so it just uses
`westley` or `westley.alt`. The scripts can be deceived by `COLUMNS=80 ./try.sh`
(or `COLUMNS=80 ./whereami.sh` etc.) but this is a feature, not a bug.
Cody also added an arg check because the program and the
[alternate version](1992/westley/westley.alt.c) might have crashed or
@@ -1650,7 +1647,7 @@ Cody also added an arg check because the program and the
world](https://en.wikipedia.org/wiki/Earth) or just the
[USA](https://en.wikipedia.org/wiki/United_States), respectively, without enough
args (2). And not that we need the help or anything for this :-) but we
encourage you to try the original :-)
encourage you to try the original without two args :-)
## [1993/cmills](1993/cmills/cmills.c) ([README.md](1993/cmills/README.md]))