
/******************************************************************************

    MODULE
	ReadLn.c

    DESCRIPTION
	read one (or more) line(s) from a specified _filehandle_

    NOTES
	Kickstart 2.0+ required
	compiles w/ SAS/C v6.51

    BUGS
	none known

    TODO

    EXAMPLES
	> set fh `Open Sys:S/Shell-startup`
	> Readln $fh
	  alias xcopy "copy clone "
	> Close $fh

    SEE ALSO

    INDEX

    HISTORY
	21-02-95 b_noll created
	21-02-95 b_noll added version/format-prefix/offset
	20-03-95 b_noll added args diagnostics
	20-03-95 b_noll ReadLn always skipped one line

    AUTHOR
	Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
	b_noll@informatik.uni-kl.de

******************************************************************************/

/**************************************
	    Includes
**************************************/

#ifndef   EXEC_LIBRARIES_H
# include <exec/libraries.h>
#endif /* EXEC_LIBRARIES_H */

#ifndef   CLIB_EXEC_PROTOS_H
# include <clib/exec_protos.h>
#endif /* CLIB_EXEC_PROTOS_H */

#ifndef   DOS_DOS_H
# include <dos/dos.h>
#endif /* DOS_DOS_H */

#ifndef   CLIB_DOS_PROTOS_H
# include <clib/dos_protos.h>
#endif /* CLIB_DOS_PROTOS_H */

#include <proto/dos.h>
#include <proto/exec.h>

/**************************************
	 Defines & Structures
**************************************/

#ifndef ABSEXECBASE
#define ABSEXECBASE ((struct ExecBase **)4L)
#endif

struct _arg {
/* ******************** USER FORMAT ******************** */
#define FORMAT "FILEHANDLE/N/A,LINES/N/K"

    BPTR  *filehandle;
    ULONG *lines;

/* ******************** USER FORMAT ******************** */
}; /* struct _argv */

#define MAXPATHLEN 256
#define MAXLINELEN 256

#define VERSIONPREFIX	"\0$VER: "
#define VERSIONOFFSET	0
#define FORMATPREFIX	"\0$ARG: "
#define FORMATOFFSET	7

/**************************************
	    Implementation
**************************************/

long _main (void)
{
    const char* version = VERSIONPREFIX "ReadLn 1.1 " __AMIGADATE__  + VERSIONOFFSET;
    long retval = RETURN_FAIL;
    struct ExecBase*SysBase = *ABSEXECBASE;
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	struct _arg argv = { 0 };
	APTR   args;
	retval	 = RETURN_ERROR;
	if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {

/* ******************** USER BODY ******************** */

	    if (argv.filehandle && *argv.filehandle) {
		UBYTE buffer[MAXLINELEN];
		ULONG lines = 1;

		/* MISSING: CHECKREGISTER(*argv.filehandle); */

		if (argv.lines) lines = *argv.lines;
		retval = RETURN_OK;

		while ((retval == RETURN_OK) && lines && FGets(*argv.filehandle, buffer, MAXLINELEN)) {
		    -- lines;
		    if (PutStr(buffer)) {
			retval = RETURN_ERROR;
			//PrintFault(IoErr(), "Read");
		    } /* if */
		} /* while */

		if (lines && !retval) {
		    //PrintFault(IoErr(), "Read");
		    retval = RETURN_WARN;
		} /* if */
	    } else {
		//PrintFault(ERROR_BAD_NUMBER, "Read");
		SetIoErr(ERROR_BAD_NUMBER);
	    } /* if */

/* ******************** USER BODY ******************** */
	    FreeArgs (args);
	} /* if */

	if (retval > RETURN_WARN)
	    PrintFault(IoErr(), "Read");

	CloseLibrary (DOSBase);
    } /* if */
    return (retval);
} /* _main */

/******************************************************************************
*****  END ReadLn.c
******************************************************************************/

