Esempio n. 1
0
int main
(  int               argc
,  const char*       argv[]
)
   {  int  a               =  0
   ;  mcxTing* argtxt      =  mcxTingEmpty(NULL, 10)

   ;  if (0)
      mclxIOsetQMode("MCLXIOVERBOSITY", MCL_APP_VB_NO)

   ;  opInitialize()       /* symtable etc */
   ;  globInitialize()     /* hdltable etc */

   ;  if (argc == 1)
      {  mcxTing* ops      =  mcxTingEmpty(NULL, 20)
      ;  mcxIO *xfin       =  mcxIOnew("-", "r")
      ;  mcxIOopen(xfin, EXIT_ON_FAIL)

      ;  fprintf
         (  stdout
         ,  "At your service: "
            "'[/<op>] help', '[/<str>] grep', 'ops', 'info', and 'quit'.\n"
         )

      ;  while (1)
         {  int ok
         ;  mcxTing* line  =  mcxTingEmpty(NULL, 30)

         ;  fprintf(stdout, "> ")
         ;  fflush(stdout)

         ;  if (STATUS_OK != mcxIOreadLine(xfin, line, MCX_READLINE_BSC))
            {  fprintf(stdout, "curtains!\n")
            ;  mcxTingFree(&line)
            ;  break
         ;  }
            mcxTingAppend(ops, line->str)
         ;  mcxTingFree(&line)

         ;  ok = zsDoSequence(ops->str)

         ;  if (ok && (v_g & V_STACK))
            zsList(0)

         ;  mcxTingEmpty(ops, 20)
      ;  }
      }
      else
      {  for (a=1;a<argc;a++)
         {  mcxTingWrite(argtxt, argv[a])
         ;  if (!zgUser(argtxt->str))
            mcxExit(1)
      ;  }
      }
      mcxTingFree(&argtxt)
   ;  return 0
;  }
Esempio n. 2
0
mcxIO* mcxIOrenew
(  mcxIO*            xf
,  const char*       name
,  const char*       mode
)
   {  mcxbool twas_stdio = xf && xf->stdio      /* it was one of STD{IN,OUT,ERR} */
   ;  if
      (  mode
      && !strstr(mode, "w") && !strstr(mode, "r") && !strstr(mode, "a")
      )
      {  mcxErr ("mcxIOrenew PBD", "unsupported open mode <%s>", mode)
      ;  return NULL
   ;  }

      if
      (  getenv("TINGEA_PLUS_APPEND")
      && (  name && (uchar) name[0] == '+' )
      && (  mode && strchr(mode, 'w') )
      )
      {  name++               /* user can specify -o +foo to append to foo */
      ;  mode = "a"
   ;  }

      if (!xf)                /* case 1)   create a new one */
      {  if (!name || !mode)
         {  mcxErr("mcxIOrenew PBD", "too few arguments")
         ;  return NULL
      ;  }

         if (!(xf = (mcxIO*) mcxAlloc(sizeof(mcxIO), RETURN_ON_FAIL)))
         return NULL

      ;  if (!(xf->fn = mcxTingEmpty(NULL, 20)))
         return NULL

      ;  if (!(xf->buffer = mcxTingEmpty(NULL, getpagesize())))
         return NULL

      ;  xf->fp      =  NULL
      ;  xf->mode    =  NULL
      ;  xf->usr     =  NULL
      ;  xf->usr_reset =  NULL
      ;  xf->buffer_consumed = 0
   ;  }
      else if (xf->stdio)     /* case 2)   have one, don't close */
      NOTHING
   ;  else if (mcxIOwarnOpenfp(xf, "mcxIOrenew"))
      mcxIOclose(xf)          /* case 3)   have one, warn and close if open */

   ;  mcxIOreset(xf)

   ;  if (name && !mcxTingWrite(xf->fn, name))
      return NULL

   ;  if (mode)
      {  if (xf->mode)
         mcxFree(xf->mode)
      ;  xf->mode = mcxStrDup(mode)
   ;  }

      xf->stdio = begets_stdio(xf->fn->str, xf->mode)

                                       /* name changed, no longer stdio */
   ;  if (twas_stdio && !xf->stdio)
      xf->fp = NULL

   ;  if (xf->stdio && mode && strchr(mode, 'a'))     /* recently added */
      {  if (xf->mode)
         mcxFree(xf->mode)
      ;  xf->mode = mcxStrDup("w")
   ;  }

      return xf
;  }