
/*
 *  GNOTE.C
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 *  GNOTE hellofile notefile
 *
 *  Allow the user to enter a note and append it to notefile.  See
 *  Getty. Password entry should have a '*' in front of the executable
 *  name for Getty to provide automatic stdin/stdout
 *
 */

#include <proto/all.h>
#include <stdio.h>
#include <time.h>
#include "/version.h"

IDENT(".00");

char *GetLine();
void PutDaChar();
void FlushChars();

void
main(ac, av)
char *av[];
{
    time_t t;
    char *ptr;
    FILE *fi;

    time(&t);

    if (ac < 3) {
	printf("gnote: expect 2 arguments, got %d\n", ac - 1);
	fflush(stdout);
	Delay(50*2);
	exit(1);
    }
    printf("%s %s", Ident, ctime(&t));
    if (fi = fopen(av[1], "r")) {
	char buf[128];
	while (fgets(buf, sizeof(buf), fi))
	    fputs(buf, stdout);
	fclose(fi);
    }
    fflush(stdout);

    if ((fi = fopen(av[2], "a")) == NULL) {
	puts("couldn't open note file");
	fflush(stdout);
	Delay(50*2);
	exit(1);
    }
    fprintf(fi, "**NOTE** %s", ctime(&t));

    while (ptr = GetLine()) {
	if (strcmp(ptr, ".") == 0)
	    break;
	if (strncmp(ptr, "**NOTE**", 8) == 0)
	    fprintf(fi, " ");
	fprintf(fi, "%s\n", ptr);
    }
    puts("Thank you, hanging up in 5 seconds");
    fflush(stdout);
    fclose(fi);
    Delay(50*5);
}


char *
GetLine()
{
    static char buf[256];
    static char tab[256];
    short col = 0;
    short i = 0;
    short limit = 5*60;     /*	5 minute idle timeout	*/
    short lcnt = 0;
    short c;

    for (;;) {
	c = GetDaChar();
	if (c == EOF) {
	    clrerr(stdin);
	    ++lcnt;
	    if (lcnt == limit - 30) {
		puts("\r\n(idle, hangup in 30)\r\n");
		fflush(stdout);
	    }
	    if (lcnt == limit)
		break;
	    continue;
	}
	lcnt = 0;

	switch(c) {
	case 10:
	case 13:
	    PutDaChar('\r');
	    PutDaChar('\n');
	    FlushChars();
	    buf[i] = 0;
	    return(buf);
	case 8:
	case 127:
	    if (i) {
		short n = 1;
		--i;
		if (buf[i] == 9)
		    n = tab[i];
		col -= n;
		while (n--) {
		    PutDaChar('\010');
		    PutDaChar(' ');
		    PutDaChar('\010');
		}
	    }
	    break;
	case 'x'&0x1F:
	    PutDaChar('^');
	    PutDaChar('X');
	    PutDaChar('\r');
	    PutDaChar('\n');
	    i = 0;
	    break;
	case 'r'&0x1F:
	    PutDaChar('\r');
	    PutDaChar('\n');
	    FlushChars();
	    buf[i] = 0;
	    printf("%s", buf);
	    fflush(stdout);
	    break;
	case 9:
	    if (i < sizeof(buf) - 2) {
		tab[i] = 8 - (col & 7);
		col += tab[i] - 1;  /*	because we ++ below */
	    }
	    /* fall through */
	default:
	    if (i < sizeof(buf) - 2) {
		PutDaChar(c);
		buf[i++] = c;
		++col;
	    } else {
		PutDaChar('g' & 0x1F);
	    }
	    break;
	}
    }
    return(NULL);
}

void
FlushChars()
{
    PutDaChar(256);
}

void
PutDaChar(c)
{
    static char buf[256];
    static short len;

    if (c == 256 || len == sizeof(buf)) {
	if (len)
	    write(0, buf, len);
	len = 0;
    }
    if (c != 256) {
	buf[len++] = c;
    }
}


GetDaChar()
{
    static char buf[256];
    static short idx, len;

    if (idx == len) {
	if (read(0, buf, 0) < 0)
	    FlushChars();
	idx = 0;
	len = read(0, buf, sizeof(buf));
	if (len <= 0) {
	    len = 0;
	    return(EOF);
	}
    }
    return((int)buf[idx++]);
}

