ram-parse.c 40.4 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
2
 * $Id: ram-parse.c,v 0.53 2003/01/12 19:56:04 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1993-1999, 2001-2002  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
25

Per Cederqvist's avatar
Per Cederqvist committed
26
27
28
29
/*
 * ram-parse.c -- parse objects from disk file.
 */

David Byers's avatar
David Byers committed
30
31
32
33
34

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

Per Cederqvist's avatar
Per Cederqvist committed
35
36
#include <ctype.h>
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
37
38
39
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
40
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
41
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
42

43
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
44
45
46
#include "kom-types.h"
#include "ram-parse.h"
#include "log.h"
47
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
48
49
#include "server/smalloc.h"
#include "kom-errno.h"
50
#include "kom-memory.h"
51
#include "local-to-global.h"
Per Cederqvist's avatar
Per Cederqvist committed
52

53
54
static int input_format = 2;

55
56
static Success fparse_text_list(FILE *fp, Local_to_global *result);

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
void
set_input_format(int fmt)
{
    input_format = fmt;
    switch(fmt)
    {
    case 0:
    case 1:
    case 2:
        break;
    default:
        restart_kom("unknown input format selected: %d\n", fmt);
        break;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
74
/*
75
76
77
 * FIXME (bug 340): Not all functions are needed.
 * FIXME (bug 341): The method for checking errors in fparse_long is
 *                  ugly, and not all callers check for errors properly.
Per Cederqvist's avatar
Per Cederqvist committed
78
79
80
81
82
83
84
85
86
87
88
 */

#define REALLOC(ptr, size)  srealloc(ptr, size)

static int fparse_long_errors = 0;

void
fskipwhite(FILE *fp)
{
    int c;

89
    while ( (c = getc(fp)) != EOF && /* isascii(c) && */ isspace(c) )
Per Cederqvist's avatar
Per Cederqvist committed
90
91
92
93
94
	;

    ungetc(c, fp);
}

Per Cederqvist's avatar
Per Cederqvist committed
95
extern unsigned long
Per Cederqvist's avatar
Per Cederqvist committed
96
97
fparse_long(FILE *fp)
{
Per Cederqvist's avatar
Per Cederqvist committed
98
    unsigned long res = 0;
Per Cederqvist's avatar
Per Cederqvist committed
99
100
101
102
    int foo = 0;
    int c;

    fskipwhite(fp);
103
    while ( (c = getc(fp)) != EOF && /* isascii(c) && */ isdigit(c))
Per Cederqvist's avatar
Per Cederqvist committed
104
105
106
107
108
109
110
    {
	foo = 1;
	res = 10 * res + c - '0';
    }

    if ( foo == 0 )
    {
David Byers's avatar
David Byers committed
111
	kom_log("fparse_long() failed at pos %lu.\n", (unsigned long)ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	++fparse_long_errors;
    }
    
    ungetc(c, fp);

    return res;
}

extern time_t
fparse_time(FILE *fp)
{
    return fparse_long(fp);
}

126
static Success
127
128
129
130
131
132
133
134
135
136
fparse_info_0(FILE *fp, Info *info)
{
    info->conf_pres_conf = fparse_long(fp);
    info->pers_pres_conf = fparse_long(fp);
    info->motd_conf      = fparse_long(fp);
    info->kom_news_conf  = fparse_long(fp);
    info->motd_of_lyskom = fparse_long(fp);

    return OK;
}
Per Cederqvist's avatar
Per Cederqvist committed
137

138
static Success
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
fparse_info_2(FILE *fp, Info *info)
{
    info->conf_pres_conf = fparse_long(fp);
    info->pers_pres_conf = fparse_long(fp);
    info->motd_conf      = fparse_long(fp);
    info->kom_news_conf  = fparse_long(fp);
    info->motd_of_lyskom = fparse_long(fp);
    info->highest_aux_no = fparse_long(fp);
    free_aux_item_list(&info->aux_item_list);
    return fparse_aux_item_list(fp, &info->aux_item_list);
}

extern Success
fparse_info(FILE *fp, Info *info)
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
156
	kom_log("fparse_info(): fparse_long_errors == %d on entry. Reset.\n",
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    switch (input_format)
    {
    case 0:
    case 1:
        return fparse_info_0(fp, info);
        break;
    case 2:
        return fparse_info_2(fp, info);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}



179
static Success
180
181
fparse_conference_2(FILE *fp,
                  Conference *result)
Per Cederqvist's avatar
Per Cederqvist committed
182
183
184
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
185
	kom_log("fparse_conference(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
186
187
188
189
190
191
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &result->name) != OK )
    {
David Byers's avatar
David Byers committed
192
	kom_log("fparse_conference(): Can't parse name.\n");
Per Cederqvist's avatar
Per Cederqvist committed
193
194
195
196
	return FAILURE;
    }

    if ( fparse_member_list(fp, &result->members) != OK
197
	|| l2g_read(fp, &result->texts) != OK
Per Cederqvist's avatar
Per Cederqvist committed
198
199
	|| fparse_conf_type(fp, &result->type) != OK )
    {
David Byers's avatar
David Byers committed
200
	kom_log("fparse_conference: file is corrupt.\n");
Per Cederqvist's avatar
Per Cederqvist committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	return FAILURE;
    }
    
    result->creation_time = fparse_time(fp);
    result->last_written = fparse_time(fp);

    result->creator = fparse_long(fp);
    result->presentation = fparse_long(fp);
    result->supervisor = fparse_long(fp);
    result->permitted_submitters = fparse_long(fp);
    result->super_conf = fparse_long(fp);
    result->msg_of_day = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
216
	kom_log("fparse_conference(): %d %s before 'nice'. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
217
	    fparse_long_errors, "fparse_long_errors");
Per Cederqvist's avatar
Per Cederqvist committed
218
219
220
221
222
223
	fparse_long_errors = 0;
	return FAILURE;
    }

    result->nice = fparse_long(fp);

224
    result->keep_commented = fparse_long(fp);
225
226
227
228
    result->expire = fparse_long(fp);
    result->highest_aux = fparse_long(fp);
    if (fparse_aux_item_list(fp, &result->aux_item_list) != OK)
        return FAILURE;
Per Cederqvist's avatar
Per Cederqvist committed
229
230
231
232
233
234
    fskipwhite(fp);

    return OK;
}


235
static Success
236
237
238
239
240
fparse_conference_0(FILE *fp,
                  Conference *result)
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
241
	kom_log("fparse_conference(): fparse_long_errors == %d on entry. Reset.\n",
242
243
244
245
246
247
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &result->name) != OK )
    {
David Byers's avatar
David Byers committed
248
	kom_log("fparse_conference(): Can't parse name.\n");
249
250
251
252
253
254
255
	return FAILURE;
    }

    if ( fparse_member_list(fp, &result->members) != OK
	|| fparse_text_list(fp, &result->texts) != OK
	|| fparse_conf_type(fp, &result->type) != OK )
    {
David Byers's avatar
David Byers committed
256
	kom_log("fparse_conference: file is corrupt.\n");
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
	return FAILURE;
    }
    
    result->creation_time = fparse_time(fp);
    result->last_written = fparse_time(fp);

    result->creator = fparse_long(fp);
    result->presentation = fparse_long(fp);
    result->supervisor = fparse_long(fp);
    result->permitted_submitters = fparse_long(fp);
    result->super_conf = fparse_long(fp);
    result->msg_of_day = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
