home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / inc-dec / source / lib / nshc_utl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-20  |  2.4 KB  |  129 lines  |  [TEXT/KAHL]

  1. /* ========================================
  2.  
  3.     nshc_utl.c
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.     Revisions:
  14.     
  15.     10/20/94    John Jensen        Rewrite nshc_is_numeric_operand to test length
  16.     
  17.    ======================================== */
  18.    
  19. #include "nshc.h"
  20.                     
  21. #include "nshc_utl.proto.h"
  22.  
  23. // ===== version tracking ======
  24.                 
  25. int nshc_bad_version(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, int version)
  26. {
  27.     if ( nshc_parms->version != version ) {
  28.         nshc_calls->NSH_putStr_err("\pThis command is not of a compatible version.\r");
  29.         nshc_parms->result = NSHC_ERR_VERSION;
  30.         nshc_parms->action = nsh_idle;
  31.         return(1);
  32.         }
  33.     else
  34.         return(0);
  35. }
  36.  
  37. // ===== option exploration routines =====
  38.  
  39. int nshc_got_option(t_nshc_parms *nshc_parms, char option)
  40. {
  41.     char    c;
  42.     int        pos;
  43.     int        arg;
  44.     int     found;
  45.     int        bad_format;
  46.     
  47.     arg = 1;
  48.     found = 0;
  49.     
  50.     while (!found && (arg < nshc_parms->argc)) {
  51.         pos = nshc_parms->argv[arg];
  52.         if ( nshc_parms->arg_buf[pos++] == '-' ) {
  53.             bad_format = 0;
  54.             while (c = nshc_parms->arg_buf[pos++]) {
  55.                 if (c == option) found = arg;
  56.                 if ( ((( c < 'a' ) || ( c > 'z' )) && (( c < 'A' ) || ( c > 'Z' ))) )
  57.                     bad_format = 1;
  58.                 }
  59.             }
  60.         if (bad_format)
  61.             found = 0;
  62.         arg++;
  63.         }
  64.         
  65.     return( found );
  66. }
  67.  
  68. int nshc_is_numeric_operand(t_nshc_parms *nshc_parms, int arg)
  69. {
  70.     int        good;
  71.     int        count;
  72.     char    c;
  73.     char    *arg_str;
  74.  
  75.     arg_str = &nshc_parms->arg_buf[nshc_parms->argv[arg]];
  76.     
  77.     count = 0;                                // keep track of argument length
  78.  
  79.     if ( *arg_str == '-' ) {                // leading '-' is ok
  80.         arg_str++;
  81.         count++;
  82.         }
  83.         
  84.     good = 1;
  85.  
  86.     while( good && ( c = *arg_str++ ) ) {
  87.     
  88.         if ((c < '0') || (c > '9'))            // everything else should be a numeral
  89.             good = 0;
  90.             
  91.         count++;
  92.         
  93.         }
  94.     
  95.     if ( count > 255 )                        // reject such big numbers out of hand
  96.         good = 0;
  97.  
  98.     return( good );
  99. }
  100.  
  101. int nshc_is_operand(t_nshc_parms *nshc_parms, int arg)
  102. {
  103.     int        pos;
  104.     char    c;
  105.     
  106.     pos = nshc_parms->argv[arg];
  107.     
  108.     c = nshc_parms->arg_buf[pos++];
  109.     
  110.     return( c != '-' );
  111. }
  112.  
  113. int nshc_next_operand(t_nshc_parms *nshc_parms, int start)
  114. {
  115.     int found;
  116.     int arg;
  117.     
  118.     arg = start + 1;
  119.     found = 0;
  120.     
  121.     while (!found && ( arg < nshc_parms->argc )) {
  122.         if (nshc_is_operand( nshc_parms, arg ))
  123.             found = arg;
  124.         arg++;
  125.         }
  126.         
  127.     return( found );
  128. }
  129.