Beginnings of the changes to fmt_compile for memory rework.
[mmh] / sbr / fmt_compile.c
index b4e8bcb..1e8964d 100644 (file)
@@ -35,6 +35,8 @@
  *
  * - Add the code in fmt_scan.c to handle your new function.
  *
+ * - Add code to fmtdump.c to display your new function.
+ *
  * - Document the new function in the mh-format(5) man page.
  *
  */
@@ -44,6 +46,7 @@
 #include <h/tws.h>
 #include <h/fmt_scan.h>
 #include <h/fmt_compile.h>
+#include <h/mts.h>
 
 #ifdef HAVE_SYS_TIME_H
 # include <sys/time.h>
@@ -53,7 +56,7 @@
 /*
  * hash table for deciding if a component is "interesting"
  */
-struct comp *wantcomp[128];
+static struct comp *wantcomp[128];
 
 static struct format *formatvec;       /* array to hold formats */
 static struct format *next_fp;         /* next free format slot */
@@ -75,6 +78,9 @@ extern struct mailname fmt_mnull;
 #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 */
+#define TF_MYHOST  10       /* special - get "local" hostname     */
+#define TF_LMBOX   11       /* special - get full local mailbox   */
 
 /* ftable->flags */
 /* NB that TFL_PUTS is also used to decide whether the test
@@ -132,7 +138,8 @@ static struct ftable functable[] = {
      { "putnum",     TF_EXPR,  FT_NUM,         0,              0 },
      { "putnumf",    TF_EXPR,  FT_NUMF,        0,              0 },
      { "putaddr",    TF_STR,   FT_PUTADDR,     0,              0 },
-     { "putlit",     TF_STR,   FT_STRLIT,      0,              0 },
+     { "putlit",     TF_EXPR,  FT_STRLIT,      0,              0 },
+     { "zputlit",    TF_EXPR,  FT_STRLITZ,     0,              0 },
      { "void",       TF_NOP,   0,              0,              0 },
 
      { "comp",       TF_COMP,  FT_LS_COMP,     0,              TFL_PUTS },
@@ -153,6 +160,9 @@ static struct ftable functable[] = {
      { "dat",        TF_NUM,   FT_LV_DAT,      0,              TFL_PUTN },
      { "strlen",     TF_NONE,  FT_LV_STRLEN,   0,              TFL_PUTN },
      { "me",         TF_MYBOX, FT_LS_LIT,      0,              TFL_PUTS },
+     { "myname",     TF_MYNAME,        FT_LS_LIT,      0,              TFL_PUTS },
+     { "myhost",     TF_MYHOST,        FT_LS_LIT,      0,              TFL_PUTS },
+     { "localmbox",  TF_LMBOX, FT_LS_LIT,      0,              TFL_PUTS },
      { "plus",       TF_NUM,   FT_LV_PLUS_L,   0,              TFL_PUTN },
      { "minus",      TF_NUM,   FT_LV_MINUS_L,  0,              TFL_PUTN },
      { "divide",     TF_NUM,   FT_LV_DIVIDE_L, 0,              TFL_PUTN },
@@ -236,7 +246,7 @@ static struct ftable functable[] = {
 #define LS(type, str)          do { NEW(type,0,0); fp->f_text = (str); } while (0)
 
 #define PUTCOMP(comp)          do { NEW(FT_COMP,0,0); ADDC(comp); } while (0)
-#define PUTLIT(str)            do { NEW(FT_LIT,0,0); fp->f_text = (str); } while (0)
+#define PUTLIT(str)            do { NEW(FT_LIT,0,0); fp->f_text = getcpy(str); } while (0)
 #define PUTC(c)                        do { NEW(FT_CHAR,0,0); fp->f_char = (c); } while (0)
 
 static char *format_string;
@@ -245,11 +255,6 @@ static unsigned char *usr_fstring; /* for CERROR */
 #define CERROR(str) compile_error (str, cp)
 
 /*
- * external prototypes
- */
-extern char *getusername(void);
-
-/*
  * static prototypes
  */
 static struct ftable *lookup(char *);
@@ -261,6 +266,8 @@ static char *do_func(char *);
 static char *do_expr (char *, int);
 static char *do_loop(char *);
 static char *do_if(char *);
+static void free_component(struct comp *);
+static void free_comptable(void);
 
 
 /*
@@ -313,16 +320,17 @@ compile_error(char *str, char *cp)
  */
 
 int
-fmt_compile(char *fstring, struct format **fmt)
+fmt_compile(char *fstring, struct format **fmt, int reset_comptable)
 {
     register char *cp;
     size_t i;
 
-    if (format_string)
-       free (format_string);
     format_string = getcpy (fstring);
     usr_fstring = fstring;
 
+    if (reset_comptable)
+       free_comptable();
+
     /* init the component hash table. */
     for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++)
        wantcomp[i] = 0;
@@ -352,6 +360,7 @@ fmt_compile(char *fstring, struct format **fmt)
     LV(FT_DONE, 0);            /* really done */
     *fmt = formatvec;
 
+    free(format_string);
     return (ncomp);
 }
 
@@ -593,6 +602,18 @@ do_func(char *sp)
        LS(t->f_type, getusername());
        break;
 
+    case TF_MYNAME:
+       LS(t->f_type, getfullname());
+       break;
+
+    case TF_MYHOST:
+       LS(t->f_type, LocalName(0));
+       break;
+
+    case TF_LMBOX:
+       LS(t->f_type, getlocalmbox());
+       break;
+
     case TF_NOW:
        LV(t->f_type, time((time_t *) 0));
        break;
@@ -752,3 +773,37 @@ do_if(char *sp)
 
     return (cp);
 }
+
+/*
+ * Free and reset our component hash table
+ */
+
+static void
+free_comptable(void)
+{
+    int i;
+    struct comp *cm, *cm2;
+
+    for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
+       cm = wantcomp[i];
+       while (cm != NULL) {
+           cm2 = cm->c_next;
+           free_component(cm);
+           cm = cm2;
+       }
+       wantcomp[i] = 0;
+    }
+}
+
+/*
+ * Decrement the reference count of a component structure.  If it reaches
+ * zero, free it
+ */
+
+static void
+free_component(struct comp *cm)
+{
+    if (--cm->c_refcount <= 0) {
+       free(cm);
+    }
+}