/*  Titlebar.c - a program to change the titlebar and icon names of DECTerm  */
/*  windows whose terminal type is of vt200 or better.

/*  Written by Kevin Hudson and Wendy Taylor of the University of Lowell     */
/*  under VMS 5.3-1 with VAXC version 3.0.  Suggestions for improvements     */
/*  can be sent to hudson@dragon.cpe.ulowell.edu.                            */

/*  This software is given freely without warranty or guarentee.  Please     */
/*  feel free to pass it on to others who find it of interest.               */

#include <time.h>
#include <descrip.h>
#include <dvidef.h>
#include <iodef.h>
#include <jpidef.h>
#include <ssdef.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <syidef.h>

#define  USAGE "titlebar -h -t -i -p -n -12 -24 -day -ds -dl -dm -s \"string\"\n \
    -h   = help: you're lookin' at it.\n \
    -t   = Set the terminal title using the flags which follow, up\n \
           to the end of line or until a -i is reached\n \
    -i   = Set the icon title using the flags which follow, up\n \
           to the end of line or until a -t is reached\n \
    -p   = Process name\n \
    -n   = Nodename\n \
    -12  = The time is displayed in 12 hour format\n \
    -24  = The time is displayed in 24 hour format\n \
    -day = The day of the week is displayed\n \
    -ds  = The date is displayed in short form, ie 01/25/90\n \
    -dl  = The date is displayed in long format, ie January 25, 1990\n \
    -dm  = The date is displayed in Military format, ie 25/01/90\n \
    -s   = A Quoted String follows\n \
Any argument may be specified more than once."

#define FALSE 0
#define TRUE 1

/* String Descriptor Macros                                                 */

#define STRINGDESC  struct dsc$descriptor_s
#define SET_DESCRIP(descrip_,string_) \
    (descrip_).dsc$w_length = strlen(string_); \
    (descrip_).dsc$b_dtype = DSC$K_DTYPE_T; \
    (descrip_).dsc$b_class = DSC$K_CLASS_S; \
    (descrip_).dsc$a_pointer = string_


/* Status block structure for system service calls                          */

struct io_status_block {
    long    status;
    long    reserved;    /* DEC Private */
    };


/* Item list descriptor for system services                                 */

struct itmlst {        /* VMS data type item_list_3          */
    short int buflen ; /* word - length of the buffer        */
    short int itemcode;/* word - item code of the proposed
                                 system routine              */
    char *bufadd ;     /* pointer - buffer for i-o           */
    int  ret_len ;     /* address - longword for the length
                                    of the data written      */
    long end;          /* 0 - marks the end of the list      */
    };


struct io_status_block iosb;
int status;

