]> git.sur5r.net Git - openldap/commitdiff
Factor out ldif entry parsing so all ldif2* tools will read the same format.
authorHallvard Furuseth <hallvard@openldap.org>
Fri, 6 Aug 1999 03:01:23 +0000 (03:01 +0000)
committerHallvard Furuseth <hallvard@openldap.org>
Fri, 6 Aug 1999 03:01:23 +0000 (03:01 +0000)
Valid LDIF files are parsed the same way as before.

servers/slapd/tools/ldif2common.c
servers/slapd/tools/ldif2common.h
servers/slapd/tools/ldif2id2children.c
servers/slapd/tools/ldif2id2entry.c
servers/slapd/tools/ldif2index.c
servers/slapd/tools/ldif2ldbm.c

index 3d15698a03b3a899c5b3c03cd7c4e0efec95e907..566391a66b0289e328868e409eff56305b8a3dd1 100644 (file)
@@ -128,3 +128,72 @@ slap_ldif_init( int argc, char **argv, int tool, const char *dbtype, int mode )
                exit( EXIT_FAILURE );
        }
 }
+
+
+#define IDBUFSIZ 100                   /* enough to hold <decimal ID>\n\0 */
+
+/*
+ * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
+ */
+int
+slap_read_ldif(
+       int         *lno,               /* ptr to line number counter              */
+       char        **bufp,     /* ptr to malloced output buffer           */
+       int         *buflenp,   /* ptr to length of *bufp                  */
+       ID          *idp,       /* ptr to ID number to be read/incremented */
+       int         idout )     /* flag to begin *buf with ID number       */
+{
+       char        linebuf[IDBUFSIZ + BUFSIZ], *line;
+       ber_len_t   lcur = 0, idlen, len, linesize;
+       int         last_ch = '\n', found_entry = 0, stop;
+
+       line     = linebuf + IDBUFSIZ;
+       linesize = sizeof( linebuf ) - IDBUFSIZ;
+
+       for ( stop = feof( stdin );  !stop;  last_ch = line[len-1] ) {
+               if ( fgets( line, linesize, stdin ) == NULL ) {
+                       stop = 1;
+                       /* Add \n in case the file does not end with newline */
+                       line = "\n";
+               }
+               len = strlen( line );
+
+               if ( last_ch == '\n' ) {
+                       (*lno)++;
+
+                       if ( line[0] == '\n' ) {
+                               if ( !found_entry )
+                                       continue;
+                               break;
+                       }
+
+                       if ( !found_entry ) {
+                               /* Found a new entry */
+                               found_entry = 1;
+
+                               if ( isdigit( (unsigned char) line[0] ) ) {
+                                       *idp = atol( line );
+                                       if ( !idout )
+                                               continue;
+                               } else {
+                                       ++(*idp);
+                                       if ( idout ) {
+                                               sprintf( linebuf, "%ld\n", (long) *idp );
+                                               idlen = strlen( linebuf );
+                                               line -= idlen;
+                                               linesize += idlen;
+                                               len += idlen;
+                                               SAFEMEMCPY( line, linebuf, idlen );
+                                       }
+                               }
+                       }                       
+               }
+
+               if ( *buflenp - lcur <= len )
+                       *bufp = ch_realloc( *bufp, *buflenp += len + BUFSIZ );
+               strcpy( *bufp + lcur, line );
+               lcur += len;
+       }
+
+       return( found_entry );
+}
index e681d2073ffc2221e47232654306293860494afe..6520cf85927b4ab74e1dc865ddce5ae2d599ace3 100644 (file)
@@ -20,5 +20,7 @@ extern        int     dbnum;
 
 
 void slap_ldif_init LDAP_P(( int, char **, int, const char *, int ));
+int  slap_read_ldif LDAP_P(( int *, char **, int *, ID *, int ));
+
 
 #endif /* LDIF2COMMON_H_ */