272
	kom_log("fparse_conference(): %d %s before 'nice'. Reset.\n",
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	    fparse_long_errors, "fparse_long_errors");
	fparse_long_errors = 0;
	return FAILURE;
    }

    result->nice = fparse_long(fp);
    result->highest_aux = 0;
    result->aux_item_list.length = 0;
    result->aux_item_list.items = 0;

    fskipwhite(fp);
    return OK;
}

extern Success
fparse_conference(FILE *fp,
                  Conference *result)
{
    switch (input_format)
    {
    case 0:
    case 1:
        return fparse_conference_0(fp, result);
        break;
    case 2:
        return fparse_conference_2(fp, result);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}


308
static Success
309
310
311
312
313
314
315
fparse_person_0(FILE *fp,
                Person *person)
{
    String pwd = EMPTY_STRING;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
316
	kom_log("fparse_person(): fparse_long_errors == %d on entry. Reset.\n",
317
318
319
320
321
322
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &pwd) != OK )
    {
David Byers's avatar
David Byers committed
323
	kom_log("fparse_person(): Failed to parse password.\n");
324
325
326
327
328
329
330
331
332
333
334
335
336
	return FAILURE;
    }

    memcpy(person->pwd, pwd.string, PASSWD_LEN);
    s_clear(&pwd);
    	
    if ( fparse_string(fp, &person->username) != OK
	|| fparse_priv_bits(fp, &person->privileges) != OK
	|| fparse_personal_flags(fp, &person->flags) != OK
	|| fparse_text_list(fp, &person->created_texts) != OK
	|| fparse_mark_list(fp, &person->marks) != OK 
	|| fparse_membership_list(fp, &person->conferences) != OK )
    {
David Byers's avatar
David Byers committed
337
	kom_log("fparse_person(): parse error.\n");
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	return FAILURE;
    }

    person->last_login = fparse_time(fp);

    person->user_area = fparse_long(fp);
    person->total_time_present = fparse_long(fp);
    person->sessions = fparse_long(fp);
    person->created_lines = fparse_long(fp);
    person->created_bytes = fparse_long(fp);
    person->read_texts = fparse_long(fp);
    person->no_of_text_fetches = fparse_long(fp);
    person->created_persons = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
354
	kom_log("fparse_person(): %d %s before 'created_confs'. Reset.\n",
355
356
357
358
359
360
361
362
363
364
365
	    fparse_long_errors, "fparse_long_errors");
	fparse_long_errors = 0;
	return FAILURE;
    }

    person->created_confs = fparse_long(fp);

    fskipwhite(fp);
    return OK;
}

