home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / TP.7_1 / TP / DOC / UTILS.DOC < prev    next >
Encoding:
Text File  |  1992-10-05  |  15.0 KB  |  389 lines

  1. =======================================================================
  2.                        Turbo Pascal Utilities
  3. =======================================================================
  4.  
  5. -----------------------------------------------------------------------
  6.                          Table of Contents
  7. -----------------------------------------------------------------------
  8.  1. The TOUCH utility
  9.  2. The GREP utility
  10.      The GREP switches
  11.      How to search using GREP
  12.      Examples using GREP
  13.  3. The BINOBJ utility
  14. -----------------------------------------------------------------------
  15.  
  16. This file describes three stand-alone utility programs that come
  17. with Borland Pascal: TOUCH, GREP, and BINOBJ.
  18.  
  19. ======================
  20.  1. The TOUCH Utility
  21. ======================
  22.  
  23. There are times when you want to force a particular target file
  24. to be recompiled or rebuilt, even though no changes have been
  25. made to its sources. One way to do this is to use the TOUCH
  26. utility included with Turbo Pascal. TOUCH changes the date and
  27. time of one or more files to the current date and time, making it
  28. "newer" than the files that depend on it.
  29.  
  30. To force a target file to be rebuilt, "touch" one of the files
  31. that target depends on. To touch a file (or files), enter
  32.  
  33.    touch filename [ filename ... ]
  34.  
  35. at the DOS prompt. TOUCH will then update the file's creation
  36. date(s).
  37.  
  38. Once you do this, you can invoke MAKE to rebuild the touched
  39. target file(s).
  40.  
  41.  
  42. =====================
  43.  2. The GREP Utility
  44. =====================
  45.  
  46. GREP is a powerful search utility that can look for text in
  47. several files at once.
  48.  
  49. The command-line syntax for GREP follows:
  50.  
  51.    GREP [options] searchstring [filespec ... ]
  52.  
  53. where options consists of one or more single characters preceded
  54. by a hyphen, searchstring defines the pattern to search for, and
  55. filespec is the file specification. filespec tells GREP which
  56. files (or groups of files) to search; it can be an explicit file
  57. name or a generic file name incorporating the DOS wildcards (?
  58. and *). You can also enter a path as part of filespec; if you use
  59. filespec without a path, GREP only searches the current
  60. directory. If you don't specify filespec, input to GREP must be
  61. specified by redirecting stdin or piping.
  62.  
  63.  
  64.  The GREP Switches
  65. ===================
  66.  
  67. In the command line, options are one or more single characters
  68. preceded by a hyphen (-). Each individual character is a switch
  69. that you can turn on or off: type the plus symbol (+) after a
  70. character to turn the option on, or type a hyphen (-) after the
  71. character to turn the option off.
  72.  
  73. The default is on (the + is implied): for example, -R means the
  74. same thing as -R+. You can list multiple options individually
  75. like this: -I -D -L). Or you can combine them like this: -ILD or
  76. -IL -D, and so on). It's all the same to GREP.
  77.  
  78. Here is a list of the switches and their meanings:
  79.  
  80.    -C   Count only: Only a count of matching lines is printed.
  81.         For each file that contains at least one matching line,
  82.         GREP prints the file name and a count of the number of
  83.         matching lines. Matching lines are not printed.
  84.  
  85.    -D   Directories: For each filespec specified on the command
  86.         line, GREP searches for all files that match the file
  87.         specification, both in the directory specified and in all
  88.         subdirectories below the specified directory. If you give
  89.         a filespec without a path, GREP assumes the files are in
  90.         the current directory.
  91.  
  92.    -I   Ignore case: GREP ignores upper/lowercase differences
  93.         (case folding). GREP treats all letters a-z as being
  94.         identical to the corresponding letters A-Z in all
  95.         situations.
  96.  
  97.    -L   List match files: Only the name of each file containing a
  98.         match is printed. After GREP finds a match, it prints the
  99.         file name and processing immediately moves on to the next
  100.         file.
  101.  
  102.    -N   Numbers: Each matching line that GREP prints is preceded
  103.         by its line number.
  104.  
  105.    -O   UNIX output format: Changes the output format of matching
  106.         lines to support more easily the UNIX style of
  107.         command-line piping. All lines of output are preceded by
  108.         the name of the file which contained the matching line.
  109.  
  110.    -R   Regular expression search: The text defined by
  111.         searchstring is treated as a regular expression instead
  112.         of as a literal string.
  113.  
  114.    -U   Update options: GREP will combine the options given on
  115.         the command line with its default options and write these
  116.         to the GREP.COM file as the new defaults. (In other
  117.         words, GREP is self-configuring.) This option allows you
  118.         to tailor the default option settings to your own taste.
  119.  
  120.    -V   Non-match: Only non-matching lines are printed. Only
  121.         lines that do not contain the search string are
  122.         considered to be non-matching lines.
  123.  
  124.    -W   Word search: Text found which matches the regular
  125.         expression will be considered a match only if the
  126.         character immediately preceding and following cannot be
  127.         part of a word. The default word character set includes
  128.         A-Z, 9-0, and the underbar (_). An alternate form of this
  129.         option allows you to specify the set of legal word
  130.         characters. Its form is -W[set], where set is any valid
  131.         regular expression set definition. If alphabetic
  132.         characters are used to define the set, the set will
  133.         automatically be defined to contain both the upper and
  134.         lower case values for each letter in the set, regardless
  135.         of how it is typed, even if the search is case-sensitive.
  136.         If the -W option is used in combination with the -U
  137.         option, the new set of legal characters is saved as the
  138.         default set.
  139.  
  140.    -Z   Verbose: GREP prints the file name of every file
  141.         searched. Each matching line is preceded by its line
  142.         number. A count of matching lines in each file is given,
  143.         even if the count is zero.
  144.  
  145. Several of these options are in direct conflict with each other.
  146. In these cases, the following order applies (the first one is the
  147. one that takes precedence):
  148.  
  149.    -Z   -L   -C   -N
  150.  
  151. Each occurrence of an option overrides the previous definition:
  152. Its state reflects the way you last set it. At any given time,
  153. each option can only be on or off.
  154.  
  155. You can install your preferred default setting for each option in
  156. GREP.COM with the -U option. For example, if you want GREP to
  157. always do a verbose search (-Z on), you can install it with the
  158. following command:
  159.  
  160.    GREP -U -Z
  161.  
  162.  
  163.  How to Search Using GREP
  164. ==========================
  165.  
  166. The value of searchstring defines the pattern GREP will search
  167. for.  A search string can be either a (via the -R switch) or a
  168. literal string. In regular expressions, operators govern the
  169. search; literal strings have no operators.
  170.  
  171. You can enclose the search string in quotation marks to prevent
  172. spaces and tabs from being treated as delimiters. Matches will
  173. not cross line boundaries (a match must be contained in a single
  174. line).
  175.  
  176. When the -R switch is used, the search string is treated as a
  177. regular expression (as opposed to a literal expression), and the
  178. following symbols take on special meanings:
  179.  
  180.    ^   A caret at the start of the expression matches the start
  181.        of a line.
  182.  
  183.    $   A dollar sign at the end of the expression matches the end
  184.        of a line.
  185.  
  186.    .   A period matches any character.
  187.  
  188.    *   An expression followed by an asterisk wildcard matches
  189.        zero or more occurrences of that expression: fo* matches
  190.        f, fo, foo, etc.
  191.  
  192.    +   An expression followed by a plus sign matches one or more
  193.        occurrences of that expression: fo+ matches fo, foo, etc.,
  194.        but not f.
  195.  
  196.    []  A string enclosed in brackets matches any character in
  197.        that string, but no others. If the first character in the
  198.        string is a caret (^), the expression matches any
  199.        character except the characters in the string. For
  200.        example, [xyz] matches x, y, and z, while [^xyz] matches a
  201.        and b, but not x or y. A range of characters can be
  202.        specified by two characters separated by a hyphen (-).
  203.        These can be combined to form expressions like [?a-bd-z]
  204.        to match ? and any letter except c.
  205.  
  206.    \   The backslash "escape character" tells GREP to seach for
  207.        the literal character that follows it. For example, \.
  208.        matches a period instead of any character.
  209.  
  210.    Note: Four characters (?, +, *, and .) do not have any special
  211.    meaning when used in a set. The character ^ is only treated
  212.    specially if it immediately follows the beginning of the set
  213.    (that is, immediately after the [).
  214.  
  215. Any ordinary character not mentioned in this list matches that
  216. character. A concatenation of regular expressions is a regular
  217. expression.
  218.  
  219.  
  220.  Examples Using GREP
  221. =====================
  222.  
  223. The following examples assume all options default to off.
  224.  
  225. ----------------------------------------------------------------
  226.  
  227. Search String   grep -n function dirdemo.pas
  228.  
  229. Finds       File DIRDEMO.PAS:
  230.             46      LessFunc = function(X, Y: DirPtr): Boolean;
  231.             55      function NumStr(N, D: Integer): string;
  232.             68      function LessName(X, Y: DirPtr): Boolean;
  233.             73      function LessSize(X, Y: DirPtr): Boolean;
  234.             78      function LessTime(X, Y: DirPtr): Boolean;
  235.  
  236. Remarks     Finds all functions in the file DIRDEMO.PAS. The -N
  237.             tells GREP to precede each matched line with its line
  238.             number.
  239.  
  240. -----------------------------------------------------------------
  241.  
  242. Search String   grep {\$ dirdemo.pas
  243.  
  244. Finds       File DIRDEMO.PAS:
  245.             {$I-,S-}
  246.             {$M 8192,8192,655360}
  247.             {$F+}
  248.             {$F-}
  249.  
  250. Remarks     Finds all compiler directives in DIRDEMO.PAS. The \
  251.             (backslash) preceding the $ is necessary. Without it,
  252.             the $ would indicate the end of the line. All lines
  253.             with { (curly bracket) as the last character would
  254.             match this pattern and be printed out.
  255.  
  256. -----------------------------------------------------------------
  257.  
  258. Search String   grep -i "^ *function.*).*real" *.pas
  259.  
  260. Finds       File MCPARSER.PAS:
  261.             function CellValue(Col, Row: Word): Real;
  262.             function Parse(S: string; var Att: Word): Real;
  263.  
  264. Remarks     Finds all lines that begin with zero or more spaces
  265.             followed by the word function, followed by any string
  266.             of zero or more characters, followed by a
  267.             parenthesis, followed by another string of zero or
  268.             more characters, followed by the word Real, and
  269.             ignores case. The net effect is to search for all
  270.             functions returning a Real. See if you can think of
  271.             other ways to do this.
  272.  
  273.             The double quotes are necessary because of the space
  274.             in the pattern string. The quotes tell the DOS
  275.             command-line processor to treat the intervening
  276.             characters as a single argument. Without the quotes,
  277.             DOS will think the search string is actually two
  278.             arguments, and GREP will think that everything after
  279.             ^ (the caret character) refers to file names, and
  280.             will complain
  281.  
  282.             No files matching: *FUNCTION.*).*.
  283.  
  284.  
  285. =======================
  286.  3. The BINOBJ Utility
  287. =======================
  288.  
  289. A utility program called BINOBJ.EXE has been added to convert any
  290. file to an .OBJ file so it can be linked into a pascal program as
  291. a "procedure." This is useful if you have a binary data file that
  292. must reside in the code segment or is too large to make into a
  293. typed constant array. For example, you can use BINOBJ with the
  294. Graph unit to link the graphics driver or font files directly
  295. into your .EXE file. Then, to use your graph program, you need
  296. only have the .EXE file (see the example BGILINK.PAS).
  297.  
  298. BINOBJ takes three parameters:
  299.  
  300.    BINOBJ  <source[.BIN]>  <destination[.OBJ]>  <public name>
  301.  
  302. where source is the binary file to convert, destination is the
  303. name of the .OBJ to be produced, and public name is the name of
  304. the procedure as it will be declared in your pascal program.
  305.  
  306. The following example, the procedure ShowScreen, takes a pointer
  307. as a parameter and moves 4000 bytes of data to screen memory. The
  308. file called MENU.DTA contains the image of the main menu screen
  309. (80 * 25 * 2 = 4000 bytes).
  310.  
  311. Here's a simple (no error-checking) version of MYPROG.PAS:
  312.  
  313.    program MyProg;
  314.  
  315.    procedure ShowScreen(var ScreenData : Pointer);
  316.    { Display a screenful of data--no error-checking! }
  317.    var
  318.      ScreenSegment: Word;
  319.  
  320.    begin
  321.      if (Lo(LastMode) = 7) then      { Mono? }
  322.        ScreenSegment := $B000
  323.      else
  324.        ScreenSegment := $B800;
  325.      Move(@^ScreenData^,             { From pointer }
  326.       Ptr(ScreenSegment, 0)^,        { To video memory }
  327.       4000);                         { 80 * 25 * 2 }
  328.    end;
  329.  
  330.    var
  331.      MenuP : Pointer;
  332.      MenuF : file;
  333.    begin
  334.      Assign(MenuF, 'MENU.DTA');      { Open screen data file }
  335.      Reset(MenuF, 1);
  336.      GetMem(MenuP, 4000);            { Allocate buffer on heap }
  337.      BlockRead(MenuF, MenuP^, 4000); { Read screen data }
  338.      Close(MenuF);
  339.      ShowScreen(MenuP);              { Display screen }
  340.    end.
  341.  
  342. The screen data file (MENU.DTA) is opened and then read into a
  343. buffer on the heap. Both MYPROG.EXE and MENU.DTA must be present
  344. at run-time for this program to work. You can use BINOBJ to
  345. convert MENU.DTA to an .OBJ file (MENUDTA.OBJ) and tell it to
  346. associate the data with a procedure called MenuData. Then you can
  347. declare the fake external procedure MenuData, which actually
  348. contains the screen data. Once you link in the .OBJ file with the
  349. $L compiler directive, MenuData will be 4000 bytes long and
  350. contain your screen data. First, run BINOBJ on MENU.DTA:
  351.  
  352.    binobj MENU.DTA MENUDTA MenuData
  353.  
  354. The first parameter, MENU.DTA, shows a familiar file of screen
  355. data; the second, MENUDTA, is the name of the .OBJ file to be
  356. created (since you didn't specify an extension, .OBJ will be
  357. added). The last parameter, MenuData, is the name of the external
  358. procedure as it will be declared in your progam. Now that you've
  359. converted MENU.DTA to an .OBJ file, here's what the new
  360. MYPROG.PAS looks like:
  361.  
  362.    program MyProg;
  363.  
  364.    procedure ShowScreen(ScreenData : Pointer);
  365.    { Display a screenful of data--no error checking! }
  366.    var
  367.      ScreenSegment: Word;
  368.    begin
  369.      if (Lo(LastMode) = 7) then             { Mono? }
  370.        ScreenSegment := $B000
  371.      else
  372.        ScreenSegment := $B800;
  373.      Move(@^ScreenData^,                    { From pointer }
  374.       Ptr(ScreenSegment, 0)^,               { To video memory }
  375.       4000);                                { 80 * 25 * 2 }
  376.    end;
  377.  
  378.    procedure MenuData; external;
  379.    {$L MENUDTA.OBJ }
  380.    begin
  381.      ShowScreen(@MenuData);                 { Display screen }
  382.    end.
  383.  
  384. Notice that ShowScreen didn't change at all, and that the ADDRESS
  385. of your procedure is passed using the @ operator.
  386.  
  387.  
  388. *  *  *  *  *
  389.