mh_format.c 29.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* This module implements execution of MH format strings. */

#include <mh.h>

#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

typedef struct       /* A string object type */
{
  int size;          /* Allocated size or 0 for static storage */
  char *ptr;         /* Actual data */
} strobj_t;

struct mh_machine
{
  strobj_t reg_str;         /* String register */
  int reg_num;              /* Numeric register */

  strobj_t arg_str;         /* String argument */
  int arg_num;              /* Numeric argument */
  
  size_t pc;                /* Program counter */
  size_t progsize;          /* Size of allocated program*/
  mh_opcode_t *prog;        /* Program itself */
  int stop;                 /* Stop execution immediately */

  char *outbuf;             /* Output buffer */
  size_t width;             /* Output buffer size */
  size_t ind;               /* Output buffer index */

  int fmtflags;             /* Current formatting flags */
  
  message_t message;        /* Current message */
  size_t msgno;             /* Its number */
};

/* Functions for handling string objects. */

void
strobj_free (strobj_t *obj)
{
  if (obj->size)
    free (obj->ptr);
  obj->size = 0;
  obj->ptr = NULL;
}

#define strobj_ptr(p) ((p)->ptr ? (p)->ptr : "")
#define strobj_len(p) strlen((p)->ptr)
#define strobj_is_static(p) ((p)->size == 0)

static void
strobj_create (strobj_t *lvalue, char *str)
{
  int len = strlen (str);
  lvalue->size = len+1;
  lvalue->ptr = xmalloc (lvalue->size);
  memcpy (lvalue->ptr, str, lvalue->size);
}

static void
strobj_set (strobj_t *lvalue, char *str)
{
  lvalue->size = 0;
  lvalue->ptr = str;
}

static void
strobj_assign (strobj_t *lvalue, strobj_t *rvalue)
{
  strobj_free (lvalue);
  *lvalue = *rvalue;
  strobj_free (rvalue);
}

static void
strobj_copy (strobj_t *lvalue, strobj_t *rvalue)
{
  if (lvalue->size >= strobj_len (rvalue) + 1)
    memcpy (lvalue->ptr, strobj_ptr (rvalue), strobj_len (rvalue) + 1);
  else
    {
      if (lvalue->size)
	strobj_free (lvalue);
      strobj_create (lvalue, strobj_ptr (rvalue));
    }
}

/* Compress whitespace in a string */
static void
compress_ws (char *str, size_t *size)
{
  unsigned char *p, *q;
  size_t len = *size;
  int space = 0;

  for (p = q = (unsigned char*) str; len; len--, q++)
    {
      if (isspace (*q))
	{
	  if (!space)
	    *p++ = ' ';
	  space++;
	  continue;
	}
      else if (space)
	space = 0;

      if (isprint (*q))
	*p++ = *q;
    }
  *size = p - (unsigned char*) str;
}

/* Print len bytes from str into mach->outbuf */
static void
print_string (struct mh_machine *mach, char *str, size_t len)
{
  size_t rest = strlen (str);

  if (len > rest)
    len = rest;
  rest = mach->width - mach->ind;
  if (len > rest)
    len = rest;
  memcpy (mach->outbuf + mach->ind, str, len);
  compress_ws (mach->outbuf + mach->ind, &len);
  mach->ind += len;
}

static void
reset_fmt_defaults (struct mh_machine *mach)
{
  mach->fmtflags = 0;
}

/* Execute pre-compiled format on message msg with number msgno.
   buffer and bufsize specify output storage */
