exerror.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /*
  11. * Glenn Fowler
  12. * AT&T Research
  13. *
  14. * expression library
  15. */
  16. #include <assert.h>
  17. #include <expr/exlib.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <util/exit.h>
  23. /*
  24. * library error handler
  25. */
  26. static char *make_msg(const char *format, va_list ap) {
  27. // retrieve buffered message
  28. char buf[64];
  29. excontext(expr.program, buf, sizeof(buf));
  30. // how many bytes do we need to construct the message?
  31. size_t len = (size_t)snprintf(NULL, 0, "%s\n -- ", buf);
  32. {
  33. va_list ap2;
  34. va_copy(ap2, ap);
  35. int r = vsnprintf(NULL, 0, format, ap2);
  36. va_end(ap2);
  37. if (r < 0) {
  38. return strdup("malformed format");
  39. }
  40. len += (size_t)r + 1; // +1 for NUL
  41. }
  42. char *s = malloc(len);
  43. if (s == NULL) {
  44. return NULL;
  45. }
  46. int offset = snprintf(s, len, "%s\n -- ", buf);
  47. assert(offset > 0);
  48. vsnprintf(s + offset, len - (size_t)offset, format, ap);
  49. return s;
  50. }
  51. void
  52. exerror(const char* format, ...)
  53. {
  54. if (expr.program->disc->errorf && !expr.program->errors)
  55. {
  56. va_list ap;
  57. expr.program->errors = 1;
  58. va_start(ap, format);
  59. char *s = make_msg(format, ap);
  60. va_end(ap);
  61. expr.program->disc->errorf(expr.program, expr.program->disc, 2, "%s",
  62. s ? s : "out of space");
  63. free(s);
  64. }
  65. }
  66. void
  67. exwarn(const char *format, ...)
  68. {
  69. if (expr.program->disc->errorf) {
  70. va_list ap;
  71. va_start(ap, format);
  72. char *s = make_msg(format, ap);
  73. va_end(ap);
  74. expr.program->disc->errorf(expr.program, expr.program->disc,
  75. ERROR_WARNING, "%s", s ? s : "out of space");
  76. free(s);
  77. }
  78. }