list.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 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. * As a special exception, isync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "isync.h"
  27. static char *
  28. skip_string (char *s)
  29. {
  30. while (*s && *s != '"')
  31. s++;
  32. return s;
  33. }
  34. list_t *
  35. parse_list (char *s, char **end)
  36. {
  37. int level = 1;
  38. list_t *cur;
  39. list_t **list;
  40. char *b;
  41. cur = calloc (1, sizeof (list_t));
  42. while (isspace ((unsigned char) *s))
  43. s++;
  44. if (*s == '(')
  45. {
  46. /* start of list. find the end of the list */
  47. s++;
  48. b = s; /* save beginning */
  49. cur->val = LIST;
  50. while (*s)
  51. {
  52. if (*s == '(')
  53. {
  54. level++;
  55. }
  56. else if (*s == ')')
  57. {
  58. level--;
  59. if (level == 0)
  60. break;
  61. }
  62. else if (*s == '"')
  63. {
  64. s = skip_string (s + 1);
  65. if (!*s)
  66. {
  67. /* parse error */
  68. free (cur);
  69. return NULL;
  70. }
  71. }
  72. s++;
  73. }
  74. if (level != 0)
  75. {
  76. free (cur); /* parse error */
  77. return NULL;
  78. }
  79. *s++ = 0;
  80. list = &cur->child;
  81. while (*b)
  82. {
  83. *list = parse_list (b, &b);
  84. if (*list == NULL)
  85. {
  86. /* parse error */
  87. free (cur);
  88. return NULL;
  89. }
  90. while (*list)
  91. list = &(*list)->next;
  92. }
  93. }
  94. else if (*s == '"')
  95. {
  96. /* quoted string */
  97. s++;
  98. cur->val = s;
  99. s = skip_string (s);
  100. if (!*s)
  101. {
  102. /* parse error */
  103. free (cur);
  104. return NULL;
  105. }
  106. *s++ = 0;
  107. cur->val = strdup (cur->val);
  108. }
  109. else
  110. {
  111. /* atom */
  112. cur->val = s;
  113. while (*s && !isspace ((unsigned char) *s))
  114. s++;
  115. if (*s)
  116. *s++ = 0;
  117. if (strcmp ("NIL", cur->val))
  118. cur->val = strdup (cur->val);
  119. else
  120. cur->val = NIL;
  121. }
  122. if (end)
  123. *end = s;
  124. return cur;
  125. }
  126. int
  127. is_atom (list_t * list)
  128. {
  129. return (list && list->val && list->val != NIL && list->val != LIST);
  130. }
  131. int
  132. is_list (list_t * list)
  133. {
  134. return (list && list->val == LIST);
  135. }
  136. int
  137. is_nil (list_t * list)
  138. {
  139. return (list && list->val == NIL);
  140. }
  141. void
  142. free_list (list_t * list)
  143. {
  144. list_t *tmp;
  145. while (list)
  146. {
  147. tmp = list;
  148. list = list->next;
  149. if (is_list (tmp))
  150. free_list (tmp->child);
  151. else if (is_atom (tmp))
  152. free (tmp->val);
  153. free (tmp);
  154. }
  155. }
  156. #if TEST
  157. int
  158. main (int argc, char **argv)
  159. {
  160. char buf[256];
  161. list_t *list;
  162. strcpy (buf,
  163. "((compound list) atom NIL \"string with a (\" (another list))");
  164. list = parse_list (buf, 0);
  165. }
  166. #endif