pike.git / lib / modules / Stdio.pmod / Terminfo.pmod

version» Context lines:

pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:1: - // $Id: Terminfo.pmod,v 1.7 1999/11/18 04:08:22 hubbe Exp $ + // $Id: Terminfo.pmod,v 1.8 2000/03/30 21:21:10 grubba Exp $         #if constant(thread_create)   #define LOCK object m_key = mutex->lock()   #define UNLOCK destruct(m_key) - #define MUTEX static private object mutex = Thread.Mutex(); + #define MUTEX static private object(Thread.Mutex) mutex = Thread.Mutex();   #else   #define LOCK   #define UNLOCK   #define MUTEX   #endif      MUTEX      static private array ctrlcharsfrom =    Array.map(indices(allocate(32)),
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:29:       mapping(string:string|int) map = ([]);       int tgetflag(string id)    {    return map[id]==1;    }       int tgetnum(string id)    { -  return intp(map[id]) && map[id]; +  return intp(map[id]) && [int]map[id];    }       string tgetstr(string id)    { -  return stringp(map[id]) && map[id]; +  return stringp(map[id]) && [string]map[id];    }       string tparam(string f, mixed ... args)    { -  array fmt=f/"%"; +  array(string) fmt=f/"%";    string res=fmt[0];    string tmp;    int z;    mapping var=([]);    array args0=args;    - #define POP (z=args[0],args=args[1..],z) + #define POP (z=[int]args[0],args=args[1..],z)   #define PUSH(x) (args=({x})+args)       while ( (fmt=fmt[1..])!=({}) )    if (fmt[0]=="") res+="%";    else    {    switch (fmt[0][0])    {    case 'd': res+=sprintf("%d%s",POP,fmt[0][1..]); break;    case 'x': res+=sprintf("%x%s",POP,fmt[0][1..]); break;
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:140:    return tstr && tputs(tstr);    }      }      class Termcap {       inherit TermMachine;       array(string) aliases; -  object parent; +  static object(Termcap) parent;       string tputs(string s)    {    // Delay stuff completely ignored...    sscanf(s, "%*d%s", s);    return s;    }       private static multiset(string) load_cap(string en)    {
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:237:    }    else // wierd    {    // ignore    }    }       return clears;    }    -  void create(string cap, object|void tcdb, int|void maxrecurse) +  void create(string cap, object(TermcapDB)|void tcdb, int|void maxrecurse)    {    int i=0;    while((i=search(cap, "\\\n", i))>=0) {    string capr;    if(2!=sscanf(cap[i..], "\\\n%*[ \t\r]%s", capr))    break;    cap = cap[..i-1]+capr;    }    multiset(string) clears = load_cap(cap);    if(map->tc) {
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:321:    while (3==sscanf(s, "%s$<%*[0-9.]>%s", pre, post))    s = pre+post;    return s;    }       static private string swab(string s)    {    return Array.map(s/2, reverse)*"";    }    -  static private int load_cap(object f, int|void bug_compat) +  static private int load_cap(object(Stdio.File) f, int|void bug_compat)    {    int magic, sname, nbool, nnum, nstr, sstr;       if (6!=sscanf(swab(f->read(12)), "%2c%2c%2c%2c%2c%2c",    magic, sname, nbool, nnum, nstr, sstr) ||    magic != 0432)    return 0;    aliases = (f->read(sname)-"\0")/"|";    {    int blen = nbool;    if((nbool&1) && !bug_compat)    blen++;    array(int) bools = values(f->read(blen)[..nbool-1]);    if (sizeof(bools)>sizeof(boolnames))    bools = bools[..sizeof(boolnames)-1];    map = mkmapping(boolnames[..sizeof(bools)-1], bools);    }    { -  array(int) nums = array_sscanf(swab(f->read(nnum*2)), "%2c"*nnum); +  array(int) nums = [array(int)] +  array_sscanf(swab(f->read(nnum*2)), "%2c"*nnum);    if (sizeof(nums)>sizeof(numnames))    nums = nums[..sizeof(numnames)-1];    mapping(string:int) tmp = mkmapping(numnames[..sizeof(nums)-1], nums);    foreach (numnames[..sizeof(nums)-1], string name)    if (tmp[name]>=0xfffe)    m_delete(tmp, name);    map += tmp;    }    {    string stroffs = swab(f->read(nstr*2));
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:379:    foreach (strnames[..sizeof(strarr)-1], string name)    if (!tmp[name])    m_delete(tmp, name);    map += tmp;    }    return 1;    }       void create(string filename)    { -  object f = Stdio.File(); +  object(Stdio.File) f = Stdio.File();    if (!f->open(filename, "r"))    error("Terminfo: unable to open terminfo file \"%s\"\n", filename);    int r = load_cap(f);    f->close();    if (!r)    error("Terminfo: unparsable terminfo file \"%s\"\n", filename);    }   }      class TermcapDB {       MUTEX       static private inherit Stdio.File;       static private string buf=""; -  static private mapping(string:int|object) cache=([]); +  static private mapping(string:int|object(Termcap)) cache=([]);    static private int complete_index=0;       void create(string|void filename)    {    if (!filename) { -  string tce = getenv("TERMCAP"); +  string tce = [string]getenv("TERMCAP");    if (tce && strlen(tce) && tce[0]=='/')    filename = tce;    else    filename = "/etc/termcap";    }    if (!::open(filename, "r"))    error("failed to open termcap file %O\n", filename);    }       static private void rewind(int|void pos)
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:510:    LOCK;    if(!complete_index) {    rewind();    while(read());    complete_index = 1;    }    UNLOCK;    return sort(indices(cache));    }    -  array(object) _values() +  array(object(Termcap)) _values()    {    array(object|int) res = ({});    mapping(int:string) extra = ([]);    LOCK;    if (complete_index)    res = Array.map(sort(indices(cache)), -  +  [function(string,mapping(int:string):object(Termcap))]    lambda(string name, mapping(int:string) extra) {    if (!objectp(cache[name]) && !extra[cache[name]])    extra[cache[name]] = readat(cache[name]);    return cache[name];    }, extra);    else {    array(string) resi = ({});    string cap;    int i = 1;    rewind();
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:542:    o = i++;    extra[o] = cap;    }    res += ({ o }) * sizeof(names);    resi += names;    }    sort(resi, res);    complete_index = 1;    }    UNLOCK; -  return Array.map(res, lambda(int|object x, mapping(int:object) y) { +  return [array(object(Termcap))] +  Array.map(res, +  lambda(int|object(Termcap) x, mapping(int:object(Termcap)) y) {    return objectp(x)? x : y[x];    },    mkmapping(indices(extra),    Array.map(values(extra),    Termcap, this_object())));    }       static private string read_next(string find) // quick search    {    for (;;)
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:583:    buf = buf[i+1..];       continue;    }    for(j=-1; (i=search(buf,"\n",j+1))>=0; j=i);    buf = buf[j+1..];    if (!more_data()) return 0; // eof    }    }    -  object load(string term, int|void maxrecurse) +  object(Termcap) load(string term, int|void maxrecurse)    { -  int|string|object cap; +  int|string|object(Termcap) cap;       LOCK;    if (zero_type(cache[term]))    {    if (!complete_index)    {    rewind();    do    cap = read_next(term);    while(cap && search(get_names(cap), term)<0);
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:610:    cap = read();    }    UNLOCK;    if (stringp(cap))    {    array(string) names = get_names(cap);    if ((cap = Termcap(cap, this_object(), maxrecurse)))    {    LOCK;    foreach(names, string name) -  cache[name] = cap; +  cache[name] = [object(Termcap)]cap;    UNLOCK;    }    } -  return objectp(cap) && cap; +  return objectp(cap) && [object(Termcap)]cap;    }    -  object `[](string name) +  object(Termcap) `[](string name)    {    return load(name);    }   }         class TerminfoDB {       MUTEX       static private string dir; -  static private mapping(string:object) cache = ([]); +  static private mapping(string:object(Terminfo)) cache = ([]);    static private int complete_index=0;       void create(string|void dirname)    {    if (!dirname)    {    foreach (({"/usr/share/lib/terminfo", "/usr/share/termcap",    "/usr/lib/terminfo", "/usr/share/misc/terminfo"}), string dn)    {    array(int) s = file_stat(dn);
pike.git/lib/modules/Stdio.pmod/Terminfo.pmod:678:    cache[b] = 0;    complete_index = 1;    }    UNLOCK;    return sort(indices(cache));    }       array(object) _values()    {    return Array.map(_indices(), +  [function(string:object(Terminfo))]    lambda(string name) {    return cache[name] ||    Terminfo(dir+name[..0]+"/"+name);    });    }    -  object load(string term) +  object(Terminfo) load(string term)    { -  object ti; +  object(Terminfo) ti;       if (!strlen(term))    return 0;    LOCK;    if (!(ti = cache[term]))    {    if (file_stat(dir+term[..0]+"/"+term))    ti = Terminfo(dir+term[..0]+"/"+term);    if (ti)    cache[term] = ti;    }    UNLOCK;    return ti;    }    -  object `[](string name) +  object(Terminfo) `[](string name)    {    return load(name);    } -  +    }    - static private object defterm, deftermcap, defterminfo; + static private object(Termcap) defterm; + static private object(TermcapDB) deftermcap; + static private object(TerminfoDB) defterminfo;    - object defaultTermcapDB() + object(TermcapDB) defaultTermcapDB()   { -  object tcdb; +  object(TermcapDB) tcdb;    LOCK;    catch { tcdb = deftermcap || (deftermcap = TermcapDB()); };    UNLOCK;    return tcdb;   }    - object defaultTerminfoDB() + object(TerminfoDB) defaultTerminfoDB()   { -  object tidb; +  object(TerminfoDB) tidb;    LOCK;    catch { tidb = defterminfo || (defterminfo = TerminfoDB()); };    UNLOCK;    return tidb;   }    - object getTermcap(string term) + object(Termcap) getTermcap(string term)   { -  object tcdb = defaultTermcapDB(); +  object(TermcapDB) tcdb = defaultTermcapDB();    return tcdb && tcdb[term];   }    - object getTerminfo(string term) + object(Terminfo) getTerminfo(string term)   { -  object tidb = defaultTerminfoDB(); +  object(TerminfoDB) tidb = defaultTerminfoDB();    return tidb && tidb[term];   }    - object getTerm(string|void term) + object(Termcap) getTerm(string|void term)   {    if (!term) { -  object t = defterm; +  object(Termcap) t = defterm;    if (!t)    { -  string tc = getenv("TERMCAP"); +  string tc = [string]getenv("TERMCAP");    t = (tc && sizeof(tc) && tc[0]!='/'?    Termcap(tc) : getTerm(getenv("TERM")||"dumb"));    LOCK;    if (!defterm)    defterm = t;    UNLOCK;    }    return t;    }    return getTerminfo(term) || getTermcap(term) || getFallbackTerm(term);   }    - static object getFallbackTerm(string term) + static object(Termcap) getFallbackTerm(string term)   {    return (term=="dumb"? Termcap("dumb:\\\n\t:am:co#80:do=^J:") :    getTerm("dumb"));   }