366
static Success
367
368
fparse_person_2(FILE *fp,
                Person *person)
Per Cederqvist's avatar
Per Cederqvist committed
369
370
371
372
373
{
    String pwd = EMPTY_STRING;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
374
	kom_log("fparse_person(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
375
376
377
378
379
380
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &pwd) != OK )
    {
David Byers's avatar
David Byers committed
381
	kom_log("fparse_person(): Failed to parse password.\n");
Per Cederqvist's avatar
Per Cederqvist committed
382
383
384
385
386
387
388
389
390
	return FAILURE;
    }

    memcpy(person->pwd, pwd.string, PASSWD_LEN);
    s_clear(&pwd);
    	
    if ( fparse_string(fp, &person->username) != OK
	|| fparse_priv_bits(fp, &person->privileges) != OK
	|| fparse_personal_flags(fp, &person->flags) != OK
391
	|| l2g_read(fp, &person->created_texts) != OK
Per Cederqvist's avatar
Per Cederqvist committed
392
393
394
	|| fparse_mark_list(fp, &person->marks) != OK 
	|| fparse_membership_list(fp, &person->conferences) != OK )
    {
David Byers's avatar
David Byers committed
395
	kom_log("fparse_person(): parse error.\n");
Per Cederqvist's avatar
Per Cederqvist committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	return FAILURE;
    }

    person->last_login = fparse_time(fp);

    person->user_area = fparse_long(fp);
    person->total_time_present = fparse_long(fp);
    person->sessions = fparse_long(fp);
    person->created_lines = fparse_long(fp);
    person->created_bytes = fparse_long(fp);
    person->read_texts = fparse_long(fp);
    person->no_of_text_fetches = fparse_long(fp);
    person->created_persons = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
412
	kom_log("fparse_person(): %d %s before 'created_confs'. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
413
	    fparse_long_errors, "fparse_long_errors");
Per Cederqvist's avatar
Per Cederqvist committed
414
415
416
417
418
419
420
421
422
423
424
	fparse_long_errors = 0;
	return FAILURE;
    }

    person->created_confs = fparse_long(fp);

    fskipwhite(fp);

    return OK;
}

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
extern Success
fparse_person(FILE *fp,
              Person *result)
{
    switch(input_format)
    {
    case 0:
    case 1:
        return fparse_person_0(fp, result);
        break;
    case 2:
        return fparse_person_2(fp, result);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}

445
static Success
446
447
448
449
450
451
452
453
fparse_membership_type(FILE *fp,
                       Membership_type *result)
{
    fskipwhite(fp);
    
    result->invitation = getc(fp) != '0';
    result->passive    = getc(fp) != '0';
    result->secret     = getc(fp) != '0';
454
    result->passive_message_invert = getc(fp) != '0';
455
456
457
458
459
460
461
462
    result->reserved2  = getc(fp) != '0';
    result->reserved3  = getc(fp) != '0';
    result->reserved4  = getc(fp) != '0';
    result->reserved5  = getc(fp) != '0';

    return OK;
}

463

464
static Success
465
466
fparse_read_range_0(FILE *fp,
		    Membership *mship)
Per Cederqvist's avatar
Per Cederqvist committed
467
{
468
469
470
    Local_text_no last_text_read;
    unsigned short no_of_read;
    unsigned short i;
Per Cederqvist's avatar
Per Cederqvist committed
471

472
473
474
475
476
477
478
479
480
481
    unsigned int n;
    struct read_range *res;
    struct read_range *tail;

    Local_text_no tmp;

    last_text_read = fparse_long(fp);
    no_of_read = fparse_long(fp);

    sfree(mship->read_ranges);
482
483
484
485
486
487
488
489
490
491
492
493
    if (last_text_read == 0)
    {
	mship->read_ranges = NULL;
	mship->no_of_read_ranges = 0;
    }
    else
    {
	mship->no_of_read_ranges = 1;
	mship->read_ranges = smalloc(sizeof(mship->read_ranges[0]));
	mship->read_ranges->first_read = 1;
	mship->read_ranges->last_read = last_text_read;
    }
Per Cederqvist's avatar
Per Cederqvist committed
494

495
    if (no_of_read > 0)
Per Cederqvist's avatar
Per Cederqvist committed
496
497
498
499
500
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
501
502
	    n = mship->no_of_read_ranges;
	    tail = res = mship->read_ranges;
503
504

	    for (i = 0; i < no_of_read; i++)
Per Cederqvist's avatar
Per Cederqvist committed
505
	    {
506
507
508
509
510
511
512
513
514
		tmp = fparse_long(fp);
		if (tail != NULL && tmp == tail->last_read + 1)
		    tail->last_read = tmp;
		else
		{
		    res = REALLOC(res, ++n * sizeof(*res));
		    tail = &res[n-1];
		    tail->first_read = tail->last_read = tmp;
		}
Per Cederqvist's avatar
Per Cederqvist committed
515
516
	    }

517
518
	    mship->no_of_read_ranges = n;
	    mship->read_ranges = res;
Per Cederqvist's avatar
Per Cederqvist committed
519
520

	    fskipwhite(fp);
521
	    if (getc(fp) != '}')
Per Cederqvist's avatar
Per Cederqvist committed
522
	    {
523
                err_stat = 1;
524
		kom_errno = KOM_LDB_ERR;
525
		kom_log("fparse_read_range_0(): expected '}' at pos %lu.\n",
526
			(unsigned long)ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
527
528
529
530
531
		return FAILURE;
	    }
	    break;
	    
	case '*':
532
533
534
535
	    kom_log("fparse_read_range_0(): "
		    "empty read_ranges with %lu elements (corrected)\n",
		    (unsigned long)no_of_read);
	    mship->no_of_read_ranges = 0;
Per Cederqvist's avatar
Per Cederqvist committed
536
537
	    break;
	default:
538
	    kom_log("fparse_read_range_0(): expected '*' or '{' at pos %lu.\n",
539
		(unsigned long)ftell(fp));
540
            err_stat = 2;
541
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
542
543
544
545
546
547
548
549
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
550
	    kom_log("fparse_read_range_0(): expected '*' at pos %lu.\n",
551
		(unsigned long)ftell(fp));
552
            err_stat = 3;
553
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
554
555
556
	    return FAILURE;
	}
    }
557
558
    return OK;
}
Per Cederqvist's avatar
Per Cederqvist committed
559

