diff --git a/CMakeLists.txt b/CMakeLists.txt index fc029bdff1..d68a5332d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,6 @@ set(AWE_SRCS ${BUILD_DIR}/xrdb.c ${BUILD_DIR}/common/atoms.c ${BUILD_DIR}/common/backtrace.c - ${BUILD_DIR}/common/buffer.c ${BUILD_DIR}/common/luaclass.c ${BUILD_DIR}/common/lualib.c ${BUILD_DIR}/common/luaobject.c diff --git a/awesome.c b/awesome.c index a08f21d429..abe26b6f27 100644 --- a/awesome.c +++ b/awesome.c @@ -492,9 +492,8 @@ a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout) static void signal_fatal(int signum) { - buffer_t buf; - backtrace_get(&buf); - fatal("signal %d, dumping backtrace\n%s", signum, buf.s); + GString* buf = backtrace_get(); + fatal("signal %d, dumping backtrace\n%s", signum, buf->str); } /* Signal handler for SIGCHLD. Causes reap_children() to be called. */ @@ -586,7 +585,7 @@ main(int argc, char **argv) globalconf.mousegrabber = LUA_REFNIL; globalconf.exit_code = EXIT_SUCCESS; globalconf.api_level = awesome_default_api_level(); - buffer_init(&globalconf.startup_errors); + globalconf.startup_errors = g_string_new(NULL); /* save argv */ awesome_argv = argv; diff --git a/common/backtrace.c b/common/backtrace.c index 815d07a268..ab642b41a9 100644 --- a/common/backtrace.c +++ b/common/backtrace.c @@ -21,6 +21,7 @@ #include "config.h" #include "common/backtrace.h" +#include "common/util.h" #ifdef HAS_EXECINFO #include @@ -31,11 +32,10 @@ /** Get a backtrace. * \param buf The buffer to fill with backtrace. */ -void -backtrace_get(buffer_t *buf) +GString* +backtrace_get(void) { - buffer_init(buf); - + GString *buf = g_string_new(NULL); #ifdef HAS_EXECINFO void *stack[MAX_STACK_SIZE]; char **bt; @@ -49,14 +49,15 @@ backtrace_get(buffer_t *buf) for(int i = 0; i < stack_size; i++) { if(i > 0) - buffer_addsl(buf, "\n"); - buffer_adds(buf, bt[i]); + g_string_append(buf, "\n"); + g_string_append(buf, bt[i]); } p_delete(&bt); } else #endif - buffer_addsl(buf, "Cannot get backtrace symbols."); + g_string_append(buf, "Cannot get backtrace symbols."); + return buf; } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/common/backtrace.h b/common/backtrace.h index 8a63a3e48a..85ebbe1308 100644 --- a/common/backtrace.h +++ b/common/backtrace.h @@ -22,9 +22,9 @@ #ifndef AWESOME_COMMON_BACKTRACE #define AWESOME_COMMON_BACKTRACE -#include "common/buffer.h" +#include -void backtrace_get(buffer_t *); +GString* backtrace_get(void); #endif diff --git a/common/buffer.c b/common/buffer.c deleted file mode 100644 index cbdd3872a0..0000000000 --- a/common/buffer.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright © 2006,2007,2008 Pierre Habouzit - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The names of its contributors may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "common/buffer.h" - -#include -#include - -char buffer_slop[1]; - -void -buffer_ensure(buffer_t *buf, int newlen) -{ - if (newlen < 0) - exit(EX_SOFTWARE); - - if (newlen < buf->size) - return; - - if (newlen < buf->offs + buf->size && buf->offs > buf->size / 4) - { - /* Data fits in the current area, shift it left */ - memmove(buf->s - buf->offs, buf->s, buf->len + 1); - buf->s -= buf->offs; - buf->size += buf->offs; - buf->offs = 0; - return; - } - - buf->size = p_alloc_nr(buf->size + buf->offs); - if (buf->size < newlen + 1) - buf->size = newlen + 1; - if (buf->alloced && !buf->offs) - p_realloc(&buf->s, buf->size); - else - { - char *new_area = xmalloc(buf->size); - memcpy(new_area, buf->s, buf->len + 1); - if (buf->alloced) - free(buf->s - buf->offs); - buf->alloced = true; - buf->s = new_area; - buf->offs = 0; - } -} - -void -buffer_addvf(buffer_t *buf, const char *fmt, va_list args) -{ - int len; - va_list ap; - - va_copy(ap, args); - buffer_ensure(buf, BUFSIZ); - - len = vsnprintf(buf->s + buf->len, buf->size - buf->len, fmt, args); - if (unlikely(len < 0)) - { - va_end(ap); - return; - } - if (len >= buf->size - buf->len) - { - buffer_ensure(buf, len); - vsnprintf(buf->s + buf->len, buf->size - buf->len, fmt, ap); - } - buf->len += len; - buf->s[buf->len] = '\0'; - va_end(ap); -} - -void -buffer_addf(buffer_t *buf, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - buffer_addvf(buf, fmt, args); - va_end(args); -} - -/** Detach the data from a buffer. - * \param Buffer from which detach. - * \return The data. - */ -char * -buffer_detach(buffer_t *buf) -{ - char *res = buf->s; - if (!buf->alloced) - res = a_strdup(buf->s); - buffer_init(buf); - return res; -} - -// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/common/buffer.h b/common/buffer.h deleted file mode 100644 index 8ada17544a..0000000000 --- a/common/buffer.h +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright © 2006,2007,2008 Pierre Habouzit - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The names of its contributors may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef AWESOME_COMMON_BUFFER_H -#define AWESOME_COMMON_BUFFER_H - -#include "common/util.h" - -typedef struct buffer_t -{ - char *s; - int len, size; - unsigned alloced: 1; - unsigned offs :31; -} buffer_t; - -extern char buffer_slop[1]; - -#define BUFFER_INIT (buffer_t) { .s = buffer_slop, .size = 1 } - -#define buffer_inita(b, sz) \ - ({ int __sz = (sz); assert (__sz < (64 << 10)); \ - buffer_init_buf((b), alloca(__sz), __sz); }) - -/** Initialize a buffer. - * \param buf A buffer pointer. - * \return The same buffer pointer. - */ -static inline buffer_t * -buffer_init(buffer_t *buf) -{ - *buf = BUFFER_INIT; - return buf; -} - -/** Initialize a buffer with data. - * \param b The buffer to init. - * \param buf The data to set. - * \param size Data size. - */ -static inline void -buffer_init_buf(buffer_t *b, void *buf, int size) -{ - *b = (buffer_t){ .s = buf, .size = size }; - b->s[0] = '\0'; -} - -/** Wipe a buffer. - * \param buf The buffer. - */ -static inline void -buffer_wipe(buffer_t *buf) -{ - if (buf->alloced) - free(buf->s - buf->offs); - buffer_init(buf); -} - -/** Get a new buffer. - * \return A new allocated buffer. - */ -static inline buffer_t * -buffer_new(void) -{ - return buffer_init(p_new(buffer_t, 1)); -} - -/** Delete a buffer. - * \param buf A pointer to a buffer pointer to free. - */ -static inline void -buffer_delete(buffer_t **buf) -{ - if(*buf) - { - buffer_wipe(*buf); - p_delete(buf); - } -} - -char *buffer_detach(buffer_t *buf); -void buffer_ensure(buffer_t *buf, int len); - -/** Grow a buffer. - * \param buf The buffer to grow. - * \param extra The number to add to length. - */ -static inline void -buffer_grow(buffer_t *buf, int extra) -{ - assert (extra >= 0); - if (buf->len + extra > buf->size) - { - buffer_ensure(buf, buf->len + extra); - } -} - -/** Add data in the buffer. - * \param buf Buffer where to add. - * \param pos Position where to add. - * \param len Length. - * \param data Data to add. - * \param dlen Data length. - */ -static inline void -buffer_splice(buffer_t *buf, int pos, int len, const void *data, int dlen) -{ - assert (pos >= 0 && len >= 0 && dlen >= 0); - - if (unlikely(pos > buf->len)) - pos = buf->len; - if (unlikely(len > buf->len - pos)) - len = buf->len - pos; - if (pos == 0 && len + buf->offs >= dlen) - { - buf->offs += len - dlen; - buf->s += len - dlen; - buf->size -= len - dlen; - buf->len -= len - dlen; - } - else if (len != dlen) - { - buffer_ensure(buf, buf->len + dlen - len); - memmove(buf->s + pos + dlen, buf->s + pos + len, buf->len - pos - len); - buf->len += dlen - len; - buf->s[buf->len] = '\0'; - } - memcpy(buf->s + pos, data, dlen); -} - -/** Add data at the end of buffer. - * \param buf Buffer where to add. - * \param data Data to add. - * \param len Data length. - */ -static inline void -buffer_add(buffer_t *buf, const void *data, int len) -{ - buffer_splice(buf, buf->len, 0, data, len); -} - -#define buffer_addsl(buf, data) \ - buffer_add(buf, data, sizeof(data) - 1); - -/** Add a string to the and of a buffer. - * \param buf The buffer where to add. - * \param s The string to add. - */ -static inline void buffer_adds(buffer_t *buf, const char *s) -{ - buffer_splice(buf, buf->len, 0, s, a_strlen(s)); -} - -/** Add a char at the end of a buffer. - * \param buf The buffer where to add. - * \param c The char to add. - */ -static inline void buffer_addc(buffer_t *buf, int c) -{ - buffer_grow(buf, 1); - buf->s[buf->len++] = c; - buf->s[buf->len] = '\0'; -} - -void buffer_addvf(buffer_t *buf, const char *fmt, va_list) - __attribute__((format(printf, 2, 0))); - -void buffer_addf(buffer_t *buf, const char *fmt, ...) - __attribute__((format(printf, 2, 3))); - -#endif - -// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/common/luaobject.c b/common/luaobject.c index 562b5e98ed..39d265a837 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -125,9 +125,9 @@ luaA_object_decref(lua_State *L, int tud, const void *pointer) /* Did we find the item in our table? (tointeger(nil)-1) is -1 */ if (count < 0) { - buffer_t buf; - backtrace_get(&buf); - warn("BUG: Reference not found: %d %p\n%s", tud, pointer, buf.s); + GString* buf = backtrace_get(); + warn("BUG: Reference not found: %d %p\n%s", tud, pointer, buf->str); + g_string_free(buf, TRUE); /* Pop reference count and metatable */ lua_pop(L, 2); diff --git a/ewmh.c b/ewmh.c index d31512ff3f..0f2d73d431 100644 --- a/ewmh.c +++ b/ewmh.c @@ -305,20 +305,18 @@ ewmh_update_net_current_desktop(lua_State *L) void ewmh_update_net_desktop_names(void) { - buffer_t buf; - - buffer_inita(&buf, BUFSIZ); + GString* buf = g_string_new(NULL); foreach(tag, globalconf.tags) { - buffer_adds(&buf, tag_get_name(*tag)); - buffer_addc(&buf, '\0'); + g_string_append(buf,tag_get_name(*tag)); + g_string_append_c(buf, '\0'); } xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, globalconf.screen->root, - _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s); - buffer_wipe(&buf); + _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf->len, buf->str); + g_string_free(buf, TRUE); } static void diff --git a/globalconf.h b/globalconf.h index 7681065634..b49249bd25 100644 --- a/globalconf.h +++ b/globalconf.h @@ -41,7 +41,6 @@ #include "objects/key.h" #include "common/xembed.h" #include "common/xcursor.h" -#include "common/buffer.h" #define ROOT_WINDOW_EVENT_MASK \ (const uint32_t []) { \ @@ -148,7 +147,7 @@ typedef struct lua_State *real_L_dont_use_directly; } L; /** All errors messages from loading config files */ - buffer_t startup_errors; + GString* startup_errors; /** main loop that awesome is running on */ GMainLoop *loop; /** The key grabber function */ diff --git a/luaa.c b/luaa.c index c9caee9f83..127866d904 100644 --- a/luaa.c +++ b/luaa.c @@ -771,9 +771,9 @@ luaA_awesome_index(lua_State *L) if(A_STREQ(buf, "startup_errors")) { - if (globalconf.startup_errors.len == 0) + if (globalconf.startup_errors->len == 0) return 0; - lua_pushstring(L, globalconf.startup_errors.s); + lua_pushlstring(L, globalconf.startup_errors->str,globalconf.startup_errors->len); return 1; } @@ -862,9 +862,8 @@ luaA_panic(lua_State *L) { warn("unprotected error in call to Lua API (%s)", lua_tostring(L, -1)); - buffer_t buf; - backtrace_get(&buf); - warn("dumping backtrace\n%s", buf.s); + GString* buf = backtrace_get(); + warn("dumping backtrace\n%s", buf->str); warn("restarting awesome"); awesome_restart(); return 0; @@ -1214,9 +1213,9 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath) static void luaA_startup_error(const char *err) { - if (globalconf.startup_errors.len > 0) - buffer_addsl(&globalconf.startup_errors, "\n\n"); - buffer_adds(&globalconf.startup_errors, err); + if (globalconf.startup_errors->len > 0) + g_string_append(globalconf.startup_errors, "\n\n"); + g_string_append(globalconf.startup_errors, err); } static bool diff --git a/property.c b/property.c index 7dee8eb2a9..0141cc966f 100644 --- a/property.c +++ b/property.c @@ -435,11 +435,10 @@ property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev) lua_State *L = globalconf_get_lua_State(); xproperty_t *prop; xproperty_t lookup = { .atom = ev->atom }; - buffer_t buf; void *obj; prop = xproperty_array_lookup(&globalconf.xproperties, &lookup); - if(!prop) + if (!prop) /* Property is not registered */ return; @@ -454,18 +453,19 @@ property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev) obj = NULL; /* Get us the name of the property */ - buffer_inita(&buf, a_strlen(prop->name) + a_strlen("xproperty::") + 1); - buffer_addf(&buf, "xproperty::%s", prop->name); + const char prefix[] = "xproperty::"; + char buf[a_strlen(prop->name) + sizeof(prefix)]; + memcpy(buf, prefix, sizeof(prefix)); + a_strcat(buf, sizeof(buf), prop->name); /* And emit the right signal */ if (obj) { luaA_object_push(L, obj); - luaA_object_emit_signal(L, -1, buf.s, 0); + luaA_object_emit_signal(L, -1, buf, 0); lua_pop(L, 1); } else - signal_object_emit(L, &global_signals, buf.s, 0); - buffer_wipe(&buf); + signal_object_emit(L, &global_signals, buf, 0); } /** The property notify event handler.