PARA_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
PARA_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
-
-#define OSL_ERRORS \
- PARA_ERROR(BAD_DB_DIR, "invalid database directory"), \
- PARA_ERROR(NO_COLUMN_DESC, "missing column description"), \
- PARA_ERROR(BAD_NAME, "invalid name for a column/table"), \
- PARA_ERROR(BAD_STORAGE_TYPE, "invalid storage type"), \
- PARA_ERROR(BAD_STORAGE_FLAGS, "invalid storage flags"), \
- PARA_ERROR(NO_COLUMN_NAME, "missing column name"), \
- PARA_ERROR(NO_COLUMNS, "at least one column required"), \
- PARA_ERROR(BAD_COLUMN_NAME, "invalid name for a table column"), \
- PARA_ERROR(NO_UNIQUE_RBTREE_COLUMN, "need at least one mapped column with OSL_UNIQE and OSL_RBTREE OSL"), \
- PARA_ERROR(NO_RBTREE_COL, "at least one column needs an rbtree"), \
- PARA_ERROR(DUPLICATE_COL_NAME, "column name given twice"), \
- PARA_ERROR(BAD_STORAGE_SIZE, "invalid storage size"), \
- PARA_ERROR(NO_COMPARE_FUNC, "missing compare function"), \
- PARA_ERROR(BAD_DATA_SIZE, "wrong data size for fixed-size column"), \
- PARA_ERROR(NOT_MAPPED, "file not mapped"), \
- PARA_ERROR(ALREADY_MAPPED, "file already mapped"), \
- PARA_ERROR(BAD_SIZE, "invalid size specified"), \
- PARA_ERROR(TRUNC, "failed to truncate file"), \
- PARA_ERROR(BAD_TABLE, "table not open"), \
- PARA_ERROR(BAD_TABLE_DESC, "invalid table description"), \
- PARA_ERROR(RB_KEY_EXISTS, "key already exists in rbtree"), \
- PARA_ERROR(RB_KEY_NOT_FOUND, "key not found in rbtree"), \
- PARA_ERROR(BAD_ROW_NUM, "invalid row number"), \
- PARA_ERROR(INDEX_CORRUPTION, "index corruption detected"), \
- PARA_ERROR(INVALID_OBJECT, "reference to invalid object"), \
- PARA_ERROR(STAT, "can not stat file"), \
- PARA_ERROR(WRITE, "write error"), \
- PARA_ERROR(LSEEK, "lseek error"), \
- PARA_ERROR(BUSY, "table is busy"), \
- PARA_ERROR(SHORT_TABLE, "table too short"), \
- PARA_ERROR(NO_MAGIC, "missing table header magic"), \
- PARA_ERROR(VERSION_MISMATCH, "table version not supported"), \
- PARA_ERROR(BAD_COLUMN_NUM, "invalid column number"), \
- PARA_ERROR(BAD_TABLE_FLAGS, "invalid flags in table description"), \
- PARA_ERROR(BAD_ROW, "invalid row"), \
-
-
-
#define AFS_ERRORS \
PARA_ERROR(BAD_TABLE_NAME, "invalid table"), \
PARA_ERROR(INPUT_TOO_LARGE, "input too large for stdin command"), \
PARA_ERROR(AFS_SIGNAL, "afs caught deadly signal"), \
PARA_ERROR(AFS_SOCKET, "afs socket not writable"), \
PARA_ERROR(AFS_SHORT_READ, "short read from afs socket"), \
+ PARA_ERROR(OSL, "osl error"), \
#define MOOD_ERRORS \
- PARA_ERROR(MOOD_SYNTAX, "mood syntax error"), \
PARA_ERROR(NO_MOOD, "no mood available"), \
- PARA_ERROR(NOT_ADMISSIBLE, "file is not admissible"), \
PARA_ERROR(DUMMY_ROW, "attempted to access blob dummy object"), \
+#define MM_ERRORS \
+ PARA_ERROR(MOOD_SYNTAX, "mood syntax error"), \
+
+
#define ATTRIBUTE_ERRORS \
PARA_ERROR(ATTR_SYNTAX, "attribute syntax error"), \
PARA_ERROR(NO_ATTRIBUTES, "no attributes defined yet"), \
#define STAT_ERRORS \
PARA_ERROR(TOO_MANY_CLIENTS, "maximal number of stat clients exceeded"), \
PARA_ERROR(UNKNOWN_STAT_ITEM, "status item not recognized"), \
+ PARA_ERROR(STAT_ITEM_PARSE, "failed to parse status item"), \
#define OGGDEC_FILTER_ERRORS \
#define GRAB_CLIENT_ERRORS \
- PARA_ERROR(PEDANTIC_GRAB, "fd not ready and pedantic grab requested"), \
PARA_ERROR(GC_WRITE, "grab client write error"), \
PARA_ERROR(BAD_GC_SLOT, "invalid slot requested by grab client"), \
PARA_ERROR(BAD_GC_FILTER_NUM, "invalid filter number given"), \
PARA_ERROR(GC_SYNTAX, "grab client syntax error"), \
- PARA_ERROR(GC_HELP_GIVEN, ""), /* not really an error */ \
- PARA_ERROR(GC_VERSION_GIVEN, ""), /* not really an error */ \
#define MP3DEC_FILTER_ERRORS \
PARA_ERROR(STRTOLL, "unknown strtoll error"), \
PARA_ERROR(ATOI_NO_DIGITS, "no digits found in string"), \
PARA_ERROR(ATOI_JUNK_AT_END, "further characters after number"), \
+ PARA_ERROR(SIZE_PREFIX, "bad size prefix"), \
+ PARA_ERROR(REGEX, "regular expression error") \
#define EXEC_ERRORS \
#define COMMAND_ERRORS \
PARA_ERROR(COMMAND_SYNTAX, "syntax error in command"), \
- PARA_ERROR(AUTH, "did not receive auth request"), \
+ PARA_ERROR(AUTH_REQUEST, "did not receive auth request"), \
PARA_ERROR(NO_AUDIO_FILE, "no audio file"), \
PARA_ERROR(BAD_CMD, "invalid command"), \
PARA_ERROR(PERM, "permission denied"), \
PARA_ERROR(LOCK, "lock error"), \
PARA_ERROR(SENDER_CMD, "command not supported by this sender"), \
PARA_ERROR(SERVER_CRASH, "para_server crashed -- can not live without it"), \
- PARA_ERROR(BAD_USER, "you don't exist. Go away."), \
+ PARA_ERROR(BAD_USER, "auth request for invalid user"), \
+ PARA_ERROR(BAD_AUTH, "authentication failure"), \
#define DCCP_RECV_ERRORS \
*/
#define SYSTEM_ERROR_BIT 30
+/**
+ * Like the SYSTEM_ERROR_BIT, but indicates an error from the osl library.
+ */
+#define OSL_ERROR_BIT 29
+
/** Check whether the system error bit is set. */
#define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
+/** Check whether the osl error bit is set. */
+#define IS_OSL_ERROR(num) (!!((num) & (1 << OSL_ERROR_BIT)))
+
/** Set the system error bit for the given number. */
#define ERRNO_TO_PARA_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
+/** Set the osl error bit for the given number. */
+#define OSL_ERRNO_TO_PARA_ERROR(num) ((num) | (1 << OSL_ERROR_BIT))
+
/** Check whether a given number is a system error number.
*
* \param num The value to be checked.
_static_inline_ const char *para_strerror(int num)
{
assert(num > 0);
+#ifdef _OSL_H
+ if (IS_OSL_ERROR(num))
+ return osl_strerror(num & ((1 << OSL_ERROR_BIT) - 1));
+#endif
if (IS_SYSTEM_ERROR(num))
- return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
- else
- return para_errlist[ERRNUM_TO_SS(num)][ERRNUM_TO_INDEX(num)];
+ return strerror(num & ((1 << SYSTEM_ERROR_BIT) - 1));
+ return para_errlist[ERRNUM_TO_SS(num)][ERRNUM_TO_INDEX(num)];
}
+
+/**
+ * Wrapper for osl library calls.
+ *
+ * \param ret The return value of an osl library function.
+ *
+ * This should be used for all calls to osl functions that return an osl error
+ * code. It changes the return value appropriately so that it can be used for
+ * printing the correct error message vi para_strerror().
+ *
+ * \return \a ret if \a ret >= 0, a paraslash error code otherwise.
+ */
+_static_inline_ int osl(int ret)
+{
+ if (ret >= 0)
+ return ret;
+ return -OSL_ERRNO_TO_PARA_ERROR(-ret);
+}
+
/**
* Define the error list for one subsystem.
#
/** \file mood.c Paraslash's mood handling functions. */
-#include <fnmatch.h>
+#include <regex.h>
+#include <osl.h>
+
#include "para.h"
#include "error.h"
#include "string.h"
#include "afs.h"
#include "list.h"
#include "ipc.h"
+#include "mm.h"
/**
* Contains statistical data of the currently admissible audio files.
};
struct afs_statistics statistics;
-/**
- * Assign scores according to a mood_method.
- *
- * Each mood_method has its own mood_score_function. The first three parameters
- * passed to that function are informations about the audio file whose score is
- * to be computed. The data argument depends on the mood method this function
- * is used for. It usually is the argument given at the end of a mood line.
- *
- * Mood score functions must return values between -100 and +100 inclusively.
- * Boolean score functions should always return either -100 or +100.
- *
- * \sa struct mood_method, mood_parser.
- */
-typedef int mood_score_function(const char *path, const struct afs_info *afsi,
- const struct afh_info *afhi, const void *data);
-
-/**
- * Pre-process a mood line.
- *
- * The mood_parser of a mood_method is called once at mood open time for each
- * line of the current mood definition that contains the mood_method's name as
- * a keyword. The line is passed to the mood_parser as the first argument. The
- * mood_parser must determine whether the line is syntactically correct and
- * return a positive value if so and a negative value otherwise.
- *
- * Some mood parsers pre-process the data given in the mood line to compute a
- * structure which depends of the particular mood_method and which is used
- * later in the mood_score_function of the mood_method. The mood_parser may
- * store a pointer to its structure via the second argument.
- *
- * \sa mood_open(), mood_cleanup_function, mood_score_function.
- */
-typedef int mood_parser(const char *, void **);
-
-/**
- * Deallocate resources which were allocated by the mood_parser.
- *
- * This optional function of a mood_method is used to free any resources
- * allocated in mood_open() by the mood_parser. The argument passed is a
- * pointer to the mood_method specific data structure that was returned by the
- * mood_parser.
- *
- * \sa mood_parser.
- */
-typedef void mood_cleanup_function(void *);
-
-/**
- * Used for scoring and to determine whether a file is admissible.
- */
-struct mood_method {
- /** The name of the method. */
- const char *name;
- /** Pointer to the mood parser. */
- mood_parser *parser;
- /** Pointer to the score function */
- mood_score_function *score_function;
- /** Optional cleanup function. */
- mood_cleanup_function *cleanup;
-};
-
/**
* Each line of the current mood corresponds to a mood_item.
*/
return res;
}
- /* returns 1 if row matches score item, negative otherwise */
- static int add_item_score(const struct osl_row *row, struct mood_item *item, long *score,
- long *score_arg_sum)
-static int mm_no_attributes_set_parser(const char *arg, __a_unused void **ignored)
-{
- if (arg && *arg)
- PARA_WARNING_LOG("ignored junk at eol: %s\n", arg);
- return 1;
-}
-
-static int mm_no_attributes_set_score_function(__a_unused const char *path,
- const struct afs_info *afsi,
- __a_unused const struct afh_info *afhi,
- __a_unused const void *data)
-{
- if (!afsi->attributes)
- return 100;
- return -100;
-}
-
-static int mm_played_rarely_score_function(__a_unused const char *path,
- const struct afs_info *afsi,
- __a_unused const struct afh_info *afhi,
- __a_unused const void *data)
-{
- unsigned num;
- int ret = get_num_admissible_files(&num);
-
- if (ret < 0)
- return 0;
- if (statistics.num_played_sum - num * afsi->num_played
- > int_sqrt(statistics.num_played_qd * num))
- return 100;
- return -100;
-}
-
-static int mm_played_rarely_parser(const char *arg, __a_unused void **ignored)
-{
- if (arg && *arg)
- PARA_WARNING_LOG("ignored junk at eol: %s\n", arg);
- return 1;
-}
-
-static int mm_path_matches_score_function(const char *path,
- __a_unused const struct afs_info *afsi,
- __a_unused const struct afh_info *afhi,
- const void *data)
-{
- if (fnmatch(data, path, 0))
- return -100;
- return 100;
-}
-
-static int mm_path_matches_parser(const char *arg, void **data)
-{
- *data = para_strdup(arg);
- return 1;
-}
-
-static void mm_path_matches_cleanup(void *data)
-{
- free(data);
-}
-
-static int mm_is_set_parser(const char *arg, void **bitnum)
-{
- unsigned char *c = para_malloc(1);
- int ret = get_attribute_bitnum_by_name(arg, c);
-
- if (ret >= 0)
- *bitnum = c;
- else
- free(c);
- return ret;
-}
-
-static int mm_is_set_score_function(__a_unused const char *path,
- __a_unused const struct afs_info *afsi,
- __a_unused const struct afh_info *afhi,
- const void *data)
-{
- const unsigned char *bn = data;
- if (afsi->attributes & (1ULL << *bn))
- return 100;
- return -100;
-}
-
+ /* returns 1 if row matches score item, 0 if not, negative on errors */
+ static int get_item_score(const struct osl_row *row, struct mood_item *item,
+ long *score, long *score_arg_sum)
{
struct afs_info afsi;
struct afh_info afhi;
char *path;
- int ret;
+ int ret, match = 1;
*score_arg_sum += item->random_score? 100 : PARA_ABS(item->score_arg);
ret = 100;
ret = get_afhi_of_row(row, &afhi);
if (ret< 0)
return ret;
- free(afhi.info_string); /* don't need the tag info */
ret = get_audio_file_path_of_row(row, &path);
if (ret< 0)
return ret;
ret = item->method->score_function(path, &afsi, &afhi,
item->parser_data);
if ((ret < 0 && !item->logical_not) || (ret >= 0 && item->logical_not))
- return -1; /* no match */
+ match = 0; /* no match */
}
if (item->random_score)
- *score += PARA_ABS(ret) * para_random(100);
+ *score = PARA_ABS(ret) * para_random(100);
else
- *score += PARA_ABS(ret) * item->score_arg;
- return 1;
+ *score = PARA_ABS(ret) * item->score_arg;
+ return match;
}
+ /* returns 1 if row admissible, 0 if not, negative on errors */
static int compute_mood_score(const struct osl_row *aft_row, struct mood *m,
long *result)
{
struct mood_item *item;
- int match = 0;
- long score_arg_sum = 0, score = 0;
+ int ret, match = 0;
+ long score_arg_sum = 0, score = 0, item_score;
if (!m)
return -E_NO_MOOD;
/* reject audio file if it matches any entry in the deny list */
- list_for_each_entry(item, &m->deny_list, mood_item_node)
- if (add_item_score(aft_row, item, &score, &score_arg_sum) > 0)
- return -E_NOT_ADMISSIBLE;
- list_for_each_entry(item, &m->accept_list, mood_item_node)
- if (add_item_score(aft_row, item, &score, &score_arg_sum) > 0)
- match = 1;
+ list_for_each_entry(item, &m->deny_list, mood_item_node) {
+ ret = get_item_score(aft_row, item, &item_score,
+ &score_arg_sum);
+ if (ret < 0)
+ return ret;
+ if (ret > 0) /* not admissible */
+ return 0;
+ score += item_score;
+ }
+ list_for_each_entry(item, &m->accept_list, mood_item_node) {
+ ret = get_item_score(aft_row, item, &item_score,
+ &score_arg_sum);
+ if (ret < 0)
+ return ret;
+ if (ret == 0)
+ continue;
+ match = 1;
+ score += item_score;
+ }
/* reject if there is no matching entry in the accept list */
if (!match && !list_empty(&m->accept_list))
- return -E_NOT_ADMISSIBLE;
- list_for_each_entry(item, &m->score_list, mood_item_node)
- add_item_score(aft_row, item, &score, &score_arg_sum);
+ return 0;
+ list_for_each_entry(item, &m->score_list, mood_item_node) {
+ ret = get_item_score(aft_row, item, &item_score,
+ &score_arg_sum);
+ if (ret < 0)
+ return ret;
+ score += item_score;
+ }
if (score_arg_sum)
score /= score_arg_sum;
*result = score;
return 1;
}
-#define DEFINE_MOOD_METHOD(_name) \
-.parser = mm_ ## _name ## _parser, \
-.score_function = mm_ ## _name ## _score_function, \
-.name = #_name
-
-#define DEFINE_MOOD_METHOD_WITH_CLEANUP(_name) \
- DEFINE_MOOD_METHOD(_name), \
- .cleanup = mm_ ## _name ## _cleanup
-
-static const struct mood_method mood_methods[] = {
- {DEFINE_MOOD_METHOD(no_attributes_set)},
- {DEFINE_MOOD_METHOD(played_rarely)},
- {DEFINE_MOOD_METHOD(is_set)},
- {DEFINE_MOOD_METHOD_WITH_CLEANUP(path_matches)},
- {.parser = NULL}
-};
-
static void cleanup_list_entry(struct mood_item *item)
{
if (item->method && item->method->cleanup)
{
struct mood_line_parser_data *mlpd = data;
char **argv;
- char *delim = " \t";
unsigned num_words;
char **w;
int i, ret;
enum mood_line_type mlt = ML_INVALID;
struct mood_item *mi = NULL;
- char *buf = para_strdup(mood_line);
mlpd->line_num++;
- num_words = split_args(buf, &argv, delim);
- ret = 1;
+ ret = create_argv(mood_line, " \t", &argv);
+ if (ret < 0)
+ return ret;
+ num_words = ret;
if (!num_words) /* empty line */
goto out;
w = argv;
ret = -E_MOOD_SYNTAX;
if (!mood_methods[i].parser)
goto out;
- w++;
- ret = mood_methods[i].parser(*w, &mi->parser_data);
+ ret = mood_methods[i].parser(num_words - 1 - (w - argv), w,
+ &mi->parser_data);
if (ret < 0)
goto out;
mi->method = &mood_methods[i];
(mlt == ML_DENY? "deny" : "score"), mi->method);
ret = 1;
out:
- free(argv);
- free(buf);
+ free_argv(argv);
if (ret >= 0)
return ret;
if (mi) {
* \param aft_row The audio file to be added.
* \param private_data Pointer to a struct admissible_file_info.
*
- * \return Negative on errors, positive on success.
+ * \return 1 if row admissible, 0 if not, negative on errors.
*/
static int add_if_admissible(struct osl_row *aft_row, void *data)
{
long score = 0;
ret = compute_mood_score(aft_row, aa->m, &score);
- if (ret < 0)
- return (ret == -E_NOT_ADMISSIBLE)? 1 : ret;
+ if (ret <= 0)
+ return ret;
if (statistics.num >= aa->size) {
aa->size *= 2;
aa->size += 100;
return ret;
was_admissible = ret;
ret = compute_mood_score(aft_row, current_mood, &score);
+ if (ret < 0)
+ return ret;
is_admissible = (ret > 0);
if (!was_admissible && !is_admissible)
return 1;
memset(&statistics, 0, sizeof(statistics));
}
-
/**
* Change the current mood.
*
.data = mood_name,
.size = strlen(mood_name) + 1
};
- ret = osl_get_row(moods_table, BLOBCOL_NAME, &obj, &row);
+ ret = osl(osl_get_row(moods_table, BLOBCOL_NAME, &obj, &row));
if (ret < 0) {
PARA_NOTICE_LOG("no such mood: %s\n", mood_name);
return ret;