560
561
562
563
564
565
566
567
568
569
static Success
fparse_membership_2(FILE *fp,
                    Membership *mship)
{
    mship->last_time_read = fparse_time(fp);

    mship->conf_no = fparse_long(fp);
    mship->priority = fparse_long(fp);
    if (fparse_read_range_0(fp, mship) != OK)
	return FAILURE;
570
571
572
573
574
    mship->added_by = fparse_long(fp);
    mship->added_at = fparse_time(fp);
    if (fparse_membership_type(fp, &mship->type) != OK)
        return FAILURE;

Per Cederqvist's avatar
Per Cederqvist committed
575
576
577
    return OK;
}

578
579
580
581
582
583
584
585
static Success
fparse_membership_0(FILE *fp,
                    Membership *mship)
{
    mship->last_time_read = fparse_time(fp);

    mship->conf_no = fparse_long(fp);
    mship->priority = fparse_long(fp);
586
587
    if (fparse_read_range_0(fp, mship) != OK)
	return FAILURE;
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

    return OK;
}

static Success
fparse_membership(FILE *fp,
                  Membership *mship)
{
    switch(input_format)
    {
    case 0:
    case 1:
        return fparse_membership_0(fp, mship);
        break;
    case 2:
        return fparse_membership_2(fp, mship);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
613
	
614
615
616
617
618
619
620
621
622
623
624
static Success
fparse_membership_list_0(FILE *fp,
                         Membership_list *result)
{
    int i;
    
    /* First free all the read_texts. */

    if ( result->confs != NULL )
    {
	for ( i = 0; i < result->no_of_confs; i++)
625
	    sfree(result->confs[ i ].read_ranges);
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    }
    
    result->no_of_confs = fparse_long(fp);

    if ( result->no_of_confs > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->confs = REALLOC(result->confs,
					  (result->no_of_confs
					   * sizeof(Membership)));
	    if ( result->confs == NULL && result->no_of_confs > 0 )
	    {
                err_stat = 0;
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }

	    for ( i = 0; i < result->no_of_confs; i++)
	    {
	        init_membership(&result->confs[ i ]);
		if ( fparse_membership_0(fp, &result->confs[i]) != OK )
		    return FAILURE;
	    }
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
656
		kom_log("fparse_membership_list(): expected '}' at pos %lu.\n",
657
658
659
660
661
662
663
664
665
666
667
668
669
		    (unsigned long)ftell(fp));
                err_stat = 4;
		kom_errno = KOM_LDB_ERR;
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->confs != NULL )
	    {
		sfree(result->confs);
		result->confs = NULL;
	    }
David Byers's avatar
David Byers committed
670
	    kom_log("%s empty list with %lu elements (corrected).\n",
671
672
673
674
675
676
		"fparse_membership_list():",
		(unsigned long)result->no_of_confs);
	    
	    result->no_of_confs = 0;
	    break;
	default:
David Byers's avatar
David Byers committed
677
	    kom_log("fparse_membership_list(): expected '*' or '{' at pos %lu.\n",
678
679
680
681
682
683
684
685
686
687
688
		(unsigned long)ftell(fp));
            err_stat = 5;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
689
	    kom_log("fparse_membership_list(): expected '*' at pos %lu.\n",
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
		(unsigned long)ftell(fp));
            err_stat = 6;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
	if ( result->confs != NULL )
	{
	    sfree(result->confs);
	    result->confs = NULL;
	}
    }
    return OK;
}

static Success
fparse_membership_list_2(FILE *fp,
                         Membership_list *result)
Per Cederqvist's avatar
Per Cederqvist committed
707
708
709
710
711
712
713
714
{
    int i;
    
    /* First free all the read_texts. */

    if ( result->confs != NULL )
    {
	for ( i = 0; i < result->no_of_confs; i++)
715
	    sfree(result->confs[ i ].read_ranges);
Per Cederqvist's avatar
Per Cederqvist committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
    }
    
    result->no_of_confs = fparse_long(fp);

    if ( result->no_of_confs > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->confs = REALLOC(result->confs,
					  (result->no_of_confs
					   * sizeof(Membership)));
	    if ( result->confs == NULL && result->no_of_confs > 0 )
	    {
731
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
732
733
734
735
736
737
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }

	    for ( i = 0; i < result->no_of_confs; i++)
	    {
738
	        init_membership(&result->confs[ i ]);
Per Cederqvist's avatar
Per Cederqvist committed
739
740
741
742
743
744
745
		if ( fparse_membership(fp, &result->confs[i]) != OK )
		    return FAILURE;
	    }
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
746
		kom_log("fparse_membership_list(): expected '}' at pos %lu.\n",
747
		    (unsigned long)ftell(fp));
748
                err_stat = 4;
749
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
750
751
752
753
754
755
756
757
758
759
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->confs != NULL )
	    {
		sfree(result->confs);
		result->confs = NULL;
	    }
David Byers's avatar
David Byers committed
760
	    kom_log("%s empty list with %lu elements (corrected).\n",
Per Cederqvist's avatar
Per Cederqvist committed
761
		"fparse_membership_list():",
Per Cederqvist's avatar
Per Cederqvist committed
762
		(unsigned long)result->no_of_confs);
Per Cederqvist's avatar
Per Cederqvist committed
763
764
765
766
	    
	    result->no_of_confs = 0;
	    break;
	default:
David Byers's avatar
David Byers committed
767
	    kom_log("fparse_membership_list(): expected '*' or '{' at pos %lu.\n",
768
		(unsigned long)ftell(fp));
769
            err_stat = 5;
770
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
771
772
773
774
775
776
777
778
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
779
	    kom_log("fparse_membership_list(): expected '*' at pos %lu.\n",
780
		(unsigned long)ftell(fp));
781
            err_stat = 6;
782
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
783
784
785
786
787
788
789
790
791
792
793
794
	    return FAILURE;
	}
	if ( result->confs != NULL )
	{
	    sfree(result->confs);
	    result->confs = NULL;
	}
    }
    return OK;
}


