home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / src / tclMacCWD.c < prev    next >
Text File  |  1993-11-18  |  9KB  |  410 lines

  1.  
  2. /*
  3. ** This source code was written by Tim Endres
  4. ** Email: time@ice.com.
  5. ** USMail: 8840 Main Street, Whitmore Lake, MI  48189
  6. **
  7. */
  8.  
  9. /*
  10. search -s GetIndString {ci}≈.h
  11. */
  12.  
  13. #pragma segment TCL
  14.  
  15. #include <resources.h>
  16. #include <memory.h>
  17. #include <files.h>
  18. #include <GestaltEqu.h>
  19. #include <string.h>
  20. #include <packages.h>
  21. #include <folders.h>
  22. #include <ToolUtils.h>
  23. #include <errors.h>
  24. #include <stdarg.h>
  25. #include <Folders.h>
  26. #include <Sound.h>
  27. #include <Traps.h>
  28. #include <Errors.h>
  29.  
  30. #include "tcl.h"
  31. #include "tclMac.h"
  32. #include "XTCL.h"
  33. #include "stat.h"
  34.  
  35. #include "version.h"
  36.  
  37. char    *tcl_check_path_termination( char *path );
  38.  
  39. /*
  40. ** NOTE
  41. **
  42. ** The entire Mac TCL current working directory (CWD) library
  43. ** lives on two assumtions:
  44. **
  45. **    1) You have a hierarchical file system.
  46. **
  47. **    2) You call Tcl_InitializeCurrentWorkingDirectory()
  48. **       at the point you want the Macintosh Default Directory
  49. **       to become the tcl current working directory.
  50. **
  51. */
  52.  
  53.  
  54. /*
  55. ** The following code is used to maintain the
  56. ** "Current Working Directory" state for tcl.
  57. */
  58.  
  59. static short    _current_working_vrefnum = 0;
  60. static long        _current_working_dirid = fsRtDirID;
  61.  
  62. static short    _current_working_wdrefnum = 0;
  63. static short    _current_wdrefnum_count = 0;
  64.  
  65.  
  66. #define MAX_PUSH_DIRS    4
  67.  
  68. static long        _tcl_push_dirs[MAX_PUSH_DIRS];
  69. static short    _tcl_push_vols[MAX_PUSH_DIRS];
  70. static int        _tcl_push_index = 0;
  71.  
  72. /*
  73. ** This function is used to set the tcl current working directory
  74. ** to the volume "vRefNum" and directory "dirID". In doing so, the
  75. ** function also creates a working directory for the cwd.
  76. **
  77. ** The function then  calls SetVol() to set the Macintosh Default
  78. ** Directory to the same location.
  79. **
  80. ** The function returns the result of PBOpenWD() && SetVol().
  81. **
  82. ** If PBOpenWD() or SetVol() returns an error, the cwd is not set.
  83. */
  84.  
  85. int
  86. TclMac_CWDChgDir(vRefNum, dirID)
  87.     int        vRefNum;
  88.     long    dirID;
  89.     {
  90.     int            myerr;
  91.     short        oldwd;
  92.     WDPBRec        wpb;
  93.     Str32        volname;
  94.     char        pathbuf[2048],
  95.                 *ptr;
  96.  
  97.     if ( vRefNum == _current_working_vrefnum && dirID == _current_working_dirid )
  98.         {
  99.         myerr = SetVol(NULL, _current_working_wdrefnum);
  100.         if ( myerr != noErr )
  101.             mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: SetVol(%d) = %d\n",
  102.                             wpb.ioVRefNum, myerr);
  103.         return myerr;
  104.         }
  105.     
  106.     wpb.ioCompletion = 0;
  107.     wpb.ioNamePtr = NULL;
  108.     wpb.ioVRefNum = vRefNum;
  109.     wpb.ioWDDirID = dirID;
  110.     wpb.ioWDProcID = 'MTcl';
  111.     myerr = PBOpenWD(&wpb, FALSE);
  112.     if (myerr != noErr)
  113.         {
  114.         mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: PBOpenWD(%d, %d) = %d\n",
  115.                             vRefNum, dirID, myerr);
  116.         }
  117.     else
  118.         {        
  119.         myerr = SetVol(NULL, wpb.ioVRefNum);
  120.         if ( myerr != noErr )
  121.             {
  122.             mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: SetVol(%d) = %d\n",
  123.                                 wpb.ioVRefNum, myerr);
  124.             wpb.ioCompletion = 0;
  125.             wpb.ioNamePtr = NULL;
  126.             wpb.ioWDDirID = 0;
  127.             wpb.ioWDProcID = 'MTcl';
  128.             PBCloseWD(&wpb, FALSE);
  129.             }
  130.         else
  131.             {
  132.             oldwd = _current_working_wdrefnum;
  133.             _current_working_wdrefnum = wpb.ioVRefNum;
  134.             if ( oldwd != 0 && oldwd != wpb.ioVRefNum && _current_wdrefnum_count == 0 )
  135.                 {
  136.                 wpb.ioCompletion = 0;
  137.                 wpb.ioNamePtr = NULL;
  138.                 wpb.ioVRefNum = oldwd;
  139.                 wpb.ioWDDirID = 0;
  140.                 wpb.ioWDProcID = 'MTcl';
  141.                 PBCloseWD(&wpb, FALSE);
  142.                 }
  143.             
  144.             _current_working_vrefnum = vRefNum;
  145.             _current_working_dirid = dirID;
  146.             
  147.             SFSaveDisk = _current_working_vrefnum * -1;
  148.             CurDirStore = _current_working_dirid;
  149.         
  150.             dirpathname( pathbuf, _current_working_vrefnum,
  151.                                     _current_working_dirid );
  152.             tcl_check_path_termination(pathbuf);
  153.             TclSetEnv(kDefaultDirTag, pathbuf);
  154.             }
  155.         }
  156.     
  157.     return myerr;
  158.     }
  159.  
  160. /*
  161. ** This function is used to set the Macintosh Default Directory
  162. ** to the tcl current working directory using the SetVol() call,
  163. ** and the working directory created for the cwd.
  164. **
  165. ** The function returns the result of SetVol().
  166. */
  167.  
  168. int
  169. TclMac_CWDSetVol()
  170.     {
  171.     SetVol(NULL, _current_working_wdrefnum);
  172.     return noErr;
  173.     }
  174.  
  175. /*
  176. ** The following routines provide the following commonly
  177. ** used construct:
  178. **
  179. **    {
  180. **    TclMac_CWDPushVol();    -- Save the current default directory
  181. **    TclMac_CWDSetVol();    -- Set the default directory to tcl's cwd
  182. **
  183. **    ... code using the tcl cwd ...
  184. **
  185. **    TclMac_CWDPopVol();    -- Restore the current default directory
  186. **    }
  187. **
  188. ** There MUST be exactly one TclMac_CWDPopVol() for each TclMac_CWDPushVol()!
  189. **
  190. */
  191.  
  192. /*
  193. ** This function pushes the current Macintosh Default Directory
  194. ** (as returned by PBHGetVol()) onto a stack.
  195. **
  196. ** The function returns the result of PBHGetVol().
  197. **
  198. ** If PBHGetVol() returns an error, the push is not performed.
  199. ** This function has *no* effect on the tcl cwd.
  200. */
  201.  
  202. int
  203. TclMac_CWDPushVol()
  204.     {
  205.     int            myerr;
  206.     Str32        volname;
  207.     WDPBRec        pb;
  208.     
  209.     if (_tcl_push_index >= MAX_PUSH_DIRS)
  210.         {
  211.         mac_fprintf(stderr, "TclMac_CWDPushVol: WARNING - OVERFLOW stack.\n");
  212.         return queueFull;
  213.         }
  214.     
  215.     pb.ioCompletion = 0;    
  216.     pb.ioNamePtr = volname;     volname[0] = '\0';
  217.     myerr = PBHGetVol(&pb, FALSE);
  218.     if (myerr == noErr)
  219.         {
  220.         _tcl_push_vols[_tcl_push_index] = pb.ioVRefNum;
  221.         _tcl_push_dirs[_tcl_push_index] = pb.ioWDDirID;
  222.         ++_tcl_push_index;
  223.         }
  224.     
  225.     return myerr;
  226.     }
  227.  
  228. /*
  229. ** This function pops the current Macintosh Default Directory
  230. ** from a stack and calls PBHSetVol() to set the default directory.
  231. **
  232. ** The function returns the result of PBHSetVol().
  233. **
  234. ** This function has *no* effect on the tcl cwd.
  235. */
  236.  
  237. int
  238. TclMac_CWDPopVol()
  239.     {
  240.     int            myerr;
  241.     WDPBRec        pb;
  242.     
  243.     if (_tcl_push_index < 1)
  244.         {
  245.         mac_fprintf(stderr, "TclMac_CWDPopVol: WARNING - UNDERFLOW stack.\n");
  246.         return unitEmptyErr;
  247.         }
  248.  
  249.     --_tcl_push_index;
  250.     pb.ioCompletion = 0;
  251.     pb.ioNamePtr = NULL;
  252.     pb.ioVRefNum = _tcl_push_vols[_tcl_push_index];
  253.     pb.ioWDDirID = _tcl_push_dirs[_tcl_push_index];
  254.     
  255.     myerr = PBHSetVol(&pb, FALSE);
  256.  
  257.     return myerr;
  258.     }
  259.  
  260. /*
  261. ** This function returns the tcl current working directory's
  262. ** volume reference number. This is never a wdrefnum.
  263. */
  264.  
  265. int
  266. TclMac_CWDVRefNum()
  267.     {
  268.     return _current_working_vrefnum;
  269.     }
  270.  
  271. /*
  272. ** This function returns the tcl current working directory's
  273. ** directory ID.
  274. */
  275.  
  276. int
  277. TclMac_CWDDirID()
  278.     {
  279.     return _current_working_dirid;
  280.     }
  281.  
  282. /*
  283. ** This function returns the tcl current working directory's
  284. ** full pathname.
  285. */
  286.  
  287. char *
  288. TclMac_CWDPathName(path)
  289.     char    *path;
  290.     {
  291.     dirpathname(path, _current_working_vrefnum, _current_working_dirid);
  292.     tcl_check_path_termination(path);
  293.     return path;
  294.     }
  295.  
  296. /*
  297. ** The following routines provide the following commonly
  298. ** used construct:
  299. **
  300. **    {
  301. **    TclMac_CWDCreateWD(&myWDRefNum);    -- Create current default directory WD
  302. **
  303. **    ... code using myWDRefNum ...
  304. **
  305. **    TclMac_CWDDisposeWD(wdRefNum);    -- Close the created WD
  306. **    }
  307. **
  308. ** You *really* want to dispose of these WD's quickly and use
  309. ** very sparingly (mostly to support old code you don't want
  310. ** to recode to use dirid's). WD's are a limited system resource.
  311. **
  312. */
  313.  
  314. /*
  315. ** This function creates a Macintosh Working Directory that
  316. ** references the tcl current working directory.
  317. **
  318. ** The result is the result of the PBOpenWD() call. 
  319. **
  320. ** If the result is noErr, the working directory reference
  321. ** number is copied into wdRefNum
  322. **
  323. ** This function has *no* effect on the tcl cwd.
  324. */
  325.  
  326. int
  327. TclMac_CWDCreateWD(wdRefNum)
  328.     short        *wdRefNum;
  329.     {
  330.     ++_current_wdrefnum_count;
  331.     *wdRefNum = _current_working_wdrefnum;
  332.     return noErr;
  333.     }
  334.  
  335. /*
  336. ** This function disposes a Macintosh Working Directory that
  337. ** was created by a call to TclMac_CWDCreateWD().
  338. **
  339. ** The result is the result of the PBCloseWD() call. 
  340. **
  341. ** This function has *no* effect on the tcl cwd.
  342. */
  343.  
  344. int
  345. TclMac_CWDDisposeWD(wdRefNum)
  346.     short    wdRefNum;
  347.     {
  348.     WDPBRec        wpb;
  349.     
  350.     if (_current_wdrefnum_count < 1)
  351.         {
  352.         mac_fprintf(stderr, "ERROR: TclMac_CWDDisposeWD: count is ZERO.\n");
  353.         }
  354.     
  355.     if (--_current_wdrefnum_count == 0 && wdRefNum != _current_working_wdrefnum)
  356.         {
  357.         wpb.ioCompletion = 0;
  358.         wpb.ioNamePtr = NULL;
  359.         wpb.ioVRefNum = wdRefNum;
  360.         wpb.ioWDDirID = 0;
  361.         wpb.ioWDProcID = 'MTcl';
  362.         PBCloseWD(&wpb, FALSE);
  363.         }
  364.     
  365.     return noErr;
  366.     }
  367.  
  368. /*
  369. ** This function initializes the tcl current working directory.
  370. **
  371. ** The function uses PBHGetVol() to get the Macintosh Default
  372. ** Directory, and sets the tcl cwd to that directory.
  373. **
  374. ** The result is always TCL_OK
  375. **
  376. ** The application must call this function once when it wishes
  377. ** to establish the tcl cwd. This is usally performed immediately
  378. ** after the application is launched to cause the tcl cwd to be
  379. ** initialized to the directory containing the application.
  380. */
  381.  
  382. int
  383. TclMac_CWDInitialize()
  384.     {
  385.     int            myerr;
  386.     Str32        volname;
  387.     WDPBRec        wpb;
  388.     
  389.     _current_wdrefnum_count = 0;
  390.     
  391.     wpb.ioCompletion = 0;    
  392.     wpb.ioNamePtr = volname; volname[0] = '\0';
  393.     myerr = PBHGetVol( &wpb, FALSE );
  394.     if (myerr != noErr)
  395.         {
  396.         _current_working_dirid = fsRtDirID;
  397.         _current_working_vrefnum = 0;
  398.         _current_working_wdrefnum = 0;
  399.         }
  400.     else
  401.         {
  402.         _current_working_dirid = wpb.ioWDDirID;
  403.         _current_working_vrefnum = wpb.ioWDVRefNum;
  404.         _current_working_wdrefnum = wpb.ioVRefNum;
  405.         //GetVol(NULL, &_current_working_wdrefnum);
  406.         }
  407.     
  408.     return TCL_OK;
  409.     }
  410.