int
mh_format (mh_format_t *fmt, message_t msg, size_t msgno,
	   char *buffer, size_t bufsize)
{
  struct mh_machine mach;
  char buf[64];
  
  memset (&mach, 0, sizeof (mach));
  mach.progsize = fmt->progsize;
  mach.prog = fmt->prog;

  mach.message = msg;
  mach.msgno = msgno;
  
  mach.width = bufsize - 1;  /* Allow for terminating NULL */
  mach.outbuf = buffer;
  mach.pc = 1;
  reset_fmt_defaults (&mach);
  
  while (!mach.stop)
    {
      mh_opcode_t opcode;
      switch (opcode = mach.prog[mach.pc++])
	{
	case mhop_stop:
	  mach.stop = 1;
	  break;

	case mhop_branch:
	  mach.pc += mach.prog[mach.pc];
	  break;

	case mhop_num_asgn:
	  mach.reg_num = mach.arg_num;
	  break;
	  
	case mhop_str_asgn:
	  strobj_assign (&mach.reg_str, &mach.arg_str);
	  break;
	  
	case mhop_num_arg:
	  mach.arg_num = mach.prog[mach.pc++];
	  break;
	  
	case mhop_str_arg:
	  {
	    size_t skip = mach.prog[mach.pc++];
	    strobj_set (&mach.arg_str, (char*)&mach.prog[mach.pc]);
	    mach.pc += skip;
	  }
	  break;

	case mhop_num_branch:
	  if (!mach.reg_num)
	    mach.pc += mach.prog[mach.pc];
	  else
	    mach.pc++;
	  break;

	case mhop_str_branch:
	  if (!*strobj_ptr (&mach.reg_str))
	    mach.pc += mach.prog[mach.pc];
	  else
	    mach.pc++;
	  break;

	case mhop_call:
	  (*(mh_builtin_fp) mach.prog[mach.pc++]) (&mach);
	  break;

	case mhop_header:
	  {
	    header_t hdr = NULL;
	    char *value = NULL;
	    message_get_header (mach.message, &hdr);
	    header_aget_value (hdr, strobj_ptr (&mach.arg_str), &value);
	    strobj_free (&mach.reg_str);
	    if (value)
	      {
		mach.reg_str.size = strlen (value) + 1;
		mach.reg_str.ptr = value;
	      }
	  }
	  break;

	case mhop_body:
	  {
	    body_t body = NULL;
	    stream_t stream = NULL;
	    size_t size = 0, off, str_off, nread;
	    size_t rest = mach.width - mach.ind;

	    strobj_free (&mach.reg_str);
	    message_get_body (mach.message, &body);
	    body_size (body, &size);
	    body_get_stream (body, &stream);
	    if (size == 0 || !stream)
	      break;
	    if (size > rest)
	      size = rest;

	    mach.reg_str.ptr = xmalloc (size+1);
	    mach.reg_str.size = size;
	    
	    off = 0;
	    str_off = 0;
	    while (!stream_read (stream, mach.reg_str.ptr + str_off,
				 mach.reg_str.size - str_off, off, &nread)
		   && nread != 0
		   && str_off < size)
	      {
		off += nread;
       		compress_ws (mach.reg_str.ptr + str_off, &nread);
		if (nread)
		  str_off += nread;
	      }
	    mach.reg_str.ptr[str_off] = 0;
	  }
	  break;
	  
	  /* assign reg_num to arg_num */
	case mhop_num_to_arg:
	  mach.arg_num = mach.reg_num;
	  break;
	  
	  /* assign reg_str to arg_str */
	case mhop_str_to_arg:
	  strobj_copy (&mach.arg_str, &mach.reg_str);
	  break;

	  /* Convert arg_str to arg_num */
	case mhop_str_to_num:
	  mach.arg_num = strtoul (strobj_ptr (&mach.arg_str), NULL, 0);
	  break;
  
	  /* Convert arg_num to arg_str */
	case mhop_num_to_str:
	  snprintf (buf, sizeof buf, "%d", mach.arg_num);
	  strobj_free (&mach.arg_str);
	  strobj_create (&mach.arg_str, buf);
	  break;

	case mhop_num_print:
	  {
	    int n;
	    char buf[64];
	    char *ptr;
	    int fmtwidth = mach.fmtflags & MH_WIDTH_MASK;
	    int padchar = mach.fmtflags & MH_FMT_ZEROPAD ? '0' : ' ';
	    
	    n = snprintf (buf, sizeof buf, "%d", mach.reg_num);

	    if (fmtwidth)
	      {
		if (n > fmtwidth)
		  {
		    ptr = buf + n - fmtwidth;
		    *ptr = '?';
		  }
		else
		  {
		    int i;
		    ptr = buf;
		    for (i = n; i < fmtwidth && mach.ind < mach.width;
			 i++, mach.ind++)
		      mach.outbuf[mach.ind] = padchar;
		  }
	      }
	    else
	      ptr = buf;

	    print_string (&mach, ptr, strlen (ptr));
	    reset_fmt_defaults (&mach);
	  }
	  break;
	  
	case mhop_str_print:
	  if (mach.fmtflags)
	    {
	      int len = strobj_len (&mach.reg_str);
	      int fmtwidth = mach.fmtflags & MH_WIDTH_MASK;
	      int padchar = ' ';
				
	      if (mach.fmtflags & MH_FMT_RALIGN)
		{
		  int i, n;

		  n = fmtwidth - len;
		  for (i = 0; i < n && mach.ind < mach.width;
		       i++, mach.ind++, fmtwidth--)
		    mach.outbuf[mach.ind] = padchar;
		}
		
	      print_string (&mach, strobj_ptr (&mach.reg_str), fmtwidth);
	      reset_fmt_defaults (&mach);
	    }
	  else
	    {
	      print_string (&mach,
			   strobj_ptr (&mach.reg_str),
			   strobj_len (&mach.reg_str));
	    }
	  break;

	case mhop_fmtspec:
	  mach.fmtflags = mach.prog[mach.pc++];
	  break;
	  
	default:
	  mh_error ("Unknown opcode: %x", opcode);
	  abort ();
	}
    }
  strobj_free (&mach.reg_str);
  strobj_free (&mach.arg_str);
  mach.outbuf[mach.ind] = 0;
  return mach.ind;
}

