Solved

Automate pascal program command line input

Posted on 2009-04-07
8
2,049 Views
Last Modified: 2012-05-06
I am looking to automate a pascal program to accept the readlnI() input automatically either via a parameter file or as parameters when the program is executed on the command line.

I am not really familiar with pascal and I am wondering how best to modify this source code such that the program will accept it's 'input' and 'output' filenames as parameters or if no parameters are supplied, prompt for the user to input them as the program is executing.
Program SPMake;

var

  f : text;

  w : text;

  In_file : String[12];

  In_pref : String[8];

  out_file : String[12];

  lines   : longint;

  suffix  : String[3];

  Alldone : Boolean;

  file_no : integer;

  c : char;

  asc : longint;

 

begin

  Write('Name of SPOOL files -> ');

  Readln(In_file);

  if (length(In_file)=0) or (pos('.',In_file)>0) then

  begin

    writeln('Invalid SPOOL file name.');

    halt;

  end;

 

  Write('File to be created -> ');

  Readln(Out_file);

 

  if (length(Out_file)=0) or (pos('.',Out_file)>9)

  or ((pos('.',In_file)=0) and (length(In_file)>8)) 

  or (length(Out_file)>12)   then

  begin

    writeln('Invalid output file name.');

    halt;

  end;

  In_Pref:=In_file;

 

  Assign(w, out_file);

  Rewrite(w);

    

 

 

  Alldone := False;

 

  file_no:=0;

  while not alldone do 

  Begin

    file_no:=file_no+1;

    str(file_no,suffix);

    while length(suffix)<3 do suffix:='0'+suffix;

 

    In_file:=In_pref+'.'+suffix;  

    Assign(f, In_file);

    {$I-}

    Reset(f);

    {$I+}

    If IOResult<>0 then

    begin

      Alldone:=True;

      if suffix='001' then 

      begin

        write('File ',in_file,' not found.');

        halt;

      end;

    end else

    begin  

      lines:=0;

      writeln('Reading ', In_file, ' into ',out_file);

      while not eof(f)  do

      begin

        read(f,c);

        asc:=ord(c);

        if (asc>9) and (asc<>29) then write(w,c);

 

        {if (asc>= 13) and (asc <= 126) then write(w,c);}

      end;

      close(f);

      erase(f);

    end;

  end;    

  Close(w);

  Writeln('Files combined.');

end.

Open in new window

foxbase-screenshot.JPG
0
Comment
Question by:james_j_hedges
  • 4
  • 3
8 Comments
 
LVL 28

Assisted Solution

by:Bill Bach
Bill Bach earned 100 total points
ID: 24088694
In the C language, the main() function accepts two arguments, known commonly as argc and argv, which contains the count and the values or each command-line argument.  I would expect that your PASCAL compiler will have a similar feature.  A quick search seems to point to using an array called ParamStr[n] when n=0 is the program name, n=1 is the first parameter, and so on.  However, checking your specific compiler manual is the best way to figure this out.  Here's some links to get you started, though:
    http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas074.htm
    http://www.mirrorservice.org/sites/www.gnu-pascal.de/gpc/Accessing-Command-Line-Arguments.html
0
 
LVL 47

Expert Comment

by:dbrunton
ID: 24095242
This looks like Turbo Pascal.

In which case the code you want is the ParamCount and ParamStr functions.

if Paramcount < 1 then
writeln('No parameters on command line')
else
writeln(Paramcount, ' paramete(s)');


for i := 1 to Paramcount do
writeln(ParamStr(i));

Paramstr(index) is an array of the command line parameters.  Blanks and tabs separate the parameters on the command line.
0
 

Author Comment

by:james_j_hedges
ID: 24095282
Thanks for the advice. How did you determine whether the code is 'Pascal' or 'Turbo Pascal'? I'm assuming that I will need to use a different compiler depending on the language?

Thanks
0
 
LVL 47

Accepted Solution

by:
dbrunton earned 400 total points
ID: 24095295
This is the type of code you are wanting.

GetBothFileNames is a procedure that does what you are doing now.  Your present file name getting code goes into this procedure.

GetOutFileName only gets the file name for the out file.  It is a subset of your present code.

These two lines assign the command line parameters to the in and out files.

InFile := ParamStr(1);
OutFile := ParamStr(2);


if ParamCount = 0 then

	GetBothFileNames

else  if ParamCount = 1 then

begin

	InFile := ParamStr(1);

	GetOutFileName

end

else

begin

	InFile := ParamStr(1);

	OutFile := ParamStr(2);

end;

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 47

Assisted Solution

by:dbrunton
dbrunton earned 400 total points
ID: 24095375
Actually it could be standard Pascal.

I assumed the IOResult code was a Turbo Pascal function.  It's not.  It's in a couple of other Pascals as well.

This compiler directive

{$I+}

is Turbo Pascal plus it's used in other Pascal compilers such as Free Pascal and GNU Pascal.

The code for your file names looks like it's designed for DOS from the way the filenames are being created and checked.  The most common DOS Pascal compiler was the Turbo Pascal range.

But which compiler are you using for your code?  Several of the Turbo Pascal compilers are free and readily available.
0
 

Author Comment

by:james_j_hedges
ID: 24095483
I'm not really sure which compiler is being used. I would imagine maybe Turbo Pascal. Do have any suggestions as to which specific compiler to use?

Thanks
0
 
LVL 47

Assisted Solution

by:dbrunton
dbrunton earned 400 total points
ID: 24095552
Turbo Pascal 5.5

http://edn.embarcadero.com/article/20803

You'll need to create a free account to get to the download site.  There's a few tricks involved to get it running on Windows 2000 and XP but all of those are fixable/solvable.

Under Windows 9x or DOS there are no problems.  You may strike problems with fast CPUs but those are fixable as well.


Free Pascal would probably do the job as well.  http://www.freepascal.org/

Shouldn't be any problems getting it running under Win 2000 and XP.
0
 

Author Comment

by:james_j_hedges
ID: 24097084
I've managed to update the pascal program to successfully command line parameters.

Thanks for your help!
Program SPMake (Output);

var

	arguments : integer;

  f : text;

  w : text;

  In_file : String[12];

  In_pref : String[8];

  Out_file : String[12];

  lines   : longint;

  suffix  : String[3];

  Alldone : Boolean;

  file_no : integer;

  c : char;

  asc : longint;
 

begin
 

	if ParamCount = 0 then

		begin

		Writeln('Please enter Input SPOOL files and Output SPOOL file names as parameters');

		end

	else if ParamCount = 1 then

					begin

			 		Writeln('Please enter both Input SPOOL files and Output SPOOL file names as parameters');

			 		end

			 else 

			 begin

  				In_file := ParamStr(1);

  				Out_file := ParamStr(2);

	end;

	

  if (length(In_file)=0) or (pos('.',In_file)>0) then

  begin

    writeln('Invalid SPOOL file name.');

    halt;

  end;

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now