boa初始化配置及改造利用

文章轉自:http://blog.chinaunix.net/uid-20718037-id-4560951.html


前言

用到boa搭建的web服務器,不懂yacc,所以也不明白參數解析原理!

索性就不理解了,退而求其次會用就好了。 看到這一篇有如神助!。

正文

一,
boa通過read_config_files()調用語法分析入口函數yyparse(), 取yyparse()函數調用詞法分析入口函數yylex(),讀取並解析boa.conf和mime.types文件, 將其內容理解成一個個的單詞,再將每一行單詞的組合理解爲相應的配置選項。其中yyparse()函數調用yylex()讀取boa.conf和mime.types文件內容。具體的,yylex()函數讀取文件內容,將文件內容按指定的"詞法規則”理解成一個個的單詞,並將單詞返回給yyparse()函數。yyparse()將得到的單詞按指定的"語法規則”理解成配置選項。

二,
yylex()是詞法分析入口函數,在lex.yy.c文件中,由"flex  boa_lexer.l"命令生成。flex是詞法分析生成工具,boa_lexer.l中的代碼指定了"詞法規則” 。
yyparse()是語法分析入口函數,在y.tab.c文件中,由"bison -y  -d boa_grammar.y"命令生成,bison是語法分析生成工具,boa_grammar.y中的代碼指定了“語法規則” 。

三,
boa_lexer.l文件
boa_lexer.l是flex程序文件,包含三個部分,各部分這間用%%分割。

...定義部分...
%%
...規則部分...
%%
...用戶子程序例程...

第一部分是定義部分,包含聲明和選項設置。
    其中的%{和%}之間的部分會被原樣複製到生成的c文件開頭部分。是一些頭文件包含及宏定義,變量聲明等C語言代碼
    %s和%x用來定義一個包含的起始狀態和獨佔的起始狀態。第二部分模式匹配時會用到。
第二部分是規則部分,包含一系列的模式和動作。
    其中的模式使用正則表達式語言。動作是模式匹配時執行的C代碼。這裏的C代碼是用{}括住的多行語句或分號";" 
第三部分是用戶子程序部分,和是則是會被複制到生成的詞法分析器裏面的C代碼。

boa_grammar.y文件
boa_grammar.y是bison程序文件,包含三個部分,各部分這間用%%分割。

..定義部分...
%%
...規則部分...
%%
...用戶子程序例程...

第一部分包含聲明定義等。
    其中的%{和%}之間的部分會被原樣複製到目標分析程序開頭。%token,%start,%union分別用來聲明記號,起始規則,名爲YYSTYPE的union類型的域
第二部分規則部分是一系列的簡單BNF定義的規則和動作。
第三部分則是會被複制到生成的語法分析器裏面的C代碼。

四,代碼改造
    將代碼進行適當修改,成爲配置文件解析工具
修改後的代碼如下:
   1,boa_lexer.l代碼  

點擊(此處)摺疊或打開

  1. %{

  2. /*
  3.  * Boa, an http server
  4.  * Copyright (C) 1995 Paul Phillips <psp@well.com>
  5.  *
  6.  * This program 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 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program 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 program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */

  21. /* $Id: boa_lexer.l,v 1.13.2.1 2002/07/07 23:19:55 jnelson Exp $*/

  22. #include "y.tab.h"
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include "parse.h"

  26. int lineno = 1;
  27. struct ccommand *k; 
  28. char *kptr; 
  29. %}

  30. %%

  31. [ \t]+        ;
  32. #.*        ;

  33. [^ \"\t\n]+    { /* XXX could use better checks that we are in a state to
  34.          * accept keywords; this version matches original behavior */
  35.          if (k = lookup_keyword(yytext)) {
  36.          yylval.cval=k;
  37.          return (k->type);
  38.          } else { yylval.sval = yytext; return STRING; }
  39.             }

  40. \n        { lineno++; }
  41. %%

  42. /* In yywrap we track which file we are on.
  43.  * 1: close boa.conf, open mime.types
  44.  * 2: return 1;
  45.  */

  46. int yywrap()
  47. {
  48.     fclose(yyin);
  49.     return 1;
  50. }

  51. int yyerror(char * msg)
  52. {
  53.     fprintf(stderr, "Error on line %d of %s: %s\n", lineno, "boa.conf", msg); 
  54.     return 1;
    }

   2,boa_grammar.y文件代碼

