}
size_t da_length(Da *dap){
- return dap->end - dap->base;
+ return (dap->end - dap->base)/dap->element_size;
}
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);
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);
/*
+.. 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:
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;
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;
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
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;
}
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;
}
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){
// 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){
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 = ':';
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;
}
--- /dev/null
+../../exec/tranche-make
\ No newline at end of file