| @@ -1,45 +1,41 @@ |
| 1 | --- | | |
| 2 | categories: | | |
| 3 | - "Tutorials" | | |
| 4 | - "FPC" | | |
| 5 | - "Lazarus" | | |
| 6 | - "PascalScript" | | |
| 7 | --- | | |
| 8 | | | |
| 9 | ::tpl[Console Mode Pascal] | 1 | ::tpl[Console Mode Pascal] |
| 10 | | 2 | |
| 11 | ## Console-Mode Pascal Programming | 3 | ## Console-Mode Pascal Programming |
| 12 | | 4 | |
| 13 | 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. | 5 | Many of us were writing [[Pascal]] [[Program|programs]] long before [[Graphical User Interface|Graphical User Interfaces]] (GUIs) and [[IDE|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 [[Command-line interface|console]]- or [[Text User Interface|text-mode]] applications in order to perform complex system control tasks. |
| 14 | | 6 | |
| 15 | ### Console mode programming without Lazarus | 7 | ### Console mode programming without Lazarus |
| 16 | | 8 | |
| 17 | Even though many prefer to use the Lazarus IDE to write console mode programs, you can also write them with any text editor and compile them yourself by calling the FPC compiler, e.g. this for a program in example.pas: | 9 | Even though many prefer to use the [[Lazarus IDE]] to write console mode programs, you can also write them with any text editor and compile them yourself by calling the [[FPC]] [[Compiler|compiler]], e.g. this for a program in example.pas: |
| 18 | | 10 | |
| 19 | ```bash | 11 | ```bash |
| 20 | fpc example.pas | 12 | fpc example.pas |
| 21 | ``` | 13 | ``` |
| 22 | | 14 | |
| 23 | Also, the text mode *fp* IDE (looks a bit like old Turbo Pascal) is available. | 15 | Also, the text mode *fp* IDE (looks a bit like old [[Turbo Pascal]]) is available. |
| 24 | In the next sections we will focus on programming with Lazarus. | 16 | In the next sections we will focus on programming with [[Lazarus]]. |
| 25 | | 17 | |
| 26 | ### Console mode programming with Lazarus | 18 | ### Console mode programming with Lazarus |
| 27 | | 19 | |
| 28 | 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. | 20 | 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|syntax highlighting]], access to the libraries, complex searching and code completion tools, and syntax checking. If you do not want a [[TForm|Form]] with its visual components, you do not 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. |
| 29 | | 21 | |
| 30 | To start a console-mode program, go to the Main Menu and select Project -> New Project and then select either **Program** or **Console Application**. 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. | 22 | To start a console-mode program, go to the Main Menu and select Project -> New Project and then select either **Program** or **Console Application**. 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. |
| 31 | | 23 | |
| 32 | :::note | 24 | :::note |
| 33 | Under Windows a GUI application has no console and thus can not **writeln** or **readln**. You will get *File not open* errors. Disable under *Project Options / Compiler Options / Linking / Target specific options / Win32 GUI application* to create a console application. | 25 | Under Windows a GUI application has no console and thus can not **writeln** or **readln**. You will get *File not open* errors. Disable under *Project Options / Compiler Options / Linking / Target specific options / Win32 GUI application* to create a console application. In Lazarus IDE 1.4, this is under *Project(menu) / Project Options / Compiler Options / Config and Target / Target specific options / Win32 GUI application*. |
| 34 | ::: | 26 | ::: |
| 35 | | 27 | |
| | 28 | :::note |
| | 29 | Lazarus has some functionality for command line programs, such as [[CopyFile|copyfile]]. To use these, add a project requirement for LazUtils, which will not pull in the entire LCL. Then add the relevant unit to your uses clause. |
| | 30 | ::: |
| | 31 | |
| 36 | #### Project type: Console Application | 32 | #### Project type: Console Application |
| 37 | | 33 | |
| 38 | Selecting this project type in Lazarus 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. | 34 | Selecting this project type in Lazarus 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 [[Command_line_parameters_and_environment_variables#Environment_variables|environment variables]] and [[Exceptions|exception]] handling. All [[LCL]] programs use this automatically. |
| 39 | | 35 | |
| 40 | #### Project type: Program | 36 | #### Project type: Program |
| 41 | | 37 | |
| 42 | As a demonstration, we will write a very minimalistic Pascal program. In the IDE, select the 'Program' project type and let the IDE helps you a bit. 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. | 38 | As a demonstration, we will write a very minimalistic Pascal program. In the IDE, select the 'Program' project type and let the IDE helps you a bit. For example when you add another [[Unit|unit]] the IDE will automatically add the unitname to the program [[Uses|uses]] section. This behavior is defined in the project options. So you can go between 'Program' and 'Custom Program' at any time. |
| 43 | | 39 | |
| 44 | An example for complete beginners: | 40 | An example for complete beginners: |
| 45 | | 41 | |
| @@ -57,21 +53,25 @@ |
| 57 | end. | 53 | end. |
| 58 | ``` | 54 | ``` |
| 59 | | 55 | |
| 60 | ### PascalScripts | 56 | ### Pascal scripts |
| 61 | | 57 | |
| 62 | Additionally, it is possible to write [[Category:PascalScript|PascalScripts]] that are dynamically compiled by [[InstantFPC]], a cross-platform solution to run small Pascal programs as scripts. The shebang | 58 | Additionally, it is possible to write scripts that are dynamically compiled by [[InstantFPC]], a cross-platform solution to run (small) Pascal programs as scripts. The shebang |
| 63 | | 59 | |
| 64 | ``` | 60 | ``` |
| 65 | #!/usr/bin/env instantfpc | 61 | #!/usr/bin/env instantfpc |
| 66 | ``` | 62 | ``` |
| 67 | | 63 | |
| 68 | permits to start the script like a stand-alone application. | 64 | permits to start the script like a stand-alone program. |
| 69 | | 65 | |
| | 66 | :::note |
| | 67 | Though it is used to run Pascal scripts, InstantFPC uses regular Object Pascal, not the PascalScript script language. So "**PascalScript**" and **Pascal script** are not the same. |
| | 68 | ::: |
| | 69 | |
| 70 | ## Running a Program | 70 | ## Running a Program |
| 71 | | 71 | |
| 72 | #### Compile | 72 | #### Compile |
| 73 | | 73 | |
| 74 | 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 | 74 | 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 |
| 75 | | 75 | |
| 76 | 'Project "Project1" successfully built.:)'. | 76 | 'Project "Project1" successfully built.:)'. |
| 77 | | 77 | |
| @@ -83,7 +83,7 @@ |
| 83 | | 83 | |
| 84 | #### Run in Console | 84 | #### Run in Console |
| 85 | | 85 | |
| 86 | 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. On Unix/Linux, if it is in the current directory you will have to start with ./ as the program probably won't be found in the PATH. | 86 | 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. On Unix/Linux, if it is in the current directory you will have to start with ./ as the program will probably not be found in the PATH. |
| 87 | | 87 | |
| 88 | Example for Linux/Unix where a program is stored in /tmp: | 88 | Example for Linux/Unix where a program is stored in /tmp: |
| 89 | | 89 | |
| @@ -96,6 +96,10 @@ |
| 96 | | 96 | |
| 97 | #### Run in IDE | 97 | #### Run in IDE |
| 98 | | 98 | |
| | 99 | In internal console: show in the terminal output window. |
| | 100 | |
| | 101 | In redirect output. |
| | 102 | |
| 99 | 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 saying: | 103 | 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 saying: |
| 100 | | 104 | |
| 101 | ``` | 105 | ``` |
| @@ -106,8 +110,10 @@ |
| 106 | | 110 | |
| 107 | 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. | 111 | 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. |
| 108 | | 112 | |
| 109 | Unfortunately, this method doesn't allow you to use the integrated debugger. | 113 | On Linux, if the above message "xterm: Can't execvp /usr/share/lazarus//tools/runwait.sh: Permission denied" does not appear after checking the box for "Use launching application", type `/usr/bin/xterm $(TargetCmdLine)` and check `Use launching application` (xterm is installed on almost all Linux systems). |
| 110 | | 114 | |
| | 115 | Unfortunately, this method does not allow you to use the integrated debugger. |
| | 116 | |
| 111 | #### Run in IDE with redirected output | 117 | #### Run in IDE with redirected output |
| 112 | | 118 | |
| 113 | If you want to see what is written to stdout and want to use the integrated debugger as well, stdout can be redirected to a file with the following code: | 119 | If you want to see what is written to stdout and want to use the integrated debugger as well, stdout can be redirected to a file with the following code: |
| @@ -137,20 +143,15 @@ |
| 137 | end. | 143 | end. |
| 138 | ``` | 144 | ``` |
| 139 | | 145 | |
| 140 | Output.txt can then be viewed with 'tail -f output.txt' or viewed with an editor if 'tail' is not available on your os. | 146 | Output.txt can then be viewed with 'tail -f output.txt' or viewed with an editor if 'tail' is not available on your [[Operating System|os]]. |
| 141 | | 147 | |
| 142 | Alternatively, using Lazarus 0.9.31 or up (on Linux only): In the "View" menu, under "Debug Windows" there is an entry for a "console output" to view stdout | 148 | Alternatively, using Lazarus 0.9.31 or up (on Linux only): In the "View" menu, under "Debug Windows" there is an entry for a "console output" to view stdout |
| 143 | | 149 | |
| 144 | ## Unicode (UTF8) output | 150 | ## Unicode (UTF8) output |
| 145 | | 151 | |
| 146 | If you want your console mode program to show Unicode (UTF8) output on Windows Vista and higher (and perhaps on earlier versions, too), you can use the **SetConsoleOutputCP** command to set the console to the UTF8 character set. | 152 | If you want your console mode program to show Unicode ([[UTF-8|UTF8]]) output on Windows Vista and higher (and perhaps on earlier versions, too), you can use the **SetConsoleOutputCP** command to set the console to the UTF8 character set. |
| 147 | | 153 | |
| 148 | :::note | 154 | :::note |
| 149 | After the program is done, the console behaves strangely on my system: entering commands and enter gives no result: no command executed, no error, nothing. Perhaps the console needs to be switched back? I tried that too, but it seemed to give the same result. | | |
| 150 | \--[[User:BigChimp|BigChimp]] 17:15, 28 June 2012 (UTC) | | |
| 151 | ::: | | |
| 152 | | | |
| 153 | :::note | | |
| 154 | you will need to make sure the font of the console can show the letters you want to output (e.g. Greek, Cyrillic, Korean). | 155 | you will need to make sure the font of the console can show the letters you want to output (e.g. Greek, Cyrillic, Korean). |
| 155 | ::: | 156 | ::: |
| 156 | | 157 | |
| @@ -191,18 +192,53 @@ |
| 191 | {$ENDIF} | 192 | {$ENDIF} |
| 192 | UTF8TestString:= 'rosé, водка and ούζο'; | 193 | UTF8TestString:= 'rosé, водка and ούζο'; |
| 193 | writeln ('plain: ' + UTF8TestString); | 194 | writeln ('plain: ' + UTF8TestString); |
| 194 | {Apparently we don't need UTF8ToConsole for this | 195 | {Apparently we do not need UTF8ToConsole for this |
| 195 | UTF8ToConsole did not do anything for me in fact.} | 196 | UTF8ToConsole did not do anything for me in fact.} |
| 196 | end. | 197 | end. |
| 197 | ``` | 198 | ``` |
| 198 | | 199 | |
| | 200 | ### Output CJK text in Windows console |
| | 201 | |
| | 202 | This example from forum member **Thaddy** shows how to properly setup Windows console to support CJK text. |
| | 203 | |
| | 204 | ```pascal |
| | 205 | {$mode delphiunicode}{$H+} |
| | 206 | {$codepage utf8} |
| | 207 | uses Windows, SysUtils; |
| | 208 | var |
| | 209 | s3, s1, s2: string; |
| | 210 | var |
| | 211 | cfi: CONSOLE_FONT_INFOEX; |
| | 212 | begin |
| | 213 | cfi.cbSize := SizeOf(cfi); |
| | 214 | cfi.nFont := 0; |
| | 215 | cfi.dwFontSize.X := 0; |
| | 216 | cfi.dwFontSize.Y := 20; |
| | 217 | cfi.FontFamily := FF_DONTCARE; |
| | 218 | cfi.FontWeight := FW_NORMAL; |
| | 219 | // two fonts that support Chinese etc. |
| | 220 | cfi.Facename := 'NSimSun'; |
| | 221 | // cfi.FaceName := 'SimSun-extB'; // this is a raster font. |
| | 222 | |
| | 223 | SetTextCodePage(Output, CP_UTF8); |
| | 224 | SetConsoleOutputCP(CP_UTF8); |
| | 225 | SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), False, @cfi); |
| | 226 | |
| | 227 | s1 := '春夏'; |
| | 228 | s2 := '秋冬'; |
| | 229 | s3 := s1+s2; |
| | 230 | Writeln('s1=', s1,' s2=', s2, ' s3=', s3); |
| | 231 | Readln; |
| | 232 | end. |
| | 233 | ``` |
| | 234 | |
| 199 | ## Examples | 235 | ## Examples |
| 200 | | 236 | |
| 201 | You can use the Lazarus editor to try out all the examples in the standard Pascal textbooks, or you can write your own. Some of the most useful procedures are those for executing system commands or for running other programs (whether written in Pascal, C or Perl, or shell or batch scripts). | 237 | You can use the Lazarus editor to try out all the examples in the standard Pascal textbooks, or you can write your own. Some of the most useful procedures are those for executing system commands or for running other programs (whether written in Pascal, C or Perl, or shell or batch scripts). |
| 202 | | 238 | |
| 203 | #### Execute shell command | 239 | #### Execute shell command |
| 204 | | 240 | |
| 205 | Here is an example for a *Custom Program* or *Program*... that is Linux/Unix/OSX specific though: | 241 | Here is an example for a *Custom Program* or *Program*... that is Linux/Unix/macOS specific though: |
| 206 | | 242 | |
| 207 | ```pascal | 243 | ```pascal |
| 208 | program TryShell; | 244 | program TryShell; |
| @@ -227,10 +263,10 @@ |
| 227 | var | 263 | var |
| 228 | S: LongInt; | 264 | S: LongInt; |
| 229 | begin | 265 | begin |
| 230 | S := fpsystem('cd /usr/local/src/fpc/devel/fpc ; make clean'); | 266 | S := fpsystem('cd /usr/local/src/fpc/devel/fpc ; make clean'); |
| 231 | S := fpsystem('cd /usr/local/src/fpc/devel/lazarus ; make clean'); | 267 | S := fpsystem('cd /usr/local/src/fpc/devel/lazarus ; make clean'); |
| 232 | S := fpsystem('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log'); | 268 | S := fpsystem('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log'); |
| 233 | S := fpsystem('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log'); | 269 | S := fpsystem('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log' ); |
| 234 | end. | 270 | end. |
| 235 | ``` | 271 | ``` |
| 236 | | 272 | |
| @@ -246,9 +282,9 @@ |
| 246 | fpsystem ('do something in that subdirectory'); | 282 | fpsystem ('do something in that subdirectory'); |
| 247 | ``` | 283 | ``` |
| 248 | | 284 | |
| 249 | 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. | 285 | does not work, because after each **fpsystem()** [[Function|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 via **fpsystem()**. \[Rather: it always starts in . \] |
| 250 | | 286 | |
| 251 | 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]]): | 287 | One does not have to enter every command as a separate line of Pascal; one could create a **bash** script file like this (from [[buildfaq]]): |
| 252 | | 288 | |
| 253 | ```bash | 289 | ```bash |
| 254 | #!/bin/sh | 290 | #!/bin/sh |
| @@ -263,9 +299,10 @@ |
| 263 | svn up lazarus >& ~/cvslaz.log | 299 | svn up lazarus >& ~/cvslaz.log |
| 264 | ``` | 300 | ``` |
| 265 | | 301 | |
| 266 | Name it updatelaz.sh, and then call it from a Pascal program thus: | 302 | Name it **updatelaz.sh**, and then instead of using **bash** to execute it directly, call it from a Pascal program thus: |
| 267 | | 303 | |
| 268 | ```pascal | 304 | ```pascal |
| | 305 | (* This can be compiled or used as a Pascal script *) |
| 269 | program LazUpdate1; | 306 | program LazUpdate1; |
| 270 | uses | 307 | uses |
| 271 | Classes, Unix; | 308 | Classes, Unix; |