From 886a904459e87f46f3f43df434307a294f85e52d Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 28 Feb 2005 16:56:51 +0000 Subject: implement pdf export / import implement pdf export / import --- src/PSEditModel.cxx | 10 +++---- src/flpsed.cxx | 67 ++++++++++++++++++++++++++++++---------------- src/util.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 27 +++++++++++++++++++ 4 files changed, 151 insertions(+), 29 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h (limited to 'src') diff --git a/src/PSEditModel.cxx b/src/PSEditModel.cxx index 5eeee9c..8792414 100644 --- a/src/PSEditModel.cxx +++ b/src/PSEditModel.cxx @@ -1,5 +1,5 @@ // -// "$Id: PSEditModel.cxx,v 1.12 2004/11/10 18:32:59 hofmann Exp $" +// "$Id: PSEditModel.cxx,v 1.13 2005/02/28 17:56:51 hofmann Exp $" // // PSEditWidget routines. // @@ -108,8 +108,6 @@ int PSEditModel::set_cur_text(int x1, int y1, int p) { } int PSEditModel::next_text(int p) { - PSEditText *t; - if (p < 0 || p >= max_pages) { return 1; } @@ -239,12 +237,12 @@ PSEditText *PSEditModel::get_text(int p) { int PSEditModel::load(FILE *fp) { char tmpname[256]; char linebuf[1024]; - int ret; + ssize_t ret; PSParser *p1 = new PSParser_1(this); PSParser *p2 = new PSParser_2(this); int tmp_fd; - strncpy(tmpname, "/tmp/PSEditorXXXXXX", 256); + strncpy(tmpname, "/tmp/PSEditorXXXXXX", sizeof(tmpname)); tmp_fd = mkstemp(tmpname); if (tmp_fd < 0) { fprintf(stderr, "Could not create temporary file (errno %d).\n", errno); @@ -254,7 +252,7 @@ int PSEditModel::load(FILE *fp) { clear(); - while (fgets(linebuf, 1024, fp) != NULL) { + while (fgets(linebuf, sizeof(linebuf), fp) != NULL) { if (!p2->parse(linebuf) && !p1->parse(linebuf)) { ret = write(tmp_fd, linebuf, strlen(linebuf)); if (ret != strlen(linebuf)) { diff --git a/src/flpsed.cxx b/src/flpsed.cxx index c3e13e0..3c712c2 100644 --- a/src/flpsed.cxx +++ b/src/flpsed.cxx @@ -1,5 +1,5 @@ // -// "$Id: flpsed.cxx,v 1.30 2005/02/03 18:10:17 hofmann Exp $" +// "$Id: flpsed.cxx,v 1.31 2005/02/28 17:56:51 hofmann Exp $" // // flpsed program. // @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -41,6 +42,7 @@ #include #include "PSEditor.H" +#include "util.h" PSEditor *psed_p = NULL; Fl_Scroll *scroll = NULL; @@ -82,52 +84,72 @@ void open_cb() { } void import_pdf_cb() { + char *file; + FILE *p; + int status; + char *args[32]; + pid_t pid; + if (!check_save()) return; - char *file = fl_file_chooser("Open File?", "*.pdf", filename); + file = fl_file_chooser("Open File?", "*.pdf", filename); if(file != NULL) { - FILE *p; - int ret; - char cmd[1000]; + args[0] = "pdftops"; + args[1] = file; + args[2] = "-"; + args[3] = NULL; + + p = pexecvp("pdftops", args, &pid, "r"); - snprintf(cmd, sizeof(cmd), "pdftops \"%s\" -", file); - p = popen(cmd, "r"); if (p) { psed_p->load(p); - ret = pclose(p); - if (WEXITSTATUS(ret) == 127 || WEXITSTATUS(ret) == 126) { + fclose(p); + waitpid(pid, &status, 0); + if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126) { fl_message("PDF import depends on pdftops from xpdf.\n" "Make sure pdftops is available on your system.\n"); - } else if (WEXITSTATUS(ret) != 0) { + } else if (WEXITSTATUS(status) != 0) { fl_message("PDF import failed\n"); } } else { - perror("popen"); + perror("pexecvp"); } } } void export_pdf_cb() { - if (!check_save()) return; - char *file = fl_file_chooser("Open File?", "*.pdf", filename); + char *file; + FILE *p; + int status; + char *args[32]; + pid_t pid; + + file = fl_file_chooser("Open File?", "*.pdf", filename); if(file != NULL) { - FILE *p; - int ret; - char cmd[1000]; + args[0] = "ps2pdf"; + args[1] = "-"; + args[2] = file; + args[3] = NULL; + + signal(SIGPIPE, SIG_IGN); - snprintf(cmd, sizeof(cmd), "ps2pdf - \"%s\"", file); - p = popen(cmd, "w"); + p = pexecvp("ps2pdf", args, &pid, "w"); + if (p) { psed_p->save(p); - ret = pclose(p); - if (WEXITSTATUS(ret) == 127 || WEXITSTATUS(ret) == 126) { + + fclose(p); + waitpid(pid, &status, 0); + if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126) { fl_message("PDF export depends on ps2pdf from ghostscript.\n" "Make sure ps2pdf is available on your system.\n"); - } else if (WEXITSTATUS(ret) != 0) { + } else if (WEXITSTATUS(status) != 0) { fl_message("PDF export failed\n"); } } else { - perror("pclose"); + perror("pexecvp"); } + + signal(SIGPIPE, SIG_DFL); } } @@ -297,7 +319,6 @@ int main(int argc, char** argv) { int err, bflag = 0, dflag = 0; Fl_Window *win; Fl_Menu_Bar *m; - Fl_Int_Input *x_in, *y_in; struct {char *tag; char *value;} tv[TV_LEN]; int tv_idx = 0, my_argc; FILE *in_fp = NULL, *out_fp = NULL; diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..f33c1b4 --- /dev/null +++ b/src/util.c @@ -0,0 +1,76 @@ +/* + * "$Id: util.c,v 1.1 2005/02/28 17:56:51 hofmann Exp $" + * + * flpsed program. + * + * Copyright 2005 by Johannes Hofmann + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#include +#include +#include + +#include "util.h" + +FILE * +pexecvp(const char *file, char *const argv[], pid_t *pid, char *type) { + FILE *iop; + int pdes[2]; + + if (pipe(pdes) < 0) { + return NULL; + } + + *pid = vfork(); + + if (*pid == -1) { + perror("vfork"); + close(pdes[0]); + close(pdes[1]); + return NULL; + } else if (*pid == 0) { + /* child */ + + if (*type == 'r') { + close(pdes[0]); + if (pdes[1] != STDOUT_FILENO) { + dup2(pdes[1], STDOUT_FILENO); + close(pdes[1]); + } + } else { + close(pdes[1]); + if (pdes[0] != STDIN_FILENO) { + dup2(pdes[0], STDIN_FILENO); + close(pdes[0]); + } + } + + execvp(file, argv); + exit(127); + } else { + /* parent */ + if (*type == 'r') { + iop = fdopen(pdes[0], "r"); + close(pdes[1]); + } else { + iop = fdopen(pdes[1], "w"); + close(pdes[0]); + } + return iop; + } +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..ebe0e49 --- /dev/null +++ b/src/util.h @@ -0,0 +1,27 @@ +/* + * "$Id: util.h,v 1.1 2005/02/28 17:56:51 hofmann Exp $" + * + * flpsed program. + * + * Copyright 2005 by Johannes Hofmann + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#include + +FILE * +pexecvp(const char *file, char *const argv[], pid_t *pid, char *type); -- cgit v1.2.3