]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mysql_selector.c
improved version of split_args()
[paraslash.git] / mysql_selector.c
index cdb36dd2d0a3d50a008c3579b8e2a733e69e8a04..98db17dc758762b1b16feb2a2cae3da4f38b8571 100644 (file)
@@ -599,7 +599,7 @@ static struct para_macro macro_list[] = {
        }
 };
 
-static int real_query(char *query)
+static int real_query(const char *query)
 {
        if (!mysql_ptr)
                return -E_NOTCONN;
@@ -616,7 +616,7 @@ static int real_query(char *query)
  * Use open connection given by mysql_ptr to query server. Returns a
  * result pointer on succes and NULL on errors
  */
-static struct MYSQL_RES *get_result(char *query)
+static struct MYSQL_RES *get_result(const char *query)
 {
        void *result;
 
@@ -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;
@@ -1337,7 +1337,7 @@ out:
                free(name);
        return ret;
 }
-static int change_stream(char *stream)
+static int change_stream(const char *stream)
 {
        char *query;
        int ret;
@@ -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]);
@@ -1516,12 +1516,12 @@ static int com_set(__unused int fd, int argc, char *argv[])
        char *q, *ebn;
        long unsigned id;
        int i, ret;
-       char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
+       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");
@@ -2435,17 +2435,9 @@ static void write_msg2mmd(int success)
 /* create database */
 static int com_cdb(int fd, int argc, char *argv[])
 {
-       char *query, *name;
+       char *query;
        int ret;
 
-       if (argc < 1)
-               name = "paraslash";
-       else {
-               ret = -E_NAMETOOLONG;
-               name = argv[1];
-               if (strlen(name) > MAXLINE)
-                       goto out;
-       }
        if (mysql_ptr) {
                PARA_INFO_LOG("%s", "closing database\n");
                mysql_close(mysql_ptr);
@@ -2455,14 +2447,15 @@ static int com_cdb(int fd, int argc, char *argv[])
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
-       query = make_message("create database %s", name);
+       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);
        free(query);
        if (ret < 0)
                goto out;
        /* reconnect with database just created */
        mysql_close(mysql_ptr);
-       conf.mysql_database_arg = para_strdup(name);
        ret = -E_MYSQL_INIT;
        if (init_mysql_server() < 0 || !mysql_ptr)
                goto out;
@@ -2493,7 +2486,8 @@ static int com_cdb(int fd, int argc, char *argv[])
        if (real_query("insert into streams (name, def) values "
                        "('current_stream', '(none)')") < 0)
                goto out;
-       ret = send_va_buffer(fd, "successfully created database %s\n", name);
+       ret = send_va_buffer(fd, "successfully created database %s\n",
+               conf.mysql_database_arg);
 out:
        return ret;
 }