[net.micro.amiga] Batch file utility

jdt@bucsb.bu.edu.UUCP (Dave Tenny) (05/26/86)

Here's a little program for use in the s/startup-sequence
profile for conditionally performing actions.  It's not fancy,
but I find it useful.

An example:

   shouldi copy df0:lib/c32.lib ram:

in your startup profile will ask you if you wish to copy the file
to ram:, you can reply y/n/<cr> as you please.
Compiled with Manx, about 2100 bytes when linked.

 Dave Tenny, 
  Happy Amiga owner.
----------------------------- Cut --------------------------------
/* Written 05/26/86 by Jeffrey D. Tenny
   shouldi command
   Asks you if you should execute the command.  Good for batch files.

   Bugs:  won't work if invoked with RUN.
   	  requires a carraige return before it sees input.
          'y' or 'n' must be first char typed to be valid.
*/
#include <libraries/dosextens.h>
extern struct FileHandle *Input(), *Output() ;

int main(argc, argv)
  int argc ;
  char **argv ;
  {
  char argstring[255] ;
  char reply[4] ;	/* 4 to prevent input spill to input handler */
  struct FileHandle *out, *in ;
  int valid = 0 ;
  int i ;

  in = Input() ;
  out = Output() ;
  if (argc <= 1) {
     Write(out, "Usage: SHOULDI command args\n", 28L) ;
     Exit(RETURN_WARN) ;
     }
  strcpy(argstring, argv[1]) ;
  for (i = 2; i < argc ; i++) {
     strcat(argstring, " ") ;
     strcat(argstring, argv[i]) ;
     }

/* Ask the question, and maybe issue the command */
  Write(out, "Should I ", 9L) ;  
  Write(out, argstring, (long) strlen(argstring)) ;
  Write(out, "? [Y/n]:", 8L) ;
  
/* Wait for valid reply */
  while (!valid) {
     Read(in, reply, 4L) ;	/* Need 'in' to read */
     valid = (reply[0] == 'y' || reply[0] == 'n' || reply[0] == 'Y' 
	|| reply[0] == 'N' || reply[0] == '\n') ;
     }

  if (reply[0] == 'Y' || reply[0] == 'y' || reply[0] == '\n')
     Execute(argstring, 0L, out) ; /* no 'in' to prevent newcli */
  Exit(0L) ;
  }