static void _save_program(const char* path) { char* txt = 0; mb_assert(path); txt = _get_code(); if(!_save_file(path, txt)) { _printf("Cannot save file \"%s\".\n", path); } else { if(_code()->count == 1) { _printf("Save done. %d line saved.\n", _code()->count); } else { _printf("Save done. %d lines saved.\n", _code()->count); } } free(txt); }
static void _load_program(const char* path) { char* txt = 0; mb_assert(path); txt = _load_file(path); if(txt) { _new_program(); _set_code(txt); free(txt); if(_code()->count == 1) { _printf("Load done. %d line loaded.\n", _code()->count); } else { _printf("Load done. %d lines loaded.\n", _code()->count); } } else { _printf("Cannot load file \"%s\".\n", path); } }
u_char * ascii2unicode(u_char *dst, char *src, size_t *l) { u_char *dp = dst, *tp = &dp[*l]; char *cp = src, *ep = &src[strlen(cp) ]; while(cp < ep) { int w = 0; if(cp[0] == '&' && &cp[3] < ep) { char *dem = &cp[3]; cp++; if(*cp == '#') cp++; while(dem[0] != ';' && !isspace(dem[0]) && dem < ep && dem < &cp[11]) dem++; if(dem[0] == ';') { char *ent = nult(cp, dem); unsigned code = 0; if(isdigit(ent[0])) code = strtoul(ent, NULL, 0); else code = _code(ent); lst16((unsigned short)code, &dp, tp); w = 1; cp = dem; } } if(!w) { lst8(*cp, &dp, tp); lst8(0, &dp, tp); } cp++; } *l = (size_t)(dp - dst); return(dst); }
static void _alter_program(const char* no) { long lno = 0; long i = 0; mb_assert(no); lno = atoi(no); if(lno < 1 || lno > _code()->count) { _printf("Line number %ld out of bound.\n", lno); return; } --lno; free(_code()->lines[lno]); for(i = lno; i < _code()->count - 1; i++) _code()->lines[i] = _code()->lines[i + 1]; _code()->count--; }
static void _list_program(const char* sn, const char* cn) { long lsn = 0; long lcn = 0; char* p = 0; mb_assert(sn && cn); lsn = atoi(sn); lcn = atoi(cn); if(lsn == 0 && lcn == 0) { long i = 0; for(i = 0; i < _code()->count; ++i) { p = _code()->lines[i]; _get_unicode_bom((const char**)&p); _list_one_line(false, i + 1, p); } } else { long i = 0; long e = 0; if(lsn < 1 || lsn > _code()->count) { _printf("Line number %ld out of bound.\n", lsn); return; } if(lcn < 0) { _printf("Invalid line count %ld.\n", lcn); return; } --lsn; e = lcn ? lsn + lcn : _code()->count; for(i = lsn; i < e; ++i) { if(i >= _code()->count) break; p = _code()->lines[i]; _get_unicode_bom((const char**)&p); _list_one_line(true, i + 1, p); } } }
static void _edit_program(const char* no) { char line[_MAX_LINE_LENGTH]; long lno = 0; int l = 0; mb_assert(no); lno = atoi(no); if(lno < 1 || lno > _code()->count) { _printf("Line number %ld out of bound.\n", lno); return; } --lno; memset(line, 0, _MAX_LINE_LENGTH); _printf("%ld]", lno + 1); mb_gets(line, _MAX_LINE_LENGTH); l = (int)strlen(line); _code()->lines[lno] = (char*)realloc(_code()->lines[lno], l + 2); strcpy(_code()->lines[lno], line); _code()->lines[lno][l] = '\n'; _code()->lines[lno][l + 1] = '\0'; }
static int _do_line(void) { int result = MB_FUNC_OK; char line[_MAX_LINE_LENGTH]; char dup[_MAX_LINE_LENGTH]; mb_assert(bas); memset(line, 0, _MAX_LINE_LENGTH); _printf("]"); mb_gets(line, _MAX_LINE_LENGTH); memcpy(dup, line, _MAX_LINE_LENGTH); strtok(line, " "); if(_str_eq(line, "")) { /* Do nothing */ } else if(_str_eq(line, "HELP")) { _show_help(); } else if(_str_eq(line, "CLS")) { _clear_screen(); } else if(_str_eq(line, "NEW")) { result = _new_program(); } else if(_str_eq(line, "RUN")) { int i = 0; mb_assert(_code()); result = mb_reset(&bas, false); for(i = 0; i < _code()->count; ++i) { if(result) break; result = mb_load_string(bas, _code()->lines[i], false); } if(result == MB_FUNC_OK) result = mb_run(bas); _printf("\n"); } else if(_str_eq(line, "BYE")) { result = MB_FUNC_BYE; } else if(_str_eq(line, "LIST")) { char* sn = line + strlen(line) + 1; char* cn = 0; strtok(sn, " "); cn = sn + strlen(sn) + 1; _list_program(sn, cn); } else if(_str_eq(line, "EDIT")) { char* no = line + strlen(line) + 1; char* ne = 0; strtok(no, " "); ne = no + strlen(no) + 1; if(!(*ne)) _edit_program(no); else if(_str_eq(no, "-I")) _insert_program(ne); else if(_str_eq(no, "-R")) _alter_program(ne); } else if(_str_eq(line, "LOAD")) { char* path = line + strlen(line) + 1; _load_program(path); } else if(_str_eq(line, "SAVE")) { char* path = line + strlen(line) + 1; _save_program(path); } else if(_str_eq(line, "KILL")) { char* path = line + strlen(line) + 1; _kill_program(path); } else if(_str_eq(line, "DIR")) { char* path = line + strlen(line) + 1; _list_directory(path); } else { _append_one_line(dup); } return result; }
static void _insert_program(const char* no) { char line[_MAX_LINE_LENGTH]; long lno = 0; int l = 0; int i = 0; mb_assert(no); lno = atoi(no); if(lno < 1 || lno > _code()->count) { _printf("Line number %ld out of bound.\n", lno); return; } --lno; memset(line, 0, _MAX_LINE_LENGTH); _printf("%ld]", lno + 1); mb_gets(line, _MAX_LINE_LENGTH); if(_code()->count + 1 == _code()->size) { _code()->size += _REALLOC_INC_STEP; _code()->lines = (char**)realloc(_code()->lines, sizeof(char*) * _code()->size); } for(i = _code()->count; i > lno; i--) _code()->lines[i] = _code()->lines[i - 1]; l = (int)strlen(line); _code()->lines[lno] = (char*)realloc(0, l + 2); strcpy(_code()->lines[lno], line); _code()->lines[lno][l] = '\n'; _code()->lines[lno][l + 1] = '\0'; _code()->count++; }