apr_status_t lwt_template_parse (const char *filename, lua_State *L, const char *flags, apr_pool_t *pool, apr_array_header_t **t, const char **err) { parser_rec *p; apr_status_t status; p = (parser_rec *) apr_pcalloc(pool, sizeof(parser_rec)); p->filename = filename; p->L = L; p->flags = parse_flags(flags != NULL ? flags : TEMPLATE_DEFAULT_FLAGS); p->pool = pool; p->t = apr_array_make(pool, 32, sizeof(template_node_t)); p->b = apr_array_make(pool, 8, sizeof(block_t)); status = parse_template(p); if (status == APR_SUCCESS) { if (!apr_is_empty_array(p->b)) { status = parse_error(p, apr_psprintf(p->pool, "%d open elements at end of template", p->b->nelts)); } } if (status != APR_SUCCESS) { if (err != NULL) { *err = p->err; } return status; } if (t != NULL) { *t = p->t; } return APR_SUCCESS; }
DependencyObject* FrameworkTemplate::GetVisualTreeWithError (FrameworkElement *templateBindingSource, MoonError *error) { if (holdManagedRef || xaml_buffer) { DependencyObject *result = parse_template (parse_template_data, GetResourceBase (), GetDeployment ()->GetSurface (), templateBindingSource, xaml_buffer, error); if (result) NameScope::GetNameScope (result)->Lock (); return result; } return NULL; }
int fw_perform_return( gchar *action, GHashTable *conf, peer *p, gchar *result, int size) { GHashTable *data; gchar *cmd; FILE * fd; int ret=0; gchar dummy_result[30]; if ( !result ) { result = dummy_result; size = sizeof(dummy_result); } data = g_hash_dup( conf ); // // Than add specifics about this particular client, if any if (p != NULL) { g_hash_set( data, "IP", p->ip ); g_hash_set( data, "MAC", p->hw ); g_hash_set( data, "Class", "Public" ); } cmd = conf_string( conf, action ); cmd = parse_template( cmd, data ); g_warning("Got command %s from action %s", cmd, action ); // add data to the environment g_hash_table_foreach( data, (GHFunc) fw_popen_set_env, NULL ); fd = popen(cmd, "r"); if(fd == NULL) { g_warning( "popen %s failed: %m", cmd ); ret=1; } else if ( fgets(result, size, fd) == NULL ) { g_warning( "fgets %s failed: %m", cmd ); ret=1; } if ( fd && pclose(fd) != 0 ) { g_warning( "pclose %s failed: %m", cmd ); ret=1; } g_hash_free(data); g_free(cmd); return ret; }
int http_serve_template ( http_request *h, gchar *file, GHashTable *data ) { gchar *form; guint r, n; form = parse_template( file, data ); n = strlen(form); http_add_header( h, "Content-Type", "text/html" ); http_send_header( h, 200, "OK" ); r = g_io_channel_write( h->sock, form, n, &n ); g_free( form ); if ( r != G_IO_ERROR_NONE ) { g_warning( "Serving template to %s failed: %m", h->peer_ip ); return 0; } return 1; }
static int fw_exec( fw_action *act, GHashTable *conf ) { GHashTable *data; GPtrArray *env; gchar *cmd, **arg, **n; data = g_hash_dup( conf ); // // Than add specifics about this particular client, if any if (act->p != NULL) { g_hash_set( data, "IP", act->p->ip ); g_hash_set( data, "MAC", act->p->hw ); g_hash_set( data, "Class", "Public" ); } cmd = conf_string( conf, act->cmd ); cmd = parse_template( cmd, data ); g_message("Got command %s from action %s", cmd, act->cmd ); arg = g_strsplit( cmd, " ", 0 ); // prime the environment with our existing environment env = g_ptr_array_new(); for ( n = environ; *n != NULL; n++ ) g_ptr_array_add( env, *n ); // Then add everything from the conf file g_hash_table_foreach( data, (GHFunc) fw_exec_add_env, env ); // Add a closing NULL so execve knows where to lay off. g_ptr_array_add( env, NULL ); /* We're not cleaning up memory references because * hopefully the exec won't fail... */ execve( *arg, arg, (char **)env->pdata ); g_error( "execve %s failed: %m", cmd ); // Shouldn't happen. return -1; }
apr_status_t http_send_custom_response(nproxy_connection_t * conn, http_resp_codes_t error_code) { size_t t; int clen; int e_code; char *tpl = NULL; char *e_msg = NULL; char *e_text = NULL; char *e_detail = NULL; char *payload = NULL; char found = 0; char timebuf[APR_RFC822_DATE_LEN]; char *additional_headers = ""; const char *tmp = NULL; nproxy_connection_side_t *side; apr_status_t rv; apr_size_t len; const char *fallback_error = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" "<html>\n" "<head><title>%d %s</title></head>\n" "<body>\n" "<h1>%d %s</h1>\n" "<p>%s</p>\n" "<hr />\n" "<p><em>Generated by NProxy</em></p>\n" "</body>\n" "</html>\n"; side = conn->browser; apr_rfc822_date(timebuf, apr_time_now()); for (t = 0; t < sizeof(ec) / sizeof(ec[0]); t++) { if (error_code == ec[t].code) { e_code = ec[t].number; e_msg = ec[t].string; found = 1; break; } } if (!found) { e_msg = "Error code string not found."; e_code = error_code; } e_detail = "No details, sorry."; if ((tmp = nproxy_connection_get_variable(conn, "additional_headers"))) { if (!zstr(tmp)) { additional_headers = (char *) tmp; } } if ((tmp = nproxy_connection_get_variable(conn, "custom_cause"))) { if (!zstr(tmp)) { e_msg = (char *) tmp; } } if ((tmp = nproxy_connection_get_variable(conn, "custom_detail"))) { if (!zstr(tmp)) { e_detail = (char *) tmp; } } tmp = nproxy_connection_get_variable(conn, "custom_template"); tpl = find_template(conn, e_code, tmp); if (tpl != NULL) { if (conn->requests != NULL) { nproxy_connection_set_variable(conn, "req_method", conn->requests->method); nproxy_connection_set_variable(conn, "req_host", conn->requests->host); nproxy_connection_set_variable(conn, "req_port", apr_psprintf(conn->pool, "%d", conn->requests->port)); nproxy_connection_set_variable(conn, "req_url", conn->requests->url); } nproxy_connection_set_variable(conn, "resp_errno", apr_psprintf(conn->pool, "%d", e_code)); nproxy_connection_set_variable(conn, "resp_cause", e_msg); nproxy_connection_set_variable(conn, "resp_detail", e_detail); nproxy_connection_set_variable(conn, "package", "NProxy"); e_text = parse_template(conn, tpl); } if (zstr(e_text)) { e_text = apr_psprintf(conn->pool, fallback_error, e_code, e_msg, e_code, e_msg, e_detail); } clen = strlen(e_text); payload = apr_psprintf(conn->pool, "%s %d %s\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" "Proxy-Connection: close\r\n" "Content-Length: %d\r\n" "Date: %s\r\n" "%s" "\r\n" "%s", "HTTP/1.0", e_code, e_msg, clen, timebuf, additional_headers, e_text); len = strlen(payload); rv = apr_socket_send(side->sock, payload, &len); if (rv == APR_STATUS_SUCCESS) { if (conn->requests != NULL) { conn->requests->bytes_in += len; conn->requests->response_headers.resp_code = error_code; } } else { rv = APR_STATUS_ERROR; } return rv; }
static void start_element (GMarkupParseContext *context, const gchar *element_name, const gchar **names, const gchar **values, gpointer user_data, GError **error) { ParserData *data = (ParserData*)user_data; #ifdef G_ENABLE_DEBUG if (GTK_DEBUG_CHECK (BUILDER)) { GString *tags = g_string_new (""); int i; for (i = 0; names[i]; i++) g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]); if (i) { g_string_insert_c (tags, 0, ' '); g_string_truncate (tags, tags->len - 1); } g_message ("<%s%s>", element_name, tags->str); g_string_free (tags, TRUE); } #endif if (!data->last_element && strcmp (element_name, "interface") != 0) { error_unhandled_tag (data, element_name, error); return; } data->last_element = element_name; if (data->subparser) { if (!subparser_start (context, element_name, names, values, data, error)) return; } if (strcmp (element_name, "requires") == 0) parse_requires (data, element_name, names, values, error); else if (strcmp (element_name, "object") == 0) parse_object (context, data, element_name, names, values, error); else if (strcmp (element_name, "template") == 0) parse_template (context, data, element_name, names, values, error); else if (data->requested_objects && !data->inside_requested_object) { /* If outside a requested object, simply ignore this tag */ } else if (strcmp (element_name, "child") == 0) parse_child (data, element_name, names, values, error); else if (strcmp (element_name, "property") == 0) parse_property (data, element_name, names, values, error); else if (strcmp (element_name, "signal") == 0) parse_signal (data, element_name, names, values, error); else if (strcmp (element_name, "interface") == 0) parse_interface (data, element_name, names, values, error); else if (strcmp (element_name, "menu") == 0) _gtk_builder_menu_start (data, element_name, names, values, error); else if (strcmp (element_name, "placeholder") == 0) { /* placeholder has no special treatmeant, but it needs an * if clause to avoid an error below. */ } else if (!parse_custom (context, element_name, names, values, data, error)) error_unhandled_tag (data, element_name, error); }
void output_templates(std::string name, std::string base, bool constructable) { std::locale loc; std::ifstream input; std::ofstream output; // construct the symbol map symbol_map symbols; symbols["NAME"] = name; symbols["BASE"] = base; symbols["NAME_UPPER"] = conv_str_case<upper>(name, loc); symbols["NAME_LOWER"] = conv_str_case<lower>(name, loc); symbols["BASE_UPPER"] = conv_str_case<upper>(base, loc); symbols["BASE_LOWER"] = conv_str_case<lower>(base, loc); symbols["H_EXT"] = "h"; symbols["SRC_EXT"] = "cpp"; symbols["DATE"] = ""; if( constructable ) symbols["CONSTRUCTABLE"] = ""; // build input and output file vector template_files_vector files; files.push_back(template_file("templates/gui.h", string_pair("include/cpaf/gui/", "." + symbols["H_EXT"]))); files.push_back(template_file("templates/initializer.h", string_pair("include/cpaf/gui/initializer/", "." + symbols["H_EXT"]))); files.push_back(template_file("templates/initializer.cpp", string_pair("src/cpaf/gui/initializer/", "." + symbols["SRC_EXT"]))); files.push_back(template_file("templates/api.h", string_pair("include/cpaf/api/gui/", "." + symbols["H_EXT"]))); files.push_back(template_file("templates/gui.cpp", string_pair("src/cpaf/gui/", "." + symbols["SRC_EXT"]))); // parse and output template files for( template_files_vector::iterator i = files.begin(), end = files.end(); i != end; ++i ) { parse_template(i, input, output, symbols); } typedef std::vector<std::string> str_vector; str_vector ports; ports.push_back("win32"); ports.push_back("gtk2"); ports.push_back("cocoa"); for( str_vector::iterator i = ports.begin(), end = ports.end(); i != end; ++i ) { std::string port = *i; symbols["PORT"] = port; symbols["PORT_UPPER"] = conv_str_case<upper>(port, loc); symbols["PORT_LOWER"] = conv_str_case<lower>(port, loc); if( symbols["PORT_LOWER"] == "cocoa" ) symbols["IMPL_SRC_EXT"] = "mm"; else symbols["IMPL_SRC_EXT"] = "cpp"; files.clear(); files.push_back(template_file("templates/impl.h", string_pair("include/cpaf/" + symbols["PORT_LOWER"] + "/gui/", "." + symbols["H_EXT"]))); files.push_back(template_file("templates/impl.cpp", string_pair("src/cpaf/" + symbols["PORT_LOWER"] + "/", "." + symbols["IMPL_SRC_EXT"]))); // parse and output template files for( template_files_vector::iterator i = files.begin(), end = files.end(); i != end; ++i ) { parse_template(i, input, output, symbols); } } }
/** * @short Compiles the infilename to outfilename. */ int work(const char *infilename, const char *outfilename, onion_assets_file *assets){ tag_init(); parser_status status; memset(&status, 0, sizeof(status)); status.mode=TEXT; status.functions=list_new((void*)function_free); status.function_stack=list_new(NULL); status.status=0; status.line=1; status.rawblock=onion_block_new(); status.infilename=infilename; char tmp2[256]; strncpy(tmp2, infilename, sizeof(tmp2)-1); const char *tname=basename(tmp2); ONION_DEBUG("Create init function on top, tname %s",tname); status.blocks_init=function_new(&status, "%s_blocks_init", tname); status.blocks_init->signature="onion_dict *context"; if (strcmp(infilename, "-")==0) status.in=stdin; else status.in=fopen(infilename,"rt"); if (!status.in){ ONION_ERROR("Could not open in file %s", infilename); goto work_end; } ONION_DEBUG("Create main function on top, tname %s",tname); function_new(&status, tname); function_add_code(&status, " int has_context=(context!=NULL);\n" " if (!has_context)\n" " context=onion_dict_new();\n" " \n" " %s(context);\n", status.blocks_init->id); parse_template(&status); ((function_data*)status.function_stack->tail->data)->flags=0; function_add_code(&status, " if (!has_context)\n" " onion_dict_free(context);\n" ); if (status.status){ ONION_ERROR("Parsing error"); goto work_end; } if (strcmp(outfilename, "-")==0) status.out=stdout; else status.out=fopen(outfilename,"wt"); if (!status.out){ ONION_ERROR("Could not open out file %s", infilename); goto work_end; } fprintf(status.out, "/** Autogenerated by otemplate v. 0.2.0 */\n" "\n" "#include <libintl.h>\n" "#include <string.h>\n\n" "#include <onion/onion.h>\n" "#include <onion/dict.h>\n" "\n" "typedef struct dict_res_t{\n" " onion_dict *dict;\n" " onion_response *res;\n" "}dict_res;\n" "\n" "\n"); functions_write_declarations_assets(&status, assets); functions_write_declarations(&status); functions_write_main_code(&status); if (use_orig_line_numbers) fprintf(status.out, "#line 1 \"%s\"\n", infilename); functions_write_code(&status); work_end: if (status.in) fclose(status.in); if (status.out) fclose(status.out); list_free(status.functions); list_free(status.function_stack); //list_free(status.blocks); onion_block_free(status.rawblock); tag_free(); return status.status; }
int main (int argc, char **argv) { GHashTable *z = read_conf_file( "nocat.conf" ); gchar *out = parse_template( argv[1], z ); puts( out ); }
/** * 显示登录页面以及处理用户登录 */ int login_process() { char* username = NULL; char* password = NULL; char* buffer = NULL; int field_buffer_size = 0; REPLACEABLE_TAG_LIST tag_list = (REPLACEABLE_TAG_LIST) malloc( sizeof(REPLACEABLE_TAG) * 2); // 获取用户提交的用户名和密码,如果没有提交数据,则 username 和 password 为空字符串 cgiFormStringSpaceNeeded("username", &field_buffer_size); username = (char*) malloc(field_buffer_size); memset(username, 0, field_buffer_size); cgiFormString("username", username, field_buffer_size); cgiFormStringSpaceNeeded("password", &field_buffer_size); password = (char*) malloc(field_buffer_size); cgiFormString("password", password, field_buffer_size); if (strcmp(username, g_login_username) == 0 && strcmp(password, g_login_password) == 0) { // 登录验证通过,设置 session 数据后重定向浏览器 session_set("USERNAME", g_login_username); session_write_close(); cgiHeaderLocation(cgiScriptName); free(username); free(password); return 0; } // 输出 ContentType header // 作为 CGI 程序必须输出这个 header,否则会导致 500 错误 fprintf(cgiOut, "Pragma: no-cache\n"); cgiHeaderContentType("text/html"); // 显示登录页面 tag_list[0].tag = "{%ERROR_MESSAGE%}"; if (strlen(username) != 0 || strlen(password) != 0) { tag_list[0].value = "您输入的用户名或者密码错误。"; } else { tag_list[0].value = ""; } tag_list[1].tag = "{%USERNAME%}"; tag_list[1].value = username; buffer = read_template(g_login_template); if (buffer == NULL) { fprintf(cgiOut, "Warning: Can't read template file: %s.<br />\n", g_login_template); free(username); free(password); return -1; } parse_template(&buffer, tag_list, 2); fprintf(cgiOut, "%s", buffer); free(buffer); free(username); free(password); // session_destroy(); return 0; }
static gboolean parse_service (xmlTextReaderPtr reader, AgService *service) { const gchar *name; int ret, type; if (!service->name) { xmlChar *_name = xmlTextReaderGetAttribute (reader, (xmlChar *) "id"); service->name = g_strdup ((const gchar *)_name); if (_name) xmlFree(_name); } ret = xmlTextReaderRead (reader); while (ret == 1) { name = (const gchar *)xmlTextReaderConstName (reader); if (G_UNLIKELY (!name)) return FALSE; type = xmlTextReaderNodeType (reader); if (type == XML_READER_TYPE_END_ELEMENT && strcmp (name, "service") == 0) break; if (type == XML_READER_TYPE_ELEMENT) { gboolean ok; if (strcmp (name, "type") == 0 && !service->type) { ok = _ag_xml_dup_element_data (reader, &service->type); } else if (strcmp (name, "name") == 0 && !service->display_name) { ok = _ag_xml_dup_element_data (reader, &service->display_name); } else if (strcmp (name, "description") == 0) { ok = _ag_xml_dup_element_data (reader, &service->description); } else if (strcmp (name, "provider") == 0 && !service->provider) { ok = _ag_xml_dup_element_data (reader, &service->provider); } else if (strcmp (name, "icon") == 0) { ok = _ag_xml_dup_element_data (reader, &service->icon_name); } else if (strcmp (name, "translations") == 0) { ok = _ag_xml_dup_element_data (reader, &service->i18n_domain); } else if (strcmp (name, "template") == 0) { ok = parse_template (reader, service); } else if (strcmp (name, "preview") == 0) { ok = parse_preview (reader, service); } else if (strcmp (name, "type_data") == 0) { static const gchar element[] = "<type_data"; gsize offset; /* find the offset in the file where this element begins */ offset = xmlTextReaderByteConsumed(reader); while (offset > 0) { if (strncmp (service->file_data + offset, element, sizeof (element)) == 0) { service->type_data_offset = offset; break; } offset--; } /* this element is placed after all the elements we are * interested in: we can stop the parsing now */ return TRUE; } else if (strcmp (name, "tags") == 0) { ok = _ag_xml_parse_element_list (reader, "tag", &service->tags); } else ok = TRUE; if (G_UNLIKELY (!ok)) return FALSE; } ret = xmlTextReaderNext (reader); } return TRUE; }