/* Free any memory associated with a format structure. The structure
   itself is assumed to be in static storage. */
void
mh_format_free (mh_format_t *fmt)
{
  if (fmt->prog)
    free (fmt->prog);
  fmt->progsize = 0;
  fmt->prog = NULL;
}

/* Built-in functions */

/* Handler for unimplemented functions */
static void
builtin_not_implemented (char *name)
{
  mh_error ("%s is not yet implemented.", name);
}

static void
builtin_msg (struct mh_machine *mach)
{
  mach->reg_num = mach->msgno;
}

static void
builtin_cur (struct mh_machine *mach)
{
  /*FIXME*/
  mach->reg_num = 0;
}

static void
builtin_size (struct mh_machine *mach)
{
  message_size (mach->message, &mach->reg_num);
}

static void
builtin_strlen (struct mh_machine *mach)
{
  mach->reg_num = strlen (strobj_ptr (&mach->reg_str));
}

static void
builtin_width (struct mh_machine *mach)
{
  mach->reg_num = mach->width;
}

static void
builtin_charleft (struct mh_machine *mach)
{
  mach->reg_num = mach->width - mach->ind;
}

static void
builtin_timenow (struct mh_machine *mach)
{
  time_t t;
  
  time (&t);
  mach->reg_num = t;
}

static void
builtin_me (struct mh_machine *mach)
{
  /*FIXME*/
  /*  mach->reg_str = "me";*/
}

static void
builtin_eq (struct mh_machine *mach)
{
  mach->reg_num = mach->reg_num == mach->arg_num;
}

static void
builtin_ne (struct mh_machine *mach)
{
  mach->reg_num = mach->reg_num != mach->arg_num;
}

static void
builtin_gt (struct mh_machine *mach)
{
  mach->reg_num = mach->reg_num > mach->arg_num;
}

static void
builtin_match (struct mh_machine *mach)
{
  mach->reg_num = strstr (strobj_ptr (&mach->reg_str),
			  strobj_ptr (&mach->arg_str));
}

