/*****************************************************************************
 * FEEDBACK.C
 * WADE 5/23/89
 * This program takes a message from stdin and appends it to a public file,
 * which has WR priviledge.  There is no limitation to the number of lines
 * a message can contain.  Each message written to the file is preceeded by
 * a header containing the sender userid and a date/time stamp.
 ****************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <curses.h>
#include "pdb.h"		/* includes destination of output file */

typedef struct list {
	struct list *next_ptr;
	char    text[80];
}       COMMENT;

feedback()
{
	time_t	secs;			/* long integer defined in types.h */
	COMMENT	comment, *comment_ptr;
	COMMENT	*last_ptr, *start_ptr;
	FILE	*fileaddr, *fopen();
	int     comment_size, line_number;
	char   *ctime(), *getlogin();
	char    name[80], subject[80];
	char   *timeofday;
	char   *username;


	/* get username and time of day in "day month date time year" format */
	username = getlogin();
	time(&secs);
	timeofday = ctime(&secs);

	/* get full name of person leaving a message */
	name[0] = '\0';
	while (strlen(name) < 2) {
		printf("Enter your name: "), flush();
		fgets(name, 80, stdin);
	}

	/* get subject */
	printf("Enter Subject (optional): "), flush();
	fgets(subject, 80, stdin);

	/* display instructions */
	printf("You are in the FEEDBACK editor.  Enter a line containing only \n");
	printf("a period (.) to terminate.  At termination, each line you typed \n");
	printf("will be written to the feedback file with a header that includes\n");
	printf("your login username and date/time stamp.  Use PDB to review the\n");
	puts("feedback file.\n"), flush();

	/* initialize first node of the structure                              */
	comment_size = sizeof(comment);	/* find the size of the node */
	comment_ptr = (struct list *) malloc(comment_size);
	/* allocate first node       */
	start_ptr = comment_ptr;/* anchor first node in list */

	/* continue loop until a line consisting of only a period is input or  */
	/* storage could not be allocated. Read a maximum of 79 characters.    */
	line_number = 0;
	fgets(comment_ptr->text, 80, stdin);
	while ((strcmp(comment_ptr->text, ".\n") != 0) &&
	       (comment_ptr != NULL)) {
		line_number++;
		last_ptr = comment_ptr;	/* location of previous node */
		comment_ptr = (struct list *) malloc(comment_size);
		last_ptr->next_ptr = comment_ptr;	/* link new node to list
							 * end */
		if (comment_ptr != NULL) {	/* only if storage was found */
			comment_ptr->next_ptr = NULL;
			fgets(comment_ptr->text, 80, stdin);
		}
	}

	fileaddr = fopen(FEEDBACK_FILE, "a");
	/*
	 * if unable to open file as an append option, then wait until the
	 * file is accessible.
	 */
	while (!fileaddr)
		fileaddr = fopen(FEEDBACK_FILE, "a");

	if (comment_ptr != start_ptr) {
		fprintf(fileaddr, "Userid:  %-32.32s (%3.3d lines) %s",
			username, line_number, timeofday);
		fprintf(fileaddr, "Name:    %s", name);
		if (strlen(subject) > 1)
			fprintf(fileaddr, "Subject: %s", subject);
		strcpy(comment_ptr->text, "\n");
		comment_ptr = start_ptr;
		last_ptr = start_ptr;
	} else {
		free(comment_ptr);
		comment_ptr = NULL;
	}

	/* write text in each node to a file */
	while (comment_ptr != NULL) {
		fprintf(fileaddr, "%s", comment_ptr->text);
		comment_ptr = comment_ptr->next_ptr;
		free(last_ptr);
		last_ptr = comment_ptr;
	}

	fclose(fileaddr);
}
