]> Nishi Git Mirror - tewi.git/commitdiff
string op works, log works too
authorNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 09:06:44 +0000 (09:06 +0000)
committerNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 09:06:44 +0000 (09:06 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@3 8739d7e6-ffea-ec47-b151-bdff447c6205

Common/Makefile
Common/cm_log.h [new file with mode: 0644]
Common/cm_string.h [new file with mode: 0644]
Common/log.c [new file with mode: 0644]
Common/string.c
Platform/generic.mk
Server/Makefile
Server/main.c
Server/tw_version.h [new file with mode: 0644]
Server/version.c [new file with mode: 0644]

index f9243e9a11e4db2608ee974e5f4b50c6662bf5b0..e6eccda7c2c7714ffd08e0a9ce2cc2d63a3d6802 100644 (file)
@@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
 .PHONY: all clean
 .SUFFIXES: .c .o
 
-OBJS = string.o
+OBJS = string.o log.o
 
 all: common.a
 
diff --git a/Common/cm_log.h b/Common/cm_log.h
new file mode 100644 (file)
index 0000000..b4807e2
--- /dev/null
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#ifndef __CM_LOG_H__
+#define __CM_LOG_H__
+
+void cm_log(const char* name, const char* log, ...);
+
+#endif
diff --git a/Common/cm_string.h b/Common/cm_string.h
new file mode 100644 (file)
index 0000000..66e646a
--- /dev/null
@@ -0,0 +1,9 @@
+/* $Id$ */
+
+#ifndef __CM_STRING_H__
+#define __CM_STRING_H__
+
+char* cm_strcat(const char* a, const char* b);
+char* cm_strdup(const char* str);
+
+#endif
diff --git a/Common/log.c b/Common/log.c
new file mode 100644 (file)
index 0000000..6eb7bed
--- /dev/null
@@ -0,0 +1,55 @@
+/* $Id$ */
+
+#include "cm_log.h"
+
+#include "cm_string.h"
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+bool cm_do_log = false;
+
+#define LOGNAME_LENGTH 12
+
+void cm_log(const char* name, const char* log, ...) {
+       if(!cm_do_log) return;
+       va_list args;
+       va_start(args, log);
+       char namebuf[LOGNAME_LENGTH + 1];
+       memset(namebuf, '.', LOGNAME_LENGTH);
+       namebuf[LOGNAME_LENGTH] = 0;
+       int i;
+       for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) {
+               namebuf[i] = name[i];
+       }
+
+       char* result = malloc(1);
+       result[0] = 0;
+
+       char cbuf[2];
+       cbuf[1] = 0;
+
+       for(i = 0; log[i] != 0; i++) {
+               if(log[i] == '%') {
+                       i++;
+                       if(log[i] == 's') {
+                               char* tmp = result;
+                               result = cm_strcat(tmp, va_arg(args, char*));
+                               free(tmp);
+                       }
+               } else {
+                       cbuf[0] = log[i];
+                       char* tmp = result;
+                       result = cm_strcat(tmp, cbuf);
+                       free(tmp);
+               }
+       }
+
+       fprintf(stderr, "%s %s\n", namebuf, result);
+       va_end(args);
+
+       free(result);
+}
index a0749a0c2c0c92aeb7c5f5fa6ace589ef08aceeb..dfd94d39b716e9c268bf8fde9b2c1f8e51f3152f 100644 (file)
@@ -1 +1,14 @@
 /* $Id$ */
+
+#include <string.h>
+#include <stdlib.h>
+
+char* cm_strcat(const char* a, const char* b) {
+       char* str = malloc(strlen(a) + strlen(b) + 1);
+       memcpy(str, a, strlen(a));
+       memcpy(str + strlen(a), b, strlen(b));
+       str[strlen(a) + strlen(b)] = 0;
+       return str;
+}
+
+char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
index 717b4c6169186d105d179d46746e8119cbd4a518..d0e4948e838b2d567415d67b06aa345da69eefe3 100644 (file)
@@ -2,6 +2,6 @@
 
 CC = cc
 AR = ar
-CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\"
+CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common
 LDFLAGS =
 LIBS =
index 974d35a3a45c2f6c550b51f4a23ded221d75f4ca..f427984f22f0127bf3ae878cf50d0e3a6f0175ca 100644 (file)
@@ -5,12 +5,12 @@ include $(PWD)/Platform/$(PLATFORM).mk
 .PHONY: all clean
 .SUFFIXES: .c .o
 
-OBJS = ../Common/common.a main.o
+OBJS = version.o main.o
 
 all: tewi$(EXEC)
 
-tewi$(EXEC): $(OBJS)
-       $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
+tewi$(EXEC): $(OBJS) ../Common/common.a
+       $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a
 
 .c.o:
        $(CC) $(CFLAGS) -c -o $@ $<
index db19da639a8d73991fd609181725db87d47ac568..ec48162ab92022157ddc5e62582b9a6337873caf 100644 (file)
@@ -1,3 +1,31 @@
 /* $Id$ */
 
-int main() {}
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include <cm_log.h>
+
+#include "tw_version.h"
+
+extern bool cm_do_log;
+
+int main(int argc, char** argv) {
+       int i;
+       for(i = 1; i < argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
+                               if(!cm_do_log) {
+                                       cm_do_log = true;
+                                       cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
+                               } else {
+                                       cm_do_log = true;
+                               }
+                       } else {
+                               fprintf(stderr, "Unknown option: %s\n", argv[i]);
+                               return 1;
+                       }
+               }
+       }
+       cm_log("Daemon", "Ready");
+}
diff --git a/Server/tw_version.h b/Server/tw_version.h
new file mode 100644 (file)
index 0000000..4711481
--- /dev/null
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#ifndef __TW_VERSION_H__
+#define __TW_VERSION_H__
+
+const char* tw_get_version(void);
+
+#endif
diff --git a/Server/version.c b/Server/version.c
new file mode 100644 (file)
index 0000000..ef33fab
--- /dev/null
@@ -0,0 +1,7 @@
+/* $Id$ */
+
+#include "tw_version.h"
+
+const char* tw_version = "0.00";
+
+const char* tw_get_version(void) { return tw_version; }