Newer
Older
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
** builtin_shopt.c for 42sh in /goinfre/seblu/42sh/src
**
** Made by Seblu
** Login <luttri_s@epita.fr>
**
** Started on Wed Mar 22 16:27:35 2006 Seblu
** Last update Tue Apr 11 00:24:22 2006 Seblu
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "builtin.h"
#include "../main/42sh.h"
#include "../opt/opt.h"
#include "mem.h"
static void getoption(char *argv[], int *argp, int opts[2]);
static int mutator_opt(char *argv[], int *argp, int opts[2], struct s_42sh *sh);
static int show_opt(char *argv[], int *argp, int opts[2], struct s_42sh *sh);
static int show_all_opt(int quiet, struct s_42sh *sh);
/*!
** This builtin change local setting of the shell
** opts[0] == set or unset
** opts[1] == quiet mode
**
** @param argv argument vector
** @param sh shell data
**
** @return success status
*/
int builtin_shopt(char *argv[], struct s_42sh *sh)
{
int opts[2];
int argp = 1;
assert(argv && argv[0] && sh);
opts[0] = -1;
opts[1] = 0;
getoption(argv, &argp, opts);
if (opts[0] == -1 && !argv[argp])
return show_all_opt(opts[1], sh);
else if (opts[0] == -1 && argv[argp])
return show_opt(argv, &argp, opts, sh);
else
return mutator_opt(argv, &argp, opts, sh);
}
/*!
** Retreinve command line option
**
** @param argv argument vector
** @param argp argument position
** @param line line flag
** @param ext extended flag
*/
static void getoption(char *argv[], int *argp, int opts[2])
{
for (; argv[*argp]; ++*argp)
if (!strcmp("-s", argv[*argp]))
opts[0] = 1;
else if (!strcmp("-u", argv[*argp]))
opts[0] = 0;
else if (!strcmp("-q", argv[*argp]))
opts[1] = 1;
else
break;
}
/*!
** Sun or unset an option
**
** @param argv arg vector
** @param argp cur pos
** @param opts argline opts
** @param sh struct info
**
** @return builtin status
*/
static int mutator_opt(char *argv[],
int *argp,
int opts[2],
struct s_42sh *sh)
{
int ret = 0;
for (; argv[*argp]; ++*argp)
{
if (opt_isset(argv[*argp], sh->opt) == -1)
{
fprintf(stderr, "42sh: shopt: %s: invalid shell option name\n",
argv[*argp]);
ret = 1;
continue;
}
if (opts[0])
opt_set(argv[*argp], sh->opt);
else
opt_unset(argv[*argp], sh->opt);
}
return ret;
}
/*!
** Show info about shell opts
**
** @param argv arg vector
** @param argp cur pos
** @param opts argline opts
** @param sh struct info
**
** @return builtin status
*/
static int show_opt(char *argv[],
int *argp,
int opts[2],
struct s_42sh *sh)
{
int ret = 0;
int isset = 0;
for (; argv[*argp]; ++*argp)
{
if ((isset = opt_isset(argv[*argp], sh->opt)) == -1)
{
fprintf(stderr, "42sh: shopt: %s: invalid shell option name\n",
argv[*argp]);
ret = 1;
continue;
}
if (!opts[1])
printf("%-15s %s\n", argv[*argp], (isset) ? "yes" : "no");
else if (!isset)
ret = 1;
}
return ret;
}
/*!
** Show all options
**
** @param quiet silent showing
**
** @return return status
*/
static int show_all_opt(int quiet, struct s_42sh *sh)
{
const char **opts;
register int i;
if (quiet)
return 0;
opts = opt_get();
for (i = 0; opts[i]; ++i)
printf("%-15s %s\n", opts[i], (opt_isset(opts[i], sh->opt)) ? "yes" : "no");
return 0;
}