list.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "isync.h"
  24. static char *
  25. skip_string (char *s)
  26. {
  27. while (*s && *s != '"')
  28. s++;
  29. return s;
  30. }
  31. list_t *
  32. parse_list (char *s, char **end)
  33. {
  34. int level = 1;
  35. list_t *cur;
  36. list_t **list;
  37. char *b;
  38. cur = calloc (1, sizeof (list_t));
  39. while (isspace ((unsigned char) *s))
  40. s++;
  41. if (*s == '(')
  42. {
  43. /* start of list. find the end of the list */
  44. s++;
  45. b = s; /* save beginning */
  46. cur->val = LIST;
  47. while (*s)
  48. {
  49. if (*s == '(')
  50. {
  51. level++;
  52. }
  53. else if (*s == ')')
  54. {
  55. level--;
  56. if (level == 0)
  57. break;
  58. }
  59. else if (*s == '"')
  60. {
  61. s = skip_string (s + 1);
  62. if (!*s)
  63. {
  64. /* parse error */
  65. free (cur);
  66. return NULL;
  67. }
  68. }
  69. s++;
  70. }
  71. if (level != 0)
  72. {
  73. free (cur); /* parse error */
  74. return NULL;
  75. }
  76. *s++ = 0;
  77. list = &cur->child;
  78. while (*b)
  79. {
  80. *list = parse_list (b, &b);
  81. if (*list == NULL)
  82. {
  83. /* parse error */
  84. free (cur);
  85. return NULL;
  86. }
  87. while (*list)
  88. list = &(*list)->next;
  89. }
  90. }
  91. else if (*s == '"')
  92. {
  93. /* quoted string */
  94. s++;
  95. cur->val = s;
  96. s = skip_string (s);
  97. if (!*s)
  98. {
  99. /* parse error */
  100. free (cur);
  101. return NULL;
  102. }
  103. *s++ = 0;
  104. cur->val = strdup (cur->val);
  105. }
  106. else
  107. {
  108. /* atom */
  109. cur->val = s;
  110. while (*s && !isspace ((unsigned char) *s))
  111. s++;
  112. if (*s)
  113. *s++ = 0;
  114. if (strcmp ("NIL", cur->val))
  115. cur->val = strdup (cur->val);
  116. else
  117. cur->val = NIL;
  118. }
  119. if (end)
  120. *end = s;
  121. return cur;
  122. }
  123. int
  124. is_atom (list_t * list)
  125. {
  126. return (list && list->val && list->val != NIL && list->val != LIST);
  127. }
  128. int
  129. is_list (list_t * list)
  130. {
  131. return (list && list->val == LIST);
  132. }
  133. int
  134. is_nil (list_t * list)
  135. {
  136. return (list && list->val == NIL);
  137. }
  138. void
  139. free_list (list_t * list)
  140. {
  141. list_t *tmp;
  142. while (list)
  143. {
  144. tmp = list;
  145. list = list->next;
  146. if (is_list (list))
  147. free_list (tmp->child);
  148. else if (is_atom (tmp))
  149. free (tmp->val);
  150. free (tmp);
  151. }
  152. }
  153. #if TEST
  154. int
  155. main (int argc, char **argv)
  156. {
  157. char buf[256];
  158. list_t *list;
  159. strcpy (buf,
  160. "((compound list) atom NIL \"string with a (\" (another list))");
  161. list = parse_list (buf, 0);
  162. }
  163. #endif