static void
builtin_amatch (struct mh_machine *mach)
{
  int len = strobj_len (&mach->arg_str);
  mach->reg_num = strncmp (strobj_ptr (&mach->reg_str),
			   strobj_ptr (&mach->arg_str), len);
}

static void
builtin_plus (struct mh_machine *mach)
{
  mach->reg_num += mach->arg_num;
}

static void
builtin_minus (struct mh_machine *mach)
{
  mach->reg_num -= mach->arg_num;
}

static void
builtin_divide (struct mh_machine *mach)
{
  if (!mach->arg_num)
    {
      mh_error ("format: divide by zero");
      mach->stop = 1;
    }
  else
    mach->reg_num /= mach->arg_num;
}

static void
builtin_modulo (struct mh_machine *mach)
{
  if (!mach->arg_num)
    {
      mh_error ("format: divide by zero");
      mach->stop = 1;
    }
  else
    mach->reg_num %= mach->arg_num;
}

static void
builtin_num (struct mh_machine *mach)
{
  mach->reg_num = mach->arg_num;
}

static void
builtin_lit (struct mh_machine *mach)
{
  strobj_assign (&mach->reg_str, &mach->arg_str);
}

static void
builtin_getenv (struct mh_machine *mach)
{
  strobj_free (&mach->reg_str);
  strobj_create (&mach->reg_str, getenv (strobj_ptr (&mach->arg_str)));
}

static void
builtin_profile (struct mh_machine *mach)
{
  /*FIXME*/
  /*mach->reg_str = "profile";*/
}

static void
builtin_nonzero (struct mh_machine *mach)
{
  /*nothing*/
}

static void
builtin_zero (struct mh_machine *mach)
{
  mach->reg_num = !mach->reg_num;
}

static void
builtin_null (struct mh_machine *mach)
{
  char *s = strobj_ptr (&mach->reg_str);
  mach->reg_num = !s && !s[0];
}

static void
builtin_nonnull (struct mh_machine *mach)
{
  char *s = strobj_ptr (&mach->reg_str);
  mach->reg_num = s && s[0];
}

/*     comp       comp     string   Set str to component text*/
static void
builtin_comp (struct mh_machine *mach)
{
  strobj_assign (&mach->reg_str, &mach->arg_str);
}

/*     compval    comp     integer  num set to "atoi(comp)"*/
static void
builtin_compval (struct mh_machine *mach)
{
  mach->reg_num = strtoul (strobj_ptr (&mach->arg_str), NULL, 0);
}

/*     trim       expr              trim trailing white-space from str*/
static void
builtin_trim (struct mh_machine *mach)
{
  char *p, *start;
  int len;
  
  if (strobj_is_static (&mach->arg_str))
    strobj_copy (&mach->arg_str, &mach->arg_str);

  start = strobj_ptr (&mach->arg_str);
  len = strlen (start);
  if (len == 0)
    return;
  for (p = start + len - 1; p >= start && isspace (*p); p--)
    ;
  p[1] = 0;
  strobj_copy (&mach->reg_str, &mach->arg_str);
}

/*     putstr     expr              print str*/
static void
builtin_putstr (struct mh_machine *mach)
{
  /*FIXME*/
}

/*     putstrf    expr              print str in a fixed width*/
static void
builtin_putstrf (struct mh_machine *mach)
{
  /*FIXME*/
}

/*     putnum     expr              print num*/
static void
builtin_putnum (struct mh_machine *mach)
{
  /*FIXME*/
}

/*     putnumf    expr              print num in a fixed width*/
static void
builtin_putnumf (struct mh_machine *mach)
{
  /*FIXME*/
}

static int
_parse_date (struct mh_machine *mach, struct tm *tm, mu_timezone *tz)
{
  char *date = strobj_ptr (&mach->arg_str);
  const char *p = date;
  
  if (parse822_date_time(&p, date+strlen(date), tm, tz))
    {
      mh_error ("can't parse date: [%s]", date);
      return -1;
    }
  return 0;
}