795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
extern Success
fparse_membership_list(FILE *fp,
                       Membership_list *result)
{
    switch(input_format)
    {
    case 0:
    case 1:
        return fparse_membership_list_0(fp, result);
        break;
    case 2:
        return fparse_membership_list_2(fp, result);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}



Per Cederqvist's avatar
Per Cederqvist committed
817
818
819
820
extern Success
fparse_conf_list(FILE *fp,
		Conf_list_old *result)
{
821
    unsigned long i;
Per Cederqvist's avatar
Per Cederqvist committed
822
823
824
825
826
827
828
829
830
831
832
833
834

    result->no_of_conf_nos = fparse_long(fp);
    if ( result->no_of_conf_nos > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->conf_nos = REALLOC(result->conf_nos,
				       (result->no_of_conf_nos
					* sizeof(Conf_no)));
	    if ( result->conf_nos == NULL )
	    {
835
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
836
837
838
839
840
841
842
843
844
845
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_conf_nos; i++)
		result->conf_nos[ i ] = fparse_long(fp);
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
846
		kom_log("fparse_conf_list(): expected '}' at pos %lu.\n",
847
		    (unsigned long)ftell(fp));
848
                err_stat = 7;
849
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
850
851
852
853
854
855
856
857
858
859
860
861
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->conf_nos != NULL )
	    {
		sfree(result->conf_nos);
		result->conf_nos = NULL;
	    }
	    break;
	default:
David Byers's avatar
David Byers committed
862
	    kom_log("fparse_conf_list(): expected '*' or '{' at pos %lu.\n",
863
		(unsigned long)ftell(fp));
864
            err_stat = 8;
865
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
866
867
868
869
870
871
872
873
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
874
	    kom_log("fparse_conf_list(): expected '*' at pos %lu.\n",
875
		(unsigned long)ftell(fp));
876
            err_stat = 9;
877
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
	    return FAILURE;
	}
	if ( result->conf_nos != NULL )
	{
	    sfree(result->conf_nos);
	    result->conf_nos = NULL;
	}
    }

    if ( result->no_of_conf_nos > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->type_of_conf = REALLOC(result->type_of_conf,
					   (result->no_of_conf_nos
					    * sizeof(Conf_type)));

	    if ( result->type_of_conf == NULL )
	    {
899
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
900
901
902
903
904
905
906
907
908
909
910
911
912
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_conf_nos; i++)
		if ( fparse_conf_type(fp, &result->type_of_conf[i]) != OK )
		{
		    return FAILURE;
		}
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
913
		kom_log("fparse_conf_list(): expected '}' at pos %lu.\n",
914
		    (unsigned long)ftell(fp));
915
                err_stat = 10;
916
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
917
918
919
920
921
922
923
924
925
926
927
928
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->type_of_conf != NULL )
	    {
		sfree(result->type_of_conf);
		result->type_of_conf = NULL;
	    }
	    break;
	default:
