]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/hello.c
Imported from ../bash-2.03.tar.gz.
[thirdparty/bash.git] / examples / loadables / hello.c
1 /* Sample builtin to be dynamically loaded with enable -f and create a new
2 builtin. */
3
4 /* See Makefile for compilation details. */
5
6 #include <config.h>
7
8 #if defined (HAVE_UNISTD_H)
9 # include <unistd.h>
10 #endif
11
12 #include <stdio.h>
13
14 #include "builtins.h"
15 #include "shell.h"
16 #include "bashgetopt.h"
17
18 /* A builtin `xxx' is normally implemented with an `xxx_builtin' function.
19 If you're converting a command that uses the normal Unix argc/argv
20 calling convention, use argv = make_builtin_argv (list, &argc) and call
21 the original `main' something like `xxx_main'. Look at cat.c for an
22 example.
23
24 Builtins should use internal_getopt to parse options. It is the same as
25 getopt(3), but it takes a WORD_LIST *. Look at print.c for an example
26 of its use.
27
28 If the builtin takes no options, call no_options(list) before doing
29 anything else. If it returns a non-zero value, your builtin should
30 immediately return EX_USAGE. Look at logname.c for an example.
31
32 A builtin command returns EXECUTION_SUCCESS for success and
33 EXECUTION_FAILURE to indicate failure. */
34 hello_builtin (list)
35 WORD_LIST *list;
36 {
37 printf("hello world\n");
38 fflush (stdout);
39 return (EXECUTION_SUCCESS);
40 }
41
42 /* An array of strings forming the `long' documentation for a builtin xxx,
43 which is printed by `help xxx'. It must end with a NULL. */
44 char *hello_doc[] = {
45 "this is the long doc for the sample hello builtin",
46 (char *)NULL
47 };
48
49 /* The standard structure describing a builtin command. bash keeps an array
50 of these structures. The flags must include BUILTIN_ENABLED so the
51 builtin can be used. */
52 struct builtin hello_struct = {
53 "hello", /* builtin name */
54 hello_builtin, /* function implementing the builtin */
55 BUILTIN_ENABLED, /* initial flags for builtin */
56 hello_doc, /* array of long documentation strings. */
57 "hello", /* usage synopsis; becomes short_doc */
58 0 /* reserved for internal use */
59 };
60