RCS_ID_C="$Id: console.c,v 1.3 1994/01/21 08:31:06 ppessi Exp $";
/*
 * Console handling
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * This file is part of the AmiTCP/IP User Library.
 *
 * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
 *                  Helsinki University of Technology, Finland.
 *
 * Created      : Sun Nov 28 17:45:55 1993 ppessi
 * Last modified: Fri Jan 21 07:27:44 1994 ppessi
 *
 * $Log: console.c,v $
 * Revision 1.3  1994/01/21  08:31:06  ppessi
 * Added "libfunc.h" include
 *
 * Revision 1.2  1994/01/21  08:13:01  ppessi
 * Updated documentation
 *
 * Revision 1.1  1994/01/19  10:03:09  ppessi
 * Initial revision
 *
 * Revision 1.1  93/11/30  03:09:47  ppessi
 * Initial revision
 *
 */

#include "base.h"
#include "libfunc.h"
#include <string.h>

/****** usergroup.library/ug_OnConsole *************************************

    NAME
        ug_OnConsole - check whether session is on local console

    SYNOPSIS
        result = ug_OnConsole(void)
        D0

        BOOL ug_OnConsole(void)

    FUNCTION
        Check if the user is logged on local console.

    RESULT
        result - 1 if the user is on console,
                 0 otherwise.

    BUGS

        Currently checking is done depending on the process window pointer.

    SEE ALSO

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

SAVEDS ASM int R_ug_OnConsole(void)
{
  struct Process *me = (struct Process *)FindTask(NULL);

  return
    me->pr_Task.tc_Node.ln_Type == NT_PROCESS &&
    me->pr_WindowPtr != (APTR)-1;
}

/****** usergroup.library/ug_GetConsoleName **********************************

    NAME
        ug_GetConsoleName --- Get Console Identifier

    SYNOPSIS
        name = ug_GetConsoleName(fh, buffer, size)
        D0                    D0  A0      D1

        UBYTE * ug_GetConsoleName(BPTR, UBYTE *, ULONG)

    FUNCTION
        Get a unique printable identifier for the interactive filehandle.
        This identifier is usually the task name of handler concatenated
        with message port address.

    INPUTS
        fh     - An interactive filehandle
        buffer - Buffer to hold console identifier
        size   - Number of bytes in buffer.

    RESULT
        name - If call is successful, pointer to buffer. NULL if
               error.

    BUGS
        May not get the proprer console name for all different console
        handlers.

    SEE ALSO
        dos.library/GetConsoleTask()

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

char *i_GetConsoleName(struct MsgPort *consoletask, char *buffer, ULONG size)
{
  struct Node *portowner = consoletask->mp_SigTask;
  static char hex_chars[] = "0123456789abcdef";
  ULONG ctx = (ULONG) consoletask;
  STRPTR poname;
  ULONG i;

  /* Fail if the there is no task for this port */
  if (portowner == NULL ||
      (consoletask->mp_Node.ln_Type != PA_SIGNAL &&
       consoletask->mp_Node.ln_Type != PA_SOFTINT) ||
      portowner->ln_Name == NULL) {
    poname = "XXX";
  } else {
    /* OK, port owner has got a name, use it */
    poname = portowner->ln_Name;
  }

  i = strlen(poname);
  if (i > 6)
    i = 6;
  if (i > size)
    i = size;
  memcpy(buffer, poname, size);

  /* Append with ":%06x" consoletask */
  if (i < size) {
    short n;

    for (n = 8, buffer[i++] = ':'; n > 0 && i < size; n--) {
      int hex_digit = ctx >> 28 & 0xf;
      if (n < 7 || hex_digit > 0)
        buffer[i++] = hex_chars[hex_digit];
      ctx <<= 4;
    }
  }

  /* NUL terminate */
  if (i < size) {
    buffer[i] = '\0';
    return buffer;
  } else {
    return NULL;
  }
}

SAVEDS ASM
char *R_ug_ConsoleName(REG(d0) LONG con, REG(a1) char *buffer,
                       REG(d1) ULONG size)
{
  struct FileHandle *confh = BADDR(con);

  if (con == 0)
    return NULL;

  if (!IsInteractive(con))
    return NULL;

  if (buffer == NULL || size < 1)
    return NULL;

  return i_GetConsoleName(confh->fh_Type, buffer, size);
}


