Check out my first novel, midnight's simulacra!

A UNIX riddle

From dankwiki

dankblog! 2022-09-15, 1002 EDT, at the danktower

here's a little UNIX brainteaser you might enjoy. it took me a second to figure out what was going on, well, like, maybe forty seconds. let's say Alice P. Hacker is reading from a USB serial port with the venerable socat. it might look like this:

socat - /dev/ttyACM0,B115200

she wishes to do something with each line of input, where "line" means what people mean when they've not had to learn terms like "carriage return", "linefeed", "vertical feed", "form feed", "line continuation", and have never been told "well yeah dumbass, you need to rebuild your terminfo database": vertically-separated strings. the arduino MEGA2560 on the other end does use hoary CRLF pairs, but it really shouldn't matter to a competent hacker: we're in a UNIX pipeline, so collapse all such contiguous characters into a single LF (and allow neither quoted strings nor line continuations into our language). players, prepare your state machines accordingly. so she takes a first stab with:

socat - /dev/ttyACM0,B115200 | while read line ; do echo "$line" ; done

figuring she'll replace echo with frobnicate or whatever once she's verified this works. well, annoyingly, all her outputs are separated by two lines, i.e. of the form:

Scribbish: 7.0

Chompsure: erpstores

Scribbish: 7.8

Chompsure: hophophop

"ugh, ok, well echo's generating a newline, let's drop in a -n"...

Scribbish: 7.0Chompsure: erpstoresScribbish: 7.8Chompsure: hophophop

all the output is on a single line. "huh?" she pipes the two outputs through od -x --endian=big, then thinks better of it and uses xxd, to determine its CRLF essence. in the first case, there is always a pair of linefeeds (0a0a), and these are the only vertical whitespace characters. in the latter case, there are no such characters. it seems like echo is adding two newlines.

she gets the same results with printf(1) and any other text-mangling program (e.g. tr(1)).

what's happening? it's not too hard.

previously: "workstation bullshit" 2022-07-21