ToolChain/bin/targets/m68k-amigaos/ndk/Tutorials/ARexx/Host/Host.c
struct RexxHost *
CreateRexxHost(
	const STRPTR		BaseName,
	ULONG			Index,
	const struct CmdFunc *	CommandTable,
	const APTR		UserData)
{
	struct Process * ThisProcess;
	struct RexxHost * Host;
	UBYTE PortName[90];
	LONG Error;
	LONG Size;
	LONG Len;
	LONG i;

		/* No host created yet. */

	Host = NULL;

		/* No error so far. */

	Error = 0;

		/* Check for NULL name. */

	if(BaseName == NULL)
		Error = ERROR_REQUIRED_ARG_MISSING;

		/* Check for illegal characters. */

	if(Error == 0)
	{
		Len = strlen(BaseName);

		for(i = 0 ; (Error == 0) && (i < Len) ; i++)
		{
			if(BaseName[i] == ' ' || BaseName[i] == '\t')
				Error = ERROR_INVALID_COMPONENT_NAME;
		}
	}

		/* Check the name length. */

	if(Error == 0)
	{
		if(Len > 80)
			Error = ERROR_OBJECT_TOO_LARGE;
	}

		/* Create the RexxHost data. */

	if(Error == 0)
	{
			/* Add the index number if provided. */

		if(Index != 0)
			sprintf(PortName,"%s.%ld",BaseName,Index);
		else
			strcpy(PortName,BaseName);

			/* Convert the port name to all upper case
			 * characters.
			 */

		for(i = 0 ; i < Len ; i++)
			PortName[i] = ToUpper(PortName[i]);

			/* Allocate space for the RexxHost. The name
			 * requires a terminating 0-byte which is included
			 * in the RexxHost structure.
			 */

		Size = sizeof(struct RexxHost) + strlen(PortName);

		Host = (struct RexxHost *)AllocVec(Size,
			MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);

			/* Did we get the memory we wanted? */

		if(Host == NULL)
			Error = ERROR_NO_FREE_STORE;
		else
		{
				/* Set up the message port. */

			Host->GlobalPort = CreateMsgPort();

				/* Did we get the port? If the creation
				 * failed we either ran out of free
				 * signal bits or memory.
				 */

			if(Host->GlobalPort == NULL)
				Error = ERROR_NO_FREE_STORE;
			else
			{
					/* Fill in the host data. Since the
					 * port will be searched for often
					 * by the ARexx resident process we
					 * assign it a priority of 1.
					 */

				strcpy(Host->PortName,PortName);

				Host->GlobalPort->mp_Node.ln_Name =
					Host->PortName;

				Host->GlobalPort->mp_Node.ln_Pri = 1;

				Host->CommandTable = CommandTable;
				Host->UserData = UserData;
			}
		}
	}

		/* Try to add the message port. */

	if(Error == 0)
	{
			/* We need to turn off multitasking for a moment
			 * while we access the global message port list.
			 */

		Forbid();

			/* Check if there already is a message port by
			 * the name we just used.
			 */

		if(FindPort(Host->PortName))
			Error = ERROR_OBJECT_EXISTS;

			/* If there is no such port yet, add the new
			 * message port to the public list.
			 */

		if(Error == 0)
			AddPort(Host->GlobalPort);

			/* Enable multitasking again. */

		Permit();
	}

		/* Did we succeed? */

	if(Error == 0)
		return(Host);
	else
	{
			/* Clean up the host data. */

		if(Host != NULL)
		{
				/* Delete the message port. This call
				 * will succeed even if the pointer is
				 * NULL.
				 */

			DeleteMsgPort(Host->GlobalPort);

				/* Free the memory allocated for the
				 * host data.
				 */

			FreeVec(Host);
		}

			/* Obtain the control structure for this Process
			 * or Task. We need to set an error code, but the
			 * system call we will make can only be made by
			 * a Process.
			 */

		ThisProcess = (struct Process *)FindTask(NULL);

			/* If a process is making the call, set the
			 * error code so it can later be retrieved
			 * through IoErr().
			 */

		if(ThisProcess->pr_Task.tc_Node.ln_Type == NT_PROCESS)
			SetIoErr(Error);

			/* Return failure. */

		return(NULL);
	}
}
/****** Host/CreateRexxHost **************************************************
*
*   NAME
*       CreateRexxHost -- Create a public message port for use as an ARexx
*           host address.
*
*   SYNOPSIS
*       Host = CreateRexxHost(BaseName,Index,CommandTable,UserData)
*
*       struct RexxHost *CreateRexxHost(const STRPTR BaseName,ULONG Index,
*           const struct CmdFunc *CommandTable,const APTR UserData);
*
*   FUNCTION
*       You supply a name and a project index, this routine will set up
*       a public message port for use as an ARexx host address for you
*       with the name you provided. The name will be checked for validity
*       (it may not hold blank spaces or tabs) and converted to all
*       upper case letters. If a non-zero project index number is provided,
*       a numeric extension will be added to the message port name.
*       This routine will try to make sure that the message port will bear
*       a unique name. For your convenience, you can have the host data
*       structure with a user data pointer.
*
*       Please keep the length of the port name down to up to 20 characters,
*       many users prefer short host names. This routine will reject names
*       longer than 80 characters.
*
*       The name is converted to all upper case characters to make it easier
*       to access the host name in ARexx scripts. Lower case names require
*       quoting whereas upper case names do not.
*
*   INPUTS
*       BaseName - Name of message port to create; the name may not
*           contain blank spaces or tabs and will be converted to
*           all upper case letters. The name you provide may be up to
*           80 characters long.
*
*       Index - Project index number; if nonzero will cause a numeric
*           extension to be added to the port name, i.e. for an index
*           number of 1 the name "BASENAME" will become "BASENAME.1".
*
*       CommandTable - Points to the first element of the array in
*           which command names, templates and functions are stored.
*
*       UserData - User data pointer which will be copied into the
*           RexxHost structure.
*
*   RESULT
*       Host - Pointer to RexxHost created or NULL if creation failed.
*           If NULL is returned you can retrieve an error code through
*           dos.library/IoErr.
*
*   SEE ALSO
*       exec.library/AddPort
*       exec.library/CreateMsgPort
*       dos.library/IoErr
*
******************************************************************************
*
*/
Host/CreateRexxHost
NAME
CreateRexxHost -- Create a public message port for use as an ARexx
host address.





