MH−FORMAT(5) — NEWS-OS Programmer’s Manual
NAME
mh−format − format file for MH message system
SYNOPSIS
some MH commands
DESCRIPTION
Several MH commands utilize either a format string or a format file during their execution. For example, scan (1) uses a format string which directs it how to generate the scan listing for each message; repl (1) uses a format file which directs it how to generate the reply to a message, and so on.
Format strings are designed to be efficiently parsed by MH which means they are not necessarily simple to write and understand. This means that novice, casual, or even advanced users of MH should not have to deal with them. Some canned scan listing formats are in /usr/new/lib/mh/scan.time, /usr/new/lib/mh/scan.size, and /usr/new/lib/mh/scan.timely. Look in /usr/new/lib/mh for other scan and repl format files which may have been written at your site. It suffices to have your local MH expert actually write new format commands or modify existing ones. This manual section explains how to do that. Note: familiarity with the C printf routine is assumed.
A format string consists of ordinary text, and special multi−character escape sequences which begin with ‘%’. When specifying a format string, the usual C backslash characters are honored: ‘\b’, ‘\f’, ‘\n’, ‘\r’, and ‘\t’. Continuation lines in format files end with ‘\’ followed by the newline character.
There are three types of escape sequences: header components, built−in functions, and, flow control. A component escape is specified as ‘%{component}’, and exists for each header found in the message being processed. For example ‘%{date}’ refers to the “Date:” field of the appropriate message.
All component escapes have a string value. Normally, component values are compressed by converting any control characters (tab and newline included) to spaces, then eliding any leading or multiple spaces. However, commands may give different interpretations to some component escapes; be sure to refer to each command’s manual entry for complete details. A function escape is specified as ‘%(function)’. All functions are built−in, and most have a string or numeric value. A control escape is one of: ‘%<’, ‘%|’, or ‘%>’. These form one of four conditional execution constructs with the following syntax:
%<(function)Command-String%>
%<{component}Command-String%>
%<(function)Then-Command-String%|Else-Command-String%>
%<{component}Then-Command-String%|Else-Command-String%>
These constructs may be nested without ambiguity. In the first two forms of the construct, if the function or component value is non−zero (for integer−valued escapes), or non−empty (for string−valued escapes), then everything up to the corresponding ‘%>’ is interpreted, otherwise, skip to the corresponding ‘%>’ and start interpreting again. In the second two forms of the construct, the first command string is interpreted if the function or component value is non−zero or non−null, and the second command string is interpreted otherwise. Only one of the command strings will be interpreted. Interpre‘ing the ‘%|’ control escape causes a skip to the corresponding ‘%>’. Most functions expect an argument of a particular type:
ArgumentDescriptionExample Syntax
literalA literal number,%(func 1234)
or string%(func text string)
compAny header component%(func{in-reply-to})
dateA date component%(func{date})
addrAn address component%(func{from})
exprAn optional component,%(func(func2))
function or control,%(func %<{reply-to}%|%{from}%>)
perhaps nested%(func(func2{comp}))
The types date and addr have the same syntax as comp, but require that the header component be a date string, or address string, respectively. All arguments except those of type expr are required. For the expr argument type, the leading ‘%’ must be omitted for component and function escape arguments, and must be present (with a leading space) for control escape arguments.
The evaluation of format strings is based on a simple machine with an integer register num, and a text string register str. When a function escape is processed, if it accepts an optional expr argument which is not present, it reads the current value of either num or str as appropriate. Component escapes write the value of their message header in str. Function escapes write their return value in num for functions returning integer or boolean values, and in str for functions returning string values. (The boolean type is a subset of integers with usual values 0=false and 1=true.) All component escapes, and those function escapes which return an integer or string value, pass this value back to their caller in addition to setting str or num. These escapes will print out this value unless called as part of an argument to another escape sequence. Function escapes which return a boolean value do pass this value back to their caller, but will never print out the value.
FunctionArgumentReturnDescription
msgintegermessage number
curintegermessage is current
sizeintegersize of message
strlenintegerlength of str
widthintegeroutput buffer size in bytes
charleftintegerbytes left in output buffer
timenowintegerseconds since the UNIX epoch
mestringthe user’s mailbox
eqliteralbooleannum == arg
neliteralbooleannum != arg
gtliteralbooleannum > arg
matchliteralbooleanstr contains arg
amatchliteralbooleanstr starts with arg
plusliteralintegerarg plus num
minusliteralintegerarg minus num
divideliteralintegernum divided by arg
numliteralintegerSet num to arg
lit literalstringSet str to arg
nonzeroexprbooleannum is non−zero
zeroexprbooleannum is zero
nullexprbooleanstr is empty
nonnullexprbooleanstr is non−empty
voidexprSet str or num
compcompstringSet str to component text
compvalcompintegernum set to “atoi(str)”
trimexprtrim trailing white−space from str
putstrexprprint str
putstrfexprprint str in a fixed width
putnumexprprint num
putnumfexprprint num in a fixed width
These functions require a date component as an argument:
FunctionArgumentReturnDescription
secdateintegerseconds of the minute
mindateintegerminutes of the hour
hourdateintegerhours of the day (0-23)
wdaydateintegerday of the week (Sun=0)
daydatestringday of the week (abbrev.)
weekdaydatestringday of the week
sdaydateintegerday of the week known?
(0=implicit,−1=unknown)
mdaydateintegerday of the month
ydaydateintegerday of the year
mondateintegermonth of the year
monthdatestringmonth of the year (abbrev.)
lmonthdatestringmonth of the year
yeardateintegeryear of the century
zonedateintegertimezone in hours
tzonedatestringtimezone string
szonedateintegertimezone explicit?
(0=implicit,−1=unknown)
date2localdatecoerce date to local timezone
date2gmtdatecoerce date to GMT
dstdateintegerdaylight savings in effect?
clockdateintegerseconds since the UNIX epoch
rclockdateintegerseconds prior to current time
twsdatestringofficial 822 rendering
prettydatestringuser−friendly rendering
nodatedateintegerstr not a date string
These functions require an address component as an argument. The return value of functions noted with ‘∗’ pertain only to the first address present in the header component.
FunctionArgumentReturnDescription
properaddrstringofficial 822 rendering
friendlyaddrstringuser−friendly rendering
persaddrstringthe personal name∗
noteaddrstringcommentary text∗
mboxaddrstringthe local mailbox∗
mymboxaddrintegerthe user’s addresses? (0=no,1=yes)
hostaddrstringthe host domain∗
nohostaddrintegerno host was present∗
typeaddrintegerhost type∗ (0=local,1=network,
−1=uucp,2=unknown)
pathaddrstringany leading host route∗
ingrpaddrintegeraddress was inside a group∗
gnameaddrstringname of group∗
formataddrexprappend arg to str as a
(comma separated) address list
putaddrliteralprint str address list with
arg as optional label;
get line width from num
When escapes are nested, evaluation is done from inner-most to outer-most. The outer-most escape must begin with ‘%’; the inner escapes must not. For example,
%<(mymbox{from} To: %{to}%>
writes the value of the header component “From:” to str; then (mymbox) reads str and writes its result to num; then the control escape evaluates num. If num is non-zero, the string “To: ” is printed followed by the value of the header component “To:”. A minor explanation of (mymbox{comp}) is in order. In general, it checks each of the addresses in the header component “comp” against the user’s mailbox name and any Alternate-Mailboxes. It returns true if any address matches, however, it also returns true if the “comp” header is not present in the message. If needed, the (null) function can be used to explicitly test for this condition. When a function or component escape is interpreted and the result will be immediately printed, an optional field width can be specified to print the field in exactly a given number of characters. For example, a numeric escape like %4(size) will print at most 4 digits of the message size; overflow will be indicated by a ‘?’ in the first position (like ‘?234’). A string escape like %4(me) will print the first 4 characters and truncate at the end. Short fields are padded at the right with the fill character (normally, a blank). If the field width argument begins with a leading zero, then the fill character is set to a zero. As above, the functions (putnumf) and (putstrf) print their result in exactly the number of characters specified by their leading field width argument. For example, %06(putnumf(size)) will print the message size in a field six characters wide filled with leading zeros; %14(putstrf{from}) will print the “From:” header component in fourteen characters with trailing spaces added as needed. For putstrf, using a negative value for the field width causes right-justification of the string within the field, with padding on the left up to the field width. The functions (putnum) and (putstr) print their result in the minimum number of characters required, and ignore any leading field width argument. The available output width is kept in an internal register; any output past this width will be truncated. With all this in mind, here’s the default format string for scan. It’s been divided into several pieces for readability. The first part is:
%4(putnumf(msg))%<(cur)+%| %>%<{replied}−%| %>
which says that the message number should be printed in four digits, if the message is the current message then a ‘+’ else a space should be printed, and if a “Replied:” field is present then a ‘−’ else a space should be printed. Next:
%02(putnumf(mon{date}))/%02(putnumf(mday{date}))
the month and date are printed in two digits (zero filled) separated by a slash. Next,
%<{date} %|∗%>
If a “Date:” field was present, then a space is printed, otherwise a ‘∗’. Next,
%<(mymbox{from})To:%14(putstrf(friendly{to}))
if the message is from me, print ‘To:’ followed by a “user−friendly” rendering of the first address in the “To:” field. Continuing,
%|%17(putstrf(friendly{from}))%>
if the message isn’t from me, then the print the “From:” address is printed. And finally,
%{subject}%<{body}<<%{body}>>%>
the subject and initial body (if any) are printed. For a more complicated example, next consider the default replcomps format file.
%(lit)%(formataddr %<{reply-to}%|
This clears str and formats the “Reply-To:” header if present. If not present, the else clause is executed:
%<{from}%|%<{sender}%|%<{return-path}%>%>%>%>)\
This formats the “From:”, “Sender:” and “Return-Path:” headers, stopping as soon as one of them is present. Next:
%<(nonnull)%(void(width))%(putaddr To: )\n%>\
If the formataddr result is non−null, it is printed as an address (with line folding if needed) in a field width wide with a leading label of “To: ”.
%(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\
str is cleared, and the “To:” and “Cc:” headers, along with the user’s address (depending on what was specified with the “−cc” switch to repl) are formatted.
%<(nonnull)%(void(width))%(putaddr cc: )\n%>\
If the result is non−null, it is printed as above with a leading label of “cc: ”.
%<{fcc}Fcc: %{fcc}\n%>\
If a “−fcc folder” switch was given to repl (see repl (1) for more details about %{fcc}), an “Fcc:” header is output.
%<{subject}Subject: Re: %{subject}\n%>\
If a subject component was present, a suitable reply subject is output.
%<{date}In-reply-to: Your message of "\
%<(nodate{date})%{date}%|%(tws{date})%>."%<{message-id}
%{message-id}%>\n%>\
--------
If a date component was present, an “In-Reply-To:” header is output with the preface “Your message of ”. If the date was parseable, it is output in official format, otherwise it is output as−is. The message-id is included if present. As with all plain−text, the row of dashes are output as−is. This last part is a good example for a little more elaboration. Here’s that part again in pseudo−code:
if (comp_exists(date)) then
print (“In-reply-to: Your message of \\*(lq”)
if (not_date_string(date.value) then
print (date.value)
else
print (rfc822(date.value))
endif
print (“\\*(rq”)
if (comp_exists(message-id)) then
print (“\n\t”)
print (message-id.value)
endif
print (“\n”)
endif
Although this seems complicated, in point of fact, this method is flexible enough to extract individual fields and print them in any format the user desires. None None scan(1), repl(1), ap(8), dp(8) None None This software was contributed for MH 6.3. Prior to this, output format specifications were much easier to write, but considerably less flexible. On hosts where MH was configured with the BERK option, address parsing is not enabled.
NEWS-OSRelease 4.1C