From 34eacf87403f8a3382beab0271a28764b030c333 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Fri, 10 Feb 2006 17:16:24 +0100 Subject: make PostscriptDSC stuff more robust --- src/GsWidget.H | 2 ++ src/GsWidget.cxx | 8 ++++++- src/PostscriptDSC.cxx | 59 ++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 51 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/GsWidget.H b/src/GsWidget.H index cc4e9fd..9fb6372 100644 --- a/src/GsWidget.H +++ b/src/GsWidget.H @@ -37,6 +37,7 @@ #include "PostscriptDSC.H" class GsWidget : public Fl_Widget { +private: PostscriptDSC *dsc; int gs_win; Atom atoms[5]; @@ -45,6 +46,7 @@ class GsWidget : public Fl_Widget { int in_fd, reload_needed; int zoom_percent; int initial_width, initial_height; + int feeding; protected: int page; diff --git a/src/GsWidget.cxx b/src/GsWidget.cxx index 3d3a081..27a8cb7 100644 --- a/src/GsWidget.cxx +++ b/src/GsWidget.cxx @@ -115,6 +115,7 @@ GsWidget::GsWidget(int X,int Y,int W, int H) : Fl_Widget(X, Y, W, H) { in_fd = -1; reload_needed = 0; dsc = NULL; + feeding = 0; } GsWidget::~GsWidget() { @@ -204,7 +205,7 @@ GsWidget::load_page(int p) { pid_t pid; int pdes[2]; - if (in_fd < 0) { + if (feeding || in_fd < 0) { return 1; } @@ -225,6 +226,8 @@ GsWidget::load_page(int p) { perror("pipe"); return 1; } + + feeding = 1; lseek(in_fd, 0L, SEEK_SET); setProps(); @@ -247,6 +250,7 @@ GsWidget::load_page(int p) { len = dsc->get_setup_len(); if (fd_copy(pdes[1], in_fd, len) != 0) { close(pdes[1]); + feeding = 0; return 1; } @@ -254,12 +258,14 @@ GsWidget::load_page(int p) { len = dsc->get_page_len(p); if (fd_copy(pdes[1], in_fd, len) != 0) { close(pdes[1]); + feeding = 0; return 1; } close(pdes[1]); } + feeding = 0; return 0; } diff --git a/src/PostscriptDSC.cxx b/src/PostscriptDSC.cxx index 40b39c9..dfc9775 100644 --- a/src/PostscriptDSC.cxx +++ b/src/PostscriptDSC.cxx @@ -36,6 +36,12 @@ PostscriptDSC::PostscriptDSC() { } PostscriptDSC::~PostscriptDSC() { + if (page_off) { + free(page_off); + } + if (page_len) { + free(page_len); + } } int @@ -52,8 +58,15 @@ PostscriptDSC::parse(int fd) { bb_h = 841; // setup_len = 0; pages = 0; - page_off = NULL; - page_len = NULL; + + if (page_off) { + free(page_off); + page_off = NULL; + } + if(page_len) { + free(page_len); + page_len = NULL; + } fp = fdopen(fd, "r"); if (!fp) { @@ -72,38 +85,50 @@ PostscriptDSC::parse(int fd) { } else if (sscanf(linebuf, "%%%%Pages: %d", &ps) == 1) { if (pages != 0) { fprintf(stderr, "Multiple Pages tags found\n"); - free(page_off); - free(page_len); return 1; } pages = ps; - page_off = (size_t*) malloc(sizeof(size_t) * (pages + 1)); - memset(page_off, 0, sizeof(size_t) * (pages + 1)); - page_len = (size_t*) malloc(sizeof(size_t) * (pages + 1)); - memset(page_len, 0, sizeof(size_t) * (pages + 1)); + page_off = (size_t*) malloc(sizeof(size_t) * pages); + memset(page_off, 0, sizeof(size_t) * pages); + page_len = (size_t*) malloc(sizeof(size_t) * pages); + memset(page_len, 0, sizeof(size_t) * pages); + } else if (sscanf(linebuf, "%%%%Page: %d %d", &p1, &p2) == 2) { + + if (!page_off || !page_len) { + fprintf(stderr, "Number of pages not defined\n"); + return 1; + } + if (p1 > pages || p1 < 1) { - fprintf(stderr, "Page %d out of range (0 - %d)\n", p1, pages); + fprintf(stderr, "Page %d out of range (1 - %d)\n", p1, pages); return 1; } - if (page_off[p1] != NULL) { + if (page_off[p1 - 1] != 0) { fprintf(stderr, "Page %d already defined\n", p1); return 1; } - if (p1 > 1 && page_off[p1 - 1] == NULL) { + if (p1 > 1 && page_off[p1 - 2] == NULL) { fprintf(stderr, "Page %d not yet defined\n", p1 - 1); return 1; } - page_off[p1] = ftello(fp); + page_off[p1 - 1] = ftello(fp); if (p1 > 1) { - page_len[p1 - 1] = page_off[p1] - page_off[p1 - 1]; + page_len[p1 - 2] = page_off[p1 - 1] - page_off[p1 - 2]; } } } - page_len[p1] = ftello(fp) - page_off[p1]; + page_len[p1 - 1] = ftello(fp) - page_off[p1 - 1]; + + for (int i=0; i