forked from andrea-rosasco/shell_bush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmm.c
84 lines (74 loc) · 2 KB
/
bmm.c
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
79
80
81
82
/*
* bush--
*
* Programma sviluppato a supporto del laboratorio di
* Sistemi di Elaborazione e Trasmissione del corso di laurea
* in Informatica classe L-31 presso l'Universita` degli Studi di
* Genova, anno accademico 2016/2017.
*
* Copyright (C) 2015,2016 by Giovanni Lagorio <[email protected]>
*
* 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 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell.h"
#include "var_table.h"
#include "utils.h"
void inject_environment_into_vartable(struct var_table *vt)
{
extern char **environ;
char **envp = environ;
char *tmp;
for(; *envp; ++envp) {
const char * const current_var = *envp;
char * const equal_sign = strchr(current_var, '=');
size_t name_len;
if (!equal_sign)
continue;
name_len = equal_sign - current_var;
tmp = my_malloc(name_len + 1);
strncpy(tmp, current_var, name_len);
tmp[name_len] = 0;
vt_set_value(vt, tmp, equal_sign + 1);
free(tmp);
}
}
void make_sure_PWD_is_set(struct var_table *vt)
{
char *cwd;
if (vt_lookup(vt, PWD))
return;
/* la variabile PWD non era presente; crearla e inizializzarla
* al valore restituito dalla system call getcwd() */
/*** TO BE DONE START ***/
/*** TO BE DONE END ***/
}
void make_sure_PATH_is_set(struct var_table *vt)
{
if (!vt_lookup(vt, PATH))
vt_set_value(vt, PATH, "/bin:/usr/bin");
}
void make_sure_HOME_is_set(struct var_table *vt)
{
if (!vt_lookup(vt, HOME))
vt_set_value(vt, HOME, "/");
}
int main()
{
void lexer_loop(struct shell *sh); /* defined inside lexer.l */
struct shell *sh = sh_new();
struct var_table *vt = sh_get_var_table(sh);
inject_environment_into_vartable(vt);
make_sure_PWD_is_set(vt);
make_sure_PATH_is_set(vt);
make_sure_HOME_is_set(vt);
sh_build_environment(sh);
lexer_loop(sh);
sh_destroy(sh);
return EXIT_SUCCESS;
}