payredu

[WIP] Cross-platform ledger GUI written in c99

commit 407405137373f77aa02a03e56d1d127c83c6d846
parent 2a0b60d627b59c3a4eff1f72306707e246bfafb8
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Thu, 11 Apr 2024 20:02:38 +0530

Add support for cl version 19.38.33134

Modify Makefile to work under bmake without breaking gmake builds

Modify Makefile to support Windows cl

Add -f <filename> flag

Fix typos in HACKING(payredo -> payredu)

Fix /W3 errors (unused variables, wrong int comparison, wrong printf %)

Add Makefile section in HACKING

Update README with proper build instructions for Windows

Add *.obj, *.a and payredu.exe to .gitignore
7 files changed, 103 insertions(+), 47 deletions(-)
M
.gitignore
|
3
+++
M
HACKING
|
25
+++++++++++++++----------
M
Makefile
|
42
+++++++++++++++++++++++++++++-------------
M
README
|
21
++++++++++++---------
M
book.c
|
26
++++++++++++++------------
M
payredu.c
|
31
+++++++++++++++++++++++++++++--
M
strn.h
|
2
+-
diff --git a/.gitignore b/.gitignore
@@ -3,6 +3,9 @@ hot
 hotbook
 *.so
 *.o
+*.obj
+*.a
+payredu.exe
 tags
 
 tests/*
diff --git a/HACKING b/HACKING
@@ -7,21 +7,27 @@ yyyy/mm/dd <till \n>
   ...\n\n // mark posting on \n\n
 
 
-payredo can be bulit as a standalone library(libpayredo) or can be built as an executable.
+payredu can be bulit as a standalone library(libpayredu) or can be built as an executable.
 
-	make libpayredo.a
-	make libpayredo.so
-	make payredo
+	make libpayredu.a
+	make libpayredu.so
+	make payredu
 
 You can control what you want to build by invoking it separately. By default builds everything.
 
+Makefile
+--------
+This project strives to use the same Makefile to work on both gmake and bmake.
+
+If you want to add any ${CC} specific flags, check the part under "#if msvc" in Makefile.
+
 commit.c
-------
+--------
 The original ledger-cli does not do edits, to make the runtime simple but since the database is completely ASCII and unorganized, consequent and programmatic writes creates a stress on the CPU and RAM, making it unsuitable for building touch friendly GUIs or general clients on top of it without comprosing UX.
 
-To prevent this, payredo exposes a commit API which can be used by text editors and other frontends to validate added content before saving it to the file.
+To prevent this, payredu exposes a commit API which can be used by text editors and other frontends to validate added content before saving it to the file.
 
-Once you are done with the changes in the text editor or other GUI, you can commit the data to payredo using the APIs
+Once you are done with the changes in the text editor or other GUI, you can commit the data to payredu using the APIs
 
 	int ledger_commit_text(new_text, new_text_len)
 	int ledger_commit_post(timestamp, comment, comment_len, entries**)

@@ -34,11 +40,10 @@ RETURN VALUE
 ------------
 Both variants return PARSE_OK if suceeded, and -1 on failure.
 
-
-payredo.c
+payredu.c
 ---------
 Similar to ledger-cli
-payredo follows the UNIX style option arguments to make the parsing easy and to combine multiple options
+payredu follows the UNIX style option arguments to make the parsing easy and to combine multiple options
 
 
 parse.c
diff --git a/Makefile b/Makefile
@@ -1,18 +1,34 @@
-GENERAL_FLAGS=-fPIC
+GENERAL_FLAGS = # -fPIC
+
 GUI_LDFLAGS=-lglfw -lGL
-LDFLAGS:=$(GENERAL_FLAGS)  -lm
-CFLAGS:=$(GENERAL_FLAGS) -O0 -I. -g -Werror #-Wpedantic
-export LD_LIBRARY_PATH=.
 
-.DEFAULT_GOAL=payredu
+CFLAGS:=${GENERAL_FLAGS} -I.
+LDFLAGS:=${GENERAL_FLAGS}
+
+# if msvc
+cl_CFLAGS := /nologo /WX /W3 /D_CRT_SECURE_NO_WARNINGS
+cl_exe_ext := .exe
+cl_out := /Fo:
+cl_exe_out := /Fe:
+#  else
+_CFLAGS := -O0 -g -Wpedantic -Werror -lm
+_out := -o
+_exe_out := ${_out}
 
-CC=gcc
+# consolidate
+CFLAGS += ${${CC}_CFLAGS}
+out := ${${CC}_out}
+
+# gmake hack
+_ ?= $^
+
+export LD_LIBRARY_PATH=.
 
-payredu: payredu.c libbook.a
-	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $> -L. -lbook
+payredu${${cl}_exe_ext}: payredu.c libbook.a
+	${CC} ${${CC}exe_out}$@ ${CFLAGS} ${LDFLAGS} $_ 
 
-.o: .c
-	$(CC) $(CFLAGS) -c $> -o $@
+.c.o:
+	${CC} ${CFLAGS} -c $_ ${out}$@
 
 bal:
 	#ledger -f october-2023.txt bal

@@ -22,10 +38,10 @@ balance: balance.c ledger.h
 hot: hot.c libbalance.so
 
 libbook.a: book.o account.o
-	ar cr $@ $>
+	ar cr $@ $_
 
 libbook.so: book.o  account.o
-	$(CC) -shared -Wl,-soname,$@ -o $@ $>
+	${CC} -shared -Wl,-soname,$@ -o $@ $_
 
 refresh:
 	git ls-files | entr sh hot.sh

@@ -36,4 +52,4 @@ format:
 include tests/tests.mk
 
 clean:
-	-rm *.so *.o hotbook libbook.so $(TESTS)
+	-${RM} payredu payredu.exe *.a *.so *.o *.obj hotbook $(TESTS)
diff --git a/README b/README
@@ -10,7 +10,7 @@ NOTE: The quality of the software is beta in the least, it's still in developmen
 To build and run,
 
 	$ make
-	$ ./payredu
+	$ ./payredu -f ledger.dat
 
 Why payredu when ledger-cli exists?
 ------------------------------------

@@ -24,22 +24,25 @@ It should be noted that payredu is usually faster than ledger-cli as it does not
 Goals
 -----
 - Compact as possible
-- Limited regex, ^,$ and *
-- BSD style arguments
-- Native Windows support
+- Useful regex queries (^ $ *)
+- UNIX style arguments
+- Native MSVC support (Windows, ReactOS)
 - csv, emacs export/import
 
 Non-Goals
 ---------
 - Python support
 - Elaborate regex
-- limited REPL
 - XML support
 
-Build
------
+Other Build Targets
+-------------------
+MSVC under Windows,
+
+	C:/payredu> make CC=cl
+
+Experimental GUI with hot reload (Linux only),
 
-	make hot
+	$ make hot
 
-will generate 'hot' which can be used to test the hot-reload functionality with GUI for development.
 This is may be removed in feature to provide only a CUI interface.
diff --git a/book.c b/book.c
@@ -7,7 +7,9 @@
 #include "common.h"
 #include "strn.h"
 
+#ifndef _WIN32
 #include <unistd.h>
+#endif
 
 #ifdef ENABLE_GUI
 #include <GLFW/glfw3.h>

@@ -136,23 +138,23 @@ typedef enum {
 
 void ledger_parse_data(char *text, size_t text_len)
 {
-	setvbuf(stdout, NULL, _IONBF, 0);
 	LedgerParseStates state = DATE;
 	size_t line_no = 1;
 	size_t current_column = 1;
 	time_t t = time(NULL);
-	// printf("1|");
 	size_t i = 0;
-	// TODO it may be possible to push these to the tree itself, explore the possibility
-	// these act as temporary register until we push back the entry to a tree
-	time_t hold_date;
-	vstr_t hold_comment = { 0 };
-	vstr_t hold_register = { 0 };
+	//time_t hold_date;
+	//vstr_t hold_comment = { 0 };
+	//vstr_t hold_register = { 0 };
 	long int hold_amount = LONG_MAX;
 	short hold_sign = -1;
 	size_t hold_denom_id = { 0 };
 	short n_count = 0;
 
+	setvbuf(stdout, NULL, _IONBF, 0);
+	// TODO it may be possible to push these to the tree itself, explore the possibility
+	// these act as temporary register until we push back the entry to a tree
+
 	while (i < text_len) {
 		char c = text[i];
 		// we use \n to identify entry done in ledger

@@ -208,7 +210,7 @@ void ledger_parse_data(char *text, size_t text_len)
 				if (isdigit(c)) {
 					// try to parse a date
 					time_t tn = ledger_timestamp_from_ledger_date(text + i);
-					warningf("%.*s: %ld	", 10, text + i, tn);
+					warningf("%.*s: %ld	", 10, text + i, (long)tn);
 					// date is expected to have the form DD/MM/YYYY (10)
 					i += 10;
 					if (tn == (time_t) - 1) goto ledger_parse_error_handle;

@@ -229,7 +231,7 @@ void ledger_parse_data(char *text, size_t text_len)
 					}
 					comment.len = comment_len;
 					warningf("Comment: %.*s", comment_len,
-							comment);
+							comment.str);
 					state = ENTRY_START;
 				}
 				break;

@@ -260,7 +262,7 @@ void ledger_parse_data(char *text, size_t text_len)
 ledger_who_parsed:
 					who_len = i - who_len;
 					account_add(&rootp, who.str, who_len);
-					warningf("\n(%d) Who: %.*s", i, who_len, who);
+					warningf("\n(%d) Who: %.*s", i, who_len, who.str);
 					state = ENTRY_SIGN_DENOM_AMOUNT;
 					// add to tags here
 				}

@@ -291,7 +293,6 @@ ledger_who_parsed:
 				}
 			 } break;
 			case ENTRY_DENOM: {
-				char _c;
 				warningf("  %d: D:", i + 1);
 				char *denom = text + i;
 				size_t denom_len = 0;

@@ -343,7 +344,7 @@ Entry** ledger_read_file(const char* filename, time_t date_start, time_t date_en
 		entry->from = &me;
 		entry->to = (Entity*)malloc(sizeof(Entity));
 		entry->to->name = (char*)malloc(sizeof(char*) * 20);
-		strcpy(entry->to->name, "Man");
+		strncpy(entry->to->name, "Man", 3);
 	}
 	return new_list;
 }

@@ -354,4 +355,5 @@ void *module_main(char *data, size_t data_len)
 	warning("\n=======| Startality |=======\n");
 	ledger_parse_data(data, data_len);
 	warning("\n========| Fatality |========\n");
+	return NULL;
 }
diff --git a/payredu.c b/payredu.c
@@ -1,8 +1,11 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <dlfcn.h>
+// TODO enable this for GUI
+//#include <dlfcn.h>
 #include <signal.h>
+#ifndef _WIN32
 #include <unistd.h>
+#endif
 
 #include <book.h>
 

@@ -21,8 +24,32 @@ void sig_handle() {
 
 typedef void* (*module_main_func)(const char*, size_t);
 
+#define die(...) \
+	do { printf(__VA_ARGS__); \
+	exit(-1); } while(0);
+
 int main(int argc, char* argv[]) {
-	FILE* in = fopen("october-2023.txt", "r");
+	FILE* in = NULL;
+	char* file_to_open = NULL;
+	// FIXME this is for debugging, payredu will not open any file by default
+	if (argc == 1) file_to_open = "october-2023.txt";
+	else for (int i = 1; i < argc; i++)
+	if (argv[i][0] == '-' && strlen(argv[i]) == 2) {
+		switch (argv[i][1]) {
+			case 'f':
+				file_to_open = argv[++i];
+				break;
+				default:
+				printf("unknown flag: %s\n", argv[i]);
+				break;
+		}
+	} else die("invalid argument: %s", argv[i]);
+
+	if (file_to_open == NULL)
+		die("require a file to open, pass -f <filename>");
+
+	in = fopen(file_to_open, "r");
+
 	char* data = (char*)malloc(2048 * sizeof(char));
 	size_t data_size = 0;
 	size_t c_read =  0;
diff --git a/strn.h b/strn.h
@@ -3,7 +3,7 @@
 
 int natoi(char* str, size_t len) {
 	int final = 0;
-	int i = 0;
+	size_t i = 0;
 	// ignore leading zeroes
 	while(i < len && str[i] == '0') i++;
 	for(;i < len; i++) {