⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pxin3.n

📁 早期freebsd实现
💻 N
字号:
.\" Copyright (c) 1979 The Regents of the University of California..\" All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"	@(#)pxin3.n	5.2 (Berkeley) 4/17/91.\".if !\n(xx .so tmac.p.ta 8n 16n 24n.nr H1 2.if n .ND.NHInput/output.NH 2The files structure.PPEach file in the Pascal environment is represented by a pointerto a.I filesstructure in the heap.At the location addressed by the pointer is the elementin the file's window variable.Behind this window variable is information about the file,at the following offsets:.so table3.1.n.PPHere.SM FBUFis a pointer to the system FILE block for the file.The standard system I/O library isused that provides block buffered input/output,with 1024 characters normally transferred at each read or write..PPThe files in thePascal environment,are all linked together on a single file chain through the.SM FCHAINlinks.For each file the.SM FLEVpointer gives its associated file variable.These are used to free files at block exit as described in section 3.3below..PPTheFNAMEandPFNAMEgive the associatedfile name for the file and the name to be used when printingerror diagnostics respectively.Although these names are usually the same,.I inputand.I outputusually have no associatedfile name so the distinction is necessary..PPTheFUNITword contains a set of flags.whose representations are:.TScenter;l l l.EOF	0x0100	At end-of-fileEOLN	0x0200	At end-of-line (text files only)SYNC	0x0400	File window is out of syncTEMP	0x0800	File is temporaryFREAD	0x1000	File is open for readingFWRITE	0x2000	File is open for writingFTEXT	0x4000	File is a text file; process EOLNFDEF	0x8000	File structure created, but file not opened.TE.PPTheEOFandEOLNbits here reflect the associated built-in function values.TEMPspecifies that the file has a generated temporary name and thatit should therefore be removed when its block exits.FREADandFWRITEspecify that.I resetand.I rewriterespectively have been done on the file so thatinput or output operations can be done.FTEXTspecifies the file is a text file so thatEOLNprocessing should be done,with newline characters turned into blanks, etc..PPTheSYNCbit,when true,specifies that there is no usable image in the file buffer window.As discussed in the.I "Berkeley Pascal User's Manual,"the interactive environment necessitates having``input^'' undefined at the beginningof execution so that a program may print a promptbefore the user is required to type input.TheSYNCbit implements this.When it is set,it specifies that the element in the windowmust be updated before it can be used.This is never done until necessary..NH 2Initialization of files.PPAll the variables in the Pascal runtime environment are cleared to zero onblock entry.This is necessary for simple processing of files.If a file is unused, its pointer will be.B nil.All references to an inactive file are thus references through a.B nilpointer.If the Pascal system did not clear storage to zero before executionit would not be possible to detect inactive files in this simple way;it would probably be necessary to generate (possibly complicated)code to initializeeach file on block entry..PPWhen a file is first mentioned in a.I resetor.I rewritecall,a buffer of the form described above is associated with it,and the necessary information about the file is placed in thisbuffer.The file is also linked into the active file chain.This chain is kept sorted by block mark address, theFLEVentries..NH 2Block exit.PPWhen block exit occurs the interpreter must free the files that are inuse in the blockand their associated buffers.This is simple and efficient because the files in the active file chain are sorted by increasing block mark address.This means that the files for the current block will be at the frontof the chain.For each file that is no longer accessiblethe interpreter first flushes the files bufferif it is an output file.The interpreter then returns the file buffer and the files structure and windowto the free space in the heap and removes the file from the active file chain..NH 2Flushing.PPFlushing all the file buffers at abnormal termination,or on a call to the procedure.I flushor.I messageis done by flushing each file on the file chain that has theFWRITEbit set in its flags word..NH 2The active file.PPFor input-output,.I pxmaintains a notion of an active file.Each operation that references a file makes the fileit will be using the active file and then does its operation.A subtle point here is that one may do a procedure call to.I writethat involves a call to a function that references another file,thereby destroying the active file set up before the.I write.Thus the active file is saved at block entryin the block mark and restored at block exit.\*(Dg.FS\*(dg\ It would probably be better to dispense with the notion ofactive file and use another mechanism that did not involve extraoverhead on each procedure and function call..FE.NH 2File operations.PPFiles in Pascal can be used in two distinct ways:as the object of.I read,.I write,.I get,and.I putcalls, or indirectly as though they were pointers.The second use as pointers must be carefulnot to destroy the active file in a reference such as.LSwrite(output, input\(ua).LEor the system would incorrectly write on the input device..PPThe fundamental operator related to the use of a file is.SM FNIL.This takes the file variable, as a pointer,insures that the pointer is not.B nil,and also that a usable image is in the file window,by forcing the.SM SYNCbit to be cleared..PPA simple example that demonstrates the use of the file operatorsis given by.LSwriteln(f).LEthat produces.DS.mD.TSlp-2w(8) l.RV:\fIl	f\fPUNITWRITLN.TE.DE.NH 2Read operations.SHGET.IPAdvance the active file to the next input element..SHFNIL.IPA file pointer is on the stack. Insure that the associated file is activeand that the file is synced so that there is input available in the window..SHREAD*.IPIf the file is a text file, read a block of textand convert it to the internal type of the specifiedoperand. If the file is not a text file then do an unformatted read of the next record.The procedure.SM READLNreads upto and including the next end of line character..SHREADE A.IPThe operator.SM READEreads a string name of an enumerated type and converts itto its internal value..SM READE takes a pointer to a data structure as shown in figure 3.2..so fig3.2.nSee the description of.SM NAMin the next section for an example..NH 2Write operations.SHPUT.IPOutput the element in the active file window..SHWRITEF s.IPThe argument(s) on the stack are outputby the.I fprintfstandard.SM I/Olibrary routine.The sub-opcode .I sspecifies the numberof longword arguments on the stack..SHWRITEC.IPThe character on the top of the stack is outputwithout formatting. Formatted characters must be output with .SM WRITEF ..SHWRITES.IPThe string specified by the pointer on the top of the stack is outputby the.I fwritestandard.SM I/Olibrary routine.All characters including nulls are printed..SHWRITLN.IPA linefeed is output to the active file.The line-count for the file isincremented and checked against the line limit..SHPAGE.IPA formfeed is output to the active file..SHNAM A.IPThe value on the top of the stack is converted to a pointerto an enumerated type string name.The address .SM Apoints to an enumerated type structure identicalto that used by.SM READE .An error is raised if the value is out of range.The form of this structure for the predefined type.B booleanis shown in figure 3.3..so fig3.3.nThe code for .SM NAM is.DS.mD_NAM:	\fBincl\fR	lc	\fBaddl3\fR	(lc)+,ap,r6	#r6 points to scalar name list	\fBmovl\fR	(sp)+,r3	#r3 has data value	\fBcmpw\fR	r3,(r6)+	#check value out of bounds	\fBbgequ\fR	enamrng	\fBmovzwl\fR	(r6)[r3],r4	#r4 has string index	\fBpushab\fR	(r6)[r4]	#push string pointer	\fBjmp\fR	(loop)enamrng:	\fBmovw\fR	$ENAMRNG,_perrno	\fBjbr\fR	error.DEThe address of the table is calculated by adding the base addressof the interpreter code,.I apto the offset pointed to by.I lc .The first word of the table gives the number of records andprovides a range check of the data to be output.The pointer is then calculated as.DS.mDtblbase = ap + A;size = *tblbase++;return(tblbase + tblbase[value]);.DE.SHMAX s,w.IPThe sub-opcode.I sis subtracted from the integer on the top of the stack.The maximum of the result and the second argument,.I w ,replaces the value on the top of the stack.This function verifies that variable specifiedwidth arguments are non-negative, and meet certain minimum widthrequirements..SHMIN s.IPThe minimum of the value on the top of the stackand the sub-opcode replaces the value on the topof the stack..sp 1.LPThe uses of files and the file operations are summarizedin an example which outputs a real variable (r) with a variablewidth field (i)..LSwriteln('r =',r:i,' ',true);.LEthat generates the code.DS.mD.TSlp-2w(8) l.UNITOUTFILECON14:1CON14:3LVCON:4	"r ="WRITESRV8\fI:l	r\fPRV4\fI:l	i\fPMAX:8	1 RV4\fI:l	i\fPMAX:1	1 LVCON:8	" %*.*E"FILEWRITEF:6CONC4	\' \'WRITECCON14:1NAM	\fIbool\fP LVCON:4	"%s"FILEWRITEF:3WRITLN.TE.DE.PPHere the operator.SM UNITOUTis an abbreviated form of the operator.SM UNITthat is used when the file to be made active is.I output .A file descriptor, record count, string size, and a pointerto the constant string ``r ='' are pushed and then output by.SM WRITES .Next the value of .I ris pushed on the stackand the precision size is calculated by takingseven less than the width, but not less than one.This is followed by the width that is reduced byone to leave space for the required leading blank.If the width is too narrow, itis expanded by.I fprintf .A pointer to the format string is pushed followedby a file descriptor and the operator.SM WRITEFthat prints out.I r .The value of six on .SM WRITEFcomes from two longs for.I rand a long each for the precision, width, format string pointer,and file descriptor.The operator.SM CONC4pushes the.I blankcharacter onto a long on the stack that is then printed out by.SM WRITEC .The internal representation for.I trueis pushed as a long onto the stack and isthen replaced by a pointer to the string ``true''by the operator.SM NAMusing the table.I boolfor conversion.This string is output by the operator.SM WRITEFusing the format string ``%s''.Finally the operator.SM WRITLNappends a newline to the file..NH 2File activation and status operations.SHUNIT*.IPThe file pointed to by the file pointer on the topof the stack is converted to be the active file.The opcodes.SM UNITINPand.SM UNITOUTimply standard input and output respectivelyinstead of explicitly pushing their file pointers..SHFILE.IPThe standard .SM I/Olibrary file descriptor associated with the active file is pushed onto the stack..SHEOF.IPThe file pointed to by the file pointer on the topof the stack is checked for end of file. A booleanis returned with .I trueindicating the end of file condition..SHEOLN.IPThe file pointed to by the file pointer on the topof the stack is checked for end of line. A booleanis returned with.I trueindicating the end of line condition.Note that only text files can check for end of line..NH 2File housekeeping operations.SHDEFNAME.IPFour data items are passed on the stack;the size of the data type associated with the file,the maximum size of the file name,a pointer to the file name,and a pointer to the file variable.A file record is created with the specified window sizeand the file variable set to point to it.The file is marked as defined but not opened.This allows.B programstatement association of file names with file variablesbefore their use by a .SM RESETor a.SM REWRITE ..SHBUFF s.IPThe sub-opcode is placed in the external variable.I _bufoptto specify the amount of I/O buffering that is desired.The current options are:.DS0 \- character at a time buffering1 \- line at a time buffering2 \- block buffering.DEThe default value is 1..SHRESET.brREWRITE.IPFour data items are passed on the stack;the size of the data type associated with the file,the maximum size of the name (possibly zero),a pointer to the file name (possibly null),and a pointer to the file variable.If the file has never existed it is created as in .SM DEFNAME .If no file name is specified and no previous name exists(for example one created by.SM DEFNAME) then a system temporary name is created..SM RESETthen opens the file for input, while.SM REWRITEopens the file for output..sp 1.PPThe three remaining file operations are.SM FLUSH that flushes the active file,.SM REMOVEthat takes the pointer to a file name and removes thespecified file, and.SM MESSAGEthat flushes all the output files and sets the standard error file to be the active file.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -