source: trunk/src/kash/shinstance.c@ 1229

Last change on this file since 1229 was 1229, checked in by bird, 18 years ago

stubs, made echo work.

  • Property svn:eol-style set to LF
File size: 7.8 KB
Line 
1/* $Id: $ */
2/** @file
3 *
4 * The shell instance methods.
5 *
6 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <string.h>
28#include "shinstance.h"
29
30
31/**
32 * Creates a root shell instance.
33 *
34 * @param inherit The shell to inherit from. If NULL inherit from environment and such.
35 * @param argc The argument count.
36 * @param argv The argument vector.
37 *
38 * @returns pointer to root shell on success, NULL on failure.
39 */
40shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
41{
42 shinstance *psh;
43
44 psh = calloc(sizeof(*psh), 1);
45 if (psh)
46 {
47 /* memalloc.c */
48 psh->stacknleft = MINSIZE;
49 psh->herefd = -1;
50 psh->stackp = &psh->stackbase;
51 psh->stacknxt = psh->stackbase.space;
52
53 /* input.c */
54 psh->plinno = 1;
55 psh->init_editline = 0;
56 psh->parsefile = &psh->basepf;
57
58 /* output.c */
59 psh->output.bufsize = OUTBUFSIZ;
60 psh->output.fd = 1;
61 psh->output.psh = psh;
62 psh->errout.bufsize = 100;
63 psh->errout.fd = 2;
64 psh->errout.psh = psh;
65 psh->memout.fd = MEM_OUT;
66 psh->memout.psh = psh;
67 psh->out1 = &psh->output;
68 psh->out2 = &psh->errout;
69
70 /* jobs.c */
71 psh->backgndpid = -1;
72#if JOBS
73 psh->curjob = -1;
74#endif
75 psh->ttyfd = -1;
76
77 }
78 return psh;
79}
80
81
82char *sh_getenv(shinstance *psh, const char *var)
83{
84#ifdef SH_PURE_STUB_MODE
85 return NULL;
86#elif defined(SH_STUB_MODE)
87 return getenv(var);
88#else
89#endif
90}
91
92char **sh_environ(shinstance *psh)
93{
94#ifdef SH_PURE_STUB_MODE
95 static char *s_null[2] = {0,0};
96 return &s_null[0];
97#elif defined(SH_STUB_MODE)
98 return environ;
99#else
100#endif
101}
102
103const char *sh_gethomedir(shinstance *psh, const char *user)
104{
105#ifdef SH_PURE_STUB_MODE
106 return NULL;
107#elif defined(SH_STUB_MODE)
108# ifdef _MSC_VER
109 return NULL;
110# else
111 struct passwd *pwd = getpwnam(user);
112 return pwd ? pwd->pw_dir;
113# endif
114#else
115#endif
116}
117
118int sh_sigaction(int signo, const struct sh_sigaction *newp, struct sh_sigaction *oldp)
119{
120#ifdef SH_PURE_STUB_MODE
121 return -1;
122#elif defined(SH_STUB_MODE)
123# ifdef _MSC_VER
124 return -1;
125# else
126 struct sigaction old;
127 if (newp)
128 return -1;
129 if (sigaction(signo, NULL, &old))
130 return -1;
131 oldp->sh_flags = old.sa_flags;
132 oldp->sh_handler = old.sa_handler;
133 oldp->sh_mask = old.sa_mask;
134 return 0;
135# endif
136#else
137#endif
138}
139
140sh_sig_t sh_signal(shinstance *psh, int signo, sh_sig_t handler)
141{
142 return (sh_sig_t)-1;
143}
144
145int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
146{
147 return -1;
148}
149
150void sh_sigemptyset(sh_sigset_t *setp)
151{
152 memset(setp, 0, sizeof(*setp));
153}
154
155int sh_sigprocmask(shinstance *psh, int operation, sh_sigset_t const *newp, sh_sigset_t *oldp)
156{
157 return -1;
158}
159
160void sh_abort(shinstance *psh)
161{
162#ifdef SH_PURE_STUB_MODE
163#elif defined(SH_STUB_MODE)
164 abort();
165#else
166#endif
167}
168
169void sh_raise_sigint(shinstance *psh)
170{
171#ifdef SH_PURE_STUB_MODE
172#elif defined(SH_STUB_MODE)
173 raise(SIGINT);
174#else
175#endif
176}
177
178int sh_kill(shinstance *psh, pid_t pid, int signo)
179{
180#ifdef SH_PURE_STUB_MODE
181 return -1;
182#elif defined(SH_STUB_MODE)
183# ifdef _MSC_VER
184 return -1;
185# else
186 return kill(pid, signo);
187# endif
188#else
189#endif
190}
191
192int sh_killpg(shinstance *psh, pid_t pgid, int signo)
193{
194#ifdef SH_PURE_STUB_MODE
195 return -1;
196#elif defined(SH_STUB_MODE)
197# ifdef _MSC_VER
198 return -1;
199# else
200 return killpg(pgid, signo);
201# endif
202#else
203#endif
204}
205
206clock_t sh_times(shinstance *psh, shtms *tmsp)
207{
208#ifdef SH_PURE_STUB_MODE
209 return 0;
210#elif defined(SH_STUB_MODE)
211# ifdef _MSC_VER
212 return 0;
213# else
214 return times(tmsp);
215# endif
216#else
217#endif
218}
219
220int sh_sysconf_clk_tck(void)
221{
222#ifdef SH_PURE_STUB_MODE
223 return 1;
224#else
225# ifdef _MSC_VER
226 return CLK_TCK;
227# else
228 return sysconf(_SC_CLK_TCK)
229# endif
230#endif
231}
232
233pid_t sh_fork(shinstance *psh)
234{
235#ifdef SH_PURE_STUB_MODE
236 return -1;
237#elif defined(SH_STUB_MODE)
238# ifdef _MSC_VER
239 return -1;
240# else
241 return fork();
242# endif
243#else
244#endif
245}
246
247pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
248{
249#ifdef SH_PURE_STUB_MODE
250 return -1;
251#elif defined(SH_STUB_MODE)
252# ifdef _MSC_VER
253 return -1;
254# else
255 return waitpid(pid, statusp, flags);
256# endif
257#else
258#endif
259}
260
261void sh__exit(shinstance *psh, int rc)
262{
263#ifdef SH_PURE_STUB_MODE
264 return -1;
265#elif defined(SH_STUB_MODE)
266 _exit(rc);
267#else
268#endif
269}
270
271int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
272{
273#ifdef SH_PURE_STUB_MODE
274 return -1;
275#elif defined(SH_STUB_MODE)
276# ifdef _MSC_VER
277 return -1;
278# else
279 return execve(exe, (char **)argv, (char **)envp);
280# endif
281#else
282#endif
283}
284
285uid_t sh_getuid(shinstance *psh)
286{
287#ifdef SH_PURE_STUB_MODE
288 return 0;
289#elif defined(SH_STUB_MODE)
290# ifdef _MSC_VER
291 return 0;
292# else
293 return getuid();
294# endif
295#else
296#endif
297}
298
299uid_t sh_geteuid(shinstance *psh)
300{
301#ifdef SH_PURE_STUB_MODE
302 return 0;
303#elif defined(SH_STUB_MODE)
304# ifdef _MSC_VER
305 return 0;
306# else
307 return geteuid();
308# endif
309#else
310#endif
311}
312
313gid_t sh_getgid(shinstance *psh)
314{
315#ifdef SH_PURE_STUB_MODE
316 return 0;
317#elif defined(SH_STUB_MODE)
318# ifdef _MSC_VER
319 return 0;
320# else
321 return getgid();
322# endif
323#else
324#endif
325}
326
327gid_t sh_getegid(shinstance *psh)
328{
329#ifdef SH_PURE_STUB_MODE
330 return 0;
331#elif defined(SH_STUB_MODE)
332# ifdef _MSC_VER
333 return 0;
334# else
335 return getegid();
336# endif
337#else
338#endif
339}
340
341pid_t sh_getpid(shinstance *psh)
342{
343#ifdef SH_PURE_STUB_MODE
344 return 0;
345#elif defined(SH_STUB_MODE)
346# ifdef _MSC_VER
347 return _getpid();
348# else
349 return getpid();
350# endif
351#else
352#endif
353}
354
355pid_t sh_getpgrp(shinstance *psh)
356{
357#ifdef SH_PURE_STUB_MODE
358 return 0;
359#elif defined(SH_STUB_MODE)
360# ifdef _MSC_VER
361 return _getpid();
362# else
363 return getpgrp();
364# endif
365#else
366#endif
367}
368
369pid_t sh_getpgid(shinstance *psh, pid_t pid)
370{
371#ifdef SH_PURE_STUB_MODE
372 return pid;
373#elif defined(SH_STUB_MODE)
374# ifdef _MSC_VER
375 return pid;
376# else
377 return getpgid(pid);
378# endif
379#else
380#endif
381}
382
383int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
384{
385#ifdef SH_PURE_STUB_MODE
386 return -1;
387#elif defined(SH_STUB_MODE)
388# ifdef _MSC_VER
389 return -1;
390# else
391 return setpgid(pid, pgid);
392# endif
393#else
394#endif
395}
396
397pid_t sh_tcgetpgrp(shinstance *psh, int fd)
398{
399#ifdef SH_PURE_STUB_MODE
400 return -1;
401#elif defined(SH_STUB_MODE)
402# ifdef _MSC_VER
403 return -1;
404# else
405 return tcgetpgrp(fd);
406# endif
407#else
408#endif
409}
410
411int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
412{
413#ifdef SH_PURE_STUB_MODE
414 return -1;
415#elif defined(SH_STUB_MODE)
416# ifdef _MSC_VER
417 return -1;
418# else
419 return tcsetpgrp(fd, pgrp);
420# endif
421#else
422#endif
423}
424
425int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
426{
427#ifdef SH_PURE_STUB_MODE
428 return -1;
429#elif defined(SH_STUB_MODE)
430# ifdef _MSC_VER
431 return -1;
432# else
433 return getrlimit(resid, limp);
434# endif
435#else
436#endif
437}
438
439int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
440{
441#ifdef SH_PURE_STUB_MODE
442 return -1;
443#elif defined(SH_STUB_MODE)
444# ifdef _MSC_VER
445 return -1;
446# else
447 return setrlimit(resid, limp);
448# endif
449#else
450#endif
451}
452
Note: See TracBrowser for help on using the repository browser.