X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/ecd50cc5f6c2a427e821483993678b78c94ffdfd..361f9cbca968760822f53a80b407f24ce1713ccd:/sbr/fmt_compile.c diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index 3930f8ca..97dc1fcc 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -35,17 +35,19 @@ * * - Add code to fmttest.c to display your new function. * - * - Document the new function in the mh-format(5) man page. + * - Document the new function in mh-format(5). * */ -#include -#include -#include -#include -#include -#include -#include +#include "h/mh.h" +#include "getcpy.h" +#include "error.h" +#include "h/addrsbr.h" +#include "h/tws.h" +#include "h/fmt_scan.h" +#include "h/fmt_compile.h" +#include "h/mts.h" +#include "h/utils.h" #include "terminal.h" #ifdef HAVE_SYS_TIME_H @@ -69,13 +71,13 @@ static int infunction; /* function nesting cnt */ extern struct mailname fmt_mnull; /* ftable->type (argument type) */ -#define TF_COMP 0 /* component expected */ -#define TF_NUM 1 /* number expected */ -#define TF_STR 2 /* string expected */ -#define TF_EXPR 3 /* component or func. expected */ -#define TF_NONE 4 /* no argument */ -#define TF_MYBOX 5 /* special - get current user's mbox */ -#define TF_NOW 6 /* special - get current unix time */ +#define TF_COMP 0 /* component expected */ +#define TF_NUM 1 /* number expected */ +#define TF_STR 2 /* string expected */ +#define TF_EXPR 3 /* component or func. expected */ +#define TF_NONE 4 /* no argument */ +#define TF_MYBOX 5 /* special - get current user's mbox */ +#define TF_NOW 6 /* special - get current unix time */ #define TF_EXPR_SV 7 /* like expr but save current str reg */ #define TF_NOP 8 /* like expr but no result */ #define TF_MYNAME 9 /* special - get current name of user */ @@ -126,7 +128,7 @@ extern struct mailname fmt_mnull; struct ftable { char *name; /* function name */ char type; /* argument type */ - char f_type; /* fmt type */ + char f_type; /* fmt type */ char extra; /* arg. type dependent extra info */ char flags; }; @@ -158,8 +160,10 @@ static struct ftable functable[] = { { "decodecomp", TF_COMP, FT_LS_DECODECOMP, 0, TFL_PUTS }, { "decode", TF_EXPR, FT_LS_DECODE, 0, TFL_PUTS }, { "trim", TF_EXPR, FT_LS_TRIM, 0, 0 }, + { "trimr", TF_EXPR, FT_LS_TRIM, 0, TFL_PUTS }, { "kilo", TF_EXPR, FT_LS_KILO, 0, TFL_PUTS }, { "kibi", TF_EXPR, FT_LS_KIBI, 0, TFL_PUTS }, + { "ordinal", TF_EXPR, FT_LS_ORDINAL, 0, TFL_PUTS }, { "compval", TF_COMP, FT_LV_COMP, 0, TFL_PUTN }, { "compflag", TF_COMP, FT_LV_COMPFLAG, 0, TFL_PUTN }, { "num", TF_NUM, FT_LV_LIT, 0, TFL_PUTN }, @@ -226,7 +230,7 @@ static struct ftable functable[] = { { "getmymbox", TF_COMP, FT_STR, FT_GETMYMBOX, 0 }, { "getmyaddr", TF_COMP, FT_LS_ADDR, FT_GETMYADDR, TFL_PUTS }, - { "unquote", TF_EXPR, FT_LS_UNQUOTE, 0, TFL_PUTS }, + { "unquote", TF_EXPR, FT_LS_UNQUOTE, 0, TFL_PUTS }, { "bold", TF_BOLD, FT_LS_LIT, 0, TFL_PUTS }, { "underline", TF_UNDERLN,FT_LS_LIT, 0, TFL_PUTS }, @@ -280,11 +284,12 @@ static struct colormap colortable[] = { /* * Find a component in the hash table. */ -#define FINDCOMP(comp,name) \ - for (comp = wantcomp[CHASH(name)]; \ - comp && strcmp(comp->c_name,name); \ - comp = comp->c_next) \ - ; +#define FINDCOMP(comp,name) do { \ + for (comp = wantcomp[CHASH(name)]; \ + comp && strcmp(comp->c_name,name); \ + comp = comp->c_next) \ + ; \ + } while (0) /* Add new component to the hash table */ #define NEWCOMP(cm,name) do { \ @@ -351,7 +356,7 @@ lookup(char *name) while ((nm = t->name)) { if (*nm == c && strcmp (nm, name) == 0) - return (ftbl = t); + return ftbl = t; t++; } @@ -375,7 +380,7 @@ compile_error(char *str, char *cp) inform("\"%s\": format compile error - %s", &usr_fstring[errpos-errctx], str); - adios (NULL, "%*s", errctx+1, "^"); + die("%*s", errctx+1, "^"); } /* @@ -389,14 +394,14 @@ fmt_compile(char *fstring, struct format **fmt, int reset_comptable) { char *cp; size_t i; - static int comptable_initialized = 0; + static bool comptable_initialized; format_string = mh_xstrdup(fstring); usr_fstring = fstring; if (reset_comptable || !comptable_initialized) { - free_comptable(); - comptable_initialized = 1; + free_comptable(); + comptable_initialized = true; } /* it takes at least 4 char to generate one format so we @@ -418,7 +423,7 @@ fmt_compile(char *fstring, struct format **fmt, int reset_comptable) *fmt = formatvec; free(format_string); - return (ncomp); + return ncomp; } static char * @@ -443,7 +448,7 @@ compile (char *sp) break; } if (c == 0) - return (cp); + return cp; switch (c = *++cp) { case '%': @@ -455,7 +460,7 @@ compile (char *sp) case '>': case '?': case ']': - return (cp); + return cp; case '<': cp = do_if(++cp); @@ -487,15 +492,13 @@ do_spec(char *sp) { char *cp = sp; int c; -#ifndef lint - int ljust = 0; -#endif /* not lint */ + bool ljust = false; int wid = 0; char fill = ' '; c = *cp++; if (c == '-') { - ljust++; + ljust = true; c = *cp++; } if (c == '0') { @@ -530,7 +533,7 @@ do_spec(char *sp) fp->f_width = wid; fp->f_fill = fill; - return (cp); + return cp; } /* @@ -544,7 +547,7 @@ do_name(char *sp, int preprocess) char *cp = sp; int c; int i; - static int primed = 0; + static bool primed; while (isalnum(c = *cp++) || c == '-' || c == '_') ; @@ -574,7 +577,7 @@ do_name(char *sp, int preprocess) case FT_GETMYADDR: if (!primed) { ismymbox(NULL); - primed++; + primed = true; } /* FALLTHRU */ case FT_PARSEADDR: @@ -594,7 +597,7 @@ do_name(char *sp, int preprocess) cm->c_type |= CT_ADDR; break; } - return (cp); + return cp; } /* @@ -661,45 +664,45 @@ do_func(char *sp) break; case TF_MYBOX: - LS(t->f_type, getusername()); + LS(t->f_type, getusername(1)); break; case TF_MYNAME: - LS(t->f_type, getfullname()); + LS(t->f_type, getfullname()); break; case TF_MYHOST: - LS(t->f_type, LocalName(0)); + LS(t->f_type, LocalName(0)); break; case TF_LMBOX: - LS(t->f_type, getlocalmbox()); + LS(t->f_type, getlocalmbox()); break; case TF_BOLD: - LS(t->f_type, get_term_stringcap("bold")); + LS(t->f_type, get_term_stringcap("bold")); break; case TF_UNDERLN: - LS(t->f_type, get_term_stringcap("smul")); + LS(t->f_type, get_term_stringcap("smul")); break; case TF_STNDOUT: - LS(t->f_type, get_term_stringcap("smso")); + LS(t->f_type, get_term_stringcap("smso")); break; case TF_RESET: - LS(t->f_type, get_term_stringcap("sgr0")); + LS(t->f_type, get_term_stringcap("sgr0")); break; case TF_HASCLR: - LV(t->f_type, get_term_numcap("colors") > 1); + LV(t->f_type, get_term_numcap("colors") > 1); break; case TF_FGCOLR: case TF_BGCOLR: { struct colormap *cmap = colortable; - char *code; + char *code; sp = cp - 1; while (c && c != ')') @@ -708,7 +711,7 @@ do_func(char *sp) while (cmap->colorname != NULL) { if (strcasecmp(sp, cmap->colorname) == 0) - break; + break; cmap++; } @@ -726,7 +729,7 @@ do_func(char *sp) if (! code) code = get_term_stringparm(t->type == TF_FGCOLR ? "setf" : "setb", - cmap->nonansinum, 0); + cmap->nonansinum, 0); LS(t->f_type, code); break; @@ -758,7 +761,7 @@ do_func(char *sp) CERROR("')' expected"); } --infunction; - return (cp); + return cp; } /* @@ -777,13 +780,13 @@ do_expr (char *sp, int preprocess) } else if (c == '(') { cp = do_func (cp); } else if (c == ')') { - return (--cp); + return --cp; } else if (c == '%' && *cp == '<') { cp = do_if (cp+1); } else { CERROR ("'(', '{', '%<' or ')' expected"); } - return (cp); + return cp; } /* @@ -855,7 +858,7 @@ do_if(char *sp) { char *cp = sp; struct format *fexpr, - *fif = (struct format *)NULL; + *fif = NULL; int c = '<'; for (;;) { @@ -896,7 +899,7 @@ do_if(char *sp) fif = fp; /* loc of GOTO */ fexpr->f_skip = next_fp - fexpr; - fexpr = (struct format *)NULL;/* no extra ENDIF */ + fexpr = NULL; /* no extra ENDIF */ cp = compile (cp); /* compile ELSE stmts */ fif->f_skip = next_fp - fif; @@ -920,7 +923,7 @@ do_if(char *sp) if (fexpr) /* IF ... [ELSIF ...] ENDIF */ fexpr->f_skip = next_fp - fexpr; - return (cp); + return cp; } /* @@ -942,18 +945,18 @@ fmt_free(struct format *fmt, int reset_comptable) struct format *fp = fmt; if (fp) { - while (! (fp->f_type == FT_DONE && fp->f_value == 0)) { + while (! (fp->f_type == FT_DONE && fp->f_value == 0)) { if (fp->f_flags & FF_STRALLOC) - free(fp->f_text); + free(fp->f_text); if (fp->f_flags & FF_COMPREF) - free_component(fp->f_comp); + free_component(fp->f_comp); fp++; } free(fmt); } if (reset_comptable) - free_comptable(); + free_comptable(); } /* @@ -968,7 +971,7 @@ fmt_freecomptext(void) for (i = 0; i < DIM(wantcomp); i++) for (cm = wantcomp[i]; cm; cm = cm->c_next) { - mh_xfree(cm->c_text); + free(cm->c_text); cm->c_text = NULL; } } @@ -1020,7 +1023,7 @@ fmt_addcompentry(char *component) FINDCOMP(cm, component); if (cm) - return 0; + return 0; NEWCOMP(cm, component); @@ -1045,13 +1048,15 @@ fmt_addcompentry(char *component) int fmt_addcomptext(char *component, char *text) { - int i, found = 0, bucket = CHASH(component); + int i; + bool found = false; + int bucket = CHASH(component); struct comp *cptr = wantcomp[bucket]; char *cp; while (cptr) { if (strcasecmp(component, FENDNULL(cptr->c_name)) == 0) { - found++; + found = true; if (! cptr->c_text) { cptr->c_text = getcpy(text); } else { @@ -1084,9 +1089,9 @@ fmt_appendcomp(int bucket, char *component, char *text) struct comp *cptr; if (bucket != -1) { - for (cptr = wantcomp[bucket]; cptr; cptr = cptr->c_next) + for (cptr = wantcomp[bucket]; cptr; cptr = cptr->c_next) if (strcasecmp(component, FENDNULL(cptr->c_name)) == 0) - cptr->c_text = add(text, cptr->c_text); + cptr->c_text = add(text, cptr->c_text); } } @@ -1120,7 +1125,7 @@ free_comptable(void) struct comp *cm, *cm2; for (i = 0; i < DIM(wantcomp); i++) { - cm = wantcomp[i]; + cm = wantcomp[i]; while (cm != NULL) { cm2 = cm->c_next; free_component(cm); @@ -1141,13 +1146,13 @@ static void free_component(struct comp *cm) { if (--cm->c_refcount <= 0) { - /* Shouldn't ever be NULL, but just in case ... */ - mh_xfree(cm->c_name); - mh_xfree(cm->c_text); + /* Shouldn't ever be NULL, but just in case ... */ + free(cm->c_name); + free(cm->c_text); if (cm->c_type & CT_DATE) free(cm->c_tws); if (cm->c_type & CT_ADDR && cm->c_mn && cm->c_mn != &fmt_mnull) mnfree(cm->c_mn); - free(cm); + free(cm); } }