hbase

heirloom base
git clone git://git.2f30.org/hbase
Log | Files | Refs | README

shell.c (1093B)


      1 #include "mk.h"
      2 
      3 static Shell *shells[] = {
      4 	&rcshell,
      5 	&shshell
      6 };
      7 
      8 Shell *shelldefault = &shshell;
      9 
     10 Shell *shellt;
     11 Word *shellcmd;
     12 
     13 typedef struct Shellstack Shellstack;
     14 struct Shellstack
     15 {
     16 	Shell *t;
     17 	Word *w;
     18 	Shellstack *next;
     19 };
     20 
     21 Shellstack *shellstack;
     22 
     23 char*
     24 setshell(Word *w)
     25 {
     26 	int i;
     27 
     28 	if(w->s == nil)
     29 		return "shell name not found on line";
     30 
     31 	for(i=0; i<nelem(shells); i++)
     32 		if(shells[i]->matchname(w->s))
     33 			break;
     34 	if(i == nelem(shells))
     35 		return "cannot determine shell type";
     36 	shellt = shells[i];
     37 	shellcmd = w;
     38 	return nil;
     39 }
     40 
     41 void
     42 initshell(void)
     43 {
     44 	shellcmd = stow(shelldefault->name);
     45 	shellt = shelldefault;
     46 	setvar("MKSHELL", shellcmd);
     47 }
     48 
     49 void
     50 pushshell(void)
     51 {
     52 	Shellstack *s;
     53 
     54 	/* save */
     55 	s = Malloc(sizeof *s);
     56 	s->t = shellt;
     57 	s->w = shellcmd;
     58 	s->next = shellstack;
     59 	shellstack = s;
     60 
     61 	initshell();	/* reset to defaults */
     62 }
     63 
     64 void
     65 popshell(void)
     66 {
     67 	Shellstack *s;
     68 
     69 	if(shellstack == nil){
     70 		fprint(2, "internal shellstack error\n");
     71 		Exit();
     72 	}
     73 
     74 	s = shellstack;
     75 	shellstack = s->next;
     76 	shellt = s->t;
     77 	shellcmd = s->w;
     78 	setvar("MKSHELL", shellcmd);
     79 	free(s);
     80 }