aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/mk/shell.c
blob: dd41ad128a09aaed54a9a11255613934339761fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "mk.h"

static Shell *shells[] = {
	&shshell,
	&rcshell
};

Shell *shellt;
Word *shellcmd;

typedef struct Shellstack Shellstack;
struct Shellstack
{
	Shell *t;
	Word *w;
	Shellstack *next;
};

Shellstack *shellstack;

char*
setshell(Word *w)
{
	int i;

	if(w->s == nil)
		return "shell name not found on line";

	for(i=0; i<nelem(shells); i++)
		if(shells[i]->matchname(w->s))
			break;
	if(i == nelem(shells))
		return "cannot determine shell type";
	shellt = shells[i];
	shellcmd = w;
	return nil;
}

void
initshell(void)
{
	shellcmd = stow(shells[0]->name);
	shellt = shells[0];
	setvar("MKSHELL", shellcmd);
}

void
pushshell(void)
{
	Shellstack *s;

	/* save */
	s = Malloc(sizeof *s);
	s->t = shellt;
	s->w = shellcmd;
	s->next = shellstack;
	shellstack = s;

	initshell();	/* reset to defaults */
}

void
popshell(void)
{
	Shellstack *s;

	if(shellstack == nil){
		fprint(2, "internal shellstack error\n");
		Exit();
	}

	s = shellstack;
	shellstack = s->next;
	shellt = s->t;
	shellcmd = s->w;
	setvar("MKSHELL", shellcmd);
	free(s);
}