點擊(此處)摺疊或打開

  1. %{
  2. /*
  3. * Boa, an http server
  4. * Copyright (C) 1995 Paul Phillips <[email protected]>
  5. *
  6. * This program 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 1, or (at your option)
  9. * any later version.
  10. *
  11. * This program 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 program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. /* $Id: boa_grammar.y,v 1.14 1999/10/12 14:49:07 jon Exp $*/
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. /* #include "boa.h" */
  27. #include "parse.h"
  28. int yyerror(char * msg);
  29. /* yydebug = 1; */
  30. #ifdef DEBUG
  31. #define DBG(x) x
  32. #else
  33. #define DBG(x)
  34. #endif
  35. char *arg1hold;
  36. char mime_type[256]; /* global to inherit */
  37. %}
  38. %union {
  39. char * sval;
  40. int ival;
  41. struct ccommand * cval;
  42. };
  43. /* boa.conf tokens */
  44. %token <cval> STMT_NO_ARGS STMT_ONE_ARG STMT_TWO_ARGS
  45. /* mime.type tokens */
  46. %token <sval> STRING
  47. %token <ival> INTEGER
  48. %token <sval> KEY
  49. %token <sval> VALUE
  50. %start BoaConfigStmts
  51. %%
  52. BoaConfigStmts: BoaConfigStmts BoaConfigStmt
  53. | /* empty */
  54. ;
  55. BoaConfigStmt:
  56. StmtNoArgs
  57. | StmtOneArg
  58. | StmtTwoArgs
  59. ;
  60. StmtNoArgs: STMT_NO_ARGS
  61. { if ($1->action) {
  62. DBG(printf("StmtNoArgs: %s\n",$1->name);)
  63. $1->action(NULL,NULL,$1->object);
  64. }
  65. }
  66. ;
  67. StmtOneArg: STMT_ONE_ARG STRING
  68. { if ($1->action) {
  69. DBG(printf("StmtOneArg: %s %s\n",$1->name,$2);)
  70. $1->action($2,NULL,$1->object);
  71. }
  72. }
  73. ;
  74. StmtTwoArgs: STMT_TWO_ARGS STRING
  75. { arg1hold = strdup($2); }
  76. STRING
  77. { if ($1->action) {
  78. DBG(printf("StmtTwoArgs: '%s' '%s' '%s'\n",
  79. $1->name,arg1hold,$4);)
  80. $1->action($4,arg1hold,$1->object);
  81. }
  82. free(arg1hold);
  83. }
  84. ;
  85. %%
3,config.c代碼
  

