[bd3f232] | 1 | #include "owl.h" |
---|
| 2 | |
---|
| 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[f1e629d] | 5 | void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description) |
---|
[bd3f232] | 6 | { |
---|
| 7 | s->type=OWL_STYLE_TYPE_INTERNAL; |
---|
| 8 | s->name=owl_strdup(name); |
---|
[f1e629d] | 9 | if (description) { |
---|
| 10 | s->description=owl_strdup(description); |
---|
| 11 | } else { |
---|
| 12 | s->description=owl_sprintf("Owl internal style %s", name); |
---|
| 13 | } |
---|
[bd3f232] | 14 | s->perlfuncname=NULL; |
---|
| 15 | s->formatfunc=formatfunc; |
---|
| 16 | } |
---|
| 17 | |
---|
[f1e629d] | 18 | void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description) |
---|
[bd3f232] | 19 | { |
---|
| 20 | s->type=OWL_STYLE_TYPE_PERL; |
---|
| 21 | s->name=owl_strdup(name); |
---|
| 22 | s->perlfuncname=owl_strdup(perlfuncname); |
---|
[f1e629d] | 23 | if (description) { |
---|
| 24 | s->description=owl_strdup(description); |
---|
| 25 | } else { |
---|
| 26 | s->description=owl_sprintf("User-defined perl style that calls %s", |
---|
| 27 | perlfuncname); |
---|
| 28 | } |
---|
[bd3f232] | 29 | s->formatfunc=NULL; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | int owl_style_matches_name(owl_style *s, char *name) |
---|
| 33 | { |
---|
| 34 | if (!strcmp(s->name, name)) return(1); |
---|
| 35 | return(0); |
---|
| 36 | } |
---|
| 37 | |
---|
[ef56a67] | 38 | char *owl_style_get_name(owl_style *s) |
---|
| 39 | { |
---|
| 40 | return(s->name); |
---|
| 41 | } |
---|
| 42 | |
---|
[f1e629d] | 43 | char *owl_style_get_description(owl_style *s) |
---|
| 44 | { |
---|
| 45 | return(s->description); |
---|
| 46 | } |
---|
| 47 | |
---|
[5639bf2] | 48 | /* Use style 's' to format message 'm' into fmtext 'fm'. |
---|
| 49 | * 'fm' should already be be initialzed |
---|
| 50 | */ |
---|
[bd3f232] | 51 | void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m) |
---|
| 52 | { |
---|
| 53 | if (s->type==OWL_STYLE_TYPE_INTERNAL) { |
---|
| 54 | (* s->formatfunc)(fm, m); |
---|
| 55 | } else if (s->type==OWL_STYLE_TYPE_PERL) { |
---|
[a7a42b9] | 56 | char *body, *indent; |
---|
| 57 | int curlen; |
---|
[bd3f232] | 58 | |
---|
| 59 | /* run the perl function */ |
---|
[f1e629d] | 60 | body=owl_perlconfig_getmsg(m, 1, s->perlfuncname); |
---|
[5a9f6fe] | 61 | if (!strcmp(body, "")) { |
---|
| 62 | owl_free(body); |
---|
| 63 | body=owl_strdup("<unformatted message>"); |
---|
| 64 | } |
---|
[bd3f232] | 65 | |
---|
[282ec9b] | 66 | /* indent and ensure ends with a newline */ |
---|
| 67 | indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10); |
---|
[bd3f232] | 68 | owl_text_indent(indent, body, OWL_TAB); |
---|
[282ec9b] | 69 | curlen = strlen(indent); |
---|
| 70 | if (curlen==0 || indent[curlen-1] != '\n') { |
---|
| 71 | indent[curlen] = '\n'; |
---|
| 72 | indent[curlen+1] = '\0'; |
---|
| 73 | } |
---|
| 74 | |
---|
[bd3f232] | 75 | /* fmtext_append. This needs to change */ |
---|
| 76 | owl_fmtext_append_ztext(fm, indent); |
---|
| 77 | |
---|
| 78 | owl_free(indent); |
---|
| 79 | owl_free(body); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
[f1e629d] | 83 | int owl_style_validate(owl_style *s) { |
---|
| 84 | if (!s) { |
---|
| 85 | return -1; |
---|
| 86 | } else if (s->type==OWL_STYLE_TYPE_INTERNAL) { |
---|
| 87 | return 0; |
---|
| 88 | } else if (s->type==OWL_STYLE_TYPE_PERL |
---|
| 89 | && s->perlfuncname |
---|
| 90 | && owl_perlconfig_is_function(s->perlfuncname)) { |
---|
| 91 | return 0; |
---|
| 92 | } else { |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[bd3f232] | 97 | void owl_style_free(owl_style *s) |
---|
| 98 | { |
---|
| 99 | if (s->name) owl_free(s->name); |
---|
[f1e629d] | 100 | if (s->description) owl_free(s->description); |
---|
[bd3f232] | 101 | if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) { |
---|
| 102 | owl_free(s->perlfuncname); |
---|
| 103 | } |
---|
| 104 | } |
---|