blob: fd30ee04d4ed9f107b881842885ef6a5bf55e45e [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH PLUMB 7
2.SH NAME
3plumb \- format of plumb messages and rules
4.SH SYNOPSIS
5.B #include <plumb.h>
6.SH DESCRIPTION
7.SS "Message format
8The messages formed by the
9.IR plumb (3)
10library are formatted for transmission between
11processes into textual form, using newlines to separate
12the fields.
13Only the data field may contain embedded newlines.
14The fields occur in a specified order,
15and each has a name, corresponding to the elements
16of the
17.B Plumbmsg
18structure, that is used in the plumbing rules.
19The fields, in order, are:
20.RS
21.TF ndata
22.TP
23.B src
24application/service generating message
25.TP
26.B dst
27destination `port' for message
28.TP
29.B wdir
30working directory (used if data is a file name)
31.TP
32.B type
33form of the data, e.g.
34.B text
35.TP
36.B attr
37attributes of the message, in
38.IB name = value
39pairs separated by white space
40(the value must follow the usual quoting convention if it contains
41white space or quote characters or equal signs; it cannot contain a newline)
42.TP
43.B ndata
44number of bytes of data
45.TP
46.B data
47the data itself
48.RE
49At the moment, only textual data
50.RB ( type=text )
51is supported.
52.PD
53.PP
54All fields are optional, but
55.B type
56should usually be set since it describes the form of the data, and
57.B ndata
58must be an accurate count (possibly zero) of the number of bytes of data.
59A missing field is represented by an empty line.
60.SS "Plumbing rules
61The
62.B plumber
63(see
64.IR plumb (1))
65receives messages on its
66.B send
67port (applications
68.I send
69messages there), interprets and reformats them, and (typically) emits them from a destination port.
70Its behavior is determined by a plumbing rules file, default
71.BR /usr/$user/lib/plumbing ,
72which defines a set of pattern/action rules with which to analyze, rewrite, and dispatch
73received messages.
74.PP
75The file is a sequence of rule sets, each of which is a set of one-line rules
76called patterns and actions.
77There must be at least one pattern and one action in each rule set.
78(The only exception is that a rule set may contain nothing but
79.B plumb
80.B to
81rules; such a rule set declares the named ports but has no other effect.)
82A blank line terminates a rule set.
83Lines beginning with a
84.B #
85character are commentary and are regarded as blank lines.
86.PP
87A line of the form
88.EX
89 include \f2file\fP
90.EE
91substitutes the contents of
92.I file
93for the line, much as in a C
94.B #include
95statement. Unlike in C, the file name is not quoted.
96If
97.I file
98is not an absolute path name, or one beginning
99.B ./
100or
101.BR ../ ,
102.I file
103is looked for first in the directory in which the plumber is executing,
104and then in
105.BR /sys/lib/plumb .
106.PP
107When a message is received by the
108.BR plumber ,
109the rule sets are examined in order.
110For each rule set, if the message matches all the patterns in the rule set,
111the actions associated with the rule set are triggered to dispose of the message.
112If a rule set is triggered, the rest are ignored for this message.
113If none is triggered, the message is discarded (giving a write error to the sender)
114unless it has a
115.B dst
116field that specifies an existing port, in which case the message is emitted, unchanged, from there.
117.PP
118Patterns and actions all consist of three components: an
119.IR object ,
120a
121.IR verb ,
122and arguments.
123These are separated by white space on the line.
124The arguments may contain quoted strings and variable substitutions,
125described below, and in some cases contain multiple words.
126The object and verb are single words from a pre-defined set.
127.PP
128The object in a pattern is the name of an element of the message, such as
129.B src
130or
131.BR data ,
132or the special case
133.BR arg ,
134which refers to the argument component of the current rule.
135The object in an action is always the word
136.BR plumb .
137.PP
138The verbs in the pattern rules
139describe how the objects and arguments are to be interpreted.
140Within a rule set, the patterns are evaluated in sequence; if one fails,
141the rule set fails.
142Some verbs are predicates that check properties of the message; others rewrite
143components of the message and implicitly always succeed.
144Such rewritings are permanent, so rules that specify them should be placed after
145all pattern-matching rules in the rule set.
146.RS
147.TF delete
148.TP
149.B add
150The object must be
151.BR attr .
152Append the argument, which must be a sequence of
153.IB name = value
154pairs, to the list of attributes of the message.
155.TP
156.B delete
157The object must be
158.BR attr .
159If the message has an attribute whose name is the argument,
160delete it from the list of attributes of the message.
161(Even if the message does not, the rule matches the message.)
162.TP
163.B is
164If the text of the object is identical to the text of the argument,
165the rule matches.
166.TP
167.B isdir
168If the text of the object
169is the name of an existing directory, the rule matches and
170sets the variable
171.B $dir
172to that directory name.
173.TP
174.B isfile
175If the text of the object is the name of an existing file (not a directory),
176the rule matches and sets the variable
177.B $file
178to that file name.
179.TP
180.B matches
181If the entire text of the object matches the regular expression
182specified in the argument, the rule matches.
183This verb is described in more detail below.
184.TP
185.B set
186The value of the object is set to the value of the argument.
187.RE
188.PP
189The
190.B matches
191verb has special properties that enable the rules to select which portion of the
192data is to be sent to the destination.
193By default, a
194.B data
195.B matches
196rule requires that the entire text matches the regular expression.
197If, however, the message has an attribute named
198.BR click ,
199that reports that the message was produced by a mouse click within the
200text and that the regular expressions in the rule set should be used to
201identify what portion of the data the user intended.
202Typically, a program such as an editor will send a white-space delimited
203block of text containing the mouse click, using the value of the
204.B click
205attribute (a number starting from 0) to indicate where in the textual data the user pointed.
206.PP
207When the message has a
208.B click
209attribute, the
210.B data
211.B matches
212rules extract the longest leftmost match to the regular expression that contains or
213abuts the textual location identified by the
214.BR click .
215For a sequence of such rules within a given rule set, each regular expression, evaluated
216by this specification, must match the same subset of the data for the rule set to match
217the message.
218For example, here is a pair of patterns that identify a message whose data contains
219the name of an existing file with a conventional ending for an encoded picture file:
220.EX
221 data matches '[a-zA-Z0-9_\-./]+'
222 data matches '([a-zA-Z0-9_\-./]+)\.(jpe?g|gif|bit|ps|pdf)'
223.EE
224The first expression extracts the largest subset of the data around the click that contains
225file name characters; the second sees if it ends with, for example,
226.BR \&.jpeg .
227If only the second pattern were present, a piece of text
228.B horse.gift
229could be misinterpreted as an image file named
230.BR horse.gif .
231.PP
232If a
233.B click
234attribute is specified in a message, it will be deleted by the
235.B plumber
236before sending the message if the
237.B data
238.B matches
239rules expand the selection.
240.PP
241The action rules all have the object
242.BR plumb .
243There are only three verbs for action rules:
244.RS
245.TF client
246.TP
247.B to
248The argument is the name of the port to which the message will be sent.
249If the message has a destination specified, it must match the
250.B to
251port of the rule set or the entire rule set will be skipped.
252(This is the only rule that is evaluated out of order.)
253.TP
254.B client
255If no application has the port open, the arguments to a
256.B plumb
257.B start
258rule specify a shell program to run in response to the message.
259The message will be held, with the supposition that the program
260will eventually open the port to retrieve it.
261.TP
262.B start
263Like
264.BR client ,
265but the message is discarded.
266Only one
267.B start
268or
269.B client
270rule should be specified in a rule set.
271.RE
272.PP
273The arguments to all rules may contain quoted strings, exactly as in
274.IR rc (1).
275They may also contain simple string variables, identified by a leading dollar sign
276.BR $ .
277Variables may be set, between rule sets, by assignment statements in the style of
278.BR rc .
279Only one variable assignment may appear on a line.
280The
281.B plumber
282also maintains some built-in variables:
283.RS
284.TF $wdir
285.TP
286.B $0
287The text that matched the entire regular expression in a previous
288.B data
289.B matches
290rule.
291.BR $1 ,
292.BR $2 ,
293etc. refer to text matching the first, second, etc. parenthesized subexpression.
294.TP
295.B $attr
296The textual representation of the attributes of the message.
297.TP
298.B $data
299The contents of the data field of the message.
300.TP
301.B $dir
302The directory name resulting from a successful
303.B isdir
304rule.
305If no such rule has been applied, it is the string constructed
306syntactically by interpreting
307.B data
308as a file name in
309.BR wdir .
310.TP
311.B $dst
312The contents of the
313.B dst
314field of the message.
315.TP
316.B $file
317The file name resulting from a successful
318.B isfile
319rule.
320If no such rule has been applied, it is the string constructed
321syntactically by interpreting
322.B data
323as a file name in
324.BR wdir .
325.TP
326.B $type
327The contents of the
328.B type
329field of the message.
330.TP
331.B $src
332The contents of the
333.B src
334field of the message.
335.TP
336.B $wdir
337The contents of the
338.B wdir
339field of the message.
rsc4de03b52005-01-11 19:46:06 +0000340.TP
341.B $plan9
342The root directory of the Plan 9 tree
343(see
344.IR get9root (3)).
rsc058b0112005-01-03 06:40:20 +0000345.RE
346.SH EXAMPLE
347The following is a modest, representative file of plumbing rules.
348.EX
349# these are generally in order from most specific to least,
350# since first rule that fires wins.
351
352addr=':(#?[0-9]+)'
353protocol='(https?|ftp|file|gopher|mailto|news|nntp|telnet|wais)'
354domain='[a-zA-Z0-9_@]+([.:][a-zA-Z0-9_@]+)*/?[a-zA-Z0-9_?,%#~&/\e-]+'
355file='([:.][a-zA-Z0-9_?,%#~&/\e-]+)*'
356
357# image files go to page
358type is text
359data matches '[a-zA-Z0-9_\e-./]+'
360data matches '([a-zA-Z0-9_\e-./]+)\.(jpe?g|gif|bit)'
361arg isfile $0
362plumb to image
363plumb start page -w $file
364
365# URLs go to web browser
366type is text
367data matches $protocol://$domain$file
368plumb to web
369plumb start window webbrowser $0
370
371# existing files, possibly tagged by line number, go to edit/sam
372type is text
373data matches '([.a-zA-Z0-9_/\-]+[a-zA-Z0-9_/\e-])('$addr')?'
374arg isfile $1
375data set $file
376attr add addr=$3
377plumb to edit
378plumb start window sam $file
379
380# .h files are looked up in /sys/include and passed to edit/sam
381type is text
382data matches '([a-zA-Z0-9]+\e.h)('$addr')?'
383arg isfile /sys/include/$1
384data set $file
385attr add addr=$3
386plumb to edit
387plumb start window sam $file
388.EE
389.PP
390The following simple plumbing rules file is a good beginning set of rules.
391.EX
392# to update: cp /usr/$user/lib/plumbing /mnt/plumb/rules
393
394editor = acme
395# or editor = sam
396include basic
397.EE
398.SH FILES
rscc8b63422005-01-13 04:49:19 +0000399.TF $HOME/lib/plumbing
rsc058b0112005-01-03 06:40:20 +0000400.TP
rscc8b63422005-01-13 04:49:19 +0000401.B $HOME/lib/plumbing
rsc058b0112005-01-03 06:40:20 +0000402default rules file.
403.TP
rscc8b63422005-01-13 04:49:19 +0000404.B plumb
405service name for
rsc058b0112005-01-03 06:40:20 +0000406.IR plumber (4).
407.TP
rscc8b63422005-01-13 04:49:19 +0000408.B \*9/plumb
rsc058b0112005-01-03 06:40:20 +0000409directory for
410.B include
411files.
412.TP
rscc8b63422005-01-13 04:49:19 +0000413.B \*9/plumb/fileaddr
rsc058b0112005-01-03 06:40:20 +0000414public macro definitions.
415.TP
rscc8b63422005-01-13 04:49:19 +0000416.B \*9/plumb/basic
rsc058b0112005-01-03 06:40:20 +0000417basic rule set.
418.SH "SEE ALSO"
419.IR plumb (1),
420.IR plumb (3),
421.IR plumber (4),
422.IR regexp (7)