/*
;+
;
; ----	LOG_PORT:	Process' terminal server and port logging 
;
;
; Facility:
;
;	VAX/VMS system management
;
; Environment:
;
;	OPENVMS VAX V5.5-2 or OPENVMS AXP 1.5 or later, kernel mode
;	Tested under VAX C and DEC C (on AXP)
;
; Linking:
;$ If f$getsyi("ARCH_NAME") .EQS. "Alpha"
;$  Then
;$	link/notrace/exec=log_port_alpha.exe log_port,sys$input/opt
;sys$loadable_images:sys$base_image.exe/share
;$  Else
;$	link/notrace log_port,sys$input/opt
;sys$share:vaxcrtl.exe/share
;sys$system:sys.stb/selective_search
;$ EndIf
;	
; Running:
;$ If f$getsyi("ARCH_NAME") .EQS. "Alpha"
;$  Then
;$	If "''F$MODE()'" .eqs. "INTERACTIVE" Then run sys$system:log_port
;$  Else
;$	If "''F$MODE()'" .eqs. "INTERACTIVE" Then run sys$system:log_port_alpha
;$  EndIf
;
; Abstract:
;	This routine will extract the current process' terminal server
;	name and port id, if it is a lat process.   This information will
;	the be stored in the remote node name and remote node id fields of 
;	the process' P1.  These fields are normally only used by RTTDRIVER
;	and REMACP for remote process', should be no problem.  These fields are
;	however written by the system accounting facility and will provide
;	a convenient means for storing and tracking this information.
;
; Version:     	V02.000
; Date:		7-Aug-1994
;                         
; Kevin	F. Homan 2-FEB-1995
; Science Applications International Corporation.
; P.O. Box 2501
; 301 Laboratory Rd
; Oak Ridge, TN  37831
; (615) 482-9031
; homan@orvb.saic.com
; http://www-itg.saic.com/
;
; Modifications:
;
;	16-Nov-1987	kfh	Initial creation
;
;	7-Aug-1994	kfh	Converted to C and modified to run under alpha
;                                                     
;-                                                    
*/
#include <string.h>
#include <starlet.h>
#include <lib$routines.h>
#include <stdio.h>
#include <descrip.h>
#include <dvidef.h>

/* storage areas for server and port */
  unsigned char serverlen, portlen;
  char		*server, *port;


int main()
{

  long int status;     
  struct   /* io status block */
    {
      long condition;
      long ignored;
    } iosb;

  $DESCRIPTOR (dev_name_desc,"tt"); /* terminal device */
  int move_info();
  char			*ptr;
  static  char		buffer[129];
  static $DESCRIPTOR(device_name,buffer);

/* standard item list structure */
  struct
  {
  short	int		buf_len;
  short	int		item;
  char			*buf_addr;
  unsigned short int	*ret_len;
  int			end;
  }item_list = {device_name.dsc$w_length, DVI$_TT_ACCPORNAM,
	     device_name.dsc$a_pointer, 
	     &device_name.dsc$w_length, 0};

  memset(buffer,0,129); /* init buffer */

/* get real device name */
  status = sys$getdviw( 0,0,&dev_name_desc,&item_list,&iosb,0,0,0);

/* check for success */
  if (! (status & 1)) lib$stop (status);
  if (! (iosb.condition)) lib$stop (status);

/* check for slash separating the server name from the port name */
/* if there is no slash then this is not a terminal server */
  if ((ptr = strrchr(buffer,'/')) != NULL) {
	server = buffer;	/* the server is first place in buffer */
	port = ptr+1;		/* separated with a slash is the port */
	*ptr = 0;		/* zero terminate server string */
	serverlen = strlen(server);	/* store server name length */
	portlen = strlen(port);		/* ditto for port name */
	if (serverlen > 6) serverlen = 6;	/* only room for six char */
	if (portlen > 16) portlen = 16;	/* sixteen in the remoteid field */

#ifdef DEBUG
	printf("server %s \n",server);
	printf("port %s \n",port);
#endif
	status = sys$cmkrnl(move_info,0);
	(void) sys$exit(status); 
	}
}

int move_info()
{
#ifdef __DECC
  #pragma extern_model save
  #pragma extern_model strict_refdef
  extern char ctl$t_nodename;
  extern char ctl$t_remoteid;
  #pragma extern_model restore
#else /* VAXC */
  globalref char ctl$t_nodename;
  globalref char ctl$t_remoteid;
#endif

  lib$establish(lib$sig_to_ret);	 /* for the hackers */
  strncpy((char *)&ctl$t_nodename+1,server,serverlen);	/* store server */
  strncpy((char *)&ctl$t_remoteid+1,port,portlen);	/* store port */
  ctl$t_nodename = serverlen;		/* its counted string */
  ctl$t_remoteid = portlen;		/* so store the lengths */
  return(1);				/* always seems to work? */
}
