Monday, March 12, 2012

Sudo, the stumpish way

This has been nagging me for a few days. It was a small thing, but it still bothered me. Whenever I want to run a command, I use the C-t ! shortcut. If I want to run it as root, I had to use gksu, but gksu does not exactly fit in the simple look and feel of StumpWM. What I wanted was a way to ask for password in the small box that appears in the top right when StumpWM wants some input.

A look at sudo man page pointed me to the right direction. sudo has an option that let's you specify another program to ask for password. Perfect. The command line option is -A. The program can be specified either in sudo config file or using the environment variable SUDO_ASKPASS. This script would do the trick:
#!/bin/bash
env SUDO_ASK_PASS=~/bin/ask.sh sudo -A $@
Now we only need the ask.sh script. But how can we get a script to call a StumpWM function? At first, I wanted to write a small server that opens a port and listens for commands. Fortunately, I accidentally learned about stumpish which is a script in StumWM's contrib directory that can send commands to StumpWM and return the results. Here's how ask.sh uses stumpish:
#!/bin/bash
stumpish stumpwm-password password:
The first argument is the name of a StumpWM command, while the rest are it's parameters. Since, as far as I know, there is no StumpWM command to ask for input, I wrote these two in my .stumpwmrc:
;; these commands are mainly intended to be called by external
;; commands through the use of stumpish
(defcommand stumpwm-input (prompt) ((:string "prompt: "))
  "prompts the user for one line of input."
  (read-one-line (current-screen) prompt))

(defcommand stumpwm-password (prompt) ((:string "prompt: "))
  "prompts the user for a password."
  (read-one-line (current-screen) prompt :password t))
I called the first script stu and put it with ask.sh in my bin directory. Now whenever I want to run synaptic, I press C-t ! and type in stu synaptic.

No comments:

Post a Comment

Better Console Font (in Arch)

In order to use Terminus as my console (tty) font, I simply installed the package terminus-font with pacman (includes both console and X11 ...