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_str_t spif_url_show(spif_url_t self, spif_charptr_t name, spif_str_t buff, size_t indent) { spif_char_t tmp[4096]; if (SPIF_URL_ISNULL(self)) { SPIF_OBJ_SHOW_NULL(url, name, buff, indent, tmp); return buff; } memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_url_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(self->proto, SPIF_CHARPTR("proto"), buff, indent + 2); buff = spif_str_show(self->user, SPIF_CHARPTR("user"), buff, indent + 2); buff = spif_str_show(self->passwd, SPIF_CHARPTR("passwd"), buff, indent + 2); buff = spif_str_show(self->host, SPIF_CHARPTR("host"), buff, indent + 2); buff = spif_str_show(self->port, SPIF_CHARPTR("port"), buff, indent + 2); buff = spif_str_show(self->path, SPIF_CHARPTR("path"), buff, indent + 2); buff = spif_str_show(self->query, SPIF_CHARPTR("query"), buff, indent + 2); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "}\n"); spif_str_append_from_ptr(buff, tmp); return buff; }
spif_str_t spif_str_show(spif_str_t self, spif_charptr_t name, spif_str_t buff, size_t indent) { spif_char_t tmp[4096]; if (SPIF_STR_ISNULL(self)) { SPIF_OBJ_SHOW_NULL(str, name, buff, indent, tmp); return buff; } memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_str_t) %s: %10p { \"", 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); } spif_str_append(buff, self); snprintf((char *) tmp, sizeof(tmp), "\", len %lu, size %lu }\n", (unsigned long) self->len, (unsigned long) self->size); spif_str_append_from_ptr(buff, tmp); return buff; }
spif_str_t spif_socket_show(spif_socket_t self, spif_charptr_t name, spif_str_t buff, size_t indent) { spif_char_t tmp[4096]; if (SPIF_SOCKET_ISNULL(self)) { SPIF_OBJ_SHOW_NULL(socket, name, buff, indent, tmp); return buff; } memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_socket_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); } indent += 2; memset(tmp, ' ', indent); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockfd_t) fd: %d\n", self->fd); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockfamily_t) fam: %d\n", (int) self->fam); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_socktype_t) type: %d\n", (int) self->type); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockproto_t) proto: %d\n", (int) self->proto); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockaddr_t) addr: %10p\n", (spif_ptr_t) self->addr); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockaddr_len_t) len: %lu\n", (unsigned long) self->len); spif_str_append_from_ptr(buff, tmp); snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_uint32_t) flags: 0x%08x\n", (unsigned) self->flags); spif_str_append_from_ptr(buff, tmp); spif_url_show(self->local_url, SPIF_CHARPTR("local_url"), buff, indent); spif_url_show(self->remote_url, SPIF_CHARPTR("remote_url"), 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_eterm_action_init_from_data(spif_eterm_action_t self, spif_eterm_action_type_t type, spif_ushort_t modifiers, spif_uchar_t button, KeySym keysym, spif_ptr_t param) { spif_obj_init(SPIF_OBJ(self)); spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(eterm_action)); self->type = type; self->modifiers = modifiers; self->button = button; self->keysym = keysym; switch (type) { case ETERM_ACTION_STRING: self->handler = (spif_eterm_action_handler_t) action_handle_string; self->parameter = (spif_obj_t) spif_str_new_from_ptr(param); /*parse_escaped_string(self->parameter.string); */ break; case ETERM_ACTION_ECHO: self->handler = (spif_eterm_action_handler_t) action_handle_echo; self->parameter = (spif_obj_t) spif_str_new_from_ptr(param); /*parse_escaped_string(self->parameter.string); */ break; case ETERM_ACTION_SCRIPT: self->handler = (spif_eterm_action_handler_t) action_handle_script; self->parameter = (spif_obj_t) spif_str_new_from_ptr(param); break; case ETERM_ACTION_MENU: self->handler = (spif_eterm_action_handler_t) action_handle_menu; /*self->parameter.menu = (menu_t *) param; */ break; default: break; } D_ACTIONS(("Added action. modifiers == 0x%08x, button == %d, keysym == 0x%08x\n", self->modifiers, self->button, (unsigned) self->keysym)); 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_init_from_ptr(spif_tok_t self, spif_charptr_t old) { ASSERT_RVAL(!SPIF_TOK_ISNULL(self), FALSE); if (!spif_obj_init(SPIF_OBJ(self))) { return FALSE; } else if (!spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(tok))) { return FALSE; } self->src = spif_str_new_from_ptr(old); self->quote = '\''; self->dquote = '\"'; self->escape = '\\'; self->tokens = (spif_list_t) NULL; self->sep = (spif_str_t) NULL; return ((SPIF_STR_ISNULL(self->src)) ? (FALSE) : (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; }
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_str_t spif_eterm_action_show(spif_eterm_action_t self, spif_charptr_t name, spif_str_t buff, size_t indent) { char tmp[4096]; if (SPIF_ETERM_ACTION_ISNULL(self)) { SPIF_OBJ_SHOW_NULL(eterm_action, name, buff, indent, tmp); return buff; } memset(tmp, ' ', indent); snprintf(tmp + indent, sizeof(tmp) - indent, "(spif_eterm_action_t) %s: {\n", name); if (SPIF_STR_ISNULL(buff)) { buff = spif_str_new_from_ptr(tmp); } else { spif_str_append_from_ptr(buff, tmp); } snprintf(tmp + indent, sizeof(tmp) - indent, " (spif_eterm_action_type_t) type: %d\n", self->type); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, " (spif_ushort_t) modifiers: %c%c%c%c\n", SHOW_MODS(self->modifiers)); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, " (spif_uchar_t) button: %d\n", self->button); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, " (KeySym) keysym: %04x\n", (unsigned) self->keysym); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, " (spif_eterm_action_handler_t) handler: %10p\n", self->handler); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, " (spif_eterm_action_parameter_t) parameter: %10p\n", self->parameter); spif_str_append_from_ptr(buff, tmp); snprintf(tmp + indent, sizeof(tmp) - indent, "}\n"); spif_str_append_from_ptr(buff, tmp); return buff; }
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; }