Charles.Forsyth20071011-11329612a0519 years agoHistory
560 lines · 13.4 KB · Text
RawDownload
.TH SH 2.SH NAMESh \- module interface to the shell.SH SYNOPSIS.EX.ps -1.vs -1include "sh.m";sh := load Sh Sh->PATH;Context, Listnode: import sh; system:           fn(drawctxt: ref Draw->Context, cmd: string): string;run:              fn(drawctxt: ref Draw->Context, argv: list of string): string;parse:            fn(s: string): (ref Cmd, string);cmd2string:       fn(c: ref Cmd): string;list2stringlist:  fn(nl: list of ref Listnode): list of string;stringlist2list:  fn(sl: list of string): list of ref Listnode; Context: adt {        new:            fn(drawcontext: ref Draw->Context): ref Context;        get:            fn(c: self ref Context,                           name: string): list of ref Listnode;        set:            fn(c: self ref Context,                           name: string,                           value: list of ref Listnode);        setlocal:       fn(c: self ref Context,                           name: string,                           value: list of ref Listnode);        envlist:        fn(c: self ref Context):			              list of (string, list of ref Listnode);        push, pop:      fn(c: self ref Context);        copy:           fn(c: self ref Context, copyenv: int): ref Context;        run:            fn(c: self ref Context,                           args: list of ref Listnode,                           last: int): string;        addmodule:      fn(c: self ref Context, name: string,                           mod: Shellbuiltin);        addbuiltin:     fn(c: self ref Context, name: string,                           mod: Shellbuiltin);        removebuiltin:  fn(c: self ref Context, name: string,                           mod: Shellbuiltin);        addsbuiltin:    fn(c: self ref Context, name: string,                           mod: Shellbuiltin);        removesbuiltin: fn(c: self ref Context, name: string,                           mod: Shellbuiltin);        fail:           fn(c: self ref Context, ename, msg: string);        options:        fn(c: self ref Context): int;        setoptions:     fn(c: self ref Context, flags, on: int): int;}; Listnode: adt {        cmd:    ref Cmd;        word:   string;}; Cmd: adt {        # private data}; Shellbuiltin: module {        initbuiltin:    fn(ctxt: ref Context, sh: Sh): string;        whatis:         fn(ctxt: ref Sh->Context, sh: Sh,                           name: string, wtype: int): string;       runbuiltin:     fn(ctxt: ref Context, sh: Sh,                           cmd: list of ref Listnode,                           last: int): string;        runsbuiltin:    fn(ctxt: ref Context, sh: Sh,                           cmd: list of ref Listnode): list of ref Listnode;        getself:        fn(): Shellbuiltin;}; .ps +1.vs +1.EE.SH DESCRIPTION.I Shis a command-line interpreter and a scripting language;it also presents a module interface to allow Limbomodules to access its functionality at a lower level.The.B Shmodule can be used in several different ways.At the simplest level, it can be run as a command-lineprogram; for details of this, see.IR sh (1).The simplest access at the Limbo level is throughthe.B systemfunction, which given a.I draw.B Context(see.IR draw-context (2))and a stringexecutes the.I shcommand contained in.I sand returns its result. It catches any exceptions raised by the command.Almost as simple is.BR exec ,which runs.I argvas a command, taking the first word as the command to beexecuted (it can be a braced block) and giving the rest as arguments,catching any exceptions raised..PPAlthough program arguments are passed to external programsas lists of strings, at the.B Shmodule level, an argument list is held as a.BR "list of ref Listnode" .A.B Listnodeholds either a simple string, or a braced blockthat has been parsed by the shell. Sometimes it can holdboth; in this case the string and the block both representthe same thing..B Parseconverts from a string to a.B Cmd(a braced block). It returns a tuple.RI ( cmd ,\  error )where.I cmdholds the parsed block,and.I erroris non-empty if an error has occurred doing so..B Cmd2stringperforms the opposite conversion; it returnsa string that when parsed will yield the same commandblock it was passed.The utility functions.B List2stringlistand.B stringlist2listconvert from and to a.B list of ref Listnodeto or from a.B list of stringrespectively..PPA.B Contextholds all the state information needed by a currently running.I shprocess; this adt holds current values of environment variablesand a list of currently loaded modules and builtin commands.It is specific to the process within which it was created.If it is desired to run.I shcommands in a newly spawned process, a new.B Contextmust be created, or a copy of an existing Context made (makingsure to synchronise access until the copy has been made)..TP 10.BI Context.new( drawcontext ).B Newcreates a new context..I Drawcontextrepresents the current graphics contextwithin which.I shcommands will be run(see.IR draw-context (2))..TP.IB ctxt .get(\fPname\fP).B Getretrieves the value of environment variable.I namefrom.IR ctxt .It is retrieved from the innermost scope in whicha value for.I namehas been set..TP.IB ctxt .set(\fPname\fP,\ \fPvalue\fP).B Setsets the value of environment variable.I namein.IR ctxtto.IR value .It is set in the innermost scope in which a valuefor.I namehas been set, or the outermost level if it hasnot been set..TP.IB ctxt .setlocal(\fPname\fP,\ \fPvalue\fP)Similar to.B set()except that the value is set in the innermost scopethat has been pushed..TP.IB ctxt .envlist().B Envlistretrieves the list of all the environment variablescurrently in scope, and their values.It returns a list of.RI ( name ,\  value )tuples, where.I nameis the name of the variable and.I valueis its value..TP.IB ctxt .push().B Pushcreates a new innermost environment variable scope..TP.IB ctxt .pop().B Popdiscards the current innermost scope, losing thevalues of all variables that have been defined there.It is an error to.B popa context that has not been pushed.Care must be taken to ensure that a.B pushis always matched by a.BR pop.In particular, exceptions should be caught,the context popped, and the exception re-raised..TP.IB ctxt .copy(\fPcopyenv\fP)The shell's.B Contextis associated with a particular process;.B copyreturns a copy of.I ctxtassociated with the current process. If.I copyenvis non-zero, the whole environment will be copied - thisshould be set if the new process is to run asynchronously - i.e.if there is a chance that there might be two processes accessing thecontext in parallel. It is an error to copy a context if notwithin a new process..TP.IB ctxt .run(\fPargs\fP,\ \fPlast\fP).B Runexecutes a.I shcommand..I Lastshould be non-zero if this is the last timethat.B runwill be called, so.I shdoes not have to spawn a new process in orderto hide file redirection side-effects..TP.IB ctxt .addmodule(\fPname\fP,\ \fPmod\fP).B Addmoduleadds the.B Shellbuiltinmodule.I modto its list of builtin modules.The module will be initialised as described in``Builtin modules'', below..TP.IB ctxt .addbuiltin(\fPname\fP,\ \fPmod\fP).B Addbuiltinmay be called by a module that has previouslybeen loaded by.B addmoduleor by the.B load.I shcommand to add a new builtin commandto the shell. Any subsequent invocation of.I namewithin.I ctxtwill result in a call of.B runbuiltin()to.IR mod .Any attempt to redefine the command.RB `` builtin ''will be ignored..TP.IB ctxt .removebuiltin(\fPname\fP,\ \fPmod\fP).B Removebuiltinremoves.I namefrom the list of builtin commands in.IR ctxt .If.I namehad not previously been defined by.IR mod ,or had subsequently been replaced, thenthis function does nothing..TP.IB ctxt .addsbuiltin(\fPname\fP,\ \fPmod\fP).B Addsbuiltinmay be called by a module that has previouslybeen loaded by.B addmoduleor by the.B load.I shcommand to add a new builtin substitution operatorto the shell.Any subsequent invocation of.BI ${ name }within.I ctxtwill result in a call of.B runsbuiltin()to.IR mod ..TP.IB ctxt .removesbuiltin(\fPname\fP,\ \fPmod\fP).B Removesbuiltinremoves.I namefrom the list of builtin substitution operators in.IR ctxt .If.I namehad not previously been defined by.IR mod ,or had subsequently been replaced, thenthis function does nothing..TP.IB ctxt .fail(\fPename\fP,\ \fPmsg\fP).B Failprints.I msgto the standard error if message printingis currently enabled, and raisesthe exception.BI fail: ename\f1.\fP.TP.IB ctxt .options().B Optionsreturns a bitmask of the options currently enabled in.IR ctxt .The bits are defined by constants declared within.BR Context .They include:.RS.TP.IB ctxt .INTERACTIVE.I Shis currently being run from an interactive command-line..TP.IB ctxt .VERBOSEMessage printing is currently enabled..TP.IB ctxt .EXECPRINTCommands are printed to standard erroras they are executed..TP.IB ctxt .ERROREXITAn exception will be raised when the firstsimple command returns an error status..PPOptions are defined in the innermost scopeof.I ctxtand will be lost when it is.BR pop ped..RE.TP.IB ctxt .setoptions(\fPflags\fP,\ \fPon\fP).B Setoptionssets the specified.I flagswithin.IR ctxt ..I Flagsis a bitmask of options, as described in.BR options ,above. If.I onis non-zero, the specified bits will be set;otherwise they will be reset..B Setoptionsreturns the previously set options bitmask..SS Builtin modules.B Shellbuiltinspecifies the interface to a loadable.I shbuiltin module. Any Limbo module.I modadhering to thisinterface may be loaded into the shell..TP 10.IB mod ->initbuiltin(\fPctxt\fP,\ \fPsh\fP).B Initbuiltinis called when.I shloads.I modeither via the.B loadcommand, or via the.B loadmodule()function..I Ctxtis the context within which the builtin has beenloaded, and.I shis the.B Shmodule itself. When.B initbuiltinis called,.I modis expected to call.IB ctxt .addbuiltinand.IB ctxt .addsbuiltinto define any builtin commands and builtin substitutionoperators that it wants. If an error occurs oninitialisation,.B initbuiltinshould return a non-nil value; this will cause the load to fail..TP.IB mod ->runbuiltin(\fPctxt\fP,\ \fPsh\fP,\ \fPcmd\fP,\ \fPlast\fP).B Runbuiltinis invoked when.I shexecutes a command that has previously beendefined as a builtin command by.IR mod ..I Ctxtis the current execution context (which may not bethe original context passed to.BR initbuiltin() ),.I shis the running.B Shmodule, and.I cmdis the command to be executed..I Lastis true if this is the last command to be executedin the current process; it can be passed to.IB ctxt .run()as appropriate.The name of the command can be found in.BR "(hd cmd).word" ..B Runbuiltinreturns its exit status; by convention thisis the exit status of the last command executed.A non-nil exit status is usually treated as false.By convention, if an invalid set of arguments arepassed to a builtin command, a.B usageexception is raised by calling .IB ctxt .failwith.B "usage"and an explanatory usage message as arguments..TP.IB mod ->runsbuiltin(\fPctxt\fP,\ \fPsh\fP,\ \fPcmd\fP)Similar to.BR runbuiltin ,.B runsbuiltinis called when.I shencounters a builtin substitution operatorthat has previously been defined by.IR mod .It returns the list of values that will besubstituted in place of the operator..TP.IB mod ->getself().B Getselfshould return the.B Shellbuiltinmodule handle for.IR mod ,usually obtained by invoking.BR "load $self" .N.B. it is important that the value returnedby.B getselfis the same as that passed to.B addbuiltinor.BR addsbuiltin .As the Limbo.B loadoperator returns a different value each time,the value to be returned by.B getself()should be initialised once,during the call to 

Lines 1–500 of 560.