XXIIVV

The Utilities are a collection of little tools created solve specific problems.

A collection of small applications that don't quite have a place in the wiki just yet.

firth

Firth, or 1th, is a postfix calculator for fractions.

Firth is a stack based rpn calculator, created to be an improved version of Unix's dc, written in ANSI C. It supports most basic math operations, variables, and basic branching.

Examples

The following example, will print the mixed fraction 3&1/6.

clr 11 4 div 5 12 div add mix

To print the decimal value of a fraction.

clr 22 7 div 1 791 div sub dec

To get the min() and max() of two numbers.

clr 4 8 < pop end .
clr 4 8 < swp pop end .

To get the floor() of a fraction.

clr 17 4 div dup 1 mod sub .

1th.c

#include <stdio.h>

/* 
Copyright (c) 2020 Devine Lu Linvega

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

typedef struct Fraction {
	int num, den;
} Fraction;

typedef struct Stack {
	int flag, len;
	Fraction data[256];
} Stack;

typedef struct Query {
	int len;
	char data[64];
} Query;

/* helpers */

int
cinu(char c)
{
	return c >= '0' && c <= '9';
}

int
sinu(char *s)
{
	int i = 0;
	if(!s[0])
		return 0;
	while(s[i])
		if(!cinu(s[i++]))
			return 0;
	return 1;
}

int
sint(char *s)
{
	int i = 0, num = 0;
	while(s[i] && cinu(s[i]))
		num = num * 10 + (s[i++] - '0');
	return num;
}

int
scmp(char *a, char *b)
{
	int i = 0;
	while(a[i] == b[i])
		if(!a[i++])
			return 1;
	return 0;
}

int
gcd(int a, int b)
{
	if(b == 0)
		return a;
	return gcd(b, a % b);
}

void
init(Query *s)
{
	s->data[0] = '\0';
	s->len = 0;
}

void
append(Query *s, char c)
{
	if(s->len < 64)
		s->data[s->len++] = c;
	s->data[s->len] = '\0';
}

/* rpn */

int
error(char *err, char *msg)
{
	printf("%s Error: %s\n", err, msg);
	return 0;
}

int
trypop(Stack *s, int len)
{
	int err = s->len - len < 0;
	if(err)
		error("Stack", "underflow");
	return !err;
}

int
trypush(Stack *s, int len)
{
	int err = s->len + len >= 256;
	if(err)
		error("Stack", "overflow");
	return !err;
}

int
tryget(Stack *s, int offset)
{
	int err = s->len - offset < 0;
	if(err)
		error("Stack", "overflow");
	return !err;
}

Fraction
pop(Stack *s)
{
	return s->data[--s->len];
}

Fraction *
push(Stack *s, Fraction f)
{
	s->data[s->len] = f;
	return &s->data[s->len++];
}

Fraction *
get(Stack *s, int offset)
{
	return &s->data[s->len - offset - 1];
}

Fraction
Frac(int num, int den)
{
	Fraction f;
	int d = gcd(num, den);
	f.num = num / d;
	f.den = den / d;
	return f;
}

void
print(Stack *s, int format)
{
	int i;
	if(s->len < 1)
		return;
	for(i = 0; i < s->len; ++i) {
		Fraction *f = &s->data[i];
		if(format == 1)
			printf("%f ", f->num / (double)f->den);
		else if(format == 2 && f->num > f->den)
			printf("%d&%d/%d ", f->num / f->den, f->num % f->den, f->den);
		else if(f->den != 1)
			printf("%d/%d ", f->num, f->den);
		else
			printf("%d ", f->num);
	}
	printf("\n");
}

/* ops */

