3f371e5587d093e8dac2f61207b3a35c5f352e0f
[platform/upstream/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 #include "builtins.h"
14 #include "shell.h"
15
16 /* A builtin `xxx' is normally implemented with an `xxx_builtin' function.
17    If you're converting a command that uses the normal Unix argc/argv
18    calling convention, use argv = word_list_to_argv (list, &argc) and call
19    the original `main' something like `xxx_main'.  Look at cat.c for an
20    example.
21
22    Builtins should use internal_getopt to parse options.  It is the same as
23    getopt(3), but it takes a WORD_LIST *.  Look at print.c for an example
24    of its use.
25
26    If the builtin takes no options, call no_options(list) before doing
27    anything else.  If it returns a non-zero value, your builtin should
28    immediately return EX_USAGE.  Look at logname.c for an example.
29
30    A builtin command returns EXECUTION_SUCCESS for success and
31    EXECUTION_FAILURE to indicate failure. */
32 hello_builtin (list)
33      WORD_LIST *list;
34 {
35   printf("hello world\n");
36   fflush (stdout);
37   return (EXECUTION_SUCCESS);
38 }
39
40 /* An array of strings forming the `long' documentation for a builtin xxx,
41    which is printed by `help xxx'.  It must end with a NULL. */
42 char *hello_doc[] = {
43         "this is the long doc for the sample hello builtin",
44         "which is a bare-bones echo",
45         (char *)NULL
46 };
47
48 /* The standard structure describing a builtin command.  bash keeps an array
49    of these structures.  The flags must include BUILTIN_ENABLED so the
50    builtin can be used. */
51 struct builtin hello_struct = {
52         "hello",                /* builtin name */
53         hello_builtin,          /* function implementing the builtin */
54         BUILTIN_ENABLED,        /* initial flags for builtin */
55         hello_doc,              /* array of long documentation strings. */
56         "hello [args]",         /* usage synopsis; becomes short_doc */
57         0                       /* reserved for internal use */
58 };
59