DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T c

⟦65b01837e⟧ TextFile

    Length: 14572 (0x38ec)
    Types: TextFile
    Names: »c_add.c«

Derivation

└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
    └─⟦653021b30⟧ »EurOpenD3/utils/downtime.tar.Z« 
        └─⟦946c717da⟧ 
            └─⟦this⟧ »c_add.c« 

TextFile

/* 
 * Copyright (c) 1988 Michael A. Cooper, University of Southern California.
 * This program may be used, copied, modified, and redistributed freely
 * for noncommercial purposes, so long as this notice remains intact.
 */

#ifndef lint
static char *RCSid = "$Header: c_add.c,v 4.3 88/07/05 15:58:17 mcooper Exp $";
#endif

/*
 *------------------------------------------------------------------
 *
 * $Source: /usr/skat3/src/common/usc/etc/downtime/RCS/c_add.c,v $
 * $Revision: 4.3 $
 * $Date: 88/07/05 15:58:17 $
 * $State: Exp $
 *
 *------------------------------------------------------------------
 *
 * Michael A. Cooper
 * Research and Development Group
 * University Computing Services 
 * University of Southern California
 * (mcooper@oberon.USC.EDU)
 *
 *------------------------------------------------------------------
 *
 * $Log:	c_add.c,v $
 * Revision 4.3  88/07/05  15:58:17  mcooper
 * Added copyright notice.
 * 
 * Revision 4.2  88/05/23  15:39:33  mcooper
 * All tm tm_mon members are now 0-11
 * instead of 1-12 for compat. with 
 * the normal BSD time routines.
 * 
 * Revision 4.1  88/05/18  14:49:17  mcooper
 * Commands no longer have extra -'s in their names.
 * 
 * Revision 4.0  88/04/20  15:41:12  mcooper
 * Version 4.
 * 
 * Revision 3.8  88/04/20  13:38:01  mcooper
 * Replace F_BROADCAST with F_MSG_REMOTE.
 * Added F_MSG_LOCAL.
 * 
 * Revision 3.7  88/04/13  19:10:15  mcooper
 * Cleaned up handling of flag bits.  No longer need
 * to define "temporary" flags for turning off a flag
 * big that is listed in the ccmd keyword list.
 * The number of special cases for turning bits on
 * has been almost eliminated.
 * 
 * Revision 3.6  88/04/11  19:47:41  mcooper
 * Converted all dt_flags to use flag
 * bits.
 * 
 * Revision 3.5  88/04/06  20:51:00  mcooper
 * Malloc dt_reason and dt_shutter.
 * 
 * Revision 3.4  88/03/02  16:08:56  mcooper
 * Cleanup time.
 * 
 * Revision 3.3  88/03/01  15:48:46  mcooper
 * Cleaned up header files.
 * 
 * Revision 3.2  88/03/01  12:37:28  mcooper
 * Initial most members of tm structs.
 * 
 * Revision 3.1  88/01/21  20:29:28  mcooper
 * Port to Alliant (Concentrix 3.0).
 * 
 * Revision 3.0  87/07/24  14:12:42  mcooper
 * Version 3.
 * 
 *------------------------------------------------------------------
 */

#include "defs.h"
#include "parse.h"

struct tm *now;
long nowl;

/*
 * c_add - Ccmd Add interface.
 *
 */

