]> git.sur5r.net Git - openldap/blob - tests/progs/ldif-filter.c
ITS#8289 fix mod Increment with inherited attr type
[openldap] / tests / progs / ldif-filter.c
1 /* ldif-filter -- clean up LDIF testdata from stdin */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2009-2015 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/ctype.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
24 #ifdef _WIN32
25 #include <fcntl.h>
26 #endif
27
28 #define DEFAULT_SPECS "ndb=a,null=n"
29
30 typedef struct { char   *val; size_t len, alloc; } String;
31 typedef struct { String *val; size_t len, alloc; } Strings;
32
33 /* Flags and corresponding program options */
34 enum { SORT_ATTRS = 1, SORT_ENTRIES = 2, NO_OUTPUT = 4, DUMMY_FLAG = 8 };
35 static const char spec_options[] = "aen"; /* option index = log2(enum flag) */
36
37 static const char *progname = "ldif-filter";
38 static const String null_string = { NULL, 0, 0 };
39
40 static void
41 usage( void )
42 {
43         fprintf( stderr, "\
44 Usage: %s [-b backend] [-s spec[,spec]...]\n\
45 Filter standard input by first <spec> matching '[<backend>]=[a][e][n]':\n\
46   - Remove LDIF comments.\n\
47   - 'a': Sort attributes in entries.\n\
48   - 'e': Sort any entries separated by just one empty line.\n\
49   - 'n': Output nothing.\n\
50 <backend> defaults to the $BACKEND environment variable.\n\
51 Use specs '%s' if no spec on the command line applies.\n",
52                 progname, DEFAULT_SPECS );
53         exit( EXIT_FAILURE );
54 }
55
56 /* Return flags from "backend=flags" in spec; nonzero if backend found */
57 static unsigned
58 get_flags( const char *backend, const char *spec )
59 {
60         size_t len = strlen( backend );
61         unsigned flags = DUMMY_FLAG;
62         const char *tmp;
63
64         while ( '=' != *(spec += strncmp( spec, backend, len ) ? 0 : len) ) {
65                 if ( (spec = strchr( spec, ',' )) == NULL ) {
66                         return 0;
67                 }
68                 ++spec;
69         }
70         while ( *++spec && *spec != ',' ) {
71                 if ( (tmp = strchr( spec_options, *spec )) == NULL ) {
72                         usage();
73                 }
74                 flags |= 1U << (tmp - spec_options);
75         }
76         return flags;
77 }
78
79 #define APPEND(s /* String or Strings */, data, count, isString) do { \
80         size_t slen = (s)->len, salloc = (s)->alloc, sz = sizeof *(s)->val; \
81         if ( salloc <= slen + (count) ) { \
82                 (s)->alloc = salloc += salloc + ((count)|7) + 1; \
83                 (s)->val   = xrealloc( (s)->val, sz * salloc ); \
84         } \
85         memcpy( (s)->val + slen, data, sz * ((count) + !!(isString)) ); \
86         (s)->len = slen + (count); \
87 } while (0)
88
89 static void *
90 xrealloc( void *ptr, size_t len )
91 {
92         if ( (ptr = realloc( ptr, len )) == NULL ) {
93                 perror( progname );
94                 exit( EXIT_FAILURE );
95         }
96         return ptr;
97 }
98
99 static int
100 cmp( const void *s, const void *t )
101 {
102         return strcmp( ((const String *) s)->val, ((const String *) t)->val );
103 }
104
105 static void
106 sort_strings( Strings *ss, size_t offset )
107 {
108         qsort( ss->val + offset, ss->len - offset, sizeof(*ss->val), cmp );
109 }
110
111 /* Build entry ss[n] from attrs ss[n...], and free the attrs */
112 static void
113 build_entry( Strings *ss, size_t n, unsigned flags, size_t new_len )
114 {
115         String *vals = ss->val, *e = &vals[n];
116         size_t end = ss->len;
117         char *ptr;
118
119         if ( flags & SORT_ATTRS ) {
120                 sort_strings( ss, n + 1 );
121         }
122         e->val = xrealloc( e->val, e->alloc = new_len + 1 );
123         ptr = e->val + e->len;
124         e->len = new_len;
125         ss->len = ++n;
126         for ( ; n < end; free( vals[n++].val )) {
127                 ptr = strcpy( ptr, vals[n].val ) + vals[n].len;
128         }
129         assert( ptr == e->val + new_len );
130 }
131
132 /* Flush entries to stdout and free them */
133 static void
134 flush_entries( Strings *ss, const char *sep, unsigned flags )
135 {
136         size_t i, end = ss->len;
137         const char *prefix = "";
138
139         if ( flags & SORT_ENTRIES ) {
140                 sort_strings( ss, 0 );
141         }
142         for ( i = 0; i < end; i++, prefix = sep ) {
143                 if ( printf( "%s%s", prefix, ss->val[i].val ) < 0 ) {
144                         perror( progname );
145                         exit( EXIT_FAILURE );
146                 }
147                 free( ss->val[i].val );
148         }
149         ss->len = 0;
150 }
151
152 static void
153 filter_stdin( unsigned flags )
154 {
155         char line[256];
156         Strings ss = { NULL, 0, 0 };    /* entries + attrs of partial entry */
157         size_t entries = 0, attrs_totlen = 0, line_len;
158         const char *entry_sep = "\n", *sep = "";
159         int comment = 0, eof = 0, eol, prev_eol = 1;    /* flags */
160         String *s;
161
162         /* LDIF = Entries ss[..entries-1] + sep + attrs ss[entries..] + line */
163         for ( ; !eof || ss.len || *sep; prev_eol = eol ) {
164                 if ( eof || (eof = !fgets( line, sizeof(line), stdin ))) {
165                         strcpy( line, prev_eol ? "" : *sep ? sep : "\n" );
166                 }
167                 line_len = strlen( line );
168                 eol = (line_len == 0 || line[line_len - 1] == '\n');
169
170                 if ( *line == ' ' ) {           /* continuation line? */
171                         prev_eol = 0;
172                 } else if ( prev_eol ) {        /* start of logical line? */
173                         comment = (*line == '#');
174                 }
175                 if ( comment || (flags & NO_OUTPUT) ) {
176                         continue;
177                 }
178
179                 /* Collect attrs for partial entry in ss[entries...] */
180                 if ( !prev_eol && attrs_totlen != 0 ) {
181                         goto grow_attr;
182                 } else if ( line_len > (*line == '\r' ? 2 : 1) ) {
183                         APPEND( &ss, &null_string, 1, 0 ); /* new attr */
184                 grow_attr:
185                         s = &ss.val[ss.len - 1];
186                         APPEND( s, line, line_len, 1 ); /* strcat to attr */
187                         attrs_totlen += line_len;
188                         continue;
189                 }
190
191                 /* Empty line - consume sep+attrs or entries+sep */
192                 if ( attrs_totlen != 0 ) {
193                         entry_sep = sep;
194                         if ( entries == 0 )
195                                 fputs( sep, stdout );
196                         build_entry( &ss, entries++, flags, attrs_totlen );
197                         attrs_totlen = 0;
198                 } else {
199                         flush_entries( &ss, entry_sep, flags );
200                         fputs( sep, stdout );
201                         entries = 0;
202                 }
203                 sep = "\r\n" + 2 - line_len;    /* sep = copy(line) */
204         }
205
206         free( ss.val );
207 }
208
209 int
210 main( int argc, char **argv )
211 {
212         const char *backend = getenv( "BACKEND" ), *specs = "", *tmp;
213         unsigned flags;
214         int i;
215
216         if ( argc > 0 ) {
217                 progname = (tmp = strrchr( argv[0], '/' )) ? tmp+1 : argv[0];
218         }
219
220         while ( (i = getopt( argc, argv, "b:s:" )) != EOF ) {
221                 switch ( i ) {
222                 case 'b':
223                         backend = optarg;
224                         break;
225                 case 's':
226                         specs = optarg;
227                         break;
228                 default:
229                         usage();
230                 }
231         }
232         if ( optind < argc ) {
233                 usage();
234         }
235         if ( backend == NULL ) {
236                 backend = "";
237         }
238
239 #ifdef _WIN32
240         _setmode(1, _O_BINARY); /* don't convert \n to \r\n on stdout */
241 #endif
242         flags = get_flags( backend, specs );
243         filter_stdin( flags ? flags : get_flags( backend, DEFAULT_SPECS ));
244         if ( fclose( stdout ) == EOF ) {
245                 perror( progname );
246                 return EXIT_FAILURE;
247         }
248
249         return EXIT_SUCCESS;
250 }