tranche-make looks to be working
authorThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Fri, 29 Mar 2019 22:54:43 +0000 (23:54 +0100)
committerThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Fri, 29 Mar 2019 22:54:43 +0000 (23:54 +0100)
12 files changed:
module/da/src/da.lib.c
module/da/src/da.lib.h
module/share/include/da.h
module/share/lib/libda.a
module/tranche/src/tranche-make.cli.c
module/tranche/src/tranche.cli.c
module/tranche/src/tranche.lib.c
module/tranche/test/try/test2stdout.dat [new file with mode: 0644]
module/tranche/test/try/tranche-make [new symlink]
tool/bin/tranche
tool/bin/tranche-make [new file with mode: 0755]
tool/bin/tranche-target [new file with mode: 0755]

index 7fa384a..f09412d 100644 (file)
@@ -33,7 +33,7 @@ bool da_emptyq(Da *dap){
 }
 
 size_t da_length(Da *dap){
-  return dap->end - dap->base;
+  return (dap->end - dap->base)/dap->element_size;
 }
 
 
index 189ee50..3188011 100644 (file)
@@ -26,6 +26,7 @@ bool da_boundq(Da *dap);
 void da_push(Da *dap, void *element);
 bool da_pop(Da *dap, void *element);
 void da_cat(Da *dap_base, Da *dap_cat);
+void da_push_string(Da *dap0, char *string);
 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);
index 189ee50..3188011 100644 (file)
@@ -26,6 +26,7 @@ bool da_boundq(Da *dap);
 void da_push(Da *dap, void *element);
 bool da_pop(Da *dap, void *element);
 void da_cat(Da *dap_base, Da *dap_cat);
+void da_push_string(Da *dap0, char *string);
 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);
