aboutsummaryrefslogtreecommitdiffstats
path: root/set-contact-numbers.c
diff options
context:
space:
mode:
Diffstat (limited to 'set-contact-numbers.c')
-rw-r--r--set-contact-numbers.c126
1 files changed, 61 insertions, 65 deletions
diff --git a/set-contact-numbers.c b/set-contact-numbers.c
index c03e175..f75f9f5 100644
--- a/set-contact-numbers.c
+++ b/set-contact-numbers.c
@@ -1,26 +1,43 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-USAGE("[-c | -n] [-F | -f] [-M | -m] contact-id context number | -u contact-id context [number] | -U contact-id [context] number");
+USAGE("[-C old-context] [-c new-context] [-N old-number] [-n new-number] [-F | -f] [-M | -m] [-u] contact-id");
int
main(int argc, char *argv[])
{
- int set_facsimile = -1, set_mobile = -1;
- int update_number = 0, update_context = 0;
- int remove_by_context = 0, remove_by_number = 0;
+ int set_facsimile = -1, set_mobile = -1, add = 1, edit = 0, remove = 0;
+ const char *context, *number, *lookup_context, *lookup_number;
struct passwd *user;
struct libcontacts_contact contact;
struct libcontacts_number **r, **w;
size_t i;
ARGBEGIN {
+ 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 'N':
+ add = 0;
+ if (lookup_number)
+ usage();
+ lookup_number = ARG();
break;
case 'n':
- update_number = 1;
+ edit = 1;
+ if (number)
+ usage();
+ number = ARG();
break;
case 'F':
if (set_facsimile >= 0)
@@ -43,23 +60,22 @@ main(int argc, char *argv[])
set_mobile = 1;
break;
case 'u':
- remove_by_context = 1;
- break;
- case 'U':
- remove_by_number = 1;
+ remove = 1;
break;
default:
usage();
} ARGEND;
- if (update_number + update_context + remove_by_context + remove_by_number > 0)
- usage();
- if (argc < 3 - remove_by_context - remove_by_number || argc > 3)
- usage();
- if ((set_facsimile >= 0 || set_mobile >= 0) && remove_by_context + remove_by_number)
- usage();
+ if (remove == edit) {
+ if (edit)
+ eprintf("-u cannot be combined with -cn\n");
+ eprintf("at least one of -cnu is required\n");
+ }
- if (!*argv[0] || strchr(argv[0], '/'))
+ if (add)
+ edit = 0;
+
+ if (argc != 0 || !*argv[0] || strchr(argv[0], '/'))
usage();
errno = 0;
@@ -71,53 +87,38 @@ 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.numbers) {
- if (update_number) {
- for (; contact.numbers[i]; i++) {
- if (!strcmpnul(contact.numbers[i]->context, argv[1])) {
- free(contact.numbers[i]->number);
- contact.numbers[i]->number = estrdup(argv[2]);
- goto save;
- }
- }
- } else if (update_context) {
- for (; contact.numbers[i]; i++) {
- if (!strcmpnul(contact.numbers[i]->number, argv[2])) {
- free(contact.numbers[i]->context);
- contact.numbers[i]->context = estrdup(argv[1]);
- goto save;
- }
+ if ((edit || remove) && contact.numbers) {
+ for (r = contact.numbers; *r; r++) {
+ if (lookup_context && strcmpnul((*r)->context, lookup_context))
+ continue;
+ if (lookup_number && strcmpnul((*r)->context, lookup_number))
+ continue;
+ break;
+ }
+ if (!edit) {
+ libcontacts_number_destroy(*r);
+ free(*r);
+ for (w = r++; (*w++ = *r++););
+ } else if (*r) {
+ if (context) {
+ free(contact.numbers[i]->context);
+ contact.numbers[i]->context = estrdup(context);
}
- } else if (!remove_by_context && !remove_by_number) {
- for (; contact.numbers[i]; i++) {
- if (!strcmpnul(contact.numbers[i]->context, argv[1]))
- if (!strcmpnul(contact.numbers[i]->number, argv[2]))
- goto save;
+ if (number) {
+ free(contact.numbers[i]->number);
+ contact.numbers[i]->number = estrdup(number);
}
- } else if (argc == 3) {
- for (; contact.numbers[i]; i++)
- if (!strcmpnul(contact.numbers[i]->context, argv[1]))
- if (!strcmpnul(contact.numbers[i]->number, argv[2]))
- break;
- } else if (remove_by_context) {
- for (; contact.numbers[i]; i++)
- if (!strcmpnul(contact.numbers[i]->context, argv[1]))
- break;
+ if (set_mobile >= 0)
+ contact.numbers[i]->is_mobile = set_mobile;
+ if (set_facsimile >= 0)
+ contact.numbers[i]->is_facsimile = set_facsimile;
} else {
- for (; contact.numbers[i]; i++)
- if (!strcmpnul(contact.numbers[i]->number, argv[1]))
- break;
- }
- }
- if (remove_by_context || remove_by_number) {
- if (contact.numbers && contact.numbers[i]) {
- libcontacts_number_destroy(contact.numbers[i]);
- free(contact.numbers[i]);
- for (r = &1[w = &contact.numbers[i]]; *r;)
- *w++ = *r++;
- *w = NULL;
+ libcontacts_contact_destroy(&contact);
+ return 0;
}
- } else {
+ } else if (!edit && !remove) {
+ if (contact.numbers)
+ for (i = 0; contact.numbers[i]; i++);
contact.numbers = erealloc(contact.numbers, (i + 2) * sizeof(*contact.numbers));
contact.numbers[i + 1] = NULL;
contact.numbers[i] = ecalloc(1, sizeof(**contact.numbers));
@@ -127,11 +128,6 @@ main(int argc, char *argv[])
contact.numbers[i]->is_facsimile = set_facsimile > 0;
}
-save:
- if (set_mobile >= 0)
- contact.numbers[i]->is_mobile = set_mobile;
- if (set_facsimile >= 0)
- contact.numbers[i]->is_facsimile = set_facsimile;
if (libcontacts_save_contact(&contact, user))
eprintf("libcontacts_save_contact %s:", argv[0]);
libcontacts_contact_destroy(&contact);