csi-bind.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it either under the terms of the GNU Lesser General Public
  6. * License version 2.1 as published by the Free Software Foundation
  7. * (the "LGPL") or, at your option, under the terms of the Mozilla
  8. * Public License Version 1.1 (the "MPL"). If you do not alter this
  9. * notice, a recipient may use your version of this file under either
  10. * the MPL or the LGPL.
  11. *
  12. * You should have received a copy of the LGPL along with this library
  13. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  15. * You should have received a copy of the MPL along with this library
  16. * in the file COPYING-MPL-1.1
  17. *
  18. * The contents of this file are subject to the Mozilla Public License
  19. * Version 1.1 (the "License"); you may not use this file except in
  20. * compliance with the License. You may obtain a copy of the License at
  21. * http://www.mozilla.org/MPL/
  22. *
  23. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  24. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  25. * the specific language governing rights and limitations.
  26. *
  27. * The Original Code is the cairo graphics library.
  28. *
  29. * The Initial Developer of the Original Code is Chris Wilson.
  30. *
  31. * Contributor(s):
  32. * Chris Wilson <chris@chris-wilson.co.uk>
  33. */
  34. #include "config.h"
  35. #include "cairo.h"
  36. #include "cairo-script-interpreter.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. static cairo_status_t
  40. write_func (void *closure,
  41. const unsigned char *data,
  42. unsigned int length)
  43. {
  44. if (fwrite (data, length, 1, closure) != 1)
  45. return CAIRO_STATUS_WRITE_ERROR;
  46. return CAIRO_STATUS_SUCCESS;
  47. }
  48. int
  49. main (int argc, char **argv)
  50. {
  51. FILE *in = stdin, *out = stdout;
  52. cairo_status_t status;
  53. int i;
  54. if (argc >= 3) {
  55. if (strcmp (argv[argc-1], "-")) {
  56. out = fopen (argv[argc-1], "w");
  57. if (out == NULL) {
  58. fprintf (stderr, "Failed to open output '%s'\n", argv[argc-1]);
  59. return 1;
  60. }
  61. }
  62. }
  63. if (argc > 2) {
  64. for (i = 1; i < argc - 1; i++) {
  65. in = fopen (argv[i], "r");
  66. if (in == NULL) {
  67. fprintf (stderr, "Failed to open input '%s'\n", argv[i]);
  68. return 1;
  69. }
  70. status = cairo_script_interpreter_translate_stream (in, write_func, out);
  71. fclose (in);
  72. if (status)
  73. break;
  74. }
  75. } else {
  76. if (argc > 1) {
  77. if (strcmp (argv[1], "-")) {
  78. in = fopen (argv[1], "r");
  79. if (in == NULL) {
  80. fprintf (stderr, "Failed to open input '%s'\n", argv[1]);
  81. return 1;
  82. }
  83. }
  84. }
  85. status = cairo_script_interpreter_translate_stream (in, write_func, out);
  86. if (in != stdin)
  87. fclose (in);
  88. }
  89. if (out != stdout)
  90. fclose (out);
  91. if (status) {
  92. fprintf (stderr, "Translation failed: %s\n",
  93. cairo_status_to_string (status));
  94. return status;
  95. }
  96. return status;
  97. }