index 64b3bcf18167b5785aa584c47eb40650cdcb95e2..3acbbcf74e7a58e6f9ff0c0d0d593a04484a61ce 100644 (file)
 int
 main( int argc, char **argv )
 {
-       int             stop;
        char            *linep, *buf;
-       char            line[BUFSIZ];
        int             lineno, elineno;
-       int             lmax, lcur;
+       int         lmax;
        ID              id;
        DBCache *db, *db2;
        Backend         *be = NULL;
@@ -54,13 +52,12 @@ main( int argc, char **argv )
        }
 
        id = 0;
-       stop = 0;
        lineno = 0;
        buf = NULL;
-       lcur = lmax = 0;
+       lmax = 0;
        vals[0] = &bv;
        vals[1] = NULL;
-       while ( ! stop ) {
+       while ( slap_read_ldif( &lineno, &buf, &lmax, &id, 0 ) ) {
                char            *type, *val, *s;
                ber_len_t               vlen;
                Datum           key, data;
@@ -68,27 +65,6 @@ main( int argc, char **argv )
                ldbm_datum_init( key );
                ldbm_datum_init( data );
 
-               if ( fgets( line, sizeof(line), stdin ) != NULL ) {
-                       int     len;
-
-                       lineno++;
-                       len = strlen( line );
-                       while ( lcur + len + 1 > lmax ) {
-                               lmax += BUFSIZ;
-                               buf = (char *) ch_realloc( buf, lmax );
-                       }
-                       strcpy( buf + lcur, line );
-                       lcur += len;
-               } else {
-                       stop = 1;
-               }
-               if ( line[0] == '\n' || stop && buf && *buf ) {
-                       if ( *buf != '\n' ) {
-                               if (isdigit((unsigned char) *buf)) {
-                                       id = atol(buf);
-                               } else {
-                                       id++;
-                               }
                                s = buf;
                                elineno = 0;
                                while ( (linep = ldif_getline( &s )) != NULL ) {
@@ -120,11 +96,6 @@ main( int argc, char **argv )
                                                exit( EXIT_FAILURE );
                                        }
                                }
-                       }
-                       *buf = '\0';
-                       lcur = 0;
-                       line[0] = '\0';
-               }
        }
        if ( buf )
                free( buf );
@@ -141,13 +112,12 @@ main( int argc, char **argv )
 
        rewind( stdin );
        id = 0;
-       stop = 0;
        buf = NULL;
        lineno = 0;
-       lcur = lmax = 0;
+       lmax = 0;
        vals[0] = &bv;
        vals[1] = NULL;
-       while ( ! stop ) {
+       while ( slap_read_ldif( &lineno, &buf, &lmax, &id, 0 ) ) {
                char    *type, *val, *s, *dn;
                ber_len_t       vlen;
                ID      pid;
@@ -157,26 +127,6 @@ main( int argc, char **argv )
                ldbm_datum_init( key );
                ldbm_datum_init( data );
 
-               if ( fgets( line, sizeof(line), stdin ) != NULL ) {
-                       int     len;
-
-                       len = strlen( line );
-                       while ( lcur + len + 1 > lmax ) {
-                               lmax += BUFSIZ;
-                               buf = (char *) ch_realloc( buf, lmax );
-                       }
-                       strcpy( buf + lcur, line );
-                       lcur += len;
-               } else {
-                       stop = 1;
-               }
-               if ( line[0] == '\n' || stop && buf && *buf ) {
-                       if ( * buf != '\n' ) {
-                               if (isdigit((unsigned char) *buf)) {
-                                       id = atol(buf);
-                               } else {
-                                       id++;
-                               }
                                s = buf;
                                while ( (linep = ldif_getline( &s )) != NULL ) {
                                        if ( ldif_parse_line( linep, &type, &val,
@@ -212,9 +162,6 @@ main( int argc, char **argv )
                                                            val ) ) {
        Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
                                                        }
-                                                       *buf = '\0';
-                                                       lcur = 0;
-                                                       line[0] = '\0';
                                                        continue;
                                                }
                                                (void) memcpy( (char *) &pid,
@@ -232,11 +179,6 @@ main( int argc, char **argv )
                                                exit( EXIT_FAILURE );
                                        }
                                }
-                       }
-                       *buf = '\0';
-                       lcur = 0;
-                       line[0] = '\0';
-               }
        }
 
 #ifdef SLAP_CLEANUP
