excontext.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <cgraph/gv_ctype.h>
  17. #include <expr/exlib.h>
  18. /*
  19. * copy input token error context into buf of n chars and reset the context
  20. * end of buf returned
  21. */
  22. char*
  23. excontext(Expr_t* p, char* buf, int n)
  24. {
  25. char* s;
  26. char* t;
  27. char* e;
  28. s = buf;
  29. if (p->linep > p->line || p->linewrap)
  30. {
  31. e = buf + n - 5;
  32. if (p->linewrap)
  33. {
  34. t = p->linep + 1;
  35. while (t < &p->line[sizeof(p->line)] && gv_isspace(*t))
  36. t++;
  37. if ((n = (sizeof(p->line) - (t - (p->linep + 1))) - (e - s)) > 0)
  38. {
  39. if (n > &p->line[sizeof(p->line)] - t)
  40. t = &p->line[sizeof(p->line)];
  41. else t += n;
  42. }
  43. while (t < &p->line[sizeof(p->line)])
  44. *s++ = *t++;
  45. }
  46. t = p->line;
  47. if (p->linewrap)
  48. p->linewrap = 0;
  49. else while (t < p->linep && gv_isspace(*t))
  50. t++;
  51. if ((n = (p->linep - t) - (e - s)) > 0)
  52. t += n;
  53. while (t < p->linep)
  54. *s++ = *t++;
  55. p->linep = p->line;
  56. t = "<<< ";
  57. while ((*s = *t++))
  58. s++;
  59. }
  60. *s = 0;
  61. return s;
  62. }