* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / imap_tools.c
1 /*
2  * $Id$
3  *
4  * Utility functions for the IMAP module.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include "imap_tools.h"
13
14 /*
15  * Break a command down into tokens, taking into consideration the
16  * possibility of escaping spaces using quoted tokens
17  */
18 int imap_parameterize(char **args, char *buf) {
19         int num = 0;
20         int start = 0;
21         int i;
22         int in_quote = 0;
23         int original_len;
24
25         strcat(buf, " ");
26
27         original_len = strlen(buf);
28
29         for (i=0; i<original_len; ++i) {
30
31                 if ( (isspace(buf[i])) && (!in_quote) ) {
32                         buf[i] = 0;
33                         args[num] = &buf[start];
34                         start = i+1;
35                         if (args[num][0] == '\"') {
36                                 ++args[num];
37                                 args[num][strlen(args[num])-1] = 0;
38                         }
39                         ++num;
40                 }
41
42                 else if ( (buf[i] == '\"') && (!in_quote) ) {
43                         in_quote = 1;
44                 }
45
46                 else if ( (buf[i] == '\"') && (in_quote) ) {
47                         in_quote = 0;
48                 }
49
50         }
51
52         return(num);
53 }
54                         
55
56