Index: /branches/nightd-1-4/Ohana/src/nightd/Makefile
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/Makefile	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/Makefile	(revision 10356)
@@ -0,0 +1,26 @@
+default: nightd
+help:
+	@echo "make options: nightd (default)"
+
+include ../../Configure
+HOME 	=	$(ROOT)/src/nightd
+BIN	=	$(HOME)/bin
+LIB	=	$(HOME)/lib
+SRC	=	$(HOME)/src
+MAN	=	$(HOME)/doc
+INC	= 	$(HOME)/include
+include ../../Makefile.Common
+
+LDFLAGS := 	-ldvo -lFITS -lohana $(LDFLAGS)
+
+nightd: $(BIN)/nightd.$(ARCH)
+install: $(DESTBIN)/nightd
+
+NIGHTD = \
+$(SRC)/nightd.$(ARCH).o 	$(SRC)/ConfigInit.$(ARCH).o 	\
+$(SRC)/DoCommand.$(ARCH).o 	$(SRC)/GetStatus.$(ARCH).o 	\
+$(SRC)/SetSignals.$(ARCH).o 	$(SRC)/StartUp.$(ARCH).o 	\
+$(SRC)/misc.$(ARCH).o
+
+$(NIGHTD): $(INC)/nightd.h
+$(BIN)/nightd.$(ARCH): $(NIGHTD)
Index: /branches/nightd-1-4/Ohana/src/nightd/bin/.cvsignore
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/bin/.cvsignore	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/bin/.cvsignore	(revision 10356)
@@ -0,0 +1,2 @@
+*.linux *.lin64 *.sol *.sun *.sid *.hp *.irix
+*.linrh
Index: /branches/nightd-1-4/Ohana/src/nightd/doc/ChangeLog.txt
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/doc/ChangeLog.txt	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/doc/ChangeLog.txt	(revision 10356)
@@ -0,0 +1,10 @@
+
+- nightd 1.3 : 2006.08.23
+  - moved to ChangeLog.txt
+
+nightd-1-2:
+  - fixed minor compile warning
+
+nightd-1-1:
+  - minor cleanups for gcc -Wall
+  - nightd will be superceded eventually by sched/pcontrol?
Index: /branches/nightd-1-4/Ohana/src/nightd/include/nightd.h
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/include/nightd.h	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/include/nightd.h	(revision 10356)
@@ -0,0 +1,47 @@
+# include <ohana.h>
+# include <signal.h>
+# include <errno.h>
+
+FILE *LogFile;
+char *Program;
+
+char logfile[256];
+char **MainCommand;
+char **InitCommand;
+char **DoneCommand;
+
+char TestCommand[256];
+
+char DateStr[256];
+char TimeStr[256];
+
+char PIDFile[256];
+
+float NightStart;
+float NightStop;
+
+int SuspendAction;
+
+int ConfigInit (int *argc, char **argv);
+void LoadConfig (int sig);
+int dms_to_ddd (double *Value, char *string);
+int SetPID (pid_t *Xpid, char *Xuser, char *Xmachine);
+int GetDateTime (char *datestr, char *timestr, float *time);
+int WaitForMinute ();
+int GetStatus ();
+int ResetConfig ();
+int SendShutdown ();
+int StartUp ();
+int SetSignals ();
+void ToggleSuspend (int sig);
+void Shutdown (int sig);
+int Expose (char *filename);
+int vsystem (char *line);
+char *ExpandWords (char *line);
+int pcommand (char *line, int timeout);
+int WaitForPeriod ();
+int DoCommand (char *command, char *name);
+int freeargs (char **arglist);
+
+int PERIOD;
+int TIMEOUT;
Index: /branches/nightd-1-4/Ohana/src/nightd/scripts/ertrc
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/scripts/ertrc	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/scripts/ertrc	(revision 10356)
@@ -0,0 +1,29 @@
+
+# multiple entries are allowed for each 
+# command line, MAIN, INIT, DONE. they 
+# are performed in sequence
+
+# there are run at the start of the night
+INIT_COMMAND el_plots init
+INIT_COMMAND elixir.reload sextract
+INIT_COMMAND elixir.reload imstats
+
+MAIN_COMMAND gettemps
+MAIN_COMMAND el_plots plot
+MAIN_COMMAND elixir.link
+
+# these are run at the end of the night
+DONE_COMMAND el_plots done
+DONE_COMMAND elixir.launch mkdetrend 
+
+# PID_FILE /h/eugene/skyprobe/ert.pid
+# LOG_FILE /h/eugene/skyprobe/ert.log
+
+PID_FILE /data/milo/elixir/log/ert.pid
+LOG_FILE /data/milo/elixir/log/ert.log
+
+NIGHT_START 18:00
+NIGHT_STOP  06:00
+
+PERIOD 180
+TIMEOUT 360
Index: /branches/nightd-1-4/Ohana/src/nightd/scripts/skyproberc
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/scripts/skyproberc	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/scripts/skyproberc	(revision 10356)
@@ -0,0 +1,20 @@
+
+HOME      /h/skyprobe
+
+DATA_PATH $HOME/data
+PID_FILE  $HOME/.skyprobe.pid
+LOG_FILE  $HOME/sp_daemon.log
+
+CCD_TEMP -20
+EXPTIME   30
+
+INIT_COMMAND sp_command cool $CCD_TEMP
+INIT_COMMAND sp_command init $DATA_PATH/&DATE
+MAIN_COMMAND sp_command expose $EXPTIME $DATA_PATH/&DATE/sp_&DATE_&TIME.fits
+DONE_COMMAND sp_command warm
+
+NIGHT_START 18:00
+NIGHT_STOP  06:00
+
+PERIOD 60
+TIMEOUT 300
Index: /branches/nightd-1-4/Ohana/src/nightd/scripts/srtrc
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/scripts/srtrc	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/scripts/srtrc	(revision 10356)
@@ -0,0 +1,21 @@
+
+# wrap the commands (init, main, done) in a single script
+# this will let the config info come from the config system
+
+INIT_COMMAND sp_plots init
+
+MAIN_COMMAND sp_plots plot 
+
+DONE_COMMAND sp_plots done &DATE
+
+PID_FILE /data/milo/elixir/log/srt.pid
+LOG_FILE /data/milo/elixir/log/srt.log
+
+# PID_FILE /h/eugene/skyprobe/skyprobe.pid
+# LOG_FILE /h/eugene/skyprobe/skyprobe.log
+
+NIGHT_START 18:00
+NIGHT_STOP  06:00
+
+PERIOD 60
+TIMEOUT 300
Index: /branches/nightd-1-4/Ohana/src/nightd/src/ConfigInit.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/ConfigInit.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/ConfigInit.c	(revision 10356)
@@ -0,0 +1,115 @@
+# include "nightd.h"
+
+static char *file;
+
+int ConfigInit (int *argc, char **argv) {
+
+  char *home;
+
+  /*** load configuration info ***/
+  home = getenv ("HOME");
+  if (home == (char *) NULL) {
+    fprintf (stderr, "can't determine HOME, please set\n");
+    exit (2);
+  }
+  ALLOCATE (file, char, strlen (home) + strlen (Program) + 10);
+
+  sprintf (file, "%s/.%src", home, Program);
+  LoadConfig (SIGKILL);
+  return (TRUE);
+}
+
+void LoadConfig (int sig) {
+
+  int i, Ncmd;
+  char *config, *found;
+  char line[256];
+  double tmp;
+
+  if (sig == SIGUSR1) {
+    fprintf (LogFile, "re-loading config file %s\n", file);
+    fflush (LogFile);
+  }
+
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (LogFile, "can't find configuration file %s\n", file);
+    exit (2);
+  }
+
+  /* need to check for error status */
+  ScanConfig (config, "PERIOD",                 "%d", 0, &PERIOD);
+  ScanConfig (config, "TIMEOUT",                "%d", 0, &TIMEOUT);
+
+  /* load list of InitCommands */
+  found = config;
+  Ncmd = 10;
+  ALLOCATE (InitCommand, char *, Ncmd);
+  for (i = 0; found != NULL; i++) {
+    ALLOCATE (InitCommand[i], char, 256);
+    bzero (InitCommand[i], 256);
+    found = ScanConfig (config, "INIT_COMMAND", "%s", i + 1, InitCommand[i]);
+    if (i == Ncmd - 1) {
+      Ncmd += 10;
+      REALLOCATE (InitCommand, char *, Ncmd);
+    }      
+  }
+  
+  /* load list of MainCommands */
+  found = config;
+  Ncmd = 10;
+  ALLOCATE (MainCommand, char *, Ncmd);
+  for (i = 0; found != NULL; i++) {
+    ALLOCATE (MainCommand[i], char, 256);
+    bzero (MainCommand[i], 256);
+    found = ScanConfig (config, "MAIN_COMMAND", "%s", i + 1, MainCommand[i]);
+    if (i == Ncmd - 1) {
+      Ncmd += 10;
+      REALLOCATE (MainCommand, char *, Ncmd);
+    }      
+  }
+
+  /* load list of DoneCommands */
+  found = config;
+  Ncmd = 10;
+  ALLOCATE (DoneCommand, char *, Ncmd);
+  for (i = 0; found != NULL; i++) {
+    ALLOCATE (DoneCommand[i], char, 256);
+    bzero (DoneCommand[i], 256);
+    found = ScanConfig (config, "DONE_COMMAND", "%s", i + 1, DoneCommand[i]);
+    if (i == Ncmd - 1) {
+      Ncmd += 10;
+      REALLOCATE (DoneCommand, char *, Ncmd);
+    }      
+  }
+
+  ScanConfig (config, "PID_FILE",               "%s", 0, PIDFile);
+  ScanConfig (config, "LOG_FILE",               "%s", 0, logfile);
+    
+  ScanConfig (config, "NIGHT_START",            "%s", 0, line);
+  if (!dms_to_ddd (&tmp, line)) { 
+    fprintf (LogFile, "format error in NIGHT_START\n");
+    exit (2);
+  } 
+  NightStart = tmp;
+  if (NightStart < 12.0) {
+    fprintf (LogFile, "warning: night starts before noon!\n");
+    fprintf (LogFile, "if you live in the Arctic Circle (or Antarctic), you might need to adjust the program\n");
+    exit (2);
+  }
+  ScanConfig (config, "NIGHT_STOP",             "%s", 0, line);
+  if (!dms_to_ddd (&tmp, line)) { 
+    fprintf (LogFile, "format error in NIGHT_STOP\n");
+    exit (2);
+  } 
+  NightStop = tmp;
+  if (NightStop > 12.0) {
+    fprintf (LogFile, "warning: night ends after noon!\n");
+    fflush (LogFile);
+  }
+  if (NightStop < 12.0) NightStop += 24.0;
+
+  /* check format of command calls: must have a certain set of %d, %s, etc */
+  free (config);
+
+}
Index: /branches/nightd-1-4/Ohana/src/nightd/src/DoCommand.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/DoCommand.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/DoCommand.c	(revision 10356)
@@ -0,0 +1,21 @@
+# include "nightd.h"
+
+/* special words:
+   word    variable
+   DATE    DateStr
+   TIME    TimeStr
+*/
+
+int DoCommand (char *command, char *name) {
+  
+  char *line;
+
+  line = strcreate (command);
+  line = ExpandWords (line);
+  if (pcommand (line, TIMEOUT)) {
+    fprintf (LogFile, "error running %s command\n", name);
+    fflush (LogFile);
+  }
+  free (line);
+  return (TRUE);
+}
Index: /branches/nightd-1-4/Ohana/src/nightd/src/GetStatus.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/GetStatus.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/GetStatus.c	(revision 10356)
@@ -0,0 +1,56 @@
+# include "nightd.h"
+
+int GetStatus () {
+
+  char user[256], machine[256];
+  pid_t pid;
+
+  if (SetPID (&pid, user, machine)) {
+    fprintf (stderr, "%s is not running\n", Program);
+    unlink (PIDFile);
+    exit (1);
+  }
+
+  fprintf (stderr, "%s apparently running:\n\n", Program);
+  fprintf (stderr, "  machine: %s\n", machine);
+  fprintf (stderr, "  user: %s\n", user);
+  fprintf (stderr, "  PID: %d\n", pid);
+  fprintf (stderr, "  remove %s if %s has died unexpectedly\n", PIDFile, Program);
+  exit (0);
+
+}
+  
+int ResetConfig () {
+
+  char user[256], machine[256], line[256];
+  pid_t pid;
+
+  if (SetPID (&pid, user, machine)) {
+    fprintf (stderr, "%s is not running\n", Program);
+    unlink (PIDFile);
+    exit (1);
+  }
+
+  sprintf (line, "rsh %s kill -USR1 %d", machine, pid);
+  system (line);
+  exit (0);
+
+}
+  
+int SendShutdown () {
+
+  char user[256], machine[256], line[256];
+  pid_t pid;
+
+  if (SetPID (&pid, user, machine)) {
+    fprintf (stderr, "%s is not running\n", Program);
+    unlink (PIDFile);
+    exit (1);
+  }
+
+  sprintf (line, "rsh %s kill -TERM %d", machine, pid);
+  system (line);
+  exit (0);
+
+}
+  
Index: /branches/nightd-1-4/Ohana/src/nightd/src/SetSignals.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/SetSignals.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/SetSignals.c	(revision 10356)
@@ -0,0 +1,28 @@
+# include "nightd.h"
+
+int SetSignals () {
+
+  signal (SIGTERM, Shutdown);
+  signal (SIGUSR1, LoadConfig);
+  signal (SIGUSR2, ToggleSuspend);
+  return (TRUE);
+}
+
+void ToggleSuspend (int sig) {
+
+  SuspendAction = SuspendAction ^ TRUE;
+
+}
+
+void Shutdown (int sig) {
+
+  fprintf (LogFile, "shutting down skyprobed\n");
+  fflush (LogFile);
+
+  if (unlink (PIDFile)) {
+    fprintf (LogFile, "error deleting PID File %s\n", PIDFile);
+    exit (1);
+  }   
+
+  exit (0);
+}
Index: /branches/nightd-1-4/Ohana/src/nightd/src/StartUp.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/StartUp.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/StartUp.c	(revision 10356)
@@ -0,0 +1,75 @@
+# include "nightd.h"
+
+int StartUp () {
+
+  int i;
+  int night, NightTime;
+  char user[256], machine[256];
+  float time;
+  pid_t pid;
+
+  if (!SetPID (&pid, user, machine)) {
+    fprintf (stderr, "%s apparently running:\n\n", Program);
+    fprintf (stderr, "  machine: %s\n", machine);
+    fprintf (stderr, "  user: %s\n", user);
+    fprintf (stderr, "  PID: %d\n", pid);
+    fprintf (stderr, "  remove %s if %s has died unexpectedly\n", PIDFile, Program);
+    exit (1);
+  }
+
+  SetSignals ();
+
+  SuspendAction = FALSE;
+  NightTime = FALSE;
+
+  /* Event Loop */
+  while (1) {
+
+    if (SuspendAction) {
+      usleep (100000);
+      continue;
+    }
+
+    /* events happen at time % PERIOD */
+    WaitForPeriod ();
+
+    /* time should be UTCmidnight - UTCmidnight + 24 */
+    GetDateTime (DateStr, TimeStr, &time);
+    night = (time > NightStart) && (time < NightStop);
+
+    /* start of night */
+    if (night && !NightTime) {
+      NightTime = TRUE;
+      fprintf (LogFile, "beginning of night %s, running INIT commands\n", DateStr);
+      fflush (LogFile);
+      for (i = 0; InitCommand[i][0]; i++) {
+	DoCommand (InitCommand[i], "INIT");
+      }
+    }
+
+    /* end of night */
+    if (!night && NightTime) {
+      NightTime = FALSE;
+      fprintf (LogFile, "end of night %s, running DONE commands\n", DateStr);
+      fflush (LogFile);
+      for (i = 0; DoneCommand[i][0]; i++) {
+	DoCommand (DoneCommand[i], "DONE");
+      }
+    }
+
+    /* main event */
+    if (NightTime) {
+      for (i = 0; MainCommand[i][0]; i++) {
+	DoCommand (MainCommand[i], "MAIN");
+      }
+    }
+
+  }
+}
+
+/* 
+   SuspendAction - loop without operations, used for testing
+   NightStart    - time in hours of beginning of night
+   NightStop     - time in hours of end of night
+   NightTime     - current night / day state
+*/
Index: /branches/nightd-1-4/Ohana/src/nightd/src/misc.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/misc.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/misc.c	(revision 10356)
@@ -0,0 +1,334 @@
+# include "nightd.h"
+# include <time.h>
+# include <sys/types.h>
+# include <sys/wait.h>
+# include <unistd.h>
+# include <fcntl.h>
+
+char **getargs (char *);
+
+/***** convert [-]00:00:00 to 0.0000 ****/
+int dms_to_ddd (double *Value, char *string) {
+  
+  int valid, neg, status;
+  double tmp, value;
+  char *p1, *p2, *px;
+
+  valid = FALSE; 
+  neg = FALSE;
+  stripwhite (string);
+  p1 = string;
+  px = string + strlen(string);
+
+  if (string[0] == '-') { 
+    valid = TRUE; 
+    neg = TRUE;
+    p1 = &string[1];
+  }
+  if (string[0] == '+') { 
+    valid = TRUE; 
+    neg = FALSE;
+    p1 = &string[1];
+  }
+  if (isdigit(string[0])) { 
+    valid = TRUE;
+    p1 = &string[0];
+  }
+  if (!valid) { return (FALSE); }
+
+  status = 1;
+  tmp = strtod (p1, &p2);
+  if (p2 == p1) return (FALSE); /* entry not a number: +fred */
+  value = tmp;
+  if (p2 == px) goto escape;    /* entry only number: +1.0 */ 
+  p1 = p2 + 1;
+
+  tmp = strtod (p1, &p2);
+  if (p2 == p1) goto escape;    /* entry not a number: +1:fred */
+  status = 2;
+  value += tmp / 60.0;
+  if (p2 == px) goto escape;    /* entry only number: +1:1 */
+  p1 = p2 + 1;
+
+  tmp = strtod (p1, &p2);
+  if (p2 == p1) goto escape;    /* entry not a number: +1:1:fred */
+  value += tmp / 3600.0;
+
+ escape:
+  if (neg) {
+    value *= -1;
+  }
+  *Value = value;
+
+  return (status);
+}
+
+/* SetPID is always called outside of daemon loop, send errors to stderr */
+int SetPID (pid_t *Xpid, char *Xuser, char *Xmachine) {
+  
+  pid_t pid;
+  char *username, machine[256];
+  FILE *f;
+
+  f = fopen (PIDFile, "r");
+  if (f == (FILE *) NULL) { 
+
+    pid = getpid ();
+    username = getenv ("USER");
+    if (username == (char *) NULL) {
+      fprintf (stderr, "error getting username\n");
+      exit (2);
+    }
+    bzero (machine, 256);
+    if (gethostname (machine, 256)) {
+      fprintf (stderr, "error getting hostname\n");
+      exit (2);
+    }
+
+    f = fopen (PIDFile, "w");
+    if (f == (FILE *) NULL) { 
+      fprintf (stderr, "can't write to PID file %s\n", PIDFile);
+      exit (2);
+    }
+
+    fprintf (f, "PID:     %d\n", pid);
+    fprintf (f, "USER:    %-s\n", username);
+    fprintf (f, "MACHINE: %-s\n", machine);
+    fclose (f);
+    return (TRUE); 
+  }
+
+  fscanf (f, "%*s %d", Xpid);
+  fscanf (f, "%*s %s", Xuser);
+  fscanf (f, "%*s %s", Xmachine);
+  fclose (f);
+  return (FALSE);
+}
+
+/* date is the UT date at 09:00 local time for day starting at 12:00 */
+int GetDateTime (char *datestr, char *timestr, float *time) {
+
+  struct tm *local, *gmt;
+  struct timeval now;
+  unsigned long tsec, tref;
+  int dsec;
+  float hour;
+
+  gettimeofday (&now, (struct timezone *) NULL);
+  tsec = now.tv_sec;
+  local = localtime (&tsec);
+
+  sprintf (timestr, "%02dh%02dm", local[0].tm_hour, local[0].tm_min);
+  hour = local[0].tm_hour + local[0].tm_min / 60.0 + local[0].tm_sec / 3600.0;
+  if (hour < 12) hour += 24.0;
+
+  dsec = 3600.0*(33.0 - hour);
+  tref = tsec + dsec;
+
+  gmt = gmtime (&tref);
+
+  sprintf (datestr, "%04d%02d%02d", 1900 + gmt[0].tm_year, gmt[0].tm_mon + 1, gmt[0].tm_mday);
+  *time = hour;
+  
+  return (TRUE);
+}
+
+int WaitForPeriod () {
+
+  int Nsec;
+  struct timeval now;
+  
+  gettimeofday (&now, (struct timezone *) NULL);
+  Nsec = PERIOD - (now.tv_sec % PERIOD);
+  sleep (Nsec);
+  return (TRUE);
+}
+
+int pcommand (char *line, int timeout) {
+
+  int i, status, eof, done, pidstat, Nread, exit_good, exit_stat;
+  int rfd[2], wfd[2];
+  pid_t pid;
+  char buffer[0x4000];
+  char **arglist;
+
+  /* create pipes for communications */
+  status = pipe (rfd);
+  if (status < 0) {
+    perror ("pipe");
+    return (1);
+  }
+  status = pipe (wfd);
+  if (status < 0) {
+    perror ("pipe");
+    return (1);
+  }
+
+  arglist = getargs (line);
+  pid = fork ();
+  if (!pid) { /* must be child process */
+    dup2 (wfd[0], STDIN_FILENO);
+    dup2 (rfd[1], STDOUT_FILENO);
+    dup2 (rfd[1], STDERR_FILENO);
+    setvbuf (stdin,  (char *) NULL, _IONBF, BUFSIZ); 
+    setvbuf (stdout, (char *) NULL, _IONBF, BUFSIZ);
+    setvbuf (stderr, (char *) NULL, _IONBF, BUFSIZ);
+
+    status = execvp (arglist[0], arglist);
+    fprintf (stderr, "error starting command %s\n", arglist[0]);
+    exit (1);
+  }
+
+  eof = 0x04;
+  write (wfd[1], &eof, 1);
+  close (wfd[1]);
+ 
+  /* ok to close these here? */
+  close (rfd[1]);
+  close (wfd[0]);
+
+  fcntl (rfd[0], F_SETFL, O_NONBLOCK);
+
+  done = FALSE;
+  exit_good = exit_stat = FALSE;
+  for (i = 0; !done && (i < 10*timeout); i++) {
+    Nread = read (rfd[0], buffer, 0x4000);
+    if (Nread > 0) {
+      fwrite (buffer, 1, Nread, LogFile);
+    }
+    fflush (LogFile);
+    pidstat = waitpid (pid, &status, WNOHANG);
+    if (pidstat == pid) { 
+      done = TRUE;
+      exit_good = WIFEXITED (status);
+      exit_stat = WEXITSTATUS (status);
+    } else {
+      usleep (100000);
+    }
+  }
+  
+  if (i == 10*timeout) {
+    exit_good = FALSE;
+    fprintf (LogFile, "timeout on %s\n", arglist[0]);
+    fflush (LogFile);
+    fprintf (stderr, "timeout on %s\n", arglist[0]);
+    kill (pid, SIGKILL);
+
+    done = FALSE;
+    for (i = 0; !done && (i < 2*timeout); i++) {
+      pidstat = waitpid (pid, &status, WNOHANG);
+      if (pidstat == pid) { 
+	done = TRUE;
+      } else {
+	usleep (100000);
+      }
+    }
+  }
+  
+  close (rfd[0]);
+  freeargs (arglist);
+  if (exit_good) return (exit_stat);
+  return (1);
+}
+    
+/* split line into words by spaces, end with NULL */
+char **getargs (char *input) {
+
+  char **args, *line, *p, *e;
+  int Nargs, N, Nchar;
+
+  line = strcreate (input);
+  stripwhite (line);
+  p = line;
+
+  /* count words in line */
+  Nargs = 0;
+  while (*p != 0) {
+    for (; (*p != 0) && !isspace (*p); p++);
+    for (; isspace (*p); p++);
+    Nargs ++;
+  }
+  Nargs ++;
+  ALLOCATE (args, char *, Nargs);
+
+  /* extract words from line */
+  N = 0;
+  e = line;
+  p = line;
+  while (*p != 0) {
+    for (; (*e != 0) && !isspace (*e); e++);
+    Nchar = e - p;
+    ALLOCATE (args[N], char, Nchar + 1);
+    strncpy (args[N], p, Nchar);
+    args[N][Nchar] = 0;
+    p = e;
+    for (; isspace (*p); p++);
+    e = p;
+    N ++;
+  }
+  args[N] = (char *) NULL;
+  free (line);
+  return (args);
+}
+
+int freeargs (char **arglist) {
+
+  int i;
+
+  for (i = 0; arglist[i] != (char *) NULL; i++) {
+    free (arglist[i]);
+  }
+  free (arglist);
+  return (TRUE);
+}
+
+char *ExpandWords (char *line) {
+
+  int Nin, Nout, Ncpy, NBYTES;
+  char *p1, *p2, word[256], value[256];
+  char *outline;
+  
+  NBYTES = 256;
+  ALLOCATE (outline, char, NBYTES);
+  Nout = 0;
+  Nin  = 0;
+  while ((p1 = strchr (&line[Nin], '&')) != (char *) NULL) {
+    Ncpy = p1 - line - Nin;
+    if (Nout + Ncpy >= NBYTES) {
+      NBYTES = Nout + Ncpy + 256;
+      REALLOCATE (outline, char, NBYTES - 2);
+    }
+    memcpy (&outline[Nout], &line[Nin], Ncpy);
+    Nout += Ncpy;
+    
+    p1 ++;
+    for (p2 = p1; isalnum (*p2); p2++);
+    memcpy (word, p1, p2 - p1);
+    word[p2-p1] = 0;
+    Nin += Ncpy + 1 + p2 - p1;
+    
+    /* substitute value for word */
+    bzero (value, 256);
+    if (!strcasecmp (word, "DATE")) strcpy (value, DateStr);
+    if (!strcasecmp (word, "TIME")) strcpy (value, TimeStr);
+
+    Ncpy = strlen(value);
+    if (Nout + Ncpy >= NBYTES - 2) {
+      NBYTES = Nout + Ncpy + 256;
+      REALLOCATE (outline, char, NBYTES);
+    }    
+    memcpy (&outline[Nout], value, Ncpy);
+    Nout += Ncpy;
+  }
+  Ncpy = strlen(&line[Nin]);
+  if (Nout + Ncpy >= NBYTES - 2) {
+    NBYTES = Nout + Ncpy + 256;
+    REALLOCATE (outline, char, NBYTES);
+  }  
+  memcpy (&outline[Nout], &line[Nin], Ncpy);
+  Nout += Ncpy;
+  outline[Nout] = 0;
+  free (line);
+  return (outline);
+
+}
Index: /branches/nightd-1-4/Ohana/src/nightd/src/nightd.c
===================================================================
--- /branches/nightd-1-4/Ohana/src/nightd/src/nightd.c	(revision 10356)
+++ /branches/nightd-1-4/Ohana/src/nightd/src/nightd.c	(revision 10356)
@@ -0,0 +1,33 @@
+# include "nightd.h"
+
+int main (int argc, char **argv) {
+
+  Program = filebasename (argv[0]);
+
+  LogFile = stderr;
+  ConfigInit (&argc, argv);  
+  
+  if (argc != 2) {
+    fprintf (stderr, "USAGE: %s [mode]\n", Program);
+    fprintf (stderr, " mode = start, stop, status, config\n");
+    exit (2);
+  }
+
+  if (!strcasecmp(argv[1], "status")) GetStatus ();
+  if (!strcasecmp(argv[1], "config")) ResetConfig ();
+  if (!strcasecmp(argv[1], "stop"))   SendShutdown ();
+
+  /* execute in background, send output to logfile */
+  LogFile = fopen (logfile, "w");
+  if (LogFile == (FILE *) NULL) {
+    LogFile = stderr;
+    fprintf (LogFile, "can't open Log File %s, writing to stderr\n", logfile);
+  }
+  if (!strcasecmp(argv[1], "start"))  StartUp ();
+  /* if (!strcasecmp(argv[1], "test"))   TestMode (); */
+
+  fprintf (stderr, "invalid %s command %s\n", Program, argv[1]);
+  exit (2);
+
+}
+