/*     sec        date     integer  seconds of the minute*/
static void
builtin_sec (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_sec;
}

/*     min        date     integer  minutes of the hour*/
static void
builtin_min (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_min;
}

/*     hour       date     integer  hours of the day (0-23)*/
static void
builtin_hour (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_hour;
}

/*     wday       date     integer  day of the week (Sun=0)*/
static void
builtin_wday (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_wday;
}

/*     day        date     string   day of the week (abbrev.)*/
static void
builtin_day (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  char buf[80];
  
  if (_parse_date (mach, &tm, &tz))
    return;

  strftime (buf, sizeof buf, "%a", &tm);
  strobj_free (&mach->arg_str);
  strobj_create (&mach->arg_str, buf);
}

/*     weekday    date     string   day of the week */
static void
builtin_weekday (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  char buf[80];
  
  if (_parse_date (mach, &tm, &tz))
    return;

  strftime (buf, sizeof buf, "%A", &tm);
  strobj_free (&mach->arg_str);
  strobj_create (&mach->arg_str, buf);
}

/*      sday       date     integer  day of the week known?
	(0=implicit,-1=unknown) */
static void
builtin_sday (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;

  /*FIXME: more elaborate check needed */
  if (_parse_date (mach, &tm, &tz))
    mach->reg_num = -1;
  else
    mach->reg_num = 1;
}

/*     mday       date     integer  day of the month*/
static void
builtin_mday (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_mday;
}

/*      yday       date     integer  day of the year */
static void
builtin_yday (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_yday;
}

/*     mon        date     integer  month of the year*/
static void
builtin_mon (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_mon+1;
}

/*     month      date     string   month of the year (abbrev.) */
static void
builtin_month (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  char buf[80];
  
  if (_parse_date (mach, &tm, &tz))
    return;

  strftime (buf, sizeof buf, "%b", &tm);
  strobj_free (&mach->arg_str);
  strobj_create (&mach->arg_str, buf);
}

/*      lmonth     date     string   month of the year*/
static void
builtin_lmonth (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  char buf[80];
  
  if (_parse_date (mach, &tm, &tz))
    return;

  strftime (buf, sizeof buf, "%B", &tm);
  strobj_free (&mach->arg_str);
  strobj_create (&mach->arg_str, buf);
}

/*     year       date     integer  year (may be > 100)*/
static void
builtin_year (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tm.tm_year + 1900;
}

/*     zone       date     integer  timezone in hours*/
static void
builtin_zone (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;

  mach->reg_num = tz.utc_offset;
}

/*     tzone      date     string   timezone string */
static void
builtin_tzone (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  if (_parse_date (mach, &tm, &tz))
    return;
  strobj_free (&mach->arg_str);
  strobj_create (&mach->arg_str, (char*) tz.tz_name);
}

/*      szone      date     integer  timezone explicit?
	(0=implicit,-1=unknown) */
static void
builtin_szone (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;

  /*FIXME: more elaborate check needed */
  if (_parse_date (mach, &tm, &tz))
    mach->reg_num = -1;
  else
    mach->reg_num = 1;
}

/*     date2local date              coerce date to local timezone*/
static void
builtin_date2local (struct mh_machine *mach)
{
  /*FIXME: Noop*/
}

/*     date2gmt   date              coerce date to GMT*/
static void
builtin_date2gmt (struct mh_machine *mach)
{
  /*FIXME: Noop*/
}

/*     dst        date     integer  daylight savings in effect?*/
static void
builtin_dst (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;

  if (_parse_date (mach, &tm, &tz))
    return;
#ifdef HAVE_TM_ISDST  
  mach->reg_num = tm.tm_isdst;
#else
  mach->reg_num = 0;
#endif
}

/*     clock      date     integer  seconds since the UNIX epoch*/
static void
builtin_clock (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;

  if (_parse_date (mach, &tm, &tz))
    return;
  mach->reg_num = mu_tm2time (&tm, &tz);
}