David Byers's avatar
David Byers committed
929
	    kom_log("fparse_conf_list(): expected '*' or '+' at pos %lu.\n",
930
		(unsigned long)ftell(fp));
931
            err_stat = 11;
932
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
933
934
935
936
937
938
939
940
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
941
	    kom_log("fparse_conf_list(): expected '*' at pos %lu.\n",
942
		(unsigned long)ftell(fp));
943
            err_stat = 12;
944
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
	    return FAILURE;
	}

	if ( result->type_of_conf != NULL )
	{
	    sfree(result->type_of_conf);
	    result->type_of_conf = NULL;
	}
    }
    return OK;
}


extern Success
fparse_mark_list(FILE *fp,
		Mark_list *result)
{
    int i;

    result->no_of_marks = fparse_long(fp);

    if ( result->no_of_marks > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->marks = REALLOC(result->marks,
				    (result->no_of_marks
				     * sizeof(Mark)));

	    if ( result->marks == NULL )
	    {
978
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
979
980
981
982
983
984
985
986
987
988
989
990
991
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_marks; i++)
		if ( fparse_mark(fp, &result->marks[ i ] ) != OK )
		{
		    return FAILURE;
		}
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
992
		kom_log("fparse_mark_list(): expected '}' at pos %lu.\n",
993
		    (unsigned long)ftell(fp));
994
                err_stat = 13;
995
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
		return FAILURE;
	    }
	    
	    break;

	case '*':
	    if ( result->marks != NULL )
	    {
		sfree(result->marks);
		result->marks = NULL;
	    }
	    break;

	default:
David Byers's avatar
David Byers committed
1010
	    kom_log("fparse_mark_list(): expected '*' or '{' at pos %lu.\n",
1011
		(unsigned long)ftell(fp));
1012
            err_stat = 14;
1013
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1014
1015
1016
1017
1018
1019
1020
1021
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
1022
	    kom_log("fparse_mark_list(): expected '*' at pos %lu.\n",
1023
		(unsigned long)ftell(fp));
1024
            err_stat = 15;
1025
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
	    return FAILURE;
	}
	if ( result->marks != NULL )
	{
	    sfree(result->marks);
	    result->marks = NULL;
	}
    }
    return OK;
}

1037
static Success
1038
1039
fparse_text_stat_2(FILE *fp,
                   Text_stat *result)
