1429 lines · 52.1 KB · Text
RawDownload
.HTML "Plumbing and Other Utilities.TLPlumbing and Other Utilities.AURob Pike.AI.MH.AB.LPPlumbing is a new mechanism for inter-process communication in Plan 9,specifically the passing of messages between interactive programs as part ofthe user interface.Although plumbing shares some properties with familiar notionssuch as cut and paste,it offers a more general data exchange mechanism without imposinga particular user interface..LPThe core of the plumbing system is a program called the.I plumber ,which handles all messages and dispatches and reformats themaccording to configuration rules written in a special-purpose language.This approach allows the contents and context of a piece of data to define howit is handled.Unlike with drag and drop or cut and paste,the user doesn't need to deliver the data;the contents of a plumbing message, as interpreted by the plumbing rules,determine its destination..LPThe plumber has an unusual architecture: it is a language-driven file server.This design has distinct advantages.It makes plumbing easy to add to an existing, Unix-like command environment;it guarantees uniform handling of inter-application messages;it off-loads from those applications most of the work of extracting and dispatching messages;and it works transparently across a network..AE.SHIntroduction.LPData moves from program to program in myriad ways.Command-line arguments,shell pipe lines,cut and paste,drag and drop, and other user interface techniques all provide some formof interprocess communication.Then there are tricks associated with special domains,such as HTML hyperlinks or the heuristics mail readersuse to highlight URLs embedded in mail messages.Some systems provide implicit ways to automate the attachment of program to data\(emthebest known examples are probably the resource forks in MacOS and thefile name extension `associations' in Microsoft Windows\(embut in practicehumans must too often carry their data from program to program..LPWhy should a human do the work?Usually there is one obvious thing to do with a piece of data,and the data itself suggests what this is.Resource forks and associations speak to this issue directly, but statically and narrowly and withlittle opportunity to control the behavior.Mechanisms with more generality,such as cut and paste or drag and drop, demand too much manipulation bythe user and are (therefore) too error-prone..LPWe want a system that, given a piece of data,hands it to the appropriate application by default with little or no human intervention,while still permitting the user to override the defaults if desired..LPThe plumbing system is an attempt to address some of these issues in a single,coherent, central way.It provides a mechanism forformatting and sending arbitrary messages between applications,typically interactive programs such as text editors, web browsers, and the window system,under the control of a central message-handling server called the.I plumber .Interactive programs provide application-specific connections to the plumber,triggering with minimal user action the transfer of data or control to other programs.The result is similar to a hypertext system in which all the links are implicit,extracted automatically by examining the data and the user's actions.It obviatescut and paste and other such hand-driven interprocess communication mechanisms.Plumbing delivers the goods to the right place automatically..SHOverview.LPThe plumber is implemented as a Plan 9 file server [Pike93];programs send messages by writing them to the plumber's file.CW /mnt/plumb/send ,and receive messages by reading them from.I ports ,which are other plumber files in.CW /mnt/plumb .For example,.CW /mnt/plumb/editis by convention the file from which a text editor reads messages requesting it toopen and display a file for editing.(See Figure 1.).if h .B1 10 60.KF.PSdownP1: ellipse "ProgramA"moveP2: ellipse "ProgramB"moveP3: ellipse "ProgramC"rightINVIS: box wid 1.3 invis at P2.eSEND: arrow from INVIS.e "\f(CWsend \fP" ""arrow -> right 0.2 from P1.e; spline -> right 0.2 then down 1 to SEND.warrow -> right 0.2 from P2.e; arrow -> to SEND.warrow -> right 0.2 from P3.e; spline -> right 0.2 then up 1 to SEND.wrightPL: box height 1 "plumber" with .w at SEND.eA3: arrow 0.8 -> "\f(CWimage\fP" ""; arrow ->O3: ellipse "Viewer"O2: ellipse "Browser" with .s at O3.n + (0, 0.1)O1: ellipse "Editor" with .s at O2.n + (0, 0.1)O4: ellipse "Faces" with .n at O3.s + (0, -0.1)O5: ellipse "..." with .n at O4.s + (0, -0.1)rightA1: arrow 0.8 -> "\f(CWedit\fP" "" from PL.e + (0, .4); spline -> right 0.15 then up 0.7 then to O1.wrightA2: arrow 0.8 -> "\f(CWweb\fP" "" from PL.e + (0, .2);  spline -> right 0.3 then up 0.3 then to O2.wrightA4: arrow 0.8 -> "\f(CWnewmail\fP" "" from PL.e + (0, -.2);  spline -> right 0.3 then down 0.3 then to O4.wrightA5: arrow 0.8 -> "\f(CW...\fP" "" from PL.e + (0, -.4);  spline -> right 0.15 then down 0.7 then to O5.w.PE.IP.ps -1Figure 1. The plumber controls the flow of messages between applications.Programs write to the file.CW sendand receive on `ports' of various names representing services such as.CW editor.CW web .Although the figure doesn't illustrate it, some programs may both send and receive messages,and some ports are read by multiple applications..sp.KE.if h .B2.LPThe plumber takes messages from the.CW sendfile and interprets their contents using rules defined bya special-purpose pattern-action language.The language specifies any rewriting of the message that is to be done by the plumberand defines how to dispose of a message, such as by sending it to a port orstarting a new process to handle it..LPThe behavior is best described by example.Imagine that the user has, in a terminal emulator window,just run a compilation that has failed:.P1% makecc -c rmstar.crmstar.c:32: syntax error\&....P2The user points the typing cursor somewhere in the string.CW rmstar.c:32:and executes the.CW plumbmenu entry.This causes the terminal emulator to format a plumbing messagecontaining the entire string surrounding the cursor,.CW rmstar:32: ,and to write it to.CW /mnt/plumb/send .The plumber receives this message and compares it sequentially to the variouspatterns in its configuration.Eventually, it will find one that breaks the string into pieces,.CW rmstar.c ,a colon,.CW 32 ,and the final colon.Other associated patterns verify that.CW rmstar.cis a file in the current directory of the program generatingthe message, and that.CW 32looks like a line number within it.The plumber rewrites the message,setting the data to the string.CW rmstar.cand attaching an indication that.CW 32is a line number to display.Finally, it sends the resulting message to the.CW editport.The text editor picks up the message, opens.CW rmstar.c(if it's not already open) and highlights line 32, the location of the syntax error..LPFrom the user's point of view, this process is simple: the error message appears,it is `plumbed', and the editor jumps to the problem..LPOf course, there are many different ways to cause compiler messages topop up the source of an error,but the design of the plumber addresses more general issues than the specificgoal of shortening the compile/debug/edit cycle.It facilitates the general exchange of data among programs, interactive or otherwise,throughout the environment, and itsarchitecture\(ema central, language-driven file server\(emalthoughunusual, has distinct advantages.It makes plumbing easy to add to an existing, Unix-like command environment;it guarantees uniform handling of inter-application messages;it off-loads from those applications most of the work of extracting and dispatching messages;and it works transparently and effortlessly across a network..LPThis paper is organized bottom-up, beginning with the format of the messagesand proceeding through the plumbing language, the handling of messages,and the interactive user interface.The last sections discuss the implications of the designand compare the plumbing system to other environments thatprovide similar services..SHFormat of messages.LPSince the language that controls the plumber is defined in terms of thecontents of plumbing messages, we begin by describing their layout..LPPlumbing messages have a fixed-format textualheader followed by a free-format data section.The header consists of six lines of text, in set order,each specifying a property of the message.Any line may be blank except the last, which is the length of the data portion of themessage, as a decimal string.The lines are, in order:.IPThe source application, the name of the program generating the message..IPThe destination port, the name of the port to which the messages should be sent..IPThe working directory in which the message was generated..IPThe type of the data, analogous to a MIME type, such as.CW textor.CW image/gif ..IPAttributes of the message, given as blank-separated.I name\f(CW=\fPvaluepairs.The values may be quoted to protectblanks or quotes; values may not contain newlines..IPThe length of the data section, in bytes..LPHere is a sample message, one that (conventionally) tells the editor to open the file.CW /usr/rob/src/mem.cand display line27 within it:.P1plumbtestedit/usr/rob/srctextaddr=275mem.c.P2Because in general it need not be text, the data section of the message has no terminating newline..LPA library interface simplifies the processing of messages by translating themto and from a data structure,.CW Plumbmsg ,defined like this:.P1.ta 4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4ntypedef struct Plumbattr Plumbattr;typedef struct Plumbmsg  Plumbmsg; struct Plumbmsg{	char			*src;		/* source application */	char			*dst;		/* destination port */	char			*wdir;	/* working directory */	char			*type;	/* type of data */	Plumbattr	*attr;	/* attribute list */	int			ndata;	/* #bytes of data */	char			*data;}; struct Plumbattr{	char			*name;	char			*value;	Plumbattr	*next;};.P2The library also includes routines to send a message, receive a message,manipulate the attribute list, and so on..SHThe Language.LPAn instance of the plumber runs for each user on each terminal or workstation.Itbegins by reading its rules from the file.CW lib/plumbingin the user's home directory,which in turn may use.CW includestatements to interpolate macro definitions andrules from standard plumbing rule libraries stored in.CW /sys/lib/plumb ..LPThe rules control the processing of messages.They are written ina pattern-action language comprising a sequence of blank-line-separated.I rule.I sets ,each of which contains one or more.I patternsfollowed by one or more.I actions .Each incoming message is compared against the rule sets in order.If all the patterns within a rule set succeed,one of the associated actions is taken and processing completes..LPThe syntax of the language is straightforward.Each rule (pattern or action) has three components, separated by white space:an.I object ,a.I verb ,and optional.I arguments .The objectidentifies a part of the message, such asthe source application.CW src ), (or the dataportion of the message.CW data ), (or the rule's own arguments.CW arg ); (or it is the keyword.CW plumb ,which introduces an action.The verb specifies an operation to perform on the object, such as the word.CW is ' `to require precise equality between the object and the argument, or.CW isdir ' `to require that the object be the name of a directory..LPFor instance, this rule set sends messages containing the names of filesending in.CW .gif ,.CW .jpg ,etc. to a program,.CW page ,to display them; it is analogous to a Windows association rule:.P1# image files go to pagetype is textdata matches '[a-zA-Z0-9_\e-./]+'data matches '([a-zA-Z0-9_\e-./]+)\e.(jpe?g|gif|bit|tiff|ppm)'arg isfile $0plumb to imageplumb client page -wi.P2(Lines beginning with.CW #are commentary.)Consider how this rule handles the following message, annotated down the left column for clarity:.P1.ta 10n\f2src\fP	plumbtest\f2dst\fP\f2wdir\fP	/usr/rob/pics\f2type\fP	text\f2attr\fP\f2ndata\fP	9\f2data\fP	horse.gif.P2The.CW isverb specifies a precise match, and the.CW typefield of the message is the string.CW text ,so the first pattern succeeds.The.CW matchesverb invokes a regular expression pattern match of the object (here.CW data )against the argument pattern.Both.CW matchespatterns in this rule set will succeed, and in the process set the variables.CW $0to the matched string,.CW $1to the first parenthesized submatch, and so on (analogous to.CW & ,.CW \e1 ,etc. in.CW ed 'sregular expressions).The pattern.CW arg.CW isfile.CW $0verifies that the named file,.CW horse.gif ,is an actual file in the directory.CW /usr/rob/pics .If all the patterns succeed, one of the actions will be executed..LPThere are two actions in this rule set.The.CW plumb.CW torule specifies.CW imageas the destination port of the message.By convention, the plumber mounts its services in the directory.CW /mnt/plumb ,so in this case if the file.CW /mnt/plumb/imagehas been opened, the message will be made available to the program reading from it.Note that the message does not name a port, but the rule set that matchesthe message does, and that is sufficient to dispatch the message.If on the other hand a message matches no rule but has an explicit port mentioned,that too is sufficient..LPIf no client has opened the.CW imageport,that is, if the program.CW pageis not already running, the.CW plumb.CW clientaction gives the execution script to start the applicationand send the message on its way; the.CW -wiarguments tell.CW pageto create a window and to receive its initial arguments from the plumbing port.The process by which the plumber starts a program is described in more detail in the next section..LPIt may seem odd that there are two.CW matchesrules in this example.The reason is related to the way the plumber can use the rules themselvesto refine the.I datain the message, somewhat in the manner of Structural Regular Expressions [Pike87a].For example, consider what happens if the cursor is at the last character of.P1% make nightmare>horse.gif.P2and the user asks to plumb what the cursor is pointing at.The program creating the plumbingmessage\(emin this case the terminal emulator running the window\(emcan send theentire white-space-delimited string.CW nightmare>horse.gifor even the entire line, and the combination of.CW matchesrules can determine that the user was referring to the string.CW horse.gif .The user could of course select the entire string.CW horse.gif ,but it's more convenient just to point in the general location and let the machinefigure out what should be done.The process is as follows..LPThe application generating the message adds a special attribute to the message, named.CW click ,whose numerical value is the offset of the cursor\(emthe selection point\(emwithin the data string.This attribute tells the plumber two things:first, that the regular expressions in.CW matchesrules should be used to identify the relevant data;and second, approximately where the relevant data lies.The plumber will then use the first.CW matchespattern to identify the longest leftmost match that touches the cursor, which will extract the string.CW horse.gif ,and the second pattern will then verify that that names a picture file.The rule set succeeds and the data is winnowed to the matching substringbefore being sent to its destination..LPEach.CW matchespattern within a given rule set must match the same portion of the string, whichguarantees that the rule set fails to match a string for which thesecond pattern matches only a portion.For instance, our example rule set should not execute if the data is the string.CW horse.gift ,and although the first pattern will match.CW horse.gift ,the second will match only.CW horse.gifand the rule set will fail..LPThe same approach of multiple

Lines 1–500 of 1429.