Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* Sample builtin to be dynamically loaded with enable -f and create a new |
| 2 | builtin. */ |
| 3 | |
| 4 | /* See Makefile for compilation details. */ |
| 5 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6 | /* |
| 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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 24 | #include <config.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 25 | |
| 26 | #if defined (HAVE_UNISTD_H) |
| 27 | # include <unistd.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <stdio.h> |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 31 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 32 | #include "builtins.h" |
| 33 | #include "shell.h" |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 34 | #include "bashgetopt.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 35 | |
| 36 | /* A builtin `xxx' is normally implemented with an `xxx_builtin' function. |
| 37 | If you're converting a command that uses the normal Unix argc/argv |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 38 | calling convention, use argv = make_builtin_argv (list, &argc) and call |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | the original `main' something like `xxx_main'. Look at cat.c for an |
| 40 | example. |
| 41 | |
| 42 | Builtins should use internal_getopt to parse options. It is the same as |
| 43 | getopt(3), but it takes a WORD_LIST *. Look at print.c for an example |
| 44 | of its use. |
| 45 | |
| 46 | If the builtin takes no options, call no_options(list) before doing |
| 47 | anything else. If it returns a non-zero value, your builtin should |
| 48 | immediately return EX_USAGE. Look at logname.c for an example. |
| 49 | |
| 50 | A builtin command returns EXECUTION_SUCCESS for success and |
| 51 | EXECUTION_FAILURE to indicate failure. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 52 | int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | hello_builtin (list) |
| 54 | WORD_LIST *list; |
| 55 | { |
| 56 | printf("hello world\n"); |
| 57 | fflush (stdout); |
| 58 | return (EXECUTION_SUCCESS); |
| 59 | } |
| 60 | |
| 61 | /* An array of strings forming the `long' documentation for a builtin xxx, |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 62 | which is printed by `help xxx'. It must end with a NULL. By convention, |
| 63 | the first line is a short description. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | char *hello_doc[] = { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 65 | "Sample builtin.", |
| 66 | "", |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | "this is the long doc for the sample hello builtin", |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | (char *)NULL |
| 69 | }; |
| 70 | |
| 71 | /* The standard structure describing a builtin command. bash keeps an array |
| 72 | of these structures. The flags must include BUILTIN_ENABLED so the |
| 73 | builtin can be used. */ |
| 74 | struct builtin hello_struct = { |
| 75 | "hello", /* builtin name */ |
| 76 | hello_builtin, /* function implementing the builtin */ |
| 77 | BUILTIN_ENABLED, /* initial flags for builtin */ |
| 78 | hello_doc, /* array of long documentation strings. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 79 | "hello", /* usage synopsis; becomes short_doc */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 80 | 0 /* reserved for internal use */ |
| 81 | }; |
| 82 | |