Changeset 01dcae5


Ignore:
Timestamp:
Mar 29, 2009, 2:43:40 PM (15 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
owl
Children:
395b32e
Parents:
3674002
Message:
sprintf fixes for owl_filter_print, owl_function_printallvars, owl_function_show_filter
        owl_function_show_zpunts, owl_function_zpunt
adopt ^(un)*%s(\\.d) style regex for punting
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • filter.c

    rfa00c5c r01dcae5  
    476476}
    477477
    478 void owl_filter_print(owl_filter *f, char *out)
     478char *owl_filter_print(owl_filter *f)
    479479{
    480480  int i, j;
    481481  owl_filterelement *fe;
    482   char *tmp;
    483 
    484   strcpy(out, owl_filter_get_name(f));
    485   strcat(out, ": ");
     482  GString *out = g_string_new("");
    486483
    487484  if (f->color!=OWL_COLOR_DEFAULT) {
    488     strcat(out, "-c ");
    489     strcat(out, owl_util_color_to_string(f->color));
    490     strcat(out, " ");
     485    g_string_append(out, "-c ");
     486    if (f->color < 8) {
     487      g_string_append(out, owl_util_color_to_string(f->color));
     488    } else {
     489      g_string_append_printf(out, "%i",f->color);
     490    }
     491    g_string_append(out, " ");
    491492  }
    492493
     
    494495  for (i=0; i<j; i++) {
    495496    fe=owl_list_get_element(&(f->fes), i);
    496     tmp=owl_filterelement_to_string(fe);
    497     strcat(out, tmp);
    498     owl_free(tmp);
    499   }
    500   strcat(out, "\n");
     497    g_string_append(out, owl_filterelement_to_string(fe));
     498  }
     499
     500  return g_string_free(out, 0);
    501501}
    502502
     
    504504int owl_filter_equiv(owl_filter *a, owl_filter *b)
    505505{
    506   char buff[LINE], buff2[LINE];
    507 
    508   owl_filter_print(a, buff);
    509   owl_filter_print(b, buff2);
    510 
    511   if (!strcmp(buff, buff2)) return(1);
    512   return(0);
     506  char *buffa, *buffb;
     507  int ret;
     508
     509  buffa = owl_filter_print(a);
     510  buffb = owl_filter_print(b);
     511
     512  ret = !strcmp(buffa, buffb);
     513  ret = ret && !strcmp(owl_filter_get_name(a),
     514                       owl_filter_get_name(b));
     515
     516  owl_free(buffa);
     517  owl_free(buffb);
     518
     519  return ret;
    513520}
    514521
  • functions.c

    r73d8a88 r01dcae5  
    17111711}
    17121712
    1713 #define PABUFLEN 5000
    17141713void owl_function_printallvars()
    17151714{
    1716   char buff[PABUFLEN], *pos, *name;
     1715  char *name;
     1716  char var[LINE];
    17171717  owl_list varnames;
    1718   int i, numvarnames, rem;
    1719 
    1720   pos = buff;
    1721   pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
    1722   pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
     1718  int i, numvarnames;
     1719  GString *str   = g_string_new("");
     1720
     1721  g_string_append_printf(str, "%-20s = %s\n", "VARIABLE", "VALUE");
     1722  g_string_append_printf(str, "%-20s   %s\n",  "--------", "-----");
    17231723  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
    1724   rem = (buff+PABUFLEN)-pos-1;
    17251724  numvarnames = owl_list_get_size(&varnames);
    17261725  for (i=0; i<numvarnames; i++) {
    17271726    name = owl_list_get_element(&varnames, i);
    17281727    if (name && name[0]!='_') {
    1729       rem = (buff+PABUFLEN)-pos-1;   
    1730       pos += snprintf(pos, rem, "\n%-20s = ", name);
    1731       rem = (buff+PABUFLEN)-pos-1;   
    1732       owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
    1733       pos = buff+strlen(buff);
    1734     }
    1735   }
    1736   rem = (buff+PABUFLEN)-pos-1;   
    1737   snprintf(pos, rem, "\n");
     1728      g_string_append_printf(str, "\n%-20s = ", name);
     1729      owl_variable_get_tostring(owl_global_get_vardict(&g), name, var, LINE);
     1730      g_string_append(str, var);
     1731    }
     1732  }
     1733  g_string_append(str, "\n");
    17381734  owl_variable_dict_namelist_free(&varnames);
    1739  
    1740   owl_function_popless_text(buff);
     1735
     1736  owl_function_popless_text(str->str);
     1737  g_string_free(str, TRUE);
    17411738}
    17421739
     
    24452442{
    24462443  owl_filter *f;
    2447   char buff[5000];
     2444  char *buff, *tmp;
    24482445
    24492446  f=owl_global_get_filter(&g, name);
     
    24522449    return;
    24532450  }
    2454   owl_filter_print(f, buff);
     2451  tmp = owl_filter_print(f);
     2452  buff = owl_sprintf("%s: %s", owl_filter_get_name(f), tmp);
    24552453  owl_function_popless_text(buff);
     2454  owl_free(buff);
     2455  owl_free(tmp);
    24562456}
    24572457
     
    24612461  owl_list *fl;
    24622462  char buff[5000];
     2463  char *tmp;
    24632464  owl_fmtext fm;
    24642465  int i, j;
     
    24722473  for (i=0; i<j; i++) {
    24732474    f=owl_list_get_element(fl, i);
    2474     owl_filter_print(f, buff);
     2475    snprintf(buff, sizeof(buff), "[% 2d] ", i+1);
    24752476    owl_fmtext_append_normal(&fm, buff);
     2477    tmp = owl_filter_print(f);
     2478    owl_fmtext_append_normal(&fm, tmp);
     2479    owl_free(tmp);
     2480    owl_fmtext_append_normal(&fm, "\n");
    24762481  }
    24772482  owl_function_popless_fmtext(&fm);
     
    28942899void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
    28952900{
    2896   owl_filter *f;
    2897   owl_list *fl;
    2898   char *buff;
     2901  char *puntexpr, *classexpr, *instexpr, *recipexpr;
    28992902  char *quoted;
    2900   int ret, i, j;
    2901 
    2902   fl=owl_global_get_puntlist(&g);
    2903 
    2904   /* first, create the filter */
    2905   f=malloc(sizeof(owl_filter));
    2906   buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
    2907   strcpy(buff, "class");
     2903
    29082904  if (!strcmp(class, "*")) {
    2909     strcat(buff, " .*");
     2905    classexpr = owl_sprintf("class .*");
    29102906  } else {
    29112907    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
     
    29132909    owl_text_tr(quoted, '\'', '.');
    29142910    owl_text_tr(quoted, '"', '.');
    2915     sprintf(buff, "%s ^%s$", buff, quoted);
     2911    classexpr = owl_sprintf("class ^(un)*%s(\\.d)*$", quoted);
    29162912    owl_free(quoted);
    29172913  }
    29182914  if (!strcmp(inst, "*")) {
    2919     strcat(buff, " and instance .*");
     2915    instexpr = owl_sprintf(" and instance .*");
    29202916  } else {
    29212917    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
     
    29232919    owl_text_tr(quoted, '\'', '.');
    29242920    owl_text_tr(quoted, '"', '.');
    2925     sprintf(buff, "%s and instance ^%s$", buff, quoted);
     2921    instexpr = owl_sprintf(" and instance ^(un)*%s(\\.d)*$", quoted);
    29262922    owl_free(quoted);
    29272923  }
    2928   if (strcmp(recip, "*")) {
     2924  if (!strcmp(recip, "*")) {
     2925    recipexpr = owl_sprintf("");
     2926  } else {
    29292927    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
    29302928    owl_text_tr(quoted, ' ', '.');
    29312929    owl_text_tr(quoted, '\'', '.');
    29322930    owl_text_tr(quoted, '"', '.');
    2933     sprintf(buff, "%s and recipient ^%s$", buff, quoted);
     2931    recipexpr = owl_sprintf(" and recipient ^%s$", quoted);
    29342932    owl_free(quoted);
    29352933  }
    2936  
    2937   owl_function_debugmsg("About to filter %s", buff);
    2938   ret=owl_filter_init_fromstring(f, "punt-filter", buff);
    2939   owl_free(buff);
     2934
     2935  puntexpr = owl_sprintf("%s %s %s", classexpr, instexpr, recipexpr);
     2936  owl_function_punt(puntexpr, direction);
     2937  owl_free(puntexpr);
     2938  owl_free(classexpr);
     2939  owl_free(instexpr);
     2940  owl_free(recipexpr);
     2941}
     2942
     2943void owl_function_punt(char *filter, int direction)
     2944{
     2945  owl_filter *f;
     2946  owl_list *fl;
     2947  int ret, i, j;
     2948  fl=owl_global_get_puntlist(&g);
     2949
     2950  /* first, create the filter */
     2951  f=malloc(sizeof(owl_filter));
     2952
     2953  owl_function_debugmsg("About to filter %s", filter);
     2954  ret=owl_filter_init_fromstring(f, "punt-filter", filter);
    29402955  if (ret) {
    29412956    owl_function_error("Error creating filter for zpunt");
     
    29482963  for (i=0; i<j; i++) {
    29492964    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
     2965      owl_function_debugmsg("found an equivalent punt filter");
    29502966      /* if we're punting, then just silently bow out on this duplicate */
    29512967      if (direction==0) {
     
    29582974        owl_filter_free(owl_list_get_element(fl, i));
    29592975        owl_list_remove_element(fl, i);
     2976        owl_filter_free(f);
    29602977        return;
    29612978      }
     
    29632980  }
    29642981
     2982  owl_function_debugmsg("punting");
    29652983  /* If we're punting, add the filter to the global punt list */
    29662984  if (direction==0) {
Note: See TracChangeset for help on using the changeset viewer.