Per Cederqvist's avatar
Per Cederqvist committed
1040
1041
1042
1043
1044
1045
{
    int i;
    int c;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
1046
	kom_log("fparse_text_stat(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    result->creation_time = fparse_time(fp);

    result->author = fparse_long(fp);
    result->file_pos = fparse_long(fp);
    result->no_of_lines = fparse_long(fp);
    result->no_of_chars = fparse_long(fp);
    result->no_of_marks = fparse_long(fp);
    result->no_of_misc = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
1062
	kom_log("%s(): %d fparse_long_errors before 'misc_items'. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
1063
	    "fparse_text_stat",
Per Cederqvist's avatar
Per Cederqvist committed
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
	    fparse_long_errors);
	fparse_long_errors = 0;
	return FAILURE;
    }

    if ( result->no_of_misc > 0 )
    {
	fskipwhite(fp);
	switch( c = getc(fp) )
	{
	case '{':
	    result->misc_items = REALLOC(result->misc_items,
					 (result->no_of_misc
					  * sizeof(Misc_info)));

	    if ( result->misc_items == NULL )
	    {
1081
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_misc; i++)
		if ( fparse_misc_info(fp, &result->misc_items[ i ]) != OK )
		    return FAILURE;
	    
	    fskipwhite(fp);
	    if ( (c = getc(fp)) != '}' )
	    {
1093
		kom_log("fparse_text_stat(): expected '}' at pos %lu.\n",
1094
			(unsigned long)ftell(fp));
1095
1096
1097
		err_stat = 16;
		kom_errno = KOM_LDB_ERR;
		return FAILURE;
Per Cederqvist's avatar
Per Cederqvist committed
1098
1099
1100
1101
1102
1103
1104
	    }

	    break;

	case '@':
	case '+':
	    ungetc(c, fp);
David Byers's avatar
David Byers committed
1105
1106
	    kom_log("fparse_text_stat(): got '%c'; expected '{' or '*'\n.", c);
	    kom_log("Character ungetc'd and interpreted as a '*'. (pos %lu).\n",
1107
		(unsigned long)ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
	    /* Fall through */
	case '*':
	    if ( result->misc_items != NULL )
	    {
		sfree(result->misc_items);
		result->misc_items = NULL;
	    }
	    break;

	default:
David Byers's avatar
David Byers committed
1118
	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %lu.\n",
1119
		(unsigned long)ftell(fp));
1120
            err_stat = 17;
1121
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1122
1123
1124
1125
1126
1127
1128
1129
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( (c = getc(fp)) != '*' ) 
	{
1130
	    kom_log("fparse_text_stat(): expected '*' at pos %lu.\n",
1131
		    (unsigned long)ftell(fp));
1132
1133
1134
	    err_stat = 18;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
Per Cederqvist's avatar
Per Cederqvist committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
	}

	if ( result->misc_items != NULL )
	{
	    sfree(result->misc_items);
	    result->misc_items = NULL;
	}
    }

1144
1145
1146
1147
    result->highest_aux = fparse_long(fp);
    if (fparse_aux_item_list(fp, &result->aux_item_list) != OK)
        return FAILURE;

Per Cederqvist's avatar
Per Cederqvist committed
1148
1149
1150
    fskipwhite(fp);

    return OK;
1151
1152
}

1153
static Success
1154
1155
1156
1157
1158
1159
1160
1161
fparse_text_stat_0(FILE *fp,
                   Text_stat *result)
{
    int i;
    int c;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
1162
	kom_log("fparse_text_stat(): fparse_long_errors == %d on entry. Reset.\n",
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    result->creation_time = fparse_time(fp);

    result->author = fparse_long(fp);
    result->file_pos = fparse_long(fp);
    result->no_of_lines = fparse_long(fp);
    result->no_of_chars = fparse_long(fp);
    result->no_of_marks = fparse_long(fp);
    result->no_of_misc = fparse_long(fp);

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
1178
	kom_log("%s(): %d fparse_long_errors before 'misc_items'. Reset.\n",
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
	    "fparse_text_stat",
	    fparse_long_errors);
	fparse_long_errors = 0;
	return FAILURE;
    }

    if ( result->no_of_misc > 0 )
    {
	fskipwhite(fp);
	switch( c = getc(fp) )
	{
	case '{':
	    result->misc_items = REALLOC(result->misc_items,
					 (result->no_of_misc
					  * sizeof(Misc_info)));

	    if ( result->misc_items == NULL )
	    {
                err_stat = 0;
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_misc; i++)
		if ( fparse_misc_info(fp, &result->misc_items[ i ]) != OK )
		    return FAILURE;
	    
	    fskipwhite(fp);
	    if ( (c = getc(fp)) != '}' )
	    {
1209
		kom_log("fparse_text_stat(): expected '}' at pos %lu.\n",
1210
			(unsigned long)ftell(fp));
1211
1212
1213
		err_stat = 19;
		kom_errno = KOM_LDB_ERR;
		return FAILURE;
1214
1215
1216
1217
1218
1219
1220
	    }

	    break;

	case '@':
	case '+':
	    ungetc(c, fp);
David Byers's avatar
David Byers committed
1221
1222
	    kom_log("fparse_text_stat(): got '%c'; expected '{' or '*'\n.", c);
	    kom_log("Character ungetc'd and interpreted as a '*'. (pos %lu).\n",
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
		(unsigned long)ftell(fp));
	    /* Fall through */
	case '*':
	    if ( result->misc_items != NULL )
	    {
		sfree(result->misc_items);
		result->misc_items = NULL;
	    }
	    break;

	default:
David Byers's avatar
David Byers committed
1234
	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %lu.\n",
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
		(unsigned long)ftell(fp));
            err_stat = 20;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( (c = getc(fp)) != '*' ) 
	{
1246
	    kom_log("fparse_text_stat(): expected '*' at pos %lu.\n",
1247
		    (unsigned long)ftell(fp));
1248
1249
1250
	    err_stat = 21;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
	}

	if ( result->misc_items != NULL )
	{
	    sfree(result->misc_items);
	    result->misc_items = NULL;
	}
    }

    result->highest_aux = 0;
    result->aux_item_list.length = 0;
    result->aux_item_list.items = NULL;

    fskipwhite(fp);
    return OK;
}

extern Success
fparse_text_stat(FILE *fp,
                 Text_stat *result)
{
    switch(input_format)
    {
    case 0:
    case 1:
        return fparse_text_stat_0(fp, result);
        break;
    case 2:
        return fparse_text_stat_2(fp, result);
        break;
    default:
        restart_kom("unknown input format: %d\n", input_format);
        return FAILURE;
        break;
    }
}
Per Cederqvist's avatar
Per Cederqvist committed
1287
1288


1289
static Success
Per Cederqvist's avatar
Per Cederqvist committed
1290
fparse_text_list(FILE *fp,
1291
		Local_to_global *result)
Per Cederqvist's avatar
Per Cederqvist committed
1292
{
1293
    unsigned long i;
1294
1295
    Local_text_no lno;
    Local_text_no no_of_texts;
Per Cederqvist's avatar
Per Cederqvist committed
1296

1297
1298
1299
    lno = fparse_long(fp);
    no_of_texts = fparse_long(fp);
    l2g_clear(result);
Per Cederqvist's avatar
Per Cederqvist committed
1300

1301
    if (no_of_texts > 0)
Per Cederqvist's avatar
Per Cederqvist committed
1302
1303
1304
1305
1306
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
1307
1308
	    for ( i = 0; i < no_of_texts; i++)
		l2g_append(result, lno++, fparse_long(fp));
Per Cederqvist's avatar
Per Cederqvist committed
1309
1310
1311
1312
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
1313
		kom_log("fparse_text_list(): expected '}' at pos %lu.\n",
1314
		    (unsigned long)ftell(fp));
1315
                err_stat = 22;
1316
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1317
1318
1319
1320
1321
1322
		return FAILURE;
	    }
	    
	    break;

	default:
David Byers's avatar
David Byers committed
1323
	    kom_log("fparse_text_list(): expected '{' at pos %lu.\n",
1324
		(unsigned long)ftell(fp));
1325
            err_stat = 23;
1326
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1327
1328
1329
1330
1331
1332
1333
1334
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( getc(fp) != '*' ) 
	{
David Byers's avatar
David Byers committed
1335
	    kom_log("fparse_text_list(): expected '*' at pos %lu.\n",
1336
		(unsigned long)ftell(fp));
1337
            err_stat = 24;
1338
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1339
1340
	    return FAILURE;
	}
1341
	l2g_set_first_appendable_key(result, lno);
Per Cederqvist's avatar
Per Cederqvist committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
    }
    return OK;
}
    

