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

  1. /* ========== the commmand file: ==========
  2.  
  3.     fss.c
  4.     
  5.     Copyright (c) 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 <script.h>
  20. #include <string.h>
  21.  
  22. #include "nshc.h"
  23.  
  24. #include "arg_utl.proto.h"
  25. #include "fss_utl.proto.h"
  26. #include "fss_utl2.proto.h"
  27. #include "nshc_utl.proto.h"
  28. #include "str_utl.proto.h"
  29.  
  30. // data definition - this struct is the root of all data
  31.  
  32. typedef struct {
  33.  
  34.     int        got_fss;        // 0 if FSSpec calls are not available
  35.     int        arg;            // position in arg list
  36.  
  37. } t_fss_data;
  38.  
  39. typedef    t_fss_data    **fss_hndl;
  40.  
  41. /* ======================================== */
  42.  
  43. // prototypes - utility
  44.  
  45. void fss_bad( t_nshc_parms *nshc_parms, int code );
  46. void fss_bad_file( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, StringPtr msg );
  47. void fss_good( t_nshc_parms *nshc_parms );
  48.  
  49. // prototypes - file routines
  50.  
  51. void  fss_one( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, t_fss_data **hData );
  52.  
  53. // prototypes - state machine
  54.  
  55. void fss_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  );
  56. void fss_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  57. void fss_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  58.  
  59. /* ======================================== */
  60.  
  61. // utility routines
  62.  
  63. /* ======================================== */
  64.  
  65. void fss_bad(  t_nshc_parms *nshc_parms, int code )
  66. {
  67.     nshc_parms->action = nsh_stop;
  68.     nshc_parms->result = code;
  69. }
  70.  
  71. void fss_bad_file(  t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, StringPtr msg )
  72. {
  73.  
  74.     nshc_calls->NSH_putStr_err("\pfss: Access error (");
  75.     nshc_calls->NSH_putStr_err(msg);
  76.     nshc_calls->NSH_putStr_err("\p)\r");
  77. }
  78.  
  79. void fss_good(  t_nshc_parms *nshc_parms )
  80. {
  81.     nshc_parms->action = nsh_stop;
  82.     nshc_parms->result = 0;
  83. }
  84.  
  85. /* ======================================== */
  86.  
  87. // file access routines
  88.  
  89. /* ======================================== */
  90.  
  91. void fss_one( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, t_fss_data **hData )
  92. {
  93.     CInfoPBRec pb;
  94.     OSErr result;
  95.     FSSpec    fsspec;
  96.         
  97.     // =====> convert argument to fsspec
  98.     
  99.     result = arg_to_real_fss( nshc_parms, nshc_calls, (**hData).arg, &fsspec );
  100.  
  101.     (**hData).arg++;
  102.     
  103.     if (result)
  104.         return;
  105.     
  106.     pb.hFileInfo.ioNamePtr = (StringPtr)&fsspec.name;
  107.     pb.hFileInfo.ioVRefNum = fsspec.vRefNum;
  108.     pb.hFileInfo.ioDirID = fsspec.parID;
  109.     pb.hFileInfo.ioFDirIndex = 0;
  110.  
  111.     result = PBGetCatInfoSync(&pb);
  112.     
  113.     if (result)
  114.         fss_bad_file( nshc_parms, nshc_calls, (StringPtr)fsspec.name );
  115.     else {
  116.         nshc_calls->NSH_printf( " %6d %6ld ", fsspec.vRefNum, fsspec.parID );
  117.         nshc_calls->NSH_putStr( fsspec.name );
  118.         nshc_calls->NSH_putchar( '\r' );
  119.         }    
  120. }
  121.  
  122. /* ======================================== */
  123.  
  124. // state machine - core routines
  125.  
  126. /* ======================================== */
  127.  
  128. void fss_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  )
  129. {
  130.     fss_hndl    hData;    // handle to hold our data
  131.     
  132.     if (nshc_parms->argc < 2) {
  133.         nshc_calls->NSH_putStr_err( "\pUsage: fss pathname [pathname...]\r" );
  134.         fss_bad( nshc_parms, NSHC_ERR_PARMS );
  135.         return;
  136.         }
  137.         
  138.     nshc_parms->action = nsh_continue;
  139.  
  140.     hData = (fss_hndl)NewHandleClear(sizeof(t_fss_data));
  141.     
  142.     if (hData) {
  143.         (**hData).arg = 1;                    // start at the arg = 1 position
  144.         (**hData).got_fss = fss_test();        // test if we can use FSSpec calls
  145.         nshc_parms->data = (Handle)hData;
  146.         }
  147.     else {
  148.         nshc_calls->NSH_putStr_err( "\pfss: Could not allocate storage.\r" );
  149.         fss_bad( nshc_parms, NSHC_ERR_MEMORY );
  150.         }
  151. }
  152.  
  153. /* ======================================== */
  154.  
  155. void fss_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  156. {
  157.     fss_hndl    hData;
  158.     
  159.     if (hData = (fss_hndl)nshc_parms->data)
  160.         if ((**hData).arg >= nshc_parms->argc)
  161.             fss_good( nshc_parms );
  162.         else
  163.             fss_one( nshc_parms, nshc_calls, hData );
  164. }
  165.  
  166. /* ======================================== */
  167.  
  168. void fss_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  169. {
  170.     fss_hndl    hData;
  171.     
  172.     if (hData = (fss_hndl)nshc_parms->data)
  173.         DisposeHandle(nshc_parms->data);
  174.         
  175.     nshc_parms->action = nsh_idle;
  176. }
  177.  
  178. /* ======================================== */
  179.  
  180. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  181. {
  182. #ifdef __MWERKS__
  183.     long oldA4  = SetCurrentA4();
  184. #endif
  185.     
  186.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  187.     
  188.         switch (nshc_parms->action) {
  189.             case nsh_start:
  190.                 fss_start(nshc_parms, nshc_calls);
  191.                 break;
  192.             case nsh_continue:
  193.                 fss_continue(nshc_parms, nshc_calls);
  194.                 break;
  195.             case nsh_stop:
  196.                 fss_stop(nshc_parms, nshc_calls);
  197.                 break;
  198.             }
  199.             
  200.         }
  201.     
  202. #ifdef __MWERKS__
  203.     SetA4(oldA4);
  204. #endif
  205. }
  206.  
  207. /* ======================================== */
  208.