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_bool_t spif_socket_send(spif_socket_t self, spif_str_t data) { size_t len; int num_written; struct timeval tv = { 0, 0 }; ASSERT_RVAL(!SPIF_SOCKET_ISNULL(self), FALSE); REQUIRE_RVAL(!SPIF_STR_ISNULL(data), FALSE); len = spif_str_get_len(data); REQUIRE_RVAL(len > 0, FALSE); num_written = write(self->fd, SPIF_STR_STR(data), len); for (; (num_written < 0) && ((errno == EAGAIN) || (errno == EINTR)); ) { tv.tv_usec += 10000; if (tv.tv_usec == 1000000) { tv.tv_usec = 0; tv.tv_sec++; } select(0, NULL, NULL, NULL, &tv); num_written = write(self->fd, SPIF_STR_STR(data), len); } if (num_written < 0) { D_OBJ(("Unable to write to socket %d -- %s\n", self->fd, strerror(errno))); switch (errno) { case EFBIG: { spif_bool_t b; spif_str_t tmp_buf; spif_charptr_t s; long left; for (left = len, s = SPIF_CHARPTR(SPIF_STR_STR(data)); left > 0; s += 1024, left -= 1024) { tmp_buf = spif_str_new_from_buff(s, 1024); b = spif_socket_send(self, tmp_buf); if (b == FALSE) { spif_str_del(tmp_buf); return b; } } } break; case EIO: case EPIPE: close(self->fd); /* Drop */ case EBADF: case EINVAL: default: self->fd = -1; SPIF_SOCKET_FLAGS_CLEAR(self, SPIF_SOCKET_FLAGS_IOSTATE); return FALSE; break; } } 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_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; }