HACKING 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. This document
  2. =============
  3. This document is a guide how to develop GNU Midnight Commander. It's
  4. quite incomplete, but may be worth reading anyway.
  5. The document was written by Miguel de Icaza and reworked by Pavel
  6. Roskin and later from Patrick Winnertz.
  7. Some parts were taken from the messages posted in the mailing
  8. lists.
  9. Compiling from GIT
  10. ==================
  11. The full list of requirements is listed in the INSTALL file.
  12. It is recommended that all those tools are installed with the same
  13. prefix. Make sure that the tools with the right version are first in
  14. PATH.
  15. Once you have the right tools, run `autogen.sh' - it will generate
  16. everything necessary for the build `configure'. Then run 'configure'
  17. and `make' as usually.
  18. The distribution tarball is created by the command `make distcheck'.
  19. This command can take a while.
  20. Note that the version of gettext doesn't affect the snapshot because the
  21. distributed files are installed by gettext from archives for the version
  22. used in the AM_GNU_GETTEXT_VERSION macro, which is 0.18.2.
  23. Working with GNU Midnight Commander
  24. ===================================
  25. Please use the GIT version. It may be quite different from the released
  26. versions. A lot of cleanup is going on. The GIT version may be easier
  27. to understand, in addition to the obvious fact that the merging is
  28. easier with the GIT version.
  29. In order to compile GNU Midnight Commander from a clean GIT checkout you
  30. should use 'autogen.sh && ./configure' instead of 'configure'.
  31. GNU Midnight Commander uses Autoconf and Automake, with make it fairly
  32. portable. However, GNU Make is strongly recommended for development
  33. because other versions of make may not track dependencies properly.
  34. This is very important for correct compilation, especially if you change
  35. any header files.
  36. If you add or remove any files, please change Makefile.am in the same
  37. directory accordingly. When doing significant changes in the tree
  38. structure, "make distcheck" is strongly recommended.
  39. GNU Autoconf allows you to test several different configurations are
  40. once. To do so, use the so called out-of-tree (or VPATH) compilation.
  41. Create separate empty directories and run configure with full path from
  42. those directories, like this:
  43. cd /usr/local/src
  44. mkdir mc-slang
  45. mkdir mc-ncurses
  46. cd mc-slang
  47. /usr/local/src/mc/configure && make all
  48. cd ../mc-ncurses
  49. /usr/local/src/mc/configure --with-screen=ncurses && make all
  50. Please use the same indentation as other developers. To indent a block,
  51. select in the internal editor and use Shift-F9 to call the external
  52. indent. For historic reasons, GNU Midnight Commander used formatting
  53. that is not default for GNU Indent. Please put following text to your
  54. ~/.indent.pro file to make GNU Indent follow the style used in GNU
  55. Midnight Commander:
  56. --gnu-style
  57. --format-first-column-comments
  58. --indent-level4
  59. --brace-indent0
  60. --line-length100
  61. --no-tabs
  62. --blank-lines-after-procedures
  63. or in short notation:
  64. indent -gnu -fc1 -i4 -bli0 -nut -bap -l100
  65. It's OK to indent the whole function if you edit it. However, please
  66. refrain from it if you are posting your patch for review. In this case
  67. you would save time of other developers if you only include significant
  68. changes. The developer applying your patch can format the code for you.
  69. Please keep in mind that the VFS subsystem is licensed under LGPL, while
  70. the rest of the code uses GPL.
  71. Code structure - outline
  72. ========================
  73. The code is located in following directories.
  74. vfs - Virtual File System.
  75. This library provides filesystem-like access to various data, such are
  76. archives and remote filesystems. To use VFS, you should use wrappers
  77. around POSIX calls. The wrappers have names composed from "mc_" and the
  78. standard name of the function. For example, to open a file on VFS, use
  79. mc_open() instead.
  80. edit - the internal editor.
  81. This code has been contributed by Paul Sheer, the author of Cooledit.
  82. The internal editor shares some code with Cooledit, but now it's
  83. developed as part of GNU Midnight Commander.
  84. src - the main part of the code.
  85. This code includes the dialog manager written by Radek Doulik and source
  86. code of the main application.
  87. Code structure - details
  88. ========================
  89. GNU Midnight Commander uses extensively the dialog manager written by
  90. Radek Doulik. To understand how the dialog manager works, please read
  91. the dialog.c. You will find the basic widgets in the files widget.c.
  92. Some more high-level functions, e.g. to display a message box, are
  93. located in wtools.c. This file also contains the Quick Dialog code,
  94. which makes it easier to create complex dialogs.
  95. The files util.c and utilunix.c have a lot of utility functions. Get
  96. familiar with them, they are very simple.
  97. glib is used for memory allocation and for some utility functions, such
  98. as manipulation with lists and trees. gmodule (part of the glib
  99. distribution) is used to load some libraries dynamically at the run
  100. time.
  101. Thanks to glib, the code has almost no hardcoded limits, since there are
  102. many ways to avoid them. For example, when you want to concatenate
  103. strings, use the g_strconcat() function:
  104. new_text = g_strconcat (username, " ", password, (char *)0);
  105. This allocates new memory for the string, so you should use g_free() on
  106. the result.
  107. The parent of all dialogs is called midnight_dlg. Both panels are
  108. widgets in that dialog. Other widgets include the menu, the command
  109. line and the button bar.
  110. Input handling
  111. ==============
  112. The routines for input handling on the Midnight Commander are:
  113. getch, get_key_code, mi_getch and get_event.
  114. getch is an interface to the low level system input mechanism. It
  115. does not deal with the mouse.
  116. In the case of ncurses, this is a function implemented in the
  117. ncurses library that translates key sequences to key codes (\E[A to
  118. something like KEY_UP and so on).
  119. In the case of S-Lang there is no such conversion, that's why we
  120. load a set of extra definitions.
  121. The get_key_code routine converts the data from getch to the
  122. constants the Midnight Commander uses.
  123. In the case of S-Lang, it will actually do all the jobs that getch
  124. does for curses. In the case of curses it patches a couple of
  125. sequences that are not available on some terminal databases. This
  126. routine is the one you want to use if you want a character without
  127. the mouse support.
  128. get_event is the routine you want to use if you want to handle mouse
  129. events, it will return 0 on a mouse event, -1 if no input is available
  130. or a key code if there is some input available. This routine in turn
  131. uses get_key_code to decode the input stream and convert it to useful
  132. constants.
  133. mi_getch is just a wrapper around get_event that ignores all the mouse
  134. events. It's used only in a couple of places, this routine may return
  135. -1 if no input is available (if you have set the nodelay option of
  136. ncurses or S-Lang with nodelay) or a character code if no such option is
  137. available.
  138. Mouse support
  139. =============
  140. The mouse support in the Midnight Commander is based on the get_event
  141. routine. The core of the mouse event dispatching is in the
  142. dlg.c:run_dlg routine.
  143. ncurses
  144. =======
  145. Although S-Lang is now used by default, we still support ncurses. We
  146. basically are using a small subset of ncurses because we want to be
  147. compatible with Slang.
  148. The Dialog manager and the Widgets
  149. ==================================
  150. The Dialog manager and the Widget structure are implemented in
  151. src/dialog.c. Everything shown on screen is a dialog. Dialogs contain
  152. widgets, but not everything on screen is a widget. Dialogs can draw
  153. themselves.
  154. Dialogs are connected into a singly linked list using "parent" field.
  155. Currently active dialog is saved in current_dlg variable. The toplevel
  156. dialog has parent NULL. Usually it's midnight_dlg.
  157. parent parent
  158. current_dlg ------->another dialog-- ... -->midnight_dlg
  159. When the screen needs to be refreshed, every dialog asks its parent to
  160. refresh first, and then refreshes itself.
  161. A dialog is created by create_dlg(). Then it's populated by widgets
  162. using add_widget(). Then the dialog is run by calling run_dlg(), which
  163. returns the id of the button selected by the user. Finally, the dialog
  164. is destroyed by calling destroy_dlg().
  165. Widgets are placed to a doubly linked circular list. Each widget has
  166. previous and next widget.
  167. prev next prev next
  168. widget1 <---------> widget2 <---------> widget3
  169. ^ ^
  170. -----------------------------------------
  171. next prev
  172. Pressing Tab moves focus to the "next" widget, pressing Shift-Tab moves
  173. focus to "prev". The tab order is equal to the add order except some
  174. old code that use the reverse order by setting DLG_REVERSE flag in
  175. create_dlg() call. Please don't use reverse order in the new code.
  176. The initial widget to get focus can be selected by calling
  177. dlg_select_widget().
  178. When creating a dialog, you may want to use a callback that would
  179. intercept some dialog events. However, many widgets will do the right
  180. thing by default, so some dialogs can work just fine without callbacks.
  181. There are also widget events, which are sent by the dialog to individual
  182. widgets. Some widgets also have user callbacks.
  183. To create your own widget, use init_widget(). In this case, you must
  184. provide a callback function. Please note that it's not the same as the
  185. user callback in some widgets.
  186. Where to Find Bug Reports and Patches
  187. =====================================
  188. The official place for bug reports is:
  189. https://www.midnight-commander.org
  190. There are various unofficial sources where bug reports and patches can
  191. be found (NOT maintained by the MC team).
  192. https://tracker.debian.org/pkg/mc
  193. https://bugs.debian.org/cgi-bin/pkgreport.cgi?pkg=mc
  194. The bug tracking system for Debian, a package collection mainly
  195. for GNU/Linux and the Hurd.
  196. https://bugzilla.redhat.com/bugzilla/buglist.cgi?component=mc
  197. https://src.fedoraproject.org/rpms/mc
  198. Bugs reported in Redhat Linux.
  199. https://gitweb.gentoo.org/repo/gentoo.git/tree/app-misc/mc/files
  200. The patches that are applied for the Gentoo Linux version of MC.
  201. https://cgit.freebsd.org/ports/tree/misc/mc/files
  202. The patches that are applied for the FreeBSD version of MC.
  203. https://cvsweb.openbsd.org/ports/misc/mc/patches/
  204. The patches that are applied for the OpenBSD version of MC.
  205. http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/sysutils/mc/patches/
  206. The patches that are applied for the NetBSD version of MC.
  207. Programming Tips
  208. ================
  209. (This list should be sorted alphabetically.)
  210. ?: This operator has a precedence that is easy to use the wrong way. You
  211. might think that
  212. int right = 25 + have_frame() ? 1 : 0; /* WRONG */
  213. results in either 25 or 26. This is not the case. The C compiler
  214. sees this as:
  215. int right = (25 + have_frame()) ? 1 : 0; /* WRONG */
  216. To avoid this, put the ?: in parentheses, like this
  217. int right = 25 + (have_frame() ? 1 : 0); /* RIGHT */
  218. If the condition is more complicated, put it in additional
  219. parentheses:
  220. int right = 25 + ((have_frame()) ? 1 : 0); /* RIGHT */
  221. const: For every function taking a string argument, decide whether you
  222. (as a user of the function) would expect that the string is modi-
  223. fied by the function. If not, declare the string argument as
  224. "const char *". If your implementation needs to modify the string,
  225. use g_strdup to create a local copy.
  226. const_cast: Has been replaced by str_unconst.
  227. g_free: g_free handles NULL argument too, no need for the comparison.
  228. Bad way:
  229. if (old_dir) g_free (old_dir);
  230. Right way:
  231. g_free (old_dir);
  232. g_strdup: When you use g_strdup to create a local copy of a string, use
  233. the following pattern to keep the reference.
  234. char * const pathref = g_strdup(argument);
  235. /* ... */
  236. g_free (pathref);
  237. The "const" will make the pointer unmodifiable (pathref++
  238. is not possible), but you can still modify the string contents.
  239. NULL: When you pass NULL as an argument of a varargs function, cast the
  240. 0 to the appropriate data type. If a system #defines NULL to
  241. be 0 (at least NetBSD and OpenBSD do), and the sizes of int and
  242. a pointer are different, the argument will be passed as int 0,
  243. not as a pointer.
  244. This tip applies at least to catstrs (edit/edit.h), execl(3),
  245. execle(3), execlp(3), g_strconcat (glib), parent_call
  246. (src/background.h), parent_call_string (src/background.h).
  247. example:
  248. char *path = g_strconcat("dir", "/", "file", (char *)0);
  249. size_t: This data type is suitable for expressing sizes of memory or the
  250. length of strings. This type is unsigned, so you need not check
  251. if the value is >= 0.
  252. strncpy: Don't use this function in newly created code. It is slow, insecure
  253. and hard to use. A much better alternative is g_strlcpy (see there).
  254. str_unconst: We use many libraries that do not know about "const char *"
  255. and thus declare their functions to require "char *". If you
  256. know for sure that an external function does not modify the
  257. string, you can "unconst" a string using the function
  258. str_unconst(). If you are not sure whether the function modifies
  259. the string, you should use g_strdup() to pass a copy of a string
  260. to the function. Don't forget to call g_free() after work is done.
  261. unused: Unused arguments of a function can be marked like this:
  262. void do_nothing(int data)
  263. {
  264. (void) &data;
  265. }
  266. This tells the GNU C Compiler not to emit a warning, and has no
  267. side effects for other compilers.