Skip to content

Commit dca0d1a

Browse files
authored
Add tools-mode shell scripts (#34)
1 parent 5fcc6d7 commit dca0d1a

26 files changed

+833
-11
lines changed

mmhlpa.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ H("");
8888

8989
g_printHelp = !strcmp(saveHelpCmd, "HELP ADD");
9090
H("This command adds a character string prefix and/or suffix to each");
91-
H("line in a file.");
91+
H("line in a file. To add only a prefix, set <endstr> to the empty string,");
92+
H("and set <begstr> to the empty string to add only a suffix.");
9293
H("Syntax: ADD <iofile> <begstr> <endstr>");
9394

9495
g_printHelp = !strcmp(saveHelpCmd, "HELP TAG");
@@ -132,7 +133,7 @@ H(" Q - Do not alter characters in quotes (ignored by T and U)");
132133
H(" T - (Tab) Convert spaces to equivalent tabs");
133134
H(" U - (Untab) Convert tabs to equivalent spaces");
134135
H("Some other subcommands are also available:");
135-
H(" P - Trim parity (8th) bit from each character");
136+
H(" P - Clear parity (8th) bit from each character");
136137
H(" G - Discard garbage characters CR,FF,ESC,BS");
137138
H(" C - Convert to upper case");
138139
H(" L - Convert to lower case");
@@ -156,7 +157,8 @@ H("Note: The SUBSTITUTE command may be abbreviated by S.");
156157

157158
g_printHelp = !strcmp(saveHelpCmd, "HELP SWAP");
158159
H("This command swaps the parts of each line before and after a");
159-
H("specified string.");
160+
H("specified string <middle>.");
161+
H("Syntax: SWAP <iofile> <middle>");
160162

161163

162164
g_printHelp = !strcmp(saveHelpCmd, "HELP INSERT");

mmvstr.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ vstring edit(vstring sin,long control) {
286286
/* EDIT$ (from VMS BASIC manual)
287287
Syntax: str-vbl = EDIT$(str-exp, int-exp)
288288
Values Effect
289-
1 Trim parity bits
289+
1 Clear parity bits
290290
2 Discard all spaces and tabs
291291
4 Discard characters: CR, LF, FF, ESC, RUBOUT, and NULL
292292
8 Discard leading spaces and tabs
@@ -307,20 +307,20 @@ vstring edit(vstring sin,long control) {
307307
vstring sout;
308308
long i, j, k, m;
309309
int last_char_is_blank;
310-
int trim_flag, discardctrl_flag, bracket_flag, quote_flag, case_flag;
310+
int clear_parity_flag, discardctrl_flag, bracket_flag, quote_flag, uppercase_flag;
311311
int alldiscard_flag, leaddiscard_flag, traildiscard_flag,
312312
traildiscardLF_flag, reduce_flag;
313313
int processing_inside_quote=0;
314314
int lowercase_flag, tab_flag, untab_flag, screen_flag, discardcr_flag;
315315
unsigned char graphicsChar;
316316

317317
/* Set up the flags */
318-
trim_flag = control & 1;
318+
clear_parity_flag = control & 1;
319319
alldiscard_flag = control & 2;
320320
discardctrl_flag = control & 4;
321321
leaddiscard_flag = control & 8;
322322
reduce_flag = control & 16;
323-
case_flag = control & 32;
323+
uppercase_flag = control & 32;
324324
bracket_flag = control & 64;
325325
traildiscard_flag = control & 128;
326326
traildiscardLF_flag = control & 16384;
@@ -361,8 +361,8 @@ vstring edit(vstring sin,long control) {
361361
if ((alldiscard_flag) && isblank_(sout[i]))
362362
sout[i] = 0;
363363

364-
/* Trim parity (eighth?) bit */
365-
if (trim_flag)
364+
/* Clear parity (eighth?) bit */
365+
if (clear_parity_flag)
366366
sout[i] = sout[i] & 0x7F;
367367

368368
/* Discard CR,LF,FF,ESC,BS */
@@ -382,13 +382,13 @@ vstring edit(vstring sin,long control) {
382382

383383
/* Convert lowercase to uppercase */
384384
/*
385-
if ((case_flag) && (islower(sout[i])))
385+
if ((uppercase_flag) && (islower(sout[i])))
386386
sout[i] = toupper(sout[i]);
387387
*/
388388
/* 13-Jun-2009 nm The upper/lower case C functions have odd behavior
389389
with characters > 127, at least in lcc. So this was rewritten to
390390
not use them. */
391-
if ((case_flag) && (sout[i] >= 'a' && sout[i] <= 'z'))
391+
if ((uppercase_flag) && (sout[i] >= 'a' && sout[i] <= 'z'))
392392
sout[i] = (char)(sout[i] - ('a' - 'A'));
393393

394394
/* Convert [] to () */

tools/add.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
# Shell script equivalent of Metamath ADD tool
3+
# Mario Carneiro 5-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
ADD - Add a specified string to each line in a file
8+
9+
This command adds a character string prefix and/or suffix to each
10+
line in a file. To add only a prefix, set ENDSTR to the empty string,
11+
and set BEGSTR to the empty string to add only a suffix.
12+
Syntax: add.sh BEGSTR ENDSTR < INFILE > OUTFILE
13+
HELP
14+
}
15+
16+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
17+
if [ $# -ne 2 ]; then usage; exit 1; fi
18+
19+
while read line; do echo $1${line}$2; done

tools/beep.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# Shell script equivalent of Metamath BEEP tool
3+
# Mario Carneiro 7-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
BEEP - Make a beep sound
8+
9+
This command will produce a beep. By typing it ahead after a long-
10+
running command has started, it will alert you that the command is
11+
finished.
12+
Syntax: beep.sh
13+
HELP
14+
}
15+
16+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
17+
if [ $# -ne 0 ]; then usage; exit 1; fi
18+
19+
beep

tools/break.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
# Shell script equivalent of Metamath BREAK tool
3+
# Mario Carneiro 7-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
BREAK - Break up (parse) a file into a list of tokens (one per line)
8+
9+
This command breaks up a file into tokens, one per line, breaking at
10+
whitespace and any special characters you specify as delimiters.
11+
Use an explicit (quoted) space as SPECCHARS to avoid the default
12+
special characters - ()[],=:;{} - and break only on whitespace.
13+
Syntax: break.sh SPECCHARS < INFILE > OUTFILE
14+
HELP
15+
}
16+
17+
if [ $# -ne 1 ]; then usage; exit 1; fi
18+
19+
if [ "$1" = "" ]; then specChars="()[],=:;{}"; else specChars=$1; fi
20+
21+
awk -v specChars="$specChars" \
22+
'BEGIN {
23+
gsub(/[\\\]\-\^]/, "\\\\&", specChars);
24+
pattern = "[" specChars "]";
25+
} {
26+
gsub(pattern, " & ");
27+
gsub(/^[ \t]+/, "");
28+
gsub(/[ \t]+$/, "");
29+
gsub(/[ \t]+/, "\n");
30+
if ($0 != "") print;
31+
}'

tools/build.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
# Shell script equivalent of Metamath BUILD tool
3+
# Mario Carneiro 7-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
BUILD - Build a file with multiple tokens per line from a list
8+
9+
This command combines a list of tokens into multiple tokens per line,
10+
as many as will fit per line, separating them with spaces.
11+
The line length is hard-coded to 73 characters long.
12+
Syntax: build.sh < INFILE > OUTFILE
13+
HELP
14+
}
15+
16+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
17+
if [ $# -ne 0 ]; then usage; exit 1; fi
18+
19+
awk '{
20+
if ($0 != "") {
21+
if (out == "")
22+
out = $0;
23+
else if (length(out) + length($0) > 72) {
24+
print out;
25+
out = $0;
26+
} else {
27+
out = out " " $0;
28+
}
29+
}
30+
} END {
31+
if (out != "") print out;
32+
}'

tools/clean.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/sh
2+
# Shell script equivalent of Metamath CLEAN tool
3+
# Mario Carneiro 5-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
CLEAN - Trim spaces and tabs on each line in a file; convert characters
8+
9+
This command processes spaces and tabs in each line of a file
10+
according to the following subcommands:
11+
D - Delete all spaces and tabs
12+
B - Delete spaces and tabs at the beginning of each line
13+
E - Delete spaces and tabs at the end of each line
14+
R - Reduce multiple spaces and tabs to one space
15+
Q - Do not alter characters in quotes (ignored by T and U)
16+
T - (Tab) Convert spaces to equivalent tabs (unsupported)
17+
U - (Untab) Convert tabs to equivalent spaces (unsupported)
18+
Some other subcommands are also available:
19+
P - Clear parity (8th) bit from each character
20+
G - Discard garbage characters CR,FF,ESC,BS
21+
C - Convert to upper case
22+
L - Convert to lower case
23+
V - Convert VT220 screen print frame graphics to -,|,+ characters
24+
Subcommands are separated by spaces, e.g., B E R Q
25+
Syntax: clean.sh subcmd subcmd ... < INFILE > OUTFILE
26+
HELP
27+
}
28+
29+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
30+
if [ $# -eq 0 ]; then usage; exit 1; fi
31+
32+
for var in "$@"; do
33+
case $var in
34+
D | d) allDiscard=1;;
35+
B | b) leadDiscard=1;;
36+
E | e) trailDiscard=1;;
37+
R | r) reduce=1;;
38+
Q | q) quote=1;;
39+
T | t) echo "T (tab) flag unsupported"; exit 2;;
40+
U | u) echo "U (untab) flag unsupported"; exit 2;;
41+
P | p) clearParity=1;;
42+
G | g) discardCtrl=1;;
43+
C | c) uppercase=1;;
44+
L | l) lowercase=1;;
45+
V | v) screen=1;;
46+
esac
47+
done
48+
49+
awk \
50+
-v clearParity=$clearParity \
51+
-v allDiscard=$allDiscard \
52+
-v discardCtrl=$discardCtrl \
53+
-v leadDiscard=$leadDiscard \
54+
-v reduce=$reduce \
55+
-v uppercase=$uppercase \
56+
-v trailDiscard=$trailDiscard \
57+
-v quote=$quote \
58+
-v lowercase=$lowercase \
59+
-v tab=$tab \
60+
-v untab=$untab \
61+
-v screen=$screen \
62+
'BEGIN {
63+
FS="";
64+
if (clearParity || uppercase || lowercase) {
65+
for (n=0; n<256; n++) {
66+
ch = sprintf("%c", n);
67+
chr[n] = ch;
68+
ord[ch] = n;
69+
}
70+
}
71+
if (discardCtrl) {
72+
discardArr["\r"] = 1;
73+
discardArr["\n"] = 1;
74+
discardArr["\f"] = 1;
75+
discardArr["\x27"] = 1;
76+
discardArr["\x08"] = 1;
77+
}
78+
if (screen) {
79+
gfxArr["\xEA"] = gfxArr["\xEB"] = gfxArr["\xEC"] = gfxArr["\xED"] = "+";
80+
gfxArr["\xDA"] = gfxArr["\xD9"] = gfxArr["\xBF"] = gfxArr["\xC0"] = "+";
81+
gfxArr["\xF1"] = gfxArr["\xC4"] = "-";
82+
gfxArr["\xF8"] = gfxArr["\xA6"] = gfxArr["\xB3"] = "|";
83+
}
84+
} {
85+
if (leadDiscard)
86+
gsub(/^[ \t]+/, "");
87+
inQuote = 0;
88+
out = "";
89+
for (i=1;i<=NF;i++) {
90+
if (quote && ($i == "\"" || $i == "\x39"))
91+
inQuote = !inQuote;
92+
if (inQuote) {
93+
out=out $i;
94+
continue;
95+
}
96+
if (allDiscard && ($i == " " || $i == "\t")) continue;
97+
if (clearParity) $i = chr[and(ord[$i], 0x7f)];
98+
if (discardCtrl && discardArr[$i]) continue;
99+
if (uppercase && ord[$i] < 128) $i = toupper($i);
100+
if (lowercase && ord[$i] < 128) $i = tolower($i);
101+
if (screen && gfxArr[$i]) $i = gfxArr[$i];
102+
out=out $i;
103+
}
104+
if (trailDiscard)
105+
gsub(/[ \t]+$/, "", out);
106+
if (reduce)
107+
gsub(/[ \t]+/, " ", out);
108+
print out;
109+
}'

