| 1 | /* |
| 2 | * |
| 3 | * Print plug-in driver utility functions for the GIMP. |
| 4 | * |
| 5 | * Copyright 1997-2000 Michael Sweet ([email protected]) and |
| 6 | * Robert Krawitz ([email protected]) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This file must include only standard C header files. The core code must |
| 24 | * compile on generic platforms that don't support glib, gimp, gtk, etc. |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | #include <gutenprint/gutenprint.h> |
| 31 | #include "gutenprint-internal.h" |
| 32 | #include <gutenprint/gutenprint-intl-internal.h> |
| 33 | #include <math.h> |
| 34 | #include <limits.h> |
| 35 | #if defined(HAVE_VARARGS_H) && !defined(HAVE_STDARG_H) |
| 36 | #include <varargs.h> |
| 37 | #else |
| 38 | #include <stdarg.h> |
| 39 | #endif |
| 40 | #include <string.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <unistd.h> |
| 46 | #include "generic-options.h" |
| 47 | |
| 48 | #define FMIN(a, b) ((a) < (b) ? (a) : (b)) |
| 49 | |
| 50 | static stp_outfunc_t global_errfunc; |
| 51 | static void *global_errdata; |
| 52 | static stp_outfunc_t global_dbgfunc; |
| 53 | static void *global_dbgdata; |
| 54 | |
| 55 | typedef struct |
| 56 | { |
| 57 | stp_outfunc_t ofunc; |
| 58 | void *odata; |
| 59 | char *data; |
| 60 | size_t bytes; |
| 61 | } debug_msgbuf_t; |
| 62 | |
| 63 | /* |
| 64 | * We cannot avoid use of the (non-ANSI) vsnprintf here; ANSI does |
| 65 | * not provide a safe, length-limited sprintf function. |
| 66 | */ |
| 67 | |
| 68 | #define STPI_VASPRINTF(result, bytes, format) \ |
| 69 | { \ |
| 70 | int current_allocation = 64; /* vsnprintf returns int */ \ |
| 71 | result = stp_malloc(current_allocation); \ |
| 72 | while (current_allocation < INT_MAX / 2) \ |
| 73 | { \ |
| 74 | va_list args; \ |
| 75 | va_start(args, format); \ |
| 76 | bytes = vsnprintf(result, current_allocation, format, args); \ |
| 77 | va_end(args); \ |
| 78 | if (bytes >= 0 && bytes < current_allocation) \ |
| 79 | break; \ |
| 80 | else \ |
| 81 | { \ |
| 82 | stp_free (result); \ |
| 83 | if (bytes < 0) \ |
| 84 | current_allocation *= 2; \ |
| 85 | else \ |
| 86 | current_allocation = bytes + 1; \ |
| 87 | result = stp_malloc(current_allocation); \ |
| 88 | } \ |
| 89 | } \ |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | stp_zprintf(const stp_vars_t *v, const char *format, ...) |
| 94 | { |
| 95 | char *result; |
| 96 | int bytes; |
| 97 | STPI_VASPRINTF(result, bytes, format); |
| 98 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), result, bytes); |
| 99 | stp_free(result); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | stp_asprintf(char **strp, const char *format, ...) |
| 104 | { |
| 105 | char *result; |
| 106 | int bytes; |
| 107 | STPI_VASPRINTF(result, bytes, format); |
| 108 | *strp = result; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | stp_catprintf(char **strp, const char *format, ...) |
| 113 | { |
| 114 | char *result1; |
| 115 | char *result2; |
| 116 | int bytes; |
| 117 | STPI_VASPRINTF(result1, bytes, format); |
| 118 | stp_asprintf(&result2, "%s%s", *strp, result1); |
| 119 | stp_free(result1); |
| 120 | *strp = result2; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void |
| 125 | stp_zfwrite(const char *buf, size_t bytes, size_t nitems, const stp_vars_t *v) |
| 126 | { |
| 127 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), buf, bytes * nitems); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | stp_write_raw(const stp_raw_t *raw, const stp_vars_t *v) |
| 132 | { |
| 133 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), raw->data, raw->bytes); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | stp_putc(int ch, const stp_vars_t *v) |
| 138 | { |
| 139 | unsigned char a = (unsigned char) ch; |
| 140 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), (char *) &a, 1); |
| 141 | } |
| 142 | |
| 143 | #define BYTE(expr, byteno) (((expr) >> (8 * byteno)) & 0xff) |
| 144 | |
| 145 | void |
| 146 | stp_put16_le(unsigned short sh, const stp_vars_t *v) |
| 147 | { |
| 148 | stp_putc(BYTE(sh, 0), v); |
| 149 | stp_putc(BYTE(sh, 1), v); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | stp_put16_be(unsigned short sh, const stp_vars_t *v) |
| 154 | { |
| 155 | stp_putc(BYTE(sh, 1), v); |
| 156 | stp_putc(BYTE(sh, 0), v); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | stp_put32_le(unsigned int in, const stp_vars_t *v) |
| 161 | { |
| 162 | stp_putc(BYTE(in, 0), v); |
| 163 | stp_putc(BYTE(in, 1), v); |
| 164 | stp_putc(BYTE(in, 2), v); |
| 165 | stp_putc(BYTE(in, 3), v); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | stp_put32_be(unsigned int in, const stp_vars_t *v) |
| 170 | { |
| 171 | stp_putc(BYTE(in, 3), v); |
| 172 | stp_putc(BYTE(in, 2), v); |
| 173 | stp_putc(BYTE(in, 1), v); |
| 174 | stp_putc(BYTE(in, 0), v); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | stp_puts(const char *s, const stp_vars_t *v) |
| 179 | { |
| 180 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), s, strlen(s)); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | stp_putraw(const stp_raw_t *r, const stp_vars_t *v) |
| 185 | { |
| 186 | (stp_get_outfunc(v))((void *)(stp_get_outdata(v)), r->data, r->bytes); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | stp_send_command(const stp_vars_t *v, const char *command, |
| 191 | const char *format, ...) |
| 192 | { |
| 193 | unsigned int i = 0; |
| 194 | char fchar; |
| 195 | const char *out_str; |
| 196 | const stp_raw_t *out_raw; |
| 197 | unsigned short byte_count = 0; |
| 198 | va_list args; |
| 199 | |
| 200 | if (strlen(format) > 0) |
| 201 | { |
| 202 | va_start(args, format); |
| 203 | for (i = 0; i < strlen(format); i++) |
| 204 | { |
| 205 | switch (format[i]) |
| 206 | { |
| 207 | case 'a': |
| 208 | case 'b': |
| 209 | case 'B': |
| 210 | case 'd': |
| 211 | case 'D': |
| 212 | break; |
| 213 | case 'c': |
| 214 | (void) va_arg(args, unsigned int); |
| 215 | byte_count += 1; |
| 216 | break; |
| 217 | case 'h': |
| 218 | case 'H': |
| 219 | (void) va_arg(args, unsigned int); |
| 220 | byte_count += 2; |
| 221 | break; |
| 222 | case 'l': |
| 223 | case 'L': |
| 224 | (void) va_arg(args, unsigned int); |
| 225 | byte_count += 4; |
| 226 | break; |
| 227 | case 'r': |
| 228 | out_raw = va_arg(args, const stp_raw_t *); |
| 229 | byte_count += out_raw->bytes; |
| 230 | break; |
| 231 | case 's': |
| 232 | out_str = va_arg(args, const char *); |
| 233 | byte_count += strlen(out_str); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | va_end(args); |
| 238 | } |
| 239 | |
| 240 | stp_puts(command, v); |
| 241 | |
| 242 | va_start(args, format); |
| 243 | while ((fchar = format[0]) != '\0') |
| 244 | { |
| 245 | switch (fchar) |
| 246 | { |
| 247 | case 'a': |
| 248 | stp_putc(byte_count, v); |
| 249 | break; |
| 250 | case 'b': |
| 251 | stp_put16_le(byte_count, v); |
| 252 | break; |
| 253 | case 'B': |
| 254 | stp_put16_be(byte_count, v); |
| 255 | break; |
| 256 | case 'd': |
| 257 | stp_put32_le(byte_count, v); |
| 258 | break; |
| 259 | case 'D': |
| 260 | stp_put32_be(byte_count, v); |
| 261 | break; |
| 262 | case 'c': |
| 263 | stp_putc(va_arg(args, unsigned int), v); |
| 264 | break; |
| 265 | case 'h': |
| 266 | stp_put16_le(va_arg(args, unsigned int), v); |
| 267 | break; |
| 268 | case 'H': |
| 269 | stp_put16_be(va_arg(args, unsigned int), v); |
| 270 | break; |
| 271 | case 'l': |
| 272 | stp_put32_le(va_arg(args, unsigned int), v); |
| 273 | break; |
| 274 | case 'L': |
| 275 | stp_put32_be(va_arg(args, unsigned int), v); |
| 276 | break; |
| 277 | case 's': |
| 278 | stp_puts(va_arg(args, const char *), v); |
| 279 | break; |
| 280 | case 'r': |
| 281 | stp_putraw(va_arg(args, const stp_raw_t *), v); |
| 282 | break; |
| 283 | } |
| 284 | format++; |
| 285 | } |
| 286 | va_end(args); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | stp_set_global_errfunc(stp_outfunc_t val) |
| 291 | { |
| 292 | global_errfunc = val; |
| 293 | } |
| 294 | |
| 295 | stp_outfunc_t |
| 296 | stp_get_global_errfunc(void) |
| 297 | { |
| 298 | return global_errfunc; |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | stp_set_global_errdata(void *val) |
| 303 | { |
| 304 | global_errdata = val; |
| 305 | } |
| 306 | |
| 307 | void * |
| 308 | stp_get_global_errdata(void) |
| 309 | { |
| 310 | return global_errdata; |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | stp_set_global_dbgfunc(stp_outfunc_t val) |
| 315 | { |
| 316 | global_dbgfunc = val; |
| 317 | } |
| 318 | |
| 319 | stp_outfunc_t |
| 320 | stp_get_global_dbgfunc(void) |
| 321 | { |
| 322 | return global_dbgfunc; |
| 323 | } |
| 324 | |
| 325 | void |
| 326 | stp_set_global_dbgdata(void *val) |
| 327 | { |
| 328 | global_dbgdata = val; |
| 329 | } |
| 330 | |
| 331 | void * |
| 332 | stp_get_global_dbgdata(void) |
| 333 | { |
| 334 | return global_dbgdata; |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | stp_eprintf(const stp_vars_t *v, const char *format, ...) |
| 339 | { |
| 340 | int bytes; |
| 341 | stp_outfunc_t errfunc = stp_get_errfunc(v); |
| 342 | if (! errfunc) |
| 343 | errfunc = global_errfunc; |
| 344 | void * errdata = stp_get_errdata(v); |
| 345 | if (! errdata) |
| 346 | errdata = global_errdata; |
| 347 | if (errfunc) |
| 348 | { |
| 349 | char *result; |
| 350 | STPI_VASPRINTF(result, bytes, format); |
| 351 | errfunc(errdata, result, bytes); |
| 352 | stp_free(result); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | va_list args; |
| 357 | va_start(args, format); |
| 358 | vfprintf(stderr, format, args); |
| 359 | va_end(args); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | stp_erputc(int ch) |
| 365 | { |
| 366 | if (global_errfunc) |
| 367 | { |
| 368 | char c[1]; |
| 369 | c[0] = (char) ch; |
| 370 | global_errfunc(global_errdata, c, 1); |
| 371 | } |
| 372 | else |
| 373 | putc(ch, stderr); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | stp_erprintf(const char *format, ...) |
| 378 | { |
| 379 | if (global_errfunc) |
| 380 | { |
| 381 | int bytes; |
| 382 | char *result; |
| 383 | STPI_VASPRINTF(result, bytes, format); |
| 384 | global_errfunc(global_errdata, result, bytes); |
| 385 | stp_free(result); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | va_list args; |
| 390 | va_start(args, format); |
| 391 | vfprintf(stderr, format, args); |
| 392 | va_end(args); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static unsigned long stpi_debug_level = 0; |
| 397 | |
| 398 | static void |
| 399 | stpi_init_debug(void) |
| 400 | { |
| 401 | static int debug_initialized = 0; |
| 402 | if (!debug_initialized) |
| 403 | { |
| 404 | const char *dval = getenv("STP_DEBUG"); |
| 405 | debug_initialized = 1; |
| 406 | if (dval) |
| 407 | { |
| 408 | stpi_debug_level = strtoul(dval, 0, 0); |
| 409 | stp_erprintf("Gutenprint %s %s\n", VERSION, RELEASE_DATE); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | unsigned long |
| 415 | stp_get_debug_level(void) |
| 416 | { |
| 417 | stpi_init_debug(); |
| 418 | return stpi_debug_level; |
| 419 | } |
| 420 | |
| 421 | void |
| 422 | stp_dprintf(unsigned long level, const stp_vars_t *v, const char *format, ...) |
| 423 | { |
| 424 | stpi_init_debug(); |
| 425 | if (level & stpi_debug_level) |
| 426 | { |
| 427 | stp_outfunc_t dbgfunc = stp_get_dbgfunc(v); |
| 428 | if (! dbgfunc) |
| 429 | dbgfunc = global_dbgfunc; |
| 430 | if (! dbgfunc) |
| 431 | dbgfunc = stp_get_errfunc(v); |
| 432 | if (! dbgfunc) |
| 433 | dbgfunc = global_errfunc; |
| 434 | void *dbgdata = stp_get_dbgdata(v); |
| 435 | if (! dbgdata) |
| 436 | dbgdata = global_dbgdata; |
| 437 | if (! dbgdata) |
| 438 | dbgdata = stp_get_errdata(v); |
| 439 | if (! dbgdata) |
| 440 | dbgdata = global_errdata; |
| 441 | if (dbgfunc) |
| 442 | { |
| 443 | int bytes; |
| 444 | char *result; |
| 445 | STPI_VASPRINTF(result, bytes, format); |
| 446 | dbgfunc(dbgdata, result, bytes); |
| 447 | stp_free(result); |
| 448 | } |
| 449 | else |
| 450 | { |
| 451 | va_list args; |
| 452 | va_start(args, format); |
| 453 | vfprintf(stderr, format, args); |
| 454 | va_end(args); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | stp_deprintf(unsigned long level, const char *format, ...) |
| 461 | { |
| 462 | stpi_init_debug(); |
| 463 | if (level & stpi_debug_level) |
| 464 | { |
| 465 | if (global_dbgfunc) |
| 466 | { |
| 467 | int bytes; |
| 468 | char *result; |
| 469 | STPI_VASPRINTF(result, bytes, format); |
| 470 | global_dbgfunc(global_dbgdata, result, bytes); |
| 471 | stp_free(result); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | va_list args; |
| 476 | va_start(args, format); |
| 477 | vfprintf(stderr, format, args); |
| 478 | va_end(args); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static void |
| 484 | fill_buffer_writefunc(void *priv, const char *buffer, size_t bytes) |
| 485 | { |
| 486 | debug_msgbuf_t *msgbuf = (debug_msgbuf_t *) priv; |
| 487 | if (msgbuf->bytes == 0) |
| 488 | msgbuf->data = stp_malloc(bytes + 1); |
| 489 | else |
| 490 | msgbuf->data = stp_realloc(msgbuf->data, msgbuf->bytes + bytes + 1); |
| 491 | memcpy(msgbuf->data + msgbuf->bytes, buffer, bytes); |
| 492 | msgbuf->bytes += bytes; |
| 493 | msgbuf->data[msgbuf->bytes] = '\0'; |
| 494 | } |
| 495 | |
| 496 | void |
| 497 | stp_init_debug_messages(stp_vars_t *v) |
| 498 | { |
| 499 | int verified_flag = stp_get_verified(v); |
| 500 | debug_msgbuf_t *msgbuf = stp_malloc(sizeof(debug_msgbuf_t)); |
| 501 | msgbuf->ofunc = stp_get_dbgfunc(v); |
| 502 | msgbuf->odata = stp_get_dbgdata(v); |
| 503 | msgbuf->data = NULL; |
| 504 | msgbuf->bytes = 0; |
| 505 | stp_set_dbgfunc((stp_vars_t *) v, fill_buffer_writefunc); |
| 506 | stp_set_dbgdata((stp_vars_t *) v, msgbuf); |
| 507 | stp_set_verified((stp_vars_t *) v, verified_flag); |
| 508 | } |
| 509 | |
| 510 | void |
| 511 | stp_flush_debug_messages(stp_vars_t *v) |
| 512 | { |
| 513 | int verified_flag = stp_get_verified(v); |
| 514 | debug_msgbuf_t *msgbuf = (debug_msgbuf_t *)stp_get_dbgdata(v); |
| 515 | stp_set_dbgfunc((stp_vars_t *) v, msgbuf->ofunc); |
| 516 | stp_set_dbgdata((stp_vars_t *) v, msgbuf->odata); |
| 517 | stp_set_verified((stp_vars_t *) v, verified_flag); |
| 518 | if (msgbuf->bytes > 0) |
| 519 | { |
| 520 | /* |
| 521 | * Messages aren't tagged by debug value, so we force them |
| 522 | * out if any debug flag is set. |
| 523 | */ |
| 524 | stp_dprintf((unsigned long) -1, v, "%s", msgbuf->data); |
| 525 | stp_free(msgbuf->data); |
| 526 | } |
| 527 | stp_free(msgbuf); |
| 528 | } |
| 529 | |
| 530 | /* pointers to the allocation functions to use, which may be set by |
| 531 | client applications */ |
| 532 | void *(*stp_malloc_func)(size_t size) = malloc; |
| 533 | void *(*stpi_realloc_func)(void *ptr, size_t size) = realloc; |
| 534 | void (*stpi_free_func)(void *ptr) = free; |
| 535 | |
| 536 | void * |
| 537 | stp_malloc (size_t size) |
| 538 | { |
| 539 | register void *memptr = NULL; |
| 540 | |
| 541 | if ((memptr = stp_malloc_func (size)) == NULL) |
| 542 | { |
| 543 | fputs("Virtual memory exhausted.\n", stderr); |
| 544 | stp_abort(); |
| 545 | } |
| 546 | return (memptr); |
| 547 | } |
| 548 | |
| 549 | void * |
| 550 | stp_zalloc (size_t size) |
| 551 | { |
| 552 | register void *memptr = stp_malloc(size); |
| 553 | (void) memset(memptr, 0, size); |
| 554 | return (memptr); |
| 555 | } |
| 556 | |
| 557 | void * |
| 558 | stp_realloc (void *ptr, size_t size) |
| 559 | { |
| 560 | register void *memptr = NULL; |
| 561 | |
| 562 | if (size > 0 && ((memptr = stpi_realloc_func (ptr, size)) == NULL)) |
| 563 | { |
| 564 | fputs("Virtual memory exhausted.\n", stderr); |
| 565 | stp_abort(); |
| 566 | } |
| 567 | return (memptr); |
| 568 | } |
| 569 | |
| 570 | void |
| 571 | stp_free(void *ptr) |
| 572 | { |
| 573 | stpi_free_func(ptr); |
| 574 | } |
| 575 | |
| 576 | int |
| 577 | stp_init(void) |
| 578 | { |
| 579 | static int stpi_is_initialised = 0; |
| 580 | if (!stpi_is_initialised) |
| 581 | { |
| 582 | /* Things that are only initialised once */ |
| 583 | /* Set up gettext */ |
| 584 | #ifdef HAVE_LOCALE_H |
| 585 | char *locale = setlocale (LC_ALL, ""); |
| 586 | if (locale) |
| 587 | { |
| 588 | locale = stp_strdup(locale); |
| 589 | } |
| 590 | #endif |
| 591 | #ifdef ENABLE_NLS |
| 592 | bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR); |
| 593 | textdomain(PACKAGE); |
| 594 | #endif |
| 595 | #ifdef HAVE_LOCALE_H |
| 596 | if (locale) |
| 597 | { |
| 598 | setlocale(LC_ALL, locale); |
| 599 | stp_free(locale); |
| 600 | } |
| 601 | #endif |
| 602 | stpi_init_debug(); |
| 603 | stp_xml_preinit(); |
| 604 | stpi_init_printer(); |
| 605 | stpi_init_dither(); |
| 606 | /* Load modules */ |
| 607 | if (stp_module_load()) |
| 608 | return 1; |
| 609 | /* Load XML data */ |
| 610 | if (stp_xml_init_defaults()) |
| 611 | return 1; |
| 612 | /* Initialise modules */ |
| 613 | if (stp_module_init()) |
| 614 | return 1; |
| 615 | /* Set up defaults for core parameters */ |
| 616 | stp_initialize_printer_defaults(); |
| 617 | } |
| 618 | |
| 619 | stpi_is_initialised = 1; |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | size_t |
| 624 | stp_strlen(const char *s) |
| 625 | { |
| 626 | return strlen(s); |
| 627 | } |
| 628 | |
| 629 | char * |
| 630 | stp_strndup(const char *s, int n) |
| 631 | { |
| 632 | #if HAVE_DECL_STRNDUP |
| 633 | return strndup(s, n); |
| 634 | #else |
| 635 | char *ret; |
| 636 | if (!s || n < 0) |
| 637 | { |
| 638 | ret = stp_malloc(1); |
| 639 | ret[0] = 0; |
| 640 | return ret; |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | ret = stp_malloc(n + 1); |
| 645 | memcpy(ret, s, n); |
| 646 | ret[n] = 0; |
| 647 | return ret; |
| 648 | } |
| 649 | #endif |
| 650 | } |
| 651 | |
| 652 | char * |
| 653 | stp_strdup(const char *s) |
| 654 | { |
| 655 | #if HAVE_DECL_STRDUP |
| 656 | return strdup(s); |
| 657 | #else |
| 658 | char *ret; |
| 659 | if (!s) |
| 660 | { |
| 661 | ret = stp_malloc(1); |
| 662 | ret[0] = '\0'; |
| 663 | return ret; |
| 664 | } |
| 665 | else |
| 666 | return stp_strndup(s, stp_strlen(s)); |
| 667 | #endif |
| 668 | } |
| 669 | |
| 670 | const char * |
| 671 | stp_set_output_codeset(const char *codeset) |
| 672 | { |
| 673 | #if defined(ENABLE_NLS) && !defined(__APPLE__) |
| 674 | return (const char *)(bind_textdomain_codeset(PACKAGE, codeset)); |
| 675 | #else |
| 676 | return "US-ASCII"; |
| 677 | #endif |
| 678 | } |
| 679 | |
| 680 | stp_curve_t * |
| 681 | stp_read_and_compose_curves(const char *s1, const char *s2, |
| 682 | stp_curve_compose_t comp, |
| 683 | size_t piecewise_point_count) |
| 684 | { |
| 685 | stp_curve_t *ret = NULL; |
| 686 | stp_curve_t *t1 = NULL; |
| 687 | stp_curve_t *t2 = NULL; |
| 688 | if (s1) |
| 689 | t1 = stp_curve_create_from_string(s1); |
| 690 | if (s2) |
| 691 | t2 = stp_curve_create_from_string(s2); |
| 692 | if (t1 && t2) |
| 693 | { |
| 694 | if (stp_curve_is_piecewise(t1) && stp_curve_is_piecewise(t2)) |
| 695 | { |
| 696 | stp_curve_resample(t1, piecewise_point_count); |
| 697 | stp_curve_resample(t2, piecewise_point_count); |
| 698 | } |
| 699 | stp_curve_compose(&ret, t1, t2, comp, -1); |
| 700 | } |
| 701 | if (ret) |
| 702 | { |
| 703 | stp_curve_destroy(t1); |
| 704 | stp_curve_destroy(t2); |
| 705 | return ret; |
| 706 | } |
| 707 | else if (t1) |
| 708 | { |
| 709 | if(t2) |
| 710 | stp_curve_destroy(t2); |
| 711 | return t1; |
| 712 | } |
| 713 | else |
| 714 | return t2; |
| 715 | } |
| 716 | |
| 717 | void |
| 718 | stp_merge_printvars(stp_vars_t *user, const stp_vars_t *print) |
| 719 | { |
| 720 | int i; |
| 721 | stp_parameter_list_t params = stp_get_parameter_list(print); |
| 722 | int count = stp_parameter_list_count(params); |
| 723 | stp_dprintf(STP_DBG_VARS, user, "Merging printvars from %s\n", |
| 724 | stp_get_driver(print)); |
| 725 | for (i = 0; i < count; i++) |
| 726 | { |
| 727 | const stp_parameter_t *p = stp_parameter_list_param(params, i); |
| 728 | if (p->p_type == STP_PARAMETER_TYPE_DOUBLE && |
| 729 | p->p_class == STP_PARAMETER_CLASS_OUTPUT && |
| 730 | stp_check_float_parameter(print, p->name, STP_PARAMETER_DEFAULTED)) |
| 731 | { |
| 732 | stp_parameter_t desc; |
| 733 | double prnval = stp_get_float_parameter(print, p->name); |
| 734 | double usrval; |
| 735 | stp_describe_parameter(print, p->name, &desc); |
| 736 | if (stp_check_float_parameter(user, p->name, STP_PARAMETER_ACTIVE)) |
| 737 | usrval = stp_get_float_parameter(user, p->name); |
| 738 | else |
| 739 | usrval = desc.deflt.dbl; |
| 740 | if (strcmp(p->name, "Gamma") == 0) |
| 741 | usrval /= prnval; |
| 742 | else |
| 743 | usrval *= prnval; |
| 744 | if (usrval < desc.bounds.dbl.lower) |
| 745 | usrval = desc.bounds.dbl.lower; |
| 746 | else if (usrval > desc.bounds.dbl.upper) |
| 747 | usrval = desc.bounds.dbl.upper; |
| 748 | if (!stp_check_float_parameter(user, p->name, STP_PARAMETER_ACTIVE)) |
| 749 | { |
| 750 | stp_clear_float_parameter(user, p->name); |
| 751 | stp_set_default_float_parameter(user, p->name, usrval); |
| 752 | } |
| 753 | else |
| 754 | stp_set_float_parameter(user, p->name, usrval); |
| 755 | stp_parameter_description_destroy(&desc); |
| 756 | } |
| 757 | } |
| 758 | stp_dprintf(STP_DBG_VARS, user, "Exiting merge printvars\n"); |
| 759 | stp_parameter_list_destroy(params); |
| 760 | } |
| 761 | |
| 762 | stp_parameter_list_t |
| 763 | stp_get_parameter_list(const stp_vars_t *v) |
| 764 | { |
| 765 | stp_parameter_list_t *ret = stp_parameter_list_create(); |
| 766 | stp_parameter_list_t *tmp_list; |
| 767 | |
| 768 | tmp_list = stp_printer_list_parameters(v); |
| 769 | stp_parameter_list_append(ret, tmp_list); |
| 770 | stp_parameter_list_destroy(tmp_list); |
| 771 | |
| 772 | tmp_list = stp_color_list_parameters(v); |
| 773 | stp_parameter_list_append(ret, tmp_list); |
| 774 | stp_parameter_list_destroy(tmp_list); |
| 775 | |
| 776 | tmp_list = stp_list_generic_parameters(v); |
| 777 | stp_parameter_list_append(ret, tmp_list); |
| 778 | stp_parameter_list_destroy(tmp_list); |
| 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | void |
| 784 | stp_abort(void) |
| 785 | { |
| 786 | if (stp_get_debug_level() > 0) |
| 787 | { |
| 788 | int i = *((volatile int *) 0); |
| 789 | stp_erprintf("Crashing here...%d\n", i); |
| 790 | } |
| 791 | abort(); |
| 792 | } |
| 793 | |
| 794 | time_t |
| 795 | stpi_time(time_t *t) |
| 796 | { |
| 797 | if (stp_get_debug_level() & STP_DBG_STATIC_TIME) |
| 798 | { |
| 799 | if (t) |
| 800 | *t = (time_t) 0; |
| 801 | return (time_t) 0; |
| 802 | } |
| 803 | else |
| 804 | return time(t); |
| 805 | } |