Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • style.c

    r864ed35 r5a9f6fe  
    1 #define OWL_PERL
    21#include "owl.h"
    32
    43static const char fileIdent[] = "$Id$";
    54
    6 void owl_style_create_perl(owl_style *s, char *name, SV *obj)
     5void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description)
    76{
     7  s->type=OWL_STYLE_TYPE_INTERNAL;
    88  s->name=owl_strdup(name);
    9   s->perlobj = SvREFCNT_inc(obj);
     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
     18void 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;
    1030}
    1131
     
    2343char *owl_style_get_description(owl_style *s)
    2444{
    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   }
     45  return(s->description);
    3846}
    3947
     
    4351void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m)
    4452{
    45   char *body, *indent;
    46   int curlen;
     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;
    4758
    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                        );
     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    }
    5874
    59   if(sv) {
    60     body = SvPV_nolen(sv);
    61   } else {
    62     body = "<unformatted message>";
     75    /* fmtext_append.  This needs to change */
     76    owl_fmtext_append_ztext(fm, indent);
     77   
     78    owl_free(indent);
     79    owl_free(body);
    6380  }
    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);
    8081}
    8182
    8283int owl_style_validate(owl_style *s) {
    83   if (!s || !s->perlobj || !SvOK(s->perlobj)) {
     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 {
    8493    return -1;
    8594  }
    86   return 0;
    8795}
    8896
     
    9098{
    9199  if (s->name) owl_free(s->name);
    92   SvREFCNT_dec(s->perlobj);
     100  if (s->description) owl_free(s->description);
     101  if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) {
     102    owl_free(s->perlfuncname);
     103  }
    93104}
Note: See TracChangeset for help on using the changeset viewer.