# include "basic.h"
# define D_NLINES 100
static char prompt[] = ">> ";

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

  int ThisList, depth, i, done;
  char *input, line[1024];
  int N, Nbytes, NBYTES, Nread, status;
  int RunCommand;
  char *A, *B, *val, *Cmd;
  FILE *f;

  RunCommand = FALSE;
  if (N = get_argument (argc, argv, "-x")) {
    remove_argument (N, &argc, argv);
    Cmd = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
    RunCommand = TRUE;
  }

  if ((argc > 2) && (!strcmp (argv[2], "-split"))) {
    if (argc == 3) {
      fprintf (stderr, "USAGE: list (root) -split (word) (word) ...\n");
      return (FALSE);
    }
    
    for (i = 0; i < argc - 3; i++) {
      sprintf (line, "%s:%d", argv[1], i);
      set_str_variable (line, argv[i+3]);
    }
    sprintf (line, "%s:n", argv[1]);
    set_int_variable (line, i);

    return (TRUE);
  }

  if (argc != 2) {
    fprintf (stderr, "USAGE: list (root) -- terminate with 'END'\n");
    return (FALSE);
  }

  if (RunCommand) {
    
    /* val will hold the result */
    NBYTES = 1024;
    ALLOCATE (val, char, NBYTES);
    
    /* need to loop until command produces no more output, 
       REALLOCATING as needed. */
    f = popen (Cmd, "r");
    done = FALSE;
    Nbytes = 0;
    while (!done) {
      Nread = fread (&val[Nbytes], 1, 1023, f);
      if (Nread < 0) { 
	fprintf (stderr, "error reading from command\n");
	done = TRUE;
      }
      if (Nread > 0) {
	Nbytes += Nread;
	NBYTES = 1024 + Nbytes;
	REALLOCATE (val, char, NBYTES);
      }
      if (Nread == 0) {
	done = TRUE;
      }
      
    }
    val[Nbytes] = 0;
    status = pclose (f);
    free (Cmd);
    
    if (status) {
      fprintf (stderr, "warning: exit status of command %d\n", status);
    }
      
    A = B = val;
    for (i = 0; B != (char *) NULL;) {
      while (isspace (*A) && (*A != 0)) A++;
      B = strchr (A, '\n');
      if (B != (char *) NULL) { *B = 0; }
      if (*A != 0) {
	sprintf (line, "%s:%d", argv[1], i);
	set_str_variable (line, A);
	A = B + 1;
	i++;
      }
    }      
    free (val);
    
    sprintf (line, "%s:n", argv[1]);
    set_int_variable (line, i);
    return (TRUE);
  }

  /* read in loop */
  depth = 0;
  ThisList = Nlists;
  for (i = 0, done = FALSE; !done; ) {

    /* get the next line (from correct place) */
    if (ThisList == 0) 
      input = readline (prompt);
    else 
      input = get_next_listentry (ThisList);

    if (input == (char *) NULL) {
      if (ThisList == 0)  {
	fprintf (stderr, "end list with 'END'\n");
	continue;
      } else {
	return (FALSE);
      }	
    }
    stripwhite (input);

    /* test for end of nested list -- if not nested, END refers to this macro */
    if (!strncasecmp (input, "END", 3)) {
      depth --;
      if (depth < 0) { /* we hit the last "END", loop is done */
	sprintf (line, "%s:n", argv[1]);
	set_int_variable (line, i);
	free (input);
	return (TRUE);
      }
    }

    if (*input) { 
      sprintf (line, "%s:%d", argv[1], i);
      set_str_variable (line, input);
      i++;
    }
  }
  return (TRUE);
}
