[Snark] C #define True "true"

Finn Alexander O'Leary submissions at badcode.rocks
Fri Oct 26 00:53:44 UTC 2018


I hope you enjoy reading this as much as I was horrified when writing it.
I deliberately went against every single design sense I had. Although, I
am perversely proud of the execl() hack, it was very much an afterthought,
and cheaper to write than string parsing.

== License ==

This code can be considered as dual-licensed under:
CC-BY-SA
GNU GPLv2

== piglatin.c ==

/* piglatin.c
 * Written for the badcode.rocks contest.
 * Author: Finn Alexander O'Leary
 * Email: finnoleary at inventati.org
 * Date: 2018-10-10
 * License: This code can be considered as dual-licensed under:
 *          CC-BY-SA
 *          GNU GPLv2
 * How To: gcc piglatin.c -o piglatin
 * Notes: I hope you enjoy this as much as I was horrified while making it.
 * 
 *        (Un-?)Surprisingly, gcc (by default) does not complain at all.
 *        CPPcheck spits out two useful diagnostics. Sparse prints out a
 *        load of fluff about mixing definitions and code.
 */


#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>

#define True "true"
#define False "false"
static char indexer_for_the_word_thingies = 1;

static char ***A;

struct __attribute__((packed)) thingimajigie {
	char **start;
	char *end;
} word_thingies[256];

struct capture {
	int i, j;
};

int _strlen(char *s)
{
	return (s ? strlen(s) : 0);
}

void load_me_the_thingie_withie(int charie, char *thisie) {
	unsigned char i = ((2)<<6);
	do {
		void *S = &(word_thingies[i]);
		if (i < charie) {
			((struct thingimajigie *)S)->start = &(*A)[i];
			((struct thingimajigie *)S)->end = malloc(_strlen((*A)[i])+1);
			memset((((struct thingimajigie *)S)->end), 'A', _strlen((*A)[i]));
			*((char*)((struct thingimajigie *)S)->end+strlen((*A)[i])) = '\0';
			S -= (sizeof(char**)+sizeof(char*));
		}
	} while (i--);
}


/* KITTEN <-> CAT <-> CONCATENATE... it's a bad joke don't @ me */
#define KITTEN(a, b) a # b
#define BALL_OF_STRING(a,b) a ## b
#define DEFISCHARACTER(string) size_t BALL_OF_STRING(is, string) (int c) { return (c == KITTEN(, string)[0] || tolower(c) == KITTEN(, string)[0]) ? 1 : 0; }

DEFISCHARACTER(a)
DEFISCHARACTER(e)
DEFISCHARACTER(i)
DEFISCHARACTER(o)
DEFISCHARACTER(u)
DEFISCHARACTER(y)

char *isvowelay(char S, char S1) /* REMOVE: USE LIKE: S = isvowelay(string[0], string[1]); strcmp(S, "true");  */
{
	#define ISVOWEL(_) (isa(_) || ise(_) || isi(_) || iso(_) || isu(_))
	if (ISVOWEL(S)) { return True; }
	else if (S == 'y' && !ISVOWEL(S1)) { return True; }
	else if (S == 'x' && S1 == 'r')
		return True;

	return False;
}

struct capture isconsonant(char *string)
{
	int l = 0;
	struct capture c = {.i = 0, .j = 0};
	if (string[0] == 'q' && string[1] == 'u') { 
		c.i = 0;
		c.j = 2;
	}
	else if (!ISVOWEL(string[0]) && string[1] == 'q' && string[2] == 'u') { 
		c.i = 0;
		c.j = 3;
	}
	else if (string[0] == 'y') {
		c.i = 0;
		c.j = 1;
	}
	else {
		int i = 0;
		do {
			i++, c.j++;
		} while (i < strlen(string) && !ISVOWEL(string[i]));
	}
	return c;
}

void write_string(char *dest, char *src, struct capture c)
{
	strncpy(dest, src+c.i, c.j);
}

void dologic(char *S, char *dest)
{
	struct capture a, b, d;
	if (strcmp(True, isvowelay(S[0], S[1])) == 0) {
		/* append -ay */
		a.i = 0;
		a.j = _strlen(S);
		b.i = 0;
		b.j = 3;
		write_string(dest, S, a);
		write_string(dest+a.j, "ay\0", b);
		return;
	}
	struct capture c = isconsonant(S);
	/* printf("c.i: %d; c.j: %d\n", c.i, c.j); */
	if (c.i == c.j) { 
		a.i = 0;
		a.j = _strlen(S);
		write_string(dest, S, a);
	}
	else {
		a.i = c.j;
		a.j = strlen(S)-c.j;

		b.i = 0;
		b.j = c.j;

		d.i = 0;
		d.j = 3;

		write_string(dest, S, a);
		write_string(dest+a.j, S, b);
		write_string(dest+a.j+b.j, "ay\0", d);
	}
}

int people_gave_one_argument_with_the_words_rather_than_giving_them_as_separate_arguments()
{
	int i;
	for (i = 0; i < strlen(*((*A)+1)); i++)
	{
		if (((*A)[1])[i] == ' ' && isalpha(((*A)[1])[i+1]))
			return 1;
	}
	return -1;
}

int main(int _, char **__)
{
	A = &__;
	load_me_the_thingie_withie(_, NULL);

	if (_ > 0 && people_gave_one_argument_with_the_words_rather_than_giving_them_as_separate_arguments() > 0) {
		/* I never designed for this shit, only noticed when it came to the tests, so instead let's design around it */
		char *command = calloc(strlen(__[0]) + strlen(__[1]) + 2, sizeof(char));
		strcpy(command, __[0]);
		command[strlen(__[0])] = ' ';
		strcpy(command+1+strlen(__[0]), __[1]);
		// printf("command: %s\n", command);
		FILE *f = popen(command, "r");
		int i;
		do {
			fputc((i = fgetc(f)), stdout);
		} while (!feof(f) && i != '\n');
		fclose(f);
		free(command);
		return 0;
	}
	
	/* testing */
	int i;
	for (i = 1; i < _; i++) {
		dologic(*word_thingies[i].start, word_thingies[i].end);
		/* printf("%s %s %s\n", *word_thingies[i].start, isvowelay((*word_thingies[i].start)[0], (*word_thingies[i].start)[1]), word_thingies[i].end); */
		printf("%s", word_thingies[i].end);
		if (i+1 < _) fputc(' ', stdout);
	}
	fputc('\n', stdout);

	return 0;
}


More information about the Snark mailing list