[comp.sys.amiga.tech] Buffered I/O Woes...

bevis@EE.ECN.PURDUE.EDU (Jeff Bevis) (07/10/90)

Help.  I don't know what to do next.  Here's what I need:  CBREAK mode!!!
How can one get such behavior from a program run in a CLI, using stdin?
Heck, forget stdio, let's use myprocess->pr_CIS!  HELP!  I'm *trying* to
interface a terminal to some amiga software (a screen editor for the terminal)
and this buffered I/O is driving me nuts! (or, rather, the inability to
unbuffer it.)   

-------------------------------------------------------------------------------
    "Three is never equal to four, except for very large values of three."
-------------------------------------------------------------------------------
Jeff Bevis		     Purdue Univeristy School of Electrical Engineering
bevis@en.ecn.purdue.edu	  	   	       Give me Amiga or nothing at all.
-------------------------------------------------------------------------------

david@starsoft.UUCP (Dave Lowrey) (07/11/90)

>In article <13154@cbmvax.commodore.com> jesup@cbmvax.commodore.com (Randell Jesup) writes:
>In article <9007100239.AA07044@en.ecn.purdue.edu> bevis@EE.ECN.PURDUE.EDU (Jeff Bevis) writes:
>>Help.  I don't know what to do next.  Here's what I need:  CBREAK mode!!!
>>How can one get such behavior from a program run in a CLI, using stdin?
>>Heck, forget stdio, let's use myprocess->pr_CIS!  HELP!  I'm *trying* to
>>interface a terminal to some amiga software (a screen editor for the terminal)
>>and this buffered I/O is driving me nuts! (or, rather, the inability to
>>unbuffer it.)
>
>	There's a packet to change a con: handler window into a RAW: window
>(and vice-versa).  Chuck McManis (if my memory is correct) has released code
>in the past to send this packet, and it's on a Fish disk (I don't remember
>which one).  Chuck?
>

At the risk of clogging up the net's valuable bandwidth, here is the
above mentioned code. I got it from Matt Dillon's UUCP package, but
it is Chuck's code. The sendpacket routine that he calls is also included.
It was written by CBM and is usefull in alot of other applications also.

===========================================================================
/*
 * raw.c
 *
 * This is a routine for setting a given stream to raw or cooked mode on the
 * Amiga. This is useful when you are using Lattice C to produce programs
 * that want to read single characters with the "getch()" or "fgetc" call.
 *
 * Written : 18-Jun-87 By Chuck McManis.
 */

#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

#ifdef LATTICE
#include <ios1.h>
#endif
#include <errno.h>
#include <stdio.h>

/*
 * Function raw() - Convert the specified file pointer to 'raw' mode. This
 * only works on TTYs and essentially keeps DOS from translating keys for
 * you, also (BIG WIN) it means getch() will return immediately rather than
 * wait for a return. You lose editing features though.
 */

long
raw(fp)
FILE *fp;
{
    struct MsgPort *mp; 	/* The File Handle message port */
    struct FileHandle *afh;
    long	    Arg[1], res;

#ifdef LATTICE
    {
	struct UFB     *ufb;
	ufb = (struct UFB *) chkufb(fileno(fp));    /* Step one, get the file   */
	afh = (struct FileHandle *) (ufb->ufbfh);
    }
#else
    afh = (struct FileHandle *)fdtofh(fileno(fp));
#endif

    if (!IsInteractive(afh)) {  /* Step two, check to see if it's a console */
	errno = ENOTTY;
	return (-1);
    }
    /* Step three, get it's message port. */
    mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
    Arg[0] = -1L;
    res = SendPacket(mp, ACTION_SCREEN_MODE, Arg, 1);   /* Put it in RAW: mode */
    if (res == 0) {
	errno = ENXIO;
	return (-1);
    }
    return (0);
}

/*
 * Function - cooked() this function returns the designate file pointer to
 * its normal, wait for a <CR> mode. This is exactly like raw() except that
 * it sends a 0 to the console to make it back into a CON: from a RAW:
 */

