home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / tsfaqp26.zip / FAQPAS.TXT next >
Internet Message Format  |  1995-07-05  |  76KB

  1. From ts@uwasa.fi Wed Jul 5 00:00:00 1995
  2. Subject: FAQPAS.TXT contents
  3.  
  4.                              Copyright (c) 1993-1995 by Timo Salmi
  5.                                                All rights reserved
  6.  
  7. FAQPAS.TXT Frequently (and not so frequently) asked Turbo Pascal
  8. questions with Timo's answers. The items are in no particular order.
  9.  
  10. You are free to quote brief passages from this file provided you
  11. clearly indicate the source with a proper acknowledgment.
  12.  
  13. Comments and corrections are solicited. But if you wish to have
  14. individual Turbo Pascal consultation, please post your questions to
  15. a suitable Usenet newsgroup like news:comp.lang.pascal.borland. It
  16. is much more efficient than asking me by email. I'd like to help,
  17. but I am very pressed for time. I prefer to pick the questions I
  18. answer from the Usenet news. Thus I can answer publicly at one go if
  19. I happen to have an answer. Besides, newsgroups have a number of
  20. readers who might know a better or an alternative answer. Don't be
  21. discouraged, though, if you get a reply like this from me. I am
  22. always glad to hear from fellow Turbo Pascal users.
  23.  
  24. If you are an experienced Turbo Pascal programmer with Turbo Pascal
  25. material you would like to circulate publicly world-wide, you are
  26. welcome to submit your material to the Garbo MS-DOS archives at the
  27. University of Vaasa. To do that you *FIRST* must *CAREFULLY* read
  28. the uploading instructions and the formal requirements in the file
  29. ftp://garbo.uwasa.fi/pc/UPLOAD.INF. You are also welcome to contact
  30. me by email for these instructions if you are not familiar with
  31. downloading them from the Garbo archives.
  32.  
  33. ....................................................................
  34. Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
  35. Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
  36. Department of Accounting and Business Finance  ; University of Vaasa
  37. ts@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland
  38.  
  39. --------------------------------------------------------------------
  40.  1) How do I disable or capture the break key in Turbo Pascal?
  41.  2) How do I get a printed documentation of my students' TP runs?
  42.  3) What is the code for the weekday of a given date?
  43.  4) Need a program to format Turbo Pascal source code consistently
  44.  5) Can someone give me advice for writing a tsr program?
  45.  6) Why can't I read / write the com ports?
  46.  7) What are interrupts and how to use them in Turbo Pascal?
  47.  8) Should I upgrade my Turbo Pascal version?
  48.  9) How do I execute an MS-DOS command from within a TP program?
  49. 10) How is millisecond timing done?
  50. 11) How can I customize the text characters to my own liking?
  51. 12) How to find the files in a directory and subdirectories?
  52. 13) I need a power function but there is none in Turbo Pascal.
  53. 14) How can I create arrays that are larger than 64 kilobytes?
  54. 15) How can I test that the printer is ready?
  55. 16) How can I clear the keyboard type-ahead buffer?
  56. 17) How can I utilize expanded memory (EMS) in my programs?
  57. 18) How can I obtain the entire command line?
  58. 19) How do I redirect text from printer to file in my TP program?
  59. 20) Turbo Pascal is for wimps. Use standard Pascal or C instead?
  60. 21) How do I turn the cursor off?
  61. 22) How to find all roots of a polynomial?
  62. 23) What is all this talk about "Pascal homework on the net"?
  63. 24) How can I link graphics drivers directly into my executable?
  64. 25) How can I trap a runtime error?
  65. 26) How to get ansi control codes working in Turbo Pascal writes?
  66. 27) How to evaluate a function given as a string to the program?
  67. 28) How does one detect whether input (or output) is redirected?
  68. 29) How does one set the 43/50 line text mode?
  69. 30) How can I assign a value to an environment variable in TP?
  70. --------------------------------------------------------------------
  71.  
  72. Unless otherwise stated the answers cover versions 4.0, 5.0, 5.5,
  73. 6.0 and 7.0 (real mode). The Q&As are not for Turbo Pascal version 3
  74. or earlier. Objects, TVision, Windows, Delphi, etc are not covered.
  75. (I do not use them myself.)
  76. --------------------------------------------------------------------
  77.  
  78. From ts@uwasa.fi Wed Jul 5 00:00:01 1995
  79. Subject: Disabling or capturing the break key
  80.  
  81. 1. *****
  82.  Q: I don't want the Break key to be able to interrupt my TP
  83. programs. How is this done?
  84.  Q2: I want to be able to capture the Break key in my TP program.
  85. How is this done?
  86.  Q3: How do I detect if a certain key has been pressed? (Often, how
  87. do I detect, for example, if the CursorUp key has been pressed?)
  88.  Q4: How do I detect if a cursor key or a function key has been
  89. pressed?
  90.  
  91.  A: This set of frequently asked questions is basically a case of
  92. RTFM (read the f*ing manual). But this feature is, admittedly, not
  93. very prominently displayed in the Turbo Pascal reference. (As a
  94. general rule we should not use the newsgroups as a replacement for
  95. our possibly missing manuals, but enough of this line.)
  96.    I'll only explain Q and Q2. The other two, Q3 and Q4 should be
  97. evident from the example code.
  98.    There is a CheckBreak variable in the Crt unit, which is true by
  99. default. To turn it off use
  100.      uses Crt;
  101.      :
  102.      CheckBreak := false;
  103.      :
  104. Besides turning off break checking this enables you to capture the
  105. pressing of the break key as you would capture pressing ctrl-c. In
  106. other words you can use e.g.
  107.      :
  108.   procedure TEST;
  109.   var key : char;
  110.   begin
  111.     repeat
  112.       if KeyPressed then
  113.         begin
  114.           key := ReadKey;
  115.           case key of
  116.              #0 : begin
  117.                     key := ReadKey;
  118.                     case key of
  119.                       #59 : write ('F1 ');
  120.                       #72 : write ('CursUp ');   { Deteting these  }
  121.                       #75 : write ('CursLf ');   { is often asked! }
  122.                       #77 : write ('CursRg ');
  123.                       #80 : write ('CursDn ');
  124.                       else write ('0 ', ord(key), ' ');
  125.                     end; {case}
  126.                   end;
  127.              #3 : begin    {ctrl-c or break}
  128.                     writeln ('Break');
  129.                     halt(1);
  130.                   end;     { Terminate the program, or whatever }
  131.             #27 : begin
  132.                     write ('<esc> ');
  133.                     exit;  { Exit test, continue program }
  134.                   end;
  135.             else write (key, ' ');
  136.           end; {case}
  137.         end; {if}
  138.     until false;
  139.   end;  (* test *)
  140.      :
  141. IMPORTANT: Don't test the ctrl-break feature just from within the TP
  142. IDE, because it has ctlr-break handler ("interceptor") of its own
  143. and may confuse you into thinking that ctrl-break cannot be
  144. circumvented by the method given above.
  145.   The above example has a double purpose. It also shows the
  146. rudiments how you can detect if a certain key has been pressed. This
  147. enables you to give input without echoing it to the screen, which is
  148. a later FAQ in this collection.
  149.   This is, however, not all there can be to break checking, since
  150. the capturing is possible only at input time. It is also possible to
  151. write a break handler to interrupt a TP program at any time. For
  152. more details see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  153. Chapter 7. (For the bibliography, see FAQPASB.TXT in this same FAQ
  154. collection).
  155.  
  156.  A2: This frequent question also elicits one of the most frequent
  157. false answers. It is often suggested erroneously that the relevant
  158. code would be
  159.   uses dos;
  160.   SetCBreak(false);
  161. This is not so. It confuses MS-DOS and TP break checking with each
  162. other. SetCBreak(false) will _*NOT*_ disable the Ctrl-Break key for
  163. your Turbo Pascal program. What it does is "Sets the state of
  164. Ctrl-Break checking in DOS. SetCBreak sets the state of Ctrl+Break
  165. checking in DOS. When off (False), DOS only checks for Ctrl+Break
  166. during I/O to console, printer, or communication devices. When on
  167. (True), checks are made at every system call."
  168.    This item goes to shows how important it is carefully to check
  169. one's code and facts before claiming something.
  170.  
  171.  A3: Using the "CheckBreak := false;" method is not the only
  172. alternative, however. Here is an example code for disabling
  173. Ctrl-Break and Ctrl-C with interrupts
  174.   uses Dos;
  175.   var OldIntr1B : pointer;  { Ctrl-Break address }
  176.       OldIntr23 : pointer;  { Ctrl-C interrupt handler }
  177.       answer    : string;   { For readln test }
  178.   {$F+}
  179.   procedure NewIntr1B (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  180.             Interrupt;
  181.   {$F-} begin end;
  182.   {$F+}
  183.   procedure NewIntr23 (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  184.             Interrupt;
  185.   {$F-} begin end;
  186.   begin
  187.     GetIntVec ($1B, OldIntr1B);
  188.     SetIntVec ($1B, @NewIntr1B);   { Disable Ctrl-Break }
  189.     GetIntVec ($23, OldIntr23);
  190.     SetIntVec ($23, @NewIntr23);   { Disable Ctrl-C }
  191.     writeln ('Try breaking, disabled');
  192.     readln (answer);
  193.     SetIntVec ($1B, OldIntr1B);    { Enable Ctrl-Break }
  194.     SetIntVec ($23, OldIntr23);    { Enable Ctrl-C }
  195.     writeln ('Try breaking, enabled');
  196.     readln (answer);
  197.     writeln ('Done');
  198.   end.
  199. --------------------------------------------------------------------
  200.  
  201. From ts@uwasa.fi Wed Jul 5 00:00:02 1995
  202. Subject: Directing output also to printer
  203.  
  204. 2. *****
  205.  Q: I want to have a printed documentation of my students' Turbo
  206. Pascal program exercises. How is all input and output directed also
  207. to the printer?
  208.  
  209.  A1: Use a screen capturing program to put everything that comes
  210. onto the screen into a file, and print the file. See FAQPROGS.TXT in
  211. ftp://garbo.uwasa.fi/pc/ts/tsfaqn43.zip (or whatever version number
  212. is the current) for more about these programs. Available by
  213. anonymous FTP or mail server from garbo.uwasa.fi.
  214.  
  215.  A2: See the code in TSPAS.NWS (item: Redirecting writes to the
  216. printer) in the ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever
  217. is the current version number) Turbo Pascal units package (70 = 40,
  218. 50, 55, 60, or 70 depending on your TP version). Alternatively use
  219. USECON and USEPRN routines in the TSUNTG unit of the same package.
  220.  
  221.      +------------------------------------------+
  222.      ! To get these and other packages given as !
  223.      !   /dir/subdir/name                       !
  224.      ! see the instructions in PD2ANS.TXT       !
  225.      +------------------------------------------+
  226.  
  227.  A3: But the really elegant solution to the problem of getting a
  228. logfile (or a printed list) of a Turbo Pascal run is to rewrite the
  229. write(ln) and read(ln) device driver functions. In itself writing
  230. such driver redirections is very advanced Turbo Pascal programming,
  231. but when the programming has once been done, the system is extremely
  232. easy to use as many times as you like. It goes like this. The driver
  233. redirections are programmed into a unit (say, tpulog or tpuprn). All
  234. that is needed after that is to include the following uses statement
  235. into the program (the target program) which has to be logged:
  236.       uses TPULOG;    ( or )    uses TPUPRN;
  237. This is all there is to it. Just adding one simple line to the
  238. target program. (If you call any other units, "uses tpulog" must
  239. come AFTER the system units (e.g. Dos), but BEFORE any which you may
  240. define yourself!)
  241.    The reason that I have named two units here instead of just one
  242. in the above example is that the preferred log for the target
  243. program may be a logfile or the printer. The better solution of
  244. these two is to use the logfile option, and then print it. The
  245. reason is simple. If the target program itself prints something,
  246. your printout will look confused.
  247.    The logging also has obvious limitations. It works for standard
  248. input and output (read(ln) and write(ln)) only. 1) It does not
  249. support graphics, in other words it is for the textmode. 2) It does
  250. not support direct (Crt) screen writes. 3) And, naturally it only
  251. shows the input and output that comes to the screen. Not any other
  252. input or output, such as from or to a file. 4) Furthermore, you are
  253. not allowed to reassign input or output. Statements like assign
  254. (output, '') will result in a crash, because the rewritten output
  255. device redirections are invalidated by such statements. 5) The
  256. device on the default drive must not be write protected, since else
  257. the logfile cannot be written to it. 6) It does not work for Turbo
  258. Pascal 4.0. Despite these restrictions, the method is perfectly
  259. suited for logging students' Turbo Pascal escapades.
  260.    It is advisable first to test and run your target program without
  261. "tpulog", so that if you get any strange errors you'll know whether
  262. they are caused by the logging.
  263.    Where to get such a unit. The code can be found in Michael
  264. Tischer (1990), Turbo Pascal Internals, Abacus, Section 4.2. Next a
  265. few of my own tips on this unit Tischer calls Prot. 1) The code is
  266. in incorrect order. The code that is listed on pages 142 - 145 goes
  267. between pages 139 and 140. 2) You can change the logfile name (const
  268. prot_name) to lpt1 for a printed list of the target program run. In
  269. that case it is advisable to include a test for the online status of
  270. the printer within Tischer's unit. 3) I see no reason why the two
  271. lines in Tischer's interface section couldn't be transferred to the
  272. implementation section. Why have any global definitions?  But all in
  273. all, it works like magic!
  274.  
  275.  A4: From: abcscnuk@csunb.csun.edu (Naoto Kimura (ACM))
  276. Subject: Re: Printing a log of students' exercises revisited
  277. To: ts@uwasa.fi
  278. Date: Fri, 2 Nov 90 20:52:03 pdt
  279. [Reproduced with Naoto's kind permission]
  280. By the way, several months ago, I had submitted a file (nktools.zip)
  281. file on SimTel that contains sources to a unit (LOGGER), which
  282. allows logging of I/O going through the standard input and output
  283. files, while still being able to use the program interactively.  I
  284. believe that I also submitted a copy to your site.  It was something
  285. I put together for use by students here at California State
  286. University at Northridge.  The source works equally well in all
  287. presently available versions of Turbo Pascal.
  288. The only requirements are that
  289.  * you place it as one of the last entries in the USES clause.  If
  290.    there is anything that redirects the standard input and output
  291.    file variables, you should put that unit before my unit in the
  292.    USES clause, so that it can see the I/O stream.
  293.  * Don't use the GotoXY and similar screen display control
  294.    procedures in the Crt unit and expect it to come out the same way
  295.    you had it on the display.  Since all my unit does is just
  296.    capture the I/O stream to send it through the normal channels and
  297.    also to the log file, all screen control information is not sent
  298.    to the log file.
  299.  * All I/O you want logged should go through the standard input and
  300.    output file variables.
  301.  * Don't close the standard input and output file variables, because
  302.    it will cause problems.  Basically, as far as I have checked, it
  303.    just causes the logging to stop at that point.
  304. --------------------------------------------------------------------
  305.  
  306. From ts@uwasa.fi Wed Jul 5 00:00:03 1995
  307. Subject: Code to give the weekday of a date
  308.  
  309. 3. *****
  310.  Q: I want code that gives the weekday of the given date.
  311.  
  312.  A1: There is a WKDAYFN function in my Turbo Pascal units collection
  313. ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version number
  314. is the latest, and where 70 is 40 50 55 60 and 70) Turbo Pascal
  315. units collection to give the modern weekday based on Zeller's
  316. congruence. Available by anonymous FTP or mail server from
  317. garbo.uwasa.fi. Also you can find a more extensive Julian and
  318. Gregorian weekday algorithm with source code in Dr.Dobb's Journal,
  319. June 1989, p. 148. Furthermore Press & Flannery & al (1986),
  320. Numerical Recipes, Cambridge University Press, present a weekday
  321. code. The Numerical Recipes codes are available as
  322. ftp://garbo.uwasa.fi/pc/turbopas/nrpas13.zip (big, 404k!).
  323.  
  324.  A2: Some will recommend the following kludge. Store the current
  325. date, change it, and let MS-DOS get you the weekday. Don't use it!
  326. It is a bad suggestion. On top of being sloppy programming, there
  327. are several snags.  The trick works only for years 1980-2079. A
  328. crash the program may leave the clock at a wrong date. And even if
  329. multitasking is rare, in a multitasking environment havoc may result
  330. for the other tasks. And you may have a TSR that requires the
  331. correct date, etc.
  332. --------------------------------------------------------------------
  333.  
  334. From ts@uwasa.fi Wed Jul 5 00:00:04 1995
  335. Subject: Pretty printers (or uniform code)
  336.  
  337. 4. *****
  338.  Q: Where can I find a program that formats my (or my students')
  339. Turbo Pascal code in a consistent matter.
  340.  
  341.  A: What you are asking for is often called "a pretty printer".
  342. TurboPower Software's (the usual disclaimer applies) commercial
  343. Turbo Analyst has a facility for this with many options. There are
  344. also PD and shareware pretty printers, such as
  345.  :
  346.  16830 Nov 28 1989 ftp://garbo.uwasa.fi/pc/turbopas/ppdk50.zip
  347.  ppdk50.zip Pascal Prettypringting Program, tweak, D.Kirschbaum
  348.  :
  349.  10015 Mar 29 1991 ftp://garbo.uwasa.fi/pc/turbopas/ppp.zip
  350.  ppp.zip Pretty Print Pascal, with Turbo Pascal 5+ source, M.Bless
  351.  :
  352.  38021 Nov 5 1994 ftp://garbo.uwasa.fi/pc/turbopas/bp7sb104.zip
  353.  bp7sb104.zip Borland and TP Source Beautifier, crippleware, J.Ferincz
  354.  :
  355.  25100 Jul 4 1992 ftp://garbo.uwasa.fi/pc/turbopas/epb232.zip
  356.  epb232.zip Ed's Pascal Beautifier, E.Lee
  357.  :
  358. and others at garbo.uwasa.fi available by anonymous FTP or mail
  359. server. See ftp://garbo.uwasa.fi/pc/INDEX.ZIP for the list of the
  360. files.
  361. --------------------------------------------------------------------
  362.  
  363. From ts@uwasa.fi Wed Jul 5 00:00:05 1995
  364. Subject: How to write TSR programs
  365.  
  366. 5. *****
  367.  Q: Can someone give me advice for writing a tsr program?
  368.  
  369.  A: Writing a terminate and stay resident program can be considered
  370. advanced programming and is beyond the scope of an electronic
  371. message with limited space. Instead, here are some references to
  372. Turbo Pascal books and papers which have a coverage of the subject.
  373. Stephen O'Brien, Turbo Pascal, The Complete Reference, Chapter 16;
  374. Stephen O'Brien, Turbo Pascal, Advanced Programmer's Guide, Chapter
  375. 6; Michael Tischer, Turbo Pascal Internals, Chapter 11 (a definite
  376. bible of TP programming!); Michael Tischer (1992), PC Intern System
  377. Programming, Chapter 32; Michael Yester, Using Turbo Pascal, Chapter
  378. 19; Kent Pottebaum, "Creating TSR Programs with Turbo Pascal", Dr.
  379. Dobb's Journal, May 1989 and June 1989; Kris Jamsa, Dos Power User's
  380. Guide, pp. 649-; Edward Mitchell (1993), Borland Pascal Developer's
  381. Guide, Section "Writing TSRs", pp. 370-400, with 778 lines of sample
  382. code.
  383.    Also see example code files like
  384. ftp://garbo.uwasa.fi/pc/turboobj/tsrhelp.zip,
  385. ftp://garbo.uwasa.fi/pc/turbopa45/tess-5.zip,
  386. ftp://garbo.uwasa.fi/pc/turbopa45/tsrunit.zip,
  387. ftp://garbo.uwasa.fi/pc/turbopa7/pptsr10.zip,
  388. ftp://garbo.uwasa.fi/pc/turbopa7/tsrsrc35.zip,
  389. ftp://garbo.uwasa.fi/pc/turbopas/deltsr.zip,
  390. ftp://garbo.uwasa.fi/pc/turbopas/tp4_tsr.zip.
  391. --------------------------------------------------------------------
  392.  
  393. From ts@uwasa.fi Wed Jul 5 00:00:06 1995
  394. Subject: Programming com ports
  395.  
  396. 6. *****
  397.  Q: Why can't I read / write the com ports.
  398.  
  399.  A: Com port programming (most often writing telecommunication
  400. programs) is much much more complicated than simply trying to use
  401.   write (com, whatever);
  402.   read  (com, whatever);
  403. This is a very advanced subject (frankly, beyond me), and the best
  404. way to learn is to try to obtain some code to show you how. One
  405. place to look at is Turbo Pascal text-books (I have a long list of
  406. them at garbo.uwasa.fi archives in my collection of Turbo Pascal
  407. units ftp://garbo.uwasa.fi/pc/ts/tsfaqp3470.zip. There also is an
  408. example by David Rind in ftp://garbo.uwasa.fi/pc/pd2/faquote.zip.
  409. Another source is International FidoNet pascal conference at some
  410. bulletin board near you. The conference has had some very good
  411. discussions in it. (No, I don't have them stored for distribution,
  412. nor any further information.) Some files you might wish to look at:
  413. ftp://garbo.uwasa.fi/pc/turbopas/comm_tp5.zip and comtty30.zip.
  414. --------------------------------------------------------------------
  415.  
  416. From ts@uwasa.fi Wed Jul 5 00:00:07 1995
  417. Subject: Primers to interrupt programming
  418.  
  419. 7. *****
  420.  Q: What are interrupts and how to use them in Turbo Pascal?
  421.  
  422.  A: An interrupt is a signal to the processor from a program, a
  423. hardware device, or the processor itself, to suspend temporarily
  424. what the program is doing, and to perform a routine that is stored
  425. in the operating system. There are 256 such interrupt routines, with
  426. many subservices stored in memory at locations, which are given in
  427. the so called interrupt table. Turbo Pascal (somewhat like C) has a
  428. special keyword Intr, and a predefined variable registers (in the
  429. Dos unit) to access these interrupt routines. One way of looking at
  430. them is as Turbo Pascal (complicated lowlevel) subroutines that are
  431. already there ready for you to call.
  432.    A detailed description of interrupt routines is way beyond a
  433. single message with limited space. Instead, I shall give a simple
  434. example, and good references to the subject. (For a somewhat more
  435. comprehensive description of what an interrupt is, see INTERRUP.PRI
  436. in Ralf Brown's ftp://garbo.uwasa.fi/pc/programming/inter46b.zip.)
  437.      :
  438.      uses Dos;
  439.      (* This procedure turns on the border color for CGA and VGA *)
  440.      procedure BORDER (color : byte);
  441.      var regs : registers;  (* Predeclared in the Dos unit *)
  442.      begin
  443.        FillChar (regs, SizeOf(regs), 0);  (* A precaution *)
  444.        regs.ax := $0B00;    (* Service number *)
  445.        regs.bh := $00;      (* Subservice number *)
  446.        regs.bl := color;
  447.        Intr ($10, regs);    (* ROM BIOS video driver interrupt *)
  448.      end;  (* border *)
  449.    If you are new the subject and / or want ideas on the most useful
  450. interrupts in Turbo Pascal programming, Ben Ezzel (1989),
  451. Programming the IBM User Interface Using Turbo Pascal, is definitely
  452. the best reference to look at. There are also many other good
  453. references for a novice interrupt user, such as Jamsa & Nameroff,
  454. Turbo Pascal Programmer's Library.
  455.    If you are a more advanced interrupt user you'll find the
  456. following references very useful. Michael Tischer (1990), Turbo
  457. Pascal Internals; Norton & Wilton (1988), The New Peter Norton
  458. Programmer's guide to the IBM PC & PS/2; Ray Duncan (1988), Advanced
  459. MS-DOS Programming; Terry Dettmann (1989), Dos Programmer's
  460. Reference, Second edition, Que. Furthermore, there is an impressive
  461. list of interrupts collected and maintained by Ralf Brown. His
  462. extensive ftp://garbo.uwasa.fi:/pc/programming/inter46a.zip,
  463. inter46b.zip, inter46c.zip, inter46d.zip, inter46e.zip and
  464. inter46f.zip (or whatever are the current versions when you read
  465. this) is available by anonymous FTP or mail server from
  466. garbo.uwasa.fi. A definite must for an advanced user. Also see the
  467. reference to Brown's and Kyle's book in the bibliography at the end
  468. of this FAQ. Another useful (but in many respects a replicate) list
  469. is contained in the file ftp://garbo.uwasa.fi/pc/programming/dosref33.zip
  470. (or, sigh, whatever is the current version). There is also a good
  471. hypertext advanced programmer's quick reference
  472. ftp://garbo.uwasa.fi/pc/programming/helppc21.zip which you might
  473. find useful.
  474.    One more point for Turbo Pascal users. When Borland upgraded from
  475. version 3 to 4.0 quite a number of tasks that needed to be done
  476. using interrupts (such as getting the current time) were included as
  477. normal TP routines. This means that while definitely useful,
  478. interrupt programming is now relevant only in advanced Turbo Pascal
  479. programming. Turbo Pascal 5.0 introduced a few more, but you can
  480. find some of the missing TP 4.0 routines in the compatibility unit
  481. in my ftp://garbo.uwasa.fi/pc/ts/tspa3440.zip TP units collection.
  482. --------------------------------------------------------------------
  483.  
  484. From ts@uwasa.fi Wed Jul 5 00:00:08 1995
  485. Subject: Borland's Turbo Pascal upgrades
  486.  
  487. 8. *****
  488.  Q: Should I upgrade my Turbo Pascal version?
  489.  
  490.  A1: Depends on what version you are using, and for what purposes.
  491. If you are using version 3, the answer is a definite yes. There are
  492. so many useful additions in the later version, including the concept
  493. of units, and a great number of new useful keywords. The only reason
  494. that I can think of for using TP 3 is that it makes .com files
  495. (which reside in one memory segment only) instead of .exe files. As
  496. an accounting and business finance teacher and researcher I've been
  497. somewhat surprised to see postings stating that some users still
  498. have to program in TP 3.0 because their employer doesn't want to
  499. take the cost of upgrading. I find this cost argument ridiculous.
  500. How about some consideration for cost effectiveness and
  501. productivity?
  502.    If you are currently using version 4.0, the most important point
  503. in considering upgrading is the integrated debugger in the later
  504. versions. It is really good, and useful if you write much code.
  505. There are some minor considerations, as well. Later versions contain
  506. some useful routines which 4.0 does not. I have programmed many of
  507. them to be available also for 4.0 in my units collection
  508. ftp://garbo.uwasa.fi/pc/ts/tspa3440.zip (or whatever is the latest
  509. when you read this). Furthermore, I find somewhat annoying that the
  510. executables will always end up in the default directory.
  511.    If you are currently using version 5.0 the rational reasons for
  512. upgrading are needing objects, and a better overlay manager. I have
  513. also version 5.5 myself, but switched back to version 5.0 after I
  514. had some problems with its linking of object files. (This is a false
  515. statement from me, since it turned out that I had made a mistake
  516. myself. My thanks are due to bj_stedm@gould2.bristol-poly.ac.uk
  517. (Bruce Stedman) for questioning this item). Anyway, I don't use nor
  518. need OOP objects (don't confuse linking object files and object
  519. oriented programming here). One further point for 5.5. It has a
  520. better help function than 5.0, and a few more procedures and
  521. predefined constants. The TP 5.5 help includes examples, which can
  522. be even pasted into your program. This is handy.
  523.    The real snag in upgrading (waiving the reasonable cost) is the
  524. fact that the units of the different versions are incompatible. If
  525. you have a large library of units (as I do) you will have to
  526. recompile the lot. This is something that has caused a fair amount
  527. of justifiable flak against an otherwise excellent product.
  528.    A tip. Don't throw away your Turbo Pascal version 3.0 manual, if
  529. you have one. It is of use if you resort to the Turbo3 and Graph3
  530. compatibility units. They give you access e.g. to turtle graphics.
  531.    At the time of first writing this Turbo Pascal 6.0 version had
  532. just been announced. I didn't have it yet myself, but I had been
  533. (correctly) informed that its units are not compatible with the
  534. earlier versions. I now have Turbo Pascal 6.0, and I must say that
  535. my reactions have been disappointment and frustration. This is
  536. probably partly (but not entirely) my own fault, since Turbo Pascal
  537. seems to be headed from a common programming language into a full
  538. professional's specialized tool, with many features I don't know how
  539. to utilize. The only advancement from my point of view really is the
  540. multiple file editing, but I have long had alternative programs for
  541. that. If I used assembler (I don't) I am sure that I would find
  542. useful TP 6.0's potential to include assembler code as such instead
  543. of having to use the cumbersome Inline procedure of entering the
  544. assembler code.
  545.    There is also a Windows Turbo Pascal, as the latest addition to
  546. the plethora. Since I don't use Windows at all, I have no further
  547. information on it.
  548.    I think a pattern is emerging here. Rather than being different
  549. versions of the same product, the consecutive Turbo Pascals are
  550. really different products for different purposes. Version 3.0 was a
  551. simple programming language. Version 4.0 extended it into a full
  552. scale programming modular platform. Version 5.0 introduced the
  553. debugger. And there an advanced hobbyist's path ended. Version 5.5
  554. introduced object oriented programming, which I'm sure is important
  555. for the initiated, but personally I just don't need it even if I
  556. write a lot of programs. And with the 6.0 we go completely out of
  557. the realm of conventional programming into Turbo Pascal visions.
  558. And Windows Turbo Pascal is for a different platform, altogether.
  559.    I find the new integrated user interface of TP 6.0 awkward in
  560. comparison to what was used in the 4.0, 5.0, and 5.5 versions. The
  561. IDE of TP leaves less free memory than the previous versions.
  562. Furthermore TP 6.0 IDE performs frequent disk accesses which cause
  563. slowdowns  making it virtually unusable from a floppy. And I
  564. wonder why Borland didn't at once go all the way to Windows, because
  565. that is what 6.0 really is. An intermediate, incomplete step in that
  566. direction. This means that we have a 5th upgrade in line with
  567. incompatible units. This is aggravating even for a TP fan, isn't it?
  568.    For information on Turbo Pascal version 7.0 and Borland email
  569. contact numbers see ftp://garbo.uwasa.fi/pc/turbopa7/bp7-info.zip.
  570. Also see ftp://garbo.uwasa.fi/pc/turbspec/bp7bugs2.zip by Duncan
  571. Murdoch. Turbo Pascal 7.0 or more extensively Borland Pascal 7.0 is
  572. a full professional's tool, and far beyond for example my moderate
  573. programming needs. To list only a few of the features are protected
  574. mode programming, handling of large programs, very fast compiling,
  575. and a daunting amount of material elbowing its away on one's disk
  576. space if one ever has the patience to look through it all. I would
  577. use the word "overwhelming". But for a serious programmer this is an
  578. impressive and a very worthwhile tool. One should not be misled
  579. skipping it because of my comments which were written from a
  580. hobbyist's point of view. As a general trend in programs, the
  581. well-known columnist John C. Dvorak calls this increasing product
  582. complexity trend "featurism" in PC Computing, May 1993. But TP 7.0
  583. (7.01) has some important features also from a hobbyist's point of
  584. view. So much so that I have finally succumbed to 7.01 myself. I
  585. particularly like the color coding of the keywords, and the TPX
  586. version enabling compiling very large programs (utilizing extended
  587. memory). A very welcome addition are the new keywords break and
  588. continue to exit or recycle a for, while or a repeat loop are.
  589. Besides they make using the outcast goto statements virtually
  590. unnecessary.
  591.  
  592.  A2: From: dmurdoch@watstat.waterloo.edu (Duncan Murdoch),
  593. Newsgroups: comp.lang.pascal. Included with Duncan's kind
  594. permission. [Duncan is one of the most knowledgeable and useful
  595. contributors to the comp.lang.pascal UseNet newsgroup (later
  596. replaced by comp.lang.pascal.borland for TP)].
  597.    One other reason:  there's a bug in the code generator for 4.0
  598. and 5.0 that makes it handle the Extended (10 byte) real type
  599. poorly.  The code generated makes very poor use of the 8 element
  600. internal stack on the coprocessor, so that expressions with lots of
  601. operands like
  602.   e1+e2+e3+e4+e5+e6+e7+e8+e9
  603. always fail, if all the e's are of type extended.  (The generated
  604. code pushes each operand onto the stack, then does all the adds.
  605. It's smarter to push and add them one at a time.)
  606.    This makes it a real pain translating numerical routines from
  607. Fortran, especially since constants are taken to be of type
  608. extended.
  609.    The bug was fixed in 5.5.
  610.  
  611.  A3: From: Bengt Oehman (d92bo@efd.lth.se): A difference between
  612. v4.0 and v5.5 is that you can calculate constants in tp55, but not
  613. in 4.0. I see this as a big advantage. For example:
  614.   CONST MaxW = 10;
  615.         MaxH = 20;
  616.         MaxSize = MaxW*MaxH;
  617.         { or }
  618.         MaxX = 100;
  619.         HalfMaxX = MaxX DIV 2;
  620. cannot be compiled with 4.0.
  621. --------------------------------------------------------------------
  622.  
  623. From ts@uwasa.fi Wed Jul 5 00:00:09 1995
  624. Subject: Shelling from a TP program
  625.  
  626. 9. *****
  627.  Q: How do I execute an MS-DOS command from within a TP program?
  628.  
  629.  A: The best way to answer this question is to give an example.
  630.      {$M 2048, 0, 0}   (* <-- Important *)
  631.      program outside;
  632.      uses dos;
  633.      begin
  634.        write ('Directory call from within TP by Timo Salmi');
  635.        SwapVectors;
  636.        Exec (GetEnv('comspec'), '/c dir *.*');  (* Execution *)
  637.        SwapVectors;
  638.        (* Testing for errors is recommended *)
  639.        if DosError <> 0 then
  640.          writeln ('Dos error number ', DosError)
  641.        else
  642.          writeln ('Mission accomplished, exit code ', DosExitCode);
  643.        (* For DosError and DosExitCode details see the TP manual *)
  644.      end.
  645. Alternatively, take a look at execdemo.pas from demos.arc which
  646. should be on the disk accompanying Turbo Pascal.
  647.    What the above Exec does is that it executes the command
  648. processor. The /c specifies that the command interpreter is to
  649. perform the command, and then stop (not halt).
  650.    I have also seen it asked how one can swap the Turbo Pascal
  651. program to the disk when shelling. It is unnecessary to program that
  652. separately because there is an excellent program to do that for you.
  653. It is ftp://garbo.uwasa.fi/pc/sysutil/shrom24b.zip.
  654.    Somewhat surprisingly some users have had difficulties with
  655. redirecting shelled output. It is straight-forward. In the above
  656. code one would use, for example
  657.   Exec (GetEnv('comspec'), '/c dir *.* > tmp');
  658. --------------------------------------------------------------------
  659.  
  660. From ts@uwasa.fi Wed Jul 5 00:00:10 1995
  661. Subject: Millisecond timing
  662.  
  663. 10. *****
  664.  Q: How is millisecond timing done?
  665.  
  666.  A: A difficult task, but the facilities are readily available.
  667. TurboPower Software's commercial Turbo Professional (don't confuse
  668. with Borland's Turbo Professional) has a unit for this. (The usual
  669. disclaimer applies). It is called tptimer and is part of the
  670. ftp://garbo.uwasa.fi/pc/turbopas/bonus507.zip package. This one has
  671. been released to the PD. I have also seen a SimTel upload
  672. announcement of a ztimer11.zip for C and ASM, but I have no further
  673. information on that. ftp://garbo.uwasa.fi/pc/turbopas/qwktimer.zip
  674. is another option. It is not quite as accurate as tptimer.
  675.    To test the tptimer unit in bonus507.zip you can use the
  676. following example code
  677.   uses Crt, tptimer;
  678.   var time1, time2 : longint;
  679.   begin
  680.     InitializeTimer;
  681.     time1 := ReadTimer;
  682.     Delay (1356);    (* Or whatever code you wish to benchmark *)
  683.     time2 := ReadTimer;
  684.     RestoreTimer;
  685.     writeln ('Elapsed = ', ElapsedTime (time1, time2)/1000.0 : 0 : 3);
  686.   end.
  687.    It is quite another question when you really need the millisecond
  688. timing. The most common purpose for millisecond timing is testing
  689. the efficiency of alternative procedures and functions, right? The
  690. way I compare mine is simple. I call the procedures or functions I
  691. want to compare for speed, say, a thousand times in a loop.  And
  692. test this for elapsed time. This way the normal resolution (18.2
  693. cycles per second) of the system clock becomes sufficient. This is
  694. accurate enough for the comparisons.
  695.      var elapsed : real; i : word;
  696.      elapsed := TIMERFN;  (* e.g. from /pc/ts/tspa3455.zip *)
  697.      for i := 1 to 1000 do YOURTEST;  (* Try out the alternatives *)
  698.      elapsed := TIMERFN - elapsed;
  699.      writeln ('Elapsed : ', elapsed : 0 : 2);
  700. Incidentally, if you want to make more elaborate evaluations of the
  701. efficiency of your code, Borland's Turbo Profiler is a useful tool.
  702. (The usual disclaimer naturally applies.)
  703. --------------------------------------------------------------------
  704.  
  705. From ts@uwasa.fi Wed Jul 5 00:00:11 1995
  706. Subject: Text font customizing
  707.  
  708. 11. *****
  709.  Q: How can I customize the text characters to my own liking?
  710.  
  711.  A: As far as I know, text-mode characters are hard-coded, and
  712. cannot be customized at all unless you have an EGA or VGA adapter.
  713. But you can always retrieve the bitmap information for the ascii
  714. characters from your PC.
  715.    The bitmap table for the lower part of the character set (0-127)
  716. starts at memory position $F000 and ends at $FA6E. The upper part is
  717. not at a fixed memory location. The pointer to the memory address of
  718. upper part of the ascii table (provided that graftabl has been
  719. loaded) is at an address $007C. One way of saying this is that the
  720. segment address of the upper part's memory location is at $007E, and
  721. its offset at $007C.
  722.    Going into more details is beyond the scope of this posting. If
  723. you want more information see Michael Tischer (1992), PC Intern
  724. System Programming, "Selecting and Programming Fonts", pp. 197-210.
  725. It also has information on a remotely related task of using sprites
  726. (pp. 305-373), a concept familiar from the days of the Commodore 64
  727. games programming. For another reference to customizing characters
  728. see Kent Porter (1987), Stretching Turbo Pascal, Chapter 12, and
  729. Kent Porter & Mike Floyd (1990), Stretching Turbo Pascal. Version
  730. 5.5. Revised Edition. Brady, Chapter 11.
  731.    If you are interested in a demonstration of utilizing the
  732. bitmapped character information (no source code available), take a
  733. look at the demo in the garbo.uwasa.fi anonymous FTP archives file
  734. ftp://garbo.uwasa.fi/pc/ts/tsdemo16.zip (or whatever version number
  735. is current).
  736.    Turbo Pascal also supports what is called stroked fonts (the
  737. .chr) files which draw characters instead of bitmapping them. The
  738. user should be able to write one's own .chr definitions, but I have
  739. no experience nor information on how this can be done.
  740.    There is something called bgikit10.zip which has facilities for
  741. making fonts and adding graphics drivers. The problem is that I
  742. cannot make it publicly available, since I think that it is not PD.
  743. I am still missing the information. Unfortunately, it is not even
  744. the only case where I encountered the fact that Borland does not
  745. seem at all interested in the UseNet users' queries about the status
  746. and distributability of their material.
  747.    (From Leonard Erickson Leonard.Erickson@f51.n105.z1.fidonet.org
  748. Well, you can *also* do it if you have a Hercules Graphics Card Plus
  749. or Hercules InColor card (as far as I know, the only cards that
  750. implemented Hercules RamFont 'standard'). And you can modify the
  751. upper 128 characters on a CGA card. BTW, the RamFont cards are
  752. *nice* pity it appeared too late to become a standard. It's a *lot*
  753. more flexible than EGA/VGA fonts (I can have several *dozen* fonts
  754. resident).)
  755. --------------------------------------------------------------------
  756.  
  757. From ts@uwasa.fi Wed Jul 5 00:00:12 1995
  758. Subject: Finding files in TP
  759.  
  760. 12. *****
  761.  Q: How to find the files in a directory AND subdirectories?
  762.  
  763.  A: Writing a program that goes through the files of the directory,
  764. and all the subdirectories below it, is based on Turbo Pascal's file
  765. finding commands and recursion. This is universal whether you are
  766. writing, for example, a directory listing program, or a program that
  767. deletes, say, all the .bak files, or some other similar task.
  768.    To find (for listing or other purposes) the files in a directory
  769. you need above all the FindFirst and FindNext keywords, and testing
  770. the predefined file attributes. You make these a procedure, and call
  771. it recursively. If you want good examples with source code, please
  772. see PC World, April 1989, p. 154; Kent Porter & Mike Floyd (1990),
  773. Stretching Turbo Pascal. Version 5.5. Revised Edition, Chapter 23;
  774. Michael Yester (1989), Using Turbo Pascal, p. 437; Michael Tischer
  775. (1992), PC Intern System Programming, pp. 796-798.
  776.    The simple (non-recursive) example listing all the read-only
  777. files in the current directory shows the rudiments of the principle
  778. of Using FindFirst, FindNext, and the file attributes, because some
  779. users find it hard to use these keywords. Also see the code in the
  780. item "How to establish if a name refers to a directory or not?" of
  781. this same FAQ collection you are now reading.
  782.     uses Dos;
  783.     var FileInfo : SearchRec;
  784.     begin
  785.       FindFirst ('*.*', AnyFile, FileInfo);
  786.       while DosError = 0 do
  787.         begin
  788.           if (FileInfo.Attr and ReadOnly) > 0 then
  789.             writeln (FileInfo.Name);
  790.           FindNext (FileInfo);
  791.         end;
  792.     end.  (* test *)
  793.  
  794.  A2: While we are on the subject related to FindFirst and FindNext,
  795. here are two useful examples:
  796.  
  797. (* Number of files in a directory (not counting directories) *)
  798. function DFILESFN (dirName : string) : word;
  799. var nberOfFiles  : word;
  800.     FileInfo     : searchRec;
  801. begin
  802.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  803.   dirName := dirName + '*.*';
  804.   {}
  805.   nberOfFiles := 0;
  806.   FindFirst (dirName, AnyFile, FileInfo);
  807.   while DosError = 0 do
  808.     begin
  809.       if ((FileInfo.Attr and VolumeId) = 0) then
  810.         if (FileInfo.Attr and Directory) = 0 then
  811.           Inc (nberOfFiles);
  812.       FindNext (FileInfo);
  813.     end; {while}
  814.   dfilesfn := nberOfFiles;
  815. end;  (* dfilesfn *)
  816.  
  817. (* Number of immediate subdirectories in a directory *)
  818. function DDIRSFN (dirName : string) : word;
  819. var nberOfDirs : word;
  820.     FileInfo    : searchRec;
  821. begin
  822.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  823.   dirName := dirName + '*.*';
  824.   {}
  825.   nberOfDirs:= 0;
  826.   FindFirst (dirName, AnyFile, FileInfo);
  827.   while DosError = 0 do
  828.     begin
  829.       if ((FileInfo.Attr and VolumeId) = 0) then
  830.         if (FileInfo.Attr and Directory) > 0 then
  831.           if (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then
  832.             Inc (nberOfDirs);
  833.       FindNext (FileInfo);
  834.     end; {while}
  835.   ddirsfn := nberOfDirs;
  836. end;  (* ddirsfn *)
  837. --------------------------------------------------------------------
  838.  
  839. From ts@uwasa.fi Wed Jul 5 00:00:13 1995
  840. Subject: A generic power function code for TP
  841.  
  842. 13. *****
  843.  Q: I need a power function but there is none in Turbo Pascal.
  844.  
  845.  A: Pascals do not have an inbuilt power function. You have to write
  846. one yourself. The common, but non-general method is defining
  847.    function POWERFN (number, exponent : real) : real;
  848.      begin
  849.        powerfn := Exp(exponent*Ln(number));
  850.      end;
  851. To make it general use:
  852.    (* Generalized power function by Prof. Timo Salmi *)
  853.    function GENPOWFN (number, exponent : real) : real;
  854.    begin
  855.      if (exponent = 0.0) then
  856.        genpowfn := 1.0
  857.      else if number = 0.0 then
  858.        genpowfn := 0.0
  859.      else if abs(exponent*Ln(abs(number))) > 87.498 then
  860.        begin writeln ('Overflow in GENPOWFN expression'); halt; end
  861.      else if number > 0.0 then
  862.        genpowfn := Exp(exponent*Ln(number))
  863.      else if (number < 0.0) and (Frac(exponent) = 0.0) then
  864.        if Odd(Round(exponent)) then
  865.          genpowfn := -GENPOWFN (-number, exponent)
  866.        else
  867.          genpowfn :=  GENPOWFN (-number, exponent)
  868.      else
  869.        begin writeln ('Invalid GENPOWFN expression'); halt; end;
  870.    end;  (* genpowfn *)
  871. On the lighter side of things an extract from an answer of mine in
  872. the late comp.lang.pascal UseNet newsgroup:
  873.  >anyone point out why X**Y is not allowed in Turbo Pascal?
  874.    The situation in TP is a left-over from standard
  875.    Pascal. You'll recall that Pascal was originally
  876.    devised for teaching programming, not for
  877.    something as silly and frivolous as actually
  878.    writing programs.  :-)
  879. --------------------------------------------------------------------
  880.  
  881. From ts@uwasa.fi Wed Jul 5 00:00:14 1995
  882. Subject: Arrays > 64K
  883.  
  884. 14. *****
  885.  Q: How can I create arrays that are larger than 64 kilobytes?
  886.  
  887.  A: Turbo Pascal does not directly support the so-called huge arrays
  888. but you can get by this problem with a clever use of pointers as
  889. presented in Kent Porter, "Handling Huge Arrays", Dr.Dobb's Journal,
  890. March 1988. In this method you point to an element of a two
  891. dimensional array using a^[row].col^[column]. The idea involves too
  892. much code and explanation to be repeated here, so you'll have to see
  893. the original reference. But I know from my own experience, that the
  894. code works like magic. (The code is available from garbo.uwasa.fi
  895. archives as ftp://garbo.uwasa.fi/pc/turbopas/ddj8803.zip). Kent
  896. Porter, "Huge Arrays Revisited", Dr.Dobb's Journal, October 1988,
  897. presents the extension of the idea to huge virtual arrays. (Virtual
  898. arrays mean arrays that utilize disk space).
  899.    Another possibility is using TurboPower Software's (the usual
  900. disclaimer applies) commercial Turbo Professional (don't confuse
  901. with Borland's Turbo Professional) package. It has facilities for
  902. huge arrays, but they involve much more overhead than Kent Porter's
  903. excellent method.
  904.  
  905. (* =================================================================
  906.    My code below is based on a UseNet posting in the late
  907.    comp.lang.pascal by Naji Mouawad nmouawad@watmath.waterloo.edu.
  908.    Naji's idea was for a vector, my adaptation is for a
  909.    two-dimensional matrix. The realization of the idea is simpler
  910.    than the one presented by Kent Porter in Dr.Dobb's Journal, March
  911.    1988. (Is something wrong, this is experimental.)
  912.    ================================================================= *)
  913.    {}
  914.    const maxm = 150;
  915.          maxn = 250;
  916.    {}
  917.    type BigVectorType = array [1..maxn] of real;
  918.         BigMatrixType = array [1..maxm] of ^BigVectorType;
  919.    {}
  920.    var BigAPtr : BigMatrixType;
  921.    {}
  922.    (* Create the dynamic variables *)
  923.    procedure MAKEBIG;
  924.    var i          : word;
  925.        heapNeeded : longint;
  926.    begin
  927.      heapNeeded := maxm * maxn * SizeOf(real) + maxm * 4 + 8196;
  928.      if (MaxAvail <= heapNeeded) then
  929.        begin writeln ('Out of memory'); halt; end;
  930.      for i := 1 to maxm do New (BigAPtr[i]);
  931.    end;  (* makebig *)
  932.    {}
  933.    (* Test that it works *)
  934.    procedure TEST;
  935.    var i, j : word;
  936.    begin
  937.      for i := 1 to maxm do
  938.        for j := 1 to maxn do
  939.          BigAPtr[i]^[j] := i * j;
  940.      {}
  941.      writeln (BigAPtr[5]^[7] : 0:0);
  942.      writeln (BigAPtr[maxm]^[maxn] : 0:0);
  943.    end;  (* test *)
  944.    {}
  945.    (* The main program *)
  946.    begin
  947.      writeln ('Big arrays test by Prof. Timo Salmi, Vaasa, Finland');
  948.      writeln;
  949.      MAKEBIG;
  950.      TEST;
  951.    end.
  952. (For a better test of the heap than in MAKEBIG see Swan (1989), pp.
  953. 462-463.)
  954. --------------------------------------------------------------------
  955.  
  956. From ts@uwasa.fi Wed Jul 5 00:00:15 1995
  957. Subject: Testing printer status
  958.  
  959. 15. *****
  960.  Q: How can I test that the printer is ready?
  961.  
  962.  A: Strictly speaking there is no guaranteed way to detect the
  963. printer status on a PC. As Brian Key Brian@fantasia.demon.co.uk
  964. wrote "Any book dealing with the PC BIOS support of a printer will
  965. quickly show you that there is no hardware definition which deals
  966. with the printer power status.  It simply wasn't designed (and I use
  967. the word loosely!) into the IBM hardware specs."
  968.  
  969.    The usually advocated method in Turbo Pascal is to test the
  970. status of regs.ah after a call to interrupt 17 Hex (the parallel
  971. port driver interrupt), service 02:
  972.       regs.dx := PrinterNumber;  (* LPT1 = 0 *)
  973.       regs.ah := $02;            (* var regs : registers, uses DOS *)
  974.       Intr ($17,regs);           (* Interrupt 17 Hex *)
  975.       status := regs.ah          (* var status : byte *)
  976. But this is not a good method since the combinations of the status
  977. bits which indicate a ready state can vary from printer to printer
  978. and PC to PC. If you want a list of the status bits, see e.g. Ray
  979. Duncan (1988), Advanced MS-DOS Programming, p. 587. For an example
  980. of a code using interrupt 17 Hex see Douglas Stivison (1986), Turbo
  981. Pascal Library, pp. 118-120. Also see Michael Yester (1989), Using
  982. Turbo Pascal, pp. 494-495.
  983.    The more generic alternative is to try to write a #13 to the
  984. printer having the i/o checking off, that is, while {$I-} is in
  985. effect, and testing the IOResult. But then you must first alter the
  986. printer retry times default (and restore it afterwards). Else the
  987. method can take up to a minute instead of an immediate response.
  988. Also, you must have set the FileMode for LPT1 appropriately (and
  989. restore it afterwards). Sounds a bit complicated, but you don't have
  990. to do all this yourself. There is a boolean function "LPTONLFN Get
  991. the online status of the first parallel printer" for this purpose in
  992. my ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version
  993. number is the latest) Turbo Pascal units collection available by
  994. anonymous FTP or mail server from garbo.uwasa.fi.
  995. --------------------------------------------------------------------
  996.  
  997. From ts@uwasa.fi Wed Jul 5 00:00:16 1995
  998. Subject: Clearing the keyboard buffer
  999.  
  1000. 16. *****
  1001.  Q: How can I clear the keyboard type-ahead buffer?
  1002.  
  1003.  A: Three methods are usually suggested for solving this problem.
  1004. a) The first is to use something like
  1005.      uses Crt;
  1006.      while KeyPressed do ReadKey;
  1007. This kludge-type method has the disadvantage of requiring the Crt
  1008. unit. Also, in connection with procedures relying on ReadKey for
  1009. input, it may cause havoc on the programs logic.
  1010. b) The second method accesses directly the circular keyboard buffer
  1011.      var head : word absolute $0040:$001A;
  1012.          tail : word absolute $0040:$001C;
  1013.      procedure FLUSHKB; begin head := tail; end;
  1014. For a slightly different formulation of the same method see Michael
  1015. Tischer (1992), PC Intern System Programming, p. 462.
  1016. c) The third method is to call interrupt 21Hex (the MS-DOS
  1017. interrupt) with the ax register set as $0C00. This method has the
  1018. advantage of not being "hard-coded" like the second method, and thus
  1019. should be less prone to incompatibility.
  1020. --------------------------------------------------------------------
  1021.  
  1022. From ts@uwasa.fi Wed Jul 5 00:00:17 1995
  1023. Subject: Utilizing expanded memory
  1024.  
  1025. 17. *****
  1026.  Q: How can I utilize expanded memory (EMS) in my programs?
  1027.  
  1028.  A: I have no experience (yet?) on this subject myself, but I can
  1029. give you a list of references: Michael Tischer (1990), Turbo Pascal
  1030. Internals, Abacus, Chapter 9; Michael Tischer (1992), PC Intern
  1031. System Programming, Chapter 12; Stephen O'Brien (1988), Turbo
  1032. Pascal, Advanced Programmer's Guide, Borland-Osborne, Chapter 4;
  1033. Chris Ohlsen & Gary Stoker (1989), Turbo Pascal Advanced Techniques,
  1034. Que, Chapter 11, Robert Jourdain (1992), Programmer's Problem
  1035. Solver, 2nd ed., Brady Publishing, pp. 68-87, and, maybe most
  1036. importantly, Dorfman & Neuberger, Turbo Pascal Memory Management
  1037. Techniques (with lots of code).
  1038.    Furthermore, Turbo Pascal delivery disks (at least 5.0) contain a
  1039. demos.arc archive which includes an ems.pas file.
  1040. --------------------------------------------------------------------
  1041.  
  1042. From ts@uwasa.fi Wed Jul 5 00:00:18 1995
  1043. Subject: Capturing the entire command line
  1044.  
  1045. 18. *****
  1046.  Q: How can I obtain the entire command line (spaces and all)?
  1047.  
  1048.  A: ParamCount and ParamStr are for parsed parts of the command line
  1049. and cannot be used to get the command line exactly as it was. See
  1050. what happens if you try to capture
  1051.   "Hello.   I'm here"
  1052. you'll end up with a false number of blanks. For obtaining the
  1053. command line unaltered use
  1054.   type CommandLineType = string[127];
  1055.   var  CommandLinePtr  : ^CommandLineType;
  1056.   begin
  1057.     CommandLinePtr := Ptr(PrefixSeg, $80);
  1058.     writeln (CommandLinePtr^);
  1059.   end;
  1060. A warning. If you want to get this correct (the same goes for TP's
  1061. own ParamStr and ParamCount) apply them early in your program. At
  1062. least they must be used before any disk I/O takes place!
  1063. :
  1064. A related example demonstrating a function giving the number of
  1065. characters on the command line
  1066.   function CMDNBRFN : byte;
  1067.   var paramPtr : ^byte;
  1068.   begin
  1069.     paramPtr := Ptr (PrefixSeg, $80);
  1070.     cmdnbrfn := paramPtr^
  1071.   end;  (* cmdnbrfn *)
  1072. For the contents of the Program Segment Prefix (PSP) see Tischer,
  1073. Michael (1992), PC Intern System Programming, p. 753.
  1074. --------------------------------------------------------------------
  1075.  
  1076. From ts@uwasa.fi Wed Jul 5 00:00:19 1995
  1077. Subject: Redirecting from printer to file
  1078.  
  1079. 19. *****
  1080.  Q: How do I redirect text from printer to file in my TP program?
  1081.  
  1082.  A: Simple. This is done in Turbo Pascal by using the assign command
  1083. (think what the word 'assign' implies). Here is a simple example of
  1084. the idea.
  1085.   uses Printer;
  1086.   begin
  1087.     assign (lst, 'printer.log');
  1088.     rewrite (lst);
  1089.     writeln (lst, 'Hello world');
  1090.     close (lst);
  1091.   end.
  1092. --------------------------------------------------------------------
  1093.  
  1094. From ts@uwasa.fi Wed Jul 5 00:00:20 1995
  1095. Subject: Turbo Pascal users are just wimps
  1096.  
  1097. 20. *****
  1098.  Q: Turbo Pascal is for wimps. Why don't you use standard Pascal or
  1099. better still why don't you use C?
  1100.  
  1101.  A: These kinds of "real-programmers" statements often reflect what
  1102. is called self-over-others attitude, and they are a part of a kind
  1103. of a programming lore or cult. Basically, these attitudes waive the
  1104. simple fact that one should select one's tools according to the task
  1105. at hand, not vice versa. On the other hand one's productivity is
  1106. usually best when being able to use tools which one is familiar and
  1107. comfortable with. (Note however that the real-programmer's lore is
  1108. not really interested in producing results.)
  1109.    In very rough terms there are two attitudes to programming
  1110. languages. They can be seen as tools for writing applications, or
  1111. (by surprisingly many) as ends themselves.
  1112.    If we first look at standard Pascal (versus Turbo Pascal),
  1113. considering the language primary and its usage secondary is common.
  1114. This results from the history of Pascal, since as we all know it was
  1115. originally meant as a means for teaching programming concepts, not
  1116. at all for writing applications. But because Pascal turned out to be
  1117. useful also for writing applications, it has been extended for some
  1118. operating systems, most notably MS-DOS (Turbo Pascal) and VAX/VMS
  1119. (VAX Pascal). Both remedy a lot of flaws from the application
  1120. programmer's point of view. Most importantly they have a true file
  1121. I/O interface, and enhanced string handling. Turbo Pascal (the more
  1122. generic of these two) clearly draws from the structure and ideas of
  1123. advanced BASICs (and vice versa). While in standard Pascal the
  1124. language is an end by itself, for Turbo Pascal the only relevant
  1125. issue is its usefulness for writing applications.
  1126.    One problem that one encounters when moving away from standard
  1127. Pascal is the problem of portability. This is a truly serious
  1128. problem, since most often extensive rewriting is necessary from
  1129. converting say Turbo Pascal to, say, Unix Pascal. I have taken Unix
  1130. Pascal as the extreme example, since Unix Pascal is almost nothing
  1131. but the standard Pascal having no useful file I/O.
  1132.    If one considers C, its best aspect from applications point of
  1133. view is portability, and its strength for system programming. But it
  1134. is not an easy language to learn. Proponents of C also often have
  1135. the tendency discussed above, that is seeing the language as
  1136. primary, and its utilization as secondary. Now why this tendency,
  1137. not only for C, but in general? I've had the opportunity of writing
  1138. programs starting from the late 1960's, and one observation I have
  1139. made, and often propounded the view is that it is not writing code
  1140. that is the really difficult part. What is really difficult it is
  1141. coming up with good and original ideas for programs to write. I see
  1142. applications as primary, and the tools as secondary. As to Turbo
  1143. Pascal, I've written in many languages (including Cobol, Fortran,
  1144. several Basics and Pascals, and command languages) and I like Turbo
  1145. Pascal because it is one of the most convenient and flexible tools
  1146. for writing the kind of applications that I usually write and
  1147. distribute for the Public Domain. That is I use Turbo Pascal because
  1148. I'm comfortable with it in writing applications, and have thus
  1149. gathered a very useful modular library for it over the years, not
  1150. because of any inherent value attached to Turbo Pascal per se.
  1151.  
  1152.  A2: Another, a somewhat resembling line is made up by the arguments
  1153. about standards in Pascal which were recycled in the late
  1154. comp.lang.pascal time after time. Very often they end up with
  1155. purists vs pragmatists arguing about the true or imaginary viles of
  1156. using GOTOs. I find all this somewhat futile, although I understand
  1157. the academic nature of the background. As you'll recall, Pascal was
  1158. first developed for academic teaching programming concepts, not for
  1159. any practical programming. That came later, and the ensuing
  1160. popularity of Pascal in practical applications must have come as a
  1161. surprise way back then. I admit being biased in not sympathizing
  1162. with Pascal standard stalwarts. I am far more interested in getting
  1163. the job done than in defending a barren orthodoxy.
  1164.    Since Turbo Pascal version 7.0 introduced the "break" and
  1165. "continue" keywords to handle jumps in loops, GOTOs are much easier
  1166. to avoid without undue complications.
  1167. --------------------------------------------------------------------
  1168.  
  1169. From ts@uwasa.fi Wed Jul 5 00:00:21 1995
  1170. Subject: Turning off the cursor
  1171.  
  1172. 21. *****
  1173.  Q: How do I turn the cursor off?
  1174.  
  1175.  A: The usually advocated trick for turning the cursor off is to
  1176. equate the lower and the upper scan line of the cursor as explained
  1177. e.g. in Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  1178. Guide.
  1179.      uses Dos;
  1180.      var regs : registers;
  1181.      begin
  1182.        regs.ax := $0100;   (* Service $01 *)
  1183.        regs.cl := $20;     (* Top scan line *)
  1184.        regs.ch := $20;     (* Bottom scan line *)
  1185.        Intr ($10, regs);   (* ROM BIOS video driver interrupt *)
  1186.      end;
  1187. To turn the cursor back on this (and many other) sources suggest
  1188. setting regs.ch and regs.cl as 12 and 13 for mono screen, and 6 and
  1189. 7 for others.
  1190.    This is not a good solution since it is equipment dependent, and
  1191. may thus produce unexpected results. Better to store the current
  1192. scan line settings, and turn off the cursor bit. Below is the code
  1193. from ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version
  1194. number is the latest) available by anonymous FTP from garbo.uwasa.fi
  1195. archives. The general idea is that regs.ch bit 5 toggles the cursor
  1196. on / off state. Thus to set the cursor off, apply
  1197.   regs.ch := regs.ch or $20;    (* $20 = 00100000 *)
  1198. and to set it on, apply
  1199.   regs.ch := regs.ch and $DF;   (* $DF = 11011111 *)
  1200. (* From TSUNTE unit, which also has a CURSON procedure *)
  1201. procedure CURSOFF;
  1202. var regs : registers;
  1203. begin
  1204.   FillChar (regs, SizeOf(regs), 0);  (* Initialize, a precaution *)
  1205.   {... find out the current cursor size (regs.ch, regs.cl) ...}
  1206.   regs.ah := $03;
  1207.   regs.bh := $00;    (* page 1, superfluous because of FillChar *)
  1208.   Intr ($10, regs);  (* ROM BIOS video driver interrupt *)
  1209.   {... turn off the cursor without changing its size ...}
  1210.   regs.ah := $01;                   (* Below are bits 76543210 *)
  1211.   regs.ch := regs.ch or $20;  (* Turn on bit 5; $20 = 00100000 *)
  1212.   Intr ($10, regs);
  1213. end;  (* cursoff *)
  1214.  
  1215.  A2: A comment from Leonard Erickson leonard@qiclab.scn.rain.com.
  1216. Reprinted with permission. There's a *reason* those sources don't
  1217. suggest storing the current scan line settings. On IBM Monochrome
  1218. Display Adapters (MDA), Hercules Graphics Cards, and the various
  1219. clones of both, the "read cursor start and end scan lines" function
  1220. *always* returns the same values. And those values are almost never
  1221. the actual settings. Most cards return 6 & 7. Some return 12 & 13.
  1222. But they return these values even if the cursor has been set to
  1223. something else.
  1224.    So you are *still* stuck with checking the hardware type if the
  1225. screen is in mode 7.
  1226.    See the Interrupt list for details on this mess.
  1227.  
  1228.  A3: Another solution that has been suggested is putting the cursor
  1229. outside the screen.  But you can't do this with the Crt's GotoXY
  1230. procedure, since it ignores off screen positions, as observed by
  1231. Luiz Marques luiz.marques%mandic@ibase.org.br. You'll need to use
  1232. video interrupt, that is $10, function $02. Fair enough, but
  1233. somewhat complicated. Besides, how do you write on the screen if the
  1234. cursor position is off it?
  1235.  
  1236.  A4: This snippet of disabling the cursor at hardware level was
  1237. posted to the late comp.lang.pascal by JAB@ib.rl.ac.uk.
  1238.   procedure turn_off_cursor;
  1239.   var num : word;
  1240.   begin
  1241.     port[$03D4]:=$0A; num:=port[$03D5];
  1242.     port[$03D4]:=$0A; port[$03D5]:=num or 32;
  1243.   end;
  1244.   {}
  1245.   procedure turn_on_cursor;
  1246.   var num : word;
  1247.   begin
  1248.     port[$03D4]:=$0A; num:=port[$03D5];
  1249.     port[$03D4]:=$0A; port[$03D5]:=num xor 32;
  1250.   end;
  1251.  
  1252.  A5: (Not to be taken seriously). Simple, turn off your computer and
  1253. the cursor stops showing :-).
  1254. --------------------------------------------------------------------
  1255.  
  1256. From ts@uwasa.fi Wed Jul 5 00:00:22 1995
  1257. Subject: Finding the roots of a polynomial
  1258.  
  1259. 22. *****
  1260.  Q: How to find all roots of a polynomial?
  1261.  
  1262.  A: If you need the code, see Turbo Pascal Numerical Toolbox and/or
  1263. Press & Flannery & Teukolsky & Vetterling (1986), Numerical Recipes,
  1264. The Art of Scientific Computing, Cambridge University Press. The
  1265. Numerical Recipes codes are available as /pc/turbopas/nrpas13.zip
  1266. (big, 404k!). If you just need to solve such a task (without code
  1267. available), get ftp://garbo.uwasa.fi/pc/ts/tsnum12.zip (or whatever
  1268. version number is the latest) from garbo.uwasa.fi archives by
  1269. anonymous FTP or mail server.
  1270. --------------------------------------------------------------------
  1271.  
  1272. From ts@uwasa.fi Wed Jul 5 00:00:23 1995
  1273. Subject: Pascal homework on the net
  1274.  
  1275. 23. *****
  1276.  Q: What is all this talk about "Pascal homework on the net"?
  1277.  
  1278.  A: This is one of the subjects that seems to pop up at regular
  1279. intervals, cause some heated exchange for awhile, and then die down
  1280. again leaving some users harboring warranted or unwarranted grudges.
  1281.    Some posters to comp.lang.pascal (later comp.lang.pascal.borland)
  1282. have been very concerned of the possibility that the questions posed
  1283. on the net are related to students' homework assignments. I don't
  1284. have any unequivocal answers or a clear-cut stand on this question,
  1285. just some comments.
  1286.    The most important task of a newsgroup like comp.lang.pascal.borland
  1287. is the exchange of information between the users. If you think that
  1288. what you are going to post is interesting and useful to the group,
  1289. that should be your topmost criterion.
  1290.    If it is really a student that wants his/her work done on the net
  1291. (how do we know anyway?) also consider the following fact. Being
  1292. able to use a newsgroup amounts to having learned at least something
  1293. about using computers, and that is something per se.
  1294.    Even if the student may short-sightedly not realize it, providing
  1295. ALL the code for a student's homework is detrimental to the student,
  1296. since it is she/he that foregoes understanding what he/she is doing.
  1297. The group should not condone outright cheating. Being (partly) a
  1298. teacher myself, I understand also this view.
  1299.    If a student is stuck with a problem in his/her code, I don't see
  1300. any real harm in helping out, especially if the problem has general
  1301. interest. Instructing is what teaching is about, anyway, isn't it?
  1302.    But, on the other hand, I must admit that I find a it rather
  1303. flagrant if a posting asks for something of the kind "I have to
  1304. complete my term assignment to write a function plotter by the end
  1305. of this month. Send me the code, since I'm too busy with my other
  1306. exams to write it myself" (a true quote).
  1307.    Finally, let's not jump to premature conclusions about anyone's
  1308. questions.  That's what most often triggers off a vicious circle of
  1309. flaming.
  1310. --------------------------------------------------------------------
  1311.  
  1312. From ts@uwasa.fi Wed Jul 5 00:00:24 1995
  1313. Subject: Linking bgi drivers into executables
  1314.  
  1315. 24. *****
  1316.  Q: How can I link graphics drivers directly into my executable?
  1317.  
  1318.  A: This is a complicated, yet a very useful task, because then you
  1319. won't need any separate graphics drivers (or fonts) to go separately
  1320. along with your program. Unfortunately, Turbo Pascal documentation
  1321. on this task is a bit confusing.
  1322.    1) The very first step is to get the necessary files from the
  1323. Turbo Pascal disks to your working directory. To start with, you'll
  1324. need binobj.exe and all the .bgi files.
  1325.    2) Run the following commands (best to place them in a batch,
  1326. call it e.g. makeobj.bat):
  1327.      binobj cga.bgi cga CGADriverProc
  1328.      binobj egavga.bgi egavga EGAVGADriverProc
  1329.      binobj herc.bgi herc HercDriverProc
  1330.      binobj pc3270.bgi pc3270 PC3270DriverProc
  1331.      binobj att.bgi att ATTDriverProc
  1332.      rem binobj ibm8514.bgi 8514 IBM8514DriverProc
  1333.    3) Get drivers.pas from the Turbo Pascal disk and compile it with
  1334. Turbo Pascal. Now you have a drivers.tpu unit which contains all the
  1335. graphics drivers.
  1336.    4) Now you won't need the .bgi and the .obj files any more. You
  1337. may delete them from your working directory.
  1338.    5) Write your graphics program in the usual manner. But before
  1339. putting your program in the graphics mode use the following
  1340. procedure if you want to link e.g. the EGAVGA graphics driver
  1341. directly into your executable. (Link just the driver(s) you'll need,
  1342. since the drivers take up a lot of space.)
  1343.      uses Graph, Drivers;
  1344.      :
  1345.      procedure EGAVGA2EXE;
  1346.      begin
  1347.        if RegisterBGIdriver(@EGAVGADriverProc) < 0 then
  1348.          begin
  1349.            writeln ('EGA/VGA: ', GraphErrorMsg(GraphResult));
  1350.            halt(1);
  1351.          end;
  1352.      end; (* egavga2exe *)
  1353.      :
  1354.    Linking the .bgi and .chr drivers is also covered in Swan (1989),
  1355. Mastering Turbo Pascal 5.5 pp. 355-359 and Mitchell (1993), Borland
  1356. Pascal Developer's Guide , pp. 221-229.
  1357.    Incidentally, although this is a slightly different matter, you
  1358. can link any data material into your executable. See Stephen
  1359. O'Brien, (1988), Turbo Pascal, Advanced Programmer's Guide, pp. 31 -
  1360. 35 for more details.
  1361. --------------------------------------------------------------------
  1362.  
  1363. From ts@uwasa.fi Wed Jul 5 00:00:25 1995
  1364. Subject: Trapping runtime errors
  1365.  
  1366. 25. *****
  1367.  Q: How can I trap a runtime error?
  1368.  
  1369.  A: What you are probably asking for is a method writing a program
  1370. termination routine of your own. To do this, you have to replace
  1371. Turbo Pascal's ExitProc with your own customized exec procedure.
  1372. Several Turbo Pascal text books show ho to do this. See e.g. Tom
  1373. Swan (1989), Mastering Turbo Pascal 5.5, Third edition, Hayden
  1374. Books, pp. 440-454; Michael Yester (1989), Using Turbo Pascal, Que,
  1375. pp. 376-382; Stephen O'Brien (1988), Turbo Pascal, Advanced
  1376. Programmer's Guide, pp. 28-30; Tom Rugg & Phil Feldman (1989), Turbo
  1377. Pascal Programmer's Toolkit, Que, pp. 510-515. Here is an example
  1378.   var OldExitProcAddress : Pointer;
  1379.       x : real;
  1380.   {$F+} procedure MyExitProcedure; {$F-}
  1381.   begin
  1382.     if ErrorAddr <> nil then
  1383.       begin
  1384.         writeln ('Runtime error number ', ExitCode, ' has occurred');
  1385.         writeln ('The error address in decimal is ',
  1386.                   Seg(ErrorAddr^):5,':',Ofs(ErrorAddr^):5);
  1387.         writeln ('That''s all folks, bye bye');
  1388.         ErrorAddr := nil;
  1389.         ExitCode  := 0;
  1390.       end;
  1391.     {... Restore the pointer to the original exit procedure ...}
  1392.     ExitProc := OldExitProcAddress;
  1393.   end;  (* MyExitProcedure *)
  1394.   (* Main *)
  1395.   begin
  1396.     OldExitProcAddress := ExitProc;
  1397.     ExitProc := @MyExitProcedure;
  1398.     x := 7.0; writeln (1.0/x);
  1399.     x := 0.0; writeln (1.0/x);   {The trap}
  1400.     x := 7.0; writeln (4.0/x);   {We won't get this far}
  1401.   end.
  1402. :
  1403. Actually, I utilize this idea in my /pc/ts/tspa3470.zip Turbo Pascal
  1404. units collection, which includes a TSERR.TPU. If you put TSERR in
  1405. your program's uses statement, all the run time errors will be given
  1406. verbally besides the usual, cryptic error number. That's all there
  1407. is to it. That is, the inclusion to the uses statement to your main
  1408. program (if you have the program in several units) is all you have
  1409. to do to enable this handy feature.
  1410. :
  1411. Hans.Siemons@f149.n512.z2.fidonet.org notes "This line:
  1412.     ExitProc := OldExitProcAddress;
  1413. should IMHO never be placed at the end of your exit handler. If for
  1414. one reason or another your own handler should cause a runtime error,
  1415. it would go in an endless loop. If the first statement restores the
  1416. exit chain, this can never happen. I do agree that is not very
  1417. likely that your exit handler produces any runtime error, but it
  1418. performs I/O, and since it is located in a FAQ, people are bound to
  1419. use, and maybe extend it with more tricky stuff."
  1420. --------------------------------------------------------------------
  1421.  
  1422. From ts@uwasa.fi Wed Jul 5 00:00:26 1995
  1423. Subject: Using ansi codes in a TP program
  1424.  
  1425. 26. *****
  1426.  Q: How to get ansi control codes working in Turbo Pascal writes?
  1427.  
  1428.  A: It is very simple, but one has to be aware of the pitfalls.
  1429. Let's start from the assumption that ansi.sys or a corresponding
  1430. driver has been loaded, and that you know ansi codes. If you don't,
  1431. you'll find that information in the standard MS-DOS manual. To apply
  1432. ansi codes you just include the ansi codes in your write statements.
  1433. For example the following first clears the screen and then puts the
  1434. text at location 10,10:
  1435.    write (#27, '[2J');         (* the ascii code for ESC is 27 *)
  1436.    write (#27, '[10;10HUsing ansi codes can be fun');
  1437. If you want to test (as you should) whether ansi.sys or some some
  1438. replacement driver has been loaded, you can use the ISANSIFN
  1439. function from my ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  1440. Now the catches. If you have a
  1441.    uses Crt;
  1442. statement in your program, direct screen writes will be used, and
  1443. the ansi codes won't work. You have either to leave out the Crt
  1444. unit, or include
  1445.    assign (output, '');
  1446.    rewrite (output);
  1447.    :
  1448.    close (output);
  1449. Occasionally I have seen it suggested that one should just set
  1450.    DirectVideo := false;
  1451. This is a popular misconception. It won't produce the desired
  1452. result. I'm not claiming to know the reason for this quirk of Turbo
  1453. Pascal. Rather it is an observation I've made.
  1454.  
  1455. -From: Bengt Oehman d92bo@efd.lth.se with a later dicussion with Bob
  1456. Peck bpeck@prairienet.org and help from Duncan Murdoch
  1457. dmurdoch@mast.queensu.ca. The `DirectVideo:=False' statement only
  1458. tells the Crt unit to use BIOS calls instead of using direct
  1459. video-memory writes. A demo program to illustrate the screen writing
  1460. modes follows:
  1461.  
  1462. Program ScreenWriteDemo;
  1463. USES Crt;
  1464. BEGIN
  1465.   Writeln('This is written directly to the video memory');
  1466.   DirectVideo:=False;
  1467.   Writeln('This is written via BIOS interrupt calls (int 10h)');
  1468.   Assign(Output,'');
  1469.   Append(Output);
  1470.   Writeln('This is written via DOS calls (int 21h)');
  1471. END.
  1472.  
  1473. A note: The latter could be also written as
  1474.   Writeln(Output, 'This is written via DOS calls (int 21h)');
  1475. since the writeln default is the standard output.
  1476. --------------------------------------------------------------------
  1477.  
  1478. From ts@uwasa.fi Wed Jul 5 00:00:27 1995
  1479. Subject: Writing an expression parser
  1480.  
  1481. 27. *****
  1482.  Q: How to evaluate a function given as a string to the program?
  1483.  
  1484.  A: To do this you have to have a routine for parsing and evaluating
  1485. your expression. This is a complicated task requiring a clever use
  1486. of recursion. You can find such code in Stephen O'Brien (1988),
  1487. Turbo Pascal, The Complete Reference. Borland-Osborne/McGraw-Hill,
  1488. Chapter 10. Another, simpler piece of code can be found in Michael
  1489. Yester (1989), Using Turbo Pascal, Que, Chapter 5.
  1490.    I've also written such a function evaluation program myself, and
  1491. much of it is based on the ideas in O'Brien with my own corrections
  1492. and enhancements. The resulting program is available as fn.exe
  1493. function evaluator in the ftp://garbo.uwasa.fi/pc/ts/tsfunc13.zip
  1494. package (or whatever version number is the latest). Note however,
  1495. that the source code is not included, nor available.
  1496.    Tips from Justin Lee (ossm1jl@rex.uokhsc.edu):
  1497.  67666 Sep 22 03:00 ftp://garbo.uwasa.fi/pc/turboobj/parstp30.zip
  1498.  parstp30.zip Recursive expression TP7.0/BP/VB/C++ parser, R.Loewy
  1499. An excellent parser is included with all the Turbo Pascal versions
  1500. since TP4.0 as part of the MCALC or TCALC spreadsheet example
  1501. program. See mcparse.pas or tcparse.pas.
  1502. --------------------------------------------------------------------
  1503.  
  1504. From ts@uwasa.fi Wed Jul 5 00:00:28 1995
  1505. Subject: Detecting redirection
  1506.  
  1507. 28. *****
  1508.  Q: How does one detect whether input (or output) is redirected?
  1509.  
  1510.  A: As we know input to a program can come from a file, from the
  1511. console, or from a pipe or redirection. Examples of the latter are
  1512.      type text.dat | program
  1513.      program < text.dat
  1514. A Turbo Pascal program can be made to detect the redirections using
  1515. Interrupt 21Hex, function 44Hex, subfunction 00Hex. See PC Magazine
  1516. April 16, 1991, p. 374 for the code, and Duncan (1988), Advanced
  1517. MS-DOS Programming, pp. 412-413 for more information. Alternatively,
  1518. you can utilize the preprogrammed routines
  1519.   PIPEDIFN Is the standard input from redirection
  1520.   PIPEDNFN Is the standard output redirected to nul
  1521.   PIPEDOFN Is the standard output redirected
  1522. from my ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip units.
  1523. --------------------------------------------------------------------
  1524.  
  1525. From ts@uwasa.fi Wed Jul 5 00:00:29 1995
  1526. Subject: Setting the 43/50 line text mode
  1527.  
  1528. 29. *****
  1529.  Q: How does one set the 43/50 line text mode?
  1530.  
  1531.  A: Quite simple. Just apply TextMode (C80 + font8x8).  Requires a
  1532. "uses Crt;". First, however, you should test that you have a at
  1533. least an EGA video adapter. (See DetectGraph in your TP manual).
  1534. Also see TSUTLE.NWS in ftp://garbo.uwasa.fi/pc/ts/tsutle22.zip (or
  1535. whichever version number is the current) for the non-standard wide
  1536. text modes like 132x43.
  1537.   { An example }
  1538.   uses Crt;
  1539.   var InitialMode : integer;
  1540.   begin
  1541.     InitialMode := LastMode;
  1542.     TextMode (CO80 + Font8x8);
  1543.     TextColor (LightCyan);
  1544.     writeln ('Test1');
  1545.     readln;
  1546.     {}
  1547.     TextMode (CO40);
  1548.     writeln ('Test2');
  1549.     readln;
  1550.     {}
  1551.     TextMode (InitialMode);
  1552.     TextColor (Yellow);
  1553.     writeln ('Test3');
  1554.     readln;
  1555.   end.
  1556. --------------------------------------------------------------------
  1557.  
  1558. From ts@uwasa.fi Wed Jul 5 00:00:30 1995
  1559. Subject: Assigning environment variable values
  1560.  
  1561. 30. *****
  1562.  Q: How can I assign a value to an environment variable in TP?
  1563.  
  1564.  A: For assigning a value to (a parent process's) environment value
  1565. you have to access and manipulate the Program Segment Prefix and
  1566. Memory Control Blocks. This is a rather complicated undertaking. A
  1567. source code with an accompanying article by Trudy Neuhaus can be
  1568. found in PC Magazine Volume 11 Number 1 pages 425-427.
  1569.    The budding TP programmers should note that the elementary trick
  1570. of Exec (GetEnv('comspec'), '/c set key=whatever') will achieve only
  1571. a transient result for the duration of the exec shell. When you exit
  1572. the shell after this endeavor, the environment will be as it was.
  1573.    Here is about the why. When the above command is executed, MS-DOS
  1574. makes a copy of the environment, and uses the copy. When the above
  1575. shelling terminates, the copy of the environment is deleted, and the
  1576. original is restored. Hence the above trick cannot be used to change
  1577. the parent environment.
  1578.    If you don't want to try to go through this rather complicated
  1579. task yourself, the routines
  1580.  "SETEVN   Set a parent environment variable (variable=value)"
  1581.  "SETENVSH Set an environment variable for the duration of shelling"
  1582. can be found in my TP TPU collection ftp://garbo.uwasa.fi/pc/ts/
  1583. tspa34*.zip (* = 40,50,55,60,70). No source code is included, nor
  1584. available, though. From zeta@tcscs.com Gregory Youngblood: For
  1585. a source code see /pc/source/setenv.zoo at Garbo.
  1586.    One further detail. Users sometimes ask how one can change the
  1587. prompt or the path from within a Turbo Pascal program. This is in no
  1588. way different from changing the value of any other environment
  1589. variable. Both PATH and PROMPT are environment variables that can be
  1590. set with the MS-DOS SET command in the fashion described in the
  1591. above. This is not changed in any way by the fact that you can apply
  1592. PROMPT and PATH also in an alternative format not requiring the SET
  1593. command.
  1594. --------------------------------------------------------------------
  1595.