home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / read-shift / source / shift.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-16  |  3.0 KB  |  140 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     shift.c - an nShell(tm) command.
  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.    ========== the commmand file: ========== */
  14.    
  15. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  16. #include <A4Stuff.h>
  17. #endif
  18.  
  19. #include "Folders.h"
  20.  
  21. #include "nshc.h"
  22.  
  23. #include "arg_utl.proto.h"
  24. #include "str_utl.proto.h"
  25. #include "nshc_utl.proto.h"
  26.  
  27. /* ======================================== */
  28.  
  29. // We do this all in one pass to try to speed things up.
  30. // It's still a slow process, what with creating and searching
  31. // for all the variables based upon their names (strings).
  32. // Perhaps in future versions I might save numeric variables
  33. // with numeric (binary) keys.  That should speed things up.
  34.  
  35. int shift(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  36.  
  37. int shift(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  38. {
  39.     OSErr    result;
  40.     long    distance;
  41.     int        count;
  42.     int        to_num;
  43.     Str32    to_name;
  44.     int        from_num;
  45.     Str32    from_name;
  46.     Str255    value;
  47.     
  48.     result = 0;
  49.     
  50.     // bail if bad parameters
  51.     
  52.     if ( nshc_parms->argc > 2 ) {
  53.         nshc_calls->NSH_putStr_err( "\pUsage: shift [distance]\r" );
  54.         return( NSHC_ERR_PARMS );
  55.         }
  56.  
  57.     if ( nshc_parms->argc == 2 ) {
  58.         if ( nshc_is_numeric_operand( nshc_parms, 1) )
  59.             distance = arg_to_int( nshc_parms, 1 );
  60.         else {
  61.             nshc_calls->NSH_putStr_err( "\pshift: Distance parameter must be an integer.\r" );
  62.             return( NSHC_ERR_PARMS );
  63.             }
  64.         }
  65.     else
  66.         distance = 1;
  67.         
  68.     // this loop shifts down variables the specified distance
  69.         
  70.     to_num = 0;
  71.     from_num = distance;
  72.     count = 0;
  73.         
  74.     while ( !result ) {
  75.     
  76.         NumToString( from_num, from_name );
  77.         result = nshc_calls->NSH_var_env( from_name, value );
  78.         
  79.         if ( !result ) {
  80.             NumToString( to_num, to_name );
  81.             result = nshc_calls->NSH_var_set( to_name, value );
  82.             count++;
  83.             }
  84.             
  85.         to_num++;
  86.         from_num++;
  87.         }
  88.         
  89.     // set $# to be the number of numeric parameters remaining
  90.     
  91.     NumToString( count, value );
  92.     nshc_calls->NSH_var_set( "\p#", value );
  93.  
  94.     // delete extra variables from the end of the list
  95.     
  96.     for ( to_num = count ; to_num < count + distance ; to_num++ ) {
  97.         NumToString( to_num, to_name );
  98.         nshc_calls->NSH_var_unset( to_name );
  99.         }
  100.         
  101.     return( !count );
  102. }
  103.     
  104. /* ======================================== */
  105.  
  106. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  107. {
  108.     OSErr    result;
  109.     
  110. #ifdef __MWERKS__
  111.     long oldA4  = SetCurrentA4();
  112. #endif
  113.     
  114.     if (!nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) {
  115.     
  116.         // otherwise, handle requests from the application
  117.     
  118.           switch (nshc_parms->action) {
  119.             case nsh_start:
  120.                 result = shift( nshc_parms, nshc_calls );
  121.                 break;
  122.             case nsh_continue:
  123.             case nsh_idle:
  124.             case nsh_stop:
  125.                 result = NSHC_NO_ERR;
  126.                 break;
  127.             }
  128.         
  129.         // tell the application we are done
  130.  
  131.          nshc_parms->action = nsh_idle;
  132.         nshc_parms->result = result;
  133.  
  134.         }
  135.         
  136. #ifdef __MWERKS__
  137.     SetA4(oldA4);        // CodeWarrior needs to restore A4
  138. #endif
  139. }
  140.