use more tokenizer, with len

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-23 14:01:33 +02:00
parent f0a01c6131
commit ab5119732d
15 changed files with 84 additions and 56 deletions

View File

@ -86,7 +86,8 @@ SET(AWE_MAN_SRCS
ADD_EXECUTABLE(${PROJECT_AWE_NAME} ${AWE_SRCS} ADD_EXECUTABLE(${PROJECT_AWE_NAME} ${AWE_SRCS}
${CMAKE_CURRENT_BINARY_DIR}/common/tokenize.c) ${CMAKE_CURRENT_BINARY_DIR}/common/tokenize.c)
ADD_EXECUTABLE(${PROJECT_AWECLIENT_NAME} ${AWE_CLIENT_SRCS}) ADD_EXECUTABLE(${PROJECT_AWECLIENT_NAME} ${AWE_CLIENT_SRCS}
${CMAKE_CURRENT_BINARY_DIR}/common/tokenize.c)
TARGET_LINK_LIBRARIES(${PROJECT_AWE_NAME} TARGET_LINK_LIBRARIES(${PROJECT_AWE_NAME}
${AWESOME_REQUIRED_LIBRARIES} ${AWESOME_REQUIRED_LIBRARIES}

View File

@ -293,6 +293,7 @@ client_raise(client_t *c)
for(screen = 0; screen < globalconf.screens_info->nscreen; screen++) for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next) for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
if(sb->sw)
{ {
xcb_configure_window(globalconf.connection, xcb_configure_window(globalconf.connection,
sb->sw->window, sb->sw->window,
@ -957,7 +958,7 @@ luaA_client_border_set(lua_State *L)
{ {
client_t **c = luaA_checkudata(L, 1, "client"); client_t **c = luaA_checkudata(L, 1, "client");
int width = luaA_getopt_number(L, 2, "width", (*c)->border); int width = luaA_getopt_number(L, 2, "width", (*c)->border);
const char *colorstr = luaA_getopt_string(L, 2, "color", NULL); const char *colorstr = luaA_getopt_string(L, 2, "color", NULL, NULL);
xcolor_t color; xcolor_t color;
client_setborder(*c, width); client_setborder(*c, width);

View File

@ -201,6 +201,7 @@ static void
draw_markup_on_element(markup_parser_data_t *p, const char *elem, draw_markup_on_element(markup_parser_data_t *p, const char *elem,
const char **names, const char **values) const char **names, const char **values)
{ {
size_t len;
draw_parser_data_t *data = p->priv; draw_parser_data_t *data = p->priv;
/* hack: markup.c validates tags so we can avoid strcmps here */ /* hack: markup.c validates tags so we can avoid strcmps here */
@ -222,10 +223,10 @@ draw_markup_on_element(markup_parser_data_t *p, const char *elem,
break; break;
case 't': /* text */ case 't': /* text */
for(; *names; names++, values++) for(; *names; names++, values++)
switch(a_tokenize(*names, -1)) switch(a_tokenize(*names, (len = a_strlen(*names))))
{ {
case A_TK_ALIGN: case A_TK_ALIGN:
data->align = draw_align_get_from_str(*values); data->align = draw_align_fromstr(*values, len);
break; break;
case A_TK_SHADOW: case A_TK_SHADOW:
xcolor_new(data->connection, data->phys_screen, *values, xcolor_new(data->connection, data->phys_screen, *values,
@ -1019,11 +1020,12 @@ draw_text_extents(xcb_connection_t *conn, int phys_screen, font_t *font, const c
/** Transform a string to a alignment_t type. /** Transform a string to a alignment_t type.
* Recognized string are left, center or right. Everything else will be * Recognized string are left, center or right. Everything else will be
* recognized as AlignAuto. * recognized as AlignAuto.
* \param align string with align text * \param align Atring with align text.
* \return alignment_t type * \param len The string length.
* \return An alignment_t type.
*/ */
alignment_t alignment_t
draw_align_get_from_str(const char *align) draw_align_fromstr(const char *align, ssize_t len)
{ {
switch (a_tokenize(align, -1)) switch (a_tokenize(align, -1))
{ {

View File

@ -188,7 +188,7 @@ void draw_image(draw_context_t *, int, int, int, draw_image_t *);
void draw_image_from_argb_data(draw_context_t *, int, int, int, int, int, unsigned char *); void draw_image_from_argb_data(draw_context_t *, int, int, int, int, int, unsigned char *);
void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int); void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_parser_data_t *); area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_parser_data_t *);
alignment_t draw_align_get_from_str(const char *); alignment_t draw_align_fromstr(const char *, ssize_t);
bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *); bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *);
void area_array_remove(area_array_t *, area_t); void area_array_remove(area_array_t *, area_t);

View File

@ -1,4 +1,5 @@
align align
auto
bg bg
border_padding border_padding
border_width border_width

View File

@ -27,6 +27,7 @@
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
#include "tokenize.h"
/** Print error and exit with EXIT_FAILURE code. /** Print error and exit with EXIT_FAILURE code.
*/ */
@ -94,24 +95,37 @@ name_func_rlookup(void * funcp, const name_func_link_t *list)
return NULL; return NULL;
} }
/** Get a position type from a string.
* \param pos The position.
* \param len The string length, -1 if unknown.
* \return A position.
*/
position_t position_t
position_get_from_str(const char *pos) position_fromstr(const char *pos, ssize_t len)
{ {
if(!a_strcmp(pos, "top")) switch(a_tokenize(pos, len))
{
case A_TK_TOP:
return Top; return Top;
else if(!a_strcmp(pos, "bottom")) case A_TK_BOTTOM:
return Bottom; return Bottom;
else if(!a_strcmp(pos, "right")) case A_TK_RIGHT:
return Right; return Right;
else if(!a_strcmp(pos, "left")) case A_TK_LEFT:
return Left; return Left;
else if(!a_strcmp(pos, "auto")) case A_TK_AUTO:
return Auto; return Auto;
default:
return Off; return Off;
}
} }
/** Convert a position type to a string.
* \param p The position.
* \return A position string.
*/
char * char *
position_to_str(position_t p) position_tostr(position_t p)
{ {
switch(p) switch(p)
{ {

View File

@ -321,8 +321,8 @@ void _fatal(int, const char *, const char *, ...)
void _warn(int, const char *, const char *, ...) void _warn(int, const char *, const char *, ...)
__attribute__ ((format(printf, 3, 4))); __attribute__ ((format(printf, 3, 4)));
position_t position_get_from_str(const char *); position_t position_fromstr(const char *, ssize_t);
char * position_to_str(position_t); char * position_tostr(position_t);
void *name_func_lookup(const char *, const name_func_link_t *); void *name_func_lookup(const char *, const name_func_link_t *);
const char * name_func_rlookup(void *, const name_func_link_t *); const char * name_func_rlookup(void *, const name_func_link_t *);
void a_exec(const char *); void a_exec(const char *);

4
lua.c
View File

@ -404,10 +404,10 @@ luaA_colors_set(lua_State *L)
{ {
const char *fg, *bg; const char *fg, *bg;
luaA_checktable(L, 1); luaA_checktable(L, 1);
if((fg = luaA_getopt_string(L, 1, "fg", NULL))) if((fg = luaA_getopt_string(L, 1, "fg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
fg, &globalconf.colors.fg); fg, &globalconf.colors.fg);
if((bg = luaA_getopt_string(L, 1, "bg",NULL))) if((bg = luaA_getopt_string(L, 1, "bg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
bg, &globalconf.colors.bg); bg, &globalconf.colors.bg);
return 0; return 0;

4
lua.h
View File

@ -147,12 +147,12 @@ luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
} }
static inline const char * static inline const char *
luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def) luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def, size_t *len)
{ {
/* assume that table is first on stack */ /* assume that table is first on stack */
lua_getfield(L, idx, name); lua_getfield(L, idx, name);
/* return luaL_optnumber result */ /* return luaL_optnumber result */
return luaL_optstring(L, -1, def); return luaL_optlstring(L, -1, def, len);
} }
static inline bool static inline bool

View File

@ -58,12 +58,14 @@ typedef enum
/** Convert a corner name into a corner type. /** Convert a corner name into a corner type.
* \param str A string. * \param str A string.
* \param len String length.
* \return A corner type. * \return A corner type.
*/ */
static corner_t static corner_t
a_strtocorner(const char *str) a_strtocorner(const char *str, size_t len)
{ {
switch (a_tokenize(str, -1)) { switch (a_tokenize(str, len))
{
case A_TK_BOTTOMRIGHT: return BottomRightCorner; case A_TK_BOTTOMRIGHT: return BottomRightCorner;
case A_TK_BOTTOMLEFT: return BottomLeftCorner; case A_TK_BOTTOMLEFT: return BottomLeftCorner;
case A_TK_TOPLEFT: return TopLeftCorner; case A_TK_TOPLEFT: return TopLeftCorner;
@ -1058,11 +1060,12 @@ luaA_client_mouse_resize(lua_State *L)
client_t **c = luaA_checkudata(L, 1, "client"); client_t **c = luaA_checkudata(L, 1, "client");
corner_t corner = AutoCorner; corner_t corner = AutoCorner;
bool infobox = true; bool infobox = true;
size_t len;
if(lua_gettop(L) == 2 && !lua_isnil(L, 2)) if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
{ {
luaA_checktable(L, 2); luaA_checktable(L, 2);
corner = a_strtocorner(luaA_getopt_string(L, 2, "corner", "auto")); corner = a_strtocorner(luaA_getopt_string(L, 2, "corner", "auto", &len), len);
infobox = luaA_getopt_boolean(L, 2, "infobox", true); infobox = luaA_getopt_boolean(L, 2, "infobox", true);
} }

View File

@ -287,9 +287,10 @@ statusbar_position_update(statusbar_t *statusbar, position_t position)
static int static int
luaA_statusbar_position_set(lua_State *L) luaA_statusbar_position_set(lua_State *L)
{ {
size_t len;
statusbar_t *s, **sb = luaA_checkudata(L, 1, "statusbar"); statusbar_t *s, **sb = luaA_checkudata(L, 1, "statusbar");
const char *pos = luaL_checkstring(L, 2); const char *pos = luaL_checklstring(L, 2, &len);
position_t position = position_get_from_str(pos); position_t position = position_fromstr(pos, len);
if(position != (*sb)->position) if(position != (*sb)->position)
{ {
@ -314,7 +315,7 @@ static int
luaA_statusbar_position_get(lua_State *L) luaA_statusbar_position_get(lua_State *L)
{ {
statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); statusbar_t **sb = luaA_checkudata(L, 1, "statusbar");
lua_pushstring(L, position_to_str((*sb)->position)); lua_pushstring(L, position_tostr((*sb)->position));
return 1; return 1;
} }
@ -328,9 +329,10 @@ luaA_statusbar_position_get(lua_State *L)
static int static int
luaA_statusbar_align_set(lua_State *L) luaA_statusbar_align_set(lua_State *L)
{ {
size_t len;
statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); statusbar_t **sb = luaA_checkudata(L, 1, "statusbar");
const char *al = luaL_checkstring(L, 2); const char *al = luaL_checklstring(L, 2, &len);
alignment_t align = draw_align_get_from_str(al); alignment_t align = draw_align_fromstr(al, len);
(*sb)->align = align; (*sb)->align = align;
statusbar_position_update(*sb, (*sb)->position); statusbar_position_update(*sb, (*sb)->position);
@ -492,7 +494,8 @@ static int
luaA_statusbar_new(lua_State *L) luaA_statusbar_new(lua_State *L)
{ {
statusbar_t *sb; statusbar_t *sb;
const char *color; const char *buf;
size_t len;
luaA_checktable(L, 1); luaA_checktable(L, 1);
@ -501,20 +504,20 @@ luaA_statusbar_new(lua_State *L)
sb->name = luaA_name_init(L); sb->name = luaA_name_init(L);
lua_getfield(L, 1, "fg"); lua_getfield(L, 1, "fg");
if((color = luaL_optstring(L, -1, NULL))) if((buf = luaL_optstring(L, -1, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &sb->colors.fg); buf, &sb->colors.fg);
else else
sb->colors.fg = globalconf.colors.fg; sb->colors.fg = globalconf.colors.fg;
lua_getfield(L, 1, "bg"); lua_getfield(L, 1, "bg");
if((color = luaL_optstring(L, -1, NULL))) if((buf = luaL_optstring(L, -1, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &sb->colors.bg); buf, &sb->colors.bg);
else else
sb->colors.bg = globalconf.colors.bg; sb->colors.bg = globalconf.colors.bg;
sb->align = draw_align_get_from_str(luaA_getopt_string(L, 1, "align", "left")); sb->align = draw_align_fromstr(luaA_getopt_string(L, 1, "align", "left", &len), len);
sb->width = luaA_getopt_number(L, 1, "width", 0); sb->width = luaA_getopt_number(L, 1, "width", 0);
if(sb->width > 0) if(sb->width > 0)
@ -524,7 +527,8 @@ luaA_statusbar_new(lua_State *L)
/* 1.5 as default factor, it fits nice but no one knows why */ /* 1.5 as default factor, it fits nice but no one knows why */
sb->height = 1.5 * globalconf.font->height; sb->height = 1.5 * globalconf.font->height;
sb->position = position_get_from_str(luaA_getopt_string(L, 1, "position", "top")); buf = luaA_getopt_string(L, 1, "position", "top", &len);
sb->position = position_fromstr(buf, len);
return luaA_statusbar_userdata_new(L, sb); return luaA_statusbar_userdata_new(L, sb);
} }

2
tag.c
View File

@ -357,7 +357,7 @@ luaA_tag_new(lua_State *L)
mwfact = luaA_getopt_number(L, 1, "mwfact", 0.5); mwfact = luaA_getopt_number(L, 1, "mwfact", 0.5);
ncol = luaA_getopt_number(L, 1, "ncol", 1); ncol = luaA_getopt_number(L, 1, "ncol", 1);
nmaster = luaA_getopt_number(L, 1, "nmaster", 1); nmaster = luaA_getopt_number(L, 1, "nmaster", 1);
lay = luaA_getopt_string(L, 1, "layout", "tile"); lay = luaA_getopt_string(L, 1, "layout", "tile", NULL);
layout = name_func_lookup(lay, LayoutList); layout = name_func_lookup(lay, LayoutList);

View File

@ -298,12 +298,13 @@ luaA_titlebar_new(lua_State *L)
{ {
titlebar_t *tb; titlebar_t *tb;
const char *color; const char *color;
size_t len;
luaA_checktable(L, 1); luaA_checktable(L, 1);
tb = p_new(titlebar_t, 1); tb = p_new(titlebar_t, 1);
tb->align = draw_align_get_from_str(luaA_getopt_string(L, 1, "align", "left")); tb->align = draw_align_fromstr(luaA_getopt_string(L, 1, "align", "left", &len), len);
tb->width = luaA_getopt_number(L, 1, "width", 0); tb->width = luaA_getopt_number(L, 1, "width", 0);
tb->height = luaA_getopt_number(L, 1, "height", 0); tb->height = luaA_getopt_number(L, 1, "height", 0);
@ -311,21 +312,21 @@ luaA_titlebar_new(lua_State *L)
/* 1.5 as default factor, it fits nice but no one knows why */ /* 1.5 as default factor, it fits nice but no one knows why */
tb->height = 1.5 * globalconf.font->height; tb->height = 1.5 * globalconf.font->height;
tb->position = position_get_from_str(luaA_getopt_string(L, 1, "position", "top")); tb->position = position_fromstr(luaA_getopt_string(L, 1, "position", "top", &len), len);
if((color = luaA_getopt_string(L, -1, "fg", NULL))) if((color = luaA_getopt_string(L, -1, "fg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &tb->colors.fg); color, &tb->colors.fg);
else else
tb->colors.fg = globalconf.colors.fg; tb->colors.fg = globalconf.colors.fg;
if((color = luaA_getopt_string(L, 1, "bg", NULL))) if((color = luaA_getopt_string(L, 1, "bg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &tb->colors.bg); color, &tb->colors.bg);
else else
tb->colors.bg = globalconf.colors.bg; tb->colors.bg = globalconf.colors.bg;
if((color = luaA_getopt_string(L, 1, "border_color", NULL))) if((color = luaA_getopt_string(L, 1, "border_color", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &tb->border.color); color, &tb->border.color);
@ -428,11 +429,11 @@ luaA_titlebar_colors_set(lua_State *L)
luaA_checktable(L, 2); luaA_checktable(L, 2);
if((color = luaA_getopt_string(L, 2, "fg", NULL))) if((color = luaA_getopt_string(L, 2, "fg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &(*tb)->colors.fg); color, &(*tb)->colors.fg);
if((color = luaA_getopt_string(L, 2, "bg", NULL))) if((color = luaA_getopt_string(L, 2, "bg", NULL, NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &(*tb)->colors.bg); color, &(*tb)->colors.bg);
@ -459,7 +460,7 @@ luaA_titlebar_border_set(lua_State *L)
luaA_checktable(L, 2); luaA_checktable(L, 2);
if((color = luaA_getopt_string(L, 2, "color", NULL))) if((color = luaA_getopt_string(L, 2, "color", NULL, NULL)))
{ {
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_new(globalconf.connection, globalconf.default_screen,
color, &(*tb)->border.color); color, &(*tb)->border.color);

View File

@ -310,11 +310,12 @@ luaA_widget_new(lua_State *L)
widget_t *w = NULL; widget_t *w = NULL;
widget_constructor_t *wc; widget_constructor_t *wc;
alignment_t align; alignment_t align;
size_t len;
luaA_checktable(L, 1); luaA_checktable(L, 1);
align = draw_align_get_from_str(luaA_getopt_string(L, 1, "align", "left")); align = draw_align_fromstr(luaA_getopt_string(L, 1, "align", "left", &len), len);
type = luaA_getopt_string(L, 1, "type", NULL); type = luaA_getopt_string(L, 1, "type", NULL, NULL);
if((wc = name_func_lookup(type, WidgetList))) if((wc = name_func_lookup(type, WidgetList)))
w = wc(align); w = wc(align);

View File

@ -322,7 +322,7 @@ graph_tell(widget_t *widget, const char *property, const char *new_value)
return WIDGET_ERROR_FORMAT_COLOR; return WIDGET_ERROR_FORMAT_COLOR;
return WIDGET_NOERROR; return WIDGET_NOERROR;
case A_TK_GROW: case A_TK_GROW:
switch((d->grow = position_get_from_str(new_value))) switch((d->grow = position_fromstr(new_value, -1)))
{ {
case Left: case Left:
case Right: case Right: