Move browscap code to the new modular INI parser

This commit is contained in:
Zeev Suraski 2000-10-29 23:10:22 +00:00
parent 3c06816532
commit 60c0835470
2 changed files with 137 additions and 136 deletions

View File

@ -25,10 +25,141 @@
#include "zend_globals.h"
HashTable browser_hash;
static HashTable browser_hash;
static zval *current_section;
#define DEFAULT_SECTION_NAME "Default Browser Capability Settings"
static void browscap_entry_dtor(zval *pvalue)
{
if (pvalue->type == IS_OBJECT || pvalue->type == IS_ARRAY) {
zend_hash_destroy(pvalue->value.obj.properties);
free(pvalue->value.obj.properties);
}
}
static void convert_browscap_pattern(zval *pattern)
{
register int i,j;
char *t;
for (i=0; i<pattern->value.str.len; i++) {
if (pattern->value.str.val[i]=='*' || pattern->value.str.val[i]=='?' || pattern->value.str.val[i]=='.') {
break;
}
}
if (i==pattern->value.str.len) { /* no wildcards */
pattern->value.str.val = zend_strndup(pattern->value.str.val, pattern->value.str.len);
return;
}
t = (char *) malloc(pattern->value.str.len*2);
for (i=0,j=0; i<pattern->value.str.len; i++,j++) {
switch (pattern->value.str.val[i]) {
case '?':
t[j] = '.';
break;
case '*':
t[j++] = '.';
t[j] = '*';
break;
case '.':
t[j++] = '\\';
t[j] = '.';
break;
default:
t[j] = pattern->value.str.val[i];
break;
}
}
t[j]=0;
pattern->value.str.val = t;
pattern->value.str.len = j;
}
static void php_browscap_parser_cb(zval *arg1, zval *arg2, int callback_type, void *arg)
{
switch (callback_type) {
case ZEND_INI_PARSER_ENTRY:
if (current_section) {
zval *new_property;
char *new_key;
new_property = (zval *) malloc(sizeof(zval));
INIT_PZVAL(new_property);
new_property->value.str.val = Z_STRVAL_P(arg2);
new_property->value.str.len = Z_STRLEN_P(arg2);
new_property->type = IS_STRING;
new_key = zend_strndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1));
zend_str_tolower(new_key, Z_STRLEN_P(arg1));
zend_hash_update(current_section->value.obj.properties, new_key, Z_STRLEN_P(arg1)+1, &new_property, sizeof(zval *), NULL);
free(new_key);
}
break;
case ZEND_INI_PARSER_SECTION: {
zval *processed;
/*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len+1);*/
current_section = (zval *) malloc(sizeof(zval));
INIT_PZVAL(current_section);
processed = (zval *) malloc(sizeof(zval));
INIT_PZVAL(processed);
current_section->value.obj.ce = &zend_standard_class_def;
current_section->value.obj.properties = (HashTable *) malloc(sizeof(HashTable));
current_section->type = IS_OBJECT;
zend_hash_init(current_section->value.obj.properties, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1);
zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, (void *) &current_section, sizeof(zval *), NULL);
processed->value.str.val = Z_STRVAL_P(arg1);
processed->value.str.len = Z_STRLEN_P(arg1);
processed->type = IS_STRING;
convert_browscap_pattern(processed);
zend_hash_update(current_section->value.obj.properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &processed, sizeof(zval *), NULL);
}
break;
}
}
PHP_MINIT_FUNCTION(browscap)
{
char *browscap = INI_STR("browscap");
if (browscap) {
zend_file_handle fh;
if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1)==FAILURE) {
return FAILURE;
}
fh.handle.fp = V_FOPEN(browscap, "r");
if (!fh.handle.fp) {
php_error(E_WARNING,"Cannot open '%s' for reading", browscap);
return FAILURE;
}
zend_parse_ini_file(&fh, (zend_ini_parser_cb_t) php_browscap_parser_cb, &browser_hash);
}
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(browscap)
{
if (INI_STR("browscap")) {
zend_hash_destroy(&browser_hash);
}
return SUCCESS;
}
static int browser_reg_compare(zval **browser,int num_args, va_list args, zend_hash_key *key)
{
zval **browser_name;

View File

@ -27,7 +27,6 @@
#include "php_ini.h"
#include "ext/standard/dl.h"
#include "ext/standard/file.h"
#include "ext/standard/php_browscap.h"
#include "zend_extensions.h"
@ -41,14 +40,11 @@
#define YYSTYPE zval
#define PARSING_MODE_CFG 0
#define PARSING_MODE_BROWSCAP 1
#define PARSING_MODE_STANDALONE 2
static HashTable configuration_hash;
extern HashTable browser_hash;
PHPAPI extern char *php_ini_path;
static HashTable *active_hash_table;
static zval *current_section;
static char *currently_parsed_filename;
static int parsing_mode;
@ -136,19 +132,10 @@ static void yyerror(char *str)
static void pvalue_config_destructor(zval *pvalue)
{
if (pvalue->type == IS_STRING && pvalue->value.str.val != empty_string) {
free(pvalue->value.str.val);
}
}
static void pvalue_browscap_destructor(zval *pvalue)
{
if (pvalue->type == IS_OBJECT || pvalue->type == IS_ARRAY) {
zend_hash_destroy(pvalue->value.obj.properties);
free(pvalue->value.obj.properties);
}
{
if (pvalue->type == IS_STRING && pvalue->value.str.val != empty_string) {
free(pvalue->value.str.val);
}
}
@ -247,32 +234,6 @@ int php_init_config(void)
}
PHP_MINIT_FUNCTION(browscap)
{
char *browscap = INI_STR("browscap");
if (browscap) {
if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) pvalue_browscap_destructor, 1)==FAILURE) {
return FAILURE;
}
cfgin = V_FOPEN(browscap, "r");
if (!cfgin) {
php_error(E_WARNING,"Cannot open '%s' for reading", browscap);
return FAILURE;
}
init_cfg_scanner();
active_hash_table = &browser_hash;
parsing_mode = PARSING_MODE_BROWSCAP;
currently_parsed_filename = browscap;
yyparse();
fclose(cfgin);
}
return SUCCESS;
}
int php_shutdown_config(void)
{
zend_hash_destroy(&configuration_hash);
@ -280,57 +241,6 @@ int php_shutdown_config(void)
}
PHP_MSHUTDOWN_FUNCTION(browscap)
{
if (INI_STR("browscap")) {
zend_hash_destroy(&browser_hash);
}
return SUCCESS;
}
static void convert_browscap_pattern(zval *pattern)
{
register int i,j;
char *t;
for (i=0; i<pattern->value.str.len; i++) {
if (pattern->value.str.val[i]=='*' || pattern->value.str.val[i]=='?' || pattern->value.str.val[i]=='.') {
break;
}
}
if (i==pattern->value.str.len) { /* no wildcards */
pattern->value.str.val = zend_strndup(pattern->value.str.val, pattern->value.str.len);
return;
}
t = (char *) malloc(pattern->value.str.len*2);
for (i=0,j=0; i<pattern->value.str.len; i++,j++) {
switch (pattern->value.str.val[i]) {
case '?':
t[j] = '.';
break;
case '*':
t[j++] = '.';
t[j] = '*';
break;
case '.':
t[j++] = '\\';
t[j] = '.';
break;
default:
t[j] = pattern->value.str.val[i];
break;
}
}
t[j]=0;
pattern->value.str.val = t;
pattern->value.str.len = j;
}
void do_cfg_op(char type, zval *result, zval *op1, zval *op2)
{
int i_result;
@ -426,23 +336,6 @@ statement:
zend_alter_ini_entry($1.value.str.val, $1.value.str.len+1, $3.value.str.val, $3.value.str.len+1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP);
}
break;
case PARSING_MODE_BROWSCAP:
if (current_section) {
zval *new_property;
char *new_key;
new_property = (zval *) malloc(sizeof(zval));
INIT_PZVAL(new_property);
new_property->value.str.val = $3.value.str.val;
new_property->value.str.len = $3.value.str.len;
new_property->type = IS_STRING;
new_key = zend_strndup($1.value.str.val, $1.value.str.len);
zend_str_tolower(new_key,$1.value.str.len);
zend_hash_update(current_section->value.obj.properties, new_key, $1.value.str.len+1, &new_property, sizeof(zval *), NULL);
free(new_key);
}
break;
case PARSING_MODE_STANDALONE: {
zval *entry;
@ -500,30 +393,7 @@ statement:
free($3.value.str.val);
}
}
| SECTION {
if (parsing_mode==PARSING_MODE_BROWSCAP) {
zval *processed;
/*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len+1);*/
current_section = (zval *) malloc(sizeof(zval));
INIT_PZVAL(current_section);
processed = (zval *) malloc(sizeof(zval));
INIT_PZVAL(processed);
current_section->value.obj.ce = &zend_standard_class_def;
current_section->value.obj.properties = (HashTable *) malloc(sizeof(HashTable));
current_section->type = IS_OBJECT;
zend_hash_init(current_section->value.obj.properties, 0, NULL, (dtor_func_t) pvalue_config_destructor, 1);
zend_hash_update(active_hash_table, $1.value.str.val, $1.value.str.len+1, (void *) &current_section, sizeof(zval *), NULL);
processed->value.str.val = $1.value.str.val;
processed->value.str.len = $1.value.str.len;
processed->type = IS_STRING;
convert_browscap_pattern(processed);
zend_hash_update(current_section->value.obj.properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &processed, sizeof(zval *), NULL);
}
free($1.value.str.val);
}
| SECTION { free($1.value.str.val); }
| '\n'
;