Changeset e3d9c77


Ignore:
Timestamp:
Oct 26, 2003, 3:23:38 PM (17 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
bc08664
Parents:
70b53ec
Message:
Write owl_text_quote
Moved some functions between util.c, text.c and zephyr.c
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • filter.c

    r37eab7f re3d9c77  
    7070          !strcasecmp(argv[i], "hostname") ||
    7171          !strcasecmp(argv[i], "login")) {
    72         regexstr=owl_util_substitute(argv[i+1], "%me%", owl_zephyr_get_sender());
     72        regexstr=owl_text_substitute(argv[i+1], "%me%", owl_zephyr_get_sender());
    7373        owl_filterelement_create_re(fe, argv[i], regexstr);
    7474        owl_free(regexstr);
  • functions.c

    reec69e1 re3d9c77  
    19551955          if (owl_global_is_smartstrip(&g)) {
    19561956            tmp2=tmp;
    1957             tmp=owl_util_smartstripped_user(tmp2);
     1957            tmp=owl_zephyr_smartstripped_user(tmp2);
    19581958            owl_free(tmp2);
    19591959          }
     
    23952395  downstr(filtname);
    23962396  /* turn spaces into hyphens */
    2397   owl_util_tr(filtname, ' ', '.');
     2397  owl_text_tr(filtname, ' ', '.');
    23982398 
    23992399  /* if it already exists then go with it.  This lets users override */
     
    24052405  argbuff=owl_malloc(len+20);
    24062406  tmpclass=owl_strdup(class);
    2407   owl_util_tr(tmpclass, ' ', '.');
     2407  owl_text_tr(tmpclass, ' ', '.');
    24082408  if (instance) {
    24092409    tmpinstance=owl_strdup(instance);
    2410     owl_util_tr(tmpinstance, ' ', '.');
     2410    owl_text_tr(tmpinstance, ' ', '.');
    24112411  }
    24122412  sprintf(argbuff, "( class ^%s$ )", tmpclass);
  • logging.c

    reec69e1 re3d9c77  
    2323
    2424  /* expand ~ in path names */
    25   logpath = owl_util_substitute(owl_global_get_logpath(&g), "~",
     25  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~",
    2626                                owl_global_get_homedir(&g));
    2727
     
    6464
    6565  /* expand ~ in path names */
    66   logpath = owl_util_substitute(owl_global_get_logpath(&g), "~",
     66  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~",
    6767                                owl_global_get_homedir(&g));
    6868
     
    105105
    106106  /* expand ~ in path names */
    107   logpath = owl_util_substitute(owl_global_get_logpath(&g), "~",
     107  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~",
    108108                                owl_global_get_homedir(&g));
    109109
     
    210210  /* create the filename (expanding ~ in path names) */
    211211  if (personal) {
    212     logpath = owl_util_substitute(owl_global_get_logpath(&g), "~",
     212    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~",
    213213                                  owl_global_get_homedir(&g));
    214214    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
     
    216216
    217217  } else {
    218     logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~",
     218    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~",
    219219                                owl_global_get_homedir(&g));
    220220
  • stylefunc.c

    r25dd31a re3d9c77  
    528528     
    529529      tmp=owl_strdup(owl_message_get_body(m));
    530       owl_util_tr(tmp, '\n', ' ');
     530      owl_text_tr(tmp, '\n', ' ');
    531531      owl_fmtext_append_ztext(fm, tmp);
    532532      owl_fmtext_append_normal(fm, "\n");
     
    568568     
    569569      tmp=owl_strdup(owl_message_get_body(m));
    570       owl_util_tr(tmp, '\n', ' ');
     570      owl_text_tr(tmp, '\n', ' ');
    571571      owl_fmtext_append_normal(fm, tmp);
    572572      owl_fmtext_append_normal(fm, "\n");
     
    583583   
    584584    tmp=owl_strdup(owl_message_get_body(m));
    585     owl_util_tr(tmp, '\n', ' ');
     585    owl_text_tr(tmp, '\n', ' ');
    586586    owl_fmtext_append_normal(fm, tmp);
    587587    owl_fmtext_append_normal(fm, "\n");
     
    592592   
    593593    tmp=owl_strdup(owl_message_get_body(m));
    594     owl_util_tr(tmp, '\n', ' ');
     594    owl_text_tr(tmp, '\n', ' ');
    595595    owl_fmtext_append_normal(fm, tmp);
    596596    owl_fmtext_append_normal(fm, "\n");
  • text.c

    r8d24696 re3d9c77  
    115115}
    116116
    117 
    118117int owl_text_num_lines(char *in)
    119118{
     
    190189  }
    191190
    192   out2=owl_util_substitute(out, "&lt;", "<");
    193   owl_free(out);
    194   out=owl_util_substitute(out2, "&gt;", ">");
     191  out2=owl_text_substitute(out, "&lt;", "<");
     192  owl_free(out);
     193  out=owl_text_substitute(out2, "&gt;", ">");
    195194  owl_free(out2);
    196   out2=owl_util_substitute(out, "&amp;", "&");
    197   owl_free(out);
    198   out=owl_util_substitute(out2, "&quot;", "\"");
     195  out2=owl_text_substitute(out, "&amp;", "&");
     196  owl_free(out);
     197  out=owl_text_substitute(out2, "&quot;", "\"");
    199198  owl_free(out2);
    200   out2=owl_util_substitute(out, "&nbsp;", " ");
    201   owl_free(out);
    202   out=owl_util_substitute(out2, "&ensp;", "  ");
     199  out2=owl_text_substitute(out, "&nbsp;", " ");
     200  owl_free(out);
     201  out=owl_text_substitute(out2, "&ensp;", "  ");
    203202  owl_free(out2);
    204   out2=owl_util_substitute(out, "&emsp;", "   ");
    205   owl_free(out);
    206   out=owl_util_substitute(out2, "&endash;", "--");
     203  out2=owl_text_substitute(out, "&emsp;", "   ");
     204  owl_free(out);
     205  out=owl_text_substitute(out2, "&endash;", "--");
    207206  owl_free(out2);
    208   out2=owl_util_substitute(out, "&emdash;", "---");
     207  out2=owl_text_substitute(out, "&emdash;", "---");
    209208  owl_free(out);
    210209
     
    257256  return(out);
    258257}
     258
     259/* exactly like strstr but case insensitive */
     260char *stristr(char *a, char *b)
     261{
     262  char *x, *y, *ret;
     263
     264  if ((x=owl_strdup(a))==NULL) return(NULL);
     265  if ((y=owl_strdup(b))==NULL) return(NULL);
     266  downstr(x);
     267  downstr(y);
     268  ret=strstr(x, y);
     269  if (ret==NULL) {
     270    owl_free(x);
     271    owl_free(y);
     272    return(NULL);
     273  }
     274  ret=ret-x+a;
     275  owl_free(x);
     276  owl_free(y);
     277  return(ret);
     278}
     279
     280/* return 1 if a string is only whitespace, otherwise 0 */
     281int only_whitespace(char *s)
     282{
     283  int i;
     284  for (i=0; s[i]; i++) {
     285    if (!isspace((int) s[i])) return(0);
     286  }
     287  return(1);
     288}
     289
     290char *owl_getquoting(char *line)
     291{
     292  if (line[0]=='\0') return("'");
     293  if (strchr(line, '\'')) return("\"");
     294  if (strchr(line, '"')) return("'");
     295  if (strchr(line, ' ')) return("'");
     296  return("");
     297}
     298
     299/* Return a string with any occurances of 'from' replaced with 'to'.
     300 * Does not currently handle backslash quoting, but may in the future.
     301 * Caller must free returned string.
     302 */
     303char *owl_text_substitute(char *in, char *from, char *to)
     304{
     305 
     306  char *out;
     307  int   outlen, tolen, fromlen, inpos=0, outpos=0;
     308
     309  if (!*from) return owl_strdup(in);
     310
     311  outlen = strlen(in)+1;
     312  tolen  = strlen(to);
     313  fromlen  = strlen(from);
     314  out = malloc(outlen);
     315
     316  while (in[inpos]) {
     317    if (!strncmp(in+inpos, from, fromlen)) {
     318      outlen += tolen;
     319      out = owl_realloc(out, outlen);
     320      strcpy(out+outpos, to);
     321      inpos += fromlen;
     322      outpos += tolen;
     323    } else {
     324      out[outpos] = in[inpos];
     325      inpos++; outpos++;
     326    }
     327  }
     328  out[outpos] = '\0';
     329  return(out);
     330}
     331
     332/* replace all instances of character a in buff with the character
     333 * b.  buff must be null terminated.
     334 */
     335void owl_text_tr(char *buff, char a, char b)
     336{
     337  int i;
     338
     339  owl_function_debugmsg("In: %s", buff);
     340  for (i=0; buff[i]!='\0'; i++) {
     341    if (buff[i]==a) buff[i]=b;
     342  }
     343  owl_function_debugmsg("Out: %s", buff);
     344}
     345
     346/* Return a string which is like 'in' except that every instance of
     347 * any character in 'toquote' found in 'in' is preceeded by the string
     348 * 'quotestr'.  For example, owl_text_quote(in, "+*.", "\") would
     349 * place a backslash before every '+', '*' or '.' in 'in'.  It is
     350 * permissable for a character in 'quotestr' to be in 'toquote'.
     351 * On success returns the string, on error returns NULL.
     352 */
     353char *owl_text_quote(char *in, char *toquote, char *quotestr)
     354{
     355  int i, x, r, place;
     356  int in_len, toquote_len, quotestr_len;
     357  char *out;
     358
     359  in_len=strlen(in);
     360  toquote_len=strlen(toquote);
     361  quotestr_len=strlen(quotestr);
     362  out=owl_malloc((in_len*quotestr_len)+30);
     363  place=0;
     364  for (i=0; i<in_len; i++) {
     365
     366    /* check if it's a character that needs quoting */
     367    for (x=0; x<toquote_len; x++) {
     368      if (in[i]==toquote[x]) {
     369        /* quote it */
     370        for (r=0; r<quotestr_len; r++) {
     371          out[place+r]=quotestr[r];
     372        }
     373        place+=quotestr_len;
     374        break;
     375      }
     376    }
     377
     378    /* either way, we now copy over the character */
     379    out[place]=in[i];
     380    place++;
     381  }
     382  out[place]='\0';
     383  return(out);
     384}
  • util.c

    rc15bbfb re3d9c77  
    22#include <stdlib.h>
    33#include <string.h>
    4 #include <unistd.h>
    54#include <unistd.h>
    65#include <ctype.h>
     
    306305}
    307306
    308 
    309307/* return the index of the last char before a change from the first one */
    310308int owl_util_find_trans(char *in, int len)
     
    317315}
    318316
    319 
    320317/* downcase the string 'foo' */
    321318void downstr(char *foo)
     
    325322    foo[i]=tolower(foo[i]);
    326323  }
    327 }
    328 
    329 /* exactly like strstr but case insensitive */
    330 char *stristr(char *a, char *b)
    331 {
    332   char *x, *y, *ret;
    333 
    334   if ((x=owl_strdup(a))==NULL) return(NULL);
    335   if ((y=owl_strdup(b))==NULL) return(NULL);
    336   downstr(x);
    337   downstr(y);
    338   ret=strstr(x, y);
    339   if (ret==NULL) {
    340     owl_free(x);
    341     owl_free(y);
    342     return(NULL);
    343   }
    344   ret=ret-x+a;
    345   owl_free(x);
    346   owl_free(y);
    347   return(ret);
    348324}
    349325
     
    382358}
    383359
    384 /* return 1 if a string is only whitespace, otherwise 0 */
    385 int only_whitespace(char *s)
    386 {
    387   int i;
    388   for (i=0; s[i]; i++) {
    389     if (!isspace((int) s[i])) return(0);
    390   }
    391   return(1);
    392 }
    393 
    394360/* hooks for doing memory allocation et. al. in owl */
    395361
     
    413379  return(realloc(ptr, size));
    414380}
    415 
    416381
    417382/* allocates memory and returns the string or null.
     
    443408}
    444409
    445 /* Strip a local realm fron the zephyr user name.
    446  * The caller must free the return
    447  */
    448 char *short_zuser(char *in)
    449 {
    450   char *out, *ptr;
    451 
    452   out=owl_strdup(in);
    453   ptr=strchr(out, '@');
    454   if (ptr) {
    455     if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
    456       *ptr='\0';
    457     }
    458   }
    459   return(out);
    460 }
    461 
    462 /* Append a local realm to the zephyr user name if necessary.
    463  * The caller must free the return.
    464  */
    465 char *long_zuser(char *in)
    466 {
    467   char *ptr;
    468 
    469   if (NULL != (ptr=strchr(in, '@'))) {
    470     return owl_strdup(in);
    471   } else {
    472     return owl_sprintf("%s@%s", in, owl_zephyr_get_realm());
    473   }
    474 }
    475 
    476 
    477 /* strip out the instance from a zsender's principal.  Preserves the
    478  * realm if present.  daemon.webzephyr is a special case.  The
    479  * caller must free the return
    480  */
    481 char *owl_util_smartstripped_user(char *in)
    482 {
    483   char *ptr, *realm, *out;
    484 
    485   out=owl_strdup(in);
    486 
    487   /* bail immeaditly if we don't have to do any work */
    488   ptr=strchr(in, '.');
    489   if (!strchr(in, '/') && !ptr) {
    490     /* no '/' and no '.' */
    491     return(out);
    492   }
    493   if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
    494     /* There's a '.' but it's in the realm */
    495     return(out);
    496   }
    497   if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
    498     return(out);
    499   }
    500 
    501   /* remove the realm from ptr, but hold on to it */
    502   realm=strchr(out, '@');
    503   if (realm) realm[0]='\0';
    504 
    505   /* strip */
    506   ptr=strchr(out, '.');
    507   if (!ptr) ptr=strchr(out, '/');
    508   ptr[0]='\0';
    509 
    510   /* reattach the realm if we had one */
    511   if (realm) {
    512     strcat(out, "@");
    513     strcat(out, realm+1);
    514   }
    515 
    516   return(out);
    517 }
    518 
    519 char *owl_getquoting(char *line)
    520 {
    521   if (line[0]=='\0') return("'");
    522   if (strchr(line, '\'')) return("\"");
    523   if (strchr(line, '"')) return("'");
    524   if (strchr(line, ' ')) return("'");
    525   return("");
    526 }
    527 
    528 
    529 
    530 /* Return a string with any occurances of 'from' replaced with 'to'.
    531  * Does not currently handle backslash quoting, but may in the future.
    532  * Caller must free returned string.
    533  */
    534 char *owl_util_substitute(char *in, char *from, char *to)
    535 {
    536  
    537   char *out;
    538   int   outlen, tolen, fromlen, inpos=0, outpos=0;
    539 
    540   if (!*from) return owl_strdup(in);
    541 
    542   outlen = strlen(in)+1;
    543   tolen  = strlen(to);
    544   fromlen  = strlen(from);
    545   out = malloc(outlen);
    546 
    547   while (in[inpos]) {
    548     if (!strncmp(in+inpos, from, fromlen)) {
    549       outlen += tolen;
    550       out = owl_realloc(out, outlen);
    551       strcpy(out+outpos, to);
    552       inpos += fromlen;
    553       outpos += tolen;
    554     } else {
    555       out[outpos] = in[inpos];
    556       inpos++; outpos++;
    557     }
    558   }
    559   out[outpos] = '\0';
    560   return(out);
    561 }
    562 
    563 /* replace all instances of character a in buff with the character
    564  * b.  buff must be null terminated.
    565  */
    566 void owl_util_tr(char *buff, char a, char b)
    567 {
    568   int i;
    569 
    570   owl_function_debugmsg("In: %s", buff);
    571   for (i=0; buff[i]!='\0'; i++) {
    572     if (buff[i]==a) buff[i]=b;
    573   }
    574   owl_function_debugmsg("Out: %s", buff);
    575 }
    576 
    577 
    578410/* Return the owl color associated with the named color */
    579411int owl_util_string_to_color(char *color)
     
    805637
    806638  FAIL_UNLESS("owl_util_substitute 1",
    807               !strcmp("foo", owl_util_substitute("foo", "", "Y")));
    808   FAIL_UNLESS("owl_util_substitute 2",
    809               !strcmp("fYZYZ", owl_util_substitute("foo", "o", "YZ")));
    810   FAIL_UNLESS("owl_util_substitute 3",
    811               !strcmp("foo", owl_util_substitute("fYZYZ", "YZ", "o")));
    812   FAIL_UNLESS("owl_util_substitute 4",
    813               !strcmp("/u/foo/meep", owl_util_substitute("~/meep", "~", "/u/foo")));
     639              !strcmp("foo", owl_text_substitute("foo", "", "Y")));
     640  FAIL_UNLESS("owl_text_substitute 2",
     641              !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
     642  FAIL_UNLESS("owl_text_substitute 3",
     643              !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
     644  FAIL_UNLESS("owl_text_substitute 4",
     645              !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
    814646
    815647  FAIL_UNLESS("skiptokens 1",
  • zephyr.c

    r269ed34 re3d9c77  
    485485
    486486  if (owl_global_is_smartstrip(&g)) {
    487     to=owl_util_smartstripped_user(owl_message_get_sender(m));
     487    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
    488488  } else {
    489489    to=owl_strdup(owl_message_get_sender(m));
     
    794794}
    795795 
     796
     797
     798/* Strip a local realm fron the zephyr user name.
     799 * The caller must free the return
     800 */
     801char *short_zuser(char *in)
     802{
     803  char *out, *ptr;
     804
     805  out=owl_strdup(in);
     806  ptr=strchr(out, '@');
     807  if (ptr) {
     808    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
     809      *ptr='\0';
     810    }
     811  }
     812  return(out);
     813}
     814
     815/* Append a local realm to the zephyr user name if necessary.
     816 * The caller must free the return.
     817 */
     818char *long_zuser(char *in)
     819{
     820  char *ptr;
     821
     822  if (NULL != (ptr=strchr(in, '@'))) {
     823    return owl_strdup(in);
     824  } else {
     825    return owl_sprintf("%s@%s", in, owl_zephyr_get_realm());
     826  }
     827}
     828
     829
     830/* strip out the instance from a zsender's principal.  Preserves the
     831 * realm if present.  daemon.webzephyr is a special case.  The
     832 * caller must free the return
     833 */
     834char *owl_zephyr_smartstripped_user(char *in)
     835{
     836  char *ptr, *realm, *out;
     837
     838  out=owl_strdup(in);
     839
     840  /* bail immeaditly if we don't have to do any work */
     841  ptr=strchr(in, '.');
     842  if (!strchr(in, '/') && !ptr) {
     843    /* no '/' and no '.' */
     844    return(out);
     845  }
     846  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
     847    /* There's a '.' but it's in the realm */
     848    return(out);
     849  }
     850  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
     851    return(out);
     852  }
     853
     854  /* remove the realm from ptr, but hold on to it */
     855  realm=strchr(out, '@');
     856  if (realm) realm[0]='\0';
     857
     858  /* strip */
     859  ptr=strchr(out, '.');
     860  if (!ptr) ptr=strchr(out, '/');
     861  ptr[0]='\0';
     862
     863  /* reattach the realm if we had one */
     864  if (realm) {
     865    strcat(out, "@");
     866    strcat(out, realm+1);
     867  }
     868
     869  return(out);
     870}
Note: See TracChangeset for help on using the changeset viewer.