<commit_before>/* console input, main loop */

#include "incs.h"
#include "getline.h"

#include <netinet/tcp.h> //#include <sys/socket.h> //#include <netinet/in.h>

#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#include <signal.h>

#include "k.h"
#include "kc.h"

Z I randomBits();
// Z I wdss(K *a,FILE *f);

I interrupted = 0;
I scrLim = 0;           // script load limit

Z void handle_SIGINT(int sig) { interrupted = 1; }

//0: not a verb pointer, 1: monadic, 2: dyadic, 3: triadic
I vn_ct;

I prompt(I n){DO(n,O(">")) O("  ");fflush(stdout);R 0;}

I wds(K* a,FILE*f){R wds_(a,f,0);}
I wds_(K*a,FILE*f,I l)
{
  S s=0,t=0; I b=0,c=0,m=0,n=0,v=0;
  K z=0; PDA p=0;
  I o=isatty(STDIN)&&f==stdin;
  if(-1==(c=getline_(&s,(size_t * __restrict__)&n,f)))GC;
  appender(&t,&m,s,n);
  while(1==(v=complete(t,m,&p,0)))
  { b=parsedepth(p);
    if(o)prompt(b+l);
    if(-1==(c=getline_(&s,(size_t * __restrict__)&n,f)))GC;
    appender(&t,&m,s,n);
  }
  SW(v){CS(2,show(kerr("unmatched"));GC) CS(3,show(kerr("nest")); GC)}
  z=newK(-3,m-1);
  strncpy(kC(z),t,m-1);
cleanup:
  if(s)free(s);
  if(t)free(t);
  if(p)pdafree(p);
  if((v||c==-1)&&z){cd(z); *a=0;}
  else *a=z;
  R v?-v:c; // -1 EOF, -2 unmatched, -3 nest
}

// Z I wdss(K*a,FILE*f)
// {
//   I c=0,n=0;
//   K k=0,z=newK(0,0);
//   while(0<(c=wds(&k,f))){kap(&z,&k); cd(k); n++;}
//   *a=z;
//   R c==-1?n:c;
// }

I lines(FILE*f) {S a=0;I n=0;PDA p=0; while(-1!=line(f,&a,&n,&p));R 0;}//You could put lines(stdin) in main() to have not-multiplexed command-line-only input
I line(FILE*f, S*a, I*n, PDA*p) // just starting or just executed: *a=*n=*p=0,  intermediate is non-zero
{
  S s=0; I b=0,c=0,m=0;
  K k; F d;

  I o = isatty(STDIN) && f==stdin; //display results to stdout?

  if(-1==(c=getline(&s,(size_t * __restrict__)&m,f))) GC;
  appender(a,n,s,c);//"strcat"(a,s)
  I v=complete(*a,*n,p,0); //will allocate if p is null
  b=parsedepth(*p);
  if(v==3){show(kerr("nest")); GC;} 
  if(v==2){show(kerr("unmatched")); b=0; GC;}
  if(v==1) goto done;//generally incomplete<commit_msg>fix #229: Scripts produce no output<commit_after>/* console input, main loop */

#include "incs.h"
#include "getline.h"

#include <netinet/tcp.h> //#include <sys/socket.h> //#include <netinet/in.h>

#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#include <signal.h>

#include "k.h"
#include "kc.h"

Z I randomBits();
// Z I wdss(K *a,FILE *f);

I interrupted = 0;
I scrLim = 0;           // script load limit

Z void handle_SIGINT(int sig) { interrupted = 1; }

//0: not a verb pointer, 1: monadic, 2: dyadic, 3: triadic
I vn_ct;

I prompt(I n){DO(n,O(">")) O("  ");fflush(stdout);R 0;}

I wds(K* a,FILE*f){R wds_(a,f,0);}
I wds_(K*a,FILE*f,I l)
{
  S s=0,t=0; I b=0,c=0,m=0,n=0,v=0;
  K z=0; PDA p=0;
  I o=isatty(STDIN)&&f==stdin;
  if(-1==(c=getline_(&s,(size_t * __restrict__)&n,f)))GC;
  appender(&t,&m,s,n);
  while(1==(v=complete(t,m,&p,0)))
  { b=parsedepth(p);
    if(o)prompt(b+l);
    if(-1==(c=getline_(&s,(size_t * __restrict__)&n,f)))GC;
    appender(&t,&m,s,n);
  }
  SW(v){CS(2,show(kerr("unmatched"));GC) CS(3,show(kerr("nest")); GC)}
  z=newK(-3,m-1);
  strncpy(kC(z),t,m-1);
cleanup:
  if(s)free(s);
  if(t)free(t);
  if(p)pdafree(p);
  if((v||c==-1)&&z){cd(z); *a=0;}
  else *a=z;
  R v?-v:c; // -1 EOF, -2 unmatched, -3 nest
}

// Z I wdss(K*a,FILE*f)
// {
//   I c=0,n=0;
//   K k=0,z=newK(0,0);
//   while(0<(c=wds(&k,f))){kap(&z,&k); cd(k); n++;}
//   *a=z;
//   R c==-1?n:c;
// }

I lines(FILE*f) {S a=0;I n=0;PDA p=0; while(-1!=line(f,&a,&n,&p));R 0;}//You could put lines(stdin) in main() to have not-multiplexed command-line-only input
I line(FILE*f, S*a, I*n, PDA*p) // just starting or just executed: *a=*n=*p=0,  intermediate is non-zero
{
  S s=0; I b=0,c=0,m=0;
  K k; F d;

  //I o = isatty(STDIN) && f==stdin; //display results to stdout?
  I o = isatty(STDIN); //display results to stdout?

  if(-1==(c=getline(&s,(size_t * __restrict__)&m,f))) GC;
  appender(a,n,s,c);//"strcat"(a,s)
  I v=complete(*a,*n,p,0); //will allocate if p is null
  b=parsedepth(*p);
  if(v==3){show(kerr("nest")); GC;} 
  if(v==2){show(kerr("unmatched")); b=0; GC;}
  if(v==1) goto done;//generally incomplete
{'fraction_non_alphanumeric': 0.26200369344413665, 'fraction_numerical': 0.02608494921514312, 'mean_word_length': 4.146080760095012, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1278548', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2150, 'n_words': 415}