Filed under Programming by Gwynne Raskind
Ah, the wonderful world of the command line. I wouldn’t be able to look at myself in the mirror (and by mirror, I mean reflection in my monitor) if I couldn’t play the shell as if it were a finely tuned 128-key instrument. But I like to add a little pizzaz to the very black-and-white (or black-and-amber, or black-and-green) world of Terminal.
To do this, one adds ANSI color codes to things. Pass a proper flag or two to ls, set CLICOLOR in your environment, and most of all, set your PS1 to something interesting. Here’s mine for my local machine:
PS1='\[\033[35m\]\u@\h\[\033[00m\]:\[\033[94m\]\w\[\033[96m\]\$\[\033[00m\] '
The result looks something like this:
gwynne@localhost:~/Desktop$ _
(more…)
ansi,
ANSI colors,
css,
highlighting,
html,
rxvt,
shell,
terminal
January 29, 2010 at 7:10 am
Filed under Programming by Gwynne Raskind
While in the process of working on the WordPress plugin mentioned in my last post, I found myself having to do a edit/save/upload cycle annoyingly often, as this WordPress install doesn’t run on my local machine and I didn’t feel like dealing with getting httpd up and running here again. More than once I caught myself trying to figure out why my changes weren’t having an effect until I realized I was forgetting the upload step. And entering a passphrase for scp every time I hit Save is tedious at best. But this blog is hosted on DreamHost, and TextMate doesn’t have SFTP support, so I couldn’t just mount a network drive in the Finder.
But I’d heard of this magical, wonderful thing called sshfs that would let me mount an sftp server as a volume on my machine. That would solve a lot of problems! At first I did the obvious: I went to Terminal and entered sudo port install macfuse sshfs. It didn’t work. Small wonder; the last version of MacFUSE in MacPorts is 2.0.3, which is a considerable distance behind the most recent release, 2.1.5). So I used the MacFUSE prefpane to update to 2.1.5, but then the sshfs install wanted nothing to do with me because it was built against the older version. I went to download a prebuilt binary, since that was just easier, but there wasn’t one for Snow Leopard. I tried to build sshfs from source, but the prefpane hadn’t installed sufficient libraries to do that with. So I went to build MacFUSE itself from source.
(more…)
10.6,
FUSE,
MacFUSE,
MacPorts,
porting,
scp,
sftp,
Snow Leopard,
ssh,
sshfs,
WordPress plugin
January 28, 2010 at 11:20 pm
Filed under Programming by Gwynne Raskind
So I was playing around with the rather nice SyntaxHighlighter WordPress plugin. It worked pretty nicely. But it had some issues, IMO:
- It repurposes the
class attribute to act like a pseudo-CSS ruleset with custom rules. To me that’s just ugly.
- It does all its work client-side. For some things that’s a nice touch, but for a blog with mostly static content it’s wasted time.
- It does almost everything in JavaScript. I have a personal distaste of working with JavaScript for no especial reason whatosever.
- It’s LGPLv3. I prefer less restrictive licenses for OSS.
On the other hand, I know the Pygments syntax highlighter pretty well, and I already had a written style plugin for it to get the syntax coloring I like. But there’s no mature Pygments plugin for WordPress that I could find. So I did what any good programmer would do and set out to write my own. I have the working knowledge of PHP (I’m a core dev, for mercy’s sake) and Python to do it with, after all. Not so much WordPress’ plugin API, though, so I took the SyntaxHighlighter plugin as a starting point.
(more…)
css,
html,
javascript,
php,
Pygments,
python,
SyntaxHighlighter,
WordPress,
WordPress plugin,
wpautop
January 28, 2010 at 10:42 pm
Filed under Programming by Gwynne Raskind
So I was looking at the macro used to calculate 16-bit parity in pure C without branching:
#define parity(v) ({ \
uint16_t pv = (v); \
pv ^= (uint16_t)(pv < < 8); \
pv ^= (uint16_t)(pv << 4); \
pv ^= (uint16_t)(pv << 2); \
pv ^= (uint16_t)(pv << 1); \
(uint16_t)(pv & 0x0001); \
})
It uses GCC’s handy compound statement syntax, but otherwise it’s plain old C. Let’s look at the 64-bit ASM this compiles to at -Os:
(more…)
assembly,
c,
gcc,
objective-c,
optimization,
parity,
Programming
January 25, 2010 at 12:45 pm
Filed under Programming by Gwynne Raskind
Given this code:
- (void)calculateData:(int32_t)n intoLeftBuffer:(int16_t *)lbuf rightBuffer:(int16_t *)rbuf
{
static uint64_t (*calculateInternalCall)(id, SEL) = NULL;
if (!calculateInternalCall)
calculateInternalCall = (uint64_t (*)(id, SEL))[self methodForSelector:@selector(calculateInternal)];
uint64_t leftEnableMask = self.isEnabled && lbuf ?
((-(is0Enabled & leftEnabled[0])) & 0xFFFF000000000000) |
((-(is1Enabled & leftEnabled[1])) & 0x0000FFFF00000000) |
((-(is2Enabled & leftEnabled[2])) & 0x00000000FFFF0000) |
((-(is3Enabled & leftEnabled[3])) & 0x000000000000FFFF)
: 0,
rightEnableMask = self.isEnabled && rbuf ?
((-(is0Enabled & rightEnabled[0])) & 0xFFFF000000000000) |
((-(is1Enabled & rightEnabled[1])) & 0x0000FFFF00000000) |
((-(is2Enabled & rightEnabled[2])) & 0x00000000FFFF0000) |
((-(is3Enabled & rightEnabled[3])) & 0x000000000000FFFF)
: 0;
for (int32_t i = 0; i < n; ++i)
{
uint64_t data = calculateInternalCall(self, @selector(calculateInternal)),
lv = data & leftEnableMask, rv = data & rightEnableMask;
int16_t *l = (int16_t *)&lv, *r = (int16_t *)&rv;
lbuf[i] += l[0] + l[1] + l[2] + l[3];
rbuf[i] += r[0] + r[1] + r[2] + r[3];
}
}
Where are the two NULL dereferences Clang claims to have found?
(more…)
c,
clang,
code mistakes,
objective-c,
Programming
January 25, 2010 at 12:12 pm
Filed under Programming by Gwynne Raskind
We all make mistakes when coding, and some of them are quite deeply foolish. But this particular snafu I just made takes the cake, in my opinion (dumbed-down code):
// Written as a static inline C function for max
// speed; this is called in a very tight loop, usually
// at something like 40,000 times per second.
static inline uint8_t _readByte(NSUIntger *cursor,
const char *bytes,
NSUInteger maxlen)
{ return (*cursor < maxlen) ? bytes[*cursor++] : 0x66; }
// Macro for readability, known to be called only from the one function
#define readByte() _readByte(&cursor, buffer, bufferlen)
// Macro to skip so many bytes
#define skipBytes(n) do { \
__typeof__(n) nn = (n); \
for (uint8_t i = 0; i < nn; ++i) \
readByte(); \
} while (0)
- (void)processBuffer
{
// assume the declarations of n, cursor, buffer, bufferlen
// ...
n = readByte();
// ...
else if (n > 0x29)
skipBytes((uint8_t[16]){0,0,0,1,1,2,0,0,0,0,2,2,3,3,4,4})(n & 0xF0) >> 4);
}
See it? (Hint: Look at the implementation of skipBytes().)
(more…)
10.6,
c,
clang,
code mistakes,
objective-c,
Programming,
Snow Leopard
January 24, 2010 at 12:03 am