0.1.3
[kbuild-mirror.git] / src / kash / shinstance.c
blob492fd30d6f2302a01c32bdb1f43a1c9bbdaba6ba
1 /* $Id$ */
2 /** @file
4 * The shell instance methods.
6 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
9 * This file is part of kBuild.
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.
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.
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
27 #include <string.h>
28 #include <stdlib.h>
29 #ifndef _MSC_VER
30 # include <unistd.h>
31 # include <pwd.h>
32 extern char **environ;
33 #endif
34 #include "shinstance.h"
37 /**
38 * Creates a root shell instance.
40 * @param inherit The shell to inherit from. If NULL inherit from environment and such.
41 * @param argc The argument count.
42 * @param argv The argument vector.
44 * @returns pointer to root shell on success, NULL on failure.
46 shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv)
48 shinstance *psh;
50 psh = calloc(sizeof(*psh), 1);
51 if (psh)
53 /* the special stuff. */
54 #ifdef _MSC_VER
55 psh->pid = _getpid();
56 #else
57 psh->pid = getpid();
58 #endif
60 /* memalloc.c */
61 psh->stacknleft = MINSIZE;
62 psh->herefd = -1;
63 psh->stackp = &psh->stackbase;
64 psh->stacknxt = psh->stackbase.space;
66 /* input.c */
67 psh->plinno = 1;
68 psh->init_editline = 0;
69 psh->parsefile = &psh->basepf;
71 /* output.c */
72 psh->output.bufsize = OUTBUFSIZ;
73 psh->output.fd = 1;
74 psh->output.psh = psh;
75 psh->errout.bufsize = 100;
76 psh->errout.fd = 2;
77 psh->errout.psh = psh;
78 psh->memout.fd = MEM_OUT;
79 psh->memout.psh = psh;
80 psh->out1 = &psh->output;
81 psh->out2 = &psh->errout;
83 /* jobs.c */
84 psh->backgndpid = -1;
85 #if JOBS
86 psh->curjob = -1;
87 #endif
88 psh->ttyfd = -1;
91 return psh;
95 char *sh_getenv(shinstance *psh, const char *var)
97 #ifdef SH_PURE_STUB_MODE
98 return NULL;
99 #elif defined(SH_STUB_MODE)
100 return getenv(var);
101 #else
102 #endif
105 char **sh_environ(shinstance *psh)
107 #ifdef SH_PURE_STUB_MODE
108 static char *s_null[2] = {0,0};
109 return &s_null[0];
110 #elif defined(SH_STUB_MODE)
111 return environ;
112 #else
113 #endif
116 const char *sh_gethomedir(shinstance *psh, const char *user)
118 #ifdef SH_PURE_STUB_MODE
119 return NULL;
120 #elif defined(SH_STUB_MODE)
121 # ifdef _MSC_VER
122 return NULL;
123 # else
124 struct passwd *pwd = getpwnam(user);
125 return pwd ? pwd->pw_dir : NULL;
126 # endif
127 #else
128 #endif
131 int sh_sigaction(shinstance *psh, int signo, const struct shsigaction *newp, struct shsigaction *oldp)
133 #ifdef SH_PURE_STUB_MODE
134 return -1;
135 #elif defined(SH_STUB_MODE)
136 # ifdef _MSC_VER
137 return -1;
138 # else
139 struct sigaction old;
140 if (newp)
141 return -1;
142 if (sigaction(signo, NULL, &old))
143 return -1;
144 oldp->sh_flags = old.sa_flags;
145 oldp->sh_handler = old.sa_handler;
146 oldp->sh_mask = old.sa_mask;
147 return 0;
148 # endif
149 #else
150 #endif
153 shsig_t sh_signal(shinstance *psh, int signo, shsig_t handler)
155 return (shsig_t)-1;
158 int sh_siginterrupt(shinstance *psh, int signo, int interrupt)
160 return -1;
163 void sh_sigemptyset(shsigset_t *setp)
165 memset(setp, 0, sizeof(*setp));
168 int sh_sigprocmask(shinstance *psh, int operation, shsigset_t const *newp, shsigset_t *oldp)
170 return -1;
173 void sh_abort(shinstance *psh)
175 #ifdef SH_PURE_STUB_MODE
176 #elif defined(SH_STUB_MODE)
177 abort();
178 #else
179 #endif
182 void sh_raise_sigint(shinstance *psh)
184 #ifdef SH_PURE_STUB_MODE
185 #elif defined(SH_STUB_MODE)
186 raise(SIGINT);
187 #else
188 #endif
191 int sh_kill(shinstance *psh, pid_t pid, int signo)
193 #ifdef SH_PURE_STUB_MODE
194 return -1;
195 #elif defined(SH_STUB_MODE)
196 # ifdef _MSC_VER
197 return -1;
198 # else
199 //fprintf(stderr, "kill(%d, %d)\n", pid, signo);
200 return kill(pid, signo);
201 # endif
202 #else
203 #endif
206 int sh_killpg(shinstance *psh, pid_t pgid, int signo)
208 #ifdef SH_PURE_STUB_MODE
209 return -1;
210 #elif defined(SH_STUB_MODE)
211 # ifdef _MSC_VER
212 return -1;
213 # else
214 //fprintf(stderr, "killpg(%d, %d)\n", pgid, signo);
215 return killpg(pgid, signo);
216 # endif
217 #else
218 #endif
221 clock_t sh_times(shinstance *psh, shtms *tmsp)
223 #ifdef SH_PURE_STUB_MODE
224 return 0;
225 #elif defined(SH_STUB_MODE)
226 # ifdef _MSC_VER
227 return 0;
228 # else
229 return times(tmsp);
230 # endif
231 #else
232 #endif
235 int sh_sysconf_clk_tck(void)
237 #ifdef SH_PURE_STUB_MODE
238 return 1;
239 #else
240 # ifdef _MSC_VER
241 return CLK_TCK;
242 # else
243 return sysconf(_SC_CLK_TCK);
244 # endif
245 #endif
248 pid_t sh_fork(shinstance *psh)
250 #ifdef SH_PURE_STUB_MODE
251 return -1;
252 #elif defined(SH_STUB_MODE)
253 # ifdef _MSC_VER
254 return -1;
255 # else
256 return fork();
257 # endif
258 #else
259 #endif
262 pid_t sh_waitpid(shinstance *psh, pid_t pid, int *statusp, int flags)
264 *statusp = 0;
265 #ifdef SH_PURE_STUB_MODE
266 return -1;
267 #elif defined(SH_STUB_MODE)
268 # ifdef _MSC_VER
269 return -1;
270 # else
271 pid = waitpid(pid, statusp, flags);
272 //fprintf(stderr, "waitpid -> %d *statusp=%d (rc=%d) flags=%#x\n", pid, *statusp, WEXITSTATUS(*statusp), flags);
273 return pid;
274 # endif
275 #else
276 #endif
279 void sh__exit(shinstance *psh, int rc)
281 #ifdef SH_PURE_STUB_MODE
282 return -1;
283 #elif defined(SH_STUB_MODE)
284 _exit(rc);
285 #else
286 #endif
289 int sh_execve(shinstance *psh, const char *exe, const char * const *argv, const char * const *envp)
291 #ifdef SH_PURE_STUB_MODE
292 return -1;
293 #elif defined(SH_STUB_MODE)
294 # ifdef _MSC_VER
295 return -1;
296 # else
297 return execve(exe, (char **)argv, (char **)envp);
298 # endif
299 #else
300 #endif
303 uid_t sh_getuid(shinstance *psh)
305 #ifdef SH_PURE_STUB_MODE
306 return 0;
307 #elif defined(SH_STUB_MODE)
308 # ifdef _MSC_VER
309 return 0;
310 # else
311 return getuid();
312 # endif
313 #else
314 #endif
317 uid_t sh_geteuid(shinstance *psh)
319 #ifdef SH_PURE_STUB_MODE
320 return 0;
321 #elif defined(SH_STUB_MODE)
322 # ifdef _MSC_VER
323 return 0;
324 # else
325 return geteuid();
326 # endif
327 #else
328 #endif
331 gid_t sh_getgid(shinstance *psh)
333 #ifdef SH_PURE_STUB_MODE
334 return 0;
335 #elif defined(SH_STUB_MODE)
336 # ifdef _MSC_VER
337 return 0;
338 # else
339 return getgid();
340 # endif
341 #else
342 #endif
345 gid_t sh_getegid(shinstance *psh)
347 #ifdef SH_PURE_STUB_MODE
348 return 0;
349 #elif defined(SH_STUB_MODE)
350 # ifdef _MSC_VER
351 return 0;
352 # else
353 return getegid();
354 # endif
355 #else
356 #endif
359 pid_t sh_getpid(shinstance *psh)
361 #ifdef SH_PURE_STUB_MODE
362 return 0;
363 #elif defined(SH_STUB_MODE)
364 # ifdef _MSC_VER
365 return _getpid();
366 # else
367 return getpid();
368 # endif
369 #else
370 #endif
373 pid_t sh_getpgrp(shinstance *psh)
375 #ifdef SH_PURE_STUB_MODE
376 return 0;
377 #elif defined(SH_STUB_MODE)
378 # ifdef _MSC_VER
379 return _getpid();
380 # else
381 return getpgrp();
382 # endif
383 #else
384 #endif
387 pid_t sh_getpgid(shinstance *psh, pid_t pid)
389 #ifdef SH_PURE_STUB_MODE
390 return pid;
391 #elif defined(SH_STUB_MODE)
392 # ifdef _MSC_VER
393 return pid;
394 # else
395 return getpgid(pid);
396 # endif
397 #else
398 #endif
401 int sh_setpgid(shinstance *psh, pid_t pid, pid_t pgid)
403 #ifdef SH_PURE_STUB_MODE
404 return -1;
405 #elif defined(SH_STUB_MODE)
406 # ifdef _MSC_VER
407 return -1;
408 # else
409 int rc;
410 rc = setpgid(pid, pgid);
411 //fprintf(stderr, "setpgid(%d,%d) -> %d\n", pid, pgid, rc);
412 return rc;
413 # endif
414 #else
415 #endif
418 pid_t sh_tcgetpgrp(shinstance *psh, int fd)
420 #ifdef SH_PURE_STUB_MODE
421 return -1;
422 #elif defined(SH_STUB_MODE)
423 # ifdef _MSC_VER
424 return -1;
425 # else
426 return tcgetpgrp(fd);
427 # endif
428 #else
429 #endif
432 int sh_tcsetpgrp(shinstance *psh, int fd, pid_t pgrp)
434 #ifdef SH_PURE_STUB_MODE
435 return -1;
436 #elif defined(SH_STUB_MODE)
437 # ifdef _MSC_VER
438 return -1;
439 # else
440 return tcsetpgrp(fd, pgrp);
441 # endif
442 #else
443 #endif
446 int sh_getrlimit(shinstance *psh, int resid, shrlimit *limp)
448 #ifdef SH_PURE_STUB_MODE
449 return -1;
450 #elif defined(SH_STUB_MODE)
451 # ifdef _MSC_VER
452 return -1;
453 # else
454 return getrlimit(resid, limp);
455 # endif
456 #else
457 #endif
460 int sh_setrlimit(shinstance *psh, int resid, const shrlimit *limp)
462 #ifdef SH_PURE_STUB_MODE
463 return -1;
464 #elif defined(SH_STUB_MODE)
465 # ifdef _MSC_VER
466 return -1;
467 # else
468 return setrlimit(resid, limp);
469 # endif
470 #else
471 #endif