coma

create simple config files from JSON input
git clone git://git.hanetzok.net/coma
Log | Files | Refs | README

commit e12cce345b82bdc57f02410d6bd19e0bb3f748f9
Author: Markus Hanetzok <markus@hanetzok.net>
Date:   Fri,  7 Nov 2025 00:09:45 +0100

initial commit

Diffstat:
A.gitignore | 3+++
AMakefile | 29+++++++++++++++++++++++++++++
AREADME | 17+++++++++++++++++
Acoma.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +coma +*.o +testfiles/ diff --git a/Makefile b/Makefile @@ -0,0 +1,29 @@ +VERSION ?= 0.1 + +CC = gcc +CFLAGS = -Wall -Wextra -Wpedantic -O2 -std=c11 -DVERSION=\"$(VERSION)\" +LDFLAGS = -lcjson + +PREFIX ?= /usr/local +BINDIR = $(PREFIX)/bin + +SRC = coma.c +OBJ = $(SRC:.c=.o) + +all: coma + +coma: $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +install: coma + mkdir -p $(BINDIR) + cp coma $(BINDIR)/coma + +uninstall: + rm -f $(BINDIR)/coma + +clean: + rm -f coma $(OBJ) diff --git a/README b/README @@ -0,0 +1,17 @@ +coma - config manipulator +========================= +Create config files from JSON input. + + +Dependencies +------------ +coma depends on libcjson. + + +Usage +----- +coma works for simple config files that use the <key> = <value> schema. + +coma reads from stdin. If no output filename is provided via the -o flag, +new files will be written to /tmp/coma.txt by default, overwriting +previously created files. diff --git a/coma.c b/coma.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <cjson/cJSON.h> + +#define BUFFSIZE 2048 + +#define VERSION "0.1" +#define OK 0 +#define INVALIDUSE 1 +#define TOOLONG 2 +#define PARSEFAIL 3 + +typedef struct JSONProperty { + char *key; + char *value; +} jprop; + +void die(char *msg, int rc) { + printf("%s\n", msg); + exit(rc); +} + +int main (int argc, char *argv[]) { + int i; + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-v")) + die("coma-"VERSION, OK); + if (!strcmp(argv[i], "-h")) + die("usage: coma [-hv]", INVALIDUSE); + } + + char *input = malloc(sizeof(char) * BUFFSIZE); + size_t inputsize = fread(input, sizeof(char), BUFFSIZE, stdin); + cJSON *json = cJSON_Parse(input); + if (json == NULL) { + const char *error_ptr = cJSON_GetErrorPtr(); + cJSON_Delete(json); + if (error_ptr != NULL) + printf("%s\n", error_ptr); + die("Thou shall not parse\n", PARSEFAIL); + } + free(input); + + cJSON *node; + char *output = malloc(sizeof(char) * inputsize); + for (node = json->child; node != NULL; node = node->next) { + char *key = node->string; + cJSON *item = cJSON_GetObjectItemCaseSensitive(json, key); + if (cJSON_IsString(item) && (item->valuestring != NULL)) { + strcat(output, key); + strcat(output, "="); + strcat(output, item->valuestring); + strcat(output, "\n"); + } + if (cJSON_IsNumber(item) && (item->valuedouble != 0)) { + char numstr[64]; + snprintf(numstr, 64, "%f", item->valuedouble); + strcat(output, key); + strcat(output, "="); + strcat(output, numstr); + strcat(output, "\n"); + } + if (cJSON_IsTrue(item)) { + strcat(output, key); + strcat(output, "=true"); + strcat(output, "\n"); + } + if (cJSON_IsFalse(item)) { + strcat(output, key); + strcat(output, "=false"); + strcat(output, "\n"); + } + } + + cJSON_Delete(json); + printf("%s\n", output); + free(output); + + return OK; +} +