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

#include "incs.h"
#include "getline.h"
#include <signal.h>
#include "k.h"
#include "kc.h"

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

#ifndef WIN32
#include <netinet/tcp.h>
#include <pthread.h>
#ifndef PTHREAD_MUTEX_RECURSIVE
#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
#endif
#else
extern void win_usleep(unsigned int); //buggy TDMGCC64 usleep()
#include <sys/types.h>
#include <pthread.h>
;    // Need semicolon, probably missing from <pthread.h>.
#endif

K KONA_GSET=0,KONA_IDX=0;

Z I randomBits();
I oerr(){R O("%s %s\n",errmsg,"error");}

volatile sig_atomic_t interrupted=0;
I scrLim=0;           //script load limit
I fCheck=0;
I fCmplt=0;
I fbs=0;              //backslash flag

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,&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,&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:
  free(s);
  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

K KONA_ARGS; //saved values from argv[1:]

I args(int n,S*v) {
  K a,k; I c,len; U(KONA_ARGS=newK(0, n))
  DO(n, len=strlen(v[i]); 
        if(!(a=newK(-3, len))){cd(KONA_ARGS);R 0;} 
        strncpy(kC(a),v[i],len); 
        kK(KONA_ARGS)[i]=a )
  while(-1!=(c=getopt(n,v,":h:i:e:x:")))SW(c) {
    CS('h',  if(IPC_PORT)O("-i accepted, cannot also have -h\n"); else HTTP_PORT=optarg;)
    CS('i',  if(HTTP_PORT)O("-h accepted, cannot also have -i\n"); else {IPC_PORT=optarg;*kI(KONA_PORT)=atol(IPC_PORT);})
    CS('e',  cd(X(optarg)); exit(0) )
    CS('x',  k=X(optarg); printAtDepth(0,k,0,0,0,0); O("\n"); cd(k); exit(0) )
    CSR(':', )
    CS('?',  O("%c\nabort",optopt); exit(0)) }
  while(optind < n) load(v[optind++]);
  R 0; }

K KFIXED;

pthread_mutex_t execute_mutex;

C khome[PATH_MAX+1];
Z void khinit()
{
  I n;S h;
  khome[0]=0;
  if((h=getenv("KHOME"))){<commit_msg>load script set in KINIT on startup<commit_after>/* console input, main loop */

#include "incs.h"
#include "getline.h"
#include <signal.h>
#include "k.h"
#include "kc.h"

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

#ifndef WIN32
#include <netinet/tcp.h>
#include <pthread.h>
#ifndef PTHREAD_MUTEX_RECURSIVE
#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
#endif
#else
extern void win_usleep(unsigned int); //buggy TDMGCC64 usleep()
#include <sys/types.h>
#include <pthread.h>
;    // Need semicolon, probably missing from <pthread.h>.
#endif

K KONA_GSET=0,KONA_IDX=0;

Z I randomBits();
I oerr(){R O("%s %s\n",errmsg,"error");}

volatile sig_atomic_t interrupted=0;
I scrLim=0;           //script load limit
I fCheck=0;
I fCmplt=0;
I fbs=0;              //backslash flag

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,&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,&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:
  free(s);
  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

K KONA_ARGS; //saved values from argv[1:]

I args(int n,S*v) {
  K a,k; I c,len; U(KONA_ARGS=newK(0, n))
  DO(n, len=strlen(v[i]); 
        if(!(a=newK(-3, len))){cd(KONA_ARGS);R 0;} 
        strncpy(kC(a),v[i],len); 
        kK(KONA_ARGS)[i]=a )
  while(-1!=(c=getopt(n,v,":h:i:e:x:")))SW(c) {
    CS('h',  if(IPC_PORT)O("-i accepted, cannot also have -h\n"); else HTTP_PORT=optarg;)
    CS('i',  if(HTTP_PORT)O("-h accepted, cannot also have -i\n"); else {IPC_PORT=optarg;*kI(KONA_PORT)=atol(IPC_PORT);})
    CS('e',  cd(X(optarg)); exit(0) )
    CS('x',  k=X(optarg); printAtDepth(0,k,0,0,0,0); O("\n"); cd(k); exit(0) )
    CSR(':', )
    CS('?',  O("%c\nabort",optopt); exit(0)) }
  S h=getenv("KINIT");if(h) load(h);
  while(optind < n) load(v[optind++]);
  R 0; }

K KFIXED;

pthread_mutex_t execute_mutex;

C khome[PATH_MAX+1];
Z void khinit()
{
  I n;S h;
  khome[0]=0;
  if((h=getenv("KHOME"))){
{'fraction_non_alphanumeric': 0.2400903676319573, 'fraction_numerical': 0.021770384062435817, 'mean_word_length': 4.47191011235955, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, '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': '6040941', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2362, 'n_words': 422}