blob: 135cdb30a32ca9cdcb0968e8947f0e86a69211d6 [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* This module should be dynamically loaded with enable -f
2 * which would create a new builtin named mypid. You'll need
3 * the source code for GNU bash to recompile this module.
4 *
5 * Then, from within bash, enable -f ./mypid enable_mypid, where ./mypid
6 * is the binary obtained from running make. Hereafter, `${MYPID}'
7 * is a shell builtin variable.
8 */
9
10#include <stdio.h>
11#include <errno.h>
12#include <string.h>
13
14#include "builtins.h"
15#include "shell.h"
16
17#define INIT_DYNAMIC_VAR(var, val, gfunc, afunc) \
18 do \
19 { SHELL_VAR *v = bind_variable (var, (val), 0); \
20 v->dynamic_value = gfunc; \
21 v->assign_func = afunc; \
22 } \
23 while (0)
24
25static SHELL_VAR *
26assign_mypid (
27 SHELL_VAR *self,
28 char *value,
29 arrayind_t unused,
30 char *key )
31{
32 return (self);
33}
34
35static SHELL_VAR *
36get_mypid (SHELL_VAR *var)
37{
38 int rv;
39 char *p;
40
41 rv = getpid();
42 p = itos (rv);
43
44 FREE (value_cell (var));
45
46 VSETATTR (var, att_integer);
47 var_setvalue (var, p);
48 return (var);
49}
50
51int
52enable_mypid_builtin(WORD_LIST *list)
53{
54 INIT_DYNAMIC_VAR ("MYPID", (char *)NULL, get_mypid, assign_mypid);
55
56 return 0;
57}
58
59char const *enable_mypid_doc[] = {
60 "Enable $MYPID.",
61 "",
62 "Enables use of the ${MYPID} dynamic variable. ",
63 "It will yield the current pid of a subshell.",
64 (char *)0
65};
66
67struct builtin enable_mypid_struct = {
68 "enable_mypid",
69 enable_mypid_builtin,
70 BUILTIN_ENABLED,
71 (char**)(void*)enable_mypid_doc,
72 "enable_mypid N",
73 0
74};