ram-parse.c 44 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: ram-parse.c,v 0.58 2003/08/28 23:11:02 ceder Exp $
3
 * Copyright (C) 1991, 1993-1999, 2001-2003  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *
Per Cederqvist's avatar
Per Cederqvist committed
23
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
Linus Tolke Y's avatar
Linus Tolke Y committed
24
 */
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
40
#include "timewrap.h"
Per Cederqvist's avatar
Per Cederqvist committed
41
#include <sys/types.h>
42
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
43

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

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

Per Cederqvist's avatar
Per Cederqvist committed
57
/*
58 59 60
 * 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
61 62 63 64 65 66 67
 */

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

static int fparse_long_errors = 0;

void
68
fskipwhite(struct dbfile *fp)
Per Cederqvist's avatar
Per Cederqvist committed
69 70 71
{
    int c;

72
    while ( (c = dbfile_getc(fp)) != EOF && /* isascii(c) && */ isspace(c) )
Per Cederqvist's avatar
Per Cederqvist committed
73 74
	;

75 76
    if (c != EOF)
	dbfile_ungetc(fp, c);
Per Cederqvist's avatar
Per Cederqvist committed
77 78
}

Per Cederqvist's avatar
Per Cederqvist committed
79
extern unsigned long
80
fparse_long(struct dbfile *fp)
Per Cederqvist's avatar
Per Cederqvist committed
81
{
Per Cederqvist's avatar
Per Cederqvist committed
82
    unsigned long res = 0;
Per Cederqvist's avatar
Per Cederqvist committed
83 84 85 86
    int foo = 0;
    int c;

    fskipwhite(fp);
87
    while ( (c = dbfile_getc(fp)) != EOF && /* isascii(c) && */ isdigit(c))
Per Cederqvist's avatar
Per Cederqvist committed
88 89 90 91 92 93 94
    {
	foo = 1;
	res = 10 * res + c - '0';
    }

    if ( foo == 0 )
    {
95 96
	restart_kom("fparse_long() failed at pos %ld.\n",
		    dbfile_ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
97 98 99
	++fparse_long_errors;
    }
    
100
    dbfile_ungetc(fp, c);
Per Cederqvist's avatar
Per Cederqvist committed
101 102 103 104 105

    return res;
}

extern time_t
106
fparse_time(struct dbfile *fp)
Per Cederqvist's avatar
Per Cederqvist committed
107 108 109 110
{
    return fparse_long(fp);
}

111
static Success
112
fparse_info_0(struct dbfile *fp, Info *info)
113 114 115 116 117 118 119 120 121
{
    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
122

123
static Success
124
fparse_info_2(struct dbfile *fp, Info *info)
125 126 127 128 129 130 131 132 133 134 135 136
{
    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
137
fparse_info(struct dbfile *fp, Info *info)
138 139 140
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
141
	kom_log("fparse_info(): fparse_long_errors == %d on entry. Reset.\n",
142 143 144 145
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

146
    switch (fp->format)
147 148 149 150 151 152
    {
    case 0:
    case 1:
        return fparse_info_0(fp, info);
        break;
    case 2:
153
    case 3:
154 155 156
        return fparse_info_2(fp, info);
        break;
    default:
157
        restart_kom("unknown input format: %d\n", fp->format);
158 159 160 161 162 163 164
        return FAILURE;
        break;
    }
}



165
static Success
166 167
fparse_conference_2(struct dbfile *fp,
		    Conference *result)
Per Cederqvist's avatar
Per Cederqvist committed
168 169 170
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
171
	kom_log("fparse_conference(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
172 173 174 175 176 177
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &result->name) != OK )
    {
David Byers's avatar
David Byers committed
178
	kom_log("fparse_conference(): Can't parse name.\n");
Per Cederqvist's avatar
Per Cederqvist committed
179 180 181 182
	return FAILURE;
    }

    if ( fparse_member_list(fp, &result->members) != OK
183
	|| l2g_read(fp, &result->texts) != OK
Per Cederqvist's avatar
Per Cederqvist committed
184 185
	|| fparse_conf_type(fp, &result->type) != OK )
    {
David Byers's avatar
David Byers committed
186
	kom_log("fparse_conference: file is corrupt.\n");
Per Cederqvist's avatar
Per Cederqvist committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	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
202
	kom_log("fparse_conference(): %d %s before 'nice'. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
203
	    fparse_long_errors, "fparse_long_errors");
Per Cederqvist's avatar
Per Cederqvist committed
204 205 206 207 208 209
	fparse_long_errors = 0;
	return FAILURE;
    }

    result->nice = fparse_long(fp);

210
    result->keep_commented = fparse_long(fp);
211 212 213 214
    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
215 216 217 218 219 220
    fskipwhite(fp);

    return OK;
}


221
static Success
222 223
fparse_conference_0(struct dbfile *fp,
		    Conference *result)
224 225 226
{
    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
227
	kom_log("fparse_conference(): fparse_long_errors == %d on entry. Reset.\n",
228 229 230 231 232 233
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &result->name) != OK )
    {
David Byers's avatar
David Byers committed
234
	kom_log("fparse_conference(): Can't parse name.\n");
235 236 237 238 239 240 241
	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
242
	kom_log("fparse_conference: file is corrupt.\n");
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	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
258
	kom_log("fparse_conference(): %d %s before 'nice'. Reset.\n",
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	    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
274
fparse_conference(struct dbfile *fp,
275 276
                  Conference *result)
{
277
    switch (fp->format)
278 279 280 281 282 283
    {
    case 0:
    case 1:
        return fparse_conference_0(fp, result);
        break;
    case 2:
284
    case 3:
285 286 287
        return fparse_conference_2(fp, result);
        break;
    default:
288
        restart_kom("unknown input format: %d\n", fp->format);
289 290 291 292 293 294
        return FAILURE;
        break;
    }
}


295
static Success
296
fparse_person_0(struct dbfile *fp,
297 298 299 300 301 302
                Person *person)
{
    String pwd = EMPTY_STRING;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
303
	kom_log("fparse_person(): fparse_long_errors == %d on entry. Reset.\n",
304 305 306 307 308 309
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &pwd) != OK )
    {
David Byers's avatar
David Byers committed
310
	kom_log("fparse_person(): Failed to parse password.\n");
311 312 313 314 315 316 317 318 319 320 321 322 323
	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
324
	kom_log("fparse_person(): parse error.\n");
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	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
341
	kom_log("fparse_person(): %d %s before 'created_confs'. Reset.\n",
342 343 344 345 346 347 348 349 350 351 352
	    fparse_long_errors, "fparse_long_errors");
	fparse_long_errors = 0;
	return FAILURE;
    }

    person->created_confs = fparse_long(fp);

    fskipwhite(fp);
    return OK;
}

353
static Success
354
fparse_person_2(struct dbfile *fp,
355
                Person *person)
Per Cederqvist's avatar
Per Cederqvist committed
356 357 358 359 360
{
    String pwd = EMPTY_STRING;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
361
	kom_log("fparse_person(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
362 363 364 365 366 367
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    if ( fparse_string(fp, &pwd) != OK )
    {
David Byers's avatar
David Byers committed
368
	kom_log("fparse_person(): Failed to parse password.\n");
Per Cederqvist's avatar
Per Cederqvist committed
369 370 371 372 373 374 375 376 377
	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
378
	|| l2g_read(fp, &person->created_texts) != OK
Per Cederqvist's avatar
Per Cederqvist committed
379 380 381
	|| fparse_mark_list(fp, &person->marks) != OK 
	|| fparse_membership_list(fp, &person->conferences) != OK )
    {
David Byers's avatar
David Byers committed
382
	kom_log("fparse_person(): parse error.\n");
Per Cederqvist's avatar
Per Cederqvist committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	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
399
	kom_log("fparse_person(): %d %s before 'created_confs'. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
400
	    fparse_long_errors, "fparse_long_errors");
Per Cederqvist's avatar
Per Cederqvist committed
401 402 403 404 405 406 407 408 409 410 411
	fparse_long_errors = 0;
	return FAILURE;
    }

    person->created_confs = fparse_long(fp);

    fskipwhite(fp);

    return OK;
}

412
extern Success
413
fparse_person(struct dbfile *fp,
414 415
              Person *result)
{
416
    switch(fp->format)
417 418 419 420 421 422
    {
    case 0:
    case 1:
        return fparse_person_0(fp, result);
        break;
    case 2:
423
    case 3:
424 425 426
        return fparse_person_2(fp, result);
        break;
    default:
427
        restart_kom("unknown input format: %d\n", fp->format);
428 429 430 431 432
        return FAILURE;
        break;
    }
}

433
static Success
434
fparse_membership_type(struct dbfile *fp,
435 436 437 438
                       Membership_type *result)
{
    fskipwhite(fp);
    
439 440 441 442 443 444 445 446
    result->invitation = dbfile_getc(fp) != '0';
    result->passive    = dbfile_getc(fp) != '0';
    result->secret     = dbfile_getc(fp) != '0';
    result->passive_message_invert = dbfile_getc(fp) != '0';
    result->reserved2  = dbfile_getc(fp) != '0';
    result->reserved3  = dbfile_getc(fp) != '0';
    result->reserved4  = dbfile_getc(fp) != '0';
    result->reserved5  = dbfile_getc(fp) != '0';
447 448 449 450

    return OK;
}

451

452
static Success
453
fparse_read_range_0(struct dbfile *fp,
454
		    Membership *mship)
Per Cederqvist's avatar
Per Cederqvist committed
455
{
456
    Local_text_no last_text_read;
457 458
    unsigned int no_of_read;
    unsigned int i;
Per Cederqvist's avatar
Per Cederqvist committed
459

460 461 462 463 464 465 466 467 468 469
    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);
470 471 472 473 474 475 476 477 478 479 480 481
    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
482

483
    if (no_of_read > 0)
Per Cederqvist's avatar
Per Cederqvist committed
484 485
    {
	fskipwhite(fp);
486
	switch(dbfile_getc(fp))
Per Cederqvist's avatar
Per Cederqvist committed
487 488
	{
	case '{':
489 490
	    n = mship->no_of_read_ranges;
	    tail = res = mship->read_ranges;
491 492

	    for (i = 0; i < no_of_read; i++)
Per Cederqvist's avatar
Per Cederqvist committed
493
	    {
494
		tmp = fparse_long(fp);
495 496 497 498 499 500 501
		if (tail != NULL && tmp <= tail->last_read)
		{
		    kom_log("fparse_read_range_0: discarded out-of-order"
			    " local number %lu probably introduced by"
			    " bug 1121\n", (unsigned long)tmp);
		}
		else if (tail != NULL && tmp == tail->last_read + 1)
502 503 504 505 506 507 508
		    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
509 510
	    }

511 512
	    mship->no_of_read_ranges = n;
	    mship->read_ranges = res;
Per Cederqvist's avatar
Per Cederqvist committed
513 514

	    fskipwhite(fp);
515
	    if (dbfile_getc(fp) != '}')
Per Cederqvist's avatar
Per Cederqvist committed
516
	    {
517
                err_stat = 1;
518
		kom_errno = KOM_LDB_ERR;
519 520
		kom_log("fparse_read_range_0(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
521 522 523 524 525
		return FAILURE;
	    }
	    break;
	    
	case '*':
526 527 528 529
	    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
530 531
	    break;
	default:
532 533
	    kom_log("fparse_read_range_0(): expected '*' or '{' at pos %ld.\n",
		    dbfile_ftell(fp));
534
            err_stat = 2;
535
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
536 537 538 539 540 541
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
542
	if ( dbfile_getc(fp) != '*' ) 
Per Cederqvist's avatar
Per Cederqvist committed
543
	{
544 545
	    kom_log("fparse_read_range_0(): expected '*' at pos %ld.\n",
		    dbfile_ftell(fp));
546
            err_stat = 3;
547
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
548 549 550
	    return FAILURE;
	}
    }
551 552
    return OK;
}
Per Cederqvist's avatar
Per Cederqvist committed
553

554
static Success
555
fparse_membership_2(struct dbfile *fp,
556 557 558 559 560 561 562 563
                    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;
564 565 566 567 568
    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
569 570 571
    return OK;
}

572
static Success
573
fparse_membership_0(struct dbfile *fp,
574 575 576 577 578 579
                    Membership *mship)
{
    mship->last_time_read = fparse_time(fp);

    mship->conf_no = fparse_long(fp);
    mship->priority = fparse_long(fp);
580 581
    if (fparse_read_range_0(fp, mship) != OK)
	return FAILURE;
582 583 584 585 586

    return OK;
}

static Success
587
fparse_membership(struct dbfile *fp,
588 589
                  Membership *mship)
{
590
    switch(fp->format)
591 592 593 594 595 596
    {
    case 0:
    case 1:
        return fparse_membership_0(fp, mship);
        break;
    case 2:
597
    case 3:
598 599 600
        return fparse_membership_2(fp, mship);
        break;
    default:
601
        restart_kom("unknown input format: %d\n", fp->format);
602 603 604 605 606 607
        return FAILURE;
        break;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
608
	
609
static Success
610
fparse_membership_list_0(struct dbfile *fp,
611 612 613 614 615 616 617 618 619
                         Membership_list *result)
{
    int i;
    
    /* First free all the read_texts. */

    if ( result->confs != NULL )
    {
	for ( i = 0; i < result->no_of_confs; i++)
620
	    sfree(result->confs[ i ].read_ranges);
621 622 623 624 625 626 627
    }
    
    result->no_of_confs = fparse_long(fp);

    if ( result->no_of_confs > 0 )
    {
	fskipwhite(fp);
628
	switch(dbfile_getc(fp))
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	{
	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);
649
	    if ( dbfile_getc(fp) != '}' )
650
	    {
651 652
		kom_log("fparse_membership_list(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
653 654 655 656 657 658 659 660 661 662 663 664
                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
665
	    kom_log("%s empty list with %lu elements (corrected).\n",
666 667 668 669 670 671
		"fparse_membership_list():",
		(unsigned long)result->no_of_confs);
	    
	    result->no_of_confs = 0;
	    break;
	default:
672 673 674
	    kom_log("fparse_membership_list(): "
		    "expected '*' or '{' at pos %ld.\n",
		    dbfile_ftell(fp));
675 676 677 678 679 680 681 682
            err_stat = 5;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
683
	if ( dbfile_getc(fp) != '*' ) 
684
	{
685 686
	    kom_log("fparse_membership_list(): expected '*' at pos %ld.\n",
		    dbfile_ftell(fp));
687 688 689 690 691 692 693 694 695 696 697 698 699 700
            err_stat = 6;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
	if ( result->confs != NULL )
	{
	    sfree(result->confs);
	    result->confs = NULL;
	}
    }
    return OK;
}

static Success
701
fparse_membership_list_2(struct dbfile *fp,
702
                         Membership_list *result)
Per Cederqvist's avatar
Per Cederqvist committed
703 704 705 706 707 708 709 710
{
    int i;
    
    /* First free all the read_texts. */

    if ( result->confs != NULL )
    {
	for ( i = 0; i < result->no_of_confs; i++)
711
	    sfree(result->confs[ i ].read_ranges);
Per Cederqvist's avatar
Per Cederqvist committed
712 713 714 715 716 717 718
    }
    
    result->no_of_confs = fparse_long(fp);

    if ( result->no_of_confs > 0 )
    {
	fskipwhite(fp);
719
	switch(dbfile_getc(fp))
Per Cederqvist's avatar
Per Cederqvist committed
720 721 722 723 724 725 726
	{
	case '{':
	    result->confs = REALLOC(result->confs,
					  (result->no_of_confs
					   * sizeof(Membership)));
	    if ( result->confs == NULL && result->no_of_confs > 0 )
	    {
727
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
728 729 730 731 732 733
		kom_errno = KOM_OUT_OF_MEMORY;
		return FAILURE;
	    }

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


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



Per Cederqvist's avatar
Per Cederqvist committed
815
extern Success
816 817
fparse_conf_list(struct dbfile *fp,
		 Conf_list_old *result)
Per Cederqvist's avatar
Per Cederqvist committed
818
{
819
    unsigned long i;
Per Cederqvist's avatar
Per Cederqvist committed
820 821 822 823 824

    result->no_of_conf_nos = fparse_long(fp);
    if ( result->no_of_conf_nos > 0 )
    {
	fskipwhite(fp);
825
	switch(dbfile_getc(fp))
Per Cederqvist's avatar
Per Cederqvist committed
826 827 828 829 830 831 832
	{
	case '{':
	    result->conf_nos = REALLOC(result->conf_nos,
				       (result->no_of_conf_nos
					* sizeof(Conf_no)));
	    if ( result->conf_nos == NULL )
	    {
833
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
834 835 836 837 838 839 840 841
		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);
842
	    if ( dbfile_getc(fp) != '}' )
Per Cederqvist's avatar
Per Cederqvist committed
843
	    {
844 845
		kom_log("fparse_conf_list(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
846
                err_stat = 7;
847
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
848 849 850 851 852 853 854 855 856 857 858 859
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->conf_nos != NULL )
	    {
		sfree(result->conf_nos);
		result->conf_nos = NULL;
	    }
	    break;
	default:
860 861
	    kom_log("fparse_conf_list(): expected '*' or '{' at pos %ld.\n",
		    dbfile_ftell(fp));
862
            err_stat = 8;
863
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
864 865 866 867 868 869
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
870
	if ( dbfile_getc(fp) != '*' ) 
Per Cederqvist's avatar
Per Cederqvist committed
871
	{
872 873
	    kom_log("fparse_conf_list(): expected '*' at pos %ld.\n",
		    dbfile_ftell(fp));
874
            err_stat = 9;
875
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
876 877 878 879 880 881 882 883 884 885 886 887
	    return FAILURE;
	}
	if ( result->conf_nos != NULL )
	{
	    sfree(result->conf_nos);
	    result->conf_nos = NULL;
	}
    }

    if ( result->no_of_conf_nos > 0 )
    {
	fskipwhite(fp);
888
	switch(dbfile_getc(fp))
Per Cederqvist's avatar
Per Cederqvist committed
889 890 891 892 893 894 895 896
	{
	case '{':
	    result->type_of_conf = REALLOC(result->type_of_conf,
					   (result->no_of_conf_nos
					    * sizeof(Conf_type)));

	    if ( result->type_of_conf == NULL )
	    {
897
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
898 899 900 901 902 903 904 905 906 907 908
		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);
909
	    if ( dbfile_getc(fp) != '}' )
Per Cederqvist's avatar
Per Cederqvist committed
910
	    {
911 912
		kom_log("fparse_conf_list(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
913
                err_stat = 10;
914
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
915 916 917 918 919 920 921 922 923 924 925 926
		return FAILURE;
	    }
	    
	    break;
	case '*':
	    if ( result->type_of_conf != NULL )
	    {
		sfree(result->type_of_conf);
		result->type_of_conf = NULL;
	    }
	    break;
	default:
927 928
	    kom_log("fparse_conf_list(): expected '*' or '+' at pos %ld.\n",
		    dbfile_ftell(fp));
929
            err_stat = 11;
930
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
931 932 933 934 935 936
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
937
	if ( dbfile_getc(fp) != '*' ) 
Per Cederqvist's avatar
Per Cederqvist committed
938
	{
939 940
	    kom_log("fparse_conf_list(): expected '*' at pos %ld.\n",
		    dbfile_ftell(fp));
941
            err_stat = 12;
942
	    kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956
	    return FAILURE;
	}

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


extern Success
957 958
fparse_mark_list(struct dbfile *fp,
		 Mark_list *result)
Per Cederqvist's avatar
Per Cederqvist committed
959 960 961 962 963 964 965 966
{
    int i;

    result->no_of_marks = fparse_long(fp);

    if ( result->no_of_marks > 0 )
    {
	fskipwhite(fp);
967
	switch(dbfile_getc(fp))
Per Cederqvist's avatar
Per Cederqvist committed
968 969 970 971 972 973 974 975
	{
	case '{':
	    result->marks = REALLOC(result->marks,
				    (result->no_of_marks
				     * sizeof(Mark)));

	    if ( result->marks == NULL )
	    {
976
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
977 978 979 980 981 982 983 984 985 986 987
		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);
988
	    if ( dbfile_getc(fp) != '}' )
Per Cederqvist's avatar
Per Cederqvist committed
989
	    {
990 991
		kom_log("fparse_mark_list(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
992
                err_stat = 13;
993
		kom_errno = KOM_LDB_ERR;
Per Cederqvist's avatar
Per Cederqvist committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
		return FAILURE;
	    }
	    
	    break;

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

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

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
static Success
fparse_text_stat_3(struct dbfile *fp,
                   Text_stat *result)
{
    int i;
    int c;

    if ( fparse_long_errors != 0 )
    {
	kom_log("fparse_text_stat(): fparse_long_errors == %d on entry. Reset.\n",
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    result->creation_time = fparse_time(fp);

    result->author = fparse_long(fp);
    result->generation = 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 )
    {
	kom_log("%s(): %d fparse_long_errors before 'misc_items' (3). Reset.\n",
	    "fparse_text_stat",
	    fparse_long_errors);
	fparse_long_errors = 0;
	return FAILURE;
    }

    if ( result->no_of_misc > 0 )
    {
	fskipwhite(fp);
	switch( c = dbfile_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 = dbfile_getc(fp)) != '}' )
	    {
		kom_log("fparse_text_stat(): expected '}' at pos %ld.\n",
			dbfile_ftell(fp));
		err_stat = 16;
		kom_errno = KOM_LDB_ERR;
		return FAILURE;
	    }

	    break;

	case '@':
	case '+':
	    dbfile_ungetc(fp, c);
	    kom_log("fparse_text_stat(): got '%c'; expected '{' or '*'\n.", c);
	    kom_log("Character ungetc'd and interpreted as a '*' at %ld.\n",
		    dbfile_ftell(fp));
	    /* Fall through */
	case '*':
	    if ( result->misc_items != NULL )
	    {
		sfree(result->misc_items);
		result->misc_items = NULL;
	    }
	    break;

	default:
	    kom_log("fparse_text_stat(): expected '*' or '}' at pos %ld.\n",
		    dbfile_ftell(fp));
            err_stat = 17;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}
    }
    else
    {
	fskipwhite(fp);
	if ( (c = dbfile_getc(fp)) != '*' ) 
	{
	    kom_log("fparse_text_stat(): expected '*' at pos %ld.\n",
		    dbfile_ftell(fp));
	    err_stat = 18;
	    kom_errno = KOM_LDB_ERR;
	    return FAILURE;
	}

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

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

    fskipwhite(fp);

    return OK;
}

1152
static Success
1153
fparse_text_stat_2(struct dbfile *fp,
1154
                   Text_stat *result)
Per Cederqvist's avatar
Per Cederqvist committed
1155 1156 1157 1158 1159 1160
{
    int i;
    int c;

    if ( fparse_long_errors != 0 )
    {
David Byers's avatar
David Byers committed
1161
	kom_log("fparse_text_stat(): fparse_long_errors == %d on entry. Reset.\n",
Per Cederqvist's avatar
Per Cederqvist committed
1162 1163 1164 1165 1166 1167 1168
	    fparse_long_errors);
	fparse_long_errors = 0;
    }

    result->creation_time = fparse_time(fp);

    result->author = fparse_long(fp);
1169
    result->generation = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1170 1171 1172 1173 1174 1175 1176 1177
    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 )
    {
1178 1179 1180
	kom_log("fparse_text_stat(): %d fparse_long_errors"
		" before 'misc_items' (2). Reset.\n",
		fparse_long_errors);
Per Cederqvist's avatar
Per Cederqvist committed
1181 1182 1183 1184 1185 1186 1187
	fparse_long_errors = 0;
	return FAILURE;
    }

    if ( result->no_of_misc > 0 )
    {
	fskipwhite(fp);
1188
	switch( c = dbfile_getc(fp) )
Per Cederqvist's avatar
Per Cederqvist committed
1189 1190 1191 1192 1193 1194 1195 1196
	{
	case '{':
	    result->misc_items = REALLOC(result->misc_items,
					 (result->no_of_misc
					  * sizeof(Misc_info)));

	    if ( result->misc_items == NULL )
	    {
1197
                err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1198 1199 1200 1201 1202 1203 1204 1205 1206
		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);
1207
	    if ( (c = dbfile_getc(fp)) != '}' )
Per Cederqvist's avatar
Per Cederqvist committed
1208
	    {
Per Cederqvist's avatar