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

  1. /* ========== the commmand file: ==========
  2.  
  3.     notify.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 "nshc.h"
  20. #include "str_utl.proto.h"
  21. #include "nshc_utl.proto.h"
  22.  
  23. /* ======================================== */
  24.  
  25. // our data record, NewHandled and attached to nshc_parms->data
  26.  
  27. typedef struct {
  28.  
  29.     Str255    theString;        // theString we are receiving fromthe keyboard
  30.     int        overrun;        // non-zero if theString has exceeded 255 chars
  31.  
  32. } t_notify_data;
  33.  
  34. typedef t_notify_data **t_notify_handle;
  35.  
  36. /* ======================================== */
  37.  
  38. // prototypes - for local use only
  39.  
  40. int notify_by_parameters( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  41. void notify_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  42. void notify_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 theString );
  43. void notify_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  44. void notify_stop( t_nshc_parms *nshc_parms );
  45.  
  46. // utility
  47.  
  48. void notify_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 theString )
  49. {
  50.     int        size;
  51.  
  52.     size = 1;
  53.     
  54.     if (nshc_got_option( nshc_parms, 's')) size = 0;
  55.     if (nshc_got_option( nshc_parms, 'l')) size = 2;
  56.     
  57.     nshc_calls->NSH_notify( theString, size );
  58.     
  59.     nshc_parms->result = NSHC_NO_ERR;
  60. }
  61.  
  62. /* ======================================== */
  63.  
  64. // if the user put his text on the command line, do it all in one operation
  65.  
  66. int notify_by_parameters( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  67. {
  68.     int                arg;
  69.     int                got_one;
  70.     int                length;
  71.     char            *p;
  72.     Str255            theString;
  73.     
  74.     arg = 1;
  75.     got_one = 0;
  76.     length = 0;
  77.     theString[0] = 0;
  78.     
  79.     while ( arg < nshc_parms->argc ) {
  80.         if ( nshc_is_operand( nshc_parms, arg) ) {
  81.             p = &nshc_parms->arg_buf[nshc_parms->argv[arg]];
  82.             length += cStrLen(p);
  83.             if (length < 255) {
  84.                 if (got_one)
  85.                     theString[++theString[0]] = ' ';
  86.                 pStrAppendC( theString, p );
  87.                 }
  88.             got_one = 1;
  89.             }
  90.         arg++;
  91.         }
  92.         
  93.     if (got_one)
  94.         if (length <= 255)
  95.             notify_display( nshc_parms, nshc_calls, theString );
  96.         else{
  97.             nshc_calls->NSH_putStr_err("\pnotify: stdin exceeds 255 chars.\r");
  98.             nshc_parms->result = NSHC_ERR_GENERAL;
  99.             }
  100.  
  101.     return( got_one );
  102. }
  103.  
  104. /* ======================================== */
  105.  
  106. // state machine
  107.  
  108. void notify_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  109. {
  110.     t_notify_handle    ourData;
  111.  
  112.     nshc_parms->action = nsh_idle;
  113.     nshc_parms->result = NSHC_NO_ERR;
  114.     
  115.     if ( notify_by_parameters( nshc_parms, nshc_calls ) )
  116.         return;
  117.  
  118.     ourData = (t_notify_handle)NewHandle( sizeof(t_notify_data) );
  119.  
  120.     if (ourData) {
  121.         (**ourData).theString[0] = 0;
  122.         (**ourData).overrun = 0;
  123.         nshc_parms->data = (Handle)ourData;
  124.         nshc_parms->action = nsh_continue;
  125.         }
  126.     else {
  127.         nshc_calls->NSH_putStr_err("\pnotify: Could not allocate storage.\r");
  128.         nshc_parms->result = NSHC_ERR_MEMORY;
  129.         }
  130. }
  131.  
  132. /* ======================================== */
  133.  
  134. // this _continue routine is used to pick up input from stdin, it loops until
  135. // an end-of-input is found. It posts the input text if it does not exceed 255
  136. // characters, otherwise it posts an error message.  If input exceeds 255 chars,
  137. // all extra chars are eaten.
  138.  
  139. void notify_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  140. {
  141.     int        value;
  142.     int        size;
  143.     Str255    localString;
  144.     
  145.     t_notify_handle    ndata;
  146.     
  147.     ndata = (t_notify_handle)(nshc_parms->data);
  148.     
  149.     HLock( (Handle)ndata );
  150.     
  151.     value = nshc_calls->NSH_getStr( localString );
  152.     
  153.     if (value) {
  154.         size = localString[0] + (**ndata).theString[0];
  155.         if (size > 255) (**ndata).overrun = 1;
  156.         if (!(**ndata).overrun) pStrAppend( (**ndata).theString, localString );
  157.         }
  158.         
  159.     if (value == -1) {
  160.     
  161.         if ((**ndata).overrun) {
  162.             nshc_calls->NSH_putStr_err("\pnotify: stdin exceeds 255 chars.\r");
  163.             nshc_parms->result = NSHC_ERR_GENERAL;
  164.             }
  165.         else
  166.             notify_display( nshc_parms, nshc_calls, (**ndata).theString );
  167.             
  168.         nshc_parms->action = nsh_stop;
  169.         }
  170.         
  171.     HUnlock( (Handle)ndata );
  172. }
  173.  
  174. /* ======================================== */
  175.  
  176. void notify_stop( t_nshc_parms *nshc_parms )
  177. {        
  178.     if (nshc_parms->data)
  179.         DisposeHandle(nshc_parms->data);
  180.  
  181.     nshc_parms->action = nsh_idle;
  182. }
  183.  
  184. /* ======================================== */
  185.  
  186. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  187. {
  188. #ifdef __MWERKS__
  189.     long oldA4  = SetCurrentA4();
  190. #endif
  191.     
  192.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  193.         
  194.         switch (nshc_parms->action) {
  195.             case nsh_start:
  196.                 notify_start(nshc_parms,nshc_calls);
  197.                 break;
  198.             case nsh_continue:
  199.                 notify_continue(nshc_parms,nshc_calls);
  200.                 break;
  201.             default:
  202.                 notify_stop(nshc_parms);
  203.                 break;
  204.             }
  205.             
  206.         }
  207.  
  208. #ifdef __MWERKS__
  209.     SetA4(oldA4);
  210. #endif
  211. }
  212.