tumble.c

Thu, 10 Apr 2003 09:02:12 +0000

author
eric
date
Thu, 10 Apr 2003 09:02:12 +0000
changeset 158
e5452e27f518
parent 157
160d624271cc
child 160
1f793b71ffff
permissions
-rw-r--r--

include tumble version in usage message.

eric@10 1 /*
eric@125 2 * tumble: build a PDF file from image files
eric@29 3 *
eric@10 4 * Main program
eric@158 5 * $Id: tumble.c,v 1.43 2003/04/10 01:02:12 eric Exp $
eric@49 6 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
eric@10 7 *
eric@10 8 * This program is free software; you can redistribute it and/or modify
eric@10 9 * it under the terms of the GNU General Public License version 2 as
eric@10 10 * published by the Free Software Foundation. Note that permission is
eric@10 11 * not granted to redistribute this program under the terms of any
eric@10 12 * other version of the General Public License.
eric@10 13 *
eric@10 14 * This program is distributed in the hope that it will be useful,
eric@10 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eric@10 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eric@10 17 * GNU General Public License for more details.
eric@10 18 *
eric@10 19 * You should have received a copy of the GNU General Public License
eric@10 20 * along with this program; if not, write to the Free Software
eric@62 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
eric@62 22 */
eric@10 23
eric@10 24
eric@49 25 #include <stdarg.h>
eric@48 26 #include <stdbool.h>
eric@48 27 #include <stdint.h>
eric@10 28 #include <stdio.h>
eric@28 29 #include <stdlib.h>
eric@62 30 #include <string.h>
eric@28 31 #include <unistd.h>
eric@47 32
eric@47 33
eric@18 34 #include "semantics.h"
eric@10 35 #include "parser.tab.h"
eric@125 36 #include "tumble.h"
eric@141 37 #include "bitblt.h"
eric@62 38 #include "pdf.h"
eric@141 39 #include "tumble_input.h"
eric@10 40
eric@10 41
eric@49 42 #define MAX_INPUT_FILES 5000
eric@49 43
eric@26 44 typedef struct output_file_t
eric@26 45 {
eric@26 46 struct output_file_t *next;
eric@26 47 char *name;
eric@62 48 pdf_file_handle pdf;
eric@26 49 } output_file_t;
eric@26 50
eric@26 51
eric@49 52 int verbose;
eric@49 53
eric@49 54
eric@26 55 output_file_t *output_files;
eric@26 56 output_file_t *out;
eric@10 57
eric@10 58
eric@49 59 char *progname;
eric@49 60
eric@49 61
eric@49 62 bool close_pdf_output_files (void);
eric@49 63
eric@49 64
eric@158 65 #define QMAKESTR(x) #x
eric@158 66 #define MAKESTR(x) QMAKESTR(x)
eric@158 67
eric@158 68
eric@49 69 void usage (void)
eric@49 70 {
eric@50 71 fprintf (stderr, "\n");
eric@158 72 fprintf (stderr, "tumble version " MAKESTR(TUMBLE_VERSION) " - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n");
eric@127 73 fprintf (stderr, "http://tumble.brouhaha.com/\n");
eric@50 74 fprintf (stderr, "\n");
eric@49 75 fprintf (stderr, "usage:\n");
eric@134 76 fprintf (stderr, " %s [options] -c <control.tum>\n", progname);
eric@49 77 fprintf (stderr, " %s [options] <input.tif>... -o <output.pdf>\n", progname);
eric@49 78 fprintf (stderr, "options:\n");
eric@134 79 fprintf (stderr, " -v verbose\n");
eric@134 80 fprintf (stderr, " -b <fmt> create bookmarks\n");
eric@62 81 fprintf (stderr, "bookmark format:\n");
eric@74 82 fprintf (stderr, " %%F file name (sans suffix)\n");
eric@62 83 fprintf (stderr, " %%p page number\n");
eric@49 84 }
eric@49 85
eric@49 86
eric@49 87 /* generate fatal error message to stderr, doesn't return */
eric@139 88 void fatal (int ret, char *format, ...) __attribute__ ((noreturn));
eric@139 89
eric@49 90 void fatal (int ret, char *format, ...)
eric@49 91 {
eric@49 92 va_list ap;
eric@49 93
eric@49 94 fprintf (stderr, "fatal error");
eric@49 95 if (format)
eric@49 96 {
eric@49 97 fprintf (stderr, ": ");
eric@49 98 va_start (ap, format);
eric@49 99 vfprintf (stderr, format, ap);
eric@49 100 va_end (ap);
eric@49 101 }
eric@49 102 else
eric@49 103 fprintf (stderr, "\n");
eric@49 104 if (ret == 1)
eric@49 105 usage ();
eric@139 106 close_input_file ();
eric@49 107 close_pdf_output_files ();
eric@49 108 exit (ret);
eric@49 109 }
eric@49 110
eric@49 111
eric@48 112 bool close_pdf_output_files (void)
eric@10 113 {
eric@26 114 output_file_t *o, *n;
eric@26 115
eric@26 116 for (o = output_files; o; o = n)
eric@26 117 {
eric@26 118 n = o->next;
eric@133 119 pdf_close (o->pdf, PDF_PAGE_MODE_USE_OUTLINES);
eric@26 120 free (o->name);
eric@26 121 free (o);
eric@26 122 }
eric@10 123 out = NULL;
eric@26 124 output_files = NULL;
eric@10 125 return (1);
eric@10 126 }
eric@10 127
eric@48 128 bool open_pdf_output_file (char *name,
eric@48 129 pdf_file_attributes_t *attributes)
eric@10 130 {
eric@26 131 output_file_t *o;
eric@26 132
eric@26 133 if (out && (strcmp (name, out->name) == 0))
eric@26 134 return (1);
eric@26 135 for (o = output_files; o; o = o->next)
eric@26 136 if (strcmp (name, o->name) == 0)
eric@26 137 {
eric@26 138 out = o;
eric@26 139 return (1);
eric@26 140 }
eric@26 141 o = calloc (1, sizeof (output_file_t));
eric@29 142 if (! o)
eric@10 143 {
eric@26 144 fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
eric@26 145 return (0);
eric@26 146 }
eric@26 147
eric@26 148 o->name = strdup (name);
eric@26 149 if (! o->name)
eric@26 150 {
eric@26 151 fprintf (stderr, "can't strdup output filename '%s'\n", name);
eric@26 152 free (o);
eric@10 153 return (0);
eric@10 154 }
eric@26 155
eric@133 156 o->pdf = pdf_create (name);
eric@26 157 if (! o->pdf)
eric@26 158 {
eric@26 159 fprintf (stderr, "can't open output file '%s'\n", name);
eric@26 160 free (o->name);
eric@26 161 free (o);
eric@26 162 return (0);
eric@26 163 }
eric@26 164
eric@30 165 if (attributes->author)
eric@62 166 pdf_set_author (o->pdf, attributes->author);
eric@30 167 if (attributes->creator)
eric@62 168 pdf_set_creator (o->pdf, attributes->creator);
eric@30 169 if (attributes->title)
eric@62 170 pdf_set_title (o->pdf, attributes->title);
eric@30 171 if (attributes->subject)
eric@62 172 pdf_set_subject (o->pdf, attributes->subject);
eric@30 173 if (attributes->keywords)
eric@62 174 pdf_set_keywords (o->pdf, attributes->keywords);
eric@30 175
eric@26 176 /* prepend new output file onto list */
eric@26 177 o->next = output_files;
eric@26 178 output_files = o;
eric@26 179
eric@26 180 out = o;
eric@10 181 return (1);
eric@10 182 }
eric@10 183
eric@10 184
eric@154 185 #define MAX_BOOKMARK_LEVEL 20
eric@154 186 static pdf_bookmark_handle bookmark_vector [MAX_BOOKMARK_LEVEL + 1] = { NULL };
eric@154 187
eric@154 188
eric@108 189 bool process_page (int image, /* range 1 .. n */
eric@108 190 input_attributes_t input_attributes,
eric@131 191 bookmark_t *bookmarks,
eric@131 192 page_label_t *page_label)
eric@108 193 {
eric@131 194 pdf_page_handle page;
eric@141 195 image_info_t image_info;
eric@141 196
eric@141 197 if (! get_image_info (image, input_attributes, & image_info))
eric@141 198 return (0);
eric@131 199
eric@141 200 page = pdf_new_page (out->pdf,
eric@141 201 image_info.width_points,
eric@141 202 image_info.height_points);
eric@141 203
eric@141 204 if (! process_image (image, input_attributes, & image_info, page))
eric@141 205 return (0);
eric@108 206
eric@131 207 while (bookmarks)
eric@131 208 {
eric@154 209 if (bookmarks->level <= MAX_BOOKMARK_LEVEL)
eric@154 210 {
eric@154 211 pdf_bookmark_handle parent = bookmark_vector [bookmarks->level - 1];
eric@154 212 bookmark_vector [bookmarks->level] = pdf_new_bookmark (parent,
eric@154 213 bookmarks->name,
eric@154 214 0,
eric@154 215 page);
eric@154 216 }
eric@154 217 else
eric@154 218 {
eric@154 219 (void) pdf_new_bookmark (bookmark_vector [MAX_BOOKMARK_LEVEL],
eric@154 220 bookmarks->name,
eric@154 221 0,
eric@154 222 page);
eric@154 223 }
eric@131 224 bookmarks = bookmarks->next;
eric@131 225 }
eric@108 226
eric@131 227 if (page_label)
eric@131 228 pdf_new_page_label (out->pdf,
eric@131 229 page_label->page_index,
eric@131 230 page_label->base,
eric@131 231 page_label->count,
eric@131 232 page_label->style,
eric@131 233 page_label->prefix);
eric@131 234
eric@131 235 return (page != NULL);
eric@108 236 }
eric@108 237
eric@108 238
eric@74 239 #define MAX_BOOKMARK_NAME_LEN 500
eric@74 240
eric@74 241
eric@74 242 static int filename_length_without_suffix (char *in_fn)
eric@74 243 {
eric@74 244 char *p;
eric@74 245 int len = strlen (in_fn);
eric@74 246
eric@74 247 p = strrchr (in_fn, '.');
eric@151 248 if (p && match_input_suffix (p))
eric@74 249 return (p - in_fn);
eric@74 250 return (len);
eric@74 251 }
eric@74 252
eric@74 253
eric@74 254 /* $$$ this function should ensure that it doesn't overflow the name string! */
eric@74 255 static void generate_bookmark_name (char *name,
eric@74 256 char *bookmark_fmt,
eric@74 257 char *in_fn,
eric@74 258 int page)
eric@74 259 {
eric@74 260 bool meta = 0;
eric@74 261 int len;
eric@74 262
eric@74 263 while (*bookmark_fmt)
eric@74 264 {
eric@74 265 if (meta)
eric@74 266 {
eric@74 267 meta = 0;
eric@74 268 switch (*bookmark_fmt)
eric@74 269 {
eric@74 270 case '%':
eric@74 271 *(name++) = '%';
eric@74 272 break;
eric@74 273 case 'F':
eric@74 274 len = filename_length_without_suffix (in_fn);
eric@74 275 strncpy (name, in_fn, len);
eric@74 276 name += len;
eric@74 277 break;
eric@74 278 case 'p':
eric@74 279 sprintf (name, "%d", page);
eric@74 280 name += strlen (name);
eric@74 281 break;
eric@74 282 default:
eric@74 283 break;
eric@74 284 }
eric@74 285 }
eric@74 286 else
eric@74 287 switch (*bookmark_fmt)
eric@74 288 {
eric@74 289 case '%':
eric@74 290 meta = 1;
eric@74 291 break;
eric@74 292 default:
eric@74 293 *(name++) = *bookmark_fmt;
eric@74 294 }
eric@74 295 bookmark_fmt++;
eric@74 296 }
eric@116 297 *name = '\0';
eric@74 298 }
eric@74 299
eric@74 300
eric@74 301 void main_args (char *out_fn,
eric@74 302 int inf_count,
eric@74 303 char **in_fn,
eric@74 304 char *bookmark_fmt)
eric@49 305 {
eric@49 306 int i, ip;
eric@49 307 input_attributes_t input_attributes;
eric@49 308 pdf_file_attributes_t output_attributes;
eric@74 309 bookmark_t bookmark;
eric@74 310 char bookmark_name [MAX_BOOKMARK_NAME_LEN];
eric@74 311
eric@74 312 bookmark.next = NULL;
eric@74 313 bookmark.level = 1;
eric@74 314 bookmark.name = & bookmark_name [0];
eric@49 315
eric@49 316 memset (& input_attributes, 0, sizeof (input_attributes));
eric@49 317 memset (& output_attributes, 0, sizeof (output_attributes));
eric@49 318
eric@49 319 if (! open_pdf_output_file (out_fn, & output_attributes))
eric@49 320 fatal (3, "error opening output file \"%s\"\n", out_fn);
eric@49 321 for (i = 0; i < inf_count; i++)
eric@49 322 {
eric@139 323 if (! open_input_file (in_fn [i]))
eric@49 324 fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
eric@49 325 for (ip = 1;; ip++)
eric@49 326 {
eric@62 327 fprintf (stderr, "processing page %d of file \"%s\"\r", ip, in_fn [i]);
eric@74 328 if (bookmark_fmt)
eric@74 329 generate_bookmark_name (& bookmark_name [0],
eric@74 330 bookmark_fmt,
eric@74 331 in_fn [i],
eric@74 332 ip);
eric@74 333 if (! process_page (ip, input_attributes,
eric@131 334 bookmark_fmt ? & bookmark : NULL,
eric@131 335 NULL))
eric@49 336 fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
eric@139 337 if (last_input_page ())
eric@49 338 break;
eric@49 339 }
eric@49 340 if (verbose)
eric@49 341 fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
eric@139 342 if (! close_input_file ())
eric@49 343 fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
eric@49 344 }
eric@49 345 if (! close_pdf_output_files ())
eric@49 346 fatal (3, "error closing output file \"%s\"\n", out_fn);
eric@49 347 }
eric@49 348
eric@49 349
eric@134 350 void main_control (char *control_fn)
eric@49 351 {
eric@134 352 if (! parse_control_file (control_fn))
eric@134 353 fatal (2, "error parsing control file\n");
eric@134 354 if (! process_controls ())
eric@134 355 fatal (3, "error processing control file\n");
eric@49 356 }
eric@49 357
eric@49 358
eric@10 359 int main (int argc, char *argv[])
eric@10 360 {
eric@134 361 char *control_fn = NULL;
eric@49 362 char *out_fn = NULL;
eric@62 363 char *bookmark_fmt = NULL;
eric@49 364 int inf_count = 0;
eric@49 365 char *in_fn [MAX_INPUT_FILES];
eric@49 366
eric@49 367 progname = argv [0];
eric@10 368
eric@62 369 pdf_init ();
eric@10 370
eric@141 371 init_tiff_handler ();
eric@141 372 init_jpeg_handler ();
eric@157 373 init_pbm_handler ();
eric@141 374
eric@49 375 while (--argc)
eric@10 376 {
eric@49 377 if (argv [1][0] == '-')
eric@49 378 {
eric@49 379 if (strcmp (argv [1], "-v") == 0)
eric@49 380 verbose++;
eric@49 381 else if (strcmp (argv [1], "-o") == 0)
eric@49 382 {
eric@49 383 if (argc)
eric@49 384 {
eric@49 385 argc--;
eric@49 386 argv++;
eric@49 387 out_fn = argv [1];
eric@49 388 }
eric@49 389 else
eric@49 390 fatal (1, "missing filename after \"-o\" option\n");
eric@49 391 }
eric@134 392 else if (strcmp (argv [1], "-c") == 0)
eric@49 393 {
eric@49 394 if (argc)
eric@49 395 {
eric@49 396 argc--;
eric@49 397 argv++;
eric@134 398 control_fn = argv [1];
eric@49 399 }
eric@49 400 else
eric@49 401 fatal (1, "missing filename after \"-s\" option\n");
eric@49 402 }
eric@62 403 else if (strcmp (argv [1], "-b") == 0)
eric@62 404 {
eric@62 405 if (argc)
eric@62 406 {
eric@62 407 argc--;
eric@62 408 argv++;
eric@62 409 bookmark_fmt = argv [1];
eric@62 410 }
eric@62 411 else
eric@62 412 fatal (1, "missing format string after \"-b\" option\n");
eric@62 413 }
eric@49 414 else
eric@49 415 fatal (1, "unrecognized option \"%s\"\n", argv [1]);
eric@49 416 }
eric@49 417 else if (inf_count < MAX_INPUT_FILES)
eric@49 418 in_fn [inf_count++] = argv [1];
eric@49 419 else
eric@49 420 fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
eric@49 421 argv++;
eric@10 422 }
eric@10 423
eric@134 424 if (! ((! out_fn) ^ (! control_fn)))
eric@134 425 fatal (1, "either a control file or an output file (but not both) must be specified\n");
eric@49 426
eric@49 427 if (out_fn && ! inf_count)
eric@49 428 fatal (1, "no input files specified\n");
eric@26 429
eric@134 430 if (control_fn && inf_count)
eric@134 431 fatal (1, "if control file is provided, input files can't be specified as arguments\n");
eric@49 432
eric@134 433 if (control_fn)
eric@134 434 main_control (control_fn);
eric@49 435 else
eric@74 436 main_args (out_fn, inf_count, in_fn, bookmark_fmt);
eric@17 437
eric@139 438 close_input_file ();
eric@26 439 close_pdf_output_files ();
eric@49 440 exit (0);
eric@10 441 }