From: Thomas Walker Lynch Date: Fri, 22 Mar 2019 22:14:15 +0000 (+0100) Subject: tranche passed initial test X-Git-Url: https://git.reasoningtechnology.com/style/static/gitweb.js?a=commitdiff_plain;h=82b14679df98a141dbf8dac2f51f30d5fc84863b;p=subu tranche passed initial test --- diff --git a/src-da/1_include/da.h b/src-da/1_include/da.h index d4e34de..6d3c43b 100644 --- a/src-da/1_include/da.h +++ b/src-da/1_include/da.h @@ -23,8 +23,10 @@ bool da_endq(Da *dap, void *pt); bool da_boundq(Da *dap); void da_push(Da *dap, void *element); bool da_pop(Da *dap, void *element); +char *da_index(Da *dap, size_t i); void da_map(Da *dap, void f(void *, void *), void *closure); void da_free_elements(Da *dap); +void da_strings_puts(Da *dap); char *da_fgets(Da *dap, FILE *fd); #endif diff --git a/src-da/da.lib.c b/src-da/da.lib.c index afa1e51..0956d7e 100644 --- a/src-da/da.lib.c +++ b/src-da/da.lib.c @@ -78,6 +78,12 @@ bool da_pop(Da *dap, void *element){ return flag; } +char *da_index(Da *dap, size_t i){ + size_t offset = i * dap->element_size; + char *pt = dap->base + offset; + return pt; +} + // passed in f(element_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. @@ -91,7 +97,7 @@ void da_map(Da *dap, void f(void *, void *), void *closure){ // da_lists are sometimes used as resource managers static void da_free_element(void *pt, void *closure){ - free(pt); + free(*(char **)pt); // free does not care about the pointer type } void da_free_elements(Da *dap){ @@ -99,6 +105,15 @@ void da_free_elements(Da *dap){ da_rewind(dap); } +// for the case of an array of strings +void da_strings_puts(Da *dap){ + char *pt = dap->base; + while( pt != dap->end ){ + puts(*(char **)pt); + pt += dap->element_size; + } +} + // Puts text from a line into buffer *dap. Does not push EOF or '\n' into the // buffer. Returns the old_base so that external pointers can be rebased. diff --git a/src-da/da.lib.h b/src-da/da.lib.h index d4e34de..6d3c43b 100644 --- a/src-da/da.lib.h +++ b/src-da/da.lib.h @@ -23,8 +23,10 @@ bool da_endq(Da *dap, void *pt); bool da_boundq(Da *dap); void da_push(Da *dap, void *element); bool da_pop(Da *dap, void *element); +char *da_index(Da *dap, size_t i); void da_map(Da *dap, void f(void *, void *), void *closure); void da_free_elements(Da *dap); +void da_strings_puts(Da *dap); char *da_fgets(Da *dap, FILE *fd); #endif diff --git a/src-tranche/0_makefile b/src-tranche/0_makefile index b30d8fb..107d319 100644 --- a/src-tranche/0_makefile +++ b/src-tranche/0_makefile @@ -6,6 +6,10 @@ SHELL=/bin/bash all: version deps lib execs +lib: + $(MAKE) $@ + cp tranche.lib.h 1_include/tranche.h + %:: $(MAKE) $@ diff --git a/src-tranche/1_include/tranche.h b/src-tranche/1_include/tranche.h new file mode 100644 index 0000000..27990a6 --- /dev/null +++ b/src-tranche/1_include/tranche.h @@ -0,0 +1,8 @@ +#ifndef TRANCHE_LIB_H +#define TRANCHE_LIB_H + +int tranche_send(FILE *src, Da *arg_fds); + + + +#endif diff --git a/src-tranche/1_tests/test.dat b/src-tranche/1_tests/test.dat deleted file mode 100644 index 455b725..0000000 --- a/src-tranche/1_tests/test.dat +++ /dev/null @@ -1,17 +0,0 @@ - -#tranche test1.dat test2.dat -The little red hen said to Mick, no thank you not today sir. -And then all the barnes animals shouted out in glee. -No more misery! - -#endtranche - -#tranche apple.dat -apple banana pear -monkey ape bear -#tranche apple.dat nectarine.dat -nectarine orange -never go there said the man -but when you do, pick three -#endtranche -#endtranche diff --git a/src-tranche/1_tests/test1.dat b/src-tranche/1_tests/test1.dat new file mode 100644 index 0000000..b03df3f --- /dev/null +++ b/src-tranche/1_tests/test1.dat @@ -0,0 +1,23 @@ + +#tranche test11.dat test12.dat +The little red hen said to Mick, no thank you not today sir. +And then all the barnes animals shouted out in glee. +No more misery! +#tranche test13.dat +apple banana pear +kiwi +#tranche-end +cows +and cats +#tranche-end + +the between space + +#tranche test14.dat +int float if while +do +function +#tranche-end + +#tranche test15.dat +#tranche-end \ No newline at end of file diff --git a/src-tranche/1_tests/test1.sh b/src-tranche/1_tests/test1.sh new file mode 100644 index 0000000..c1140ca --- /dev/null +++ b/src-tranche/1_tests/test1.sh @@ -0,0 +1,10 @@ +#!/bin/bash -x +./tranche test1.dat >test1stdout.dat +diff test11.dat test11.dat.expected +diff test12.dat test12.dat.expected +diff test13.dat test13.dat.expected +diff test14.dat test14.dat.expected +diff test15.dat test15.dat.expected +diff test1stdout.dat test1stdout.dat.expected +rm test11.dat test12.dat test13.dat test14.dat test15.dat test1stdout.dat + diff --git a/src-tranche/1_tests/test11.dat.expected b/src-tranche/1_tests/test11.dat.expected new file mode 100644 index 0000000..2c2904a --- /dev/null +++ b/src-tranche/1_tests/test11.dat.expected @@ -0,0 +1,5 @@ +The little red hen said to Mick, no thank you not today sir. +And then all the barnes animals shouted out in glee. +No more misery! +cows +and cats diff --git a/src-tranche/1_tests/test12.dat.expected b/src-tranche/1_tests/test12.dat.expected new file mode 100644 index 0000000..2c2904a --- /dev/null +++ b/src-tranche/1_tests/test12.dat.expected @@ -0,0 +1,5 @@ +The little red hen said to Mick, no thank you not today sir. +And then all the barnes animals shouted out in glee. +No more misery! +cows +and cats diff --git a/src-tranche/1_tests/test13.dat.expected b/src-tranche/1_tests/test13.dat.expected new file mode 100644 index 0000000..81fb20c --- /dev/null +++ b/src-tranche/1_tests/test13.dat.expected @@ -0,0 +1,2 @@ +apple banana pear +kiwi diff --git a/src-tranche/1_tests/test14.dat.expected b/src-tranche/1_tests/test14.dat.expected new file mode 100644 index 0000000..0d8b89b --- /dev/null +++ b/src-tranche/1_tests/test14.dat.expected @@ -0,0 +1,3 @@ +int float if while +do +function diff --git a/src-tranche/1_tests/test15.dat.expected b/src-tranche/1_tests/test15.dat.expected new file mode 100644 index 0000000..e69de29 diff --git a/src-tranche/1_tests/test1stdout.dat.expected b/src-tranche/1_tests/test1stdout.dat.expected new file mode 100644 index 0000000..4e519ff --- /dev/null +++ b/src-tranche/1_tests/test1stdout.dat.expected @@ -0,0 +1,5 @@ + + +the between space + + diff --git a/src-tranche/1_tests/tranche b/src-tranche/1_tests/tranche new file mode 120000 index 0000000..acf4a6f --- /dev/null +++ b/src-tranche/1_tests/tranche @@ -0,0 +1 @@ +../1_execs/tranche \ No newline at end of file diff --git a/src-tranche/tranche.cli.c b/src-tranche/tranche.cli.c index cbad345..8df4337 100644 --- a/src-tranche/tranche.cli.c +++ b/src-tranche/tranche.cli.c @@ -1,17 +1,25 @@ +#include +#include +#include +#include "tranche.lib.h" int main(int argc, char **argv, char **envp){ - if(argc != 2){ fprintf(stderr, "usage: %s \n",argv[0]); + return 1; } FILE *file = fopen(argv[1], "r"); - + if(!file){ + fprintf(stderr,"could not open file %s\n", argv[1]); + return 2; + } Da targets; da_alloc(&targets, sizeof(int)); - da_push(&da_targets, stdout); - tranche_send(file, &da_targets); - da_free(targets); + int fd = STDOUT_FILENO; + da_push(&targets, &fd); + tranche_send(file, &targets); + da_free(&targets); fclose(file); - + return 0; } diff --git a/src-tranche/tranche.lib.c b/src-tranche/tranche.lib.c index 07b0165..4679dbe 100644 --- a/src-tranche/tranche.lib.c +++ b/src-tranche/tranche.lib.c @@ -31,11 +31,14 @@ the source file. //-------------------------------------------------------------------------------- // parsing +char newline = '\n'; +char terminator = 0; + char tranche_begin_tag[] = "#tranche"; size_t tranche_begin_tag_len = 8; -char tranche_end_tag[] = "#endtranche"; -size_t tranche_end_tag_len = 11; +char tranche_end_tag[] = "#tranche-end"; +size_t tranche_end_tag_len = 12; // given a line // returns beginning of file name list @@ -56,13 +59,13 @@ static char *is_tranche_end(char *pt){ static bool parse_file_list(Da *file_names, char *pt0){ char *pt1; while( *pt0 && isspace(*pt0) ) pt0++; + pt1 = pt0; while( *pt0 ){ - pt1 = pt0; - while( *pt1 && !isspace(pt1) ) pt1++; + while( *pt1 && !isspace(*pt1) ) pt1++; char *file_name = strndup(pt0, pt1 - pt0); - da_push(file_names, file_name); + da_push(file_names, &file_name); + while( *pt1 && isspace(*pt1) ) pt1++; pt0 = pt1; - while( *pt0 && isspace(*pt0) ) pt0++; } } @@ -71,65 +74,70 @@ static bool parse_file_list(Da *file_names, char *pt0){ //-------------------------------------------------------------------------------- // da_map calls -static void tranche_open_fd(void *fn, void *closure){ - char *file_name = (char *)fn; - Da *fds = (Da *)closure; - int fd = open(fn, O_CREAT | O_APPEND); +static void tranche_open_fd(void *fnp, void *closure){ + char *file_name = *(char **)fnp; + Da *fdap = (Da *)closure; + int fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0666); if(fd == -1){ fprintf(stderr, "Could not open file %s\n", file_name); return; } - da_push(fds, &fd); + da_push(fdap, &fd); return; } -static void tranche_open_fds(Da *fns, Da *fds){ - da_map(fns, tranche_open_fd, fds); +static void tranche_open_fds(Da *fnap, Da *fdap){ + da_map(fnap, tranche_open_fd, fdap); } -static void tranche_close_fd(void *fd, void *closure){ - close(*(int *)fd); +static void tranche_close_fd(void *fdp, void *closure){ + close(*(int *)fdp); } -static void tranche_close_fds(Da *fds){ - da_map(fds, tranche_close_fd, NULL); +static void tranche_close_fds(Da *fdap){ + da_map(fdap, tranche_close_fd, NULL); + da_rewind(fdap); } -static void tranche_puts(void *fd, void *string){ - write(*(int *)fd, string, strlen(string)); +static void tranche_puts(void *fdp, void *string){ + write(*(int *)fdp, string, strlen(string)); } -static void tranche_puts_all(Da *fds, char *string){ - da_map(fds, tranche_puts, string); +static void tranche_puts_all(Da *fdap, char *string){ + da_map(fdap, tranche_puts, string); } -//-------------------------------------------------------------------------------- -// -int tranche_send(FILE *src, Da *arg_fds){ + +//-------------------------------------------------------------------------------- +// we have a little problem if the user tries to tranche two things to the same file .. +int tranche_send(FILE *src, Da *arg_fdap){ char *pt; Da line; - Da file_names; - Da fds; + Da file_name_arr; + Da fda; da_alloc(&line, sizeof(char)); - da_alloc(&file_names, sizeof(char *)); - da_alloc(&fds, sizeof(int)); - - da_fgets(&line, src); - while( !feof(src) && !is_tranche_end(line.base) ){ + da_alloc(&file_name_arr, sizeof(char *)); + da_alloc(&fda, sizeof(int)); + + while( !feof(src) ){ + da_fgets(&line, src); + if( is_tranche_end(line.base) ) break; pt = is_tranche_begin(line.base); if(pt){ // then this line is the start of a nested tranche block - parse_file_list(&file_names, pt); - tranche_open_fds(&file_names, &fds); - da_free_elements(&file_names); - tranche_send(src, &fds); - tranche_close_fds(&fds); + parse_file_list(&file_name_arr, pt); + tranche_open_fds(&file_name_arr, &fda); + da_free_elements(&file_name_arr); + tranche_send(src, &fda); + tranche_close_fds(&fda); }else{ - tranche_puts_all(arg_fds, line.base); - da_rewind(&line); - da_fgets(&line, src); + da_pop(&line, NULL); // pop the terminating zero + da_push(&line, &newline); + da_push(&line, &terminator); + tranche_puts_all(arg_fdap, line.base); } + da_rewind(&line); } - + da_free(&line); - da_free(&file_names); - da_free(&fds); + da_free(&file_name_arr); + da_free(&fda); return 0; } diff --git a/stage/include/da.h b/stage/include/da.h index d4e34de..6d3c43b 100644 --- a/stage/include/da.h +++ b/stage/include/da.h @@ -23,8 +23,10 @@ bool da_endq(Da *dap, void *pt); bool da_boundq(Da *dap); void da_push(Da *dap, void *element); bool da_pop(Da *dap, void *element); +char *da_index(Da *dap, size_t i); void da_map(Da *dap, void f(void *, void *), void *closure); void da_free_elements(Da *dap); +void da_strings_puts(Da *dap); char *da_fgets(Da *dap, FILE *fd); #endif diff --git a/stage/include/tranche.h b/stage/include/tranche.h new file mode 100644 index 0000000..27990a6 --- /dev/null +++ b/stage/include/tranche.h @@ -0,0 +1,8 @@ +#ifndef TRANCHE_LIB_H +#define TRANCHE_LIB_H + +int tranche_send(FILE *src, Da *arg_fds); + + + +#endif diff --git a/tools/lib/makefile_cc b/tools/lib/makefile_cc index 5cdfb39..6de582b 100755 --- a/tools/lib/makefile_cc +++ b/tools/lib/makefile_cc @@ -90,6 +90,7 @@ sub_execs: $(patsubst %, $(EXECSDIR)/%, $(EXECS)) stage: if [ -f $(LIBDIR)/$(LIBFILE) ]; then cp $(LIBDIR)/$(LIBFILE) $(PROJECT_SUBU)/stage/lib; fi if [ -f $(INCDIR)/$(INCFILE) ]; then cp $(INCDIR)/$(INCFILE) $(PROJECT_SUBU)/stage/include; fi + cp $(EXECSDIR)/* $(PROJECT_SUBU)/stage/bin clean: for i in $(wildcard *~); do mv $$i $(TMPDIR); done