static spif_bool_t spif_url_init_from_ipaddr(spif_url_t self, spif_ipsockaddr_t ipaddr) { spif_uint8_t tries; spif_hostinfo_t hinfo; ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); spif_str_init(SPIF_STR(self)); spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url)); self->proto = (spif_str_t) NULL; self->user = (spif_str_t) NULL; self->passwd = (spif_str_t) NULL; self->path = (spif_str_t) NULL; self->query = (spif_str_t) NULL; /* Try up to 3 times to resolve the hostname. */ h_errno = 0; tries = 0; do { tries++; hinfo = gethostbyaddr((const char *) &(ipaddr->sin_addr), sizeof(ipaddr->sin_addr), AF_INET); } while ((tries <= 3) && (!hinfo) && (h_errno == TRY_AGAIN)); if (!hinfo || !hinfo->h_name) { spif_charptr_t buff; buff = SPIF_CHARPTR(inet_ntoa(ipaddr->sin_addr)); self->host = spif_str_new_from_ptr(buff); } else { self->host = spif_str_new_from_ptr(SPIF_CHARPTR(hinfo->h_name)); } self->port = spif_str_new_from_num(ntohs(ipaddr->sin_port)); return TRUE; }
spif_bool_t spif_url_done(spif_url_t self) { ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); if (!SPIF_STR_ISNULL(self->proto)) { spif_str_del(self->proto); self->proto = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->user)) { spif_str_del(self->user); self->user = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->passwd)) { spif_str_del(self->passwd); self->passwd = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->host)) { spif_str_del(self->host); self->host = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->port)) { spif_str_del(self->port); self->port = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->path)) { spif_str_del(self->path); self->path = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->query)) { spif_str_del(self->query); self->query = (spif_str_t) NULL; } spif_str_done(SPIF_STR(self)); return TRUE; }
spif_tok_t spif_tok_dup(spif_tok_t self) { spif_tok_t tmp; ASSERT_RVAL(!SPIF_TOK_ISNULL(self), (spif_tok_t) NULL); tmp = spif_tok_new(); tmp->src = spif_str_dup(SPIF_STR(self->src)); tmp->quote = self->quote; tmp->dquote = self->dquote; tmp->escape = self->escape; tmp->tokens = SPIF_LIST_DUP(self->tokens); tmp->sep = spif_str_dup(SPIF_STR(self->sep)); return tmp; }
static spif_bool_t action_handle_string(event_t *ev, spif_eterm_action_t action) { REQUIRE_RVAL(!SPIF_PTR_ISNULL(ev), FALSE); REQUIRE_RVAL(!SPIF_STR_ISNULL(action->parameter), FALSE); cmd_write(SPIF_STR_STR(action->parameter), spif_str_get_len(SPIF_STR(action->parameter))); return 1; }
spif_url_t spif_url_dup(spif_url_t self) { spif_url_t tmp; ASSERT_RVAL(!SPIF_URL_ISNULL(self), (spif_url_t) NULL); tmp = spif_url_new_from_str(SPIF_STR(self)); return tmp; }
spif_str_t spif_tok_show(spif_tok_t self, spif_charptr_t name, spif_str_t buff, size_t indent) { spif_char_t tmp[4096]; if (SPIF_TOK_ISNULL(self)) { SPIF_OBJ_SHOW_NULL(tok, name, buff, indent, tmp); return buff; } memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_tok_t) %s: %10p {\n", name, (spif_ptr_t) self); if (SPIF_STR_ISNULL(buff)) { buff = spif_str_new_from_ptr(tmp); } else { spif_str_append_from_ptr(buff, tmp); } buff = spif_str_show(SPIF_STR(self->src), SPIF_CHARPTR("src"), buff, indent + 2); buff = spif_str_show(SPIF_STR(self->sep), SPIF_CHARPTR("sep"), buff, indent + 2); indent += 2; memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) quote: '%c' (0x%02x)\n", (char) self->quote, (unsigned int) self->quote); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) dquote: '%c' (0x%02x)\n", (char) self->dquote, (unsigned int) self->dquote); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) escape: '%c' (0x%02x)\n", (char) self->escape, (unsigned int) self->escape); spif_str_append_from_ptr(buff, tmp); SPIF_LIST_SHOW(self->tokens, buff, indent); indent -= 2; snprintf((char *) tmp + indent, sizeof(tmp) - indent, "}\n"); spif_str_append_from_ptr(buff, tmp); return buff; }
spif_bool_t spif_tok_done(spif_tok_t self) { ASSERT_RVAL(!SPIF_TOK_ISNULL(self), FALSE); if (!SPIF_LIST_ISNULL(self->tokens)) { SPIF_LIST_DEL(self->tokens); self->tokens = (spif_list_t) NULL; } if (!SPIF_STR_ISNULL(self->src)) { spif_str_del(SPIF_STR(self->src)); self->src = (spif_str_t) NULL; } if (!SPIF_STR_ISNULL(self->sep)) { spif_str_del(SPIF_STR(self->sep)); self->sep = (spif_str_t) NULL; } self->quote = '\''; self->dquote = '\"'; self->escape = '\\'; return TRUE; }
static spif_bool_t action_handle_echo(event_t *ev, spif_eterm_action_t action) { REQUIRE_RVAL(!SPIF_PTR_ISNULL(ev), FALSE); REQUIRE_RVAL(!SPIF_STR_ISNULL(action->parameter), FALSE); #ifdef ESCREEN if (TermWin.screen && TermWin.screen->backend) { # ifdef NS_HAVE_SCREEN /* translate escapes */ ns_parse_screen_interactive(TermWin.screen, SPIF_STR_STR(action->parameter)); # endif } else #endif tt_write(SPIF_STR_STR(action->parameter), spif_str_get_len(SPIF_STR(action->parameter))); return 1; }
spif_bool_t spif_url_init(spif_url_t self) { ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); if (!spif_str_init(SPIF_STR(self))) { return FALSE; } spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url)); self->proto = (spif_str_t) NULL; self->user = (spif_str_t) NULL; self->passwd = (spif_str_t) NULL; self->host = (spif_str_t) NULL; self->port = (spif_str_t) NULL; self->path = (spif_str_t) NULL; self->query = (spif_str_t) NULL; return TRUE; }
spif_bool_t spif_url_init_from_ptr(spif_url_t self, spif_charptr_t other) { ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); if (!spif_str_init_from_ptr(SPIF_STR(self), other)) { return FALSE; } spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url)); self->proto = (spif_str_t) NULL; self->user = (spif_str_t) NULL; self->passwd = (spif_str_t) NULL; self->host = (spif_str_t) NULL; self->port = (spif_str_t) NULL; self->path = (spif_str_t) NULL; self->query = (spif_str_t) NULL; spif_url_parse(self); return TRUE; }
spif_bool_t spif_eterm_action_done(spif_eterm_action_t self) { if (!SPIF_OBJ_ISNULL(self->parameter)) { if ((self->type == ETERM_ACTION_STRING) || (self->type == ETERM_ACTION_ECHO) || (self->type == ETERM_ACTION_SCRIPT)) { spif_str_del(SPIF_STR(self->parameter)); } } self->type = ETERM_ACTION_NONE; self->modifiers = ETERM_MOD_NONE; self->button = ETERM_BUTTON_NONE; self->keysym = ETERM_KEYSYM_NONE; self->handler = (spif_eterm_action_handler_t) NULL; self->parameter = (spif_eterm_action_parameter_t) NULL; spif_obj_done(SPIF_OBJ(self)); return TRUE; }
static spif_bool_t spif_url_init_from_unixaddr(spif_url_t self, spif_unixsockaddr_t unixaddr) { ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); spif_str_init(SPIF_STR(self)); spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url)); self->proto = (spif_str_t) NULL; self->user = (spif_str_t) NULL; self->passwd = (spif_str_t) NULL; self->host = (spif_str_t) NULL; self->port = (spif_str_t) NULL; self->query = (spif_str_t) NULL; if (unixaddr->sun_path) { self->path = spif_str_new_from_ptr(SPIF_CHARPTR(unixaddr->sun_path)); } else { self->path = (spif_str_t) NULL; } return TRUE; }
static spif_ipsockaddr_t spif_url_get_ipaddr(spif_url_t self) { spif_uint8_t tries; spif_hostinfo_t hinfo; spif_ipsockaddr_t addr; spif_str_t hostname; ASSERT_RVAL(!SPIF_URL_ISNULL(self), (spif_ipsockaddr_t) NULL); /* We need a hostname of some type to connect to. */ hostname = SPIF_STR(spif_url_get_host(self)); REQUIRE_RVAL(!SPIF_STR_ISNULL(hostname), (spif_ipsockaddr_t) NULL); /* Try up to 3 times to resolve the hostname. */ h_errno = 0; tries = 0; do { tries++; hinfo = gethostbyname((char *) SPIF_STR_STR(hostname)); } while ((tries <= 3) && (!hinfo) && (h_errno == TRY_AGAIN)); if (!hinfo) { libast_print_error("Unable to resolve hostname \"%s\" -- %s\n", SPIF_STR_STR(hostname), hstrerror(h_errno)); return (spif_ipsockaddr_t) NULL; } if (!hinfo->h_addr_list) { libast_print_error("Invalid address list returned by gethostbyname()\n"); return (spif_ipsockaddr_t) NULL; } /* Grab the host IP address and port number, both in network byte order. */ addr = SPIF_ALLOC(ipsockaddr); addr->sin_family = AF_INET; addr->sin_port = htons(spif_url_get_portnum(self)); memcpy(&(addr->sin_addr), (void *) (hinfo->h_addr_list[0]), sizeof(addr->sin_addr)); D_OBJ(("Got address 0x%08x and port %d for name \"%s\"\n", (long) ntohl(*((int *) (&addr->sin_addr))), (int) ntohs(addr->sin_port), SPIF_STR_STR(hostname))); return addr; }
spif_cmp_t spif_url_comp(spif_url_t self, spif_url_t other) { SPIF_OBJ_COMP_CHECK_NULL(self, other); return spif_str_comp(SPIF_STR(self), SPIF_STR(other)); }
static spif_bool_t spif_url_parse(spif_url_t self) { spif_charptr_t s = SPIF_STR_STR(SPIF_STR(self)); spif_charptr_t pstr, pend, ptmp; ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); pstr = s; /* Check for "proto:" at the beginning. */ pend = SPIF_CHARPTR(strchr((char *) s, ':')); if (pend) { for (; pstr < pend; pstr++) { if (!isalnum(*pstr)) { break; } } if (pstr == pend) { /* Got one. */ self->proto = spif_str_new_from_buff(s, pend - s); pstr++; } else { /* Nope, reset. */ pstr = s; } } if ((*pstr == '/') && (pstr[1] == '/')) { pstr += 2; } /* Knock out the path and query if they're there. */ pend = SPIF_CHARPTR(strchr((char *) pstr, '/')); if (pend) { spif_charptr_t tmp = SPIF_CHARPTR(strchr((char *) pend, '?')); if (tmp) { self->query = spif_str_new_from_ptr(tmp + 1); self->path = spif_str_new_from_buff(pend, tmp - pend); } else { self->path = spif_str_new_from_ptr(pend); } } else if ((pend = SPIF_CHARPTR(strchr((char *)pstr, '?')))) { self->query = spif_str_new_from_ptr(pend + 1); } else { for (pend = pstr; *pend; pend++); } /* At this point, pend *must* point to the end of the user/pass/host/port part. */ /* Check for an @ sign, which would mean we have auth info. */ ptmp = SPIF_CHARPTR(strchr((char *) pstr, '@')); if ((ptmp) && (ptmp < pend)) { spif_charptr_t tmp = SPIF_CHARPTR(strchr((char *) pstr, ':')); if ((tmp) && (tmp < ptmp)) { /* Both username and password. */ self->user = spif_str_new_from_buff(pstr, tmp - pstr); self->passwd = spif_str_new_from_buff((tmp + 1), ptmp - tmp - 1); } else { self->user = spif_str_new_from_buff(pstr, ptmp - pstr); } pstr = ptmp + 1; } /* All that remains now between pstr and pend is host and maybe port. */ ptmp = SPIF_CHARPTR(strchr((char *) pstr, ':')); if ((ptmp) && (ptmp < pend)) { self->host = spif_str_new_from_buff(pstr, ptmp - pstr); self->port = spif_str_new_from_buff((ptmp + 1), pend - ptmp - 1); } else if (pstr != pend) { self->host = spif_str_new_from_buff(pstr, pend - pstr); } /* If we have a proto but no port, see if we can resolve the port using the proto. */ if (SPIF_STR_ISNULL(self->port) && !SPIF_STR_ISNULL(self->proto)) { spif_protoinfo_t proto; spif_servinfo_t serv; proto = getprotobyname((char *) SPIF_STR_STR(self->proto)); if (!proto) { /* If it's not a protocol, it's probably a service. */ serv = getservbyname((char *) SPIF_STR_STR(self->proto), "tcp"); if (!serv) { serv = getservbyname((char *) SPIF_STR_STR(self->proto), "udp"); } if (serv) { proto = getprotobyname(serv->s_proto); REQUIRE_RVAL(proto != NULL, FALSE); } } if (proto) { spif_char_t buff[32]; snprintf((char *) buff, sizeof(buff), "%d", ntohs(serv->s_port)); self->port = spif_str_new_from_ptr(buff); } } return TRUE; }
spif_bool_t spif_url_unparse(spif_url_t self) { ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE); spif_str_done(SPIF_STR(self)); spif_str_init_from_ptr(SPIF_STR(self), SPIF_CHARPTR("")); /* First, proto followed by a colon. */ if (!SPIF_STR_ISNULL(self->proto)) { spif_str_append(SPIF_STR(self), self->proto); spif_str_append_char(SPIF_STR(self), ':'); } /* If we have a port but no host, make it localhost. */ if (!SPIF_STR_ISNULL(self->port) && SPIF_STR_ISNULL(self->host)) { self->host = spif_str_new_from_ptr(SPIF_CHARPTR("localhost")); } /* We need the // if we have a hostname. */ if (!SPIF_STR_ISNULL(self->host)) { spif_str_append_from_ptr(SPIF_STR(self), SPIF_CHARPTR("//")); } if (!SPIF_STR_ISNULL(self->user)) { spif_str_append(SPIF_STR(self), self->user); if (!SPIF_STR_ISNULL(self->passwd)) { spif_str_append_char(SPIF_STR(self), ':'); spif_str_append(SPIF_STR(self), self->passwd); } spif_str_append_char(SPIF_STR(self), '@'); } if (!SPIF_STR_ISNULL(self->host)) { spif_str_append(SPIF_STR(self), self->host); if (!SPIF_STR_ISNULL(self->port)) { spif_str_append_char(SPIF_STR(self), ':'); spif_str_append(SPIF_STR(self), self->port); } } if (!SPIF_STR_ISNULL(self->path)) { spif_str_append(SPIF_STR(self), self->path); } if (!SPIF_STR_ISNULL(self->query)) { spif_str_append_char(SPIF_STR(self), '?'); spif_str_append(SPIF_STR(self), self->query); } return TRUE; }
spif_bool_t spif_tok_eval(spif_tok_t self) { const char *pstr, *delim = NULL; spif_str_t tmp; char quote; size_t len; ASSERT_RVAL(!SPIF_TOK_ISNULL(self), FALSE); REQUIRE_RVAL(!SPIF_STR_ISNULL(self->src), FALSE); pstr = (const char *) SPIF_STR_STR(SPIF_STR(self->src)); len = spif_str_get_len(SPIF_STR(self->src)); if (!SPIF_STR_ISNULL(self->sep)) { delim = (const char *) SPIF_STR_STR(SPIF_STR(self->sep)); } if (!SPIF_LIST_ISNULL(self->tokens)) { SPIF_LIST_DEL(self->tokens); } self->tokens = SPIF_LIST_NEW(dlinked_list); /* Before we do anything, skip leading "whitespace." */ for (; *pstr && IS_DELIM(*pstr); pstr++); /* The outermost for loop is where we traverse the string. Each new word brings us back to the top where we resize our string list. */ for (quote = 0; *pstr; ) { tmp = spif_str_new_from_buff(SPIF_CHARPTR(""), len); spif_str_clear(tmp, 0); /* This for loop is where we process each character. */ for (; *pstr && (quote || !IS_DELIM(*pstr));) { if (*pstr == self->dquote || *pstr == self->quote) { /* It's a quote character, so set or reset the quote variable. */ if (quote) { if (quote == *pstr) { quote = 0; } else { /* It's a single quote inside double quotes, or vice versa. Leave it alone. */ spif_str_append_char(tmp, *pstr); } } else { quote = *pstr; } pstr++; } else { /* Handle any backslashes that are escaping delimiters or quotes. */ if ((*pstr == self->escape) && (IS_DELIM(*(pstr + 1)) || IS_QUOTE(*(pstr + 1)))) { /* Incrementing pstr here moves us past the backslash so that the line below will copy the next character to the new token, no questions asked. */ pstr++; } spif_str_append_char(tmp, *pstr++); } } /* Reallocate the new string to be just the right size. */ spif_str_trim(tmp); len -= spif_str_get_len(tmp); /* Add it to the list */ SPIF_LIST_APPEND(self->tokens, tmp); /* Move past any trailing "whitespace." */ for (; *pstr && IS_DELIM(*pstr); pstr++); } return TRUE; }