2
0
peter 27 жил өмнө
parent
commit
5a638754f4

+ 23 - 14
rtl/dos/go32v1/system.pp

@@ -442,6 +442,25 @@ begin
    do_seekend(filerec(f).handle);
    do_seekend(filerec(f).handle);
 end;
 end;
 
 
+
+function do_isdevice(handle : longint):boolean;assembler;
+asm
+        movl    $0x4400,%eax
+        movl    handle,%ebx
+        pushl   %ebp
+        int     $0x21
+        popl    %ebp
+        jnc     .LDOSSEEK1
+        movw    %ax,inoutres
+	xorl	%edx,%edx
+.LDOSSEEK1:
+	movl	%edx,%eax
+	shrl	$7,%eax
+	andl	$1,%eax
+end;
+
+
+
 {*****************************************************************************
 {*****************************************************************************
                            UnTyped File Handling
                            UnTyped File Handling
 *****************************************************************************}
 *****************************************************************************}
@@ -555,20 +574,7 @@ end;
                          SystemUnit Initialization
                          SystemUnit Initialization
 *****************************************************************************}
 *****************************************************************************}
 
 
-procedure OpenStdIO(var f:text;mode:word;hdl:longint);
-begin
-  Assign(f,'');
-  TextRec(f).Handle:=hdl;
-  TextRec(f).Mode:=mode;
-  TextRec(f).InOutFunc:=@FileInOutFunc;
-  TextRec(f).FlushFunc:=@FileInOutFunc;
-  TextRec(f).Closefunc:=@fileclosefunc;
-end;
-
-
 Begin
 Begin
-{ Initialize ExitProc }
-  ExitProc:=Nil;
 { to test stack depth }
 { to test stack depth }
   loweststack:=maxlongint;
   loweststack:=maxlongint;
 { Setup heap }
 { Setup heap }
