close

Chapter 9: You've made a mess of it


         9: 你把程式弄的亂七八糟了


 


That's OK, we all do.  You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for you, and you've noticed that yes, it does do that, but the defaults it uses are less than desirable (in fact, they are worse than random typing - an infinite number of monkeys typing into GNU emacs would never make a good program).


沒關係我們都會遇到這種問題可能有unix老手已經告訴過你 "GNU emacs" 可以自動的幫你重排C的程式碼而你也發現它的確會做但是它的預設值有點令人不滿意 (事實上這些值還比打字亂打糟糕 - GNU emacs上任意的亂打還是造不出好程式的)


 


So, you can either get rid of GNU emacs, or change it to use saner values.  To do the latter, you can stick the following in your .emacs file:


所以你可以把GNU emacs丟掉或者設定一些較合理的預設值後者可以把下面這些碼放到你的 .emacs檔達成:


 


(defun linux-c-mode ()


  "C mode with adjusted defaults for use with the Linux kernel."


  (interactive)


  (c-mode)


  (c-set-style "K&R")


  (setq tab-width 8)


  (setq indent-tabs-mode t)


  (setq c-basic-offset 8))


 


This will define the M-x linux-c-mode command.  When hacking on a module, if you put the string -*- linux-c -*- somewhere on the first two lines, this mode will be automatically invoked. Also, you may want to add


這樣定義了 M-x linux-c-mode 指令當你 hack 某個模組時如果你在前兩行的任意地方放 -*- linux-c -*- 這個字串這個模式就 會自動的起動同時你也可能想要加這行:


 


(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)


            auto-mode-alist))


 


to your .emacs file if you want to have linux-c-mode switched on automagically when you edit source files under /usr/src/linux.


到你的 .emacs檔去如果當你編輯/usr/src/linux下的程式檔的時後想讓linux-c-mode自動起動的話


 


But even if you fail in getting emacs to do sane formatting, not everything is lost: use "indent".


不過僅管你還是不能利用emacs作合理的重排的話你還沒絕望:使用 "indent"


 


Now, again, GNU indent has the same brain-dead settings that GNU emacs has, which is why you need to give it a few command line options.


However, that's not too bad, because even the makers of GNU indent recognize the authority of K&R (the GNU people aren't evil, they are just severely misguided in this matter), so you just give indent the options "-kr -i8" (stands for "K&R, 8 character indents"), or use "scripts/Lindent", which indents in the latest style.


這裡又遇到同樣的問題GNU indentGNU emacs有同樣的白癡設定這就是為什麼你須要給它一些命令列選項的原因還好這不會太麻煩因為連GNU indent的作者也體認到K&R的權威性 (GNU的人不壞他們在這方面只是被嚴重的誤導了罷了) 所以你只要給indent這樣的選項 "-kr -i8"即可 (表示 "K&R8格的縮排")


 


"indent" has a lot of options, and especially when it comes to comment re-formatting you may want to take a look at the man page.  But remember: "indent" is not a fix for bad programming.


"indent" 有許多的選項特別是預到重排註解的時候你可能需要看一下使用手冊. 不過要記得:"indent" 不是修理不良程式碼的工具。


 


 


 




 


         Chapter 10: Kconfig configuration files


 


For all of the Kconfig* configuration files throughout the source tree, the indentation is somewhat different.  Lines under a "config" definition are indented with one tab, while help text is indented an additional two spaces.  Example:


 


config AUDIT


    bool "Auditing support"


    depends on NET


    help


      Enable auditing infrastructure that can be used with another


      kernel subsystem, such as SELinux (which requires this for


      logging of avc messages output).  Does not do system-call


      auditing without CONFIG_AUDITSYSCALL.


 


Features that might still be considered unstable should be defined as dependent on "EXPERIMENTAL":


 


config SLUB


    depends on EXPERIMENTAL && !ARCH_USES_SLAB_PAGE_STRUCT


    bool "SLUB (Unqueued Allocator)"


    ...


 


while seriously dangerous features (such as write support for certain filesystems) should advertise this prominently in their prompt string:


 


config ADFS_FS_RW


    bool "ADFS write support (DANGEROUS)"


    depends on ADFS_FS


    ...


 


For full documentation on the configuration files, see the file Documentation/kbuild/kconfig-language.txt.

arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()