X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=filter.c;h=a58868c5f8835352204a167cc4393dad7b2cc849;hb=c6c64f87def0694eb80ecc10fde881a203e82fcf;hp=184128017b9b4bdd8c4c6942be77535334f90ac6;hpb=8420245db43e7f7ac1c8414a275e633e1d56c883;p=pkg%2Fabook.git diff --git a/filter.c b/filter.c index 1841280..a58868c 100644 --- a/filter.c +++ b/filter.c @@ -60,11 +60,13 @@ static int allcsv_export_database(FILE *out, struct db_enumerator e); static int palm_export_database(FILE *out, struct db_enumerator e); static int vcard_export_database(FILE *out, struct db_enumerator e); static int mutt_alias_export(FILE *out, struct db_enumerator e); +static int mutt_query_export_database(FILE *out, struct db_enumerator e); static int elm_alias_export(FILE *out, struct db_enumerator e); static int text_export_database(FILE *out, struct db_enumerator e); static int spruce_export_database(FILE *out, struct db_enumerator e); static int wl_export_database(FILE *out, struct db_enumerator e); static int bsdcal_export_database(FILE *out, struct db_enumerator e); +static int custom_export_database(FILE *out, struct db_enumerator e); /* * end of function declarations @@ -87,6 +89,7 @@ struct abook_output_filter e_filters[] = { { "ldif", N_("ldif / Netscape addressbook (.4ld)"), ldif_export_database }, { "vcard", N_("vCard 2 file"), vcard_export_database }, { "mutt", N_("mutt alias"), mutt_alias_export }, + { "muttq", N_("mutt query format (internal use)"), mutt_query_export_database }, { "html", N_("html document"), html_export_database }, { "pine", N_("pine addressbook"), pine_export_database }, { "csv", N_("comma separated values"), csv_export_database }, @@ -100,6 +103,11 @@ struct abook_output_filter e_filters[] = { { "\0", NULL, NULL } }; +struct abook_output_item_filter u_filters[] = { + { "muttq", N_("mutt alias"), muttq_print_item }, + { "\0", NULL } +}; + /* * common functions */ @@ -122,6 +130,13 @@ print_filters() gettext(e_filters[i].desc)); putchar('\n'); + + puts(_("output (with query):")); + for(i=0; *u_filters[i].filtname ; i++) + printf("\t%s\t%s\n", u_filters[i].filtname, + gettext(u_filters[i].desc)); + + putchar('\n'); } static int @@ -361,6 +376,25 @@ export_database() return 0; } +struct abook_output_item_filter select_output_item_filter(char filtname[FILTNAME_LEN]) { + int i; + for(i=0;; i++) { + if(!strncasecmp(u_filters[i].filtname, filtname, FILTNAME_LEN)) + break; + if(!*u_filters[i].filtname) { + i = -1; + break; + } + } + return u_filters[i]; +} + +void +e_write_item(FILE *out, int item, void (*func) (FILE *in, int item)) +{ + (*func) (out, item); +} + static int e_write_file(char *filename, int (*func) (FILE *in, struct db_enumerator e), int mode) @@ -2008,6 +2042,33 @@ mutt_alias_export(FILE *out, struct db_enumerator e) return 0; } +void muttq_print_item(FILE *file, int item) +{ + abook_list *emails, *e; + char *tmp = db_email_get(item); + + emails = csv_to_abook_list(tmp); + free(tmp); + + for(e = emails; e; e = e->next) { + fprintf(file, "%s\t%s\t%s\n", e->data, db_name_get(item), + !db_fget(item, NOTES) ?" " :db_fget(item, NOTES) + ); + if(!opt_get_bool(BOOL_MUTT_RETURN_ALL_EMAILS)) + break; + } + abook_list_free(&emails); +} + +static int +mutt_query_export_database(FILE *out, struct db_enumerator e) +{ + fprintf(out, "All items\n"); + db_enumerate_items(e) + muttq_print_item(out, e.item); + return 0; +} + /* * end of mutt alias export filter */ @@ -2272,6 +2333,176 @@ bsdcal_export_database(FILE *out, struct db_enumerator e) return 0; } +// see enum field_types @database.h +static char *conv_table[] = { + "name", + "email", + "address", + "address2", + "city", + "state", + "zip", + "country", + "phone", + "workphone", + "fax", + "mobile", + "nick", + "url", + "notes", + "anniversary", + 0 /* ITEM_FIELDS */ +}; + +static int +find_field_enum(char *s) { + int i = 0; + while (conv_table[i]) { + if(!safe_strcmp(conv_table[i], s)) + return i; + i++; + } + // failed + return -1; +} + +/* Convert a string with named placeholders to + a *printf() compatible string. + Stores the abook field values into ft. */ +void +parse_custom_format(char *s, char *fmt_string, enum field_types *ft) +{ + if(! fmt_string || ! ft) { + fprintf(stderr, _("parse_custom_format: fmt_string or ft not allocated\n")); + exit(EXIT_FAILURE); + } + + char *p, *start, *field_name = NULL; + p = start = s; + + while(*p) { + if(*p == '{') { + start = ++p; + p = strchr(start, '}'); + if(! *p) { + fprintf(stderr, _("parse_custom_format: invalid format\n")); + exit(EXIT_FAILURE); + } + strcat(fmt_string, "%s"); + field_name = strndup(start, (size_t)(p-start)); + *ft = find_field_enum(field_name); + if(*ft == -1) { + fprintf(stderr, _("parse_custom_format: invalid placeholder: {%s}\n"), field_name); + exit(EXIT_FAILURE); + } + + ft++; + p++; + start = p; + } else { + p = strchr(start, '{'); + if(p && *p) { + strncat(fmt_string, start, (size_t)(p-start)); + start = p; + } + else { + strncat( fmt_string, + start, + FORMAT_STRING_LEN - strlen(fmt_string) - 1 ); + break; + } + } + } + *ft = 66; +} + +static int +custom_export_item(FILE *out, int item, char *s, enum field_types *ft); + + +// used to store the format string from --outformatstr when "custom" format is used +// default value overriden in export_file() +extern char *parsed_custom_format; +extern enum field_types *custom_format_fields; + +/* wrapper for custom_export_item: + 1) avoid messing with extern pointer + 2) adds \n + 3) follow the prototype needed for an abook_output_item_filter entry */ +void +custom_print_item(FILE *out, int item) +{ + + if(custom_export_item(out, item, parsed_custom_format, custom_format_fields) == 0) + fprintf(out, "\n"); +} + +static int +custom_export_item(FILE *out, int item, char *fmt, enum field_types *ft) +{ + char *p, *q = 0; + + // if the first character is '!': + // we first check that all fields exist before continuing + if(*fmt == '!') { + enum field_types *ftp = ft; + while(*ft != 66) { + if(! db_fget(item, *ft) ) + return 1; + ft++; + } + ft = ftp; + fmt++; + } + + while (*fmt) { + if(!strncmp(fmt, "%s", 2)) { + fprintf(out, "%s", safe_str(db_fget(item, *ft))); + ft++; + fmt+=2; + } else if (*ft == 66) { + fprintf(out, "%s", fmt); + return 0; + } else { + p = strchr(fmt, '%'); + if(*p) { + q = strndup(fmt, (size_t)(p-fmt)); + fprintf(out, "%s", q); + free(q); + fmt = p; + } + else { + fprintf(out, "%s", fmt); + return 0; + } + } + } + + return 0; +} + +// used to store the format string from --outformatstr when "custom" format is used +// default value overriden from abook.c +extern char custom_format[FORMAT_STRING_LEN]; + +static int +custom_export_database(FILE *out, struct db_enumerator e) +{ + char *format_string = + (char *)malloc(FORMAT_STRING_LEN * sizeof(char*)); + + enum field_types *ft = + (enum field_types *)malloc(FORMAT_STRING_MAX_FIELDS * sizeof(enum field_types *)); + + parse_custom_format(custom_format, format_string, ft); + + db_enumerate_items(e) { + if(custom_export_item(out, e.item, format_string, ft) == 0) + fprintf(out, "\n"); + } + return 0; +} + /* * end of BSD calendar export filter */