From: Hallvard Furuseth Date: Fri, 6 Aug 1999 03:01:23 +0000 (+0000) Subject: Factor out ldif entry parsing so all ldif2* tools will read the same format. X-Git-Tag: TWEB_OL_BASE~271 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=496f9b147692658c14a494a9db337f2bc29d5eb7;p=openldap Factor out ldif entry parsing so all ldif2* tools will read the same format. Valid LDIF files are parsed the same way as before. --- diff --git a/servers/slapd/tools/ldif2common.c b/servers/slapd/tools/ldif2common.c index 3d15698a03..566391a66b 100644 --- a/servers/slapd/tools/ldif2common.c +++ b/servers/slapd/tools/ldif2common.c @@ -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 \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 ); +} diff --git a/servers/slapd/tools/ldif2common.h b/servers/slapd/tools/ldif2common.h index e681d2073f..6520cf8592 100644 --- a/servers/slapd/tools/ldif2common.h +++ b/servers/slapd/tools/ldif2common.h @@ -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_ */ diff --git a/servers/slapd/tools/ldif2id2children.c b/servers/slapd/tools/ldif2id2children.c index 64b3bcf181..3acbbcf74e 100644 --- a/servers/slapd/tools/ldif2id2children.c +++ b/servers/slapd/tools/ldif2id2children.c @@ -20,11 +20,9 @@ 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 diff --git a/servers/slapd/tools/ldif2id2entry.c b/servers/slapd/tools/ldif2id2entry.c index 3d6ed8b668..5f171bdea8 100644 --- a/servers/slapd/tools/ldif2id2entry.c +++ b/servers/slapd/tools/ldif2id2entry.c @@ -19,10 +19,10 @@ 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 diff --git a/servers/slapd/tools/ldif2index.c b/servers/slapd/tools/ldif2index.c index 702a8ec4b5..d69cf8a1ba 100644 --- a/servers/slapd/tools/ldif2index.c +++ b/servers/slapd/tools/ldif2index.c @@ -20,12 +20,11 @@ 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); diff --git a/servers/slapd/tools/ldif2ldbm.c b/servers/slapd/tools/ldif2ldbm.c index 60f856891c..1af98050ff 100644 --- a/servers/slapd/tools/ldif2ldbm.c +++ b/servers/slapd/tools/ldif2ldbm.c @@ -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 );