From f98f768869eac86701bd7291c966df46fd6df97d Mon Sep 17 00:00:00 2001
From: turret <turret@duck.com>
Date: Thu, 29 Jun 2023 17:00:38 -0500
Subject: donut: add cli arguments

---
 arg.h        | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 config.def.h |  3 +--
 donut.c      | 34 ++++++++++++++++++++++++++++++----
 3 files changed, 80 insertions(+), 6 deletions(-)
 create mode 100644 arg.h

diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..e94e02b
--- /dev/null
+++ b/arg.h
@@ -0,0 +1,49 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN	for (argv0 = *argv, argv++, argc--;\
+					argv[0] && argv[0][0] == '-'\
+					&& argv[0][1];\
+					argc--, argv++) {\
+				char argc_;\
+				char **argv_;\
+				int brk_;\
+				if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+					argv++;\
+					argc--;\
+					break;\
+				}\
+				for (brk_ = 0, argv[0]++, argv_ = argv;\
+						argv[0][0] && !brk_;\
+						argv[0]++) {\
+					if (argv_ != argv)\
+						break;\
+					argc_ = argv[0][0];\
+					switch (argc_)
+
+#define ARGEND			}\
+			}
+
+#define ARGC()		argc_
+
+#define EARGF(x)	((argv[0][1] == '\0' && argv[1] == NULL)?\
+				((x), abort(), (char *)0) :\
+				(brk_ = 1, (argv[0][1] != '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#define ARGF()		((argv[0][1] == '\0' && argv[1] == NULL)?\
+				(char *)0 :\
+				(brk_ = 1, (argv[0][1] != '\0')?\
+					(&argv[0][1]) :\
+					(argc--, argv++, argv[0])))
+
+#endif
diff --git a/config.def.h b/config.def.h
index f8a1bb3..968b48c 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,3 +1,2 @@
 #define FPS_MAX 50
-#define SCALE 2
-#define ROTATION_SPEED_FACTOR 1
+
diff --git a/donut.c b/donut.c
index 250d0ce..b136c36 100644
--- a/donut.c
+++ b/donut.c
@@ -23,13 +23,15 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
-#define __USE_MISC
 #include <math.h>
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include <X11/Xos.h>
 
+char *argv0;
+#include "arg.h"
+
 #define BETWEEN(x, min, max) min < x && x< max
 
 const float theta_spacing = 0.07;
@@ -80,7 +82,32 @@ void drawTorus (float *b, const float *Az, const float *Bz) {
     free(z);
 }
 
-int main() {
+void usage() {
+    fprintf(stderr, "usage: %s [-h] [-s scale] [-r speed]\n", argv0);
+    exit(1);
+}
+
+int main(int argc, char **argv) {
+    /*#define SCALE 2
+    #define ROTATION_SPEED_FACTOR 1*/
+
+    int SCALE = 2;
+    int ROTATION_SPEED_FACTOR = 1;
+
+    ARGBEGIN {
+        case 's':
+            SCALE = atoi(EARGF(usage()));
+            break;
+        case 'r':
+            ROTATION_SPEED_FACTOR = atoi(EARGF(usage()));
+            break;
+        case 'h':
+            usage();
+            break;
+        default:
+            break;
+    } ARGEND
+
     Display *dis = XOpenDisplay((char*)0);
     int screen = DefaultScreen(dis);
     unsigned long black = BlackPixel(dis, screen), white = WhitePixel(dis, screen);
@@ -137,8 +164,7 @@ int main() {
         A += (rtime-stime) * (0.04f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR);
         B += (rtime-stime) * (0.02f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR);
 
-        printf("%.1f %d  ", (rtime-stime), 1000/(FPS_MAX));
-        printf("fps: %.1f %d     \r", 1000/(rtime-stime), FPS_MAX);
+        printf("fps: %.1f     \r", 1000/(rtime-stime));
     }
 }
 
-- 
cgit v1.2.3