Esempio n. 1
0
File: options.c Progetto: mej/libast
/**
 * Option-type-to-string translator.
 *
 * This function is used internally by spifopt_usage() to convert an
 * option's numerical type to a short string representing that type of
 * option.
 *
 * @param type The numeric option type (SPIFOPT_OPT_FLAGS()).
 * @return     A 6-char-max string describing the option type.
 *
 * @see @link DOXGRP_OPT Command Line Option Parser @endlink
 * @ingroup DOXGRP_OPT
 */
static spif_charptr_t 
get_option_type_string(spif_uint16_t type)
{
    switch (type) {
        case SPIFOPT_FLAG_BOOLEAN: return SPIF_CHARPTR("(bool)"); break;
        case SPIFOPT_FLAG_INTEGER: return SPIF_CHARPTR("(int)"); break;
        case SPIFOPT_FLAG_ARGLIST: return SPIF_CHARPTR("(strs)"); break;
        default: return SPIF_CHARPTR("(str)");
    }
    ASSERT_NOTREACHED_RVAL(NULL);
}
Esempio n. 2
0
File: str.c Progetto: mej/libast
spif_bool_t
spif_str_sprintf(spif_str_t self, spif_charptr_t format, ...)
{
    va_list ap;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    if (self->s != (spif_charptr_t) NULL) {
        spif_str_done(self);
    }
    if (!format) {
        return FALSE;
    } else if (*format == 0) {
        return TRUE;
    } else {
        int c;
        char buff[2];

        va_start(ap, format);
        c = vsnprintf(buff, sizeof(buff), format, ap);
        va_end(ap);
        if (c <= 0) {
            return FALSE;
        } else {
            self->size = c + 1;
            self->s = (spif_charptr_t) MALLOC(self->size);
            va_start(ap, format);
            c = vsnprintf(self->s, self->size, format, ap);
            va_end(ap);
            if (c > -1 && c < self->size) {
                self->len = c;
                return TRUE;
            } else {
                self->s[0] = 0;
                return FALSE;
            }
        }
    }
    ASSERT_NOTREACHED_RVAL(FALSE);
}
Esempio n. 3
0
static spif_bool_t
action_matches_event(spif_eterm_action_t action, event_t *ev)
{
    /* The very first thing we do is match the event type to the type
       of the current action.  This means that we'll only run through
       the modifier checks below if we absolutely have to. */
    D_ACTIONS(("Checking action %10p for match to event %10p.\n", action, ev));
    if (ev->xany.type == ButtonPress) {
        if (!action_check_button(action->button, ev->xbutton.button)) {
            return FALSE;
        }
    } else if (ev->xany.type == KeyPress) {
        if (!action_check_keysym(action->keysym, XKeycodeToKeysym(Xdisplay, ev->xkey.keycode, 0))) {
            return FALSE;
        }
    } else {
        ASSERT_NOTREACHED_RVAL(FALSE);
    }
    if (action_check_modifiers(action->modifiers, ev->xkey.state)) {
        D_ACTIONS(("Match found.\n"));
        return TRUE;
    }
    return FALSE;
}
Esempio n. 4
0
File: socket.c Progetto: mej/libast
spif_bool_t
spif_socket_open(spif_socket_t self)
{
    ASSERT_RVAL(!SPIF_SOCKET_ISNULL(self), FALSE);

    if (!(self->addr)) {
        /* Set family, protocol, and type flags. */
        spif_socket_get_proto(self);

        /* Get remote address, if we have one. */
        if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_INET)) {
            self->fam = AF_INET;
            if (!SPIF_URL_ISNULL(self->remote_url)) {
                self->addr = (spif_sockaddr_t) spif_url_get_ipaddr(self->remote_url);
            } else {
                self->addr = (spif_sockaddr_t) NULL;
            }
            if (self->addr == (spif_sockaddr_t) NULL) {
                self->len = 0;
            } else {
                self->len = SPIF_SIZEOF_TYPE(ipsockaddr);
            }
        } else if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_UNIX)) {
            self->fam = AF_UNIX;
            if (!SPIF_URL_ISNULL(self->remote_url)) {
                self->addr = (spif_sockaddr_t) spif_url_get_unixaddr(self->remote_url);
            } else {
                self->addr = (spif_sockaddr_t) NULL;
            }
            if (self->addr == (spif_sockaddr_t) NULL) {
                self->len = 0;
            } else {
                self->len = SPIF_SIZEOF_TYPE(unixsockaddr);
            }
        } else {
            D_OBJ(("Unknown socket family 0x%08x!\n", SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_FAMILY)));
            ASSERT_NOTREACHED_RVAL(FALSE);
        }
    }

    /* If the socket isn't open yet, open it and get a file descriptor. */
    if (self->fd < 0) {
        if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_TYPE_STREAM)) {
            self->type = SOCK_STREAM;
        } else if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_TYPE_DGRAM)) {
            self->type = SOCK_DGRAM;
        } else if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_TYPE_RAW)) {
            self->type = SOCK_RAW;
        } else {
            D_OBJ(("Unknown socket type 0x%08x!\n", SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_TYPE)));
            ASSERT_NOTREACHED_RVAL(FALSE);
        }

        self->fd = (spif_sockfd_t) socket(self->fam, self->type, self->proto);
        if (self->fd < 0) {
            libast_print_error("Unable to create socket(%d, %d, %d) -- %s\n", (int) self->fam,
                        (int) self->type, (int) self->proto, strerror(errno));
            return FALSE;
        }

        /* If we have a local URL, bind it to the appropriate address and port. */
        if (!SPIF_URL_ISNULL(self->local_url)) {
            if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_INET)) {
                spif_ipsockaddr_t addr;

                addr = spif_url_get_ipaddr(self->local_url);

                D_OBJ(("Binding to port %d\n", ntohs(addr->sin_port)));
                if (bind(self->fd, (spif_sockaddr_t) addr, SPIF_SIZEOF_TYPE(ipsockaddr))) {
                    libast_print_error("Unable to bind socket %d to %s -- %s\n", (int) self->fd,
                                SPIF_STR_STR(self->local_url), strerror(errno));
                    FREE(addr);
                    return FALSE;
                }
                FREE(addr);
            } else if (SPIF_SOCKET_FLAGS_IS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_UNIX)) {
                spif_unixsockaddr_t addr;

                addr = spif_url_get_unixaddr(self->local_url);

                if (bind(self->fd, (spif_sockaddr_t) addr, SPIF_SIZEOF_TYPE(unixsockaddr))) {
                    libast_print_error("Unable to bind socket %d to %s -- %s\n", (int) self->fd,
                                SPIF_STR_STR(self->local_url), strerror(errno));
                    FREE(addr);
                    return FALSE;
                }
                FREE(addr);
            }
        }
        SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_OPEN);
    }

    /* Connect if we have a destination.  If not, open a listening socket. */
    if (!SPIF_URL_ISNULL(self->remote_url)) {
        spif_socket_clear_nbio(self);
        if ((connect(self->fd, self->addr, self->len)) < 0) {
            libast_print_error("Unable to connect socket %d to %s -- %s\n", (int) self->fd,
                        SPIF_STR_STR(self->remote_url), strerror(errno));
            return FALSE;
        }
        SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_CONNECTED);
    } else if (!SPIF_URL_ISNULL(self->local_url)) {
        if ((listen(self->fd, 5)) < 0) {
            libast_print_error("Unable to listen at %s on socket %d -- %s\n", 
                        SPIF_STR_STR(self->local_url), (int) self->fd, strerror(errno));
            return FALSE;
        }
        SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_LISTEN);
    }

    return TRUE;
}