error.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* tripl/error.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <tripl/tripl.h>
  19. #include <tripl/limits.h>
  20. #include <string.h>
  21. extern char *line_eof;
  22. static int ge_dump_line(int ln, char *buffer);
  23. /* yeah, you know, the color_printk() in that book :-) */
  24. #define color_error(mode, fg, msg, ...) \
  25. ansi_csi_st(stderr, mode, fg, BG_BLACK); \
  26. fprintf(stderr, msg, ##__VA_ARGS__); \
  27. ansi_csi_ed(stderr); \
  28. int generic_error(char *msg, char *line, int lno) {
  29. char *buffer = strndup(line, line_eof - line + 1);
  30. color_error(MODE_HIGHLIGHT, FG_WHITE, "tripl: %s:%d: ", fname, lno);
  31. color_error(MODE_HIGHLIGHT, FG_RED, "error: ");
  32. fprintf(stderr, "%s\n", msg);
  33. ge_dump_line(lno, buffer);
  34. free(buffer);
  35. return 1;
  36. }
  37. static int ge_dump_line(int ln, char *buffer) {
  38. while (*buffer && *buffer++ == '\n')
  39. ;
  40. fprintf(stderr, "%5d | %s\n", ln, buffer);
  41. return 0;
  42. }