new-applet-HOWTO.txt 4.8 KB

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