/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    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 GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#include "io.h"
#include <sys/param.h>


char *
killsp(s)
char *s ;
{
    while(*s == ' ')
      s++ ;
    return s ;
}


/* Case Independent strncmp */

int
ci_strncmp(s1,s2,n)
register char *s1,*s2 ;
register int n ;
{
    for(;n;s1++,s2++,n--) {
        if(*s1=='\0' && *s2 == '\0')
          break ;
        if((isalpha(*s1)?*s1|0x20:*s1) != (isalpha(*s2)?*s2|0x20:*s2))
          return YEA ;
    }
    return NA ;
}



pressreturn()
{
    extern int t_lines ;
    char buf[2] ;
    getdata(t_lines-1,0,"Press [RETURN] to continue",buf,2,NOECHO,NULL) ;
    move(t_lines-1,0) ;
    clrtoeol() ;
    refresh() ;
	return 0 ;
}

dumbreturn()
{
	char ch ;
	prints("Press [RETURN] to continue") ;
	while((ch = igetch()) != EOF)
	  if(ch == '\n' || ch == '\r')
		break ;
}

bell()
{
    fprintf(stderr,"%c",0x07) ;
}

Getyn(s)
char *s ;
{
	CreateNameList() ;
	AddNameList("Yes") ;
	AddNameList("No") ;
	AddNameList("yes") ;
	AddNameList("no") ;
	namecomplete("(Yes or No): ",s) ;
}

char *bbsenv[MAXENVS] ;
int numbbsenvs = 0 ;

bbssetenv(env,val)
char *env, *val ;
{
    register int i,len ;
    extern char *malloc() ;

    if(numbbsenvs == 0)
      bbsenv[0] = NULL ;
    len = strlen(env) ;
    for(i=0;bbsenv[i];i++)
      if(!ci_strncmp(env,bbsenv[i],len))
        break ;
    if(i>=MAXENVS)
      return -1 ;
    if(bbsenv[i])
      free(bbsenv[i]) ;
    else
      bbsenv[++numbbsenvs] = NULL ;
    bbsenv[i] = malloc(strlen(env)+strlen(val)+2) ;
    strcpy(bbsenv[i],env) ;
    strcat(bbsenv[i],"=") ;
    strcat(bbsenv[i],val) ;
}

#define LOOKFIRST  (0)
#define LOOKLAST   (1)
#define QUOTEMODE  (2)

do_exec(com,wd)
char *com, *wd ;
{
    char path[MAXPATHLEN] ;
    char pcom[MAXCOMSZ] ;
    char *arglist[MAXARGS] ;
    register int i,len ;
    register int argptr ;
    register char *lparse ;
    int status, pid, w ;
    int pmode ;
    void (*isig)(), (*qsig)() ;

    strncpy(path,BINDIR,MAXPATHLEN) ;
    strncpy(pcom,com,MAXCOMSZ) ;
    len = MIN(strlen(com)+1,MAXCOMSZ) ;
    pmode = LOOKFIRST ;
    for(i=0,argptr=0;i<len;i++) {
        if(pcom[i] == '\0')
          break ;
        if(pmode == QUOTEMODE) {
            if(pcom[i] == '\001') {
                pmode = LOOKFIRST ;
                pcom[i] = '\0' ;
                continue ;
            }
            continue ;
        }
        if(pcom[i] == '\001') {
            pmode = QUOTEMODE ;
            arglist[argptr++] = &pcom[i+1] ;
            if(argptr+1 == MAXARGS)
              break ;
            continue ;
        }
        if(pmode == LOOKFIRST)
          if(pcom[i] != ' ') {
              arglist[argptr++] = &pcom[i] ;
              if(argptr+1 == MAXARGS)
                break ;
              pmode = LOOKLAST ;
          } else continue ;
        if(pcom[i] == ' ') {
            pmode = LOOKFIRST ;
            pcom[i] = '\0' ;
        }
    }
    arglist[argptr] = NULL ;
    if(argptr == 0)
      return -1 ;
    if(*arglist[0] == '/')
      strncpy(path,arglist[0],MAXPATHLEN) ;
    else
      strncat(path,arglist[0],MAXPATHLEN) ;
    reset_tty() ;
    alarm(0) ;
#ifdef SYSV
    if((pid = fork()) == 0) {
#else
    if((pid = vfork()) == 0) {
#endif
        if(wd)
          if(chdir(wd)) {
              fprintf(stderr,"Unable to chdir to '%s'\n",wd) ;
              exit(-1) ;
          }
        if(numbbsenvs == 0)
          bbsenv[0] = NULL ;
        execve(path,arglist,bbsenv) ;
        fprintf(stderr,"EXECV FAILED... path = '%s'\n",path) ;
        exit(-1) ;
    }
    isig = signal(SIGINT, SIG_IGN) ;
    qsig = signal(SIGQUIT, SIG_IGN) ;
    while((w = wait(&status)) != pid && w != 1)
      /* NULL STATEMENT */ ;
    signal(SIGINT, isig) ;
    signal(SIGQUIT, qsig) ;
    restore_tty() ;
    alarm(SNOOZE) ;
    return((w == -1)? w: status) ;
}



