/*
** Copyright (C) iX Corporation 1993-1994.  All rights reserved.
**
** Module =
**
**   rxqueue.c
**
** Abstract =
**
**   This is the rxqueue routine.  It's a demonstration of how inter-process
**   APIs work (just like the ones that are linked into an application, only
**   slower due to IPC overhead).
**
**   This program works like the OS/2 rxqueue command.  It copies it's
**   standard input to Open-REXX's stack.  This is useful for capturing
**   output of a command.  For example:
**
**   "ls | rxqueue"
**
**   would place the ls command's output in the Open-REXX stack.
**
** History =
**
**   20-Apr-92 nfnm 0.00 added this comment
**
** Possible future enhancements =
**
**   Demostrate other APIs, such as irxexcom() updating variables.
**
*/
/*
 * buffer size increments
 */
#define RXQUBFSZ 100
/*
 * includes
 */
#include <stdio.h>
#include "irx.h"
/*
** Routine =
**
**   main
**
** Abstract =
**
**   main() for rxqueue
**
** Parameters =
**
**   1) argc
**   2) argv
**
** Returns =
**
**   void
**
** Possible future enhancements =
**
*/

#ifdef ORXXPrototype
int ORXXCDecl main(int argc, char **argv)
#else
int ORXXCDecl main(argc, argv)
int argc;
char **argv;
#endif
{
/*
 * input character
 */
  int ch;
/*
 * return code
 */
  int rqrc = 0;
/*
 * input buffer size and length remaining
 */
  unsigned srsz = RXQUBFSZ, srle = srsz;
/*
 * input buffer and input buffer pointer
 */
  char *sr, *srpt;
/*
 * create the buffer
 */
  if ((sr = malloc(srsz)) == NULL)
  {
    (void) printf("rxqueue - out of memory, rc = 20\n");
    rqrc = 20;
  }
  srpt = sr;
/*
 * there shouldn't be any arguments
 */
  if (argc != 0)
  {
/*
 * read characters until EOF
 */
    do
    {
/*
 * if the character is newline or EOF, send the line to the REXX queue
 */
      if ((ch = getchar()) == '\n' || (ch == EOF && (srpt - sr) != 0))
      {
/*
 * if adding to the queue fails, exit with the irxstk rc
 */
        if ((rqrc = irxstk("QUEUE",
                           sr,
                           srpt - sr,
                           NULL)) != 0)
        {
          (void) printf("rxqueue - irxstk failed, rc = %d\n",
                        rqrc);
          break;
        }
/*
 * reset the buffer pointer and remaining size
 */
        else
        {
          srpt = sr;
          srle = srsz;
        }
      }
/*
 * if it's a normal character ...
 */
      else
      {
/*
 * if there isn't room in the buffer ...
 */
        if (srle == 0)
        {
/*
 * new buffer
 */
          char *nwsr;
/*
 * new buffer size
 */
          unsigned nwsz;
/*
 * if creating the new buffer fails, exit rc = 20
 */
          if ((nwsr = malloc(nwsz = srsz + (srle = RXQUBFSZ))) == NULL)
          {
            (void) printf("rxqueue - out of memory, rc = 20\n");
            rqrc = 20;
            break;
          }
/*
 * copy the old buffer into the new buffer
 */
          (void) memcpy(nwsr,
                        sr,
                        srsz);
/*
 * free the old buffer
 */
          free(sr);
/*
 * set the new buffer pointers
 */
          srpt = (sr = nwsr) + srsz;
/*
 * set the new buffer size
 */
          srsz = nwsz;
        }
/*
 * put the character in the buffer
 */
        --srle;
        *srpt++ = (char) ch;
      }
    }
    while (ch != EOF);
  }
  else
    (void) printf("too many arguements to RXQUEUE\n");
/*
 * free the buffer
 */
  free(sr);
/*
 * return with the rxqueue return code
 */
  return rqrc;
}