/*     rclock     date     integer  seconds prior to current time*/
void
builtin_rclock (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  time_t now = time (NULL);
  
  if (_parse_date (mach, &tm, &tz))
    return;
  mach->reg_num = now - mu_tm2time (&tm, &tz);
}

/*      tws        date     string   official 822 rendering */
static void
builtin_tws (struct mh_machine *mach)
{
  /*FIXME: noop*/
  strobj_copy (&mach->reg_str, &mach->arg_str);
}

/*     pretty     date     string   user-friendly rendering*/
static void
builtin_pretty (struct mh_machine *mach)
{
  /*FIXME: noop*/
  strobj_copy (&mach->reg_str, &mach->arg_str);
}

/*     nodate     date     integer  str not a date string */
static void
builtin_nodate (struct mh_machine *mach)
{
  struct tm tm;
  mu_timezone tz;
  
  mach->reg_num = _parse_date (mach, &tm, &tz);
}

/*     proper     addr     string   official 822 rendering */
static void
builtin_proper (struct mh_machine *mach)
{
  /*FIXME: noop*/
  strobj_copy (&mach->reg_str, &mach->arg_str);
}

/*     friendly   addr     string   user-friendly rendering*/
static void
builtin_friendly (struct mh_machine *mach)
{
  /*FIXME: noop*/
  strobj_copy (&mach->reg_str, &mach->arg_str);
}

/*     addr       addr     string   mbox@host or host!mbox rendering*/
static void
builtin_addr (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    strobj_create (&mach->reg_str, buf);
  address_destroy (&addr);
}

/*     pers       addr     string   the personal name**/
static void
builtin_pers (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_personal (addr, 1, buf, sizeof buf, &n) == 0)
    strobj_create (&mach->reg_str, buf);
  address_destroy (&addr);
}

/*     note       addr     string   commentary text*/
static void
builtin_note (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_comments (addr, 1, buf, sizeof buf, &n) == 0)
    strobj_create (&mach->reg_str, buf);
  address_destroy (&addr);
}

/*     mbox       addr     string   the local mailbox**/
static void
builtin_mbox (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    {
      char *p = strchr (buf, '@');
      if (p)
	*p = NULL;
      strobj_create (&mach->reg_str, p);
    }
  address_destroy (&addr);
}

/*     mymbox     addr     integer  the user's addresses? (0=no,1=yes)*/
static void
builtin_mymbox (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  mach->reg_num = 0;
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    mach->reg_num = mh_is_my_name (buf);
  address_destroy (&addr);
}

/*     host       addr     string   the host domain**/
static void
builtin_host (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    {
      char *p = strchr (buf, '@');
      if (p)
	strobj_create (&mach->reg_str, p+1);
    }
  address_destroy (&addr);
}

/*     nohost     addr     integer  no host was present**/
static void
builtin_nohost (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    mach->reg_num = strchr (buf, '@') != NULL;
  else
    mach->reg_num = 0;
  address_destroy (&addr);
}

/*     type       addr     integer  host type* (0=local,1=network,
       -1=uucp,2=unknown)*/
static void
builtin_type (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;

  if (address_get_email (addr, 1, buf, sizeof buf, &n) == 0)
    {
      if (strchr (buf, '@'))
	mach->reg_num = 1;
      else if (strchr (buf, '@'))
	mach->reg_num = -1;
      else
	mach->reg_num = 0; /* assume local */
    }
  else
    mach->reg_num = 2;
  address_destroy (&addr);
}

/*     path       addr     string   any leading host route**/
static void
builtin_path (struct mh_machine *mach)
{
  address_t addr;
  size_t n;
  char buf[80];
  
  strobj_free (&mach->reg_str);
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;
  if (address_get_route (addr, 1, buf, sizeof buf, &n))
    strobj_create (&mach->reg_str, buf);
  address_destroy (&addr);
}

