]> git.sur5r.net Git - openldap/blob - libraries/librewrite/parse.c
More contrib cleanout
[openldap] / libraries / librewrite / parse.c
1 /******************************************************************************
2  *
3  * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
4  * All rights reserved.
5  *
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  *
10  * 1. The author is not responsible for the consequences of use of this
11  * software, no matter how awful, even if they arise from flaws in it.
12  *
13  * 2. The origin of this software must not be misrepresented, either by
14  * explicit claim or by omission.  Since few users ever read sources,
15  * credits should appear in the documentation.
16  *
17  * 3. Altered versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.  Since few users
19  * ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  *
23  ******************************************************************************/
24
25 #include <portable.h>
26
27 #include "rewrite-int.h"
28
29 static int
30 parse_line(
31                 char **argv,
32                 int *argc,
33                 int maxargs, 
34                 char *buf
35 )
36 {
37         char *p, *begin;
38         int in_quoted_field = 0, cnt = 0;
39         char quote = '\0';
40         
41         for ( p = buf; isspace( p[ 0 ] ); p++ );
42         
43         if ( p[ 0 ] == '#' ) {
44                 return 0;
45         }
46         
47         for ( begin = p;  p[ 0 ] != '\0'; p++ ) {
48                 if ( p[ 0 ] == '\\' ) {
49                         p++;
50                 } else if ( p[ 0 ] == '\'' || p[ 0 ] == '\"') {
51                         if ( in_quoted_field && p[ 0 ] == quote ) {
52                                 in_quoted_field = 1 - in_quoted_field;
53                                 quote = '\0';
54                                 p[ 0 ] = '\0';
55                                 argv[ cnt ] = begin;
56                                 if ( ++cnt == maxargs ) {
57                                         *argc = cnt;
58                                         return 1;
59                                 }
60                                 for ( p++; isspace( p[ 0 ] ); p++ );
61                                 begin = p;
62                                 p--;
63                                 
64                         } else if ( !in_quoted_field ) {
65                                 if ( p != begin ) {
66                                         return -1;
67                                 }
68                                 begin++;
69                                 in_quoted_field = 1 - in_quoted_field;
70                                 quote = p[ 0 ];
71                         }
72                 } else if ( isspace( p[ 0 ] ) && !in_quoted_field ) {
73                         p[ 0 ] = '\0';
74                         argv[ cnt ] = begin;
75
76                         if ( ++cnt == maxargs ) {
77                                 *argc = cnt;
78                                 return 1;
79                         }
80
81                         for ( p++; isspace( p[ 0 ] ); p++ );
82                         begin = p;
83                         p--;
84                 }
85         }
86         
87         *argc = cnt;
88
89         return 1;
90 }
91
92 int
93 read_rewrite( 
94                 FILE *fin,
95                 struct rewrite_info *info
96 )
97 {
98         char buf[ 1024 ];
99         char *argv[11];
100         int argc, lineno;
101
102         /* 
103          * Empty rule at the beginning of the context
104          */
105
106         for ( lineno = 0; fgets( buf, sizeof( buf ), fin ); lineno++ ) {
107                 switch ( parse_line( argv, &argc, sizeof( argv ) - 1, buf ) ) {
108                 case -1:
109                         return REWRITE_ERR;
110                 case 0:
111                         break;
112                 case 1:
113                         if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
114                                 int rc;
115                                 rc = rewrite_parse( info, "file", lineno, 
116                                                 argc, argv );
117                                 if ( rc != REWRITE_SUCCESS ) {
118                                         return rc;
119                                 }
120                         }
121                         break;
122                 }
123         }
124
125         return REWRITE_SUCCESS;
126 }
127