Beispiel #1
0
void mushrooms()
{
  stile(player->y, player->x, TL_MUSH_CLEARED);
  
  switch (rand() % 3)
  {
  case 0:
    game->beer = 20;
    draw_board_norefresh();
    pwait("@ WOW !!! @");
    break;

  case 1:
    game->hallucination = 20;
    draw_board_norefresh();
    pwait("@ WOW !!! @");
    break;

    // TODO: Teleport to subquest
    
  default:
    pwait("WOW!!!\n\nYOU FEEL MUCH STRONGER!");
    change_pl_st(+4);
    break;
  }


  return;
}
Beispiel #2
0
uintptr
sys_wait(va_list list)
{
	ulong pid;
	Waitmsg w;
	OWaitmsg *ow;

	ow = va_arg(list, OWaitmsg*);
	if(ow == nil)
		pid = pwait(nil);
	else {
		validaddr((uintptr)ow, sizeof(OWaitmsg), 1);
		evenaddr((uintptr)ow);
		pid = pwait(&w);
	}
	if(ow != nil){
		readnum(0, ow->pid, NUMSIZE, w.pid, NUMSIZE);
		readnum(0, ow->time+TUser*NUMSIZE, NUMSIZE, w.time[TUser], NUMSIZE);
		readnum(0, ow->time+TSys*NUMSIZE, NUMSIZE, w.time[TSys], NUMSIZE);
		readnum(0, ow->time+TReal*NUMSIZE, NUMSIZE, w.time[TReal], NUMSIZE);
		strncpy(ow->msg, w.msg, sizeof(ow->msg)-1);
		ow->msg[sizeof(ow->msg)-1] = '\0';
	}
	return pid;
}
Beispiel #3
0
void sword_in_stone()
{
  if (psel("THERE IS A SWORD SET IN STONE HERE\n"
	   "\n"
	   "<- DRAW IT#LEAVE IT ->") == 0)
  {
    if (player->strength < SWORD_IN_STONE_STR)
    {
      draw_board();
      pwait("ALAS, YOU ARE NOT STRONG ENOUGH");
    }
    else
    {
      give_weapon(WPN_RUNESWORD);

      stile(player->y, player->x, TL_VOID);
      stile(player->y - 1, player->x, TL_VOID);
      stile(player->y - 2, player->x, TL_VOID);

      draw_board();
	 
      pwait("YOU DRAW THE SWORD FROM THE STONE\n"
	    "\n"
	    "THIS IS THE FABLED *RUNESWORD* !!!");
    }
  }

  return;
}
static int
do_wait (int pid, const char *prog)
{
  int status = 0;
  int ret = 0;

  pid = pwait (pid, &status, 0);

  if (WIFSIGNALED (status))
    {
      if (!signal_count &&
	  WEXITSTATUS (status) > greatest_status)
	greatest_status = WEXITSTATUS (status);
      ret = -1;
    }
  else if (WIFEXITED (status)
	   && WEXITSTATUS (status) >= 1)
    {
      if (WEXITSTATUS (status) > greatest_status)
	greatest_status = WEXITSTATUS (status);
      signal_count++;
      ret = -1;
    }
  return ret;
}
Beispiel #5
0
void rogue_escape(mob_t * m)
{
  int dir;
  int i;
  char line[DEFLEN];
  int stolen;

  if (game->player_gold == 0)
    return;

  if (m->x < player->x)
  {
    dir = -2;
    m->flip = true;
  }
  else
  {
    dir = +2;
    m->flip = false;
  }

  for (i = 0; i < 2; i++)
  {
    m->flags = GFX_HUMAN_FALL1;
    draw_board(); mpause();
    m->flags = GFX_HUMAN_FALL2;
    draw_board(); mpause();
  }

  m->flags = 0;

  while (m->x > view_x - 2 && m->x <= view_x + BOARD_W + 2)
  {
    m->x += dir;
    draw_board(); spause();
  }

  m->type = MOB_NONE;

  if (game->player_gold)
  {
    if (has_eq(EQ_SHADES) && rand() % 10 == 0)
    {
      game->equipment[EQ_SHADES] = false;
      snprintf(line, DEFLEN, "ROGUE STOLE YOUR SHADES AND RAN AWAY!\n\n(WHAT A JERK)");
    }
    else
    {
      stolen = 1 + rand() % ROGUE_STEAL_GOLD;
      game->player_gold -= stolen;
      snprintf(line, DEFLEN, "ROGUE STOLE %d GOLD AND RAN AWAY!", stolen);
    }

    draw_stats();
    flush_input();
    pwait(line);
  }

  return;
}
//Para ejecutar
//   ./mem-lector numAuto
int main(int argc, const char *argv[])
{

    //attach
    key_t key  = ftok("/temp",40);
    int shmid = shmget(key, sizeof(int), 0660);  //leer mem 4 byte
    int *litros = (int*)shmat(shmid, NULL, 0);
    printf("cant litros: %d, litros fue attached\n",*litros);

    //acceso a semaforo
    key_t keys  = ftok("/temp",50);
    int semid = semget(keys,1,0660);

    printf("soy auto %d esperando entrar a cargar\n", atoi(argv[1]));
    //resta un lugar para entrar
    pwait(semid,1);
    //pedir 40 litros o los disponibles
    int cargados = 40;
    int disponibles;
    disponibles = *litros;
    if(*litros>=cargados)
        *litros -= cargados;
    else {
        cargados = *litros;
        *litros = 0;
    }
    printf("litros, disponibles en memoria: %d cargados: %d\n",disponibles, cargados);
    sleep(15);
    //suma al semaforo un lugar al salir
    vsignal(semid,1);
    printf("final de carga\n\n");
    //detach
    shmdt(&litros);
    return 0;
}
Beispiel #7
0
/* 02Feb2012, More code from Lantz TNOS 2.40 source */
char *wpage_exp (char *to, int hier, int exphome)
{
	char origbbs[40], buf[12], *cp2, *cp = (char*) -1;

	if (strchr (to, '%'))
		return (to);

	/* This section looks up the destination bbs in the White Pages file,
	 * if user not a BBS. If found, the complete address is substituted.
	 */

	if (cp == (char *) -1)
	{
		cp2 = strchr (to, '@');

		if (cp2 == NULLCHAR)	/* no dest BBS given */
		{
			if ((cp2 = wpageCheck (to, 0, 0, 'X')) != NULLCHAR)	/* found bbs */
			{
				free (to);
				strlwr (cp2);
				to = cp2;
			}
			cp2 = strchr (to, '@');
		}
		if (hier && cp2 != NULLCHAR)
		{
 			/* we have a dest bbs, get correct, full haddress */
			cp = cp2++;
			if ((cp2 = wpageCheck (cp2, 1, 0, 'X')) != NULLCHAR)
			{
				*cp = '\0';
				sprintf (origbbs, "%s@%s", to, cp2);
				free (to);
				free (cp2);
				strlwr (origbbs);
				to = strdup (origbbs);
			}
		}
	}
	else if (exphome)
	{
		pax25 (buf, Mycall);
		if ((cp = strpbrk (buf, DIGI_IDS)) != NULLCHAR)
			*cp = '\0';	/* remove SSID */
		sprintf (origbbs, "%s@%s%s%s", to, buf,
			 (Mbhaddress != NULLCHAR) ? "." : "",
			 (Mbhaddress != NULLCHAR) ? Mbhaddress : "");
		free (to);
		to = strdup (origbbs);
	}

	pwait (NULL);

	return (to);
}
Beispiel #8
0
Datei: run.c Projekt: kmillar/rho
/* used in rpipeOpen */
static DWORD CALLBACK
threadedwait(LPVOID param)
{
    rpipe *p = (rpipe *) param;

    p->exitcode = pwait(p->pi.hProcess);
    FlushFileBuffers(p->write);
    FlushFileBuffers(p->read);
    p->active = 0;
    CloseHandle(p->thread);
    p->thread = NULL;
    return 0;
}
Beispiel #9
0
long
sys_wait(ulong *arg)
{
	int pid;
	Waitmsg w;
	OWaitmsg *ow;

	if(arg[0] == 0)
		return pwait(nil);

	validaddr(arg[0], sizeof(OWaitmsg), 1);
	evenaddr(arg[0]);
	pid = pwait(&w);
	if(pid >= 0){
		ow = (OWaitmsg*)arg[0];
		readnum(0, ow->pid, NUMSIZE, w.pid, NUMSIZE);
		readnum(0, ow->time+TUser*NUMSIZE, NUMSIZE, w.time[TUser], NUMSIZE);
		readnum(0, ow->time+TSys*NUMSIZE, NUMSIZE, w.time[TSys], NUMSIZE);
		readnum(0, ow->time+TReal*NUMSIZE, NUMSIZE, w.time[TReal], NUMSIZE);
		strncpy(ow->msg, w.msg, sizeof(ow->msg));
		ow->msg[sizeof(ow->msg)-1] = '\0';
	}
	return pid;
}
Beispiel #10
0
uintptr
sysawait(va_list list)
{
	char *p;
	Waitmsg w;
	uint n;

	p = va_arg(list, char*);
	n = va_arg(list, uint);
	validaddr((uintptr)p, n, 1);
	pwait(&w);
	return (uintptr)snprint(p, n, "%d %lud %lud %lud %q",
		w.pid,
		w.time[TUser], w.time[TSys], w.time[TReal],
		w.msg);
}
Beispiel #11
0
/**
   Give the player a random item suitable for the current floor or
   @gold if nothing useful was available.
*/
void give_item(char * msg, int gold, int type)
{
  char line[DEFLEN];

  if (game->weapon == WPN_UNARMED && rand() % 3 == 0)
  {
    give_weapon(WPN_BONECLUB);
    snprintf(line, DEFLEN, "%sYOU FIND A BONE CLUB", msg);
    goto print_msg;
  }

  switch (rand() % 20)
  {
  case 0:
    if (!has_eq(EQ_TORCH))
    {
      give_eq(EQ_TORCH);
      draw_stats();
      snprintf(line, DEFLEN, "%sYOU FIND A TORCH", msg);
      goto print_msg;
    }
    break;

  case 1:
    if (!has_eq(EQ_SHADES))
    {
      give_eq(EQ_SHADES);
      draw_stats();
      snprintf(line, DEFLEN, "%sYOU FIND SOME SHADES\n\nYOU LOOK REALLY COOL NOW", msg);
      goto print_msg;
    }
    break;

  default:
    break;
  }

  game->player_gold += gold;
  game->total_gold_earned += gold;
  draw_stats();
  snprintf(line, DEFLEN, "%sYOU FIND %d GOLD", msg, gold);

print_msg:
  pwait(line);

  return;
}
Beispiel #12
0
int dombwpages (int argc, char **argv, void *p)
{
	struct mbx *m;
	char buf[80], *cp, origchar = 0;

	m = (struct mbx *) p;

	if (argc < 2) {
		tprintf (wp_syntax, m->privs & SYSOP_CMD ? " [@bbsname]" : "");
		return 0;
	}
	if (!(m->privs & SYSOP_CMD) && argc > 2) {
		j2tputs (sorry);
		return 0;
	}
	if (argc == 2)		/* lookup */
	{
		/* 12Mar2012, Maiko, Original code put into separate function */
		wpage_lookup (&argv[1], (char*)0, (struct mbx*)0);
	}
	else
	{		/* else, setting it */
		if (argv[2][0] != '@')
			j2tputs (wp_sysopsyntax);
		else {
			if ((cp = strchr (argv[2], '.')) != NULLCHAR) {
				sprintf (buf, &argv[2][1]);
				(void) strupr (buf);
				wpageAdd (buf, 1, 1, 'U');
				origchar = *cp;
				*cp = 0;
			}
			sprintf (buf, "%s%s", argv[1], argv[2]);
			(void) strlwr (buf);
			wpageAdd (buf, 0, 1, 'U');
			if (cp && origchar)
				*cp = origchar;
			j2tputs (updating);
			tflush ();
			pwait (NULL);
			RenewWPages ();
		}
	}
	return 0;
}
Beispiel #13
0
long
sysawait(ulong *arg)
{
	int i;
	int pid;
	Waitmsg w;
	ulong n;

	n = arg[1];
	validaddr(arg[0], n, 1);
	pid = pwait(&w);
	if(pid < 0)
		return -1;
	i = snprint((char*)arg[0], n, "%d %lud %lud %lud %q",
		w.pid,
		w.time[TUser], w.time[TSys], w.time[TReal],
		w.msg);

	return i;
}
Beispiel #14
0
void
execute(struct command *t, int wtty, int *pipein, int *pipeout)
{
    static sigset_t csigset, ocsigset; 
    static int nosigchld = 0, onosigchld = 0;
    volatile int wanttty = wtty;
    struct biltins * volatile bifunc;
    int pv[2], pid;
    sigset_t nsigset;
    int forked;

    UNREGISTER(forked);
    UNREGISTER(bifunc);
    UNREGISTER(wanttty);

    forked = 0;
    pid = 0;

    if (t == 0)
	return;

    if (t->t_dflg & F_AMPERSAND)
	wanttty = 0;
    switch (t->t_dtyp) {
    case NODE_COMMAND:
	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
	    (void)Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
	if ((t->t_dflg & F_REPEAT) == 0)
	    Dfix(t);		/* $ " ' \ */
	if (t->t_dcom[0] == 0)
	    return;
	/* FALLTHROUGH */
    case NODE_PAREN:
	if (t->t_dflg & F_PIPEOUT)
	    mypipe(pipeout);
	/*
	 * Must do << early so parent will know where input pointer should be.
	 * If noexec then this is all we do.
	 */
	if (t->t_dflg & F_READ) {
	    (void)close(0);
	    heredoc(t->t_dlef);
	    if (noexec)
		(void)close(0);
	}

	set(STRstatus, Strsave(STR0));

	/*
	 * This mess is the necessary kludge to handle the prefix builtins:
	 * nice, nohup, time.  These commands can also be used by themselves,
	 * and this is not handled here. This will also work when loops are
	 * parsed.
	 */
	while (t->t_dtyp == NODE_COMMAND)
	    if (eq(t->t_dcom[0], STRnice)) {
		if (t->t_dcom[1]) {
		    if (strchr("+-", t->t_dcom[1][0])) {
			if (t->t_dcom[2]) {
			    setname("nice");
			    t->t_nice =
				getn(t->t_dcom[1]);
			    lshift(t->t_dcom, 2);
			    t->t_dflg |= F_NICE;
			}
			else
			    break;
		    } else {
			t->t_nice = 4;
			lshift(t->t_dcom, 1);
			t->t_dflg |= F_NICE;
		    }
		} else
		    break;
	    } else if (eq(t->t_dcom[0], STRnohup)) {
		if (t->t_dcom[1]) {
		    t->t_dflg |= F_NOHUP;
		    lshift(t->t_dcom, 1);
		}
		else
		    break;
	    } else if (eq(t->t_dcom[0], STRtime)) {
		if (t->t_dcom[1]) {
		    t->t_dflg |= F_TIME;
		    lshift(t->t_dcom, 1);
		}
		else
		    break;
	    } else
		break;

	/* is it a command */
	if (t->t_dtyp == NODE_COMMAND) {
	    /*
	     * Check if we have a builtin function and remember which one.
	     */
	    bifunc = isbfunc(t);
 	    if (noexec && bifunc != NULL) {
		/*
		 * Continue for builtins that are part of the scripting language
		 */
		if (bifunc->bfunct != dobreak   && bifunc->bfunct != docontin &&
		    bifunc->bfunct != doelse    && bifunc->bfunct != doend    &&
		    bifunc->bfunct != doforeach && bifunc->bfunct != dogoto   &&
		    bifunc->bfunct != doif      && bifunc->bfunct != dorepeat &&
		    bifunc->bfunct != doswbrk   && bifunc->bfunct != doswitch &&
		    bifunc->bfunct != dowhile   && bifunc->bfunct != dozip)
		    break;
	    }
	}
	else {			/* not a command */
	    bifunc = NULL;
	    if (noexec)
		break;
	}

	/*
	 * We fork only if we are timed, or are not the end of a parenthesized
	 * list and not a simple builtin function. Simple meaning one that is
	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
	 * fork in some of these cases.
	 */
	/*
	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
	 * shell not to change dir!
	 */
	if (bifunc && (bifunc->bfunct == dochngd ||
		       bifunc->bfunct == dopushd ||
		       bifunc->bfunct == dopopd))
	    t->t_dflg &= ~(F_NICE);
	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
	     (!bifunc || t->t_dflg &
	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
	/*
	 * We have to fork for eval too.
	 */
	    (bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
	     bifunc->bfunct == doeval)) {
	    if (t->t_dtyp == NODE_PAREN ||
		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
		forked++;
		/*
		 * We need to block SIGCHLD here, so that if the process does
		 * not die before we can set the process group
		 */
		if (wanttty >= 0 && !nosigchld) {
		    sigemptyset(&nsigset);
		    (void)sigaddset(&nsigset, SIGCHLD);
		    (void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
		    nosigchld = 1;
		}

		pid = pfork(t, wanttty);
		if (pid == 0 && nosigchld) {
		    (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
		    nosigchld = 0;
		}
		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
		    backpid = pid;

	    }
	    else {
		int ochild, osetintr, ohaderr, odidfds;
		int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
		sigset_t osigset;

		/*
		 * Prepare for the vfork by saving everything that the child
		 * corrupts before it exec's. Note that in some signal
		 * implementations which keep the signal info in user space
		 * (e.g. Sun's) it will also be necessary to save and restore
		 * the current sigaction's for the signals the child touches
		 * before it exec's.
		 */
		if (wanttty >= 0 && !nosigchld && !noexec) {
		    sigemptyset(&nsigset);
		    (void)sigaddset(&nsigset, SIGCHLD);
		    (void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
		    nosigchld = 1;
		}
		sigemptyset(&nsigset);
		(void)sigaddset(&nsigset, SIGCHLD);
		(void)sigaddset(&nsigset, SIGINT);
		(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
		ochild = child;
		osetintr = setintr;
		ohaderr = haderr;
		odidfds = didfds;
		oSHIN = SHIN;
		oSHOUT = SHOUT;
		oSHERR = SHERR;
		oOLDSTD = OLDSTD;
		otpgrp = tpgrp;
		ocsigset = csigset;
		onosigchld = nosigchld;
		Vsav = Vdp = 0;
		Vexpath = 0;
		Vt = 0;
		pid = vfork();

		if (pid < 0) {
		    (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
		    stderror(ERR_NOPROC);
		}
		forked++;
		if (pid) {	/* parent */
		    child = ochild;
		    setintr = osetintr;
		    haderr = ohaderr;
		    didfds = odidfds;
		    SHIN = oSHIN;
		    SHOUT = oSHOUT;
		    SHERR = oSHERR;
		    OLDSTD = oOLDSTD;
		    tpgrp = otpgrp;
		    csigset = ocsigset;
		    nosigchld = onosigchld;

		    xfree((ptr_t) Vsav);
		    Vsav = 0;
		    xfree((ptr_t) Vdp);
		    Vdp = 0;
		    xfree((ptr_t) Vexpath);
		    Vexpath = 0;
		    blkfree((Char **) Vt);
		    Vt = 0;
		    /* this is from pfork() */
		    palloc(pid, t);
		    (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
		}
		else {		/* child */
		    /* this is from pfork() */
		    int pgrp;
		    int ignint = 0;

		    if (nosigchld) {
		        (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
			nosigchld = 0;
		    }

		    if (setintr)
			ignint =
			    (tpgrp == -1 &&
			     (t->t_dflg & F_NOINTERRUPT))
			    || (gointr && eq(gointr, STRminus));
		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
		    child++;
		    if (setintr) {
			setintr = 0;
			if (ignint) {
			    (void)signal(SIGINT, SIG_IGN);
			    (void)signal(SIGQUIT, SIG_IGN);
			}
			else {
			    (void)signal(SIGINT, vffree);
			    (void)signal(SIGQUIT, SIG_DFL);
			}

			if (wanttty >= 0) {
			    (void)signal(SIGTSTP, SIG_DFL);
			    (void)signal(SIGTTIN, SIG_DFL);
			    (void)signal(SIGTTOU, SIG_DFL);
			}

			(void)signal(SIGTERM, parterm);
		    }
		    else if (tpgrp == -1 &&
			     (t->t_dflg & F_NOINTERRUPT)) {
			(void)signal(SIGINT, SIG_IGN);
			(void)signal(SIGQUIT, SIG_IGN);
		    }

		    pgetty(wanttty, pgrp);
		    if (t->t_dflg & F_NOHUP)
			(void)signal(SIGHUP, SIG_IGN);
		    if (t->t_dflg & F_NICE)
			(void)setpriority(PRIO_PROCESS, 0, t->t_nice);
		}

	    }
	}
	if (pid != 0) {
	    /*
	     * It would be better if we could wait for the whole job when we
	     * knew the last process had been started.  Pwait, in fact, does
	     * wait for the whole job anyway, but this test doesn't really
	     * express our intentions.
	     */
	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
		(void)close(pipein[0]);
		(void)close(pipein[1]);
	    }
	    if ((t->t_dflg & F_PIPEOUT) == 0) {
		if (nosigchld) {
		    (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
		    nosigchld = 0;
		}
		if ((t->t_dflg & F_AMPERSAND) == 0)
		    pwait();
	    }
	    break;
	}
	doio(t, pipein, pipeout);
	if (t->t_dflg & F_PIPEOUT) {
	    (void)close(pipeout[0]);
	    (void)close(pipeout[1]);
	}
	/*
	 * Perform a builtin function. If we are not forked, arrange for
	 * possible stopping
	 */
	if (bifunc) {
	    func(t, bifunc);
	    if (forked)
		exitstat();
	    break;
	}
	if (t->t_dtyp != NODE_PAREN)
	    doexec(NULL, t);
	/*
	 * For () commands must put new 0,1,2 in FSH* and recurse
	 */
	(void) ioctl(OLDSTD = dcopy(0, FOLDSTD), FIOCLEX, NULL);
	(void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
	(void) ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
	(void) close(SHIN);

	SHIN = -1;
	didfds = 0;
	wanttty = -1;
	t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
	execute(t->t_dspr, wanttty, NULL, NULL);
	exitstat();
	/* NOTREACHED */
    case NODE_PIPE:
	t->t_dcar->t_dflg |= F_PIPEOUT |
	    (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
	execute(t->t_dcar, wanttty, pipein, pv);
	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
			(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
	if (wanttty > 0)
	    wanttty = 0;	/* got tty already */
	execute(t->t_dcdr, wanttty, pv, pipeout);
	break;
    case NODE_LIST:
	if (t->t_dcar) {
	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
	    execute(t->t_dcar, wanttty, NULL, NULL);
	    /*
	     * In strange case of A&B make a new job after A
	     */
	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
		pendjob();
	}
	if (t->t_dcdr) {
	    t->t_dcdr->t_dflg |= t->t_dflg &
		(F_NOFORK | F_NOINTERRUPT);
	    execute(t->t_dcdr, wanttty, NULL, NULL);
	}
	break;
    case NODE_OR:
    case NODE_AND:
	if (t->t_dcar) {
	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
	    execute(t->t_dcar, wanttty, NULL, NULL);
	    if ((getn(value(STRstatus)) == 0) !=
		(t->t_dtyp == NODE_AND))
		return;
	}
	if (t->t_dcdr) {
	    t->t_dcdr->t_dflg |= t->t_dflg &
		(F_NOFORK | F_NOINTERRUPT);
	    execute(t->t_dcdr, wanttty, NULL, NULL);
	}
	break;
    }
    /*
     * Fall through for all breaks from switch
     * 
     * If there will be no more executions of this command, flush all file
     * descriptors. Places that turn on the F_REPEAT bit are responsible for
     * doing donefds after the last re-execution
     */
    if (didfds && !(t->t_dflg & F_REPEAT))
	donefds();
}
Beispiel #15
0
Datei: sh.c Projekt: Ju2ender/c-e
/*
 * 该函数从根节点开始深度优先遍历整个语法树,为每个命令创建进程并执行 
 * pf1和pf2是管道线I/O句柄,pf1表示命令输入端,pf2表示命令输出端 
 * pf1[0]: 管道线输入端句柄 
 * pf1[1]: 闲置 
 * pf2[0]: 闲置 
 * pf2[1]: 管道线输出端句柄 
*/ 
void execute(int *t, int *pf1, int *pf2) 
{ 
        int i, f, pv[2]; 
        int *t1; 
        char *cp1, *cp2; 
        extern int errno; 

        if(t != 0) 
        switch(t[DTYP]) { 

        case TCOM: 
                cp1 = (char *)t[DCOM]; 
                /* 以下为内置命令*/ 
                if(equal(cp1, "chdir")) { 
                        if(t[DCOM+1] != 0) { 
                                if(chdir(t[DCOM+1]) < 0) 
                                        err("chdir: bad directory"); 
                        } else 
                                err("chdir: arg count"); 
                        return; 
                } 
                if(equal(cp1, "shift")) { 
                        if(dolc < 1) { 
                                prs("shift: no args\n"); 
                                return; 
                        } 
                        dolv[1] = dolv[0]; 
                        dolv++; 
                        dolc--; 
                        return; 
                } 
                if(equal(cp1, "login")) { 
                        if(promp != 0) { 
                                close(acctf); 
                                execv("/bin/login", t+DCOM); 
                        } 
                        prs("login: cannot execute\n"); 
                        return; 
                } 
                if(equal(cp1, "newgrp")) { 
                        if(promp != 0) { 
                                close(acctf); 
                                execv("/bin/newgrp", t+DCOM); 
                        } 
                        prs("newgrp: cannot execute\n"); 
                        return; 
                } 
                if(equal(cp1, "wait")) { 
                        pwait(-1, 0); 
                        return; 
                } 
                if(equal(cp1, ":")) 
                        return; 
/* 注意,这里没有break,筛选掉内置命令后,外部命令会继续往下走*/ 
        case TPAR: 
                f = t[DFLG]; 
                i = 0; 
                if((f&FPAR) == 0)        /* 除了复合命令中最后一个子命令,其它一律fork子进程*/ 
                        i = fork(); 
                if(i == -1) { 
                        err("try again"); 
                        return; 
                } 
                if(i != 0) {        /* 父进程代码*/ 
                        if((f&FPIN) != 0) { 
                                close(pf1[0]);        /* fork之后,子进程获得父进程的管道线句柄拷贝,我们要在父进程中关闭它,以免资源泄漏*/ 
                                close(pf1[1]); /* 注意,这里只关闭pf1,因为pf1、pf2都是pv的镜像,因此关闭pf1就是关闭pf2 */ 
                        } 
                        if((f&FPRS) != 0) {/* 打印子进程pid */ 
                                prn(i); 
                                prs("\n"); 
                        } 
                        if((f&FAND) != 0)        /* 后台异步进程无需等待*/ 
                                return; 
                        if((f&FPOU) == 0)        /* 节点为管道线末端,等待子进程终止*/ 
                                pwait(i, t); 
                        return; 
                } 
/* 以下为子进程代码,或者FPAR属性节点命令进程*/ 
                if(t[DLEF] != 0) { 
                        close(0); 
                        i = open(t[DLEF], 0);        /* 重定向stdin到左子树节点*/ 
                        if(i < 0) { 
                                prs((char *)t[DLEF]); 
                                err(": cannot open"); 
                                exit(-1); 
                        } 
                } 
                if(t[DRIT] != 0) { 
                        if((f&FCAT) != 0) { 
                                i = open(t[DRIT], 1); 
                                if(i >= 0) { 
                                        seek(i, 0, 2);                /* 追加模式*/ 
                                        goto f1; 
                                } 
                        } 
                        i = creat(t[DRIT], 0666);        /* 创建新的文件句柄*/ 
                        if(i < 0) { 
                                prs((char *)t[DRIT]); 
                                err(": cannot create"); 
                                exit(-1); 
                        } 
                f1: 
                        close(1); 
                        dup(i);                /* 重定向stdout到右子树节点*/ 
                        close(i); 
                } 
                if((f&FPIN) != 0) { 
                        close(0); 
                        dup(pf1[0]);        /* 重定向stdin到管道线输入端*/ 
                        close(pf1[0]); 
                        close(pf1[1]);        /* 子进程关闭管道线句柄*/ 
                } 
                if((f&FPOU) != 0) { 
                        close(1); 
                        dup(pf2[1]);        /* 重定向stdout  到管道线输出*/ 
                        close(pf2[0]); 
                        close(pf2[1]);        /* 子进程关闭管道线句柄*/  
                } 
                if((f&FINT)!=0 && t[DLEF]==0 && (f&FPIN)==0) { 
                        close(0);        /* 若忽略中断,且不存在管道线输入,则关闭stdin并重定向到位桶*/ 
                        open("/dev/null", 0);        /* 以免来自其它进程stdin干扰,但stdout是保留的*/ 
                } 
                if((f&FINT) == 0 && setintr) { 
                        signal(INTR, 0);        /* 无FINT标识,则恢复中断信号响应(默认是忽略的) */ 
                        signal(QUIT, 0); 
                } 
                if(t[DTYP] == TPAR) {        /* 如果是复合类型,则执行子命令,同时下推FINT标识*/ 
                        if(t1 = (int *)t[DSPR]) 
                                t1[DFLG] |= f&FINT; 
                        execute(t1, NULL, NULL); 
                        exit(0); 
                } 
                close(acctf); 
                gflg = 0; 
                scan(t, &tglob);        /* 扫描通配符*/ 
                if(gflg) { 
                        t[DSPR] = (int)"/etc/glob"; 
                        execv(t[DSPR], t+DSPR);        /* 包含通配符的命令在/etc/glob中执行*/ 
                        prs("glob: cannot execute\n"); 
                        exit(-1); 
                } 
                scan(t, &trim);        /* 解除引用标识*/ 
                *linep = 0; 
                texec((char *)t[DCOM], t);        /* 最先执行当前目录下的命令*/ 
                cp1 = linep; 
                cp2 = "/usr/bin/"; 
                while(*cp1 = *cp2++)    /* strcpy,cp1停在'\0'位置 */
                        cp1++; 
                cp2 = (char *)t[DCOM]; 
                while(*cp1++ = *cp2++);     /* strcpy,'\0'结束 */
                texec(linep+4, t);        /* 接着执行/bin目录下的命令*/ 
                texec(linep, t);        /* 最后执行/usr/bin目录下的命令*/ 
                prs((char *)t[DCOM]);   /* 若进程异常退出,表示找不到命令,退出shell */
                err(": not found"); 
                exit(-1); 

        case TFIL: 
                f = t[DFLG]; 
                pipe(pv);        /* 创建管道线,pv[0]为输入句柄,pv[1]为输出句柄*/ 
                t1 = (int *)t[DLEF]; 
                t1[DFLG] |= FPOU | (f&(FPIN|FINT|FPRS)); 
                execute(t1, pf1, pv);        /* 对于左子树节点,pv[1]作为管道线输出句柄传入,pv[0]闲置*/ 
                t1 = (int *)t[DRIT]; 
                t1[DFLG] |= FPIN | (f&(FPOU|FINT|FAND|FPRS));        /* 只有管道线末端命令才能继承FAND属性,与FPOU排斥*/ 
                execute(t1, pv, pf2);        /* 对于右子树节点,pv[0]作为管道线输入句柄传入,pv[1]闲置*/ 
                return; 

        case TLST: 
                f = t[DFLG]&FINT;                /* 对于命令序列节点,下推FINT属性到左右子树*/ 
                if(t1 = (int *)t[DLEF]) 
                        t1[DFLG] |= f; 
                execute(t1, NULL, NULL);        /* 先左后右依次执行 */
                if(t1 = (int *)t[DRIT]) 
                        t1[DFLG] |= f; 
                execute(t1, NULL, NULL); 
                return; 

        } 
} 
Beispiel #16
0
void wait_event(enum Event e)
{
    CURRENT_TASK->block_event = e;
    pwait();
}
Beispiel #17
0
void disco()
{
  bool old_flip;
  mob_t * m;
  int i;

  pwait("THIS LOOKS LIKE A DANCE FLOOR!");

  stile(player->y, player->x, TL_VOID);

  m = player;
  old_flip = m->flip;

  m->flip = true;
  m->type = MOB_DANCER;

  draw_board();
  opause();

  for (i = 0; i < 9; i++)
  {
    stile(m->y - 5 + i % 2, m->x - 4 + i, TL_DISCOLIGHT1 + i);
    draw_board();
    lpause();
  }

  NOW_DANCE(0); NOW_DANCE(1); NOW_DANCE(0); NOW_DANCE(1);
  DANCE_FLIP();
  NOW_DANCE(0); NOW_DANCE(1); NOW_DANCE(0); NOW_DANCE(1);

  NOW_DANCE(0);
  
  NOW_DANCE(10);

  NOW_DANCE(11);
  NOW_DANCE(12);
  DANCE_FLIP();
  NOW_DANCE(11); 

  NOW_DANCE(10);

  NOW_DANCE(11);
  NOW_DANCE(12);
  DANCE_FLIP();
  NOW_DANCE(11);

  DANCE_FLIP();
  NOW_DANCE(20); NOW_DANCE(21); NOW_DANCE(20); NOW_DANCE(22);
  DANCE_FLIP();
  NOW_DANCE(20); NOW_DANCE(21); NOW_DANCE(20); NOW_DANCE(22);

//  DANCE_FLIP();
  m->x -= 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x += 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x += 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x -= 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x -= 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x += 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x += 1; NOW_DANCE(30);
  DANCE_FLIP();
  m->x -= 1; NOW_DANCE(30);
  
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  m->x += 1;
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  m->x += 1;
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);

  //DANCE_FLIP();
  m->x -= 1; m->y -= 1;
  NOW_DANCE(40);
  m->x -= 1;
  NOW_DANCE(40);
  m->x -= 1; m->y += 1;
  NOW_DANCE(40);

  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);

  DANCE_FLIP();
  m->x += 1; m->y -= 1;
  NOW_DANCE(40);
  m->x += 1;
  NOW_DANCE(40);
  m->x += 1; m->y += 1;
  NOW_DANCE(40);

  NOW_DANCE(50);
  m->x -= 1;
  NOW_DANCE(51);
  NOW_DANCE(50);
  m->x -= 1;
  NOW_DANCE(51);
  NOW_DANCE(50);
  m->x -= 1;
  NOW_DANCE(51);
  NOW_DANCE(50);
  m->x -= 1;
  NOW_DANCE(51);
  NOW_DANCE(50);
//  NOW_DANCE(51);

  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  m->x += 1;
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);
  m->x += 1;
  DANCE_FLIP(); Q_DANCE(31);
  DANCE_FLIP(); Q_DANCE(31);

  NOW_DANCE(60);
  DANCE_FLIP();
  NOW_DANCE(60);
  m->y += 1;
  NOW_DANCE(60);

  DANCE_FLIP();
  m->y -= 1;
  NOW_DANCE(70); lpause();
  NOW_DANCE(71); lpause();
  NOW_DANCE(70); lpause();
  NOW_DANCE(71);

  opause();

  for (i = 0; i < 9; i++)
  {
    stile(m->y - 5, m->x - 4 + i, TL_VOID);
    stile(m->y - 4, m->x - 4 + i, TL_VOID);
  }

  m->type = MOB_PLAYER;
  m->flags = 0;
  m->flip = old_flip;

  draw_board();

  lpause();

  char line[DEFLEN];
  snprintf(line, DEFLEN, "YOU GET %d EXPERIENCE", DISCO_EXP);
  pwait(line);
  draw_board();
  give_exp(DISCO_EXP);
  draw_stats();
  
  return;
}
Beispiel #18
0
void idol()
{
  stile(player->y, player->x, TL_IDOL_BASE);
   
  if (psel("YOU FIND THE BLOOD-STAINED IDOL\n"
	   "OF A DARK AND NAMELESS GOD\n"
	   "\n"
	   "<- SMASH IT#SACRIFICE ->") == 0)
  {
    decorate(player->y, player->x, DEC_BROKEN_IDOL);
    game->piety += IDOL_SMASH_PIETY + rand() % IDOL_SMASH_PIETY;
    game->sacrifice -= IDOL_SACRIFICE_PEN;
  }
  else
  {
    stile(player->y - 3, player->x, TL_IDOL_HEAD_LIT);
    
    player->hp--;
    draw_bars();

    draw_board();
    
    pwait("YOU DRAW A SINGLE DROP\n"
	  "OF BLOOD AS TRIBUTE");
    
    game->sacrifice += IDOL_SACRIFICE_GAIN;
    game->piety -= IDOL_SMASH_PIETY + rand() % IDOL_SMASH_PIETY;

    if (player->hp <= 0)
    {
      draw_board();
      pwait("YOU HAVE GIVEN YOUR LIFE!!!");

      draw_board();
      game_over("YOU SACRIFICED YOURSELF\n"
		"TO A DARK AND NAMELESS GOD", false);
    }
    else if (rand() % MAX(1, game->sacrifice) > IDOL_SACRIFICE_LIMIT)
    {
      draw_board();
 
      game->sacrifice -= IDOL_SACRIFICE_LIMIT;

      draw_board();

      switch(rand() % 2)
      {
      case 0:
	pwait("YOU HAVE BEEN REWARDED");
	refill_hp(IDOL_HP_REFILL);
	break;

      case 1:
	pwait("YOU HEAR A MOCKING LAUGHTER");
	refill_hp(5);
	player->strength = MAX(1, player->strength - 4 + rand() % 9);
	player->speed = MAX(1, player->speed - 4 + rand() % 9);
	break;
      }
      
    }
  }

  return;
}
Beispiel #19
0
void fountain()
{
  char line[DEFLEN];
  char liquid[DEFLEN];
  char * insert;
  int temp;
  int blood;
  int rlen;

  blood = (gtile(player->y, player->x) == TL_P_BLOOD_FOUNTAIN);

  snprintf(liquid, DEFLEN, "%s", blood ? "BLOOD" : "WATER");

  stile(player->y, player->x, TL_FOUNT_BASE);
  stile(player->y - 1, player->x, TL_VOID);
  stile(player->y - 2, player->x, TL_VOID);
  stile(player->y - 2, player->x - 1, TL_VOID);
  stile(player->y - 2, player->x + 1, TL_VOID);

  snprintf(line, DEFLEN, "YOU DRINK FROM THE %sFOUNTAIN\n\n", (blood ? "BLOOD " : ""));

  rlen = strlen(line);
  insert = &line[rlen];
  rlen = DEFLEN - rlen;

  draw_board();

  switch (rand() % 10)
  {
  case 0:
    snprintf(insert, rlen, "THE %s TASTES FOUL!!", liquid);

    pwait(line);

    player->flags = GFX_HURT;
    draw_board(); lpause();
    player->flags = 0;

    player->hp -= 1 + rand() % 10;
    draw_bars();
     
    if (player->hp <= 0)
    {
      draw_board();

      game_over("YOU DIED FROM A\n"
		"POISONED FOUNTAIN", false);
    }
    break;

  case 1:
    snprintf(insert, rlen, "THE %s TASTES BITTER!\n\nYOU FEEL SLOWER!", liquid);

    pwait(line);

    change_pl_sp(-1);

    draw_stats();
    break;

  case 2:
  case 3:
    temp = 100 + (rand() % 5) * 100;
    temp += MAX(0, game->current_floor - 7) * 100;

    snprintf(insert, rlen,
	     "THIS IS A FOUNTAIN OF WISDOM!\n\n"
	     "YOU GET %d EXP",
	     temp);

    pwait(line);

    draw_board();

    give_exp(temp);
    break;

  case 4:
  case 5:
    snprintf(insert, rlen, "THE %s IS INVIGORATING!\n\nYOU FEEL STRONGER!", liquid);
    pwait(line);

    change_pl_st(+1);

    draw_stats();
    break;

  default:
    snprintf(insert, rlen, "THE %s IS REFRESHING!", liquid);
    pwait(line);

    refill_hp(FOUNTAIN_HEAL);
    break;
  }

  return;
}
Beispiel #20
0
void camp()
{
  int dish;
  int meat;
  char food[100];

  strcpy(food, "YOU SPEND AN HOUR EATING\n");

  meat = rand() % 7;
  dish = rand() % 8;

  switch (dish)
  {
  case 0: strcat(food, "DEEP FRIED "); break;

  default:
    switch (rand() % 15)
    {
    case 0: strcat(food, "SALTY "); break;
    case 1: strcat(food, "SPICY "); break;
    case 2: strcat(food, "PEPPERED "); break;
    }
    
    break;
  }

  switch (meat)
  {
  case 0: strcat(food, "GNOBLIN"); break;
  case 1: strcat(food, "SPIDER"); break;
  case 2: strcat(food, "BLURK"); break;
  case 3: strcat(food, "SNAKE"); break;
  case 4: strcat(food, "MIMIC"); break;
  case 5: strcat(food, "IMP"); break;
  case 6: strcat(food, "GHOUL"); break;
  }

  switch (dish)
  {
  case 0: break;
  case 1: strcat(food, "-ON-A-STICK"); break;
  case 2: strcat(food, " RAGU"); break;
  case 3: strcat(food, " ROAST"); break;
  case 4: strcat(food, " STEW"); break;
  case 5: strcat(food, " SOUP"); break;
  case 6: strcat(food, " GUMBO"); break;
  case 7: strcat(food, " NUGGETS"); break;
  }

  switch (rand() % 20)
  {
  case 0: strcat(food, " WITH BEANS"); break;
  case 1: strcat(food, " WITH BROCCOLI"); break;
  case 2: strcat(food, " WITH CHEESE"); break;
  case 3: strcat(food, " WITH PICKLES"); break;
  case 4: strcat(food, " WITH ONION RINGS"); break;
  default: break;
  }

  strcat(food, "\n\n");

  switch (rand() % 4)
  {
  case 0: strcat(food, "IT WAS DELICIOUS!"); break;
  case 1: strcat(food, "YOU'VE HAD BETTER"); break;
  case 2: strcat(food, "YOU FEEL SLIGHTLY NAUSEOUS"); break;
  case 3: strcat(food, "THAT HIT THE SPOT!"); break;
  }

  pwait(food);

  decorate(player->y, player->x, DEC_SPENTCAMP);
  draw_board();

  refill_hp(CAMP_HEAL);  

  return;
}
Beispiel #21
0
void loot_chest(int ty, int tx)
{
  char line[DEFLEN];
  int tile;
  int gold_amount;

  tile = gtile(ty, tx);

  if (!game->skill_lockpick && game->weapon == WPN_UNARMED)
  {
    pwait("YOU HAVE NOTHING TO\n"
	  "OPEN THE CHEST WITH");
    return;
  }
  
  if (game->skill_lockpick)
  {
    strcpy(line, "YOU PICK THE LOCK\n\n");
  }
  else if (game->weapon == WPN_AXE      ||
	   game->weapon == WPN_MACE     ||
	   game->weapon == WPN_DIAMOND  ||
	   game->weapon == WPN_FLAIL    ||
    	   game->weapon == WPN_BONECLUB )
  {
    strcpy(line, "YOU SMASH THE CHEST OPEN\n\n");
  }
  else
    strcpy(line, "YOU PRY THE CHEST OPEN\n\n");
  
  /*
    Weapon has a random chance of breaking, but even if it does we
    still get the loot. Don't damage weapon if we're capable of
    lockpicking.
  */
  if (!game->skill_lockpick)
    damage_weapon(rand() % 10);
  
  if (tile == TL_P_UWCHEST)
  {
    gold_amount = 10 + rand() % UWCHEST_GOLD;

    stile(player->y, player->x, TL_UWWOOD);
    stile(player->y - 1, player->x - 2, TL_UW_UL);
    stile(player->y - 1, player->x - 1, TL_UW_HL);
    stile(player->y - 1, player->x, TL_UW_HL);
    stile(player->y - 1, player->x + 1, TL_UW_HL);
    stile(player->y - 1, player->x + 2, TL_UW_UR);
  }
  else
  {
    gold_amount = 10 + rand() % CHEST_GOLD;
	
    stile(player->y, player->x, TL_WOOD);
    stile(player->y - 1, player->x - 2, TL_BR_UL);
    stile(player->y - 1, player->x - 1, TL_BR_HL);
    stile(player->y - 1, player->x, TL_BR_HL);
    stile(player->y - 1, player->x + 1, TL_BR_HL);
    stile(player->y - 1, player->x + 2, TL_BR_UR);
  }
  
  draw_board();

  give_item(line, gold_amount, LOOT_NORMAL);
  
  return;
}
Beispiel #22
0
/*VARARGS 1*/
void
execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
    int do_glob)
{
    int    forked = 0;
    const struct biltins * volatile bifunc;
    pid_t pid = 0;
    int     pv[2];
    sigset_t set;
    static sigset_t csigset;
#ifdef VFORK
    static int onosigchld = 0;
#endif /* VFORK */
    static int nosigchld = 0;

    (void) &wanttty;
    (void) &forked;
    (void) &bifunc;

    if (t == 0) 
	return;

#ifdef WINNT_NATIVE
    {
        if ((varval(STRNTslowexec) == STRNULL) &&
            !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
            (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
	    !isbfunc(t)) {
	    if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
		(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
	    Dfix(t);
            if (nt_try_fast_exec(t) == 0)
                return;
        }
    }
#endif /* WINNT_NATIVE */

    /*
     * Ed [email protected] & Dominic [email protected]
     * Sat Feb 25 03:13:11 PST 1995
     * try implicit cd if we have a 1 word command 
     */
    if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
	 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
	Char *sCName;
	struct stat stbuf;
	char *pathname;

	sCName = dollar(t->t_dcom[0]);
	if (sCName != NULL && sCName[0] == '~') {
	    struct Strbuf buf = Strbuf_INIT;
	    const Char *name_end;

	    for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
		 name_end++)
		continue;
	    if (name_end != sCName + 1) {
		Char *name, *home;

		name = Strnsave(sCName + 1, name_end - (sCName + 1));
		home = gethdir(name);
		if (home != NULL) {
		    Strbuf_append(&buf, home);
		    xfree(home);
		} else
		    Strbuf_append(&buf, name);
		xfree(name);
	    } else
		Strbuf_append(&buf, varval(STRhome));
	    Strbuf_append(&buf, name_end);
	    xfree(sCName);
	    sCName = Strbuf_finish(&buf);
	}

	pathname = short2str(sCName);
	xfree(sCName);
	/* if this is a dir, tack a "cd" on as the first arg */
	if (pathname != NULL &&
	    ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
#ifdef WINNT_NATIVE
	     || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
#endif /* WINNT_NATIVE */
	     )) {
	    Char *vCD[2];
	    Char **ot_dcom = t->t_dcom;
	
	    vCD[0] = Strsave(STRcd);
	    vCD[1] = NULL;
	    t->t_dcom = blkspl(vCD, ot_dcom);
	    xfree(ot_dcom);
	    if (implicit_cd > 1) {
		blkpr(t->t_dcom);
		xputchar( '\n' );
	    }
	}
    }

    /*
     * From: Michael Schroeder <*****@*****.**>
     * Don't check for wantty > 0...
     */
    if (t->t_dflg & F_AMPERSAND)
	wanttty = 0;
    switch (t->t_dtyp) {

    case NODE_COMMAND:
	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
	    memmove(t->t_dcom[0], t->t_dcom[0] + 1,
		    (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
	if ((t->t_dflg & F_REPEAT) == 0)
	    Dfix(t);		/* $ " ' \ */
	if (t->t_dcom[0] == 0) {
	    return;
	}
	/*FALLTHROUGH*/

    case NODE_PAREN:
#ifdef BACKPIPE
	if (t->t_dflg & F_PIPEIN)
	    mypipe(pipein);
#else /* !BACKPIPE */
	if (t->t_dflg & F_PIPEOUT)
	    mypipe(pipeout);
#endif /* BACKPIPE */
	/*
	 * Must do << early so parent will know where input pointer should be.
	 * If noexec then this is all we do.
	 */
	if (t->t_dflg & F_READ) {
	    xclose(0);
	    heredoc(t->t_dlef);
	    if (noexec)
		xclose(0);
	}

	setcopy(STRstatus, STR0, VAR_READWRITE);

	/*
	 * This mess is the necessary kludge to handle the prefix builtins:
	 * nice, nohup, time.  These commands can also be used by themselves,
	 * and this is not handled here. This will also work when loops are
	 * parsed.
	 */
	while (t->t_dtyp == NODE_COMMAND)
	    if (eq(t->t_dcom[0], STRnice)) {
		if (t->t_dcom[1]) {
		    if (strchr("+-", t->t_dcom[1][0])) {
			if (t->t_dcom[2]) {
			    setname("nice");
			    t->t_nice = (unsigned char)getn(t->t_dcom[1]);
			    lshift(t->t_dcom, 2);
			    t->t_dflg |= F_NICE;
			}
			else
			    break;
		    }
		    else {
			t->t_nice = 4;
			lshift(t->t_dcom, 1);
			t->t_dflg |= F_NICE;
		    }
		}
		else
		    break;
	    }
	    else if (eq(t->t_dcom[0], STRnohup)) {
		if (t->t_dcom[1]) {
		    t->t_dflg |= F_NOHUP;
		    lshift(t->t_dcom, 1);
		}
		else
		    break;
	    }
	    else if (eq(t->t_dcom[0], STRhup)) {
		if (t->t_dcom[1]) {
		    t->t_dflg |= F_HUP;
		    lshift(t->t_dcom, 1);
		}
		else
		    break;
	    }
	    else if (eq(t->t_dcom[0], STRtime)) {
		if (t->t_dcom[1]) {
		    t->t_dflg |= F_TIME;
		    lshift(t->t_dcom, 1);
		}
		else
		    break;
	    }
#ifdef F_VER
	    else if (eq(t->t_dcom[0], STRver))
		if (t->t_dcom[1] && t->t_dcom[2]) {
		    setname("ver");
		    t->t_systype = getv(t->t_dcom[1]);
		    lshift(t->t_dcom, 2);
		    t->t_dflg |= F_VER;
		}
		else
		    break;
#endif  /* F_VER */
	    else
		break;

	/* is it a command */
	if (t->t_dtyp == NODE_COMMAND) {
	    /*
	     * Check if we have a builtin function and remember which one.
	     */
	    bifunc = isbfunc(t);
 	    if (noexec) {
		/*
		 * Continue for builtins that are part of the scripting language
		 */
		if (bifunc == NULL)
		    break;
		if (bifunc->bfunct != (bfunc_t)dobreak	&&
		    bifunc->bfunct != (bfunc_t)docontin	&&
		    bifunc->bfunct != (bfunc_t)doelse	&&
		    bifunc->bfunct != (bfunc_t)doend	&&
		    bifunc->bfunct != (bfunc_t)doforeach&&
		    bifunc->bfunct != (bfunc_t)dogoto	&&
		    bifunc->bfunct != (bfunc_t)doif	&&
		    bifunc->bfunct != (bfunc_t)dorepeat	&&
		    bifunc->bfunct != (bfunc_t)doswbrk	&&
		    bifunc->bfunct != (bfunc_t)doswitch	&&
		    bifunc->bfunct != (bfunc_t)dowhile	&&
		    bifunc->bfunct != (bfunc_t)dozip)
		    break;
	    }
	}
	else {			/* not a command */
	    bifunc = NULL;
	    if (noexec)
		break;
	}

	/* 
	 * GrP Executing a command - run jobcmd hook
	 * Don't run for builtins
	 * Don't run if we're not in a tty
	 * Don't run if we're not really executing 
	 */
	/*
	 * CR  -  Charles Ross Aug 2005
	 * added "isoutatty".
	 * The new behavior is that the jobcmd won't be executed
	 * if stdout (SHOUT) isnt attached to a tty.. IE when
	 * redirecting, or using backquotes etc..
	 */
	if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty && isoutatty) {
	    Char *cmd = unparse(t);

	    cleanup_push(cmd, xfree);
	    job_cmd(cmd);
	    cleanup_until(cmd);
	}
	   
	/*
	 * We fork only if we are timed, or are not the end of a parenthesized
	 * list and not a simple builtin function. Simple meaning one that is
	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
	 * fork in some of these cases.
	 */
#ifdef BACKPIPE
	/*
	 * Can't have NOFORK for the tail of a pipe - because it is not the
	 * last command spawned (even if it is at the end of a parenthesised
	 * list).
	 */
	if (t->t_dflg & F_PIPEIN)
	    t->t_dflg &= ~(F_NOFORK);
#else
	/*
	 * "command | builtin" may cause major misbehaviour as noted in
	 * in the BUGS file entry
	 * Subject: Redirected input to built-in functions misbehaves badly
	 * forking when the builtin is the end of the pipe corrects the
	 * problem.
	 */
	if (bifunc && (t->t_dflg & F_PIPEIN))
	    t->t_dflg &= ~(F_NOFORK);
#endif /* BACKPIPE */
	/*
	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
	 * shell not to change dir! (XXX: but only for nice?)
	 */
	if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
		       bifunc->bfunct == (bfunc_t)dopushd ||
		       bifunc->bfunct == (bfunc_t)dopopd))
	    t->t_dflg &= ~(F_NICE);

	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
	     (!bifunc || t->t_dflg &
	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
	/*
	 * We have to fork for eval too.
	 */
	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
	     bifunc->bfunct == (bfunc_t)doeval)) {
#ifdef VFORK
	    if (t->t_dtyp == NODE_PAREN ||
		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
#endif /* VFORK */
	    {
		forked++;
		/*
		 * We need to block SIGCHLD here, so that if the process does
		 * not die before we can set the process group
		 */
		if (wanttty >= 0 && !nosigchld) {
		    sigemptyset(&set);
		    sigaddset(&set, SIGCHLD);
		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);

		    nosigchld = 1;
		}

		pid = pfork(t, wanttty);
		if (pid == 0 && nosigchld) {
		    sigprocmask(SIG_SETMASK, &csigset, NULL);
		    nosigchld = 0;
		}
		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
		    backpid = pid;
	    }

#ifdef VFORK
	    else {
		int     ochild, osetintr, ohaderr, odidfds;
		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
		int     oisoutatty, oisdiagatty;
		sigset_t oset, ocsigset;
# ifndef CLOSE_ON_EXEC
		int     odidcch;
# endif  /* !CLOSE_ON_EXEC */

		/*
		 * Prepare for the vfork by saving everything that the child
		 * corrupts before it exec's. Note that in some signal
		 * implementations which keep the signal info in user space
		 * (e.g. Sun's) it will also be necessary to save and restore
		 * the current sigvec's for the signals the child touches
		 * before it exec's.
		 */

		/*
		 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
		 * Gilbrech - 11/22/87)
		 */
# ifdef SAVESIGVEC
		struct sigaction savesv[NSIGSAVED];
		sigset_t savesm;

# endif /* SAVESIGVEC */
		if (wanttty >= 0 && !nosigchld && !noexec) {
		    sigemptyset(&set);
		    sigaddset(&set, SIGCHLD);
		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
		    nosigchld = 1;
		}
		sigemptyset(&set);
		sigaddset(&set, SIGCHLD);
		sigaddset(&set, SIGINT);
		(void)sigprocmask(SIG_BLOCK, &set, &oset);
		ochild = child;
		osetintr = setintr;
		ohaderr = haderr;
		odidfds = didfds;
# ifndef CLOSE_ON_EXEC
		odidcch = didcch;
# endif /* !CLOSE_ON_EXEC */
		oSHIN = SHIN;
		oSHOUT = SHOUT;
		oSHDIAG = SHDIAG;
		oOLDSTD = OLDSTD;
		otpgrp = tpgrp;
		oisoutatty = isoutatty;
		oisdiagatty = isdiagatty;
		ocsigset = csigset;
		onosigchld = nosigchld;
		Vsav = Vdp = 0;
		Vexpath = 0;
		Vt = 0;
# ifdef SAVESIGVEC
		savesigvec(savesv, savesm);
# endif /* SAVESIGVEC */
		if (use_fork)
		    pid = fork();
		else
		    pid = vfork();

		if (pid < 0) {
# ifdef SAVESIGVEC
		    restoresigvec(savesv, savesm);
# endif /* SAVESIGVEC */
		    sigprocmask(SIG_SETMASK, &oset, NULL);
		    stderror(ERR_NOPROC);
		}
		forked++;
		if (pid) {	/* parent */
# ifdef SAVESIGVEC
		    restoresigvec(savesv, savesm);
# endif /* SAVESIGVEC */
		    child = ochild;
		    setintr = osetintr;
		    haderr = ohaderr;
		    didfds = odidfds;
		    SHIN = oSHIN;
# ifndef CLOSE_ON_EXEC
		    didcch = odidcch;
# endif /* !CLOSE_ON_EXEC */
		    SHOUT = oSHOUT;
		    SHDIAG = oSHDIAG;
		    OLDSTD = oOLDSTD;
		    tpgrp = otpgrp;
		    isoutatty = oisoutatty;
		    isdiagatty = oisdiagatty;
		    csigset = ocsigset;
		    nosigchld = onosigchld;

		    xfree(Vsav);
		    Vsav = 0;
		    xfree(Vdp);
		    Vdp = 0;
		    xfree(Vexpath);
		    Vexpath = 0;
		    blk_cleanup(Vt);
		    Vt = 0;
		    /* this is from pfork() */
		    palloc(pid, t);
		    sigprocmask(SIG_SETMASK, &oset, NULL);
		}
		else {		/* child */
		    /* this is from pfork() */
		    pid_t pgrp;
		    int    ignint = 0;
		    if (nosigchld) {
			sigprocmask(SIG_SETMASK, &csigset, NULL);
			nosigchld = 0;
		    }

		    if (setintr)
			ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
				|| (gointr && eq(gointr, STRminus));
		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
		    child++;
		    if (setintr) {
			setintr = 0;
/*
 * casts made right for SunOS 4.0 by Douglas C. Schmidt
 * <*****@*****.**>
 * (thanks! -- PWP)
 *
 * ignint ifs cleaned by Johan Widen <[email protected]>
 * (thanks again)
 */
			if (ignint) {
			    (void) signal(SIGINT, SIG_IGN);
			    (void) signal(SIGQUIT, SIG_IGN);
			}
			else {
			    (void) signal(SIGINT, vffree);
			    (void) signal(SIGQUIT, SIG_DFL);
			}
# ifdef BSDJOBS
			if (wanttty >= 0) {
			    (void) signal(SIGTSTP, SIG_DFL);
			    (void) signal(SIGTTIN, SIG_DFL);
			    (void) signal(SIGTTOU, SIG_DFL);
			}
# endif /* BSDJOBS */

			sigaction(SIGTERM, &parterm, NULL);
		    }
		    else if (tpgrp == -1 &&
			     (t->t_dflg & F_NOINTERRUPT)) {
			(void) signal(SIGINT, SIG_IGN);
			(void) signal(SIGQUIT, SIG_IGN);
		    }

		    pgetty(wanttty, pgrp);

		    if (t->t_dflg & F_NOHUP)
			(void) signal(SIGHUP, SIG_IGN);
		    if (t->t_dflg & F_HUP)
			(void) signal(SIGHUP, SIG_DFL);
		    if (t->t_dflg & F_NICE) {
			int nval = SIGN_EXTEND_CHAR(t->t_nice);
# if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
			if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
				stderror(ERR_SYSTEM, "setpriority",
				    strerror(errno));
# else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
			(void) nice(nval);
# endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
		    }
# ifdef F_VER
		    if (t->t_dflg & F_VER) {
			tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
			dohash(NULL, NULL);
		    }
# endif /* F_VER */
		}

	    }
#endif /* VFORK */
	}
	if (pid != 0) {
	    /*
	     * It would be better if we could wait for the whole job when we
	     * knew the last process had been started.  Pwait, in fact, does
	     * wait for the whole job anyway, but this test doesn't really
	     * express our intentions.
	     */
#ifdef BACKPIPE
	    if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
		xclose(pipeout[0]);
		xclose(pipeout[1]);
	    }
	    if ((t->t_dflg & F_PIPEIN) != 0)
		break;
#else /* !BACKPIPE */
	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
		xclose(pipein[0]);
		xclose(pipein[1]);
	    }
	    if ((t->t_dflg & F_PIPEOUT) != 0)
		break;
#endif /* BACKPIPE */

	    if (nosigchld) {
		sigprocmask(SIG_SETMASK, &csigset, NULL);
		nosigchld = 0;
	    }
	    if ((t->t_dflg & F_AMPERSAND) == 0)
		pwait();
	    break;
	}

	doio(t, pipein, pipeout);
#ifdef BACKPIPE
	if (t->t_dflg & F_PIPEIN) {
	    xclose(pipein[0]);
	    xclose(pipein[1]);
	}
#else /* !BACKPIPE */
	if (t->t_dflg & F_PIPEOUT) {
	    xclose(pipeout[0]);
	    xclose(pipeout[1]);
	}
#endif /* BACKPIPE */
	/*
	 * Perform a builtin function. If we are not forked, arrange for
	 * possible stopping
	 */
	if (bifunc) {
	    if (forked) {
		func(t, bifunc);
		exitstat();
	    } else {
		jmp_buf_t oldexit;
		int ohaderr = haderr;

		getexit(oldexit);
		if (setexit() == 0)
		    func(t, bifunc);
		resexit(oldexit);
		haderr = ohaderr;

		if (adrof(STRprintexitvalue)) {
		    int rv = getn(varval(STRstatus));
		    if (rv != 0)
			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
		}
	    }
	    break;
	}
	if (t->t_dtyp != NODE_PAREN) {
	    doexec(t, do_glob);
	    /* NOTREACHED */
	}
	/*
	 * For () commands must put new 0,1,2 in FSH* and recurse
	 */
	if ((OLDSTD = dcopy(0, FOLDSTD)) >= 0)
	    (void)close_on_exec(OLDSTD, 1);
	if ((SHOUT = dcopy(1, FSHOUT)) >= 0) {
	    (void)close_on_exec(SHOUT, 1);
	    isoutatty = isatty(SHOUT);
	}
	if ((SHDIAG = dcopy(2, FSHDIAG)) >= 0) {
	    (void)close_on_exec(SHDIAG, 1);
	    isdiagatty = isatty(SHDIAG);
    	}
	xclose(SHIN);
	SHIN = -1;
#ifndef CLOSE_ON_EXEC
	didcch = 0;
#else
	(void) close_on_exec(FSHOUT, 1);
	(void) close_on_exec(FSHDIAG, 1);
	(void) close_on_exec(FOLDSTD, 1);
#endif /* !CLOSE_ON_EXEC */
	didfds = 0;
	wanttty = -1;
	t->t_dspr->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
	execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
	exitstat();

    case NODE_PIPE:
#ifdef BACKPIPE
	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
#else /* !BACKPIPE */
	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
#endif /* BACKPIPE */
	break;

    case NODE_LIST:
	if (t->t_dcar) {
	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
	    /*
	     * In strange case of A&B make a new job after A
	     */
	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
		pendjob();
	}
	if (t->t_dcdr) {
	    t->t_dcdr->t_dflg |= t->t_dflg &
		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
	}
	break;

    case NODE_OR:
    case NODE_AND:
	if (t->t_dcar) {
	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
	    if ((getn(varval(STRstatus)) == 0) !=
		(t->t_dtyp == NODE_AND)) {
		return;
	    }
	}
	if (t->t_dcdr) {
	    t->t_dcdr->t_dflg |= t->t_dflg &
		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
	}
	break;

    default:
	break;
    }
    /*
     * Fall through for all breaks from switch
     * 
     * If there will be no more executions of this command, flush all file
     * descriptors. Places that turn on the F_REPEAT bit are responsible for
     * doing donefds after the last re-execution
     */
    if (didfds && !(t->t_dflg & F_REPEAT))
	donefds();
}
Beispiel #23
0
/*
  Interacts with the tile at players feet. This will usually change
  the tile to an identical-looking but functionally inert tile.
*/
int interact()
{
  int tile;
  char line[DEFLEN];

  tile = gtile(player->y, player->x);

  if (tile == TL_P_CHEST || tile == TL_P_UWCHEST)
  {
    loot_chest(player->y, player->x);
  }
  else if (tile == TL_P_NPC1)
  {
    shop_chef();
  }
  else if (tile == TL_P_NPC3)
  {
    shop_train();
  }
  else if (tile == TL_P_NPC4)
  {
    shop_map();
  }
  else if (tile == TL_P_NPC_ARMOR)
  {
    shop_armor();
  }
  else if (tile == TL_P_NPC_RANGED)
  {
    shop_weapons(1);
  }
  else if (tile == TL_P_NPC_WEAPONS)
  {
    shop_weapons(0);
  }
  else if (tile == TL_P_NPC_CLOSED)
  {
    pwait("SORRY WE ARE CLOSED FOR RENOVATION");
  }
  else if (tile == TL_P_NPC_SCUBA)
  {
    shop_scuba();
  }
  else if (tile == TL_P_NPC_BAR)
  {
    ye_olde_bar();
  }
  else if (tile == TL_P_NPC_SUSHI)
  {
    shop_sushi();
  }
  else if (tile == TL_P_COFFIN)
  {
    stile(player->y, player->x, TL_COFFIN_HL);
    stile(player->y - 1, player->x, TL_VOID);
    stile(player->y - 1, player->x - 1, TL_VOID);
    stile(player->y - 1, player->x + 1, TL_VOID);
    stile(player->y - 2, player->x, TL_VOID);

    if (player->flip)
    {
      stile(player->y, player->x + 4, TL_COFFIN_LID_R);
      stile(player->y - 1, player->x + 3, TL_COFFIN_LID_R);
      stile(player->y - 2, player->x + 2, TL_COFFIN_LID_R);
    }
    else
    {
      stile(player->y, player->x - 4, TL_COFFIN_LID_L);
      stile(player->y - 1, player->x - 3, TL_COFFIN_LID_L);
      stile(player->y - 2, player->x - 2, TL_COFFIN_LID_L);
    }

    strcpy(line, "YOU OPEN THE COFFIN\n\n");

    if (rand() % 5 == 0)
    {
      strcat(line, "A GHOUL ESCAPES!");

      if (rand() % 2 == 0)
	make_monster(player->y, player->x - 5, MOB_GHOUL);
      else
	make_monster(player->y, player->x + 5, MOB_GHOUL);

      draw_board();

      pwait(line);
    }
    else if (rand() % 5 == 0)
    {
      draw_board();
      find_random_armor(line);
    }
    else
    {
      draw_board_norefresh();
      give_item(line, 10 + rand() % COFFIN_GOLD, LOOT_NORMAL);
    }
  }
  else if (tile == TL_P_FOUNTAIN || tile == TL_P_BLOOD_FOUNTAIN)
  {
    fountain();
  }
  else if (tile == TL_P_SWSTONE)
  {
    sword_in_stone();
  }
  else if (tile == TL_P_ORB)
  {
    gaze_orb();
  }
  else if (tile == TL_P_TABLET)
  {
    stone_tablet();
  }
  else if (tile == TL_P_HELL)
  {
    stile(player->y, player->x, TL_VOID);
    pwait("YOU ARE IN HELL");
    draw_board();
  }
  else if (tile == TL_P_PORTAL)
  {
    portal_travel();
  }
  else if (tile == TL_P_IDOL)
  {
    idol();
  }
  else if (tile == TL_P_MUSHROOMS)
  {
    mushrooms();
  }
  else if (tile == TL_P_BOOKSHELF)
  {
    bookshelf();
  }
  else if (tile == TL_P_CAMP)
  {
    if (anything_near())
    {
      pwait("YOU CANNOT REST NOW!\nENEMIES ARE NEAR");
      return false;
    }

    camp();
  }
  else if (tile == TL_P_DISCO)
  {
    if (anything_near())
    {
      pwait("YOU CANNOT DANCE WHILE\nSOMEONE IS WATCHING");
      return false;
    }

    disco();
  }
  else if (tile == TL_P_ALTAR)
  {
    altar();
  }
  else if (tile == TL_P_CORPSE || tile == TL_P_SKELETON)
  {
    if (tile == TL_P_CORPSE)
    {
      stile(player->y, player->x, TL_VOID);
      strcpy(line, "YOU FIND A DECAYING CORPSE\n\n");
    }
    else
    {
      stile(player->y, player->x, TL_VOID);
      strcpy(line, "YOU FIND A LONG-DEAD SKELETON\n\n");
    }

    switch(rand() % 10)
    {
    case 0:
    case 1:
    case 2:
      find_random_armor(line);
      break;

    case 3:
    case 4:
    case 5:
      find_random_weapon(line);
      break;

    default:
      if (tile == TL_P_SKELETON)
	give_item(line, 5 + rand() % 25, LOOT_BONES);
      else
	give_item(line, 5 + rand() % 25, LOOT_NORMAL);
      break;
    }

    //pwait(line);

    return true;
  }

  return true;
}
Beispiel #24
0
void stone_tablet()
{
  char line[DEFLEN];
  int seq[15];
  int inp_seq[15];
  int i;
  int states = 7;
  int exp;

  pwait("YOU FIND A MYSTERIOUS STONE TABLET");

  stile(player->y, player->x, TL_VOID);
  stile(player->y - 1, player->x, TL_VOID);
  
  states = game->tablet_diff;
  
  draw_tablet(-1);

  pwait("REPEAT THE SEQUENCE\n"
	"USING THE DIRECTIONAL KEYS");

  draw_tablet(-1);

  for (i = 0; i < states; i++)
  {
    opause();
    seq[i] = rand() % 4;
    draw_tablet(seq[i]);
    opause();
    draw_tablet(-1);
  }

  for (i = 0; i < states; i++)
  {
    int gk;

    gk = getch();
    
    if (gk == KEY_UP)
      inp_seq[i] = 0;
    else if (gk == KEY_RIGHT)
      inp_seq[i] = 1;
    else if (gk == KEY_DOWN)
      inp_seq[i] = 2;
    else if (gk == KEY_LEFT)
      inp_seq[i] = 3;
    else
    {
      snprintf(lowmsg, DEFLEN, "INVALID INPUT");
      draw_lowmsg();
      continue;
    }

    if (inp_seq[i] != seq[i])
    {
      lpause();
      board_attr(A_REVERSE | A_BOLD, PAIR_RED);
      wrefresh(board);
      pwait("SHIT");
      draw_board();
      return;
    }
    else
    {
      draw_tablet(seq[i]);
      opause();
      draw_tablet(-1);
      flush_input();
    }
  }

  draw_board();

  switch (states)
  {
  case 7:  exp = 1000; break;
  case 8:  exp = 2000; break;
  case 9:  exp = 3000; break;
  default: exp = 1000; break;
  }
    
  snprintf(line, DEFLEN,
	   "YOU HAVE DECODED THE MYSTIC RUNES\n"
	   "\nYOU GET %d EXP",
	   exp);

  pwait(line);
  draw_board();
  
  give_exp(exp);
  draw_stats();

  // Make it harder next time
  game->tablet_diff += 1;
  
  draw_stats();
  
  return;
}
Beispiel #25
0
static void
backeval(struct blk_buf *bb, struct Strbuf *word, Char *cp, int literal)
{
    ssize_t icnt;
    Char c, *ip;
    struct command faket;
    int    hadnl;
    int     pvec[2], quoted;
    Char   *fakecom[2], ibuf[BUFSIZE];
    char    tibuf[BUFSIZE];

    hadnl = 0;
    icnt = 0;
    quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
    faket.t_dtyp = NODE_COMMAND;
    faket.t_dflg = F_BACKQ;
    faket.t_dlef = 0;
    faket.t_drit = 0;
    faket.t_dspr = 0;
    faket.t_dcom = fakecom;
    fakecom[0] = STRfakecom1;
    fakecom[1] = 0;

    /*
     * We do the psave job to temporarily change the current job so that the
     * following fork is considered a separate job.  This is so that when
     * backquotes are used in a builtin function that calls glob the "current
     * job" is not corrupted.  We only need one level of pushed jobs as long as
     * we are sure to fork here.
     */
    psavejob();
    cleanup_push(&faket, psavejob_cleanup); /* faket is only a marker */

    /*
     * It would be nicer if we could integrate this redirection more with the
     * routines in sh.sem.c by doing a fake execute on a builtin function that
     * was piped out.
     */
    mypipe(pvec);
    cleanup_push(&pvec[0], open_cleanup);
    cleanup_push(&pvec[1], open_cleanup);
    if (pfork(&faket, -1) == 0) {
	jmp_buf_t osetexit;
	struct command *t;
	size_t omark;

	xclose(pvec[0]);
	(void) dmove(pvec[1], 1);
	(void) dmove(SHDIAG,  2);
	initdesc();
	closem();
	arginp = cp;
	for (arginp = cp; *cp; cp++) {
	    *cp &= TRIM;
	    if (is_set(STRcsubstnonl) && (*cp == '\n' || *cp == '\r'))
		*cp = ' ';
	}

        /*
	 * In the child ``forget'' everything about current aliases or
	 * eval vectors.
	 */
	alvec = NULL;
	evalvec = NULL;
	alvecp = NULL;
	evalp = NULL;

	omark = cleanup_push_mark();
	getexit(osetexit);
	for (;;) {
	    (void) setexit();
	    justpr = 0;
	    
	    if (haderr) {
		/* unwind */
		doneinp = 0;
		cleanup_pop_mark(omark);
		resexit(osetexit);
		reset();
	    }
	    if (seterr) {
		xfree(seterr);
		seterr = NULL;
	    }

	    (void) lex(&paraml);
	    cleanup_push(&paraml, lex_cleanup);
	    if (seterr)
		stderror(ERR_OLD);
	    alias(&paraml);
	    t = syntax(paraml.next, &paraml, 0);
	    if (t == NULL)
		return;
	    cleanup_push(t, syntax_cleanup);
	    /* The F_BACKQ flag must set so the job output is correct if
	     * printexitvalue is set.  If it's not set, the job output
	     * will have "Exit N" appended where N is the exit status. */
	    t->t_dflg = F_BACKQ|F_NOFORK;
	    if (seterr)
		stderror(ERR_OLD);
#ifdef SIGTSTP
	    signal(SIGTSTP, SIG_IGN);
#endif
#ifdef SIGTTIN
	    signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTTOU
	    signal(SIGTTOU, SIG_IGN);
#endif
	    execute(t, -1, NULL, NULL, TRUE);

	    cleanup_until(&paraml);
	}
    }
    cleanup_until(&pvec[1]);
    c = 0;
    ip = NULL;
    do {
	ssize_t     cnt = 0;
	char   *tmp;

	tmp = tibuf;
	for (;;) {
	    while (icnt == 0) {
		int     i, eof;

		ip = ibuf;
		icnt = xread(pvec[0], tmp, tibuf + BUFSIZE - tmp);
		eof = 0;
		if (icnt <= 0) {
		    if (tmp == tibuf)
			goto eof;
		    icnt = 0;
		    eof = 1;
		}
		icnt += tmp - tibuf;
		i = 0;
		tmp = tibuf;
		while (tmp < tibuf + icnt) {
		    int len;

		    len = normal_mbtowc(&ip[i], tmp, tibuf + icnt - tmp);
		    if (len == -1) {
		        reset_mbtowc();
		        if (!eof && (size_t)(tibuf + icnt - tmp) < MB_CUR_MAX) {
			    break; /* Maybe a partial character */
			}
			ip[i] = (unsigned char) *tmp | INVALID_BYTE; /* Error */
		    }
		    if (len <= 0)
		        len = 1;
		    i++;
		    tmp += len;
		}
		if (tmp != tibuf)
		    memmove (tibuf, tmp, tibuf + icnt - tmp);
		tmp = tibuf + (tibuf + icnt - tmp);
		icnt = i;
	    }
	    if (hadnl)
		break;
	    --icnt;
	    c = (*ip++ & TRIM);
	    if (c == 0)
		break;
#if defined(WINNT_NATIVE) || defined(__CYGWIN__)
	    if (c == '\r')
	    	c = ' ';
#endif /* WINNT_NATIVE || __CYGWIN__ */
	    if (c == '\n') {
		/*
		 * Continue around the loop one more time, so that we can eat
		 * the last newline without terminating this word.
		 */
		hadnl = 1;
		continue;
	    }
	    if (!quoted && (c == ' ' || c == '\t'))
		break;
	    cnt++;
	    Strbuf_append1(word, c | quoted);
	}
	/*
	 * Unless at end-of-file, we will form a new word here if there were
	 * characters in the word, or in any case when we take text literally.
	 * If we didn't make empty words here when literal was set then we
	 * would lose blank lines.
	 */
	if (c != 0 && (cnt || literal))
	    pword(bb, word);
	hadnl = 0;
    } while (c > 0);
 eof:
    cleanup_until(&pvec[0]);
    pwait();
    cleanup_until(&faket); /* psavejob_cleanup(); */
}
Beispiel #26
0
void gaze_orb(void)
{
  int turns;
  int inp;
  int y;
  int x;
  
  turns = 400;

  pwait("AS YOU GAZE INTO THE ORB, YOUR\n"
	"MIND DRIFTS INTO THE ASTRAL REALM");
  
  setlowmsg("PRESS DIRECTION TO MOVE VISION");
  draw_lowmsg();

  game->scrying = true;
  draw_board();

  while (turns > 0)
  {
    inp = getch();

    if (inp == ' ')
      break;
    else if (inp == KEY_LEFT)
    {
      view_x = MAX(0, view_x - 5);
      turns -= 2;
    }
    else if (inp == KEY_RIGHT)
    {
      view_x = MIN(MAP_W - BOARD_W - 1, view_x + 5);
      turns -= 2;
    }
    else if (inp == KEY_UP)
    {
      view_y = MAX(0, view_y - 2);
      turns -= 3;
    }
    else if (inp == KEY_DOWN)
    {
      // *2 is to prevent the player from scrying the bottom floor
      view_y = MIN(MAP_H - (FLOOR_H * 2) - BOARD_H - 1, view_y + 2);
      turns -= 3;
    }
    else
      continue;


    y = (view_y + (FLOOR_H / 2)) / FLOOR_H;
    x = (view_x + (BOARD_W / 2)) / CELL_TO_TILES;
    
    y = MAX(0, MIN(MAX_FLOORS - 1, y));
    x = MAX(1, MIN(CELLS_W - 2, x));
    
    game->automap[y][x]     |= AUTOMAP_KNOWN;
    game->automap[y][x - 2] |= AUTOMAP_KNOWN;
    game->automap[y][x - 1] |= AUTOMAP_KNOWN;
    game->automap[y][x + 1] |= AUTOMAP_KNOWN;
    game->automap[y][x + 2] |= AUTOMAP_KNOWN;

    draw_board();
  }

  stile(player->y, player->x, TL_ORB_C);
  stile(player->y - 1, player->x, TL_ORB_DARK);

  game->scrying = false;
  view_y = player->y - FLOOR_Y + 1;
  recenter(false);

  pwait("YOU ARE YANKED BACK TO YOUR BODY");

  draw_board();

  lowmsg[0] = '\0';
  draw_lowmsg();

  return;
}
Beispiel #27
0
int rcmdfn (int cmdarg, int argc, char **argv)
{
    /* tasks:
       find R_HOME, set as env variable (with / as separator)
       set R_ARCH
       set PATH to include R_HOME\bin
       set TMPDIR if unset
       set HOME if unset
       set R_CMD (depends on how this was launched), R_VERSION
       read R_HOME\etc\Rcmd_environ
       launch %R_HOME%\bin\$*
    */
    int i, iused, status = 0;
    char *p, cmd[CMD_LEN];
    char RCMD[] = "R CMD";
    int len = strlen(argv[0]);
    char env_path[MAX_PATH];
    int timing = 1;
    char *RHome = getRHOME(3);

    if(!strncmp(argv[0]+len-4, "Rcmd", 4) ||
       !strncmp(argv[0]+len-4, "rcmd", 4) ||
       !strncmp(argv[0]+len-8, "Rcmd.exe", 8) ||
       !strncmp(argv[0]+len-8, "rcmd.exe", 8))
	strcpy(RCMD, "Rcmd");


    if (argc <= cmdarg) {
	fprintf(stderr, "%s%s%s", "Usage: ", RCMD, " command args\n\n");
	rcmdusage(RCMD);
	return(0);
    }

    if (argc == cmdarg+1 &&
	(!strcmp(argv[cmdarg], "--help") || !strcmp(argv[cmdarg], "-h"))
	) {
	/* need to cover Rcmd --help, R CMD --help and R --help,
	   as well as -h versions.
	 */
	if(cmdarg >= 2 || (cmdarg == 1 && !strcmp(RCMD, "Rcmd"))) {
	    fprintf(stderr, "%s%s%s", "Usage: ", RCMD, " command args\n\n");
	    rcmdusage(RCMD);
	    return(0);
	}
	/* R --help */
	snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe --help", getRHOME(3), BINDIR);
	system(cmd);
	fprintf(stderr, "%s", "\n\nOr: R CMD command args\n\n");
	rcmdusage(RCMD);
	return(0);
    }

    if (cmdarg == 0) {
	/* use of R.exe without CMD, -h, --help */
	if (argc > 1 && !strcmp(argv[1], "RHOME")) {
	    /* An historical special case */
	    fprintf(stdout, "%s", getRHOME(3));
	    return(0);
	}
	snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe", getRHOME(3), BINDIR);
	for (i = cmdarg + 1; i < argc; i++){
	    strcat(cmd, " ");
	    if (strlen(cmd) + strlen(argv[i]) > 9900) {
		fprintf(stderr, "command line too long\n");
		return(27);
	    }
	    if(strchr(argv[i], ' ') || !strlen(argv[i])) {
		strcat(cmd, "\"");
		strcat(cmd, argv[i]);
		strcat(cmd, "\"");
	    } else strcat(cmd, argv[i]);
	}
	/* R.exe should ignore Ctrl-C, and let Rterm.exe handle it */
	SetConsoleCtrlHandler(NULL, TRUE);
	return system(cmd);
    }

    /* From here on down, this was called as Rcmd or R CMD */

    char RHOME[MAX_PATH];
    strcpy(RHOME, "R_HOME=");
    strcat(RHOME, RHome);
    for (p = RHOME; *p; p++) if (*p == '\\') *p = '/';
    putenv(RHOME);

    /* We follow Unix-alikes as from R 2.12.0 in setting environment
       variables in Rcmd BATCH.

       NB: Rcmd_environ uses R_HOME.
    */
    strcpy(env_path, RHome); strcat(env_path, "/etc/Rcmd_environ");
    process_Renviron(env_path);

    if (!strcmp(argv[cmdarg], "BATCH")) {
	/* ----- handle Rcmd BATCH  ---- */
	char infile[MAX_PATH], outfile[MAX_PATH], *p, cmd_extra[CMD_LEN];
	DWORD ret;
	SECURITY_ATTRIBUTES sa;
	PROCESS_INFORMATION pi;
	STARTUPINFO si;
	HANDLE hOUT = INVALID_HANDLE_VALUE;

	/* process the command line */
	cmd_extra[0] = '\0';
	if((p = getenv("R_BATCH_OPTIONS")) && strlen(p)) {
	    if(1+strlen(p) >= CMD_LEN) {
		fprintf(stderr, "command line too long\n");
		return(27);
	    }
	    strcat(cmd_extra, " ");
	    strcat(cmd_extra, p);
	}

	for(i = cmdarg + 1, iused = cmdarg; i < argc; i++) {
	    if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
		fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
"Usage: ", RCMD, " BATCH [options] infile [outfile]\n\n",
"Run R non-interactively with input from infile and place output (stdout\n",
"and stderr) to another file.  If not given, the name of the output file\n",
"is the one of the input file, with a possible '.R' extension stripped,\n",
"and '.Rout' appended.\n\n",
"Options:\n"
"  -h, --help		print short help message and exit\n",
"  -v, --version		print version info and exit\n",
"  --no-timing		do not report the timings\n",
"  --			end processing of options\n\n",
"Further arguments starting with a '-' are considered as options as long\n",
"as '--' was not encountered, and are passed on to the R process, which\n",
"by default is started with '--restore --save'.\n\n",
"Report bugs at bugs.r-project.org .");
		return(0);
	    }
	    if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
		fprintf(stderr, "BATCH %s\n%s%s%s\n", "1.2",
"Copyright (C) 1997-2004 R Core Team.\n",
"This is free software; see the GNU General Public Licence version 2\n",
"or later for copying conditions.  There is NO warranty.");
		return(0);
	    }
	    if (!strcmp(argv[i], "--no-timing")) {
		timing = 0;
		iused = i;
		continue;
	    }

	    if (!strcmp(argv[i], "--")) {
		iused = i;
		break;
	    }
	    if (argv[i][0] == '-') {
		if (strlen(cmd_extra) + strlen(argv[i]) > 9900) {
		    fprintf(stderr, "command line too long\n");
		    return(27);
		}
		strcat(cmd_extra, " ");
		strcat(cmd_extra, argv[i]);
		iused = i;
	    } else break;
	}
	if (iused+1 < argc)
	    strcpy(infile, argv[iused+1]);
	else {
	    fprintf(stderr, "no input file\n");
	    return(1);
	}
	if (iused+2 < argc)
	    strcpy(outfile, argv[iused+2]);
	else {
	    int len = strlen(infile);
	    strcpy(outfile, infile);
	    if (!strcmp(outfile+len-2, ".R")) strcat(outfile, "out");
	    else strcat(outfile, ".Rout");
	}

	/* Unix has --restore --save --no-readline */
	snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -f \"%s\" --restore --save",
		 getRHOME(3), BINDIR, infile);
	if(strlen(cmd) + strlen(cmd_extra) >= CMD_LEN) {
	    fprintf(stderr, "command line too long\n");
	    return(27);
	}
	strcat(cmd, cmd_extra);

	/* to get .Last.sys run: see profile/Common.R */
	if(timing) putenv("R_BATCH=1234");

	/* fprintf(stderr, "%s->%s\n", infile, outfile);
	   fprintf(stderr, "%s\n", cmd); */

	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;

	hOUT = CreateFile(outfile, GENERIC_WRITE, FILE_SHARE_READ,
			  &sa, CREATE_ALWAYS, 0, NULL);
	if (hOUT == INVALID_HANDLE_VALUE) {
	    fprintf(stderr, "unable to open output file\n");
	    return(2);
	}
	SetStdHandle(STD_OUTPUT_HANDLE, hOUT);
	SetStdHandle(STD_ERROR_HANDLE, hOUT);
	si.cb = sizeof(si);
	si.lpReserved = NULL;
	si.lpReserved2 = NULL;
	si.cbReserved2 = 0;
	si.lpDesktop = NULL;
	si.lpTitle = NULL;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_SHOWDEFAULT;
	ret = CreateProcess(0, cmd, &sa, &sa, TRUE, 0, NULL, NULL, &si, &pi);
	CloseHandle(hOUT);
	if (!ret) {
	    fprintf(stderr, "unable to run Rterm.exe\n");
	    return(3);
	}
	CloseHandle(pi.hThread);
	return(pwait(pi.hProcess));
	/* ------- end of BATCH -------- */
    }

    /* Now Rcmd <cmd> or R CMD <cmd>: most commands are nowadays
     * handled internally on Windows
     */

    /* Not sure that we still need these set -- they are Windows-only */
    char Rversion[25];
    snprintf(Rversion, 25, "R_VERSION=%s.%s", R_MAJOR, R_MINOR);
    putenv(Rversion);

    putenv("R_CMD=R CMD");

    char *Path = malloc( strlen(getenv("PATH")) + MAX_PATH + 10 );
    if (!Path) {fprintf(stderr, "PATH too long\n"); return(4);}
    strcpy(Path, "PATH=");
    strcat(Path, RHome);
    strcat(Path, "\\");
    strcat(Path, BINDIR);
    strcat(Path, ";");
    strcat(Path, getenv("PATH"));
    putenv(Path);
    free(Path);

    char Rarch[30];
    strcpy(Rarch, "R_ARCH=/");
    strcat(Rarch, R_ARCH);
    putenv(Rarch);

    char Bindir[30];
    strcpy(Bindir, "BINDIR=");
    strcat(Bindir, BINDIR);
    putenv(Bindir);

    char Tmpdir[MAX_PATH+10];
    if ( (p = getenv("TMPDIR")) && isDir(p)) {
	/* TMPDIR is already set */
    } else {
	if ( (p = getenv("TEMP")) && isDir(p)) {
	    strcpy(Tmpdir, "TMPDIR=");
	    strcat(Tmpdir, p);
	    putenv(Tmpdir);
	} else if ( (p = getenv("TMP")) && isDir(p)) {
	    strcpy(Tmpdir, "TMPDIR=");
	    strcat(Tmpdir, p);
	    putenv(Tmpdir);
	} else {
	    strcpy(Tmpdir, "TMPDIR=");
	    strcat(Tmpdir, getRUser());
	    putenv(Tmpdir);
	}
    }

    char HOME[MAX_PATH+10];
    if( !getenv("HOME") ) {
	strcpy(HOME, "HOME=");
	strcat(HOME, getRUser());
	putenv(HOME);
    }


    if (!strcmp(argv[cmdarg], "INSTALL")) {
	/* Unix has --no-restore except for MM's undocumented --use-vanilla */
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.install_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "REMOVE")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -f \"%s/share/R/REMOVE.R\" R_DEFAULT_PACKAGES=NULL --no-restore --slave --args",
		 getRHOME(3), BINDIR, getRHOME(3));
	for (i = cmdarg + 1; i < argc; i++){
	    strcat(cmd, " ");
	    if (strlen(cmd) + strlen(argv[i]) > 9900) {
		fprintf(stderr, "command line too long\n");
		return(27);
	    }
	    /* Library names could contain spaces */
	    if(strchr(argv[i], ' ')) {
		strcat(cmd, "\"");
		strcat(cmd, argv[i]);
		strcat(cmd, "\"");
	    } else strcat(cmd, argv[i]);
	}
	return(system(cmd));
    } else if (!strcmp(argv[cmdarg], "build")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.build_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "check")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.check_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Rprof")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.Rprof() R_DEFAULT_PACKAGES=utils LC_COLLATE=C --vanilla --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
   } else if (!strcmp(argv[cmdarg], "texify")) {
	if (argc < cmdarg+2) {
	    fprintf(stderr, "\nUsage: %s texify [options] filename\n", RCMD);
	    return(1);
	}
	snprintf(cmd, CMD_LEN,
		 "texify.exe -I %s/share/texmf/tex/latex -I %s/share/texmf/bibtex/bst", getRHOME(3), getRHOME(3));
	PROCESS_CMD(" ");
    } else if (!strcmp(argv[cmdarg], "SHLIB")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.SHLIB() R_DEFAULT_PACKAGES=NULL --no-restore --slave --no-site-file --no-init-file --args",
		 getRHOME(3), BINDIR);
	PROCESS_CMD(" ");
    } else if (!strcmp(argv[cmdarg], "Rdiff")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.Rdiff() R_DEFAULT_PACKAGES=NULL --vanilla --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Rdconv")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.Rdconv() R_DEFAULT_PACKAGES= LC_COLLATE=C --vanilla --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Rd2txt")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::.Rdconv() R_DEFAULT_PACKAGES= LC_COLLATE=C --vanilla --slave --args nextArg-tnextArgtxt",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Rd2pdf")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe -e tools:::..Rd2pdf() R_DEFAULT_PACKAGES= LC_ALL=C --vanilla --slave --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Sweave")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe --no-restore --slave -e utils:::.Sweave() --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else if (!strcmp(argv[cmdarg], "Stangle")) {
	snprintf(cmd, CMD_LEN,
		 "%s/%s/Rterm.exe --vanilla --slave -e utils:::.Stangle() --args ",
		 getRHOME(3), BINDIR);
	PROCESS_CMD("nextArg");
    } else {
	/* not one of those handled internally */
	p = argv[cmdarg];
	if (!strcmp(p, "config"))
	    snprintf(cmd, CMD_LEN, "sh %s/bin/config.sh", RHome);
	else if (!strcmp(p, "open"))
	    snprintf(cmd, CMD_LEN, "%s/%s/open.exe", RHome, BINDIR);
	else {
	    /* RHOME/BINDIR is first in the path, so looks there first */
	    if (!strcmp(".sh", p + strlen(p) - 3)) strcpy(cmd, "sh ");
	    else if (!strcmp(".pl", p + strlen(p) - 3)) strcpy(cmd, "perl ");
	    else strcpy(cmd, "");
	    strcat(cmd, p);
	}

	for (i = cmdarg + 1; i < argc; i++){
	    strcat(cmd, " ");
	    if (strlen(cmd) + strlen(argv[i]) > 9900) {
		fprintf(stderr, "command line too long\n");
		return(27);
	    }
	    if(strchr(argv[i], ' ')) {
		strcat(cmd, "\"");
		strcat(cmd, argv[i]);
		strcat(cmd, "\"");
	    } else strcat(cmd, argv[i]);
	}
	/* printf("cmd is %s\n", cmd); */
	status = system(cmd);
    }
    return(status);
}
Beispiel #28
0
void altar()
{
  char line[DEFLEN];
  int temp;
  char * p;

  stile(player->y, player->x, TL_GR_HL);

  /*
    Praying increases piety (hidden from the player); when the
    player has enough it will cause a beneficial effect. If the
    player chooses to loot the altar they will lose all piety.
  */

  if (psel("YOU APPROACH THE ALTAR\n"
	   "\n"
	   "<- LOOT IT#PRAY ->") == 0)
  {
    stile(player->y - 2, player->x - 3, TL_VOID);
    stile(player->y - 2, player->x + 3, TL_VOID);

    stile(player->y - 3, player->x, TL_VOID);
    stile(player->y - 2, player->x - 1, TL_VOID);
    stile(player->y - 2, player->x, TL_VOID);
    stile(player->y - 2, player->x + 1, TL_VOID);

    draw_board();
    give_gold("YOU LOOT THE ALTAR\n\n", 1 + rand() % ALTAR_LOOT_GOLD);

    game->piety -= ALTAR_LOOT_PENALTY + (rand() % ALTAR_LOOT_PENALTY);

    if (game->piety < -3000 && rand() % 3 == 0)
    {
      draw_board();
      pwait("A MIGHTY VOICE BOOMS OUT:\n"
	    "\"THOU HATH DEFILED MY ALTAR\n"
	    " TOO MANY TIMES, MORTAL!\"");
      
      draw_board();

      if (rand() % 2 == 0)
      {
	pwait("YOU ARE STRUCK BY LIGHTNING!");
	draw_board();
      
	lightning(player->y - view_y, player->x - view_x);

	player->hp -= 25 + rand() % 46;

	if (player->hp <= 0)
	  game_over("YOU DIED FOR YOUR SINS", false);

	draw_bars();
      }
      else
      {
	pwait("YOU FEEL WEAK!");
	player->strength = MAX(1, player->strength - 1 - rand() % 3);
	player->speed = MAX(1, player->speed - 1 - rand() % 3);
      }
    }
    else if (game->piety < -2000 && rand() % 3 == 0)
    {
      tremor(5);
      draw_board();
      pwait("YOU HEAR A RUMBLE IN THE DISTANCE");
    }
    else if (game->piety < -1000 && rand() % 3 == 0)
    {
      draw_board();
      pwait("YOU GET AN UNEASY FEELING");
    }
  }
  else
  {
    stile(player->y - 3, player->x - 1, TL_YDOT);
    stile(player->y - 3, player->x, TL_YDOT);
    stile(player->y - 3, player->x + 1, TL_YDOT);

    // Update board with lit candles
    draw_board();

    strcpy(line, "YOU PRAY AT THE ALTAR");

    if (game->piety < 0)
      game->piety += 500;

    game->piety += ALTAR_PRAYER_PIETY + (rand() % ALTAR_PRAYER_PIETY);

    if (game->piety < 0)
    {
      strcat(line,
	     "\n\nYOU ARE LEFT WITH A DISAPPROVING\n"
	     "SENSATION OF EMPTINESS");

      pwait(line);
    }
    else if (rand() % MAX(1, game->piety) > ALTAR_HEAL_PIETY)
    {
      game->piety -= ALTAR_HEAL_PIETY;

      strcat(line,
	     "\n\n YOU ARE EMBRACED BY\n"
	     "   A WARM FEELING!  ");
      pwait(line);
      draw_board();
	
      refill_hp(ALTAR_EMBRACE_HEAL);
    }
    else if (rand() % MAX(1, game->piety) > ALTAR_WISDOM_PIETY)
    {
      game->piety -= ALTAR_WISDOM_PIETY;

      temp = (1 + rand() % game->current_floor) * 100;
      temp += MAX(0, game->current_floor - 7) * 200;

      // Find end of string
      for (p = line; *p != '\0'; p++) { }

      sprintf(p,
	      "\n\nYOU HAVE BEEN ENLIGHTENED!\n\n"
	      "YOU GET %d EXP",
	      temp);
      pwait(line);

      draw_board();

      give_exp(temp);
    }
    else
    {
      pwait(line);
    }
  }

  return;
}
Beispiel #29
0
int dbf_getrecords(int s, FILE *f, dbf_header *H, const char *target, int n)
/*
  Read the data records and displays records on the screen that match
  target on field n.
  Uses structure information in fields and the header structure H.
*/
{
   char *recbuf, *fldbuf, targbuf[79];
   int c, i, found;
   int index = 0;

   /* copy target string into a buffer and uppercase it */
   strcpy(targbuf, target);
   strupr(targbuf);

   /* allocate a buffer to hold one record */
   recbuf = (char *) mallocw(H->rlen);

   /* allocate a buffer for the field */
   /* A pointer to this is passed to 'match' so we don't have to allocate
   /* a buffer each time the loop executes */
   fldbuf = (char *) mallocw(fields[n].length+1);


   /* read the records */
   for (i=1; i <= H->nrecs; i++)
   {
	/* read a data record */
	if ((fread(recbuf, H->rlen, 1, f)) != 1)
	{
	   tprintf("read error\n");
	   return(1);
	}

	/* check for eof and errors */
	if (feof(f) || ferror(f))
	{
	   tprintf("EOF or error while reading data\n");
	   return(1);
	}

	/* search for a record */
	if (match(s, recbuf, fldbuf, targbuf, n, H->nflds, &index))
	   found = 1;
	pwait(NULL);            /* give up the CPU for a while */
    }

    free(recbuf);                 /* release memory for record buffer */
    free(fldbuf);
    if (found == 0)
    {
       if (output_format == CSOTEXT)
	  usprintf(s, "501:There are no matches to your query.\n200:Ok.\n");
       else
	  usprintf(s, "\nNo Records match with field %s = %s\n",
			  fields[n].name, target);
    }
    else
       if (output_format == CSOTEXT)
       {
	   usprintf(s, "200:Ok.\n");
	   usputc(s, 26);
       }

     return 0;
}
Beispiel #30
0
static int
run (const char *what, char *args)
{
  char *s;
  int pid, wait_status, retcode;
  int i;
  const char **argv;
  char *errmsg_fmt, *errmsg_arg;
  char *temp_base = choose_temp_base ();
  int in_quote;
  char sep;

  if (verbose || dry_run)
    fprintf (stderr, "%s %s\n", what, args);

  /* Count the args */
  i = 0;
  for (s = args; *s; s++)
    if (*s == ' ')
      i++;
  i++;
  argv = xmalloc (sizeof (char *) * (i + 3));
  i = 0;
  argv[i++] = what;
  s = args;
  while (1)
    {
      while (*s == ' ' && *s != 0)
	s++;
      if (*s == 0)
	break;
      in_quote = (*s == '\'' || *s == '"');
      sep = (in_quote) ? *s++ : ' ';
      argv[i++] = s;
      while (*s != sep && *s != 0)
	s++;
      if (*s == 0)
	break;
      *s++ = 0;
      if (in_quote)
	s++;
    }
  argv[i++] = NULL;

  if (dry_run)
    return 0;

  pid = pexecute (argv[0], (char * const *) argv, prog_name, temp_base,
		  &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
  free (argv);

  if (pid == -1)
    {
      int errno_val = errno;

      fprintf (stderr, "%s: ", prog_name);
      fprintf (stderr, errmsg_fmt, errmsg_arg);
      fprintf (stderr, ": %s\n", strerror (errno_val));
      return 1;
    }

  retcode = 0;
  pid = pwait (pid, &wait_status, 0);
  if (pid == -1)
    {
      warn (_("pwait returns: %s"), strerror (errno));
      retcode = 1;
    }
  else if (WIFSIGNALED (wait_status))
    {
      warn (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
      retcode = 1;
    }
  else if (WIFEXITED (wait_status))
    {
      if (WEXITSTATUS (wait_status) != 0)
	{
	  warn (_("%s exited with status %d"), what, WEXITSTATUS (wait_status));
	  retcode = 1;
	}
    }
  else
    retcode = 1;

  return retcode;
}