]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
improved version of split_args()
authorAndre <maan@p133.(none)>
Mon, 10 Apr 2006 17:40:11 +0000 (19:40 +0200)
committerAndre <maan@p133.(none)>
Mon, 10 Apr 2006 17:40:11 +0000 (19:40 +0200)
Thanks to Lorenzo Bettini for pointing this out.

This patch also fixes a bug with the default filter configuration which
allocated too little memory for the array of filter configurations. Now
we always allocate space for at least three entries.

audiod.c
command.c
exec.c
filter_chain.c
grab_client.c
mysql_selector.c
recv_common.c
stat.c
string.c
string.h

index 7029ea9116f9c115a5c4d5d41599fcd414ea930e..7596c5c54939e9178d186a49e21a1f5f395c93c6 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -811,6 +811,7 @@ static void check_stat_line(char *line)
        struct timeval tv;
        char *tmp;
 
+       PARA_INFO_LOG("line: %s\n", line);
        if (!line)
                return;
        itemnum = stat_line_valid(line);
@@ -1107,7 +1108,7 @@ out:
 
 static int init_stream_io(void)
 {
-       int i, ret, receiver_num;
+       int i, ret, receiver_num, nf;
        char *cmd;
 
        for (i = 0; i < conf.stream_write_cmd_given; i++) {
@@ -1156,9 +1157,11 @@ static int init_stream_io(void)
        free(cmd);
        /* filters */
        filter_init(filters);
+       nf = MAX(2,  conf.filter_given) + 1;
+       PARA_INFO_LOG("allocating space for %d filters\n", nf);
        FOR_EACH_AUDIO_FORMAT(i) {
-               afi[i].filter_conf = para_malloc((conf.filter_given + 1) * sizeof(char *));
-               afi[i].filters = para_malloc((conf.filter_given + 1) * sizeof(struct filter *));
+               afi[i].filter_conf = para_malloc(nf * sizeof(char *));
+               afi[i].filters = para_malloc(nf * sizeof(struct filter *));
        }
        if (!conf.no_default_filters_given)
                return setup_default_filters();
@@ -1445,12 +1448,12 @@ static int handle_connect(void)
        ret = check_perms(&c);
        if (ret < 0)
                goto out;
-       argc = split_args(buf, &argv, '\n');
-       PARA_INFO_LOG("argv[0]: %s\n", argv[0]);
+       argc = split_args(buf, &argv, "\n");
+       PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
        for (i = 0; cmds[i].name; i++) {
                if (strcmp(cmds[i].name, argv[0]))
                        continue;
-               ret = cmds[i].handler(clifd, argc + 1, argv);
+               ret = cmds[i].handler(clifd, argc, argv);
                goto out;
        }
        ret = -E_INVALID_AUDIOD_CMD; /* cmd not found */
@@ -1643,6 +1646,7 @@ int __noreturn main(int argc, char *argv[])
        char *cf;
        int i;
 
+       fprintf(stderr, "argc: %d\n", argc);
        valid_fd_012();
        hostname = para_hostname();
        cmdline_parser(argc, argv, &conf);
index 3b92be657bd28b49ca690923583d2b42cb5dd770..e820e41a5d1deefbdf6b40601e88e04c19c09a01 100644 (file)
--- a/command.c
+++ b/command.c
@@ -453,22 +453,21 @@ static int check_sender_args(int argc, char **argv, struct sender_command_data *
        const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
 
        scd->sender_num = -1;
-       if (argc < 0)
+       if (argc < 2)
                return -E_COMMAND_SYNTAX;
        for (i = 0; senders[i].name; i++)
-               if (!strcmp(senders[i].name, argv[0]))
+               if (!strcmp(senders[i].name, argv[1]))
                        break;
-//     PARA_DEBUG_LOG("%d:%s\n", argc, argv[0]);
+       PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
        if (!senders[i].name)
                return -E_COMMAND_SYNTAX;
        scd->sender_num = i;
        for (i = 0; subcmds[i]; i++)
-               if (!strcmp(subcmds[i], argv[1]))
+               if (!strcmp(subcmds[i], argv[2]))
                        break;
        if (!subcmds[i])
                return -E_COMMAND_SYNTAX;
        scd->cmd_num = i;
-//     scd->self = *in_addr;
        mmd_lock();
        if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
                mmd_unlock();
@@ -478,31 +477,31 @@ static int check_sender_args(int argc, char **argv, struct sender_command_data *
        switch (scd->cmd_num) {
        case SENDER_ON:
        case SENDER_OFF:
-               if (argc != 1)
+               if (argc != 3)
                        return -E_COMMAND_SYNTAX;
                break;
        case SENDER_DENY:
        case SENDER_ALLOW:
-               if (argc != 2 && argc != 3)
+               if (argc != 4 && argc != 5)
                        return -E_COMMAND_SYNTAX;
-               if (!inet_aton(argv[2], &scd->addr))
+               if (!inet_aton(argv[3], &scd->addr))
                        return -E_COMMAND_SYNTAX;
                scd->netmask = 32;
-               if (argc == 3) {
-                       scd->netmask = atoi(argv[3]);
+               if (argc == 5) {
+                       scd->netmask = atoi(argv[4]);
                        if (scd->netmask < 0 || scd->netmask > 32)
                                return -E_COMMAND_SYNTAX;
                }
                break;
        case SENDER_ADD:
        case SENDER_DELETE:
-               if (argc != 2 && argc != 3)
+               if (argc != 4 && argc != 5)
                        return -E_COMMAND_SYNTAX;
-               if (!inet_aton(argv[2], &scd->addr))
+               if (!inet_aton(argv[3], &scd->addr))
                        return -E_COMMAND_SYNTAX;
                scd->port = -1;
-               if (argc == 3) {
-                       scd->port = atoi(argv[3]);
+               if (argc == 5) {
+                       scd->port = atoi(argv[4]);
                        if (scd->port < 0 || scd->port > 65535)
                                return -E_COMMAND_SYNTAX;
                }
@@ -518,7 +517,7 @@ static int com_sender(int fd, int argc, char **argv)
        int i, ret;
        struct sender_command_data scd;
 
-       if (!argc) {
+       if (argc < 2) {
                char *msg = NULL;
                for (i = 0; senders[i].name; i++) {
                        char *tmp = make_message("%s%s\n",
@@ -530,7 +529,7 @@ static int com_sender(int fd, int argc, char **argv)
                free(msg);
                return ret;
        }
-       ret = check_sender_args(argc - 1, argv + 1, &scd);
+       ret = check_sender_args(argc, argv, &scd);
        if (ret < 0) {
                char *msg;
                if (scd.sender_num < 0)
@@ -562,7 +561,7 @@ static int com_si(int fd, int argc, __unused char **argv)
        char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
        struct mallinfo mi = mallinfo();
 
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        for (i = 0; selectors[i].name; i++) {
@@ -609,7 +608,7 @@ static int com_si(int fd, int argc, __unused char **argv)
 /* version */
 static int com_version(int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        return send_buffer(socket_fd, "para_server-" VERSION ", \"" CODENAME "\"\n"
                        COPYRIGHT "\n"
@@ -624,7 +623,7 @@ static int com_sc(int socket_fd, int argc, char **argv)
        char *name = NULL;
        int ret, old = 0, count = -1; /* print af change forever */
 
-       if (argc)
+       if (argc > 1)
                count = atoi(argv[1]);
 repeat:
        mmd_lock();
@@ -639,7 +638,7 @@ repeat:
                name = NULL;
                if (ret < 0)
                        return ret;
-               if (argc && !--count)
+               if (argc > 1 && !--count)
                        return 1;
        }
        usleep(500000);
@@ -654,7 +653,7 @@ static int com_sb(int socket_fd, int argc, char **argv)
                                 * times. Negative value means: print
                                 * forever
                                 */
-       if (argc)
+       if (argc > 1)
                nr = atoi(argv[1]);
        while (nr) {
                mmd_lock();
@@ -684,7 +683,7 @@ static int com_stat(int socket_fd, int argc, char **argv)
 
        signal(SIGUSR1, dummy);
 
-       if (argc)
+       if (argc > 1)
                num = atoi(argv[1]);
        for (;;) {
 
@@ -720,7 +719,7 @@ static int send_description(int fd, struct server_command *cmd, const char *hand
        return 1;
 }
 
-/* always returns string that must be freed by the caller in handeler */
+/* always returns string that must be freed by the caller in handler */
 static struct server_command *get_cmd_ptr(char *name, char **handler)
 {
        struct server_command *cmd = cmd_struct;
@@ -751,7 +750,7 @@ static int com_help(int fd, int argc, char **argv)
        char *perms, *handler;
        int ret;
 
-       if (!argc) {
+       if (argc < 2) {
                /* no argument given, print list of commands */
                if ((ret = send_description(fd, cmd_struct, "server", 0)) < 0)
                        return ret;
@@ -794,7 +793,7 @@ static int com_help(int fd, int argc, char **argv)
 /* hup */
 static int com_hup(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGHUP);
        return 1;
@@ -803,7 +802,7 @@ static int com_hup(__unused int socket_fd, int argc, __unused char **argv)
 /* term */
 static int com_term(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGTERM);
        return 1;
@@ -811,7 +810,7 @@ static int com_term(__unused int socket_fd, int argc, __unused char **argv)
 
 static int com_play(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        mmd->new_afs_status_flags |= AFS_PLAYING;
@@ -824,7 +823,7 @@ static int com_play(__unused int socket_fd, int argc, __unused char **argv)
 /* stop */
 static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        mmd->new_afs_status_flags &= ~AFS_PLAYING;
@@ -837,7 +836,7 @@ static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
 /* pause */
 static int com_pause(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        if (!afs_paused())
@@ -852,7 +851,7 @@ static int com_chs(int fd, int argc, char **argv)
 {
        int i, ret;
 
-       if (!argc) {
+       if (argc == 1) {
                char *selector;
                mmd_lock();
                selector = para_strdup(selectors[mmd->selector_num].name);
@@ -876,7 +875,7 @@ static int com_chs(int fd, int argc, char **argv)
 /* next */
 static int com_next(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc =! 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        mmd->events++;
@@ -888,7 +887,7 @@ static int com_next(__unused int socket_fd, int argc, __unused char **argv)
 /* nomore */
 static int com_nomore(__unused int socket_fd, int argc, __unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        if (afs_playing() || afs_paused())
@@ -905,7 +904,7 @@ static int com_ff(__unused int socket_fd, int argc, char **argv)
        unsigned i;
        char c;
 
-       if (!argc)
+       if (argc != 2)
                return -E_COMMAND_SYNTAX;
        if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
                return -E_COMMAND_SYNTAX;
@@ -942,7 +941,7 @@ static int com_jmp(__unused int socket_fd, int argc, char **argv)
        long unsigned int i;
        int ret;
 
-       if (!argc)
+       if (argc != 2)
                return -E_COMMAND_SYNTAX;
        if (sscanf(argv[1], "%lu", &i) <= 0)
                return -E_COMMAND_SYNTAX;
@@ -1204,7 +1203,7 @@ int handle_connect(int fd, struct sockaddr_in *addr)
                goto err_out;
        /* valid command and sufficient perms */
        alarm(0);
-       argc = split_args(command, &argv, '\n');
+       argc = split_args(command, &argv, "\n");
        mmd_lock();
        mmd->num_commands++;
        mmd_unlock();
diff --git a/exec.c b/exec.c
index b8f2df57f8c3c5768bb586aa34a5427decd29eae..95bc43e4fae19d9791b36e5e7be2b06b1dc2ebb8 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -147,7 +147,7 @@ int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
 
        if (!tmp)
                exit(EXIT_FAILURE);
-       argc = split_args(tmp, &argv, ' ');
+       argc = split_args(tmp, &argv, " \t");
        ret = para_exec(pid, argv[0], argv, fds);
        free(argv);
        free(tmp);
index a04e2d8206a78520eaa8b95ecc8970eeb8366325..4c1f94bcb890526548e1702209625add432232e1 100644 (file)
@@ -193,11 +193,12 @@ static int parse_filter_args(int filter_num, char *options, void **conf)
        if (!f->parse_config)
                return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
 //     PARA_DEBUG_LOG("options: %s\n", options);
-       argc = split_args(options, &argv, ' ');
+       argc = split_args(options, &argv, " \t");
 //             PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
-       for (i = argc; i >= 0; i--)
+       for (i = argc - 1; i >= 0; i--)
                argv[i + 1] = argv[i];
-       argc += 2;
+       argv[0] = para_strdup(f->name);
+       argc += 1;
        *conf = f->parse_config(argc, argv);
        return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
 }
index 6769ee522ef3dfcd7dac66fb0c299c4ee0e37ef7..f853a76432b65e597cf987f5a00a468b5bffff7f 100644 (file)
@@ -231,20 +231,19 @@ void activate_inactive_grab_clients(int slot, int audio_format_num,
  * argc, argv get freed when com_grab() returns, so we have to make a
  * copy.
  */
-__malloc struct grab_client *grab_client_new(int fd, int argc, char **argv, int *err)
+struct grab_client *grab_client_new(int fd, int argc, char **argv, int *err)
 {
        int i, ret;
        struct grab_client *gc = para_calloc(sizeof(struct grab_client));
 
        gc->conf = para_calloc(sizeof(struct grab_client_args_info));
        gc->argc = argc;
-       gc->argv = para_calloc((argc + 1) * sizeof(char *));
+       gc->argv = para_calloc((argc + 2) * sizeof(char *));
 
-       for (i = 0; i < argc; i++) {
+       for (i = 0; argv[i]; i++) {
                gc->argv[i] = para_strdup(argv[i]);
                PARA_INFO_LOG("argc: %d, argv[%d]: %s\n", argc, i, gc->argv[i]);
        }
-       PARA_INFO_LOG("argv[%d]: %s\n", argc, gc->argv[argc]);
        ret = grab_client_cmdline_parser(gc->argc, gc->argv , gc->conf);
        *err = -E_GC_SYNTAX;
        if (ret)
index a53632c524c8be7bdc05d91d2365deb9ff00289e..98db17dc758762b1b16feb2a2cae3da4f38b8571 100644 (file)
@@ -698,7 +698,7 @@ static int com_na(__unused int fd, int argc, char *argv[])
        char *q;
        int ret;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        q = make_message("alter table data add %s char(1) "
                "not null default 0", argv[1]);
@@ -715,7 +715,7 @@ static int com_da(__unused int fd, int argc, char *argv[])
        char *q;
        int ret;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        q = make_message("alter table data drop %s", argv[1]);
        ret = real_query(q);
@@ -731,7 +731,7 @@ static int com_stradd_picadd(int fd, int argc, char *argv[])
        int ret, stradd = strcmp(argv[0], "picadd");
        size_t size;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        if (strlen(argv[1]) >= MAXLINE - 1)
                return -E_NAMETOOLONG;
@@ -807,7 +807,7 @@ static int com_verb(int fd, int argc, char *argv[])
        int ret;
        unsigned int num_rows, num_fields;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        result = get_result(argv[1]);
        if (!result)
@@ -848,7 +848,7 @@ static int com_laa(int fd, int argc, __unused char *argv[])
        void *result;
        int ret;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        result = get_all_attributes();
        if (!result)
@@ -869,9 +869,9 @@ static int com_hist(int fd, int argc, char *argv[]) {
 
        q = make_message("select name, to_days(now()) - to_days(lastplayed) from "
                "data%s%s%s order by lastplayed",
-               (argc < 1)? "" : " where ",
-               (argc < 1)? "" : argv[1],
-               (argc < 1)? "" : " = '1'");
+               (argc < 2)? "" : " where ",
+               (argc < 2)? "" : argv[1],
+               (argc < 2)? "" : " = '1'");
        result = get_result(q);
        free(q);
        if (!result)
@@ -893,7 +893,7 @@ static int com_last(int fd, int argc, char *argv[])
        char *q;
        int num, ret;
 
-       if (argc < 1)
+       if (argc < 2)
                num = 10;
        else
                num = atoi(argv[1]);
@@ -942,7 +942,7 @@ static int com_mbox(int fd, int argc, char *argv[])
                "\n\n\n"
                "') from data"
        );
-       if (argc >= 1) {
+       if (argc >= 2) {
                char *tmp = make_message("%s where name LIKE '%s'", query,
                        argv[1]);
                free(query);
@@ -1306,7 +1306,7 @@ static int com_info(int fd, int argc, char *argv[])
        char *name = NULL, *meta = NULL, *atts = NULL, *dir = NULL;
        int ret, com_la = strcmp(argv[0], "info");
 
-       if (argc < 1) {
+       if (argc < 2) {
                ret = -E_GET_AUDIO_FILE;
                if (!(name = get_current_audio_file()))
                        goto out;
@@ -1441,9 +1441,9 @@ static int com_rm_ne(__unused int fd, int argc, char *argv[])
 {
        int ne = !strcmp(argv[0], "ne");
        int i, ret;
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
-       for (i = 1; i <= argc; i++) {
+       for (i = 1; i < argc; i++) {
                ret = remove_entry(argv[i]);
                if (ret < 0)
                        return ret;
@@ -1464,7 +1464,7 @@ static int com_mv(__unused int fd, int argc, char *argv[])
        char *q, *dn, *ebn1 = NULL, *ebn2 = NULL, *edn = NULL;
        int ret;
 
-       if (argc != 2)
+       if (argc != 3)
                return -E_MYSQL_SYNTAX;
        ebn1 = escaped_basename(argv[1]);
        ebn2 = escaped_basename(argv[2]);
@@ -1518,10 +1518,10 @@ static int com_set(__unused int fd, int argc, char *argv[])
        int i, ret;
        const char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
 
-       if (argc < 2)
+       if (argc < 3)
                return -E_MYSQL_SYNTAX;
        id = atol(argv[1]);
-       for (i = 2; i <= argc; i++) {
+       for (i = 2; i < argc; i++) {
                ebn = escaped_basename(argv[i]);
                if (!ebn)
                        return -E_ESCAPE;
@@ -1545,7 +1545,7 @@ static int com_picch(__unused int fd, int argc, char *argv[])
        long unsigned id;
        char *q;
 
-       if (argc != 2)
+       if (argc != 3)
                return -E_MYSQL_SYNTAX;
        id = atol(argv[1]);
        if (strlen(argv[2]) > MAXLINE)
@@ -1566,7 +1566,7 @@ static int com_piclist(__unused int fd, int argc, __unused char *argv[])
        unsigned long *length;
        int ret;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        result = get_result("select id,name,pic from pics order by id");
        if (!result)
@@ -1595,9 +1595,9 @@ static int com_picdel(int fd, int argc, char *argv[])
        my_ulonglong aff;
        int i, ret;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
-       for (i = 1; i <= argc; i++) {
+       for (i = 1; i < argc; i++) {
                id = atol(argv[i]);
                q = make_message("delete from pics where id = %lu", id);
                ret = real_query(q);
@@ -1628,7 +1628,7 @@ static int com_pic(int fd, int argc, char *argv[])
        int ret;
        char *q, *name = NULL;
 
-       if (argc < 1) {
+       if (argc < 2) {
                ret = -E_GET_AUDIO_FILE;
                name = get_current_audio_file();
        } else {
@@ -1666,7 +1666,7 @@ static int com_strdel(__unused int fd, int argc, char *argv[])
        char *tmp;
        int ret = -1;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        tmp = make_message("delete from streams where name='%s'", argv[1]);
        ret = real_query(tmp);
@@ -1690,7 +1690,7 @@ static int com_ls(int fd, int argc, char *argv[])
        int ret;
        unsigned int num_rows;
 
-       if (argc > 0)
+       if (argc > 1)
                q = make_message("select name from data where name LIKE '%s'",
                        argv[1]);
        else
@@ -1718,7 +1718,7 @@ static int com_summary(__unused int fd, int argc, __unused char *argv[])
        const char *fmt = "select count(name) from data where %s='1'";
        int ret = -E_NORESULT;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        result = get_all_attributes();
        if (!result)
@@ -1821,7 +1821,7 @@ static void update_audio_file_server_handler(char *name)
 
 static int com_us(__unused int fd, int argc, char *argv[])
 {
-       if (argc != 1)
+       if (argc != 2)
                return -E_MYSQL_SYNTAX;
        return update_audio_file(argv[1]);
 }
@@ -1850,7 +1850,7 @@ static int com_ps(__unused int fd, int argc, char *argv[])
        int match = -1, ret, i;
        unsigned int num_rows;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        ret = -E_NORESULT;
        if (!result)
@@ -1916,9 +1916,9 @@ static int com_streams(int fd, int argc, __unused char *argv[])
        void *result;
        MYSQL_ROW row;
 
-       if (argc && strcmp(argv[1], "current_stream"))
+       if (argc > 1 && strcmp(argv[1], "current_stream"))
                return -E_MYSQL_SYNTAX;
-       if (argc) {
+       if (argc > 1) {
                char *cs = get_current_stream();
                ret = send_va_buffer(fd, "%s\n", cs);
                free(cs);
@@ -1954,7 +1954,7 @@ static int com_strq(int fd, int argc, char *argv[])
        void *result;
        int ret;
 
-       if (argc < 1) {
+       if (argc < 2) {
                ret = -E_GET_STREAM;
                name = get_current_stream();
        } else {
@@ -1990,7 +1990,7 @@ static int com_cs(int fd, int argc, char *argv[])
        char *old_stream = get_current_stream();
        int csp = !strcmp(argv[0], "csp");
 
-       if (!argc) {
+       if (argc == 1) {
                ret = -E_MYSQL_SYNTAX;
                if (csp)
                        goto out;
@@ -2034,12 +2034,12 @@ static int com_sl(int fd, int argc, char *argv[])
        char *query, *stream, *tmp;
        unsigned int num_rows, num;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
        num = atoi(argv[1]);
        if (!num)
                return -E_MYSQL_SYNTAX;
-       stream = (argc == 1)?  get_current_stream() : para_strdup(argv[2]);
+       stream = (argc == 2)?  get_current_stream() : para_strdup(argv[2]);
        tmp = get_query(stream, NULL, 0);
        query = make_message("%s limit %d", tmp, num);
        free(tmp);
@@ -2108,9 +2108,9 @@ static int com_sa(int fd, int argc, char *argv[])
        int i, ret;
        char *atts = NULL, *name;
 
-       if (argc < 1)
+       if (argc < 2)
                return -E_MYSQL_SYNTAX;
-       for (i = 1; i <= argc; i++) {
+       for (i = 1; i < argc; i++) {
                int unset = 0;
                char *tmp, *p =argv[i];
                int len = strlen(p);
@@ -2136,7 +2136,7 @@ static int com_sa(int fd, int argc, char *argv[])
 no_more_atts:
        if (!atts)
                return -E_NOATTS;
-       if (i > argc) { /* no name given, use current af */
+       if (i >= argc) { /* no name given, use current af */
                ret = -E_GET_AUDIO_FILE;
                if (!(name = get_current_audio_file()))
                        goto out;
@@ -2160,7 +2160,7 @@ static int com_cam(int fd, int argc, char *argv[])
        char *name = NULL, *meta = NULL, *atts = NULL;
        int i, ret;
 
-       if (argc < 2)
+       if (argc < 3)
                return -E_MYSQL_SYNTAX;
        if (!(name = escaped_basename(argv[1])))
                return -E_ESCAPE;
@@ -2170,7 +2170,7 @@ static int com_cam(int fd, int argc, char *argv[])
        ret = -E_META;
        if (!(meta = get_meta(name, 0)))
                goto out;
-       for (i = 2; i <= argc; i++) {
+       for (i = 2; i < argc; i++) {
                char *ebn, *q;
                ret = -E_ESCAPE;
                if (!(ebn = escaped_basename(argv[i])))
@@ -2212,7 +2212,7 @@ static int com_vrfy(int fd, int argc, __unused char *argv[])
        MYSQL_ROW row;
        char *escaped_name;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        ret = -E_NORESULT;
        result = get_result("select data.name from data left join dir on "
@@ -2277,7 +2277,7 @@ static int com_upd(int fd, int argc, __unused char *argv[])
        unsigned int num_rows;
        MYSQL_ROW row;
 
-       if (argc)
+       if (argc != 1)
                return -E_MYSQL_SYNTAX;
        out_file = NULL;
        tempname = para_strdup("/tmp/mysql.tmp.XXXXXX");
@@ -2447,7 +2447,7 @@ static int com_cdb(int fd, int argc, char *argv[])
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
-       conf.mysql_database_arg = para_strdup((argc < 1)?
+       conf.mysql_database_arg = para_strdup((argc < 2)?
                "paraslash" : argv[1]);
        query = make_message("create database %s", conf.mysql_database_arg);
        ret = real_query(query);
index 39a840873a4495250cdfc519a4d0d5b0712e8260..078e44d17eee3dd97770b5694574c243244d5b30 100644 (file)
@@ -38,12 +38,13 @@ static void *parse_receiver_args(int receiver_num, char *options)
 //     PARA_DEBUG_LOG("%s, options: %s\n", r->name,
 //             options? options : "(none)");
        if (options) {
-//             PARA_DEBUG_LOG("%s options: %s\n", name, options);
-               argc = split_args(options, &argv, ' ');
-//             PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", fn->argc, fn->argv[0]);
-               for (i = argc; i >= 0; i--)
+               PARA_DEBUG_LOG("options: %s\n", options);
+               argc = split_args(options, &argv, " \t");
+               for (i = argc - 1; i >= 0; i--)
                        argv[i + 1] = argv[i];
-               argc += 2;
+               argv[0] = para_strdup(r->name);
+               argc += 1;
+               PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
        } else {
                argc = 1;
                argv = para_malloc(2 * sizeof(char*));
@@ -64,7 +65,7 @@ void *check_receiver_arg(char *ra, int *receiver_num)
 {
        int j;
 
-//     PARA_DEBUG_LOG("checking %s\n", ra);
+       PARA_DEBUG_LOG("checking %s\n", ra);
        for (j = 0; receivers[j].name; j++) {
                const char *name = receivers[j].name;
                size_t len = strlen(name);
diff --git a/stat.c b/stat.c
index 7346f097e61e9933759563d64af7e7b2dedd3eb6..532f9cdfc5449add435bcfb4ba71a91924991c9b 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -211,8 +211,10 @@ void dump_empty_status(void)
 int stat_item_valid(const char *item)
 {
        int i;
-       if (!item || !*item)
+       if (!item || !*item) {
+       PARA_ERROR_LOG("%s\n", "no item");
                return -E_UNKNOWN_STAT_ITEM;
+       }
        FOR_EACH_STAT_ITEM(i)
                if (!strcmp(status_item_list[i], item))
                        return i;
@@ -267,6 +269,7 @@ unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
        char *start = buf, *end;
        int i, num_lines = 0;
 
+       PARA_INFO_LOG("buf: %s", buf);
        while (start < buf + n) {
                char *next_null;
                char *next_cr;
@@ -284,6 +287,7 @@ unsigned for_each_line(char *buf, int n, void (*line_handler)(char *))
                num_lines++;
                if (line_handler) {
                        *end = '\0';
+                       PARA_INFO_LOG("calling line handler: %s\n", start);
                        line_handler(start);
                        start = ++end;
                } else
index 056a414bb48b8d0cf1d62444aa9586cbde36c91c..78b862c933ced61396aadb6df04f43d8185eee21 100644 (file)
--- a/string.c
+++ b/string.c
@@ -382,32 +382,35 @@ __must_check __malloc char *para_homedir(void)
  *
  * \return The number of substrings found in \a args.
  */
-__must_check unsigned split_args(char *args, char ***argv_ptr, int delim)
+
+__must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim)
 {
        char *p = args;
        char **argv;
-       ssize_t n = 0, i;
+       size_t n = 0, i, j;
 
-       while (p && (p = strchr(p, delim))) {
-               p++;
+       while ((i = strcspn(p, delim)) && (p += i)) {
+               p += strspn(p, delim);
                n++;
        }
-       *argv_ptr = para_calloc((n + 3) * sizeof(char *));
+       *argv_ptr = para_malloc((n + 1) * sizeof(char *));
        argv = *argv_ptr;
        i = 0;
        p = args;
-//     printf("split_args: a:%s\n", p);
        while (p) {
                argv[i] = p;
-               p = strchr(p, delim);
-               if (p) {
-//             printf("a:%s\n", p);
+               j = strcspn(p, delim);
+               if (!j)
+                       break;
+               p += strcspn(p, delim);
+               if (*p) {
                        *p = '\0';
-//             printf("b:\n");
                        p++;
-                       i++;
+                       p += strspn(p, delim);
                }
+               i++;
        }
+       argv[n] = NULL;
        return n;
 }
 
index bd82fd58b3bda14d73dadb68fd079dbb9f57b1ba..e5033db2b91bd3ca4f0e4a6b27474f924f74343f 100644 (file)
--- a/string.h
+++ b/string.h
@@ -43,7 +43,7 @@ __must_check __malloc char *para_tmpname(void);
 __must_check int para_mkstemp(char *template, mode_t mode);
 __must_check __malloc char *para_logname(void);
 __must_check __malloc char *para_homedir(void);
-__must_check unsigned split_args(char *args, char ***argv_ptr, int delim);
+__must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim);
 __malloc char *para_hostname(void);
 void valid_fd_012(void);