From 74f632203d187148b3d063b7023051138e872d4c Mon Sep 17 00:00:00 2001
From: "B. Watson" <urchlay@slackware.uk>
Date: Wed, 29 May 2024 15:00:43 -0400
Subject: unprotbas: add -s option.

---
 unprotbas.1   | 17 ++++++++++++++---
 unprotbas.c   | 20 ++++++++++++++++----
 unprotbas.rst | 17 ++++++++++++++---
 3 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/unprotbas.1 b/unprotbas.1
index 7fb818c..0f34aec 100644
--- a/unprotbas.1
+++ b/unprotbas.1
@@ -32,7 +32,9 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
 unprotbas \- Unprotect or create LIST-protected Atari 8-bit BASIC programs
 .SH SYNOPSIS
 .sp
-unprotbas [\fB\-v\fP] [ [\fB\-f\fP] [\fB\-n\fP] [\fB\-g\fP] [\fB\-c\fP] [\fB\-r\fP | \fB\-w\fP] ] | [ [\fB\-p\fP | \fB\-pc\fP | \fB\-pv\fP] [\fB\-x\fP[\fIr|XX\fP] ] \fBinput\-file\fP \fBoutput\-file\fP
+unprotbas [\fB\-v\fP] [\fB\-f\fP] [\fB\-n\fP] [\fB\-g\fP] [\fB\-c\fP] [\fB\-r\fP | \fB\-w\fP] \fBinput\-file\fP \fBoutput\-file\fP
+.sp
+unprotbas [\fB\-v\fP] [\fB\-p\fP | \fB\-pc\fP | \fB\-pv\fP] [\fB\-x\fP[\fIr|NN\fP] \fBinput\-file\fP \fBoutput\-file\fP
 .SH DESCRIPTION
 .sp
 \fBunprotbas\fP modifies a LIST\-protected Atari 8\-bit BASIC program,
@@ -52,6 +54,10 @@ will refuse to write to standard output if it\(aqs a terminal (since
 tokenized BASIC is binary data and may confuse the terminal).
 .SH OPTIONS
 .sp
+Options may appear in any order. The first non\-option argument is used
+for \fBinput\-file\fP; the second is \fBoutput\-file\fP\&. A third non\-option
+argument is an error.
+.sp
 Option bundling is not supported, use e.g. \fB\-v \-f\fP, not \fB\-vf\fP\&.
 To use filenames beginning with \fI\-\fP, write them as \fI\&./\-file\fP, or they
 will be treated as options.
@@ -108,12 +114,17 @@ replaces the variable names with the Atari EOL character (\fB$9B\fP).
 \fB\-p\fP does both. None of the other options except \fB\-v\fP (verbose)
 and \fB\-x\fP can be used with these.
 .TP
-.B \fB\-xr\fP, \fB\-xXX\fP
+.B \fB\-xr\fP, \fB\-xNN\fP
 Character to use for variable name protection, with \fB\-p\fP or
-\fB\-pv\fP\&. \fIXX\fP is the character code in hex, e.g. \fB\-x20\fP to use
+\fB\-pv\fP\&. \fINN\fP is the character code in hex, e.g. \fB\-x20\fP to use
 a space. Default is \fB9b\fP (the EOL character). \fB\-xr\fP means random
 codes. Do not put a space between the \fB\-x\fP and the hex digits or \fBr\fP\&.
 This option only works if \fB\-p\fP or \fB\-pv\fP is used.
+.TP
+.B \fB\-s\fP
+Shrink variable name table to one byte per variable name, with \fB\-p\fP or
+\fB\-pv\fP\&. Cannot be used with \fB\-xr\fP\&. Programs protected this way are
+very similar to ones protected with \fBPROTECT.BAS\fP\&.
 .UNINDENT
 .SH EXIT STATUS
 .INDENT 0.0
diff --git a/unprotbas.c b/unprotbas.c
index b2b3d98..8c8651f 100644
--- a/unprotbas.c
+++ b/unprotbas.c
@@ -58,6 +58,9 @@ unsigned char badcode[] = {
 /* for -p/-pv */
 int varname_char = 0x9b;
 
+/* for -s */
+int shrinktable = 0;
+
 /* for the -r option */
 #define MAP_FILE "varnames.txt"
 unsigned char varnames[BUFSIZE];
@@ -634,6 +637,11 @@ void scramble_vars(void) {
 		exit(2);
 	}
 
+	if(shrinktable) {
+		if(verbose) fprintf(stderr, "shrinking variable name table\n");
+		adjust_vntable_size((vvstart - 1) - vnstart, (codestart - vvstart) / 8);
+	}
+
 	if(varname_char == -1) srand(time(NULL));
 
 	for(i = vnstart; i < vvstart - 1; i++)
@@ -652,7 +660,8 @@ void scramble_vars(void) {
 }
 
 void print_help(void) {
-	fprintf(stderr, "Usage: %s [-v] [[-f] [-n] [-g] [-c] [-r|-w] | [-p|-pc|-pv]] <inputfile> <outputfile>\n", self);
+	fprintf(stderr, "Usage: %s [-v] [-f] [-n] [-g] [-c] [-r|-w]  <inputfile> <outputfile>\n", self);
+	fprintf(stderr, "       %s [-v] [-p|-pc|-pv] [-xr|-xNN] [-s] <inputfile> <outputfile>\n", self);
 	fprintf(stderr, "-v: verbose\n");
 	fprintf(stderr, "-f: force variable name table rebuild\n");
 	fprintf(stderr, "-n: do not rebuild variable name table, even if it's invalid\n");
@@ -661,8 +670,8 @@ void print_help(void) {
 	fprintf(stderr, "-w: write variable names to varnames.txt\n");
 	fprintf(stderr, "-r: read variable names from varnames.txt\n");
 	fprintf(stderr, "-pc/-pv/-p: protect code/variables/both\n");
-	fprintf(stderr, "-xXX: hex code XX for variable names, with -p/-pc\n");
-	fprintf(stderr, "-xr: random variable names, with -p/-pc\n");
+	fprintf(stderr, "-xNN: hex code NN for variable names, with -p/-pv\n");
+	fprintf(stderr, "-xr: random variable names, with -p/-pv\n");
 	fprintf(stderr, "Use - as a filename to read from stdin and/or write to stdout\n");
 }
 
@@ -773,11 +782,12 @@ void parse_args(int argc, char **argv) {
 											char *e;
 											varname_char = (int)strtol(&(*argv)[2], &e, 16);
 											if(*e != 0 || varname_char > 0xff)
-												die("invalid hex character for -x option (range is 0 to ff)");
+												die("invalid hex value for -x option (range is 0 to ff)");
 										}
 								 }
 							 }
 							 break;
+				case 's': shrinktable = 1; break;
 				case 0:
 							 if(!input_file)
 								 open_input(NULL);
@@ -809,6 +819,8 @@ void parse_args(int argc, char **argv) {
 	}
 	if(xopt_used && !protect_vars)
 		die("-x option requires -p or -pv");
+	if(shrinktable && !protect_vars)
+		die("-s option requires -p or -pv");
 }
 
 int main(int argc, char **argv) {
diff --git a/unprotbas.rst b/unprotbas.rst
index a3f5e28..1840e71 100644
--- a/unprotbas.rst
+++ b/unprotbas.rst
@@ -11,7 +11,9 @@ Unprotect or create LIST-protected Atari 8-bit BASIC programs
 SYNOPSIS
 ========
 
-unprotbas [**-v**] [ [**-f**] [**-n**] [**-g**] [**-c**] [**-r** | **-w**] ] | [ [**-p** | **-pc** | **-pv**] [**-x**\[*r|XX*] ] **input-file** **output-file**
+unprotbas [**-v**] [**-f**] [**-n**] [**-g**] [**-c**] [**-r** | **-w**] **input-file** **output-file**
+
+unprotbas [**-v**] [**-p** | **-pc** | **-pv**] [**-x**\[*r|NN*] **input-file** **output-file**
 
 DESCRIPTION
 ===========
@@ -35,6 +37,10 @@ tokenized BASIC is binary data and may confuse the terminal).
 OPTIONS
 =======
 
+Options may appear in any order. The first non-option argument is used
+for **input-file**; the second is **output-file**. A third non-option
+argument is an error.
+
 Option bundling is not supported, use e.g. **-v -f**, not **-vf**.
 To use filenames beginning with *-*, write them as *./-file*, or they
 will be treated as options.
@@ -89,13 +95,18 @@ Protection Options
   **-p** does both. None of the other options except **-v** (verbose)
   and **-x** can be used with these.
 
-**-xr**, **-xXX**
+**-xr**, **-xNN**
   Character to use for variable name protection, with **-p** or
-  **-pv**. *XX* is the character code in hex, e.g. **-x20** to use
+  **-pv**. *NN* is the character code in hex, e.g. **-x20** to use
   a space. Default is **9b** (the EOL character). **-xr** means random
   codes. Do not put a space between the **-x** and the hex digits or **r**.
   This option only works if **-p** or **-pv** is used.
 
+**-s**
+  Shrink variable name table to one byte per variable name, with **-p** or
+  **-pv**. Cannot be used with **-xr**. Programs protected this way are
+  very similar to ones protected with **PROTECT.BAS**.
+
 EXIT STATUS
 ===========
 
-- 
cgit v1.2.3