tools/copy.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Shell script equivalent of Metamath COPY tool
3+
# Mario Carneiro 7-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
COPY - Similar to Unix "cat" but safe (same input & output name allowed)
8+
9+
This command copies (concatenates) all input files in a space separated
10+
list to an output file. The output file may have
11+
the same name as an input file.
12+
Unlike the metamath version, this script does not save a previous
13+
version of the output file with a ~1 extension. (TODO: implement?)
14+
Example: "copy.sh 1.tmp 1.tmp 1.tmp 2.tmp" followed by "unique.sh 1.tmp"
15+
will result in 1.tmp containing those lines of 2.tmp that didn't
16+
previously exist in 1.tmp.
17+
Syntax: copy.sh OUTFILE INFILE INFILE ...
18+
HELP
19+
}
20+
21+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
22+
23+
outfile=$1; shift
24+
buf=$(cat $@)
25+
echo "$buf" > "$outfile"

tools/delete.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
# Shell script equivalent of Metamath DELETE tool
3+
# Mario Carneiro 5-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
DELETE - Delete a section of each line in a file
8+
9+
This command deletes the part of a line between (and including) the first
10+
occurrence of STARTSTR and the first occurrence of ENDSTR (when both
11+
exist) for all lines in a file. If either string doesn't exist in a line,
12+
the line will be unchanged. If STARTSTR is blank (''), the deletion
13+
will start from the beginning of the line. If ENDSTR is blank, the
14+
deletion will end at the end of the line.
15+
Syntax: delete.sh STARTSTR ENDSTR < INFILE > OUTFILE
16+
HELP
17+
}
18+
19+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
20+
if [ $# -ne 2 ]; then usage; exit 1; fi
21+
22+
awk -v startStr="$1" -v endStr="$2" '{
23+
start = index($0, startStr);
24+
if (start) {
25+
if (endStr == "") {
26+
$0 = substr($0, 1, start - 1);
27+
} else {
28+
start2 = index(substr($0, start), endStr);
29+
if (start2) {
30+
end = start - 1 + start2 + length(endStr);
31+
$0 = substr($0, 1, start - 1) substr($0, end);
32+
}
33+
}
34+
}
35+
print;
36+
}'

tools/duplicate.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# Shell script equivalent of Metamath DUPLICATE tool
3+
# Mario Carneiro 7-Jan-2022
4+
5+
usage() {
6+
cat << HELP
7+
DUPLICATE - Extract first occurrence of any line occurring more than
8+
once in a file, discarding lines occurring exactly once
9+
10+
This command finds all duplicate lines in a file and places them, in
11+
sorted order, into the output file.
12+
Syntax: duplicate.sh < INFILE > OUTFILE
13+
HELP
14+
}
15+
16+
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then usage; exit; fi
17+
if [ $# -ne 0 ]; then usage; exit 1; fi
18+
19+
sort | uniq -d

0 commit comments

Comments
 (0)