cxxforth: A Simple Forth Implementation in C++

Because implementing obsolete programming languages is a great use of my free time, I've created cxxforth, an implementation of Forth in C++.

Inspired by JONESFORTH, I wrote it in the style of a tutorial. The source code is automatically converted to a pretty-printed format: https://github.com/kristopherjohnson/cxxforth/blob/master/cxxforth.cpp.md.

Now, on to the next waste of my time...

A Gforth Interface to the wiringPi Library

I recently obtained a Raspberry Pi, an Arduino, and a model train set. This should keep me busy and out of trouble for a while.

My plan is to use the Arduino to read sensors and control the turnout switches on the track, but I think it is easier to use the RPi to do some initial experimentation with driving relays and other electronic components. I did the typical make-an-LED-blink exercise using the wiringPi library. It wasn't difficult, but I wished there was an interactive way to play with the GPIO pins, rather than going through a cycle of editing, compiling, and executing a C program whenever I wanted to try something.

Then I remembered Forth. Forth was designed as an interactive language for controlling hardware, and seemed like the perfect solution for me.

After looking at a few possibilities for Forth-based solutions, I decided to use Gforth, as it provides a straightforward way to call functions in the wiringPi library without the need to build my own Forth.

Here is my Gforth interface to the wiringPi library: https://github.com/kristopherjohnson/wiringPi_gforth

Now, back to the train set...

Performing Privileged Operations in an OS X Application

