ram-output.c 22 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: ram-output.c,v 0.36 1999/06/26 11:38:12 ceder Exp $
3
 * Copyright (C) 1991, 1993-1999  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 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. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30 31 32 33 34 35
/*
 * ram-output.c  -  write objects to disk.
 *
 * This is a hack. It shouldn't be used except for debugging and as a
 * temporary substitute for what Willf|r is (or should:-) be doing.
 *
 * Written by ceder 1990-07-13. Rewritten 1990-08-31.
 * Some functions rewritten for speed by Inge Wallin.
 * (It worked - now saving is twice as fast.)
 */

David Byers's avatar
David Byers committed
36 37 38 39 40

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

41
static const char *
42
rcsid = "$Id: ram-output.c,v 0.36 1999/06/26 11:38:12 ceder Exp $";
43 44
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
45

Per Cederqvist's avatar
Per Cederqvist committed
46 47
#include <stdio.h>
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
48
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
49

50
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
51
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
52
#include "ram-output.h"
Per Cederqvist's avatar
Per Cederqvist committed
53
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
54
#include "log.h"
55
#include "local-to-global.h"
Per Cederqvist's avatar
Per Cederqvist committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/* Forward declarations. */
static void foutput_aux_item_list(FILE *, const Aux_item_list *);
static void foutput_conf_type (FILE *, Conf_type);
static void foutput_mark(FILE *, Mark);
static void foutput_mark_list(FILE *, const Mark_list);
static void foutput_member(FILE *, Member);
static void foutput_member_list(FILE *, Member_list);
static void foutput_membership_list(FILE *, Membership_list);
static void foutput_misc_info(FILE *, Misc_info);
static void foutput_personal_flags(FILE *, Personal_flags);
static void foutput_priv_bits(FILE *, Priv_bits);
static void foutput_string(FILE *, String);
static void foutput_text_list(FILE *, const Local_to_global *);
static void foutput_time(FILE *, time_t);
static void foutput_ulong(unsigned long, FILE *);


74 75
static int output_format = 2;

Per Cederqvist's avatar
Per Cederqvist committed
76
void
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
set_output_format(int fmt)
{
    output_format = fmt;
    switch (fmt)
    {
    case 0:
    case 1:
    case 2:
        break;
    default:
        restart_kom("unknown output format selected: %d\n", fmt);
        break;
    }   
}

92 93
static void
foutput_info_0(FILE *fp, Info *info)
94 95 96 97 98 99 100 101 102
{
    fprintf(fp, " %lu %lu %lu %lu %lu",
	    (unsigned long)info->conf_pres_conf,
	    (unsigned long)info->pers_pres_conf,
	    (unsigned long)info->motd_conf,
	    (unsigned long)info->kom_news_conf,
	    (unsigned long)info->motd_of_lyskom);
}

103 104
static void
foutput_info_2(FILE *fp, Info *info)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
    fprintf(fp, " %lu %lu %lu %lu %lu %lu",
	    (unsigned long)info->conf_pres_conf,
	    (unsigned long)info->pers_pres_conf,
	    (unsigned long)info->motd_conf,
	    (unsigned long)info->kom_news_conf,
	    (unsigned long)info->motd_of_lyskom,
            (unsigned long)info->highest_aux_no);
    foutput_aux_item_list(fp, &info->aux_item_list);
}


void foutput_info(FILE *fp, Info *info)
{
    switch(output_format)
    {
    case 0:
    case 1:
        foutput_info_0(fp, info);
        break;
    case 2:
        foutput_info_2(fp, info);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
}


135
static void
136
foutput_person_0 (FILE *fp,
137
                  const Person *person)
138 139 140 141 142
{
    foutput_string (fp, person->username);
    foutput_priv_bits (fp, person->privileges);
    foutput_personal_flags (fp, person->flags);

143
    foutput_text_list (fp, &person->created_texts);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    foutput_mark_list (fp, person->marks);
    foutput_membership_list (fp, person->conferences);

    foutput_time(fp, person->last_login);

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu %lu %lu",
	     (unsigned long) person -> user_area,
	     (unsigned long) person -> total_time_present, /* This is not a time,
						     * but a number of seconds.
						     */
	     (unsigned long) person -> sessions,
	     (unsigned long) person -> created_lines,
	     (unsigned long) person -> created_bytes,
	     (unsigned long) person -> read_texts,
	     (unsigned long) person -> no_of_text_fetches,
	     (unsigned long) person -> created_persons,
	     (unsigned long) person -> created_confs);
}

