Back to index

openldap  2.4.31
parse.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2000-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENT:
00016  * This work was initially developed by Pierangelo Masarati for
00017  * inclusion in OpenLDAP Software.
00018  */
00019 
00020 #include <portable.h>
00021 
00022 #include <stdio.h>
00023 
00024 #include "rewrite-int.h"
00025 
00026 static int
00027 parse_line(
00028               char **argv,
00029               int *argc,
00030               int maxargs, 
00031               char *buf
00032 )
00033 {
00034        char *p, *begin;
00035        int in_quoted_field = 0, cnt = 0;
00036        char quote = '\0';
00037        
00038        for ( p = buf; isspace( (unsigned char) p[ 0 ] ); p++ );
00039        
00040        if ( p[ 0 ] == '#' ) {
00041               return 0;
00042        }
00043        
00044        for ( begin = p;  p[ 0 ] != '\0'; p++ ) {
00045               if ( p[ 0 ] == '\\' && p[ 1 ] != '\0' ) {
00046                      p++;
00047               } else if ( p[ 0 ] == '\'' || p[ 0 ] == '\"') {
00048                      if ( in_quoted_field && p[ 0 ] == quote ) {
00049                             in_quoted_field = 1 - in_quoted_field;
00050                             quote = '\0';
00051                             p[ 0 ] = '\0';
00052                             argv[ cnt ] = begin;
00053                             if ( ++cnt == maxargs ) {
00054                                    *argc = cnt;
00055                                    return 1;
00056                             }
00057                             for ( p++; isspace( (unsigned char) p[ 0 ] ); p++ );
00058                             begin = p;
00059                             p--;
00060                             
00061                      } else if ( !in_quoted_field ) {
00062                             if ( p != begin ) {
00063                                    return -1;
00064                             }
00065                             begin++;
00066                             in_quoted_field = 1 - in_quoted_field;
00067                             quote = p[ 0 ];
00068                      }
00069               } else if ( isspace( (unsigned char) p[ 0 ] ) && !in_quoted_field ) {
00070                      p[ 0 ] = '\0';
00071                      argv[ cnt ] = begin;
00072 
00073                      if ( ++cnt == maxargs ) {
00074                             *argc = cnt;
00075                             return 1;
00076                      }
00077 
00078                      for ( p++; isspace( (unsigned char) p[ 0 ] ); p++ );
00079                      begin = p;
00080                      p--;
00081               }
00082        }
00083        
00084        *argc = cnt;
00085 
00086        return 1;
00087 }
00088 
00089 int
00090 rewrite_read( 
00091               FILE *fin,
00092               struct rewrite_info *info
00093 )
00094 {
00095        char buf[ 1024 ];
00096        char *argv[11];
00097        int argc, lineno;
00098 
00099        /* 
00100         * Empty rule at the beginning of the context
00101         */
00102 
00103        for ( lineno = 0; fgets( buf, sizeof( buf ), fin ); lineno++ ) {
00104               switch ( parse_line( argv, &argc, sizeof( argv ) - 1, buf ) ) {
00105               case -1:
00106                      return REWRITE_ERR;
00107               case 0:
00108                      break;
00109               case 1:
00110                      if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
00111                             int rc;
00112                             rc = rewrite_parse( info, "file", lineno, 
00113                                           argc, argv );
00114                             if ( rc != REWRITE_SUCCESS ) {
00115                                    return rc;
00116                             }
00117                      }
00118                      break;
00119               }
00120        }
00121 
00122        return REWRITE_SUCCESS;
00123 }
00124