/*
   <p><i>popen()</i> test harness.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include <string.h>
#include "environ.h"

#ifdef LINUX
#include <stdlib.h>
#endif

#define MAXCMD    4000
#define BUFLEN    75
     
main(int argc, char **argv)
{
    FILE         *fp;
    static char  cmd[MAXCMD+100];
    static char  buf[BUFLEN+1];
    int          i,
                 j;
    int          nomore;
    char         *mode;
    static char  dmode[] = "r";

    mode = dmode;

    setprog(argv[0]);
    
    for (i=1; i<argc; i++)
    {
        if (argv[i][0] != '-')
            break;
        for (j=1; j<strlen(argv[i]); j++)
        {
            if (argv[i][j] == '?')
            {
                printf("%s -?rw cmd args\n", progname());
                exit(0);
            }
            else
            if (argv[i][j] == 'r')
                mode = "r";
            else
            if (argv[i][j] == 'w')
                mode = "w";
            else
            {
                printf("Unknown option: %s\n", argv[i]);
                exit(1);
            }
        }
    }
    j=0;
    strcpy(cmd, "");
    while(i<argc && j<MAXCMD)
    {
        if (j>0)
            strcat(cmd, " ");
        j += strlen(argv[i]);
        strcat(cmd,argv[i++]);
    }

    printf("%s: mode=%s for popen cmd=%s\n", progname(), mode, cmd);

    fp = popen(cmd, mode);
    printf("%s: popen() returned %ld\n", progname(), (long)fp);

    i=0;
    if (strcmp(mode,"r") == 0)
    {
        while (fgets(buf, BUFLEN, fp) != NULL)
        {
            j = strlen(buf) - 1;
            if (buf[j] == '\n')
                buf[j] = '\0';
            printf("%2d:%s\n", ++i, buf);
        }
    }
    else
    {
        printf("\n^d or '*end' to end input.\n\n");
        nomore = FALSE;
        while (nomore == FALSE)
        {
            printf("%2d:", ++i);
            if (fgets(buf, BUFLEN, stdin) == NULL)
                nomore=TRUE;
            else
            {
                if (strcmp(buf,"*end\n") == 0)
                {
                    nomore = TRUE;
                    printf("Terminated by *end\n");
                }
                else
                    fputs(buf, fp);
            }
        }
    }
    
    printf("\n%s: pclose() returned %ld\n", progname(), pclose(fp));
    printf("%s: run ended\n", progname());
    exit(0);
}