long
cooked(fp)
FILE *fp;
{
    struct MsgPort *mp; 	/* The File Handle message port */
    struct FileHandle *afh;
    long	    Arg[1], res;

#ifdef LATTICE
    {
	struct UFB     *ufb;
	ufb = (struct UFB *) chkufb(fileno(fp));
	afh = (struct FileHandle *) (ufb->ufbfh);
    }
#else
    afh = (struct FileHandle *)fdtofh(fileno(fp));
#endif

    if (!IsInteractive(afh)) {
	errno = ENOTTY;
	return (-1);
    }
    mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
    Arg[0] = 0;
    res = SendPacket(mp, ACTION_SCREEN_MODE, Arg, 1);
    if (res == 0) {
	errno = ENXIO;
	return (-1);
    }
    return (0);
}
=============================================================================
/*
 * Sendpacket.c
 *
 * An invaluable addition to your Amiga.lib file. This code sends a packet the
 * given message port. This makes working around DOS lots easier.
 *
 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
 * say 'oml lib:amiga.lib -r sendpacket.o'
 */

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <protos.h>

/*
 * Function - SendPacket written by Phil Lindsay, Carolyn Scheppner, and Andy
 * Finkel. This function will send a packet of the given type to the Message
 * Port supplied.
 */

long
SendPacket(pid, action, args, nargs)
struct MsgPort	*pid;	/* process indentifier ... (handler's message port ) */
long		action, /* packet type ... (what you want handler to do )   */
		args[], /* a pointer to an argument list */
		nargs;	/* number of arguments in list	*/
{
	struct MsgPort *replyport;
	struct StandardPacket *packet;
	long count, *pargs, res1;

	replyport = CreatePort(NULL, 0L);
	if (!replyport)
		return (0);

	/* Allocate space for a packet, make it public and clear it */
	packet = (struct StandardPacket *)
		AllocMem((long) sizeof(struct StandardPacket),
			MEMF_PUBLIC | MEMF_CLEAR);
	if (!packet) {
		DeletePort(replyport);
		return (0);
	}
	packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
	packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
	packet->sp_Pkt.dp_Port = replyport;
	packet->sp_Pkt.dp_Type = action;

	/* copy the args into the packet */
	pargs = &(packet->sp_Pkt.dp_Arg1);      /* address of first argument */
	for (count = 0; count < nargs; count++)
		pargs[count] = args[count];

	PutMsg(pid, packet);    /* send packet */

	WaitPort(replyport);
	GetMsg(replyport);

	res1 = packet->sp_Pkt.dp_Res1;

	FreeMem(packet, (long) sizeof(struct StandardPacket));
	DeletePort(replyport);

	return (res1);
}

==========================================================================
--
----------------------------------------------------------------------------
These words be mine. The company doesn't care, because I am the company! :-)

      Dave Lowrey	 |  In Texas: {uhnix1,moray}!starsoft!david
Starbound Software Group | The World: dwl10@uts.amdahl.com (amdahl!dwl10)
      Houston, TX	 |

jesup@cbmvax.commodore.com (Randell Jesup) (07/12/90)

In article <9007100239.AA07044@en.ecn.purdue.edu> bevis@EE.ECN.PURDUE.EDU (Jeff Bevis) writes:
>Help.  I don't know what to do next.  Here's what I need:  CBREAK mode!!!
>How can one get such behavior from a program run in a CLI, using stdin?
>Heck, forget stdio, let's use myprocess->pr_CIS!  HELP!  I'm *trying* to
>interface a terminal to some amiga software (a screen editor for the terminal)
>and this buffered I/O is driving me nuts! (or, rather, the inability to
>unbuffer it.)   

	There's a packet to change a con: handler window into a RAW: window
(and vice-versa).  Chuck McManis (if my memory is correct) has released code
in the past to send this packet, and it's on a Fish disk (I don't remember
which one).  Chuck?

-- 
Randell Jesup, Keeper of AmigaDos, Commodore Engineering.
{uunet|rutgers}!cbmvax!jesup, jesup@cbmvax.cbm.commodore.com  BIX: rjesup  
Common phrase heard at Amiga Devcon '89: "It's in there!"

jesup@cbmvax.commodore.com (Randell Jesup) (07/13/90)

I'd propose that the information on where to get Chuck's raw io code (to send
the ACTION_SCREEN_MODE packet) be included in the monthly posting.

-- 
Randell Jesup, Keeper of AmigaDos, Commodore Engineering.
{uunet|rutgers}!cbmvax!jesup, jesup@cbmvax.cbm.commodore.com  BIX: rjesup  
Common phrase heard at Amiga Devcon '89: "It's in there!"