@@ -582,7 +588,10 @@ Begin
 End.
 End.
 {
 {
   $Log$
   $Log$
-  Revision 1.4  1998-05-31 14:18:19  peter
+  Revision 1.5  1998-07-01 15:29:56  peter
+    * better readln/writeln
+
+  Revision 1.4  1998/05/31 14:18:19  peter
     * force att or direct assembling
     * force att or direct assembling
     * cleanup of some files
     * cleanup of some files
 
 

+ 67 - 64
rtl/dos/go32v2/system.pp

@@ -168,7 +168,7 @@ _is_not_lowest:
 {$endif SYSTEMDEBUG}
 {$endif SYSTEMDEBUG}
         movl    __stkbottom,%ebx
         movl    __stkbottom,%ebx
         cmpl    %eax,%ebx
         cmpl    %eax,%ebx
-		jae     __short_on_stack
+        jae     __short_on_stack
         popl    %ebx
         popl    %ebx
         popl    %eax
         popl    %eax
         leave
         leave
@@ -244,7 +244,7 @@ end;
               movw dseg,%ax
               movw dseg,%ax
               movw %ax,%es
               movw %ax,%es
               movw sseg,%ax
               movw sseg,%ax
-			  movw %ax,%ds
+              movw %ax,%ds
               movl %ecx,%eax
               movl %ecx,%eax
               shrl $2,%ecx
               shrl $2,%ecx
               rep
               rep
@@ -282,7 +282,7 @@ end;
               rep
               rep
               movsb
               movsb
               incl %esi
               incl %esi
-			  incl %edi
+              incl %edi
            .LSEG_MOVE1:
            .LSEG_MOVE1:
               subl $4,%esi
               subl $4,%esi
               subl $4,%edi
               subl $4,%edi
@@ -316,11 +316,11 @@ end;
 procedure setup_arguments;
 procedure setup_arguments;
 type  arrayword = array [0..0] of word;
 type  arrayword = array [0..0] of word;
 var psp : word;
 var psp : word;
-	i,j : byte;
-	quote : char;
-	proxy_s : string[7];
-	tempargv : ppchar;
-	al,proxy_argc,proxy_seg,proxy_ofs,lin : longint;
+    i,j : byte;
+    quote : char;
+    proxy_s : string[7];
+    tempargv : ppchar;
+    al,proxy_argc,proxy_seg,proxy_ofs,lin : longint;
     largs : array[0..127] of pchar;
     largs : array[0..127] of pchar;
     rm_argv : ^arrayword;
     rm_argv : ^arrayword;
 begin
 begin
@@ -342,7 +342,7 @@ for i:=1 to length(doscmd) do
     quote := #0;
     quote := #0;
     doscmd[i] := #0;
     doscmd[i] := #0;
     largs[argc]:=@doscmd[j];
     largs[argc]:=@doscmd[j];
-	inc(argc);
+    inc(argc);
     j := i+1;
     j := i+1;
     end else
     end else
   if (quote = #0) and ((doscmd[i] = '''') or (doscmd[i]='"')) then
   if (quote = #0) and ((doscmd[i] = '''') or (doscmd[i]='"')) then
@@ -380,7 +380,7 @@ if (argc > 1) and (far_strlen(get_ds,longint(largs[1])) = 6)  then
     Writeln('proxy command line ');
     Writeln('proxy command line ');
 {$EndIf SYSTEMDEBUG}
 {$EndIf SYSTEMDEBUG}
     proxy_argc := atohex(largs[2]);
     proxy_argc := atohex(largs[2]);
-	proxy_seg  := atohex(largs[3]);
+    proxy_seg  := atohex(largs[3]);
     proxy_ofs := atohex(largs[4]);
     proxy_ofs := atohex(largs[4]);
     getmem(rm_argv,proxy_argc*sizeof(word));
     getmem(rm_argv,proxy_argc*sizeof(word));
     sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
     sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
@@ -418,7 +418,7 @@ function strcopy(dest,source : pchar) : pchar;
             movl 12(%ebp),%edi
             movl 12(%ebp),%edi
             movl $0xffffffff,%ecx
             movl $0xffffffff,%ecx
             xorb %al,%al
             xorb %al,%al
-			repne
+            repne
             scasb
             scasb
             not %ecx
             not %ecx
             movl 8(%ebp),%edi
             movl 8(%ebp),%edi
@@ -494,7 +494,7 @@ end;
      begin
      begin
         if len > tb_size then runerror(217);
         if len > tb_size then runerror(217);
         sysseg_move(dos_selector,tb,get_ds,addr,len);
         sysseg_move(dos_selector,tb,get_ds,addr,len);
-	 end;
+     end;
 
 
     procedure sysrealintr(intnr : word;var regs : trealregs);
     procedure sysrealintr(intnr : word;var regs : trealregs);
 
 
@@ -653,24 +653,24 @@ begin
   writesize:=0;
   writesize:=0;
   while len > 0 do
   while len > 0 do
    begin
    begin
-	 if len>tb_size then
-	  size:=tb_size
-	 else
-	  size:=len;
-	 syscopytodos(addr+writesize,size);
-	 regs.realecx:=size;
-	 regs.realedx:=tb and 15;
-	 regs.realds:=tb shr 4;
-	 regs.realebx:=h;
-	 regs.realeax:=$4000;
-	 sysrealintr($21,regs);
-	 if (regs.realflags and carryflag) <> 0 then
-	  begin
-		InOutRes:=lo(regs.realeax);
-		exit(writesize);
-	  end;
-	 len:=len-size;
-	 writesize:=writesize+size;
+     if len>tb_size then
+      size:=tb_size
+     else
+      size:=len;
+     syscopytodos(addr+writesize,size);
+     regs.realecx:=size;
+     regs.realedx:=tb and 15;
+     regs.realds:=tb shr 4;
+     regs.realebx:=h;
+     regs.realeax:=$4000;
+     sysrealintr($21,regs);
+     if (regs.realflags and carryflag) <> 0 then
+      begin
+        InOutRes:=lo(regs.realeax);
+        exit(writesize);
+      end;
+     len:=len-size;
+     writesize:=writesize+size;
    end;
    end;
   Do_Write:=WriteSize
   Do_Write:=WriteSize
 end;
 end;
@@ -702,7 +702,7 @@ begin
         exit;
         exit;
       end
       end
      else
      else
-	  if regs.realeax<size then
+      if regs.realeax<size then
        begin
        begin
          syscopyfromdos(addr+readsize,regs.realeax);
          syscopyfromdos(addr+readsize,regs.realeax);
          do_read:=readsize+regs.realeax;
          do_read:=readsize+regs.realeax;
@@ -816,7 +816,7 @@ begin
       fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
       fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
       fmclosed : ;
       fmclosed : ;
      else
      else
-	  begin
+      begin
         inoutres:=102; {not assigned}
         inoutres:=102; {not assigned}
         exit;
         exit;
       end;
       end;
@@ -865,8 +865,8 @@ begin
   sysrealintr($21,regs);
   sysrealintr($21,regs);
   if (regs.realflags and carryflag) <> 0 then
   if (regs.realflags and carryflag) <> 0 then
    begin
    begin
-	 InOutRes:=lo(regs.realeax);
-	 exit;
+     InOutRes:=lo(regs.realeax);
+     exit;
    end
    end
   else
   else
    filerec(f).handle:=regs.realeax;
    filerec(f).handle:=regs.realeax;
@@ -881,19 +881,33 @@ begin
 { append mode }
 { append mode }
   if (flags and $10)<>0 then
   if (flags and $10)<>0 then
    begin
    begin
-	 do_seekend(filerec(f).handle);
-	 filerec(f).mode:=fmoutput; {fool fmappend}
+     do_seekend(filerec(f).handle);
+     filerec(f).mode:=fmoutput; {fool fmappend}
    end;
    end;
 end;
 end;
 
 
+
+function do_isdevice(handle:longint):boolean;
+var
+  regs : trealregs;
+begin
+  regs.realebx:=handle;
+  regs.realeax:=$4400;
+  sysrealintr($21,regs);
+  do_isdevice:=(regs.realedx and $80)<>0;
+  if (regs.realflags and carryflag) <> 0 then
+   InOutRes:=lo(regs.realeax);
+end;
+
+
 {*****************************************************************************
 {*****************************************************************************
-						   UnTyped File Handling
+                           UnTyped File Handling
 *****************************************************************************}
 *****************************************************************************}
 
 
 {$i file.inc}
 {$i file.inc}
 
 
 {*****************************************************************************
 {*****************************************************************************
-						   Typed File Handling
+                           Typed File Handling
 *****************************************************************************}
 *****************************************************************************}
 
 
 {$i typefile.inc}
 {$i typefile.inc}
@@ -969,8 +983,8 @@ begin
   sysrealintr($21,regs);
   sysrealintr($21,regs);
   if (regs.realflags and carryflag) <> 0 then
   if (regs.realflags and carryflag) <> 0 then
    Begin
    Begin
-	 InOutRes:=lo(regs.realeax);
-	 exit;
+     InOutRes:=lo(regs.realeax);
+     exit;
    end
    end
   else
   else
    syscopyfromdos(longint(@temp),251);
    syscopyfromdos(longint(@temp),251);
@@ -978,10 +992,10 @@ begin
   i:=0;
   i:=0;
   while (temp[i]<>#0) do
   while (temp[i]<>#0) do
    begin
    begin
-	 if temp[i]='/' then
-	  temp[i]:='\';
-	 dir[i+4]:=temp[i];
-	 inc(i);
+     if temp[i]='/' then
+      temp[i]:='\';
+     dir[i+4]:=temp[i];
+     inc(i);
    end;
    end;
   dir[2]:=':';
   dir[2]:=':';
   dir[3]:='\';
   dir[3]:='\';
@@ -994,16 +1008,16 @@ begin
    begin
    begin
    { We need to get the current drive from DOS function 19H  }
    { We need to get the current drive from DOS function 19H  }
    { because the drive was the default, which can be unknown }
    { because the drive was the default, which can be unknown }
-	 regs.realeax:=$1900;
-	 sysrealintr($21,regs);
-	 i:= (regs.realeax and $ff) + ord('A');
-	 dir[1]:=chr(i);
+     regs.realeax:=$1900;
+     sysrealintr($21,regs);
+     i:= (regs.realeax and $ff) + ord('A');
+     dir[1]:=chr(i);
    end;
    end;
 end;
 end;
 
 
 
 
 {*****************************************************************************
 {*****************************************************************************
-						 SystemUnit Initialization
+                         SystemUnit Initialization
 *****************************************************************************}
 *****************************************************************************}
 
 
 {$ifndef RTLLITE}
 {$ifndef RTLLITE}
@@ -1018,20 +1032,7 @@ end;
 {$endif RTLLITE}
 {$endif RTLLITE}
 
 
 
 
-procedure OpenStdIO(var f:text;mode:word;hdl:longint);
-begin
-  Assign(f,'');
-  TextRec(f).Handle:=hdl;
-  TextRec(f).Mode:=mode;
-  TextRec(f).InOutFunc:=@FileInOutFunc;
-  TextRec(f).FlushFunc:=@FileInOutFunc;
-  TextRec(f).Closefunc:=@fileclosefunc;
-end;
-
-
 Begin
 Begin
-{ Initialize ExitProc }
-  ExitProc:=Nil;
 { to test stack depth }
 { to test stack depth }
   loweststack:=maxlongint;
   loweststack:=maxlongint;
 { Setup heap }
 { Setup heap }
@@ -1050,7 +1051,10 @@ Begin
 End.
 End.
 {
 {
   $Log$
   $Log$
-  Revision 1.8  1998-06-26 08:19:10  pierre
+  Revision 1.9  1998-07-01 15:29:57  peter
+    * better readln/writeln
+
+  Revision 1.8  1998/06/26 08:19:10  pierre
     + all debug in ifdef SYSTEMDEBUG
     + all debug in ifdef SYSTEMDEBUG
     + added local arrays :
     + added local arrays :
       opennames names of opened files
       opennames names of opened files
@@ -1059,7 +1063,6 @@ End.
       many open files !!
       many open files !!
 
 
   Revision 1.7  1998/06/15 15:17:08  daniel
   Revision 1.7  1998/06/15 15:17:08  daniel
-
   * RTLLITE conditional added to produce smaller RTL.
   * RTLLITE conditional added to produce smaller RTL.
 
 
   Revision 1.6  1998/05/31 14:18:29  peter
   Revision 1.6  1998/05/31 14:18:29  peter

+ 6 - 1
rtl/i386/i386.inc

@@ -687,6 +687,7 @@ end ['EAX'];
          end;
          end;
       end;
       end;
 
 
+{$IFNDEF NEW_READWRITE}
     procedure f1;[public,alias: 'FLUSH_STDOUT'];
     procedure f1;[public,alias: 'FLUSH_STDOUT'];
 
 
       begin
       begin
@@ -698,6 +699,7 @@ end ['EAX'];
             popal
             popal
          end;
          end;
       end;
       end;
+{$ENDIF NEW_READWRITE}
 
 
 
 
 Function Sptr : Longint;
 Function Sptr : Longint;
@@ -728,7 +730,10 @@ end;
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.13  1998-06-26 08:20:57  daniel
+  Revision 1.14  1998-07-01 15:29:58  peter
+    * better readln/writeln
+
+  Revision 1.13  1998/06/26 08:20:57  daniel
   - Doerror removed.
   - Doerror removed.
 
 
   Revision 1.12  1998/05/31 14:15:47  peter
   Revision 1.12  1998/05/31 14:15:47  peter

+ 10 - 24
rtl/inc/system.inc

@@ -120,11 +120,11 @@ Procedure incr_ansi_ref (P : pointer);[Alias : 'INCR_ANSI_REF'];
 
 
 
 
 {****************************************************************************
 {****************************************************************************
-		     Run-Time Type Information (RTTI)
+                                                 Run-Time Type Information (RTTI)
 ****************************************************************************}
 ****************************************************************************}
 
 
-{$i rtti.inc}
 
 
+{$i rtti.inc}
 
 
 {****************************************************************************
 {****************************************************************************
                                Math Routines
                                Math Routines
@@ -332,14 +332,7 @@ Begin
   Halt(0);
   Halt(0);
 End;
 End;
 
 
-{ Seems not to be used (PFV)
-Procedure Initexception;[Public,Alias: 'INITEXCEPTION'];
-Begin
-  Writeln('Exception occurred during program initialization.');
-  halt(216);
-End;
-}
-{$ifndef RTLLITE}
+
 Procedure dump_stack(bp : Longint);
 Procedure dump_stack(bp : Longint);
 
 
   Procedure dump_frame(addr : Longint);
   Procedure dump_frame(addr : Longint);
@@ -365,7 +358,6 @@ Begin
    End;
    End;
 End;
 End;
 
 
-{$endif RTLLITE}
 
 
 Procedure Do_exit;[Public,Alias: '__EXIT'];
 Procedure Do_exit;[Public,Alias: '__EXIT'];
 {
 {
@@ -381,23 +373,17 @@ Begin
      exitProc:=nil;
      exitProc:=nil;
      current_exit();
      current_exit();
    End;
    End;
-  If erroraddr<>nil Then
+  If DoError Then
    Begin
    Begin
-{$ifndef RTLLITE}
      Writeln('Run time error  ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
      Writeln('Run time error  ',Errorcode,' at 0x',hexstr(Longint(Erroraddr),8));
      dump_stack(ErrorBase);
      dump_stack(ErrorBase);
-{$else RTLLITE}
-     writeln('Runerror ',errorcode,' at ',longint(erroraddr));
-{$endif RTLLITE}
    End;
    End;
+{$IFNDEF NEW_READWRITE}
   Flush(stderr);
   Flush(stderr);
-
+{$ENDIF NEW_READWRITE}
 End;
 End;
 
 
 
 
-{$ifndef RTLLITE}
-
-
 Type
 Type
   PExitProcInfo = ^TExitProcInfo;
   PExitProcInfo = ^TExitProcInfo;
   TExitProcInfo = Record
   TExitProcInfo = Record
@@ -434,11 +420,12 @@ Begin
   ExitProc:=@DoExitProc;
   ExitProc:=@DoExitProc;
 End;
 End;
 
 
-{$endif RTLLITE}
-
 {
 {
   $Log$
   $Log$
-  Revision 1.13  1998-06-26 08:21:09  daniel
+  Revision 1.14  1998-07-01 15:29:59  peter
+    * better readln/writeln
+
+  Revision 1.13  1998/06/26 08:21:09  daniel
   - Doerror removed.
   - Doerror removed.
 
 
   Revision 1.12  1998/06/25 14:04:25  peter
   Revision 1.12  1998/06/25 14:04:25  peter
@@ -448,7 +435,6 @@ End;
   + RTLLITE directive to compile minimal RTL.
   + RTLLITE directive to compile minimal RTL.
 
 
   Revision 1.10  1998/06/15 15:16:26  daniel
   Revision 1.10  1998/06/15 15:16:26  daniel
-
   * RTLLITE conditional added to produce smaller RTL
   * RTLLITE conditional added to produce smaller RTL
 
 
   Revision 1.9  1998/06/10 07:46:45  michael
   Revision 1.9  1998/06/10 07:46:45  michael

+ 231 - 140
rtl/inc/text.inc

@@ -1,4 +1,5 @@
-{    $Id$
+{
+    $Id$
     This file is part of the Free Pascal Run time library.
     This file is part of the Free Pascal Run time library.
     Copyright (c) 1993,97 by the Free Pascal development team
     Copyright (c) 1993,97 by the Free Pascal development team
 
 
@@ -35,42 +36,54 @@ Begin
 End;
 End;
 
 
 
 
-Procedure FileInOutFunc(var t:TextRec);
+Procedure FileReadFunc(var t:TextRec);
 Begin
 Begin
-  Case t.mode Of
-   fmoutput : Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
-    fminput : t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
-  else
-   RunError(102);
-  End;
+  t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
   t.BufPos:=0;
   t.BufPos:=0;
 End;
 End;
 
 
 
 
+Procedure FileWriteFunc(var t:TextRec);
+Begin
+  Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
+  t.BufPos:=0;
+End;
+
+
+
 Procedure FileOpenFunc(var t:TextRec);
 Procedure FileOpenFunc(var t:TextRec);
 var
 var
   Flags : Longint;
   Flags : Longint;
 Begin
 Begin
-  t.InOutFunc:=@FileInOutFunc;
-  t.FlushFunc:=@FileInOutFunc;
-  t.CloseFunc:=@FileCloseFunc;
   Case t.mode Of
   Case t.mode Of
     fmInput : Flags:=$1000;
     fmInput : Flags:=$1000;
    fmOutput : Flags:=$1101;
    fmOutput : Flags:=$1101;
    fmAppend : Flags:=$1011;
    fmAppend : Flags:=$1011;
+  else
+   RunError(102);
   End;
   End;
-  Do_Open(t,PChar(@TextRec(t).Name),Flags);
+  Do_Open(t,PChar(@t.Name),Flags);
+  t.CloseFunc:=@FileCloseFunc;
+  t.FlushFunc:=nil;
+  if t.Mode=fmInput then
+   t.InOutFunc:=@FileReadFunc
+  else
+   begin
+     t.InOutFunc:=@FileWriteFunc;
+   { Only install flushing if its a NOT a file }
+     if Do_Isdevice(t.Handle) then
+      t.FlushFunc:=@FileWriteFunc;
+   end;
 End;
 End;
 
 
 
 
 Procedure assign(var t:Text;const s:String);
 Procedure assign(var t:Text;const s:String);
 Begin
 Begin
   FillChar(t,SizEof(TextRec),0);
   FillChar(t,SizEof(TextRec),0);
+{ only set things that are not zero }
   TextRec(t).Handle:=UnusedHandle;
   TextRec(t).Handle:=UnusedHandle;
   TextRec(t).mode:=fmClosed;
   TextRec(t).mode:=fmClosed;
   TextRec(t).BufSize:=128;
   TextRec(t).BufSize:=128;
-  TextRec(t).Bufpos:=0;
-  TextRec(T).Bufend:=0;
   TextRec(t).Bufptr:=@TextRec(t).Buffer;
   TextRec(t).Bufptr:=@TextRec(t).Buffer;
   TextRec(t).OpenFunc:=@FileOpenFunc;
   TextRec(t).OpenFunc:=@FileOpenFunc;
   Move(s[1],TextRec(t).Name,Length(s));
   Move(s[1],TextRec(t).Name,Length(s));
@@ -93,9 +106,10 @@ Procedure Close(var t : Text);[Public,Alias: 'CLOSE_TEXT',IOCheck];
 Begin
 Begin
   If (TextRec(t).mode<>fmClosed) Then
   If (TextRec(t).mode<>fmClosed) Then
    Begin
    Begin
-     FileFunc(TextRec(t).FlushFunc)(TextRec(t));
+   { Write pending buffer }
+     FileFunc(TextRec(t).InOutFunc)(TextRec(t));
      TextRec(t).mode:=fmClosed;
      TextRec(t).mode:=fmClosed;
-     { Only close functions not connected to stdout.}
+   { Only close functions not connected to stdout.}
      If ((TextRec(t).Handle<>StdInputHandle) or
      If ((TextRec(t).Handle<>StdInputHandle) or
          (TextRec(t).Handle<>StdOutputHandle) or
          (TextRec(t).Handle<>StdOutputHandle) or
          (TextRec(t).Handle<>StdErrorHandle)) Then
          (TextRec(t).Handle<>StdErrorHandle)) Then
@@ -116,15 +130,7 @@ Begin
    End;
    End;
   End;
   End;
   TextRec(t).mode:=word(mode);
   TextRec(t).mode:=word(mode);
-{  If TextRec(t).Name[0]<>#0 Then }
-   FileFunc(TextRec(t).OpenFunc)(TextRec(t))
-{  else
-   Begin
-     TextRec(t).Handle:=defHdl;
-     TextRec(t).InOutFunc:=@FileInOutFunc;
-     TextRec(t).FlushFunc:=@FileInOutFunc;
-     TextRec(t).CloseFunc:=@FileCloseFunc;
-   End; }
+  FileFunc(TextRec(t).OpenFunc)(TextRec(t))
 End;
 End;
 
 
 
 
@@ -150,7 +156,9 @@ Procedure Flush(var t : Text);[IOCheck];
 Begin
 Begin
   If TextRec(t).mode<>fmOutput Then
   If TextRec(t).mode<>fmOutput Then
    exit;
    exit;
-  FileFunc(TextRec(t).FlushFunc)(TextRec(t));
+{ Not the flushfunc but the inoutfunc should be used, becuase that
+  writes the data, flushfunc doesn't need to be assigned }
+  FileFunc(TextRec(t).InOutFunc)(TextRec(t));
 End;
 End;
 
 
 
 
@@ -342,101 +350,120 @@ End;
                                Write(Ln)
                                Write(Ln)
 *****************************************************************************}
 *****************************************************************************}
 
 
-Procedure w(Len : Longint;var f : TextRec;var s : String);[Public,Alias: 'WRITE_TEXT_STRING'];
+Procedure WriteBuffer(var f:TextRec;var b;len:longint);
 var
 var
-  hbytes,Pos,copybytes : Longint;
-  hs : String;
-Begin
-  If f.mode<>fmOutput Then
-   exit;
-  copybytes:=Length(s);
-  If Len>copybytes Then
-   Begin
-     hs:=Space(Len-copybytes);
-     w(0,f,hs);
-   End;
-  Pos:=1;
-  hbytes:=f.BufSize-f.BufPos;
-  { If no room in Buffer, do a flush. }
-  If hbytes=0 Then
-   FileFunc(f.FlushFunc)(f);
-  while copybytes>hbytes Do
-   Begin
-     Move(s[Pos],f.Bufptr^[f.BufPos],hbytes);
-     f.BufPos:=f.BufPos+hbytes;
-     copybytes:=copybytes-hbytes;
-     pos:=pos+hbytes;
+  p   : pchar;
+  left,
+  idx : longint;
+begin
+  p:=pchar(@b);
+  idx:=0;
+  left:=f.BufSize-f.BufPos;
+  while len>left do
+   begin
+     move(p[idx],f.Bufptr^[f.BufPos],left);
+     dec(len,left);
+     inc(idx,left);
+     inc(f.BufPos,left);
      FileFunc(f.InOutFunc)(f);
      FileFunc(f.InOutFunc)(f);
-     hbytes:=f.BufSize-f.BufPos;
-   End;
-  Move(s[Pos],f.Bufptr^[f.BufPos],copybytes);
-  f.BufPos:=f.BufPos+copybytes;
-End;
+     left:=f.BufSize-f.BufPos;
+   end;
+  move(p[idx],f.Bufptr^[f.BufPos],len);
+  inc(f.BufPos,len);
+end;
 
 
 
 
-Procedure w(var t : TextRec);[Public,Alias: 'WRITELN_TEXT'];
+Procedure WriteBlanks(var f:TextRec;len:longint);
 var
 var
-  hs : String;
-Begin
+  left : longint;
+begin
+  left:=f.BufSize-f.BufPos;
+  while len>left do
+   begin
+     FillChar(f.Bufptr^[f.BufPos],left,' ');
+     dec(len,left);
+     inc(f.BufPos,left);
+     FileFunc(f.InOutFunc)(f);
+     left:=f.BufSize-f.BufPos;
+   end;
+  FillChar(f.Bufptr^[f.BufPos],len,' ');
+  inc(f.BufPos,len);
+end;
+
+
+Procedure Write_End(var f:TextRec);[Public,Alias:'WRITE_END'];
+begin
+  if f.FlushFunc<>nil then
+   FileFunc(f.FlushFunc)(f);
+end;
+
+
+Procedure Writeln_End(var f:TextRec);[Public,Alias:'WRITELN_END'];
+const
 {$IFDEF SHORT_LINEBREAK}
 {$IFDEF SHORT_LINEBREAK}
-  hs:=#10;
-{$ELSE}
-  hs:=#13#10;
-{$ENDIF}
-  w(0,t,hs);
+  eollen=1;
+  eol : array[0..0] of char=(#10);
+{$ELSE SHORT_LINEBREAK}
+  eollen=2;
+  eol : array[0..1] of char=(#13,#10);
+{$ENDIF SHORT_LINEBREAK}
+begin
+{ Write EOL }
+  WriteBuffer(f,eol,eollen);
+{ Flush }
+  if f.FlushFunc<>nil then
+   FileFunc(f.FlushFunc)(f);
+end;
+
+
+Procedure Write_Str(Len : Longint;var f : TextRec;const s : String);[Public,Alias: 'WRITE_TEXT_STRING'];
+Begin
+  If f.mode<>fmOutput Then
+   exit;
+  If Len>Length(s) Then
+   WriteBlanks(f,Len-Length(s));
+  WriteBuffer(f,s[1],Length(s));
 End;
 End;
 
 
 
 
 Type
 Type
    array00 = array[0..0] Of Char;
    array00 = array[0..0] Of Char;
-Procedure w(Len : Longint;var f : TextRec;const p : array00);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_ARRAY'];
+Procedure Write_Array(Len : Longint;var f : TextRec;const p : array00);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_ARRAY'];
 var
 var
-  hbytes,Pos,copybytes : Longint;
-  hs : String;
+  ArrayLen : longint;
 Begin
 Begin
   If f.mode<>fmOutput Then
   If f.mode<>fmOutput Then
    exit;
    exit;
-  copybytes:=StrLen(p);
-  If Len>copybytes Then
-   Begin
-     hs:=Space(Len-copybytes);
-     w(0,f,hs);
-   End;
-  Pos:=0;
-  hbytes:=f.BufSize-f.BufPos;
-  { If no room in buffer , do a flush. }
-  If hbytes=0 Then
-    FileFunc(f.FlushFunc)(f);
-  while copybytes>hbytes Do
-   Begin
-     Move(p[Pos],f.Bufptr^[f.BufPos],hbytes);
-     f.BufPos:=f.BufPos+hbytes;
-     copybytes:=copybytes-hbytes;
-     pos:=pos+hbytes;
-     FileFunc(f.InOutFunc)(f);
-     hbytes:=f.BufSize-f.BufPos;
-   End;
-  Move(p[Pos],f.Bufptr^[f.BufPos],copybytes);
-  f.BufPos:=f.BufPos+copybytes;
+  ArrayLen:=StrLen(p);
+  If Len>ArrayLen Then
+   WriteBlanks(f,Len-ArrayLen);
+  WriteBuffer(f,p,ArrayLen);
 End;
 End;
 
 
 
 
-Procedure wa(Len : Longint;var f : TextRec;p : PChar);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_POINTER'];
+Procedure Write_PChar(Len : Longint;var f : TextRec;p : PChar);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_POINTER'];
+var
+  PCharLen : longint;
 Begin
 Begin
-  w(Len,f,p);
+  If f.mode<>fmOutput Then
+   exit;
+  PCharLen:=StrLen(p);
+  If Len>PCharLen Then
+   WriteBlanks(f,Len-PCharLen);
+  WriteBuffer(f,p^,PCharLen);
 End;
 End;
 
 
 
 
-Procedure w(Len : Longint;var t : TextRec;l : Longint);[Public,Alias: 'WRITE_TEXT_LONGINT'];
+Procedure Write_LongInt(Len : Longint;var t : TextRec;l : Longint);[Public,Alias: 'WRITE_TEXT_LONGINT'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str(l,s);
   Str(l,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 
 
 
 
-Procedure w(fixkomma,Len : Longint;var t : TextRec;r : real);[Public,Alias: 'WRITE_TEXT_REAL'];
+Procedure Write_Real(fixkomma,Len : Longint;var t : TextRec;r : real);[Public,Alias: 'WRITE_TEXT_REAL'];
 var
 var
    s : String;
    s : String;
 Begin
 Begin
@@ -445,88 +472,97 @@ Begin
 {$else}
 {$else}
    Str_real(Len,fixkomma,r,rt_s32real,s);
    Str_real(Len,fixkomma,r,rt_s32real,s);
 {$endif}
 {$endif}
-   w(Len,t,s);
+   Write_Str(Len,t,s);
 End;
 End;
 
 
 
 
-Procedure w(Len : Longint;var t : TextRec;l : cardinal);[Public,Alias: 'WRITE_TEXT_CARDINAL'];
+Procedure Write_Cardinal(Len : Longint;var t : TextRec;l : cardinal);[Public,Alias: 'WRITE_TEXT_CARDINAL'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str(L,s);
   Str(L,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 
 
 
 
-Procedure w(fixkomma,Len : Longint;var t : TextRec;r : single);[Public,Alias: 'WRITE_TEXT_SINGLE'];
+Procedure Write_Single(fixkomma,Len : Longint;var t : TextRec;r : single);[Public,Alias: 'WRITE_TEXT_SINGLE'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str_real(Len,fixkomma,r,rt_s32real,s);
   Str_real(Len,fixkomma,r,rt_s32real,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 
 
 
 
 {$ifdef SUPPORT_EXTENDED}
 {$ifdef SUPPORT_EXTENDED}
-Procedure w(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias: 'WRITE_TEXT_EXTENDED'];
+Procedure Write_Extended(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias: 'WRITE_TEXT_EXTENDED'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str_real(Len,fixkomma,r,rt_s80real,s);
   Str_real(Len,fixkomma,r,rt_s80real,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 {$endif SUPPORT_EXTENDED}
 {$endif SUPPORT_EXTENDED}
 
 
 
 
 {$ifdef SUPPORT_COMP}
 {$ifdef SUPPORT_COMP}
-Procedure w(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias: 'WRITE_TEXT_COMP'];
+Procedure Write_Comp(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias: 'WRITE_TEXT_COMP'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str_real(Len,fixkomma,r,rt_s64bit,s);
   Str_real(Len,fixkomma,r,rt_s64bit,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 {$endif SUPPORT_COMP}
 {$endif SUPPORT_COMP}
 
 
-Procedure w(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias: 'WRITE_TEXT_FIXED'];
+
+Procedure Write_Fixed(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias: 'WRITE_TEXT_FIXED'];
 var
 var
   s : String;
   s : String;
 Begin
 Begin
   Str_real(Len,fixkomma,r,rt_f32bit,s);
   Str_real(Len,fixkomma,r,rt_f32bit,s);
-  w(Len,t,s);
+  Write_Str(Len,t,s);
 End;
 End;
 
 
 
 
-{ Is called wc to avoid recursive calling. }
-Procedure wc(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias: 'WRITE_TEXT_BOOLEAN'];
-const
-  BoolString:array[0..1] Of String[5]=('FALSE','TRUE');
+Procedure Write_Boolean(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias: 'WRITE_TEXT_BOOLEAN'];
 Begin
 Begin
-   if b then
-     w(Len,t,String(BoolString[1]))
-   else
-     w(Len,t,String(BoolString[0]));
+{ Can't use array[boolean] because b can be >0 ! }
+  if b then
+    Write_Str(Len,t,'TRUE')
+  else
+    Write_Str(Len,t,'FALSE');
 End;
 End;
 
 
 
 
-Procedure wc(Len : Longint;var t : TextRec;c : Char);[Public,Alias: 'WRITE_TEXT_CHAR'];
-var
-   hs : String;
+Procedure Write_Char(Len : Longint;var t : TextRec;c : Char);[Public,Alias: 'WRITE_TEXT_CHAR'];
 Begin
 Begin
   If t.mode<>fmOutput Then
   If t.mode<>fmOutput Then
    exit;
    exit;
   If Len>1 Then
   If Len>1 Then
-   Begin
-     hs:=Space(Len-1);
-     w(0,t,hs);
-   End;
+   WriteBlanks(t,Len-1);
   If t.BufPos+1>=t.BufSize Then
   If t.BufPos+1>=t.BufSize Then
-   FileFunc(t.FlushFunc)(t);
+   FileFunc(t.InOutFunc)(t);
   t.Bufptr^[t.BufPos]:=c;
   t.Bufptr^[t.BufPos]:=c;
   Inc(t.BufPos);
   Inc(t.BufPos);
 End;
 End;
 
 
 
 
+{$IFNDEF NEW_READWRITE}
+Procedure w(var t : TextRec);[Public,Alias: 'WRITELN_TEXT'];
+var
+  hs : String;
+Begin
+  {$IFDEF SHORT_LINEBREAK}
+   hs:=#10;
+  {$ELSE}
+   hs:=#13#10;
+  {$ENDIF}
+  Write_Str(0,t,hs);
+End;
+{$ENDIF NEW_READWRITE}
+
+
 {*****************************************************************************
 {*****************************************************************************
                                 Read(Ln)
                                 Read(Ln)
 *****************************************************************************}
 *****************************************************************************}
@@ -624,10 +660,18 @@ begin
 end;
 end;
 
 
 
 
-Procedure r(var f : TextRec);[Public,Alias: 'READLN_TEXT'];
+Procedure Read_End(var f:TextRec);[Public,Alias:'READ_END'];
+begin
+  if f.FlushFunc<>nil then
+   FileFunc(f.FlushFunc)(f);
+end;
+
+
+Procedure ReadLn_End(var f : TextRec);[Public,Alias: 'READLN_END'];
 Begin
 Begin
   if not OpenInput(f) then
   if not OpenInput(f) then
    exit;
    exit;
+{ Read until a linebreak }
   while (f.BufPos<f.BufEnd) do
   while (f.BufPos<f.BufEnd) do
    begin
    begin
      inc(f.BufPos);
      inc(f.BufPos);
@@ -636,10 +680,13 @@ Begin
      If f.BufPos>=f.BufEnd Then
      If f.BufPos>=f.BufEnd Then
       FileFunc(f.InOutFunc)(f);
       FileFunc(f.InOutFunc)(f);
    end;
    end;
+{ Flush if set }
+  if f.FlushFunc<>nil then
+   FileFunc(f.FlushFunc)(f);
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var s : String);[Public,Alias: 'READ_TEXT_STRING'];
+Procedure Read_String(var f : TextRec;var s : String);[Public,Alias: 'READ_TEXT_STRING'];
 var
 var
   Temp,sPos : Word;
   Temp,sPos : Word;
 Begin
 Begin
@@ -659,6 +706,7 @@ Begin
       Begin
       Begin
         Move (f.Bufptr^[f.BufPos],s[sPos],Temp-f.BufPos);
         Move (f.Bufptr^[f.BufPos],s[sPos],Temp-f.BufPos);
         sPos:=sPos+Temp-f.BufPos;
         sPos:=sPos+Temp-f.BufPos;
+      { Remove #13 from a #13#10 break }
         If s[sPos-1]=#13 Then
         If s[sPos-1]=#13 Then
          dec(sPos);
          dec(sPos);
       End
       End
@@ -680,7 +728,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var c : Char);[Public,Alias: 'READ_TEXT_CHAR'];
+Procedure Read_Char(var f : TextRec;var c : Char);[Public,Alias: 'READ_TEXT_CHAR'];
 Begin
 Begin
   c:=#0;
   c:=#0;
   if not OpenInput(f) then
   if not OpenInput(f) then
@@ -693,7 +741,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var s : PChar);[Public,Alias:'READ_TEXT_PCHAR_AS_POINTER'];
+Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'READ_TEXT_PCHAR_AS_POINTER'];
 var
 var
   p    : PChar;
   p    : PChar;
   Temp : byte;
   Temp : byte;
@@ -711,7 +759,7 @@ Begin
       inc(Temp);
       inc(Temp);
      { copy string. }
      { copy string. }
      Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
      Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
-     longint(p):=longint(p)+(temp-f.bufpos);
+     Inc(Longint(p),Temp-f.BufPos);
      If pchar(p-1)^=#13 Then
      If pchar(p-1)^=#13 Then
       dec(p);
       dec(p);
      { update f.BufPos }
      { update f.BufPos }
@@ -726,7 +774,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var s : array00);[Public,Alias:'READ_TEXT_PCHAR_AS_ARRAY'];
+Procedure Read_Array(var f : TextRec;var s : array00);[Public,Alias:'READ_TEXT_PCHAR_AS_ARRAY'];
 var
 var
   p    : PChar;
   p    : PChar;
   Temp : byte;
   Temp : byte;
@@ -744,7 +792,7 @@ Begin
       inc(Temp);
       inc(Temp);
      { copy string. }
      { copy string. }
      Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
      Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
-     longint(p):=longint(p)+(temp-f.bufpos);
+     Inc(Longint(p),Temp-f.BufPos);
      If pchar(p-1)^=#13 Then
      If pchar(p-1)^=#13 Then
       dec(p);
       dec(p);
      { update f.BufPos }
      { update f.BufPos }
@@ -759,7 +807,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : Longint);[Public,Alias: 'READ_TEXT_LONGINT'];
+Procedure Read_Longint(var f : TextRec;var l : Longint);[Public,Alias: 'READ_TEXT_LONGINT'];
 var
 var
   hs   : String;
   hs   : String;
   code : Word;
   code : Word;
@@ -777,11 +825,11 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : Integer);[Public,Alias: 'READ_TEXT_INTEGER'];
+Procedure Read_Integer(var f : TextRec;var l : Integer);[Public,Alias: 'READ_TEXT_INTEGER'];
 var
 var
   ll : Longint;
   ll : Longint;
 Begin
 Begin
-  r(f,ll);
+  Read_Longint(f,ll);
   l:=0;
   l:=0;
   If (ll<-32768) or (ll>32767) Then
   If (ll<-32768) or (ll>32767) Then
    RunError(106);
    RunError(106);
@@ -789,11 +837,11 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : Word);[Public,Alias: 'READ_TEXT_WORD'];
+Procedure Read_Word(var f : TextRec;var l : Word);[Public,Alias: 'READ_TEXT_WORD'];
 var
 var
   ll : Longint;
   ll : Longint;
 Begin
 Begin
-  r(f,ll);
+  Read_Longint(f,ll);
   l:=0;
   l:=0;
   If (ll<0) or (ll>$ffff) Then
   If (ll<0) or (ll>$ffff) Then
    RunError(106);
    RunError(106);
@@ -801,11 +849,11 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : byte);[Public,Alias: 'READ_TEXT_BYTE'];
+Procedure Read_Byte(var f : TextRec;var l : byte);[Public,Alias: 'READ_TEXT_BYTE'];
 var
 var
   ll : Longint;
   ll : Longint;
 Begin
 Begin
-  r(f,ll);
+  Read_Longint(f,ll);
   l:=0;
   l:=0;
   If (ll<0) or (ll>255) Then
   If (ll<0) or (ll>255) Then
    RunError(106);
    RunError(106);
@@ -813,11 +861,11 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : shortint);[Public,Alias: 'READ_TEXT_SHORTINT'];
+Procedure Read_Shortint(var f : TextRec;var l : shortint);[Public,Alias: 'READ_TEXT_SHORTINT'];
 var
 var
    ll : Longint;
    ll : Longint;
 Begin
 Begin
-  r(f,ll);
+  Read_Longint(f,ll);
   l:=0;
   l:=0;
   If (ll<-128) or (ll>127) Then
   If (ll<-128) or (ll>127) Then
    RunError(106);
    RunError(106);
@@ -825,7 +873,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var l : cardinal);[Public,Alias: 'READ_TEXT_CARDINAL'];
+Procedure Read_Cardinal(var f : TextRec;var l : cardinal);[Public,Alias: 'READ_TEXT_CARDINAL'];
 var
 var
   hs   : String;
   hs   : String;
   code : Word;
   code : Word;
@@ -843,7 +891,7 @@ Begin
 End;
 End;
 
 
 
 
-Procedure r(var f : TextRec;var d : Real);[Public,Alias: 'READ_TEXT_REAL'];
+Procedure Read_Real(var f : TextRec;var d : Real);[Public,Alias: 'READ_TEXT_REAL'];
 var
 var
   hs   : String;
   hs   : String;
   code : Word;
   code : Word;
@@ -881,7 +929,7 @@ End;
 
 
 
 
 {$ifdef SUPPORT_EXTENDED}
 {$ifdef SUPPORT_EXTENDED}
-Procedure r(var f : TextRec;var d : extended);[Public,Alias: 'READ_TEXT_EXTENDED'];
+Procedure Read_Extended(var f : TextRec;var d : extended);[Public,Alias: 'READ_TEXT_EXTENDED'];
 var
 var
   hs   : String;
   hs   : String;
   code : Word;
   code : Word;
@@ -920,7 +968,7 @@ End;
 
 
 
 
 {$ifdef SUPPORT_COMP}
 {$ifdef SUPPORT_COMP}
-Procedure r(var f : TextRec;var d : comp);[Public,Alias: 'READ_TEXT_COMP'];
+Procedure Read_Comp(var f : TextRec;var d : comp);[Public,Alias: 'READ_TEXT_COMP'];
 var
 var
   hs   : String;
   hs   : String;
   code : Word;
   code : Word;
@@ -957,9 +1005,52 @@ Begin
 End;
 End;
 {$endif SUPPORT_COMP}
 {$endif SUPPORT_COMP}
 
 
+
+{$IFNDEF NEW_READWRITE}
+Procedure r(var f : TextRec);[Public,Alias: 'READLN_TEXT'];
+Begin
+  if not OpenInput(f) then
+   exit;
+  while (f.BufPos<f.BufEnd) do
+   begin
+     inc(f.BufPos);
+     if (f.BufPtr^[f.BufPos-1]=#10) then
+      exit;
+     If f.BufPos>=f.BufEnd Then
+      FileFunc(f.InOutFunc)(f);
+   end;
+End;
+{$ENDIF NEW_READWRITE}
+
+
+{*****************************************************************************
+                               Initializing
+*****************************************************************************}
+
+procedure OpenStdIO(var f:text;mode:word;hdl:longint);
+begin
+  Assign(f,'');
+  TextRec(f).Handle:=hdl;
+  TextRec(f).Mode:=mode;
+  TextRec(f).Closefunc:=@FileCloseFunc;
+  case mode of
+  fmInput : TextRec(f).InOutFunc:=@FileReadFunc;
+ fmOutput : begin
+              TextRec(f).InOutFunc:=@FileWriteFunc;
+              TextRec(f).FlushFunc:=@FileWriteFunc;
+            end;
+  else
+   RunError(102);
+  end;
+end;
+
+
 {
 {
   $Log$
   $Log$
-  Revision 1.12  1998-07-01 14:48:10  carl
+  Revision 1.13  1998-07-01 15:30:00  peter
+    * better readln/writeln
+
+  Revision 1.12  1998/07/01 14:48:10  carl
     * bugfix of WRITE_TEXT_BOOLEAN , was not TP compatible
     * bugfix of WRITE_TEXT_BOOLEAN , was not TP compatible
     + added explicit typecast in OpenText
     + added explicit typecast in OpenText
 
 

+ 27 - 17
rtl/linux/syslinux.pp

@@ -461,11 +461,32 @@ Begin
    begin
    begin
      Oflags:=Oflags and not(Open_RDWR);
      Oflags:=Oflags and not(Open_RDWR);
      FileRec(f).Handle:=sys_open(p,oflags,438);
      FileRec(f).Handle:=sys_open(p,oflags,438);
-   end;     
+   end;
+
   Errno2Inoutres;
   Errno2Inoutres;
 {$endif}
 {$endif}
 End;
 End;
 
 
+
+Function Do_IsDevice(Handle:Longint):boolean;
+{
+  Interface to Unix ioctl call.
+  Performs various operations on the filedescriptor Handle.
+  Ndx describes the operation to perform.
+  Data points to data needed for the Ndx function. The structure of this
+  data is function-dependent.
+}
+var
+  sr: SysCallRegs;
+  Data : array[0..255] of byte; {Large enough for termios info}
+begin
+  sr.reg2:=Handle;
+  sr.reg3:=$5401; {=TCGETS}
+  sr.reg4:=Longint(@Data);
+  Do_IsDevice:=(SysCall(Syscall_nr_ioctl,sr)=0);
+end;
+
+
 {*****************************************************************************
 {*****************************************************************************
                            UnTyped File Handling
                            UnTyped File Handling
 *****************************************************************************}
 *****************************************************************************}
@@ -639,34 +660,23 @@ begin
 end;
 end;
 
 
 
 
-procedure OpenStdIO(var f:text;mode:word;const std:string;hdl:longint);
-begin
-  Assign(f,std);
-  TextRec(f).Handle:=hdl;
-  TextRec(f).Mode:=mode;
-  TextRec(f).InOutFunc:=@FileInOutFunc;
-  TextRec(f).FlushFunc:=@FileInOutFunc;
-  TextRec(f).Closefunc:=@fileclosefunc;
-end;
-
-
 Begin
 Begin
 { Set up segfault Handler }
 { Set up segfault Handler }
   InstallSegFaultHandler;
   InstallSegFaultHandler;
 { Setup heap }
 { Setup heap }
   InitHeap;
   InitHeap;
 { Setup stdin, stdout and stderr }
 { Setup stdin, stdout and stderr }
-  OpenStdIO(Input,fmInput,'stdin',StdInputHandle);
-  OpenStdIO(Output,fmOutput,'stdout',StdOutputHandle);
-  OpenStdIO(StdErr,fmOutput,'stderr',StdErrorHandle);
+  OpenStdIO(Input,fmInput,StdInputHandle);
+  OpenStdIO(Output,fmOutput,StdOutputHandle);
+  OpenStdIO(StdErr,fmOutput,StdErrorHandle);
 { Reset IO Error }
 { Reset IO Error }
   InOutRes:=0;
   InOutRes:=0;
 End.
 End.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.5  1998-06-23 16:57:17  peter
-    * fixed the filesize() problems under linux and filerec.size=0 error
+  Revision 1.6  1998-07-01 15:30:01  peter
+    * better readln/writeln
 
 
   Revision 1.4  1998/05/30 14:18:43  peter
   Revision 1.4  1998/05/30 14:18:43  peter
     * fixed to remake with -Rintel in the ppc386.cfg
     * fixed to remake with -Rintel in the ppc386.cfg

+ 156 - 106
rtl/win32/syswin32.pp

@@ -18,6 +18,7 @@
 unit syswin32;
 unit syswin32;
 
 
 {$I os.inc}
 {$I os.inc}
+{$DEFINE WINHEAP}
 
 
 interface
 interface
 
 
@@ -25,6 +26,11 @@ interface
 
 
 {$I systemh.inc}
 {$I systemh.inc}
 
 
+{$ifndef WinHeap}
+  { include heap support headers }
+  {$I heaph.inc}
+{$endif}
+
 const
 const
 { Default filehandles }
 { Default filehandles }
    UnusedHandle    : longint = -1;
    UnusedHandle    : longint = -1;
@@ -55,16 +61,25 @@ type
   end;
   end;
 
 
 var
 var
+{ C compatible arguments }
+  argc  : longint;
+  argv  : ppchar;
+{ Win32 Info }
   startupinfo : tstartupinfo;
   startupinfo : tstartupinfo;
   hprevinst,
   hprevinst,
   hinstance,
   hinstance,
   cmdshow     : longint;
   cmdshow     : longint;
-  heaperror   : pointer;
+
+{$ifdef WinHeap}
+var
+  heaperror  : pointer;
+
+function HeapSize:longint;
+{$endif}
 
 
 implementation
 implementation
 
 
 { include system independent routines }
 { include system independent routines }
-
 {$I system.inc}
 {$I system.inc}
 
 
 { some declarations for Win32 API calls }
 { some declarations for Win32 API calls }
@@ -79,12 +94,10 @@ type
    function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
    function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
      external 'user32' name 'MessageBoxA';
      external 'user32' name 'MessageBoxA';
 
 
-   { command line/enviroment functions }
-   function GetCommandLine : LPTSTR;
-     external 'kernel32' name 'GetCommandLineA';
    { time and date functions }
    { time and date functions }
    function GetTickCount : longint;
    function GetTickCount : longint;
      external 'kernel32' name 'GetTickCount';
      external 'kernel32' name 'GetTickCount';
+
    { process functions }
    { process functions }
    procedure ExitProcess(uExitCode : UINT);
    procedure ExitProcess(uExitCode : UINT);
      external 'kernel32' name 'ExitProcess';
      external 'kernel32' name 'ExitProcess';
@@ -131,92 +144,22 @@ end;
 procedure halt(errnum : byte);
 procedure halt(errnum : byte);
 begin
 begin
   do_exit;
   do_exit;
-  flush(stderr);
   ExitProcess(errnum);
   ExitProcess(errnum);
 end;
 end;
 
 
 
 
 function paramcount : longint;
 function paramcount : longint;
-var
-  count   : longint;
-  cmdline : pchar;
-  quote   : set of char;
 begin
 begin
-  cmdline:=GetCommandLine;
-  count:=0;
-  while true do
-   begin
-     { skip leading spaces }
-     while cmdline^ in [' ',#9] do
-       cmdline:=cmdline+1;
-     if cmdline^='"' then
-       begin
-          quote:=['"'];
-          cmdline:=cmdline+1;
-       end
-     else
-       quote:=[' ',#9];
-     if cmdline^=#0 then
-       break;
-     inc(count);
-     while (cmdline^<>#0) and not(cmdline^ in quote) do
-       cmdline:=cmdline+1;
-     { skip quote }
-     if cmdline^ in quote then
-       cmdline:=cmdline+1;
-   end;
-  paramcount:=count-1;
+  paramcount := argc - 1;
 end;
 end;
 
 
 
 
 function paramstr(l : longint) : string;
 function paramstr(l : longint) : string;
-var
-  s       : string;
-  count   : longint;
-  cmdline : pchar;
-  quote   : set of char;
 begin
 begin
-  s:='';
-  if (l>=0) and (l<=paramcount) then
-    begin
-       cmdline:=GetCommandLine;
-       count:=0;
-       while true do
-         begin
-            { skip leading spaces }
-            while cmdline^ in [' ',#9] do
-              cmdline:=cmdline+1;
-            if cmdline^='"' then
-              begin
-                 quote:=['"'];
-                 cmdline:=cmdline+1;
-              end
-            else
-              quote:=[' ',#9];
-            if cmdline^=#0 then
-              break;
-            if count=l then
-              begin
-                 while (cmdline^<>#0) and not(cmdline^ in quote) do
-                   begin
-                      s:=s+cmdline^;
-                      cmdline:=cmdline+1;
-                   end;
-                 break;
-              end
-            else
-              begin
-                 while (cmdline^<>#0) and not(cmdline^ in quote) do
-                   cmdline:=cmdline+1;
-              end;
-            { skip quote }
-            if cmdline^ in quote then
-              cmdline:=cmdline+1;
-            inc(count);
-         end;
-
-    end;
-  paramstr:=s;
+  if (l>=0) and (l+1<=argc) then
+   paramstr:=strpas(argv[l])
+  else
+   paramstr:='';
 end;
 end;
 
 
 
 
@@ -230,8 +173,50 @@ end;
                               Heap Management
                               Heap Management
 *****************************************************************************}
 *****************************************************************************}
 
 
-{ Include Windows Heap manager }
-{$I winheap.inc}
+{$ifdef WinHeap}
+
+  {$i winheap.inc}
+
+{$else}
+
+   { memory functions }
+   function GlobalAlloc(mode,size:longint):longint;
+     external 'kernel32' name 'GlobalAlloc';
+   function GlobalReAlloc(mode,size:longint):longint;
+     external 'kernel32' name 'GlobalReAlloc';
+   function GlobalHandle(p:pointer):longint;
+     external 'kernel32' name 'GlobalHandle';
+   function GlobalLock(handle:longint):pointer;
+     external 'kernel32' name 'GlobalLock';
+   function GlobalUnlock(h:longint):longint;
+     external 'kernel32' name 'GlobalUnlock';
+   function GlobalFree(h:longint):longint;
+     external 'kernel32' name 'GlobalFree';
+   function GlobalSize(h:longint):longint;
+     external 'kernel32' name 'GlobalSize';
+   procedure GlobalMemoryStatus(p:pointer);
+     external 'kernel32' name 'GlobalMemoryStatus';
+   function LocalAlloc(uFlags : UINT;uBytes :UINT) : HLOCAL;
+     external 'kernel32' name 'LocalAlloc';
+   function LocalFree(hMem:HLOCAL):HLOCAL;
+     external 'kernel32' name 'LocalFree';
+
+function Sbrk(size : longint):longint;
+var
+  h,l : longint;
+begin
+  h:=GlobalAlloc(258,size);
+  GlobalLock(h);
+  l:=GlobalSize(h);
+  writeln(l);
+  sbrk:=l;
+end;
+
+{ include standard heap management }
+{$I heap.inc}
+
+{$endif WinHeap}
+
 
 
 {*****************************************************************************
 {*****************************************************************************
                           Low Level File Routines
                           Low Level File Routines
@@ -258,6 +243,8 @@ end;
      external 'kernel32' name 'CreateFileA';
      external 'kernel32' name 'CreateFileA';
    function SetEndOfFile(h : longint) : boolean;
    function SetEndOfFile(h : longint) : boolean;
      external 'kernel32' name 'SetEndOfFile';
      external 'kernel32' name 'SetEndOfFile';
+   function GetFileType(Handle:DWORD):DWord;
+     external 'kernel32' name 'GetFileType';
 
 
 
 
 procedure AllowSlash(p:pchar);
 procedure AllowSlash(p:pchar);
@@ -442,6 +429,14 @@ begin
    inoutres:=GetLastError;
    inoutres:=GetLastError;
 end;
 end;
 
 
+
+function do_isdevice(handle:longint):boolean;
+begin
+  do_isdevice:=(getfiletype(handle)=2);
+end;
+
+
+
 {*****************************************************************************
 {*****************************************************************************
                            UnTyped File Handling
                            UnTyped File Handling
 *****************************************************************************}
 *****************************************************************************}
@@ -539,15 +534,80 @@ procedure getdir(drivenr:byte;var dir:string);
    function GetStdHandle(nStdHandle:DWORD):THANDLE;
    function GetStdHandle(nStdHandle:DWORD):THANDLE;
      external 'kernel32' name 'GetStdHandle';
      external 'kernel32' name 'GetStdHandle';
 
 
+   { command line/enviroment functions }
+   function GetCommandLine : pchar;
+     external 'kernel32' name 'GetCommandLineA';
+
    { module functions }
    { module functions }
    function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
    function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
      external 'kernel32' name 'GetModuleFileNameA';
      external 'kernel32' name 'GetModuleFileNameA';
    function GetModuleHandle(p : pointer) : longint;
    function GetModuleHandle(p : pointer) : longint;
      external 'kernel32' name 'GetModuleHandleA';
      external 'kernel32' name 'GetModuleHandleA';
 
 
+var
+  ModuleName : array[0..255] of char;
+function GetCommandFile:pchar;
+begin
+  GetModuleFileName(0,@ModuleName,255);
+  GetCommandFile:=@ModuleName;
+end;
+
+
+procedure setup_arguments;
+var
+  arglen,
+  count   : longint;
+  argstart,
+  cmdline : pchar;
+  quote   : set of char;
+  argsbuf : array[0..127] of pchar;
+begin
+{ create commandline, it starts with the executed filename which is argv[0] }
+  cmdline:=GetCommandLine;
+  count:=0;
+  repeat
+  { skip leading spaces }
+    while cmdline^ in [' ',#9,#13] do
+     inc(longint(cmdline));
+    case cmdline^ of
+      #0 : break;
+     '"' : begin
+             quote:=['"'];
+             inc(longint(cmdline));
+           end;
+    '''' : begin
+             quote:=[''''];
+             inc(longint(cmdline));
+           end;
+    else
+     quote:=[' ',#9,#13];
+    end;
+  { scan until the end of the argument }
+    argstart:=cmdline;
+    while (cmdline^<>#0) and not(cmdline^ in quote) do
+     inc(longint(cmdline));
+  { reserve some memory }
+    arglen:=cmdline-argstart;
+    getmem(argsbuf[count],arglen+1);
+    move(argstart^,argsbuf[count]^,arglen);
+    argsbuf[count][arglen]:=#0;
+  { skip quote }
+    if cmdline^ in quote then
+     inc(longint(cmdline));
+    inc(count);
+  until false;
+{ create argc }
+  argc:=count;
+{ create an nil entry }
+  argsbuf[count]:=nil;
+  inc(count);
+{ create the argv }
+  getmem(argv,count shl 2);
+  move(argsbuf,argv^,count shl 2);
+end;
 
 
-{$ASMMODE DIRECT}
 
 
+{$ASMMODE DIRECT}
 procedure Entry;[public,alias: '_mainCRTStartup'];
 procedure Entry;[public,alias: '_mainCRTStartup'];
 begin
 begin
    { call to the pascal main }
    { call to the pascal main }
@@ -557,32 +617,22 @@ begin
    { that's all folks }
    { that's all folks }
    ExitProcess(0);
    ExitProcess(0);
 end;
 end;
-
 {$ASMMODE ATT}
 {$ASMMODE ATT}
 
 
 
 
-procedure OpenStdIO(var f:text;mode:word;hdl:longint);
-begin
-  Assign(f,'');
-  TextRec(f).Handle:=hdl;
-  TextRec(f).Mode:=mode;
-  TextRec(f).InOutFunc:=@FileInOutFunc;
-  TextRec(f).FlushFunc:=@FileInOutFunc;
-  TextRec(f).Closefunc:=@fileclosefunc;
-end;
-
-
-var
-  s : string;
 begin
 begin
 { get some helpful informations }
 { get some helpful informations }
   GetStartupInfo(@startupinfo);
   GetStartupInfo(@startupinfo);
-{ Initialize ExitProc }
-  ExitProc:=Nil;
+{ some misc Win32 stuff }
+  hprevinst:=0;
+  hinstance:=getmodulehandle(GetCommandFile);
+  cmdshow:=startupinfo.wshowwindow;
 { to test stack depth }
 { to test stack depth }
   loweststack:=maxlongint;
   loweststack:=maxlongint;
 { Setup heap }
 { Setup heap }
-{!!!  InitHeap; }
+{$ifndef WinHeap}
+  InitHeap;
+{$endif WinHeap}
 { Setup stdin, stdout and stderr }
 { Setup stdin, stdout and stderr }
   StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
   StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
   StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
   StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
@@ -590,18 +640,18 @@ begin
   OpenStdIO(Input,fmInput,StdInputHandle);
   OpenStdIO(Input,fmInput,StdInputHandle);
   OpenStdIO(Output,fmOutput,StdOutputHandle);
   OpenStdIO(Output,fmOutput,StdOutputHandle);
   OpenStdIO(StdErr,fmOutput,StdErrorHandle);
   OpenStdIO(StdErr,fmOutput,StdErrorHandle);
+{ Arguments }
+  setup_arguments;
 { Reset IO Error }
 { Reset IO Error }
   InOutRes:=0;
   InOutRes:=0;
-{ some misc Win32 stuff }
-  hprevinst:=0;
-  getmodulefilename(0,@s,256);
-  hinstance:=getmodulehandle(@s);
-  cmdshow:=startupinfo.wshowwindow;
 end.
 end.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.9  1998-06-10 10:39:17  peter
+  Revision 1.10  1998-07-01 15:30:02  peter
+    * better readln/writeln
+
+  Revision 1.9  1998/06/10 10:39:17  peter
     * working w32 rtl
     * working w32 rtl
 
 
   Revision 1.8  1998/06/08 23:07:47  peter
   Revision 1.8  1998/06/08 23:07:47  peter

+ 17 - 14
rtl/win32/winheap.inc

@@ -24,13 +24,9 @@
    function GlobalUnlock(h:longint):longint;
    function GlobalUnlock(h:longint):longint;
      external 'kernel32' name 'GlobalUnlock';
      external 'kernel32' name 'GlobalUnlock';
    function GlobalFree(h:longint):longint;
    function GlobalFree(h:longint):longint;
-     external 'kernel32' name 'GlobalUnlock';
+     external 'kernel32' name 'GlobalFree';
    procedure GlobalMemoryStatus(p:pointer);
    procedure GlobalMemoryStatus(p:pointer);
      external 'kernel32' name 'GlobalMemoryStatus';
      external 'kernel32' name 'GlobalMemoryStatus';
-   function LocalAlloc(uFlags : UINT;uBytes :UINT) : HLOCAL;
-     external 'kernel32' name 'LocalAlloc';
-   function LocalFree(hMem:HLOCAL):HLOCAL;
-     external 'kernel32' name 'LocalFree';
 
 
 
 
 type
 type
@@ -56,7 +52,7 @@ end;
 
 
 procedure getmem(var p:pointer;size:longint);[public,alias: 'GETMEM'];
 procedure getmem(var p:pointer;size:longint);[public,alias: 'GETMEM'];
 begin
 begin
-  p:=GlobalLock(GlobalAlloc(258,size));
+  p:=GlobalLock(GlobalAlloc($102,size));
   if p=nil then
   if p=nil then
    memerror(size)
    memerror(size)
 end;
 end;
@@ -67,13 +63,11 @@ var
   h:longint;
   h:longint;
 begin
 begin
   h:=GlobalHandle(p);
   h:=GlobalHandle(p);
-  if h<>0 then
-   if globalunlock(h)=0 then
-    if GlobalFree(h)=0 then
-     begin
-      p:=nil;
-      exit{allways if success!!!}
-     end;
+  if (h<>0) and (globalunlock(h)=0) and (GlobalFree(h)=0) then
+   begin
+     p:=nil;
+     exit;
+   end;
   p:=nil;
   p:=nil;
   memerror(size);
   memerror(size);
 end;
 end;
@@ -112,6 +106,12 @@ begin
 end;
 end;
 
 
 
 
+function HeapSize:longint;
+begin
+  HeapSize:=memmax(true);
+end;
+
+
 function growheap(size:longint):integer;
 function growheap(size:longint):integer;
 begin
 begin
   growheap:=0;
   growheap:=0;
@@ -119,7 +119,10 @@ end;
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.3  1998-06-10 10:39:19  peter
+  Revision 1.4  1998-07-01 15:30:03  peter
+    * better readln/writeln
+
+  Revision 1.3  1998/06/10 10:39:19  peter
     * working w32 rtl
     * working w32 rtl
 
 
 }
 }