extract urls
This commit is contained in:
parent
b5f4feee27
commit
9a662f2313
36
container.c
36
container.c
@ -1,5 +1,6 @@
|
||||
#include "stdlib.h"
|
||||
#include "stdio.h"
|
||||
#include <string.h>
|
||||
|
||||
#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: */
|
||||
|
10
container.h
10
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);
|
||||
|
||||
/************
|
||||
|
58
dunst.c
58
dunst.c
@ -11,6 +11,7 @@
|
||||
#include <string.h>
|
||||
#include <fnmatch.h>
|
||||
#include <sys/time.h>
|
||||
#include <regex.h>
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <X11/Xlib.h>
|
||||
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user