int urldecode(const char *src, size_t srclen, char *dst, size_t dstlen) { size_t bytes = 0; while (srclen > 0 && dstlen > 0) { if (*src == '%') { if (srclen < 3) break; *dst++ = (htoc(*(src + 1)) << 4) | htoc(*(src + 2)); src += 3, dstlen++, srclen -= 3, bytes++; } else { *dst++ = *src++, dstlen--, srclen--, bytes++; } } if (srclen) return -ENOSPC; return bytes; }
static void evaluate_property(const char* data, const char* value) { if (!strcmp(data, COLOR_LED_PROP_NAME)) { color c; if (!htoc(value, &c)) { led_color = c; fprintf(stderr, "%s: Led color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_BKGROUND_PROP_NAME)) { color c; if (!htoc(value, &c)) { background_color = c; fprintf(stderr, "%s: Background color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_TITLE_PROP_NAME)) { color c; if (!htoc(value, &c)) { title_color = c; fprintf(stderr, "%s: Title color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_MENU_PROP_NAME)) { color c; if (!htoc(value, &c)) { menu_color = c; fprintf(stderr, "%s: Menu color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_MENU_SEL_PROP_NAME)) { color c; if (!htoc(value, &c)) { menu_sel_color = c; fprintf(stderr, "%s: Selection color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_SCRIPT_PROP_NAME)) { color c; if (!htoc(value, &c)) { script_color = c; fprintf(stderr, "%s: Script color set to:\n" "r = %d, g = %d, b = %d, a = %d\n", __func__, c.r, c.g, c.b, c.a); } } else if (!strcmp(data, COLOR_CONSOLE_HEADER_PROP_NAME)) { color c; if (!htoc(value, &c)) { console_header_color = c; fprintf(stderr, "%s: Console header color set to:\n" "r = %d, g = %d, b = %d\n", __func__, c.r, c.g, c.b); } } else if (!strcmp(data, COLOR_CONSOLE_BACKGROUND_PROP_NAME)) { color c; if (!htoc(value, &c)) { console_background_color = c; fprintf(stderr, "%s: Console background color set to:\n" "r = %d, g = %d, b = %d\n", __func__, c.r, c.g, c.b); } } else if (!strcmp(data, COLOR_CONSOLE_FRONT_PROP_NAME)) { color c; if (!htoc(value, &c)) { console_front_color = c; fprintf(stderr, "%s: Console front color set to:\n" "r = %d, g = %d, b = %d\n", __func__, c.r, c.g, c.b); } } else if (!strncmp(data, COLOR_CONSOLE_TERMCLR_PROP_NAME_BASE, strlen(COLOR_CONSOLE_TERMCLR_PROP_NAME_BASE))) { color c; if (!htoc(value, &c)) { const char* termclr_ascii = data + strlen(COLOR_CONSOLE_TERMCLR_PROP_NAME_BASE); int val = atoi(termclr_ascii); if (val >= 30 && val <= 37) { console_term_colors[val - 30] = c; fprintf(stderr, "%s: Console terminal front color no. %d set to:\n" "r = %d, g = %d, b = %d\n", __func__, val, c.r, c.g, c.b); } else if (val >= 90 && val <= 97) { console_term_colors[val - 90 + 8] = c; fprintf(stderr, "%s: Console terminal bright front color no. %d set to:\n" "r = %d, g = %d, b = %d\n", __func__, val, c.r, c.g, c.b); } } } else if (!strncmp(data, UI_INDETERMINATE_FRAMES_PROP_NAME, strlen(UI_INDETERMINATE_FRAMES_PROP_NAME))) { const char* val_str = data + strlen(UI_INDETERMINATE_FRAMES_PROP_NAME) + 1; ui_parameters.indeterminate_frames = atoi(val_str); fprintf(stderr, "%s: ui_parameters.indeterminate_frames = %d\n", __func__, ui_parameters.indeterminate_frames); } else if (!strncmp(data, UI_INSTALL_FRAMES_PROP_NAME, strlen(UI_INSTALL_FRAMES_PROP_NAME))) { const char* val_str = data + strlen(UI_INSTALL_FRAMES_PROP_NAME) + 1; ui_parameters.installing_frames = atoi(val_str); fprintf(stderr, "%s: ui_parameters.installing_frames = %d\n", __func__, ui_parameters.installing_frames); } else if (!strncmp(data, UI_INSTALL_LOC_X_PROP_NAME, strlen(UI_INSTALL_LOC_X_PROP_NAME))) { const char* val_str = data + strlen(UI_INSTALL_LOC_X_PROP_NAME) + 1; ui_parameters.install_overlay_offset_x = atoi(val_str); fprintf(stderr, "%s: ui_parameters.install_overlay_offset_x = %d\n", __func__, ui_parameters.install_overlay_offset_x); } else if (!strncmp(data, UI_INSTALL_LOC_Y_PROP_NAME, strlen(UI_INSTALL_LOC_Y_PROP_NAME))) { const char* val_str = data + strlen(UI_INSTALL_LOC_Y_PROP_NAME) + 1; ui_parameters.install_overlay_offset_y = atoi(val_str); fprintf(stderr, "%s: ui_parameters.install_overlay_offset_y = %d\n", __func__, ui_parameters.install_overlay_offset_y); } }
// parsing d'une chaîne de caractères int Parser::getstring(Memory* m,char separator) { int c,n,i; char* name=token; name++; Prodbuffer* output=m->util->interpreter->output; output->reinit(); n=0; while(1) { c=*(name++); if (c=='\\') { c=(*(name++))&255; if (c<32) { while(((*name)&255)<32) name++; } else { if (c=='n') c=10; else if (c=='z') c=0; else if (c=='$') { i=0; c=*name; if (ishex(c)) { name++; i=htoc(c); c=*name; if (ishex(c)) { name++; i=(i<<4)+htoc(c); } } c=i; } else if ((c>='0')&&(c<='9')) { i=c-'0'; c=*name; if ((c>='0')&&(c<='9')) { name++; i=(i*10)+c-'0'; c=*name; if ((c>='0')&&(c<='9')) { name++; i=(i*10)+c-'0'; } } c=i; } output->addchar(c); } } else if (c==separator) { return STRPUSHBINARY(m,output->getstart(),output->getsize()); } else output->addchar(c); } }
int main (int argc, char **argv) { FILE *f; int i; int done=0; int maxy=0; int maxx=0; srand (time (0)); if (argc < 2) { printf ("USAGE: rube [-d] [-q] [-i] [-y delay] [-f skip] foo.rub\n"); exit (0); } for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d")) { debug = 0; } if (!strcmp(argv[i], "-q")) { quiet = 1; debug = 0; } if (!strcmp(argv[i], "-i")) { interactive = 1; debug = 1; } if (!strcmp(argv[i], "-y")) { deldur = atoi(argv[i + 1]); } if (!strcmp(argv[i], "-f")) { debskip = atoi(argv[i + 1]); } } if (!quiet) printf ("Cat's Eye Technologies' RUBE Interpreter v1.5\n"); f = fopen (argv[argc - 1], "r"); if (f == NULL) { printf ("Error : couldn't open '%s' for input.\n", argv[argc - 1]); exit (0); } while (!feof (f)) { int cur = fgetc(f); pg[y * LINEWIDTH + x].c = cur; if (cur == '\n') { pg[y * LINEWIDTH + x].c = ' '; x = 0; y++; if (y >= PAGEHEIGHT) break; } else { x++; if (x > maxx) maxx = x; if (x >= LINEWIDTH) { x = 0; y++; if (y >= PAGEHEIGHT) break; } } } fclose (f); maxy = y; #if MSDOS _setcursortype(_NOCURSOR); #endif if (debug) { printf ("%c[1;1H%c[2J", 27, 27); } while (!done) /*** Intepreting Phase */ { if ((debug) && (!(frame++ % debskip) || (!frame))) { printf ("%c[1;1H", 27); for(y = 0; (y <= maxy) && (y <= SCREENHEIGHT); y++) { for(x = 0; (x <= maxx) && (x <= SCREENWIDTH); x++) { int cur = pg[y * LINEWIDTH + x].c; putc(isprint(cur) ? cur : ' ', stdout); } printf("\n"); } } else { /* putc('.', stdout); */ } fflush (stdout); fflush (stdin); for (x=0; x<=(maxx); x++) { for (y=0; y<=(maxy); y++) { int cur = pg[y * LINEWIDTH + x].c; if (cur <= 32) { if (iscrate(curd(0,-1))) nex = curd(0,-1); /* falling in from above */ if (curd(0,-1) == '(') nex = '('; if (curd(0,-1) == ')') nex = ')'; if (curd(1,1) == 'W') nex = curd(2,2); if (curd(-1,1) == 'W') nex = curd(-2,2); if ((curd(1,1) == 'V') && iscrate(curd(2,1))) nex = curd(2,1); if ((curd(-1,1) == 'V') && iscrate(curd(-2,1))) nex = curd(-2,1); if (curd(1,-1) == 'M') nex = curd(2,-2); if (curd(-1,-1) == 'M') nex = curd(-2,-2); if ((curd(1,-1) == 'A') && iscrate(curd(2,-1))) nex = curd(2,-1); if ((curd(-1,-1) == 'A') && iscrate(curd(-2,-1))) nex = curd(-2,-1); if (curd(0,-1) == '~') nex = '~'; if ((curd(-1,0) == '~') && (issupport(curd(-1,1)))) nex = '~'; if ((curd(1,0) == '~') && (issupport(curd(1,1)))) nex = '~'; if (curd(1,-1) == '+') { if (iscrate(curd(1,0)) && iscrate(curd(2,0))) { nex = htoc((ctoh(curd(1,0))+ctoh(curd(2,0))) % 16); } } if (curd(-1,-1) == '+') { if (iscrate(curd(-1,0)) && iscrate(curd(-2,0))) { nex = htoc((ctoh(curd(-1,0))+ctoh(curd(-2,0))) % 16); } } if (curd(1,-1) == '-') { if (iscrate(curd(1,0)) && iscrate(curd(2,0))) { int z; z = ctoh(curd(2,0)) - ctoh(curd(1,0)); while (z < 0) z += 16; nex = htoc(z); } } if (curd(-1,-1) == '-') { if (iscrate(curd(-1,0)) && iscrate(curd(-2,0))) { int z; z = ctoh(curd(-2,0)) - ctoh(curd(-1,0)); while (z < 0) z += 16; nex = htoc(z); } } if ((curd(1,-1) == 'K') && (iscrate(curd(1,-2)))) { if(ctoh(curd(1,-2)) < ctoh(curd(1,0))) nex = curd(1,-2); } if ((curd(-1,-1) == 'K') && (iscrate(curd(-1,-2)))) { if(ctoh(curd(-1,-2)) >= ctoh(curd(-1,0))) nex = curd(-1,-2); } if ((iscrate(curd(-1,0))) && (curd(-1,1) == '>')) nex = curd(-1,0); if ((iscrate(curd(1,0))) && (curd(1,1) == '<')) nex = curd(1,0); if (curd(0,-1) == ':') nex = curd(0,-2); if ((curd(0,-1) == ';') && (iscrate(curd(0,-2)))) nex = curd(0,-2); if ((curd(0,1) == '.') && (iscrate(curd(0,2)))) nex = curd(0,2); if ((curd(-1,0) == '(') && (curd(1,0) == ')')) /* collision */ { nex = ' '; } else { if ((curd(-1,0) == '(') && (issupport(curd(-1,1)))) nex = '('; if ((curd(1,0) == ')') && (issupport(curd(1,1)))) nex = ')'; if ((curd(0,1) == '/') || (curd(0,1) == '\\')) { if ((curd(-1,1) == '(') && (issupport(curd(-1,2)))) nex = '('; if ((curd(1,1) == ')') && (issupport(curd(1,2)))) nex = ')'; } } if (iscrate(curd(-1,0))) { /* shift crates */ int bx=-1; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { if (curd(bx-1,0) == '(') { nex = curd(-1,0); } bx--; } } if (iscrate(curd(1,0))) { int bx=1; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { if (curd(bx+1,0) == ')') { nex = curd(1,0); } bx++; } } } else switch (cur) { case '(': if (((curd(1,0) == '(') || (curd(1,0) <= ' ') || (curd(0,1) <= ' ') || (curd(0,1) == '('))) nex = ' '; if (isramp(curd(0,1))) nex = ' '; if (isramp(curd(1,0))) nex = ' '; if (isramp(curd(-1,0))) nex = ' '; if ((isblock(curd(1,0))) || (curd(1,-1) == ',') || (curd(1,0) == '*')) nex = ')'; if (iscrate(curd(1,0))) { int bx=1; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { if (isblock(curd(bx+1,0))) { nex = ')'; } bx++; } } break; case ')': if (((curd(-1,0) == ')') || (curd(-1,0) <= ' ') || (curd(0,1) <= ' ') || (curd(0,1) == ')'))) nex = ' '; if (isramp(curd(0,1))) nex = ' '; if (isramp(curd(1,0))) nex = ' '; if (isramp(curd(-1,0))) nex = ' '; if ((isblock(curd(-1,0))) || (curd(-1,-1) == ',') || (curd(-1,0) == '*')) nex = '('; if (iscrate(curd(-1,0))) { int bx=-1; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { if (isblock(curd(bx-1,0))) { nex = '('; } bx--; } } break; case 'O': if ((iscrate(curd(0,-1))) && (iscrate(curd(0,-2)))) { int d; d = ctoh(curd(0,-1)) + ctoh(curd(0,-2)) * 16; if (curd(0, 1) == 'b') { if (debug) { char s[80]; printf ("%c[%d;%dH", 27, 25, debopos); sprintf(s, "%d ", (int)d); debopos += strlen(s); if (debopos > SCREENWIDTH) { debopos = 1; printf ("%c[%d;%dH%c[K", 27, 25, 1, 27); debopos += strlen(s); } printf("%s", s); } else { printf("%d ", (int)d); } } if (curd(0, 1) == 'c') { if (debug) { printf ("%c[%d;%dH", 27, 25, debopos++); if (debopos > SCREENWIDTH) { debopos = 1; printf ("%c[%d;%dH%c[K", 27, 25, 1, 27); debopos++; } printf ("%c", (char)d); } else { putc((char)d, stdout); } } } case 'A': if (iscrate(curd(-1,0)) || iscrate(curd(1,0))) nex = 'V'; else nex = cur; break; case 'V': if (iscrate(curd(-1,0)) || iscrate(curd(1,0))) nex = 'A'; else nex = cur; break; default: nex = cur; } if (iscrate(cur)) { if (issupport(curd(0,1))) nex = cur; else nex = ' '; if ((curd(1,0) <= ' ') && (curd(0,1) == '>')) nex = ' '; if ((curd(-1,0) <= ' ') && (curd(0,1) == '<')) nex = ' '; if ((curd(1,-1) == 'W') && (curd(2,-2) == cur)) nex = ' '; if ((curd(-1,-1) == 'W') && (curd(2,-2) == cur)) nex = ' '; if ((curd(1,1) == 'M') && (curd(2,2) == cur)) nex = ' '; if ((curd(-1,1) == 'M') && (curd(-2,2) == cur)) nex = ' '; if (curd(1,0) == 'V') nex = ' '; if (curd(-1,0) == 'V') nex = ' '; if (curd(1,0) == 'A') nex = ' '; if (curd(-1,0) == 'A') nex = ' '; if (iscrate(curd(-1,0)) && ((curd(-1,-1) == '+') || (curd(-1,-1) == '-'))) nex = ' '; if (iscrate(curd(1,0)) && ((curd(1,-1) == '+') || (curd(1,-1) == '-'))) nex = ' '; if ((iscrate(curd(-1,0)) || iscrate(curd(1,0))) && ((curd(0,-1) == '+') || (curd(0,-1) == '-'))) nex = ' '; } } } /* fix nex array */ for (x=0; x<=(maxx); x++) { for (y=0; y<=(maxy); y++) { int cur = pg[y * LINEWIDTH + x].c; switch (cur) { case '*': if (curd(-1,0) == ')') nex = ' '; if (curd(1,0) == '(') nex = ' '; break; case 'O': if ((iscrate(curd(0,-1))) && (iscrate(curd(0,-2)))) { nexd(0,-1)=' '; nexd(0,-2)=' '; } break; } if (iscrate(cur)) { if (curd(1,0) == ')') { int bx=0; int flag=0; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { if (curd(bx-1,0) <= ' ') { flag = 1; } bx--; } if (flag) { bx=0; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { nexd(bx-1,0) = curd(bx,0); bx--; } nex = ')'; nexd(1,0) = ' '; } } if (curd(-1,0) == '(') { int bx=0; int flag=0; while (iscrate(curd(bx,0)) && (issupport(curd(bx,1)))) { if (curd(bx+1,0) <= ' ') { flag=1; } bx++; } if (flag) { bx=0; while ((iscrate(curd(bx,0))) && (issupport(curd(bx,1)))) { nexd(bx+1,0) = curd(bx,0); bx++; } nex = '('; nexd(-1,0)= ' '; } } if ((curd(-1,0)=='C') || (curd(1,0)=='C') || (curd(0,-1)=='C') || (curd(0,1)=='C')) nex = ' '; } if ((curd(-1,0)=='F') || (curd(1,0)=='F') || (curd(0,-1)=='F') || (curd(0,1)=='F')) nex = ' '; } } if (interactive) { char s[80]; fgets(s, 79, stdin); if (s[0] == 'q') done = 1; } else { if (deldur > 0) { rube_delay (deldur); } #ifdef MSDOS if (kbhit()) { char c; c = getch(); if (c == 27) done = 1; } #endif } memcpy(pg, pg2, LINEWIDTH * PAGEHEIGHT * sizeof(cell)); } if (debug) printf ("%c[22;1H", 27); #if MSDOS _setcursortype(_NORMALCURSOR); #endif exit (0); }