Skip to content

Instantly share code, notes, and snippets.

@ingramj
ingramj / test.h
Created November 30, 2014 02:31
Unit Tests for C, in a single header.
#ifndef TEST_H_
#define TEST_H_
/* Unit testing framework.
*
* Copyright (c) 2014, Jim Ingram <ingramj@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
# HTML Tidy filter. This is pretty Unix-specific. It uses the external 'tidy'
# command, which it looks for using 'which', and communicates with using open3.
require 'open3'
module Tidy
# Runs the source through tidy and returns the result. If tidy isn't found,
# just returns the source as is. The source should be string-like.
#
# Takes an optional hash parameter, with the keys :strict and :verbose
#!/usr/bin/ruby
# Save attachments from a saved email.
require 'rubygems'
require 'tmail'
return 1 unless ARGV.size > 0
ARGV.each do |in_file|
mail = TMail::Mail.load(in_file)
#!/usr/bin/ruby
# Takes a list of files and verifies the gpg signature on each, and makes
# sure that it was signed by an allowed key id
ALLOWED_KEY_IDS = ['C81FD50E']
ARGV.each do |e|
result = `gpg --verify #{e} 2>&1`
# If the the signature isn't verified, print and error and go the next file
unless result.include?('Good signature')
#!/usr/bin/env ruby
require 'readline'
require 'pp'
class Stsh
def initialize
@prompt = "-$ "
@aliases = {}
@running = false
@ingramj
ingramj / narepl
Created January 14, 2010 06:17
Oh dear.
#!/bin/sh
# narepl - Not A Read-Eval-Print Loop.
echo "This is not a REPL. Press ctrl-d to compile and run, ctrl-c to exit."
echo -n "> "
while true ; do
echo "#include </dev/tty>" | gcc -x c -ldl - && ./a.out && rm ./a.out
echo -n "> "
done
@ingramj
ingramj / error.c
Created July 23, 2011 03:26
Error reporting functions and macros. Includes goodies like color-coding and time stamps. Originally based on eprintf() from Kernighan and Pike's "The Practice of Programming".
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
const char *progname = NULL;
# print-clean.sh
targets=`grep '^.*:' Makefile | cut -d: -f1` # this isn't a general solution
echo rm -f $targets
# Makefile
a:
echo 'contents of a' > $@
clean:
sh print-clean.sh > $@
@ingramj
ingramj / brainfuck.rb
Created February 25, 2009 00:37
A Brainfuck interpreter written in Ruby.
#!/usr/bin/env ruby
class BrainFuck
def initialize
@ops = create_ops
@tape = Array.new(1024,0)
@tp = 0
@code = []
@cp = 0
end
@ingramj
ingramj / getdelim.c
Created July 25, 2011 20:27
Implementations of the getdelim() and getline() functions from POSIX 2008, just in case your libc doesn't have them. They should comply with the POSIX standard, but they haven't been thoroughly tested yet.
/* Implementations of the getdelim() and getline() functions from POSIX 2008,
just in case your libc doesn't have them.
getdelim() reads from a stream until a specified delimiter is encountered.
getline() reads from a stream until a newline is encountered.
See: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html
NOTE: It is always the caller's responsibility to free the line buffer, even
when an error occurs.