int
request(Stack *s, char *qd)
{
	if((qd[0] == '(' || scmp(qd, "esc")))
		s->flag += 2;
	else if((qd[0] == ')' || scmp(qd, "end")))
		s->flag -= s->flag > 0 ? 2 : 0;
	else if(s->flag)
		return 1;
	else if(!qd[0] || qd[0] == ' ' || qd[0] == '.')
		print(s, 0);
	else if(qd[0] == ',' || scmp(qd, "dec"))
		print(s, 1);
	else if(qd[0] == '_' || scmp(qd, "mix"))
		print(s, 2);
	/* stack */
	else if(sinu(qd) && trypush(s, 1))
		push(s, Frac(sint(qd), 1));
	else if(scmp(qd, "pop") && trypop(s, 1))
		pop(s);
	else if(scmp(qd, "clr"))
		s->len = 0;
	else if(scmp(qd, "swp") && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(b.num, b.den));
		push(s, Frac(a.num, a.den));
	} else if(scmp(qd, "dup") && trypop(s, 1) && trypush(s, 1)) {
		Fraction *f = get(s, 0);
		push(s, Frac(f->num, f->den));
	} else if(scmp(qd, "ovr") && trypop(s, 2) && trypush(s, 1)) {
		Fraction *f = get(s, 1);
		push(s, Frac(f->num, f->den));
	} else if(scmp(qd, "rot") && trypop(s, 3)) {
		Fraction c = pop(s);
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(b.num, b.den));
		push(s, Frac(c.num, c.den));
		push(s, Frac(a.num, a.den));
		/* arithmetic */
	} else if((qd[0] == '+' || scmp(qd, "add")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) + (a.den * b.num), a.den * b.den));
	} else if((qd[0] == '-' || scmp(qd, "sub")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) - (a.den * b.num), a.den * b.den));
	} else if((qd[0] == '*' || scmp(qd, "mul")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(a.num * b.num, a.den * b.den));
	} else if((qd[0] == '/' || scmp(qd, "div")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(a.num * b.den, a.den * b.num));
	} else if((qd[0] == '%' || scmp(qd, "mod")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) % (a.den * b.num), a.den * b.den));
	} else if((scmp(qd, "inv")) && trypop(s, 1)) {
		Fraction f = pop(s);
		push(s, Frac(f.den, f.num));
	} else if(scmp(qd, "vid") && trypop(s, 1) && trypush(s, 1)) {
		Fraction f = pop(s);
		push(s, Frac(f.num, 1));
		push(s, Frac(f.den, 1));
		/* Logic */
	} else if((qd[0] == '=' || scmp(qd, "beq")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num != a->den * b->num) * 2;
	} else if((qd[0] == '!' || scmp(qd, "bne")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num == a->den * b->num) * 2;
	} else if((qd[0] == '>' || scmp(qd, "bgt")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num < a->den * b->num) * 2;
	} else if((qd[0] == '<' || scmp(qd, "blt")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num > a->den * b->num) * 2;
	} else if(qd)
		return error("Command", qd);
	return 1;
}

int
run(Stack *s)
{
	char c;
	Query q;
	init(&q);
	while((c = fgetc(stdin)) != EOF) {
		if(c == ' ' || c == '\n' || !c) {
			request(s, q.data);
			init(&q);
		} else
			append(&q, c);
	}
	return 0;
}

int
main()
{
	Stack s;
	s.len = 0;
	s.flag = 0;
	while(run(&s))
		;
	print(&s, 0);
	return 0;
}
— Submit an edit to 1th.c.txt(293 lines)

seconth

Seconth, or 2th, is a plain-text calendar with events.

Second is a calendar utility created to overlay events on pages of the gregorian calendar, written in ANSI C.

Event Format

The events are stored in text files in the format:

20201126 Sailing trip to Port Townsend

2th.c

#include <stdio.h>
#include <time.h>

