/* Copyright (c) 2003-2005, Dirk Krause All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Dirk Krause nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "png2pdf.h" $(trace-include) static int isdir DK_P1(char *,fn) { int back = 0; dk_stat_t st; $? "+ isdir %s", TR_STR(fn) if(dkstat_get(&st, fn)) { if((dkstat_filetype(&st) & (~DK_FT_SYMLINK)) == DK_FT_DIR) { back = 1; } } $? "- isdir %d", back return back; } $* Check whether we have to run silently or as a filter. $* static void silence_check DK_P4(int,argc,char **,argv,int *,rs,int *,rf) { char *filename, *ptr, **lfdptr; int i; int fn; int isd; $? "+ DK_P4(int,argc,char" lfdptr = argv; lfdptr++; i = 1; fn = 0; isd = 0; filename = NULL; fn = 0; while(i < argc) { ptr = *lfdptr; if(*ptr == '-') { ptr++; switch(*ptr) { case 'p': case 'm': { ptr++; if(!(*ptr)) { lfdptr++; i++; } } break; } } else { fn++; filename = ptr; $? ". filename = %s", TR_STR(filename) } lfdptr++; i++; } if(fn < 2) { $? ". run as filter" if(fn == 1) { $? ". genau ein Dateiname" if(filename) { $? ". Dateiname da" if(dksf_must_expand_filename(filename)) { if(rf) { *rf = 1; } $? ". rf=1 (1)" } else { $? ". keine Expansion" if(!isdir(filename)) { $? ". kein Verzeichnis" if(rf) { *rf = 1; } $? ". rf=1 (2)" } } } else { if(rf) { *rf = 1; } $? ". rf=1 (3)" } } else { if(rf) { *rf = 1; } $? ". rf=1 (4)" } } $? "- silence_check" } static int run_for_filenames DK_P3(Png2PdfCmd *,c,char *,i,char *,o) { int back = 0; $? "+ run_for_filenames %s %s %s", TR_PTR(c), TR_STR(i), TR_STR(o) if(c->a) { $? ". have c->a" c->inf = dkapp_fopen(c->a, i, "rb"); if(c->inf) { $? ". input file opened" c->realfilename = dksf_get_last_filename(i); if(o) { $? ". have output name" c->of = dkapp_fopen(c->a, o, "wb"); if(c->of) { $? ". output file opened" back = png2pdf_for_files(c); fclose(c->of); c->of = NULL; } else { $? "! failed to open output" dkapp_err_fopenw(c->a, o); } } else { $? ". no output name" c->of = stdout; dksf_set_file_binary(stdout); back = png2pdf_for_files(c); c->of = NULL; } fclose(c->inf); c->inf = NULL; c->realfilename = NULL; } else { $? "! failed to open input" dkapp_err_fopenr(c->a, i); } } else { $? ". no c->a" c->inf = dksf_fopen(i, "rb"); if(c->inf) { $? ". input file opened" if(o) { $? ". have output name" c->of = dksf_fopen(o, "wb"); if(c->of) { $? ". output file opened" back = png2pdf_for_files(c); fclose(c->of); c->of = NULL; } else { $? "! failed to open output" } } else { $? ". no output name" c->of = stdout; dksf_set_file_binary(stdout); back = png2pdf_for_files(c); c->of = NULL; } fclose(c->inf); c->inf = NULL; } else { $? "! failed to open input" } } $? "- run_for_filenames %d", back return back; } static int source_newer DK_P3(Png2PdfCmd *,c,char *,src,char *,dst) { int back = 0; dk_stat_t st1, st2; char *c1, *c2; int ft; $? "+ source_newer %s %s %s", TR_PTR(c), TR_STR(src), TR_STR(dst) if(dkstat_get(&st1, src)) { $? ". stat ok for input" back = 1; if(dkstat_get(&st2, dst)) { $? ". stat ok for output" ft = dkstat_filetype(&st2); if((ft & (~DK_FT_SYMLINK)) == DK_FT_REG) { c1 = dkstat_mtime(&st1); c2 = dkstat_mtime(&st2); $? ". output is regular file" $? ". timestamps src=%s dst=%s", c1, c2 if(strcmp(c1,c2) > 0) { back = 1; } else { back = 0; } } else { $? "! destination not a regular file" back = 0; /* ##### ERROR: destination no regular file */ } } else { $? "! stat failed for output file" } } else { $? "! stat failed for input file" } $? "- source_newer %d", back return back; } static int is_png DK_P1(char *,n) { int back = 0; char *suffix; $? "+ is_png %s", TR_STR(n) suffix = dksf_get_file_type_dot(n); if(suffix) { $? ". suffix found" #if DK_HAVE_FNCASEINS if(dkstr_casecmp(suffix, ".png") == 0) { $? ". is png file (1)" back = 1; } #else if(strcmp(suffix, ".png") == 0) { $? ". is png file (2)" back = 1; } #endif } $? "- is_png %d", back return back; } static int run_for_directory DK_P1(Png2PdfCmd *,c) { int back = 0; int ft; char *ofbuffer, *in, *ins, *suffix; unsigned long mpl; dk_dir_t *dirptr; mpl = dksf_get_maxpathlen(); ofbuffer = dk_new(char,mpl); if(ofbuffer) { $? ". output name buffer ok" dirptr = dkdir_open(c->inputfilename); if(dirptr) { $? ". directory opened" back = 1; while(dkdir_next(dirptr)) { $? ". have dir entry" in = dkdir_get_fullname(dirptr); if(in) { $? ". dir entry name = \"%s\"", in ins = dkdir_get_shortname(dirptr); if(ins) { $? ". short name = \"%s\"", ins if(strcmp(ins, ".") && strcmp(ins, "..")) { ft = dkdir_filetype(dirptr); $? ". not . or .." if((ft & (~DK_FT_SYMLINK)) == DK_FT_REG) { $? ". regular file" if(is_png(in)) { $? ". PNG file" /* + 3 because in worst case we have to replace filename. by filename.pdf */ if((strlen(in) + 3) < mpl) { $? ". file name length ok" strcpy(ofbuffer, in); suffix = dksf_get_file_type_dot(ofbuffer); if(suffix) { $? ". suffix found" strcpy(suffix, ".pdf"); ft = 1; if((c->options) & PNG2PDF_OPT_MAKE) { $? ". timestamp check necessary" ft = source_newer(c, in, ofbuffer); } if(ft) { $? ". must run" if((c->a) && (c->msgptr)) { char *msgptr[3]; msgptr[0] = (c->msgptr)[12]; msgptr[1] = in; msgptr[2] = (c->msgptr)[13]; dkapp_log_msg(c->a, DK_LOG_LEVEL_PROGRESS, msgptr, 3); } if(!run_for_filenames(c, in, ofbuffer)) { back = 0; } } } else { $? ". no suffix in file name" back = 0; /* no suffix in file name, already tested in is_png() */ } } else { $? "! file name too long" /* File name too long, should not happen */ back = 0; if((c->a) && (c->msgptr)) { char *msgptr[3]; msgptr[0] = (c->msgptr)[25]; msgptr[1] = in; msgptr[2] = (c->msgptr)[26]; dkapp_log_msg(c->a, DK_LOG_LEVEL_ERROR, msgptr, 3); } } } } } } else { back = 0; /* no short filename for entry */ if((c->a) && (c->msgptr)) { dkapp_log_msg(c->a, DK_LOG_LEVEL_ERROR, &((c->msgptr)[24]), 1); } } } else { back = 0; /* no filename for entry */ if((c->a) && (c->msgptr)) { dkapp_log_msg(c->a, DK_LOG_LEVEL_ERROR, &((c->msgptr)[24]), 1); } } } dkdir_close(dirptr); } else { $? "! failed to open directory" if(c->a) { dkapp_err_traverse_dir(c->a, c->inputfilename); } } dk_delete(ofbuffer); } else { $? "! failed to allocate memory for output name buffer" if(c->a) { dkapp_err_memory(c->a, 1, mpl); } } return back; } static int run_for_output_name DK_P3(Png2PdfCmd *,c,char *,i,char *,o) { int back = 0; $? "+ run_for_output_name %s %s %s", TR_PTR(c), TR_STR(i), TR_STR(o) if(dksf_must_expand_filename(i)) { dk_fne_t *ifne; char *in; $? ". must expand filename" ifne = dkfne_open(i, 1, 0); if(ifne) { $? ". file name expander ok" if(c->a) { $? ". c->a available" in = dkapp_fne_one(c->a, ifne, i); if(in) { $? ". pattern ok" back = run_for_filenames(c, in, o); dk_delete(in); } else { $? "! no such file or multiple files" } } else { $? ". c->a not available" if(dkfne_next(ifne)) { $? ". next file name ok" in = dkfne_get_fullname(ifne); if(in) { back = run_for_filenames(c, in, o); } if(dkfne_next(ifne)) { $? "! multiple files" /* ERROR: Pattern not unique */ } } else { $? "! no such file name" /* ERROR: No such file name */ } } dkfne_close(ifne); } else { $? "! failed to create file name expander" if(c->a) { dkapp_err_memory(c->a, sizeof(dk_fne_t), 1); } } } else { $? ". not necessary to expand file name" back = run_for_filenames(c, i, o); } $? "- run_for_output_name %d", back return back; } static int run_normally DK_P1(Png2PdfCmd *,c) { int back = 0; $? "+ run_normally %s", TR_PTR(c) if(c->inputfilename) { $? ". have input filename" if(c->outputfilename) { $? ". have output file name" if(dksf_must_expand_filename(c->outputfilename)) { dk_fne_t *ofne; char *on; $? ". must expand output file name" ofne = dkfne_open(c->outputfilename, 1, 0); if(ofne) { $? ". expander created" if(c->a) { $? ". c->a ok" on = dkapp_fne_one(c->a, ofne, c->outputfilename); if(on) { $? ". file name found" back = run_for_output_name(c, c->inputfilename, on); dk_delete(on); } else { $? "! problem with file name pattern" } } else { $? ". no c->a" if(dkfne_next(ofne)) { on = dkfne_get_fullname(ofne); if(on) { back = run_for_output_name(c, c->inputfilename, on); } } else { $? "! no such file" /* ERROR: No such file */ } } dkfne_close(ofne); } else { $? "! failed to create output name expander" if(c->a) { dkapp_err_memory(c->a, sizeof(dk_fne_t), 1); } } } else { $? ". no need to expand output file name" back = run_for_output_name(c, c->inputfilename, c->outputfilename); } } else { $? ". no output file name specified" if(dksf_must_expand_filename(c->inputfilename)) { $? ". input name contains wildcards" back = run_for_output_name(c, c->inputfilename, NULL); } else { $? ". no wildcards in input name" if(isdir(c->inputfilename)) { $? ". input is directory" back = run_for_directory(c); } else { $? ". input is file" back = run_for_output_name(c, c->inputfilename, NULL); } } } } else { $? ". run for standard input / standard output" c->inf = stdin; c->of = stdout; dksf_set_file_binary(stdin); dksf_set_file_binary(stdout); back = png2pdf_for_files(c); c->inf = NULL; c->of = NULL; } $? "- run_normally %d", back return back; } #ifndef VERSNUMB #define VERSNUMB "developer" #endif static char versno[] = { VERSNUMB }; static char *help_text[] = { "png2pdf [ ] [ [ ] ]", "", "Options:", "-p PDF-level PDF-level: 1.2, 1.3 or 1.4", "-m r,g,b default background color (used if there is no background chunk)", "-s always use default background color from -m option", "-i create image mask", "-a write transparency data", "-b fill background before rendering image", "-f file time check when running on directory", "", "Permanent options handling:", "png2pdf -c configures permanent options", "png2pdf -u removes all permanent options", "png2pdf -C shows the permanent options", "png2pdf -r ... temporarily overrides permanent options", "", "Help and version information:", "png2pdf -h shows this help text", "png2pdf -v shows version information", "", NULL }; static char *license_terms[] = { "Redistribution and use in source and binary forms, with or without", "modification, are permitted provided that the following conditions are met:", "* Redistributions of source code must retain the above copyright notice, this", " list of conditions and the following disclaimer.", "* Redistributions in binary form must reproduce the above copyright notice,", " this list of conditions and the following disclaimer in the documentation", " and/or other materials provided with the distribution.", "* Neither the name of the Dirk Krause nor the names of other contributors may", " be used to endorse or promote products derived from this software without", " specific prior written permission.", "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"", "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE", "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE", "ARE DISCLAIMED.", "IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY", "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES", "(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;", "LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND", "ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT", "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS", "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", NULL }; static int run DK_P3(dk_app_t *,a,int,xargc,char **,xargv) { int back = 0; Png2PdfCmd c; $? "+ run %s %d %s", TR_PTR(a), xargc, TR_PTR(xargv) if(png2pdf_initialize_cmd(&c, a)) { back = 1; png2pdf_process_args(&c, xargc, xargv); if(c.error_code) { c.cmd |= PNG2PDF_CMD_HELP; back = 0; } if(c.cmd) { if(c.cmd & (PNG2PDF_CMD_HELP | PNG2PDF_CMD_VERSION)) { char **ptr; fputc('\n', stdout); fputs("png2pdf, version ", stdout); fputs(versno, stdout); fputc('\n', stdout); fputs("Copyright (C) 2003-2005 Dipl.-Ing. D. Krause\n", stdout); fputs("http://png2pdf.sourceforge.net\n", stdout); ptr = license_terms; while(*ptr) { fputs(*(ptr++), stdout); fputc('\n', stdout); } fputs("\nLibraries used:\n\n", stdout); fputs("libpng\tthe official PNG reference library\n", stdout); fputs("zlib\tgeneral compression library\n", stdout); fputs("dklibs\tportability layer and general purpose library\n", stdout); fputc('\n', stdout); if(c.cmd & PNG2PDF_CMD_HELP) { /* ##### print help */ dkapp_help(a, "png2pdf.txt", help_text); } } else { if(c.cmd & PNG2PDF_CMD_UNCONFIGURE) { png2pdf_reset_cmd(&c); dkapp_unconfigure(a); } if(c.cmd & PNG2PDF_CMD_CONFIGURE) { png2pdf_save_configuration(&c); } png2pdf_print_configuration(&c); } } else { $? ". run normally" back = run_normally(&c); } png2pdf_uninitialize_cmd(&c); } $? "- run %d", back return back; } #ifndef SYSCONFDIR #define SYSCONFDIR "/etc" #endif static char sysconfdir[] = { SYSCONFDIR }; static char packagename[] = { "png2pdf" }; #if DK_HAVE_PROTOTYPES int main(int argc, char *argv[]) #else int main(argc,argv) int argc; char *argv[]; #endif { int exval = 0, rs = 0, rf = 0; int xargc; char **xargv; dk_app_t *app; $(trace-init png2pdf.deb) $? "+ main" silence_check(argc,argv,&rs,&rf); $? ". rs=%d rf=%d", rs, rf if(!rs) { rs = dkapp_silence_check(argc,argv); } app = dkapp_open_ext1(argc, argv, packagename, sysconfdir, rs, rf); if(app) { xargc = dkapp_get_argc(app); xargv = dkapp_get_argv(app); exval = run(app, xargc, xargv); dkapp_close(app); app = NULL; } else { if(!rs) { fputs("ERROR: Not enough memory!\n", stderr); fflush(stderr); } } exval = (exval ? 0 : 1); $? "- main %d", exval $(trace-end) exit(exval); return exval; } #ifndef LINT static char sccs_id[] = { "@(#)png2pdfm.ctr 1.24 07/18/06 (krause)\tPNG to PDF converter" }; #endif