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