
/*
 * Copyright (C) 1990 by Jay Konigsberg. uucp: jak@sactoh0
 *
 * Permission to use, copy, modify, and distribute this  software  and  its
 * documentation is hereby  granted,  provided  that  the  above  copyright
 * notice appear in all copies  and that  both  the  copyright  notice  and
 * this permission notice appear in supporting documentation. This software
 * is provided "as is" without express or implied  warranty.  However,  the
 * author retains all Copyright priviliges and rights  to  renumeration  if
 * this software is sold.
 */

/*
 * addlines - add text to the text buffer. This routine
 * does double service reading from stdin or a file.
 * and handeles inserts and appends.
 */

#include "simped.h"

char **addlines(text, overflow, count, location, fd, newfile, postnews)
char **text;
char *overflow;
int  *count;
int  location;		/* the line number after which the add occures */
FILE *fd;
int  newfile;		/* newfile flag to statt in insert mode */
int  postnews;
{
int	printf();

extern	char	*getline(),
		**allocate();

char	buffer[LINELEN+2];

int	char_read_in=0,		/* characters read in from fd */
	text_entered=TRUE;	/* boolean to determin when to allocate
				   space AND the flag for EOF */

if (*count >= 0)
    ++(*count);
for(;;)
    {
    if (fd == stdin || newfile)
	{
	printf("%3d> ",location);
	overflow = getline(buffer, &text_entered, stdin, '\0', FALSE);
	}
    else
	{
	overflow = getline(buffer, &text_entered, fd, '\0', FALSE);

	/* see if a blank line is being read in. if so, add 1 sp */
	if (text_entered != EOF && buffer[0] == '\n')
	    {
	    buffer[0]=' ';
	    buffer[1]='\n';
	    buffer[2]='\0';
	    text_entered=TRUE;
	    }
	}
    if (text_entered && text_entered != EOF)
	{
	text = allocate(text, buffer, overflow, location, *count);
	char_read_in += (strlen(buffer));
	(*count)++;
	location++;
	}
    else
	break;
    }
if (postnews)
    {
    text = allocate(text, "--\n\0", overflow, *count, *count);
    ++(*count);
    }

if(fd != stdin && ! newfile)
    {
    printf("%d characters read in\n", char_read_in);
    }
if (*count >= 1)
    --(*count);
return(text);
}
