semantics.c

Mon, 31 Dec 2001 08:25:04 +0000

author
eric
date
Mon, 31 Dec 2001 08:25:04 +0000
changeset 24
6575fcfbdb1b
parent 22
198616589af5
child 25
139b91f9a224
permissions
-rw-r--r--

track bookmark level.

eric@18 1 #include <stdlib.h>
eric@18 2 #include <string.h>
eric@16 3 #include <stdio.h>
eric@16 4
eric@16 5 #include "type.h"
eric@18 6 #include "semantics.h"
eric@16 7 #include "parser.tab.h"
eric@16 8
eric@16 9
eric@22 10 typedef struct
eric@22 11 {
eric@22 12 boolean has_size;
eric@22 13 page_size_t size;
eric@22 14
eric@22 15 boolean has_rotation;
eric@22 16 int rotation;
eric@22 17
eric@22 18 boolean has_crop;
eric@22 19 crop_t crop;
eric@22 20 } input_modifiers_t;
eric@22 21
eric@22 22
eric@22 23 typedef struct input_context_t
eric@22 24 {
eric@22 25 struct input_context_t *parent;
eric@22 26 struct input_context_t *next;
eric@22 27
eric@22 28 int image_count; /* how many pages reference this context,
eric@22 29 including those from subcontexts */
eric@22 30
eric@22 31 char *input_file;
eric@22 32
eric@22 33 input_modifiers_t modifiers [INPUT_MODIFIER_TYPE_COUNT];
eric@22 34 } input_context_t;
eric@22 35
eric@22 36
eric@22 37 typedef struct input_image_t
eric@22 38 {
eric@22 39 struct input_image_t *next;
eric@22 40 input_context_t *input_context;
eric@22 41 range_t range;
eric@22 42 } input_image_t;
eric@22 43
eric@22 44
eric@22 45 typedef struct output_context_t
eric@22 46 {
eric@22 47 struct output_context_t *parent;
eric@22 48 struct output_context_t *next;
eric@22 49
eric@22 50 int page_count; /* how many pages reference this context,
eric@22 51 including those from subcontexts */
eric@22 52
eric@22 53 char *output_file;
eric@22 54 bookmark_t *first_bookmark;
eric@22 55 bookmark_t *last_bookmark;
eric@22 56 char *page_number_format;
eric@22 57 } output_context_t;
eric@22 58
eric@22 59
eric@22 60 typedef struct output_page_t
eric@22 61 {
eric@22 62 struct output_page_t *next;
eric@22 63 output_context_t *output_context;
eric@22 64 range_t range;
eric@22 65 bookmark_t *bookmark_list;
eric@22 66 } output_page_t;
eric@22 67
eric@22 68
eric@19 69 #define SEMANTIC_DEBUG
eric@19 70 #ifdef SEMANTIC_DEBUG
eric@19 71 #define SDBG(x) printf x
eric@19 72 #else
eric@19 73 #define SDBG(x)
eric@19 74 #endif
eric@19 75
eric@19 76
eric@16 77 FILE *yyin;
eric@16 78 int line; /* line number in spec file */
eric@16 79
eric@24 80 int bookmark_level;
eric@16 81
eric@20 82 input_context_t *first_input_context;
eric@20 83 input_context_t *last_input_context;
eric@20 84
eric@20 85 input_modifier_type_t current_modifier_context;
eric@18 86
eric@20 87 input_image_t *first_input_image;
eric@20 88 input_image_t *last_input_image;
eric@18 89
eric@20 90 output_context_t *first_output_context;
eric@20 91 output_context_t *last_output_context;
eric@20 92
eric@20 93 output_page_t *first_output_page;
eric@20 94 output_page_t *last_output_page;
eric@16 95
eric@16 96
eric@19 97 void input_push_context (void)
eric@16 98 {
eric@18 99 input_context_t *new_input_context;
eric@18 100
eric@18 101 new_input_context = malloc (sizeof (input_context_t));
eric@18 102 if (! new_input_context)
eric@18 103 {
eric@20 104 fprintf (stderr, "failed to malloc an input context\n");
eric@18 105 return;
eric@18 106 }
eric@18 107
eric@20 108 if (last_input_context)
eric@18 109 {
eric@20 110 memcpy (new_input_context, last_input_context, sizeof (input_context_t));
eric@20 111 new_input_context->image_count = 0;
eric@18 112 }
eric@18 113 else
eric@20 114 {
eric@20 115 memset (new_input_context, 0, sizeof (input_context_t));
eric@20 116 first_input_context = new_input_context;
eric@20 117 }
eric@18 118
eric@20 119 new_input_context->parent = last_input_context;
eric@20 120 last_input_context = new_input_context;
eric@16 121 };
eric@16 122
eric@16 123 void input_pop_context (void)
eric@16 124 {
eric@20 125 if (! last_input_context)
eric@18 126 {
eric@18 127 fprintf (stderr, "failed to pop an input context\n");
eric@18 128 return;
eric@18 129 }
eric@18 130
eric@20 131 last_input_context = last_input_context->parent;
eric@16 132 };
eric@16 133
eric@19 134 void input_set_modifier_context (input_modifier_type_t type)
eric@19 135 {
eric@19 136 current_modifier_context = type;
eric@19 137 #ifdef SEMANTIC_DEBUG
eric@19 138 SDBG(("modifier type "));
eric@19 139 switch (type)
eric@19 140 {
eric@19 141 case INPUT_MODIFIER_ALL: SDBG(("all")); break;
eric@19 142 case INPUT_MODIFIER_ODD: SDBG(("odd")); break;
eric@19 143 case INPUT_MODIFIER_EVEN: SDBG(("even")); break;
eric@19 144 default: SDBG(("unknown %d", type));
eric@19 145 }
eric@19 146 SDBG(("\n"));
eric@19 147 #endif /* SEMANTIC_DEBUG */
eric@19 148 }
eric@19 149
eric@22 150 static void input_clone (void)
eric@20 151 {
eric@20 152 input_context_t *new_input_context;
eric@20 153
eric@20 154 if (! last_input_context->image_count)
eric@20 155 return;
eric@20 156
eric@20 157 new_input_context = malloc (sizeof (input_context_t));
eric@20 158 if (! new_input_context)
eric@20 159 {
eric@20 160 fprintf (stderr, "failed to malloc an input context\n");
eric@20 161 return;
eric@20 162 }
eric@20 163
eric@20 164 memcpy (new_input_context, last_input_context, sizeof (input_context_t));
eric@20 165 new_input_context->image_count = 0;
eric@20 166 last_input_context->next = new_input_context;
eric@20 167 }
eric@20 168
eric@16 169 void input_set_file (char *name)
eric@16 170 {
eric@20 171 input_clone ();
eric@20 172 last_input_context->input_file = name;
eric@16 173 };
eric@16 174
eric@19 175 void input_set_rotation (int rotation)
eric@19 176 {
eric@20 177 last_input_context->modifiers [current_modifier_context].has_rotation = 1;
eric@20 178 last_input_context->modifiers [current_modifier_context].rotation = rotation;
eric@19 179 SDBG(("rotation %d\n", rotation));
eric@19 180 }
eric@19 181
eric@22 182 static void increment_input_image_count (int count)
eric@16 183 {
eric@20 184 input_context_t *context;
eric@20 185
eric@20 186 for (context = last_input_context; context; context = context->parent)
eric@20 187 context->image_count += count;
eric@20 188 }
eric@20 189
eric@20 190 void input_images (range_t range)
eric@20 191 {
eric@20 192 input_image_t *new_image;
eric@20 193 int count = ((range.last - range.first) + 1);
eric@20 194
eric@19 195 #ifdef SEMANTIC_DEBUG
eric@20 196 if (range.first == range.last)
eric@20 197 SDBG(("image %d\n", range.first));
eric@16 198 else
eric@20 199 SDBG(("images %d..%d\n", range.first, range.last));
eric@19 200 #endif /* SEMANTIC_DEBUG */
eric@20 201
eric@20 202 new_image = calloc (1, sizeof (input_image_t));
eric@20 203 if (! new_image)
eric@20 204 {
eric@20 205 fprintf (stderr, "failed to malloc an input image struct\n");
eric@20 206 return;
eric@20 207 }
eric@20 208 if (first_input_image)
eric@20 209 {
eric@20 210 last_input_image->next = new_image;
eric@20 211 last_input_image = new_image;
eric@20 212 }
eric@20 213 else
eric@20 214 {
eric@20 215 first_input_image = last_input_image = new_image;
eric@20 216 }
eric@20 217 new_image->range = range;
eric@20 218 new_image->input_context = last_input_context;
eric@20 219 increment_input_image_count (count);
eric@16 220 }
eric@16 221
eric@18 222
eric@18 223 void output_push_context (void)
eric@18 224 {
eric@20 225 output_context_t *new_output_context;
eric@20 226
eric@20 227 new_output_context = malloc (sizeof (output_context_t));
eric@20 228 if (! new_output_context)
eric@20 229 {
eric@20 230 fprintf (stderr, "failed to malloc an output context\n");
eric@20 231 return;
eric@20 232 }
eric@20 233
eric@20 234 if (last_output_context)
eric@20 235 {
eric@20 236 memcpy (new_output_context, last_output_context, sizeof (output_context_t));
eric@20 237 new_output_context->page_count = 0;
eric@21 238 new_output_context->first_bookmark = NULL;
eric@21 239 new_output_context->last_bookmark = NULL;
eric@20 240 }
eric@20 241 else
eric@20 242 {
eric@20 243 memset (new_output_context, 0, sizeof (output_context_t));
eric@20 244 first_output_context = new_output_context;
eric@20 245 }
eric@20 246
eric@20 247 new_output_context->parent = last_output_context;
eric@20 248 last_output_context = new_output_context;
eric@18 249 };
eric@18 250
eric@20 251 void output_pop_context (void)
eric@20 252 {
eric@20 253 if (! last_output_context)
eric@20 254 {
eric@20 255 fprintf (stderr, "failed to pop an output context\n");
eric@20 256 return;
eric@20 257 }
eric@20 258
eric@20 259 last_output_context = last_output_context->parent;
eric@20 260 };
eric@20 261
eric@22 262 static void output_clone (void)
eric@20 263 {
eric@20 264 output_context_t *new_output_context;
eric@20 265
eric@20 266 if (! last_output_context->page_count)
eric@20 267 return;
eric@20 268
eric@20 269 new_output_context = malloc (sizeof (output_context_t));
eric@20 270 if (! new_output_context)
eric@20 271 {
eric@20 272 fprintf (stderr, "failed to malloc an output context\n");
eric@20 273 return;
eric@20 274 }
eric@20 275
eric@20 276 memcpy (new_output_context, last_output_context, sizeof (output_context_t));
eric@20 277 new_output_context->page_count = 0;
eric@20 278 last_output_context->next = new_output_context;
eric@20 279 }
eric@20 280
eric@16 281 void output_set_file (char *name)
eric@16 282 {
eric@20 283 output_clone ();
eric@20 284 last_output_context->output_file = name;
eric@16 285 };
eric@16 286
eric@20 287 void output_set_bookmark (char *name)
eric@16 288 {
eric@20 289 bookmark_t *new_bookmark;
eric@20 290
eric@20 291 /* As the language is defined (parser.y), a bookmark can only appear
eric@20 292 at the beginning of a context! */
eric@20 293 if (last_output_context->page_count)
eric@20 294 {
eric@20 295 fprintf (stderr, "internal error, bookmark not at beginning of context\n");
eric@20 296 exit (2);
eric@20 297 }
eric@20 298
eric@20 299 new_bookmark = calloc (1, sizeof (bookmark_t));
eric@20 300 if (! new_bookmark)
eric@20 301 {
eric@20 302 fprintf (stderr, "failed to calloc a bookmark\n");
eric@20 303 return;
eric@20 304 }
eric@20 305
eric@24 306 new_bookmark->level = bookmark_level;
eric@20 307 new_bookmark->name = name;
eric@20 308 if (last_output_context->first_bookmark)
eric@20 309 last_output_context->last_bookmark->next = new_bookmark;
eric@20 310 else
eric@20 311 last_output_context->first_bookmark = new_bookmark;
eric@20 312 last_output_context->last_bookmark = new_bookmark;
eric@20 313 }
eric@20 314
eric@20 315 void output_set_page_number_format (char *format)
eric@20 316 {
eric@20 317 output_clone ();
eric@20 318 last_output_context->page_number_format = format;
eric@20 319 }
eric@20 320
eric@22 321 static void increment_output_page_count (int count)
eric@20 322 {
eric@20 323 output_context_t *context;
eric@20 324
eric@20 325 for (context = last_output_context; context; context = context->parent)
eric@20 326 context->page_count += count;
eric@20 327 }
eric@20 328
eric@21 329
eric@20 330 void output_pages (range_t range)
eric@20 331 {
eric@20 332 output_page_t *new_page;
eric@21 333 output_context_t *context;
eric@20 334 int count = ((range.last - range.first) + 1);
eric@20 335
eric@19 336 #ifdef SEMANTIC_DEBUG
eric@20 337 if (range.first == range.last)
eric@20 338 SDBG(("page %d\n", range.first));
eric@16 339 else
eric@20 340 SDBG(("pages %d..%d\n", range.first, range.last));
eric@19 341 #endif /* SEMANTIC_DEBUG */
eric@20 342
eric@20 343 new_page = calloc (1, sizeof (output_page_t));
eric@20 344 if (! new_page)
eric@20 345 {
eric@20 346 fprintf (stderr, "failed to malloc an output page struct\n");
eric@20 347 return;
eric@20 348 }
eric@20 349 if (first_output_page)
eric@20 350 {
eric@20 351 last_output_page->next = new_page;
eric@20 352 last_output_page = new_page;
eric@20 353 }
eric@20 354 else
eric@20 355 {
eric@20 356 first_output_page = last_output_page = new_page;
eric@20 357 }
eric@20 358 new_page->range = range;
eric@20 359 new_page->output_context = last_output_context;
eric@20 360
eric@21 361 /* transfer bookmarks from context(s) to page */
eric@21 362 for (context = last_output_context; context; context = context->parent)
eric@21 363 if (context->first_bookmark)
eric@21 364 {
eric@21 365 context->last_bookmark->next = new_page->bookmark_list;
eric@21 366 new_page->bookmark_list = context->first_bookmark;
eric@21 367 context->first_bookmark = NULL;
eric@21 368 context->last_bookmark = NULL;
eric@21 369 }
eric@20 370
eric@20 371 increment_output_page_count (count);
eric@16 372 }
eric@16 373
eric@16 374
eric@16 375 void yyerror (char *s)
eric@16 376 {
eric@16 377 fprintf (stderr, "%d: %s\n", line, s);
eric@16 378 }
eric@16 379
eric@16 380
eric@22 381 static char *get_input_file (input_context_t *context)
eric@20 382 {
eric@20 383 for (; context; context = context->parent)
eric@20 384 if (context->input_file)
eric@20 385 return (context->input_file);
eric@20 386 fprintf (stderr, "no input file name found\n");
eric@20 387 exit (2);
eric@20 388 }
eric@20 389
eric@22 390 static int get_input_rotation (input_context_t *context, input_modifier_type_t type)
eric@20 391 {
eric@20 392 for (; context; context = context->parent)
eric@20 393 {
eric@20 394 if (context->modifiers [type].has_rotation)
eric@20 395 return (context->modifiers [type].rotation);
eric@20 396 if (context->modifiers [INPUT_MODIFIER_ALL].has_rotation)
eric@20 397 return (context->modifiers [INPUT_MODIFIER_ALL].rotation);
eric@20 398 }
eric@20 399 return (0); /* default */
eric@20 400 }
eric@20 401
eric@22 402 static char *get_output_file (output_context_t *context)
eric@20 403 {
eric@20 404 for (; context; context = context->parent)
eric@20 405 if (context->output_file)
eric@20 406 return (context->output_file);
eric@20 407 fprintf (stderr, "no output file name found\n");
eric@20 408 exit (2);
eric@20 409 }
eric@20 410
eric@22 411 static char *get_output_page_number_format (output_context_t *context)
eric@20 412 {
eric@20 413 for (; context; context = context->parent)
eric@20 414 if (context->page_number_format)
eric@20 415 return (context->page_number_format);
eric@20 416 return (NULL); /* default */
eric@20 417 }
eric@20 418
eric@20 419
eric@20 420 #ifdef SEMANTIC_DEBUG
eric@20 421 void dump_input_tree (void)
eric@20 422 {
eric@20 423 input_image_t *image;
eric@20 424 int i;
eric@20 425
eric@20 426 printf ("input images:\n");
eric@20 427 for (image = first_input_image; image; image = image->next)
eric@20 428 for (i = image->range.first; i <= image->range.last; i++)
eric@20 429 {
eric@20 430 input_modifier_type_t parity = (i % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN;
eric@20 431 printf ("file '%s' image %d, rotation %d\n",
eric@20 432 get_input_file (image->input_context),
eric@20 433 i,
eric@20 434 get_input_rotation (image->input_context, parity));
eric@20 435 }
eric@20 436 }
eric@20 437
eric@20 438 void dump_output_tree (void)
eric@20 439 {
eric@20 440 int i;
eric@20 441 output_page_t *page;
eric@20 442 bookmark_t *bookmark;
eric@20 443
eric@20 444 printf ("output pages:\n");
eric@20 445 for (page = first_output_page; page; page = page->next)
eric@20 446 {
eric@20 447 if (page->bookmark_list)
eric@20 448 {
eric@20 449 for (bookmark = page->bookmark_list; bookmark; bookmark = bookmark->next)
eric@24 450 printf ("bookmark %d '%s'\n", bookmark->level, bookmark->name);
eric@20 451 }
eric@20 452 for (i = page->range.first; i <= page->range.last; i++)
eric@20 453 {
eric@20 454 printf ("file '%s' ", get_output_file (page->output_context));
eric@20 455 printf ("format '%s' ", get_output_page_number_format (page->output_context));
eric@20 456 printf ("page %d\n", i);
eric@20 457 }
eric@20 458 }
eric@20 459 }
eric@20 460 #endif /* SEMANTIC_DEBUG */
eric@20 461
eric@16 462 boolean parse_spec_file (char *fn)
eric@16 463 {
eric@16 464 boolean result = 0;
eric@16 465
eric@16 466 yyin = fopen (fn, "r");
eric@16 467 if (! yyin)
eric@16 468 {
eric@16 469 fprintf (stderr, "can't open spec file '%s'\n", fn);
eric@16 470 goto fail;
eric@16 471 }
eric@16 472
eric@16 473 line = 1;
eric@16 474
eric@20 475 input_push_context (); /* create root input context */
eric@20 476 input_push_context (); /* create inital input context */
eric@20 477
eric@20 478 output_push_context (); /* create root output context */
eric@18 479 output_push_context (); /* create initial output context */
eric@18 480
eric@16 481 yyparse ();
eric@16 482
eric@20 483 if (first_input_context->image_count != first_output_context->page_count)
eric@18 484 {
eric@20 485 fprintf (stderr, "input image count %d != output page count %d\n",
eric@20 486 first_input_context->image_count,
eric@20 487 first_output_context->page_count);
eric@18 488 goto fail;
eric@18 489 }
eric@18 490
eric@20 491 fprintf (stderr, "%d pages specified\n", first_input_context->image_count);
eric@18 492
eric@16 493 result = 1;
eric@16 494
eric@20 495 #ifdef SEMANTIC_DEBUG
eric@20 496 dump_input_tree ();
eric@20 497 dump_output_tree ();
eric@20 498 #endif /* SEMANTIC_DEBUG */
eric@20 499
eric@16 500 fail:
eric@16 501 if (yyin)
eric@16 502 fclose (yyin);
eric@16 503
eric@16 504 return (result);
eric@16 505 }