X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fconf.c;h=13dc5d50c5cdeb1c4ddd8891c49560cf902944f3;hb=607bf2d55826f861d12b774884cb201bc683051c;hp=979fd91c4bfb8f7c26e877aba9e915dfb10ae021;hpb=11ab59f9d7b3c247bf7b2fecafde9e60e96ea60f;p=ncmpc.git diff --git a/src/conf.c b/src/conf.c index 979fd91..13dc5d5 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1,20 +1,21 @@ -/* - * (c) 2004 by Kalle Wallin - * +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 The Music Player Daemon Project + * Project homepage: http://musicpd.org + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ + + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ #include "conf.h" #include "config.h" @@ -54,6 +55,7 @@ #define CONF_FIND_SHOW_LAST "find-show-last" #define CONF_AUDIBLE_BELL "audible-bell" #define CONF_VISIBLE_BELL "visible-bell" +#define CONF_BELL_ON_WRAP "bell-on-wrap" #define CONF_XTERM_TITLE "set-xterm-title" #define CONF_ENABLE_MOUSE "enable-mouse" #define CONF_CROSSFADE_TIME "crossfade-time" @@ -71,14 +73,7 @@ #define CONF_SCROLL_SEP "scroll-sep" #define CONF_VISIBLE_BITRATE "visible-bitrate" #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list" - -typedef enum { - KEY_PARSER_UNKNOWN, - KEY_PARSER_CHAR, - KEY_PARSER_DEC, - KEY_PARSER_HEX, - KEY_PARSER_DONE -} key_parser_state_t; +#define CONF_DISPLAY_TIME "display-time" static bool str2bool(char *str) @@ -96,77 +91,32 @@ print_error(const char *msg, const char *input) } static int -parse_key_value(char *str, size_t len, char **end) +parse_key_value(char *str, char **end) { - size_t i; - int value; - key_parser_state_t state; - - i=0; - value=0; - state=KEY_PARSER_UNKNOWN; - *end = str; - - while (i < len && state != KEY_PARSER_DONE) { - int next = 0; - int c = str[i]; - - if( i+1 str+len ) - *end = str+len; + *end = str + 3; + return str[1]; + } else { + long value = strtol(str, end, 0); + if (*end == str) { + print_error(_("Malformed hotkey definition"), str); + return -1; + } - return value; + return (int)value; + } } static int parse_key_definition(char *str) { char buf[MAX_LINE_LENGTH]; - char *p, *end; + char *p; size_t len = strlen(str), i; int j,key; int keys[MAX_COMMAND_KEYS]; @@ -179,7 +129,9 @@ parse_key_definition(char *str) while (i < len && str[i] != '=' && !g_ascii_isspace(str[i])) buf[j++] = str[i++]; if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) { - print_error(_("Unknown key command"), buf); + /* the hotkey configuration contains an unknown + command */ + print_error(_("Unknown command"), buf); return -1; } @@ -190,31 +142,26 @@ parse_key_definition(char *str) /* get the value part */ memset(buf, 0, MAX_LINE_LENGTH); g_strlcpy(buf, str+i, MAX_LINE_LENGTH); - len = strlen(buf); - if (len == 0) { - print_error(_("Incomplete key definition"), str); + if (*buf == 0) { + /* the hotkey configuration line is incomplete */ + print_error(_("Incomplete hotkey configuration"), str); return -1; } /* parse key values */ i = 0; key = 0; - len = strlen(buf); p = buf; - end = buf+len; memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS); - while (i < MAX_COMMAND_KEYS && p < end && - (key = parse_key_value(p, len + 1, &p)) >= 0) { + while (i < MAX_COMMAND_KEYS && *p != 0 && + (key = parse_key_value(p, &p)) >= 0) { keys[i++] = key; - while (p < end && (*p==',' || *p==' ' || *p=='\t')) + while (*p==',' || *p==' ' || *p=='\t') p++; - len = strlen(p); } - if (key < 0) { - print_error(_("Bad key definition"), str); + if (key < 0) return -1; - } return assign_keys(cmd, keys); } @@ -225,97 +172,109 @@ parse_timedisplay_type(const char *str) if (!strcmp(str,"elapsed") || !strcmp(str,"remaining")) return str; else { + /* translators: ncmpc supports displaying the + "elapsed" or "remaining" time of a song being + played; in this case, the configuration file + contained an invalid setting */ print_error(_("Bad time display type"), str); return DEFAULT_TIMEDISPLAY_TYPE; } } #ifdef ENABLE_COLORS +static char * +separate_value(char *p) +{ + char *value; + + value = strchr(p, '='); + if (value == NULL) { + /* an equals sign '=' was expected while parsing a + configuration file line */ + fprintf(stderr, "%s\n", _("Missing '='")); + return NULL; + } + + *value++ = 0; + + g_strchomp(p); + return g_strchug(value); +} + static int parse_color(char *str) { - const char *name = str; - const char *value = NULL; - int len,i; + char *value; - i=0; - len=strlen(str); - /* get the color name */ - while (i < len && str[i] != '=' && !g_ascii_isspace(str[i])) - i++; + value = separate_value(str); + if (value == NULL) + return -1; - /* skip whitespace */ - while (i < len && (str[i] == '=' || g_ascii_isspace(str[i]))) { - str[i]='\0'; - i++; - } + return colors_assign(str, value); +} + +/** + * Returns the first non-whitespace character after the next comma + * character, or the end of the string. This is used to parse comma + * separated values. + */ +static char * +after_comma(char *p) +{ + char *comma = strchr(p, ','); - if (i < len) - value = str+i; + if (comma != NULL) { + *comma++ = 0; + comma = g_strchug(comma); + } else + comma = p + strlen(p); - return colors_assign(name, value); + g_strchomp(p); + return comma; } static int parse_color_definition(char *str) { char buf[MAX_LINE_LENGTH]; - char *p, *end, *name; - size_t len = strlen(str), i; - int j,value; + char *value; short color, rgb[3]; + value = separate_value(str); + if (value == NULL) + return -1; + /* get the command name */ - i=0; - j=0; - memset(buf, 0, MAX_LINE_LENGTH); - while (i < len && str[i] != '=' && !g_ascii_isspace(str[i])) - buf[j++] = str[i++]; - color = colors_str2color(buf); + color = colors_str2color(str); if (color < 0) { - print_error(_("Bad color"), buf); + print_error(_("Bad color name"), buf); return -1; } - name = g_strdup(buf); - /* skip whitespace */ - while (i < len && (str[i] == '=' || g_ascii_isspace(str[i]))) - i++; + /* parse r,g,b values */ - /* get the value part */ - memset(buf, 0, MAX_LINE_LENGTH); - g_strlcpy(buf, str+i, MAX_LINE_LENGTH); - len = strlen(buf); - if (len == 0) { - print_error(_("Incomplete color definition"), str); - g_free(name); - return -1; - } + for (unsigned i = 0; i < 3; ++i) { + char *next = after_comma(value), *endptr; + if (*value == 0) { + print_error(_("Incomplete color definition"), str); + return -1; + } - /* parse r,g.b values with the key definition parser */ - i = 0; - value = 0; - len = strlen(buf); - p = buf; - end = buf + len; - memset(rgb, 0, sizeof(short)*3); - while (i < 3 && p < end && - (value = parse_key_value(p,len+1,&p)) >= 0) { - rgb[i++] = value; - while( p= 0 && g_ascii_isspace(line[i])) { - line[i] = '\0'; - i--; - } + while (fgets(line, sizeof(line), file) != NULL) { + char *p = g_strchug(line); - len = i + 1; - if (len > 0) { - i = 0; - /* skip whitespace */ - while (i < len && g_ascii_isspace(line[i])) - i++; - - /* continue if this line is not a comment */ - if (line[i] != COMMENT_TOKEN) { - parse_line(line + i); - } - } + if (*p != 0 && *p != COMMENT_TOKEN) + parse_line(g_strchomp(p)); } + fclose(file); return 0; }