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