home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol132 / readcmd.p < prev    next >
Encoding:
Text File  |  1984-04-29  |  7.3 KB  |  140 lines

  1.    PROCEDURE readcmd(VAR arg: wrdarray; VAR numargs: byte);
  2. {  
  3.          +------------------------------------------------------------+
  4.          |WRITTEN FOR ZUG USE.                  PASCAL Z, version 4.0 |
  5.          |BY       Clif Kinne                   DATE:  12/15/82       |
  6.          |                                                            |
  7.          |PURPOSE  This procedure was designed to read and parse the  |
  8.          |         command tail on a program call.                    |
  9.          |                                                            |
  10.          |CALL:    Readcmd(name,nargs)   - typical.                   |
  11.          |                                                            |
  12.          |INPUT    Reads a string, LINE, of up to 80 characters from  |
  13.          |DATA           from the console.                            |
  14.          |                                                            |
  15.          |RETURNS: 1. The words from LINE in an array, ARG[1..maxargs]|
  16.          |         2.  NARGS, the actual number of words found.       |
  17.          |         3.  Dummy 1-character arguments to pad out the     |
  18.          |              array from numargs to maxargs.                |
  19.          |                                                            |
  20.          |GLOBALS  CONST maxword  = Max. no. of characters in a word  |
  21.          |REQUIRED       maxargs  = Max. no. of arguments allowed     |
  22.          |               maxline  = 80;                               |
  23.          |                                                            |
  24.          |         TYPE  wrdtype  = string maxword;                   |
  25.          |               wrdarray = ARRAY[1..maxargs] OF wrdtype;     |
  26.          |               byte     = 0..255;                           |
  27.          |                                                            |
  28.          |         VAR   <argname>: wrdarray;                         |
  29.          |                                                            |
  30.          |FEATURES 1.  Truncates long words to MAXWORD characters.    |
  31.          |         2.  If command tail is empty, invites operator to  |
  32.          |              enter arguments, continue without arguments,  |
  33.          |              or quit.                                      |
  34.          |         3.  Trailing dummy arguments can signal calling    |
  35.          |              program it has asked for unavailable argument.|
  36.          |         4.  Can have up to 5 14-character or 38 1-character|
  37.          |              arguments, or any mixture up to 77 characters.|
  38.          |         5.  Even though MAXARGS arguments are allowed, any |
  39.          |             lesser no. of arguments, including none, may be|
  40.          |             entered. If none are needed, the command should|
  41.          |             be followed by 2 spaces to avoid the reminder. |
  42.          |                                                            |
  43.          |BUGS     Does not handle tab characters satisfactorily.     |
  44.          +------------------------------------------------------------+
  45. .pa}
  46.     CONST   blank     = ' ';
  47.             dummy     = '\';
  48.  
  49.     TYPE    linetype  = string maxline;
  50.             string0   = string 0;                         
  51.             string255 = string 255;                       
  52.  
  53.     VAR     pos,i     : byte;
  54.             line      : linetype;
  55.  
  56.     FUNCTION length(x: string255): INTEGER; external;  
  57.  
  58.     PROCEDURE reminder; 
  59.  
  60. {           +------------------------------------------------------+
  61.             |This procedure is intended for use with the procedure,|
  62.             |READCMD. It can be made external to READCMD so that it|
  63.             |may be tailored to the calling program.               |
  64.             +------------------------------------------------------+
  65. }
  66.       BEGIN {----------------------------reminder-----------------------------}
  67.  
  68.         WRITELN(margin,
  69.             'You neglected to include any arguments with the program call.'); 
  70.         WRITELN; WRITELN(margin,
  71.                'Please enter them now (or enter "Q" to quit, or RETURN to');
  72.         WRITELN; WRITELN(margin,'continue without arguments).'); 
  73.         WRITELN; WRITELN; WRITE(margin,'===> ')
  74.   
  75.       END;  {----------------------------reminder-----------------------------}
  76.  
  77.     PROCEDURE linetoolong;
  78.  
  79.       BEGIN {---------------------------linetoolong---------------------------}
  80.  
  81.         WRITELN(margin,'Command line is too long.'); WRITELN;
  82.         WRITELN(margin,'Reenter arguments here.  Do not exceed',
  83.                         (maxline - 3):3,' characters.');
  84.         WRITELN; READLN(line)
  85.  
  86.       END;  {---------------------------linetoolong---------------------------}
  87. {.p.a}
  88.     PROCEDURE getword(VAR word: string0; VAR line: linetype; VAR pos: byte);
  89.             
  90. {         +----------------------------------------------------------+
  91.           |This procedure receives a string, LINE, and a character   |
  92.           |position, POS.                                            |
  93.           |It extracts the first group of non-space characters after |
  94.           |POS, and returns it to the calling program as WORD.       |
  95.           |Long words will be truncated at MAXWORD characters.       |
  96.           |POS is returned to the calling program so that additional |
  97.           |words may be extracted.                                   |
  98.           +----------------------------------------------------------+
  99. }
  100.       CONST   blank = ' ';
  101.   
  102.       PROCEDURE setlength(VAR x: string0; y: INTEGER); external;
  103.   
  104.       BEGIN {-----------------------------getword-----------------------------}
  105.   
  106.         WHILE ((line[pos] = blank) OR (line[pos]= CHR(9))) AND (pos<maxline) DO
  107.           pos := pos+1; {SKIPBLANKS}
  108.         setlength(word,0);                 {INITIALIZES word TO A NULL STRING.}
  109.         WHILE (line[pos] <> blank) AND (line[pos] <> CHR(9)) AND 
  110.               (length(word) < maxword) DO
  111.           BEGIN
  112.             APPEND(word,line[pos]);        {BUILDS THE STRING, word.          }
  113.             pos := pos + 1
  114.           END;
  115.         WHILE (line[pos] <> blank) AND (line[pos] <> CHR(9)) DO 
  116.           pos := pos + 1    {SKIPS OVER ANY EXTRA NON-BLANK CHARACTERS IN word}
  117.       END;  {-----------------------------getword-----------------------------}
  118.  
  119.     BEGIN {-------------------------------readcmd-----------------------------}
  120.  
  121.       pos := 1;
  122.       IF EOLN THEN reminder;         {REPORT NO COMMAND LINE ARGUMENTS AT ALL }
  123.       READLN(line); WRITELN;
  124.       WHILE length(line) > (maxline - 3) DO linetoolong;
  125.       APPEND(line,blank);            {APPEND DUMMY ARGUMENT TO SIGNIFY NO MORE}
  126.       APPEND(line,dummy);            {ARGUMENTS IN THE COMMAND LINE           }
  127.       FOR i := length(line) TO maxline - 1 DO append(line,blank);
  128.                       {NEEDED SO THAT getword WILL BEHAVE AFTER DUMMY ARGUMENT}
  129.       numargs := 0;
  130.       REPEAT                         {GET ACTUAL ARGUMENTS IN ARRAY, arg, AND }
  131.         numargs := numargs + 1;      {THE COUNT OF THE ARGUMENTS IN numargs:  }
  132.         getword(arg[numargs],line,pos)
  133.       UNTIL (arg[numargs] = dummy) OR (numargs = maxargs);  
  134.       FOR i := numargs + 1 TO maxargs DO arg[i] := dummy;  {PAD ARGUMENT LIST }
  135.                                           {OUT TO maxargs WITH DUMMY ARGUMENTS}
  136.       IF arg[numargs] = dummy THEN numargs := numargs - 1;     {ADJUST numargs}
  137.       
  138.     END;  {-----------------------------readcmd ------------------------------}
  139.  
  140.