Subversion Repositories Krakow BASIC

Rev

Rev 39 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 39 Rev 40
Line 1... Line 1...
1
/* $Id: basic.c 39 2024-09-06 12:56:29Z nishi $ */
1
/* $Id: basic.c 40 2024-09-06 14:17:50Z nishi $ */
2
 
2
 
3
/* Krakow BASIC - Multi-platform simple BASIC */
3
/* Krakow BASIC - Multi-platform simple BASIC */
4
 
4
 
5
#if defined(PLATFORM_X11)
5
#if defined(PLATFORM_X11)
6
#define PLATFORM_UNIX
6
#define PLATFORM_UNIX
Line 217... Line 217...
217
#if defined(PLATFORM_X11)
217
#if defined(PLATFORM_X11)
218
#include <X11/Xlib.h>
218
#include <X11/Xlib.h>
219
#include <pthread.h>
219
#include <pthread.h>
220
 
220
 
221
void* x11_thread(void* arg);
221
void* x11_thread(void* arg);
-
 
222
char x11_putchar(char c);
-
 
223
void x11_init(void);
222
#else
224
#else
223
#include <termios.h>
225
#include <termios.h>
224
#include <sys/ioctl.h>
226
#include <sys/ioctl.h>
225
#endif
227
#endif
226
#elif defined(PLATFORM_PET)
228
#elif defined(PLATFORM_PET)
Line 265... Line 267...
265
 
267
 
266
	UCSR0C |= _BV(UCSZ00) | _BV(UCSZ01);
268
	UCSR0C |= _BV(UCSZ00) | _BV(UCSZ01);
267
}
269
}
268
#endif
270
#endif
269
 
271
 
-
 
272
#if defined(PLATFORM_X11)
-
 
273
#undef putchar
-
 
274
#define putchar(x) x11_putchar(x)
-
 
275
#endif
-
 
276
 
270
#define agetch() oggetch(0)
277
#define agetch() oggetch(0)
271
char oggetch(char wait) {
278
char oggetch(char wait)
-
 
279
#if defined(PLATFORM_X11)
-
 
280
;
-
 
281
#else
-
 
282
{
272
	int c;
283
	int c;
273
#if defined(PLATFORM_WINDOWS)
284
#if defined(PLATFORM_WINDOWS)
274
rescan:
285
rescan:
275
	if(!wait){
286
	if(!wait){
276
		if(!_kbhit()) return 0;
287
		if(!_kbhit()) return 0;
277
	}
288
	}
278
	c = _getch();
289
	c = _getch();
279
	if(c == '\r') return '\n';
290
	if(c == '\r') return '\n';
280
	if(c == '\n') goto rescan;
291
	if(c == '\n') goto rescan;
281
#elif defined(PLATFORM_UNIX)
292
#elif defined(PLATFORM_UNIX)
282
#if defined(PLATFORM_X11)
-
 
283
#else
-
 
284
	if(!wait){
293
	if(!wait){
285
		int b;
294
		int b;
286
		ioctl(0, FIONREAD, &b);
295
		ioctl(0, FIONREAD, &b);
287
		if(b == 0) return 0;
296
		if(b == 0) return 0;
288
	}
297
	}
289
	c = getchar();
298
	c = getchar();
290
	if(c == EOF) return -1;
299
	if(c == EOF) return -1;
291
	if(c == '\r') return '\n';
300
	if(c == '\r') return '\n';
292
#endif
-
 
293
#elif defined(PLATFORM_ARDUINO)
301
#elif defined(PLATFORM_ARDUINO)
294
rescan:
302
rescan:
295
	if(!wait) {
303
	if(!wait) {
296
		if(!(UCSR0A & _BV(RXC0))) return 0;
304
		if(!(UCSR0A & _BV(RXC0))) return 0;
297
	} else {
305
	} else {
Line 329... Line 337...
329
	if(c == '\r') return '\n';
337
	if(c == '\r') return '\n';
330
	if(c == 3) return 1;
338
	if(c == 3) return 1;
331
#endif
339
#endif
332
	return c;
340
	return c;
333
}
341
}
-
 
342
#endif
334
 
343
 
335
bool strcaseequ(const char* a, const char* b) { return strcasecmp(a, b) == 0; }
344
bool strcaseequ(const char* a, const char* b) { return strcasecmp(a, b) == 0; }
336
 
345
 
337
#if defined(PLATFORM_ARDUINO)
346
#if defined(PLATFORM_ARDUINO)
338
void putstr(const char* n) {
347
void putstr(const char* n) {
Line 352... Line 361...
352
		if(n == 0) break;
361
		if(n == 0) break;
353
	}
362
	}
354
	putstr(buf + incr + 1);
363
	putstr(buf + incr + 1);
355
}
364
}
356
#else
365
#else
357
void putnum(int n) {
366
void putnum(int n)
-
 
367
#if defined(PLATFORM_X11)
-
 
368
;
-
 
369
#else
-
 
370
{
358
	printf("%d", n);
371
	printf("%d", n);
359
	fflush(stdout);
372
	fflush(stdout);
360
}
373
}
-
 