I am currently developing an OS X application. The application needs to perform some operations that require root privileges (installing and uninstalling launchd daemons, sending signals to other users' processes, etc.) So I started looking for some documentation about how to do that. I figured it would take a few minutes.

A day later, I finally think I understand it. The solution isn't complicated, but learning about it was difficult for a few reasons:

  • Apple's "Authorization Services Programming Guide" is woefully out of date. It tells you to use AuthorizationExecuteWithPrivileges(), but that has been deprecated since OS X 10.7. It also has links to sample code that doesn't exist anymore. (Apple developer documentation people: It seems many of your docs are way out of date. Pls fix.)
  • Apple provides a SMJobBless sample that purports to demonstrate how to use the new SMJobBless() API that replaces AuthorizationExecuteWithPrivileges(). However, while it does indeed install some kind of service that could in theory run as root, the service it installs doesn't do anything, so it is useless as an example. (But you could waste a lot of time with it before you figure that out.)
  • A lot of old documentation and sample code for the original low-level XPC API is still floating around, but once you have a clue (which I didn't), you'll just use the high-level NSXPCConnection API.

So, in the hope I can help others avoid this tortuous path, here is how i would suggest others learn how to do this:

And here is what I wish somebody could have told me before I started:

  • What you will need to do is create a "helper tool" application to perform the privileged operations. This will be a command-line app that creates an XPC listener and handles requests.
  • The helper app has to have an info.plist and a launchd plist with some magical keys (see the EvenBetterAuthorizationSample for details)
  • The helper app will be embedded in the main app's package.
  • The main app will use the AuthorizationCreate() and SMJobBless() functions to authenticate the user and install the helper tool as a privileged launchd service. (This is the point where the system will display a dialog asking the user for an admin username and password.)
  • After the helper tool is installed, use -[NSXPCConnection initWithMachServiceName:options:] to connect to the helper tool. This will launch the helper tool on demand, running with root privileges.

Marathon Aborted

A few months ago, I signed up for the Chickamauga Battlefield Marathon, which will take place this Saturday (November 14). I thought I would have adequate time to train, but it hasn't worked out that way. I ran a half marathon last week, with a time of 2:30 (not great) and in my longer training runs over the past month, I've found that after mile 12, I have to do a lot of walking.

So I've canceled my hotel reservations and will not be running that marathon this weekend. Instead, I've decided I will concentrate on the half-marathon distance. When I get that down to around 2 hours, I'll start thinking about a full marathon again.

A Brainf__k compiler in C++

For fun, I wrote an interpreter for a programming language with a rude name, in C++.

It's available here: https://gist.github.com/kristopherjohnson/e5fc3d19c251dc561f62

There are other C++ interpreters for this language, but most of them look a lot more like C than like C++. My goal was to avoid archaic C-isms like pointers, fixed-size arrays, global variables, and the use of C stdio and UNIX functions, and write something that looks like "modern C++" (which may be an oxymoron).

C++ beginners may prefer to look at the code for a simplified implementation that represents my first pass at the problem. That is available here: https://gist.github.com/kristopherjohnson/55092ba62e82c2166125

A collection of BF example programs is available here: http://esoteric.sange.fi/brainfuck/bf-source/prog/. It pleases me that the ones I've tried all work with my interpreter. The hanoi.bf program is especially impressive.

Labor Day Run for Autism Half Marathon 2015

Today I ran the "Labor Day Run for Autism Half Marathon" at Fowler Park/Big Creek Greenway.

My time was 2:37. Not as good as I had expected. I was hoping for a time in the 2:20-2:30 range. I ran out of gas around mile 9, and walked a lot of the last few miles.

But, the November marathon for which I'm training still seems doable.

A Clock, using D3.js

Hey, look! I made a clock! The Apple Watch is now obsolete.

I am learning how to use the D3.js library to do data visualizations. It doesn't really make sense to consider hour, minute, and second as data to be "visualized", but this was a simple exercise to learn how to create graphical elements and animate them as the data changes.

If you're interested, you can click the JavaScript, CSS, and HTML links at the top to see exactly how it is implemented. (Note that JSFiddle is magically importing the D3 library, so you don't see that in the HTML.)

Here is another way to view the clock along with its source: http://bl.ocks.org/kristopherjohnson/1db0d39a9ed54d224ee7

Setting Up a Personal TiddlyWiki Server on OS X

For a new job, I decided to set up a personal wiki to keep notes. I wanted to keep it simple, meeting these requirements:

  • All the data is in a Dropbox folder (so it can be automatically synced between machines)
  • It must support Markdown syntax

After looking at the options, I settled on TiddlyWiki. I've used "classic TiddlyWiki" before, and liked its simplicity, but I was always a little annoyed with the weird steps you have to go through to save changes. The new version of TiddlyWiki ("TiddlyWiki5") includes support for running it as an HTTP server, so you can use it just like an online wiki.

But it took me a couple of hours to figure out how to set that up. The TiddlyWiki documentation is not clear ("not clear" is a euphemistic way of saying "terrible"). So I've written up these instructions in the hope it will spare somebody else all the frustration I had.

Prerequisites

The following instructions assume you are using a Mac running OS X, and that you know how to use the Terminal to run commands and how to create and edit text files.

(If you're using Linux or BSD, you can probably figure out what you need to do differently. If you're running Windows, you have my sympathy.)

Dropbox

Dropbox is not required to run TiddlyWiki, but I use it so that my personal notes will be available on all my machines.

If you don't already have Dropbox, go to https://www.dropbox.com to get started.

Node

The TiddlyWiki server requires Node, so you will need to install that if you don't already have it.

If you are already using Homebrew, then installation is as easy as this:

brew install node

If you aren't using Homebrew, then go to https://nodejs.org and click the Install button.

Time Machine

The first rule of using TiddlyWiki is back up your data. Using Dropbox serves as a rudimentary backup system, but it's not a real backup system.

If you haven't already set up Time Machine on your Mac, then go do it right now. See https://support.apple.com/en-us/HT201250 for details.

Installing TiddlyWiki

The TiddlyWiki server is available as an NPM module, so once you have Node installed, all you have to do is this:

npm install -g tiddlywiki

You can do this to verify it is installed and usable:

tiddlywiki --help

Initializing Your Wiki Directory

You'll need to decide where to store your TiddlyWiki data. As I'm using Dropbox, I'll store everything in /Users/kdj/Dropbox/tw, but you can use whatever directory makes sense for you.

Run this command to initialize the directory for a TiddlyWiki server:

tiddlywiki /Users/kdj/Dropbox/tw --init server

Note: you can run tiddlywiki --editions to see if any edition other than server might serve as a better starting point for you. I know that server works.

After running the above command, you should see that the specified directory contains a tiddlywiki.info file. This is the configuration file that controls how the server works.

Enabling the Markdown Plugin

TiddlyWiki's Markdown plugin is included with the distribution, but is not enabled by default. To enable it, you have to edit your tiddlywiki.info file and add "tiddlywiki/markdown" to the plugins section. When you have finished editing, the file should look like this:

{
    "description": "Basic client-server edition",
    "plugins": [
        "tiddlywiki/tiddlyweb",
        "tiddlywiki/filesystem",
        "tiddlywiki/codemirror",
        "tiddlywiki/highlight",
        "tiddlywiki/markdown"
    ],
    "themes": [
        "tiddlywiki/vanilla",
        "tiddlywiki/snowwhite"
    ]
}

Running the Server

With everything set up, you can do this to run the server:

tiddlywiki /Users/kdj/Dropbox/tw --server 19671

And then view it in a web browser: http://localhost:19671

Run tiddlywiki --help server to see what other options are available. You may want to use a different port, set a username/password, or otherwise customize the behavior.

Starting the Server Automatically When You Log In

It would get annoying to have to type "tiddlywiki /Users/kdj/Dropbox/tw --server 19671" every time you wanted to use your personal wiki. Let's create a launchd configuration file in the ~/Library/LaunchAgents directory that will cause it to be automatically started every time we log in.

Go to your ~/Library/LaunchAgents directory and create a file named com.tiddlywiki.plist with the following contents, substituting the appropriate path for your data directory and the paths to the node and tiddlywiki executables. (Run which node and which tiddlywiki if you don't know what those paths are.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.tiddlywiki</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/node</string>
    <string>/usr/local/bin/tiddlywiki</string>
    <string>/Users/kdj/Dropbox/tw</string>
    <string>--server</string>
    <string>19671</string>
  </array>
  <key>EnvironmentVariables</key>
  <dict>
    <key>PATH</key>
    <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
  </dict>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/Users/kdj/Dropbox/tw</string>
  <key>StandardErrorPath</key>
  <string>error.log</string>
  <key>StandardOutputPath</key>
  <string>output.log</string>
</dict>
</plist>

After saving that file, log out and then log back in, and try to visit http://localhost:19671. If it works, great! If not, look for an error.log or output.log file in your data directory that may explain what went wrong.

Restarting the Server

Unfortunately, using Dropbox to sync TiddlyWiki data between machines does not work as expected. The TiddlyWiki server does not monitor changes to the filesystem, so even though Dropbox will copy changed files between machines, each TiddlyWiki instance just keeps displaying whatever data it read when it was launched.

So, after saving changes on one machine, we have to restart the TiddlyWiki server on the other machines to have those changes displayed everywhere.

The TiddlyWiki developers may eventually fix this, but in the meantime, we can define some shell commands to make it easy to restart the server when necessary. Add these lines to your ~/.bashrc file:

export TWPLIST=~/Library/LaunchAgents/com.tiddlywiki.plist
alias twstart="launchctl load $TWPLIST"
alias twstop="launchctl unload $TWPLIST"
alias twreload="twstop && sleep 1 && twstart && echo 'TiddlyWiki restarted'"

With these definitions, you can just execute twreload and refresh your browser whenever you sit down at one of your computers, and that local TiddlyWiki will refresh itself from Dropbox.

Further Steps

Once you have everything working, explore the TiddlyWiki website to learn more about how to use it.

One of the first things you'll want to do is click the gear icon to go to the Control Panel and customize the site title and other settings.

Getting Root on Huawei U8665 Fusion 2 Phone

I've had an old Samsung Galaxy S Captivate phone, running Android 2.2, that I've used as a test device while developing Android apps. In my new job, I no longer need to support Android 2.2 (hooray!), but I do need to support Android 2.3 (boo!). I tried installing CyanogenMod to update the Captivate to Android 2.3, but I ended up bricking the device.

Rather than spend more time trying to figure out how to fix that, I looked around for a cheap new Android 2.3 device, and found the Huawei U8865, also known as the "Fusion 2", for $60. That seemed like a reasonable price for a brand-new old phone, so I purchased it and it arrived the next day.

Unfortunately, when I tried using it for development, I hit a snag. My work requires using adb shell and related utilities, and whenever I tried those, I just got a "permission denied" response. I couldn't even take a screenshot.

I looked around for instructions to "root" the phone (that is, get privileged access). They aren't hard to find, but the people who write up these instructions all assume that (a) their readers have no idea what they are doing, and (b) everybody uses Windows. Neither of those are true for me, so I had to translate those instructions into developer-speak.

So, anyway, if you are running Mac OS X and you already know how to use Terminal and adb, then here are instructions for you:

(These instructions are based on those at http://androidforums.com/threads/step-by-step-root-walkthrough-for-huawei-fusion-2.685504/.)

  1. Download https://www.dropbox.com/s/qtc37pley0vinj8/Huawei-Fusion-2-Recovery-Root.zip and extract the contents.
  2. Open Terminal and cd to the extracted Huawei-Fusion-2-Recovery-Root/Huawei-Fusion-2-Recovery-Root directory
  3. Turn off phone
  4. Reboot phone into fastboot by holding Volume Down and Power buttons simultaneously for 10-20 seconds. (It will freeze at the AT&T logo.)
  5. Connect phone to computer.
  6. Type fastboot devices to verify phone is connected.
  7. Type fastboot flash recovery recovery.superrecovery.img
  8. After it finishes, unplug phone.
  9. Remove back of phone, remove battery, wait 15 seconds, then re-insert battery and attach back.
  10. Hold Volume-Up and Power button simultaneously until it boots into recovery (15-20 seconds of holding both buttons)
  11. In recovery menu, select the "reboot device" option with the volume buttons (it should already start with reboot highlighted) and press the power button to reboot.

After that, you will "have root". So if you use adb shell to get a command shell on the device, you can run su to get superuser privileges. There is also a new SuperSU app on the phone.

But that's not really what I wanted. What I want to do is run adb shell <some-command> from the Mac. If you read the adb documentation, you might think you can run adb root to enable that, but you will just get an error message saying "adbd cannot run as root in production builds".

One needs to install an alternative version of adbd. I downloaded and installed this one from Google Play: adbd insecure. There is an option to have it automatically grant superuser access at boot, and I enabled that.

Monochrome Color Themes for Xcode and Sublime Text

A lot of programmers like brightly colored syntax-highlighting themes for their source code editors. I do not. I find colorful high-contrast themes to be fatiguing, distracting, and annoying, so I've gravitated toward low-contrast themes like Zenburn and Havenjark. But even those feel too "busy" for me.

I've been on a retrocomputing kick lately, and I've missed the clean look of source code on the monochrome monitors I used in the good old days. So I've created some monochrome Xcode themes. If you like them, you can download them from here:

and copy them to your ~/Library/Developer/Xcode/UserData/FontAndColorThemes directory, and then select them from the Xcode → Preferences → Fonts & Colors window.

These themes are also available as Sublime Text color schemes:

These were my simple rules for creating the themes:

  • All colors have the same hue. They differ only in saturation and brightness.
  • Comments are dim.
  • Keywords are dim, but not as dim as comments.
  • Numeric and string constants are bright, almost white.
  • Other textual elements all have the same color.

I use Source Code Pro Light as my font, as I like the way the thin lines give a vector-graphics look.

Amber

Amber theme screenshot

Green

Green theme screenshot

Blue

Blue theme screenshot

Blueprint

Blueprint theme screenshot

daVinci

daVinci theme screenshot

Syndicate content