diff --git a/lib/include/array.pre.pike b/lib/include/array.pre.pike deleted file mode 100644 index 3269245e9f15b184cb512c987475bad489ef3efd..0000000000000000000000000000000000000000 --- a/lib/include/array.pre.pike +++ /dev/null @@ -1,174 +0,0 @@ -#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) - -mixed map(mixed arr, mixed fun, mixed ... args) -{ - int e; - mixed *ret; - - if(mappingp(arr)) - return mkmapping(indices(arr),map(values(arr),fun,@args)); - - if(intp(fun)) - return arr(@args); - - if(stringp(fun)) - return column(arr, fun)(@args); - - if(functionp(fun)) - { - ret=allocate(sizeof(arr)); - for(e=0;e<sizeof(arr);e++) - ret[e]=fun(arr[e],@args); - return ret; - } - - error("Bad argument 2 to map_array().\n"); -} - -mixed filter(mixed arr, mixed fun, mixed ... args) -{ - int e; - mixed *ret; - - if(mappingp(arr)) - { - mixed *i, *v, r; - i=indices(arr); - ret=map(v=values(arr),fun,@args); - r=([]); - for(e=0;e<sizeof(ret);e++) if(ret[e]) r[i[e]]=v[e]; - - return ret; - }else{ - int d; - ret=map(arr,fun,@args); - for(e=0;e<sizeof(arr);e++) if(ret[e]) ret[d++]=arr[e]; - - return ret[..d-1]; - } -} - - -int search_array(mixed *arr, mixed fun, mixed ... args) -{ - int e; - - if(stringp(fun)) - { - for(e=0;e<sizeof(arr);e++) - if(arr[e][fun](@args)) - return e; - return -1; - } - else if(functionp(fun)) - { - for(e=0;e<sizeof(arr);e++) - if(fun(arr[e],@args)) - return e; - return -1; - } - else if(intp(fun)) - { - for(e=0;e<sizeof(arr);e++) - if(arr[e](@args)) - return e; - return -1; - } - else - { - error("Bad argument 2 to filter().\n"); - } -} - -mixed *sum_arrays(function foo, mixed * ... args) -{ - mixed *ret; - int e,d; - ret=allocate(sizeof(args[0])); - for(e=0;e<sizeof(args[0]);e++) - ret[e]=foo(@ column(args, e)); - return ret; -} - -varargs mixed *sort_array(array foo,function cmp, mixed ... args) -{ - array bar,tmp; - int len,start; - int length; - int foop, fooend, barp, barend; - - if(!cmp || cmp==`>) - { - foo+=({}); - sort(foo); - return foo; - } - - if(cmp == `<) - { - foo+=({}); - sort(foo); - return reverse(foo); - } - - length=sizeof(foo); - - foo+=({}); - bar=allocate(length); - - for(len=1;len<length;len*=2) - { - start=0; - while(start+len < length) - { - foop=start; - barp=start+len; - fooend=barp; - barend=barp+len; - if(barend > length) barend=length; - - while(1) - { - if(cmp(foo[foop],foo[barp],@args) <= 0) - { - bar[start++]=foo[foop++]; - if(foop == fooend) - { - while(barp < barend) bar[start++]=foo[barp++]; - break; - } - }else{ - bar[start++]=foo[barp++]; - if(barp == barend) - { - while(foop < fooend) bar[start++]=foo[foop++]; - break; - } - } - } - } - while(start < length) bar[start]=foo[start++]; - - tmp=foo; - foo=bar; - bar=tmp; - } - - return foo; -} - -array uniq(array a) -{ - return indices(mkmapping(a,a)); -} - - -void create() -{ - add_constant("filter",filter); - add_constant("map",map); - add_constant("sum_arrays",sum_arrays); - add_constant("search_array",search_array); - add_constant("sort_array",sort_array); - add_constant("uniq",uniq); -} diff --git a/lib/include/fifo.pre.pike b/lib/include/fifo.pre.pike deleted file mode 100644 index a9ddd6f6b968ae14505f9b2f7582994d61cda237..0000000000000000000000000000000000000000 --- a/lib/include/fifo.pre.pike +++ /dev/null @@ -1,72 +0,0 @@ -void create() -{ - master()->add_precompiled_program("/precompiled/fifo", class { - inherit "/precompiled/condition": r_cond; - inherit "/precompiled/condition": w_cond; - inherit "/precompiled/mutex": lock; - - mixed *buffer; - int r_ptr, w_ptr; - - int size() { return (w_ptr+sizeof(buffer) - r_ptr) % sizeof(buffer); } - - mixed read() - { - mixed tmp; - object key=lock::lock(); - while(!size()) r_cond::wait(key); - tmp=buffer[r_ptr]; - if(++r_ptr >= sizeof(buffer)) r_ptr=0; - w_cond::signal(); - return tmp; - } - - void write(mixed v) - { - object key=lock::lock(); - while(size() == sizeof(buffer)) w_cond::wait(key); - buffer[w_ptr]=v; - if(++w_ptr >= sizeof(buffer)) r_ptr=0; - r_cond::signal(); - } - - varargs void create(int size) - { - buffer=allocate(size || 128); - } - }); - - master()->add_precompiled_program("/precompiled/queue", class { - inherit "/precompiled/condition": r_cond; - inherit "/precompiled/mutex": lock; - - mixed *buffer=allocate(16); - int r_ptr, w_ptr; - - int size() { return w_ptr - r_ptr; } - - mixed read() - { - mixed tmp; - object key=lock::lock(); - while(!size()) r_cond::wait(key); - tmp=buffer[r_ptr++]; - return tmp; - } - - void write(mixed v) - { - object key=lock::lock(); - if(w_ptr >= sizeof(buffer)) - { - buffer=buffer[r_ptr..]; - buffer+=allocate(sizeof(buffer)+1); - w_ptr-=r_ptr; - r_ptr=0; - } - buffer[w_ptr]=v; - w_ptr++; - r_cond::signal(); - } - }); -} diff --git a/lib/include/getopt.pre.pike b/lib/include/getopt.pre.pike deleted file mode 100644 index 3a2003d4a71787b1d35ccdf8242d081c74137a30..0000000000000000000000000000000000000000 --- a/lib/include/getopt.pre.pike +++ /dev/null @@ -1,281 +0,0 @@ -// startpid = (int)find_arg(argv, "s", ({ "start-script-pid" }), -// ({ "ROXEN_START_SCRIPT_PID"})); - -// configuration_dir = find_arg(argv, "d", ({ "config-dir", -// "configurations", -// "configuration-directory" }), -// ({ "ROXEN_CONFIGDIR", "CONFIGURATIONS" }), -// "../configurations"); - - -string|int find_option(array argv, - array|string shortform, - array|string|void longform, - array|string|void envvars, - mixed|void def) -{ - mixed value; - int i,hasarg; - - hasarg=query_num_arg() > 4; - if(!arrayp(longform)) longform=({longform}); - if(!arrayp(shortform)) shortform=({shortform}); - if(!arrayp(envvars)) envvars=({envvars}); - - for(i=1; i<sizeof(argv); i++) - { - if(argv[i] && strlen(argv[i]) > 1) - { - if(argv[i][0] == '-') - { - if(argv[i][1] == '-') - { - string tmp; - int nf; - - if(argv[i]=="--") break; - - sscanf(tmp=argv[i], "%s=%s", tmp, value); - - if(search(longform, tmp[2..]) != -1) - { - argv[i]=0; - if(hasarg) - { - if(!value) - { - if(i == sizeof(argv)-1) - { - werror("No argument to option "+tmp+".\n"); - exit(1); - } - value=argv[i+1]; - argv[i+1]=0; - } - return value; - } else { - return value || 1; - } - } - } else { - int j; - for(j=1;j<strlen(argv[i]);j++) - { - string opt; - int pos; - if(search(shortform, opt=argv[i][j..j]) != -1) - { - string arg; - arg=argv[i][j+1..]; - - if(hasarg) - { - if(arg=="") - { - if(i == sizeof(argv)-1) - { - werror("No argument to option -"+argv[i][j..j]+".\n"); - exit(1); - } - - value=argv[i+1]; - argv[i+1] = 0; - } else { - value=arg; - arg=""; - } - } else { - value=1; - } - - argv[i]=argv[i][..j-1]+arg; - if(argv[i]=="-") argv[i]=0; - return value; - } - } - } - } - } - } - - if(arrayp(envvars)) - foreach(envvars, value) - if(value && (value=getenv(value))) - return value; - - return def; -} - -/* - * ({ "name", type, ({aliases}), env_var, default }) - */ - -constant HAS_ARG=1; -constant NO_ARG=2; -constant MAY_HAVE_ARG=3; - -#define NAME 0 -#define TYPE 1 -#define ALIASES 2 -#define ENV 3 -#define DEF 4 - -mixed *find_all_options(string *argv, mixed *options, void|int posix_me_harder) -{ - mapping quick=([]); - foreach(options, mixed opt) - { - foreach(stringp(opt[ALIASES])?({opt[ALIASES]}):opt[ALIASES], - mixed optname) - { - if(optname[0..1]=="--") - { - quick[optname]=opt; - }else{ - foreach(optname[1..]/"",string optletter) - { - quick["-"+optletter]=opt; - } - } - } - } - - mixed *ret=({}); - for(int e=1;e<sizeof(argv);e++) - { - if(!argv[e]) continue; - - if(strlen(argv[e]) && argv[e][0]=='-') - { - if(strlen(argv[e])>1 && argv[e][1]=='-') - { - string opt=argv[e]; - if(opt=="--") break; - - string arg; - sscanf(opt,"%s=%s",opt, arg); - if(mixed *option=quick[opt]) - { - argv[e]=0; - if(!arg && option[TYPE]==HAS_ARG) - { - if(e==sizeof(argv)-1) - { - werror("No argument to option "+opt+".\n"); - exit(1); - } - arg=argv[e+1]; - argv[e+1]=0; - } - ret+=({ ({ option[0], arg || 1 }) }); - } - }else{ - string *foo=argv[e]/""; - for(int j=1;j<strlen(foo);j++) - { - string opt="-"+foo[j]; - if(mixed *option=quick[opt]) - { - foo[j]=0; - string arg; - if(option[TYPE]!=NO_ARG) - { - arg=argv[e][j+1..]; - - if(option[TYPE]==HAS_ARG && arg=="") - { - if(e==sizeof(argv)-1) - { - werror("No argument to option "+opt+".\n"); - exit(1); - } - arg=argv[e+1]; - argv[e+1]=0; - }else{ - foo=foo[..j]; - } - } - - ret+=({ ({ option[0], arg || 1 }) }); - } - } - argv[e]=foo*""; - if(argv[e]=="-") argv[e]=0; - } - }else{ - if(posix_me_harder || getenv("POSIX_ME_HARDER")) - break; - } - } - - multiset done=mkmultiset(column(ret, 0)); - foreach(options, string *option) - { - if(done[ret]) continue; - if(sizeof(option) > 2) - { - mixed foo=option[2]; - string name=option[0]; - if(!foo) continue; - if(stringp(foo)) foo=({foo}); - foreach(foo, foo) - { - if(foo=getenv(foo)) - { - ret+=({ ({name, foo}) }); - done[name]=1; - break; - } - } - - if(!done && sizeof(option)>3 && option[3]) - { - ret+=({ ({name, option[3]}) }); - done[name]=1; - } - } - } - return ret; -} - - -string *get_args(string *argv, void|int posix_me_harder) -{ - int i; - for(i=1;i<sizeof(argv);i++) - { - if(argv[i] && strlen(argv[i])>1 && argv[i][0]=='-') - { - if(argv[i][1]=='-') - { - if(argv[i]=="--") - { - argv[i]=0; - break; - }else{ - werror("Unknown option "+argv[i]+".\n"); - exit(1); - } - }else{ - if(strlen(argv[i]) == 2) - werror("Unknown option "+argv[i]+".\n"); - else - werror("Unknown options "+argv[i]+".\n"); - exit(1); - } - }else{ - if(posix_me_harder || getenv("POSIX_ME_HARDER")) - break; - } - } - - argv-=({0,1}); - - return argv; -} - -void create() -{ - add_constant("find_option",find_option); - add_constant("get_args",get_args); -} diff --git a/lib/include/process.pre.pike b/lib/include/process.pre.pike deleted file mode 100644 index f9c244646d58508f59ebe607a2b7cd4d6b9e0ea8..0000000000000000000000000000000000000000 --- a/lib/include/process.pre.pike +++ /dev/null @@ -1,99 +0,0 @@ -#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) -inherit "/precompiled/file"; - -varargs int exec(string file,string ... foo) -{ - string path; - if(search(file,"/")) - return exece(combine_path(getcwd(),file),foo,getenv()); - - path=getenv("PATH"); - - foreach(path/":",path) - if(file_stat(path=combine_path(path,file))) - return exece(path, foo,getenv()); - - return 69; -} - -varargs int spawn(string s,object stdin,object stdout,object stderr) -{ - object p; - int pid; - string t; - - pid=fork(); - - if(pid==-1) - error("No more processes.\n"); - - if(pid) - { - return pid; - }else{ - if(stdin) - stdin->dup2(File("stdin")); - - if(stdout) - stdout->dup2(File("stdout")); - - if(stderr) - stderr->dup2(File,"stderr"); - - exec("/bin/sh","-c",s); - exit(69); - } -} - -string popen(string s) -{ - object p; - string t; - - p=file::pipe(); - if(!p) error("Popen failed. (couldn't create pipe)\n"); - spawn(s,0,p,0); - destruct(p); - - t=read(0x7fffffff); - if(!t) - { - int e; - e=errno(); - close(); - error("Popen failed with error "+e+".\n"); - }else{ - close(); - } - return t; -} - -void system(string s) -{ - object p; - int pid; - string t; - - p=file::pipe(); - if(!p) error("System() failed.\n"); - p->set_close_on_exec(0); - if(pid=fork()) - { - destruct(p); - /* Nothing will ever be written here, we are just waiting for it - * to close - */ - file::read(1); - }else{ - exec("/bin/sh","-c",s); - exit(69); - } -} - -void create() -{ - add_constant("system",system); - add_constant("exec",exec); - add_constant("spawn",spawn); - add_constant("popen",popen); -} diff --git a/lib/include/simulate.pre.pike b/lib/include/simulate.pre.pike deleted file mode 100644 index 0f86e51b9e4b3d3c4a48772bf598b02e17fa0adf..0000000000000000000000000000000000000000 --- a/lib/include/simulate.pre.pike +++ /dev/null @@ -1,87 +0,0 @@ -#include <process.h> -#include <array.h> -#include <stdio.h> -#include <string.h> - -#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) - -inherit "/precompiled/regexp"; - -varargs int member_array(mixed needle,mixed *haystack,int start) -{ - return search(haystack,needle,start); -} - -object previous_object() -{ - int e; - mixed **trace; - object o,ret; - trace=backtrace(); - o=function_object(trace[-2][2]); - for(e=sizeof(trace)-3;e>=0;e--) - { - if(!trace[e][2]) continue; - ret=function_object(trace[e][2]); - if(o!=ret) return ret; - } - return 0; -} - -function this_function() -{ - return backtrace()[-2][2]; -} - -string capitalize(string s) -{ - return upper_case(s[0..0])+s[1..sizeof(s)]; -} - -function get_function(object o, string a) -{ - mixed ret; - ret=o[a]; - return functionp(ret) ? ret : 0; -} - -string *regexp(string *s, string reg) -{ - - regexp::create(reg); - s=filter(s,match); - regexp::create(); /* Free compiled regexp */ - return s; -} - -void create() -{ - add_constant("PI",3.1415926535897932384626433832795080); - add_constant("capitalize",capitalize); - add_constant("explode",`/); - add_constant("all_efuns",all_constants); - - add_constant("filter_array",filter); - add_constant("map_array",map); - - add_constant("get_function",get_function); - add_constant("implode",`*); - add_constant("m_indices",indices); - add_constant("m_sizeof",sizeof); - add_constant("m_values",values); - add_constant("member_array",member_array); - add_constant("previous_object",previous_object); - add_constant("regexp",regexp); - add_constant("strstr",search); - add_constant("sum",`+); - add_constant("this_function",this_function); - add_constant("add_efun",add_constant); - - add_constant("l_sizeof",sizeof); - add_constant("listp",multisetp); - add_constant("mklist",mkmultiset); - add_constant("aggregage_list",aggregate_multiset); -#if efun(gethostname) - add_constant("query_host_name",gethostname); -#endif -} diff --git a/lib/include/stack.pre.pike b/lib/include/stack.pre.pike deleted file mode 100644 index 2d5b2f89b51cb425933ac0c2d46807c973ebd0e6..0000000000000000000000000000000000000000 --- a/lib/include/stack.pre.pike +++ /dev/null @@ -1,34 +0,0 @@ -#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) - -class stack { - int ptr; - mixed *arr=allocate(32); - - void push(mixed val) - { - if(ptr==sizeof(arr)) arr+=allocate(ptr); - arr[ptr++]=val; - } - - mixed pop(mixed val) - { - mixed foo; - if(--ptr < 0) - error("Stack underflow\n"); - - foo=arr[ptr]; - arr[ptr]=0; /* Don't waste references */ - return foo; - } - - void reset() - { - arr=allocate(32); - ptr=0; - } -}; - -void create() -{ - master()->add_precompiled_program("/precompiled/stack",stack); -} diff --git a/lib/include/stdio.pre.pike b/lib/include/stdio.pre.pike deleted file mode 100644 index 5640225c563ed10ee591edce9060b5a1cbf1922a..0000000000000000000000000000000000000000 --- a/lib/include/stdio.pre.pike +++ /dev/null @@ -1,222 +0,0 @@ -#include <string.h> -object stderr=File("stderr"); -#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) -program FILE = class { - -#define BUFSIZE 16384 - inherit "/precompiled/file"; - - /* Private functions / buffers etc. */ - - private string b=""; - private int bpos=0; - - inline private static nomask int get_data() - { - string s; - b = b[bpos .. ]; - bpos=0; - s = file::read(BUFSIZE,1); - if(!s || !strlen(s)) - return 0; - b += s; - return 1; - } - - inline private static nomask string extract(int bytes, int|void skip) - { - string s; - s=b[bpos..bpos+bytes-1]; - bpos += bytes+skip; - return s; - } - - - /* Public functions. */ - string gets() - { - int p; - while((p=search(b, "\n", bpos)) == -1) - if(!get_data()) - return 0; - return extract(p-bpos, 1); - } - - int seek(int pos) - { - bpos=0; - b=""; - return file::seek(pos); - } - - int close(void|string mode) - { - bpos=0; - b=""; - if(!mode) mode="rw"; - file::close(mode); - } - - int open(string file, void|string mode) - { - bpos=0; - b=""; - if(!mode) mode="rwc"; - return file::open(file,mode); - } - - int open_socket() - { - bpos=0; - b=""; - return file::open_socket(); - } - - object pipe() - { - bpos=0; - b=""; - return file::pipe(); - } - - object dup() - { - object o; - o=object_program(this_object())(); - o->assign(file::dup()); - return o; - } - - void set_nonblocking() - { - error("Cannot use nonblocking IO with buffered files.\n"); - } - - int printf(string fmt, mixed ... data) - { - if(!sizeof(data)) - return file::write(fmt); - else - return file::write(sprintf(fmt,@data)); - } - - string read(int bytes) - { - while(strlen(b) - bpos < bytes) - if(!get_data()) - break; - - return extract(bytes); - } - - string ungets(string s) - { - b=s+b[bpos..]; - bpos=0; - } - - int getchar() - { - if(strlen(b) - bpos < 1) - if(!get_data()) - return -1; - - return b[bpos++]; - } - }; - -inherit "/precompiled/file"; - -string read_file(string filename,void|int start,void|int len) -{ - object buf,f; - string ret,tmp; - f=FILE(); - if(!f->open(filename,"r")) return 0; - - switch(query_num_arg()) - { - case 1: - ret=f->read(0x7fffffff); - break; - - case 2: - len=0x7fffffff; - case 3: - while(start-- && f->gets()); - buf=String_buffer(); - while(len-- && (tmp=f->gets())) - { - buf->append(tmp); - buf->append("\n"); - } - ret=buf->get_buffer(); - destruct(buf); - } - destruct(f); - - return ret; -} - -string read_bytes(string filename,void|int start,void|int len) -{ - string ret; - if(!open(filename,"r")) - return 0; - - switch(query_num_arg()) - { - case 1: - case 2: - len=0x7fffffff; - case 3: - seek(start); - } - ret=read(len); - close(); - return ret; -} - -int write_file(string filename,string what) -{ - int ret; - if(!open(filename,"awc")) - error("Couldn't open file "+filename+".\n"); - - ret=write(what); - close(); - return ret; -} - -int file_size(string s) -{ - int *stat; - stat=file_stat(s); - if(!stat) return -1; - return stat[1]; -} - -void perror(string s) -{ -#if efun(strerror) - stderr->write(s+": "+strerror(predef::errno())+"\n"); -#else - stderr->write(s+": errno: "+predef::errno()+"\n"); -#endif -} - -void create() -{ - add_constant("file_size",file_size); - add_constant("write_file",write_file); - add_constant("read_bytes",read_bytes); - add_constant("read_file",read_file); - add_constant("perror",perror); - - - master()->add_precompiled_program("/precompiled/FILE", FILE); - - add_constant("stdin",FILE("stdin")); - add_constant("stdout",File("stdout")); - add_constant("stderr",stderr); -} diff --git a/lib/include/string.pre.pike b/lib/include/string.pre.pike deleted file mode 100644 index 5d1f5f2b5e8e747ae87262809a4aedf41e902fbe..0000000000000000000000000000000000000000 --- a/lib/include/string.pre.pike +++ /dev/null @@ -1,71 +0,0 @@ -#define BEGIN 32 - -/* - * Implode an array of strings to an english 'list' - * ie. ({"foo","bar","gazonk"}) beomces "foo, bar and gazonk" - */ -string implode_nicely(string *foo, string|void and) -{ - if(!and) and="and"; - switch(sizeof(foo)) - { - case 0: return ""; - case 1: return foo[0]; - default: return foo[0..sizeof(foo)-2]*", "+" "+and+" "+foo[-1]; - } -} - -string strmult(string str, int num) -{ -#if 1 - num*=strlen(str); - while(strlen(str) < num) str+=str; - return str[0..num-1]; -#endif -#if 0 - return sprintf("%~n",str,strlen(str)*num); -#endif -} - -void create() -{ - add_constant("strmult",strmult); - add_constant("implode_nicely",implode_nicely); - - master()->add_precompiled_program("/precompiled/string_buffer", class { - string *buffer=allocate(BEGIN); - int ptr=0; - - static void fix() - { - string tmp=buffer*""; - buffer=allocate(strlen(tmp)/128+BEGIN); - buffer[0]=tmp; - ptr=1; - } - - string get_buffer() - { - if(ptr != 1) fix(); - return buffer[0]; - } - - void append(string s) - { - if(ptr==sizeof(buffer)) fix(); - buffer[ptr++]=s; - } - - mixed cast(string to) - { - if(to=="string") return get_buffer(); - return 0; - } - - void flush() - { - buffer=allocate(BEGIN); - ptr=0; - } - }); -}