c_add()
{
  static keywrd howkeys[] = {
    { "Kill",               0,          F_KILL            },
    { "Reboot",             0,          F_REBOOT          },
    { "Halt",               0,          F_HALT            },
    { "Fake",               0,          F_FAKE            },
  };
  static keytab howtab = { (sizeof(howkeys)/sizeof(keywrd)), howkeys };
  static fdb howfdb = { _CMKEY, 0, NULL, (pdat) &(howtab), NULL, "Kill", NULL};
  static keywrd shutmsgkeys[] = {
    { "ShortMessages",      0,          F_SHUTMSG_ONE     },
    { "VerboseMessages",    0,          F_SHUTMSG_VER     },
  };
  static keytab shutmsgtab = { (sizeof(shutmsgkeys)/sizeof(keywrd)), 
				 shutmsgkeys };
  static fdb shutmsgfdb = { _CMKEY, 0, NULL, (pdat) &(shutmsgtab), NULL, 
			      NULL, NULL};
  static keywrd fskeys[] = {
    { "Fastboot",           0,          F_FASTBOOT        },
    { "NoSync",             0,          F_NOSYNC          },
    { "LocalMessages",      0,          F_MSG_LOCAL       },
    { "NoLocalMessages",    0,          -F_MSG_LOCAL      },
#ifdef RPCWALL
    { "Broadcast",          KEY_INV,    F_MSG_REMOTE      },
    { "NoBroadcast",        KEY_INV,    -F_MSG_REMOTE     },
    { "RemoteMessages",     0,          F_MSG_REMOTE      },
    { "NoRemoteMessages",   0,          -F_MSG_REMOTE     },
#endif RPCWALL
    { "NoMessages",         0,          -(F_MSG_REMOTE|F_MSG_LOCAL) },
    { "Motd",               0,          F_MOTD            },
    { "NoMotd",             0,          -F_MOTD           },
  };
  static keytab fstab = { (sizeof(fskeys)/sizeof(keywrd)), fskeys };
  static fdb opt2fdb = { _CMKEY, 0, NULL, (pdat) &(fstab), NULL, NULL, NULL };
  static keywrd nowkeys[] = {
    { "Now", 0, 1 },
  };
  static keytab nowtab = { (sizeof(nowkeys)/sizeof(keywrd)), nowkeys };
  static fdb nowfdb = { _CMKEY, 0, NULL, (pdat) &(nowtab), NULL, NULL, NULL };
  static fdb minfdb = { _CMTOK, 0, NULL, (pdat) "-", NULL, NULL, NULL };
  static fdb plsfdb = { _CMTOK, 0, NULL, (pdat) "+", NULL, NULL, NULL };
  struct downtime d;
  struct tm *gt;
  int tmp = -1, addflags = 0;
  int month, day, year, isdst;

  month = day = year = isdst = -1;

  d.dt_up = (struct tm *) xmalloc(sizeof(struct tm));
  d.dt_down = (struct tm *) xmalloc(sizeof(struct tm));
  d.dt_orderdown = (struct tm *) malloc(sizeof(struct tm));
  txtfdb._cmhlp = "Shutdown Message";
  howfdb._cmhlp = "Shutdown Method, ";
  shutmsgfdb._cmhlp = "Shutdown Message Type, ";
  opt2fdb._cmhlp = "Shutdown Flag, ";

  time(&nowl);
  now = localtime(&nowl);

  month = now->tm_mon;
  day = now->tm_mday;
  year = now->tm_year;
  isdst = now->tm_isdst;

  d.dt_down->tm_mon = month;
  d.dt_down->tm_mday = day;
  d.dt_down->tm_year = year;
  d.dt_down->tm_isdst = isdst;
  d.dt_down->tm_sec = 0;
  d.dt_down->tm_wday = 0;
  d.dt_down->tm_yday = 0;

  d.dt_up->tm_mon = month;
  d.dt_up->tm_mday = day;
  d.dt_up->tm_year = year;
  d.dt_up->tm_isdst = isdst;
  d.dt_up->tm_sec = 0;
  d.dt_up->tm_wday = 0;
  d.dt_up->tm_yday = 0;

  d.dt_orderdown->tm_mon = month;
  d.dt_orderdown->tm_mday = day;
  d.dt_orderdown->tm_year = year;
  d.dt_orderdown->tm_isdst = isdst;
  d.dt_orderdown->tm_sec = now->tm_sec;
  d.dt_orderdown->tm_min = now->tm_min;
  d.dt_orderdown->tm_hour = now->tm_hour;
  d.dt_orderdown->tm_wday = 0;
  d.dt_orderdown->tm_yday = 0;

  d.dt_flags = 0;
  /*
   * Setup default shutdown flags 
   */
  if (fake || (!killflg && !halt && !reboot))
    d.dt_flags |= F_FAKE;
  else if (halt)
    d.dt_flags |= F_HALT;
  else if (reboot)
    d.dt_flags |= F_REBOOT;
  else if (killflg)
    d.dt_flags |= F_KILL;

  if (fastboot)
    d.dt_flags |= F_FASTBOOT;
  else if (nosync)
    d.dt_flags |= F_NOSYNC;

  if (shortshutmsg)
    d.dt_flags |= F_SHUTMSG_ONE;
  else
    d.dt_flags |= F_SHUTMSG_VER;

  d.dt_flags |= F_MOTD;
  d.dt_flags |= F_MSG_LOCAL;
#ifdef RPCWALL
  d.dt_flags |= F_MSG_REMOTE;
#endif RPCWALL
  offdb._cmdef = "ON";

  if (username != NULL) {
    d.dt_shutter = (char *) xmalloc(strlen(username)+1);
    strcpy(d.dt_shutter, username);
  }
  if (host != NULL)
    strcpy(d.dt_host, host);
  d.dt_reason = (char *) xmalloc(BUFSIZ);
  strcpy(d.dt_reason, EMPTY);
  d.dt_islocked = FALSE;

  noise("option OR down-time");
 start:
  numfdb._cmlst = NULL;
  numfdb._cmhlp = "Hour";
  parse(fdbchn(&plsfdb, &minfdb, &nowfdb, &numfdb, NULL), &val, &used);

  if (used == &plsfdb) {
    numfdb._cmhlp = "Number of minutes until downtime";
    parse(&numfdb, &val, &used);

    if (val._pvint < 1) {
      printf(
"?You cannot specify ``+0''.  Use ``now'' to take the system down immediately.\n");
      return(-1);
    }

    gt = getplus(val._pvint);
    
    d.dt_down->tm_min = gt->tm_min;
    d.dt_down->tm_sec = gt->tm_sec;
    d.dt_down->tm_hour = gt->tm_hour;
    d.dt_down->tm_mon = gt->tm_mon;
    d.dt_down->tm_mday = gt->tm_mday;
    d.dt_down->tm_year = gt->tm_year;

    goto uptime;
  } else if (used == &nowfdb) {
    d.dt_down->tm_min = now->tm_min;
    d.dt_down->tm_sec = now->tm_sec;
    d.dt_down->tm_hour = now->tm_hour;
    d.dt_down->tm_mon = now->tm_mon;
    d.dt_down->tm_mday = now->tm_mday;
    d.dt_down->tm_year = now->tm_year;

    addflags |= ISNOW;
    noise("up-time");
    goto uptime;
  } else if (used == &minfdb) {
    howfdb._cmlst = opt2fdb._cmlst = shutmsgfdb._cmlst = NULL;

    noise("option");
    parse(fdbchn(&howfdb, &opt2fdb, &shutmsgfdb, NULL), &val, &used);
    
    if (used == &howfdb) {
      d.dt_flags &= ~(F_KILL | F_REBOOT | F_HALT | F_FAKE);
      d.dt_flags |= val._pvint;
    } else if (used == &shutmsgfdb) {
      d.dt_flags &= ~(F_SHUTMSG_ONE | F_SHUTMSG_VER);
      d.dt_flags |= val._pvint;
    } else if (used == &opt2fdb) {
      if (val._pvint < 0) {
	d.dt_flags &= ~-val._pvint;
      } else {
	d.dt_flags |= val._pvint;
      }
    } else {
      printf("Unknown option fdb.\n");
    }

    goto start;
  }

  if (val._pvint < 0 || val._pvint > 23) {
    printf("?The hour must be between 0 and 23.\n");
    return(-1);
  }
  d.dt_down->tm_hour = val._pvint;

  tok2fdb._cmlst = NULL;
  parse(&tok2fdb, &val, &used);

  numfdb._cmlst = NULL;
  numfdb._cmhlp = "Minute";
  parse(&numfdb, &val, &used);
  if (val._pvint < 0 || val._pvint > 59) {
    printf("?Minute must be between 0 and 59.\n");
    return(-1);
  }
  d.dt_down->tm_min = val._pvint;

  noise("down-date OR up-time");
 uptime:
  numfdb._cmlst = NULL;
  numfdb._cmhlp = "Down time Month or Up time Hour";
  parse(fdbchn(&plsfdb, &numfdb, NULL), &val, &used);

  if (used == &plsfdb) {
    numfdb._cmhlp = "Number of minutes until uptime";
    parse(&numfdb, &val, &used);

    if (val._pvint < 1) {
      printf("?You must specify a plus time of at least ``1''.\n");
      return(-1);
    }

    gt = getplus(val._pvint);

    d.dt_up->tm_min = gt->tm_min;
    d.dt_up->tm_sec = gt->tm_sec;
    d.dt_up->tm_hour = gt->tm_hour;
    d.dt_up->tm_mon = gt->tm_mon;
    d.dt_up->tm_mday = gt->tm_mday;
    d.dt_up->tm_year = gt->tm_year;

    noise("shutdown message");
    txtfdb._cmlst = NULL;
    cfmfdb._cmlst = NULL;
    /*
     * Ccmd is very picky about the order of the following FDB chain
     */
    parse(fdbchn(&cfmfdb, &txtfdb, NULL), &val, &used);
    goto postup;
  }

  tmp = val._pvint;

  tok2fdb._cmlst = NULL;
  tok1fdb._cmlst = NULL;
  parse(fdbchn(&tok1fdb, &tok2fdb, NULL), &val, &used);

  if (used == &tok1fdb) { /* parse down date */
    d.dt_down->tm_mon = tmp - 1;

    numfdb._cmlst = NULL;
    numfdb._cmhlp = "Day";
    parse(&numfdb, &val, &used);

    if (val._pvint < 1 || val._pvint > dom(d.dt_down->tm_mon)) {
      printf("?Day must be between 1 and %d.\n", dom(d.dt_down->tm_mon));
      return(-1);
    }
    d.dt_down->tm_mday = val._pvint;
  
    noise("up-time");
    numfdb._cmhlp = "Up time";
    tok1fdb._cmlst = NULL;
    numfdb._cmlst = NULL;
    plsfdb._cmlst = NULL;
    parse(fdbchn(&tok1fdb, &plsfdb, &numfdb, NULL), &val, &used);

    if (used == &plsfdb) {
    doplus:
      numfdb._cmhlp = "Number of minutes until uptime";
      parse(&numfdb, &val, &used);
      
      if (val._pvint < 1) {
	printf("?You must specify a plus time of at least ``1''.\n");
	return(-1);
      }
      
      gt = getplus(val._pvint);

      d.dt_up->tm_min = gt->tm_min;
      d.dt_up->tm_sec = gt->tm_sec;
      d.dt_up->tm_hour = gt->tm_hour;
      d.dt_up->tm_mon = gt->tm_mon;
      d.dt_up->tm_mday = gt->tm_mday;
      d.dt_up->tm_year = gt->tm_year;

      noise("shutdown message");
      txtfdb._cmlst = NULL;
      cfmfdb._cmlst = NULL;
      /*
       * Ccmd is very picky about the order of the following FDB chain.
       */
      parse(fdbchn(&cfmfdb, &txtfdb, NULL), &val, &used);
      goto postup;
    } else if (used == &tok1fdb) {
      numfdb._cmhlp = "Year";
      numfdb._cmlst = NULL;
      parse(&numfdb, &val, &used);
      if (val._pvint < 1) {
	printf("?Year must be greater than 0.\n");
	return(-1);
      }
      d.dt_down->tm_year = val._pvint;
      noise("up-time");
      numfdb._cmhlp = "Hour";
      numfdb._cmlst = NULL;
      plsfdb._cmlst = NULL;
      parse(fdbchn(&plsfdb, &numfdb, NULL), &val, &used);
      if (used == &plsfdb) {
	goto doplus;
      }
    } else if (used == &numfdb) {
      d.dt_down->tm_year = now->tm_year;
    } else {
      printf("?Bad parse value.\n");
      return(-1);
    } 

    if (val._pvint < 0 || val._pvint > 23) {
      printf("?Hour must be between 0 and 23.\n");
      return(-1);
    }
    d.dt_up->tm_hour = val._pvint;

    tok2fdb._cmlst = NULL;
    parse(&tok2fdb, &val, &used);

  } else if (used == &tok2fdb) { /* parse up time */
    d.dt_up->tm_hour = tmp;
  } else {
    printf("?Bad parse value.\n");
    return(-1);
  }

  numfdb._cmlst = NULL;
  numfdb._cmhlp = "Minute";
  parse(&numfdb, &val, &used);

  if (used != &numfdb) {
    printf("?You must enter a number.\n");
    return(-1);
  }
  if (val._pvint < 0 || val._pvint > 59) {
    printf("?Minute must be between 0 and 59.\n");
    return(-1);
  }
  d.dt_up->tm_min = val._pvint;
  
  d.dt_up->tm_mon = d.dt_down->tm_mon;
  d.dt_up->tm_mday = d.dt_down->tm_mday;
  d.dt_up->tm_year = d.dt_down->tm_year;
  d.dt_up->tm_isdst = d.dt_down->tm_isdst;

  noise("up-date OR shutdown message");
  numfdb._cmhlp = "Up time Month";
  numfdb._cmlst = NULL;
  txtfdb._cmlst = NULL;
  cfmfdb._cmlst = NULL;
  parse(fdbchn(&cfmfdb, &numfdb, &txtfdb, NULL), &val, &used);

  if (used == &cfmfdb) { /* stupid kludge to keep ccmd happy */
    tmp = -1;
  } else if (used == &numfdb) {
    if (val._pvint < 1 || val._pvint > 12) {
      printf("?month must be between 1 and 12.\n");
      return(-1);
    }
    d.dt_up->tm_mon = val._pvint - 1;

    tok1fdb._cmlst = NULL;
    parse(&tok1fdb, &val, &used);

    numfdb._cmlst = NULL;
    numfdb._cmhlp = "Day";
    parse(&numfdb, &val, &used);
    if (used != &numfdb) {
      printf("?You must enter a number for the day.\n");
      return(-1);
    }
    if (val._pvint < 1 || val._pvint > dom(d.dt_up->tm_mon)) {
      printf("?You must enter a day between 1 and %d.\n",
	     dom(d.dt_up->tm_mon));
      return(-1);
    }
    d.dt_up->tm_mday = val._pvint;

    tok1fdb._cmlst = NULL;
    txtfdb._cmlst = NULL;
    cfmfdb._cmlst = NULL;
    /*
     * The order of the fdb's is *very* important.  
     */
    parse(fdbchn(&cfmfdb, &tok1fdb, &txtfdb, NULL), &val, &used);

    if (used == &tok1fdb) {
      numfdb._cmhlp = "Year";
      numfdb._cmlst = NULL;
      parse(&numfdb, &val, &used);

      if (val._pvint < 1) {
	printf("?The year must be greater than 0.\n");
	return(-1);
      }
      d.dt_up->tm_year = val._pvint;

      noise("shutdown message");
      txtfdb._cmlst = NULL;
      cfmfdb._cmlst = NULL;
      parse(fdbchn(&txtfdb, &cfmfdb, NULL), &val, &used);
    }
  }

 postup:

  if (used == &txtfdb) {
    if (strlen(atmbuf) <= 0 || (strlen(atmbuf) == 1 && atmbuf[0] == '\n'))
      strcpy(d.dt_reason, EMPTY);
    else
      strcpy(d.dt_reason, atmbuf);

    confirm();
  }
  
  if (force) {
    addflags |= FORCE;
  }
  if (quiet) {
    addflags |= QUIET;
  }
    
  if (add(&d, addflags) < 0) {
    fprintf(stderr, "Add failed: Entry not added.\n");
    return(-1);
  }
  return(0);
}