diff os9/mc09/mclibos9.c @ 101:15569aa4098e

micro c continue
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 27 Dec 2018 15:40:50 +0900
parents 990add11e9f8
children 09148bb27659
line wrap: on
line diff
--- a/os9/mc09/mclibos9.c	Thu Dec 27 11:49:54 2018 +0900
+++ b/os9/mc09/mclibos9.c	Thu Dec 27 15:40:50 2018 +0900
@@ -1,5 +1,24 @@
-#define	FILE	char
-#define	FCBSIZE	320
+/*
+ *  os9  driver
+ */
+
+#asm
+         ifp1
+         use   defsfile
+         endc
+#endasm
+
+typedef	struct {
+    int fd;                   /*  0 */
+    int mode;                 /*  2 */
+    int len;                  /*  4 */
+    char *name;               /*  6 */
+    unsigned char*ptr;        /*  8 */
+    unsigned char buf[256];       /*  10 */
+ } FILE ;
+
+#define	FCBSIZE	(sizeof(FILE))
+
 #define	NFILES	8
 
 #define	NULL	0
@@ -9,21 +28,55 @@
 #define	stdout	_fcbtbl[1]
 #define	stderr	_fcbtbl[2]
 
-#define	STDIN	0xffff
-#define	STDOUT	0xfffe
-#define	STDERR	0xfffd
-
 FILE *_fcbtbl[NFILES];
 
-_main(argc,argv)
-int argc;
-char **argv;
+FILE _s0[3];
+
+#define STDIN (&_s0[0])
+#define STDOUT (&_s0[1])
+#define STDERR (&_s0[2])
+
+_main(args)
+char *args;
 {int i;
-	stdin = STDIN;
-	stdout = STDOUT;
-	stderr = STDERR;
+ char **argv,*p;
+ int argc,n,quote,c;
+	stdin = STDIN;  stdin->fd = 0;
+	stdout = STDOUT;  stdout->fd = 1;
+	stderr = STDERR;  stderr->fd = 2;
 	initheap();
 	for ( i = 3; i < NFILES; i++ ) _fcbtbl[i] = NULL;
+        /* create argv here */
+        argc = 0;
+        argv = 0;
+        n = 0;
+        quote = 0;
+        for( i = 0; i < 2 ; i++ ) {
+            if (i==1 && argc) { argv = (char**)malloc(sizeof(char*)*argc; }
+            p = args;
+            while((c = *p++)) {
+                if (i==1) argv[n] = args;
+                if (c=='\'') { 
+                    if (!quote) {
+                        if (i==1) argv[n]++;
+                        quote = 1;
+                        continue;
+                    } else {
+                        if (i==1) p[-1]=0;
+                        quote = 0;
+                    }
+                } else if (c=='\\') {
+                    p++;
+                    continue;
+                } else if (c==' ') {
+                    if (quote) 
+                        continue;
+                    if (i==1) p[-1]=0;
+                }
+                n++;
+            }
+            p[-1]=0;
+        }
 	main(argc,argv);
 }
 
@@ -60,9 +113,30 @@
 	if ( i >= NFILES) return NULL;
 	if ( (fcbp = malloc(FCBSIZE)) == NULL ) return NULL;
 	if ( _setname(name,fcbp) == 0 ) return NULL;
-	if ( FMS(fcbp,1) < 0 ) return NULL;
-	fcbp[59] = cm ? 0 : 0xff;
-	fcbp[60] = 0;
+#asm
+        pshs      x,y,u
+        ldx       -4,u
+        lda       2,x          mode
+        ldb       3,x          src attribute
+        leax      6,x          name
+        os9       I$Open
+        bcc        _LC0001
+        ldx       -4,u
+        clrb
+        std       ,x
+        bra       _LC0002
+_LC0001
+        ldx       -4,u
+        clra
+        stD       2,x          err code
+        ldd       #-1
+        std       ,x
+_LC0002
+        puls      x,y,u
+#endasm
+	if fcbp->fd < 0 ) return NULL;
+	fcbp->ptr = fcpp->buf;
+	fcbp->len = 0;
 	return (_fcbtbl[i] = fcbp);
 }
 
@@ -76,14 +150,30 @@
 	if ( i >= NFILES) return NULL;
 	if ( (fcbp = malloc(FCBSIZE)) == NULL ) return NULL;
 	if ( _setname(name,fcbp) == 0 ) return NULL;
-	if ( FMS(fcbp,2) < 0 )
-	{	if ( (fcbp[1] != 3) || (FMS(fcbp,12) < 0) ) return NULL;
-		_setname(name,fcbp);
-		if (FMS(fcbp,2) < 0) return NULL;
-	}
-	fcbp[15] = 0;
-	fcbp[59] = cm ? 0 : 0xff;
-	fcbp[60] = 0;
+#asm
+        pshs      x,y,u
+        ldx       -4,u
+        lda       2,x          mode
+        ldb       3,x          src attribute
+        leax      6,x          name
+        os9       F$Create
+        bcc        _LC0001
+        ldx       -4,u
+        clrb
+        std       ,x
+        bra       _LC0002
+_LC0001
+        ldx       -4,u
+        clra
+        stD       2,x          err code
+        ldd       #-1
+        std       ,x
+_LC0002
+        puls      x,y,u
+#endasm
+	if fcbp->fd < 0 ) return NULL;
+	fcbp->ptr = fcpp->buf;
+	fcbp->len = 0;
 	return (_fcbtbl[i] = fcbp);
 }
 
@@ -95,7 +185,13 @@
 	if ( i >= NFILES ) return EOF;
 	_fcbtbl[i] = NULL;
 	if ( (fcbp == STDIN) || (fcbp == STDOUT) || (fcbp == STDERR) ) return 0;
-	if ( FMS(fcbp,4) < 0 ) return EOF;
+#asm
+        pshs      x,y,u
+        ldx       -4,u
+        lda       ,x
+        os9       I$Close
+        puls      x,y,u
+#endasm
 	mfree(fcbp);
 	return 0;
 }
@@ -130,33 +226,41 @@
 getc(fcbp)
 char *fcbp;
 {
-	switch (fcbp)
-	{case STDIN:
-		return GETCH();
-	case STDOUT:
-	case STDERR:
-		return EOF;
-	default:
-		if (fcbp[2] != 1) return EOF;
-		return FMS(fcbp,0);
-	}
+    int c;
+#asm
+        pshs      x,y,u
+        ldd       ,u        c
+        PSHS      A,B
+        ldx       2,u         
+        lda       1,x       file descriptor
+        leax      1,S
+        clr       ,s
+        ldy       #1
+        os9       I$Read
+        bcc       _LC0003
+        ldd       #-1
+        std       ,s
+_LC0003
+        puls      d,x,y,u
+#endasm
 }
 
 putc(c,fcbp)
 char c,*fcbp;
-{	if ( c == '\t' ) c = ' ';
-	switch (fcbp)
-	{case STDIN:
-		return EOF;
-	case STDOUT:
-		return PUTCH(c);
-	case STDERR:
-		return PUTCH2(c);
-	default:
-		if (fcbp[2] != 2) return EOF;
-		if (FMS(fcbp,0,c) < 0) return EOF;
-		return c;
-	}
+{	
+    int ret;
+#asm
+        pshs      x,y,u
+        ldd       ,u        c
+        PSHS      A,B
+        ldx       2,u         
+        lda       1,x       file descriptor
+        leax      1,S
+        ldy       #1
+        os9       I$Write
+        puls      d,x,y,u
+#endasm
+     return c;
 }
 
 getchar()