/* This file is part of mailfromd. -*- c -*- Copyright (C) 2006, 2007, 2008 Sergey Poznyakoff 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 3, 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, see . */ MF_DEFUN(toupper, STRING, STRING string) { size_t off; char *s = MF_COPY_STRING(off, string); char *p; for (p = s; *p; p++) *p = toupper(*p); MF_RETURN(off); } END MF_DEFUN(tolower, STRING, STRING string) { size_t off; char *s = MF_COPY_STRING(off, string); char *p; for (p = s; *p; p++) *p = tolower(*p); MF_RETURN(off); } END MF_DEFUN(length, NUMBER, STRING string) { MF_RETURN(strlen(string)); } END MF_DEFUN(substring, STRING, STRING string, NUMBER start, NUMBER end) { size_t off; long len = strlen(string); char *s; if (end < 0) end = len - 1; if (end < start) { long t = end; end = start; start = t; } MF_ASSERT(start < len && end < len, mfe_range, _("Argument out of range")); len = end - start + 1; s = MF_ALLOC_HEAP(off, len + 1); memcpy(s, string + start, len); s[len] = 0; MF_RETURN(off); } END MF_DEFUN(substr, STRING, STRING string, NUMBER start, OPTIONAL, NUMBER nbytes) { size_t off; long len = strlen(string); char *s; MF_ASSERT(start >= 0, mfe_range, _("Argument out of range: start=%ld"), start); if (!MF_DEFINED(nbytes)) nbytes = len - start; MF_ASSERT(nbytes >= 0, mfe_range, _("Argument out of range: start=%ld, len=%ld"), start, len); s = MF_ALLOC_HEAP(off, nbytes + 1); memcpy(s, string + start, nbytes); s[nbytes] = 0; MF_RETURN(off); } END MF_DEFUN(index, NUMBER, STRING str, STRING sub) { char *p = strstr(str, sub); MF_RETURN(p ? p - str : -1); } END MF_DEFUN(rindex, NUMBER, STRING str, STRING sub) { int slen, xlen, rc; char *p, *s, *x; char *temp; slen = strlen(str); xlen = strlen(sub); temp = MF_ALLOC_HEAP_TEMP(xlen + slen + 2); s = temp; x = s + slen + 1; /* Reverse str */ #define REV(v,s,l) \ l = strlen(s); \ v[l] = 0; \ for (p = v + l - 1; p >= v; p--, s++) \ *p = *s; REV(s,str,slen); REV(x,sub,xlen); p = strstr(s, x); if (p) rc = slen - (p - s + xlen); else rc = -1; MF_RETURN(rc); } END MF_DEFUN(revstr, STRING, STRING str) { int len = strlen(str); char *p; size_t off; char *s = MF_ALLOC_HEAP(off, len + 1); s[len] = 0; for (p = s + len - 1; p >= s; p--, str++) *p = *str; MF_RETURN(off); } END MF_DEFUN(message_header_decode, STRING, STRING text, OPTIONAL, STRING charset) { char *p; int rc = mu_rfc2047_decode (MF_OPTVAL(charset, "utf-8"), text, &p); MF_ASSERT(rc == 0, mf_failure, _("Error decoding string: %s"), mu_strerror(rc)); pushs(env, p); free(p); } END MF_DEFUN(message_header_encode, STRING, STRING text, OPTIONAL, STRING encoding, STRING charset) { char *p; int rc = mu_rfc2047_encode (MF_OPTVAL(charset, "utf-8"), MF_OPTVAL(encoding, "quoted-printable"), text, &p); MF_ASSERT(rc == 0, mf_failure, _("Error encoding string: %s"), mu_strerror(rc)); pushs(env, p); free(p); } END MF_DEFUN(unfold, STRING, STRING text) { size_t off; char *s = MF_COPY_STRING(off, text); mu_string_unfold(s, NULL); MF_RETURN(off); } END MF_INIT