/*     ingrp      addr     integer  address was inside a group**/
static void
builtin_ingrp (struct mh_machine *mach)
{
  /*FIXME:*/
  builtin_not_implemented ("ingrp");
}

/*     gname      addr     string   name of group**/
static void
builtin_gname (struct mh_machine *mach)
{
  /*FIXME:*/
  builtin_not_implemented ("gname");
}

/*     formataddr expr              append arg to str as a
       (comma separated) address list */
static void
builtin_formataddr (struct mh_machine *mach)
{
  address_t addr;
  size_t num = 0, n, i;
  char buf[80], *p;
  
  if (address_create (&addr, strobj_ptr (&mach->arg_str)))
    return;
  address_get_count (addr, &num);
  for (i = 1; i <= num; i++)
    {
      address_get_email (addr, i, buf, sizeof buf, &n);
      num += n+1;
    }

  num += strobj_len (&mach->reg_str) + 1;
  mach->reg_str.ptr = xrealloc (mach->reg_str.ptr, num);
  mach->reg_str.size = num + 1;
  p = strobj_ptr (&mach->reg_str) + strobj_len (&mach->reg_str) ;
  for (i = 1; i <= num; i++)
    {
      address_get_email (addr, i, buf, sizeof buf, &n);
      memcpy (p, buf, n);
      p += n;
    }
  *p = 0;
}

/*      putaddr    literal           print str address list with
                                  arg as optional label;
                                  get line width from num */
static void
builtin_putaddr (struct mh_machine *mach)
{
  /*FIXME:*/
  builtin_not_implemented ("putaddr");
}

/* Builtin function table */