index 3d6ed8b668c4f677b207487467a050d1ca61eb24..5f171bdea86d75c22bac66373ea3a427f302dfcf 100644 (file)
 int
 main( int argc, char **argv )
 {
-       int             stop;
        char            *buf;
-       char            line[BUFSIZ], idbuf[BUFSIZ];
-       int             lmax, lcur;
+       int         lineno;
+       char        line[BUFSIZ];
+       int         lmax;
        ID              id;
        ID              maxid;
        DBCache *db;
@@ -52,72 +52,28 @@ main( int argc, char **argv )
 
        id = 0;
        maxid = 0;
-       stop = 0;
        buf = NULL;
-       lcur = lmax = 0;
+       lmax = 0;
        vals[0] = &bv;
        vals[1] = NULL;
-       while ( ! stop ) {
+       while ( slap_read_ldif( &lineno, &buf, &lmax, &id, 1 ) ) {
                Datum           key, data;
 
                ldbm_datum_init( key );
                ldbm_datum_init( data );
 
-               if ( fgets( line, sizeof(line), stdin ) != NULL ) {
-                       int     len, idlen;
-
-                       len = strlen( line );
-                       if ( buf == NULL || *buf == '\0' ) {
-                               if (!isdigit((unsigned char) line[0])) {
-                                       sprintf( idbuf, "%ld\n", id + 1 );
-                                       idlen = strlen( idbuf );
-                               } else {
-                                       id = atol(line) - 1;
-                                       idlen = 0;
-                               }
-                       } else {
-                               idlen = 0;
-                       }
-
-                       while ( lcur + len + idlen + 1 > lmax ) {
-                               lmax += BUFSIZ;
-                               buf = (char *) ch_realloc( buf, lmax );
-                       }
-
-                       if ( idlen > 0 ) {
-                               strcpy( buf + lcur, idbuf );
-                               lcur += idlen;
-                       }
-                       strcpy( buf + lcur, line );
-                       lcur += len;
-               } else {
-                       stop = 1;
-               }
-               if ( line[0] == '\n' || stop && buf && *buf ) {
-                       if ( *buf != '\n' ) {
-                               int len;
-
-                               id++;
                                if ( id > maxid )
                                        maxid = id;
                                key.dptr = (char *) &id;
                                key.dsize = sizeof(ID);
                                data.dptr = buf;
-                               len = strlen(buf);
-                               if (buf[len - 1] == '\n')
-                                       buf[--len] = '\0';
-                               data.dsize = len + 1;
+                               data.dsize = strlen( buf ) + 1;
                                if ( ldbm_store( db->dbc_db, key, data,
                                    LDBM_INSERT ) != 0 ) {
                                        fputs("id2entry ldbm_store failed\n",
                                              stderr);
                                        exit( EXIT_FAILURE );
                                }
-                       }
-                       *buf = '\0';
-                       lcur = 0;
-                       line[0] = '\0';
-               }
        }
 
 #ifdef SLAP_CLEANUP
index 702a8ec4b53b1132c26b9cd923df13198b83505f..d69cf8a1ba6de351f15d8319b1c681ac9f5db969 100644 (file)
 int
 main( int argc, char **argv )
 {
-       int             i, stop;
        char            *linep, *buf, *attr;
-       char            line[BUFSIZ];
        int             lineno, elineno;
-       int             lmax, lcur, indexmask, syntaxmask;
-       unsigned long   id;
+       int         lmax;
+       int         indexmask, syntaxmask;
+       ID              id;
        Backend         *be = NULL;
        struct ldbminfo *li;
        struct berval   bv;
@@ -50,37 +49,15 @@ main( int argc, char **argv )
        }
 
        id = 0;
-       stop = 0;
        lineno = 0;
        buf = NULL;
-       lcur = lmax = 0;
+       lmax = 0;
        vals[0] = &bv;
        vals[1] = NULL;
-       while ( ! stop ) {
+       while ( slap_read_ldif( &lineno, &buf, &lmax, &id, 0 ) ) {
                char            *type, *val, *s;
                ber_len_t               vlen;
 
-               if ( fgets( line, sizeof(line), stdin ) != NULL ) {
-                       int     len;
-
-                       lineno++;
-                       len = strlen( line );
-                       while ( lcur + len + 1 > lmax ) {
-                               lmax += BUFSIZ;
-                               buf = (char *) ch_realloc( buf, lmax );
-                       }
-                       strcpy( buf + lcur, line );
-                       lcur += len;
-               } else {
-                       stop = 1;
-               }
-               if ( line[0] == '\n' || stop && buf && *buf ) {
-                       if ( *buf != '\n' ) {
-                               if (isdigit((unsigned char) *buf)) {
-                                       id = atol(buf);
-                               } else {
-                                       id++;
-                               }
                                s = buf;
                                elineno = 0;
                                while ( (linep = ldif_getline( &s )) != NULL ) {
@@ -103,10 +80,6 @@ main( int argc, char **argv )
                                                               __INDEX_ADD_OP);
                                        }
                                }
-                       }
-                       *buf = '\0';
-                       lcur = 0;
-               }
        }
 
        slap_shutdown(dbnum);
index 60f856891c9a2a6d5659d3f35625079ed0bb1e72..1af98050ff9874277ca5d5eb7a746802dcbd0417 100644 (file)
@@ -36,14 +36,14 @@ static int      nkids;
 int
 main( int argc, char **argv )
 {
-       int             i, stop;
+       int             i;
        char            *linep, *buf;
        char            *args[MAXARGS];
        char            buf2[20], buf3[20];
        char            line[BUFSIZ];
        char            cmd[MAXPATHLEN];
        int             lineno, elineno;
-       int             lmax, lcur;
+       int             lmax;
        ID              id;
        Backend         *be = NULL;
        struct ldbminfo *li;
@@ -132,33 +132,16 @@ main( int argc, char **argv )
        args[i++] = NULL;
 
        id = 0;
-       stop = 0;
        buf = NULL;
        lineno = 0;
-       lcur = lmax = 0;
+       lmax = 0;
        vals[0] = &bv;
        vals[1] = NULL;
-       while ( ! stop ) {
+       while ( slap_read_ldif( &lineno, &buf, &lmax, &id, 0 ) ) {
                char            *type, *val, *s;
                ber_len_t       vlen;
                int                     indexmask, syntaxmask;
 
-               if ( fgets( line, sizeof(line), stdin ) != NULL ) {
-                       int     len;
-
-                       lineno++;
-                       len = strlen( line );
-                       while ( lcur + len + 1 > lmax ) {
-                               lmax += BUFSIZ;
-                               buf = (char *) ch_realloc( buf, lmax );
-                       }
-                       strcpy( buf + lcur, line );
-                       lcur += len;
-               } else {
-                       stop = 1;
-               }
-               if ( line[0] == '\n' || stop && buf && *buf ) {
-                       id++;
                        s = buf;
                        elineno = 0;
                        while ( (linep = ldif_getline( &s )) != NULL ) {
@@ -187,9 +170,6 @@ main( int argc, char **argv )
                                        }
                                }
                        }
-                       *buf = '\0';
-                       lcur = 0;
-               }
        }
 
        wait4kids( -1 );