error.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. int generic_error(char *msg, char *line, int lno) {
  22. char *buffer = malloc(TRIPL_LINE_MAX);
  23. char *p;
  24. int l;
  25. for (l = 0, p = line; *p != '\n' && l < TRIPL_LINE_MAX; p++, l++)
  26. ;
  27. strncpy(buffer, line, l);
  28. buffer[l] = 0;
  29. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_WHITE, BG_BLACK);
  30. fprintf(stderr, "tripl: %s:%d: ", fname, lno);
  31. ansi_csi_ed(stderr);
  32. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_RED, BG_BLACK);
  33. fprintf(stderr, "error: ");
  34. ansi_csi_ed(stderr);
  35. fprintf(stderr, "%s\n", msg);
  36. fprintf(stderr, "%5d | ", lno);
  37. ansi_csi_st(stderr, MODE_LINE, FG_WHITE, BG_BLACK);
  38. fprintf(stderr, "%s\n", buffer);
  39. ansi_csi_ed(stderr);
  40. free(buffer);
  41. return 1;
  42. }