3
0

new-applet-HOWTO.txt 5.5 KB

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