diff --git a/cameo/README b/cameo/README index 72ea427..0529e28 100644 --- a/cameo/README +++ b/cameo/README @@ -45,10 +45,13 @@ translation. File output: write [] + append [] Writes all loaded paths in gnuplot format to the specified file. If the file name is omitted, "write" writes to standard output. +"append" is like "write", except that it appends to an existing file. + File names can contain spaces and any printable characters, but no leading or trailing spaces. It is not possible to place a comment after a file name or at a place where a file name could be. diff --git a/cameo/gnuplot.c b/cameo/gnuplot.c index d02e8e0..4e582de 100644 --- a/cameo/gnuplot.c +++ b/cameo/gnuplot.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "path.h" #include "gnuplot.h" @@ -99,8 +100,13 @@ static int gnuplot_do_write(FILE *file, const struct path *paths) if (fprintf(file, "%f %f %f\n", p->x, p->y, p->z) < 0) return 0; } - if (file != stdout && fclose(file) < 0) - return 0; + if (file == stdout) { + if (fflush(file) == EOF) + return 0; + } else { + if (fclose(file) < 0) + return 0; + } return 1; } @@ -120,3 +126,30 @@ void gnuplot_write(const char *name, const struct path *paths) } } + +void gnuplot_append(const char *name, const struct path *paths) +{ + FILE *file; + struct stat st; + + if (!name) { + printf("\n"); + gnuplot_write(NULL, paths); + return; + } + file = fopen(name, "a"); + if (!file) { + perror(name); + exit(1); + } + if (stat(name, &st) < 0) { + perror("lstat"); + exit(1); + } + if (!S_ISREG(st.st_mode) || st.st_size) + fprintf(file, "\n"); + if (!gnuplot_do_write(file, paths)) { + perror(name); + exit(1); + } +} diff --git a/cameo/lang.l b/cameo/lang.l index 4e1bd72..9fb0de8 100644 --- a/cameo/lang.l +++ b/cameo/lang.l @@ -51,6 +51,8 @@ NUM -?[0-9]+\.?[0-9]* translate return TOK_TRANSLATE; z return TOK_Z; +append { BEGIN(FILENAME); + return TOK_APPEND; } gerber { file_name_follows = 1; return TOK_GERBER; } gnuplot { file_name_follows = 1; diff --git a/cameo/lang.y b/cameo/lang.y index 029a79e..c2a7483 100644 --- a/cameo/lang.y +++ b/cameo/lang.y @@ -152,7 +152,7 @@ static struct path **classify(struct path **anchor, struct path *path) %token TOK_ALIGN TOK_ARRAY TOK_CLEAR TOK_DRILL TOK_EMPTY %token TOK_MILL TOK_OFFSET TOK_REMAINDER TOK_RESET %token TOK_TRANSLATE TOK_Z -%token TOK_GERBER TOK_GNUPLOT TOK_EXCELLON TOK_WRITE +%token TOK_APPEND TOK_GERBER TOK_GNUPLOT TOK_EXCELLON TOK_WRITE %token TOK_DOG TOK_INSIDE %token NUM_EXP_MIL NUM_EXP_MM NUM_IMP_MIL NUM_IMP_MM REF @@ -241,6 +241,12 @@ command: gnuplot_write($2, paths); translate(0, 0, -zo); } + | TOK_APPEND opt_filename + { + translate(0, 0, zo); + gnuplot_append($2, paths); + translate(0, 0, -zo); + } | TOK_DRILL dimen dimen { struct path **walk;