busybox: update to 1.23.2
[tomato.git] / release / src / router / busybox / docs / new-applet-HOWTO.txt
blob078e77bce2bcec0aa5ae1912241daf4c24d57e36
1 How to Add a New Applet to BusyBox
2 ==================================
4 This document details the steps you must take to add a new applet to BusyBox.
6 Credits:
7 Matt Kraai - initial writeup
8 Mark Whitley - the remix
9 Thomas Lundquist - trying to keep it updated
11 When doing this you should consider using the latest git HEAD.
12 This is a good thing if you plan to getting it committed into mainline.
14 Initial Write
15 -------------
17 First, write your applet.  Be sure to include copyright information at the top,
18 such as who you stole the code from and so forth. Also include the mini-GPL
19 boilerplate and Config.in/Kbuild/usage/applet.h snippets (more on that below
20 in this document). Be sure to name the main function <applet>_main instead
21 of main. And be sure to put it in <applet>.c. Make sure to #include "libbb.h"
22 as the first include file in your applet.
24 For a new applet mu, here is the code that would go in mu.c:
26 (libbb.h already includes most usual header files. You do not need
27 #include <stdio.h> etc...)
30 ----begin example code------
32 /* vi: set sw=4 ts=4: */
34  * Mini mu implementation for busybox
35  *
36  * Copyright (C) [YEAR] by [YOUR NAME] <YOUR EMAIL>
37  *
38  * Licensed under GPLv2, see file LICENSE in this source tree.
39  */
41 #include "libbb.h"
42 #include "other.h"
44 //config:config MU
45 //config:       bool "MU"
46 //config:       default y
47 //config:       help
48 //config:         Returns an indeterminate value.
50 //kbuild:lib-$(CONFIG_MU) += mu.o
51 //applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP))
53 //usage:#define mu_trivial_usage
54 //usage:        "[-abcde] FILE..."
55 //usage:#define mu_full_usage
56 //usage:        "Returns an indeterminate value\n"
57 //usage:     "\n        -a      First function"
58 //usage:     "\n        -b      Second function"
60 int mu_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
61 int mu_main(int argc, char **argv)
63         int fd;
64         ssize_t n;
65         char mu;
67         fd = xopen("/dev/random", O_RDONLY);
69         if ((n = safe_read(fd, &mu, 1)) < 1)
70                 bb_perror_msg_and_die("/dev/random");
72         return mu;
75 ----end example code------
78 Coding Style
79 ------------
81 Before you submit your applet for inclusion in BusyBox, (or better yet, before
82 you _write_ your applet) please read through the style guide in the docs
83 directory and make your program compliant.
86 Some Words on libbb
87 -------------------
89 As you are writing your applet, please be aware of the body of pre-existing
90 useful functions in libbb. Use these instead of reinventing the wheel.
92 Additionally, if you have any useful, general-purpose functions in your
93 applet that could be useful in other applets, consider putting them in libbb.
95 And it may be possible that some of the other applets uses functions you
96 could use. If so, you have to rip the function out of the applet and make
97 a libbb function out of it.
99 Adding a libbb function:
100 ------------------------
102 Make a new file named <function_name>.c
104 ----start example code------
106 #include "libbb.h"
107 #include "other.h"
109 //kbuild:lib-y += function.o
111 int function(char *a)
113         return *a;
116 ----end example code------
118 Remember about the kbuild snippet.
120 You should also try to find a suitable place in include/libbb.h for
121 the function declaration. If not, add it somewhere anyway, with or without
122 ifdefs to include or not.
124 You can look at libbb/Config.src and try to find out if the function is
125 tunable and add it there if it is.
128 Kbuild/Config.in/usage/applets.h snippets in .c files
129 -----------------------------------------------------
131 The old way of adding new applets was to put all the information needed by the
132 configuration and build system into appropriate files (namely: Kbuild.src and
133 Config.src in new applet's directory) and to add the applet declaration and
134 usage info text to include/applets.src.h and include/usage.src.h respectively.
136 Since the scripts/gen_build_files.sh script had been introduced, the preferred
137 way is to have all these declarations contained within the applet .c files.
139 Every line intended to be processed by gen_build_files.sh should start as a
140 comment without any preceding whitespaces and be followed by an appropriate
141 keyword - kbuild, config, usage or applet - and a colon, just like shown in the
142 first example above.
145 Placement / Directory
146 ---------------------
148 Find the appropriate directory for your new applet.
150 Add the kbuild snippet to the .c file:
152 //kbuild:lib-$(CONFIG_MU) += mu.o
154 Add the config snippet to the .c file:
156 //config:config MU
157 //config:       bool "MU"
158 //config:       default y
159 //config:       help
160 //config:         Returns an indeterminate value.
163 Usage String(s)
164 ---------------
166 Next, add usage information for your applet to the .c file.
167 This should look like the following:
169 //usage:#define mu_trivial_usage
170 //usage:        "[-abcde] FILE..."
171 //usage:#define mu_full_usage
172 //usage:        "Returns an indeterminate value\n"
173 //usage:     "\n        -a      First function"
174 //usage:     "\n        -b      Second function"
175 //usage:        ...
177 If your program supports flags, the flags should be mentioned on the first
178 line ([-abcde]) and a detailed description of each flag should go in the
179 mu_full_usage section, one flag per line.
182 Header Files
183 ------------
185 Finally add the applet declaration snippet. Be sure to read the top of
186 applets.src.h before adding your applet - it contains important info
187 on applet macros and conventions.
189 //applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP))
192 The Grand Announcement
193 ----------------------
195 Then create a diff by adding the new files to git (remember your libbb files)
196         git add <where you put it>/mu.c
197 eventually also:
198         git add libbb/function.c
199 then
200         git commit
201         git format-patch HEAD^
202 and send it to the mailing list:
203         busybox@busybox.net
204         http://busybox.net/mailman/listinfo/busybox
206 Sending patches as attachments is preferred, but not required.