163
static void
164 165
foutput_person_2(FILE *fp,
		 const Person *person)
Per Cederqvist's avatar
Per Cederqvist committed
166 167 168 169 170
{
    foutput_string (fp, person->username);
    foutput_priv_bits (fp, person->privileges);
    foutput_personal_flags (fp, person->flags);

David Byers's avatar
David Byers committed
171
    putc(' ', fp);
172
    l2g_write (fp, &person->created_texts);
Per Cederqvist's avatar
Per Cederqvist committed
173 174 175 176 177 178
    foutput_mark_list (fp, person->marks);
    foutput_membership_list (fp, person->conferences);

    foutput_time(fp, person->last_login);

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu %lu %lu",
Per Cederqvist's avatar
Per Cederqvist committed
179 180
	     (unsigned long) person -> user_area,
	     (unsigned long) person -> total_time_present, /* This is not a time,
Per Cederqvist's avatar
Per Cederqvist committed
181 182
						     * but a number of seconds.
						     */
Per Cederqvist's avatar
Per Cederqvist committed
183 184 185 186 187 188 189
	     (unsigned long) person -> sessions,
	     (unsigned long) person -> created_lines,
	     (unsigned long) person -> created_bytes,
	     (unsigned long) person -> read_texts,
	     (unsigned long) person -> no_of_text_fetches,
	     (unsigned long) person -> created_persons,
	     (unsigned long) person -> created_confs);
Per Cederqvist's avatar
Per Cederqvist committed
190 191
}

192
extern void
193
foutput_person (FILE *fp,
194
                const Person *person)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
    switch(output_format)
    {
    case 0:
    case 1:
        foutput_person_0(fp, person);
        break;
    case 2:
        foutput_person_2(fp, person);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
}


212
static void
213 214
foutput_conference_2(FILE *fp,
		     const Conference *conf_c)
215 216 217
{
    foutput_string(fp, conf_c->name);
    foutput_member_list(fp, conf_c->members);
David Byers's avatar
David Byers committed
218
    putc(' ', fp);
219
    l2g_write(fp, &conf_c->texts);
220 221 222 223
    foutput_conf_type(fp, conf_c->type);

    foutput_time(fp, conf_c -> creation_time );
    foutput_time(fp, conf_c -> last_written );
Per Cederqvist's avatar
Per Cederqvist committed
224

225 226 227 228 229 230 231 232
    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu",
	     (unsigned long) conf_c -> creator,
	     (unsigned long) conf_c -> presentation,
	     (unsigned long) conf_c -> supervisor,
	     (unsigned long) conf_c -> permitted_submitters,
	     (unsigned long) conf_c -> super_conf,
	     (unsigned long) conf_c -> msg_of_day,
	     (unsigned long) conf_c -> nice);
233
    foutput_ulong((unsigned long) conf_c->keep_commented, fp);
234 235 236 237
    foutput_ulong((unsigned long) conf_c->expire, fp);
    foutput_ulong((unsigned long) conf_c->highest_aux, fp);
    foutput_aux_item_list(fp, &conf_c->aux_item_list);
}
Per Cederqvist's avatar
Per Cederqvist committed
238

