aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-04-11 20:39:49 +0200
committerMattias Andrée <maandree@kth.se>2021-04-11 20:39:49 +0200
commitbc5c3034cab84e9a7d587c2590c2d3f06283972e (patch)
treef895ac024c2ff37907781890ee672c74e7a96ed4
parentRewrite set-contact-numbers to work like set-contact-addresses (diff)
downloadcontacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.gz
contacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.bz2
contacts-bc5c3034cab84e9a7d587c2590c2d3f06283972e.tar.xz
Improve set-contact- utils
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--set-contact-addresses.c10
-rw-r--r--set-contact-birthday.c15
-rw-r--r--set-contact-blocks.c20
-rw-r--r--set-contact-chats.c133
-rw-r--r--set-contact-emails.c116
-rw-r--r--set-contact-numbers.c23
-rw-r--r--set-contact-organisations.c116
-rw-r--r--set-contact-pgpkeys.c116
-rw-r--r--set-contact-sites.c116
9 files changed, 371 insertions, 294 deletions
diff --git a/set-contact-addresses.c b/set-contact-addresses.c
index 90fd9a6..9c50637 100644
--- a/set-contact-addresses.c
+++ b/set-contact-addresses.c
@@ -1,10 +1,10 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-A old-address] [-a new-address] [-C old-context] [-c new-context] "
- "[-G [old-latitude]:[old-longitude]] [-g new-latitude:new-longitude] "
- "[-N old-country] [-n new-country] [-O old-care-of] [-o new-care-of] "
- "[-P post-code] [-p new-post-code] [-T old-city] [-t new-city] [-u] contact-id");
+USAGE("[-A old-address] [-C old-context] [-G [old-latitude]:[old-longitude]] [-N old-country] "
+ "[-O old-care-of] [-P post-code] [-T old-city] "
+ "([-a new-address] [-c new-context] [-g new-latitude:new-longitude] [-n new-country] "
+ "[-o new-care-of] [-p new-post-code] [-t new-city] | -u) contact-id");
static int
@@ -131,7 +131,7 @@ main(int argc, char *argv[])
if (remove == edit) {
if (edit)
- eprintf("-u cannot be combined with -acglopt\n");
+ usage();
eprintf("at least one of -acgloptu is required\n");
}
diff --git a/set-contact-birthday.c b/set-contact-birthday.c
index 9db0987..74dadc7 100644
--- a/set-contact-birthday.c
+++ b/set-contact-birthday.c
@@ -2,20 +2,23 @@
#include "common.h"
/* Date components are in different options to avoid date format confusion */
-USAGE("([-y year | -Y] [-m month | -M] [-d day | -D] [-b | -B] | -u) contact-id ...");
+USAGE("([-Y | -y year] [-M | -m month] [-D | -d day] [-B | -b] | -u) contact-id ...");
+#if defined(__GNUC__)
+__attribute__((__pure__))
+#endif
static int
getintarg(const char *arg)
{
int ret = 0;
for (; isdigit(*arg); arg++) {
if (ret > (INT_MAX - (*arg & 15)) / 10)
- usage();
+ return -1;
ret = ret * 10 + (*arg & 15);
}
if (*arg)
- usage();
+ return -1;
return ret;
}
@@ -34,6 +37,8 @@ main(int argc, char *argv[])
if (year)
usage();
year = getintarg(ARG());
+ if (year < 0)
+ usage();
break;
case 'Y':
if (year)
@@ -44,6 +49,8 @@ main(int argc, char *argv[])
if (month)
usage();
month = getintarg(ARG());
+ if (month < 0)
+ usage();
break;
case 'M':
if (month)
@@ -54,6 +61,8 @@ main(int argc, char *argv[])
if (day)
usage();
day = getintarg(ARG());
+ if (day < 0)
+ usage();
break;
case 'D':
if (day)
diff --git a/set-contact-blocks.c b/set-contact-blocks.c
index 9ad0e2e..dc01919 100644
--- a/set-contact-blocks.c
+++ b/set-contact-blocks.c
@@ -1,8 +1,8 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-A old-ask-at] [-a new-ask-at] [-S old-service] [-s new-service] [-T old-type] [-t new-type] "
- "[-U old-unblock-at] [-u new-unblock-at] [-Y old-style] [-y new-style] contact-id");
+USAGE("[-A old-ask-at] [-S old-service] [-T old-type] [-U old-unblock-at] [-Y old-style] "
+ "[-a new-ask-at] [-s new-service] [-t new-type] [-u new-unblock-at] [-y new-style] contact-id");
int
@@ -13,9 +13,9 @@ main(int argc, char *argv[])
enum libcontacts_block_type lookup_shadow_block = LIBCONTACTS_BLOCK_IGNORE;
time_t soft_unblock = 0, hard_unblock = 0;
time_t lookup_soft_unblock = 0, lookup_hard_unblock = 0;
- const char *srv = NULL, *type = NULL, *style = NULL, *ask = NULL, *ublk = NULL;
- const char *lookup_srv = NULL, *lookup_type = NULL, *lookup_style = NULL;
- const char *lookup_ask = NULL, *lookup_ublk = NULL;
+ char *srv = NULL, *type = NULL, *style = NULL, *ask = NULL, *ublk = NULL;
+ char *lookup_srv = NULL, *lookup_type = NULL, *lookup_style = NULL;
+ char *lookup_ask = NULL, *lookup_ublk = NULL, *old_srv = NULL, global[] = ".global";
struct passwd *user;
struct libcontacts_contact contact;
char *p;
@@ -164,6 +164,7 @@ main(int argc, char *argv[])
if (libcontacts_load_contact(argv[0], &contact, user))
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
+ i = 0;
if (edit && contact.blocks) {
for (i = 0; contact.blocks[i]; i++) {
if (lookup_srv && strcmpnul(contact.blocks[i]->service, lookup_srv))
@@ -177,8 +178,8 @@ main(int argc, char *argv[])
if (lookup_ublk && contact.blocks[i]->hard_unblock != lookup_hard_unblock)
continue;
if (srv) {
- free(contact.blocks[i]->service);
- contact.blocks[i]->service = estrdup(srv);
+ old_srv = contact.blocks[i]->service;
+ contact.blocks[i]->service = srv;
}
if (type)
contact.blocks[i]->explicit = explicit;
@@ -190,13 +191,12 @@ main(int argc, char *argv[])
contact.blocks[i]->hard_unblock = hard_unblock;
}
} else if (!edit) {
- i = 0;
if (contact.blocks)
for (; contact.blocks[i]; i++);
contact.blocks = erealloc(contact.blocks, (i + 2) * sizeof(*contact.blocks));
contact.blocks[i + 1] = NULL;
contact.blocks[i] = ecalloc(1, sizeof(**contact.emails));
- contact.blocks[i]->service = estrdup(srv ? srv : ".global");
+ contact.blocks[i]->service = srv ? srv : global;
contact.blocks[i]->explicit = explicit;
contact.blocks[i]->shadow_block = shadow_block;
contact.blocks[i]->soft_unblock = soft_unblock;
@@ -205,6 +205,8 @@ main(int argc, char *argv[])
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.blocks[i]->service = old_srv;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-chats.c b/set-contact-chats.c
index 3d4bae9..d2811d0 100644
--- a/set-contact-chats.c
+++ b/set-contact-chats.c
@@ -1,28 +1,57 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-a | -c | -s | -u] contact-id context service address");
+USAGE("[-A old-address] [-C old-context] [-S old-service] ([-a new-adress] [-c new-context] [-s new-service] | -u) contact-id");
int
main(int argc, char *argv[])
{
- int update_address = 0, update_context = 0, update_service = 0;
- int remove = 0, edit;
+ int add = 1, edit = 0, remove = 0;
+ char *address = NULL, *context = NULL, *service = NULL;
+ char *lookup_address = NULL, *lookup_context = NULL, *lookup_service = NULL;
+ char *old_address = NULL, *old_context = NULL, *old_service = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_chat **r, **w;
size_t i;
ARGBEGIN {
+ case 'A':
+ add = 0;
+ if (lookup_address)
+ usage();
+ lookup_address = ARG();
+ break;
case 'a':
- update_address = 1;
+ edit = 1;
+ if (address)
+ usage();
+ address = ARG();
+ break;
+ case 'C':
+ add = 0;
+ if (lookup_context)
+ usage();
+ lookup_context = ARG();
break;
case 'c':
- update_context = 1;
+ edit = 1;
+ if (context)
+ usage();
+ context = ARG();
+ break;
+ case 'S':
+ add = 0;
+ if (lookup_service)
+ usage();
+ lookup_service = ARG();
break;
case 's':
- update_service = 1;
+ edit = 1;
+ if (service)
+ usage();
+ service = ARG();
break;
case 'u':
remove = 1;
@@ -31,11 +60,16 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- edit = update_address + update_context + update_service + remove;
- if (edit > 1 || argc != 4)
- usage();
+ if (remove == edit) {
+ if (edit)
+ usage();
+ eprintf("at least one of -acsu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 1 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -47,65 +81,52 @@ main(int argc, char *argv[])
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
i = 0;
- if (contact.chats) {
+ if ((edit || remove) && contact.chats) {
+ for (r = contact.chats; *r; r++) {
+ if (lookup_context && strcmpnul((*r)->context, lookup_context))
+ continue;
+ if (lookup_address && strcmpnul((*r)->context, lookup_address))
+ continue;
+ break;
+ }
if (!edit) {
- for (; contact.chats[i]; i++);
- } else if (update_address) {
- for (; contact.chats[i]; i++) {
- if (!strcmpnul(contact.chats[i]->context, argv[1])) {
- if (!strcmpnul(contact.chats[i]->service, argv[2])) {
- free(contact.chats[i]->address);
- contact.chats[i]->address = estrdup(argv[3]);
- goto save;
- }
- }
+ libcontacts_chat_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (context) {
+ old_context = contact.chats[i]->context;
+ contact.chats[i]->context = context;
}
- } else if (update_context) {
- for (; contact.chats[i]; i++) {
- if (!strcmpnul(contact.chats[i]->service, argv[2])) {
- if (!strcmpnul(contact.chats[i]->address, argv[3])) {
- free(contact.chats[i]->context);
- contact.chats[i]->context = estrdup(argv[1]);
- goto save;
- }
- }
+ if (service) {
+ old_service = contact.chats[i]->service;
+ contact.chats[i]->service = service;
}
- } else if (update_service) {
- for (; contact.chats[i]; i++) {
- if (!strcmpnul(contact.chats[i]->context, argv[1])) {
- if (!strcmpnul(contact.chats[i]->address, argv[3])) {
- free(contact.chats[i]->context);
- contact.chats[i]->service = estrdup(argv[2]);
- goto save;
- }
- }
+ if (address) {
+ old_address = contact.chats[i]->address;
+ contact.chats[i]->address = address;
}
} else {
- for (; contact.chats[i]; i++)
- if (!strcmpnul(contact.chats[i]->context, argv[1]))
- if (!strcmpnul(contact.chats[i]->service, argv[2]))
- if (!strcmpnul(contact.chats[i]->address, argv[3]))
- break;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- }
- if (!edit || update_address || update_context || update_service) {
+ } else if (!edit && !remove) {
+ if (contact.chats)
+ for (i = 0; contact.chats[i]; i++);
contact.chats = erealloc(contact.chats, (i + 2) * sizeof(*contact.chats));
contact.chats[i + 1] = NULL;
contact.chats[i] = ecalloc(1, sizeof(**contact.chats));
- contact.chats[i]->context = estrdup(argv[1]);
- contact.chats[i]->service = estrdup(argv[2]);
- contact.chats[i]->address = estrdup(argv[3]);
- } else if (contact.chats && contact.chats[i]) {
- libcontacts_chat_destroy(contact.chats[i]);
- free(contact.chats[i]);
- for (r = &1[w = &contact.chats[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ contact.chats[i]->context = context;
+ contact.chats[i]->service = service;
+ contact.chats[i]->address = address;
}
-save:
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.chats[i]->context = old_context;
+ contact.chats[i]->service = old_service;
+ contact.chats[i]->address = old_address;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-emails.c b/set-contact-emails.c
index d015533..d1cece5 100644
--- a/set-contact-emails.c
+++ b/set-contact-emails.c
@@ -1,42 +1,63 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-a | -c] contact-id context address | -u contact-id context [address] | -U contact-id [context] address");
+USAGE("[-A old-address] [-C old-context] ([-a new-adress] [-c new-context] | -u) contact-id");
int
main(int argc, char *argv[])
{
- int update_address = 0, update_context = 0;
- int remove_by_context = 0, remove_by_address = 0;
- int edit;
+ int add = 1, edit = 0, remove = 0;
+ char *address = NULL, *context = NULL;
+ char *lookup_address = NULL, *lookup_context = NULL;
+ char *old_address = NULL, *old_context = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_email **r, **w;
size_t i;
ARGBEGIN {
+ case 'A':
+ add = 0;
+ if (lookup_address)
+ usage();
+ lookup_address = ARG();
+ break;
case 'a':
- update_address = 1;
+ edit = 1;
+ if (address)
+ usage();
+ address = ARG();
+ break;
+ case 'C':
+ add = 0;
+ if (lookup_context)
+ usage();
+ lookup_context = ARG();
break;
case 'c':
- update_context = 1;
+ edit = 1;
+ if (context)
+ usage();
+ context = ARG();
break;
case 'u':
- remove_by_context = 1;
- break;
- case 'U':
- remove_by_address = 1;
+ remove = 1;
break;
default:
usage();
} ARGEND;
- edit = update_address + update_context + remove_by_context + remove_by_address;
- if (edit > 1 || argc < 3 - remove_by_context - remove_by_address || argc > 3)
- usage();
+ if (remove == edit) {
+ if (edit)
+ usage();
+ eprintf("at least one of -acu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 1 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -48,57 +69,46 @@ main(int argc, char *argv[])
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
i = 0;
- if (contact.emails) {
+ if ((edit || remove) && contact.emails) {
+ for (r = contact.emails; *r; r++) {
+ if (lookup_context && strcmpnul((*r)->context, lookup_context))
+ continue;
+ if (lookup_address && strcmpnul((*r)->context, lookup_address))
+ continue;
+ break;
+ }
if (!edit) {
- for (; contact.emails[i]; i++);
- } else if (update_address) {
- for (; contact.emails[i]; i++) {
- if (!strcmpnul(contact.emails[i]->context, argv[1])) {
- free(contact.emails[i]->address);
- contact.emails[i]->address = estrdup(argv[2]);
- goto save;
- }
+ libcontacts_email_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (context) {
+ old_context = contact.emails[i]->context;
+ contact.emails[i]->context = context;
}
- } else if (update_context) {
- for (; contact.emails[i]; i++) {
- if (!strcmpnul(contact.emails[i]->address, argv[2])) {
- free(contact.emails[i]->context);
- contact.emails[i]->context = estrdup(argv[1]);
- goto save;
- }
+ if (address) {
+ old_address = contact.emails[i]->address;
+ contact.emails[i]->address = address;
}
- } else if (argc == 3) {
- for (; contact.emails[i]; i++)
- if (!strcmpnul(contact.emails[i]->context, argv[1]))
- if (!strcmpnul(contact.emails[i]->address, argv[2]))
- break;
- } else if (remove_by_context) {
- for (; contact.emails[i]; i++)
- if (!strcmpnul(contact.emails[i]->context, argv[1]))
- break;
} else {
- for (; contact.emails[i]; i++)
- if (!strcmpnul(contact.emails[i]->address, argv[1]))
- break;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- }
- if (!edit || update_address || update_context) {
+ } else if (!edit && !remove) {
+ if (contact.emails)
+ for (i = 0; contact.emails[i]; i++);
contact.emails = erealloc(contact.emails, (i + 2) * sizeof(*contact.emails));
contact.emails[i + 1] = NULL;
contact.emails[i] = ecalloc(1, sizeof(**contact.emails));
- contact.emails[i]->context = estrdup(argv[1]);
- contact.emails[i]->address = estrdup(argv[2]);
- } else if (contact.emails && contact.emails[i]) {
- libcontacts_email_destroy(contact.emails[i]);
- free(contact.emails[i]);
- for (r = &1[w = &contact.emails[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ contact.emails[i]->context = context;
+ contact.emails[i]->address = address;
}
-save:
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.emails[i]->context = old_context;
+ contact.emails[i]->address = old_address;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-numbers.c b/set-contact-numbers.c
index f75f9f5..b3ecfb2 100644
--- a/set-contact-numbers.c
+++ b/set-contact-numbers.c
@@ -1,14 +1,16 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-C old-context] [-c new-context] [-N old-number] [-n new-number] [-F | -f] [-M | -m] [-u] contact-id");
+USAGE("[-C old-context] [-N old-number] [-F | -f] [-M | -m] ([-c new-context] [-n new-number] | -u) contact-id");
int
main(int argc, char *argv[])
{
int set_facsimile = -1, set_mobile = -1, add = 1, edit = 0, remove = 0;
- const char *context, *number, *lookup_context, *lookup_number;
+ char *context = NULL, *number = NULL;
+ char *lookup_context = NULL, *lookup_number = NULL;
+ char *old_context = NULL, *old_number = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_number **r, **w;
@@ -68,7 +70,7 @@ main(int argc, char *argv[])
if (remove == edit) {
if (edit)
- eprintf("-u cannot be combined with -cn\n");
+ usage();
eprintf("at least one of -cnu is required\n");
}
@@ -101,12 +103,12 @@ main(int argc, char *argv[])
for (w = r++; (*w++ = *r++););
} else if (*r) {
if (context) {
- free(contact.numbers[i]->context);
- contact.numbers[i]->context = estrdup(context);
+ old_context = contact.numbers[i]->context;
+ contact.numbers[i]->context = context;
}
if (number) {
- free(contact.numbers[i]->number);
- contact.numbers[i]->number = estrdup(number);
+ old_number = contact.numbers[i]->number;
+ contact.numbers[i]->number = number;
}
if (set_mobile >= 0)
contact.numbers[i]->is_mobile = set_mobile;
@@ -122,14 +124,17 @@ main(int argc, char *argv[])
contact.numbers = erealloc(contact.numbers, (i + 2) * sizeof(*contact.numbers));
contact.numbers[i + 1] = NULL;
contact.numbers[i] = ecalloc(1, sizeof(**contact.numbers));
- contact.numbers[i]->context = estrdup(argv[1]);
- contact.numbers[i]->number = estrdup(argv[2]);
+ contact.numbers[i]->context = context;
+ contact.numbers[i]->number = number;
contact.numbers[i]->is_mobile = set_mobile > 0;
contact.numbers[i]->is_facsimile = set_facsimile > 0;
}
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.numbers[i]->context = old_context;
+ contact.numbers[i]->number = old_number;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-organisations.c b/set-contact-organisations.c
index d1fbeaf..733b8de 100644
--- a/set-contact-organisations.c
+++ b/set-contact-organisations.c
@@ -1,42 +1,63 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-o | -t] contact-id organisation title | -u contact-id organisation [title] | -U contact-id [organisation] title");
+USAGE("[-O old-organisation] [-T old-title] ([-o new-adress] [-t new-title] | -u) contact-id");
int
main(int argc, char *argv[])
{
- int update_title = 0, update_organisation = 0;
- int remove_by_organisation = 0, remove_by_title = 0;
- int edit;
+ int add = 1, edit = 0, remove = 0;
+ char *organisation = NULL, *title = NULL;
+ char *lookup_organisation = NULL, *lookup_title = NULL;
+ char *old_organisation = NULL, *old_title = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_organisation **r, **w;
size_t i;
ARGBEGIN {
+ case 'O':
+ add = 0;
+ if (lookup_organisation)
+ usage();
+ lookup_organisation = ARG();
+ break;
case 'o':
- update_title = 1;
+ edit = 1;
+ if (organisation)
+ usage();
+ organisation = ARG();
+ break;
+ case 'T':
+ add = 0;
+ if (lookup_title)
+ usage();
+ lookup_title = ARG();
break;
case 't':
- update_organisation = 1;
+ edit = 1;
+ if (title)
+ usage();
+ title = ARG();
break;
case 'u':
- remove_by_organisation = 1;
- break;
- case 'U':
- remove_by_title = 1;
+ remove = 1;
break;
default:
usage();
} ARGEND;
- edit = update_title + update_organisation + remove_by_organisation + remove_by_title;
- if (edit > 1 || argc < 3 - remove_by_organisation - remove_by_title || argc > 3)
- usage();
+ if (remove == edit) {
+ if (edit)
+ usage();
+ eprintf("at least one of -otu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 1 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -48,57 +69,46 @@ main(int argc, char *argv[])
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
i = 0;
- if (contact.organisations) {
+ if ((edit || remove) && contact.organisations) {
+ for (r = contact.organisations; *r; r++) {
+ if (lookup_title && strcmpnul((*r)->title, lookup_title))
+ continue;
+ if (lookup_organisation && strcmpnul((*r)->title, lookup_organisation))
+ continue;
+ break;
+ }
if (!edit) {
- for (; contact.organisations[i]; i++);
- } else if (update_title) {
- for (; contact.organisations[i]; i++) {
- if (!strcmpnul(contact.organisations[i]->organisation, argv[1])) {
- free(contact.organisations[i]->title);
- contact.organisations[i]->title = estrdup(argv[2]);
- goto save;
- }
+ libcontacts_organisation_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (title) {
+ old_title = contact.organisations[i]->title;
+ contact.organisations[i]->title = title;
}
- } else if (update_organisation) {
- for (; contact.organisations[i]; i++) {
- if (!strcmpnul(contact.organisations[i]->title, argv[2])) {
- free(contact.organisations[i]->organisation);
- contact.organisations[i]->organisation = estrdup(argv[1]);
- goto save;
- }
+ if (organisation) {
+ old_organisation = contact.organisations[i]->organisation;
+ contact.organisations[i]->organisation = organisation;
}
- } else if (argc == 3) {
- for (; contact.organisations[i]; i++)
- if (!strcmpnul(contact.organisations[i]->organisation, argv[1]))
- if (!strcmpnul(contact.organisations[i]->title, argv[2]))
- break;
- } else if (remove_by_organisation) {
- for (; contact.organisations[i]; i++)
- if (!strcmpnul(contact.organisations[i]->organisation, argv[1]))
- break;
} else {
- for (; contact.organisations[i]; i++)
- if (!strcmpnul(contact.organisations[i]->title, argv[1]))
- break;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- }
- if (!edit || update_title || update_organisation) {
+ } else if (!edit && !remove) {
+ if (contact.organisations)
+ for (i = 0; contact.organisations[i]; i++);
contact.organisations = erealloc(contact.organisations, (i + 2) * sizeof(*contact.organisations));
contact.organisations[i + 1] = NULL;
contact.organisations[i] = ecalloc(1, sizeof(**contact.organisations));
- contact.organisations[i]->organisation = estrdup(argv[1]);
- contact.organisations[i]->title = estrdup(argv[2]);
- } else if (contact.organisations && contact.organisations[i]) {
- libcontacts_organisation_destroy(contact.organisations[i]);
- free(contact.organisations[i]);
- for (r = &1[w = &contact.organisations[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ contact.organisations[i]->title = title;
+ contact.organisations[i]->organisation = organisation;
}
-save:
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.organisations[i]->title = old_title;
+ contact.organisations[i]->organisation = old_organisation;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-pgpkeys.c b/set-contact-pgpkeys.c
index adb62d3..93ad0ab 100644
--- a/set-contact-pgpkeys.c
+++ b/set-contact-pgpkeys.c
@@ -1,42 +1,63 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-c | -f] contact-id context fingerprint | -u contact-id context [fingerprint] | -U contact-id [context] fingerprint");
+USAGE("[-C old-context] [-F old-fingerprint] ([-c new-context] [-f new-fingerprint] | -u) contact-id");
int
main(int argc, char *argv[])
{
- int update_id = 0, update_context = 0;
- int remove_by_context = 0, remove_by_id = 0;
- int edit;
+ int add = 1, edit = 0, remove = 0;
+ char *id = NULL, *context = NULL;
+ char *lookup_id = NULL, *lookup_context = NULL;
+ char *old_id = NULL, *old_context = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_pgpkey **r, **w;
size_t i;
ARGBEGIN {
+ case 'C':
+ add = 0;
+ if (lookup_context)
+ usage();
+ lookup_context = ARG();
+ break;
case 'c':
- update_context = 1;
+ edit = 0;
+ if (context)
+ usage();
+ context = ARG();
+ break;
+ case 'F':
+ add = 1;
+ if (lookup_id)
+ usage();
+ lookup_id = ARG();
break;
case 'f':
- update_id = 1;
+ edit = 1;
+ if (id)
+ usage();
+ id = ARG();
break;
case 'u':
- remove_by_context = 1;
- break;
- case 'U':
- remove_by_id = 1;
+ remove = 1;
break;
default:
usage();
} ARGEND;
- edit = update_id + update_context + remove_by_context + remove_by_id;
- if (edit > 1 || argc < 3 - remove_by_context - remove_by_id || argc > 3)
- usage();
+ if (remove == edit) {
+ if (edit)
+ usage();
+ eprintf("at least one of -cfu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 1 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -48,57 +69,46 @@ main(int argc, char *argv[])
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
i = 0;
- if (contact.pgpkeys) {
+ if ((edit || remove) && contact.pgpkeys) {
+ for (r = contact.pgpkeys; *r; r++) {
+ if (lookup_context && strcmpnul((*r)->context, lookup_context))
+ continue;
+ if (lookup_id && strcmpnul((*r)->context, lookup_id))
+ continue;
+ break;
+ }
if (!edit) {
- for (; contact.pgpkeys[i]; i++);
- } else if (update_id) {
- for (; contact.pgpkeys[i]; i++) {
- if (!strcmpnul(contact.pgpkeys[i]->context, argv[1])) {
- free(contact.pgpkeys[i]->id);
- contact.pgpkeys[i]->id = estrdup(argv[2]);
- goto save;
- }
+ libcontacts_pgpkey_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (context) {
+ old_context = contact.pgpkeys[i]->context;
+ contact.pgpkeys[i]->context = context;
}
- } else if (update_context) {
- for (; contact.pgpkeys[i]; i++) {
- if (!strcmpnul(contact.pgpkeys[i]->id, argv[2])) {
- free(contact.pgpkeys[i]->context);
- contact.pgpkeys[i]->context = estrdup(argv[1]);
- goto save;
- }
+ if (id) {
+ old_id = contact.pgpkeys[i]->id;
+ contact.pgpkeys[i]->id = id;
}
- } else if (argc == 3) {
- for (; contact.pgpkeys[i]; i++)
- if (!strcmpnul(contact.pgpkeys[i]->context, argv[1]))
- if (!strcmpnul(contact.pgpkeys[i]->id, argv[2]))
- break;
- } else if (remove_by_context) {
- for (; contact.pgpkeys[i]; i++)
- if (!strcmpnul(contact.pgpkeys[i]->context, argv[1]))
- break;
} else {
- for (; contact.pgpkeys[i]; i++)
- if (!strcmpnul(contact.pgpkeys[i]->id, argv[1]))
- break;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- }
- if (!edit || update_id || update_context) {
+ } else if (!edit && !remove) {
+ if (contact.pgpkeys)
+ for (i = 0; contact.pgpkeys[i]; i++);
contact.pgpkeys = erealloc(contact.pgpkeys, (i + 2) * sizeof(*contact.pgpkeys));
contact.pgpkeys[i + 1] = NULL;
contact.pgpkeys[i] = ecalloc(1, sizeof(**contact.pgpkeys));
- contact.pgpkeys[i]->context = estrdup(argv[1]);
- contact.pgpkeys[i]->id = estrdup(argv[2]);
- } else if (contact.pgpkeys && contact.pgpkeys[i]) {
- libcontacts_pgpkey_destroy(contact.pgpkeys[i]);
- free(contact.pgpkeys[i]);
- for (r = &1[w = &contact.pgpkeys[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ contact.pgpkeys[i]->context = context;
+ contact.pgpkeys[i]->id = id;
}
-save:
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.pgpkeys[i]->context = old_context;
+ contact.pgpkeys[i]->id = old_id;
libcontacts_contact_destroy(&contact);
return 0;
diff --git a/set-contact-sites.c b/set-contact-sites.c
index 56a31ea..6a9f60f 100644
--- a/set-contact-sites.c
+++ b/set-contact-sites.c
@@ -1,42 +1,63 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-a | -c] contact-id context address | -u contact-id context [address] | -U contact-id [context] address");
+USAGE("[-A old-address] [-C old-context] ([-a new-adress] [-c new-context] | -u) contact-id");
int
main(int argc, char *argv[])
{
- int update_address = 0, update_context = 0;
- int remove_by_context = 0, remove_by_address = 0;
- int edit;
+ int add = 1, edit = 0, remove = 0;
+ char *address = NULL, *context = NULL;
+ char *lookup_address = NULL, *lookup_context = NULL;
+ char *old_address = NULL, *old_context = NULL;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_site **r, **w;
size_t i;
ARGBEGIN {
+ case 'A':
+ add = 0;
+ if (lookup_address)
+ usage();
+ lookup_address = ARG();
+ break;
case 'a':
- update_address = 1;
+ edit = 1;
+ if (address)
+ usage();
+ address = ARG();
+ break;
+ case 'C':
+ add = 0;
+ if (lookup_context)
+ usage();
+ lookup_context = ARG();
break;
case 'c':
- update_context = 1;
+ edit = 1;
+ if (context)
+ usage();
+ context = ARG();
break;
case 'u':
- remove_by_context = 1;
- break;
- case 'U':
- remove_by_address = 1;
+ remove = 1;
break;
default:
usage();
} ARGEND;
- edit = update_address + update_context + remove_by_context + remove_by_address;
- if (edit > 1 || argc < 3 - remove_by_context - remove_by_address || argc > 3)
- usage();
+ if (remove == edit) {
+ if (edit)
+ usage();
+ eprintf("at least one of -acu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 1 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -48,57 +69,46 @@ main(int argc, char *argv[])
eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");
i = 0;
- if (contact.sites) {
+ if ((edit || remove) && contact.sites) {
+ for (r = contact.sites; *r; r++) {
+ if (lookup_context && strcmpnul((*r)->context, lookup_context))
+ continue;
+ if (lookup_address && strcmpnul((*r)->context, lookup_address))
+ continue;
+ break;
+ }
if (!edit) {
- for (; contact.sites[i]; i++);
- } else if (update_address) {
- for (; contact.sites[i]; i++) {
- if (!strcmpnul(contact.sites[i]->context, argv[1])) {
- free(contact.sites[i]->address);
- contact.sites[i]->address = estrdup(argv[2]);
- goto save;
- }
+ libcontacts_site_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (context) {
+ old_context = contact.sites[i]->context;
+ contact.sites[i]->context = context;
}
- } else if (update_context) {
- for (; contact.sites[i]; i++) {
- if (!strcmpnul(contact.sites[i]->address, argv[2])) {
- free(contact.sites[i]->context);
- contact.sites[i]->context = estrdup(argv[1]);
- goto save;
- }
+ if (address) {
+ old_address = contact.sites[i]->address;
+ contact.sites[i]->address = address;
}
- } else if (argc == 3) {
- for (; contact.sites[i]; i++)
- if (!strcmpnul(contact.sites[i]->context, argv[1]))
- if (!strcmpnul(contact.sites[i]->address, argv[2]))
- break;
- } else if (remove_by_context) {
- for (; contact.sites[i]; i++)
- if (!strcmpnul(contact.sites[i]->context, argv[1]))
- break;
} else {
- for (; contact.sites[i]; i++)
- if (!strcmpnul(contact.sites[i]->address, argv[1]))
- break;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- }
- if (!edit || update_address || update_context) {
+ } else if (!edit && !remove) {
+ if (contact.sites)
+ for (i = 0; contact.sites[i]; i++);
contact.sites = erealloc(contact.sites, (i + 2) * sizeof(*contact.sites));
contact.sites[i + 1] = NULL;
contact.sites[i] = ecalloc(1, sizeof(**contact.sites));
- contact.sites[i]->context = estrdup(argv[1]);
- contact.sites[i]->address = estrdup(argv[2]);
- } else if (contact.sites && contact.sites[i]) {
- libcontacts_site_destroy(contact.sites[i]);
- free(contact.sites[i]);
- for (r = &1[w = &contact.sites[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ contact.sites[i]->context = context;
+ contact.sites[i]->address = address;
}
-save:
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
+
+ contact.sites[i]->context = old_context;
+ contact.sites[i]->address = old_address;
libcontacts_contact_destroy(&contact);
return 0;