亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2718 | 回復(fù): 4
打印 上一主題 下一主題

vncserver注釋,不當(dāng)之處請指正(1) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-03-05 13:24 |只看該作者 |倒序瀏覽
本帖最后由 xiaozi0lei 于 2013-03-05 13:32 編輯

轉(zhuǎn)載請注明出處,謝謝!
http://blog.sina.com.cn/s/blog_5e2308c80101hssf.html


  1. #!/usr/bin//perl
  2. #
  3. #  Copyright (C) 2002-2003 RealVNC Ltd.
  4. #  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
  5. #
  6. #  This is free software; you can redistribute it and/or modify
  7. #  it under the terms of the GNU General Public License as published by
  8. #  the Free Software Foundation; either version 2 of the License, or
  9. #  (at your option) any later version.
  10. #
  11. #  This software is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #
  16. #  You should have received a copy of the GNU General Public License
  17. #  along with this software; if not, write to the Free Software
  18. #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  19. #  USA.
  20. #
  21. #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
  22. #  This document is translated and commented by 弦上風(fēng).
  23. #  本文檔由弦上風(fēng)注釋翻譯。
  24. #  If you have any questions and good comments, please feel free to tell me.
  25. #  如果你有任何問題或者好的建議,請積極聯(lián)系我。
  26. #  @弦上風(fēng)
  27. #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #

  28. #此腳本用來啟動一個X VNC 服務(wù)器
  29. # vncserver - wrapper script to start an X VNC server.
  30. #

  31. #首先,保證我們在一個穩(wěn)定可靠的環(huán)境進(jìn)行操作
  32. # First make sure we're operating in a sane environment.
  33. #
  34. #調(diào)用腳本下方的SanityCheck子過程
  35. &SanityCheck();

  36. #全局變量,可以針對自己的site進(jìn)行自定義配置
  37. # Global variables.  You may want to configure some of these for your site.
  38. #
  39. #初始化顯示的分辨率,默認(rèn)分辨率
  40. $geometry = "1024x768";
  41. #顏色深度,默認(rèn)色深
  42. $depth = 16;

  43. #定義vncJavaFiles的目錄
  44. $vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
  45.                  ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
  46. #定義用戶vnc目錄
  47. $vncUserDir = "$ENV{HOME}/.vnc";
  48. #定義X驗證文件目錄
  49. $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";

  50. #定義默認(rèn)的Xstartup啟動腳本
  51. $defaultXStartup
  52.     = ("#!/bin/sh\n\n".
  53.        "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
  54.        "xsetroot -solid grey\n".
  55.        "vncconfig -iconic &\n".
  56.        "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
  57.        "twm &\n");
  58.           
  59. #獲取hostname
  60. chop($host = `uname -n`);

  61. #檢查命令行選項
  62. # Check command line options
  63. #分析命令行中的參數(shù)
  64. &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
  65.               "-help",0,"-h",0,"--help",0);

  66. #如果命令行中包括"-help","-h","--help",輸出使用幫助
  67. &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});

  68. #如果命令行中帶有"-kill"參數(shù),調(diào)用Kill子過程
  69. &Kill() if ($opt{'-kill'});

  70. #如果你想使用默認(rèn)的分辨率,顏色深度和像素格式來匹配當(dāng)前的X顯示,請去掉下面這行注釋
  71. # Uncomment this line if you want default geometry, depth and pixelformat
  72. # to match the current X display:

  73. # &GetXDisplayDefaults();

  74. #讀取命令行中的分辨率設(shè)置,賦給分辨率全局變量
  75. if ($opt{'-geometry'}) {
  76.     $geometry = $opt{'-geometry'};
  77. }
  78. #讀取命令行中的顏色深度設(shè)置,賦給顏色深度全局變量
  79. if ($opt{'-depth'}) {
  80.     $depth = $opt{'-depth'};
  81.     $pixelformat = "";
  82. }
  83. #讀取命令行中的像素格式設(shè)置,賦給像素格式全局變量
  84. if ($opt{'-pixelformat'}) {
  85.     $pixelformat = $opt{'-pixelformat'};
  86. }
  87. #調(diào)用檢查分辨率和色深的子過程
  88. &CheckGeometryAndDepth();

  89. #如果沒有則創(chuàng)建用戶的vnc目錄
  90. # Create the user's vnc directory if necessary.

  91. #如果不存在用戶的vnc目錄
  92. if (!(-e $vncUserDir)) {
  93. #如果無法創(chuàng)建用戶的vnc目錄,退出并輸出錯誤提示信息
  94.     if (!mkdir($vncUserDir,0755)) {
  95.         die "$prog: Could not create $vncUserDir.\n";
  96.     }
  97. }
  98.    
  99. #保證用戶有密碼
  100. # Make sure the user has a password.

  101. #獲取passwd文件的權(quán)限屬性
  102. ($z,$z,$mode) = stat("$vncUserDir/passwd");
  103. #如果不存在passwd文件或者非擁有者對該文件具有可讀寫權(quán)限,提示用戶需要設(shè)置密碼
  104. if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
  105.     warn "\nYou will require a password to access your desktops.\n\n";
  106. #從外部系統(tǒng)執(zhí)行vncpasswd -q $vncUserDir/passwd命令
  107.     system("vncpasswd -q $vncUserDir/passwd");
  108. #判斷外部系統(tǒng)退出狀態(tài)是否為O,不為0則提示錯誤退出,0是正常退出狀態(tài)
  109.     if (($? >> 8) != 0) {
  110.         exit 1;
  111.     }
  112. }

  113. #拿到display號
  114. # Find display number.

  115. #如果剩余的命令參數(shù)的數(shù)量大于0且第一個參數(shù)是以:開頭,后面是整數(shù)
  116. if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
  117. #將$1中的值賦給變量$displayNumber
  118.     $displayNumber = $1;
  119. #從左邊彈出一個參數(shù)
  120.     shift(@ARGV);
  121. #檢查輸入的display號是否被占用,若被占用,則退出并輸出錯誤提示信息
  122.     if (!&CheckDisplayNumber($displayNumber)) {
  123.         die "A VNC server is already running as :$displayNumber\n";
  124.     }
  125. #如果剩余的命令參數(shù)的數(shù)量大于0且第一個參數(shù)不是-開頭,則輸出使用說明
  126. } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) {
  127.     &Usage();
  128. #如果以上都不符號,則自動分配display號
  129. } else {
  130.     $displayNumber = &GetDisplayNumber();
  131. }
  132. #將5900加上有效的displaynumber賦給變量vncPort
  133. $vncPort = 5900 + $displayNumber;

  134. #定義用戶vnc的log路徑
  135. $desktopLog = "$vncUserDir/$host:$displayNumber.log";
  136. #刪除之前的vnc  log
  137. unlink($desktopLog);

  138. #制作一個X server的cookie,作為種子,當(dāng)前時間,我們的PID,passwd加密格式的一部分
  139. # Make an X server cookie - use as the seed the sum of the current time, our
  140. # PID and part of the encrypted form of the password.  Ideally we'd use
  141. # /dev/urandom, but that's only available on Linux.

  142. #用srand函數(shù)生成隨機數(shù)種子提供給rand函數(shù)使用
  143. srand(time+$+unpack("L",`cat $vncUserDir/passwd`));
  144. $cookie = "";
  145. for (1..16) {
  146. #生成32位的隨機數(shù)作為cookie
  147.     $cookie .= sprintf("%02x", int(rand(256)) % 256);
  148. }
  149.    
  150. #調(diào)用外部命令為當(dāng)前的displayNumber在xauth驗證文件添加cookie
  151. system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
  152. system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");

  153. #如果數(shù)組%opt中的-name參數(shù)有值,則將值賦給變量$desktopName
  154. if ($opt{'-name'}) {
  155.     $desktopName = $opt{'-name'};
  156. #如果沒有,則取默認(rèn)的組合值
  157. } else {
  158.     $desktopName = "$host:$displayNumber ($ENV{USER})";
  159. }

  160. #開始啟動X VNC服務(wù)器
  161. # Now start the X VNC Server

  162. #將之前得到的所有變量連接起來,形成一條完整的命令
  163. $cmd = "Xvnc :$displayNumber";
  164. $cmd .= " -desktop " . "edString($desktopName);
  165. $cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
  166. $cmd .= " -auth $xauthorityFile";
  167. $cmd .= " -geometry $geometry" if ($geometry);
  168. $cmd .= " -depth $depth" if ($depth);
  169. $cmd .= " -pixelformat $pixelformat" if ($pixelformat);
  170. $cmd .= " -rfbwait 30000";
  171. $cmd .= " -rfbauth $vncUserDir/passwd";
  172. $cmd .= " -rfbport $vncPort";
  173. $cmd .= " -pn";

  174. #添加字庫和顏色相關(guān)
  175. # Add font path and color database stuff here, e.g.:
  176. #
  177. # $cmd .= " -fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/";
  178. # $cmd .= " -co /usr/lib/X11/rgb";
  179. #

  180. #循環(huán)取出剩余的命令行參數(shù)加到命令的末尾
  181. foreach $arg (@ARGV) {
  182.     $cmd .= " " . "edString($arg);
  183. }
  184. #將命令執(zhí)行結(jié)果輸出到vnc目錄下的log中,包括標(biāo)準(zhǔn)錯誤輸出
  185. $cmd .= " >> " . "edString($desktopLog) . " 2>&1";

  186. #運行cmd命令并記錄進(jìn)程ID
  187. # Run $cmd and record the process ID.

  188. #定義進(jìn)程ID文件路徑
  189. $pidFile = "$vncUserDir/$host:$displayNumber.pid";
  190. #執(zhí)行cmd中的命令并將進(jìn)程ID寫入文件$pidFile
  191. system("$cmd & echo \$! >$pidFile");

  192. #啟動Xvnc
  193. # Give Xvnc a chance to start up

  194. #等待3秒
  195. sleep(3);
  196. #提示vncserver啟動成功
  197. warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";

  198. #創(chuàng)建用戶的啟動腳本
  199. # Create the user's xstartup script if necessary.

  200. #如果用戶啟動腳本不存在
  201. if (!(-e "$vncUserDir/xstartup")) {
  202. #提示創(chuàng)建默認(rèn)的啟動腳本,存放在vnc用戶目錄下
  203.     warn "Creating default startup script $vncUserDir/xstartup\n";
  204. #以寫權(quán)限打開用戶啟動腳本
  205.     open(XSTARTUP, ">$vncUserDir/xstartup");
  206. #將默認(rèn)啟動參數(shù)寫入啟動腳本中
  207.     print XSTARTUP $defaultXStartup;
  208. #關(guān)閉腳本文件
  209.     close(XSTARTUP);
  210. #給腳本文件增加755的權(quán)限
  211.     chmod 0755, "$vncUserDir/xstartup";
  212. }

  213. #運行用戶腳本文件
  214. # Run the X startup script.

  215. #提示按照用戶目錄下的腳本啟動程序
  216. warn "Starting applications specified in $vncUserDir/xstartup\n";
  217. #提示log文件存放在用戶目錄下
  218. warn "Log file is $desktopLog\n\n";

  219. #如果存在unix domain socket,則設(shè)置環(huán)境變量$DISPLAY=:n,否則使用TCP socket,設(shè)置環(huán)境變量$DISPLAY=host:n
  220. # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
  221. # TCP (DISPLAY=host:n)

  222. #如果tmp目錄下存在對應(yīng)的socket文件或者usr的sockets目錄下存在對應(yīng)的文件
  223. if (-e "/tmp/.X11-unix/X$displayNumber" ||
  224.     -e "/usr/spool/sockets/X11/$displayNumber")
  225. {
  226. #設(shè)置環(huán)境變量DISPLAY=:n
  227.     $ENV{DISPLAY}= ":$displayNumber";
  228. } else {
  229. #否則設(shè)置環(huán)境變量DISPLAY=host:n
  230.     $ENV{DISPLAY}= "$host:$displayNumber";
  231. }
  232. #設(shè)置環(huán)境變量$VNCDESKTOP
  233. $ENV{VNCDESKTOP}= $desktopName;

  234. #調(diào)用外部命令執(zhí)行xstartup腳本并將標(biāo)準(zhǔn)輸出和錯誤追加到日志里
  235. system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &");

  236. exit;


  237. ###############################################################################
  238. #
  239. # CheckGeometryAndDepth simply makes sure that the geometry and depth values
  240. # are sensible.
  241. #簡單的檢查分辨率和顏色深度

  242. sub CheckGeometryAndDepth
  243. {
  244. #如果分辨率是按照整數(shù)x整數(shù)格式
  245.     if ($geometry =~ /^(\d+)x(\d+)$/) {
  246. #取第一個整數(shù)作為寬,第二個整數(shù)作為高
  247.                 $width = $1; $height = $2;
  248. #如果寬和高其中一個小于1,退出并提示錯誤信息
  249.                 if (($width<1) || ($height<1)) {
  250.                         die "$prog: geometry $geometry is invalid\n";
  251.                 }
  252. #保證分辨率的寬是4的倍數(shù)
  253.                 while (($width % 4)!=0) {
  254.                         $width = $width + 1;
  255.                 }

  256. #保證分辨率的高是2的倍數(shù)
  257.                 while (($height % 2)!=0) {
  258.                         $height = $height + 1;
  259.                 }
  260. #算出分辨率并賦給變量$geometry
  261.                 $geometry = "${width}x$height";
  262.     } else {
  263. #如果分辨率格式不對,退出并提示錯誤
  264.                 die "$prog: geometry $geometry is invalid\n";
  265.     }
  266. #如果顏色深度不是8到32,退出并提示錯誤
  267.     if (($depth < 8) || ($depth > 32)) {
  268.                 die "Depth must be between 8 and 32\n";
  269.     }
  270. }


  271. #從最低有效的display號匹配
  272. # GetDisplayNumber gets the lowest available display number.  A display number
  273. # n is taken if something is listening on the VNC server port (5900+n) or the
  274. # X server port (6000+n).
  275. #

  276. sub GetDisplayNumber
  277. {
  278. #檢查1到99的display號是否被占用,沒被占用就返回該號碼,全被占用則退出輸出錯誤提示信息
  279.     foreach $n (1..99) {
  280.         if (&CheckDisplayNumber($n)) {
  281.             return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
  282.         }
  283.     }
  284.    
  285.     die "$prog: no free display number on $host.\n";
  286. }


  287. #檢查DisplayNumber,判斷是否命令行給定的DisplayNumber是可用的。判斷給定端口是否被其他程序占用
  288. # CheckDisplayNumber checks if the given display number is available.  A
  289. # display number n is taken if something is listening on the VNC server port
  290. # (5900+n) or the X server port (6000+n).
  291. #

  292. sub CheckDisplayNumber
  293. {
  294.     local ($n) = @_;
  295.        
  296. #Perl的函數(shù)Socket的格式為:Socket(FILE,Domain,Type,Protocol);
  297. #其中:
  298. #FILE 綁定一個套接字句柄(類似于打開文件的句柄);
  299. #Domain 表示套接字的協(xié)議域類型。它可以定義為Internet域,其值為2,也可以定義為UNIX域,如果把這個變量賦值為1表示域的類型為UNIX域。如果這個變量付值為2,則表示定義類型為Internet。我們常用的是的套接字協(xié)議族為Internet協(xié)議族。
  300. #Type 表示套接字類型,定義為SOCK_STREAM類型,表示的是基于流傳輸模式的TCP套接字。定義為SOCK_DGRAM類型,表示為基于簡單數(shù)據(jù)傳遞的UDP套接字。
  301. #Protocol 表示套接字的協(xié)議號,這個協(xié)議號可以通過Perl內(nèi)置函數(shù)getprotobyname()得到

  302. #打開一個internet域的TCP套接字,如果失敗,退出并提示錯誤信息
  303.     socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  304. #設(shè)置socket選項,并捕獲錯誤信息
  305.     eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  306. #如果綁定失敗,關(guān)閉套接字,返回0
  307.     if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
  308.                 close(S);
  309.                 return 0;
  310.     }
  311.     close(S);

  312. #打開一個internet域的TCP套接字,如果失敗,退出并提示錯誤信息
  313.     socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  314. #設(shè)置socket選項,并捕獲錯誤信息
  315.     eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  316. #如果綁定失敗,關(guān)閉套接字,返回0
  317.     if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
  318.                 close(S);
  319.                 return 0;
  320.     }
  321.     close(S);

  322. #如果tmp目錄下存在-lock,提示警告信息
  323.     if (-e "/tmp/.X$n-lock") {
  324.                 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
  325.                 warn "Remove this file if there is no X server $host:$n\n";
  326.                 return 0;
  327.     }

  328. #如果tmp目錄下存在socket文件,提示警告信息
  329.     if (-e "/tmp/.X11-unix/X$n") {
  330.                 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
  331.                 warn "Remove this file if there is no X server $host:$n\n";
  332.                 return 0;
  333.     }

  334. #如果/usr/spool/sockets目錄下存在socket文件,提示警告信息
  335.     if (-e "/usr/spool/sockets/X11/$n") {
  336.                 warn("\nWarning: $host:$n is taken because of ".
  337.              "/usr/spool/sockets/X11/$n\n");
  338.                 warn "Remove this file if there is no X server $host:$n\n";
  339.                 return 0;
  340.     }

  341.     return 1;
  342. }
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2013-03-05 13:29 |只看該作者

  1. #
  2. # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
  3. # format of the current X display being used.  If successful, it sets the
  4. # options as appropriate so that the X VNC server will use the same settings
  5. # (minus an allowance for window manager decorations on the geometry).  Using
  6. # the same depth and pixel format means that the VNC server won't have to
  7. # translate pixels when the desktop is being viewed on this X display (for
  8. # TrueColor displays anyway).
  9. #

  10. sub GetXDisplayDefaults
  11. {
  12. #定義本地標(biāo)量和列表變量
  13.     local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
  14.            $red, $green, $blue);

  15. #wmDecorationWidth (class WmDecorationWidth)
  16. #For calculating maximum VNC window size.  Default  4.
  17. #wmDecorationHeight (class WmDecorationHeight)
  18. #For calculating maximum VNC window size.  Default  24.
  19. #不明白這兩個變量是用來干嘛的,個人猜測是窗口的邊框。decoration是裝飾的意思,窗口渲染時,大小必須去掉裝飾的長寬
  20.     $wmDecorationWidth = 4;        # a guess at typical size for window manager
  21.     $wmDecorationHeight = 24;        # decoration size

  22. #如果沒有定義環(huán)境變量DISPLAY,就直接返回
  23.     return if (!defined($ENV{DISPLAY}));
  24. #利用unix下的xdpyinfo工具讀取相關(guān)的顯示信息,賦給數(shù)組@lines
  25.     @lines = `xdpyinfo 2>/dev/null`;

  26. #如果上一個外部命令執(zhí)行錯誤,則直接返回
  27.     return if ($? != 0);

  28. #找到分辨率,將分辨率賦值給數(shù)組
  29.     @matchlines = grep(/dimensions/, @lines);
  30. #如果數(shù)組里有值
  31.     if (@matchlines) {
  32. #將數(shù)組中字符串通過正則表達(dá)式將寬和高取出賦給列表中的兩個變量
  33.                 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);

  34. #去掉裝飾的長寬,個人理解是去掉邊框
  35.                 $width -= $wmDecorationWidth;
  36.                 $height -= $wmDecorationHeight;

  37. #將得到的寬高賦給分辨率變量
  38.                 $geometry = "${width}x$height";
  39.     }

  40. #找到默認(rèn)圖像ID
  41.     @matchlines = grep(/default visual id/, @lines);
  42.     if (@matchlines) {
  43. #取出默認(rèn)圖像ID
  44.         ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);

  45. #遍歷數(shù)組@lines中所有的元素,查找默認(rèn)圖像ID對應(yīng)的TrueColor,depth以及RGB
  46.         for ($i = 0; $i < @lines; $i++) {
  47.             if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
  48. #如果找不到TrueColor,depth,RGB任何一個,直接返回
  49.                         if (($lines[$i+1] !~ /TrueColor/) ||
  50.                                 ($lines[$i+2] !~ /depth/) ||
  51.                                 ($lines[$i+4] !~ /red, green, blue masks/))
  52.                         {
  53.                                 return;
  54.                         }
  55.                         last;
  56.             }
  57.         }
  58. #如果遍歷的所有的元素,找不到匹配的元素,直接返回
  59.         return if ($i >= @lines);

  60. #將找到的默認(rèn)色深賦給變量$depth
  61.         ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
  62. #將找到的默認(rèn)RGB色彩模式值賦給變量
  63.         ($red,$green,$blue)
  64.             = ($lines[$i+4]
  65.                =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);

  66. #將十六進(jìn)制轉(zhuǎn)換成十進(jìn)制
  67.         $red = hex($red);
  68.         $green = hex($green);
  69.         $blue = hex($blue);

  70. #計算紅綠藍(lán)然后得出像素格式,不懂圖形學(xué),不知道這些公式干嘛的...囧
  71.         if ($red > $blue) {
  72.             $red = int(log($red) / log(2)) - int(log($green) / log(2));
  73.             $green = int(log($green) / log(2)) - int(log($blue) / log(2));
  74.             $blue = int(log($blue) / log(2)) + 1;
  75.             $pixelformat = "rgb$red$green$blue";
  76.         } else {
  77.             $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
  78.             $green = int(log($green) / log(2)) - int(log($red) / log(2));
  79.             $red = int(log($red) / log(2)) + 1;
  80.             $pixelformat = "bgr$blue$green$red";
  81.         }
  82.     }
  83. }


  84. #沒明白這個子過程有什么用...把'替換成'"'"',然后再加上'',不明白意圖~囧
  85. # quotedString returns a string which yields the original string when parsed
  86. # by a shell.
  87. #

  88. sub quotedString
  89. {
  90.     local ($in) = @_;

  91.     $in =~ s/\'/\'\"\'\"\'/g;

  92.     return "'$in'";
  93. }


  94. #移除斜線號,用下劃線取代
  95. # removeSlashes turns slashes into underscores for use as a file name.
  96. #

  97. sub removeSlashes
  98. {
  99.     local ($in) = @_;

  100. #將斜線替換成下劃線
  101.     $in =~ s|/|_|g;

  102.     return "$in";
  103. }


  104. #
  105. # Usage
  106. #

  107. #輸入格式錯誤時,提示使用方法
  108. sub Usage
  109. {
  110.     die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
  111.         "                 [-geometry <width>x<height>]\n".
  112.         "                 [-pixelformat rgbNNN|bgrNNN]\n".
  113.         "                 <Xvnc-options>...\n\n".
  114.         "       $prog -kill <X-display>\n\n");
  115. }


  116. #
  117. # Kill
  118. #

  119. sub Kill
  120. {
  121. #轉(zhuǎn)換格式,看句末注釋
  122.     $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1

  123. #如果-kill參數(shù)值合法
  124.     if ($opt{'-kill'} =~ /^:\d+$/) {
  125. #將對應(yīng)的pid文件路徑賦給變量$pidFile
  126.                 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
  127.     } else {
  128. #如果-kill參數(shù)值不以$host:開頭,退出并提示錯誤信息
  129.                 if ($opt{'-kill'} !~ /^$host:/) {
  130.                         die "\nCan't tell if $opt{'-kill'} is on $host\n".
  131.                         "Use -kill :<number> instead\n\n";
  132.                 }
  133. #將對應(yīng)的pid文件路徑賦給變量
  134.                 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
  135.     }
  136. #如果pid文件不可讀,退出并提示錯誤信息
  137.     if (! -r $pidFile) {
  138.         die "\nCan't find file $pidFile\n".
  139.             "You'll have to kill the Xvnc process manually\n\n";
  140.     }
  141. #
  142.     $SIG{'HUP'} = 'IGNORE';
  143. #讀出pid并去除末尾的回車符
  144.     chop($pid = `cat $pidFile`);
  145. #提示殺死Xvnc進(jìn)行
  146.     warn "Killing Xvnc process ID $pid\n";
  147. #調(diào)用外部系統(tǒng)殺死進(jìn)程
  148.     system("kill $pid");
  149. #刪除進(jìn)程文件
  150.     unlink $pidFile;
  151. #退出
  152.     exit;
  153. }


  154. #
  155. # ParseOptions takes a list of possible options and a boolean indicating
  156. # whether the option has a value following, and sets up an associative array
  157. # %opt of the values of the options given on the command line. It removes all
  158. # the arguments it uses from @ARGV and returns them in @optArgs.
  159. #
  160. #分析選項
  161. sub ParseOptions
  162. {
  163. #將模板里的參數(shù)讀到數(shù)組@optval
  164.     local (@optval) = @_;
  165.        
  166. #定義局部變量$opt,數(shù)組@opts,@newargs,哈希表%valFollows
  167.     local ($opt, @opts, %valFollows, @newargs);

  168. #利用while循環(huán)取出數(shù)組中下標(biāo)為單數(shù)的標(biāo)量值作為哈希表的鍵,下標(biāo)為雙數(shù)的標(biāo)量值作為哈希表的值
  169.     while (@optval) {
  170. #從左側(cè)彈出下標(biāo)為單數(shù)的標(biāo)量值賦給變量$opt
  171.         $opt = shift(@optval);
  172. #將彈出的標(biāo)量值從右側(cè)彈入數(shù)組@opts
  173.         push(@opts,$opt);
  174. #從左側(cè)彈出下標(biāo)為雙數(shù)的標(biāo)量值賦給對應(yīng)的哈希表的鍵
  175.         $valFollows{$opt} = shift(@optval);
  176.     }
  177. #定義空的數(shù)組和哈希表
  178.     @optArgs = ();
  179.     %opt = ();

  180. #利用while循環(huán)讀取命令行參數(shù)
  181.     arg: while (defined($arg = shift(@ARGV))) {
  182. #利用foreach循環(huán)讀取數(shù)組@opts中的值與命令行中對應(yīng)的值
  183.                 foreach $opt (@opts) {
  184.                         if ($arg eq $opt) {
  185. #將命令行中的參數(shù)從右側(cè)彈入數(shù)組@optArgs
  186.                                 push(@optArgs, $arg);
  187. #如果哈希表%valFollows中鍵的預(yù)設(shè)值不為0
  188.                                 if ($valFollows{$opt}) {
  189. #如果命令行中只輸入了一個參數(shù)且不帶參數(shù)值(如果兩個參數(shù)連在一起,中間沒有參數(shù)值怎么辦?)
  190.                                         if (@ARGV == 0) {
  191. #提示使用方法
  192.                                                 &Usage();
  193.                                         }
  194. #將命令行中的參數(shù)值賦給哈希表%opt對應(yīng)的鍵
  195.                                         $opt{$opt} = shift(@ARGV);
  196. #將哈希表中的值存入數(shù)組@optArgs中
  197.                                         push(@optArgs, $opt{$opt});
  198.                                 } else {
  199. #如果哈希表%valFollows中鍵的預(yù)設(shè)值為0
  200.                                         $opt{$opt} = 1;
  201.                                 }
  202. #讀取下一個命令行參數(shù)
  203.                                 next arg;
  204.                         }
  205.                 }
  206. #將命令行中無法匹配模板的參數(shù)存入數(shù)組@newargs
  207.                 push(@newargs,$arg);
  208.     }
  209. #將沒能匹配上的命令行參數(shù)重新賦給@ARGV
  210.     @ARGV = @newargs;
  211. }


  212. #例行檢查,保證我們在一個可靠的環(huán)境中操作
  213. # Routine to make sure we're operating in a sane environment.
  214. #

  215. sub SanityCheck
  216. {
  217.         #定義本地變量,local相當(dāng)于my
  218.     local ($cmd);

  219.     #下面的正則表達(dá)將獲取程序的名字
  220.     # Get the program name
  221.     #
  222.         #"=~"叫做綁定操作符,綁定右側(cè)的正則表達(dá)式與左側(cè)的$0,默認(rèn)的右側(cè)正則表達(dá)式會與perl默認(rèn)的變量$_進(jìn)行匹配。
  223.         #m||,m是perl正則表達(dá)式(模式匹配)操作符的縮寫match
  224.     ($prog) = ($0 =~ m|([^/]+)$|);

  225.     #檢查環(huán)境變量PATH中包含我們所需要的命令
  226.     # Check we have all the commands we'll need on the path.
  227.     #

  228. cmd:
  229.         #foreach循環(huán),每次取括號中的一個參數(shù)賦給$cmd
  230.     foreach $cmd ("uname","xauth","Xvnc","vncpasswd") {
  231.         #用split操作符將PATH環(huán)境變量通過符號":"拆分開,通過循環(huán)取出環(huán)境變量賦給$_,判斷$_目錄中的$cmd是否有可執(zhí)行權(quán)限,$_是perl的默認(rèn)變量
  232.                 for (split(/:/,$ENV{PATH})) {
  233.         #檢查當(dāng)前用戶對$cmd命令是否具有可執(zhí)行的權(quán)限
  234.                         if (-x "$_/$cmd") {
  235.         #如果找到了第一個可執(zhí)行的$cmd,就檢查下一個$cmd
  236.                         next cmd;
  237.                         }
  238.                 }
  239.         #如果遍歷了PATH環(huán)境變量,找不到可執(zhí)行的$cmd,則打印出沒找到的$cmd并退出程序
  240.         #函數(shù)名:die
  241.         #調(diào)用語法:die (message);
  242.         #解說:終止程序并向STDERR輸出錯誤信息。message可以為字符串或列表。如果最后一個參數(shù)不包含換行符,則程序文件名和行號也被輸出。
  243.                 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
  244.     }

  245.     #
  246.     # Check the HOME environment variable is set
  247.     #檢查家目錄環(huán)境變量已經(jīng)設(shè)置好了

  248.         #如果查詢發(fā)現(xiàn)家目錄的環(huán)境變量沒有定義,則打印出家目錄環(huán)境變量沒設(shè)置并退出程序
  249.     if (!defined($ENV{HOME})) {
  250.                 die "$prog: The HOME environment variable is not set.\n";
  251.     }
  252.         #改變目錄到用戶的家目錄
  253.     chdir($ENV{HOME});

  254.     #
  255.     # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
  256.     # eval, and if it fails we try 'require "sys/socket.ph"'.  If this fails,
  257.     # we just guess at the values.  If you find perl moaning here, just
  258.     # hard-code the values of AF_INET and SOCK_STREAM.  You can find these out
  259.     # for your platform by looking in /usr/include/sys/socket.h and related
  260.     # files.
  261.     #

  262.         #獲取操作系統(tǒng)的名字
  263.     chop($os = `uname`);
  264.         #獲取操作系統(tǒng)的版本
  265.     chop($osrev = `uname -r`);

  266.         #執(zhí)行perl目錄下的Socket.pm查找AF_INET和SOCK_STREAM
  267.     eval 'use Socket';
  268.         # $@存儲 Perl解釋器從eval語句返回的錯誤消息,如果$@有內(nèi)容的話,就接著查找sys/socket.ph
  269.     if ($@) {
  270.         #檢查是否能夠找到sys/socket.ph
  271.                 eval 'require "sys/socket.ph"';
  272.         #如果$@有內(nèi)容的話,表示require失敗
  273.                 if ($@) {
  274.         #通過判斷操作系統(tǒng)是SunOS,如果版本是非4的話,hard-code AF_INET=2和SOCK_STREAM=2,如果版本是4的話,hard-code AF_INET=2和SOCK_STREAM=1
  275.                         if (($os eq "SunOS") && ($osrev !~ /^4/)) {
  276.                                 $AF_INET = 2;
  277.                                 $SOCK_STREAM = 2;
  278.                         } else {
  279.                                 $AF_INET = 2;
  280.                                 $SOCK_STREAM = 1;
  281.                         }
  282.         #如果require成功的話,就取sys/socket.ph里面的地址組和流套接字的值
  283.                 } else {
  284.                         $AF_INET = &AF_INET;
  285.                         $SOCK_STREAM = &SOCK_STREAM;
  286.                 }
  287.         #如果use Socket成功的話,就取Socket.pm里面的地址組和流套接字的值
  288.     } else {
  289.                 $AF_INET = &AF_INET;
  290.                 $SOCK_STREAM = &SOCK_STREAM;
  291.     }
  292. }
復(fù)制代碼

論壇徽章:
0
3 [報告]
發(fā)表于 2013-03-05 13:31 |只看該作者
剛開始學(xué)習(xí)perl語言,借助別人優(yōu)秀的程序提高自己的能力,如有錯誤,請幫忙指正~

論壇徽章:
46
15-16賽季CBA聯(lián)賽之四川
日期:2018-03-27 11:59:132015年亞洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49雙魚座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亞冠之布里斯班獅吼
日期:2015-07-13 10:44:56
4 [報告]
發(fā)表于 2013-03-05 14:23 |只看該作者
這用中文把代碼一行一行念出來,對寫的人來說是一種磨煉因為要每一行每一個字符都看懂,但是對讀的人來說沒什么意義啊。

論壇徽章:
0
5 [報告]
發(fā)表于 2013-03-06 13:15 |只看該作者
回復(fù) 4# zhlong8


   因為我的工作中要用到unix中的vnc這個遠(yuǎn)程控制工具,但是很多人都對這個工具的運作過程不了解,我借助看這個工具的代碼來學(xué)習(xí)perl,也正好拿出來讓用vnc這個工具的人能了解一下它是怎么啟動的,希望能幫到需要的人,呵呵。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP