Difference between revisions of "Console Mode Pascal"

From Lazarus wiki
Jump to navigationJump to search
Line 18: Line 18:
  
 
An example for complete beginners:
 
An example for complete beginners:
<pre>
+
<delphi>program Project1;
program Project1;
 
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
 
uses
 
uses
Line 28: Line 27:
 
   { you can add units after this };
 
   { you can add units after this };
 
begin
 
begin
   writeln('Hello World!');
+
   WriteLn('Hello World!');
   readln;
+
   ReadLn;
end.
+
end.</delphi>
</pre>
 
  
 
==== Console Application ====
 
==== Console Application ====
Line 77: Line 75:
 
Here is an example for a ''Custom Program'' or ''Program'':
 
Here is an example for a ''Custom Program'' or ''Program'':
  
<pre>
+
<delphi>program TryShell;
Program TryShell;
+
uses  
uses classes, unix;
+
  Classes, Unix;
var S: longint;
+
var  
 +
  S: LongInt;
 
begin
 
begin
   S := fpsystem ('/bin/ls -la *.p*'); //lists .pp, .pas, .php, .png etc in current directory
+
   S := fpsystem('/bin/ls -la *.p*'); //lists .pp, .pas, .php, .png etc in current directory
   writeln ('Program exited with status : ', S)
+
   WriteLn('Program exited with status : ', S)
end.
+
end.</delphi>
</pre>
 
  
 
==== Example: update fpc and lazarus ====
 
==== Example: update fpc and lazarus ====
Line 91: Line 89:
 
Rather more complex commands can be executed. For example, if you have already checked out the SVN repositories for FPC and Lazarus (see [[buildfaq]]) you could keep your FPC and Lazarus source files up-to-date by retrieval from the SVN repository with the following sequence of calls:
 
Rather more complex commands can be executed. For example, if you have already checked out the SVN repositories for FPC and Lazarus (see [[buildfaq]]) you could keep your FPC and Lazarus source files up-to-date by retrieval from the SVN repository with the following sequence of calls:
  
<pre>
+
<delphi>program LazUpdate;
Program LazUpdate;
+
uses
uses classes, unix;
+
  Classes, Unix;
var s : longint;
+
var  
 +
  S: LongInt;
 
begin  
 
begin  
   S := fpsystem ('cd /usr/local/src/fpc/devel/fpc ; make clean');
+
   S := fpsystem('cd /usr/local/src/fpc/devel/fpc ; make clean');
   S := fpsystem ('cd /usr/local/src/fpc/devel/lazarus ; make clean');
+
   S := fpsystem('cd /usr/local/src/fpc/devel/lazarus ; make clean');
   S := fpsystem ('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log');
+
   S := fpsystem('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log');
   S := fpsystem ('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log');
+
   S := fpsystem('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log');
end.
+
end.</delphi>
</pre>
 
  
 
Note that issuing the command
 
Note that issuing the command
  
fpsystem ('cd /somedirectory')
+
<delphi>fpsystem('cd /somedirectory');</delphi>
 
followed by
 
followed by
fpsystem ('do something in that subdirectory')
+
<delphi>fpsystem ('do something in that subdirectory');</delphi>
 
doesn't work, because after each fpsystem function call the execution of the program returns to the directory it started in; so we need to include multiple statements per line within our calls to shell.
 
doesn't work, because after each fpsystem function call the execution of the program returns to the directory it started in; so we need to include multiple statements per line within our calls to shell.
  
Line 126: Line 124:
 
Name it updatelaz.sh, and then call it from a Pascal program thus:
 
Name it updatelaz.sh, and then call it from a Pascal program thus:
  
<pre>
+
<delphi>program LazUpdate1;
Program LazUpdate1;
+
uses  
uses classes, unix;
+
  Classes, Unix;
var S : longint;  
+
var  
 +
  S: LongInt;  
 
begin
 
begin
   S := fpsystem ('updatelaz.sh')
+
   S := fpsystem('updatelaz.sh')
end.
+
end.</delphi>
</pre>
 
  
 
==See also==
 
==See also==

Revision as of 11:59, 4 September 2010

Deutsch (de) English (en) español (es) suomi (fi) magyar (hu) 日本語 (ja) русский (ru) slovenčina (sk)

Console-Mode Pascal Programming

Many of us were writing Pascal programs long before Graphic User Interfaces (GUIs) and Integrated Development Environments (IDEs) became fashionable. Many others are beginners at Pascal programming and need to be able to try out the basic tools of the language. Still others need to write console- or text-mode applications in order to perform complex system-control tasks.

Lazarus provides an ideal environment for learning Pascal, and for developing text-mode programs. All the functionality of the Integrated Development Environment can be used, including the Source Editor with its syntax highlighting, access to the libraries, complex searching and code completion tools, and syntax checking. If you don't want a Form with its visual components, you don't need one, but the Lazarus Source Editor is still a great environment for program development. You can compile and run your program during development, without ever leaving the Editor.

To start a console-mode program, go to the Main Menu and select Project -> New Project and then select either Program, Console Application or Custom Program. The IDE will not generate all the extra files associated with a full graphic application, and will not open an Object Inspector box, but will open the Source Editor with a skeleton program structure and await your programming input.

Custom Program

A very minimalistic pascal program. Choose this if you want to do everything yourself or just do some quick tests and delete the files soon.

Program

Same as Custom Program, but the IDE helps you a bit more. For example when you add another unit the IDE will automatically add the unitname to the program uses section. This behavior is defined in the project options. So you can go between 'Program' and 'Custom Program' at any time.

An example for complete beginners: <delphi>program Project1; {$mode objfpc}{$H+} uses

 {$IFDEF UNIX}{$IFDEF UseCThreads}
 cthreads,
 {$ENDIF}{$ENDIF}
 Classes
 { you can add units after this };

begin

 WriteLn('Hello World!');
 ReadLn;

end.</delphi>

Console Application

This creates a program with a new class derived from TCustomApplication. TCustomApplication provides a lot of the common things and makes programming command line utilities easy. For example checking command line options, writing help, checking environment variables and exception handling. All LCL programs use this automatically.

Running a Program

Compile

When you have finished your program (or program fragment) you can compile and run it by selecting Run -> Build (or Quick Compile) from the Main Menu or clicking on the Green (Run) triangle symbol in the Button Bar. Any compiler messages (warnings, progress reports or error messages) will appear in the Message Box, and hopefully there will eventually be a message to say

'Project "Project1" successfully built.:)'.

But where is the program??!!

If you have not yet saved the program, then the IDE put it into the temporary directory (e.g. /tmp under linux, C:\temp under windows, see Environment Options / Files / Directory for building test projects).

If you already saved the project, then the program was created in the same directory, where you saved the project1.lpi file.

Run in Console

You can execute the program by going to a console (terminal) window, then use cd to change to the directory and typing the name of the program (in Unix/Linux, if it is in the current directory you will probably have to type

./Project1

as it won't be found in the standard PATH). However, it can be very inconvenient to keep skipping out of the Lazarus Editor and into a terminal window and back again. Fortunately, there is a mechanism that allows a terminal window to be opened from within the Lazarus environment.

Run in IDE

From the Main Menu, select Run -> Run Parameters, then check the box for "Use launching application". The first time you do this and try the Compile/Run sequence, you will probably get a rude message to say

"xterm: Can't execvp /usr/share/lazarus//tools/runwait.sh: Permission denied".  

If this happens, you need to change the permissions on the appropriate file (for example using chmod +x filename, or using the Windows utility for changing permissions); you might have to do this as root. After this, each time you launch your program, a console box will appear and all your text i/o (readln, writeln etc) will appear in it.

After your program has finished execution, a message "Press enter" appears on the screen. Thus any output your program generated will remain on the screen until you have had a chance to read it; after you press 'enter' the console window closes.

Examples

You can use the Lazarus editor to try out all the examples in the standard Pascal text-books, or you can write your own. Some of the most useful procedures are those for executing system commands or for running other programs (which may have been written in Pascal, C or Perl, or may be shell or Batch scripts).

Execute shell command

Here is an example for a Custom Program or Program:

<delphi>program TryShell; uses

 Classes, Unix;

var

 S: LongInt;

begin

 S := fpsystem('/bin/ls -la *.p*'); //lists .pp, .pas, .php, .png etc in current directory
 WriteLn('Program exited with status : ', S)

end.</delphi>

Example: update fpc and lazarus

Rather more complex commands can be executed. For example, if you have already checked out the SVN repositories for FPC and Lazarus (see buildfaq) you could keep your FPC and Lazarus source files up-to-date by retrieval from the SVN repository with the following sequence of calls:

<delphi>program LazUpdate; uses

 Classes, Unix;

var

 S: LongInt;

begin

 S := fpsystem('cd /usr/local/src/fpc/devel/fpc ; make clean');
 S := fpsystem('cd /usr/local/src/fpc/devel/lazarus ; make clean');
 S := fpsystem('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log');
 S := fpsystem('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log');

end.</delphi>

Note that issuing the command

<delphi>fpsystem('cd /somedirectory');</delphi> followed by <delphi>fpsystem ('do something in that subdirectory');</delphi> doesn't work, because after each fpsystem function call the execution of the program returns to the directory it started in; so we need to include multiple statements per line within our calls to shell.

Of course, you don't have to enter every command as a separate line of Pascal; you could create a script file like this (from buildfaq):

#!/bin/sh
cd /usr/local/src/fpc/devel
cd fpc
make clean
cd ..
cd lazarus
make clean
cd ..
svn up fpc >& ~/cvscheckout.log
svn up lazarus >& ~/cvslaz.log 

Name it updatelaz.sh, and then call it from a Pascal program thus:

<delphi>program LazUpdate1; uses

 Classes, Unix;

var

 S: LongInt; 

begin

 S := fpsystem('updatelaz.sh')

end.</delphi>

See also

Original contributors

This page has been converted from the epikwiki version. Original content by User:Kirkpatc.