# include "basic.h"
# include "macro.h"

int macro (int argc, char **argv) {

  int i, N, status;

  if ((argc != 2) && (argc != 3) && (argc != 4)) {
    fprintf (stderr, "USAGE: macro (cmd)\n");
    fprintf (stderr, "  (cmd) can be one of:\n");
    fprintf (stderr, "    (name)         -- create macro (name)\n");
    fprintf (stderr, "    create (name)  -- create macro (name)\n");
    fprintf (stderr, "    delete (name)  -- delete macro (name)\n");
    fprintf (stderr, "    list   (name)  -- list macro (name)\n");
    fprintf (stderr, "    edit   (name)  -- edit macro (name) <not working yet!> *\n");
    fprintf (stderr, "    read   (name)  -- read macro(s) from file (name) <not working yet!> *\n");
    fprintf (stderr, "    write  (name)  -- write macro (name) to a file <not working yet!> *\n");
    return (FALSE);
  }

  N = sizeof (in_macro) / sizeof (Command);

  /* find the macro sub-command which matches from the list. */
  /* if sub-command is not found, assume this is creating a new macro */
  for (i = 0; i < N; i++) {
    if (!strcmp (in_macro[i].name, argv[1])) {
      (*in_macro[i].func) (argc - 1, argv + 1);
      return (status);
    }
  }

  /* sub-command was not found, pass argv[1..N] to macro create,
     make argv[0] say "create" to be consistent (probably not needed) */
  REALLOCATE (argv[0], char, 8);
  strcpy (argv[0], "create");
  status = macro_create (argc, argv);
  return (status);
}


/* macro is called with the command "macro".  
   the command line word "macro" is meant to be followed the one of several 
   possible options:
   
   macro create
   macro delete
   macro list
   macro edit
   macro read
   macro write

*/