main (argc, argv)
int argc;
char **argv;
{
  char osc = 157;
  char st = 156;
  char icon_string[80] = "";
  char title_string[80] = "";
  char nodename[15]; 
  char process_name[15];
  char default_directory[64];
  int string;
  char time_string[8];
  char date_string[50];
  char dir_string[40];
  int icon = FALSE;
  int title = FALSE;
  struct tm *time_structure;
  int time_val, i;
  static char *weekday[7] = {"Sunday", "Monday", "Tuesday",
				"Wednesday", "Thursday", "Friday",
				"Saturday"};
  static char *month[12] = {"January", "February", "March", "April",
				"May", "June", "July", "August",
	       		"September", "October", "November", "December"};
  static char *hour[2] = {"am", "pm"};
  string = &title_string;
  time(&time_val);
  time_structure = localtime(&time_val);
  get_node_name(&nodename);
  get_process_name(&process_name);
  getcwd(dir_string,40);
  while (++argv,--argc)
    {
      if (!strcmp(*argv,"-i"))
        {
          string = &icon_string;
          icon = TRUE;
          continue;
        }
      if (!strcmp(*argv,"-t"))
        {
          string = &title_string;
          title = TRUE;
          continue;
        }
      if (!strcmp(*argv,"-dir"))
        {
          strcat(string, dir_string);
          continue;
        }
      if (!strcmp(*argv,"-12"))
        {
          if (time_structure->tm_hour > 12)
	        {
	        time_structure->tm_hour = (time_structure->tm_hour) - 12;
	        i = 1;
	        }
          else
	        i = 0;
          sprintf(time_string,
                  "%d:%02d%s",
                  time_structure->tm_hour,
                  time_structure->tm_min,
                  hour[i]);
          strcat(string, time_string);
          continue;
        }
      if (!strcmp(*argv,"-24"))
        {
          sprintf(time_string,
                  "%d:%02d",
                  time_structure->tm_hour,
                  time_structure->tm_min);
          strcat(string, time_string);
          continue;
        }
      if (!strcmp(*argv,"-ds"))
        {
          sprintf(date_string,
                  "%02d/%02d/%02d",
                  time_structure->tm_mon,
                  time_structure->tm_mday,
                  time_structure->tm_year);
          strcat(string, date_string);
          continue;
        }
      if (!strcmp(*argv,"-dm"))
        {
          sprintf(date_string,
                  "%02d/%02d/%02d",
                  time_structure->tm_mday,
                  time_structure->tm_mon,
                  time_structure->tm_year);
          strcat(string, date_string);
          continue;
        }
      if (!strcmp(*argv,"-dl"))
        {
          sprintf(date_string,
                  "%s %d, 19%d",
                  month[time_structure->tm_mon],
                  time_structure->tm_mday,
                  time_structure->tm_year);
          strcat(string, date_string);
          continue;
        }
      if (!strcmp(*argv,"-day"))
        {
          strcat(string,weekday[time_structure->tm_wday]);
        }
      if (!strcmp(*argv,"-n"))
        {
          strcat(string,nodename);
          continue;
        }
      
      if (!strcmp(*argv,"-p"))
        {
          strcat(string,process_name);
          continue;
        }

      if (!strcmp(*argv,"-s"))
        {
          if (++argv,--argc)
            {
              strcat(string,*argv);
	      continue;
	    }
	  
        }
      if (!strcmp(*argv,"-h"))
        {
          printf(USAGE);
          exit(1);
        }
    }

  if (!strcmp(title_string,"")) {
      if (!strcmp(icon_string,"")) {
          printf(USAGE);
          exit(1);
      }
  }

  if (title == FALSE & icon == FALSE) {
	strcpy(icon_string, title_string);
  }
       
  if (strcmp(title_string,"")) {
      printf("%c21;%s%c", osc, title_string, st);
  }

  if (strcmp(icon_string,"")) {
      printf("%c2L;%s%c", osc, icon_string, st);
  }

}

/****************************************************************************/
get_node_name(nodename_ptr)
/****************************************************************************/
int nodename_ptr;
{
    int retlen;
    struct itmlst itemlist[1];

    itemlist[0].buflen = 15;
    itemlist[0].itemcode = SYI$_NODENAME;
    itemlist[0].bufadd = nodename_ptr;
    itemlist[0].ret_len = &retlen;
    itemlist[0].end = 0;

    status = sys$getsyi(0,0,0,itemlist,&iosb,0,0);

    return(status);

}

get_process_name (process_name_ptr)
int process_name_ptr;

{
  int retlen;
  struct itmlst itemlist[1] ;

  itemlist[0].buflen = 15 ;
  itemlist[0].itemcode = JPI$_PRCNAM ;
  itemlist[0].bufadd = process_name_ptr ;
  itemlist[0].ret_len = &retlen ;
  itemlist[0].end = 0;

  status = sys$getjpi(0, 0, 0, itemlist, &iosb, 0, 0) ;

  return(status);
}
/*
Kevin Hudson (Kevin Hudson)                 
UUCP: hudson@raven.ulowell.edu        Dis-clamor: QUIET !!!!!!!

USnail                        "We do well to celebrate, for we 
430 Broadway St. #10              can do nothing with sorrow"
Lowell, MA 01854                    _Mirror of Her Dreams_
                                        
*/
