# include "Ximage.h"
# define STRCONST(A) ((int)(0x1000000*A[0] + 0x10000*A[1] + 0x100*A[2] + 0x1*A[3]))

int CheckPipe (Graphic *graphic, Layout *layout) {

  int status;
  char buffer[1024];

  /* attempt to read 4 bytes from stdin */

  status = read (layout[0].Ximage, buffer, 4);
  buffer[4] = 0;

  switch (status) {
  case -1:
    /* no input from pipe: continue */
    return (TRUE);
    break;

  case 0:
    fprintf (stderr, "pipe has died!\n");
    return (FALSE);
    break;

  case 4:
/*    fprintf (stderr, "got signal %s from pipe\n", buffer);    */
    break;

  default:
    fprintf (stderr, "weird signal: too many or few bytes!  %d\n", status);
    return (TRUE);
    break;
  }

  
  if (!strcmp (buffer, "QUIT")) {
    /* fprintf (stderr, "Goodbye!\n"); */
    return (FALSE);
  }
  
  if (!strcmp (buffer, "READ")) {
    status = NewPicture (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CURS")) {
    status = cursor (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "ERAS")) {
    status = EraseOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "LOAD")) {
    status = LoadOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "TICK")) {
    status = LoadTickmarks (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "SAVE")) {
    status = SaveOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "PSIT")) {
    status = PSit (graphic, layout, 0);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "PSRW")) {
    status = PSit (graphic, layout, 1);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "JPEG")) {
    status = JPEGit (graphic, layout);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "RSIZ")) {
    status = Resize (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CSVE")) {
    status = CSaveOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CENT")) {
    status = Center (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "NPIX")) {
    sprintf (buffer, "NPIX:   %8d", layout[0].Npixels);
    write (layout[0].Ximage, buffer, 16);
    return (TRUE);
  }

  fprintf (stderr, "unknown signal %s\n", buffer);

  return (TRUE);

}


    
