Provide examples for most commands, enhance pyesc, remove superfluous printfesc

This commit is contained in:
Neale Pickett 2017-08-09 00:44:44 +00:00
parent 5dd38fb4ea
commit 9079cc81b7
5 changed files with 47 additions and 67 deletions

View File

@ -7,6 +7,8 @@ TARGETS += printfesc
TARGETS += xor
TARGETS += unhex
TARGETS += pcat
TARGETS += skip
TARGETS += hex
all: $(TARGETS)

View File

@ -42,15 +42,18 @@ Like the normal hd,
but with unicode characters to represent all 256 octets,
instead of using "." for unprintable characters.
$ printf "\0\x01\x02\x03\x30\x52\x9a" | hd
00000000 00 01 02 03 30 52 9a ┆·☺☻♥0RÜ┆
00000007
### unhex: unescape hex
Reads ASCII hex codes on stdin,
writes those octets to stdout.
The following pipe is equivalent to "cat":
./hd | cut -b 11-58 | ./unhex
$ echo 68 65 6c 6c 6f 0a | unhex
hello
### xor: xor mask octets
@ -62,24 +65,27 @@ For a 16-value mask, the mask is applied to 16-octet chunks at a time.
The "-x" option treats values as hex.
The following pipe is equivalent to "cat":
./xor 42 | ./xor -x 2A
$ printf 'hello' | xor 22; echo
~szzy
$ printf 'hello' | xor 0x16; echo
~szzy
$ printf 'hello' | xor -x 16; echo
~szzy
$ printf 'bbbbbb' | xor 1 0; echo
cbcbcb
### skip: discard initial octets
Throws away some initial octets from stdin,
and sends the rest to stdout.
You could use `dd` for the same purpose.
This skip command:
skip 5
Is equivalent to this `dd` command:
dd skip=5 bs=1 status=none
$ echo abcdefgh | dd skip=5 bs=1 status=none
fgh
$ echo abcdefgh | skip 5
fgh
### pcat: print text representation of pcap file
@ -120,22 +126,19 @@ writing to output.
### hex: hex-encode input
The opposite of `unhex`.
The opposite of `unhex`:
encoding all input into a single output line.
The following is the equivalent of `cat`:
hex | unhex
### printfesc: printf escape input
Reads octets,
writes a string suitable for copy-paste into printf.
$ printf "hello\nworld\n" | hex
68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a
### pyesc: python escape input
Escapes input octets for pasting into a python "print" statement.
Also suitable for use as a C string,
a Go string,
and many other languages' string literals.
$ printf "hello\nworld\n" | pyesc
hello\nworld\n

9
hex.c
View File

@ -11,10 +11,15 @@ main(int argc, char *argv[])
if (EOF == c) {
break;
}
printf("%02x ", c);
if (7 == count % 8) {
if (count) {
putchar(' ');
if (0 == count % 8) {
putchar(' ');
}
}
printf("%02x", c);
}
putchar('\n');

View File

@ -1,40 +0,0 @@
#include <stdio.h>
#include <ctype.h>
int
main(int argc, char *argv[])
{
printf("printf ");
while (!feof(stdin)) {
int c = getchar();
switch (c) {
case EOF:
break;
case '\n':
printf("\\n");
break;
case '\r':
printf("\\r");
break;
case '\t':
printf("\\t");
break;
case '"':
printf("\\\"");
break;
default:
if (isprint(c)) {
putchar(c);
} else {
printf("\\%03o", c);
}
break;
}
}
putchar('\n');
return 0;
}

10
pyesc.c
View File

@ -8,7 +8,17 @@ main(int argc, char *argv[])
switch (c) {
case EOF:
putchar('\n');
return 0;
case 8:
printf("\\t");
break;
case 10:
printf("\\n");
break;
case 13:
printf("\\r");
break;
case 134:
printf("\\\\");
break;