點擊(此處)摺疊或打開

  1. /*
  2.  * Boa, an http server
  3.  * Copyright (C) 1999 Larry Doolittle <ldoolitt@boa.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */

  20. /* $Id: config.c,v 1.31.2.3 2002/07/26 03:04:29 jnelson Exp $*/

  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <pwd.h>
  27. #include <grp.h>

  28. #include "y.tab.h"
  29. #include "parse.h"

  30. int yyparse(void); /* Better match the output of lex */
  31. char *mime_types;

  32. #ifdef DEBUG
  33. #define DBG(x) x
  34. #else
  35. #define DBG(x)
  36. #endif


  37. int server_port;
  38. uid_t server_uid;
  39. gid_t server_gid;
  40. char *server_root;
  41. char *server_name;
  42. char *server_admin;
  43. char *server_ip;
  44. char *myString;
  45. int virtualhost;
  46. long int max_connections;

  47. char *document_root;
  48. char *user_dir;
  49. char *directory_index;
  50. char *default_type;
  51. char *dirmaker;
  52. char *cachedir;

  53. char *tempdir;

  54. char *cgi_path = NULL;
  55. int single_post_limit = (1024 * 1024);
  56. int verbose_cgi_logs = 0;

  57. int ka_timeout;
  58. int ka_max;
  59. int myint;

  60. int backlog = 250;

  61. /* These came from log.*/
  62. char *error_log_name;
  63. char *access_log_name;
  64. char *cgi_log_name;

  65. int use_localtime;

  66. /* These are new */
  67. static void c_set_user(char *v1, char *v2, void *t);
  68. static void c_set_group(char *v1, char *v2, void *t);
  69. static void c_set_string(char *v1, char *v2, void *t);
  70. static void c_set_int(char *v1, char *v2, void *t);
  71. static void c_set_unity(char *v1, char *v2, void *t);
  72. static void c_add_type(char *v1, char *v2, void *t);
  73. static void c_add_alias(char *v1, char *v2, void *t);

  74. /* Fakery to keep the value passed to action() a void *,
  75.    see usage in table and c_add_alias() below */
  76. static int script_number = 1;
  77. static int redirect_number = 2;
  78. static int alias_number = 0;
  79. static uid_t current_uid=0;

  80. /* Help keep the table below compact */
  81. #define S0A STMT_NO_ARGS
  82. #define S1A STMT_ONE_ARG
  83. #define S2A STMT_TWO_ARGS

  84. struct ccommand clist[] = {
  85.     {"Port", S1A, c_set_int, &server_port},
  86.     {"Listen", S1A, c_set_string, &server_ip},
  87.     {"MyString", S1A, c_set_string, &myString},
  88.     {"BackLog", S1A, c_set_int, &backlog},
  89.     {"User", S1A, c_set_user, NULL},
  90.     {"Group", S1A, c_set_group, NULL},
  91.     {"ServerAdmin", S1A, c_set_string, &server_admin},
  92.     {"ServerRoot", S1A, c_set_string, &server_root},
  93.     {"ErrorLog", S1A, c_set_string, &error_log_name},
  94.     {"AccessLog", S1A, c_set_string, &access_log_name},
  95.     {"UseLocaltime", S0A, c_set_unity, &use_localtime},
  96.     {"CgiLog", S1A, c_set_string, &cgi_log_name},
  97.     {"VerboseCGILogs", S0A, c_set_unity, &verbose_cgi_logs},
  98.     {"ServerName", S1A, c_set_string, &server_name},
  99.     {"VirtualHost", S0A, c_set_unity, &virtualhost},
  100.     {"DocumentRoot", S1A, c_set_string, &document_root},
  101.     {"UserDir", S1A, c_set_string, &user_dir},
  102.     {"DirectoryIndex", S1A, c_set_string, &directory_index},
  103.     {"DirectoryMaker", S1A, c_set_string, &dirmaker},
  104.     {"DirectoryCache", S1A, c_set_string, &cachedir},
  105.     {"KeepAliveMax", S1A, c_set_int, &ka_max},
  106.     {"Myint", S1A, c_set_int, &myint},
  107.     {"KeepAliveTimeout", S1A, c_set_int, &ka_timeout},
  108.     {"MimeTypes", S1A, c_set_string, &mime_types},
  109.     {"DefaultType", S1A, c_set_string, &default_type},
  110.     {"AddType", S2A, c_add_type, NULL},
  111.     {"ScriptAlias", S2A, c_add_alias, &script_number},
  112.     {"Redirect", S2A, c_add_alias, &redirect_number},
  113.     {"Alias", S2A, c_add_alias, &alias_number},
  114.     {"SinglePostLimit", S1A, c_set_int, &single_post_limit},
  115.     {"CGIPath", S1A, c_set_string, &cgi_path},
  116.     {"MaxConnections", S1A, c_set_int, &max_connections},
  117. };

  118. void printConfig(void)
  119. {
  120.     printf("\n\nserver_port = %d\n", server_port);
  121.     printf("server_ip = %s\n", server_ip);
  122.     printf("MyString = %s\n", myString);
  123.     printf("backlog = %d\n", backlog);
  124.     printf("server_admin = %s\n", server_admin);
  125.     printf("server_root = %s\n", server_root);
  126.     printf("error_log_name = %s\n", error_log_name);
  127.     printf("access_log_name = %s\n", access_log_name);
  128.     printf("use_localtime = %d\n", use_localtime);
  129.     printf("cgi_log_name = %s\n", cgi_log_name);
  130.     printf("verbose_cgi_logs = %d\n", verbose_cgi_logs);
  131.     printf("server_name = %s\n", server_name);
  132.     printf("virtualhost = %d\n", virtualhost);
  133.     printf("document_root = %s\n", document_root);
  134.     printf("user_dir = %s\n", user_dir);
  135.     printf("directory_index = %s\n", directory_index);
  136.     printf("dirmaker = %s\n", dirmaker);
  137.     printf("cachedir = %s\n", cachedir);
  138.     printf("ka_max = %d\n", ka_max);
  139.     printf("myint = %d\n", myint);
  140.     printf("ka_timeout = %d\n", ka_timeout);
  141.     printf("mime_types = %s\n", mime_types);
  142.     printf("default_type = %s\n", default_type);
  143.     printf("script_number = %d\n", script_number);
  144.     printf("redirect_number = %d\n", redirect_number);
  145.     printf("alias_number = %d\n", alias_number);
  146.     printf("single_post_limit = %d\n", single_post_limit);
  147.     printf("cgi_path = %s\n", cgi_path);
  148.     printf("max_connections = %ld\n", max_connections);
  149. }

  150. void add_mime_type(char *extension, char *type)
  151. {
  152. }

  153. void add_alias(char *fakename, char *realname, int type)
  154. {
  155. }

  156. static void c_set_user(char *v1, char *v2, void *t)
  157. {
  158.     struct passwd *passwdbuf;
  159.     char *endptr;
  160.     int i;

  161.     DBG(printf("User %s = ", v1);
  162.         )
  163.         i = strtol(v1, &endptr, 0);
  164.     if (*v1 != '\0' && *endptr == '\0') {
  165.         server_uid = i;
  166.     } else {
  167.         passwdbuf = getpwnam(v1);
  168.         if (!passwdbuf) {
  169.             if (current_uid)
  170.                 return;
  171.             fprintf(stderr, "No such user: %s\n", v1);
  172.             exit(1);
  173.         }
  174.         server_uid = passwdbuf->pw_uid;
  175.     }
  176.     DBG(printf("%d\n", server_uid);
  177.         )
  178. }

  179. static void c_set_group(char *v1, char *v2, void *t)
  180. {
  181.     struct group *groupbuf;
  182.     char *endptr;
  183.     int i;
  184.     DBG(printf("Group %s = ", v1);
  185.         )
  186.         i = strtol(v1, &endptr, 0);
  187.     if (*v1 != '\0' && *endptr == '\0') {
  188.         server_gid = i;
  189.     } else {
  190.         groupbuf = getgrnam(v1);
  191.         if (!groupbuf) {
  192.             if (current_uid)
  193.                 return;
  194.             fprintf(stderr, "No such group: %s\n", v1);
  195.             exit(1);
  196.         }
  197.         server_gid = groupbuf->gr_gid;
  198.     }
  199.     DBG(printf("%d\n", server_gid);
  200.         )
  201. }

  202. static void c_set_string(char *v1, char *v2, void *t)
  203. {
  204.     char *s;
  205.     DBG(printf("Setting pointer %p to string %s ..", t, v1);
  206.         )
  207.         if (t) {
  208.         s = *(char **) t;
  209.         if (s)
  210.             free(s);
  211.         *(char **) t = strdup(v1);
  212.         if (!*(char **) t) {
  213.             printf("[%s-%d]Unable to strdup in c_set_string", __FILE__, __LINE__);
  214.             exit(1);
  215.         }
  216.         DBG(printf("done.\n");
  217.             )
  218.     } else {
  219.         DBG(printf("skipped.\n");
  220.             )
  221.     }
  222. }

  223. static void c_set_int(char *v1, char *v2, void *t)
  224. {
  225.     char *endptr;
  226.     int i;
  227.     DBG(printf("Setting pointer %p to integer string %s ..", t, v1);
  228.         )
  229.         if (t) {
  230.         i = strtol(v1, &endptr, 0); /* Automatic base 10/16/8 switching */
  231.         if (*v1 != '\0' && *endptr == '\0') {
  232.             *(int *) t = i;
  233.             DBG(printf(" Integer converted as %d, done\n", i);
  234.                 )
  235.         } else {
  236.             /* XXX should tell line number to user */
  237.             fprintf(stderr, "Error: %s found where integer expected\n",
  238.                     v1);
  239.         }
  240.     } else {
  241.         DBG(printf("skipped.\n");
  242.             )
  243.     }
  244. }

  245. static void c_set_unity(char *v1, char *v2, void *t)
  246. {
  247.     DBG(printf("Setting pointer %p to unity\n", t);
  248.         )
  249.         if (t)
  250.         *(int *) t = 1;
  251. }

  252. static void c_add_type(char *v1, char *v2, void *t)
  253. {
  254.     add_mime_type(v1, v2);
  255. }

  256. static void c_add_alias(char *v1, char *v2, void *t)
  257. {
  258.     add_alias(v2, v1, *(int *) t);
  259. }

  260. struct ccommand *lookup_keyword(char *c)
  261. {
  262.     struct ccommand *p;
  263.     DBG(printf("Checking string '%s' against keyword list\n", c);
  264.         )
  265.         for (= clist;
  266.              p < clist + (sizeof (clist) / sizeof (struct ccommand)); p++) {
  267.         if (strcmp(c, p->name) == 0)
  268.             return p;
  269.     }
  270.     return NULL;
  271. }

  272. extern FILE *yyin;
  273. int main(int argc, char * argv[])
  274. {
  275.     yyin = fopen("./boa.conf", "r");
  276.     if (!yyin) {
  277.         fputs("Could not open boa.conf for reading.\n", stderr);
  278.         exit(1);
  279.     } 

  280.     yyparse();

  281.     printConfig();
  282.     return 0;
  283. }
