blob: 2f237564d5680c846cb70ff8ff3caa5cdf864a8a [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
Denis Vlasenko7e84e532007-05-08 17:52:17 +00009Thomas Lundquist - Trying to keep it updated.
10
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
19boilerplate. Be sure to name the main function <applet>_main instead of main.
20And be sure to put it in <applet>.c. Usage does not have to be taken care of by
21your applet.
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022Make sure to #include "libbb.h" as the first include file in your applet so
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000023the bb_config.h and appropriate platform specific files are included properly.
Glenn L McGrath9c91e412003-10-01 11:33:46 +000024
25For a new applet mu, here is the code that would go in mu.c:
Mark Whitleyeac26362000-12-19 17:35:24 +000026
Denis Vlasenko7e84e532007-05-08 17:52:17 +000027(busybox.h already includes most usual header files. You do not need
28#include <stdio.h> etc...)
29
30
Mark Whitleyeac26362000-12-19 17:35:24 +000031----begin example code------
32
33/* vi: set sw=4 ts=4: */
34/*
35 * Mini mu implementation for busybox
36 *
Mark Whitleyeac26362000-12-19 17:35:24 +000037 * Copyright (C) [YEAR] by [YOUR NAME] <YOUR EMAIL>
38 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020039 * Licensed under GPLv2, see file LICENSE in this source tree.
Mark Whitleyeac26362000-12-19 17:35:24 +000040 */
41
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Denis Vlasenko7e84e532007-05-08 17:52:17 +000043#include "other.h"
Mark Whitleyeac26362000-12-19 17:35:24 +000044
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int mu_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Mark Whitleyeac26362000-12-19 17:35:24 +000046int mu_main(int argc, char **argv)
47{
48 int fd;
Denis Vlasenko360362d2007-11-04 04:46:46 +000049 ssize_t n;
Mark Whitleyeac26362000-12-19 17:35:24 +000050 char mu;
51
Bernhard Reutner-Fischer8c1eda52006-08-28 23:39:36 +000052 fd = xopen("/dev/random", O_RDONLY);
Mark Whitleyeac26362000-12-19 17:35:24 +000053
54 if ((n = safe_read(fd, &mu, 1)) < 1)
Mike Frysinger0ea3a6f2005-04-23 01:43:45 +000055 bb_perror_msg_and_die("/dev/random");
Mark Whitleyeac26362000-12-19 17:35:24 +000056
57 return mu;
58}
59
60----end example code------
61
Mark Whitley8a6b6192000-12-19 17:54:38 +000062
63Coding Style
64------------
65
66Before you submit your applet for inclusion in BusyBox, (or better yet, before
67you _write_ your applet) please read through the style guide in the docs
68directory and make your program compliant.
69
70
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +000071Some Words on libbb
72-------------------
Mark Whitley8a6b6192000-12-19 17:54:38 +000073
Mark Whitleyeac26362000-12-19 17:35:24 +000074As you are writing your applet, please be aware of the body of pre-existing
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +000075useful functions in libbb. Use these instead of reinventing the wheel.
Mark Whitley8a6b6192000-12-19 17:54:38 +000076
Mark Whitleyeac26362000-12-19 17:35:24 +000077Additionally, if you have any useful, general-purpose functions in your
Mike Frysinger9754b912005-09-02 23:06:30 +000078applet that could be useful in other applets, consider putting them in libbb.
Mark Whitleyeac26362000-12-19 17:35:24 +000079
Denis Vlasenko7e84e532007-05-08 17:52:17 +000080And it may be possible that some of the other applets uses functions you
81could use. If so, you have to rip the function out of the applet and make
82a libbb function out of it.
83
84Adding a libbb function:
85------------------------
86
87Make a new file named <function_name>.c
88
89----start example code------
90
91#include "libbb.h"
92#include "other.h"
93
94int function(char *a)
95{
96 return *a;
97}
98
99----end example code------
100
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000101Add <function_name>.o in the right alphabetically sorted place
102in libbb/Kbuild. You should look at the conditional part of
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000103libbb/Kbuild aswell.
104
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000105You should also try to find a suitable place in include/libbb.h for
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000106the function declaration. If not, add it somewhere anyway, with or without
107ifdefs to include or not.
108
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000109You can look at libbb/Config.in and try to find out if the function is
Denys Vlasenko5370bfb2009-09-06 02:58:59 +0200110tunable and add it there if it is.
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000111
Mark Whitleyeac26362000-12-19 17:35:24 +0000112
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000113Placement / Directory
114---------------------
115
116Find the appropriate directory for your new applet.
117
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000118Make sure you find the appropriate places in the files, the applets are
119sorted alphabetically.
120
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000121Add the applet to Kbuild in the chosen directory:
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000122
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000123lib-$(CONFIG_MU) += mu.o
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000124
Mike Frysinger9754b912005-09-02 23:06:30 +0000125Add the applet to Config.in in the chosen directory:
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000126
Denis Vlasenko360362d2007-11-04 04:46:46 +0000127config MU
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000128 bool "MU"
129 default n
130 help
131 Returns an indeterminate value.
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000132
133
Mark Whitleyeac26362000-12-19 17:35:24 +0000134Usage String(s)
135---------------
136
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137Next, add usage information for you applet to include/usage.h.
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000138This should look like the following:
Mark Whitleyeac26362000-12-19 17:35:24 +0000139
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000140 #define mu_trivial_usage \
141 "-[abcde] FILES"
142 #define mu_full_usage \
143 "Returns an indeterminate value.\n\n" \
144 "Options:\n" \
145 "\t-a\t\tfirst function\n" \
146 "\t-b\t\tsecond function\n" \
Mike Frysinger9754b912005-09-02 23:06:30 +0000147 ...
Mark Whitleyeac26362000-12-19 17:35:24 +0000148
149If your program supports flags, the flags should be mentioned on the first
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000150line (-[abcde]) and a detailed description of each flag should go in the
151mu_full_usage section, one flag per line. (Numerous examples of this
152currently exist in usage.h.)
Mark Whitleyeac26362000-12-19 17:35:24 +0000153
154
155Header Files
156------------
157
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158Next, add an entry to include/applets.h. Be *sure* to keep the list
159in alphabetical order, or else it will break the binary-search lookup
Matt Kraai3b1cbd72002-03-18 16:03:00 +0000160algorithm in busybox.c and the Gods of BusyBox smite you. Yea, verily:
Mark Whitleyeac26362000-12-19 17:35:24 +0000161
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000162Be sure to read the top of applets.h before adding your applet.
163
Mark Whitleyeac26362000-12-19 17:35:24 +0000164 /* all programs above here are alphabetically "less than" 'mu' */
Denys Vlasenko6d48d3e2009-07-30 12:57:19 +0200165 IF_MU(APPLET(mu, _BB_DIR_USR_BIN, _BB_SUID_DROP))
Mark Whitleyeac26362000-12-19 17:35:24 +0000166 /* all programs below here are alphabetically "greater than" 'mu' */
167
168
Mark Whitleyeac26362000-12-19 17:35:24 +0000169The Grand Announcement
170----------------------
171
Dan Fandrich96693632010-08-10 23:45:27 -0700172Then create a diff by adding the new files to git (remember your libbb files)
173 git add <where you put it>/mu.c
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000174eventually also:
Dan Fandrich96693632010-08-10 23:45:27 -0700175 git add libbb/function.c
Denis Vlasenko7e84e532007-05-08 17:52:17 +0000176then
Dan Fandrich96693632010-08-10 23:45:27 -0700177 git commit
178 git format-patch HEAD^
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000179and send it to the mailing list:
Mike Frysinger9754b912005-09-02 23:06:30 +0000180 busybox@busybox.net
181 http://busybox.net/mailman/listinfo/busybox
Mark Whitleyeac26362000-12-19 17:35:24 +0000182
Glenn L McGrath9c91e412003-10-01 11:33:46 +0000183Sending patches as attachments is preferred, but not required.