blob: b09362bf31b1737bdf2ca8800b8d1bc4dccd9c3a [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* Sample builtin to be dynamically loaded with enable -f and create a new
2 builtin. */
3
4/* See Makefile for compilation details. */
5
Chet Rameyac50fba2014-02-26 09:36:43 -05006/*
7 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8
9 This file is part of GNU Bash.
10 Bash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22*/
23
Jari Aaltob72432f1999-02-19 17:11:39 +000024#include <config.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000025
26#if defined (HAVE_UNISTD_H)
27# include <unistd.h>
28#endif
29
30#include <stdio.h>
Jari Aaltob72432f1999-02-19 17:11:39 +000031
Chet Rameya0c0a002016-09-15 16:59:08 -040032#include "loadables.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000033
34/* A builtin `xxx' is normally implemented with an `xxx_builtin' function.
35 If you're converting a command that uses the normal Unix argc/argv
Jari Aaltob72432f1999-02-19 17:11:39 +000036 calling convention, use argv = make_builtin_argv (list, &argc) and call
Jari Aaltoccc6cda1996-12-23 17:02:34 +000037 the original `main' something like `xxx_main'. Look at cat.c for an
38 example.
39
40 Builtins should use internal_getopt to parse options. It is the same as
41 getopt(3), but it takes a WORD_LIST *. Look at print.c for an example
42 of its use.
43
44 If the builtin takes no options, call no_options(list) before doing
45 anything else. If it returns a non-zero value, your builtin should
46 immediately return EX_USAGE. Look at logname.c for an example.
47
48 A builtin command returns EXECUTION_SUCCESS for success and
49 EXECUTION_FAILURE to indicate failure. */
Jari Aaltof73dda02001-11-13 17:56:06 +000050int
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051hello_builtin (list)
52 WORD_LIST *list;
53{
54 printf("hello world\n");
55 fflush (stdout);
56 return (EXECUTION_SUCCESS);
57}
58
Chet Rameya0c0a002016-09-15 16:59:08 -040059int
60hello_builtin_load (s)
61 char *s;
62{
63 printf ("hello builtin loaded\n");
64 fflush (stdout);
65 return (1);
66}
67
68void
69hello_builtin_unload (s)
70 char *s;
71{
72 printf ("hello builtin unloaded\n");
73 fflush (stdout);
74}
75
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076/* An array of strings forming the `long' documentation for a builtin xxx,
Jari Aalto31859422009-01-12 13:36:28 +000077 which is printed by `help xxx'. It must end with a NULL. By convention,
78 the first line is a short description. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000079char *hello_doc[] = {
Jari Aalto31859422009-01-12 13:36:28 +000080 "Sample builtin.",
81 "",
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082 "this is the long doc for the sample hello builtin",
Jari Aaltoccc6cda1996-12-23 17:02:34 +000083 (char *)NULL
84};
85
86/* The standard structure describing a builtin command. bash keeps an array
87 of these structures. The flags must include BUILTIN_ENABLED so the
88 builtin can be used. */
89struct builtin hello_struct = {
90 "hello", /* builtin name */
91 hello_builtin, /* function implementing the builtin */
92 BUILTIN_ENABLED, /* initial flags for builtin */
93 hello_doc, /* array of long documentation strings. */
Jari Aaltob72432f1999-02-19 17:11:39 +000094 "hello", /* usage synopsis; becomes short_doc */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000095 0 /* reserved for internal use */
96};