new-applet-HOWTO.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 svn trunk.
  9. This is a good thing if you plan to getting it commited 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. Be sure to name the main function <applet>_main instead of main.
  15. And be sure to put it in <applet>.c. Usage does not have to be taken care of by
  16. your applet.
  17. Make sure to #include "libbb.h" as the first include file in your applet so
  18. the bb_config.h and appropriate platform specific files are included properly.
  19. For a new applet mu, here is the code that would go in mu.c:
  20. (busybox.h already includes most usual header files. You do not need
  21. #include <stdio.h> etc...)
  22. ----begin example code------
  23. /* vi: set sw=4 ts=4: */
  24. /*
  25. * Mini mu implementation for busybox
  26. *
  27. * Copyright (C) [YEAR] by [YOUR NAME] <YOUR EMAIL>
  28. *
  29. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  30. */
  31. #include "libbb.h"
  32. #include "other.h"
  33. int mu_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int mu_main(int argc, char **argv)
  35. {
  36. int fd;
  37. ssize_t n;
  38. char mu;
  39. fd = xopen("/dev/random", O_RDONLY);
  40. if ((n = safe_read(fd, &mu, 1)) < 1)
  41. bb_perror_msg_and_die("/dev/random");
  42. return mu;
  43. }
  44. ----end example code------
  45. Coding Style
  46. ------------
  47. Before you submit your applet for inclusion in BusyBox, (or better yet, before
  48. you _write_ your applet) please read through the style guide in the docs
  49. directory and make your program compliant.
  50. Some Words on libbb
  51. -------------------
  52. As you are writing your applet, please be aware of the body of pre-existing
  53. useful functions in libbb. Use these instead of reinventing the wheel.
  54. Additionally, if you have any useful, general-purpose functions in your
  55. applet that could be useful in other applets, consider putting them in libbb.
  56. And it may be possible that some of the other applets uses functions you
  57. could use. If so, you have to rip the function out of the applet and make
  58. a libbb function out of it.
  59. Adding a libbb function:
  60. ------------------------
  61. Make a new file named <function_name>.c
  62. ----start example code------
  63. #include "libbb.h"
  64. #include "other.h"
  65. int function(char *a)
  66. {
  67. return *a;
  68. }
  69. ----end example code------
  70. Add <function_name>.o in the right alphabetically sorted place
  71. in libbb/Kbuild. You should look at the conditional part of
  72. libbb/Kbuild aswell.
  73. You should also try to find a suitable place in include/libbb.h for
  74. the function declaration. If not, add it somewhere anyway, with or without
  75. ifdefs to include or not.
  76. You can look at libbb/Config.in and try to find out if the function is
  77. tuneable and add it there if it is.
  78. Placement / Directory
  79. ---------------------
  80. Find the appropriate directory for your new applet.
  81. Make sure you find the appropriate places in the files, the applets are
  82. sorted alphabetically.
  83. Add the applet to Kbuild in the chosen directory:
  84. lib-$(CONFIG_MU) += mu.o
  85. Add the applet to Config.in in the chosen directory:
  86. config MU
  87. bool "MU"
  88. default n
  89. help
  90. Returns an indeterminate value.
  91. Usage String(s)
  92. ---------------
  93. Next, add usage information for you applet to include/usage.h.
  94. This should look like the following:
  95. #define mu_trivial_usage \
  96. "-[abcde] FILES"
  97. #define mu_full_usage \
  98. "Returns an indeterminate value.\n\n" \
  99. "Options:\n" \
  100. "\t-a\t\tfirst function\n" \
  101. "\t-b\t\tsecond function\n" \
  102. ...
  103. If your program supports flags, the flags should be mentioned on the first
  104. line (-[abcde]) and a detailed description of each flag should go in the
  105. mu_full_usage section, one flag per line. (Numerous examples of this
  106. currently exist in usage.h.)
  107. Header Files
  108. ------------
  109. Next, add an entry to include/applets.h. Be *sure* to keep the list
  110. in alphabetical order, or else it will break the binary-search lookup
  111. algorithm in busybox.c and the Gods of BusyBox smite you. Yea, verily:
  112. Be sure to read the top of applets.h before adding your applet.
  113. /* all programs above here are alphabetically "less than" 'mu' */
  114. IF_MU(APPLET(mu, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
  115. /* all programs below here are alphabetically "greater than" 'mu' */
  116. The Grand Announcement
  117. ----------------------
  118. Then create a diff by adding the new files with svn (remember your libbb files)
  119. svn add <where you put it>/mu.c
  120. eventually also:
  121. svn add libbb/function.c
  122. then
  123. svn diff
  124. and send it to the mailing list:
  125. busybox@busybox.net
  126. http://busybox.net/mailman/listinfo/busybox
  127. Sending patches as attachments is preferred, but not required.