summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-08-27 19:12:36 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-08-27 19:12:36 -0400
commit75d4d72a1f5352d1c3520fbaf96911309fc7f96a (patch)
tree942dfa89658964e5954f159fa4d9b41c6b7bc423
parentb373a3a6e1702e7514bb405122a2311d16d85fcd (diff)
index.atom, also: write-atomic
-rw-r--r--Makefile29
-rwxr-xr-xindex14
-rw-r--r--index.atom.erb25
-rw-r--r--index.md.erb13
-rwxr-xr-xindex.rb32
-rwxr-xr-xpage10
-rw-r--r--page.atom.erb13
-rw-r--r--page.html.erb (renamed from template.html.erb)8
-rwxr-xr-xpagerender.rb50
-rw-r--r--public/index.atom1329
-rw-r--r--public/index.html4
-rw-r--r--public/index.md7
-rw-r--r--template.atom.erb13
-rw-r--r--util.rb47
-rwxr-xr-xwrite-atomic21
15 files changed, 1497 insertions, 118 deletions
diff --git a/Makefile b/Makefile
index b144c89..94f7ae8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,26 +1,27 @@
-articles = $(filter-out public/index,$(patsubst %.md,%,$(wildcard public/*.md)))
+articles = $(sort $(filter-out public/index,$(patsubst %.md,%,$(wildcard public/*.md))))
-.DELETE_ON_ERROR:
-.SECONDARY:
+all: public/index.html public/index.atom $(addsuffix .html,$(articles))
-all: public/index.html $(addsuffix .html,$(articles))
+public/%.html: public/%.md page page.html.erb util.rb Makefile write-atomic
+ ./page html $< | ./write-atomic $@
+public/%.atom: public/%.md page page.atom.erb util.rb Makefile write-atomic
+ ./page atom $< | ./write-atomic $@
+public/index.md : index index.md.erb .var.articles $(addsuffix .md,$(articles)) util.rb Makefile write-atomic
+ ./index md $(filter %.md,$^) | ./write-atomic $@
+public/index.atom: index index.atom.erb .var.articles $(addsuffix .md,$(articles)) util.rb Makefile write-atomic
+ ./index atom $(filter %.md,$^) | ./write-atomic $@
-public/%.html: public/%.md pagerender.rb template.html.erb util.rb Makefile
- ./pagerender.rb html $< > $@
-public/%.atom: public/%.md pagerender.rb template.atom.erb util.rb Makefile
- ./pagerender.rb atom $< > $@
-public/index.md: .var.articles $(addsuffix .md,$(articles)) index.rb Makefile
- ./index.rb $(filter %.md,$^) > $@
+clean:
+ rm -f -- public/*.html public/*.atom public/index.md .var* .tmp*
-.var.%: FORCE
+.var.%: FORCE write-ifchanged
@printf '%s' $(call quote.shell,$($*)) | sed 's/^/#/' | ./write-ifchanged $@
-include $(wildcard .var.*)
-clean:
- rm -f -- public/*.html public/*.atom public/index.md .var* .tmp*
-
.PHONY: FORCE
.PHONY: all clean
+.DELETE_ON_ERROR:
+.SECONDARY:
define nl
diff --git a/index b/index
new file mode 100755
index 0000000..44cdc35
--- /dev/null
+++ b/index
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+load 'util.rb'
+
+template = "index.#{ARGV.shift}.erb"
+
+@pages = []
+for filename in ARGV do
+ @pages.push(Page.new(filename))
+end
+
+erb = ERB.new(File.read(template));
+erb.filename = template
+erb.run()
diff --git a/index.atom.erb b/index.atom.erb
new file mode 100644
index 0000000..1784635
--- /dev/null
+++ b/index.atom.erb
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+ <title>Luke Shumaker's Web Log</title>
+ <link rel="self" type="application/atom+xml" href="/blog/index.atom"/>
+ <link rel="alternate" type="text/markdown" href="/blog/index.md"/>
+ <link rel="alternate" type="text/html" href="/blog"/>
+ <updated><%= @pages.map{|p|p.gitdate}.sort.last.rfc3339 %></updated>
+ <author><%= Person.new("Luke Shumaker").atom %></author>
+ <id>https://lukeshu.com/blog/</id>
+
+ <% @pages.sort_by{|p| p.date}.reverse.each do |page| %>
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/<%= page.slug %>.md"/>
+ <link rel="alternate" type="text/html" href="/blog/<%= page.slug %>.html"/>
+ <id>https://lukeshu.com/blog/<%= page.slug %>.html</id>
+ <updated><%= page.gitdate.rfc3339 %></updated>
+ <published><%= page.date.rfc3339 %></published>
+ <title><%= page.title %></title>
+ <content type="html"><%= html_escape(page.content) %></content>
+ <author><%= page.author.atom %></author>
+ <rights type="html"><%= html_escape(page.rights) %></rights>
+ </entry>
+ <% end %>
+</feed>
diff --git a/index.md.erb b/index.md.erb
new file mode 100644
index 0000000..4da9a5d
--- /dev/null
+++ b/index.md.erb
@@ -0,0 +1,13 @@
+Web log entries
+===============
+<style>
+li {
+ list-style-type: none;
+}
+time {
+ color: #AAAAAA;
+ font-family: monospace;
+}
+</style>
+<% @pages.sort_by{|p| p.date}.reverse.each do |a| %>
+ * <time><%= a.date.strftime('%Y-%m-%d') %></time> - [<%= a.title %>](./<%= a.slug %>.html)<% end %>
diff --git a/index.rb b/index.rb
deleted file mode 100755
index 61450d5..0000000
--- a/index.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/usr/bin/env ruby
-# -*- coding: utf-8 -*-
-load 'pandoc.rb'
-require 'erb'
-require 'date'
-
-puts "Web log entries\n=====\n"
-
-puts '<style>
-li {
- list-style-type: none;
-}
-time {
- color: #AAAAAA;
- font-family: monospace;
-}
-</style>'
-
-articles = []
-for filename in ARGV do
- input = File.read(filename)
- doc = Pandoc::load('markdown',input)
- articles.push({
- :title => doc["title"] || input.split("\n",2).first,
- :date => Date.parse(doc['date']),
- :slug => filename.sub(/^public\//,'').sub(/\.md$/,''),
- })
-end
-
-articles.sort_by{|a| a[:date]}.reverse.each do |a|
- puts " * <time>#{a[:date].strftime('%Y-%m-%d')}</time> - [#{a[:title]}](./#{a[:slug]}.html)"
-end
diff --git a/page b/page
new file mode 100755
index 0000000..bca7cdc
--- /dev/null
+++ b/page
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+load 'util.rb'
+
+template = "page.#{ARGV[0]}.erb"
+@page = Page.new(ARGV[1])
+
+erb = ERB.new(File.read(template));
+erb.filename = template
+erb.run()
diff --git a/page.atom.erb b/page.atom.erb
new file mode 100644
index 0000000..bc81380
--- /dev/null
+++ b/page.atom.erb
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/<%= @page.slug %>.md"/>
+ <link rel="alternate" type="text/application/atom+xml" href="/blog/<%= @page.slug %>.atom"/>
+ <link rel="alternate" type="text/html" href="/blog/<%= @page.slug %>.html"/>
+ <id>https://lukeshu.com/blog/<%= @page.slug %>.html</id>
+ <updated><%= @page.gitdate.rfc3339 %></updated>
+ <published><%= @page.date.rfc3339 %></published>
+ <title><%= @page.title %></title>
+ <content type="html"><%= html_escape(@page.content) %></content>
+ <author><%= @page.author.atom %></author>
+ <rights type="html"><%= html_escape(@page.rights) %></rights>
+</entry>
diff --git a/template.html.erb b/page.html.erb
index af6a6f4..586240e 100644
--- a/template.html.erb
+++ b/page.html.erb
@@ -2,16 +2,16 @@
<html lang="en">
<head>
<meta charset="utf-8">
- <title><%= @title %> — Luke Shumaker</title>
+ <title><%= @page.title %> — Luke Shumaker</title>
<link rel="stylesheet" type="text/css" href="assets/style.css">
</head>
<body>
-<header><%= @breadcrumbs %></header>
+<header><%= @page.breadcrumbs %></header>
<article>
-<%= @content %>
+<%= @page.content %>
</article>
<footer>
-<%= @rights %>
+<%= @page.rights %>
</footer>
</body>
</html>
diff --git a/pagerender.rb b/pagerender.rb
deleted file mode 100755
index 3c122bc..0000000
--- a/pagerender.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-#!/usr/bin/env ruby
-# -*- coding: utf-8 -*-
-load 'pandoc.rb'
-load 'util.rb'
-require 'erb'
-require 'date'
-
-$license_urls = {
- "CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/',
- 'WTFPL-2' => "http://www.wtfpl.net/txt/copying/",
-}
-$person_uris = {
- "Luke Shumaker" => "https://lukeshu.com/",
-}
-$person_emails = {
- "Luke Shumaker" => "lukeshu@sbcglobal.net",
-}
-
-template = "template.#{ARGV[0]}.erb"
-infile = ARGV[1]
-
-input = File.read(infile)
-doc = Pandoc::load('markdown', input)
-
-if doc['markdown_options']
- doc = Pandoc::load('markdown'+doc['markdown_options'], input)
-end
-
-gitdate = `git log -n1 --format='%cI' -- #{infile}`
-
-@title = doc['title'] || input.split("\n",2).first
-@author = Person.new(doc['author'] || "Luke Shumaker")
-@gitdate = DateTime.iso8601(gitdate) unless gitdate.empty?
-@date = Date.parse(doc['date']) unless doc['date'].nil?
-@license = License.new(doc['license'] || "CC BY-SA-3.0")
-@slug = infile.sub(/\..*$/,'').sub(/^.*\//,'')
-@content = doc.to('html5')
-@rights = "<p>The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.</p>\n" +
- "<p>This page is licensed under the #{@license.html} license.</p>"
-
-@breadcrumbs = '<a href="/">Luke Shumaker</a> » '
-if (@slug == 'index')
- @breadcrumbs += "blog"
-else
- @breadcrumbs += '<a href=/blog>blog</a> » ' + @slug
-end
-
-erb = ERB.new(File.read(template));
-erb.filename = template
-erb.run()
diff --git a/public/index.atom b/public/index.atom
new file mode 100644
index 0000000..7d9366b
--- /dev/null
+++ b/public/index.atom
@@ -0,0 +1,1329 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+ <title>Luke Shumaker's Web Log</title>
+ <link rel="self" type="application/atom+xml" href="/blog/index.atom"/>
+ <link rel="alternate" type="text/markdown" href="/blog/index.md"/>
+ <link rel="alternate" type="text/html" href="/blog"/>
+ <updated>2016-05-02T02:28:19-04:00</updated>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <id>https://lukeshu.com/blog/</id>
+
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/x11-systemd.md"/>
+ <link rel="alternate" type="text/html" href="/blog/x11-systemd.html"/>
+ <id>https://lukeshu.com/blog/x11-systemd.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2016-02-28T00:00:00+00:00</published>
+ <title>My X11 setup with systemd</title>
+ <content type="html">&lt;h1 id="my-x11-setup-with-systemd"&gt;My X11 setup with systemd&lt;/h1&gt;
+&lt;p&gt;Somewhere along the way, I decided to use systemd user sessions to manage the various parts of my X11 environment would be a good idea. If that was a good idea or not... we'll see.&lt;/p&gt;
+&lt;p&gt;I've sort-of been running this setup as my daily-driver for &lt;a href="https://lukeshu.com/git/dotfiles.git/commit/?id=a9935b7a12a522937d91cb44a0e138132b555e16"&gt;a bit over a year&lt;/a&gt;, continually tweaking it though.&lt;/p&gt;
+&lt;p&gt;My setup is substantially different than the one on &lt;a href="https://wiki.archlinux.org/index.php/Systemd/User"&gt;ArchWiki&lt;/a&gt;, because the ArchWiki solution assumes that there is only ever one X server for a user; I like the ability to run &lt;code&gt;Xorg&lt;/code&gt; on my real monitor, and also have &lt;code&gt;Xvnc&lt;/code&gt; running headless, or start my desktop environment on a remote X server. Though, I would like to figure out how to use systemd socket activation for the X server, as the ArchWiki solution does.&lt;/p&gt;
+&lt;p&gt;This means that all of my graphical units take &lt;code&gt;DISPLAY&lt;/code&gt; as an &lt;code&gt;@&lt;/code&gt; argument. To get this to all work out, this goes in each &lt;code&gt;.service&lt;/code&gt; file, unless otherwise noted:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;[Unit]
+After=X11@%i.target
+Requisite=X11@%i.target
+[Service]
+Environment=DISPLAY=%I&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;We'll get to &lt;code&gt;X11@.target&lt;/code&gt; later, what it says is &amp;quot;I should only be running if X11 is running&amp;quot;.&lt;/p&gt;
+&lt;p&gt;I eschew complex XDMs or &lt;code&gt;startx&lt;/code&gt; wrapper scripts, opting for the more simple &lt;code&gt;xinit&lt;/code&gt;, which I either run on login for some boxes (my media station), or type &lt;code&gt;xinit&lt;/code&gt; when I want X11 on others (most everything else). Essentially, what &lt;code&gt;xinit&lt;/code&gt; does is run &lt;code&gt;~/.xserverrc&lt;/code&gt; (or &lt;code&gt;/etc/X11/xinit/xserverrc&lt;/code&gt;) to start the server, then once the server is started (which it takes a substantial amount of magic to detect) it runs run &lt;code&gt;~/.xinitrc&lt;/code&gt; (or &lt;code&gt;/etc/X11/xinit/xinitrc&lt;/code&gt;) to start the clients. Once &lt;code&gt;.xinitrc&lt;/code&gt; finishes running, it stops the X server and exits. Now, when I say &amp;quot;run&amp;quot;, I don't mean execute, it passes each file to the system shell (&lt;code&gt;/bin/sh&lt;/code&gt;) as input.&lt;/p&gt;
+&lt;p&gt;Xorg requires a TTY to run on; if we log in to a TTY with &lt;code&gt;logind&lt;/code&gt;, it will give us the &lt;code&gt;XDG_VTNR&lt;/code&gt; variable to tell us which one we have, so I pass this to &lt;code&gt;X&lt;/code&gt; in &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/X11/serverrc"&gt;my &lt;code&gt;.xserverrc&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#!/hint/sh
+if [ -z &amp;quot;$XDG_VTNR&amp;quot; ]; then
+ exec /usr/bin/X -nolisten tcp &amp;quot;$@&amp;quot;
+else
+ exec /usr/bin/X -nolisten tcp &amp;quot;$@&amp;quot; vt$XDG_VTNR
+fi&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;This was the default for &lt;a href="https://projects.archlinux.org/svntogit/packages.git/commit/trunk/xserverrc?h=packages/xorg-xinit&amp;amp;id=f9f5de58df03aae6c8a8c8231a83327d19b943a1"&gt;a while&lt;/a&gt; in Arch, to support &lt;code&gt;logind&lt;/code&gt;, but was &lt;a href="https://projects.archlinux.org/svntogit/packages.git/commit/trunk/xserverrc?h=packages/xorg-xinit&amp;amp;id=5a163ddd5dae300e7da4b027e28c37ad3b535804"&gt;later removed&lt;/a&gt; in part because &lt;code&gt;startx&lt;/code&gt; (which calls &lt;code&gt;xinit&lt;/code&gt;) started adding it as an argument as well, so &lt;code&gt;vt$XDG_VTNR&lt;/code&gt; was being listed as an argument twice, which is an error. IMO, that was a problem in &lt;code&gt;startx&lt;/code&gt;, and they shouldn't have removed it from the default system &lt;code&gt;xserverrc&lt;/code&gt;, but that's just me. So I copy/pasted it into my user &lt;code&gt;xserverrc&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;That's the boring part, though. Where the magic starts happening is in &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/X11/clientrc"&gt;my &lt;code&gt;.xinitrc&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#!/hint/sh
+
+if [ -z &amp;quot;$XDG_RUNTIME_DIR&amp;quot; ]; then
+ printf &amp;quot;XDG_RUNTIME_DIR isn&amp;#39;t set\n&amp;quot; &amp;gt;&amp;amp;2
+ exit 6
+fi
+
+_DISPLAY=&amp;quot;$(systemd-escape -- &amp;quot;$DISPLAY&amp;quot;)&amp;quot;
+trap &amp;quot;rm -f $(printf &amp;#39;%q&amp;#39; &amp;quot;${XDG_RUNTIME_DIR}/x11-wm@${_DISPLAY}&amp;quot;)&amp;quot; EXIT
+mkfifo &amp;quot;${XDG_RUNTIME_DIR}/x11-wm@${_DISPLAY}&amp;quot;
+
+cat &amp;lt; &amp;quot;${XDG_RUNTIME_DIR}/x11-wm@${_DISPLAY}&amp;quot; &amp;amp;
+systemctl --user start &amp;quot;X11@${_DISPLAY}.target&amp;quot; &amp;amp;
+wait
+systemctl --user stop &amp;quot;X11@${_DISPLAY}.target&amp;quot;&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;There are two contracts/interfaces here: the &lt;code&gt;X11@DISPLAY.target&lt;/code&gt; systemd target, and the &lt;code&gt;${XDG_RUNTIME_DIR}/x11-wm@DISPLAY&lt;/code&gt; named pipe. The systemd &lt;code&gt;.target&lt;/code&gt; should be pretty self explanatory; the most important part is that it starts the window manager. The named pipe is just a hacky way of blocking until the window manager exits (&amp;quot;traditional&amp;quot; &lt;code&gt;.xinitrc&lt;/code&gt; files end with the line &lt;code&gt;exec your-window-manager&lt;/code&gt;, so this mimics that behavior). It works by assuming that the window manager will open the pipe at startup, and keep it open (without necessarily writing anything to it); when the window manager exits, the pipe will get closed, sending EOF to the &lt;code&gt;wait&lt;/code&gt;ed-for &lt;code&gt;cat&lt;/code&gt;, allowing it to exit, letting the script resume. The window manager (WMII) is made to have the pipe opened by executing it this way in &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/wmii@.service"&gt;its &lt;code&gt;.service&lt;/code&gt; file&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;ExecStart=/usr/bin/env bash -c &amp;#39;exec 8&amp;gt;${XDG_RUNTIME_DIR}/x11-wm@%I; exec wmii&amp;#39;&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;which just opens the file on file descriptor 8, then launches the window manager normally. The only further logic required by the window manager with regard to the pipe is that in the window manager &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/wmii-hg/config.sh"&gt;configuration&lt;/a&gt;, I should close that file descriptor after forking any process that isn't &amp;quot;part of&amp;quot; the window manager:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;runcmd() (
+ ...
+ exec 8&amp;gt;&amp;amp;- # xinit/systemd handshake
+ ...
+)&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;So, back to the &lt;code&gt;X11@DISPLAY.target&lt;/code&gt;; I configure what it &amp;quot;does&amp;quot; with symlinks in the &lt;code&gt;.requires&lt;/code&gt; and &lt;code&gt;.wants&lt;/code&gt; directories:&lt;/p&gt;
+&lt;ul class="tree"&gt;
+&lt;li&gt;
+&lt;p&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user"&gt;.config/systemd/user/&lt;/a&gt;&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/X11@.target"&gt;X11@.target&lt;/a&gt;&lt;/li&gt;
+&lt;li&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/X11@.target.requires"&gt;X11@.target.requires&lt;/a&gt;/
+&lt;ul&gt;
+&lt;li&gt;wmii@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/wmii@.service"&gt;wmii@.service&lt;/a&gt;&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;li&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/X11@.target.wants"&gt;X11@.target.wants&lt;/a&gt;/
+&lt;ul&gt;
+&lt;li&gt;xmodmap@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xmodmap@.service"&gt;xmodmap@.service&lt;/a&gt;&lt;/li&gt;
+&lt;li&gt;xresources-dpi@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xresources-dpi@.service"&gt;xresources-dpi@.service&lt;/a&gt;
+&lt;ul&gt;
+&lt;li&gt;doodle&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;li&gt;xresources@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xresources@.service"&gt;xresources@.service&lt;/a&gt;&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;/ul&gt;
+&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;The &lt;code&gt;.requires&lt;/code&gt; directory is how I configure which window manager it starts. This would allow me to configure different window managers on different displays, by creating a &lt;code&gt;.requires&lt;/code&gt; directory with the &lt;code&gt;DISPLAY&lt;/code&gt; included, e.g. &lt;code&gt;X11@:2.requires&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;The &lt;code&gt;.wants&lt;/code&gt; directory is for general X display setup; it's analogous to &lt;code&gt;/etc/X11/xinit/xinitrc.d/&lt;/code&gt;. All of the files in it are simple &lt;code&gt;Type=oneshot&lt;/code&gt; service files. The &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xmodmap@.service"&gt;xmodmap&lt;/a&gt; and &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xresources@.service"&gt;xresources&lt;/a&gt; files are pretty boring, they're just systemd versions of the couple lines that just about every traditional &lt;code&gt;.xinitrc&lt;/code&gt; contains, the biggest difference being that they look at &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/X11/modmap"&gt;&lt;code&gt;~/.config/X11/modmap&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/X11/resources"&gt;&lt;code&gt;~/.config/X11/resources&lt;/code&gt;&lt;/a&gt; instead of the traditional locations &lt;code&gt;~/.xmodmap&lt;/code&gt; and &lt;code&gt;~/.Xresources&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;What's possibly of note is &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xresources-dpi@.service"&gt;&lt;code&gt;xresources-dpi@.service&lt;/code&gt;&lt;/a&gt;. In X11, there are two sources of DPI information, the X display resolution, and the XRDB &lt;code&gt;Xft.dpi&lt;/code&gt; setting. It isn't defined which takes precedence (to my knowledge), and even if it were (is), application authors wouldn't be arsed to actually do the right thing. For years, Firefox (well, Iceweasel) happily listened to the X display resolution, but recently it decided to only look at &lt;code&gt;Xft.dpi&lt;/code&gt;, which objectively seems a little silly, since the X display resolution is always present, but &lt;code&gt;Xft.dpi&lt;/code&gt; isn't. Anyway, Mozilla's change drove me to to create a &lt;a href="https://lukeshu.com/git/dotfiles/tree/.local/bin/xrdb-set-dpi"&gt;script&lt;/a&gt; to make the &lt;code&gt;Xft.dpi&lt;/code&gt; setting match the X display resolution. Disclaimer: I have no idea if it works if the X server has multiple displays (with possibly varying resolution).&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#!/usr/bin/env bash
+dpi=$(LC_ALL=C xdpyinfo|sed -rn &amp;#39;s/^\s*resolution:\s*(.*) dots per inch$/\1/p&amp;#39;)
+xrdb -merge &amp;lt;&amp;lt;&amp;lt;&amp;quot;Xft.dpi: ${dpi}&amp;quot;&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Since we want XRDB to be set up before any other programs launch, we give both of the &lt;code&gt;xresources&lt;/code&gt; units &lt;code&gt;Before=X11@%i.target&lt;/code&gt; (instead of &lt;code&gt;After=&lt;/code&gt; like everything else). Also, two programs writing to &lt;code&gt;xrdb&lt;/code&gt; at the same time has the same problem as two programs writing to the same file; one might trash the other's changes. So, I stuck &lt;code&gt;Conflicts=xresources@:i.service&lt;/code&gt; into &lt;code&gt;xresources-dpi.service&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;And that's the &amp;quot;core&amp;quot; of my X11 systemd setup. But, you generally want more things running than just the window manager, like a desktop notification daemon, a system panel, and an X composition manager (unless your window manager is bloated and has a composition manager built in). Since these things are probably window-manager specific, I've stuck them in a directory &lt;code&gt;wmii@.service.wants&lt;/code&gt;:&lt;/p&gt;
+&lt;ul class="tree"&gt;
+&lt;li&gt;
+&lt;p&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user"&gt;.config/systemd/user/&lt;/a&gt;&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/wmii@.service.wants"&gt;wmii@.service.wants&lt;/a&gt;/
+&lt;ul&gt;
+&lt;li&gt;dunst@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/dunst@.service"&gt;dunst@.service&lt;/a&gt;        # a notification daemon&lt;/li&gt;
+&lt;li&gt;lxpanel@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/lxpanel@.service"&gt;lxpanel@.service&lt;/a&gt;    # a system panel&lt;/li&gt;
+&lt;li&gt;rbar@97_acpi.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/rbar@.service"&gt;rbar@.service&lt;/a&gt;   # wmii stuff&lt;/li&gt;
+&lt;li&gt;rbar@99_clock.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/rbar@.service"&gt;rbar@.service&lt;/a&gt;  # wmii stuff&lt;/li&gt;
+&lt;li&gt;xcompmgr@.service -&amp;gt; ../&lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/xcompmgr@.service"&gt;xcompmgr@.service&lt;/a&gt;  # an X composition manager&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;/ul&gt;
+&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;For the window manager &lt;code&gt;.service&lt;/code&gt;, I &lt;em&gt;could&lt;/em&gt; just say &lt;code&gt;Type=simple&lt;/code&gt; and call it a day (and I did for a while). But, I like to have &lt;code&gt;lxpanel&lt;/code&gt; show up on all of my WMII tags (desktops), so I have &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/wmii-hg/config.sh"&gt;my WMII configuration&lt;/a&gt; stick this in the WMII &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/wmii-hg/rules"&gt;&lt;code&gt;/rules&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;/panel/ tags=/.*/ floating=always&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Unfortunately, for this to work, &lt;code&gt;lxpanel&lt;/code&gt; must be started &lt;em&gt;after&lt;/em&gt; that gets inserted into WMII's rules. That wasn't a problem pre-systemd, because &lt;code&gt;lxpanel&lt;/code&gt; was started by my WMII configuration, so ordering was simple. For systemd to get this right, I must have a way of notifying systemd that WMII's fully started, and it's safe to start &lt;code&gt;lxpanel&lt;/code&gt;. So, I stuck this in &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/wmii@.service"&gt;my WMII &lt;code&gt;.service&lt;/code&gt; file&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# This assumes that you write READY=1 to $NOTIFY_SOCKET in wmiirc
+Type=notify
+NotifyAccess=all&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;and this in &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/wmii-hg/wmiirc"&gt;my WMII configuration&lt;/a&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;systemd-notify --ready || true&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Now, this setup means that &lt;code&gt;NOTIFY_SOCKET&lt;/code&gt; is set for all the children of &lt;code&gt;wmii&lt;/code&gt;; I'd rather not have it leak into the applications that I start from the window manager, so I also stuck &lt;code&gt;unset NOTIFY_SOCKET&lt;/code&gt; after forking a process that isn't part of the window manager:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;runcmd() (
+ ...
+ unset NOTIFY_SOCKET # systemd
+ ...
+ exec 8&amp;gt;&amp;amp;- # xinit/systemd handshake
+ ...
+)&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Unfortunately, because of a couple of &lt;a href="https://github.com/systemd/systemd/issues/2739"&gt;bugs&lt;/a&gt; and &lt;a href="https://github.com/systemd/systemd/issues/2737"&gt;race conditions&lt;/a&gt; in systemd, &lt;code&gt;systemd-notify&lt;/code&gt; isn't reliable. If systemd can't receive the &lt;code&gt;READY=1&lt;/code&gt; signal from my WMII configuration, there are two consequences:&lt;/p&gt;
+&lt;ol type="1"&gt;
+&lt;li&gt;&lt;code&gt;lxpanel&lt;/code&gt; will never start, because it will always be waiting for &lt;code&gt;wmii&lt;/code&gt; to be ready, which will never happen.&lt;/li&gt;
+&lt;li&gt;After a couple of minutes, systemd will consider &lt;code&gt;wmii&lt;/code&gt; to be timed out, which is a failure, so then it will kill &lt;code&gt;wmii&lt;/code&gt;, and exit my X11 session. That's no good!&lt;/li&gt;
+&lt;/ol&gt;
+&lt;p&gt;Using &lt;code&gt;socat&lt;/code&gt; to send the message to systemd instead of &lt;code&gt;systemd-notify&lt;/code&gt; &amp;quot;should&amp;quot; always work, because it tries to read from both ends of the bi-directional stream, and I can't imagine that getting EOF from the &lt;code&gt;UNIX-SENDTO&lt;/code&gt; end will ever be faster than the systemd manager from handling the datagram that got sent. Which is to say, &amp;quot;we work around the race condition by being slow and shitty.&amp;quot;&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;socat STDIO UNIX-SENDTO:&amp;quot;$NOTIFY_SOCKET&amp;quot; &amp;lt;&amp;lt;&amp;lt;READY=1 || true&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;But, I don't like that. I'd rather write my WMII configuration to the world as I wish it existed, and have workarounds encapsulated elsewhere; &lt;a href="http://blog.robertelder.org/interfaces-most-important-software-engineering-concept/"&gt;&amp;quot;If you have to cut corners in your project, do it inside the implementation, and wrap a very good interface around it.&amp;quot;&lt;/a&gt;. So, I wrote a &lt;code&gt;systemd-notify&lt;/code&gt; compatible &lt;a href="https://lukeshu.com/git/dotfiles.git/tree/.config/wmii-hg/workarounds.sh"&gt;function&lt;/a&gt; that ultimately calls &lt;code&gt;socat&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;##
+# Just like systemd-notify(1), but slower, which is a shitty
+# workaround for a race condition in systemd.
+##
+systemd-notify() {
+ local args
+ args=&amp;quot;$(getopt -n systemd-notify -o h -l help,version,ready,pid::,status:,booted -- &amp;quot;$@&amp;quot;)&amp;quot;
+ ret=$?; [[ $ret == 0 ]] || return $ret
+ eval set -- &amp;quot;$args&amp;quot;
+
+ local arg_ready=false
+ local arg_pid=0
+ local arg_status=
+ while [[ $# -gt 0 ]]; do
+ case &amp;quot;$1&amp;quot; in
+ -h|--help) command systemd-notify --help; return $?;;
+ --version) command systemd-notify --version; return $?;;
+ --ready) arg_ready=true; shift 1;;
+ --pid) arg_pid=${2:-$$}; shift 2;;
+ --status) arg_status=$2; shift 2;;
+ --booted) command systemd-notify --booted; return $?;;
+ --) shift 1; break;;
+ esac
+ done
+
+ local our_env=()
+ if $arg_ready; then
+ our_env+=(&amp;quot;READY=1&amp;quot;)
+ fi
+ if [[ -n &amp;quot;$arg_status&amp;quot; ]]; then
+ our_env+=(&amp;quot;STATUS=$arg_status&amp;quot;)
+ fi
+ if [[ &amp;quot;$arg_pid&amp;quot; -gt 0 ]]; then
+ our_env+=(&amp;quot;MAINPID=$arg_pid&amp;quot;)
+ fi
+ our_env+=(&amp;quot;$@&amp;quot;)
+ local n
+ printf -v n &amp;#39;%s\n&amp;#39; &amp;quot;${our_env[@]}&amp;quot;
+ socat STDIO UNIX-SENDTO:&amp;quot;$NOTIFY_SOCKET&amp;quot; &amp;lt;&amp;lt;&amp;lt;&amp;quot;$n&amp;quot;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;So, one day when the systemd bugs have been fixed (and presumably the Linux kernel supports passing the cgroup of a process as part of its credentials), I can remove that from &lt;code&gt;workarounds.sh&lt;/code&gt;, and not have to touch anything else in my WMII configuration (I do use &lt;code&gt;systemd-notify&lt;/code&gt; in a couple of other, non-essential, places too; this wasn't to avoid having to change just 1 line).&lt;/p&gt;
+&lt;p&gt;So, now that &lt;code&gt;wmii@.service&lt;/code&gt; properly has &lt;code&gt;Type=notify&lt;/code&gt;, I can just stick &lt;code&gt;After=wmii@.service&lt;/code&gt; into my &lt;code&gt;lxpanel@.service&lt;/code&gt;, right? Wrong! Well, I &lt;em&gt;could&lt;/em&gt;, but my &lt;code&gt;lxpanel&lt;/code&gt; service has nothing to do with WMII; why should I couple them? Instead, I create &lt;a href="https://lukeshu.com/git/dotfiles/tree/.config/systemd/user/wm-running@.target"&gt;&lt;code&gt;wm-running@.target&lt;/code&gt;&lt;/a&gt; that can be used as a synchronization point:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# wmii@.service
+Before=wm-running@%i.target
+
+# lxpanel@.service
+After=X11@%i.target wm-running@%i.target
+Requires=wm-running@%i.target&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Finally, I have my desktop started and running. Now, I'd like for programs that aren't part of the window manager to not dump their stdout and stderr into WMII's part of the journal, like to have a record of which graphical programs crashed, and like to have a prettier cgroup/process graph. So, I use &lt;code&gt;systemd-run&lt;/code&gt; to run external programs from the window manager:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;runcmd() (
+ ...
+ unset NOTIFY_SOCKET # systemd
+ ...
+ exec 8&amp;gt;&amp;amp;- # xinit/systemd handshake
+ exec systemd-run --user --scope -- sh -c &amp;quot;$*&amp;quot;
+)&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;I run them as a scope instead of a service so that they inherit environment variables, and don't have to mess with getting &lt;code&gt;DISPLAY&lt;/code&gt; or &lt;code&gt;XAUTHORITY&lt;/code&gt; into their units (as I &lt;em&gt;don't&lt;/em&gt; want to make them global variables in my systemd user session).&lt;/p&gt;
+&lt;p&gt;I'd like to get &lt;code&gt;lxpanel&lt;/code&gt; to also use &lt;code&gt;systemd-run&lt;/code&gt; when launching programs, but it's a low priority because I don't really actually use &lt;code&gt;lxpanel&lt;/code&gt; to launch programs, I just have the menu there to make sure that I didn't break the icons for programs that I package (I did that once back when I was Parabola's packager for Iceweasel and IceCat).&lt;/p&gt;
+&lt;p&gt;And that's how I use systemd with X11.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2016 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/java-segfault-redux.md"/>
+ <link rel="alternate" type="text/html" href="/blog/java-segfault-redux.html"/>
+ <id>https://lukeshu.com/blog/java-segfault-redux.html</id>
+ <updated>2016-05-02T02:28:19-04:00</updated>
+ <published>2016-02-28T00:00:00+00:00</published>
+ <title>My favorite bug: segfaults in Java (redux)</title>
+ <content type="html">&lt;h1 id="my-favorite-bug-segfaults-in-java-redux"&gt;My favorite bug: segfaults in Java (redux)&lt;/h1&gt;
+&lt;p&gt;Two years ago, I &lt;a href="./java-segfault.html"&gt;wrote&lt;/a&gt; about one of my favorite bugs that I'd squashed two years before that. About a year after that, someone posted it &lt;a href="https://news.ycombinator.com/item?id=9283571"&gt;on Hacker News&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;There was some fun discussion about it, but also some confusion. After finishing a season of mentoring team 4272, I've decided that it would be fun to re-visit the article, and dig up the old actual code, instead of pseudo-code, hopefully improving the clarity (and providing a light introduction for anyone wanting to get into modifying the current SmartDashbaord).&lt;/p&gt;
+&lt;h2 id="the-context"&gt;The context&lt;/h2&gt;
+&lt;p&gt;In 2012, I was a high school senior, and lead programmer programmer on the FIRST Robotics Competition team 1024. For the unfamiliar, the relevant part of the setup is that there are 2 minute and 15 second matches in which you have a 120 pound robot that sometimes runs autonomously, and sometimes is controlled over WiFi from a person at a laptop running stock &amp;quot;driver station&amp;quot; software and modifiable &amp;quot;dashboard&amp;quot; software.&lt;/p&gt;
+&lt;p&gt;That year, we mostly used the dashboard software to allow the human driver and operator to monitor sensors on the robot, one of them being a video feed from a web-cam mounted on it. This was really easy because the new standard dashboard program had a click-and drag interface to add stock widgets; you just had to make sure the code on the robot was actually sending the data.&lt;/p&gt;
+&lt;p&gt;That's great, until when debugging things, the dashboard would suddenly vanish. If it was run manually from a terminal (instead of letting the driver station software launch it), you would see a core dump indicating a segmentation fault.&lt;/p&gt;
+&lt;p&gt;This wasn't just us either; I spoke with people on other teams, everyone who was streaming video had this issue. But, because it only happened every couple of minutes, and a match is only 2:15, it didn't need to run very long, they just crossed their fingers and hoped it didn't happen during a match.&lt;/p&gt;
+&lt;p&gt;The dashboard was written in Java, and the source was available (under a 3-clause BSD license) via read-only SVN at &lt;code&gt;http://firstforge.wpi.edu/svn/repos/smart_dashboard/trunk&lt;/code&gt; (which is unfortunately no longer online, fortunately I'd posted some snapshots on the web). So I dove in, hunting for the bug.&lt;/p&gt;
+&lt;p&gt;The repository was divided into several NetBeans projects (not exhaustively listed):&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;a href="https://gitorious.org/absfrc/sources.git/?p=absfrc:sources.git;a=blob_plain;f=smartdashboard-client-2012-1-any.src.tar.xz;hb=HEAD"&gt;&lt;code&gt;client/smartdashboard&lt;/code&gt;&lt;/a&gt;: The main dashboard program, has a plugin architecture.&lt;/li&gt;
+&lt;li&gt;&lt;a href="https://gitorious.org/absfrc/sources.git/?p=absfrc:sources.git;a=blob_plain;f=wpijavacv-208-1-any.src.tar.xz;hb=HEAD"&gt;&lt;code&gt;WPIJavaCV&lt;/code&gt;&lt;/a&gt;: A higher-level wrapper around JavaCV, itself a Java Native Interface (JNI) wrapper to talk to OpenCV (C and C++).&lt;/li&gt;
+&lt;li&gt;&lt;a href="https://gitorious.org/absfrc/sources.git/?p=absfrc:sources.git;a=blob_plain;f=smartdashboard-extension-wpicameraextension-210-1-any.src.tar.xz;hb=HEAD"&gt;&lt;code&gt;extensions/camera/WPICameraExtension&lt;/code&gt;&lt;/a&gt;: The standard camera feed plugin, processes the video through WPIJavaCV.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;I figured that the bug must be somewhere in the C or C++ code that was being called by JavaCV, because that's the language where segfaults happen. It was especially a pain to track down the pointers that were causing the issue, because it was hard with native debuggers to see through all of the JVM stuff to the OpenCV code, and the OpenCV stuff is opaque to Java debuggers.&lt;/p&gt;
+&lt;p&gt;Eventually the issue lead me back into the WPICameraExtension, then into WPIJavaCV--there was a native pointer being stored in a Java variable; Java code called the native routine to &lt;code&gt;free()&lt;/code&gt; the structure, but then tried to feed it to another routine later. This lead to difficulty again--tracking objects with Java debuggers was hard because they don't expect the program to suddenly segfault; it's Java code, Java doesn't segfault, it throws exceptions!&lt;/p&gt;
+&lt;p&gt;With the help of &lt;code&gt;println()&lt;/code&gt; I was eventually able to see that some code was executing in an order that straight didn't make sense.&lt;/p&gt;
+&lt;h2 id="the-bug"&gt;The bug&lt;/h2&gt;
+&lt;p&gt;The basic flow of WPIJavaCV is you have a &lt;code&gt;WPICamera&lt;/code&gt;, and you call &lt;code&gt;.getNewImage()&lt;/code&gt; on it, which gives you a &lt;code&gt;WPIImage&lt;/code&gt;, which you could do all kinds of fancy OpenCV things on, but then ultimately call &lt;code&gt;.getBufferedImage()&lt;/code&gt;, which gives you a &lt;code&gt;java.awt.image.BufferedImage&lt;/code&gt; that you can pass to Swing to draw on the screen. You do this every for frame. Which is exactly what &lt;code&gt;WPICameraExtension.java&lt;/code&gt; did, except that &amp;quot;all kinds of fancy OpenCV things&amp;quot; consisted only of:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;public WPIImage processImage(WPIColorImage rawImage) {
+ return rawImage;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;The idea was that you would extend the class, overriding that one method, if you wanted to do anything fancy.&lt;/p&gt;
+&lt;p&gt;One of the neat things about WPIJavaCV was that every OpenCV object class extended had a &lt;code&gt;finalize()&lt;/code&gt; method (via inheriting from the abstract class &lt;code&gt;WPIDisposable&lt;/code&gt;) that freed the underlying C/C++ memory, so you didn't have to worry about memory leaks like in plain JavaCV. To inherit from &lt;code&gt;WPIDisposable&lt;/code&gt;, you had to write a &lt;code&gt;disposed()&lt;/code&gt; method that actually freed the memory. This was better than writing &lt;code&gt;finalize()&lt;/code&gt; directly, because it did some safety with NULL pointers and idempotency if you wanted to manually free something early.&lt;/p&gt;
+&lt;p&gt;Now, &lt;code&gt;edu.wpi.first.WPIImage.disposed()&lt;/code&gt; called &lt;code&gt;&lt;a href="https://github.com/bytedeco/javacv/blob/svn/src/com/googlecode/javacv/cpp/opencv_core.java#L398"&gt;com.googlecode.javacv.cpp.opencv_core.IplImage&lt;/a&gt;.release()&lt;/code&gt;, which called (via JNI) &lt;code&gt;IplImage:::release()&lt;/code&gt;, which called libc &lt;code&gt;free()&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;@Override
+protected void disposed() {
+ image.release();
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Elsewhere, the C buffer for the image was copied into a Java buffer via a similar chain kicked off by &lt;code&gt;edu.wpi.first.WPIImage.getBufferedImage()&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;/**
+ * Copies this {@link WPIImage} into a {@link BufferedImage}.
+ * This method will always generate a new image.
+ * @return a copy of the image
+ */
+public BufferedImage getBufferedImage() {
+ validateDisposed();
+
+ return image.getBufferedImage();
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;The &lt;code&gt;println()&lt;/code&gt; output I saw that didn't make sense was that &lt;code&gt;someFrame.finalize()&lt;/code&gt; was running before &lt;code&gt;someFrame.getBuffereImage()&lt;/code&gt; had returned!&lt;/p&gt;
+&lt;p&gt;You see, if it is waiting for the return value of a method &lt;code&gt;m()&lt;/code&gt; of object &lt;code&gt;a&lt;/code&gt;, and code in &lt;code&gt;m()&lt;/code&gt; that is yet to be executed doesn't access any other methods or properties of &lt;code&gt;a&lt;/code&gt;, then it will go ahead and consider &lt;code&gt;a&lt;/code&gt; eligible for garbage collection before &lt;code&gt;m()&lt;/code&gt; has finished running.&lt;/p&gt;
+&lt;p&gt;Put another way, &lt;code&gt;this&lt;/code&gt; is passed to a method just like any other argument. If a method is done accessing &lt;code&gt;this&lt;/code&gt;, then it's &amp;quot;safe&amp;quot; for the JVM to go ahead and garbage collect it.&lt;/p&gt;
+&lt;p&gt;That is normally a safe &amp;quot;optimization&amp;quot; to make… except for when a destructor method (&lt;code&gt;finalize()&lt;/code&gt;) is defined for the object; the destructor can have side effects, and Java has no way to know whether it is safe for them to happen before &lt;code&gt;m()&lt;/code&gt; has finished running.&lt;/p&gt;
+&lt;p&gt;I'm not entirely sure if this is a &amp;quot;bug&amp;quot; in the compiler or the language specification, but I do believe that it's broken behavior.&lt;/p&gt;
+&lt;p&gt;Anyway, in this case it's unsafe with WPI's code.&lt;/p&gt;
+&lt;h2 id="my-work-around"&gt;My work-around&lt;/h2&gt;
+&lt;p&gt;My work-around was to change this function in &lt;code&gt;WPIImage&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;public BufferedImage getBufferedImage() {
+ validateDisposed();
+
+ return image.getBufferedImage(); // `this` may get garbage collected before it returns!
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;In the above code, &lt;code&gt;this&lt;/code&gt; is a &lt;code&gt;WPIImage&lt;/code&gt;, and it may get garbage collected between the time that &lt;code&gt;image.getBufferedImage()&lt;/code&gt; is dispatched, and the time that &lt;code&gt;image.getBufferedImage()&lt;/code&gt; accesses native memory. When it is garbage collected, it calls &lt;code&gt;image.release()&lt;/code&gt;, which &lt;code&gt;free()&lt;/code&gt;s that native memory. That seems pretty unlikely to happen; that's a very small gap of time. However, running 30 times a second, eventually bad luck with the garbage collector happens, and the program crashes.&lt;/p&gt;
+&lt;p&gt;The work-around was to insert a bogus call to this to keep &lt;code&gt;this&lt;/code&gt; around until after we were also done with &lt;code&gt;image&lt;/code&gt;:&lt;/p&gt;
+&lt;p&gt;to this:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;public BufferedImage getBufferedImage() {
+ validateDisposed();
+ BufferedImage ret = image.getBufferedImage();
+ getWidth(); // bogus call to keep `this` around
+ return ret;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Yeah. After spending weeks wading through though thousands of lines of Java, C, and C++, a bogus call to a method I didn't care about was the fix.&lt;/p&gt;
+&lt;p&gt;TheLoneWolfling on Hacker News noted that they'd be worried about the JVM optimizing out the call to &lt;code&gt;getWidth()&lt;/code&gt;. I'm not, because &lt;code&gt;WPIImage.getWidth()&lt;/code&gt; calls &lt;code&gt;IplImage.width()&lt;/code&gt;, which is declared as &lt;code&gt;native&lt;/code&gt;; the JVM must run it because it might have side effects. On the other hand, looking back, I think I just shrunk the window for things to go wrong: it may be possible for the garbage collection to trigger in the time between &lt;code&gt;getWidth()&lt;/code&gt; being dispatched and &lt;code&gt;width()&lt;/code&gt; running. Perhaps there was something in the C/C++ code that made it safe, I don't recall, and don't care quite enough to dig into OpenCV internals again. Or perhaps I'm mis-remembering the fix (which I don't actually have a file of), and I called some other method that &lt;em&gt;could&lt;/em&gt; get optimized out (though I &lt;em&gt;do&lt;/em&gt; believe that it was either &lt;code&gt;getWidth()&lt;/code&gt; or &lt;code&gt;getHeight()&lt;/code&gt;).&lt;/p&gt;
+&lt;h2 id="wpis-fix"&gt;WPI's fix&lt;/h2&gt;
+&lt;p&gt;Four years later, the SmartDashboard is still being used! But it no longer has this bug, and it's not using my workaround. So, how did the WPILib developers fix it?&lt;/p&gt;
+&lt;p&gt;Well, the code now lives &lt;a href="https://usfirst.collab.net/gerrit/#/admin/projects/"&gt;in git at collab.net&lt;/a&gt;, so I decided to take a look.&lt;/p&gt;
+&lt;p&gt;The stripped out WPIJavaCV from the main video feed widget, and now use a purely Java implementation of MPJPEG streaming.&lt;/p&gt;
+&lt;p&gt;However, the old video feed widget is still available as an extension (so that you can still do cool things with &lt;code&gt;processImage&lt;/code&gt;), and it also no longer has this bug. Their fix was to put a mutex around all accesses to &lt;code&gt;image&lt;/code&gt;, which should have been the obvious solution to me.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2016 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/nginx-mediawiki.md"/>
+ <link rel="alternate" type="text/html" href="/blog/nginx-mediawiki.html"/>
+ <id>https://lukeshu.com/blog/nginx-mediawiki.html</id>
+ <updated>2015-05-19T23:53:52-06:00</updated>
+ <published>2015-05-19T00:00:00+00:00</published>
+ <title>An Nginx configuration for MediaWiki</title>
+ <content type="html">&lt;h1 id="an-nginx-configuration-for-mediawiki"&gt;An Nginx configuration for MediaWiki&lt;/h1&gt;
+&lt;p&gt;There are &lt;a href="http://wiki.nginx.org/MediaWiki"&gt;several&lt;/a&gt; &lt;a href="https://wiki.archlinux.org/index.php/MediaWiki#Nginx"&gt;example&lt;/a&gt; &lt;a href="https://www.mediawiki.org/wiki/Manual:Short_URL/wiki/Page_title_--_nginx_rewrite--root_access"&gt;Nginx&lt;/a&gt; &lt;a href="https://www.mediawiki.org/wiki/Manual:Short_URL/Page_title_-_nginx,_Root_Access,_PHP_as_a_CGI_module"&gt;configurations&lt;/a&gt; &lt;a href="http://wiki.nginx.org/RHEL_5.4_%2B_Nginx_%2B_Mediawiki"&gt;for&lt;/a&gt; &lt;a href="http://stackoverflow.com/questions/11080666/mediawiki-on-nginx"&gt;MediaWiki&lt;/a&gt; floating around the web. Many of them don't block the user from accessing things like &lt;code&gt;/serialized/&lt;/code&gt;. Many of them also &lt;a href="https://labs.parabola.nu/issues/725"&gt;don't correctly handle&lt;/a&gt; a wiki page named &lt;code&gt;FAQ&lt;/code&gt;, since that is a name of a file in the MediaWiki root! In fact, the configuration used on the official Nginx Wiki has both of those issues!&lt;/p&gt;
+&lt;p&gt;This is because most of the configurations floating around basically try to pass all requests through, and blacklist certain requests, either denying them, or passing them through to &lt;code&gt;index.php&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;It's my view that blacklisting is inferior to whitelisting in situations like this. So, I developed the following configuration that instead works by whitelisting certain paths.&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;root /path/to/your/mediawiki; # obviously, change this line
+
+index index.php;
+location / { try_files /var/empty @rewrite; }
+location /images/ { try_files $uri $uri/ @rewrite; }
+location /skins/ { try_files $uri $uri/ @rewrite; }
+location /api.php { try_files /var/empty @php; }
+location /api.php5 { try_files /var/empty @php; }
+location /img_auth.php { try_files /var/empty @php; }
+location /img_auth.php5 { try_files /var/empty @php; }
+location /index.php { try_files /var/empty @php; }
+location /index.php5 { try_files /var/empty @php; }
+location /load.php { try_files /var/empty @php; }
+location /load.php5 { try_files /var/empty @php; }
+location /opensearch_desc.php { try_files /var/empty @php; }
+location /opensearch_desc.php5 { try_files /var/empty @php; }
+location /profileinfo.php { try_files /var/empty @php; }
+location /thumb.php { try_files /var/empty @php; }
+location /thumb.php5 { try_files /var/empty @php; }
+location /thumb_handler.php { try_files /var/empty @php; }
+location /thumb_handler.php5 { try_files /var/empty @php; }
+location /wiki.phtml { try_files /var/empty @php; }
+
+location @rewrite {
+ rewrite ^/(.*)$ /index.php?title=$1&amp;amp;$args;
+}
+
+location @php {
+ # obviously, change this according to your PHP setup
+ include fastcgi.conf;
+ fastcgi_pass unix:/run/php-fpm/wiki.sock;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;We are now using this configuration on &lt;a href="https://wiki.parabola.nu/"&gt;ParabolaWiki&lt;/a&gt;, but with an alias for &lt;code&gt;location = /favicon.ico&lt;/code&gt; to the correct file in the skin, and with FastCGI caching for PHP.&lt;/p&gt;
+&lt;p&gt;The only thing I don't like about this is the &lt;code&gt;try_files /var/emtpy&lt;/code&gt; bits--surely there is a better way to have it go to one of the &lt;code&gt;@&lt;/code&gt; location blocks, but I couldn't figure it out.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2015 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/lp2015-videos.md"/>
+ <link rel="alternate" type="text/html" href="/blog/lp2015-videos.html"/>
+ <id>https://lukeshu.com/blog/lp2015-videos.html</id>
+ <updated>2015-03-22T07:52:39-04:00</updated>
+ <published>2015-03-22T00:00:00+00:00</published>
+ <title>I took some videos at LibrePlanet</title>
+ <content type="html">&lt;h1 id="i-took-some-videos-at-libreplanet"&gt;I took some videos at LibrePlanet&lt;/h1&gt;
+&lt;p&gt;I'm at &lt;a href="https://libreplanet.org/2015/"&gt;LibrePlanet&lt;/a&gt;, and have been loving the talks. For most of yesterday, there was a series of short &amp;quot;lightning&amp;quot; talks in room 144. I decided to hang out in that room for the later part of the day, because while most of the talks were live streamed and recorded, there were no cameras in room 144; so I couldn't watch them later.&lt;/p&gt;
+&lt;p&gt;Way too late in the day, I remembered that I have the capability to record videos, so I cought the last two talks in 144.&lt;/p&gt;
+&lt;p&gt;I appologize for the changing orientation.&lt;/p&gt;
+&lt;p&gt;&lt;a href="https://lukeshu.com/dump/lp-2015-last-2-short-talks.ogg"&gt;Here's the video I took&lt;/a&gt;.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2015 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/build-bash-1.md"/>
+ <link rel="alternate" type="text/html" href="/blog/build-bash-1.html"/>
+ <id>https://lukeshu.com/blog/build-bash-1.html</id>
+ <updated>2016-05-02T02:20:41-04:00</updated>
+ <published>2015-03-18T00:00:00+00:00</published>
+ <title>Building Bash 1.14.7 on a modern system</title>
+ <content type="html">&lt;h1 id="building-bash-1.14.7-on-a-modern-system"&gt;Building Bash 1.14.7 on a modern system&lt;/h1&gt;
+&lt;p&gt;In a previous revision of my &lt;a href="./bash-arrays.html"&gt;Bash arrays post&lt;/a&gt;, I wrote:&lt;/p&gt;
+&lt;blockquote&gt;
+&lt;p&gt;Bash 1.x won't compile with modern GCC, so I couldn't verify how it behaves.&lt;/p&gt;
+&lt;/blockquote&gt;
+&lt;p&gt;I recall spending a little time fighting with it, but apparently I didn't try very hard: getting Bash 1.14.7 to build on a modern box is mostly just adjusting it to use &lt;code&gt;stdarg&lt;/code&gt; instead of the no-longer-implemented &lt;code&gt;varargs&lt;/code&gt;. There's also a little fiddling with the pre-autoconf automatic configuration.&lt;/p&gt;
+&lt;h2 id="stdarg"&gt;stdarg&lt;/h2&gt;
+&lt;p&gt;Converting to &lt;code&gt;stdarg&lt;/code&gt; is pretty simple: For each variadic function (functions that take a variable number of arguments), follow these steps:&lt;/p&gt;
+&lt;ol type="1"&gt;
+&lt;li&gt;Replace &lt;code&gt;#include &amp;lt;varargs.h&amp;gt;&lt;/code&gt; with &lt;code&gt;#include &amp;lt;stdarg.h&amp;gt;&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;Replace &lt;code&gt;function_name (va_alist) va_dcl&lt;/code&gt; with &lt;code&gt;function_name (char *format, ...)&lt;/code&gt;.&lt;/li&gt;
+&lt;li&gt;Removing the declaration and assignment for &lt;code&gt;format&lt;/code&gt; from the function body.&lt;/li&gt;
+&lt;li&gt;Replace &lt;code&gt;va_start (args);&lt;/code&gt; with &lt;code&gt;va_start (args, format);&lt;/code&gt; in the function bodies.&lt;/li&gt;
+&lt;li&gt;Replace &lt;code&gt;function_name ();&lt;/code&gt; with &lt;code&gt;function_name (char *, ...)&lt;/code&gt; in header files and/or at the top of C files.&lt;/li&gt;
+&lt;/ol&gt;
+&lt;p&gt;There's one function that uses the variable name &lt;code&gt;control&lt;/code&gt; instead of &lt;code&gt;format&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;I've prepared &lt;a href="./bash-1.14.7-gcc4-stdarg.patch"&gt;a patch&lt;/a&gt; that does this.&lt;/p&gt;
+&lt;h2 id="configuration"&gt;Configuration&lt;/h2&gt;
+&lt;p&gt;Instead of using autoconf-style tests to test for compiler and platform features, Bash 1 used the file &lt;code&gt;machines.h&lt;/code&gt; that had &lt;code&gt;#ifdefs&lt;/code&gt; and a huge database of of different operating systems for different platforms. It's gross. And quite likely won't handle your modern operating system.&lt;/p&gt;
+&lt;p&gt;I made these two small changes to &lt;code&gt;machines.h&lt;/code&gt; to get it to work correctly on my box:&lt;/p&gt;
+&lt;ol type="1"&gt;
+&lt;li&gt;Replace &lt;code&gt;#if defined (i386)&lt;/code&gt; with &lt;code&gt;#if defined (i386) || defined (__x86_64__)&lt;/code&gt;. The purpose of this is obvious.&lt;/li&gt;
+&lt;li&gt;Add &lt;code&gt;#define USE_TERMCAP_EMULATION&lt;/code&gt; to the section for Linux [sic] on i386 (&lt;code&gt;# if !defined (done386) &amp;amp;&amp;amp; (defined (__linux__) || defined (linux))&lt;/code&gt;). What this does is tell it to link against libcurses to use curses termcap emulation, instead of linking against libtermcap (which doesn't exist on modern GNU/Linux systems).&lt;/li&gt;
+&lt;/ol&gt;
+&lt;p&gt;Again, I've prepared &lt;a href="./bash-1.14.7-machines-config.patch"&gt;a patch&lt;/a&gt; that does this.&lt;/p&gt;
+&lt;h2 id="building"&gt;Building&lt;/h2&gt;
+&lt;p&gt;With those adjustments, it should build, but with quite a few warnings. Making a couple of changes to &lt;code&gt;CFLAGS&lt;/code&gt; should fix that:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;make CFLAGS=&amp;#39;-O -g -Werror -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -Wno-deprecated-declarations -include stdio.h -include stdlib.h -include string.h -Dexp2=bash_exp2&amp;#39;&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;That's a doozy! Let's break it down:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;-O -g&lt;/code&gt; The default value for CFLAGS (defined in &lt;code&gt;cpp-Makefile&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;-Werror&lt;/code&gt; Treat warnings as errors; force us to deal with any issues.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;-Wno-int-to-pointer-cast -Wno-pointer-to-int-cast&lt;/code&gt; Allow casting between integers and pointers. Unfortunately, the way this version of Bash was designed requires this.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;-Wno-deprecated-declarations&lt;/code&gt; The &lt;code&gt;getwd&lt;/code&gt; function in &lt;code&gt;unistd.h&lt;/code&gt; is considered deprecated (use &lt;code&gt;getcwd&lt;/code&gt; instead). However, if &lt;code&gt;getcwd&lt;/code&gt; is available, Bash uses it's own &lt;code&gt;getwd&lt;/code&gt; wrapper around &lt;code&gt;getcwd&lt;/code&gt; (implemented in &lt;code&gt;general.c&lt;/code&gt;), and only uses the signature from &lt;code&gt;unistd.h&lt;/code&gt;, not the actuall implementation from libc.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;-include stdio.h -include stdlib.h -include string.h&lt;/code&gt; Several files are missing these header file includes. If not for &lt;code&gt;-Werror&lt;/code&gt;, the default function signature fallbacks would work.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;-Dexp2=bash_exp2&lt;/code&gt; Avoid a conflict between the parser's &lt;code&gt;exp2&lt;/code&gt; helper function and &lt;code&gt;math.h&lt;/code&gt;'s base-2 exponential function.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;Have fun, software archaeologists!&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2015 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/purdue-cs-login.md"/>
+ <link rel="alternate" type="text/html" href="/blog/purdue-cs-login.html"/>
+ <id>https://lukeshu.com/blog/purdue-cs-login.html</id>
+ <updated>2016-03-21T02:34:10-04:00</updated>
+ <published>2015-02-06T00:00:00+00:00</published>
+ <title>Customizing your login on Purdue CS computers (WIP, but updated)</title>
+ <content type="html">&lt;h1 id="customizing-your-login-on-purdue-cs-computers-wip-but-updated"&gt;Customizing your login on Purdue CS computers (WIP, but updated)&lt;/h1&gt;
+&lt;blockquote&gt;
+&lt;p&gt;This article is currently a Work-In-Progress. Other than the one place where I say &amp;quot;I'm not sure&amp;quot;, the GDM section is complete. The network shares section is a mess, but has some good information.&lt;/p&gt;
+&lt;/blockquote&gt;
+&lt;p&gt;Most CS students at Purdue spend a lot of time on the lab boxes, but don't know a lot about them. This document tries to fix that.&lt;/p&gt;
+&lt;p&gt;The lab boxes all run Gentoo.&lt;/p&gt;
+&lt;h2 id="gdm-the-gnome-display-manager"&gt;GDM, the Gnome Display Manager&lt;/h2&gt;
+&lt;p&gt;The boxes run &lt;code&gt;gdm&lt;/code&gt; (Gnome Display Manager) 2.20.11 for the login screen. This is an old version, and has a couple behaviors that are slightly different than new versions, but here are the important bits:&lt;/p&gt;
+&lt;p&gt;System configuration:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;/usr/share/gdm/defaults.conf&lt;/code&gt; (lower precidence)&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;/etc/X11/gdm/custom.conf&lt;/code&gt; (higher precidence)&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;User configuration:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;~/.dmrc&lt;/code&gt; (more recent versions use &lt;code&gt;~/.desktop&lt;/code&gt;, but Purdue boxes aren't running more recent versions)&lt;/li&gt;
+&lt;/ul&gt;
+&lt;h3 id="purdues-gdm-configuration"&gt;Purdue's GDM configuration&lt;/h3&gt;
+&lt;p&gt;Now, &lt;code&gt;custom.conf&lt;/code&gt; sets&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;BaseXsession=/usr/local/share/xsessions/Xsession
+SessionDesktopDir=/usr/local/share/xsessions/&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;This is important, because there are &lt;em&gt;multiple&lt;/em&gt; locations that look like these files; I take it that they were used at sometime in the past. Don't get tricked into thinking that it looks at &lt;code&gt;/etc/X11/gdm/Xsession&lt;/code&gt; (which exists, and is where it would look by default).&lt;/p&gt;
+&lt;p&gt;If you look at the GDM login screen, it has a &amp;quot;Sessions&amp;quot; button that opens a prompt where you can select any of several sessions:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;Last session&lt;/li&gt;
+&lt;li&gt;1. MATE (&lt;code&gt;mate.desktop&lt;/code&gt;; &lt;code&gt;Exec=mate-session&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;2. CS Default Session (&lt;code&gt;default.desktop&lt;/code&gt;; &lt;code&gt;Exec=default&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;3. Custom Session (&lt;code&gt;custom.desktop&lt;/code&gt;; &lt;code&gt;Exec=custom&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;4. FVWM2 (&lt;code&gt;fvwm2.desktop&lt;/code&gt;; &lt;code&gt;Exec=fvwm2&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;5. gnome.desktop (&lt;code&gt;gnome.desktop&lt;/code&gt;; &lt;code&gt;Exec=gnome-session&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;6. KDE (&lt;code&gt;kde.desktop&lt;/code&gt;, &lt;code&gt;Exec=startkde&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;Failsafe MATE (&lt;code&gt;ShowGnomeFailsafeSession=true&lt;/code&gt;)&lt;/li&gt;
+&lt;li&gt;Failsafe Terminal (&lt;code&gt;ShowXtermFailsafeSession=true&lt;/code&gt;)&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;The main 6 are configured by the &lt;code&gt;.desktop&lt;/code&gt; files in &lt;code&gt;SessionDesktopDir=/usr/local/share/xsessions&lt;/code&gt;; the last 2 are auto-generated. The reason &lt;code&gt;ShowGnomeFailsafeSession&lt;/code&gt; correctly generates a Mate session instead of a Gnome session is because of the patch &lt;code&gt;/p/portage/*/overlay/gnome-base/gdm/files/gdm-2.20.11-mate.patch&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;I'm not sure why Gnome shows up as &lt;code&gt;gnome.desktop&lt;/code&gt; instead of &lt;code&gt;GNOME&lt;/code&gt; as specified by &lt;code&gt;gnome.desktop:Name&lt;/code&gt;. I imagine it might be something related to the aforementioned patch, but I can't find anything in the patch that looks like it would screw that up; at least not without a better understanding of GDM's code.&lt;/p&gt;
+&lt;p&gt;Which of the main 6 is used by default (&amp;quot;Last Session&amp;quot;) is configured with &lt;code&gt;~/.dmrc:Session&lt;/code&gt;, which contains the basename of the associated &lt;code&gt;.desktop&lt;/code&gt; file (that is, without any directory part or file extension).&lt;/p&gt;
+&lt;p&gt;Every one of the &lt;code&gt;.desktop&lt;/code&gt; files sets &lt;code&gt;Type=XSession&lt;/code&gt;, which means that instead of running the argument in &lt;code&gt;Exec=&lt;/code&gt; directly, it passes it as arguments to the &lt;code&gt;Xsession&lt;/code&gt; program (in the location configured by &lt;code&gt;BaseXsession&lt;/code&gt;).&lt;/p&gt;
+&lt;h4 id="xsession"&gt;Xsession&lt;/h4&gt;
+&lt;p&gt;So, now we get to read &lt;code&gt;/usr/local/share/xsessions/Xsession&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;Before it does anything else, it:&lt;/p&gt;
+&lt;ol type="1"&gt;
+&lt;li&gt;&lt;code&gt;. /etc/profile.env&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;unset ROOTPATH&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;Try to set up logging to one of &lt;code&gt;~/.xsession-errors&lt;/code&gt;, &lt;code&gt;$TMPDIR/xses-$USER&lt;/code&gt;, or &lt;code&gt;/tmp/xses-$USER&lt;/code&gt; (it tries them in that order).&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;xsetroot -default&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;Fiddles with the maximum number of processes.&lt;/li&gt;
+&lt;/ol&gt;
+&lt;p&gt;After that, it handles these 3 &amp;quot;special&amp;quot; arguments that were given to it by various &lt;code&gt;.desktop&lt;/code&gt; &lt;code&gt;Exec=&lt;/code&gt; lines:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;failsafe&lt;/code&gt;: Runs a single xterm window. NB: This is NOT run by either of the failsafe options. It is likey a vestiage from a prior configuration.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;startkde&lt;/code&gt;: Displays a message saying KDE is no longer available.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;gnome-session&lt;/code&gt;: Displays a message saying GNOME has been replaced by MATE.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;Assuming that none of those were triggered, it then does:&lt;/p&gt;
+&lt;ol type="1"&gt;
+&lt;li&gt;&lt;code&gt;source ~/.xprofile&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;xrdb -merge ~/.Xresources&lt;/code&gt;&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;xmodmap ~/.xmodmaprc&lt;/code&gt;&lt;/li&gt;
+&lt;/ol&gt;
+&lt;p&gt;Finally, it has a switch statement over the arguments given to it by the various &lt;code&gt;.desktop&lt;/code&gt; &lt;code&gt;Exec=&lt;/code&gt; lines:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;custom&lt;/code&gt;: Executes &lt;code&gt;~/.xsession&lt;/code&gt;.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;default&lt;/code&gt;: Executes &lt;code&gt;~/.Xrc.cs&lt;/code&gt;.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;mate-session&lt;/code&gt;: It has this whole script to start DBus, run the &lt;code&gt;mate-session&lt;/code&gt; command, then cleanup when it's done.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;*&lt;/code&gt; (&lt;code&gt;fvwm2&lt;/code&gt;): Runs &lt;code&gt;eval exec &amp;quot;$@&amp;quot;&lt;/code&gt;, which results in it executing the &lt;code&gt;fvwm2&lt;/code&gt; command.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;h2 id="network-shares"&gt;Network Shares&lt;/h2&gt;
+&lt;p&gt;Your data is on various hosts. I believe most undergrads have their data on &lt;code&gt;data.cs.purdue.edu&lt;/code&gt; (or just &lt;a href="https://en.wikipedia.org/wiki/Data_%28Star_Trek%29"&gt;&lt;code&gt;data&lt;/code&gt;&lt;/a&gt;). Others have theirs on &lt;a href="http://swfanon.wikia.com/wiki/Antor"&gt;&lt;code&gt;antor&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Tux"&gt;&lt;code&gt;tux&lt;/code&gt;&lt;/a&gt; (that I know of).&lt;/p&gt;
+&lt;p&gt;Most of the boxes with tons of storage have many network cards; each with a different IP; a single host's IPs are mostly the same, but with varying 3rd octets. For example, &lt;code&gt;data&lt;/code&gt; is 128.10.X.13. If you need a particular value of X, but don't want to remember the other octets; they are individually addressed with &lt;code&gt;BASENAME-NUMBER.cs.purdue.edu&lt;/code&gt;. For example, &lt;code&gt;data-25.cs.purdu.edu&lt;/code&gt; is 128.10.25.13.&lt;/p&gt;
+&lt;p&gt;They use &lt;a href="https://www.kernel.org/pub/linux/daemons/autofs/"&gt;AutoFS&lt;/a&gt; quite extensively. The maps are generated dynamically by &lt;code&gt;/etc/autofs/*.map&lt;/code&gt;, which are all symlinks to &lt;code&gt;/usr/libexec/amd2autofs&lt;/code&gt;. As far as I can tell, &lt;code&gt;amd2autofs&lt;/code&gt; is custom to Purdue. Its source lives in &lt;code&gt;/p/portage/*/overlay/net-fs/autofs/files/amd2autofs.c&lt;/code&gt;. The name appears to be a misnomer; seems to claim to dynamically translate from the configuration of &lt;a href="http://www.am-utils.org/"&gt;Auto Mounter Daemon (AMD)&lt;/a&gt; to AutoFS, but it actually talks to NIS. It does so using the &lt;code&gt;yp&lt;/code&gt; interface, which is in Glibc for compatibility, but is undocumented. For documentation for that interface, look at the one of the BSDs, or Mac OS X. From the comments in the file, it appears that it once did look at the AMD configuration, but has since been changed.&lt;/p&gt;
+&lt;p&gt;There are 3 mountpoints using AutoFS: &lt;code&gt;/homes&lt;/code&gt;, &lt;code&gt;/p&lt;/code&gt;, and &lt;code&gt;/u&lt;/code&gt;. &lt;code&gt;/homes&lt;/code&gt; creates symlinks on-demand from &lt;code&gt;/homes/USERNAME&lt;/code&gt; to &lt;code&gt;/u/BUCKET/USERNAME&lt;/code&gt;. &lt;code&gt;/u&lt;/code&gt; mounts NFS shares to &lt;code&gt;/u/SERVERNAME&lt;/code&gt; on-demand, and creates symlinks from &lt;code&gt;/u/BUCKET&lt;/code&gt; to &lt;code&gt;/u/SERVERNAME/BUCKET&lt;/code&gt; on-demand. &lt;code&gt;/p&lt;/code&gt; mounts on-demand various NFS shares that are organized by topic; the Xinu/MIPS tools are in &lt;code&gt;/p/xinu&lt;/code&gt;, the Portage tree is in &lt;code&gt;/p/portage&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;I'm not sure how &lt;code&gt;scratch&lt;/code&gt; works; it seems to be heterogenous between different servers and families of lab boxes. Sometimes it's in &lt;code&gt;/u&lt;/code&gt;, sometimes it isn't.&lt;/p&gt;
+&lt;p&gt;This 3rd-party documentation was very helpful to me: &lt;a href="http://www.linux-consulting.com/Amd_AutoFS/" class="uri"&gt;http://www.linux-consulting.com/Amd_AutoFS/&lt;/a&gt; It's where Gentoo points for the AutoFS homepage, as it doesn't have a real homepage. Arch just points to FreshMeat. Debian points to kernel.org.&lt;/p&gt;
+&lt;h3 id="lore"&gt;Lore&lt;/h3&gt;
+&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/List_of_Star_Trek:_The_Next_Generation_characters#Lore"&gt;&lt;code&gt;lore&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
+&lt;p&gt;Lore is a SunOS 5.10 box running on Sun-Fire V445 (sun4u) hardware. SunOS is NOT GNU/Linux, and sun4u is NOT x86.&lt;/p&gt;
+&lt;p&gt;Instead of &lt;code&gt;/etc/fstab&lt;/code&gt; it is &lt;code&gt;/etc/mnttab&lt;/code&gt;.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2015 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/make-memoize.md"/>
+ <link rel="alternate" type="text/html" href="/blog/make-memoize.html"/>
+ <id>https://lukeshu.com/blog/make-memoize.html</id>
+ <updated>2016-03-21T02:34:10-04:00</updated>
+ <published>2014-11-20T00:00:00+00:00</published>
+ <title>A memoization routine for GNU Make functions</title>
+ <content type="html">&lt;h1 id="a-memoization-routine-for-gnu-make-functions"&gt;A memoization routine for GNU Make functions&lt;/h1&gt;
+&lt;p&gt;I'm a big fan of &lt;a href="https://www.gnu.org/software/make/"&gt;GNU Make&lt;/a&gt;. I'm pretty knowledgeable about it, and was pretty active on the help-make mailing list for a while. Something that many experienced make-ers know of is John Graham-Cumming's &amp;quot;GNU Make Standard Library&amp;quot;, or &lt;a href="http://gmsl.sourceforge.net/"&gt;GMSL&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;I don't like to use it, as I'm capable of defining macros myself as I need them instead of pulling in a 3rd party dependency (and generally like to stay away from the kind of Makefile that would lean heavily on something like GMSL).&lt;/p&gt;
+&lt;p&gt;However, one really neat thing that GMSL offers is a way to memoize expensive functions (such as those that shell out). I was considering pulling in GMSL for one of my projects, almost just for the &lt;code&gt;memoize&lt;/code&gt; function.&lt;/p&gt;
+&lt;p&gt;However, John's &lt;code&gt;memoize&lt;/code&gt; has a couple short-comings that made it unsuitable for my needs.&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;Only allows functions that take one argument.&lt;/li&gt;
+&lt;li&gt;Considers empty values to be unset; for my needs, an empty string is a valid value that should be cached.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;So, I implemented my own, more flexible memoization routine for Make.&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# This definition of `rest` is equivalent to that in GMSL
+rest = $(wordlist 2,$(words $1),$1)
+
+# How to use: Define 2 variables (the type you would pass to $(call):
+# `_&lt;var&gt;NAME&lt;/var&gt;_main` and `_&lt;var&gt;NAME&lt;/var&gt;_hash`. Now, `_&lt;var&gt;NAME&lt;/var&gt;_main` is the function getting
+# memoized, and _&lt;var&gt;NAME&lt;/var&gt;_hash is a function that hashes the function arguments
+# into a string suitable for a variable name.
+#
+# Then, define the final function like:
+#
+# &lt;var&gt;NAME&lt;/var&gt; = $(foreach func,&lt;var&gt;NAME&lt;/var&gt;,$(memoized))
+
+_main = $(_$(func)_main)
+_hash = __memoized_$(_$(func)_hash)
+memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash)))&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;However, I later removed it from the Makefile, as I &lt;a href="https://projects.parabola.nu/~lukeshu/maven-dist.git/commit/?id=fec5a7281b3824cb952aa0bb76bbbaa41eaafdf9"&gt;re-implemented&lt;/a&gt; the bits that it memoized in a more efficient way, such that memoization was no longer needed, and the whole thing was faster.&lt;/p&gt;
+&lt;p&gt;Later, I realized that my memoized routine could have been improved by replacing &lt;code&gt;func&lt;/code&gt; with &lt;code&gt;$0&lt;/code&gt;, which would simplify how the final function is declared:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# This definition of `rest` is equivalent to that in GMSL
+rest = $(wordlist 2,$(words $1),$1)
+
+# How to use:
+#
+# _&lt;var&gt;NAME&lt;/var&gt;_main = &lt;var&gt;your main function to be memoized&lt;/var&gt;
+# _&lt;var&gt;NAME&lt;/var&gt;_hash = &lt;var&gt;your hash function for parameters&lt;/var&gt;
+# &lt;var&gt;NAME&lt;/var&gt; = $(memoized)
+#
+# The output of your hash function should be a string following
+# the same rules that variable names follow.
+
+_main = $(_$0_main)
+_hash = __memoized_$(_$0_hash)
+memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash)))&lt;/pre&gt;
+&lt;p&gt;&lt;/code&gt;&lt;/p&gt;
+&lt;p&gt;Now, I'm pretty sure that should work, but I have only actually tested the first version.&lt;/p&gt;
+&lt;h2 id="tldr"&gt;TL;DR&lt;/h2&gt;
+&lt;p&gt;Avoid doing things in Make that would make you lean on complex solutions like an external memoize function.&lt;/p&gt;
+&lt;p&gt;However, if you do end up needing a more flexible memoize routine, I wrote one that you can use.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="http://www.wtfpl.net/txt/copying/"&gt;WTFPL-2&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/ryf-routers.md"/>
+ <link rel="alternate" type="text/html" href="/blog/ryf-routers.html"/>
+ <id>https://lukeshu.com/blog/ryf-routers.html</id>
+ <updated>2014-09-12T00:21:05-04:00</updated>
+ <published>2014-09-12T00:00:00+00:00</published>
+ <title>I'm excited about the new RYF-certified routers from ThinkPenguin</title>
+ <content type="html">&lt;h1 id="im-excited-about-the-new-ryf-certified-routers-from-thinkpenguin"&gt;I'm excited about the new RYF-certified routers from ThinkPenguin&lt;/h1&gt;
+&lt;p&gt;I just learned that on Wednesday, the FSF &lt;a href="https://www.fsf.org/resources/hw/endorsement/thinkpenguin"&gt;awarded&lt;/a&gt; the &lt;abbr title="Respects Your Freedom"&gt;RYF&lt;/abbr&gt; certification to the &lt;a href="https://www.thinkpenguin.com/TPE-NWIFIROUTER"&gt;Think Penguin TPE-NWIFIROUTER&lt;/a&gt; wireless router.&lt;/p&gt;
+&lt;p&gt;I didn't find this information directly published up front, but simply: It is a re-branded &lt;strong&gt;TP-Link TL-841ND&lt;/strong&gt; modded to be running &lt;a href="http://librecmc.com/"&gt;libreCMC&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;I've been a fan of the TL-841/740 line of routers for several years now. They are dirt cheap (if you go to Newegg and sort by &amp;quot;cheapest,&amp;quot; it's frequently the TL-740N), are extremely reliable, and run OpenWRT like a champ. They are my go-to routers.&lt;/p&gt;
+&lt;p&gt;(And they sure beat the snot out of the Arris TG862 that it seems like everyone has in their homes now. I hate that thing, it even has buggy packet scheduling.)&lt;/p&gt;
+&lt;p&gt;So this announcement is &lt;del&gt;doubly&lt;/del&gt;triply exciting for me:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;I have a solid recommendation for a router that doesn't require me or them to manually install an after-market firmware (buy it from ThinkPenguin).&lt;/li&gt;
+&lt;li&gt;If it's for me, or someone technical, I can cut costs by getting a stock TP-Link from Newegg, installing libreCMC ourselves.&lt;/li&gt;
+&lt;li&gt;I can install a 100% libre distribution on my existing routers (until recently, they were not supported by any of the libre distributions, not for technical reasons, but lack of manpower).&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;I hope to get libreCMC installed on my boxes this weekend!&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/what-im-working-on-fall-2014.md"/>
+ <link rel="alternate" type="text/html" href="/blog/what-im-working-on-fall-2014.html"/>
+ <id>https://lukeshu.com/blog/what-im-working-on-fall-2014.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2014-09-11T00:00:00+00:00</published>
+ <title>What I'm working on (Fall 2014)</title>
+ <content type="html">&lt;h1 id="what-im-working-on-fall-2014"&gt;What I'm working on (Fall 2014)&lt;/h1&gt;
+&lt;p&gt;I realized today that I haven't updated my log in a while, and I don't have any &amp;quot;finished&amp;quot; stuff to show off right now, but I should just talk about all the cool stuff I'm working on right now.&lt;/p&gt;
+&lt;h2 id="static-parsing-of-subshells"&gt;Static parsing of subshells&lt;/h2&gt;
+&lt;p&gt;Last year I wrote a shell (for my Systems Programming class); however, I went above-and-beyond and added some really novel features. In my opinion, the most significant is that it parses arbitrarily deep subshells in one pass, instead of deferring them until execution. No shell that I know of does this.&lt;/p&gt;
+&lt;p&gt;At first this sounds like a really difficult, but minor feature. Until you think about scripting, and maintenance of those scripts. Being able to do a full syntax check of a script is &lt;em&gt;crucial&lt;/em&gt; for long-term maintenance, yet it's something that is missing from every major shell. I'd love to get this code merged into bash. It would be incredibly useful for &lt;a href="/git/mirror/parabola/packages/libretools.git"&gt;some software I maintain&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;Anyway, I'm trying to publish this code, but because of a recent kerfuffle with a student publishing all of his projects on the web (and other students trying to pass it off as their own), I'm being cautious with this and making sure Purdue is alright with what I'm putting online.&lt;/p&gt;
+&lt;h2 id="stateless-user-configuration-for-pamnss"&gt;&lt;a href="https://lukeshu.com/git/mirror/parabola/hackers.git/log/?h=lukeshu/restructure"&gt;Stateless user configuration for PAM/NSS&lt;/a&gt;&lt;/h2&gt;
+&lt;p&gt;Parabola GNU/Linux-libre users know that over this summer, we had a &lt;em&gt;mess&lt;/em&gt; with server outages. One of the servers is still out (due to things out of our control), and we don't have some of the data on it (because volunteer developers are terrible about back-ups, apparently).&lt;/p&gt;
+&lt;p&gt;This has caused us to look at how we manage our servers, back-ups, and several other things.&lt;/p&gt;
+&lt;p&gt;One thing that I've taken on as my pet project is making sure that if a server goes down, or we need to migrate (for example, Jon is telling us that he wants us to hurry up and switch to the new 64 bit hardware so he can turn off the 32 bit box), we can spin up a new server from scratch pretty easily. Part of that is making configurations stateless, and dynamic based on external data; having data be located in one place instead of duplicated across 12 config files and 3 databases... on the same box.&lt;/p&gt;
+&lt;p&gt;Right now, that's looking like some custom software interfacing with OpenLDAP and OpenSSH via sockets (OpenLDAP being a middle-man between us and PAM (Linux) and NSS (libc)). However, the OpenLDAP documentation is... inconsistent and frustrating. I might end up hacking up the LDAP modules for NSS and PAM to talk to our system directly, and cut OpenLDAP out of the picture. We'll see!&lt;/p&gt;
+&lt;p&gt;PS: Pablo says that tomorrow we should be getting out-of-band access to the drive of the server that is down, so that we can finally restore those services on a different server.&lt;/p&gt;
+&lt;h2 id="project-leaguer"&gt;&lt;a href="https://lukeshu.com/git/mirror/leaguer.git/"&gt;Project Leaguer&lt;/a&gt;&lt;/h2&gt;
+&lt;p&gt;Last year, some friends and I began writing some &amp;quot;eSports tournament management software&amp;quot;, primarily targeting League of Legends (though it has a module system that will allow it to support tons of different data sources). We mostly got it done last semester, but it had some rough spots and sharp edges we need to work out. Because we were all out of communication for the summer, we didn't work on it very much (but we did a little!). It's weird that I care about this, because I'm not a gamer. Huh, I guess coding with friends is just fun.&lt;/p&gt;
+&lt;p&gt;Anyway, this year, &lt;a href="https://github.com/AndrewMurrell"&gt;Andrew&lt;/a&gt;, &lt;a href="https://github.com/DavisLWebb"&gt;Davis&lt;/a&gt;, and I are planning to get it to a polished state by the end of the semester. We could probably do it faster, but we'd all also like to focus on classes and other projects a little more.&lt;/p&gt;
+&lt;h2 id="c1"&gt;C+=1&lt;/h2&gt;
+&lt;p&gt;People tend to lump C and C++ together, which upsets me, because I love C, but have a dislike for C++. That's not to say that C++ is entirely bad; it has some good features. My &amp;quot;favorite&amp;quot; code is actually code that is basically C, but takes advantage of a couple C++ features, while still being idiomatic C, not C++.&lt;/p&gt;
+&lt;p&gt;Anyway, with the perspective of history (what worked and what didn't), and a slightly opinionated view on language design (I'm pretty much a Rob Pike fan-boy), I thought I'd try to tackle &amp;quot;object-oriented C&amp;quot; with roughly the same design criteria as Stroustrup had when designing C++. I'm calling mine C+=1, for obvious reasons.&lt;/p&gt;
+&lt;p&gt;I haven't published anything yet, because calling it &amp;quot;working&amp;quot; would be stretching the truth. But I am using it for my assignments in CS 334 (Intro to Graphics), so it should move along fairly quickly, as my grade depends on it.&lt;/p&gt;
+&lt;p&gt;I'm not taking it too seriously; I don't expect it to be much more than a toy language, but it is an excuse to dive into the GCC internals.&lt;/p&gt;
+&lt;h2 id="projects-that-ive-put-on-the-back-burner"&gt;Projects that I've put on the back-burner&lt;/h2&gt;
+&lt;p&gt;I've got several other projects that I'm putting on hold for a while.&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;code&gt;maven-dist&lt;/code&gt; (was hosted with Parabola, apparently I haven't pushed it anywhere except the server that is down): A tool to build Apache Maven from source. That sounds easy, it's open source, right? Well, except that Maven is the build system from hell. It doesn't support cyclic dependencies, yet uses them internally to build itself. It &lt;em&gt;loves&lt;/em&gt; to just get binaries from Maven Central to &amp;quot;optimize&amp;quot; the build process. It depends on code that depends on compiler bugs that no longer exist (which I guess means that &lt;em&gt;no one&lt;/em&gt; has tried to build it from source after it was originally published). I've been working on-and-off on this for more than a year. My favorite part of it was writing a &lt;a href="/dump/jflex2jlex.sed.txt"&gt;sed script&lt;/a&gt; that translates a JFlex grammar specification into a JLex grammar, which is used to bootstrap JFlex; its both gross and delightful at the same time.&lt;/li&gt;
+&lt;li&gt;Integration between &lt;code&gt;dbscripts&lt;/code&gt; and &lt;code&gt;abslibre&lt;/code&gt;. If you search IRC logs, mailing lists, and ParabolaWiki, you can find numerous rants by me against &lt;a href="/git/mirror/parabola/dbscripts.git/tree/db-sync"&gt;&lt;code&gt;dbscripts:db-sync&lt;/code&gt;&lt;/a&gt;. I just hate the data-flow, it is almost designed to make things get out of sync, and broken. I mean, does &lt;a href="/dump/parabola-data-flow.svg"&gt;this&lt;/a&gt; look like a simple diagram? For contrast, &lt;a href="/dump/parabola-data-flow-xbs.svg"&gt;here's&lt;/a&gt; a rough (slightly incomplete) diagram of what I want to replace it with.&lt;/li&gt;
+&lt;li&gt;Git backend for MediaWiki (or, pulling out the rendering module of MediaWiki). I've made decent progress on that front, but there is &lt;em&gt;crazy&lt;/em&gt; de-normalization going on in the MediaWiki schema that makes this very difficult. I'm sure some of it is for historical reasons, and some of it for performance, but either way it is a mess for someone trying to neatly gut that part of the codebase.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;h2 id="other"&gt;Other&lt;/h2&gt;
+&lt;p&gt;I should consider doing a write-up of deterministic-&lt;code&gt;tar&lt;/code&gt; behavior (something that I've been implementing in Parabola for a while, meanwhile the Debian people have also been working on it).&lt;/p&gt;
+&lt;p&gt;I should also consider doing a &amp;quot;post-mortem&amp;quot; of &lt;a href="https://lukeshu.com/git/mirror/parabola/packages/pbs-tools.git/"&gt;PBS&lt;/a&gt;, which never actually got used, but launched XBS (part of the &lt;code&gt;dbscripts&lt;/code&gt;/&lt;code&gt;abslibre&lt;/code&gt; integration mentioned above), as well as serving as a good test-bed for features that did get implemented.&lt;/p&gt;
+&lt;p&gt;I over-use the word &amp;quot;anyway.&amp;quot;&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/rails-improvements.md"/>
+ <link rel="alternate" type="text/html" href="/blog/rails-improvements.html"/>
+ <id>https://lukeshu.com/blog/rails-improvements.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2014-05-08T00:00:00+00:00</published>
+ <title>Miscellaneous ways to improve your Rails experience</title>
+ <content type="html">&lt;h1 id="miscellaneous-ways-to-improve-your-rails-experience"&gt;Miscellaneous ways to improve your Rails experience&lt;/h1&gt;
+&lt;p&gt;Recently, I've been working on &lt;a href="https://github.com/LukeShu/leaguer"&gt;a Rails web application&lt;/a&gt;, that's really the baby of a friend of mine. Anyway, through its development, I've come up with a couple things that should make your interactions with Rails more pleasant.&lt;/p&gt;
+&lt;h2 id="auto-reload-classes-from-other-directories-than-app"&gt;Auto-(re)load classes from other directories than &lt;code&gt;app/&lt;/code&gt;&lt;/h2&gt;
+&lt;p&gt;The development server automatically loads and reloads files from the &lt;code&gt;app/&lt;/code&gt; directory, which is extremely nice. However, most web applications are going to involve modules that aren't in that directory; and editing those files requires re-starting the server for the changes to take effect.&lt;/p&gt;
+&lt;p&gt;Adding the following lines to your &lt;a href="https://github.com/LukeShu/leaguer/blob/c846cd71411ec3373a5229cacafe0df6b3673543/config/application.rb#L15"&gt;&lt;code&gt;config/application.rb&lt;/code&gt;&lt;/a&gt; will allow it to automatically load and reload files from the &lt;code&gt;lib/&lt;/code&gt; directory. You can of course change this to whichever directory/ies you like.&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;module YourApp
+ class Application &amp;lt; Rails::Application
+ …
+ config.autoload_paths += [&amp;quot;#{Rails.root}/lib&amp;quot;]
+ config.watchable_dirs[&amp;quot;#{Rails.root}/lib&amp;quot;] = [:rb]
+ …
+ end
+end&lt;/code&gt;&lt;/pre&gt;
+&lt;h2 id="have-submit_tag-generate-a-button-instead-of-an-input"&gt;Have &lt;code&gt;submit_tag&lt;/code&gt; generate a button instead of an input&lt;/h2&gt;
+&lt;p&gt;In HTML, the &lt;code&gt;&amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;/code&gt; tag styles slightly differently than other inputs or buttons. It is impossible to precisely controll the hight via CSS, which makes designing forms a pain. This is particularly noticable if you use Bootstrap 3, and put it next to another button; the submit button will be slightly shorter vertically.&lt;/p&gt;
+&lt;p&gt;The obvious fix here is to use &lt;code&gt;&amp;lt;button type=&amp;quot;submit&amp;quot;&amp;gt;&lt;/code&gt; instead. The following code will modify the default Rails form helpers to generate a button tag instead of an input tag. Just stick the code in &lt;a href="https://github.com/LukeShu/leaguer/blob/521eae01be1ca3f69b47b3170a0548c3268f4a22/config/initializers/form_improvements.rb"&gt;&lt;code&gt;config/initializers/form_improvements.rb&lt;/code&gt;&lt;/a&gt;; it will override &lt;code&gt;ActionView::Hlepers::FormTagHelper#submit_tag&lt;/code&gt;. It is mostly the standard definition of the function, except for the last line, which has changed.&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# -*- ruby-indent-level: 2; indent-tabs-mode: nil -*-
+module ActionView
+ module Helpers
+ module FormTagHelper
+
+ # This is modified from actionpack-4.0.2/lib/action_view/helpers/form_tag_helper.rb#submit_tag
+ def submit_tag(value = &amp;quot;Save changes&amp;quot;, options = {})
+ options = options.stringify_keys
+
+ if disable_with = options.delete(&amp;quot;disable_with&amp;quot;)
+ message = &amp;quot;:disable_with option is deprecated and will be removed from Rails 4.1. &amp;quot; \
+ &amp;quot;Use &amp;#39;data: { disable_with: \&amp;#39;Text\&amp;#39; }&amp;#39; instead.&amp;quot;
+ ActiveSupport::Deprecation.warn message
+
+ options[&amp;quot;data-disable-with&amp;quot;] = disable_with
+ end
+
+ if confirm = options.delete(&amp;quot;confirm&amp;quot;)
+ message = &amp;quot;:confirm option is deprecated and will be removed from Rails 4.1. &amp;quot; \
+ &amp;quot;Use &amp;#39;data: { confirm: \&amp;#39;Text\&amp;#39; }&amp;#39; instead&amp;#39;.&amp;quot;
+ ActiveSupport::Deprecation.warn message
+
+ options[&amp;quot;data-confirm&amp;quot;] = confirm
+ end
+
+ content_tag(:button, value, { &amp;quot;type&amp;quot; =&amp;gt; &amp;quot;submit&amp;quot;, &amp;quot;name&amp;quot; =&amp;gt; &amp;quot;commit&amp;quot;, &amp;quot;value&amp;quot; =&amp;gt; value }.update(options))
+ end
+
+ end
+ end
+end&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;I'll probably update this page as I tweak other things I don't like.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/bash-redirection.md"/>
+ <link rel="alternate" type="text/html" href="/blog/bash-redirection.html"/>
+ <id>https://lukeshu.com/blog/bash-redirection.html</id>
+ <updated>2014-05-08T14:36:47-04:00</updated>
+ <published>2014-02-13T00:00:00+00:00</published>
+ <title>Bash redirection</title>
+ <content type="html">&lt;h1 id="bash-redirection"&gt;Bash redirection&lt;/h1&gt;
+&lt;p&gt;Apparently, too many people don't understand Bash redirection. They might get the basic syntax, but they think of the process as declarative; in Bourne-ish shells, it is procedural.&lt;/p&gt;
+&lt;p&gt;In Bash, streams are handled in terms of &amp;quot;file descriptors&amp;quot; of &amp;quot;FDs&amp;quot;. FD 0 is stdin, FD 1 is stdout, and FD 2 is stderr. The equivalence (or lack thereof) between using a numeric file descriptor, and using the associated file in &lt;code&gt;/dev/*&lt;/code&gt; and &lt;code&gt;/proc/*&lt;/code&gt; is interesting, but beyond the scope of this article.&lt;/p&gt;
+&lt;h2 id="step-1-pipes"&gt;Step 1: Pipes&lt;/h2&gt;
+&lt;p&gt;To quote the Bash manual:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;A &amp;#39;pipeline&amp;#39; is a sequence of simple commands separated by one of the
+control operators &amp;#39;|&amp;#39; or &amp;#39;|&amp;amp;&amp;#39;.
+
+ The format for a pipeline is
+ [time [-p]] [!] COMMAND1 [ [| or |&amp;amp;] COMMAND2 ...]&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Now, &lt;code&gt;|&amp;amp;&lt;/code&gt; is just shorthand for &lt;code&gt;2&amp;gt;&amp;amp;1 |&lt;/code&gt;, the pipe part happens here, but the &lt;code&gt;2&amp;gt;&amp;amp;1&lt;/code&gt; part doesn't happen until step 2.&lt;/p&gt;
+&lt;p&gt;First, if the command is part of a pipeline, the pipes are set up. For every instance of the &lt;code&gt;|&lt;/code&gt; metacharacter, Bash creates a pipe (&lt;code&gt;pipe(3)&lt;/code&gt;), and duplicates (&lt;code&gt;dup2(3)&lt;/code&gt;) the write end of the pipe to FD 1 of the process on the left side of the &lt;code&gt;|&lt;/code&gt;, and duplicate the read end of the pipe to FD 0 of the process on the right side.&lt;/p&gt;
+&lt;h2 id="step-2-redirections"&gt;Step 2: Redirections&lt;/h2&gt;
+&lt;p&gt;&lt;em&gt;After&lt;/em&gt; the initial FD 0 and FD 1 fiddling by pipes is done, Bash looks at the redirections. &lt;strong&gt;This means that redirections can override pipes.&lt;/strong&gt;&lt;/p&gt;
+&lt;p&gt;Redirections are read left-to-right, and are executed as they are read, using &lt;code&gt;dup2(right-side, left-side)&lt;/code&gt;. This is where most of the confusion comes from, people think of them as declarative, which leads to them doing the first of these, when they mean to do the second:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;cmd 2&amp;gt;&amp;amp;1 &amp;gt;file # stdout goes to file, stderr goes to stdout
+cmd &amp;gt;file 2&amp;gt;&amp;amp;1 # both stdout and stderr go to file&lt;/code&gt;&lt;/pre&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/java-segfault.md"/>
+ <link rel="alternate" type="text/html" href="/blog/java-segfault.html"/>
+ <id>https://lukeshu.com/blog/java-segfault.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2014-01-13T00:00:00+00:00</published>
+ <title>My favorite bug: segfaults in Java</title>
+ <content type="html">&lt;h1 id="my-favorite-bug-segfaults-in-java"&gt;My favorite bug: segfaults in Java&lt;/h1&gt;
+&lt;blockquote&gt;
+&lt;p&gt;Update: Two years later, I wrote a more detailed version of this article: &lt;a href="./java-segfault-redux.html"&gt;My favorite bug: segfaults in Java (redux)&lt;/a&gt;.&lt;/p&gt;
+&lt;/blockquote&gt;
+&lt;p&gt;I've told this story orally a number of times, but realized that I have never written it down. This is my favorite bug story; it might not be my hardest bug, but it is the one I most like to tell.&lt;/p&gt;
+&lt;h2 id="the-context"&gt;The context&lt;/h2&gt;
+&lt;p&gt;In 2012, I was a Senior programmer on the FIRST Robotics Competition team 1024. For the unfamiliar, the relevant part of the setup is that there are 2 minute and 15 second matches in which you have a 120 pound robot that sometimes runs autonomously, and sometimes is controlled over WiFi from a person at a laptop running stock &amp;quot;driver station&amp;quot; software and modifiable &amp;quot;dashboard&amp;quot; software.&lt;/p&gt;
+&lt;p&gt;That year, we mostly used the dashboard software to allow the human driver and operator to monitor sensors on the robot, one of them being a video feed from a web-cam mounted on it. This was really easy because the new standard dashboard program had a click-and drag interface to add stock widgets; you just had to make sure the code on the robot was actually sending the data.&lt;/p&gt;
+&lt;p&gt;That's great, until when debugging things, the dashboard would suddenly vanish. If it was run manually from a terminal (instead of letting the driver station software launch it), you would see a core dump indicating a segmentation fault.&lt;/p&gt;
+&lt;p&gt;This wasn't just us either; I spoke with people on other teams, everyone who was streaming video had this issue. But, because it only happened every couple of minutes, and a match is only 2:15, it didn't need to run very long, they just crossed their fingers and hoped it didn't happen during a match.&lt;/p&gt;
+&lt;p&gt;The dashboard was written in Java, and the source was available (under a 3-clause BSD license), so I dove in, hunting for the bug. Now, the program did use Java Native Interface to talk to OpenCV, which the video ran through; so I figured that it must be a bug in the C/C++ code that was being called. It was especially a pain to track down the pointers that were causing the issue, because it was hard with native debuggers to see through all of the JVM stuff to the OpenCV code, and the OpenCV stuff is opaque to Java debuggers.&lt;/p&gt;
+&lt;p&gt;Eventually the issue lead me back into the Java code--there was a native pointer being stored in a Java variable; Java code called the native routine to &lt;code&gt;free()&lt;/code&gt; the structure, but then tried to feed it to another routine later. This lead to difficulty again--tracking objects with Java debuggers was hard because they don't expect the program to suddenly segfault; it's Java code, Java doesn't segfault, it throws exceptions!&lt;/p&gt;
+&lt;p&gt;With the help of &lt;code&gt;println()&lt;/code&gt; I was eventually able to see that some code was executing in an order that straight didn't make sense.&lt;/p&gt;
+&lt;h2 id="the-bug"&gt;The bug&lt;/h2&gt;
+&lt;p&gt;The issue was that Java was making an unsafe optimization (I never bothered to figure out if it is the compiler or the JVM making the mistake, I was satisfied once I had a work-around).&lt;/p&gt;
+&lt;p&gt;Java was doing something similar to tail-call optimization with regard to garbage collection. You see, if it is waiting for the return value of a method &lt;code&gt;m()&lt;/code&gt; of object &lt;code&gt;o&lt;/code&gt;, and code in &lt;code&gt;m()&lt;/code&gt; that is yet to be executed doesn't access any other methods or properties of &lt;code&gt;o&lt;/code&gt;, then it will go ahead and consider &lt;code&gt;o&lt;/code&gt; eligible for garbage collection before &lt;code&gt;m()&lt;/code&gt; has finished running.&lt;/p&gt;
+&lt;p&gt;That is normally a safe optimization to make… except for when a destructor method (&lt;code&gt;finalize()&lt;/code&gt;) is defined for the object; the destructor can have side effects, and Java has no way to know whether it is safe for them to happen before &lt;code&gt;m()&lt;/code&gt; has finished running.&lt;/p&gt;
+&lt;h2 id="the-work-around"&gt;The work-around&lt;/h2&gt;
+&lt;p&gt;The routine that the segmentation fault was occurring in was something like:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;public type1 getFrame() {
+ type2 child = this.getChild();
+ type3 var = this.something();
+ // `this` may now be garbage collected
+ return child.somethingElse(var); // segfault comes here
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Where the destructor method of &lt;code&gt;this&lt;/code&gt; calls a method that will &lt;code&gt;free()&lt;/code&gt; native memory that is also accessed by &lt;code&gt;child&lt;/code&gt;; if &lt;code&gt;this&lt;/code&gt; is garbage collected before &lt;code&gt;child.somethingElse()&lt;/code&gt; runs, the backing native code will try to access memory that has been &lt;code&gt;free()&lt;/code&gt;ed, and receive a segmentation fault. That usually didn't happen, as the routines were pretty fast. However, running 30 times a second, eventually bad luck with the garbage collector happens, and the program crashes.&lt;/p&gt;
+&lt;p&gt;The work-around was to insert a bogus call to this to keep &lt;code&gt;this&lt;/code&gt; around until after we were also done with &lt;code&gt;child&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;public type1 getFrame() {
+ type2 child = this.getChild();
+ type3 var = this.something();
+ type1 ret = child.somethingElse(var);
+ this.getSize(); // bogus call to keep `this` around
+ return ret;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Yeah. After spending weeks wading through though thousands of lines of Java, C, and C++, a bogus call to a method I didn't care about was the fix.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2014 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/bash-arrays.md"/>
+ <link rel="alternate" type="text/html" href="/blog/bash-arrays.html"/>
+ <id>https://lukeshu.com/blog/bash-arrays.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2013-10-13T00:00:00+00:00</published>
+ <title>Bash arrays</title>
+ <content type="html">&lt;h1 id="bash-arrays"&gt;Bash arrays&lt;/h1&gt;
+&lt;p&gt;Way too many people don't understand Bash arrays. Many of them argue that if you need arrays, you shouldn't be using Bash. If we reject the notion that one should never use Bash for scripting, then thinking you don't need Bash arrays is what I like to call &amp;quot;wrong&amp;quot;. I don't even mean real scripting; even these little stubs in &lt;code&gt;/usr/bin&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#!/bin/sh
+java -jar /…/something.jar $* # WRONG!&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Command line arguments are exposed as an array, that little &lt;code&gt;$*&lt;/code&gt; is accessing it, and is doing the wrong thing (for the lazy, the correct thing is &lt;code&gt;-- &amp;quot;$@&amp;quot;&lt;/code&gt;). Arrays in Bash offer a safe way preserve field separation.&lt;/p&gt;
+&lt;p&gt;One of the main sources of bugs (and security holes) in shell scripts is field separation. That's what arrays are about.&lt;/p&gt;
+&lt;h2 id="what-field-separation"&gt;What? Field separation?&lt;/h2&gt;
+&lt;p&gt;Field separation is just splitting a larger unit into a list of &amp;quot;fields&amp;quot;. The most common case is when Bash splits a &amp;quot;simple command&amp;quot; (in the Bash manual's terminology) into a list of arguments. Understanding how this works is an important prerequisite to understanding arrays, and even why they are important.&lt;/p&gt;
+&lt;p&gt;Dealing with lists is something that is very common in Bash scripts; from dealing with lists of arguments, to lists of files; they pop up a lot, and each time, you need to think about how the list is separated. In the case of &lt;code&gt;$PATH&lt;/code&gt;, the list is separated by colons. In the case of &lt;code&gt;$CFLAGS&lt;/code&gt;, the list is separated by whitespace. In the case of actual arrays, it's easy, there's no special character to worry about, just quote it, and you're good to go.&lt;/p&gt;
+&lt;h2 id="bash-word-splitting"&gt;Bash word splitting&lt;/h2&gt;
+&lt;p&gt;When Bash reads a &amp;quot;simple command&amp;quot;, it splits the whole thing into a list of &amp;quot;words&amp;quot;. &amp;quot;The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.&amp;quot; (to quote &lt;code&gt;bash(1)&lt;/code&gt;)&lt;/p&gt;
+&lt;p&gt;It is often hard for those unfamiliar with Bash to understand when something is multiple words, and when it is a single word that just contains a space or newline. To help gain an intuitive understanding, I recommend using the following command to print a bullet list of words, to see how Bash splits them up:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;printf ' -&gt; %s\n' &lt;var&gt;words…&lt;/var&gt;&lt;hr&gt; -&amp;gt; word one
+ -&amp;gt; multiline
+word
+ -&amp;gt; third word
+&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;In a simple command, in absence of quoting, Bash separates the &amp;quot;raw&amp;quot; input into words by splitting on spaces and tabs. In other places, such as when expanding a variable, it uses the same process, but splits on the characters in the &lt;code&gt;$IFS&lt;/code&gt; variable (which has the default value of space/tab/newline). This process is, creatively enough, called &amp;quot;word splitting&amp;quot;.&lt;/p&gt;
+&lt;p&gt;In most discussions of Bash arrays, one of the frequent criticisms is all the footnotes and &amp;quot;gotchas&amp;quot; about when to quote things. That's because they usually don't set the context of word splitting. &lt;strong&gt;Double quotes (&lt;code&gt;&amp;quot;&lt;/code&gt;) inhibit Bash from doing word splitting.&lt;/strong&gt; That's it, that's all they do. Arrays are already split into words; without wrapping them in double quotes Bash re-word splits them, which is almost &lt;em&gt;never&lt;/em&gt; what you want; otherwise, you wouldn't be working with an array.&lt;/p&gt;
+&lt;h2 id="normal-array-syntax"&gt;Normal array syntax&lt;/h2&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Setting an array&lt;/h1&gt;
+ &lt;p&gt;&lt;var&gt;words…&lt;/var&gt; is expanded and subject to word splitting
+ based on &lt;code&gt;$IFS&lt;/code&gt;.&lt;/p&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;array=(&lt;var&gt;words…&lt;/var&gt;)&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Set the contents of the entire array.&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;array+=(&lt;var&gt;words…&lt;/var&gt;)&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Appends &lt;var&gt;words…&lt;/var&gt; to the end of the array.&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;array[&lt;var&gt;n&lt;/var&gt;]=&lt;var&gt;word&lt;/var&gt;&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Sets an individual entry in the array, the first entry is at
+ &lt;var&gt;n&lt;/var&gt;=0.&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;Now, for accessing the array. The most important things to understanding arrays is to quote them, and understanding the difference between &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Getting an entire array&lt;/h1&gt;
+ &lt;p&gt;Unless these are wrapped in double quotes, they are subject to
+ word splitting, which defeats the purpose of arrays.&lt;/p&gt;
+ &lt;p&gt;I guess it's worth mentioning that if you don't quote them, and
+ word splitting is applied, &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;
+ end up being equivalent.&lt;/p&gt;
+ &lt;p&gt;With &lt;code&gt;*&lt;/code&gt;, when joining the elements into a single
+ string, the elements are separated by the first character in
+ &lt;code&gt;$IFS&lt;/code&gt;, which is, by default, a space.&lt;/p&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to every element of the array, as a separate
+ words.&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[*]}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to every element of the array, as a single
+ word.&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;It's really that simple—that covers most usages of arrays, and most of the mistakes made with them.&lt;/p&gt;
+&lt;p&gt;To help you understand the difference between &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;, here is a sample of each:&lt;/p&gt;
+&lt;table&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;&lt;th&gt;&lt;code&gt;@&lt;/code&gt;&lt;/th&gt;&lt;th&gt;&lt;code&gt;*&lt;/code&gt;&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;
+ &lt;td&gt;Input:&lt;pre&gt;&lt;code&gt;#!/bin/bash
+array=(foo bar baz)
+for item in "${array[@]}"; do
+ echo " - &amp;lt;${item}&amp;gt;"
+done&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
+ &lt;td&gt;Input:&lt;pre&gt;&lt;code&gt;#!/bin/bash
+array=(foo bar baz)
+for item in "${array[*]}"; do
+ echo " - &amp;lt;${item}&amp;gt;"
+done&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;tr&gt;
+ &lt;td&gt;Output:&lt;pre&gt;&lt;code&gt; - &amp;lt;foo&amp;gt;
+ - &amp;lt;bar&amp;gt;
+ - &amp;lt;baz&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
+ &lt;td&gt;Output:&lt;pre&gt;&lt;code&gt; - &amp;lt;foo bar baz&amp;gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;In most cases, &lt;code&gt;@&lt;/code&gt; is what you want, but &lt;code&gt;*&lt;/code&gt; comes up often enough too.&lt;/p&gt;
+&lt;p&gt;To get individual entries, the syntax is &lt;code&gt;${array[&lt;var&gt;n&lt;/var&gt;]}&lt;/code&gt;, where &lt;var&gt;n&lt;/var&gt; starts at 0.&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Getting a single entry from an array&lt;/h1&gt;
+ &lt;p&gt;Also subject to word splitting if you don't wrap it in
+ quotes.&lt;/p&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[&lt;var&gt;n&lt;/var&gt;]}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to the &lt;var&gt;n&lt;/var&gt;&lt;sup&gt;th&lt;/sup&gt; entry of the
+ array, where the first entry is at &lt;var&gt;n&lt;/var&gt;=0.&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;To get a subset of the array, there are a few options:&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Getting subsets of an array&lt;/h1&gt;
+ &lt;p&gt;Substitute &lt;code&gt;*&lt;/code&gt; for &lt;code&gt;@&lt;/code&gt; to get the subset
+ as a &lt;code&gt;$IFS&lt;/code&gt;-separated string instead of separate
+ words, as described above.&lt;/p&gt;
+ &lt;p&gt;Again, if you don't wrap these in double quotes, they are
+ subject to word splitting, which defeats the purpose of
+ arrays.&lt;/p&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]:&lt;var&gt;start&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to the entries from &lt;var&gt;n&lt;/var&gt;=&lt;var&gt;start&lt;/var&gt; to the end
+ of the array.&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]:&lt;var&gt;start&lt;/var&gt;:&lt;var&gt;count&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to &lt;var&gt;count&lt;/var&gt; entries, starting at
+ &lt;var&gt;n&lt;/var&gt;=&lt;var&gt;start&lt;/var&gt;.&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]::&lt;var&gt;count&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;Evaluates to &lt;var&gt;count&lt;/var&gt; entries from the beginning of
+ the array.&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;Notice that &lt;code&gt;&amp;quot;${array[@]}&amp;quot;&lt;/code&gt; is equivalent to &lt;code&gt;&amp;quot;${array[@]:0}&amp;quot;&lt;/code&gt;.&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Getting the length of an array&lt;/h1&gt;
+ &lt;p&gt;The is the only situation with arrays where quoting doesn't
+ make a difference.&lt;/p&gt;
+ &lt;p&gt;True to my earlier statement, when unquoted, there is no
+ difference between &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;td&gt;
+ &lt;code&gt;${#array[@]}&lt;/code&gt;
+ &lt;br&gt;or&lt;br&gt;
+ &lt;code&gt;${#array[*]}&lt;/code&gt;
+ &lt;/td&gt;
+ &lt;td&gt;
+ Evaluates to the length of the array
+ &lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;h2 id="argument-array-syntax"&gt;Argument array syntax&lt;/h2&gt;
+&lt;p&gt;Accessing the arguments is mostly that simple, but that array doesn't actually have a variable name. It's special. Instead, it is exposed through a series of special variables (normal variables can only start with letters and underscore), that &lt;em&gt;mostly&lt;/em&gt; match up with the normal array syntax.&lt;/p&gt;
+&lt;p&gt;Setting the arguments array, on the other hand, is pretty different. That's fine, because setting the arguments array is less useful anyway.&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Accessing the arguments array&lt;/h1&gt;
+ &lt;aside&gt;Note that for values of &lt;var&gt;n&lt;/var&gt; with more than 1
+ digit, you need to wrap it in &lt;code&gt;{}&lt;/code&gt;.
+ Otherwise, &lt;code&gt;"$10"&lt;/code&gt; would be parsed
+ as &lt;code&gt;"${1}0"&lt;/code&gt;.&lt;/aside&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;&lt;th colspan=2&gt;Individual entries&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${array[0]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;$0&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${array[1]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;$1&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td colspan=2 style="text-align:center"&gt;…&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${array[9]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;$9&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${array[10]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;${10}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td colspan=2 style="text-align:center"&gt;…&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${array[&lt;var&gt;n&lt;/var&gt;]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;${&lt;var&gt;n&lt;/var&gt;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;th colspan=2&gt;Subset arrays (array)&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[@]}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${@:0}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[@]:1}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"$@"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[@]:&lt;var&gt;pos&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${@:&lt;var&gt;pos&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[@]:&lt;var&gt;pos&lt;/var&gt;:&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${@:&lt;var&gt;pos&lt;/var&gt;:&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[@]::&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${@::&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;th colspan=2&gt;Subset arrays (string)&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[*]}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${*:0}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[*]:1}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"$*"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[*]:&lt;var&gt;pos&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${*:&lt;var&gt;pos&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[*]:&lt;var&gt;pos&lt;/var&gt;:&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${*:&lt;var&gt;pos&lt;/var&gt;:&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;"${array[*]::&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;"${*::&lt;var&gt;len&lt;/var&gt;}"&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;th colspan=2&gt;Array length&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;${#array[@]}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;$#&lt;/code&gt; + 1&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;th colspan=2&gt;Setting the array&lt;/th&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;array=("${array[0]}" &lt;var&gt;words…&lt;/var&gt;)&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;set -- &lt;var&gt;words…&lt;/var&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;array=("${array[0]}" "${array[@]:2}")&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;shift&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;tr&gt;&lt;td&gt;&lt;code&gt;array=("${array[0]}" "${array[@]:&lt;var&gt;n+1&lt;/var&gt;}")&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;shift &lt;var&gt;n&lt;/var&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;Did you notice what was inconsistent? The variables &lt;code&gt;$*&lt;/code&gt;, &lt;code&gt;$@&lt;/code&gt;, and &lt;code&gt;$#&lt;/code&gt; behave like the &lt;var&gt;n&lt;/var&gt;=0 entry doesn't exist.&lt;/p&gt;
+&lt;table&gt;
+ &lt;caption&gt;
+ &lt;h1&gt;Inconsistencies&lt;/h1&gt;
+ &lt;/caption&gt;
+ &lt;tbody&gt;
+ &lt;tr&gt;
+ &lt;th colspan=3&gt;&lt;code&gt;@&lt;/code&gt; or &lt;code&gt;*&lt;/code&gt;&lt;/th&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;→&lt;/td&gt;
+ &lt;td&gt;&lt;code&gt;"${array[@]:0}"&lt;/code&gt;&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${@}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;→&lt;/td&gt;
+ &lt;td&gt;&lt;code&gt;"${@:1}"&lt;/code&gt;&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;th colspan=3&gt;&lt;code&gt;#&lt;/code&gt;&lt;/th&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${#array[@]}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;→&lt;/td&gt;
+ &lt;td&gt;length&lt;/td&gt;
+ &lt;/tr&gt;&lt;tr&gt;
+ &lt;td&gt;&lt;code&gt;"${#}"&lt;/code&gt;&lt;/td&gt;
+ &lt;td&gt;→&lt;/td&gt;
+ &lt;td&gt;length-1&lt;/td&gt;
+ &lt;/tr&gt;
+ &lt;/tbody&gt;
+&lt;/table&gt;
+&lt;p&gt;These make sense because argument 0 is the name of the script—we almost never want that when parsing arguments. You'd spend more code getting the values that it currently gives you.&lt;/p&gt;
+&lt;p&gt;Now, for an explanation of setting the arguments array. You cannot set argument &lt;var&gt;n&lt;/var&gt;=0. The &lt;code&gt;set&lt;/code&gt; command is used to manipulate the arguments passed to Bash after the fact—similarly, you could use &lt;code&gt;set -x&lt;/code&gt; to make Bash behave like you ran it as &lt;code&gt;bash -x&lt;/code&gt;; like most GNU programs, the &lt;code&gt;--&lt;/code&gt; tells it to not parse any of the options as flags. The &lt;code&gt;shift&lt;/code&gt; command shifts each entry &lt;var&gt;n&lt;/var&gt; spots to the left, using &lt;var&gt;n&lt;/var&gt;=1 if no value is specified; and leaving argument 0 alone.&lt;/p&gt;
+&lt;h2 id="but-you-mentioned-gotchas-about-quoting"&gt;But you mentioned &amp;quot;gotchas&amp;quot; about quoting!&lt;/h2&gt;
+&lt;p&gt;But I explained that quoting simply inhibits word splitting, which you pretty much never want when working with arrays. If, for some odd reason, you do what word splitting, then that's when you don't quote. Simple, easy to understand.&lt;/p&gt;
+&lt;p&gt;I think possibly the only case where you do want word splitting with an array is when you didn't want an array, but it's what you get (arguments are, by necessity, an array). For example:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;# Usage: path_ls PATH1 PATH2…
+# Description:
+# Takes any number of PATH-style values; that is,
+# colon-separated lists of directories, and prints a
+# newline-separated list of executables found in them.
+# Bugs:
+# Does not correctly handle programs with a newline in the name,
+# as the output is newline-separated.
+path_ls() {
+ local IFS dirs
+ IFS=:
+ dirs=($@) # The odd-ball time that it needs to be unquoted
+ find -L &amp;quot;${dirs[@]}&amp;quot; -maxdepth 1 -type f -executable \
+ -printf &amp;#39;%f\n&amp;#39; 2&amp;gt;/dev/null | sort -u
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Logically, there shouldn't be multiple arguments, just a single &lt;code&gt;$PATH&lt;/code&gt; value; but, we can't enforce that, as the array can have any size. So, we do the robust thing, and just act on the entire array, not really caring about the fact that it is an array. Alas, there is still a field-separation bug in the program, with the output.&lt;/p&gt;
+&lt;h2 id="i-still-dont-think-i-need-arrays-in-my-scripts"&gt;I still don't think I need arrays in my scripts&lt;/h2&gt;
+&lt;p&gt;Consider the common code:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;ARGS=&amp;#39; -f -q&amp;#39;
+…
+command $ARGS # unquoted variables are a bad code-smell anyway&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Here, &lt;code&gt;$ARGS&lt;/code&gt; is field-separated by &lt;code&gt;$IFS&lt;/code&gt;, which we are assuming has the default value. This is fine, as long as &lt;code&gt;$ARGS&lt;/code&gt; is known to never need an embedded space; which you do as long as it isn't based on anything outside of the program. But wait until you want to do this:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;ARGS=&amp;#39; -f -q&amp;#39;
+…
+if [[ -f &amp;quot;$filename&amp;quot; ]]; then
+ ARGS+=&amp;quot; -F $filename&amp;quot;
+fi
+…
+command $ARGS&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;Now you're hosed if &lt;code&gt;$filename&lt;/code&gt; contains a space! More than just breaking, it could have unwanted side effects, such as when someone figures out how to make &lt;code&gt;filename='foo --dangerous-flag'&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;Compare that with the array version:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;ARGS=(-f -q)
+…
+if [[ -f &amp;quot;$filename&amp;quot; ]]; then
+ ARGS+=(-F &amp;quot;$filename&amp;quot;)
+fi
+…
+command &amp;quot;${ARGS[@]}&amp;quot;&lt;/code&gt;&lt;/pre&gt;
+&lt;h2 id="what-about-portability"&gt;What about portability?&lt;/h2&gt;
+&lt;p&gt;Except for the little stubs that call another program with &lt;code&gt;&amp;quot;$@&amp;quot;&lt;/code&gt; at the end, trying to write for multiple shells (including the ambiguous &lt;code&gt;/bin/sh&lt;/code&gt;) is not a task for mere mortals. If you do try that, your best bet is probably sticking to POSIX. Arrays are not POSIX; except for the arguments array, which is; though getting subset arrays from &lt;code&gt;$@&lt;/code&gt; and &lt;code&gt;$*&lt;/code&gt; is not (tip: use &lt;code&gt;set --&lt;/code&gt; to re-purpose the arguments array).&lt;/p&gt;
+&lt;p&gt;Writing for various versions of Bash, though, is pretty do-able. Everything here works all the way back in bash-2.0 (December 1996), with the following exceptions:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;p&gt;The &lt;code&gt;+=&lt;/code&gt; operator wasn't added until Bash 3.1.&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;As a work-around, use &lt;code&gt;array[${#array[*]}]=&lt;var&gt;word&lt;/var&gt;&lt;/code&gt; to append a single element.&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;li&gt;&lt;p&gt;Accessing subset arrays of the arguments array is inconsistent if &lt;var&gt;pos&lt;/var&gt;=0 in &lt;code&gt;${@:&lt;var&gt;pos&lt;/var&gt;:&lt;var&gt;len&lt;/var&gt;}&lt;/code&gt;.&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;In Bash 2.x and 3.x, it works as expected, except that argument 0 is silently missing. For example &lt;code&gt;${@:0:3}&lt;/code&gt; gives arguments 1 and 2; where &lt;code&gt;${@:1:3}&lt;/code&gt; gives arguments 1, 2, and 3. This means that if &lt;var&gt;pos&lt;/var&gt;=0, then only &lt;var&gt;len&lt;/var&gt;-1 arguments are given back.&lt;/li&gt;
+&lt;li&gt;In Bash 4.0, argument 0 can be accessed, but if &lt;var&gt;pos&lt;/var&gt;=0, then it only gives back &lt;var&gt;len&lt;/var&gt;-1 arguments. So, &lt;code&gt;${@:0:3}&lt;/code&gt; gives arguments 0 and 1.&lt;/li&gt;
+&lt;li&gt;In Bash 4.1 and higher, it works in the way described in the main part of this document.&lt;/li&gt;
+&lt;/ul&gt;&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;Now, Bash 1.x doesn't have arrays at all. &lt;code&gt;$@&lt;/code&gt; and &lt;code&gt;$*&lt;/code&gt; work, but using &lt;code&gt;:&lt;/code&gt; to select a range of elements from them doesn't. Good thing most boxes have been updated since 1996!&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/git-go-pre-commit.md"/>
+ <link rel="alternate" type="text/html" href="/blog/git-go-pre-commit.html"/>
+ <id>https://lukeshu.com/blog/git-go-pre-commit.html</id>
+ <updated>2014-01-26T17:00:58-05:00</updated>
+ <published>2013-10-12T00:00:00+00:00</published>
+ <title>A git pre-commit hook for automatically formatting Go code</title>
+ <content type="html">&lt;h1 id="a-git-pre-commit-hook-for-automatically-formatting-go-code"&gt;A git pre-commit hook for automatically formatting Go code&lt;/h1&gt;
+&lt;p&gt;One of the (many) wonderful things about the Go programming language is the &lt;code&gt;gofmt&lt;/code&gt; tool, which formats your source in a canonical way. I thought it would be nice to integrate this in my &lt;code&gt;git&lt;/code&gt; workflow by adding it in a pre-commit hook to automatically format my source code when I committed it.&lt;/p&gt;
+&lt;p&gt;The Go distribution contains a git pre-commit hook that checks whether the source code is formatted, and aborts the commit if it isn't. I don't remember if I was aware of this at the time (or if it even existed at the time, or if it is new), but I wanted it to go ahead and format the code for me.&lt;/p&gt;
+&lt;p&gt;I found a few solutions online, but they were all missing something—support for partial commits. I frequently use &lt;code&gt;git add -p&lt;/code&gt;/&lt;code&gt;git gui&lt;/code&gt; to commit a subset of the changes I've made to a file, the existing solutions would end up adding the entire set of changes to my commit.&lt;/p&gt;
+&lt;p&gt;I ended up writing a solution that only formats the version of the that is staged for commit; here's my &lt;code&gt;.git/hooks/pre-commit&lt;/code&gt;:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#!/bin/bash
+
+# This would only loop over files that are already staged for commit.
+# git diff --cached --numstat |
+# while read add del file; do
+# …
+# done
+
+shopt -s globstar
+for file in **/*.go; do
+ tmp=&amp;quot;$(mktemp &amp;quot;$file.bak.XXXXXXXXXX&amp;quot;)&amp;quot;
+ mv &amp;quot;$file&amp;quot; &amp;quot;$tmp&amp;quot;
+ git checkout &amp;quot;$file&amp;quot;
+ gofmt -w &amp;quot;$file&amp;quot;
+ git add &amp;quot;$file&amp;quot;
+ mv &amp;quot;$tmp&amp;quot; &amp;quot;$file&amp;quot;
+done&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;It's still not perfect. It will try to operate on every &lt;code&gt;*.go&lt;/code&gt; file—which might do weird things if you have a file that hasn't been checked in at all. This also has the effect of formatting files that were checked in without being formatted, but weren't modified in this commit.&lt;/p&gt;
+&lt;p&gt;I don't remember why I did that—as you can see from the comment, I knew how to only select files that were staged for commit. I haven't worked on any projects in Go in a while—if I return to one of them, and remember why I did that, I will update this page.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="http://www.wtfpl.net/txt/copying/"&gt;WTFPL-2&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/fd_printf.md"/>
+ <link rel="alternate" type="text/html" href="/blog/fd_printf.html"/>
+ <id>https://lukeshu.com/blog/fd_printf.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2013-10-12T00:00:00+00:00</published>
+ <title>`dprintf`: print formatted text directly to a file descriptor</title>
+ <content type="html">&lt;h1 id="dprintf-print-formatted-text-directly-to-a-file-descriptor"&gt;&lt;code&gt;dprintf&lt;/code&gt;: print formatted text directly to a file descriptor&lt;/h1&gt;
+&lt;p&gt;This already existed as &lt;code&gt;dprintf(3)&lt;/code&gt;. I now feel stupid for having Implemented &lt;code&gt;fd_printf&lt;/code&gt;.&lt;/p&gt;
+&lt;p&gt;The original post is as follows:&lt;/p&gt;
+&lt;hr /&gt;
+&lt;p&gt;I wrote this while debugging some code, and thought it might be useful to others:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;#define _GNU_SOURCE /* vasprintf() */
+#include &amp;lt;stdarg.h&amp;gt; /* va_start()/va_end() */
+#include &amp;lt;stdio.h&amp;gt; /* vasprintf() */
+#include &amp;lt;stdlib.h&amp;gt; /* free() */
+#include &amp;lt;unistd.h&amp;gt; /* write() */
+
+int
+fd_printf(int fd, const char *format, ...)
+{
+ va_list arg;
+ int len;
+ char *str;
+
+ va_start(arg, format);
+ len = vasprintf(&amp;amp;str, format, arg);
+ va_end(arg);
+
+ write(fd, str, len);
+
+ free(str);
+ return len;
+}&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;It is a version of &lt;code&gt;printf&lt;/code&gt; that prints to a file descriptor—where &lt;code&gt;fprintf&lt;/code&gt; prints to a &lt;code&gt;FILE*&lt;/code&gt; data structure.&lt;/p&gt;
+&lt;p&gt;The appeal of this is that &lt;code&gt;FILE*&lt;/code&gt; I/O is buffered—which means mixing it with raw file descriptor I/O is going to produce weird results.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="http://www.wtfpl.net/txt/copying/"&gt;WTFPL-2&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/emacs-as-an-os.md"/>
+ <link rel="alternate" type="text/html" href="/blog/emacs-as-an-os.html"/>
+ <id>https://lukeshu.com/blog/emacs-as-an-os.html</id>
+ <updated>2014-01-26T17:00:58-05:00</updated>
+ <published>2013-08-29T00:00:00+00:00</published>
+ <title>Emacs as an operating system</title>
+ <content type="html">&lt;h1 id="emacs-as-an-operating-system"&gt;Emacs as an operating system&lt;/h1&gt;
+&lt;p&gt;This was originally published on &lt;a href="https://news.ycombinator.com/item?id=6292742"&gt;Hacker News&lt;/a&gt; on 2013-08-29.&lt;/p&gt;
+&lt;p&gt;Calling Emacs an OS is dubious, it certainly isn't a general purpose OS, and won't run on real hardware. But, let me make the case that Emacs is an OS.&lt;/p&gt;
+&lt;p&gt;Emacs has two parts, the C part, and the Emacs Lisp part.&lt;/p&gt;
+&lt;p&gt;The C part isn't just a Lisp interpreter, it is a Lisp Machine emulator. It doesn't particularly resemble any of the real Lisp machines. The TCP, Keyboard/Mouse, display support, and filesystem are done at the hardware level (the operations to work with these things are among the primitive operations provided by the hardware). Of these, the display being handled by the hardware isn't particularly uncommon, historically; the filesystem is a little stranger.&lt;/p&gt;
+&lt;p&gt;The Lisp part of Emacs is the operating system that runs on that emulated hardware. It's not a particularly powerful OS, it not a multitasking system. It has many packages available for it (though not until recently was there a official package manager). It has reasonably powerful IPC mechanisms. It has shells, mail clients (MUAs and MSAs), web browsers, web servers and more, all written entirely in Emacs Lisp.&lt;/p&gt;
+&lt;p&gt;You might say, &amp;quot;but a lot of that is being done by the host operating system!&amp;quot; Sure, some of it is, but all of it is sufficiently low level. If you wanted to share the filesystem with another OS running in a VM, you might do it by sharing it as a network filesystem; this is necessary when the VM OS is not designed around running in a VM. However, because Emacs OS will always be running in the Emacs VM, we can optimize it by having the Emacs VM include processor features mapping the native OS, and have the Emacs OS be aware of them. It would be slower and more code to do that all over the network.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/emacs-shells.md"/>
+ <link rel="alternate" type="text/html" href="/blog/emacs-shells.html"/>
+ <id>https://lukeshu.com/blog/emacs-shells.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2013-04-09T00:00:00+00:00</published>
+ <title>A summary of Emacs' bundled shell and terminal modes</title>
+ <content type="html">&lt;h1 id="a-summary-of-emacs-bundled-shell-and-terminal-modes"&gt;A summary of Emacs' bundled shell and terminal modes&lt;/h1&gt;
+&lt;p&gt;This is based on a post on &lt;a href="http://www.reddit.com/r/emacs/comments/1bzl8b/how_can_i_get_a_dumbersimpler_shell_in_emacs/c9blzyb"&gt;reddit&lt;/a&gt;, published on 2013-04-09.&lt;/p&gt;
+&lt;p&gt;Emacs comes bundled with a few different shell and terminal modes. It can be hard to keep them straight. What's the difference between &lt;code&gt;M-x term&lt;/code&gt; and &lt;code&gt;M-x ansi-term&lt;/code&gt;?&lt;/p&gt;
+&lt;p&gt;Here's a good breakdown of the different bundled shells and terminals for Emacs, from dumbest to most Emacs-y.&lt;/p&gt;
+&lt;h2 id="term-mode"&gt;term-mode&lt;/h2&gt;
+&lt;p&gt;Your VT100-esque terminal emulator; it does what most terminal programs do. Ncurses-things work OK, but dumping large amounts of text can be slow. By default it asks you which shell to run, defaulting to the environmental variable &lt;code&gt;$SHELL&lt;/code&gt; (&lt;code&gt;/bin/bash&lt;/code&gt; for me). There are two modes of operation:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;char mode: Keys are sent immediately to the shell (including keys that are normally Emacs keystrokes), with the following exceptions:&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;(term-escape-char) (term-escape-char)&lt;/code&gt; sends &lt;code&gt;(term-escape-char)&lt;/code&gt; to the shell (see above for what the default value is).&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;(term-escape-char) &amp;lt;anything-else&amp;gt;&lt;/code&gt; is like doing equates to &lt;code&gt;C-x &amp;lt;anything-else&amp;gt;&lt;/code&gt; in normal Emacs.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;(term-escape-char) C-j&lt;/code&gt; switches to line mode.&lt;/li&gt;
+&lt;li&gt;line mode: Editing is done like in a normal Emacs buffer, &lt;code&gt;&amp;lt;enter&amp;gt;&lt;/code&gt; sends the current line to the shell. This is useful for working with a program's output.&lt;/li&gt;
+&lt;li&gt;&lt;code&gt;C-c C-k&lt;/code&gt; switches to char mode.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;This mode is activated with&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;; Creates or switches to an existing &amp;quot;*terminal*&amp;quot; buffer.
+; The default &amp;#39;term-escape-char&amp;#39; is &amp;quot;C-c&amp;quot;
+M-x term&lt;/code&gt;&lt;/pre&gt;
+&lt;p&gt;or&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;; Creates a new &amp;quot;*ansi-term*&amp;quot; or &amp;quot;*ansi-term*&amp;lt;n&amp;gt;&amp;quot; buffer.
+; The default &amp;#39;term-escape-char&amp;#39; is &amp;quot;C-c&amp;quot; and &amp;quot;C-x&amp;quot;
+M-x ansi-term&lt;/code&gt;&lt;/pre&gt;
+&lt;h2 id="shell-mode"&gt;shell-mode&lt;/h2&gt;
+&lt;p&gt;The name is a misnomer; shell-mode is a terminal emulator, not a shell; it's called that because it is used for running a shell (bash, zsh, …). The idea of this mode is to use an external shell, but make it Emacs-y. History is not handled by the shell, but by Emacs; &lt;code&gt;M-p&lt;/code&gt; and &lt;code&gt;M-n&lt;/code&gt; access the history, while arrows/&lt;code&gt;C-p&lt;/code&gt;/&lt;code&gt;C-n&lt;/code&gt; move the point (which is is consistent with other Emacs REPL-type interfaces). It ignores VT100-type terminal colors, and colorizes things itself (it inspects words to see if they are directories, in the case of &lt;code&gt;ls&lt;/code&gt;). This has the benefit that it does syntax highlighting on the currently being typed command. Ncurses programs will of course not work. This mode is activated with:&lt;/p&gt;
+&lt;pre&gt;&lt;code&gt;M-x shell&lt;/code&gt;&lt;/pre&gt;
+&lt;h2 id="eshell-mode"&gt;eshell-mode&lt;/h2&gt;
+&lt;p&gt;This is a shell+terminal, entirely written in Emacs lisp. (Interestingly, it doesn't set &lt;code&gt;$SHELL&lt;/code&gt;, so that will be whatever it was when you launched Emacs). This won't even be running zsh or bash, it will be running &amp;quot;esh&amp;quot;, part of Emacs.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/term-colors.md"/>
+ <link rel="alternate" type="text/html" href="/blog/term-colors.html"/>
+ <id>https://lukeshu.com/blog/term-colors.html</id>
+ <updated>2014-01-26T17:00:58-05:00</updated>
+ <published>2013-03-21T00:00:00+00:00</published>
+ <title>An explanation of common terminal emulator color codes</title>
+ <content type="html">&lt;h1 id="an-explanation-of-common-terminal-emulator-color-codes"&gt;An explanation of common terminal emulator color codes&lt;/h1&gt;
+&lt;p&gt;This is based on a post on &lt;a href="http://www.reddit.com/r/commandline/comments/1aotaj/solarized_is_a_sixteen_color_palette_designed_for/c8ztxpt?context=1"&gt;reddit&lt;/a&gt;, published on 2013-03-21.&lt;/p&gt;
+&lt;blockquote&gt;
+&lt;p&gt;So all terminals support the same 256 colors? What about 88 color mode: is that a subset?&lt;/p&gt;
+&lt;/blockquote&gt;
+&lt;p&gt;TL;DR: yes&lt;/p&gt;
+&lt;p&gt;Terminal compatibility is crazy complex, because nobody actually reads the spec, they just write something that is compatible for their tests. Then things have to be compatible with that terminal's quirks.&lt;/p&gt;
+&lt;p&gt;But, here's how 8-color, 16-color, and 256 color work. IIRC, 88 color is a subset of the 256 color scheme, but I'm not sure.&lt;/p&gt;
+&lt;p&gt;&lt;strong&gt;8 colors: (actually 9)&lt;/strong&gt; First we had 8 colors (9 with &amp;quot;default&amp;quot;, which doesn't have to be one of the 8). These are always roughly the same color: black, red, green, yellow/orange, blue, purple, cyan, and white, which are colors 0-7 respectively. Color 9 is default.&lt;/p&gt;
+&lt;p&gt;&lt;strong&gt;16 colors: (actually 18)&lt;/strong&gt; Later, someone wanted to add more colors, so they added a &amp;quot;bright&amp;quot; attribute. So when bright is on, you get &amp;quot;bright red&amp;quot; instead of &amp;quot;red&amp;quot;. Hence 8*2=16 (plus two more for &amp;quot;default&amp;quot; and &amp;quot;bright default&amp;quot;).&lt;/p&gt;
+&lt;p&gt;&lt;strong&gt;256 colors: (actually 274)&lt;/strong&gt; You may have noticed, colors 0-7 and 9 are used, but 8 isn't. So, someone decided that color 8 should put the terminal into 256 color mode. In this mode, it reads another byte, which is an 8-bit RGB value (2 bits for red, 2 for green, 2 for blue). The bright property has no effect on these colors. However, a terminal can display 256-color-mode colors and 16-color-mode colors at the same time, so you actually get 256+18 colors.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/fs-licensing-explanation.md"/>
+ <link rel="alternate" type="text/html" href="/blog/fs-licensing-explanation.html"/>
+ <id>https://lukeshu.com/blog/fs-licensing-explanation.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2013-02-21T00:00:00+00:00</published>
+ <title>An explanation of how "copyleft" licensing works</title>
+ <content type="html">&lt;h1 id="an-explanation-of-how-copyleft-licensing-works"&gt;An explanation of how &amp;quot;copyleft&amp;quot; licensing works&lt;/h1&gt;
+&lt;p&gt;This is based on a post on &lt;a href="http://www.reddit.com/r/freesoftware/comments/18xplw/can_software_be_free_gnu_and_still_be_owned_by_an/c8ixwq2"&gt;reddit&lt;/a&gt;, published on 2013-02-21.&lt;/p&gt;
+&lt;blockquote&gt;
+&lt;p&gt;While reading the man page for readline I noticed the copyright section said &amp;quot;Readline is Copyright (C) 1989-2011 Free Software Foundation Inc&amp;quot;. How can software be both licensed under GNU and copyrighted to a single group? It was my understanding that once code became free it didn't belong to any particular group or individual.&lt;/p&gt;
+&lt;p&gt;[LiveCode is GPLv3, but also sells non-free licenses] Can you really have the same code under two conflicting licenses? Once licensed under GPL3 wouldn't they too be required to adhere to its rules?&lt;/p&gt;
+&lt;/blockquote&gt;
+&lt;p&gt;I believe that GNU/the FSF has an FAQ that addresses this, but I can't find it, so here we go.&lt;/p&gt;
+&lt;h3 id="glossary"&gt;Glossary:&lt;/h3&gt;
+&lt;ul&gt;
+&lt;li&gt;&amp;quot;&lt;em&gt;Copyright&lt;/em&gt;&amp;quot; is the right to control how copies are made of something.&lt;/li&gt;
+&lt;li&gt;Something for which no one holds the copyright is in the &amp;quot;&lt;em&gt;public domain&lt;/em&gt;&amp;quot;, because anyone (&amp;quot;the public&amp;quot;) is allowed to do &lt;em&gt;anything&lt;/em&gt; with it.&lt;/li&gt;
+&lt;li&gt;A &amp;quot;&lt;em&gt;license&lt;/em&gt;&amp;quot; is basically a legal document that says &amp;quot;I promise not to sue you if make copies in these specific ways.&amp;quot;&lt;/li&gt;
+&lt;li&gt;A &amp;quot;&lt;em&gt;non-free&lt;/em&gt;&amp;quot; license basically says &amp;quot;There are no conditions under which you can make copies that I won't sue you.&amp;quot;&lt;/li&gt;
+&lt;li&gt;A &amp;quot;&lt;em&gt;permissive&lt;/em&gt;&amp;quot; (type of free) license basically says &amp;quot;You can do whatever you want, BUT have to give me credit&amp;quot;, and is very similar to the public domain. If the copyright holder didn't have the copyright, they couldn't sue you to make sure that you gave them credit, and nobody would have to give them credit.&lt;/li&gt;
+&lt;li&gt;A &amp;quot;&lt;em&gt;copyleft&lt;/em&gt;&amp;quot; (type of free) license basically says, &amp;quot;You can do whatever you want, BUT anyone who gets a copy from you has to be able to do whatever they want too.&amp;quot; If the copyright holder didn't have the copyright, they couldn't sue you to make sure that you gave the source to people go got it from you, and non-free versions of these programs would start to exist.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;h3 id="specific-questions"&gt;Specific questions:&lt;/h3&gt;
+&lt;p&gt;Readline: The GNU GPL is a copyleft license. If you make a modified version of Readline, and give it to others without letting them have the source code, the FSF will sue you. They can do this because they have the copyright on Readline, and in the GNU GPL (the license they used) it only says that they won't sue you if you distribute the source with the modified version. If they didn't have the copyright, they couldn't sue you, and the GNU GPL would be worthless.&lt;/p&gt;
+&lt;p&gt;LiveCode: The copyright holder for something is not required to obey the license—the license is only a promise not to sue you; of course they won't sue themselves. They can also offer different terms to different people. They can tell most people &amp;quot;I won't sue you as long as you share the source,&amp;quot; but if someone gave them a little money, they might say, &amp;quot;I also promise not sue sue this guy, even if he doesn't give out the source.&amp;quot;&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/pacman-overview.md"/>
+ <link rel="alternate" type="text/html" href="/blog/pacman-overview.html"/>
+ <id>https://lukeshu.com/blog/pacman-overview.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2013-01-23T00:00:00+00:00</published>
+ <title>A quick overview of usage of the Pacman package manager</title>
+ <content type="html">&lt;h1 id="a-quick-overview-of-usage-of-the-pacman-package-manager"&gt;A quick overview of usage of the Pacman package manager&lt;/h1&gt;
+&lt;p&gt;This was originally published on &lt;a href="https://news.ycombinator.com/item?id=5101416"&gt;Hacker News&lt;/a&gt; on 2013-01-23.&lt;/p&gt;
+&lt;p&gt;Note: I've over-done quotation marks to make it clear when precise wording matters.&lt;/p&gt;
+&lt;p&gt;&lt;code&gt;pacman&lt;/code&gt; is a little awkward, but I prefer it to apt/dpkg, which have sub-commands, each with their own flags, some of which are undocumented. pacman, on the other hand, has ALL options documented in one fairly short man page.&lt;/p&gt;
+&lt;p&gt;The trick to understanding pacman is to understand how it maintains databases of packages, and what it means to &amp;quot;sync&amp;quot;.&lt;/p&gt;
+&lt;p&gt;There are several &amp;quot;databases&amp;quot; that pacman deals with:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&amp;quot;the database&amp;quot;, (&lt;code&gt;/var/lib/pacman/local/&lt;/code&gt;)&lt;br&gt; The database of currently installed packages&lt;/li&gt;
+&lt;li&gt;&amp;quot;package databases&amp;quot;, (&lt;code&gt;/var/lib/pacman/sync/${repo}.db&lt;/code&gt;)&lt;br&gt; There is one of these for each repository. It is a file that is fetched over plain http(s) from the server; it is not modified locally, only updated.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;The &amp;quot;operation&amp;quot; of pacman is set with a capital flag, one of &amp;quot;DQRSTU&amp;quot; (plus &lt;code&gt;-V&lt;/code&gt; and &lt;code&gt;-h&lt;/code&gt; for version and help). Of these, &amp;quot;DTU&amp;quot; are &amp;quot;low-level&amp;quot; (analogous to dpkg) and &amp;quot;QRS&amp;quot; are &amp;quot;high-level&amp;quot; (analogous to apt).&lt;/p&gt;
+&lt;p&gt;To give a brief explanation of cover the &amp;quot;high-level&amp;quot; operations, and which databases they deal with:&lt;/p&gt;
+&lt;ul&gt;
+&lt;li&gt;&amp;quot;Q&amp;quot; Queries &amp;quot;the database&amp;quot; of locally installed packages.&lt;/li&gt;
+&lt;li&gt;&amp;quot;S&amp;quot; deals with &amp;quot;package databases&amp;quot;, and Syncing &amp;quot;the database&amp;quot; with them; meaning it installs/updates packages that are in package databases, but not installed on the local system.&lt;/li&gt;
+&lt;li&gt;&amp;quot;R&amp;quot; Removes packages &amp;quot;the database&amp;quot;; removing them from the local system.&lt;/li&gt;
+&lt;/ul&gt;
+&lt;p&gt;The biggest &amp;quot;gotcha&amp;quot; is that &amp;quot;S&amp;quot; deals with all operations with &amp;quot;package databases&amp;quot;, not just syncing &amp;quot;the database&amp;quot; with them.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2013 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/poor-system-documentation.md"/>
+ <link rel="alternate" type="text/html" href="/blog/poor-system-documentation.html"/>
+ <id>https://lukeshu.com/blog/poor-system-documentation.html</id>
+ <updated>2014-01-26T17:00:58-05:00</updated>
+ <published>2012-09-12T00:00:00+00:00</published>
+ <title>Why documentation on GNU/Linux sucks</title>
+ <content type="html">&lt;h1 id="why-documentation-on-gnulinux-sucks"&gt;Why documentation on GNU/Linux sucks&lt;/h1&gt;
+&lt;p&gt;This is based on a post on &lt;a href="http://www.reddit.com/r/archlinux/comments/zoffo/systemd_we_will_keep_making_it_the_distro_we_like/c66uu57"&gt;reddit&lt;/a&gt;, published on 2012-09-12.&lt;/p&gt;
+&lt;p&gt;The documentation situation on GNU/Linux based operating systems is right now a mess. In the world of documentation, there are basically 3 camps, the &amp;quot;UNIX&amp;quot; camp, the &amp;quot;GNU&amp;quot; camp, and the &amp;quot;GNU/Linux&amp;quot; camp.&lt;/p&gt;
+&lt;p&gt;The UNIX camp is the &lt;code&gt;man&lt;/code&gt; page camp, they have quality, terse but informative man pages, on &lt;em&gt;everything&lt;/em&gt;, including the system's design and all system files. If it was up to the UNIX camp, &lt;code&gt;man grub.cfg&lt;/code&gt;, &lt;code&gt;man grub.d&lt;/code&gt;, and &lt;code&gt;man grub-mkconfig_lib&lt;/code&gt; would exist and actually be helpful. The man page would either include inline examples, or point you to a directory. If I were to print off all of the man pages, it would actually be a useful manual for the system.&lt;/p&gt;
+&lt;p&gt;Then GNU camp is the &lt;code&gt;info&lt;/code&gt; camp. They basically thought that each piece of software was more complex than a man page could handle. They essentially think that some individual pieces software warrant a book. So, they developed the &lt;code&gt;info&lt;/code&gt; system. The info pages are usually quite high quality, but are very long, and a pain if you just want a quick look. The &lt;code&gt;info&lt;/code&gt; system can generate good HTML (and PDF, etc.) documentation. But the standard &lt;code&gt;info&lt;/code&gt; is awkward as hell to use for non-Emacs users.&lt;/p&gt;
+&lt;p&gt;Then we have the &amp;quot;GNU/Linux&amp;quot; camp, they use GNU software, but want to use &lt;code&gt;man&lt;/code&gt; pages. This means that we get low-quality man pages for GNU software, and then we don't have a good baseline for documentation, developers each try to create their own. The documentation that gets written is frequently either low-quality, or non-standard. A lot of man pages are auto-generated from &lt;code&gt;--help&lt;/code&gt; output or info pages, meaning they are either not helpful, or overly verbose with low information density. This camp gets the worst of both worlds, and a few problems of its own.&lt;/p&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2012 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+ <entry xmlns="http://www.w3.org/2005/Atom">
+ <link rel="alternate" type="text/markdown" href="/blog/arch-systemd.md"/>
+ <link rel="alternate" type="text/html" href="/blog/arch-systemd.html"/>
+ <id>https://lukeshu.com/blog/arch-systemd.html</id>
+ <updated>2016-02-28T07:12:18-05:00</updated>
+ <published>2012-09-11T00:00:00+00:00</published>
+ <title>What Arch Linux's switch to systemd means for users</title>
+ <content type="html">&lt;h1 id="what-arch-linuxs-switch-to-systemd-means-for-users"&gt;What Arch Linux's switch to systemd means for users&lt;/h1&gt;
+&lt;p&gt;This is based on a post on &lt;a href="http://www.reddit.com/r/archlinux/comments/zoffo/systemd_we_will_keep_making_it_the_distro_we_like/c66nrcb"&gt;reddit&lt;/a&gt;, published on 2012-09-11.&lt;/p&gt;
+&lt;p&gt;systemd is a replacement for UNIX System V-style init; instead of having &lt;code&gt;/etc/init.d/*&lt;/code&gt; or &lt;code&gt;/etc/rc.d/*&lt;/code&gt; scripts, systemd runs in the background to manage them.&lt;/p&gt;
+&lt;p&gt;This has the &lt;strong&gt;advantages&lt;/strong&gt; that there is proper dependency tracking, easing the life of the administrator and allowing for things to be run in parallel safely. It also uses &amp;quot;targets&amp;quot; instead of &amp;quot;init levels&amp;quot;, which just makes more sense. It also means that a target can be started or stopped on the fly, such as mounting or unmounting a drive, which has in the past only been done at boot up and shut down.&lt;/p&gt;
+&lt;p&gt;The &lt;strong&gt;downside&lt;/strong&gt; is that it is (allegedly) big, bloated&lt;a href="#fn1" class="footnoteRef" id="fnref1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;, and does (arguably) more than it should. Why is there a dedicated systemd-fsck? Why does systemd encapsulate the functionality of syslog? That, and it means somebody is standing on my lawn.&lt;/p&gt;
+&lt;p&gt;The &lt;strong&gt;changes&lt;/strong&gt; an Arch user needs to worry about is that everything is being moved out of &lt;code&gt;/etc/rc.conf&lt;/code&gt;. Arch users will still have the choice between systemd and SysV-init, but rc.conf is becoming the SysV-init configuration file, rather than the general system configuration file. If you will still be using SysV-init, basically the only thing in rc.conf will be &lt;code&gt;DAEMONS&lt;/code&gt;.&lt;a href="#fn2" class="footnoteRef" id="fnref2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; For now there is compatibility for the variables that used to be there, but that is going away.&lt;/p&gt;
+&lt;section class="footnotes"&gt;
+&lt;hr /&gt;
+&lt;ol&gt;
+&lt;li id="fn1"&gt;&lt;p&gt;&lt;em&gt;I&lt;/em&gt; don't think it's bloated, but that is the criticism. Basically, I discount any argument that uses &amp;quot;bloated&amp;quot; without backing it up. I was trying to say that it takes a lot of heat for being bloated, and that there is be some truth to that (the systemd-fsck and syslog comments), but that these claims are largely unsubstantiated, and more along the lines of &amp;quot;I would have done it differently&amp;quot;. Maybe your ideas are better, but you haven't written the code.&lt;/p&gt;
+&lt;p&gt;I personally don't have an opinion either way about SysV-init vs systemd. I recently migrated my boxes to systemd, but that was because the SysV init scripts for NFSv4 in Arch are problematic. I suppose this is another &lt;strong&gt;advantage&lt;/strong&gt; I missed: &lt;em&gt;people generally consider systemd &amp;quot;units&amp;quot; to be more robust and easier to write than SysV &amp;quot;scripts&amp;quot;.&lt;/em&gt;&lt;/p&gt;
+&lt;p&gt;I'm actually not a fan of either. If I had more time on my hands, I'd be running a &lt;code&gt;make&lt;/code&gt;-based init system based on a research project IBM did a while ago. So I consider myself fairly objective; my horse isn't in this race.&lt;a href="#fnref1"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
+&lt;li id="fn2"&gt;&lt;p&gt;You can still have &lt;code&gt;USEDMRAID&lt;/code&gt;, &lt;code&gt;USELVM&lt;/code&gt;, &lt;code&gt;interface&lt;/code&gt;, &lt;code&gt;address&lt;/code&gt;, &lt;code&gt;netmask&lt;/code&gt;, and &lt;code&gt;gateway&lt;/code&gt;. But those are minor.&lt;a href="#fnref2"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
+&lt;/ol&gt;
+&lt;/section&gt;
+</content>
+ <author><name>Luke Shumaker</name><uri>https://lukeshu.com/</uri><email>lukeshu@sbcglobal.net</email></author>
+ <rights type="html">&lt;p&gt;The content of this page is Copyright © 2012 &lt;a href="mailto:lukeshu@sbcglobal.net"&gt;Luke Shumaker&lt;/a&gt;.&lt;/p&gt;
+&lt;p&gt;This page is licensed under the &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA-3.0&lt;/a&gt; license.&lt;/p&gt;</rights>
+ </entry>
+
+</feed>
diff --git a/public/index.html b/public/index.html
index 9f68b3b..a7d776f 100644
--- a/public/index.html
+++ b/public/index.html
@@ -19,8 +19,8 @@ time {
}
</style>
<ul>
-<li><time>2016-02-28</time> - <a href="./java-segfault-redux.html">My favorite bug: segfaults in Java (redux)</a></li>
<li><time>2016-02-28</time> - <a href="./x11-systemd.html">My X11 setup with systemd</a></li>
+<li><time>2016-02-28</time> - <a href="./java-segfault-redux.html">My favorite bug: segfaults in Java (redux)</a></li>
<li><time>2015-05-19</time> - <a href="./nginx-mediawiki.html">An Nginx configuration for MediaWiki</a></li>
<li><time>2015-03-22</time> - <a href="./lp2015-videos.html">I took some videos at LibrePlanet</a></li>
<li><time>2015-03-18</time> - <a href="./build-bash-1.html">Building Bash 1.14.7 on a modern system</a></li>
@@ -32,8 +32,8 @@ time {
<li><time>2014-02-13</time> - <a href="./bash-redirection.html">Bash redirection</a></li>
<li><time>2014-01-13</time> - <a href="./java-segfault.html">My favorite bug: segfaults in Java</a></li>
<li><time>2013-10-13</time> - <a href="./bash-arrays.html">Bash arrays</a></li>
-<li><time>2013-10-12</time> - <a href="./fd_printf.html"><code>dprintf</code>: print formatted text directly to a file descriptor</a></li>
<li><time>2013-10-12</time> - <a href="./git-go-pre-commit.html">A git pre-commit hook for automatically formatting Go code</a></li>
+<li><time>2013-10-12</time> - <a href="./fd_printf.html"><code>dprintf</code>: print formatted text directly to a file descriptor</a></li>
<li><time>2013-08-29</time> - <a href="./emacs-as-an-os.html">Emacs as an operating system</a></li>
<li><time>2013-04-09</time> - <a href="./emacs-shells.html">A summary of Emacs' bundled shell and terminal modes</a></li>
<li><time>2013-03-21</time> - <a href="./term-colors.html">An explanation of common terminal emulator color codes</a></li>
diff --git a/public/index.md b/public/index.md
index e5b5b9e..2cde070 100644
--- a/public/index.md
+++ b/public/index.md
@@ -1,5 +1,5 @@
Web log entries
-=====
+===============
<style>
li {
list-style-type: none;
@@ -9,8 +9,9 @@ time {
font-family: monospace;
}
</style>
- * <time>2016-02-28</time> - [My favorite bug: segfaults in Java (redux)](./java-segfault-redux.html)
+
* <time>2016-02-28</time> - [My X11 setup with systemd](./x11-systemd.html)
+ * <time>2016-02-28</time> - [My favorite bug: segfaults in Java (redux)](./java-segfault-redux.html)
* <time>2015-05-19</time> - [An Nginx configuration for MediaWiki](./nginx-mediawiki.html)
* <time>2015-03-22</time> - [I took some videos at LibrePlanet](./lp2015-videos.html)
* <time>2015-03-18</time> - [Building Bash 1.14.7 on a modern system](./build-bash-1.html)
@@ -22,8 +23,8 @@ time {
* <time>2014-02-13</time> - [Bash redirection](./bash-redirection.html)
* <time>2014-01-13</time> - [My favorite bug: segfaults in Java](./java-segfault.html)
* <time>2013-10-13</time> - [Bash arrays](./bash-arrays.html)
- * <time>2013-10-12</time> - [`dprintf`: print formatted text directly to a file descriptor](./fd_printf.html)
* <time>2013-10-12</time> - [A git pre-commit hook for automatically formatting Go code](./git-go-pre-commit.html)
+ * <time>2013-10-12</time> - [`dprintf`: print formatted text directly to a file descriptor](./fd_printf.html)
* <time>2013-08-29</time> - [Emacs as an operating system](./emacs-as-an-os.html)
* <time>2013-04-09</time> - [A summary of Emacs' bundled shell and terminal modes](./emacs-shells.html)
* <time>2013-03-21</time> - [An explanation of common terminal emulator color codes](./term-colors.html)
diff --git a/template.atom.erb b/template.atom.erb
deleted file mode 100644
index da52b79..0000000
--- a/template.atom.erb
+++ /dev/null
@@ -1,13 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<entry xmlns="http://www.w3.org/2005/Atom">
- <link rel="alternate" type="text/markdown" href="/blog/<%= @slug %>.md"/>
- <link rel="alternate" type="text/application/atom+xml" href="/blog/<%= @slug %>.atom"/>
- <link rel="alternate" type="text/html" href="/blog/<%= @slug %>.html"/>
- <id>https://lukeshu.com/blog/<%= @slug %>.html</id>
- <updated><%= @gitdate.rfc3339 %></updated>
- <published><%= @date.rfc3339 %></published>
- <title><%= @title %></title>
- <content type="html"><%= html_escape(@content) %></content>
- <author><%= @author.atom %></author>
- <rights type="html"><%= html_escape(@rights) %></rights>
-</entry>
diff --git a/util.rb b/util.rb
index f7bd1b9..e50406a 100644
--- a/util.rb
+++ b/util.rb
@@ -1,3 +1,19 @@
+# coding: utf-8
+load 'pandoc.rb'
+require 'erb'
+require 'date'
+
+$license_urls = {
+ "CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/',
+ 'WTFPL-2' => "http://www.wtfpl.net/txt/copying/",
+}
+$person_uris = {
+ "Luke Shumaker" => "https://lukeshu.com/",
+}
+$person_emails = {
+ "Luke Shumaker" => "lukeshu@sbcglobal.net",
+}
+
class Person
def initialize(name)
@name = name
@@ -43,6 +59,37 @@ class License
end
end
+class Page
+ attr_accessor :title, :author, :gitdate, :date, :license, :slug, :content, :rights, :breadcrumbs
+ def initialize(infile)
+ input = File.read(infile)
+ doc = Pandoc::load('markdown', input)
+
+ if doc['markdown_options']
+ doc = Pandoc::load('markdown'+doc['markdown_options'], input)
+ end
+
+ gitdate = `git log -n1 --format='%cI' -- #{infile}`
+
+ @title = doc['title'] || input.split("\n",2).first
+ @author = Person.new(doc['author'] || "Luke Shumaker")
+ @gitdate = DateTime.iso8601(gitdate) unless gitdate.empty?
+ @date = Date.parse(doc['date']) unless doc['date'].nil?
+ @license = License.new(doc['license'] || "CC BY-SA-3.0")
+ @slug = infile.sub(/\..*$/,'').sub(/^.*\//,'')
+ @content = doc.to('html5')
+ @rights = "<p>The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.</p>\n" +
+ "<p>This page is licensed under the #{@license.html} license.</p>"
+
+ @breadcrumbs = '<a href="/">Luke Shumaker</a> » '
+ if (@slug == 'index')
+ @breadcrumbs += "blog"
+ else
+ @breadcrumbs += '<a href=/blog>blog</a> » ' + @slug
+ end
+ end
+end
+
def html_escape(html)
html
.gsub('&', '&amp;')
diff --git a/write-atomic b/write-atomic
new file mode 100755
index 0000000..efb2551
--- /dev/null
+++ b/write-atomic
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+# Copyright (C) 2015-2016 Luke Shumaker
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+outfile=$1
+tmpfile="$(dirname "$outfile")/.tmp${outfile##*/}"
+
+cat > "$tmpfile" || { r=$?; rm -f "$tmpfile"; exit $r; }
+mv -f "$tmpfile" "$outfile"