home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols200 / vol270 / fs.pas < prev    next >
Pascal/Delphi Source File  |  1986-05-22  |  8KB  |  355 lines

  1. { [FS.PAS of JUGPDS Vol.16]            85-09-15    }
  2. {                                                               }
  3. {    Fortran Coding Format Converter:                 }
  4. {    Free Format to Standard Format                 }
  5. {                                                               }
  6. {        by H. Miyasaka (JUG-CP/M, No.6)            }
  7. {
  8. {             Created  84/11/01   Ver 1.0                          }
  9. {             Updated  85/02/19       1.0A  ... debug              }
  10. {                      85/03/16       1.1   ... auto indent        }
  11. {                      85/04/22       1.1A  ... default indent     }
  12. {                                                                  }
  13. {$A-}
  14.  
  15. program fs;
  16. const
  17.   MAXLINE  = 128;                  {  max input line  }
  18.   MAXLINE1 = 129;                  {  max input line plus one  }
  19.   CONTCHAR = '$';                  {  '$' or '1' or ect.  }
  20.   COMMENT  = 'C';                  {  'C' or '*'  }
  21.   MAXNEST  =  20;                  {  max do nesting  }
  22.   INDENTVAL=   2;                  {  1,2,3,4,...  }
  23.  
  24. type
  25.   maxstr   = string[MAXLINE];
  26.   maxstr1  = string[MAXLINE1];
  27.   filstr   = string[15];           {  filenames  }
  28.  
  29. var
  30.   inf       : text;
  31.   tempf     : text;
  32.   eraf      : text;
  33.  
  34.   infile    : filstr;              {  input filename  }
  35.   tempfile  : filstr;              {  temporary filename  }
  36.   outfile   : filstr;              {  output filename  }
  37.  
  38.   inputline : maxstr;              {  one line input buff  }
  39.   outnumber : string[5];           {  number output buff  }
  40.   outcont   : char;                {  continuation output buff  }
  41.   outtext   : string[65];          {  text output buff  }
  42.  
  43.   lastchar  : char;
  44.  
  45.   options   : maxstr;              {  command tail options  }
  46.  
  47.   numbers   : array[1..MAXNEST] of integer;
  48.   index     : byte;                {  numbers[] index  }
  49.   indent    : byte;                {  auto indent  }
  50.  
  51.   cnt       : integer;             {  line count  }
  52.  
  53.   cond,fend : boolean;
  54.  
  55. procedure exit;
  56. begin
  57.   bdos(0);
  58. end;
  59.  
  60. function exist(filename:filstr):boolean;
  61. var
  62.   fil : text;
  63. begin
  64.   assign(fil,filename);
  65.   {$I-}
  66.   reset(fil);
  67.   {$I+}
  68.   exist := (ioresult = 0)
  69. end;
  70.  
  71. procedure delleft(var st:maxstr);
  72. var
  73.   i : byte;
  74. begin
  75.   i := 1;
  76.   while  copy(st,i,1) = ' ' do
  77.     i := i + 1;
  78.   delete(st,1,i-1);
  79. end;
  80.  
  81. procedure arguments(var arg1:filstr;var arg2:maxstr;var cond:boolean);
  82. label
  83.   001;
  84. var
  85.   arg : maxstr absolute $0080;
  86.   i   : byte;
  87. begin
  88.   if length(arg) = 0
  89.     then
  90.       cond := False
  91.     else
  92.       begin
  93.         delleft(arg);
  94.         for i := 1 to length(arg) do
  95.           if (arg[i] = ' ') or (arg[i] = '[')
  96.             then
  97.               begin
  98.                 arg2 := copy(arg,i,length(arg)-i+1);
  99.                 i := i - 1;
  100.                 goto 001;
  101.               end;
  102.         arg2 := ' ';
  103. 001:    arg1 := copy(arg,1,i);
  104.         cond := True;
  105.       end;
  106. end;
  107.  
  108. procedure outputf(var infile,tempfile,outfile:filstr);
  109. var
  110.   name : filstr;
  111.   i    : byte;
  112. begin
  113.   i := pos ('.',infile);
  114.   if i = 0
  115.     then
  116.       begin
  117.         name  := infile;
  118.         infile:= infile + '.FRE';
  119.       end
  120.     else
  121.       name := copy(infile,1,i-1);
  122.   tempfile := name + '.$$$';
  123.   outfile  := name + '.FOR';
  124. end;
  125.  
  126. procedure linput(var st:maxstr;var fend:boolean);
  127. var
  128.   st1 : maxstr1;
  129.   i : byte;
  130. begin
  131.   if not EOF(inf)
  132.     then
  133.       begin
  134.         cnt := cnt + 1;
  135.         readln(inf,st1);
  136.         if length(st1) = 129
  137.           then
  138.             begin
  139.               write  ('Warning ... Input line number ',cnt);
  140.               writeln(', *** Record length too long ***');
  141.             end;
  142.         st := st1;
  143.         fend := False
  144.       end
  145.     else
  146.       fend := True;
  147. end;
  148.  
  149. function firsts(st:maxstr):char;
  150. begin
  151.   delleft(st);
  152.   firsts := st[1];
  153. end;
  154.  
  155. procedure outclear;
  156. begin
  157.   outnumber    := '     ';
  158.   outcont      := ' ';
  159.   outtext      := ' ';
  160. end;
  161.  
  162. function lasts:char;
  163. var
  164.   i : byte;
  165. begin
  166.   i := length(inputline);
  167.   while inputline[i] = ' ' do
  168.     i := i - 1;
  169.   lasts := inputline[i];
  170.   if inputline[i] = '-'
  171.     then
  172.       inputline[i] := ' '
  173. end;
  174.  
  175. procedure numzero;
  176. var
  177.   i  :  byte;
  178. begin
  179.   for i:=1 to MAXNEST do
  180.     numbers[i] := 0
  181. end;
  182.  
  183. procedure indadd;
  184. var
  185.   numstr  : maxstr;
  186.   tempstr : maxstr;
  187.   num     : integer;
  188.   code    : integer;
  189.   i,j     : byte;
  190. begin
  191.   if indent <> 0
  192.     then
  193.       for i:=1 to indent do
  194.         insert(' ',inputline,1);
  195.   i := pos('DO',inputline);
  196.   if i = 0
  197.     then
  198.       i := pos('do',inputline);
  199.   if i <> 0
  200.     then
  201.       begin
  202.         tempstr := copy(inputline,i+2,length(inputline)-(i-1));
  203.         delleft(tempstr);
  204.         i := 1;
  205.         while (tempstr[i] <> ' ') and (length(tempstr) > i) do
  206.           i := i + 1;
  207.         numstr := copy(tempstr,1,i-1);
  208.         j := 0;
  209.         val(numstr,num,code);
  210.         if code <> 0
  211.           then
  212.             writeln('Warnning ... Input line number ',cnt,
  213.                      '  *** DO number error ***');
  214.         index := 1;
  215.         while numbers[index] <> 0 do
  216.           index := index + 1;
  217.         numbers[index] := num;
  218.         indent := indent + INDENTVAL;
  219.       end;
  220. end;
  221.  
  222. procedure indsub(tnumber:maxstr);
  223. var
  224.   num  : integer;
  225.   code : integer;
  226.   i    : byte;
  227. begin
  228.   for i:=index downto 1 do
  229.     begin
  230.       val(tnumber,num,code);
  231.       if numbers[i] = num
  232.         then
  233.           begin
  234.             numbers[i] := 0;
  235.             indent := indent - INDENTVAL;
  236.             if indent < 0
  237.               then
  238.                 begin
  239.                   writeln(' ******* Indent error !!!! *********');
  240.                   indent := 0
  241.                 end
  242.           end
  243.      end
  244. end;
  245.  
  246. procedure number;
  247. var
  248.   tnumber : maxstr;
  249.   i       : byte;
  250. begin
  251.   delleft(inputline);
  252.   i := 1;
  253.   while inputline[i] <> ' ' do
  254.     i := i + 1;
  255.   tnumber := copy(inputline,1,i-1);
  256.   if length(tnumber) > 5
  257.     then
  258.       writeln('Warning ... Input line number ',cnt,
  259.                    ', *** Line number too long ***');
  260.   if pos('N',options) = 0
  261.     then
  262.       indsub(tnumber);
  263.   tnumber := '     ' + tnumber;
  264.   outnumber := copy(tnumber,length(tnumber)-4,5);
  265.   inputline := copy(inputline,i+1,length(inputline)-i);
  266. end;
  267.  
  268. procedure texts;
  269. begin
  270.   if pos('N',options) = 0
  271.     then
  272.       indadd;
  273.   if lastchar = '-'
  274.     then
  275.       outcont := CONTCHAR;
  276.   if length(inputline) > 66
  277.     then
  278.       begin
  279.         lastchar := '-';
  280.         outtext  := copy(inputline,1,65);
  281.         inputline := copy(inputline,66,length(inputline)-65);
  282.       end
  283.     else
  284.       begin
  285.         lastchar := lasts;
  286.         outtext  := inputline;
  287.         inputline := '';
  288.       end;
  289.   writeln(tempf,outnumber,outcont,outtext);
  290.   if length(inputline) <> 0
  291.     then
  292.       begin
  293.         outclear;
  294.         texts;
  295.       end;
  296. end;
  297.  
  298. begin
  299.   cnt := 0;
  300.   indent := 0;
  301.   lastchar := ' ';
  302.   numzero;
  303.   arguments(infile,options,cond);
  304.   if not cond
  305.     then
  306.       begin
  307.         writeln('Fortan Free-format to Standard-format converter.');
  308.         writeln('Usage : fs file-name [n]');
  309.         exit;
  310.       end;
  311.   writeln('---------------------------------------------------------');
  312.   writeln('Fortran Free-Format to Standard-Format Converter Ver 1.1A');
  313.   writeln('---------------------------------------------------------');
  314.   outputf(infile,tempfile,outfile);
  315.   if not exist(infile)
  316.     then
  317.       begin
  318.         writeln(infile,' not found');
  319.         exit;
  320.       end;
  321.   assign(inf,infile);
  322.   assign(tempf,tempfile);
  323.   reset(inf);
  324.   rewrite(tempf);
  325.   linput(inputline,fend);
  326.   while not fend do
  327.     begin
  328.       outclear;
  329.       case firsts(inputline) of
  330.         '"'     : begin
  331.                     inputline[1] := COMMENT;
  332.                     writeln(tempf,inputline);
  333.                   end;
  334.         '0'..'9': begin
  335.                     if lastchar <> '-'
  336.                       then
  337.                         number;
  338.                     texts;
  339.                   end;
  340.          else     texts;
  341.       end;
  342.       linput(inputline,fend);
  343.     end;
  344.     close(inf);
  345.     close(tempf);
  346.     if exist(outfile)
  347.       then
  348.         begin
  349.           assign(eraf,outfile);
  350.           erase(eraf);
  351.         end;
  352.     rename(tempf,outfile);
  353.     writeln;
  354.     writeln('complete');
  355. end.