4,parse.h

點擊(此處)摺疊或打開

  1. /*
  2.  * Boa, an http server
  3.  * Copyright (C) 1999 Larry Doolittle <ldoolitt@boa.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * parse.h
  20.  * minimum interaction point between Boa's parser (boa_lexer.and
  21.  * boa_grammar.y) and the rest of Boa.
  22.  */

  23. /* $Id: parse.h,v 1.5 2000/02/12 21:52:45 jon Exp $*/

  24. struct ccommand {
  25.     char *name;
  26.     int type;
  27.     void (*action) (char *, char *, void *);
  28.     void *object;
  29. };

  30. struct ckey {
  31.     char *name;
  32.     int type;
  33. };
  34. struct ckey *lookup_key(char *c);
  35. struct ccommand *lookup_keyword(char *c);
  36. void add_mime_type(char *extension, char *type);
5,測試用的文件boa.conf
    

點擊(此處)摺疊或打開

  1. Port 80
  2. User root
  3. Group root
  4. ErrorLog /dev/console
  5. AccessLog /dev/null
  6. ServerName zhanglong
  7. DocumentRoot /web
  8. DirectoryIndex index.html
  9. KeepAliveMax 1000
  10. Myint 123456
  11. KeepAliveTimeout 10
  12. MimeTypes /etc/mime.types
  13. DefaultType text/plain
  14. CGIPath /bin
  15. AddType application/x-httpd-cgi cgi
  16. MyString valueOfMyString