mh_builtin_t builtin_tab[] = {
  /* Name       Handling function Return type  Arg type */ 
  { "msg",      builtin_msg,      mhtype_num,  mhtype_none },
  { "cur",      builtin_cur,      mhtype_num,  mhtype_none },
  { "size",     builtin_size,     mhtype_num,  mhtype_none },
  { "strlen",   builtin_strlen,   mhtype_num,  mhtype_none },
  { "width",    builtin_width,    mhtype_num,  mhtype_none },
  { "charleft", builtin_charleft, mhtype_num,  mhtype_none },
  { "timenow",  builtin_timenow,  mhtype_num,  mhtype_none },
  { "me",       builtin_me,       mhtype_str,  mhtype_none },
  { "eq",       builtin_eq,       mhtype_num,  mhtype_num  },
  { "ne",       builtin_ne,       mhtype_num,  mhtype_num  },
  { "gt",       builtin_gt,       mhtype_num,  mhtype_num  },
  { "match",    builtin_match,    mhtype_num,  mhtype_str },
  { "amatch",   builtin_amatch,   mhtype_num,  mhtype_str },
  { "plus",     builtin_plus,     mhtype_num,  mhtype_num },
  { "minus",    builtin_minus,    mhtype_num,  mhtype_num },
  { "divide",   builtin_divide,   mhtype_num,  mhtype_num },
  { "modulo",   builtin_modulo,   mhtype_num,  mhtype_num },
  { "num",      builtin_num,      mhtype_num,  mhtype_num },
  { "lit",      builtin_lit,      mhtype_str,  mhtype_str },
  { "getenv",   builtin_getenv,   mhtype_str,  mhtype_str },
  { "profile",  builtin_profile,  mhtype_str,  mhtype_str },
  { "nonzero",  builtin_nonzero,  mhtype_num,  mhtype_num, 1 },
  { "zero",     builtin_zero,     mhtype_num,  mhtype_num, 1 },
  { "null",     builtin_null,     mhtype_num,  mhtype_str, 1 },
  { "nonnull",  builtin_nonnull,  mhtype_num,  mhtype_str, 1 },
  { "comp",     builtin_comp,     mhtype_num,  mhtype_str },
  { "compval",  builtin_compval,  mhtype_num,  mhtype_str },
  { "trim",     builtin_trim,     mhtype_str,  mhtype_str, 1 },
  { "putstr",   builtin_putstr,   mhtype_str,  mhtype_str, 1 },
  { "putstrf",  builtin_putstrf,  mhtype_str,  mhtype_str, 1 },
  { "putnum",   builtin_putnum,   mhtype_str,  mhtype_num, 1 },
  { "putnumf",  builtin_putnumf,  mhtype_str,  mhtype_num, 1 },
  { "sec",      builtin_sec,      mhtype_num,  mhtype_str },
  { "min",      builtin_min,      mhtype_num,  mhtype_str },
  { "hour",     builtin_hour,     mhtype_num,  mhtype_str },
  { "wday",     builtin_wday,     mhtype_num,  mhtype_str },
  { "day",      builtin_day,      mhtype_str,  mhtype_str },
  { "weekday",  builtin_weekday,  mhtype_str,  mhtype_str },
  { "sday",     builtin_sday,     mhtype_num,  mhtype_str },
  { "mday",     builtin_mday,     mhtype_num,  mhtype_str },
  { "yday",     builtin_yday,     mhtype_num,  mhtype_str },
  { "mon",      builtin_mon,      mhtype_num,  mhtype_str },
  { "month",    builtin_month,    mhtype_str,  mhtype_str },
  { "lmonth",   builtin_lmonth,   mhtype_str,  mhtype_str },
  { "year",     builtin_year,     mhtype_num,  mhtype_str },
  { "zone",     builtin_zone,     mhtype_num,  mhtype_str },
  { "tzone",    builtin_tzone,    mhtype_str,  mhtype_str },
  { "szone",    builtin_szone,    mhtype_num,  mhtype_str },
  { "date2local", builtin_date2local, mhtype_str, mhtype_str },
  { "date2gmt", builtin_date2gmt, mhtype_str,  mhtype_str },
  { "dst",      builtin_dst,      mhtype_num,  mhtype_str },
  { "clock",    builtin_clock,    mhtype_num,  mhtype_str },
  { "rclock",   builtin_rclock,   mhtype_num,  mhtype_str },
  { "tws",      builtin_tws,      mhtype_str,  mhtype_str },
  { "pretty",   builtin_pretty,   mhtype_str,  mhtype_str },
  { "nodate",   builtin_nodate,   mhtype_num,  mhtype_str },
  { "proper",   builtin_proper,   mhtype_str,  mhtype_str },
  { "friendly", builtin_friendly, mhtype_str,  mhtype_str },
  { "addr",     builtin_addr,     mhtype_str,  mhtype_str },
  { "pers",     builtin_pers,     mhtype_str,  mhtype_str },
  { "note",     builtin_note,     mhtype_str,  mhtype_str },
  { "mbox",     builtin_mbox,     mhtype_str,  mhtype_str },
  { "mymbox",   builtin_mymbox,   mhtype_num,  mhtype_str },
  { "host",     builtin_host,     mhtype_str,  mhtype_str },
  { "nohost",   builtin_nohost,   mhtype_num,  mhtype_str },
  { "type",     builtin_type,     mhtype_num,  mhtype_str },
  { "path",     builtin_path,     mhtype_str,  mhtype_str },
  { "ingrp",    builtin_ingrp,    mhtype_num,  mhtype_str },
  { "gname",    builtin_gname,    mhtype_str,  mhtype_str},
  { "formataddr", builtin_formataddr, mhtype_none, mhtype_str, 1 },
  { "putaddr",  builtin_putaddr,  mhtype_none, mhtype_str },
  { 0 }
};

mh_builtin_t *
mh_lookup_builtin (char *name, int *rest)
{
  mh_builtin_t *bp;
  int namelen = strlen (name);
  
  for (bp = builtin_tab; bp->name; bp++)
    {
      int len = strlen (bp->name);
      if (len >= namelen
	  && memcmp (name, bp->name, len) == 0)
	{
	  *rest = namelen - len;
	  return bp;
	}
    }
  return NULL;
}