From 28759402393354365d756b3e1aaf7c8206505303 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 11 Jul 2012 23:07:59 -0300 Subject: [PATCH] gencat/: move script execution from pdf.c to run.c --- gencat/Makefile | 2 +- gencat/pdf.c | 51 +++-------------------------------- gencat/run.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ gencat/run.h | 18 +++++++++++++ 4 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 gencat/run.c create mode 100644 gencat/run.h diff --git a/gencat/Makefile b/gencat/Makefile index d4efcc0..1f31e21 100644 --- a/gencat/Makefile +++ b/gencat/Makefile @@ -11,7 +11,7 @@ PREFIX ?= /usr/local -OBJS = gencat.o tree.o libs.o comp.o pdf.o +OBJS = gencat.o tree.o libs.o run.o comp.o pdf.o SHELL = /bin/bash CFLAGS = -Wall -g diff --git a/gencat/pdf.c b/gencat/pdf.c index e4da63a..ea79308 100644 --- a/gencat/pdf.c +++ b/gencat/pdf.c @@ -9,16 +9,15 @@ * (at your option) any later version. */ -#define _GNU_SOURCE #include #include #include -#include #include "util.h" #include "gencat.h" #include "tree.h" #include "libs.h" +#include "run.h" #include "pdf.h" @@ -279,39 +278,9 @@ static void print_comment(FILE *file, const struct line *comment) /* ----- Component conversion ---------------------------------------------- */ -static void cat(FILE *out, const char *name) -{ - FILE *in; - char buf[10000]; /* pick any good size */ - size_t got, wrote; - - in = fopen(name, "r"); - if (!in) { - perror(name); - exit(1); - } - while (1) { - got = fread(buf, 1, sizeof(buf), in); - if (!got) - break; - wrote = fwrite(buf, 1, got, out); - if (wrote != got) { - perror("fwrite"); - exit(1); - } - } - if (ferror(in)) { - perror(name); - exit(1); - } - fclose(in); -} - - static void convert_comp(const struct node *node, FILE *out) { - char *tmp; - int i, res; + int i; for (i = 0; i != node->e->units; i++) { if (!quiet) { @@ -321,21 +290,9 @@ static void convert_comp(const struct node *node, FILE *out) make_title(out, node, i); if (!i && node->comment) print_comment(out, node->comment); - if (asprintf(&tmp, "sym2xps '%s' '%s' %d '%s' '%s'", + run_cmd("sym2xps '%s' '%s' %d '%s' '%s'", node->e->file->path, node->e->names->s, i+1, - "tmp", "tmp.ps") < 0) { - perror("asprintf"); - exit(1); - } - res = system(tmp); - if (res < 0) { - perror("system"); - exit(1); - } - if (res) { - fprintf(stderr, "sym2xps returned %d\n", res); - exit(1); - } + "tmp", "tmp.ps"); fprintf(out, "gsave %s\n", format.comp_setup); cat(out, "tmp.ps"); fprintf(out, "\ngrestore\n"); diff --git a/gencat/run.c b/gencat/run.c new file mode 100644 index 0000000..ba9acc4 --- /dev/null +++ b/gencat/run.c @@ -0,0 +1,70 @@ +/* + * run.c - Run helper scripts + * + * Copyright 2012 by Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#define _GNU_SOURCE /* for vasprintf */ +#include +#include +#include +#include + +#include "run.h" + + +void run_cmd(const char *fmt, ...) +{ + va_list ap; + char *tmp; + int res; + + va_start(ap, fmt); + if (vasprintf(&tmp, fmt, ap) < 0) { + perror("vasprintf"); + exit(1); + } + res = system(tmp); + if (res < 0) { + perror("system"); + exit(1); + } + if (res) { + fprintf(stderr, "sym2xps returned %d\n", res); + exit(1); + } +} + + +void cat(FILE *out, const char *name) +{ + FILE *in; + char buf[10000]; /* pick any good size */ + size_t got, wrote; + + in = fopen(name, "r"); + if (!in) { + perror(name); + exit(1); + } + while (1) { + got = fread(buf, 1, sizeof(buf), in); + if (!got) + break; + wrote = fwrite(buf, 1, got, out); + if (wrote != got) { + perror("fwrite"); + exit(1); + } + } + if (ferror(in)) { + perror(name); + exit(1); + } + fclose(in); +} diff --git a/gencat/run.h b/gencat/run.h new file mode 100644 index 0000000..09e348b --- /dev/null +++ b/gencat/run.h @@ -0,0 +1,18 @@ +/* + * run.h - Run helper scripts + * + * Copyright 2012 by Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef RUN_H +#define RUN_H + +void run_cmd(const char *fmt, ...); +void cat(FILE *out, const char *name); + +#endif /* !RUN_H */