void parse_selector(const char* str, string& enm, string& snm) { enm.clear(); snm.clear(); if (!str || !strlen(str) || strlen(str) <= 3) { return; } if ( str[0] == '#' && str[1] == '(' && str[strlen(str)-1] == ')') { const char* p = str + 2, *e = str + strlen(str) - 1; string tk; while ( p != e ) { if ( enm.length() == 0 && *p == ':' ) { if (tk.length()) { if ( !is_valid_identifier(tk.c_str()) ) { return; } else { enm = tk; tk.clear(); } } else { return; } } else { tk.push_back(*p); } p++; } if ( tk.length() ) { snm = tk; if ( !is_valid_identifier(snm.c_str())) { enm.clear(); snm.clear(); return; } else { return; } } else { enm.clear(); return; } } else { enm.clear(); snm.clear(); return; } }
bool is_call(const char* str, string& procnm) { if ( !str || strlen(str) < strlen("$call()") ) return false; if ( 0 == strncmp(str, "$call(", strlen("$call(")) ) { if ( str[strlen(str) - 1] != ')') { return false; } else { string tmp(str); tmp = tmp.substr(strlen("$call(")); tmp = tmp.substr(0, tmp.size() - 1); if ( is_valid_identifier(tmp.c_str())) { procnm = tmp; } else { return false; } } } else { return false; } return true; }
TokenType token_type(const string &str) { if (is_valid_operator(str)) return operator_symbol; if (is_valid_operator_as_function(str)) return op_function; if (is_valid_identifier(str)) return plain_identifier; if (is_valid_symbol(str)) return symbol; if (is_valid_label(str)) return label; if (is_valid_number(str)) return number; if (is_valid_type_name(str)) return type_id; if (is_valid_type_var(str)) return type_var; if (is_valid_builtin(str)) return builtin; if (is_valid_string_lit(str)) return string_lit; return invalid; }
void parse_macros(const char* str, hash_multimap<string, int>& idxes, vector<string>& segments) { if ( !str || !strlen(str) ) { idxes.clear(); segments.clear(); return; } string v (str); //string cur_token; size_t pend_pos = 0, chk_pos = 0; size_t ps1,ps2; do { ps1 = v.find("#(", chk_pos); if( ps1 != string::npos ) { ps2 = v.find(")", ps1 + 2); if (ps2 != string::npos) { if ( ps1 + 2 == ps2 ) { chk_pos = ps2 + 1; } else { string nm = v.substr(ps1 + 2, ps2 - ps1 - 2 ); if ( is_valid_identifier(nm.c_str()) ) { if (pend_pos < ps1) { segments.push_back(v.substr(pend_pos, ps1 - pend_pos)); } chk_pos = pend_pos = ps2 + 1; segments.push_back(nm); idxes.insert(make_pair(nm, segments.size() -1 )); //idxes[nm] = segments.size() - 1; } else { chk_pos = ps2 + 1; } } } else { //chk_pos = cur_token.size(); chk_pos = v.size(); } } else { //chk_pos = cur_token.size(); chk_pos = v.size(); } if (chk_pos == v.size()) { if ( pend_pos < chk_pos ) { segments.push_back(v.substr(pend_pos, chk_pos - 1)); } break; } } while(1); }
bool is_valid_label(const string &str) { int len = str.length(); if (len <= 1 || str[len-1] != ':') return false; string s = str.substr(0, len-1); return is_valid_identifier(s); }
void zpl_add_parameter(const char* def) { const char* warning = "--- Warning 175: Illegal syntax for command line define \"%s\" -- ignored\n"; Set* set; Symbol* sym; Numb* numb; Tuple* tuple; Entry* entry; char* name; char* value; assert(def != NULL); name = strdup(def); value = strchr(name, '='); if (value == NULL) { fprintf(stderr, warning, def); free(name); return; } *value = '\0'; value++; if (strlen(name) == 0 || strlen(value) == 0 || !is_valid_identifier(name)) { if (verbose > VERB_QUIET) fprintf(stderr, warning, def); free(name); return; } set = set_pseudo_new(); sym = symbol_new(str_new(name), SYM_ERR, set, 1, ENTRY_NULL); tuple = tuple_new(0); if (!numb_is_number(value)) entry = entry_new_strg(tuple, str_new(value)); else { numb = numb_new_ascii(value); entry = entry_new_numb(tuple, numb); numb_free(numb); } symbol_add_entry(sym, entry); tuple_free(tuple); set_free(set); free(name); }
bool is_valid_symbol(const string &s) { return s.length() > 1 && s[0] == ':' && is_valid_identifier(s.substr(1)); }
int main(int argc, char *argv[]) { const char *progname = argv[0]; struct ndn_ping_client client = {.identifier = 0, .interval = 1, .sent = 0, .received = 0, .total = -1, .number = -1, .print_timestamp = 0, .allow_caching = 0}; struct ndn_closure in_content = {.p = &incoming_content}; struct hashtb_param param = {0}; int res; struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &handle_interrupt; sigaction(SIGINT, &sa, &osa); memset(&sta, 0, sizeof(sta)); gettimeofday(&sta.start, 0); sta.min = INT_MAX; while ((res = getopt(argc, argv, "htai:c:n:p:")) != -1) { switch (res) { case 'c': client.total = atol(optarg); if (client.total <= 0) usage(progname); break; case 'i': client.interval = atof(optarg); if (client.interval < PING_MIN_INTERVAL) usage(progname); break; case 'n': client.number = atol(optarg); if (client.number < 0) usage(progname); break; case 'p': client.identifier = optarg; if (!is_valid_identifier(client.identifier)) usage(progname); break; case 't': client.print_timestamp = 1; break; case 'a': client.allow_caching = 1; break; case 'h': default: usage(progname); break; } } if (client.number < 0) srandom(time(NULL) * getpid()); argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); sta.prefix = argv[0]; client.original_prefix = argv[0]; client.prefix = ndn_charbuf_create(); res = ndn_name_from_uri(client.prefix, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ndn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); // Append "/ping" to the given name prefix. res = ndn_name_append_str(client.prefix, PING_COMPONENT); if (res < 0) { fprintf(stderr, "%s: error constructing ndn URI: %s/%s\n", progname, argv[0], PING_COMPONENT); exit(1); } // Append identifier if not empty. if (client.identifier) { res = ndn_name_append_str(client.prefix, client.identifier); if (res < 0) { fprintf(stderr, "%s: error constructing ndn URI: %s/%s/%s\n", progname, argv[0], PING_COMPONENT, client.identifier); exit(1); } } // Connect to ndnd. client.h = ndn_create(); if (ndn_connect(client.h, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } client.closure = &in_content; in_content.data = &client; client.ndn_ping_table = hashtb_create(sizeof(struct ndn_ping_entry), ¶m); client.sched = ndn_schedule_create(&client, &ndn_ping_ticker); client.event = ndn_schedule_event(client.sched, 0, &do_ping, NULL, 0); print_log(client.print_timestamp, "NDNPING %s\n", client.original_prefix); res = 0; while (res >= 0 && (client.total <= 0 || client.sent < client.total || hashtb_n(client.ndn_ping_table) > 0)) { if (client.total <= 0 || client.sent < client.total) ndn_schedule_run(client.sched); res = ndn_run(client.h, 10); } ndn_schedule_destroy(&client.sched); ndn_destroy(&client.h); ndn_charbuf_destroy(&client.prefix); print_statistics(); return 0; }