--- /dev/null
+# src-da/0_makefile
+
+SHELL=/bin/bash
+
+-include 0_makefile-flags
+
+MAKE=/usr/bin/make -f $(PROJECT_SUBU)/tools/lib/makefile_cc
+
+all: version deps lib execs
+
+lib:
+ $(MAKE) $@
+ cp da.lib.h 1_include/da.h
+
+%::
+ $(MAKE) $@
+
+
+
+
--- /dev/null
+
+# some versions of Linux need a -e option others complain if there is a -e .. and it isn't the binary for echo ..
+ECHO= echo
+#ECHO= echo -e
+
+# directories used by this makefile, these could all be set to dot for
+# the simplest source directory structure
+
+#LIDBIR, EXECSDIR, HDIR hold the make results that might later be staged
+#$(PWD) is the directory that make was called from, this is already build in
+#set to dot to use the same directory as the source code
+#leave blank to ommit
+DEPRDIR=1_deprecated
+DOCDIR=1_doc
+EXECSDIR=1_execs
+INCDIR=1_include
+LIBDIR=1_lib
+TESTDIR=1_tests
+TMPDIR=1_tmp
+TOOLSDIR=$(realpath $(PROJECT_SUBU)/tools)
+TRYDIR=1_try
+
+
+# compiler and flags
+CC=gcc
+CFLAGS=-std=gnu11 -fPIC -I. -ggdb -Werror -DDEBUG -DDEBUGDB
+#CFLAGS=-std=gnu11 -fPIC -I. -Werror
+LINKFLAGS=-L1_lib -lda
+
+LIB_FILE=libda.a
+INC_FILE=da.h
--- /dev/null
+#ifndef DA_LIB_H
+#define DA_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct Da{
+ char *base;
+ char *end; // one byte/one item off the end of the array
+ size_t size; // size >= (end - base) + 1;
+ size_t item_size;
+} Da;
+
+#define RETURN(dap, r) \
+ { da_map(dap, da_free, NULL); return r; }
+
+void da_alloc(Da *dap, size_t item_size);
+char *da_expand(Da *dap);
+void da_rebase(Da *dap, char *old_base, void *pta);
+bool da_endq(Da *dap, void *pt);
+bool da_boundq(Da *dap);
+void da_push(Da *dap, void *item);
+void da_map(Da *dap, void f(void *, void *), void *closure);
+void da_free(void *pt, void *closure);
+
+#endif
+
--- /dev/null
+# src-da/1_tests/0_makefile
+
+SHELL=/bin/bash
+
+-include 0_makefile-flags
+
+MAKE=/usr/bin/make -f $(PROJECT_SUBU)/tools/lib/makefile_cc
+
+all: version deps lib execs
+
+%::
+ $(MAKE) $@
+
+
+
+
--- /dev/null
+
+# some versions of Linux need a -e option others complain if there is a -e .. and it isn't the binary for echo ..
+ECHO= echo
+#ECHO= echo -e
+
+# directories used by this makefile, these could all be set to dot for
+# the simplest source directory structure
+
+#LIDBIR, EXECSDIR, HDIR hold the make results that might later be staged
+#$(PWD) is the directory that make was called from, this is already build in
+#set to dot to use the same directory as the source code
+#leave blank to ommit
+DEPRDIR=
+DOCDIR=
+EXECSDIR=.
+INCDIR=.
+LIBDIR=.
+TESTDIR=.
+TMPDIR=1_tmp
+TOOLSDIR=$(realpath $(PROJECT_SUBU)/tools)
+TRYDIR=
+
+
+# compiler and flags
+CC=gcc
+CFLAGS=-std=gnu11 -fPIC -I. -I../1_include -ggdb -Werror -DDEBUG -DDEBUGDB
+#CFLAGS=-std=gnu11 -fPIC -I. -I../1_include -Werror
+LINKFLAGS=-L. -L../1_lib -ltests -lda
+
+LIB_FILE=libtests.a
+
--- /dev/null
+/*
+Tests for da.
+
+*/
+
+#include "da.h"
+#include "test_da.lib.h"
+#include <stdbool.h>
+
+int test_da_0(){
+ da da0;
+ da_alloc(&da0, sizeof(int)); // leaves room for 4 ints
+ int i = 0;
+ int *pt = da0->base;
+ // will double, 4 -> 8, then double 8 -> 16
+ while( i < 10 ){
+ if(da_boundq(&da0, pt)){
+ char *old_base = da_expand(&da);
+ da_rebase(&da, old_base, pt);
+ }
+ *pt = i;
+ i++;
+ pt++;
+ }
+
+ bool f0 = da.size == sizof(int) * 16;
+ bool f1 = 10 == (da.end - da.base) / sizeof(int);
+ bool f2 = true;
+ pt = da0->base;
+ while( i < 10 ){
+ f2 = f2 && *pt == i && !da_endq(&da, pt);
+ i++;
+ pt++;
+ }
+ bool f3 = da_endq(&da, pt);
+
+ return f0 && f1 && f2 && f3;
+}
+
+
--- /dev/null
+passed all 2 tests
--- /dev/null
+
+#include <stdio.h>
+#include <stdbool.h>
+#include "test_da.lib.h"
+
+int main(){
+ bool da_0_passed = test_da_0();
+
+ unsigned int passed = 0;
+ unsigned int failed = 0;
+
+ if( !test_da_0() ){
+ failed++;
+ printf("test_da_0 failed\n");
+ }else
+ passed++;
+
+ if( !test_da_1() ){
+ failed++;
+ printf("test_da_1 failed\n");
+ }else
+ passed++;
+
+ if( passed == 0 && failed == 0)
+ printf("no tests ran\n");
+ else if( passed == 0 )
+ printf("failed all %u tests\n", failed);
+ else if( failed == 0 )
+ printf("passed all %u tests\n", passed);
+ else
+ printf("failed %u of %u tests\n", failed, passed + failed);
+
+ if( passed == 0 || failed != 0 ) return 1;
+ return 0;
+}
--- /dev/null
+/*
+Tests for Da.
+
+*/
+
+#include <stdbool.h>
+#include <da.h>
+
+#include "test_da.lib.h"
+
+
+int test_da_0(){
+ Da da;
+ da_alloc(&da, sizeof(int)); // leaves room for 4 ints
+ int i = 0;
+ int *pt = (int *)da.base;
+ // will double, 4 -> 8, then double 8 -> 16
+ while( i < 10 ){
+ da_push(&da, &i);
+ i++;
+ pt++;
+ }
+
+ bool f0 = da.size == sizeof(int) * 16;
+ bool f1 = 10 == (da.end - da.base) / sizeof(int);
+ bool f2 = true;
+ pt = (int *)da.base;
+ i = 0;
+ while( i < 10 ){
+ f2 = f2 && *pt == i && !da_endq(&da, pt);
+ i++;
+ pt++;
+ }
+ bool f3 = da_endq(&da, pt);
+
+ return f0 && f1 && f2 && f3;
+}
+
+int test_da_1(){
+ Da da;
+ da_alloc(&da, sizeof(int)); // leaves room for 4 ints
+ int i = 0;
+ int *pt = (int *)da.base;
+ // will double, 4 -> 8, then double 8 -> 16
+ while( i < 10 ){
+ da.end += da.item_size;
+ if( da_boundq(&da) ){
+ char *old_base = da_expand(&da);
+ da_rebase(&da, old_base, &pt);
+ }
+ *pt = i;
+ i++;
+ pt++;
+ }
+
+ bool f0 = da.size == sizeof(int) * 16;
+ bool f1 = 10 == (da.end - da.base) / sizeof(int);
+ bool f2 = true;
+ pt = (int *)da.base;
+ i = 0;
+ while( i < 10 ){
+ f2 = f2 && *pt == i && !da_endq(&da, pt);
+ i++;
+ pt++;
+ }
+ bool f3 = da_endq(&da, pt);
+
+ return f0 && f1 && f2 && f3;
+}
+
+
--- /dev/null
+#ifndef DA_LIB_H
+#define DA_LIB_H
+
+int test_da_0();
+int test_da_1();
+
+#endif
#include "da.lib.h"
-#if INTERFACE
#include<stdlib.h>
#include<stdbool.h>
-#endif
#include<string.h>
//--------------------------------------------------------------------------------
// We manipulate pointers to a smallest addressable unit. The sizeof operator
// returns counts in these addressable units. Sizeof(char) is defined to be 1.
-#if INTERFACE
-struct da{
- char *base;
- char *end; // one byte/one item off the end of the array
- size_t size; // size >= (end - base) + 1;
- size_t item_size;
-};
-#endif
-
-void da_alloc(da *dap, size_t item_size){
- dap->size = 4 * item_size;
+void da_alloc(Da *dap, size_t item_size){
dap->item_size = item_size;
- dap->end = 0;
+ dap->size = 4 * item_size;
dap->base = malloc(dap->size);
+ dap->end = dap->base;
}
// Doubles size of of da. Returns old base, so that existing pointers into the
// array can be moved to the new array
-char *da_expand(da *dap){
+char *da_expand(Da *dap){
size_t end_offset = ((char *)dap->end - (char *)dap->base);
- size_t new_size = dap->size << 1;
char *old_base = dap->base;
+ size_t new_size = dap->size << 1;
char *new_base = malloc( new_size );
- memcpy( new_base, dap->base, end_offset + 1);
+ memcpy( new_base, old_base, end_offset + dap->item_size);
free(old_base);
dap->base = new_base;
dap->end = new_base + end_offset;
return old_base;
}
-void da_rebase(da *dap, char *old_base, void *pta){
+void da_rebase(Da *dap, char *old_base, void *pta){
char **pt = (char **)pta;
size_t offset = *pt - old_base;
*pt = dap->base + offset;
}
// true when pt has run off the end of the area currently allocated for the array
-bool da_endq(da *dap, void *pt){
+bool da_endq(Da *dap, void *pt){
return (char *)pt >= dap->end;
}
// true when pt has run off the end of the area allocated for the array
-bool da_boundq(da *dap, void *pt){
- return (char *)pt >= dap->base + dap->size;
+bool da_boundq(Da *dap){
+ return dap->end >= dap->base + dap->size;
}
-void da_push(da *dap, void *item){
- if( dap->end + dap->item_size >= dap->base + dap->size ) da_expand(dap);
+void da_push(Da *dap, void *item){
+ if( dap->end >= dap->base + dap->size ) da_expand(dap);
memcpy(dap->end, item, dap->item_size);
dap->end += dap->item_size;
}
// passed in f(item_pt, arg_pt)
// We have no language support closures, so we pass in an argument for it.
// The closure may be set to NULL if it is not needed.
-void da_map(da *dap, void f(void *, void *), void *closure){
+void da_map(Da *dap, void f(void *, void *), void *closure){
char *pt = dap->base;
while( pt != dap->end ){
f(pt, closure);
free(pt);
}
-#if INTERFACE
-#define RETURN(dap, r) \
- { da_map(dap, da_free, NULL); return r; }
-#endif
-
+// Puts text from a line into buffer *dap. Does not push EOF or '\n' into the
+// buffer. Returns the line terminator, which will either be EOF or '\n'. This
+// will not work very well if the line gets very long, as da doubles in size at
+// buffer overrun. items_size for dap must be sizeof(char).
+int da_fgets(Da *dap, FILE *fd){
+ int c = fgetc(fd);
+ while( c != EOF && c != '\n' ){
+ da_push(dap, &c);
+ c = fgetc(fd);
+ }
+ int terminator = 0;
+ da_push(dap, &terminator);
+ return c;
+}
--- /dev/null
+#ifndef DA_LIB_H
+#define DA_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct Da{
+ char *base;
+ char *end; // one byte/one item off the end of the array
+ size_t size; // size >= (end - base) + 1;
+ size_t item_size;
+} Da;
+
+#define RETURN(dap, r) \
+ { da_map(dap, da_free, NULL); return r; }
+
+void da_alloc(Da *dap, size_t item_size);
+char *da_expand(Da *dap);
+void da_rebase(Da *dap, char *old_base, void *pta);
+bool da_endq(Da *dap, void *pt);
+bool da_boundq(Da *dap);
+void da_push(Da *dap, void *item);
+void da_map(Da *dap, void f(void *, void *), void *closure);
+void da_free(void *pt, void *closure);
+
+#endif
+
--- /dev/null
+# src/0_makefile
+
+SHELL=/bin/bash
+
+-include 0_makefile_flags
+
+SUID_TOOL=$(TOOLSDIR)/bin/setuid_root.sh
+MAKE=/usr/bin/make -f $(PROJECT_SUBU)/tools/lib/makefile_cc
+
+SOURCES=$(wildcard *.c)
+HFILES=$(wildcard *.h)
+
+all: version deps lib execs
+
+version:
+ $(MAKE) $@
+ @echo "SUID_TOOL: " $(SUID_TOOL)
+
+deps:
+ makeheaders $(SOURCES) $(HFILES)
+ sed -i '/^ *int *main *(.*)/d' *.h
+ $(MAKE) $@
+
+execs:
+ $(MAKE) $@
+ @echo "-> $(SUID_TOOL) $(EXECSDIR)/subu-mk-0 $(EXECSDIR)/subu-rm-0 $(EXECSDIR)/subu-bind-all"
+ cat $(SUID_TOOL)
+ @echo -n "Are you sure? [y/N] " && read ans && [ $${ans:-N} == y ]
+ sudo $(SUID_TOOL) $(EXECSDIR)/subu-mk-0 $(EXECSDIR)/subu-rm-0 $(EXECSDIR)/subu-bind-all
+
+clean:
+ $(MAKE) $@
+ for i in $(HFILES); do rm $$i; done
+
+%::
+ $(MAKE) $@
+
+
+
+
--- /dev/null
+
+# some versions of Linux need a -e option others complain if there is a -e .. and it isn't the binary for echo ..
+ECHO= echo
+#ECHO= echo -e
+
+# directories used by this makefile, these could all be set to dot for
+# the simplest source directory structure
+
+#LIDBIR, EXECSDIR, HDIR hold the make results that might later be staged
+#$(PWD) is the directory that make was called from, this is already build in
+#set to dot to use the same directory as the source code
+#leave blank to ommit
+DEPRDIR=1_deprecated
+DOCDIR=1_doc
+EXECSDIR=1_execs
+HDIR=1_headers
+LIBDIR=1_lib
+TESTDIR=1_tests
+TMPDIR=1_tmp
+TOOLSDIR=$(realpath $(PROJECT_SUBU)/tools)
+TRYDIR=1_try
+
+
+# compiler and flags
+CC=gcc
+CFLAGS=-std=gnu11 -fPIC -I. -ggdb -Werror -DDEBUG -DDEBUGDB
+#CFLAGS=-std=gnu11 -fPIC -I. -Werror
+LINKFLAGS=-L1_lib -lsubu -lsqlite3
+
+LIB_FILE=$(LIBDIR)/libsubu.a
+
--- /dev/null
+dbprintf.lib.o: dbprintf.lib.c dbprintf.lib.h
--- /dev/null
+/* \aThis file was automatically generated. Do not edit! */
+#undef INTERFACE
+int dbprintf(const char *format,...);
--- /dev/null
+# src/0_makefile
+
+SHELL=/bin/bash
+
+-include 0_makefile_flags
+
+SUID_TOOL=$(TOOLSDIR)/bin/setuid_root.sh
+MAKE=/usr/bin/make -f $(PROJECT_SUBU)/tools/lib/makefile_cc
+
+SOURCES=$(wildcard *.c)
+HFILES=$(wildcard *.h)
+
+all: version deps lib execs
+
+version:
+ $(MAKE) $@
+ @echo "SUID_TOOL: " $(SUID_TOOL)
+
+deps:
+ makeheaders $(SOURCES) $(HFILES)
+ sed -i '/^ *int *main *(.*)/d' *.h
+ $(MAKE) $@
+
+execs:
+ $(MAKE) $@
+ @echo "-> $(SUID_TOOL) $(EXECSDIR)/subu-mk-0 $(EXECSDIR)/subu-rm-0 $(EXECSDIR)/subu-bind-all"
+ cat $(SUID_TOOL)
+ @echo -n "Are you sure? [y/N] " && read ans && [ $${ans:-N} == y ]
+ sudo $(SUID_TOOL) $(EXECSDIR)/subu-mk-0 $(EXECSDIR)/subu-rm-0 $(EXECSDIR)/subu-bind-all
+
+clean:
+ $(MAKE) $@
+ for i in $(HFILES); do rm $$i; done
+
+%::
+ $(MAKE) $@
+
+
+
+
--- /dev/null
+
+# some versions of Linux need a -e option others complain if there is a -e .. and it isn't the binary for echo ..
+ECHO= echo
+#ECHO= echo -e
+
+# directories used by this makefile, these could all be set to dot for
+# the simplest source directory structure
+
+#LIDBIR, EXECSDIR, HDIR hold the make results that might later be staged
+#$(PWD) is the directory that make was called from, this is already build in
+#set to dot to use the same directory as the source code
+#leave blank to ommit
+DEPRDIR=1_deprecated
+DOCDIR=1_doc
+EXECSDIR=1_execs
+HDIR=1_headers
+LIBDIR=1_lib
+TESTDIR=1_tests
+TMPDIR=1_tmp
+TOOLSDIR=$(realpath $(PROJECT_SUBU)/tools)
+TRYDIR=1_try
+
+
+# compiler and flags
+CC=gcc
+CFLAGS=-std=gnu11 -fPIC -I. -ggdb -Werror -DDEBUG -DDEBUGDB
+#CFLAGS=-std=gnu11 -fPIC -I. -Werror
+LINKFLAGS=-L1_lib -lsubu -lsqlite3
+
+LIB_FILE=$(LIBDIR)/libsubu.a
+
--- /dev/null
+/*
+The purpose of this tools is to facilitate putting prototypes (declarations) next
+to implementations (definitions) in a single source file of a C/C++ programs.
+
+Splits a single source file into multiple files. Scans through the single
+source file looking for lines of the form:
+
+ #tranche filename ...
+
+With the # mandatory at column 1 (like the old C preprocessor). Upon finding
+such a line, copies all following lines into the listed files, until reaching the
+end marker:
+
+ #endtranche
+
+A next improvement of this file would be to support variables to be passed in,
+as per a make file.
+
+*/
+
+
+void tranche(FILE *file){
+
+
+
+}
--- /dev/null
+#ifndef DA_LIB_H
+#define DA_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct Da{
+ char *base;
+ char *end; // one byte/one item off the end of the array
+ size_t size; // size >= (end - base) + 1;
+ size_t item_size;
+} Da;
+
+#define RETURN(dap, r) \
+ { da_map(dap, da_free, NULL); return r; }
+
+void da_alloc(Da *dap, size_t item_size);
+char *da_expand(Da *dap);
+void da_rebase(Da *dap, char *old_base, void *pta);
+bool da_endq(Da *dap, void *pt);
+bool da_boundq(Da *dap);
+void da_push(Da *dap, void *item);
+void da_map(Da *dap, void f(void *, void *), void *closure);
+void da_free(void *pt, void *closure);
+
+#endif
+
SHELL=/bin/bash
--include 0_makefile_flags
+-include 0_makefile-flags
DEPS_FILE=$(TMPDIR)/makefile_deps
@echo "DEPDIR: " $(DEPDIR)
@echo "DOCDIR: " $(DOCDIR)
@echo "EXECSDIR: " $(EXECSDIR)
- @echo "HDIR: " $(HDIR)
+ @echo "INCDIR: " $(INCDIR)
@echo "LIBDIR: " $(LIBDIR)
@echo "TESTDIR: " $(TESTDIR)
@echo "TMPDIR: " $(TMPDIR)
if [ ! -e $(DEPRDIR) ]; then mkdir $(DEPRDIR); fi
if [ ! -e $(DOCDIR) ]; then mkdir $(DOCDIR); fi
if [ ! -e $(EXECSDIR) ]; then mkdir $(EXECSDIR); fi
- if [ ! -e $(HDIR) ]; then mkdir $(HDIR); fi
+ if [ ! -e $(INCDIR) ]; then mkdir $(INCDIR); fi
if [ ! -e $(LIBDIR) ]; then mkdir $(LIBDIR); fi
if [ ! -e $(TESTDIR) ]; then mkdir $(TESTDIR); fi
if [ ! -e $(TMPDIR) ]; then mkdir $(TMPDIR); fi
$(CC) $(CFLAGS) -MM $(SOURCES) 1> $(DEPS_FILE)
for i in $(EXECS) ; do\
$(ECHO) >> $(DEPS_FILE);\
- $(ECHO) "$(EXECSDIR)/$$i : $$i.cli.o $(LIB_FILE)" >> $(DEPS_FILE);\
+ $(ECHO) "$(EXECSDIR)/$$i : $$i.cli.o $(LIBDIR)/$(LIB_FILE)" >> $(DEPS_FILE);\
$(ECHO) " $(CC) -o $(EXECSDIR)/$$i $$i.cli.o $(LINKFLAGS)" >> $(DEPS_FILE);\
done
@echo '______end make $@_____'
make sub_lib
@echo '______end make $@_____'
-sub_lib: $(LIB_FILE)
+sub_lib: $(LIBDIR)/$(LIB_FILE)
-execs: $(LIB_FILE)
+execs: $(LIBDIR)/$(LIB_FILE)
@echo '---- make $@:------------------------------------------------------------'
@echo `pwd`'>'
if [ ! -e $(DEPS_FILE) ]; then make deps; fi
sub_execs: $(patsubst %, $(EXECSDIR)/%, $(EXECS))
+stage:
+ if [ -f $(LBIDIR)/$(LIBFILE) ]; then cp $(LIBDIR)/$(LIBFILE) $(PROJECT_SUBU)/stage/lib; fi
+ if [ -f $(INCDIR)/$(INC_FILE) ]; then cp $(INCDIR)/$(INC_FILE) $(PROJECT_SUBU)/stage/include; fi
+
clean:
@echo '---- make $@:------------------------------------------------------------'
@echo `pwd`'>'
for i in $(wildcard *~); do mv $$i $(TMPDIR); done
for i in $(wildcard *.lib.o) $(wildcard *.cli.o); do rm $$i; done
for i in $(EXECS); do if [ -e $(EXECSDIR)/$$i ]; then rm $(EXECSDIR)/$$i; fi; done
- if [ -f $(LIB_FILE) ]; then rm $(LIB_FILE); fi
+ if [ -f $(LIBDIR)/$(LIB_FILE) ]; then rm $(LIBDIR)/$(LIB_FILE); fi
if [ -f $(DEPS_FILE) ]; then rm $(DEPS_FILE); fi
@echo '______end make $@_____'
@echo '______end make $@_____'
#
-$(LIB_FILE) : $(OBJECTS_LIB)
- ar rcs $(LIB_FILE) $(OBJECTS_LIB)
+$(LIBDIR)/$(LIB_FILE) : $(OBJECTS_LIB)
+ ar rcs $(LIBDIR)/$(LIB_FILE) $(OBJECTS_LIB)
-include $(DEPS_FILE)
+++ /dev/null
-/*
-looks for
-
-#tranche filename ...
-
-Starting at column 1 of each line. Upon finding that, it copies all following lines
-into filename, until reaching a line that has:
-
-#endtranche
-
-
-*/