int main() { CFStack *stack = malloc(sizeof(CFStack)); CFHashMap *words = cf_hash_new(); cf_hash_add(words, "+", words_add); cf_hash_add(words, "-", words_minus); cf_hash_add(words, "*", words_multiply); cf_hash_add(words, "/", words_divide); cf_hash_add(words, ".", words_print); char commands[] = "2 3 + 4 * ."; char *token = strtok(commands, " "); while (token != NULL) { ValueType word = cf_hash_get(words, token); if (word != NULL) { (*word)(stack); } else { int *entry = malloc(sizeof(int)); *entry = atoi(token); cf_stack_push(stack, entry); } token = strtok(NULL, " "); } return 0; }
/* {{{ flt_failsafe_handle_command */ int flt_failsafe_handle_command(cf_configfile_t *cf,cf_conf_opt_t *opt,const u_char *context,u_char **args,size_t argnum) { cf_failsafe_t *fl,fl1; u_char buff[512]; if(flt_failsafe_hsh == NULL) flt_failsafe_hsh = cf_hash_new(flt_failsafe_cleanup_hash); if(argnum == 1) { if((fl = cf_hash_get(flt_failsafe_hsh,(u_char *)context,strlen(context))) == NULL) { fl1.BackupFile = strdup(args[0]); snprintf(buff,512,"BackupMutex_%s",context); cf_mutex_init(buff,&fl1.BackupMutex); cf_hash_set(flt_failsafe_hsh,(u_char *)context,strlen(context),&fl1,sizeof(fl1)); } else { free(fl->BackupFile); fl->BackupFile = strdup(args[0]); } } else { cf_log(CF_ERR,__FILE__,__LINE__,"flt_failsafe: expecting one argument for directive BackupFile!\n"); } return 0; }
/* {{{ flt_phpsessauth_getvar */ u_char *flt_phpsessauth_getvar(const u_char *vname) { cf_string_t path; int fd,rc; cf_hash_t *hash = cf_hash_new(NULL); u_char *start,*ptr,*name = NULL; struct stat st; cf_str_init_growth(&path,128); if(flt_phpsessauth_sesspath) cf_str_char_set(&path,flt_phpsessauth_sesspath,strlen(flt_phpsessauth_sesspath)); else cf_str_char_set(&path,"/tmp",4); cf_str_chars_append(&path,"/sess_",6); cf_str_chars_append(&path,flt_phpsessauth_sid,strlen(flt_phpsessauth_sid)); /* {{{ open file and map it into our memory */ if((fd = open(path.content,O_RDONLY)) == -1) { fprintf(stderr,"flt_phpsessauth: open: could not open file '%s': %s\n",path.content,strerror(errno)); cf_str_cleanup(&path); return NULL; } if(stat(path.content,&st) == -1) { fprintf(stderr,"flt_phpsessauth: stat: could not stat file '%s': %s\n",path.content,strerror(errno)); close(fd); cf_str_cleanup(&path); return NULL; } if(st.st_size == 0) { fprintf(stderr,"flt_phpsessauth: file '%s' is empty!\n",path.content); close(fd); cf_str_cleanup(&path); return NULL; } if((caddr_t)(ptr = start = mmap(0,st.st_size+1,PROT_READ,MAP_FILE|MAP_SHARED,fd,0)) == (caddr_t)-1) { fprintf(stderr,"flt_phpsessauth: mmap: could not map file '%s': %s\n",path.content,strerror(errno)); close(fd); cf_str_cleanup(&path); return NULL; } /* }}} */ rc = flt_psa_parser(hash,&ptr); munmap(start,st.st_size); close(fd); cf_str_cleanup(&path); if(rc != 0) { cf_hash_destroy(hash); return NULL; } name = cf_hash_get(hash,(u_char *)vname,strlen(vname)); if(name) name = strdup(name); cf_hash_destroy(hash); return name; }
/* {{{ cf_html_register_directive */ int cf_html_register_directive(const u_char *name,cf_directive_filter_t filter,int type) { cf_directive_callback_t clbck; size_t len = strlen(name); if(!registered_directives) registered_directives = cf_hash_new(NULL); if(cf_hash_get(registered_directives,(u_char *)name,len) != NULL) return -1; clbck.type = type; clbck.callback = filter; cf_hash_set(registered_directives,(u_char *)name,len,&clbck,sizeof(clbck)); return 0; }
/* {{{ cf_validate_msg */ int cf_validate_msg(cf_cfg_config_t *cfg,cf_cl_thread_t *thread,const u_char *msg,cf_tpl_variable_t *var) { cf_array_t my_stack; int rc; if(registered_validators == NULL) registered_validators = cf_hash_new(NULL); cf_array_init(&my_stack,sizeof(cf_html_stack_elem_t),NULL); rc = cf_validate_message(cfg,&my_stack,thread,msg,NULL,var); cf_array_destroy(&my_stack); return rc; }
/* {{{ cf_msg_to_html */ void cf_msg_to_html(cf_cfg_config_t *cfg,cf_cl_thread_t *thread,const u_char *msg,cf_string_t *content,cf_string_t *cite,u_char *quote_chars,int max_sig_lines,int show_sig) { cf_cfg_config_value_t *cs = cf_cfg_get_value(cfg,"DF:ExternCharset"); cf_cfg_config_value_t *xmlm = cf_cfg_get_value(cfg,"DF:XHTMLMode"); u_char *qchars,*ptr; size_t qclen; int utf8 = cf_strcmp(cs->sval,"UTF-8") == 0,xml; cf_array_t my_stack; cf_string_t content1; if(registered_directives == NULL) registered_directives = cf_hash_new(NULL); xml = cf_strcmp(xmlm->sval,"yes") == 0; if(utf8 || (qchars = htmlentities_charset_convert(quote_chars,"UTF-8",cs->sval,&qclen,0)) == NULL) { qchars = htmlentities(quote_chars,0); qclen = strlen(qchars); } cf_run_content_filters(cfg,PRE_CONTENT_FILTER,thread,content,cite,qchars); /* first line has no linebreak, so append quoting chars to cite */ if(cite) cf_str_chars_append(cite,qchars,qclen); cf_array_init(&my_stack,sizeof(cf_html_stack_elem_t),NULL); cf_str_init(&content1); cf_parse_message(cfg,thread,(u_char *)msg,&my_stack,content,cite,qchars,qclen,utf8,xml,max_sig_lines,show_sig,0,NULL,0); /* doin this because of plugins like the syntax parser; they could match quoting chars as operators */ for(ptr=content1.content;*ptr;++ptr) { if(*ptr == 0x7F) cf_str_chars_append(content,qchars,qclen); else cf_str_char_append(content,*ptr); } cf_str_cleanup(&content1); cf_run_content_filters(cfg,POST_CONTENT_FILTER,thread,content,cite,qchars); free(qchars); }
/* {{{ flt_httpauth_run */ int flt_phpsessauth_run(cf_hash_t *head,cf_configuration_t *dc,cf_configuration_t *vc) { u_char *fn = cf_hash_get(GlobalValues,"FORUM_NAME",10); cf_name_value_t *v = cf_cfg_get_first_value(dc,fn,"DF:AuthMode"); u_char *name = NULL,*path; cf_hash_t *cookies; cf_string_t *sess; if(!flt_phpsessauth_vname) return FLT_DECLINE; if(!v || !v->values[0] || cf_strcmp(v->values[0],"phpsess") != 0) return FLT_DECLINE; sess = cf_cgi_get(head,flt_phpsessauth_sessname); if(head == NULL || (flt_phpsessauth_sid = sess->content) == NULL) { cookies = cf_hash_new(cf_cgi_destroy_entry); cf_cgi_parse_cookies(cookies); if((sess = cf_cgi_get(cookies,flt_phpsessauth_sessname)) == NULL) { cf_hash_destroy(cookies); return FLT_DECLINE; } else flt_phpsessauth_sid = sess->content; cf_hash_destroy(cookies); } else cf_add_static_uri_flag(flt_phpsessauth_sessname,flt_phpsessauth_sid,0); if((name = flt_phpsessauth_getvar(flt_phpsessauth_vname)) != NULL) { path = cf_get_uconf_name(name); if(path) { free(path); cf_hash_set(GlobalValues,"UserName",8,name,strlen(name)+1); } free(name); } return FLT_OK; }
int flt_http_execute(cf_hash_t *head,cf_configuration_t *dc,cf_configuration_t *vc,void *sock) #endif { u_char buff[100]; time_t t,t1; struct tm *tm; struct tm tm_lm; u_char *lm; int ret; cf_hash_t *header_table = cf_hash_new(NULL); cf_hash_keylist_t *key; /* {{{ http header management */ ret = flt_http_header_callbacks(head,header_table,dc,vc,sock); cf_hash_entry_delete(header_table,"Last-Modified",14); for(key=header_table->keys.elems;key;key=key->next) { printf("%s: %s\015\012",key->key,(char *)cf_hash_get(header_table,key->key,strlen(key->key)+1)); } cf_hash_destroy(header_table); if(ret == FLT_EXIT) return FLT_EXIT; /* }}} */ if(http_config.handle_last_modified_since || http_config.send_last_modified) { t1 = flt_http_get_lm(sock); t1 = flt_http_lm_callbacks(head,dc,vc,sock,t1); tm = gmtime(&t1); if(http_config.send_last_modified) { strftime(buff,100,"%a, %d %b %Y %H:%M:%S GMT",tm); printf("Last-Modified: %s\015\012",buff); } if(http_config.handle_last_modified_since) { if((lm = getenv("HTTP_IF_MODIFIED_SINCE")) != NULL && *lm != '\0') { tm_lm.tm_mday = atoi(lm+5); tm_lm.tm_mon = flt_http_get_month(lm+8); tm_lm.tm_year = atoi(lm+12) - 1900; tm_lm.tm_wday = 0; tm_lm.tm_hour = atoi(lm+17); tm_lm.tm_min = atoi(lm+20); tm_lm.tm_sec = atoi(lm+23); tm_lm.tm_yday = 0; tm_lm.tm_isdst = 0; if((t = timegm(&tm_lm)) >= 0) { #ifdef DEBUG printf("X-Debug: last modified from forum: %ld, last modified from browser: %ld\n",t1,t); printf("X-Debug: forum: %s",ctime(&t1)); printf("X-Debug: browser: %s",ctime(&t)); #endif if(t >= t1) { ret = flt_http_validate_cache(head,dc,vc,t,sock); if(ret != FLT_EXIT) { printf("Status: 304 Not Modified\015\012\015\012"); return FLT_EXIT; } } } } } } return FLT_DECLINE; }