libexplain  1.4.D001
libexplain/buffer/pollfd.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2010, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/poll.h>
00020 
00021 #include <libexplain/buffer/fildes.h>
00022 #include <libexplain/buffer/pollfd.h>
00023 #include <libexplain/buffer/pointer.h>
00024 #include <libexplain/parse_bits.h>
00025 #include <libexplain/is_efault.h>
00026 
00027 
00028 static void
00029 explain_buffer_pollfd_events(explain_string_buffer_t *sb, int value)
00030 {
00031     static const explain_parse_bits_table_t table[] =
00032     {
00033 #ifdef POLLIN
00034         { "POLLIN", POLLIN },
00035 #endif
00036 #ifdef POLLPRI
00037         { "POLLPRI", POLLPRI },
00038 #endif
00039 #ifdef POLLOUT
00040         { "POLLOUT", POLLOUT },
00041 #endif
00042 #ifdef POLLRDNORM
00043         { "POLLRDNORM", POLLRDNORM },
00044 #endif
00045 #ifdef POLLRDBAND
00046         { "POLLRDBAND", POLLRDBAND },
00047 #endif
00048 #ifdef POLLWRNORM
00049         { "POLLWRNORM", POLLWRNORM },
00050 #endif
00051 #ifdef POLLWRBAND
00052         { "POLLWRBAND", POLLWRBAND },
00053 #endif
00054 #ifdef POLLMSG
00055         { "POLLMSG", POLLMSG },
00056 #endif
00057 #ifdef POLLREMOVE
00058         { "POLLREMOVE", POLLREMOVE },
00059 #endif
00060 #ifdef POLLRDHUP
00061         { "POLLRDHUP", POLLRDHUP },
00062 #endif
00063 #ifdef POLLERR
00064         { "POLLERR", POLLERR },
00065 #endif
00066 #ifdef POLLHUP
00067         { "POLLHUP", POLLHUP },
00068 #endif
00069 #ifdef POLLNVAL
00070         { "POLLNVAL", POLLNVAL },
00071 #endif
00072     };
00073     explain_parse_bits_print(sb, value, table, SIZEOF(table));
00074 }
00075 
00076 
00077 void
00078 explain_buffer_pollfd(explain_string_buffer_t *sb,
00079     const struct pollfd *data, int include_revents)
00080 {
00081 #ifdef HAVE_POLL_H
00082     if (explain_is_efault_pointer(data, sizeof(*data)))
00083     {
00084         explain_buffer_pointer(sb, data);
00085         return;
00086     }
00087 
00088     explain_string_buffer_puts(sb, "{ fd = ");
00089     explain_buffer_fildes(sb, data->fd);
00090     explain_string_buffer_puts(sb, ", events = ");
00091     explain_buffer_pollfd_events(sb, data->events);
00092     if (include_revents)
00093     {
00094         explain_string_buffer_puts(sb, ", revents = ");
00095         explain_buffer_pollfd_events(sb, data->revents);
00096     }
00097     explain_string_buffer_puts(sb, " }");
00098 #else
00099     (void)include_revents;
00100     explain_buffer_pointer(sb, data);
00101 #endif
00102 }
00103 
00104 
00105 void
00106 explain_buffer_pollfd_array(explain_string_buffer_t *sb,
00107     const struct pollfd *data, int data_size, int include_revents)
00108 {
00109 #ifdef HAVE_POLL_H
00110     int             j;
00111 
00112     if
00113     (
00114         data_size <= 0
00115     ||
00116         explain_is_efault_pointer(data, sizeof(*data) * data_size)
00117     )
00118     {
00119         explain_buffer_pointer(sb, data);
00120         return;
00121     }
00122 
00123     explain_string_buffer_putc(sb, '{');
00124     for (j = 0; j < data_size; ++j)
00125     {
00126         if (j)
00127             explain_string_buffer_putc(sb, ',');
00128         explain_string_buffer_putc(sb, ' ');
00129         explain_buffer_pollfd(sb, data + j, include_revents);
00130     }
00131     explain_string_buffer_puts(sb, " }");
00132 #else
00133     (void)data_size;
00134     (void)include_revents;
00135     explain_buffer_pointer(sb, data);
00136 #endif
00137 }
00138 
00139 
00140 /* vim: set ts=8 sw=4 et : */