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

  1. /* ========== the commmand file: ==========
  2.  
  3.     man.c - where normal Macintosh stuff is handled.
  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. #define        MAN_ERR_FILE    1
  20. #define        MAN_ERR_RSRC    2
  21.  
  22. #include "nshc.h"
  23.                     
  24. #include "str_utl.proto.h"
  25. #include "nshc_utl.proto.h"
  26.  
  27. extern short ResErr;
  28.  
  29. /* ========== Utility Functions. ========== */
  30.  
  31. void man_display( t_nshc_calls *nshc_calls, char **text );
  32.  
  33. void man_display( t_nshc_calls *nshc_calls, char **text )
  34. {
  35.     long    i;
  36.     char    *p;
  37.     
  38.     i = SizeResource(text);
  39.     SetHandleSize(text,i+2);
  40.     HLock(text);
  41.     p=*text;
  42.     if ( p[i-1] != '\r' ) {
  43.         p[i]='\r';
  44.         p[i+1]=0;
  45.         }
  46.     else
  47.         p[i]=0;
  48.     nshc_calls->NSH_puts(p);
  49.     HUnlock(text);
  50.     ReleaseResource(text);
  51. }
  52.  
  53. /* ========== find the resource in the specified file ========== */
  54.  
  55. int man_get( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, int arg );
  56.  
  57. int man_get( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, int arg )
  58. {
  59.     char    **text;
  60.     Str255    rsrcName;
  61.     Str255    fileName;
  62.     short    iFileRef;
  63.     OSErr    error;
  64.     
  65.     error = 0;
  66.  
  67.     pStrFromC( fileName, &nshc_parms->arg_buf[nshc_parms->argv[arg]] );
  68.     
  69.     iFileRef = -1;
  70.     
  71.     if ( !nshc_calls->NSH_path_which( fileName ) )
  72.         iFileRef = OpenResFile( fileName );
  73.         
  74.     if ( iFileRef < 0 )
  75.         return( MAN_ERR_FILE );
  76.  
  77.     pStrFromC( rsrcName, "man ");
  78.     
  79.     if (arg == 0) {
  80.         pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[1]]);
  81.         pStrAppendC( rsrcName, " " );
  82.         }
  83.         
  84.     if (nshc_parms->argc > 2)
  85.         pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[2]] );
  86.     else
  87.         pStrAppendC( rsrcName, "general");
  88.     
  89.     text = (char **)GetNamedResource( 'TEXT', rsrcName);
  90.     
  91.     if ( !text )
  92.         error = MAN_ERR_RSRC;
  93.     else
  94.         man_display( nshc_calls, text );
  95.  
  96.     if (iFileRef)
  97.         CloseResFile(iFileRef);
  98.         
  99.     return( error );
  100. }
  101.  
  102. /* ========== the command. ========== */
  103.  
  104. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  105. {
  106.     int    result;
  107.     int    argc;
  108.  
  109. #ifdef __MWERKS__
  110.     long oldA4  = SetCurrentA4();
  111. #endif
  112.     
  113.     nshc_parms->action = nsh_idle;        // assume completion
  114.     result = 1;                            // assume failure (resource not found)
  115.  
  116.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  117.  
  118.         argc = nshc_parms->argc;
  119.     
  120.         if ((argc <2) || (argc >3)) {
  121.             nshc_calls->NSH_putStr_err("\pUsage: man command [section].\r");
  122.             result = NSHC_ERR_PARMS;
  123.             }
  124.         else
  125.             if ( result = man_get( nshc_parms, nshc_calls, 1 ) )
  126.                 if ( result = man_get( nshc_parms, nshc_calls, 0 ) )
  127.                     nshc_calls->NSH_putStr_err("\pman: Manual Page Not Found.\r");
  128.         
  129.         nshc_parms->result = result;
  130.         nshc_parms->action = nsh_idle;
  131.         
  132.         }
  133.     
  134. #ifdef __MWERKS__
  135.     SetA4(oldA4);
  136. #endif
  137. }
  138.