range* do_range_expand(range_request* rr, const char* text) { yyscan_t scanner; struct range_extras extra; int result; if (text == NULL) { range* r = range_new(rr); range_request_set(rr, r); return r; } current_rr = rr; extra.rr = rr; yylex_init(&scanner); yyset_extra(&extra, scanner); yy_scan_string(text, scanner); result = yyparse(scanner); yylex_destroy(scanner); current_rr = NULL; if (result != 0) { range* r = range_new(rr); range_request_warn(rr, "parsing [%s]", text); range_request_set(rr, r); return r; } range_request_set(rr, range_evaluate(rr, extra.theast)); return range_request_results(rr); }
static range* _expand_cluster(range_request* rr, const char* cluster, const char* section) { struct stat st; const char* res; libcrange* lr = range_request_lr(rr); set* cache = libcrange_get_cache(lr, "nodescf:cluster_keys"); apr_pool_t* req_pool = range_request_pool(rr); apr_pool_t* lr_pool = range_request_lr_pool(rr); const char* cluster_file; cache_entry* e; if (strcmp(section, "VIPS") == 0) return _cluster_vips(rr, cluster); if (strcmp(section, "VIPHOSTS") == 0) return _cluster_viphosts(rr, cluster); cluster_file = apr_psprintf(req_pool, "%s/%s/nodes.cf", nodescf_path, cluster); if (!cache) { cache = set_new(lr_pool, 0); libcrange_set_cache(lr, "nodescf:cluster_keys", cache); } if (stat(cluster_file, &st) == -1) { range_request_warn_type(rr, "NOCLUSTERDEF", cluster); return range_new(rr); } e = set_get_data(cache, cluster_file); if (!e) { e = apr_palloc(lr_pool, sizeof(struct cache_entry)); apr_pool_create(&e->pool, lr_pool); e->sections = _cluster_keys(rr, e->pool, cluster, cluster_file); e->mtime = st.st_mtime; set_add(cache, cluster_file, e); } else { time_t cached_mtime = e->mtime; if (cached_mtime != st.st_mtime) { apr_pool_clear(e->pool); e->sections = _cluster_keys(rr, e->pool, cluster, cluster_file); e->mtime = st.st_mtime; } } res = set_get_data(e->sections, section); if (!res) { char* cluster_section = apr_psprintf(req_pool, "%s:%s", cluster, section); range_request_warn_type(rr, "NOCLUSTER", cluster_section); return range_new(rr); } return do_range_expand(rr, res); }
//*********************my_atk_text_set_run_attributes************************ gboolean my_atk_text_set_run_attributes(AtkEditableText* text, AtkAttributeSet* attrib_set, gint start_offset, gint end_offset) { MyAtkText* self = (MyAtkText*)text; gint len = atk_text_get_character_count((AtkText*)text); if(start_offset < 0 || start_offset >= end_offset || end_offset > len) return FALSE; GList *attributes = self->attributes; GList *tmp = attributes; while(tmp != NULL) { Range *range = (Range*)tmp->data; if(range->start < start_offset) { if(range->end <= end_offset) { if(range->end > start_offset) range->end = start_offset; tmp = tmp->next; continue; } /*range->end > end_offset*/ Range* additional_range = range_new(end_offset, range->end); additional_range->attributeSet = attribute_set_copy(range->attributeSet); range->end = start_offset; attributes = g_list_insert_before(attributes, tmp->next, additional_range); tmp = tmp->next; break; } else/*range->start >= start_offset*/ { if(range->end <= end_offset) { GList *tmp1 = tmp->next; attributes = g_list_remove_link(attributes, tmp); tmp = tmp1; continue; } /*range->end > end_offset*/ if(range->start < end_offset) range->start = end_offset; break; } } Range *new_range = range_new(start_offset, end_offset); new_range->attributeSet = attribute_set_copy(attrib_set); if(tmp == NULL)attributes = g_list_append(attributes, new_range); else attributes = g_list_insert_before(attributes, tmp, new_range); self->attributes = attributes; g_signal_emit_by_name(self, "text_attributes_changed"); return TRUE; }
range* range_from_match(range_request* rr, const range* r, const char* regex) { range* ret; int i; int err_offset; int ovector[30]; int count; const char* error; const char** members; pcre* re; apr_pool_t* pool = range_request_pool(rr); members = range_get_hostnames(pool, r); ret = range_new(rr); re = pcre_compile(regex, 0, &error, &err_offset, NULL); if (!re) { range_request_warn(rr, "regex [%s] [%s]", regex, error); return ret; } for (i = 0; members[i]; i++) { count = pcre_exec(re, NULL, members[i], strlen(members[i]), 0, 0, ovector, 30); if (count > 0) /* it matched */ range_add(ret, members[i]); } pcre_free(re); return ret; }
range* rangefunc_has(range_request* rr, range** r) { range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** tag_names = range_get_hostnames(pool, r[0]); const char** tag_values = range_get_hostnames(pool, r[1]); const char* tag_name = tag_names[0]; const char* tag_value = tag_values[0]; const char** all_clusters = _all_clusters(rr); const char** cluster = all_clusters; int warn_enabled = range_request_warn_enabled(rr); if (!cluster) return ret; range_request_disable_warns(rr); while (*cluster) { range* vals = _expand_cluster(rr, *cluster, tag_name); if (set_get(vals->nodes, tag_value) != NULL) { range_add(ret, *cluster); } cluster++; } if (warn_enabled) range_request_enable_warns(rr); return ret; }
range* rangefunc_mem(range_request* rr, range** r) { range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** clusters = range_get_hostnames(pool, r[0]); const char* cluster = clusters[0]; const char** wanted = range_get_hostnames(pool, r[1]); range* keys = _expand_cluster(rr, cluster, "KEYS"); const char** all_sections = range_get_hostnames(pool, keys); const char** p_section = all_sections; SECTION: while (*p_section) { range* r_s = _expand_cluster(rr, cluster, *p_section); const char** p_wanted = wanted; while (*p_wanted) { if (set_get(r_s->nodes, *p_wanted) != NULL) { range_add(ret, *p_section++); goto SECTION; } ++p_wanted; } ++p_section; } return ret; }
static range* _expand_cluster(range_request* rr, const char* cluster, const char* section) { set * ret_set; MDBM * db = _open_mdbm(rr); apr_pool_t* req_pool = range_request_pool(rr); // return a range * of the section // build the key:val strncpy(fetch_key, cluster, MAX_CLUSTER_STRING); strncat(fetch_key, ":", MAX_CLUSTER_STRING); strncat(fetch_key, section, MAX_CLUSTER_STRING); // first, query the data from mdbm datum val; datum key; key.dptr = fetch_key; key.dsize = strlen(fetch_key); val = mdbm_fetch(db, key); if (val.dsize) { ret_set = set_unpack(req_pool, val.dptr); return range_from_set(rr, ret_set); } else { /* FIXME warn -- ok fixed*/ range_request_warn_type(rr, "NOCLUSTER", fetch_key); return range_new(rr); } }
range* rangefunc_clusters(range_request* rr, range** r) { range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** nodes = range_get_hostnames(pool, r[0]); const char** p_nodes = nodes; set* node_cluster = _get_clusters(rr); while (*p_nodes) { apr_array_header_t* clusters = set_get_data(node_cluster, *p_nodes); if (!clusters) range_request_warn_type(rr, "NO_CLUSTER", *p_nodes); else { /* get all */ int i; for (i=0; i<clusters->nelts; ++i) { const char* cluster = ((const char**)clusters->elts)[i]; range_add(ret, cluster); } } ++p_nodes; } return ret; }
range* range_from_function(range_request* rr, const char* funcname, const range** r) { range* returned_ret; range* passed_ret; range* (*f)(range_request*, const range**); const char* plugin_module; libcrange* lr = range_request_lr(rr); // is this a perl function? plugin_module = libcrange_get_perl_module(lr, funcname); if (plugin_module) { return perl_function(rr, funcname, r); } // or python? plugin_module = libcrange_get_python_module(lr, funcname); if (plugin_module) { returned_ret = python_function(rr, funcname, r, &passed_ret); return passed_ret; } // or C? f = libcrange_get_function(lr, funcname); if (!f) { range_request_warn_type(rr, "NO_FUNCTION", funcname); return range_new(rr); } return (*f)(rr, r); }
range* rangefunc_clusters(range_request* rr, range** r) { sqlite3* db; sqlite3_stmt* stmt; int err; range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** nodes = range_get_hostnames(pool, r[0]); const char** p_nodes = nodes; db = _open_db(rr); err = sqlite3_prepare(db, CLUSTERS_SQL, strlen(CLUSTERS_SQL), &stmt, NULL); if (err != SQLITE_OK) { range_request_warn(rr, "clusters(): cannot query sqlite db"); return ret; } while (*p_nodes) { char * node_name = *p_nodes; sqlite3_bind_text(stmt, 1, node_name, strlen(node_name), SQLITE_STATIC); while(sqlite3_step(stmt) == SQLITE_ROW) { const char* answer = (const char*)sqlite3_column_text(stmt, 0); range_add(ret, answer); } sqlite3_reset(stmt); sqlite3_clear_bindings(stmt); ++p_nodes; } sqlite3_finalize(stmt); return ret; }
range* range_from_literal(range_request* rr, const char* literal) { range* r = range_new(rr); range_add(r, literal); return r; }
range* rangefunc_get_cluster(range_request* rr, range** r) { range* ret = range_new(rr); if (!validate_range_args(rr, r, 1)) { return ret; } apr_pool_t* pool = range_request_lr_pool(rr); const char** nodes = range_get_hostnames(pool, r[0]); const char** p_nodes = nodes; set* node_cluster = _get_clusters(rr); while (*p_nodes) { apr_array_header_t* clusters = set_get_data(node_cluster, *p_nodes); if (!clusters) range_request_warn_type(rr, "NO_CLUSTER", *p_nodes); else { /* just get one */ const char* cluster = ((const char**)clusters->elts)[0]; assert(cluster); range_add(ret, cluster); } ++p_nodes; } return ret; }
range* rangefunc_get_groups(range_request* rr, range** r) { sqlite3* db; sqlite3_stmt* stmt; int err; range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** tag_names = range_get_hostnames(pool, r[0]); const char* tag_name = tag_names[0]; int warn_enabled = range_request_warn_enabled(rr); if (NULL == tag_name) { return ret; } db = _open_db(rr); err = sqlite3_prepare(db, GROUPS_SQL, strlen(GROUPS_SQL), &stmt, NULL); if (err != SQLITE_OK) { range_request_warn(rr, "?%s: cannot query sqlite db", tag_name ); return ret; } assert(err == SQLITE_OK); sqlite3_bind_text(stmt, 1, tag_name, strlen(tag_name), SQLITE_STATIC); while(sqlite3_step(stmt) == SQLITE_ROW) { const char* answer = (const char*)sqlite3_column_text(stmt, 0); range_add(ret, answer); } sqlite3_finalize(stmt); return ret; }
static range_t *range_merge(range_t *r1, range_t *r2) { // Note: this assumes positive values all around, no modulo range_t *out = NULL; int edge, overlap; if (r1->x >= r2->x) { edge = r1->x - r1->w; if (edge > r2->x) { // no overlap: // r1----> // --|-----|-----|-----| // r2----> return NULL; } // r1 overlaps onto r2: // r1----> // --|##|##|#####| // r2-------> out = range_new(); overlap = r2->x - edge; range_set_value(out, r1->x, r1->w + r2->w - overlap); return out; } edge = r2->x - r2->w; if (edge > r1->x) { // no overlap: // r1----> // -----|-----|----|----| // r2----> // return NULL; } // r2 overlaps onto r1: // r2----> // --|##|##|#####| // r1-------> out = range_new(); overlap = r1->x - edge; range_set_value(out, r2->x, r1->w + r2->w - overlap); return out; }
range* range_from_diff(range_request* rr, const range* r1, const range* r2) { range* r3 = range_new(rr); apr_pool_t* pool = range_request_pool(rr); r3->nodes = set_diff(pool, r1->nodes, r2->nodes); r3->quoted = r1->quoted || r2->quoted; return r3; }
static gpointer range_dup(gconstpointer data) { const range_t *org = (const range_t *)data; range_t *range; range = (range_t *)range_new(NULL); range->entity = stnode_dup(org->entity); range->drange = drange_dup(org->drange); return (gpointer) range; }
range* range_from_hostnames(range_request* rr, const char** names) { range* r; int i; r = range_new(rr); for(i = 0; names[i]; i++) range_add(r, names[i]); return r; }
range* rangefunc_group(range_request* rr, range** r) { range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); const char** groups = range_get_hostnames(pool, r[0]); while (*groups) { range_union_inplace(rr, ret, _expand_cluster(rr, "GROUPS", *groups)); ++groups; } return ret; }
range* range_from_braces(range_request* rr, const range* r1, const range* r2, const range* r3) { int i, j, k; set_element** m1; set_element** m2; set_element** m3; set* temp = NULL; range* bigrange; char* bundle; apr_pool_t* pool = range_request_pool(rr); if(r1->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m1 = set_members(temp); } else m1 = set_members(r1->nodes); if(r2->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m2 = set_members(temp); } else m2 = set_members(r2->nodes); if(r3->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m3 = set_members(temp); } else m3 = set_members(r3->nodes); bigrange = range_new(rr); for(i = 0; m1[i]; i++) for(j = 0; m2[j]; j++) for(k = 0; m3[k]; k++) { bundle = apr_pstrcat(pool, m1[i]->name, m2[j]->name, m3[k]->name, NULL); range_add(bigrange, bundle); } if (temp) set_destroy(temp); bigrange->quoted = r1->quoted || r2->quoted || r3->quoted; return bigrange; }
range* _do_has_mem(range_request* rr, range** r, char* sql_query) { sqlite3* db; sqlite3_stmt* stmt; int err; range* ret = range_new(rr); apr_pool_t* pool = range_request_pool(rr); if (NULL == r[0]) { // don't attempt anything without arg #1 (key) return ret; } const char** tag_names = range_get_hostnames(pool, r[0]); const char* tag_name = tag_names[0]; if (NULL == tag_name) { return ret; } const char** tag_values; const char* tag_value; if (NULL == r[1]) { // if we don't have arg #2 (val) then search for keys with empty string value tag_value = EMPTY_STRING; } else { tag_values = range_get_hostnames(pool, r[1]); tag_value = tag_values[0]; } const char** all_clusters = _all_clusters(rr); const char** cluster = all_clusters; int warn_enabled = range_request_warn_enabled(rr); db = _open_db(rr); err = sqlite3_prepare(db, sql_query, strlen(sql_query), &stmt, NULL); if (err != SQLITE_OK) { range_request_warn(rr, "has(%s,%s): cannot query sqlite db", tag_name, tag_value); return ret; } assert(err == SQLITE_OK); sqlite3_bind_text(stmt, 1, tag_name, strlen(tag_name), SQLITE_STATIC); sqlite3_bind_text(stmt, 2, tag_value, strlen(tag_value), SQLITE_STATIC); while(sqlite3_step(stmt) == SQLITE_ROW) { const char* answer = (const char*)sqlite3_column_text(stmt, 0); range_add(ret, answer); } sqlite3_finalize(stmt); return ret; }
range * rangefunc_group(range_request* rr, range** r) { sqlite3* db; db = _open_db(rr); range* ret = range_new(rr), *expanded; apr_pool_t* pool = range_request_pool(rr); const char** groups = range_get_hostnames(pool, r[0]); while (*groups) { expanded = _expand_cluster(rr, "GROUPS", *groups); range_union_inplace(rr, ret, expanded); ++groups; } return ret; }
static range* _python_function(range_request* rr, const char* funcname, const range** r) { range* result = range_new(rr); PyObject * pLibcrangeCallFunc; PyObject * pNodesReturned; PyObject * pMain = PyImport_AddModule("__main__"); // printf("rr: %p, funcname=%s, range**r = %p\n", rr, funcname, r); pLibcrangeCallFunc = PyObject_GetAttrString(pMain, "libcrange_call_func"); if (pLibcrangeCallFunc && PyCallable_Check(pLibcrangeCallFunc)) { PyObject * pRangeFuncName; PyObject * item; pRangeFuncName = PyString_FromString(funcname); PyObject * pFuncArgs; PyObject * pTempArgList = PyList_New(0); PyList_Append(pTempArgList, pRangeFuncName); Py_DECREF(pRangeFuncName); // build our range** into python function args const range** p_r = r; while (*p_r) { item = range_to_py_array(range_request_pool(rr), *p_r); PyList_Append(pTempArgList, item); Py_DECREF(item); p_r++; } pFuncArgs = PyList_AsTuple(pTempArgList); Py_DECREF(pTempArgList); // call the function pNodesReturned = PyObject_CallObject(pLibcrangeCallFunc, pFuncArgs); Py_DECREF(pFuncArgs); PyObject *iterator = PyObject_GetIter(pNodesReturned); if (iterator == NULL) { printf("ERROR: python function %s ran, but didn't return an iteratable object", funcname); return result; } // an iteratable object was returned, transform it into result while (item = PyIter_Next(iterator)) { // PyObject_Print(item, stdout, 0 ); range_add(result, PyString_AsString(item)); Py_DECREF(item); } Py_DECREF(pNodesReturned); } return result; }
src_map * src_map_new_submap(const src_map *map, const range *r) { size_t i; size_t first_item = -1; src_map *new_map = NULL; size_t remain_len = 0; if (!map || !map->size || !r || !r->len) return NULL; /* find first item */ for (i = 0; i < (size_t)map->size; ++i) { range *it = (range *)map->item[i]; if (r->loc >= it->loc && r->loc < it->loc + it->len) { first_item = i; break; } } if (first_item == (size_t)-1) return NULL; /* create new map */ new_map = src_map_new(); remain_len = r->len; for (i = first_item; i < (size_t)map->size; ++i) { range *it = (range *)map->item[i]; size_t add_loc = (i == first_item) ? r->loc : it->loc; size_t add_len = (it->len < remain_len) ? it->len : remain_len; range *add_range = range_new(add_loc, add_len); stack_push(new_map, add_range); remain_len -= add_len; if (remain_len <= 0) break; } return new_map; }
range* rangefunc_get_groups(range_request* rr, range** r) { range* ret = range_new(rr); if (!validate_range_args(rr, r, 1)) { return ret; } range* n = r[0]; apr_pool_t* pool = range_request_pool(rr); const char** in_nodes = range_get_hostnames(pool, n); set* node_group = set_new(pool, 40000); range* groups_r = _expand_cluster(rr, "GROUPS", "KEYS"); const char** groups = range_get_hostnames(pool, groups_r); const char** p_group = groups; while (*p_group) { range* nodes_r = _expand_cluster(rr, "GROUPS", *p_group); const char** nodes = range_get_hostnames(pool, nodes_r); const char** p_nodes = nodes; while (*p_nodes) { apr_array_header_t* my_groups = set_get_data(node_group, *p_nodes); if (!my_groups) { my_groups = apr_array_make(pool, 4, sizeof(char*)); set_add(node_group, *p_nodes, my_groups); } *(const char**)apr_array_push(my_groups) = *p_group; ++p_nodes; } ++p_group; } while (*in_nodes) { apr_array_header_t* my_groups = set_get_data(node_group, *in_nodes); if (!my_groups) range_request_warn_type(rr, "NO_GROUPS", *in_nodes); else { int i; for (i=0; i<my_groups->nelts; ++i) range_add(ret, ((const char**)my_groups->elts)[i]); } in_nodes++; } return ret; }
range* rangefunc_group(range_request* rr, range** r) { range* ret = range_new(rr); if (!validate_range_args(rr, r, 1)) { return ret; } apr_pool_t* pool = range_request_pool(rr); const char** groups = range_get_hostnames(pool, r[0]); while (*groups) { if (islower(*groups[0])) range_union_inplace(rr, ret, _expand_cluster(rr, "HOSTS", *groups)); else range_union_inplace(rr, ret, _expand_cluster(rr, "GROUPS", *groups)); ++groups; } return ret; }
range* rangefunc_allclusters(range_request* rr, range** r) { range* ret = range_new(rr); const char** all_clusters = _all_clusters(rr); const char** cluster = all_clusters; int warn_enabled = range_request_warn_enabled(rr); if (!cluster) return ret; range_request_disable_warns(rr); while (*cluster) { range_add(ret, *cluster); cluster++; } if (warn_enabled) range_request_enable_warns(rr); return ret; }
range* rangefunc_cluster(range_request* rr, range** r) { range* ret = range_new(rr); if (!validate_range_args(rr, r, 1)) { return ret; } apr_pool_t* pool = range_request_pool(rr); const char** clusters = range_get_hostnames(pool, r[0]); const char** p = clusters; while (*p) { const char* colon = strchr(*p, ':'); range* r1; if (colon) { int len = strlen(*p); int cluster_len = colon - *p; int section_len = len - cluster_len - 1; char* cl = apr_palloc(pool, cluster_len + 1); char* sec = apr_palloc(pool, section_len + 1); memcpy(cl, *p, cluster_len); cl[cluster_len] = '\0'; memcpy(sec, colon + 1, section_len); sec[section_len] = '\0'; r1 = _expand_cluster(rr, cl, sec); } else r1 = _expand_cluster(rr, *p, "CLUSTER"); if (range_members(r1) > range_members(ret)) { /* swap them */ range* tmp = r1; r1 = ret; ret = tmp; } range_union_inplace(rr, ret, r1); range_destroy(r1); ++p; } return ret; }
void range_request_warn_type(range_request* rr, const char* type, const char* node) { range* nodes; if (!rr->warn_enabled) return; if (!rr->warn_type) rr->warn_type = set_new(rr->pool, 0); /* nodes that generated a particular warning type */ nodes = set_get_data(rr->warn_type, type); if (!nodes) { nodes = range_new(rr); set_add(rr->warn_type, type, nodes); } range_add(nodes, node); }
/* create new src_map from index onward */ src_map * src_map_new_tail(const src_map *map, size_t index, size_t maxlen) { size_t i = 0; size_t cur = 0; size_t first_item = -1; size_t count = 0; src_map *new_map = NULL; if (!map || !map->size) return NULL; for (i = 0; i < (size_t)map->size; ++i) { range *it = (range *)map->item[i]; if (index < cur + it->len) { first_item = i; break; } cur += it->len; } if (first_item == (size_t)-1) return NULL; /* create new map */ new_map = src_map_new(); for (i = first_item; i < map->size && count < maxlen; ++i) { range *it = (range *)map->item[i]; range *add_range = range_new(it->loc, it->len); if (count + it->len > maxlen) { add_range->len = maxlen - count; } count += it->len; stack_push(new_map, add_range); } return new_map; }
/** * Add markup for the detected hyphen * @param u the userdata * @param text the current text after the hyphen * @param len its length */ static void process_hyphen( userdata *u, XML_Char *text, int len ) { XML_Char *next = first_word(text,len); if ( next != NULL && strlen(next)>0 ) { char *force = "weak"; XML_Char *last = userdata_last_word(u); XML_Char *combined = combine_words(last,next); if ( (userdata_has_word(u,last) && userdata_has_word(u,next) && combined!=NULL && (!userdata_has_word(u,combined) ||userdata_has_hh_exception(u,combined))) || (strlen(next)>0&&isupper(next[0])) ) { force = "strong"; //printf("strong: %s-%s\n",last,next); } //else // printf("weak: %s\n",combined); // create a range to describe a hard hyphen char **atts = calloc(1,sizeof(char*)); if ( atts != NULL ) { range *r = range_new( 0, force, atts, userdata_hoffset(u) ); if ( r != NULL ) { dest_file *df = userdata_get_markup_dest( u, force ); userdata_set_hyphen_state(u,HYPHEN_NONE); range_set_len( r, 1 ); dest_file_enqueue( df, r ); } } else fprintf(stderr,"stripper: failed to create hyphen range\n"); if ( combined != NULL ) free( combined ); } if ( next != NULL ) free( next ); }