6,Makefile
   

點擊(此處)摺疊或打開

  1. all:
  2. bison -y -d boa_grammar.y
  3. #gcc -g -O2 -pipe -Wall -I. -c -o y.tab.o y.tab.c -DDEBUG
  4. gcc -g -O2 -pipe -Wall -I. -c -o y.tab.o y.tab.c
  5. flex boa_lexer.l
  6. gcc -g -O2 -pipe -Wall -I. -c -o lex.yy.o lex.yy.c
  7. gcc -g -O2 -pipe -Wall -I. -c -o config.o config.c
  8. gcc -o parse y.tab.o lex.yy.o config.o
  9. clean:
  10. rm -f y.tab.c y.tab.h lex.yy.c *.o parse
以上是修改及測試涉及到的全部文件。

五,測試輸出及說明
將以上文件置於同一目錄下,運行make生成parse可執行文件,輸出如下:
  
然後執行./parse,輸出如下:

上面的輸出結果中有“MyString = ValueOfMyString”, "myint = 123456"是自己另外加上的。
要添加自己的配置項,只要修改4個地方即可。以添加myint項爲例:
        1,添加一個全局變量int myint;  
        2,在struct ccommand clist[]數組中添加一項“ {"Myint", S1A, c_set_int, &myint},”
        3,如果要看修改後的結果,在“printConfig()函數”中添加“ printf("myint = %d\n", myint);”
         4,在boa.conf文件中添加相應的賦值項。
添加"MyString"項的步驟與添加“myint”的相同,只是myint 是int型數據,在struct ccommand clist[]數組中的賦值函數用c_set_int;而MyString是char *類型,在struct ccommand clist[]數組中的賦值函數用c_set_string。

   修改config.c文件main()函數指定要解析的文件,或將文件名以函數參數的形式傳入,就可以解析指定的文件名中的配置參數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章