attributes.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <string.h>
  19. #include "tmux.h"
  20. const char *
  21. attributes_tostring(u_char attr)
  22. {
  23. static char buf[128];
  24. size_t len;
  25. if (attr == 0)
  26. return ("none");
  27. len = xsnprintf(buf, sizeof buf, "%s%s%s%s%s%s%s",
  28. attr & GRID_ATTR_BRIGHT ? "bright," : "",
  29. attr & GRID_ATTR_DIM ? "dim," : "",
  30. attr & GRID_ATTR_UNDERSCORE ? "underscore," : "",
  31. attr & GRID_ATTR_BLINK ? "blink," : "",
  32. attr & GRID_ATTR_REVERSE ? "reverse," : "",
  33. attr & GRID_ATTR_HIDDEN ? "hidden," : "",
  34. attr & GRID_ATTR_ITALICS ? "italics," : "");
  35. if (len > 0)
  36. buf[len - 1] = '\0';
  37. return (buf);
  38. }
  39. int
  40. attributes_fromstring(const char *str)
  41. {
  42. const char delimiters[] = " ,|";
  43. u_char attr;
  44. size_t end;
  45. if (*str == '\0' || strcspn(str, delimiters) == 0)
  46. return (-1);
  47. if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
  48. return (-1);
  49. if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
  50. return (0);
  51. attr = 0;
  52. do {
  53. end = strcspn(str, delimiters);
  54. if ((end == 6 && strncasecmp(str, "bright", end) == 0) ||
  55. (end == 4 && strncasecmp(str, "bold", end) == 0))
  56. attr |= GRID_ATTR_BRIGHT;
  57. else if (end == 3 && strncasecmp(str, "dim", end) == 0)
  58. attr |= GRID_ATTR_DIM;
  59. else if (end == 10 && strncasecmp(str, "underscore", end) == 0)
  60. attr |= GRID_ATTR_UNDERSCORE;
  61. else if (end == 5 && strncasecmp(str, "blink", end) == 0)
  62. attr |= GRID_ATTR_BLINK;
  63. else if (end == 7 && strncasecmp(str, "reverse", end) == 0)
  64. attr |= GRID_ATTR_REVERSE;
  65. else if (end == 6 && strncasecmp(str, "hidden", end) == 0)
  66. attr |= GRID_ATTR_HIDDEN;
  67. else if (end == 7 && strncasecmp(str, "italics", end) == 0)
  68. attr |= GRID_ATTR_ITALICS;
  69. else
  70. return (-1);
  71. str += end + strspn(str + end, delimiters);
  72. } while (*str != '\0');
  73. return (attr);
  74. }