summaryrefslogtreecommitdiff
path: root/obsolete/support/png2pdf/png2pdfm.ctr
blob: 3bc8d934a6a6502e9099b6c6cc6216cca2cde933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
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 <dk.h>
#include <dksf.h>

#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> ]    [ <inputfile> [ <outputfile> ] ]",
"",
"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 <options>       configures permanent options",
"png2pdf -u                 removes all permanent options",
"png2pdf -C                 shows the permanent options",
"png2pdf -r <options> ...   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