Skip to content

Instantly share code, notes, and snippets.

@aniruddha-a
aniruddha-a / rl.c
Created February 12, 2019 17:13
rl.c - readline for crepl.sh
/* shamelessly copied from:
* https://stackoverflow.com/questions/34660459/changing-tab-completion-for-read-builtin-in-bash
* And modified a bit
* Compile it like
* gcc rl.c -lreadline -o rl
* And copy the rl binary to the same directory as crepl.sh
* */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@aniruddha-a
aniruddha-a / crepl.sh
Last active February 13, 2019 04:18
C REPL
#!/bin/bash
# A REPL for C
# Mon Feb 11, 2019
# Aniruddha. A (aniruddha.a@gmail.com)
if [[ -z $(which gcc) ]]; then
echo gcc not installed
exit 1
fi
readonly FG_BLUE=""
@aniruddha-a
aniruddha-a / split-2col.pl
Created February 12, 2015 11:12
Two-column code (like two-column research papers ;-) )
#!/usr/bin/perl -w
$blk = 0;
while (<STDIN>) {
chomp $_;
if (m/\s>\|<\s/) {
($l, $r) = split (/\s>\|<\s/);
print "$l\n";
$rhalf{$blk} = { 'start'=>$., 'code' => '' } if not defined $rhalf{$blk};
$rhalf{$blk}{'code'} .= "$r\n";
@aniruddha-a
aniruddha-a / augparse.c
Last active August 29, 2015 14:08
augeas crud on /etc/network/interfaces
// Test CRUD of an iface in /etc/network/interfaces ( on Ubuntu)
// using the Augeas lib
// Aniruddha. A (aniruddha.a@gmail.com)
// Nov 1, 2014
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
@aniruddha-a
aniruddha-a / csv-merge.pl
Created April 28, 2014 05:48
Match CSV files based on a field (name) and merge them
#!/usr/bin/perl
use Parse::CSV;
use strict;
my %wb2_table = ();
my $workbook2_data = Parse::CSV->new ( file => 'merged_workbook2.csv' );
# Note: the header lines are removed in the CSV files for simplicity
# Parse one workbook and load it to a hash
while (my $ref = $workbook2_data->fetch) {
my @line = @$ref;
my $name = $line[5]; # 5th field is the name here
@aniruddha-a
aniruddha-a / prettytable.lua
Created April 23, 2014 10:45
PrettyTable - in Lua (just a test, not a full fledged module, or as powerful as the Py counterpart)
-- Tue Apr 22 15:46:42 IST 2014
-- Aniruddha. A
--
-- just to see how much effort it is to simulate Py like PrettyTable module
local function max(a,b) return a < b and b or a end
local function printf(...) return io.stdout:write(string.format(...)) end
function pad(s, width, padchar)
local padder = string.rep(padchar or ' ', width)
@aniruddha-a
aniruddha-a / highlight-gdb-bt.pl
Created February 17, 2014 09:34
Perl highlighter for GDB backtrace output
#!/usr/bin/perl -w
##
# Mon Feb 17 14:38:23 IST 2014
# Aniruddha. A (aniruddha.a@gmail.com)
##
$FG_RESET = "";
$FG_RED = "";
$FG_GREEN = "";
$FG_YELLOW = "";
@aniruddha-a
aniruddha-a / linenoise-cli.lua
Created January 11, 2014 09:17
Example to show how an application can create CLIs (multi-word, with help) using Lua-linenoise (in ~120 lines).
-- An command line example
--local S = require 'serpent' -- to see the CLI table
local L = require 'linenoise'
gt = {} -- The global table to hold all CLIs
gfn = {} -- The global table to map CLI to a function
function trim(s)
local n = s:find("%S")
return n and s:match(".*%S", n) or ""
@aniruddha-a
aniruddha-a / lispparse.c
Last active December 31, 2015 11:59
Lisp parse and indent
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef enum {
TOK_ID_VAR = 257,
TOK_SQSTR,
@aniruddha-a
aniruddha-a / lcoroc.c
Created September 13, 2013 06:20
Start a Lua coroutine from C (with a function that can yield) - Lua5.1
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
static int
doyield (lua_State *T)
{
return lua_yield (T, 0);
}