static int scan (void * arg, word segm, word ofs) { Context_Tag tag; ctxt_info_t *info; (void)arg; assert(segm); assert(ofs != (word)-1); redo: if((tag = peekb(segm, ofs), ctxtIsInfoTag(tag))) { info = &CTXT_INFO_STRUCT(tag); if(peekb(segm, ofs + 1) == '=' && peekb(segm, ofs + 4) == 0) { /* status entry */ info->c_sizemax = peekw(segm, ofs + 2) & ~0x8001; } else { /* normal entry */ char far *p = MK_FP(segm, ofs + 1); unsigned num; for(num = 0; isxdigit(*p); ++p) { num <<= 8; if(isdigit(*p)) num |= *p & 0xf; else { assert(islower(*p)); num |= *p - 'a' + 10; } } if(*p == '=') { info->c_sizecur += env_varlen(segm, ofs); if(num < info->c_nummin) info->c_nummin = num; if(num > info->c_nummax) info->c_nummax = num; } else { /* Invalid entry -> remove to make room for useful stuff */ env_subVarOffset(segm, ofs); goto redo; } } } else if(tag > ' ') { /* Alias */ CTXT_INFO(CTXT_TAG_ALIAS, sizecur) += env_varlen(segm ,ofs); } return 0; /* proceed */ }
word env_endOfVars(const word segm) { word offset; DBG_ENTER("env_endOfVars", Suppl_env) DBG_ARGUMENTS( ("env=%u", segm) ) chkMem assert(segm != 0); offset = 0; /* env starts always at location 0 */ while(peekb(segm, offset)) /* there is a variable */ offset += env_varlen(segm, offset); DBG_RETURN_U( offset) /* This is the terminator byte */ }
void env_subVarOffset(word segm, word offset) { register unsigned moveStart; DBG_ENTER("env_subVarOffset", Suppl_env) DBG_ARGUMENTS( ("env=%u, ofs=%u", segm, offset) ) chkMem assert(segm); assert(offset < env_endOfVars(segm)); moveStart = offset + env_varlen(segm, offset); _fmemmove(MK_FP(segm, offset), MK_FP(segm, moveStart), env_firstFree(segm) - moveStart); chkMem DBG_EXIT }
int chgCtxt(const Context_Tag tag, const char * const name, const char * const value) { word segm, ofs; int newlen; assert(name); ctxtCheckInfoTag(tag); segm = ctxtFromTag(tag); assert(segm); newlen = value? strlen(name) + strlen(value) + 2: 0; if((ofs = env_findVar(segm, name)) != (word)-1) newlen -= env_varlen(segm, ofs); /* Make sure the context has enough room to add the value to */ if(newlen > 0) { /* contents size will grow */ ctxt_info_t *hinfo, *dinfo; /* aliases may not exceed sizemax */ if(tag == CTXT_TAG_ALIAS) { if(CTXT_INFO(CTXT_TAG_ALIAS, sizemax) - CTXT_INFO(CTXT_TAG_ALIAS, sizecur) < newlen) { error_alias_out_of_memory(); return E_NoMem; } } /* Otherwise a removeable entry is removed; those are: oldest item of history oldest item of dirstack */ hinfo = segm == ctxtFromTag(CTXT_TAG_HISTORY) ? &CTXT_INFO_STRUCT(CTXT_TAG_HISTORY): 0; dinfo = segm == ctxtFromTag(CTXT_TAG_DIRSTACK) ? &CTXT_INFO_STRUCT(CTXT_TAG_DIRSTACK): 0; while(env_freeCount(segm) < newlen) { /* There are two structures that can shrink: history & dirstack */ if(hinfo && (hinfo->c_sizecur <= hinfo->c_sizemax /* inconsitency of redundant info */ || hinfo->c_nummin >= hinfo->c_nummax)) /* in range -> ignore */ hinfo = 0; if(dinfo && (dinfo->c_sizecur <= dinfo->c_sizemax /* inconsitency of redundant info */ || dinfo->c_nummin >= dinfo->c_nummax)) /* in range -> ignore */ dinfo = 0; if(hinfo && dinfo) { /* Choose one */ if(weight(hinfo) < weight(dinfo)) ctxtGet(1, CTXT_TAG_DIRSTACK, ++dinfo->c_nummin, 0); else ctxtGet(1, CTXT_TAG_HISTORY, ++hinfo->c_nummin, 0); } else if(dinfo) ctxtGet(1, CTXT_TAG_DIRSTACK, ++dinfo->c_nummin, 0); else if(hinfo) ctxtGet(1, CTXT_TAG_HISTORY, ++hinfo->c_nummin, 0); else { /* no space left */ error_context_out_of_memory(); return E_NoMem; } } } /* return values 1 and 3 are OK */ switch(env_change(segm, name, value)) { case 2: /* variable to delete not found <-> no problem */ case 1: case 3: /* var replaced | deleted | inserted ==> OK */ CTXT_INFO(tag, sizecur) += newlen; return 0; case 0: /* Cannot insert */ dprintf(("chgCtxt(): out-of-mem shouldn't occure here!\n")); error_context_out_of_memory(); return E_NoMem; default: return E_Syntax; } }