void getDebuggerInput(char * msg) { char command[MAX_LINE]; char * context; jmp_buf errorJumpSave; UINT * resultStackIdxSave; SYMBOL * contextSave; while(TRUE) { if(currentContext != mainContext) context = currentContext->name; else context = ""; if(!evalSilent) varPrintf(OUT_CONSOLE, "\n[%s %d %s]%s", msg, recursionCount, context, footerStr); else evalSilent = FALSE; if(fgets(command, MAX_LINE - 1, IOchannel) == NULL) fatalError(ERR_IO_ERROR, 0, 0); /* client and server could have different line-termination */ if(*(command + 1) == '\n' || *(command + 1) == '\r') { if(*command == 'n') { traceFlag |= TRACE_DEBUG_NEXT; currentLevel = recursionCount; break; } if(*command == 's') { traceFlag &= ~TRACE_DEBUG_NEXT; break; } if(*command == 'q') { closeTrace(); longjmp(errorJump, ERR_USER_RESET); } if(*command == 'c') { closeTrace(); break; } } resultStackIdxSave = resultStackIdx; memcpy(errorJumpSave, errorJump, sizeof(jmp_buf)); contextSave = currentContext; currentContext = currentFunc->context; if(setjmp(errorJump)) { cleanupResults(resultStackIdxSave); goto DEBUG_EVAL_END; } executeCommandLine(command, OUT_CONSOLE, NULL); DEBUG_EVAL_END: currentContext = contextSave; memcpy(errorJump, errorJumpSave, sizeof(jmp_buf)); } }
CELL * p_find(CELL * params) { char * key; char * str; ssize_t found; CELL * next; CELL * keyCell; CELL * funcCell; size_t size; long options = -1; size_t offset = 0; UINT * resultIdxSave; keyCell = evaluateExpression(params); params = getEvalDefault(params->next, &next); if(keyCell->type == CELL_STRING && next->type == CELL_STRING) { key = (char *)keyCell->contents; str = (char *)next->contents; size = next->aux - 1; if(params != nilCell) { if(params->next != nilCell) getInteger(params->next, (UINT*)&offset); if(offset > size) offset = size; params = evaluateExpression(params); if(!isNil(params)) getIntegerExt(params, (UINT *)&options, FALSE); } if(options == -1) found = searchBuffer(str + offset, size - offset, key, keyCell->aux - 1, TRUE); else found = searchBufferRegex(str, (int)offset, key, (int)size, options, NULL) - offset; if(found < 0) return(nilCell); } else { /* list mode with optional functor */ if(!isList(next->type)) return(nilCell); next = (CELL *)next->contents; found = 0; if(params != nilCell) funcCell = evaluateExpression(params); else funcCell = NULL; /* do regex when first arg is string and option# is present */ if(funcCell && isNumber(funcCell->type) && keyCell->type == CELL_STRING) { getIntegerExt(funcCell, (UINT*)&options, FALSE); key = (char *)keyCell->contents; while(next != nilCell) { if(next->type == CELL_STRING) { if(searchBufferRegex((char *)next->contents, 0, key, next->aux - 1 , options, NULL) != -1) break; } found++; next = next->next; } if(next == nilCell) return(nilCell); else return(stuffInteger(found)); } resultIdxSave = resultStackIdx; while(next != nilCell) { if(compareFunc(keyCell, next, funcCell) == 0) { if(funcCell) { deleteList((CELL*)sysSymbol[0]->contents); sysSymbol[0]->contents = (UINT)copyCell(next); } break; } found++; next = next->next; cleanupResults(resultIdxSave); } if(next == nilCell) return(nilCell); } return(stuffInteger(found + offset)); }
CELL * findAllList(CELL * pattern, CELL * list, CELL * exprCell) { CELL * result = nilCell; CELL * cell = NULL; CELL * exprRes; CELL * match; CELL * funcCell; int errNo; UINT * resultIdxSave; funcCell = evaluateExpression(exprCell->next); resultIdxSave = resultStackIdx; if(funcCell == nilCell && !isList(pattern->type)) return(errorProcExt(ERR_LIST_EXPECTED, pattern)); while(list != nilCell) { if(funcCell == nilCell) { /* match only takes lists*/ if(!isList(list->type)) goto CONTINUE_NEXT; match = patternMatchL((CELL *)pattern->contents, (CELL *)list->contents, TRUE); if(match == NULL || match == nilCell) goto CONTINUE_NEXT; deleteList(match); } else { cleanupResults(resultIdxSave); if(compareFunc(pattern, list, funcCell) != 0) goto CONTINUE_NEXT; } /* take out sysSymbol in future, should only be used in regex */ deleteList((CELL*)sysSymbol[0]->contents); sysSymbol[0]->contents = (UINT)copyCell(list); itSymbol->contents = (UINT)list; if(exprCell != nilCell) { if((exprRes = evaluateExpressionSafe(exprCell, &errNo)) == NULL) { pushResult(result); /* push for later deletion */ longjmp(errorJump, errNo); } } else exprRes = list; exprRes = copyCell(exprRes); if(result == nilCell) { cell = exprRes; result = makeCell(CELL_EXPRESSION, (UINT)cell); } else { cell->next = exprRes; cell = cell->next; } CONTINUE_NEXT: list = list->next; } if(result == nilCell) return(getCell(CELL_EXPRESSION)); itSymbol->contents = (UINT)nilCell; return(result); }
CELL * p_replace(CELL * params) { CELL * keyCell; CELL * repCell; CELL * funcCell = NULL; CELL * list; CELL * cell; CELL * newList; char * keyStr; char * buff; char * newBuff; UINT cnt; size_t newLen; long options; UINT * resultIdxSave; SYMBOL * refSymbol; keyCell = copyCell(evaluateExpression(params)); pushResult(keyCell); params = getEvalDefault(params->next, &cell); newList = cell; refSymbol = symbolCheck; if(symbolCheck && (isProtected(symbolCheck->flags) || isBuiltin(symbolCheck->flags))) return(errorProcExt2(ERR_SYMBOL_PROTECTED, stuffSymbol(symbolCheck))); cnt = 0; resultIdxSave = resultStackIdx; if(isList(cell->type)) { cell->aux = (UINT)nilCell; /* undo last element optimization */ list = (CELL *)cell->contents; if(params != nilCell) { repCell = params; if(params->next != nilCell) funcCell = evaluateExpression(params->next); } else repCell = NULL; COMPARE_START: if(compareFunc(keyCell, list, funcCell) == 0) { if(repCell != NULL) { /* take out usage of sysSymbol0] in 10.2 should only be used for regex replacements then $it doesn't need to be a copy */ deleteList((CELL*)sysSymbol[0]->contents); itSymbol->contents = (UINT)copyCell(list); sysSymbol[0]->contents = itSymbol->contents; cell->contents = (UINT)copyCell(evaluateExpression(repCell)); cell = (CELL*)cell->contents; cell->next = list->next; } else /* remove mode */ cell->contents = (UINT)list->next; list->next = nilCell; deleteList(list); cnt++; if(repCell != NULL) list = cell; else /* remove mode */ { list = (CELL*)cell->contents; if(list != nilCell) goto COMPARE_START; } } while(list->next != nilCell) { if(compareFunc(keyCell, list->next, funcCell) == 0) { cell = list->next; /* cell = old elmnt */ if(repCell != NULL) { /* take out usage of sysSymbol0] in 10.2 should only be used for regex replacements */ deleteList((CELL*)sysSymbol[0]->contents); itSymbol->contents = (UINT)copyCell(cell); sysSymbol[0]->contents = itSymbol->contents; list->next = copyCell(evaluateExpression(repCell)); list = list->next; } list->next = cell->next; cell->next = nilCell; deleteList(cell); cnt++; } else list = list->next; cleanupResults(resultIdxSave); } deleteList((CELL*)sysSymbol[0]->contents); /* sysSymbol[0] should not be used here, introduce $count */ sysSymbol[0]->contents = (UINT)stuffInteger(cnt); itSymbol->contents = (UINT)nilCell; symbolCheck = refSymbol; pushResultFlag = FALSE; return(newList); } if(cell->type == CELL_STRING) { if(keyCell->type != CELL_STRING) return(errorProc(ERR_STRING_EXPECTED)); keyStr = (char *)keyCell->contents; buff = (char *)cell->contents; repCell = params; if(repCell == nilCell) return(errorProc(ERR_MISSING_ARGUMENT)); options = -1; if(repCell->next != nilCell) getInteger(repCell->next, (UINT*)&options); newBuff = replaceString(keyStr, keyCell->aux - 1, buff, (size_t)cell->aux -1, repCell, &cnt, options, &newLen); if(newBuff != NULL) { freeMemory(buff); cell->contents = (UINT)newBuff; cell->aux = newLen + 1; } deleteList((CELL*)sysSymbol[0]->contents); sysSymbol[0]->contents = (UINT)stuffInteger(cnt); symbolCheck = refSymbol; pushResultFlag = FALSE; return(cell); } return(errorProcExt(ERR_LIST_OR_STRING_EXPECTED, cell)); }
CELL * findAllString(char * pattern, char * str, size_t size, CELL * params) { long options = 0; ssize_t findPos = -1; int len; int offset = 0; CELL * result = nilCell; CELL * cell = NULL; CELL * exprCell; CELL * exprRes; UINT * resultIdxSave; int errNo; int lastPos = -1; exprCell = params; if((params = params->next) != nilCell) getInteger(params, (UINT *)&options); resultIdxSave = resultStackIdx; while( (findPos = searchBufferRegex(str, offset, pattern, (int)size, options, &len)) != -1) { if(exprCell != nilCell) { itSymbol->contents = sysSymbol[0]->contents; if((exprRes = evaluateExpressionSafe(exprCell, &errNo)) == NULL) { pushResult(result); /* push for later deletion */ longjmp(errorJump, errNo); } exprRes = copyCell(exprRes); } else exprRes = stuffStringN(str + findPos, len); if(lastPos == findPos) { ++findPos; pushResult(exprRes); goto FINDALL_CONTINUE; } lastPos = findPos; if(result == nilCell) { cell = exprRes; result = makeCell(CELL_EXPRESSION, (UINT)cell); } else { cell->next = exprRes; cell = cell->next; } FINDALL_CONTINUE: offset = (findPos + len); cleanupResults(resultIdxSave); } if(result == nilCell) return(getCell(CELL_EXPRESSION)); itSymbol->contents = (UINT)nilCell; return(result); }