From 9a662f23138f999f49dd97f77de39ef99f80425e Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Wed, 19 Dec 2012 15:03:56 +0100 Subject: [PATCH] extract urls --- container.c | 36 +++++++++++++++++++++++++++++++++ container.h | 10 +++++++++ dunst.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ dunst.h | 1 + 4 files changed, 105 insertions(+) diff --git a/container.c b/container.c index 8e89fa3..2db3e80 100644 --- a/container.c +++ b/container.c @@ -1,5 +1,6 @@ #include "stdlib.h" #include "stdio.h" +#include #include "container.h" @@ -138,4 +139,39 @@ int n_queue_len(n_queue **q) return n_stack_len(q); } +str_array *str_array_malloc(void) +{ + str_array *a = malloc(sizeof(str_array)); + a->count = 0; + a->strs = NULL; + return a; +} + +void str_array_append(str_array *a, char *str) +{ + if (!a) + return; + a->count++; + a->strs = realloc(a->strs, a->count); + (a->strs)[a->count-1] = str; +} + +void str_array_dup_append(str_array *a, const char *str) +{ + if (!a) + return; + char *dup = strdup(str); + str_array_append(a, dup); +} + +void str_array_deep_free(str_array *a) +{ + if (!a) + return; + for (int i = 0; i < a->count; i++) { + free((a->strs)[i]); + } + free(a); +} + /* vim: set ts=8 sw=8 tw=0: */ diff --git a/container.h b/container.h index 9a16150..1e8e0b5 100644 --- a/container.h +++ b/container.h @@ -10,6 +10,16 @@ typedef struct _n_stack { typedef n_stack n_queue; +typedef struct _str_array { + int count; + char **strs; +} str_array; + +str_array *str_array_malloc(void); +void str_array_dup_append(str_array *a, const char *str); +void str_array_append(str_array *a, char *str); +void str_array_deep_free(str_array *a); + int cmp_notification(notification *a, notification *b); /************ diff --git a/dunst.c b/dunst.c index efc7d21..bdd9255 100644 --- a/dunst.c +++ b/dunst.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -100,6 +101,7 @@ void draw_win(void); void hide_win(void); void move_all_to_history(void); void print_version(void); +str_array *extract_urls(const char *str); void r_line_cache_init(r_line_cache *c); void r_line_cache_append(r_line_cache *c, const char *s, ColorSet *col, bool continues); @@ -108,6 +110,49 @@ void r_line_cache_reset(r_line_cache *c); void init_shortcut(keyboard_shortcut * shortcut); KeySym string_to_mask(char *str); +str_array *extract_urls( const char * to_match) +{ + static bool is_initialized = false; + static regex_t cregex; + + if (!is_initialized) { + char *regex = "((http|ftp|https)(://))?(www\\.)?[[:alnum:]_-]+\\.[^[:space:]]+"; + int ret = regcomp(&cregex, regex, REG_EXTENDED|REG_ICASE); + if (ret != 0) { + printf("failed to compile regex\n"); + return NULL; + } + } + + str_array *urls = str_array_malloc(); + + const char * p = to_match; + regmatch_t m; + + while (1) { + int nomatch = regexec (&cregex, p, 1, &m, 0); + if (nomatch) { + return urls; + } + int start; + int finish; + if (m.rm_so == -1) { + break; + } + start = m.rm_so + (p - to_match); + finish = m.rm_eo + (p - to_match); + + char *match = strndup(to_match+start, finish-start); + + str_array_append(urls, match); + + p += m.rm_eo; + } + return 0; + + return urls; +} + void pause_signal_handler(int sig) { if (sig == SIGUSR1) { @@ -130,6 +175,12 @@ static void print_notification(notification * n) printf("\turgency: %d\n", n->urgency); printf("\tformatted: %s\n", n->msg); printf("\tid: %d\n", n->id); + printf("urls\n"); + printf("\t{\n"); + for (int i = 0; i < n->urls->count; i++) { + printf("\t\t%s\n", (n->urls->strs)[i]); + } + printf("\t}\n"); printf("}\n"); } @@ -911,6 +962,13 @@ int init_notification(notification * n, int id) n_queue_enqueue(&queue, n); } + char *tmp; + asprintf(&tmp, "%s %s", n->summary, n->body); + + n->urls = extract_urls(tmp); + + free(tmp); + if (print_notifications) print_notification(n); diff --git a/dunst.h b/dunst.h index 6f9b854..b68a2ef 100644 --- a/dunst.h +++ b/dunst.h @@ -51,6 +51,7 @@ typedef struct _notification { char *color_strings[2]; int progress; /* percentage + 1, 0 to hide */ int line_count; + struct { int count; char **strs; } *urls; } notification; typedef struct _notification_buffer {