home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / commands / gestalt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-24  |  2.7 KB  |  129 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     gestalt.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.    ========== the commmand file: ========== */
  14.  
  15. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  16. #include <A4Stuff.h>
  17. #endif
  18.  
  19. #include <GestaltEqu.h>
  20.  
  21. #include "nshc.h"
  22.                     
  23. #include "nshc_utl.proto.h"
  24.  
  25. int is_OSType(t_nshc_parms *nshc_parms, int arg, OSType *selector);
  26.  
  27. int is_OSType(t_nshc_parms *nshc_parms, int arg, OSType *selector)
  28. {
  29.     char    c;
  30.     char    *p;
  31.     int        i;
  32.     int        success;
  33.     
  34.     success = 1;
  35.     *selector = 0;
  36.     
  37.     p = &nshc_parms->arg_buf[ nshc_parms->argv[ arg ] ];
  38.     
  39.     for (i = 0 ; i < 4 ; i++ )
  40.         if ( c = *p++ ) {
  41.             *selector = *selector << 8;
  42.             *selector = *selector + c;
  43.             }
  44.         else
  45.             success = 0;
  46.             
  47.     if (*p) success = 0;
  48.     
  49.     return(success);
  50. }
  51.     
  52. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  53. {
  54.     int        arg;
  55.     int        return_status;
  56.     OSErr    error;
  57.     OSType    selector;
  58.     long    response;
  59.     char    num_option;
  60.     
  61. #ifdef __MWERKS__
  62.     long oldA4  = SetCurrentA4();
  63. #endif
  64.     
  65.     return_status = NSHC_NO_ERR;
  66.     
  67.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) goto Exit;
  68.     
  69.     if ( nshc_parms->argc < 2 ) {
  70.         nshc_calls->NSH_putStr_err("\pUsage: gestalt OSType [OSType...]\r");
  71.         nshc_parms->action = nsh_idle;
  72.         nshc_parms->result = NSHC_ERR_PARMS;
  73.         goto Exit;
  74.         }
  75.     
  76.     if ( nshc_got_option( nshc_parms, 'd' ) )
  77.         num_option = 'd';
  78.     else
  79.         num_option = 'h';
  80.     
  81.     arg = 0;    // do the ones that work
  82.     
  83.     while ( arg = nshc_next_operand( nshc_parms, arg ) ) 
  84.         if ( is_OSType( nshc_parms, arg, &selector ) )
  85.          {
  86.  
  87.             error = Gestalt( selector, &response );
  88.     
  89.             if (!error) {
  90.                 if ( num_option == 'h' )
  91.                     nshc_calls->NSH_printf(  "'%.4s' %8lX\r", &selector, response );
  92.                 else
  93.                     nshc_calls->NSH_printf(  "'%.4s' %10ld\r", &selector, response );
  94.                 }
  95.                 
  96.             }
  97.  
  98.     arg = 0;    // report the ones that don't work
  99.     
  100.     while ( arg = nshc_next_operand( nshc_parms, arg ) ) 
  101.         if ( is_OSType( nshc_parms, arg, &selector ) )
  102.          {
  103.  
  104.             error = Gestalt( selector, &response );
  105.     
  106.             if (error) {
  107.                 nshc_calls->NSH_printf_err("gestalt: The selector '%.4s' produces an error = %d\r", &selector, error );
  108.                 return_status = NSHC_ERR_GENERAL;
  109.                 }
  110.                 
  111.             }
  112.         else {
  113.             nshc_calls->NSH_printf_err("gestalt: Parameter number %d is not an OSType.\r", arg );
  114.             return_status = NSHC_ERR_GENERAL;
  115.             }
  116.             
  117.     nshc_parms->action = nsh_idle;
  118.     nshc_parms->result = return_status;
  119.  
  120. Exit:
  121.  
  122. #ifdef __MWERKS__
  123.     SetA4(oldA4);        // CodeWarrior needs to restore A4
  124. #else
  125.     ;                    // Think needs a ; to go with the Exit label
  126. #endif
  127. }
  128.  
  129.