/* File: tst_1.c
** Last: Sun 21 Aug 2011 10:40:29 PM CDT 
** Prev: Sat 20 Aug 2011 12:48:59 PM CDT
** Init: Sat 20 Aug 2011 07:15:48 AM CDT
** Vers: 0r4
** Tgts: compile with ccna32
** Goal: this is a test to check puts()
** NASM: -@tst_1.mak
	-f elf
	-l tst_1.lst
	-o tst_1.o
	-Z tst_1_err.txt
	tst_1.nsm
** Link: ld --dynamic-linker /lib/ld-linux.so.2 -lc -o tst_1 tst_1.o
*/

#define FILE int
/*	Define the input line			*/
#define	LINESIZE	255
#define	LINEMAX		254	/** LINESIZE-1 **/
#define EOF		-1	/** these are changed to intenal labels _EOF **/
#define EOL		10
#define CR		10
#define FP_STD_OUT	1	/** stdin=0, stdout=1, stderr=2 **/




FILE *fp_output, *fp_input, *fp_stdout;

int chr, v1, v2, desc1;

char *c_fmt, *d_fmt, chr2;
/** char *pbufr; **/

int result; /** v.1, does work **/


char	linebufr[LINESIZE];	/** parsing buffer **/
int	ln_ndx,			/** index into buffer **/
	linecnt,
	k,			/** chr **/
	eof_flg;		/** set non-zero on final input eof **/

int resultInput, resultOutput;

main()
{
/**	int result; ** v.0, doesn't work. **/

	c_fmt = "%c";
	d_fmt = " %d";
	chr2 = '^';
	result = 1;

/**	pbufr = &linebufr[0]; **/

	puts("Testing puts.");
	if ((fp_output = fopen("mytst.txt","w")) == NULL) /* if given, open */
		{
		fp_output = NULL;	/* can't open */
		puts("Open output-file failure!");
		}
	else puts("output-file opened.");
	if ((fp_input = fopen("tst1.c","r")) == NULL) /* if given, open */
		{
		fp_input = NULL;	/* can't open */
		puts("Open input-file failure!");
		}
	else puts("input-file opened.");

/**  get stdio FILE * value so that we can fputs() to it. **/
/**  fdopen (int filedes, const char *opentype) **/

	
	if ((fp_stdout = fdopen (FP_STD_OUT, "w")) == NULL)
		puts ("fdopen() error. ");

	printf(d_fmt,fp_stdout);
	putchar(10);

	if ((desc1 = fileno(fp_stdout)) == -1)
		puts ("fileno() error. ");
/**** (d_fmt, desc1);    ********/
	putchar(10);

/**	pbufr = "fputs string."; **/
/***
	fputs (" fdopen fputs", fp_stdout);
	putchar(10);
***/

	puts("Do file operations here.., and close.");

/**	while (result == 1) ** works
		{
		putchar('.');
		chr = fgetc(fp_input);** ** works **/

/**		if (chr == EOF)
			{
			result = 0;
			break;
			}
		else fputc(chr,fp_output); ** works **
		}
**/

/**	while ((chr = fgetc(fp_input)) != EOF)  fputc(chr,fp_output); ** works **/
/**	printf(c_fmt, chr2); ** works **/
/** 	while ((chr = fgetc(fp_input)) != EOF) putchar(chr); ** works **/
/**	while ((chr = fgetc(fp_input)) != EOF) printf(c_fmt,chr); ** works **/
	putchar(10);
	processfile();
	putchar(10);
	
	puts("Goodbye!");
}

 /** main input function, chrs accumulate into static linebufr[], **/
 /** also echoes to file commentary tokens, if desired by user. **/
 /** a byte value > 7Fh (sign set) is read as eof in this function **/
 /** On return, linebufr[] holds string of k's, where k is of the set **/
 /** of bytes {1..7Fh} with a null termination, 0. **/


processfile()
{
	ln_ndx = 0;
	eof_flg = 0;
	linecnt = 0;

	while(1)
		{
		
		if (eof_flg) break;
		
		clr_ln();					/** clear line index **/

		while ((k = fgetc (fp_input)) != EOF) 		/** main fetch fn **/
			{
			if (ln_ndx >= 254)			/** linemax **/
				{
				puts ("Error, line length exceeds line buffer size.");
				clr_ln();
				continue;
				}
			if (k == EOL)
				{
		/**		linebufr[ln_ndx++] = k;	** store input in static char array **/
				linebufr[ln_ndx] = 0;	/** null terminate line to make string **/
		/**		putchar(k); **/	/** test **/
				linecnt++;
				process_ln();
				clr_ln();
				}

			linebufr[ln_ndx++] = k;	/** store input char in static char array **/
			} /** eo while **/

		/** EOF **/
		eof_flg = TRUE;
		
		} /** eo while **/
	
	resultOutput = fclose(fp_output);
	resultInput = fclose(fp_input);
	
	fputc(EOL, fp_stdout);
	fputc(EOL, fp_stdout);
	
	if(resultOutput == 0)
		fputs("Output file closed.", fp_stdout);
	else
		fputs("Output file close error.", fp_stdout);
	
	fputc(EOL, fp_stdout);
	
	if(resultOutput == 0)
		fputs("Input file closed.", fp_stdout);
	else
		fputs("Input file close error.", fp_stdout);
	
	fputc(EOL, fp_stdout);
}

/*
** process line in the buffer as a string.
*/

process_ln()
{
/**	fputs (linebufr, stdout); ** need to find stdout's ptr value **/

	fputs (linebufr, fp_stdout);
	fputs (linebufr, fp_output);

/**	puts (linebufr);	**/		/** FILE output, fp_output **/
/**	fputc (EOL, fp_output);  */
}

/*
** reindex & null terminate input line.
*/

clr_ln()
{
	ln_ndx = 0;
	linebufr[ln_ndx] = 0;
}

/*
** eo tst_1.c
*/