Implement ranges for rppart. - rohrpost - A commandline mail client to change the world as we see it.
 (HTM) git clone git://r-36.net/rohrpost
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0ecd913b33cf12aba6fd01963a555084a051cf5e
 (DIR) parent 170a028c6f93a03cec59677c378fab08e56df8a4
 (HTM) Author: Christoph Lohmann <20h@r-36.net>
       Date:   Tue,  7 Feb 2023 19:33:08 +0100
       
       Implement ranges for rppart.
       
       Diffstat:
         M imap.c                              |     161 ++++++++++++++++++-------------
         M part.c                              |       2 +-
       
       2 files changed, 95 insertions(+), 68 deletions(-)
       ---
 (DIR) diff --git a/imap.c b/imap.c
       @@ -86,80 +86,93 @@ imap_llist2ids(char *cfgn, char *mailbox, llist_t *elems)
        {
                llist_t *ids, *result, *ret;
                llistelem_t *elem;
       -        mark_t *marks;
       +        mark_t *marks = NULL;
                char *split0, *split1, *rstr;
                int last, first, cur, a, b, c, d;
        
                ret = NULL;
                first = 1;
        
       -        rstr = inc_getstr(cfgn, mailbox, "messages");
       -        if (rstr == NULL)
       -                return NULL;
       -        last = atoi(rstr);
       -        free(rstr);
       -
       -        marks = mark_init(cfgn, mailbox);
       -        if (marks == NULL)
       -                return NULL;
       -
       -        rstr = mark_getstr(marks, "cur");
       -        if (rstr == NULL)
       -                cur = first;
       -        else
       -                cur = atoi(rstr);
       +        /*
       +         * If no configuration is given, only handle ranges.
       +         * Is used in part.c.
       +         */
       +        if (mailbox != NULL) {
       +                rstr = inc_getstr(cfgn, mailbox, "messages");
       +                if (rstr == NULL)
       +                        return NULL;
       +                last = atoi(rstr);
       +                free(rstr);
       +
       +                marks = mark_init(cfgn, mailbox);
       +                if (marks == NULL)
       +                        return NULL;
       +
       +                rstr = mark_getstr(marks, "cur");
       +                if (rstr == NULL)
       +                        cur = first;
       +                else
       +                        cur = atoi(rstr);
       +        }
        
                ids = llist_new();
                forllist(elems, elem) {
       -                result = mark_getlist(marks, elem->key);
       -                if (result != NULL) {
       -                        ids = llist_rawlistadd(ids, result);
       -                        llist_bfree(result);
       -                        continue;
       -                }
       +                if (mailbox != NULL) {
       +                        result = mark_getlist(marks, elem->key);
       +                        if (result != NULL) {
       +                                ids = llist_rawlistadd(ids, result);
       +                                llist_bfree(result);
       +                                continue;
       +                        }
        
       -                if (!strcmp(elem->key, "c")) {
       -                        llist_addraw(ids, smprintf("%d", cur), NULL, 0);
       -                        continue;
       -                }
       +                        if (!strcmp(elem->key, "c")) {
       +                                llist_addraw(ids, smprintf("%d", cur), NULL, 0);
       +                                continue;
       +                        }
        
       -                if (elem->key[0] == 'c') {
       -                        b = atoi(&elem->key[1]);
       -                        if (b < 0) {
       -                                if (cur + b < 1)
       -                                        b = 1 - cur;
       +                        if (elem->key[0] == 'c') {
       +                                b = atoi(&elem->key[1]);
       +                                if (b < 0) {
       +                                        if (cur + b < 1)
       +                                                b = 1 - cur;
        
       -                                result = llist_genrange(cur + b, cur + 1, 1);
       -                        } else {
       -                                if (cur + b + 1 > last)
       -                                        b = last - cur - 1;
       +                                        result = llist_genrange(cur + b, cur + 1, 1);
       +                                } else {
       +                                        if (cur + b + 1 > last)
       +                                                b = last - cur - 1;
        
       -                                result = llist_genrange(cur, cur + b + 1, 1);
       -                        }
       -                        if (result == NULL)
       +                                        result = llist_genrange(cur, cur + b + 1, 1);
       +                                }
       +                                if (result == NULL)
       +                                        continue;
       +                                llist_rawlistadd(ids, result);
       +                                llist_bfree(result);
                                        continue;
       -                        llist_rawlistadd(ids, result);
       -                        llist_bfree(result);
       -                        continue;
       +                        }
                        }
        
                        split0 = strchr(elem->key, ':');
                        if (split0 == NULL) {
       -                        if (elem->key[0] == '-') {
       -                                if (!isdigit(elem->key[1]))
       +                        if (mailbox != NULL) {
       +                                if (elem->key[0] == '-') {
       +                                        if (!isdigit(elem->key[1]))
       +                                                continue;
       +                                } else if (!isdigit(elem->key[0])) {
                                                continue;
       -                        } else if (!isdigit(elem->key[0])) {
       -                                continue;
       +                                }
       +                                b = atoi(elem->key);
       +                                if (b < 1)
       +                                        b = last + b + 1;
       +                                if (b < 1)
       +                                        b = 1;
       +                                if (b > last)
       +                                        b = last;
       +
       +                                llist_addraw(ids, smprintf("%d", b), NULL, 0);
       +                        } else {
       +                                llist_addraw(ids, smprintf("%s", elem->key),
       +                                        NULL, 0);
                                }
       -                        b = atoi(elem->key);
       -                        if (b < 1)
       -                                b = last + b + 1;
       -                        if (b < 1)
       -                                b = 1;
       -                        if (b > last)
       -                                b = last;
       -
       -                        llist_addraw(ids, smprintf("%d", b), NULL, 0);
                                continue;
                        } else {
                                c = 1;
       @@ -173,28 +186,41 @@ imap_llist2ids(char *cfgn, char *mailbox, llist_t *elems)
                                        a = atoi(elem->key);
                                }
                                if (split0[1] == ':' || split0[1] == '\0') {
       -                                b = last;
       +                                if (cfgn != NULL) {
       +                                        b = last;
       +                                } else {
       +                                        b = 0;
       +                                }
                                } else {
                                        b = atoi(&split0[1]);
                                }
        
       -                        if (a < 0)
       -                                a = last + a + 1;
       -                        if (b < 0)
       -                                b = last + b + 1;
       +                        if (mailbox != NULL) {
       +                                if (a < 0)
       +                                        a = last + a + 1;
       +                                if (b < 0)
       +                                        b = last + b + 1;
       +                        }
                                if (a > b) {
                                        d = a;
                                        a = b;
                                        b = d;
                                }
       -                        if (b + 1 > last)
       -                                b = last;
       +                        if (cfgn != NULL) {
       +                                if (b + 1 > last)
       +                                        b = last;
       +                        }
        
       -                        result = llist_genrange(a, b + 1, c);
       -                        if (result == NULL)
       -                                continue;
       -                        llist_rawlistadd(ids, result);
       -                        llist_bfree(result);
       +                        if (a == 0 || b == 0) {
       +                                llist_addraw(ids, smprintf("%s", elem->key),
       +                                        NULL, 0);
       +                        } else {
       +                                result = llist_genrange(a, b + 1, c);
       +                                if (result == NULL)
       +                                        continue;
       +                                llist_rawlistadd(ids, result);
       +                                llist_bfree(result);
       +                        }
                        }
                        continue;
                }
       @@ -206,7 +232,8 @@ imap_llist2ids(char *cfgn, char *mailbox, llist_t *elems)
                //llist_intsort(ids);
                ret = ids;
        badmarkending:
       -        mark_free(marks);
       +        if (marks != NULL)
       +                mark_free(marks);
                return ret;
        }
        
 (DIR) diff --git a/part.c b/part.c
       @@ -213,7 +213,7 @@ partmain(int argc, char *argv[])
        
                partl = NULL;
                if (parts != NULL)
       -                partl = llist_splitstr(parts, " ,");
       +                partl = imap_str2ids(NULL, NULL, parts);
        
                if (partl == NULL && !(status & ALLPARTS) && type == NULL &&
                                !(status & LISTPARTS)) {