index 49ece65..6809926 100644 (file)
Binary files a/module/share/lib/libda.a and b/module/share/lib/libda.a differ
index 5286e3c..6fe480b 100644 (file)
@@ -1,7 +1,11 @@
 /*
 
+.. don't think I need sdir, just pass src_file_path through as the source,
+need to add -tdir option to the tranche call, an mod tranche to take it.
+sname then becomes also becomes a path to src. 
+
 usage: 
-   argv[0] [<source_file_path>] [-sname <sname>] [-sdir <dir>] [-tdir <dir>] [-mfile <mfile_path>]
+   argv[0] [<src_file_path>] [-sname <sname>] [-sdir <dir>] [-tdir <dir>] [-mfile <mfile_path>]
 
 gets the names of all the targets from the source file, then appends
 to the mfile a couple of lines of the form:
@@ -13,27 +17,33 @@ Our makefile is typically run in the directory above where the sources are
 located.
 
 options
-<source_file_path>  the trc file to be read
+<src_file_path>  the trc file to be read
 -sdir <sdir>   prepend <sdir> to <src> in the makefile deps line that is printed
 -tdir <tdir>   prepend <tdir> to each <target> "
 -mfile <mfile_path> where to send the output, defaults to stdout
 -sname <sname> replaces sourcename as the name to write as the source - useful for pipes
 
-If <source_file_path> is not provided stdin is used.
+If <src_file_path> is not provided stdin is used.
 If -mfile is not provided, stdout is used. 
-If -sdir is not provided, the directory part of the <source_file_path> is used. If the
+If -sdir is not provided, the directory part of the <src_file_path> is used. If the
 user does not want this behavior, give a value of "." for -sdir.
 
+.. should modify this to allow multiple source files on the command line ..
+
 */
   
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <string.h>
 #include <da.h>
 #include "tranche.lib.h"
 
 int main(int argc, char **argv, char **envp){
 
-  char *source_file_path = 0;
+  char *src_file_path = 0;
   char *sname = 0;
   char *sdir = 0;  
   char *tdir = 0;  
@@ -43,7 +53,6 @@ int main(int argc, char **argv, char **envp){
     Da args; // we will queue the non option args here
     Da *argsp = &args;
     da_alloc(argsp, sizeof(char *));
-    int args_cnt = 0;
 
     int err_cnt = 0;
     char **pt = argv;
@@ -55,54 +64,57 @@ int main(int argc, char **argv, char **envp){
         if(!*option){
           fprintf(stderr, "Currently there is no lone '-' option.\n");
           err_cnt++;
-          continue;
+          goto endif;
         }
         pt++; if(!*pt || **pt == '-'){
           fprintf(stderr, "Missing value for option %s.\n", option);
           err_cnt++;
-          if(!*pt) break;
-          continue;
+          if(!*pt) break; // then nothing more to parse
+          goto endif;
         }
         value = *pt;
         if( !strcmp(option, "mfile") ){
           mfile_path = value;
-          continue;
+          goto endif;
         }
         if( !strcmp(option, "sdir") ){
           sdir = value;
-          continue;
+          goto endif;
         }
         if( !strcmp(option, "tdir") ){
           tdir = value;
-          continue;
+          goto endif;
         }
         if( !strcmp(option, "sname") ){
           sname = value;
-          continue;
+          goto endif;
         }
         fprintf(stderr, "Unrecognized option %s.", option);
         err_cnt++;
-        continue;
+        goto endif;
       }
+      // else clause
       da_push(argsp, pt);
-      arg_cnt++;
-      pt++;
+    endif:
+    pt++;
     }
-    if(!da_emptyq(argsp)) src_file_path = da_index(args, 0);
+    int args_cnt = da_length(argsp);
+    if(args_cnt > 1) src_file_path = *(char **)da_index(argsp, 1);
 
     // arg contracts
-    if(da_length(argsp) > 1){
-      fprintf(stderr, "too many args/n");
+    if(da_length(argsp) > 2){
+      fprintf(stderr, "too many args\n");
       err_cnt++;
     }
-    if(!src_file_name && !sname){
-      fprintf(stderr, "must specify at least one eof a source_file_path or an sname/n");
+    if(!src_file_path && !sname){
+      fprintf(stderr, "must specify at least one eof a src_file_path or an sname\n");
       err_cnt++;
     }
     if(err_cnt > 0){
-      fprintf(stderr, "usage: %s [<source_file_path>] [-sname <sname>] [-sdir <dir>] [-tdir <dir>] [-mfile <mfile_path>]\n", argv[0]);
+      fprintf(stderr, "usage: %s [<src_file_path>] [-sname <sname>] [-sdir <dir>] [-tdir <dir>] [-mfile <mfile_path>]\n", argv[0]);
       return TRANCHE_ERR_ARG_PARSE;
     }
+    da_free(argsp); // this only frees the array itself, not the things it points to
 
   }// end of argument parse
  
@@ -114,19 +126,19 @@ int main(int argc, char **argv, char **envp){
     else
       src_file = fopen(src_file_path, "r");
 
-    if(mfile_path == "")
-      mfile_fd = STDOUT_FILENO;
+    if(mfile_path)
+      mfile_fd = open(mfile_path, O_WRONLY | O_APPEND | O_CREAT, 0666);
     else
-      mfile_fd = open(file_name, O_WRONLY | O_APPEND | O_CREAT, 0666);
+      mfile_fd = STDOUT_FILENO;
 
     int err = 0;
     if(!src_file){
       fprintf(stderr,"could not open tranche source file %s\n", src_file_path);
-      err+= ERR_SRC_OPEN;
+      err+= TRANCHE_ERR_SRC_OPEN;
     }
     if(mfile_fd == -1){
       fprintf(stderr, "Could not open the dep file %s\n", mfile_path);
-      err+= ERR_DST_OPEN;
+      err+= TRANCHE_ERR_DST_OPEN;
     }
     if(err) return err;
   }
@@ -141,14 +153,13 @@ int main(int argc, char **argv, char **envp){
   tranche_make(src_file, sname,  mfile_fd, sdir, tdir);
 
   { // deallocate resources instead of just existing, so as to catch any errors
-    da_free(argsp);
     int err_cnt = 0;
     if(src_file != stdin)
-      if( !fclose(src_file) ){perror(); err_cnt++;}
+      if( fclose(src_file) ){perror(NULL); err_cnt++;}
     if(mfile_fd != STDOUT_FILENO)
-      if( close(mfile_fd) == -1 ){perror(); err_cnt++;}
+      if( close(mfile_fd) == -1 ){perror(NULL); err_cnt++;}
     if( err_cnt )
-      return TRANCHE_ERR_CLOSE;
+      return TRANCHE_ERR_FCLOSE;
     else
       return 0;
   }
index 97fe653..1e6ee60 100644 (file)
@@ -28,7 +28,7 @@ the same file as one already open in the containing tranche ..
 int main(int argc, char **argv, char **envp){
   if(argc != 2){
     fprintf(stderr, "usage: %s <source-file>\n",argv[0]);
-    return TRANCHE_ERR_ARGC;
+    return TRANCHE_ERR_ARG_PARSE;
   }
   FILE *file = fopen(argv[1], "r");
   if(!file){
index 455c6d7..b27aaa6 100644 (file)
@@ -190,7 +190,7 @@ int tranche_target(FILE *src, Da *target_arrp){
 // Inserts a zero to chop off the filename similar to the old basename.
 // Returns a pointer to the first character after the inserted zero, i.e. to the filename.
 char *path_chop(char *path){
-  file = path + strlen(path);
+  char *file = path + strlen(path);
   if(file == path) return file;
   file--;
   if(file == path){
@@ -216,10 +216,10 @@ char *path_chop(char *path){
 void tranche_make(FILE *src_file, char *src_name, int mfile_fd, char *sdir, char *tdir){
 
   // target list
-  Da tarr;
-  Da *tarrp; // target array pointer
-  da_alloc(tarrp, sizeof(char *));
-  tranche_target(src_file, tarrp);
+  Da ta;
+  Da *tap=&ta; // target array pointer
+  da_alloc(tap, sizeof(char *));
+  tranche_target(src_file, tap);
 
   char sp = ' ';
   char colon = ':';
@@ -229,41 +229,39 @@ void tranche_make(FILE *src_file, char *src_name, int mfile_fd, char *sdir, char
   char terminator = 0;
 
   // output the dependency line ----------------------------------------
-  Da dlarr;
-  Da *dlarrp; // dependency line array pointer
-  da_alloc(dlarrp, sizeof(char));
-  char *pt = tarrp->base; // char * because it points to a byte in the array
-  while( pt < tarrp->end ){
+  Da dla;
+  Da *dlap=&dla; // dependency line array pointer
+  da_alloc(dlap, sizeof(char));
+  char *pt = tap->base; // char * because it points to a byte in the array
+  while( pt < tap->end ){
     if(tdir){
-      da_push_string(dlarrp, tdir);
-      da_push(dlarrp, &slash);
+      da_push_string(dlap, tdir);
+      da_push(dlap, &slash);
     }
-    da_push_string(dlarrp, *(char **)pt);
-    da_push(dlarrp, &sp);
-  pt += dap->element_size;
+    da_push_string(dlap, *(char **)pt);
+    da_push(dlap, &sp);
+  pt += tap->element_size;
   }
-  da_push(dlarrp, &colon);
-  da_push(dlarrp, &sp);
+  da_push(dlap, &colon);
+  da_push(dlap, &sp);
   if(sdir){
-    da_push_string(dlarrp, sdir);
-    da_push(dlarrp, &slash);
+    da_push_string(dlap, sdir);
+    da_push(dlap, &slash);
   }
-  da_push_string(dlarrp, src_name);
-  da_push(dlarrp, &newline);
-  da_push(dlarrp, &terminator);
-  write(mfile_fd, dlarrp->base, dlarrp->end - dlarrp->base);
-  da_free_elements(tarrp);
-  da_free(tarrp);
+  da_push_string(dlap, src_name);
+  da_push(dlap, &newline);
+  write(mfile_fd, dlap->base, dlap->end - dlap->base);
+  da_free_elements(tap);
+  da_free(tap);
   
   // output acction line ----------------------------------------
-  da_rewind(dlarrp); // reuse the line buffer
-  da_push(dlarrp, &tab);
-  da_push_string(dlarrp, "tranche $<");
-  da_push(dlarrp, &newline);
-  da_push(dlarrp, &newline);
-  da_push(dlarrp, &terminator);
-  write(mfile_fd, dlarrp->base, dlarrp->end - dlarrp->base);
-  da_free(dlarrp);
+  da_rewind(dlap); // reuse the line buffer
+  da_push(dlap, &tab);
+  da_push_string(dlap, "tranche $<");
+  da_push(dlap, &newline);
+  da_push(dlap, &newline);
+  write(mfile_fd, dlap->base, dlap->end - dlap->base);
+  da_free(dlap);
 
   return;
 }
diff --git a/module/tranche/test/try/test2stdout.dat b/module/tranche/test/try/test2stdout.dat
new file mode 100644 (file)
index 0000000..139597f
--- /dev/null
@@ -0,0 +1,2 @@
+
+
diff --git a/module/tranche/test/try/tranche-make b/module/tranche/test/try/tranche-make
new file mode 120000 (symlink)
index 0000000..b3adcec
--- /dev/null
@@ -0,0 +1 @@
+../../exec/tranche-make
\ No newline at end of file
index eb63c28..b105ce0 100755 (executable)
Binary files a/tool/bin/tranche and b/tool/bin/tranche differ
diff --git a/tool/bin/tranche-make b/tool/bin/tranche-make
new file mode 100755 (executable)
index 0000000..3330f46
Binary files /dev/null and b/tool/bin/tranche-make differ
diff --git a/tool/bin/tranche-target b/tool/bin/tranche-target
new file mode 100755 (executable)
index 0000000..e9fd5ff
Binary files /dev/null and b/tool/bin/tranche-target differ