Patch-ID: bash40-021
[bash.git] / examples / loadables / mypid.c
blob135cdb30a32ca9cdcb0968e8947f0e86a69211d6
1 /* 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.
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 */
10 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
14 #include "builtins.h"
15 #include "shell.h"
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)
25 static SHELL_VAR *
26 assign_mypid (
27 SHELL_VAR *self,
28 char *value,
29 arrayind_t unused,
30 char *key )
32 return (self);
35 static SHELL_VAR *
36 get_mypid (SHELL_VAR *var)
38 int rv;
39 char *p;
41 rv = getpid();
42 p = itos (rv);
44 FREE (value_cell (var));
46 VSETATTR (var, att_integer);
47 var_setvalue (var, p);
48 return (var);
51 int
52 enable_mypid_builtin(WORD_LIST *list)
54 INIT_DYNAMIC_VAR ("MYPID", (char *)NULL, get_mypid, assign_mypid);
56 return 0;
59 char 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
67 struct builtin enable_mypid_struct = {
68 "enable_mypid",
69 enable_mypid_builtin,
70 BUILTIN_ENABLED,
71 (char**)(void*)enable_mypid_doc,
72 "enable_mypid N",