int zlecharasstring(ZLE_CHAR_T inchar, char *buf) { #ifdef MULTIBYTE_SUPPORT size_t ret; char *ptr; #ifdef __STDC_ISO_10646__ if (ZSH_INVALID_WCHAR_TEST(inchar)) { buf[0] = ZSH_INVALID_WCHAR_TO_CHAR(inchar); ret = 1; } else #endif { ret = wctomb(buf, inchar); if (ret <= 0) { /* Ick. */ buf[0] = '?'; return 1; } } ptr = buf + ret - 1; for (;;) { if (imeta(*ptr)) { char *ptr2 = buf + ret - 1; for (;;) { ptr2[1] = ptr2[0]; if (ptr2 == ptr) break; ptr2--; } *ptr = Meta; ret++; } if (ptr == buf) return ret; ptr--; } #else if (imeta(inchar)) { buf[0] = Meta; buf[1] = inchar ^ 32; return 2; } else { buf[0] = inchar; return 1; } #endif }
mod_export char * bracketedstring(void) { static const char endesc[] = "\033[201~"; int endpos = 0; size_t psize = 64; char *pbuf = zalloc(psize); size_t current = 0; int next, timeout; while (endesc[endpos]) { if (current + 1 >= psize) pbuf = zrealloc(pbuf, psize *= 2); if ((next = getbyte(1L, &timeout)) == EOF) break; if (!endpos || next != endesc[endpos++]) endpos = (next == *endesc); if (imeta(next)) { pbuf[current++] = Meta; pbuf[current++] = next ^ 32; } else if (next == '\r') pbuf[current++] = '\n'; else pbuf[current++] = next; } pbuf[current-endpos] = '\0'; return pbuf; }
static void pputc(char c) { if (imeta(c)) { *bv->bp++ = Meta; c ^= 32; } *bv->bp++ = c; if (c == '\n' && !bv->dontcount) bv->bufline = bv->bp; }
mod_export char * shingetline(void) { char *line = NULL; int ll = 0; int c; char buf[BUFSIZ]; char *p; p = buf; winch_unblock(); for (;;) { do { errno = 0; c = fgetc(bshin); } while (c < 0 && errno == EINTR); if (c < 0 || c == '\n') { winch_block(); if (c == '\n') *p++ = '\n'; if (p > buf) { *p++ = '\0'; line = zrealloc(line, ll + (p - buf)); memcpy(line + ll, buf, p - buf); } return line; } if (imeta(c)) { *p++ = Meta; *p++ = c ^ 32; } else *p++ = c; if (p >= buf + BUFSIZ - 1) { winch_block(); line = zrealloc(line, ll + (p - buf) + 1); memcpy(line + ll, buf, p - buf); ll += p - buf; line[ll] = '\0'; p = buf; winch_unblock(); } } }
/***************************************************************************************************************************** open Prepares the database for use. An existing database is opened or otherwise an empty database is created. If the database is openend a table _META with meta info is loaded into a dictionary _META. The dictionary key is the code of the data element. A data element is either a tabel (dimension/event) or a field (characteristic/property). The value of the dictionary is the MET_INFO structure. This structure contains meta data about the data element. If the database is created an empty table _META is created. The function calls virtual functions, db_, which must be overriden by a derived class for a specific database implementation. ******************************************************************************************************************************/ void ARAS::open(DBInfo dbInfo) { if (isopen) return; // store db information this->dbInfo = dbInfo; // open or create database // performs the database specific initialization code, the specific information to be used is stored in the struct DBInfo. db_init_database(); // performs the database specific code to check whether a database alreay exists and open it. if (db_open_database()) { // read _META table from database into dictionary _META TABLE values; db_select_table("_META", {"CODE","INFO"}, values); for (auto i : values) _META.insert(std::pair<std::string, META_INFO>(i[0], META_INFO(i[1]))); } else { // performs the database specific code to create a new database based on DBInfo. db_create_database(); // create table _META: define the columns META_INFO imeta(data_type::DIM, 0, "_META", "", "", "_META", "CODE", 0, 0, 0, 0); META_INFO icode(data_type::STR, SIZE_META_CODE_MAX, "_META", "", "","_META", "", 0, 0, 1, 0); META_INFO idescs(data_type::STR, SIZE_DESCS_MAX, "_META", "", "", "_META", "", 0, 0, 0, 0); META_INFO idescl(data_type::STR, SIZE_DESCL_MAX, "_META", "", "", "_META", "", 0, 0, 0, 0); META_INFO iinfo(data_type::STR, 0, "_META", "", "", "_META", "",0, 0, 0, 0); // define the table META_TABLE meta = { { "_META", "Meta data", "Meta data", imeta }, { "CODE", "CODE", "CODE", icode }, { "DESCS", "Short Description", "Short Description", idescs }, { "DESCL", "Long Description", "Long Description", idescl }, { "INFO", "INFO", "INFO", iinfo } }; // performs the database specific code to create a new table db_create_table(meta); // load the meta data into the dictionary and the database table _META for (auto i : meta) insert_meta(i); } }
mod_export char * zlelineasstring(ZLE_STRING_T instr, int inll, int incs, int *outllp, int *outcsp, int useheap) { int outcs, outll, sub; struct region_highlight *rhp; #ifdef MULTIBYTE_SUPPORT char *s; int i, j; size_t mb_len = 0; mbstate_t mbs; s = zalloc(inll * MB_CUR_MAX + 1); outcs = 0; memset(&mbs, 0, sizeof(mbs)); for (i=0; i < inll; i++) { if (incs == 0) outcs = mb_len; incs--; if (region_highlights && outcsp == &zlemetacs) { for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS; rhp < region_highlights + n_region_highlights; rhp++) { if (rhp->flags & ZRH_PREDISPLAY) sub = predisplaylen; else sub = 0; if (rhp->start - sub == 0) rhp->start_meta = sub + mb_len; rhp->start--; if (rhp->end - sub == 0) rhp->end_meta = sub + mb_len; rhp->end--; } } #ifdef __STDC_ISO_10646__ if (ZSH_INVALID_WCHAR_TEST(instr[i])) { s[mb_len++] = ZSH_INVALID_WCHAR_TO_CHAR(instr[i]); } else #endif { j = wcrtomb(s + mb_len, instr[i], &mbs); if (j == -1) { /* invalid char */ s[mb_len++] = ZWC('?'); memset(&mbs, 0, sizeof(mbs)); } else { mb_len += j; } } } if (incs == 0) outcs = mb_len; if (region_highlights && outcsp == &zlemetacs) { for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS; rhp < region_highlights + n_region_highlights; rhp++) { if (rhp->flags & ZRH_PREDISPLAY) sub = predisplaylen; else sub = 0; if (rhp->start - sub == 0) rhp->start_meta = sub + mb_len; if (rhp->end - sub == 0) rhp->end_meta = sub + mb_len; } } s[mb_len] = '\0'; outll = mb_len; #else outll = inll; outcs = incs; if (region_highlights && outcsp == &zlemetacs) { for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS; rhp < region_highlights + n_region_highlights; rhp++) { rhp->start_meta = rhp->start; rhp->end_meta = rhp->end; } } #endif /* * *outcsp and *outllp are to be indexes into the final string, * not character offsets, so we need to take account of any * metafiable characters. */ if (outcsp != NULL || outllp != NULL) { #ifdef MULTIBYTE_SUPPORT char *strp = s; #else char *strp = instr; #endif char *stopcs = strp + outcs; char *stopll = strp + outll; char *startp = strp; if (region_highlights && outcsp == &zlemetacs) { for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS; rhp < region_highlights + n_region_highlights; rhp++) { /* Used as temporary storage */ rhp->start = rhp->start_meta; rhp->end = rhp->end_meta; } } while (strp < stopll) { if (imeta(*strp)) { if (strp < stopcs) outcs++; if (region_highlights && outcsp == &zlemetacs) { for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS; rhp < region_highlights + n_region_highlights; rhp++) { if (rhp->flags & ZRH_PREDISPLAY) sub = predisplaylen; else sub = 0; if (strp < startp + rhp->start - sub) { rhp->start_meta++; } if (strp < startp + rhp->end - sub) { rhp->end_meta++; } } } outll++; } strp++; } if (outcsp != NULL) *outcsp = outcs; if (outllp != NULL) *outllp = outll; } #ifdef MULTIBYTE_SUPPORT if (useheap) { char *ret = metafy(s, mb_len, META_HEAPDUP); zfree(s, inll * MB_CUR_MAX + 1); return ret; } return metafy(s, mb_len, META_REALLOC); #else return metafy(instr, inll, useheap ? META_HEAPDUP : META_DUP); #endif }
static int ptyread(char *nam, Ptycmd cmd, char **args, int noblock, int mustmatch) { int blen, used, seen = 0, ret = 0, matchok = 0; char *buf; Patprog prog = NULL; if (*args && args[1]) { char *p; if (args[2]) { zwarnnam(nam, "too many arguments"); return 1; } p = dupstring(args[1]); tokenize(p); remnulargs(p); /* Signals handlers might stomp PAT_STATIC */ if (!(prog = patcompile(p, PAT_ZDUP, NULL))) { zwarnnam(nam, "bad pattern: %s", args[1]); return 1; } } else fflush(stdout); if (cmd->old) { used = cmd->olen; buf = (char *) zhalloc((blen = 256 + used) + 1); memcpy(buf, cmd->old, cmd->olen); zfree(cmd->old, cmd->olen); cmd->old = NULL; cmd->olen = 0; } else { used = 0; buf = (char *) zhalloc((blen = 256) + 1); } if (cmd->read != -1) { buf[used] = (char) cmd->read; buf[used + 1] = '\0'; seen = used = 1; cmd->read = -1; } do { if (noblock && cmd->read == -1) { int pollret; /* * Check there is data available. Borrowed from * poll_read() in utils.c and simplified. */ #ifdef HAVE_SELECT fd_set foofd; struct timeval expire_tv; expire_tv.tv_sec = 0; expire_tv.tv_usec = 0; FD_ZERO(&foofd); FD_SET(cmd->fd, &foofd); pollret = select(cmd->fd+1, (SELECT_ARG_2_T) &foofd, NULL, NULL, &expire_tv); #else #ifdef FIONREAD if (ioctl(cmd->fd, FIONREAD, (char *) &val) == 0) pollret = (val > 0); #endif #endif if (pollret < 0) { /* * See read_poll() for this. * Last despairing effort to poll: attempt to * set nonblocking I/O and actually read the * character. cmd->read stores the character read. */ long mode; if (setblock_fd(0, cmd->fd, &mode)) pollret = read(cmd->fd, &cmd->read, 1); if (mode != -1) fcntl(cmd->fd, F_SETFL, mode); } if (pollret == 0) break; } if (!ret) { checkptycmd(cmd); if (cmd->fin) break; } if (cmd->read != -1 || (ret = read(cmd->fd, buf + used, 1)) == 1) { int readchar; if (cmd->read != -1) { ret = 1; readchar = cmd->read; cmd->read = -1; } else readchar = STOUC(buf[used]); if (imeta(readchar)) { buf[used++] = Meta; buf[used++] = (char) (readchar ^ 32); } else buf[used++] = (char) readchar; seen = 1; if (used >= blen-1) { if (!*args) { buf[used] = '\0'; unmetafy(buf, &used); write_loop(1, buf, used); used = 0; } else { buf = hrealloc(buf, blen, blen << 1); blen <<= 1; } } } buf[used] = '\0'; if (!prog) { if (ret <= 0 || (*args && buf[used - 1] == '\n' && (used < 2 || buf[used-2] != Meta))) break; } else { if (ret < 0 #ifdef EWOULDBLOCK && errno != EWOULDBLOCK #else #ifdef EAGAIN && errno != EAGAIN #endif #endif ) break; } } while (!(errflag || breaks || retflag || contflag) && used < READ_MAX && !(prog && ret && (matchok = pattry(prog, buf)))); if (prog && ret < 0 && #ifdef EWOULDBLOCK errno == EWOULDBLOCK #else #ifdef EAGAIN errno == EAGAIN #endif #endif ) { cmd->old = (char *) zalloc(cmd->olen = used); memcpy(cmd->old, buf, cmd->olen); return 1; } if (*args) setsparam(*args, ztrdup(buf)); else if (used) { unmetafy(buf, &used); write_loop(1, buf, used); } { int ret = cmd->fin + 1; if (seen && (!prog || matchok || !mustmatch)) ret = 0; if (prog) freepatprog(prog); return ret; } }