/* 
Copyright (c) 2020 Devine Lu Linvega

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#define EVENTMAX 512

typedef struct Event {
	int y, m, d;
	char name[64];
} Event;

typedef struct Calendar {
	int y, m, d, len;
	Event events[EVENTMAX];
} Calendar;

int
slen(char *s)
{
	int i = 0;
	while(s[i] && s[++i])
		;
	return i;
}

int
sint(char *s, int len)
{
	int num = 0, i = 0;
	while(i < len && s[i] && s[i] >= '0' && s[i] <= '9') {
		num = num * 10 + (s[i] - '0');
		i++;
	}
	return num;
}

char *
scpy(char *src, char *dst, int len)
{
	int i = 0;
	while(src[i] && src[i] != '\n' && (dst[i] = src[i]) && i < len - 1)
		i++;
	dst[i] = '\0';
	return dst;
}

int
daysmonth(int y, int m)
{
	if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
		return 31;
	else if((m == 2) && ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)))
		return 29;
	else if(m == 2)
		return 28;
	else
		return 30;
	return 0;
}

int
dayweek(int y, int m, int d)
{
	int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
	y -= m < 3;
	return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}

void
setevent(Calendar *c, int y, int m, int d, char *name)
{
	Event *e = &c->events[c->len++];
	scpy(name, e->name, 64);
	e->y = y;
	e->m = m;
	e->d = d;
}

Event *
getevent(Calendar *c, int y, int m, int d)
{
	int i;
	for(i = 0; i < c->len; i++) {
		Event *e = &c->events[i];
		if(e->y == y && e->m == m && e->d == d)
			return e;
	}
	return NULL;
}

void
printevents(Calendar *c)
{
	int i, y, m, d, l = c->m;
	y = c->y;
	m = c->m;
	d = c->d;
	for(i = 0; i < 90; i++) {
		Event *e = getevent(c, y, m, d);
		if(e) {
			if(l != m)
				printf("\n");
			if(i == 0)
				printf(" Today        ");
			else if(i == 1)
				printf(" Tomorrow     ");
			else
				printf(" In %02d days   ", i);
			printf("%s\n", e->name);
			l = m;
		}
		d++;
		if(d > daysmonth(y, m)) {
			d = 1;
			m++;
			if(m > 12) {
				m = 1;
				y++;
			}
		}
	}
	printf("\n");
}

void
printcalendar(Calendar *c)
{
	int i;
	int dw = dayweek(c->y, c->m, 1);
	int dm = daysmonth(c->y, c->m);
	switch(c->m) {
	case 1: printf(" Jan %d", c->y); break;
	case 2: printf(" Feb %d", c->y); break;
	case 3: printf(" Mar %d", c->y); break;
	case 4: printf(" Apr %d", c->y); break;
	case 5: printf(" May %d", c->y); break;
	case 6: printf(" Jun %d", c->y); break;
	case 7: printf(" Jul %d", c->y); break;
	case 8: printf(" Aug %d", c->y); break;
	case 9: printf(" Sep %d", c->y); break;
	case 10: printf(" Oct %d", c->y); break;
	case 11: printf(" Nov %d", c->y); break;
	case 12: printf(" Dec %d", c->y); break;
	default: printf(" --- %d", c->y);
	}
	printf("\n Su  Mo  Tu  We  Th  Fr  Sa ");
	for(i = 0; i <= 35; i++) {
		int d = i - dw + 1;
		if(i % 7 == 0)
			printf("\n");
		if(d == c->d)
			printf("<%2d>", d);
		else if(getevent(c, c->y, c->m, d))
			printf("[%2d]", d);
		else if(d > 0 && d <= dm)
			printf(" %2d ", d);
		else
			printf("     ");
	}
	printf("\n");
}

void
loadevents(FILE *f, Calendar *c)
{
	char line[256];
	if(!f)
		return;
	while(fgets(line, 256, f)) {
		if(line[0] == ';')
			continue;
		if(slen(line) < 9)
			continue;
		if(c->len >= EVENTMAX)
			break;
		setevent(c,
			line[0] == '*' ? c->y : sint(line, 4),
			line[5] == '*' ? c->m : sint(line + 4, 2),
			line[7] == '*' ? c->d : sint(line + 6, 2),
			line + 9);
	}
	fclose(f);
}

int
main(int argc, char *argv[])
{
	Calendar cal;
	time_t t;
	struct tm *local;
	time(&t);
	local = localtime(&t);
	cal.y = local->tm_year + 1900;
	cal.m = local->tm_mon + 1;
	cal.d = local->tm_mday;
	cal.len = 0;
	if(argc > 1)
		loadevents(fopen(argv[1], "r"), &cal);
	printcalendar(&cal);
	printevents(&cal);
	return 0;
}
— Submit an edit to 2th.c.txt(212 lines)