239
static void
240
foutput_conference_1 (FILE *fp,
Per Cederqvist's avatar
Per Cederqvist committed
241 242 243 244
		    Conference *conf_c)
{
    foutput_string(fp, conf_c->name);
    foutput_member_list(fp, conf_c->members);
245
    foutput_text_list(fp, &conf_c->texts);
Per Cederqvist's avatar
Per Cederqvist committed
246 247 248 249 250 251
    foutput_conf_type(fp, conf_c->type);

    foutput_time(fp, conf_c -> creation_time );
    foutput_time(fp, conf_c -> last_written );

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu",
Per Cederqvist's avatar
Per Cederqvist committed
252 253 254 255 256 257 258
	     (unsigned long) conf_c -> creator,
	     (unsigned long) conf_c -> presentation,
	     (unsigned long) conf_c -> supervisor,
	     (unsigned long) conf_c -> permitted_submitters,
	     (unsigned long) conf_c -> super_conf,
	     (unsigned long) conf_c -> msg_of_day,
	     (unsigned long) conf_c -> nice);
Per Cederqvist's avatar
Per Cederqvist committed
259 260
}

261
static void
262
foutput_conference_0 (FILE *fp,
263
                      Conference *conf_c)
264 265 266
{
    foutput_string(fp, conf_c->name);
    foutput_member_list(fp, conf_c->members);
267
    foutput_text_list(fp, &conf_c->texts);
268
    foutput_conf_type(fp, conf_c->type);
269 270 271 272 273 274 275 276 277 278 279 280 281 282

    foutput_time(fp, conf_c -> creation_time );
    foutput_time(fp, conf_c -> last_written );

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu",
	     (unsigned long) conf_c -> creator,
	     (unsigned long) conf_c -> presentation,
	     (unsigned long) conf_c -> supervisor,
	     (unsigned long) conf_c -> permitted_submitters,
	     (unsigned long) conf_c -> super_conf,
	     (unsigned long) conf_c -> msg_of_day,
	     (unsigned long) conf_c -> nice);
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
void foutput_conference(FILE *fp,
                        Conference *conf_c)
{
    switch(output_format)
    {
    case 0:
        foutput_conference_0(fp, conf_c);
        break;
    case 1:
        foutput_conference_1(fp, conf_c);
        break;
    case 2:
        foutput_conference_2(fp, conf_c);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
304

305
static void
306 307
foutput_text_stat_0(FILE *fp,
                    Text_stat *t_stat)
Per Cederqvist's avatar
Per Cederqvist committed
308 309 310 311 312
{
    int i;

    foutput_time(fp, t_stat->creation_time);
    
Per Cederqvist's avatar
Per Cederqvist committed
313 314 315 316 317 318
    foutput_ulong((unsigned long) t_stat->author, fp);
    foutput_ulong((unsigned long) t_stat->file_pos, fp);
    foutput_ulong((unsigned long) t_stat->no_of_lines, fp);
    foutput_ulong((unsigned long) t_stat->no_of_chars, fp);
    foutput_ulong((unsigned long) t_stat->no_of_marks, fp);
    foutput_ulong((unsigned long) t_stat->no_of_misc, fp);
Per Cederqvist's avatar
Per Cederqvist committed
319 320 321 322 323 324 325 326 327 328

    if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
    {
	fputs(" {", fp);
	for ( i = 0; i < t_stat->no_of_misc; i++ )
	    foutput_misc_info(fp, t_stat->misc_items[ i ]);
	fputs(" }", fp);
    }
    else
	fputs(" *", fp);
329 330
}

331
static void
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
foutput_text_stat_2(FILE *fp,
                    Text_stat *t_stat)
{
    int i;

    foutput_time(fp, t_stat->creation_time);
    
    foutput_ulong((unsigned long) t_stat->author, fp);
    foutput_ulong((unsigned long) t_stat->file_pos, fp);
    foutput_ulong((unsigned long) t_stat->no_of_lines, fp);
    foutput_ulong((unsigned long) t_stat->no_of_chars, fp);
    foutput_ulong((unsigned long) t_stat->no_of_marks, fp);
    foutput_ulong((unsigned long) t_stat->no_of_misc, fp);

    if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
    {
	fputs(" {", fp);
	for ( i = 0; i < t_stat->no_of_misc; i++ )
	    foutput_misc_info(fp, t_stat->misc_items[ i ]);
	fputs(" }", fp);
    }
    else
	fputs(" *", fp);

    foutput_ulong((unsigned long) t_stat->highest_aux, fp);
    foutput_aux_item_list(fp, &t_stat->aux_item_list);
}

void
foutput_text_stat(FILE *fp,
                  Text_stat *t_stat)
{
    switch (output_format)
    {
    case 0:
    case 1:
        foutput_text_stat_0(fp, t_stat);
        break;
    case 2:
        foutput_text_stat_2(fp, t_stat);
        break;
    default:
        restart_kom("unknown database format: %d\n", output_format);
        break;
    }
}



381
static void
382 383 384 385 386 387 388 389
foutput_aux_flags(FILE *fp,
                  Aux_item_flags f)
{
    putc(' ', fp);
    putc(f.deleted + '0', fp);
    putc(f.inherit + '0', fp);
    putc(f.secret + '0', fp);
    putc(f.hide_creator + '0', fp);
390
    putc(f.dont_garb + '0', fp);
391 392 393 394 395
    putc(f.reserved3 + '0', fp);
    putc(f.reserved4 + '0', fp);
    putc(f.reserved5 + '0', fp);
}

David Byers's avatar
David Byers committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static void
foutput_aux_item_link(FILE *fp,
                      Aux_item_link *link)
{
    foutput_ulong((unsigned long) link->target_type, fp);
    foutput_ulong((unsigned long) link->target_item, fp);
    switch (link->target_type)
    {
    case CONF_OBJECT_TYPE:
        foutput_ulong((unsigned long) link->target_object.conf, fp);
        break;
    case TEXT_OBJECT_TYPE:
        foutput_ulong((unsigned long) link->target_object.text, fp);
        break;
    default:
411
	;
David Byers's avatar
David Byers committed
412 413 414
    }
}

415
static void
416 417 418 419 420 421 422 423 424 425
foutput_aux_item(FILE *fp,
                 Aux_item *a_item)
{
    foutput_ulong((unsigned long) a_item->aux_no, fp);
    foutput_ulong((unsigned long) a_item->tag, fp);
    foutput_ulong((unsigned long) a_item->creator, fp);
    foutput_time(fp, a_item->sent_at);
    foutput_aux_flags(fp, a_item->flags);
    foutput_ulong((unsigned long) a_item->inherit_limit, fp);
    foutput_string(fp, a_item->data);
David Byers's avatar
David Byers committed
426
    foutput_aux_item_link(fp, &a_item->linked_item);
427 428
}

429
static void
430
foutput_aux_item_list(FILE *fp,
431
		      const Aux_item_list *aux)
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
{
    int i;

    foutput_ulong((unsigned long) aux->length, fp);
    if (aux->items && aux->length > 0)
    {
        fputs(" {", fp);
        for (i = 0; i < aux->length; i++)
            foutput_aux_item(fp, &aux->items[i]);
        fputs(" }", fp);
    }
    else
        fputs(" *", fp);
}
		      

448
static void
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
foutput_membership_type(FILE *fp,
                        Membership_type type)
{
    putc(' ', fp);
    putc(type.invitation + '0', fp);
    putc(type.passive    + '0', fp);
    putc(type.secret     + '0', fp);
    putc(type.reserved1  + '0', fp);
    putc(type.reserved2  + '0', fp);
    putc(type.reserved3  + '0', fp);
    putc(type.reserved4  + '0', fp);
    putc(type.reserved5  + '0', fp);
}

static void
foutput_membership_0(FILE *fp,
		   Membership *mship)
{
    int i;
    
    foutput_time(fp, mship->last_time_read );
    
    if ( mship->read_texts == NULL && mship->no_of_read != 0 )
    {
David Byers's avatar
David Byers committed
473 474
	kom_log("%s(): no_of_read forced to 0 in a membership in %lu.\n",
                "foutput_membership", (unsigned long)mship->conf_no);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	mship->no_of_read = 0;
    }
    
    fprintf(fp, " %lu %lu %lu %lu",
	    (unsigned long)mship->conf_no,
	    (unsigned long)mship->priority,
	    (unsigned long)mship->last_text_read,
	    (unsigned long)mship->no_of_read);
    
    if ( mship->read_texts != NULL && mship->no_of_read > 0)
    {
	fprintf(fp, " {");
	for ( i = 0; i < mship->no_of_read; i++)
	    fprintf(fp, " %lu", (unsigned long)mship->read_texts[ i ]);
	
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}

static void
foutput_membership_2(FILE *fp,
                     Membership *mship)
{
    int i;
    
    foutput_time(fp, mship->last_time_read );
    
    if ( mship->read_texts == NULL && mship->no_of_read != 0 )
    {
David Byers's avatar
David Byers committed
506 507
	kom_log("%s(): no_of_read forced to 0 in a membership in %lu.\n",
                "foutput_membership", (unsigned long)mship->conf_no);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	mship->no_of_read = 0;
    }
    
    fprintf(fp, " %lu %lu %lu %lu",
	    (unsigned long)mship->conf_no,
	    (unsigned long)mship->priority,
	    (unsigned long)mship->last_text_read,
	    (unsigned long)mship->no_of_read);
    
    if ( mship->read_texts != NULL && mship->no_of_read > 0)
    {
	fprintf(fp, " {");
	for ( i = 0; i < mship->no_of_read; i++)
	    fprintf(fp, " %lu", (unsigned long)mship->read_texts[ i ]);
	
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
    fprintf(fp, " %lu", (unsigned long)mship->added_by);
    foutput_time(fp, mship->added_at);
    foutput_membership_type(fp, mship->type);
}


533
extern void
534 535 536 537 538 539
foutput_membership(FILE *fp,
                   Membership *mship)
{
    switch (output_format)
    {
    case 0:
540
    case 1:
541 542 543 544 545 546 547 548 549 550 551
        foutput_membership_0(fp, mship);
        break;
    case 2:
        foutput_membership_2(fp, mship);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
552 553


554
static void
Per Cederqvist's avatar
Per Cederqvist committed
555 556 557
foutput_string(FILE *fp,
	       String str)
{
Per Cederqvist's avatar
Per Cederqvist committed
558
    foutput_ulong((unsigned long)str.len, fp);
Per Cederqvist's avatar
Per Cederqvist committed
559 560 561 562 563 564
    putc('H', fp);

    if (str.len)
	fwrite(str.string, str.len, 1, fp);
}

565
static void
Per Cederqvist's avatar
Per Cederqvist committed
566 567 568 569 570 571 572 573 574 575
foutput_priv_bits(FILE *fp,
		  Priv_bits bits)
{
    putc(' ', fp);
    putc(bits.wheel + '0', fp);
    putc(bits.admin + '0', fp);
    putc(bits.statistic + '0', fp);
    putc(bits.create_pers + '0', fp);
    putc(bits.create_conf + '0', fp);
    putc(bits.change_name + '0', fp);
576
    putc(bits.extern_gw + '0', fp);
Per Cederqvist's avatar
Per Cederqvist committed
577 578 579 580 581 582 583 584 585 586 587
    putc(bits.flg8 + '0', fp);
    putc(bits.flg9 + '0', fp);
    putc(bits.flg10 + '0', fp);
    putc(bits.flg11 + '0', fp);
    putc(bits.flg12 + '0', fp);
    putc(bits.flg13 + '0', fp);
    putc(bits.flg14 + '0', fp);
    putc(bits.flg15 + '0', fp);
    putc(bits.flg16 + '0', fp);
}

588
static void
Per Cederqvist's avatar
Per Cederqvist committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
foutput_personal_flags(FILE *fp,
		       Personal_flags flags)
{
    putc(' ', fp);
    putc(flags.unread_is_secret + '0', fp);
    putc(flags.flg2 + '0', fp);
    putc(flags.flg3 + '0', fp);
    putc(flags.flg4 + '0', fp);
    putc(flags.flg5 + '0', fp);
    putc(flags.flg6 + '0', fp);
    putc(flags.flg7 + '0', fp);
    putc(flags.flg8 + '0', fp);
}	


604
static void
Per Cederqvist's avatar
Per Cederqvist committed
605
foutput_text_list(FILE *fp,
606
		  const Local_to_global *text_list)
Per Cederqvist's avatar
Per Cederqvist committed
607
{
608 609
    Local_text_no first;
    Local_text_no end;
Per Cederqvist's avatar
Per Cederqvist committed
610

611 612
    first = l2g_next_key(text_list, 0);
    end = l2g_first_appendable_key(text_list);
613 614 615

    if (first == 0)
	first = end;
616 617 618
    
    foutput_ulong((unsigned long)first, fp);
    foutput_ulong((unsigned long)(end - first), fp);
Per Cederqvist's avatar
Per Cederqvist committed
619

620
    if (first < end)
Per Cederqvist's avatar
Per Cederqvist committed
621 622
    {
	fputs(" {", fp);
623
	while (first < end)
624
	    foutput_ulong((unsigned long)l2g_lookup(text_list, first++), fp);
Per Cederqvist's avatar
Per Cederqvist committed
625 626 627 628 629 630 631
	fputs(" }", fp);
    }
    else
	fprintf(fp, " *");
}


632
static void
Per Cederqvist's avatar
Per Cederqvist committed
633
foutput_mark_list(FILE *fp,
634
		  const Mark_list mark_list)
Per Cederqvist's avatar
Per Cederqvist committed
635 636 637
{
    int i;

Per Cederqvist's avatar
Per Cederqvist committed
638
    fprintf(fp, " %lu", (unsigned long)mark_list.no_of_marks);
Per Cederqvist's avatar
Per Cederqvist committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652

    if ( mark_list.marks != NULL && mark_list.no_of_marks > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < mark_list.no_of_marks; i++ )
	    foutput_mark(fp, mark_list.marks[ i ]);
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
    
}


653
static void
Per Cederqvist's avatar
Per Cederqvist committed
654 655 656
foutput_mark(FILE *fp,
	     Mark mark)
{
Per Cederqvist's avatar
Per Cederqvist committed
657
    fprintf(fp, " %lu %lu", (unsigned long)mark.text_no, (unsigned long)mark.mark_type);
Per Cederqvist's avatar
Per Cederqvist committed
658 659
}

660 661 662
static void
foutput_membership_list_0 (FILE		* fp,
                           Membership_list  mlist)
Per Cederqvist's avatar
Per Cederqvist committed
663 664 665
{
    int i;
    
Per Cederqvist's avatar
Per Cederqvist committed
666
    fprintf(fp, " %lu", (unsigned long)mlist.no_of_confs);
Per Cederqvist's avatar
Per Cederqvist committed
667 668 669 670 671

    if ( mlist.confs != NULL && mlist.no_of_confs > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < mlist.no_of_confs; i++)
672
	    foutput_membership_0(fp, mlist.confs + i);
Per Cederqvist's avatar
Per Cederqvist committed
673 674 675 676 677 678
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}

679 680 681
static void
foutput_membership_list_2 (FILE		* fp,
                           Membership_list  mlist)
Per Cederqvist's avatar
Per Cederqvist committed
682 683 684
{
    int i;
    
685 686 687
    fprintf(fp, " %lu", (unsigned long)mlist.no_of_confs);

    if ( mlist.confs != NULL && mlist.no_of_confs > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
688 689
    {
	fprintf(fp, " {");
690 691
	for ( i = 0; i < mlist.no_of_confs; i++)
	    foutput_membership_2(fp, mlist.confs + i);
Per Cederqvist's avatar
Per Cederqvist committed
692 693 694 695 696 697
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}

698
static void
699 700 701 702 703 704
foutput_membership_list(FILE        * fp,
                        Membership_list mlist)
{
    switch (output_format)
    {
    case 0:
705
    case 1:
706 707 708 709 710 711 712 713 714 715 716
        foutput_membership_list_0(fp, mlist);
        break;
    case 2:
        foutput_membership_list_2(fp, mlist);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }    
}

717
static void
Per Cederqvist's avatar
Per Cederqvist committed
718
foutput_time(FILE *fp,
719
	     time_t clk)
Per Cederqvist's avatar
Per Cederqvist committed
720
{
721
    foutput_ulong((unsigned long) clk, fp);
Per Cederqvist's avatar
Per Cederqvist committed
722 723 724
}


725
static void
Per Cederqvist's avatar
Per Cederqvist committed
726 727 728 729 730
foutput_member_list(FILE *fp,
		    Member_list m_list)
{
    int i;

Per Cederqvist's avatar
Per Cederqvist committed
731
    fprintf(fp, " %lu", (unsigned long)m_list.no_of_members);
Per Cederqvist's avatar
Per Cederqvist committed
732 733 734 735 736 737 738 739 740 741 742 743
    if ( m_list.members != NULL && m_list.no_of_members > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < m_list.no_of_members; i++ )
	    foutput_member(fp, m_list.members[ i ]);
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}


744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
static void
foutput_member_0(FILE *fp,
                 Member member)
{
    fprintf(fp, " %lu", (unsigned long)member.member);
}

static void
foutput_member_2(FILE *fp,
                 Member member)
{
    fprintf(fp, " %lu %lu",
            (unsigned long)member.member,
            (unsigned long)member.added_by);
    foutput_time(fp, member.added_at);
    foutput_membership_type(fp, member.type);
}

762
static void
Per Cederqvist's avatar
Per Cederqvist committed
763 764 765
foutput_member(FILE *fp,
	       Member member)
{
766 767 768 769 770 771 772 773 774 775 776 777 778
    switch (output_format)
    {
    case 0:
    case 1:
        foutput_member_0(fp, member);
        break;
    case 2:
        foutput_member_2(fp, member);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
Per Cederqvist's avatar
Per Cederqvist committed
779 780
}

781
static void
782 783
foutput_conf_type_1(FILE *fp,
                    Conf_type type)
Per Cederqvist's avatar
Per Cederqvist committed
784 785 786 787 788 789
{
    putc(' ', fp);
    putc(type.rd_prot + '0', fp);
    putc(type.original + '0', fp);
    putc(type.secret + '0', fp);
    putc(type.letter_box + '0', fp);
790
    putc(type.allow_anon + '0', fp);
David Byers's avatar
David Byers committed
791
    putc(type.forbid_secret + '0', fp);
792 793
    putc(type.reserved2 + '0', fp);
    putc(type.reserved3 + '0', fp);
Per Cederqvist's avatar
Per Cederqvist committed
794 795
}

796
static void
797
foutput_conf_type_0(FILE *fp,
798
                    Conf_type type)
799 800 801 802 803 804 805 806
{
    putc(' ', fp);
    putc(type.rd_prot + '0', fp);
    putc(type.original + '0', fp);
    putc(type.secret + '0', fp);
    putc(type.letter_box + '0', fp);
}

807
static void
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
foutput_conf_type (FILE *fp,
                   Conf_type type)
{
    switch (output_format)
    {
    case 0:
        foutput_conf_type_0(fp, type);
        break;
    case 1:
    case 2:
        foutput_conf_type_1(fp, type);
        break;
    default:
        restart_kom("unknown database format: %d", output_format);
        break;
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
826

827
static void
Per Cederqvist's avatar
Per Cederqvist committed
828 829 830
foutput_misc_info(FILE *fp, 
		  Misc_info misc)
{
Per Cederqvist's avatar
Per Cederqvist committed
831
    foutput_ulong((unsigned long)misc.type, fp);
Per Cederqvist's avatar
Per Cederqvist committed
832 833 834 835
    
    switch(misc.type)
    {
    case recpt:
Per Cederqvist's avatar
Per Cederqvist committed
836
	foutput_ulong((unsigned long)misc.datum.recipient, fp);
Per Cederqvist's avatar
Per Cederqvist committed
837 838 839
	break;
	
    case cc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
840
	foutput_ulong((unsigned long)misc.datum.cc_recipient, fp);
Per Cederqvist's avatar
Per Cederqvist committed
841 842
	break;
	
843 844 845 846
    case bcc_recpt:
	foutput_ulong((unsigned long)misc.datum.bcc_recipient, fp);
	break;
	
Per Cederqvist's avatar
Per Cederqvist committed
847
    case loc_no:
Per Cederqvist's avatar
Per Cederqvist committed
848
	foutput_ulong((unsigned long)misc.datum.local_no, fp);
Per Cederqvist's avatar
Per Cederqvist committed
849 850 851 852 853 854 855
	break;
	
    case rec_time:
	foutput_time(fp, misc.datum.received_at);
	break;
	
    case comm_to:
Per Cederqvist's avatar
Per Cederqvist committed
856
	foutput_ulong((unsigned long)misc.datum.comment_to, fp);
Per Cederqvist's avatar
Per Cederqvist committed
857 858 859
	break;
	
    case comm_in:
Per Cederqvist's avatar
Per Cederqvist committed
860
	foutput_ulong((unsigned long)misc.datum.commented_in, fp);
Per Cederqvist's avatar
Per Cederqvist committed
861 862 863
	break;
	
    case footn_to:
Per Cederqvist's avatar
Per Cederqvist committed
864
	foutput_ulong((unsigned long)misc.datum.footnote_to, fp);
Per Cederqvist's avatar
Per Cederqvist committed
865 866 867
	break;
	
    case footn_in:
Per Cederqvist's avatar
Per Cederqvist committed
868
	foutput_ulong((unsigned long)misc.datum.footnoted_in, fp);
Per Cederqvist's avatar
Per Cederqvist committed
869 870 871
	break;
	
    case sent_by:
Per Cederqvist's avatar
Per Cederqvist committed
872
	foutput_ulong((unsigned long)misc.datum.sender, fp);
Per Cederqvist's avatar
Per Cederqvist committed
873 874 875 876 877 878 879 880 881
	break;
	
    case sent_at:
	foutput_time(fp, misc.datum.sent_at);
	break;

#ifndef COMPILE_CHECKS
    default:
#endif
882 883
    case unknown_info:
	restart_kom("prot_a_output_misc_info: Illegal misc\n");
Per Cederqvist's avatar
Per Cederqvist committed
884 885 886 887 888 889 890 891 892
    }
}


/* 
 * Output the unsigned long L in the fastest way possible to the file
 * FP. Ok, it's ugly, but it's fast (or is it?).
 */

893
static void
Per Cederqvist's avatar
Per Cederqvist committed
894
foutput_ulong (unsigned long l,
Per Cederqvist's avatar
Per Cederqvist committed
895 896
	       FILE *fp)
{
897
    static char   buf[sizeof(unsigned long) * 3 + 1];
Per Cederqvist's avatar
Per Cederqvist committed
898 899 900 901 902
    char         *cp;

    putc(' ', fp);
    if (l < 10)
	putc("0123456789"[l], fp);
903 904 905 906 907 908
    else
    {
	cp = buf + sizeof(buf);
	while (l > 0)
	{
	    *--cp = (l % 10) + '0';
Per Cederqvist's avatar
Per Cederqvist committed
909 910
	    l /= 10;
	}
911
	fwrite(cp, buf + sizeof(buf) - cp, 1, fp);
Per Cederqvist's avatar
Per Cederqvist committed
912 913
    }
}