From: erg Date: Mon, 21 Feb 2011 23:20:51 +0000 (+0000) Subject: Fix code not to call exit if it can't open a file for writing. This is not X-Git-Tag: LAST_LIBGRAPH~32^2~1014 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d78967b116caa6ec4b4f15c415c0de19dcd16f87;p=graphviz Fix code not to call exit if it can't open a file for writing. This is not library-friendly. In general, all of the calls to exit() should be reviewed with an eye to this. I believe I have altered things correctly in gvRenderJobs so that cleanup is done correctly and finalizers are called as needed for good jobs. It would be good for John to review the changes. --- diff --git a/lib/common/emit.c b/lib/common/emit.c index e32399747..33a473db6 100644 --- a/lib/common/emit.c +++ b/lib/common/emit.c @@ -3580,8 +3580,9 @@ int gvRenderJobs (GVC_t * gvc, graph_t * g) job->output_file = prevjob->output_file; /* FIXME - this is dumb ! */ } else { + if (gvrender_begin_job(job)) + continue; gvc->active_jobs = job; /* first job of new list */ - gvrender_begin_job(job); } job->next_active = NULL; /* terminate active list */ job->callbacks = &gvdevice_callbacks; diff --git a/lib/gvc/gvcproc.h b/lib/gvc/gvcproc.h index ca4b8af23..53d6c63c4 100644 --- a/lib/gvc/gvcproc.h +++ b/lib/gvc/gvcproc.h @@ -65,7 +65,7 @@ extern usershape_t *gvusershape_find(char *name); /* device */ - extern void gvdevice_initialize(GVJ_t * job); + extern int gvdevice_initialize(GVJ_t * job); extern void gvdevice_format(GVJ_t * job); extern void gvdevice_finalize(GVJ_t * job); @@ -74,7 +74,7 @@ extern pointf gvrender_ptf(GVJ_t *job, pointf p); extern pointf* gvrender_ptf_A(GVJ_t *job, pointf *af, pointf *AF, int n); - extern void gvrender_begin_job(GVJ_t * job); + extern int gvrender_begin_job(GVJ_t * job); extern void gvrender_end_job(GVJ_t * job); extern int gvrender_select(GVJ_t * job, const char *lang); extern int gvrender_features(GVJ_t * job); diff --git a/lib/gvc/gvdevice.c b/lib/gvc/gvdevice.c index 9caab3458..0c910fd00 100644 --- a/lib/gvc/gvdevice.c +++ b/lib/gvc/gvdevice.c @@ -23,6 +23,9 @@ #include #include +#ifdef HAVE_ERRNO_H +#include +#endif #ifdef HAVE_UNISTD_H #include #endif @@ -120,7 +123,10 @@ static void auto_output_filename(GVJ_t *job) job->output_filename = buf; } -void gvdevice_initialize(GVJ_t * job) +/* gvdevice_initialize: + * Return 0 on success, non-zero on failure + */ +int gvdevice_initialize(GVJ_t * job) { gvdevice_engine_t *gvde = job->device.engine; GVC_t *gvc = job->gvc; @@ -137,8 +143,10 @@ void gvdevice_initialize(GVJ_t * job) if (job->output_filename) { job->output_file = fopen(job->output_filename, "w"); if (job->output_file == NULL) { - perror(job->output_filename); - exit(1); + (job->common->errorfn) ("Could not open \"%s\" for writing : %s\n", + job->output_filename, strerror(errno)); + /* perror(job->output_filename); */ + return(1); } } else @@ -171,14 +179,15 @@ void gvdevice_initialize(GVJ_t * job) if (deflateInit2(z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) { (job->common->errorfn) ("Error initializing for deflation\n"); - exit(1); + return(1); } gvwrite_no_z(job, z_file_header, sizeof(z_file_header)); #else (job->common->errorfn) ("No libz support.\n"); - exit(1); + return(1); #endif } + return 0; } size_t gvwrite (GVJ_t * job, const char *s, size_t len) diff --git a/lib/gvc/gvrender.c b/lib/gvc/gvrender.c index 24708193a..d585fa0b5 100644 --- a/lib/gvc/gvrender.c +++ b/lib/gvc/gvrender.c @@ -111,15 +111,20 @@ int gvrender_features(GVJ_t * job) return features; } -void gvrender_begin_job(GVJ_t * job) +/* gvrender_begin_job: + * Return 0 on success + */ +int gvrender_begin_job(GVJ_t * job) { gvrender_engine_t *gvre = job->render.engine; - gvdevice_initialize(job); + if (gvdevice_initialize(job)) + return 1; if (gvre) { if (gvre->begin_job) gvre->begin_job(job); } + return 0; } void gvrender_end_job(GVJ_t * job)