FUNCTION
You supply a name and a project index, this routine will set up
a public message port for use as an ARexx host address for you
with the name you provided. The name will be checked for validity
(it may not hold blank spaces or tabs) and converted to all
upper case letters. If a non-zero project index number is provided,
a numeric extension will be added to the message port name.
This routine will try to make sure that the message port will bear
a unique name. For your convenience, you can have the host data
structure with a user data pointer.

Please keep the length of the port name down to up to 20 characters,
many users prefer short host names. This routine will reject names
longer than 80 characters.

The name is converted to all upper case characters to make it easier
to access the host name in ARexx scripts. Lower case names require
quoting whereas upper case names do not.

INPUTS
BaseName - Name of message port to create; the name may not
contain blank spaces or tabs and will be converted to
all upper case letters. The name you provide may be up to
80 characters long.

Project index number; if nonzero will cause a numeric
extension to be added to the port name, i.e. for an index
number of 1 the name "BASENAME" will become "BASENAME.1".

Points to the first element of the array in
which command names, templates and functions are stored.

User data pointer which will be copied into the
RexxHost structure.

RESULT
Host - Pointer to RexxHost created or NULL if creation failed.
If NULL is returned you can retrieve an error code through
dos.library/IoErr.


Host / CreateRexxHost NAME CreateRexxHost -- Create a public message port for use as an ARexx host address . FUNCTION You supply a name and a project index this routine will set up a public message port for use as an ARexx host address for you with the name you provided . The name will be checked for validity ( it may not hold blank spaces or tabs ) and converted to all upper case letters . If a non - zero project index number is provided a numeric extension will be added to the message port name . This routine will try to make sure that the message port will bear a unique name . For your convenience you can have the host data structure with a user data pointer . Please keep the length of the port name down to up to 20 characters many users prefer short host names . This routine will reject names longer than 80 characters . The name is converted to all upper case characters to make it easier to access the host name in ARexx scripts . Lower case names require quoting whereas upper case names do not . INPUTS BaseName - Name of message port to create ; the name may not contain blank spaces or tabs and will be converted to all upper case letters . The name you provide may be up to 80 characters long . Project index number ; if nonzero will cause a numeric extension to be added to the port name i . e . for an index number of 1 the name " BASENAME " will become " BASENAME . 1 " . Points to the first element of the array in which command names templates and functions are stored . User data pointer which will be copied into the RexxHost structure . RESULT Host - Pointer to RexxHost created or NULL if creation failed . If NULL is returned you can retrieve an error code through dos . library / IoErr .
struct RexxHost *
CreateRexxHost(
	const STRPTR		BaseName,
	ULONG			Index,
	const struct CmdFunc *	CommandTable,
	const APTR		UserData)
{
	struct Process * ThisProcess;
	struct RexxHost * Host;
	UBYTE PortName[90];
	LONG Error;
	LONG Size;
	LONG Len;
	LONG i;
	Host = NULL;
	Error = 0;
	if(BaseName == NULL)
		Error = ERROR_REQUIRED_ARG_MISSING;
	if(Error == 0)
	{
		Len = strlen(BaseName);
		for(i = 0 ; (Error == 0) && (i < Len) ; i++)
		{
			if(BaseName[i] == ' ' || BaseName[i] == '\t')
				Error = ERROR_INVALID_COMPONENT_NAME;
		}
	}
	if(Error == 0)
	{
		if(Len > 80)
			Error = ERROR_OBJECT_TOO_LARGE;
	}
	if(Error == 0)
	{
		if(Index != 0)
			sprintf(PortName,"%s.%ld",BaseName,Index);
		else
			strcpy(PortName,BaseName);
		for(i = 0 ; i < Len ; i++)
			PortName[i] = ToUpper(PortName[i]);
		Size = sizeof(struct RexxHost) + strlen(PortName);
		Host = (struct RexxHost *)AllocVec(Size,
			MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);
		if(Host == NULL)
			Error = ERROR_NO_FREE_STORE;
		else
		{
			Host->GlobalPort = CreateMsgPort();
			if(Host->GlobalPort == NULL)
				Error = ERROR_NO_FREE_STORE;
			else
			{
				strcpy(Host->PortName,PortName);
				Host->GlobalPort->mp_Node.ln_Name =
					Host->PortName;
				Host->GlobalPort->mp_Node.ln_Pri = 1;
				Host->CommandTable = CommandTable;
				Host->UserData = UserData;
			}
		}
	}
	if(Error == 0)
	{
		Forbid();
		if(FindPort(Host->PortName))
			Error = ERROR_OBJECT_EXISTS;
		if(Error == 0)
			AddPort(Host->GlobalPort);
		Permit();
	}
	if(Error == 0)
		return(Host);
	else
	{
		if(Host != NULL)
		{
			DeleteMsgPort(Host->GlobalPort);
			FreeVec(Host);
		}
		ThisProcess = (struct Process *)FindTask(NULL);
		if(ThisProcess->pr_Task.tc_Node.ln_Type == NT_PROCESS)
			SetIoErr(Error);
		return(NULL);
	}
}
struct RexxHost * CreateRexxHost ( const STRPTR BaseName , ULONG Index , const struct CmdFunc * CommandTable , const APTR UserData ) { struct Process * ThisProcess ; struct RexxHost * Host ; UBYTE PortName [ 90 ] ; LONG Error ; LONG Size ; LONG Len ; LONG i ; Host = NULL ; Error = 0 ; if ( BaseName == NULL ) Error = ERROR_REQUIRED_ARG_MISSING ; if ( Error == 0 ) { Len = strlen ( BaseName ) ; for ( i = 0 ; ( Error == 0 ) && ( i < Len ) ; i ++ ) { if ( BaseName [ i ] == ' ' || BaseName [ i ] == ' \t ' ) Error = ERROR_INVALID_COMPONENT_NAME ; } } if ( Error == 0 ) { if ( Len > 80 ) Error = ERROR_OBJECT_TOO_LARGE ; } if ( Error == 0 ) { if ( Index != 0 ) sprintf ( PortName , " " , BaseName , Index ) ; else strcpy ( PortName , BaseName ) ; for ( i = 0 ; i < Len ; i ++ ) PortName [ i ] = ToUpper ( PortName [ i ] ) ; Size = sizeof ( struct RexxHost ) + strlen ( PortName ) ; Host = ( struct RexxHost * ) AllocVec ( Size , MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC ) ; if ( Host == NULL ) Error = ERROR_NO_FREE_STORE ; else { Host -> GlobalPort = CreateMsgPort ( ) ; if ( Host -> GlobalPort == NULL ) Error = ERROR_NO_FREE_STORE ; else { strcpy ( Host -> PortName , PortName ) ; Host -> GlobalPort -> mp_Node . ln_Name = Host -> PortName ; Host -> GlobalPort -> mp_Node . ln_Pri = 1 ; Host -> CommandTable = CommandTable ; Host -> UserData = UserData ; } } } if ( Error == 0 ) { Forbid ( ) ; if ( FindPort ( Host -> PortName ) ) Error = ERROR_OBJECT_EXISTS ; if ( Error == 0 ) AddPort ( Host -> GlobalPort ) ; Permit ( ) ; } if ( Error == 0 ) return ( Host ) ; else { if ( Host != NULL ) { DeleteMsgPort ( Host -> GlobalPort ) ; FreeVec ( Host ) ; } ThisProcess = ( struct Process * ) FindTask ( NULL ) ; if ( ThisProcess -> pr_Task . tc_Node . ln_Type == NT_PROCESS ) SetIoErr ( Error ) ; return ( NULL ) ; } }
Host/CreateRexxHost
NAME
CreateRexxHost -- Create a public message port for use as an ARexx
host address.
Host / CreateRexxHost NAME CreateRexxHost -- Create a public message port for use as an ARexx host address .
/* No host created yet. */ /* No error so far. */ /* Check for NULL name. */ /* Check for illegal characters. */ /* Check the name length. */ /* Create the RexxHost data. */ /* Add the index number if provided. */ /* Convert the port name to all upper case
			 * characters.
			 */ /* Allocate space for the RexxHost. The name
			 * requires a terminating 0-byte which is included
			 * in the RexxHost structure.
			 */ /* Did we get the memory we wanted? */ /* Set up the message port. */ /* Did we get the port? If the creation
				 * failed we either ran out of free
				 * signal bits or memory.
				 */ /* Fill in the host data. Since the
					 * port will be searched for often
					 * by the ARexx resident process we
					 * assign it a priority of 1.
					 */ /* Try to add the message port. */ /* We need to turn off multitasking for a moment
			 * while we access the global message port list.
			 */ /* Check if there already is a message port by
			 * the name we just used.
			 */ /* If there is no such port yet, add the new
			 * message port to the public list.
			 */ /* Enable multitasking again. */ /* Did we succeed? */ /* Clean up the host data. */ /* Delete the message port. This call
				 * will succeed even if the pointer is
				 * NULL.
				 */ /* Free the memory allocated for the
				 * host data.
				 */ /* Obtain the control structure for this Process
			 * or Task. We need to set an error code, but the
			 * system call we will make can only be made by
			 * a Process.
			 */ /* If a process is making the call, set the
			 * error code so it can later be retrieved
			 * through IoErr().
			 */ /* Return failure. */