374
#endif
361
 
375
 
362
void putstr(const char* n) {
376
void putstr(const char* n)
-
 
377
#if defined(PLATFORM_X11)
-
 
378
;
-
 
379
#else
-
 
380
{
363
	printf("%s", n);
381
	printf("%s", n);
364
	fflush(stdout);
382
	fflush(stdout);
365
}
383
}
366
#endif
384
#endif
367
 
385
 
-
 
386
#endif
-
 
387
 
368
void change_color(int a) {
388
void change_color(int a)
-
 
389
#if defined(PLATFORM_X11)
-
 
390
;
-
 
391
#else
-
 
392
{
369
#if defined(PLATFORM_ARDUINO) || defined(PLATFORM_UNIX)
393
#if defined(PLATFORM_ARDUINO) || defined(PLATFORM_UNIX)
370
	int fg = (a >> 4) & 0xf;
394
	int fg = (a >> 4) & 0xf;
371
	int bg = (a & 0xf);
395
	int bg = (a & 0xf);
372
	char color[2];
396
	char color[2];
373
	if(!(0 <= fg && fg <= 15)) return;
397
	if(!(0 <= fg && fg <= 15)) return;
Line 397... Line 421...
397
	int bg = (a & 0xf);
421
	int bg = (a & 0xf);
398
	bgcolor(bg);
422
	bgcolor(bg);
399
	textcolor(fg);
423
	textcolor(fg);
400
#endif
424
#endif
401
}
425
}
-
 
426
#endif
402
 
427
 
403
void clear(void) {
428
void clear(void)
-
 
429
#if defined(PLATFORM_X11)
-
 
430
;
-
 
431
#else
-
 
432
{
404
#if defined(PLATFORM_WINDOWS)
433
#if defined(PLATFORM_WINDOWS)
405
	system("cls");
434
	system("cls");
406
#elif defined(PLATFORM_UNIX) || defined(PLATFORM_ARDUINO)
435
#elif defined(PLATFORM_UNIX) || defined(PLATFORM_ARDUINO)
407
	putstr("\x1b[0m\x1b[2J\x1b[1;1H");
436
	putstr("\x1b[0m\x1b[2J\x1b[1;1H");
408
#elif defined(PLATFORM_C64) || defined(PLATFORM_A800XL) || defined(PLATFORM_APPLE2) || defined(PLATFORM_PET)
437
#elif defined(PLATFORM_C64) || defined(PLATFORM_A800XL) || defined(PLATFORM_APPLE2) || defined(PLATFORM_PET)
409
	clrscr();
438
	clrscr();
410
#endif
439
#endif
411
}
440
}
-
 
441
#endif
412
 
442
 
413
void basic(void);
443
void basic(void);
414
 
444
 
415
int main() {
445
int main() {
416
#if defined(PLATFORM_PET)
446
#if defined(PLATFORM_PET)
Line 426... Line 456...
426
	mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
456
	mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
427
	SetConsoleMode(winstdout, mode);
457
	SetConsoleMode(winstdout, mode);
428
#elif defined(PLATFORM_UNIX)
458
#elif defined(PLATFORM_UNIX)
429
#if defined(PLATFORM_X11)
459
#if defined(PLATFORM_X11)
430
	pthread_t thread;
460
	pthread_t thread;
-
 
461
	x11_init();
431
	pthread_create(&thread, NULL, x11_thread, NULL);
462
	pthread_create(&thread, NULL, x11_thread, NULL);
432
#else
463
#else
433
	struct termios old, new;
464
	struct termios old, new;
434
	tcgetattr(0, &old);
465
	tcgetattr(0, &old);
435
	new = old;
466
	new = old;
Line 1106... Line 1137...
1106
			}
1137
			}
1107
		} else if(c == -1) {
1138
		} else if(c == -1) {
1108
			break;
1139
			break;
1109
		} else if(c != 0) {
1140
		} else if(c != 0) {
1110
			putchar(c);
1141
			putchar(c);
-
 
1142
			if(lineind <= LINE_BUFFER_SIZE){
1111
			linebuf[lineind++] = c;
1143
				linebuf[lineind++] = c;
-
 
1144
			}
1112
		}
1145
		}
1113
		cursor();
1146
		cursor();
1114
	}
1147
	}
1115
}
1148
}