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

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