extern Success
fparse_string(FILE *fp,
	     String *result)
{
Per Cederqvist's avatar
Per Cederqvist committed
1351
1352
1353
    String_size length;
   
    length = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
1354
1355
1356

    if ( getc(fp) != 'H' )
    {
David Byers's avatar
David Byers committed
1357
	kom_log("fparse_string(): expected 'H' at pos %lu.\n",
1358
	    (unsigned long)ftell(fp));
1359
        err_stat = 25;
1360
	kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1361
1362
1363
	return FAILURE;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1364
1365
    s_size_crea_str(result, length);
    
Per Cederqvist's avatar
Per Cederqvist committed
1366
1367
    if ( result->string == NULL )
    {
1368
1369
        err_stat = 0;
        kom_errno = KOM_OUT_OF_MEMORY;
Per Cederqvist's avatar
Per Cederqvist committed
1370
1371
1372
1373
	return FAILURE;
    }
    
    if ( fread(result->string, sizeof(char), result->len, fp)
David Byers's avatar
David Byers committed
1374
	!= (size_t)result->len )
Per Cederqvist's avatar
Per Cederqvist committed
1375
    {
David Byers's avatar
David Byers committed
1376
	kom_log("fparse_string(): unexpected eof at pos %lu.\n",
1377
	    (unsigned long)ftell(fp));
1378
        err_stat = 26;
1379
	kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
	return FAILURE;
    }
    
    return OK;
}

extern Success
fparse_member_list(FILE *fp,
		  Member_list *result)
{
    int i;

    result->no_of_members = fparse_long(fp);
    if ( result->no_of_members > 0 )
    {
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '{':
	    result->members = REALLOC(result->members,
				      (result->no_of_members
				       * sizeof(Member)));

	    if ( result->members == NULL )
	    {
1405
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1406
1407
1408
1409
1410
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }
	    
	    for ( i = 0; i < result->no_of_members; i++)
1411
1412
            {
                init_member(&result->members[i]);
Per Cederqvist's avatar
Per Cederqvist committed
1413
1414
1415
1416
		if ( fparse_member(fp, &result->members[ i ]) != OK )
		{
		    return FAILURE;
		}
1417
            }
Per Cederqvist's avatar
Per Cederqvist committed
1418
1419
1420
1421
1422
	    
	    
	    fskipwhite(fp);
	    if ( getc(fp) != '}' )
	    {
David Byers's avatar
David Byers committed
1423
		kom_log("fparse_member_list(): expected '}' at pos %lu.\n",
1424
		    (unsigned long)ftell(fp));
1425
                err_stat = 27;
1426
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
		return FAILURE;
	    }
	    
	    break;

	case '*':
	    if ( result->members != NULL )
	    {
		sfree(result->members);
		result->members = NULL;
	    }
	    break;

	default:
David Byers's avatar
David Byers committed
1441
	    kom_log("fparse_member_list(): expected '*' or '{' at pos %lu.\n",
1442
		(unsigned long)ftell(fp