static void nc_sigwinch(int s) /* SIGWINCH signal handler */ { mpdm_t v; #ifdef NCURSES_VERSION /* Make sure that window size changes... */ struct winsize ws; int fd = open("/dev/tty", O_RDWR); if (fd == -1) return; /* This should never have to happen! */ if (ioctl(fd, TIOCGWINSZ, &ws) == 0) resizeterm(ws.ws_row, ws.ws_col); close(fd); #else /* restart curses */ /* ... */ #endif /* invalidate main window */ clearok(stdscr, 1); refresh(); /* re-set dimensions */ v = mpdm_hget_s(mp, L"window"); mpdm_hset_s(v, L"tx", MPDM_I(COLS)); mpdm_hset_s(v, L"ty", MPDM_I(LINES)); /* reattach */ signal(SIGWINCH, nc_sigwinch); }
static mpdm_t kde4_drv_confirm(mpdm_t a, mpdm_t ctxt) /* confirm driver function */ { int r; /* 1# arg: prompt */ r = KMessageBox::questionYesNoCancel(0, str_to_qstring(mpdm_aget(a, 0)), i18n("mp" VERSION)); switch (r) { case KMessageBox::Yes: r = 1; break; case KMessageBox::No: r = 2; break; case KMessageBox::Cancel: r = 0; break; } return MPDM_I(r); }
/** integer = ord(str); */ static mpdm_t F_ord(F_ARGS) { int ret = 0; mpdm_t v = mpdm_get_i(a, 0); if (v != NULL) { wchar_t *ptr = mpdm_string(v); ret = (int) *ptr; } return MPDM_I(ret); }
/** * mpsl_startup - Initializes MPSL. * * Initializes the Minimum Profit Scripting Language. Returns 0 if * everything went OK. */ int mpsl_startup(void) { mpdm_t r; mpdm_t m; /* startup MPDM */ mpdm_startup(); r = mpdm_root(); /* creates INC, unless already defined */ if (mpdm_hget_s(r, L"INC") == NULL) mpdm_hset_s(r, L"INC", MPDM_A(0)); /* the TRUE value */ mpdm_hset_s(r, L"TRUE", MPDM_I(1)); /* standard file descriptors */ mpdm_hset_s(r, L"STDIN", MPDM_F(stdin)); mpdm_hset_s(r, L"STDOUT", MPDM_F(stdout)); mpdm_hset_s(r, L"STDERR", MPDM_F(stderr)); /* home and application directories */ mpdm_hset_s(r, L"HOMEDIR", mpdm_home_dir()); mpdm_hset_s(r, L"APPDIR", mpdm_app_dir()); /* fill now the MPSL hash */ m = MPDM_H(0); mpdm_hset_s(r, L"MPSL", m); /* store things there */ mpdm_hset_s(m, L"VERSION", MPDM_MBS(VERSION)); mpdm_hset_s(m, L"OPCODE", mpsl_build_opcodes()); mpdm_hset_s(m, L"LC", MPDM_H(0)); mpdm_hset_s(m, L"CORE", mpsl_build_funcs()); mpdm_dump_1 = mpsl_dump_1; return 0; }
static mpdm_t kde4_drv_form(mpdm_t a, mpdm_t ctxt) { int n; mpdm_t widget_list; QWidget *qlist[100]; mpdm_t r; KDialog *dialog = new KDialog(window); dialog->setModal(true); dialog->setButtons(KDialog::Ok | KDialog::Cancel); widget_list = mpdm_aget(a, 0); KVBox *vb = new KVBox(dialog); dialog->setMainWidget(vb); for (n = 0; n < mpdm_size(widget_list); n++) { mpdm_t w = mpdm_aget(widget_list, n); wchar_t *type; mpdm_t t; KHBox *hb = new KHBox(vb); type = mpdm_string(mpdm_hget_s(w, L"type")); if ((t = mpdm_hget_s(w, L"label")) != NULL) { QLabel *ql = new QLabel(hb); ql->setText(str_to_qstring(mpdm_gettext(t))); } t = mpdm_hget_s(w, L"value"); if (wcscmp(type, L"text") == 0) { mpdm_t h; QComboBox *ql = new QComboBox(hb); ql->setEditable(true); ql->setMinimumContentsLength(30); ql->setMaxVisibleItems(8); if (t != NULL) ql->setEditText(str_to_qstring(t)); qlist[n] = ql; if ((h = mpdm_hget_s(w, L"history")) != NULL) { int i; /* has history; fill it */ h = mp_get_history(h); for (i = mpdm_size(h) - 1; i >= 0; i--) ql->addItem(str_to_qstring(mpdm_aget(h, i))); } } else if (wcscmp(type, L"password") == 0) { QLineEdit *ql = new QLineEdit(hb); ql->setEchoMode(QLineEdit::Password); qlist[n] = ql; } else if (wcscmp(type, L"checkbox") == 0) { QCheckBox *qc = new QCheckBox(hb); if (mpdm_ival(t)) qc->setCheckState(Qt::Checked); qlist[n] = qc; } else if (wcscmp(type, L"list") == 0) { int i; QListWidget *ql = new QListWidget(hb); ql->setMinimumWidth(480); /* use a monospaced font */ ql->setFont(QFont(QString("Mono"))); mpdm_t l = mpdm_hget_s(w, L"list"); for (i = 0; i < mpdm_size(l); i++) ql->addItem(str_to_qstring(mpdm_aget(l, i))); ql->setCurrentRow(mpdm_ival(t)); qlist[n] = ql; } if (n == 0) qlist[n]->setFocus(Qt::OtherFocusReason); } n = dialog->exec(); if (!n) return NULL; r = MPDM_A(mpdm_size(widget_list)); /* fill the return values */ for (n = 0; n < mpdm_size(widget_list); n++) { mpdm_t w = mpdm_aget(widget_list, n); mpdm_t v = NULL; wchar_t *type; type = mpdm_string(mpdm_hget_s(w, L"type")); if (wcscmp(type, L"text") == 0) { mpdm_t h; QComboBox *ql = (QComboBox *) qlist[n]; v = qstring_to_str(ql->currentText()); /* if it has history, add to it */ if ((h = mpdm_hget_s(w, L"history")) != NULL && v != NULL && mpdm_cmp_s(v, L"") != 0) { h = mp_get_history(h); if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0) mpdm_push(h, v); } } else if (wcscmp(type, L"password") == 0) { QLineEdit *ql = (QLineEdit *) qlist[n]; v = qstring_to_str(ql->text()); } else if (wcscmp(type, L"checkbox") == 0) { QCheckBox *qb = (QCheckBox *) qlist[n]; v = MPDM_I(qb->checkState() == Qt::Checked); } else if (wcscmp(type, L"list") == 0) { QListWidget *ql = (QListWidget *) qlist[n]; v = MPDM_I(ql->currentRow()); } mpdm_aset(r, v, n); } return r; }
/** integer = chdir(dir); */ static mpdm_t F_chdir(F_ARGS) { return MPDM_I(mpdm_chdir(A0)); }
static mpdm_t F_bitand(F_ARGS) { return MPDM_I(IA0 & IA1); }
O_TYPE O_shl(O_ARGS) { return MPDM_I(IM1 << IM2); }
static mpdm_t F_bitshl(F_ARGS) { return MPDM_I(IA0 << IA1); }
O_TYPE O_mod(O_ARGS) { return MPDM_I(IM1 % IM2); }
O_TYPE O_bitor(O_ARGS) { return MPDM_I(IM1 | IM2); }
/** bool = rename(oldpath, newpath); */ static mpdm_t F_rename(F_ARGS) { return MPDM_I(mpdm_rename(A0, A1)); }
/** integer = chmod(filename, perms); */ static mpdm_t F_chmod(F_ARGS) { return MPDM_I(mpdm_chmod(A0, A1)); }
/* ; */ static mpdm_t F_size(F_ARGS) { return MPDM_I(mpdm_size(A0)); }
/** bool = unlink(filename); */ static mpdm_t F_unlink(F_ARGS) { return MPDM_I(mpdm_unlink(A0)); }
/** integer = ftell(fd); */ static mpdm_t F_ftell(F_ARGS) { return MPDM_I(mpdm_ftell(A0)); }
/** integer = fseek(fd, offset, whence); */ static mpdm_t F_fseek(F_ARGS) { return MPDM_I(mpdm_fseek(A0, IA1, IA2)); }
/** s = putchar(fd, s); */ static mpdm_t F_putchar(F_ARGS) { return MPDM_I(mpdm_putchar(A0, A1)); }
void test_mpsl2(void) { mpdm_t v; mpdm_t w; /* execution tests */ v = do_test_mpsl("666;"); mpdm_dump(v); v = do_test_exec(v, NULL); do_test("literal number", mpdm_ival(v) == 666); v = do_test_mpsl("\"goodbye\";"); v = do_test_exec(v, NULL); do_test("literal string", mpdm_cmp(v, MPDM_S(L"goodbye")) == 0); v = do_test_mpsl("1 + 3 + 5;"); v = do_test_exec(v, NULL); do_test("mpsl calculator 1", mpdm_rval(v) == 9.0); v = do_test_mpsl("1 + ((3 - 5) * 8);"); v = do_test_exec(v, NULL); do_test("mpsl calculator 2", mpdm_rval(v) == -15.0); /* next value cannot be tested as an exact equality, as rounding errors will manifest */ v = do_test_mpsl("1.5 + ((3.1 - 5.8) * 8.0);"); v = do_test_exec(v, NULL); do_test("mpsl calculator 3", mpdm_rval(v) < -20.0 && mpdm_rval(v) > -21.0); v = do_test_mpsl("2 + 3 * 4;"); v = do_test_exec(v, NULL); do_test("mpsl calculator 4", mpdm_rval(v) == 14.0); v = do_test_mpsl("2 * 3 + 4;"); v = do_test_exec(v, NULL); do_test("mpsl calculator 5", mpdm_rval(v) == 10.0); v = do_test_exec(do_test_mpsl("2 + 3 * 4;"), NULL); mpdm_ref(v); w = do_test_exec(do_test_mpsl("2 + (3 * 4);"), NULL); do_test("mpsl calculator 6 (operator precedence)", mpdm_rval(v) == mpdm_rval(w)); mpdm_unref(v); v = do_test_exec(do_test_mpsl("2 + 3 * 4;"), NULL); mpdm_ref(v); w = do_test_exec(do_test_mpsl("(2 + 3) * 4;"), NULL); do_test("mpsl calculator 7 (operator precedence)", mpdm_rval(v) != mpdm_rval(w)); mpdm_unref(v); v = do_test_mpsl("/* array */ [\"this\", \"one\", \"is\", 666, \"cool\"];"); v = do_test_exec(v, NULL); mpdm_dump(v); do_test("mpsl array", mpdm_ival(mpdm_get_i(v, 3)) == 666); v = do_test_mpsl ("/* hash */ { \"enero\" => \"january\", \"febrero\" => \"february\" };"); v = do_test_exec(v, NULL); mpdm_dump(v); do_test("mpsl hash", mpdm_cmp(mpdm_get(v, MPDM_S(L"febrero")), MPDM_S(L"february")) == 0); v = do_test_mpsl("! 1;"); v = do_test_exec(v, NULL); do_test("boolean not 1", !mpdm_is_true(v)); v = do_test_mpsl("! 0;"); v = do_test_exec(v, NULL); do_test("boolean not 2", v != NULL); v = do_test_mpsl("1 && 3;"); v = do_test_exec(v, NULL); do_test("boolean and 1", mpdm_ival(v) == 3); v = do_test_mpsl("1 && 0;"); v = do_test_exec(v, NULL); do_test("boolean and 2", !mpdm_is_true(v)); v = do_test_mpsl("0 && 1;"); v = do_test_exec(v, NULL); do_test("boolean and 3", !mpdm_is_true(v)); v = do_test_mpsl("1 || 3;"); v = do_test_exec(v, NULL); do_test("boolean or 1", mpdm_ival(v) == 1); v = do_test_mpsl("2 || 0;"); v = do_test_exec(v, NULL); do_test("boolean or 2", mpdm_ival(v) == 2); v = do_test_mpsl("0 || 3;"); v = do_test_exec(v, NULL); do_test("boolean or 3", mpdm_ival(v) == 3); v = do_test_mpsl("6 == 6;"); v = do_test_exec(v, NULL); do_test("numeric == 1", v != NULL); v = do_test_mpsl("8.0 == 8.0;"); v = do_test_exec(v, NULL); do_test("numeric == 2", v != NULL); v = do_test_mpsl("6 == 8;"); v = do_test_exec(v, NULL); do_test("numeric == 3", !mpdm_is_true(v)); v = do_test_mpsl("6 != 6;"); v = do_test_exec(v, NULL); do_test("numeric != 1", !mpdm_is_true(v)); v = do_test_mpsl("8.0 != 8.0;"); v = do_test_exec(v, NULL); do_test("numeric != 2", !mpdm_is_true(v)); v = do_test_mpsl("6 != 8;"); v = do_test_exec(v, NULL); do_test("numeric != 3", v != NULL); v = do_test_mpsl("6 < 6;"); v = do_test_exec(v, NULL); do_test("numeric < 1", !mpdm_is_true(v)); v = do_test_mpsl("8 < 6;"); v = do_test_exec(v, NULL); do_test("numeric < 2", !mpdm_is_true(v)); v = do_test_mpsl("5 < 6;"); v = do_test_exec(v, NULL); do_test("numeric < 3", v != NULL); v = do_test_mpsl("6 > 6;"); v = do_test_exec(v, NULL); do_test("numeric > 1", !mpdm_is_true(v)); v = do_test_mpsl("8 > 6;"); v = do_test_exec(v, NULL); do_test("numeric > 2", v != NULL); v = do_test_mpsl("5 > 6;"); v = do_test_exec(v, NULL); do_test("numeric > 3", !mpdm_is_true(v)); v = do_test_mpsl("6 <= 6;"); v = do_test_exec(v, NULL); do_test("numeric <= 1", v != NULL); v = do_test_mpsl("8 <= 6;"); v = do_test_exec(v, NULL); do_test("numeric <= 2", !mpdm_is_true(v)); v = do_test_mpsl("5 <= 6;"); v = do_test_exec(v, NULL); do_test("numeric <= 3", v != NULL); v = do_test_mpsl("6 >= 6;"); v = do_test_exec(v, NULL); do_test("numeric >= 1", v != NULL); v = do_test_mpsl("8 >= 6;"); v = do_test_exec(v, NULL); do_test("numeric >= 2", v != NULL); v = do_test_mpsl("5 >= 6;"); v = do_test_exec(v, NULL); do_test("numeric >= 3", !mpdm_is_true(v)); v = do_test_mpsl("11 % 6;"); v = do_test_exec(v, NULL); do_test("modulo", mpdm_ival(v) == 5); v = do_test_mpsl("variable=16384;"); mpdm_dump(v); v = do_test_exec(v, NULL); do_test("assign 1", mpdm_ival(v) == 16384); v = do_test_mpsl("array=[10, 20, 30, 40];"); v = do_test_exec(v, NULL); do_test("assign 2", mpdm_ival(mpdm_get_i(v, 2)) == 30); v = do_test_mpsl("a=1; b=2; c=3;"); mpdm_dump(v); v = do_test_exec(v, NULL); v = do_test_mpsl("CACHE={}; CACHE.regex=[]; CACHE.regex[0]=12345;"); v = do_test_exec(v, NULL); v = do_test_mpsl("variable;"); v = do_test_exec(v, NULL); do_test("symval 1", mpdm_ival(v) == 16384); v = do_test_mpsl("variable2=1 + ((3 - 5) * 8); variable2;"); mpdm_dump(v); v = do_test_exec(v, NULL); do_test("symval 2", mpdm_rval(v) == -15); v = do_test_mpsl("variable3=variable2 * 2;"); v = do_test_exec(v, NULL); do_test("symval 3", mpdm_ival(v) == -30); v = do_test_mpsl("sub mysum(a, b) { a + b; }"); mpdm_dump(v); v = do_test_exec(v, NULL); do_test("sub 1", v != NULL); v = do_test_mpsl("sub pi() { 3.1416; }"); mpdm_dump(v); v = do_test_exec(v, NULL); do_test("sub 2", v != NULL); v = do_test_mpsl("var10=pi();"); v = do_test_exec(v, NULL); do_test("exec 1", mpdm_rval(v) == 3.1416); v = do_test_mpsl("var11=pi() * 10000; var11;"); v = do_test_exec(v, NULL); do_test("exec 2", mpdm_rval(v) == 31416); v = do_test_mpsl("mysum(100, 20);"); v = do_test_exec(v, NULL); do_test("exec 3", mpdm_rval(v) == 120.0); v = do_test_mpsl("a = NULL;"); v = do_test_exec(v, NULL); do_test("NULL 1", v == NULL); v = do_test_mpsl("a == NULL;"); v = do_test_exec(v, NULL); do_test("NULL 2", mpdm_ival(v) == 1); v = do_test_mpsl("local a, b; a = 1; b = 2;"); v = do_test_exec(v, NULL); v = do_test_mpsl("a == NULL;"); v = do_test_exec(v, NULL); do_test("local 1", mpdm_ival(v) == 1); v = do_test_mpsl("66 * -1;"); v = do_test_exec(v, NULL); do_test("uminus", mpdm_ival(v) == -66); v = do_test_mpsl("\"test\" eq \"test\";"); v = do_test_exec(v, NULL); do_test("streq 1", mpdm_is_true(v)); v = do_test_mpsl("\"test\" eq \"prueba\";"); v = do_test_exec(v, NULL); do_test("streq 1", !mpdm_is_true(v)); v = do_test_mpsl("a = 6; ++ a;"); v = do_test_exec(v, NULL); do_test("pinc", mpdm_ival(v) == 7); v = do_test_mpsl("a++;"); v = do_test_exec(v, NULL); do_test("sinc", mpdm_ival(v) == 7); v = do_test_mpsl("a += 10;"); v = do_test_exec(v, NULL); do_test("iadd", mpdm_ival(v) == 18); v = do_test_mpsl("local a, b, c; a=1; b=2; c=3; if(a == b) c=1000; c;"); v = do_test_exec(v, NULL); do_test("if 1", mpdm_ival(v) == 3); v = do_test_mpsl("local a, b, c; a=1; b=2; c=3; if(a <= b) c=1000; c;"); v = do_test_exec(v, NULL); do_test("if 2", mpdm_ival(v) == 1000); v = do_test_mpsl("local a, b, c; a=1; b=2; if(a == b) c=1000; else c=2000; c;"); v = do_test_exec(v, NULL); do_test("ifelse", mpdm_ival(v) == 2000); v = do_test_mpsl("local a; a = 0; while(a < 100) { a++; } a;"); v = do_test_exec(v, NULL); do_test("ifelse", mpdm_ival(v) == 100); v = do_test_mpsl("a=mysum(100, 50); a;"); v = do_test_exec(v, NULL); do_test("mysum 1", mpdm_ival(v) == 150); v = do_test_mpsl("a=mysum(2000, 500); a;"); v = do_test_exec(v, NULL); do_test("mysum 2", mpdm_ival(v) == 2500); w = mpdm_ref(MPDM_A(2)); mpdm_set_i(w, MPDM_I(100), 0); mpdm_set_i(w, MPDM_I(50), 1); /* asks for the value of the mysum symbol (the code) */ v = do_test_mpsl("mysum;"); /* executes, so mysum() itself is being returned */ v = do_test_exec(v, NULL); mpdm_dump(v); do_test("mysum 3", mpdm_ival(do_test_exec(v, w)) == 150); mpdm_set_i(w, MPDM_I(75), 1); do_test("mysum 4", mpdm_ival(do_test_exec(v, w)) == 175); /* compiles (and executes) the definition of gcd() */ v = do_test_mpsl ("/* greatest common divisor (Euclid's algorithm) */ sub gcd(m, n) { while (m > 0) { if(n > m) { local t = m; m = n; n = t; } m -= n; } n; }"); do_test_exec(v, NULL); /* gets a pointer to gcd() */ v = do_test_exec(do_test_mpsl("gcd;"), NULL); mpdm_dump(v); /* executes gcd(100, 50); */ mpdm_set_i(w, MPDM_I(50), 1); do_test("gcd() 1", mpdm_ival(do_test_exec(v, w)) == 50); /* executes gcd(100, 75); */ mpdm_set_i(w, MPDM_I(75), 1); do_test("gcd() 2", mpdm_ival(do_test_exec(v, w)) == 25); mpdm_unref(w); /* string concatenation */ w = mpdm_ref(MPDM_S(L"big lebowski")); v = do_test_mpsl("\"big\" ~ \" lebowski\";"); do_test("~ (strcat 1)", mpdm_cmp(do_test_exec(v, NULL), w) == 0); v = do_test_mpsl("\"big\" ~ \" \" ~ \"lebowski\";"); do_test("~ (strcat 2)", mpdm_cmp(do_test_exec(v, NULL), w) == 0); mpdm_unref(w); }
/** integer = chown(filename, uid, gid); */ static mpdm_t F_chown(F_ARGS) { return MPDM_I(mpdm_chown(A0, A1, A2)); }
static mpdm_t F_count(F_ARGS) { return MPDM_I(mpdm_count(A0)); }
static mpdm_t F_integer(F_ARGS) { return MPDM_I(mpdm_ival(A0)); }
O_TYPE O_bitand(O_ARGS) { return MPDM_I(IM1 & IM2); }
/** pclose(fd); */ static mpdm_t F_pclose(F_ARGS) { return MPDM_I(mpdm_close(A0)); }
O_TYPE O_bitxor(O_ARGS) { return MPDM_I(IM1 ^ IM2); }
/** bool = gettext_domain(dom, data); */ static mpdm_t F_gettext_domain(F_ARGS) { return MPDM_I(mpdm_gettext_domain(A0, A1)); }
/** integer = encoding(charset); */ static mpdm_t F_encoding(F_ARGS) { return MPDM_I(mpdm_encoding(A0)); }
static mpdm_t F_bitxor(F_ARGS) { return MPDM_I(IA0 ^ IA1); }