/* PbView for use with NComm V1.9 or later */
/*  Displays an NComm phonebook file in a  */
/*  very ugly and badly formatted way :-)  */
/*  Public Domain by Torkel Lodberg 1991   */

#include <stdio.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>
#include <fcntl.h>
#include <ctype.h>
#include <math.h>

#define NEW_FILETYPE_PHONE  6

struct pb_Data {		/*** V2.0-> file format ***/
    UBYTE name[41];             /* Name of this board */
    UBYTE phone[61];            /* The phonenumber(s) */
    UBYTE comment[41];          /* A comment */
    UBYTE pass_word[30];        /* Password for this board */
    UBYTE script_file[33];      /* Script filename */
    UBYTE config_file[33];      /* Config filename */
    UBYTE macro_file[33];       /* Macro filename */
    UBYTE char_set;		/* Char. set (0-12), 0 == first item in submenu etc. */
    UBYTE baud;			/* Baud rate (0-12), 0 == first item in submenu etc. */
    UBYTE data_length;		/* TRUE if seven data bits */
    UBYTE parity;		/* Parity (0-4), 0 == first item in submenu etc. */
    UBYTE stop_bits;		/* TRUE if two stop bits */
    UBYTE duplex;		/* TRUE if half duplex */
    UBYTE swap_del_bs;		/* TRUE if we should swap Del/BS */
    UBYTE protocol;		/* Protocol (0-8), 0 == first item in submenu etc. */
    long last_login;		/* Date/time stamp created with time() */
    UBYTE arexx_file[33];       /* ARexx filename */
    UBYTE future[25];           /* Reserved for future use, keep these zero'ed */
};

static int      fd = NULL;	/* File handle */

UBYTE           wb_open;	/* Started from workbench? */

void
cleanup(text)			/* Exit program cleanly */
    char           *text;
{
    if (text)
	printf("%s\n", text);
    if (fd > 0)
	close(fd);
    if (wb_open)
	Delay(100);
    exit(0);
}

void
brk()
{				/* Called by every CTRL-C / D keypress */
    printf("*** BREAK\n");
    cleanup(NULL);
}

void
main(argc, argv)		/* Main program */
    int             argc;
    char           *argv[];
{
    char            ch, answer[256], infile[256];
    struct pb_Data  data;

    wb_open = !argc;

    if (onbreak(&brk))
	cleanup(NULL);
    chkabort();

    if (wb_open || argc > 2) {
	printf("PbView for use with NComm V1.9 or later.\n"
	       "Public Domain by Torkel Lodberg 1991\n\n");
	if (!wb_open) {
	    printf("   Usage: PbConvert [phonebook]\n\n");
	    cleanup(NULL);
	}
    }

    if (argc < 2)
       strcpy(infile, "NComm:NComm.phone");
    else
       strcpy(infile, argv[1]);

    if (wb_open) {
	printf("Enter name of phonebook file (Enter=NComm:NComm.phone): ");
	gets(answer);
	if (strlen(answer))
	    strcpy(infile, answer);
        printf("\n");
    }

    if ((fd = open(infile, O_RDONLY, NULL)) == -1 || read(fd, &ch, 1) != 1)
       cleanup("Cannot open data-file");

    if (ch != NEW_FILETYPE_PHONE)
	cleanup("Error reading NComm.phone: Illegal file type");

    while ((read(fd, (char *) &data, sizeof(struct pb_Data))) == sizeof(struct pb_Data)) {
          printf("%-30s %s\n", data.name, data.phone);
    }

    cleanup(NULL);
}
