Anonymous

A UNIX riddle: Difference between revisions

From dankwiki
no edit summary
(Created page with "'''dankblog! 2022-09-15, 1002 EDT, at the danktower'''")
 
No edit summary
 
Line 1: Line 1:
'''[[Dankblog|dankblog!]] 2022-09-15, 1002 EDT, at [[Viewpoint|the danktower]]'''
'''[[Dankblog|dankblog!]] 2022-09-15, 1002 EDT, at [[Viewpoint|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 <tt>socat</tt>. it might look like this:
<pre>socat - /dev/ttyACM0,B115200</pre>
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|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:
<pre>socat - /dev/ttyACM0,B115200 | while read line ; do echo "$line" ; done</pre>
figuring she'll replace <tt>echo</tt> with <tt>frobnicate</tt> or whatever once she's verified this works. well, annoyingly, all her outputs are separated by two lines, i.e. of the form:
<pre>
Scribbish: 7.0
Chompsure: erpstores
Scribbish: 7.8
Chompsure: hophophop
</pre>
"ugh, ok, well <tt>echo</tt>'s generating a newline, let's drop in a <tt>-n</tt>"...
<pre>
Scribbish: 7.0Chompsure: erpstoresScribbish: 7.8Chompsure: hophophop
</pre>
all the output is on a single line. "huh?" she pipes the two outputs through <tt>od -x --endian=big</tt>, then thinks better of it and uses <tt>xxd</tt>, to determine its CRLF essence. in the first case, there is always a pair of linefeeds (<tt>0a0a</tt>), and these are the only vertical whitespace characters. in the latter case, there are no such characters. it seems like <tt>echo</tt> is adding two newlines.
she gets the same results with <tt>printf(1)</tt> and any other text-mangling program (e.g. <tt>tr(1)</tt>).
what's happening? it's not too hard.
'''previously: "[[Workstation_bullshit|workstation bullshit]]" 2022-07-21'''
[[Category:Blog]]