- Timestamp:
- Apr 29, 2008, 1:21:13 AM (17 years ago)
- Branches:
- master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- b67ab6b
- Parents:
- 120291c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
style.c
r5a9f6fe r864ed35 1 #define OWL_PERL 1 2 #include "owl.h" 2 3 3 4 static const char fileIdent[] = "$Id$"; 4 5 5 void owl_style_create_ internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description)6 void owl_style_create_perl(owl_style *s, char *name, SV *obj) 6 7 { 7 s->type=OWL_STYLE_TYPE_INTERNAL;8 8 s->name=owl_strdup(name); 9 if (description) { 10 s->description=owl_strdup(description); 11 } else { 12 s->description=owl_sprintf("Owl internal style %s", name); 13 } 14 s->perlfuncname=NULL; 15 s->formatfunc=formatfunc; 16 } 17 18 void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description) 19 { 20 s->type=OWL_STYLE_TYPE_PERL; 21 s->name=owl_strdup(name); 22 s->perlfuncname=owl_strdup(perlfuncname); 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 } 29 s->formatfunc=NULL; 9 s->perlobj = SvREFCNT_inc(obj); 30 10 } 31 11 … … 43 23 char *owl_style_get_description(owl_style *s) 44 24 { 45 return(s->description); 25 SV *sv = NULL; 26 OWL_PERL_CALL_METHOD(s->perlobj, 27 "description", 28 /* no args */, 29 "Error in style_get_description: %s", 30 0, 31 sv = SvREFCNT_inc(POPs); 32 ); 33 if(sv) { 34 return SvPV_nolen(sv_2mortal(sv)); 35 } else { 36 return "[error getting description]"; 37 } 46 38 } 47 39 … … 51 43 void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m) 52 44 { 53 if (s->type==OWL_STYLE_TYPE_INTERNAL) { 54 (* s->formatfunc)(fm, m); 55 } else if (s->type==OWL_STYLE_TYPE_PERL) { 56 char *body, *indent; 57 int curlen; 45 char *body, *indent; 46 int curlen; 58 47 59 /* run the perl function */ 60 body=owl_perlconfig_getmsg(m, 1, s->perlfuncname); 61 if (!strcmp(body, "")) { 62 owl_free(body); 63 body=owl_strdup("<unformatted message>"); 64 } 65 66 /* indent and ensure ends with a newline */ 67 indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10); 68 owl_text_indent(indent, body, OWL_TAB); 69 curlen = strlen(indent); 70 if (curlen==0 || indent[curlen-1] != '\n') { 71 indent[curlen] = '\n'; 72 indent[curlen+1] = '\0'; 73 } 48 SV *sv = NULL; 49 50 /* Call the perl object */ 51 OWL_PERL_CALL_METHOD(s->perlobj, 52 "format_message", 53 XPUSHs(owl_perlconfig_message2hashref(m));, 54 "Error in format_message: %s", 55 0, 56 sv = SvREFCNT_inc(POPs); 57 ); 74 58 75 /* fmtext_append. This needs to change */ 76 owl_fmtext_append_ztext(fm, indent); 77 78 owl_free(indent); 79 owl_free(body); 59 if(sv) { 60 body = SvPV_nolen(sv); 61 } else { 62 body = "<unformatted message>"; 80 63 } 64 65 /* indent and ensure ends with a newline */ 66 indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10); 67 owl_text_indent(indent, body, OWL_TAB); 68 curlen = strlen(indent); 69 if (curlen==0 || indent[curlen-1] != '\n') { 70 indent[curlen] = '\n'; 71 indent[curlen+1] = '\0'; 72 } 73 74 /* fmtext_append. This needs to change */ 75 owl_fmtext_append_ztext(fm, indent); 76 77 owl_free(indent); 78 if(sv) 79 SvREFCNT_dec(body); 81 80 } 82 81 83 82 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 { 83 if (!s || !s->perlobj || !SvOK(s->perlobj)) { 93 84 return -1; 94 85 } 86 return 0; 95 87 } 96 88 … … 98 90 { 99 91 if (s->name) owl_free(s->name); 100 if (s->description) owl_free(s->description); 101 if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) { 102 owl_free(s->perlfuncname); 103 } 92 SvREFCNT_dec(s->perlobj); 104 93 }
Note: See TracChangeset
for help on using the changeset viewer.