blob: 078e77bce2bcec0aa5ae1912241daf4c24d57e36 [file] [log] [blame]
Mark Whitleyeac26362000-12-19 17:35:24 +00001How to Add a New Applet to BusyBox
2==================================
3
4This document details the steps you must take to add a new applet to BusyBox.
5
6Credits:
7Matt Kraai - initial writeup
8Mark Whitley - the remix
Denys Vlasenkof3326172014-03-17 15:06:29 +01009Thomas Lundquist - trying to keep it updated
Denis Vlasenko7e84e532007-05-08 17:52:17 +000010
Dan Fandrich96693632010-08-10 23:45:27 -070011When doing this you should consider using the latest git HEAD.
Denys Vlasenko5370bfb2009-09-06 02:58:59 +020012This is a good thing if you plan to getting it committed into mainline.
Mark Whitleyeac26362000-12-19 17:35:24 +000013
14Initial Write
15-------------
16
Mike Frysinger9754b912005-09-02 23:06:30 +000017First, write your applet. Be sure to include copyright information at the top,
18such as who you stole the code from and so forth. Also include the mini-GPL
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +010019boilerplate and Config.in/Kbuild/usage/applet.h snippets (more on that below
20in this document). Be sure to name the main function <applet>_main instead
21of main. And be sure to put it in <applet>.c. Make sure to #include "libbb.h"
22as the first include file in your applet.
Glenn L McGrath9c91e412003-10-01 11:33:46 +000023
24For a new applet mu, here is the code that would go in mu.c:
Mark Whitleyeac26362000-12-19 17:35:24 +000025
Denys Vlasenkof3326172014-03-17 15:06:29 +010026(libbb.h already includes most usual header files. You do not need
Denis Vlasenko7e84e532007-05-08 17:52:17 +000027#include <stdio.h> etc...)
28
29
Mark Whitleyeac26362000-12-19 17:35:24 +000030----begin example code------
31
32/* vi: set sw=4 ts=4: */
33/*
34 * Mini mu implementation for busybox
35 *
Mark Whitleyeac26362000-12-19 17:35:24 +000036 * Copyright (C) [YEAR] by [YOUR NAME] <YOUR EMAIL>
37 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020038 * Licensed under GPLv2, see file LICENSE in this source tree.
Mark Whitleyeac26362000-12-19 17:35:24 +000039 */
40
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000041#include "libbb.h"
Denis Vlasenko7e84e532007-05-08 17:52:17 +000042#include "other.h"
Mark Whitleyeac26362000-12-19 17:35:24 +000043
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +010044//config:config MU
45//config: bool "MU"
Denys Vlasenkof3326172014-03-17 15:06:29 +010046//config: default y
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +010047//config: help
48//config: Returns an indeterminate value.
49
50//kbuild:lib-$(CONFIG_MU) += mu.o
51//applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP))
52
53//usage:#define mu_trivial_usage
Denys Vlasenkof3326172014-03-17 15:06:29 +010054//usage: "[-abcde] FILE..."
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +010055//usage:#define mu_full_usage
Denys Vlasenkof3326172014-03-17 15:06:29 +010056//usage: "Returns an indeterminate value\n"
57//usage: "\n -a First function"
58//usage: "\n -b Second function"
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +010059
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int mu_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Mark Whitleyeac26362000-12-19 17:35:24 +000061int mu_main(int argc, char **argv)
62{
63 int fd;
Denis Vlasenko360362d2007-11-04 04:46:46 +000064 ssize_t n;
Mark Whitleyeac26362000-12-19 17:35:24 +000065 char mu;
66
Bernhard Reutner-Fischer8c1eda52006-08-28 23:39:36 +000067 fd = xopen("/dev/random", O_RDONLY);
Mark Whitleyeac26362000-12-19 17:35:24 +000068
69 if ((n = safe_read(fd, &mu, 1)) < 1)
Mike Frysinger0ea3a6f2005-04-23 01:43:45 +000070 bb_perror_msg_and_die("/dev/random");
Mark Whitleyeac26362000-12-19 17:35:24 +000071
72 return mu;
73}
74
75----end example code------
76
Mark Whitley8a6b6192000-12-19 17:54:38 +000077
78Coding Style
79------------
80
81Before you submit your applet for inclusion in BusyBox, (or better yet, before
82you _write_ your applet) please read through the style guide in the docs
83directory and make your program compliant.
84
85
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +000086Some Words on libbb
87-------------------
Mark Whitley8a6b6192000-12-19 17:54:38 +000088
Mark Whitleyeac26362000-12-19 17:35:24 +000089As you are writing your applet, please be aware of the body of pre-existing
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +000090useful functions in libbb. Use these instead of reinventing the wheel.
Mark Whitley8a6b6192000-12-19 17:54:38 +000091
Mark Whitleyeac26362000-12-19 17:35:24 +000092Additionally, if you have any useful, general-purpose functions in your
Mike Frysinger9754b912005-09-02 23:06:30 +000093applet that could be useful in other applets, consider putting them in libbb.
Mark Whitleyeac26362000-12-19 17:35:24 +000094
Denis Vlasenko7e84e532007-05-08 17:52:17 +000095And it may be possible that some of the other applets uses functions you
96could use. If so, you have to rip the function out of the applet and make
97a libbb function out of it.
98
99Adding a libbb function:
100------------------------
101
102Make a new file named <function_name>.c
103
104----start example code------
105
106#include "libbb.h"
107#include "other.h"
108
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100109//kbuild:lib-y += function.o
110
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000111int function(char *a)
112{
113 return *a;
114}
115
116----end example code------
117
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100118Remember about the kbuild snippet.
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000119
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000120You should also try to find a suitable place in include/libbb.h for
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000121the function declaration. If not, add it somewhere anyway, with or without
122ifdefs to include or not.
123
Holger Blasumb27d62a2010-10-28 20:37:05 +0200124You can look at libbb/Config.src and try to find out if the function is
Denys Vlasenko5370bfb2009-09-06 02:58:59 +0200125tunable and add it there if it is.
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000126
Mark Whitleyeac26362000-12-19 17:35:24 +0000127
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100128Kbuild/Config.in/usage/applets.h snippets in .c files
129-----------------------------------------------------
130
131The old way of adding new applets was to put all the information needed by the
132configuration and build system into appropriate files (namely: Kbuild.src and
133Config.src in new applet's directory) and to add the applet declaration and
134usage info text to include/applets.src.h and include/usage.src.h respectively.
135
136Since the scripts/gen_build_files.sh script had been introduced, the preferred
137way is to have all these declarations contained within the applet .c files.
138
139Every line intended to be processed by gen_build_files.sh should start as a
140comment without any preceding whitespaces and be followed by an appropriate
141keyword - kbuild, config, usage or applet - and a colon, just like shown in the
142first example above.
143
144
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000145Placement / Directory
146---------------------
147
148Find the appropriate directory for your new applet.
149
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100150Add the kbuild snippet to the .c file:
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000151
Denys Vlasenkof3326172014-03-17 15:06:29 +0100152//kbuild:lib-$(CONFIG_MU) += mu.o
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000153
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100154Add the config snippet to the .c file:
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000155
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100156//config:config MU
157//config: bool "MU"
Denys Vlasenkof3326172014-03-17 15:06:29 +0100158//config: default y
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100159//config: help
160//config: Returns an indeterminate value.
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000161
162
Mark Whitleyeac26362000-12-19 17:35:24 +0000163Usage String(s)
164---------------
165
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100166Next, add usage information for your applet to the .c file.
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000167This should look like the following:
Mark Whitleyeac26362000-12-19 17:35:24 +0000168
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100169//usage:#define mu_trivial_usage
Denys Vlasenkof3326172014-03-17 15:06:29 +0100170//usage: "[-abcde] FILE..."
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100171//usage:#define mu_full_usage
Denys Vlasenkof3326172014-03-17 15:06:29 +0100172//usage: "Returns an indeterminate value\n"
173//usage: "\n -a First function"
174//usage: "\n -b Second function"
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100175//usage: ...
Mark Whitleyeac26362000-12-19 17:35:24 +0000176
177If your program supports flags, the flags should be mentioned on the first
Denys Vlasenkof3326172014-03-17 15:06:29 +0100178line ([-abcde]) and a detailed description of each flag should go in the
179mu_full_usage section, one flag per line.
Mark Whitleyeac26362000-12-19 17:35:24 +0000180
181
182Header Files
183------------
184
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100185Finally add the applet declaration snippet. Be sure to read the top of
186applets.src.h before adding your applet - it contains important info
187on applet macros and conventions.
Mark Whitleyeac26362000-12-19 17:35:24 +0000188
Bartosz Golaszewski9dbe4d02014-03-12 22:43:50 +0100189//applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP))
Mark Whitleyeac26362000-12-19 17:35:24 +0000190
191
Mark Whitleyeac26362000-12-19 17:35:24 +0000192The Grand Announcement
193----------------------
194
Dan Fandrich96693632010-08-10 23:45:27 -0700195Then create a diff by adding the new files to git (remember your libbb files)
196 git add <where you put it>/mu.c
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000197eventually also:
Dan Fandrich96693632010-08-10 23:45:27 -0700198 git add libbb/function.c
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000199then
Dan Fandrich96693632010-08-10 23:45:27 -0700200 git commit
201 git format-patch HEAD^
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000202and send it to the mailing list:
Mike Frysinger9754b912005-09-02 23:06:30 +0000203 busybox@busybox.net
204 http://busybox.net/mailman/listinfo/busybox
Mark Whitleyeac26362000-12-19 17:35:24 +0000205
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000206Sending patches as attachments is preferred, but not required.