Valid LDIF files are parsed the same way as before.
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 );
+}
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_ */
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;
}
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;
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 ) {
exit( EXIT_FAILURE );
}
}
- }
- *buf = '\0';
- lcur = 0;
- line[0] = '\0';
- }
}
if ( buf )
free( buf );
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;
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,
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,
exit( EXIT_FAILURE );
}
}
- }
- *buf = '\0';
- lcur = 0;
- line[0] = '\0';
- }
}
#ifdef SLAP_CLEANUP
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;
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
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;
}
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 ) {
__INDEX_ADD_OP);
}
}
- }
- *buf = '\0';
- lcur = 0;
- }
}
slap_shutdown(dbnum);
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;
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 ) {
}
}
}
- *buf = '\0';
- lcur = 0;
- }
}
wait4kids( -1 );