     1                                  ; ****************************************************************************
     2                                  ; sh386.s (sh4.s) - Retro Unix 386 v1.2 Shell - /bin/sh
     3                                  ; ----------------------------------------------------------------------------
     4                                  ;
     5                                  ; RETRO UNIX 386 (Retro Unix == Turkish Rational Unix)
     6                                  ; Operating System Project (v0.2) by ERDOGAN TAN (Beginning: 24/12/2013)
     7                                  ;
     8                                  ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
     9                                  ; (v0.1 - Beginning: 11/07/2012)
    10                                  ;
    11                                  ; [ Last Modification: 13/06/2022 ]
    12                                  ;
    13                                  ; Derived from UNIX Operating System (v1.0 for PDP-11) 
    14                                  ; (Original) Source Code by Ken Thompson (Bell Laboratories, 1971-1972)
    15                                  ; ****************************************************************************
    16                                  ;
    17                                  ; <Preliminary Release of UNIX Implementation Document>
    18                                  ; <Isuse: D, Date: 17/3/1972, ID: IMO.1-1, Section: E.11> 
    19                                  ; <sh - command interpreter>
    20                                  ;
    21                                  ; ****************************************************************************
    22                                  
    23                                  ; Assembler: NASM v2.15
    24                                  ; ((nasm sh4.s -l sh4.txt -o sh4 -Z error.txt))
    25                                  
    26                                  ; sh4.s (13/06/2022, Retro UNIX 386 v1.2) -- (modified sh2.s) ---
    27                                  ; sh3.s (29/05/2022, Retro UNIX 386 v1) -- (modified sh0.s) ---
    28                                  ; sh2.s (29/05/2022, Retro UNIX 386 v1.1 & v1.2) -- (modified sh1.s) --
    29                                  ; sh1.s (03/01/2016, Retro UNIX 386 v1.1)
    30                                  ; sh0.s (28/12/2015, Retro UNIX 386 v1, NASM 2.11, 32 bit version of 'sh.asm')
    31                                  ; SHELL03.ASM, 13/11/2013 - 27/06/2014 (sh.asm, Retro UNIX 8086 v1, MASM 6.11) 
    32                                  
    33                                  ; 12/01/2022 (Retro UNIX 386 v1.2)
    34                                  ; 13/10/2015
    35                                  ; 27/08/2015
    36                                  ; 24/08/2015
    37                                  ; 08/04/2014
    38                                  ; 13/11/2013
    39                                  
    40                                  ; UNIX v1 system calls
    41                                  _rele 	equ 0
    42                                  _exit 	equ 1
    43                                  _fork 	equ 2
    44                                  _read 	equ 3
    45                                  _write	equ 4
    46                                  _open	equ 5
    47                                  _close 	equ 6
    48                                  _wait 	equ 7
    49                                  _creat 	equ 8
    50                                  _link 	equ 9
    51                                  _unlink	equ 10
    52                                  _exec	equ 11
    53                                  _chdir	equ 12
    54                                  _time 	equ 13
    55                                  _mkdir 	equ 14
    56                                  _chmod	equ 15
    57                                  _chown	equ 16
    58                                  _break	equ 17
    59                                  _stat	equ 18
    60                                  _seek	equ 19
    61                                  _tell 	equ 20
    62                                  _mount	equ 21
    63                                  _umount	equ 22
    64                                  _setuid	equ 23
    65                                  _getuid	equ 24
    66                                  _stime	equ 25
    67                                  _quit	equ 26	
    68                                  _intr	equ 27
    69                                  _fstat	equ 28
    70                                  _emt 	equ 29
    71                                  _mdate 	equ 30
    72                                  _stty 	equ 31
    73                                  _gtty	equ 32
    74                                  _ilgins	equ 33
    75                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    76                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    77                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    78                                  ; 12/01/2022 - Retro UNIX 386 v1.2
    79                                  ; Retro UNIX 386 v2 system calls
    80                                  _setgid	equ 37
    81                                  _getgid	equ 38
    82                                  _sysver	equ 39 ; (get) Retro Unix 386 version
    83                                  
    84                                  %macro sys 1-4
    85                                      ; 03/09/2015
    86                                      ; 13/04/2015
    87                                      ; Retro UNIX 386 v1 system call.
    88                                      %if %0 >= 2   
    89                                          mov ebx, %2
    90                                          %if %0 >= 3
    91                                              mov ecx, %3
    92                                              ;%if %0 = 4
    93                                              %if	%0 >= 4 ; 11/03/2022
    94                                  		mov edx, %4
    95                                              %endif
    96                                          %endif
    97                                      %endif
    98                                      mov eax, %1
    99                                      int 30h
   100                                  %endmacro
   101                                  
   102                                  ; Retro UNIX 386 v1 system call format:
   103                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   104                                  
   105                                  ;-----------------------------------------------------------------
   106                                  ;  text - code
   107                                  ;-----------------------------------------------------------------
   108                                  
   109                                  [BITS 32] ; 32-bit intructions (for 80386 protected mode)
   110                                  
   111                                  [ORG 0] 
   112                                  
   113                                  START_CODE:
   114                                  ;/ sh -- command interpreter
   115                                  
   116                                  	; 13/06/2022
   117                                  	; 29/05/2022 - Erdogan Tan
   118                                  	; (Modified by using disassembled unix v2 'sh' code.)
   119                                  	
   120                                  	;;27/12/2015
   121                                  	;;clear BSS
   122                                  	;mov	ecx, ((bss_end - bss_start) + 3) / 4
   123                                  	;sub	eax, eax
   124                                  	;mov	edi, bss_start
   125                                  	;rep	stosd
   126                                  s0:
   127 00000000 89E5                    	mov	ebp, esp
   128                                  		; mov sp,r5
   129 00000002 892D[2E0C0000]          	mov	[shellarg], ebp
   130                                  		; mov r5,shellarg / save orig sp in shellarg
   131 00000008 8B5D04                  	mov	ebx, [ebp+4]
   132 0000000B 803B2D                  	cmp	byte [ebx], '-'
   133                                  		; cmpb	*2(r5),$'- / was this sh called by init or login
   134 0000000E 752E                    	jne	short s1
   135                                  		; bne 2f / no
   136                                  	sys	_intr, 0
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000010 BB00000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000015 B81B000000          <1>  mov eax, %1
    99 0000001A CD30                <1>  int 30h
   137                                  		; sys intr; 0 / yes, turn off interrupts
   138                                  	sys	_quit, 0
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 0000001C BB00000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000021 B81A000000          <1>  mov eax, %1
    99 00000026 CD30                <1>  int 30h
   139                                  		; sys quit; 0
   140                                  	sys	_write, 1, msg_unix_sh, msgsh_size
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000028 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 0000002D B9[A7050000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 00000032 BA1D000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000037 B804000000          <1>  mov eax, %1
    99 0000003C CD30                <1>  int 30h
   141                                  s1: ;2:
   142                                  	sys	_getuid
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000003E B818000000          <1>  mov eax, %1
    99 00000043 CD30                <1>  int 30h
   143                                  		; sys	getuid / who is user
   144                                  	;and	eax, eax
   145 00000045 20C0                    	and	al, al
   146                                  		; tst r0 / is it superuser
   147 00000047 7507                    	jnz	short s2
   148                                  		; bne 2f / no
   149 00000049 C605[35060000]23        	mov	byte [_at], '#'
   150                                  		; movb $'#,at / yes, set new prompt symbol
   151                                  s2: ;2:
   152 00000050 837D0001                	cmp	dword [ebp], 1
   153                                  		; cmp (r5),$1 / tty input?
   154 00000054 7631                    	jna	short newline
   155                                  		; ble newline / yes, call with '-' (or with no command
   156                                  		      ; / file name)
   157 00000056 31DB                    	xor	ebx, ebx
   158                                  		; clr r0 / no, set tty
   159                                  	sys	_close
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000058 B806000000          <1>  mov eax, %1
    99 0000005D CD30                <1>  int 30h
   160                                  		; sys close / close it
   161 0000005F 8B5D08                  	mov	ebx, [ebp+8] ; arg 1
   162                                  		; mov 4(r5),0f / get new file name
   163 00000062 31C9                    	xor	ecx, ecx ; arg 2
   164                                  	sys	_open
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000064 B805000000          <1>  mov eax, %1
    99 00000069 CD30                <1>  int 30h
   165                                  		; sys open; 0:..; 0 / open it
   166 0000006B 7311                    	jnc	short s3
   167                                  		; bec 1f / branch if no error
   168 0000006D BE[D1050000]            	mov	esi, msgNotFound
   169 00000072 E8EA010000              	call	error
   170                                  		; jsr r5,error / error in file name
   171                                  		; <Input not found\n\0>; .even
   172                                  	sys	_exit
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000077 B801000000          <1>  mov eax, %1
    99 0000007C CD30                <1>  int 30h
   173                                  		; sys exit
   174                                  s3: ;1:
   175 0000007E C605[35060000]00        	mov	byte [_at], 0
   176                                  		; clr at / clear prompt character, if reading non-tty
   177                                  		   ; / input file
   178 00000085 EB1F                    	jmp	short newcom
   179                                  newline:
   180 00000087 803D[35060000]00        	cmp	byte [_at], 0
   181                                  		; tst at / is there a prompt symbol
   182 0000008E 7616                    	jna	short newcom
   183                                  		; beq newcom / no
   184                                  		; mov $1,r0 / yes
   185                                  nl:
   186                                  	sys	_write, 1, prompt, p_size
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000090 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000095 B9[33060000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 0000009A BA04000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000009F B804000000          <1>  mov eax, %1
    99 000000A4 CD30                <1>  int 30h
   187                                  	;sys	_write, 1, _at, 2
   188                                  		; sys write; at; 2. / print prompt
   189                                  newcom:
   190 000000A6 8B25[2E0C0000]          	mov	esp, [shellarg]
   191                                  		; mov shellarg,sp /
   192 000000AC BE[62060000]            	mov	esi, parbuf 
   193                                  		; mov $parbuf,r3 / initialize command list area
   194 000000B1 BF[5C0A0000]            	mov	edi, parp
   195                                  		; mov $parp,r4 / initialize command list pointers
   196 000000B6 31C0                    	xor	eax, eax
   197 000000B8 A3[500A0000]            	mov	[infile], eax ; 0	
   198                                  		; clr infile / initialize alternate input
   199 000000BD A3[540A0000]            	mov	[outfile], eax ; 0
   200                                  		; clr outfile / initialize alternate output
   201 000000C2 A2[4E0A0000]            	mov	[glflag], al ; 0
   202                                  	;mov	[glflag], ax ; 0
   203                                  		; clr glflag / initialize global flag
   204                                  	; 27/12/2015
   205 000000C7 A2[32060000]            	mov	[FCAT], al ; 0
   206                                  newarg:
   207 000000CC E862030000              	call	blank
   208                                  		; jsr pc,blank / squeeze out leading blanks
   209 000000D1 E83E030000              	call	delim
   210 000000D6 7467                    	je	short nch4 ; '\n', ';', '&'
   211                                  		; jsr r5,delim / is new character a ; \n or &
   212                                  		;     br 2f / yes
   213 000000D8 56                      	push	esi
   214                                  		; mov r3,-(sp) / no, push arg pointer onto stack
   215 000000D9 3C3C                    	cmp	al, '<'
   216                                  		; cmp r0,$'< / new input file?
   217 000000DB 7508                    	jne	short na1
   218                                  		; bne 1f / no
   219 000000DD 8935[500A0000]          	mov	[infile], esi
   220                                  		; mov (sp),infile / yes, save arg pointer
   221                                  	;jmp 	short na4
   222 000000E3 EB1E                    	jmp	short na3
   223                                  	;mov	dword [esp], 0
   224                                  		; clr (sp) / clear pointer
   225                                  	;jmp	short nch1
   226                                  		; br 3f
   227                                  na1: ;1:
   228 000000E5 3C3E                    	cmp	al, '>'
   229                                  		; cmp r0,$'> / new output file?
   230 000000E7 7530                    	jne	short nch0
   231                                  	;jne	short newchar
   232                                  		; bne newchar / no
   233 000000E9 8935[540A0000]          	mov	[outfile], esi
   234                                  		; mov (sp),outfile / yes, save arg pointer
   235                                  	; 27/12/2015
   236 000000EF E851030000              	call	getc
   237 000000F4 3C3E                    	cmp	al, '>'
   238 000000F6 7507                    	jne	short na2
   239                                  	;
   240 000000F8 A2[32060000]            	mov	[FCAT], al ; '>>'
   241 000000FD EB04                    	jmp	short na3
   242                                  na2:
   243 000000FF 3C20                    	cmp	al, 20h ; ' '
   244 00000101 7505                    	jne	short na4
   245                                  na3:
   246 00000103 E82B030000              	call	blank
   247                                  na4:	
   248 00000108 C7042400000000          	mov	dword [esp], 0
   249                                  		; clr (sp) / clear pointer
   250                                  	;jmp	short nch1
   251                                  		; br 3f
   252 0000010F EB12                    	jmp	short nch7
   253                                  newchar:
   254 00000111 3C20                    	cmp	al, 20h
   255                                  		; cmp $' ,r0 / is character a blank
   256 00000113 7415                    	je	short nch2
   257                                  		; beq 1f / branch if it is (blank as arg separator)
   258 00000115 3C8D                            cmp     al, 8Dh ; 128 + 13
   259 00000117 7411                    	je	short nch2
   260                                  		; cmp $'\n+200,r0 / treat \n preceded by 		; beq 1f / as blank
   262                                  nch0:
   263 00000119 E8A0010000              	call	putc
   264                                  		; jsr pc,putc / put this character in parbuf list
   265                                  nch1: ;3:
   266 0000011E E822030000              	call	getc
   267                                  		; jsr pc,getc / get next character
   268                                  nch7:
   269 00000123 E8EC020000              	call	delim
   270 00000128 75E7                    	jne	short newchar
   271                                  	;jz	short nch2 ; '\n', ';', '&'
   272                                  		; jsr r5,delim / is char a ; \n or &,
   273                                  		;     br 1f / yes
   274                                  	;jmp	short newchar
   275                                  		; br newchar / no, start new character tests
   276                                  nch2: ;1:
   277 0000012A C60600                  	mov	byte [esi], 0
   278 0000012D 46                      	inc	esi
   279                                  		; clrb (r3)+ / end name with \0 when read blank, 
   280                                  			  ; or delim
   281 0000012E 5B                      	pop	ebx
   282 0000012F 891F                    	mov	[edi], ebx
   283                                  		; mov (sp)+,(r4)+ / move arg ptr to parp location
   284 00000131 09DB                    	or	ebx, ebx
   285 00000133 7403                    	jz	short nch3
   286                                  	;jnz	short nch3
   287                                  		; bne 1f / if (sp)=0, in file or out file points
   288                                  				; to arg
   289 00000135 83C704                  	add	edi, 4
   290                                  		; tst -(r4) / so ignore dummy (0), in pointer list
   291                                  nch3: ;1:
   292 00000138 E8D7020000              	call	delim
   293 0000013D 758D                    	jne	short newarg
   294                                  	;jz	short nch4 ; '\n', ';', '&'
   295                                  		; jsr r5,delim / is char a ; \n or &.
   296                                  		;     br 2f / yes
   297                                  	;jmp	short newarg
   298                                  		; br newarg / no, start newarg processing
   299                                  nch4: ;2:
   300 0000013F C70700000000            	mov	dword [edi], 0
   301                                  		; clr (r4) / \n, &, or ; takes to here 
   302                                  			 ; / (end of arg list) after 'delim' call
   303 00000145 50                      	push	eax
   304                                  		; mov r0,-(sp) / save delimiter in stack
   305 00000146 E827000000              	call	docom
   306                                  		; jsr pc,docom / go to exec command in parbuf
   307 0000014B 803C2426                	cmp	byte [esp], '&'
   308                                  		; cmpb (sp),$'& / get a new command without wait?
   309                                          ;je	newcom
   310                                  	;	; beq newcom / yes
   311                                  	; 29/05/2022
   312 0000014F 7505                    	jne	short nch_9
   313                                  nch_8:
   314 00000151 E950FFFFFF              	jmp	newcom
   315                                  nch_9:
   316 00000156 21D2                    	and	edx, edx
   317                                  		; tst r1 / was chdir just executed or line ended
   318                                  			; / with ampersand?
   319 00000158 740D                    	jz	short nch6
   320                                  		; beq 2f / yes
   321                                  nch5: ;1:
   322                                  	sys	_wait
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000015A B807000000          <1>  mov eax, %1
    99 0000015F CD30                <1>  int 30h
   323                                  		; sys wait / no, wait for new process to terminate
   324                                                          ; / command executed)
   325 00000161 7204                    	jc	short nch6
   326                                  		; bcs 2f / no, children not previously waited for
   327 00000163 39D0                    	cmp	eax, edx
   328                                  		; cmp r0,r1 / is this my child
   329 00000165 75F3                    	jne	short nch5
   330                                  		; bne 1b
   331                                  nch6: ;2:
   332 00000167 803C240D                	cmp	byte [esp], 0Dh
   333                                  	;cmp	byte [esp], 0Ah
   334                                  		; cmp (sp),$'\n / was delimiter a new line
   335                                          ;je	newline
   336                                  	;	; beq newline / yes
   337                                  	; 29/05/2022
   338                                  	;jmp	newcom
   339                                  	;	; br newcom / no, pick up next command
   340 0000016B 75E4                    	jne	short nch_8  ; jne newcom
   341 0000016D E915FFFFFF              	jmp	newline  ; je newline
   342                                  docom:
   343 00000172 81EF[5C0A0000]          	sub	edi, parp
   344                                  		; sub $parp,r4 / out arg count in r4
   345 00000178 7503                    	jne	short dcom1
   346                                  		; bne 1f / any arguments?
   347                                  dcom0:
   348 0000017A 29D2                    	sub 	edx, edx ; 0
   349                                  		; clr r1 / no, line ended with ampersand
   350 0000017C C3                      	retn
   351                                  		; rts pc / return from call
   352                                  dcom1: ;1:
   353 0000017D 89FB                    	mov	ebx, edi
   354                                  	; 06/12/2013
   355 0000017F BE[37060000]            	mov	esi, qecho 
   356 00000184 E827010000              	call	chcom
   357 00000189 7543                    	jnz	short dcom7
   358                                  	; 28/12/2015
   359 0000018B 89DD                    	mov	ebp, ebx
   360 0000018D BF[5C0A0000]            	mov	edi, parp
   361                                  dcom9:  ; CRLF
   362                                  	sys	_write, 1, nextline, 2
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000192 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000197 B9[CE050000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 0000019C BA02000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000001A1 B804000000          <1>  mov eax, %1
    99 000001A6 CD30                <1>  int 30h
   363                                  	;
   364 000001A8 83ED04                  	sub	ebp, 4 ; remain arg count x 4
   365 000001AB 76CD                    	jna	short dcom0
   366 000001AD 83C704                  	add	edi, 4
   367 000001B0 8B37                    	mov	esi, [edi]
   368                                  	;or 	esi, esi
   369                                  	;jz	short dcom0
   370 000001B2 89F2                    	mov	edx, esi ; string address
   371                                  dcom10:
   372 000001B4 AC                      	lodsb
   373 000001B5 08C0                    	or 	al, al
   374 000001B7 75FB                    	jnz	short dcom10
   375 000001B9 87D6                    	xchg 	edx, esi
   376 000001BB 29F2                    	sub	edx, esi  ; byte count
   377 000001BD 4A                      	dec	edx
   378                                  	;jz	short dcom0
   379                                  	; write string
   380                                  	sys	_write, 1, esi ; edx = byte count
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 000001BE BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 000001C3 89F1                <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000001C5 B804000000          <1>  mov eax, %1
    99 000001CA CD30                <1>  int 30h
   381 000001CC EBC4                    	jmp	short dcom9
   382                                  dcom7:
   383 000001CE BE[3F060000]            	mov	esi, qchdir
   384 000001D3 E8D8000000              	call	chcom
   385 000001D8 752D                    	jnz	short dcom4
   386                                  		; jsr r5,chcom; qchdir / is command chdir?
   387                                  		;     br 2f / command not chdir
   388                                  dcom12:
   389 000001DA 80FB08                  	cmp	bl, 8 ; 8 = arg count x 4 (24/08/2015)
   390                                  	;cmp	bx, 8
   391                                  		; cmp r4,$4 / prepare to exec chdir, 
   392                                  			  ; 4 = arg count x 2
   393 000001DD 740C                    	je	short dcom2
   394                                  		; beq 3f
   395                                  dcom8:
   396 000001DF BE[E1050000]            	mov	esi, msgArgCount
   397 000001E4 E878000000              	call	error
   398                                  		; jsr r5,error / go to print error
   399                                  		;  	<Arg count\n\0>; .even
   400                                  	;jmp	short dcom3
   401                                  		; br 4f
   402 000001E9 EB8F                    	jmp	short dcom0
   403                                  dcom2: ;3:
   404 000001EB 8B1D[600A0000]          	mov	ebx, [parp+4]
   405                                  		;mov parp+2,0f / move directory name to sys call
   406                                  	sys	_chdir
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000001F1 B80C000000          <1>  mov eax, %1
    99 000001F6 CD30                <1>  int 30h
   407                                  		; sys chdir; 0:0 / exec chdir
   408 000001F8 730A                    	jnc	short dcom3
   409                                  		; bec 4f / no error exit
   410 000001FA BE[EB050000]            	mov	esi, msgBadDir
   411 000001FF E85D000000              	call	error
   412                                  		; jsr r5,error / go to print error
   413                                  		; 	<Bad directory\n\0>; .even
   414                                  				; / this diagnostic
   415                                  dcom3: ;4:
   416 00000204 31D2                    	xor	edx, edx ; 0
   417                                  		; clr r1 / set r1 to zero to skip wait
   418 00000206 C3                      	retn
   419                                  		; rts pc / and return
   420                                  dcom4: ;2:
   421                                  	; 06/12/2013
   422 00000207 BE[3C060000]            	mov	esi, qcd
   423 0000020C E89F000000              	call	chcom
   424 00000211 74C7                    	jz	short dcom12
   425                                  dcom11:
   426 00000213 BE[45060000]            	mov	esi, glogin
   427 00000218 E893000000              	call	chcom
   428 0000021D 7522                    	jnz	short dcom5
   429                                  		; jsr r5,chcom; glogin / is command login?
   430                                  		;     br 2f / not loqin, go to fork
   431                                  	sys	_exec, parbuf, parp
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 0000021F BB[62060000]        <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000224 B9[5C0A0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000229 B80B000000          <1>  mov eax, %1
    99 0000022E CD30                <1>  int 30h
   432                                  		; sys exec; parbuf; parp / exec login
   433                                  	sys	_exec, binpb, parp
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000230 BB[5D060000]        <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000235 B9[5C0A0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000023A B80B000000          <1>  mov eax, %1
    99 0000023F CD30                <1>  int 30h
   434                                  		; sys exec; binpb; parp / or /bin/login
   435                                  dcom5: ;2: / no error return??
   436 00000241 BB[F3020000]            	mov	ebx, newproc
   437                                  	; child process will return to 'newproc' address
   438                                  	sys	_fork
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000246 B802000000          <1>  mov eax, %1
    99 0000024B CD30                <1>  int 30h
   439                                  		; sys fork / generate sh child process
   440                                  			 ; for command
   441                                                  ;     br newproc / exec command with 
   442                                  			 ; new process
   443                                  	; parent process will return here
   444 0000024D 730F                    	jnc	short dcom6
   445                                  		; bec 1f / no error exit, old process
   446 0000024F BE[F9050000]            	mov	esi, msgTryAgain
   447 00000254 E808000000              	call	error
   448                                  		; jsr r5,error / go to print error
   449                                  		;     <Try again\n\0>; .even / this diagnostic
   450 00000259 E929FEFFFF                      jmp     newline
   451                                  		; jmp newline / and return for next try
   452                                  dcom6: ;1:
   453 0000025E 89C2                    	mov	edx, eax ; child process ID
   454                                  		; mov r0,r1 / save id of child sh
   455 00000260 C3                      	retn
   456                                  		; rts pc / return to "jsr pc, docom" call
   457                                  			; in parent sh
   458                                  error:
   459                                  	sys	_write, 1, nextline, 2
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000261 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000266 B9[CE050000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 0000026B BA02000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000270 B804000000          <1>  mov eax, %1
    99 00000275 CD30                <1>  int 30h
   460                                  s4:
   461 00000277 AC                      	lodsb
   462 00000278 A2[2C0C0000]            	mov	[och], al
   463                                                  ; movb (r5)+,och / pick up diagnostic character
   464 0000027D 20C0                    	and	al, al
   465 0000027F 7418                    	jz	short s5
   466                                  		; beq 1f / 0 is end of line
   467                                  	sys	_write, 1, och, 1
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000281 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000286 B9[2C0C0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 0000028B BA01000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000290 B804000000          <1>  mov eax, %1
    99 00000295 CD30                <1>  int 30h
   468                                  		; mov $1,r0 / set for tty output
   469                                  		; sys write; och; 1 / print it
   470 00000297 EBDE                    	jmp	short s4
   471                                  	;jmp	short error
   472                                  		; br error / continue to get characters
   473                                  s5: ;1:
   474                                  	;inc	esi
   475                                  		; inc r5 / inc r5 to point to return
   476                                  	;;and	si, 0FFFEh
   477                                  	;shr	esi, 1
   478                                  	;shl	esi, 1
   479                                  		; bic $1,r5 / make it even
   480                                  	sys	_seek, 0, 0, 2	
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000299 BB00000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 0000029E B900000000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 000002A3 BA02000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002A8 B813000000          <1>  mov eax, %1
    99 000002AD CD30                <1>  int 30h
   481                                  		; clr r0 / set for input
   482                                  		; sys seek; 0; 2 / exit from runcom. skip to
   483                                  			       ; / end of input file
   484 000002AF C3                      	retn
   485                                  		;; rts r5 ; unix v2 shell ; 29/05/2022
   486                                  		;; (not in original unix v1 'sh.s')
   487                                  
   488                                  chcom: ; / has no effect if tty input
   489                                  		; mov (r5)+,r1 / glogin gchdir r1, bump r5
   490 000002B0 BF[62060000]            	mov	edi, parbuf
   491                                  		; mov $parbuf,r2 / command address r2 'login'
   492                                  s6: ;1:
   493 000002B5 AC                      	lodsb
   494                                  		; movb (r1)+,r0 / is this command 'chdir'
   495 000002B6 AE                      	scasb
   496                                                  ; cmpb (r2)+,r0 / compare command name byte
   497                                                                ; / with 'login' or 'chdir'
   498 000002B7 7504                    	jne	short s7
   499                                  		; bne 1f / doesn't compare
   500 000002B9 08C0                    	or	al, al
   501                                  		; tst r0 / is this
   502 000002BB 75F8                    	jnz	short s6
   503                                  		; bne 1b / end of names
   504                                  		; tst (r5)+ / yes, bump r5 again to execute 
   505                                  			; / login or chdir
   506                                  s7: ;1:
   507 000002BD C3                      	retn
   508                                  		; rts r5 / no, return to exec command
   509                                  
   510                                  putc:
   511 000002BE 3C27                    	cmp	al, 27h ; '
   512                                  		; cmp r0,$'' / single quote?
   513 000002C0 740A                    	je	short pch1	
   514                                  		; beq 1f / yes
   515 000002C2 3C22                    	cmp	al, 22h ; "
   516                                  		; cmp r0,$'" / double quote
   517 000002C4 7406                    	je	short pch1
   518                                  		; beq 1f / yes
   519 000002C6 247F                    	and	al, 7Fh
   520                                  		; bic $!177,r0 / no, remove 200, if present
   521 000002C8 8806                    	mov	[esi], al
   522 000002CA 46                      	inc	esi
   523                                  		; movb r0,(r3)+ / store character in parbuf
   524 000002CB C3                      	retn
   525                                  		; rts pc
   526                                  pch1: ;1:
   527 000002CC 50                      	push	eax
   528                                  		; mov r0,-(sp) / push quote mark onto stack
   529                                  pch2: ;1:
   530 000002CD E873010000              	call	getc
   531                                  		; jsr pc,getc / get a quoted character
   532 000002D2 3C0D                    	cmp	al, 0Dh
   533                                  	;cmp	al, 0Ah ; \n
   534                                  		; cmp r0,$'\n / is it end or line
   535 000002D4 750F                    	jne	short pch3
   536                                  		; bne 2f / no
   537 000002D6 BE[03060000]            	mov	esi, msgImbalance
   538 000002DB E881FFFFFF              	call	error
   539                                  		; jsr r5,error / yes, indicate missing
   540                                  			      ; quote mark
   541                                  		;     <"' imbalance\n\0>; .even
   542 000002E0 E9A2FDFFFF                      jmp     newline
   543                                  		; jmp newline / ask for new line
   544                                  pch3: ;2:
   545 000002E5 380424                  	cmp	[esp], al
   546                                  		; cmp r0,(sp) / is this closing quote mark
   547 000002E8 7407                    	je	short pch4
   548                                  		; beq 1f / yes
   549 000002EA 247F                    	and	al, 7Fh
   550                                  		; bic $!177,r0 / no, strip off 200
   551                                  			      ; if present
   552 000002EC 8806                    	mov	[esi], al
   553 000002EE 46                      	inc	esi
   554                                  		; movb r0,(r3)+ / store quoted character
   555                                  			      ; in parbuf
   556 000002EF EBDC                    	jmp	short pch2
   557                                  		; br 1b / continue
   558                                  pch4: ;1:
   559 000002F1 58                      	pop	eax
   560                                  		; tst (sp)+ / pop quote mark off stack
   561 000002F2 C3                      	retn
   562                                  		; rts pc / return
   563                                  
   564                                  ; / thp`e new process
   565                                  
   566                                  newproc:
   567 000002F3 8B35[500A0000]          	mov	esi, [infile]
   568 000002F9 09F6                    	or	esi, esi
   569 000002FB 7432                    	jz	short np2
   570                                  		; mov infile,0f / move pointer to new file name
   571                                  		; beq 1f / branch if no alternate read file given
   572 000002FD 803E00                  	cmp 	byte [esi], 0
   573                                  		; tstb *0f
   574 00000300 761C                    	jna	short np1
   575                                  		; beq 3f / branch if no file name given
   576                                  	sys	_close, 0
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000302 BB00000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000307 B806000000          <1>  mov eax, %1
    99 0000030C CD30                <1>  int 30h
   577                                  		; clr r0 / set tty input file name
   578                                  		; sys close / close it
   579                                  	sys	_open, esi, 0  
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 0000030E 89F3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000310 B900000000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000315 B805000000          <1>  mov eax, %1
    99 0000031A CD30                <1>  int 30h
   580                                  		; sys open; 0:..; 0 / open new input file 
   581                                  				  ; for reading
   582 0000031C 7311                    	jnc	short np2
   583                                  		; bcc 1f / branch if input file ok
   584                                  np1: ;3:
   585 0000031E BE[10060000]            	mov	esi, msgInputFile
   586 00000323 E839FFFFFF              	call	error
   587                                  		; jsr r5,error / file not ok, print error
   588                                  		;     <Input file\n\0>; .even / this diagnostic
   589                                  	sys	_exit
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000328 B801000000          <1>  mov eax, %1
    99 0000032D CD30                <1>  int 30h
   590                                  		; sys exit / terminate this process 
   591                                  			  ; and make parent sh
   592                                  np2: ;1:
   593 0000032F 8B35[540A0000]          	mov	esi, [outfile]
   594                                  		; mov outfile,r2 / more pointer to new file name
   595 00000335 21F6                    	and	esi, esi
   596 00000337 747E                    	jz	short np6
   597                                  		; beq 1f / branch if no alternate write file
   598                                  	; 27/12/2015
   599 00000339 803E00                  	cmp	byte [esi], 0
   600 0000033C 7629                    	jna	short np4
   601                                  	;
   602                                  	;cmp	byte [esi], '>'
   603                                  		; cmpb (r2),$'> / is > at beqinning of file name?
   604                                  	;jne	short np3
   605                                  		; bne 4f / branch if it isn't
   606                                  	;inc	esi
   607                                  		; inc r2 / yes, increment pointer
   608                                  	; 27/12/2015
   609 0000033E 803D[32060000]3E        	cmp	byte [FCAT], '>' ; '>>'
   610 00000345 7510                    	jne	short np3
   611                                  	;
   612                                  	sys	_open, esi, 1
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000347 89F3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000349 B901000000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000034E B805000000          <1>  mov eax, %1
    99 00000353 CD30                <1>  int 30h
   613                                  		; mov r2,0f
   614                                  		; sys open; 0:..; 1 / open file for writing
   615 00000355 7321                    	jnc	short np5
   616                                  		; bec 3f / if no error
   617                                  np3: ;4:
   618                                  	;sys	_creat, esi, 15 ; Decimal 15 = Octal 17
   619                                  	; 13/06/2022 - Retro UNIX 386 1.2 (runix v2 inode)
   620                                  	sys	_creat, esi, 01B6h  ; Decimal 438 = Octal 666 
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000357 89F3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000359 B9B6010000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000035E B808000000          <1>  mov eax, %1
    99 00000363 CD30                <1>  int 30h
   621                                  		; mov r2,0f
   622                                  		; sys creat; 0:..; 17 / create new file 
   623                                  				    ; with this name
   624 00000365 7311                    	jnc	short np5
   625                                  		; bec 3f / branch if no error
   626                                  np4: ;2:
   627 00000367 BE[1B060000]            	mov	esi, msgOutputFile
   628 0000036C E8F0FEFFFF              	call	error
   629                                  		; jsr r5,error
   630                                  		;     <Output file\n\0>; .even
   631                                  	sys	_exit
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000371 B801000000          <1>  mov eax, %1
    99 00000376 CD30                <1>  int 30h
   632                                  		; sys exit
   633                                  np5: ;3:
   634                                  	sys	_close, eax
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000378 89C3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000037A B806000000          <1>  mov eax, %1
    99 0000037F CD30                <1>  int 30h
   635                                  		; sys close / close the new write file
   636                                  		; mov	r2,0f / move new name to open
   637                                  np10:
   638                                  	sys	_close, 1
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000381 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000386 B806000000          <1>  mov eax, %1
    99 0000038B CD30                <1>  int 30h
   639                                  		; mov $1,r0 / set tty file name
   640                                  		; sys close / close it
   641                                  	sys	_open, esi, 1
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 0000038D 89F3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 0000038F B901000000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000394 B805000000          <1>  mov eax, %1
    99 00000399 CD30                <1>  int 30h
   642                                  		; sys open; 0:..; 1 / open new output file,
   643                                  			      ; /it now has file descriptor 1
   644                                  	; 27/12/2015
   645 0000039B 803D[32060000]3E        	cmp	byte [FCAT], '>' ; '>>'
   646 000003A2 7513                    	jne	short np6
   647                                  	;
   648                                  	sys	_seek, eax, 0, 2
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 000003A4 89C3                <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 000003A6 B900000000          <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94 000003AB BA02000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000003B0 B813000000          <1>  mov eax, %1
    99 000003B5 CD30                <1>  int 30h
   649                                  		; sys seek; 0; 2 / set pointer to 
   650                                  				; current end of file
   651                                  np6: ;1:
   652 000003B7 803D[4E0A0000]00        	cmp	byte [glflag], 0
   653                                  		; tst glflag / was *, ? or [ encountered?
   654 000003BE 7739                            ja      short np9
   655                                  		; bne 1f / yes
   656                                  	sys	_exec, parbuf, parp
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 000003C0 BB[62060000]        <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 000003C5 B9[5C0A0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000003CA B80B000000          <1>  mov eax, %1
    99 000003CF CD30                <1>  int 30h
   657                                  		; sys exec; parbuf; parp / no, execute
   658                                  					;  this command
   659                                  	sys	_exec, binpb, parp
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 000003D1 BB[5D060000]        <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 000003D6 B9[5C0A0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000003DB B80B000000          <1>  mov eax, %1
    99 000003E0 CD30                <1>  int 30h
   660                                  		; sys exec; binpb; parp / or /bin/this command
   661                                  
   662                                  ;------ 29/05/2022 - unix v2 shell
   663                                  ;	 (source code from disassembled /bin/sh binary)
   664                                  ;	following part is not existing in v2 shell
   665                                  ;
   666                                  ;np7: ;2:
   667                                  ;	sys	_stat, binpb, inbuf
   668                                  ;		; sys stat; binpb; inbuf / if can't execute
   669                                  ;					; / does it exist?
   670                                  ;	jc	short np8
   671                                  ;		; bes 2f / branch if it doesn't
   672                                  ;	mov	esi, parp-4
   673                                  ;	mov	dword [esi], shell
   674                                  ;		; mov $shell,parp-2 / does exist,
   675                                  ;				   ;  not executable
   676                                  ;	mov	eax, binpb
   677                                  ;	mov	[parp], eax
   678                                  ;		; mov $binpb,parp / so it must be
   679                                  ;	sys	_exec, shell, esi 
   680                                  ;		; sys exec; shell; parp-2 / a command file,
   681                                  ;		; / get it with sh /bin/x (if x name of file)
   682                                  ;------
   683                                  
   684                                  np8: ;2:
   685 000003E2 BE[27060000]            	mov	esi, msgNoCmd
   686 000003E7 E875FEFFFF              	call	error
   687                                  		; jsr r5,error / a return for exec 
   688                                  			       ; is the diagnostic
   689                                  		;     <No command\n\0>; .even
   690 000003EC 8B25[2E0C0000]          	mov	esp, [shellarg]
   691                                  	sys 	_exit
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000003F2 B801000000          <1>  mov eax, %1
    99 000003F7 CD30                <1>  int 30h
   692                                  		; sys exit
   693                                  np9: ;1:
   694 000003F9 BE[580A0000]            	mov	esi, parp-4
   695 000003FE C706[53060000]          	mov	dword [esi], glob
   696                                  		; mov $glob,parp-2 / prepare to process *,?
   697                                  	sys	_exec, glob, esi
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000404 BB[53060000]        <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 00000409 89F1                <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000040B B80B000000          <1>  mov eax, %1
    99 00000410 CD30                <1>  int 30h
   698                                  		; sys exec; glob; parp-2
   699                                  			    ; / execute modified command
   700 00000412 EBCE                    	jmp	short np8
   701                                  		; br 2b
   702                                  
   703                                  delim:
   704 00000414 3C0D                            cmp     al, 0Dh ; carriage return
   705 00000416 741A                    	je	short dlim2
   706                                  	;cmp	al, 0Ah
   707                                  		; cmp r0,$'\n / is character a newline
   708                                  	;je	short dlim2
   709                                  		; beq 1f
   710 00000418 3C26                    	cmp	al, '&'
   711                                  		;cmp r0,$'& / is it &
   712 0000041A 7416                    	je	short dlim2
   713                                  		; beq 1f / yes
   714 0000041C 3C3B                    	cmp	al, ';'
   715                                  		; cmp r0,$'; / is it ;
   716 0000041E 7412                    	je	short dlim2
   717                                  		; beq 1f / yes
   718 00000420 3C3F                    	cmp	al, '?'
   719                                  		; cmp r0,$'? / is it ?
   720 00000422 7408                    	je	short dlim1
   721                                  		; beq 3f
   722                                  ;
   723                                  ;------ 29/05/2022 - unix v2 shell
   724                                  ;	 (source code from disassembled /bin/sh binary)
   725 00000424 3C2A                    	cmp	al, '*'
   726                                  		; cmp r0,$'* / is it *
   727 00000426 7404                    	je	short dlim1
   728                                  		; beq 3f
   729                                  
   730 00000428 3C5B                    	cmp	al, '['
   731                                  		; cmp r0,$'[ / is it beginning of character string
   732                                  		      ; / (for glob)
   733 0000042A 7506                    	jne	short dlim2
   734                                  		; bne 2f
   735                                  dlim1: ;3:
   736 0000042C FE05[4E0A0000]          	inc	byte [glflag]
   737                                  		; inc glflag / ? or * or [ set flag
   738                                  ;2:
   739                                  	;tst	(r5)+ / bump to process all except \n,;,&
   740                                  dlim2: ;1:
   741                                  	; zf = 1 if the char is '\n' or ';' or '&'
   742 00000432 C3                      	retn
   743                                  		; rts r5
   744                                  blank:
   745 00000433 E80D000000              	call	getc
   746                                  		; jsr pc,getc / get next character
   747 00000438 3C20                    	cmp	al, 20h
   748                                  		; cmp $' ,r0 / leading blanks
   749 0000043A 74F7                    	je	short blank
   750                                  		; beq blank / yes, 'squeeze out'
   751 0000043C 3C8D                    	cmp	al, 8Dh ; 80h + 0Dh
   752                                          ;cmp	al, 8Ah ; 80h + 0Ah
   753 0000043E 74F3                    	je	short blank
   754                                  	        ; cmp r0,$200+'\n / new-line preceded by 				 ;  is translated
   756                                  		; beq blank / into blank
   757                                  	; 28/12/2015
   758 00000440 3C0A                    	cmp	al, 0Ah
   759 00000442 74EF                    	je	short blank
   760                                  	;
   761 00000444 C3                      	retn
   762                                  		; rts pc
   763                                  getc:
   764 00000445 833D[4A0A0000]00        	cmp	dword [param], 0
   765                                  		; tst param / are we substituting for $n
   766 0000044C 773D                    	ja	short gch3
   767                                  		; bne 2f/ yes
   768                                  gch0:
   769 0000044E 8B1D[240C0000]                  mov     ebx, [inbufp]
   770                                  		; mov inbufp,r1 / no, move normal input pointer to r1
   771                                  s8:
   772 00000454 3B1D[280C0000]          	cmp	ebx, [einbuf]
   773                                  		; cmp r1,einbuf / end of input line?
   774 0000045A 7207                    	jb	short gch1
   775                                  		; bne 1f / no
   776 0000045C E877000000              	call	getbuf	
   777                                  		; jsr pc,getbuf / yes, put next console line
   778                                  				;  in buffer
   779 00000461 EBEB                    	jmp	short gch0
   780                                  		; br getc
   781                                  gch1: ;1:
   782 00000463 8A03                    	mov	al, [ebx]
   783 00000465 43                      	inc	ebx
   784                                  		; movb (r1)+,r0 / move byte from input buffer to r0
   785 00000466 891D[240C0000]          	mov	[inbufp], ebx
   786                                  		; mov r1,inbufp / increment routine
   787 0000046C 0A05[320C0000]          	or	al, [escap]
   788                                  	;or	ax, escap
   789                                  		; bis escap,r0 / if last character was \ this adds
   790                                  		        	; / 200 to current character
   791                                  	;mov	byte [escap], 0
   792                                  	;mov	word [escap], 0
   793                                  		; clr escap / clear, so escap normally zero
   794 00000472 3C5C                    	cmp	al, '\'
   795                                  		; cmp r0,$'\\ / note that \\ is equal \ in as
   796 00000474 740C                    	je	short gch2
   797                                  		; beq 1f
   798 00000476 C605[320C0000]00        	mov	byte [escap], 0
   799 0000047D 3C24                    	cmp	al, '$'
   800                                  		; cmp r0,$'$ / is it $
   801 0000047F 7429                    	je	short gch5
   802                                  		; beq 3f / yes
   803 00000481 C3                      	retn
   804                                  		; rts pc / no
   805                                  gch2: ;1:
   806 00000482 C605[320C0000]80                mov     byte [escap], 80h
   807                                  	;mov	word [escap], 128
   808                                  		; mov $200,escap / mark presence of \ in command line
   809 00000489 EBC9                    	jmp	short s8
   810                                  	;jmp	short gch0
   811                                  		; br getc / get next character
   812                                  gch3: ;2:
   813 0000048B 8B1D[4A0A0000]          	mov	ebx, [param]
   814 00000491 8A03                    	mov	al, [ebx]
   815                                  		; movb *param,r0 / pick up substitution character
   816                                  				; / put in r0
   817 00000493 08C0                    	or	al, al
   818 00000495 7407                    	jz	short gch4
   819                                  		; beq 1f / if end of substitution arg, branch
   820 00000497 FF05[4A0A0000]          	inc	dword [param]
   821                                  		; inc param / if not end, set for next character
   822 0000049D C3                      	retn
   823                                  		; rts pc / return as though character in ro is normal
   824                                  		       ; / input
   825                                  gch4: ;1:
   826 0000049E C705[4A0A0000]0000-     	mov	dword [param], 0
   826 000004A6 0000               
   827                                  		; clr param / unset substitution pointer
   828 000004A8 EBA4                    	jmp	short gch0
   829                                  		; br getc / get next char in normal input
   830                                  gch5: ;3:
   831 000004AA E89FFFFFFF              	call	gch0
   832                                  	;call	getc
   833                                  		; jsr pc,getc / get digit after $
   834 000004AF 2C30                    	sub	al, '0'
   835                                  		; sub $'0,r0 / strip off zone bits
   836 000004B1 3C09                    	cmp	al, 9
   837                                  		; cmp r0,$9. / compare with digit 9
   838 000004B3 7602                    	jna	short gch6 
   839                                  		; blos 1f / less than or equal 9
   840 000004B5 B009                    	mov	al, 9
   841                                  		; mov $9.,r0 / if larger than 9, force 9
   842                                  gch6: ;1:
   843 000004B7 8B1D[2E0C0000]          	mov	ebx, [shellarg]
   844                                  		; mov shellarg,r1 / get pointer to stack for
   845                                  		           ; / this call of shell
   846 000004BD 0FB6C0                  	movzx 	eax, al	; al->eax
   847 000004C0 FEC0                    	inc	al
   848                                  	;inc	eax
   849                                  		; inc r0 / digit +1
   850 000004C2 3B03                    	cmp	eax, [ebx]
   851                                  		; cmp r0,(r1) / is it less than # of args 
   852                                  			      ;	in this call
   853 000004C4 7388                    	jnb	short gch0
   854                                  		; bge getc / no, ignore it. so this $n is not replaced
   855 000004C6 C0E002                  	shl	al, 2 ; multiply by 4 (24/08/2015)
   856                                  	;shl	al, 1
   857                                  	;;shl	ax, 1
   858                                  		; asl r0 / yes, multiply by 2 (to skip words)
   859 000004C9 01C3                    	add	ebx, eax
   860                                  		; add r1,r0 / form pointer to arg pointer (-2)
   861 000004CB 8B4304                  	mov	eax, [ebx+4]
   862 000004CE A3[4A0A0000]            	mov	[param], eax
   863                                  		; mov 2(r0),param / move arg pointer to param
   864 000004D3 E96DFFFFFF                      jmp     getc
   865                                  		; br getc / go to get substitution arg for $n
   866                                  getbuf:
   867 000004D8 B9[240B0000]            	mov	ecx, inbuf
   868                                  		; mov $inbuf,r0 / move input buffer address
   869 000004DD 890D[240C0000]                  mov     [inbufp], ecx
   870                                  		; mov r0,inbufp / to input buffer pointer
   871 000004E3 890D[280C0000]          	mov	[einbuf], ecx
   872                                  		; mov r0,einbuf / and initialize pointer to end of
   873                                  		         ; / character string
   874 000004E9 49                      	dec	ecx
   875                                  		; dec r0 / decrement pointer so can utilize normal
   876                                  		       ; / 100p starting at 1f
   877                                  		; mov r0,0f / initialize address for reading 1st char
   878 000004EA BA01000000              	mov	edx, 1
   879                                  gbuf0: ;1:
   880 000004EF 41                      	inc	ecx
   881                                  		; inc 0f / this routine filles inbuf with line from
   882                                  		       ; / console - if there is cnc
   883 000004F0 51                      	push	ecx
   884                                  	; edx = 1
   885                                  	sys	_read, 0, och
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 000004F1 BB00000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 000004F6 B9[2C0C0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000004FB B803000000          <1>  mov eax, %1
    99 00000500 CD30                <1>  int 30h
   886 00000502 59                      	pop	ecx
   887                                  	;xor	ebx, ebx ; 0
   888                                  	;sys	_read ; sys _read, ebx, ecx, edx ; ebx = 0, edx = 1
   889                                  		; sys read; 0:0; 1 / read next char into inbuf
   890 00000503 7254                            jc	short xit1 ; 29/05/2022
   891                                  		; bcs xit1 / error exit
   892 00000505 21C0                    	and	eax, eax
   893                                  		; tst r0 / a zero input is end of file
   894 00000507 7450                            jz	short xit1 ; 29/05/2022
   895                                  		; beq xit1 / exit
   896 00000509 FF05[280C0000]          	inc	dword [einbuf]  ; 08/04/2014 (24/08/2015, 32 bit)
   897 0000050F A0[2C0C0000]            	mov	al, [och]
   898 00000514 803D[35060000]00        	cmp	byte [_at], 0
   899 0000051B 7608                    	jna	short gbuf1
   900 0000051D 3C08                    	cmp	al, 8 ; backspace
   901 0000051F 746B                    	je	short gbuf3
   902 00000521 3C7F                    	cmp	al, 127 ; delete
   903 00000523 7460                    	je	short gbuf6 ; 06/12/2013
   904                                  gbuf1:
   905                                  	;mov	ebx, ecx
   906                                  	;inc	dword [einbuf]
   907                                  		; inc einbuf / eventually einbuf points to \n
   908                                  		       ; / (+1) of this line
   909 00000525 81F9[240C0000]          	cmp	ecx, inbuf + 256
   910                                  		; cmp 0b,$inbuf+256. / have we exceeded 
   911                                  				   ; input buffer size
   912 0000052B 732C                            jnb	short xit1 ; 29/05/2022
   913                                  		; bhis xit1 / if so, exit assume some sort of binary
   914                                  	; 08/04/2014
   915 0000052D 3C0D                    	cmp	al, 0Dh
   916 0000052F 752F                    	jne	short gbuf8
   917 00000531 8B1D[280C0000]          	mov	ebx, [einbuf]
   918 00000537 4B                      	dec	ebx
   919 00000538 8803                    	mov	[ebx], al
   920 0000053A C3                      	retn
   921                                  
   922                                  gbuf2:
   923                                  	 ; 28/12/2015
   924 0000053B 803D[35060000]00        	cmp	byte [_at], 0
   925 00000542 76AB                    	jna	short gbuf0 ; 29/05/2022
   926                                  gbuf7:
   927 00000544 51                      	push	ecx
   928                                  	;mov	[och], al
   929                                  	; edx = 1
   930                                  	sys	_write, 1, och
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89 00000545 BB01000000          <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91 0000054A B9[2C0C0000]        <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000054F B804000000          <1>  mov eax, %1
    99 00000554 CD30                <1>  int 30h
   931                                  	;sys	_write, 1, och, 1  ; echo (write char on tty)
   932 00000556 59                      	pop	ecx
   933 00000557 EB96                    	jmp	short gbuf0 ; 29/05/2022
   934                                  
   935                                  xit1:	; 29/05/2022
   936                                  	sys	_exit
    85                              <1> 
    86                              <1> 
    87                              <1> 
    88                              <1>  %if %0 >= 2
    89                              <1>  mov ebx, %2
    90                              <1>  %if %0 >= 3
    91                              <1>  mov ecx, %3
    92                              <1> 
    93                              <1>  %if %0 >= 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000559 B801000000          <1>  mov eax, %1
    99 0000055E CD30                <1>  int 30h
   937                                  		; sys exit
   938                                  
   939                                  gbuf8:
   940 00000560 89CB                    	mov	ebx, ecx
   941 00000562 8803                    	mov	[ebx], al
   942                                  	;cmp	al, 0Ah ; \n
   943                                  		; cmpb *0b,$'\n / end of line?
   944                                  	;je	short gbuf5
   945                                  	;jne	short gbuf1
   946                                  		; bne 1b / no, go to get next char
   947                                  	;cmp	al, 0Dh ; ENTER
   948                                  	;je	short gbuf5
   949 00000564 803D[35060000]00        	cmp	byte [_at], 0 ; at > 0 --> tty input
   950 0000056B 7682                    	jna	short gbuf0
   951 0000056D 3C1B                    	cmp	al, 1Bh	; ESC
   952 0000056F 75CA                    	jne	short gbuf2
   953 00000571 B8[240B0000]            	mov	eax, inbuf
   954 00000576 A3[240C0000]            	mov	[inbufp], eax
   955 0000057B A3[280C0000]            	mov	[einbuf], eax
   956 00000580 E90BFBFFFF                      jmp     nl  ; cancel current command, new line
   957                                  
   958                                  	; 29/05/2022
   959                                  ;gbuf2:
   960                                  ;	 ; 28/12/2015
   961                                  ;	cmp	byte [_at], 0
   962                                  ;	jna	gbuf0
   963                                  ;gbuf7:
   964                                  ;	push	ecx
   965                                  ;	;mov	[och], al
   966                                  ;	; edx = 1
   967                                  ;	sys	_write, 1, och
   968                                  ;	;sys	_write, 1, och, 1  ; echo (write char on tty)
   969                                  ;	pop	ecx
   970                                  ;	jmp	gbuf0
   971                                  
   972                                  gbuf6: ; DELETE key -> BACKSPACE key
   973                                  	;mov 	al, 8
   974 00000585 C605[2C0C0000]08        	mov	byte [och], 8 ; 06/12/2013
   975                                  gbuf3:
   976                                  	; 08/04/2014
   977 0000058C FF0D[280C0000]          	dec	dword [einbuf] ; (24/08/2015, 32 bit code)
   978                                  	; 12/12/2013
   979 00000592 49                      	dec	ecx
   980 00000593 81F9[240B0000]          	cmp	ecx, inbuf
   981 00000599 7203                    	jb	short gbuf4
   982 0000059B 49                      	dec 	ecx
   983                                  	; 08/04/2014
   984                                  	;jmp	short gbuf2
   985 0000059C EBA6                    	jmp 	short gbuf7
   986                                  gbuf4:
   987                                  	;mov 	al, 7
   988 0000059E C605[2C0C0000]07        	mov	byte [och], 07h ; beep
   989                                  	; 08/04/2014
   990                                  	;jmp	short gbuf2
   991 000005A5 EB9D                    	jmp 	short gbuf7
   992                                  ;gbuf5:
   993                                  ;	retn
   994                                  		; rts pc / yes, return
   995                                  
   996                                  	; 29/05/2022
   997                                  ;xit1:
   998                                  ;	sys	_exit
   999                                  ;		; sys exit
  1000                                  
  1001                                  ;-----------------------------------------------------------------
  1002                                  ;  data - initialized data
  1003                                  ;-----------------------------------------------------------------
  1004                                  
  1005                                  ;  /// Messages
  1006                                  
  1007 000005A7 0D0A                    msg_unix_sh:	db 0Dh, 0Ah
  1008 000005A9 526574726F20556E69-     		db 'Retro Unix 386 v1.2 - shell'
  1008 000005B2 78203338362076312E-
  1008 000005BB 32202D207368656C6C 
  1009                                  		;db 0Dh, 0Ah
  1010                                  msgsh_size equ  $ - msg_unix_sh
  1011                                  		;db 0
  1012 000005C4 31332F30362F323032-     		db '13/06/2022'
  1012 000005CD 32                 
  1013 000005CE 0D0A00                  nextline:	db 0Dh, 0Ah, 0
  1014                                  ;Error messages:
  1015 000005D1 496E707574206E6F74-     msgNotFound: 	db 'Input not found', 0
  1015 000005DA 20666F756E6400     
  1016 000005E1 41726720636F756E74-     msgArgCount: 	db 'Arg count',  0
  1016 000005EA 00                 
  1017 000005EB 426164206469726563-     msgBadDir: 	db 'Bad directory', 0
  1017 000005F4 746F727900         
  1018 000005F9 54727920616761696E-     msgTryAgain: 	db 'Try again', 0
  1018 00000602 00                 
  1019 00000603 222720696D62616C61-     msgImbalance:   db 22h, 27h, 20h, 'imbalance',  0
  1019 0000060C 6E636500           
  1020 00000610 496E7075742066696C-     msgInputFile: 	db 'Input file', 0
  1020 00000619 6500               
  1021 0000061B 4F7574707574206669-     msgOutputFile: 	db 'Output file', 0
  1021 00000624 6C6500             
  1022 00000627 4E6F20636F6D6D616E-     msgNoCmd: 	db 'No command',  0
  1022 00000630 6400               
  1023                                  
  1024                                  ; /// Commands, files, parameters
  1025                                  
  1026                                  ; 27/12/2015
  1027                                  FCAT:	; '>>' 
  1028 00000632 00                      	db 0
  1029                                  
  1030                                  ;quest:
  1031                                  	;db '?', 0Dh, 0Ah
  1032                                  	;<?\n>
  1033                                  
  1034                                  prompt:
  1035 00000633 0D0A                    	db 0Dh, 0Ah
  1036                                  _at:
  1037 00000635 4020                    	db '@ '
  1038                                  	;<@ >
  1039                                  p_size  equ $ - prompt
  1040                                  
  1041 00000637 6563686F00              qecho: 	db 'echo', 0
  1042                                  ;
  1043 0000063C 636400                  qcd:	db 'cd', 0
  1044                                  ;
  1045                                  qchdir:
  1046 0000063F 636864697200            	db 'chdir', 0
  1047                                  	;<chdir\0>
  1048                                  glogin:
  1049 00000645 6C6F67696E00            	db 'login', 0
  1050                                  	;<login\0>
  1051                                  shell:
  1052 0000064B 2F62696E2F736800        	db '/bin/sh', 0
  1053                                  	;</bin/sh\0>
  1054                                  glob:
  1055 00000653 2F6574632F676C6F62-     	db '/etc/glob', 0
  1055 0000065C 00                 
  1056                                  	;</etc/glob\0>
  1057                                  binpb:
  1058 0000065D 2F62696E2F              	db '/bin/'
  1059                                  	;</bin/>
  1060                                  
  1061                                  ;-----------------------------------------------------------------
  1062                                  ;  bss - uninitialized data
  1063                                  ;-----------------------------------------------------------------
  1064                                  
  1065                                  bss_start:
  1066                                  
  1067                                  ABSOLUTE bss_start
  1068                                  
  1069                                  parbuf:
  1070 00000662 <res 3E8h>                      resb 1000
  1071                                   	; .=.+1000.
  1072                                  alignb 2
  1073                                  	;.even
  1074                                  param:
  1075 00000A4A ????????                	resd 1
  1076                                  	;.=.+2
  1077                                  glflag:
  1078 00000A4E ??                      	resb 1
  1079 00000A4F ??                      	resb 1
  1080                                  	;.=.+2
  1081                                  infile:
  1082 00000A50 ????????                	resd 1
  1083                                  	; .=.+2 
  1084                                  outfile:
  1085 00000A54 ????????                	resd 1
  1086                                  	;.=.+2
  1087                                  ; parp-4
  1088 00000A58 ????????                	resd 1
  1089                                  	;.=.+2 / room for glob
  1090                                  parp:
  1091 00000A5C <res C8h>                       resb 200
  1092                                  	;.=.+200.
  1093                                  inbuf:
  1094 00000B24 <res 100h>                      resb 256
  1095                                  	;.=.+256.
  1096                                  ;escap:
  1097                                  	;resw 1
  1098                                  	;.=.+2
  1099                                  inbufp:
  1100 00000C24 ????????                	resd 1
  1101                                  	;.=.+2
  1102                                  einbuf:
  1103 00000C28 ????????                	resd 1
  1104                                  	;.=.+2
  1105                                  och:
  1106 00000C2C ??                      	resb 1
  1107 00000C2D ??                      	resb 1
  1108                                  	;.=.+2
  1109                                  shellarg:
  1110 00000C2E ????????                	resd 1
  1111                                  	;.=.+2
  1112                                  escap:
  1113 00000C32 ??                      	resb 1
  1114                                  	;
  1115 00000C33 ??                      	resb 1
  1116                                  
  1117                                  bss_end:
  1118                                  
  1119                                  ; 29/05/2022
  1120                                  ;-----------------------------------------------------------------
  1121                                  ; Original UNIX v2 - shell - PDP-11 assembly source code (sh.s)
  1122                                  ;-----------------------------------------------------------------
  1123                                  ; UNIX V2 source code: see www.tuhs.org for details.
  1124                                  ;-----------------------------------------------------------------
  1125                                  ; /// Modified unix v1 shell (sh.s) source code by Erdogan Tan /// 
  1126                                  ;-----------------------------------------------------------------
  1127                                  ; s2.tar.gz - \bin\sh 
  1128                                  ;
  1129                                  ; Source code modified via disassembled unix v2 /bin/sh binary file
  1130                                  ; Dissasembler: Hex-Rays Interactive Disassembler (IDA)
  1131                                  ;
  1132                                  ;/ sh -- command interpreter
  1133                                  ;	mov	sp,r5
  1134                                  ;	mov	r5,shellarg / save orig sp in shellarg
  1135                                  ;	cmpb	*2(r5),$'- / was this sh called by init or login
  1136                                  ;	bne	2f / no
  1137                                  ;	sys	intr; 0 / yes, turn off interrupts
  1138                                  ;	sys	quit; 0
  1139                                  ;2:
  1140                                  ;	sys	getuid / who is user
  1141                                  ;	tst	r0 / is it superuser
  1142                                  ;	bne	2f / no
  1143                                  ;	movb	$'#,at / yes, set new prompt symbol
  1144                                  ;2:
  1145                                  ;	cmp	(r5),$1 / tty input?
  1146                                  ;	ble	newline / yes, call with '-(or with no command
  1147                                  ;		        / file name)
  1148                                  ;	clr	r0 / no, set ttv
  1149                                  ;	sys	close / close it
  1150                                  ;	mov	4(r5),0f / get new file name
  1151                                  ;	sys	open; 0:..; 0 / open it
  1152                                  ;	bec	1f / branch if no error
  1153                                  ;	jsr	r5,error / error in file name
  1154                                  ;		<Input not found\n\0>; .even
  1155                                  ;	sys	exit
  1156                                  ;1:
  1157                                  ;	clr	at / clear prompt character, if reading non-tty
  1158                                  ;		   / input file
  1159                                  ;newline:
  1160                                  ;	tst	at / is there a prompt symbol
  1161                                  ;	beq	newcom / no
  1162                                  ;	mov	$1,r0 / yes
  1163                                  ;	sys	write; at; 2. / print prompt
  1164                                  ;newcom:
  1165                                  ;	mov	shellarg,sp /
  1166                                  ;	mov	$parbuf,r3 / initialize command list area
  1167                                  ;	mov	$parp,r4 / initialize command list pointers
  1168                                  ;	clr	infile / initialize alternate input
  1169                                  ;	clr	outfile / initialize alternate output
  1170                                  ;	clr	glflag / initialize global flag
  1171                                  ;newarg:
  1172                                  ;	jsr	pc,blank / squeeze out leading blanks
  1173                                  ;	jsr	r5,delim / is new character a ; \n or &
  1174                                  ;		br 2f / yes
  1175                                  ;	mov	r3,-(sp) / no, push arg pointer onto stack
  1176                                  ;	cmp	r0,$'< / new input file?
  1177                                  ;	bne	1f / no
  1178                                  ;	mov	(sp),infile / yes, save arg pointer
  1179                                  ;	clr	(sp) / clear pointer
  1180                                  ;	br	3f
  1181                                  ;1:
  1182                                  ;	cmp	r0,$'> / new output file?
  1183                                  ;	bne	newchar / no
  1184                                  ;	mov	(sp),outfile / yes, save arg pointer
  1185                                  ;	clr	(sp) / clear pointer
  1186                                  ;	br	3f
  1187                                  ;newchar:
  1188                                  ;	cmp	$' ,r0 / is character a blank
  1189                                  ;	beq	1f / branch if it is (blank as arg separator)
  1190                                  ;	cmp	$'\n+200,r0 / treat \n preceded by ;	beq	1f / as blank
  1192                                  ;	jsr	pc,putc / put this character in parbuf list
  1193                                  ;3:
  1194                                  ;	jsr	pc,getc / get next character
  1195                                  ;	jsr	r5,delim / is char a ; \n or &,
  1196                                  ;		br 1f / yes
  1197                                  ;	br	newchar / no, start new character tests
  1198                                  ;1:
  1199                                  ;	clrb	(r3)+ / end name with \0 when read blank, or
  1200                                  ;		      / delim
  1201                                  ;	mov	(sp)+,(r4)+ / move arg ptr to parp location
  1202                                  ;	bne	1f / if (sp)=0, in file or out file points to arg
  1203                                  ;	tst	-(r4) / so ignore dummy (0), in pointer list
  1204                                  ;1:
  1205                                  ;	jsr	r5,delim / is char a ; \n or &.
  1206                                  ;		br 2f / yes
  1207                                  ;	br	newarg / no, start newarg processing
  1208                                  ;2:
  1209                                  ;	clr	(r4) / \n, &, or ; takes to here (end of arg list)
  1210                                  ;		     / after 'delim' call
  1211                                  ;	mov	r0,-(sp) / save delimiter in stack
  1212                                  ;	jsr	pc,docom / go to exec command in parbuf
  1213                                  ;	cmpb	(sp),$'& / get a new command without wait?
  1214                                  ;	beq	newcom / yes
  1215                                  ;	tst	r1 / was chdir just executed or line ended with
  1216                                  ;		   / ampersand?
  1217                                  ;	beq	2f / yes
  1218                                  ;1:
  1219                                  ;	sys	wait / no, wait for new process to terminate
  1220                                  ;		     / command executed)
  1221                                  ;	bcs	2f / no, children not previously waited for
  1222                                  ;	cmp	r0,r1 / is this my child
  1223                                  ;	bne	1b
  1224                                  ;2:
  1225                                  ;	cmp	(sp),$'\n / was delimiter a new line
  1226                                  ;	beq	newline / yes
  1227                                  ;	br	newcom / no, pick up next command
  1228                                  ;docom:
  1229                                  ;	sub	$parp,r4 / out arg count in r4
  1230                                  ;	bne	1f / any arguments?
  1231                                  ;	clr	r1 / no, line ended with ampersand
  1232                                  ;	rts	pc / return from call
  1233                                  ;1:
  1234                                  ;	jsr	r5,chcom; qchdir / is command chdir?
  1235                                  ;		br 2f / command not chdir
  1236                                  ;	cmp	r4,$4 / prepare to exec chdir, 4=arg count x 2
  1237                                  ;	beq	3f
  1238                                  ;	jsr	r5,error / go to print error
  1239                                  ;		<Arg count\n\0>; .even
  1240                                  ;	br	4f
  1241                                  ;3:
  1242                                  ;	mov	parp+2,0f / more directory name to sys coll
  1243                                  ;	sys	chdir; 0:0 / exec chdir
  1244                                  ;	bec	4f / no error exit
  1245                                  ;	jsr	r5,error / go to print error
  1246                                  ;		<Bad directory\n\0>; .even / this diagnostic
  1247                                  ;4:
  1248                                  ;	clr	r1 / set r1 to zero to dkip wait
  1249                                  ;	rts	pc / and return
  1250                                  ;2:
  1251                                  ;	jsr	r5,chcom; glogin / is command login?
  1252                                  ;		br 2f / not loqin, go to fork
  1253                                  ;	sys	exec; parbuf; parp / exec login
  1254                                  ;	sys	exec; binpb; parp / or /bin/login
  1255                                  ;2: / no error return??
  1256                                  ;	sys	fork / generate sh child process for command
  1257                                  ;		br newproc / exec command with new process
  1258                                  ;	bec	1f / no error exit, old orocess
  1259                                  ;	jsr	r5,error / go to print error
  1260                                  ;		<Try again\n\0>; .even / this diaonostic
  1261                                  ;	jmp	newline / and return for next try
  1262                                  ;1:
  1263                                  ;	mov	r0,r1 / save id of child sh
  1264                                  ;	rts	pc / return to "jsr pc, docom" call in parent sh
  1265                                  ;
  1266                                  ;error:
  1267                                  ;	movb	(r5)+,och / pick up diagnostic character
  1268                                  ;	beq	1f / 0 is end of line
  1269                                  ;	mov	$1,r0 / set for tty output
  1270                                  ;	sys	write; och; 1 / print it
  1271                                  ;	br	error / continue to get characters
  1272                                  ;1:
  1273                                  ;	inc	r5 / inc r5 to point to return
  1274                                  ;	bic	$1,r5 / make it even
  1275                                  ;	clr	r0 / set for input
  1276                                  ;	sys	seek; 0; 2 / exit from runcom. skip to end of
  1277                                  ;		           / input file
  1278                                  ;
  1279                                  ;/------ 29/05/2022 - unix v2 shell
  1280                                  ;/	 (source code from disassembled /bin/sh binary)
  1281                                  ;	rts	r5
  1282                                  ;
  1283                                  ;chcom: / has no effect if tty input
  1284                                  ;	mov	(r5)+,r1 / glogin gchdir r1, bump r5
  1285                                  ;	mov	$parbuf,r2 / command address  r2 'login'
  1286                                  ;1:
  1287                                  ;	movb	 (r1)+,r0 / is this command 'chdir'
  1288                                  ;	cmpb	(r2)+,r0 / compare command name byte with 'login'
  1289                                  ;		         / or 'chdir'
  1290                                  ;	bne	1f / doesn't compare
  1291                                  ;	tst	r0 / is this
  1292                                  ;	bne	1b / end of names
  1293                                  ;	tst	(r5)+ / yes, bump r5 again to execute login
  1294                                  ;		      / chdir
  1295                                  ;1:
  1296                                  ;	rts	r5 / no, return to exec command
  1297                                  ;
  1298                                  ;putc:
  1299                                  ;	cmp	r0,$'' / single quote?
  1300                                  ;	beq	1f / yes
  1301                                  ;	cmp	r0,$'" / double quote
  1302                                  ;	beq	1f / yes
  1303                                  ;	bic	$!177,r0 / no, remove 200, if present
  1304                                  ;	movb	r0,(r3)+ / store character in parbuf
  1305                                  ;	rts	pc
  1306                                  ;1:
  1307                                  ;	mov	r0,-(sp) / push quote mark onto stack
  1308                                  ;1:
  1309                                  ;	jsr	pc,getc / get a quoted character
  1310                                  ;	cmp	r0,$'\n / is it end or line
  1311                                  ;	bne	2f / no
  1312                                  ;	jsr	r5,error / yes, indicate missing quote mark
  1313                                  ;		<"' imbalance\n\0>; .even
  1314                                  ;	jmp	newline / ask for new line
  1315                                  ;2:
  1316                                  ;	cmp	r0,(sp) / is this closing quote mark
  1317                                  ;	beq	1f / yes
  1318                                  ;	bic	$!177,r0 / no, strip off 200 if present
  1319                                  ;	movb	r0,(r3)+ / store quoted character in parbuf
  1320                                  ;	br	1b / continue
  1321                                  ;1:
  1322                                  ;	tst	(sp)+ / pop quote mark off stack
  1323                                  ;	rts	pc / return
  1324                                  ;
  1325                                  ;/ thp`e new process
  1326                                  ;
  1327                                  ;newproc:
  1328                                  ;	mov	infile,0f / move pointer to new file name
  1329                                  ;	beq	1f / branch if no alternate read file given
  1330                                  ;	tstb	*0f
  1331                                  ;	beq	3f / branch if no file name miven
  1332                                  ;	clr	r0 / set tty input file name
  1333                                  ;	sys	close / close it
  1334                                  ;	sys	open; 0:..; 0 / open new input file for reading
  1335                                  ;	bcc	1f / branch if input file ok
  1336                                  ;3:
  1337                                  ;	jsr	r5,error / file not ok, print error
  1338                                  ;		<Input file\n\0>; .even / this diagnostic
  1339                                  ;	sys	exit / terminate this process and make parent sh
  1340                                  ;1:
  1341                                  ;	mov	outfile,r2 / more pointer to new file name
  1342                                  ;	beq	1f / branch if no alternate write file
  1343                                  ;	cmpb	(r2),$'> / is > at beqinning of file name?
  1344                                  ;	bne	4f / branch if it isn't
  1345                                  ;	inc	r2 / yes, increment pointer
  1346                                  ;	mov	r2,0f
  1347                                  ;	sys	open; 0:..; 1 / open file for writing
  1348                                  ;	bec	3f / if no error
  1349                                  ;4:
  1350                                  ;	mov	r2,0f
  1351                                  ;	sys	creat; 0:..; 17 / create new file with this name
  1352                                  ;	bec	3f / branch if no error
  1353                                  ;2:
  1354                                  ;	jsr	r5,error
  1355                                  ;		<Output file\n\0>; .even
  1356                                  ;	sys	exit
  1357                                  ;3:
  1358                                  ;	sys	close / close the new write file
  1359                                  ;	mov	r2,0f / move new name to open
  1360                                  ;	mov	$1,r0 / set ttv file name
  1361                                  ;	sys	close / close it
  1362                                  ;	sys	open; 0:..; 1 / open new output file, it now has
  1363                                  ;		              / file descriptor 1
  1364                                  ;	sys	seek; 0; 2 / set pointer to current end of file
  1365                                  ;1:
  1366                                  ;	tst	glflag / was *, ? or [ encountered?
  1367                                  ;	bne	1f / yes
  1368                                  ;	sys	exec; parbuf; parp / no, execute this commend
  1369                                  ;	sys	exec; binpb; parp / or /bin/this command
  1370                                  ;
  1371                                  ;/------ 29/05/2022 - unix v2 shell
  1372                                  ;/	 (source code from disassembled /bin/sh binary)
  1373                                  ;/	following part is not existing in v2 shell
  1374                                  ;
  1375                                  ;/2:
  1376                                  ;/	sys	stat; binpb; inbuf / if can't execute does it
  1377                                  ;/		                   / exist?
  1378                                  ;/	bes	2f / branch if it doesn't
  1379                                  ;/	mov	$shell,parp-2 / does exist, not executable
  1380                                  ;/	mov	$binpb,parp / so it must be
  1381                                  ;/	sys	exec; shell; parp-2 / a command file, get it with
  1382                                  ;/		                    / sh /bin/x (if x name of file)
  1383                                  ;/------
  1384                                  ;2:
  1385                                  ;	jsr	r5,error / a return for exec is the diagnostic
  1386                                  ;		<No command\n\0>; .even
  1387                                  ;	sys	exit
  1388                                  ;1:
  1389                                  ;	mov	$glob,parp-2 / prepare to process *,?
  1390                                  ;	sys	exec; glob; parp-2 / execute modified command
  1391                                  ;	br	2b
  1392                                  ;
  1393                                  ;delim:
  1394                                  ;	cmp	r0,$'\n / is character a newline
  1395                                  ;	beq	1f
  1396                                  ;	cmp	r0,$'& / is it &
  1397                                  ;	beq	1f / yes
  1398                                  ;	cmp	r0,$'; / is it ;
  1399                                  ;	beq	1f / yes
  1400                                  ;	cmp	r0,$'? / is it ?
  1401                                  ;	beq	3f
  1402                                  ;
  1403                                  ;/------ 29/05/2022 - unix v2 shell
  1404                                  ;/	 (source code from disassembled /bin/sh binary)
  1405                                  ;	cmp	r0,$'* / is it *
  1406                                  ;	beq	3f
  1407                                  ;
  1408                                  ;	cmp	r0,$'[ / is it beginning of character string
  1409                                  ;		       / (for glob)
  1410                                  ;	bne	2f
  1411                                  ;3:
  1412                                  ;	inc	glflag / ? or * or [ set flag
  1413                                  ;2:
  1414                                  ;	tst	(r5)+ / bump to process all except \n,;,&
  1415                                  ;1:
  1416                                  ;	rts	r5
  1417                                  ;
  1418                                  ;blank:
  1419                                  ;	jsr	pc,getc / get next character
  1420                                  ;	cmp	$' ,r0 / leading blanks
  1421                                  ;	beq	blank / yes, 'squeeze out'
  1422                                  ;	cmp	r0,$200+'\n / new-line preceded by \ is translated
  1423                                  ;	beq	blank / into blank
  1424                                  ;	rts	pc
  1425                                  ;getc:
  1426                                  ;	tst	param / are we substituting for $n
  1427                                  ;	bne	2f/ yes
  1428                                  ;	mov	inbufp,r1 / no, move normal input pointer to r1
  1429                                  ;	cmp	r1,einbuf / end of input line?
  1430                                  ;	bne	1f / no
  1431                                  ;	jsr	pc,getbuf / yes, put next console line in buffer
  1432                                  ;	br	getc
  1433                                  ;1:
  1434                                  ;	movb	(r1)+,r0 / move byte from input buffer to r0
  1435                                  ;	mov	r1,inbufp / increment routine
  1436                                  ;	bis	escap,r0 / if last character was \ this adds
  1437                                  ;		         / 200 to current character
  1438                                  ;	clr	escap / clear, so escap normally zero
  1439                                  ;	cmp	r0,$'\\ / note that \\ is equal \ in as
  1440                                  ;	beq	1f
  1441                                  ;	cmp	r0,$'$ / is it $
  1442                                  ;	beq	3f / yes
  1443                                  ;	rts	pc / no
  1444                                  ;1:
  1445                                  ;	mov	$200,escap / mark presence of \ in command line
  1446                                  ;	br	getc / get next character
  1447                                  ;2:
  1448                                  ;	movb	*param,r0 / pick up substitution character put in
  1449                                  ;		          / r0
  1450                                  ;	beq	1f / if end of substitution arg, branch
  1451                                  ;	inc	param / if not end, set for next character
  1452                                  ;	rts	pc / return as though character in ro is normal
  1453                                  ;		   / input
  1454                                  ;1:
  1455                                  ;	clr	param / unset substitution pointer
  1456                                  ;	br	getc / get next char in normal input
  1457                                  ;3:
  1458                                  ;	jsr	pc,getc / get digit after $
  1459                                  ;	sub	$'0,r0 / strip off zone bits
  1460                                  ;	cmp	r0,$9. / compare with digit 9 
  1461                                  ;	blos	1f / less than or equal 9
  1462                                  ;	mov	$9.,r0 / if larger than 9, force 9
  1463                                  ;1:
  1464                                  ;	mov	shellarg,r1 / get pointer to stack for
  1465                                  ;		            / this call of shell
  1466                                  ;	inc	r0 / digit +1
  1467                                  ;	cmp	r0,(r1) / is it less than # of args in this call
  1468                                  ;	bge	getc / no, ignore it. so this $n is not replaced
  1469                                  ;	asl	r0 / yes, multiply by 2 (to skip words)
  1470                                  ;	add	r1,r0 / form pointer to arg pointer (-2)
  1471                                  ;	mov	2(r0),param / move arg pointer to param
  1472                                  ;	br	getc / go to get substitution arg for $n
  1473                                  ;getbuf:
  1474                                  ;	mov	$inbuf,r0 / move input buffer address
  1475                                  ;	mov	r0,inbufp / to input buffer pointer
  1476                                  ;	mov	r0,einbuf / and initialize pointer to end of
  1477                                  ;		          / character string
  1478                                  ;	dec	r0 / decrement pointer so can utilize normal
  1479                                  ;		   / 100p starting at 1f
  1480                                  ;	mov	r0,0f / initialize address for reading 1st char
  1481                                  ;1:
  1482                                  ;	inc	0f / this routine filles inbuf with line from
  1483                                  ;		   / console - if there is cnc
  1484                                  ;	clr	r0 / set for tty input
  1485                                  ;	sys	read; 0:0; 1 / read next char into inbuf
  1486                                  ;	bcs	xit1 / error exit
  1487                                  ;	tst	r0 / a zero input is end of file
  1488                                  ;	beq	xit1 / exit
  1489                                  ;	inc	einbuf / eventually einbuf points to \n
  1490                                  ;		       / (+1) of this line
  1491                                  ;	cmp	0b,$inbuf+256. / have we exceeded input buffer size
  1492                                  ;	bhis	xit1 / if so, exit assume some sort of binary
  1493                                  ;	cmpb	*0b,$'\n / end of line?
  1494                                  ;	bne	1b / no, go to get next char
  1495                                  ;	rts	pc / yes, return
  1496                                  ;
  1497                                  ;xit1:
  1498                                  ;	sys	exit
  1499                                  ;
  1500                                  ;quest:
  1501                                  ;	<?\n>
  1502                                  ;
  1503                                  ;at:
  1504                                  ;	<@ >
  1505                                  ;
  1506                                  ;qchdir:
  1507                                  ;	<chdir\0>
  1508                                  ;glogin:
  1509                                  ;	<login\0>
  1510                                  ;shell:
  1511                                  ;	</bin/sh\0>
  1512                                  ;glob:
  1513                                  ;	</etc/glob\0>
  1514                                  ;binpb:
  1515                                  ;	</bin/>
  1516                                  ;parbuf: .=.+1000.
  1517                                  ;	.even
  1518                                  ;param:	.=.+2
  1519                                  ;glflag: .=.+2
  1520                                  ;infile: .=.+2 
  1521                                  ;outfile:.=.+2
  1522                                  ;	.=.+2 / room for glob
  1523                                  ;parp:	.=.+200.
  1524                                  ;inbuf:	.=.+256.
  1525                                  ;escap:	.=.+2
  1526                                  ;inbufp: .=.+2
  1527                                  ;einbuf: .=.+2
  1528                                  ;och:	.=.+2
  1529                                  ;shellarg:.=.+2
  1530                                  
  1531                                  ; 29/05/2022
  1532                                  ;-----------------------------------------------------------------
  1533                                  ; Original UNIX v1 - shell - PDP-11 assembly source code (sh.s)
  1534                                  ;-----------------------------------------------------------------
  1535                                  ; UNIX V1 source code: see www.tuhs.org for details.
  1536                                  ;-----------------------------------------------------------------
  1537                                  ; PreliminaryUnixImplementationDocument_Jun72.pdf - Section: E11
  1538                                  ;-----------------------------------------------------------------
  1539                                  ; https://minnie.tuhs.org/cgi-bin/utree.pl?file=V1/sh.s
  1540                                  ;
  1541                                  ;/ sh -- command interpreter
  1542                                  ;	mov	sp,r5
  1543                                  ;	mov	r5,shellarg / save orig sp in shellarg
  1544                                  ;	cmpb	*2(r5),$'- / was this sh called by init or login
  1545                                  ;	bne	2f / no
  1546                                  ;	sys	intr; 0 / yes, turn off interrupts
  1547                                  ;	sys	quit; 0
  1548                                  ;2:
  1549                                  ;	sys	getuid / who is user
  1550                                  ;	tst	r0 / is it superuser
  1551                                  ;	bne	2f / no
  1552                                  ;	movb	$'#,at / yes, set new prompt symbol
  1553                                  ;2:
  1554                                  ;	cmp	(r5),$1 / tty input?
  1555                                  ;	ble	newline / yes, call with '-(or with no command
  1556                                  ;		        / file name)
  1557                                  ;	clr	r0 / no, set ttv
  1558                                  ;	sys	close / close it
  1559                                  ;	mov	4(r5),0f / get new file name
  1560                                  ;	sys	open; 0:..; 0 / open it
  1561                                  ;	bec	1f / branch if no error
  1562                                  ;	jsr	r5,error / error in file name
  1563                                  ;		<Input not found\n\0>; .even
  1564                                  ;	sys	exit
  1565                                  ;1:
  1566                                  ;	clr	at / clear prompt character, if reading non-tty
  1567                                  ;		   / input file
  1568                                  ;newline:
  1569                                  ;	tst	at / is there a prompt symbol
  1570                                  ;	beq	newcom / no
  1571                                  ;	mov	$1,r0 / yes
  1572                                  ;	sys	write; at; 2. / print prompt
  1573                                  ;newcom:
  1574                                  ;	mov	shellarg,sp /
  1575                                  ;	mov	$parbuf,r3 / initialize command list area
  1576                                  ;	mov	$parp,r4 / initialize command list pointers
  1577                                  ;	clr	infile / initialize alternate input
  1578                                  ;	clr	outfile / initialize alternate output
  1579                                  ;	clr	glflag / initialize global flag
  1580                                  ;newarg:
  1581                                  ;	jsr	pc,blank / squeeze out leading blanks
  1582                                  ;	jsr	r5,delim / is new character a ; \n or &
  1583                                  ;		br 2f / yes
  1584                                  ;	mov	r3,-(sp) / no, push arg pointer onto stack
  1585                                  ;	cmp	r0,$'< / new input file?
  1586                                  ;	bne	1f / no
  1587                                  ;	mov	(sp),infile / yes, save arg pointer
  1588                                  ;	clr	(sp) / clear pointer
  1589                                  ;	br	3f
  1590                                  ;1:
  1591                                  ;	cmp	r0,$'> / new output file?
  1592                                  ;	bne	newchar / no
  1593                                  ;	mov	(sp),outfile / yes, save arg pointer
  1594                                  ;	clr	(sp) / clear pointer
  1595                                  ;	br	3f
  1596                                  ;newchar:
  1597                                  ;	cmp	$' ,r0 / is character a blank
  1598                                  ;	beq	1f / branch if it is (blank as arg separator)
  1599                                  ;	cmp	$'\n+200,r0 / treat \n preceded by ;	beq	1f / as blank
  1601                                  ;	jsr	pc,putc / put this character in parbuf list
  1602                                  ;3:
  1603                                  ;	jsr	pc,getc / get next character
  1604                                  ;	jsr	r5,delim / is char a ; \n or &,
  1605                                  ;		br 1f / yes
  1606                                  ;	br	newchar / no, start new character tests
  1607                                  ;1:
  1608                                  ;	clrb	(r3)+ / end name with \0 when read blank, or
  1609                                  ;		      / delim
  1610                                  ;	mov	(sp)+,(r4)+ / move arg ptr to parp location
  1611                                  ;	bne	1f / if (sp)=0, in file or out file points to arg
  1612                                  ;	tst	-(r4) / so ignore dummy (0), in pointer list
  1613                                  ;1:
  1614                                  ;	jsr	r5,delim / is char a ; \n or &.
  1615                                  ;		br 2f / yes
  1616                                  ;	br	newarg / no, start newarg processing
  1617                                  ;2:
  1618                                  ;	clr	(r4) / \n, &, or ; takes to here (end of arg list)
  1619                                  ;		     / after 'delim' call
  1620                                  ;	mov	r0,-(sp) / save delimiter in stack
  1621                                  ;	jsr	pc,docom / go to exec command in parbuf
  1622                                  ;	cmpb	(sp),$'& / get a new command without wait?
  1623                                  ;	beq	newcom / yes
  1624                                  ;	tst	r1 / was chdir just executed or line ended with
  1625                                  ;		   / ampersand?
  1626                                  ;	beq	2f / yes
  1627                                  ;1:
  1628                                  ;	sys	wait / no, wait for new process to terminate
  1629                                  ;		     / command executed)
  1630                                  ;	bcs	2f / no, children not previously waited for
  1631                                  ;	cmp	r0,r1 / is this my child
  1632                                  ;	bne	1b
  1633                                  ;2:
  1634                                  ;	cmp	(sp),$'\n / was delimiter a new line
  1635                                  ;	beq	newline / yes
  1636                                  ;	br	newcom / no, pick up next command
  1637                                  ;docom:
  1638                                  ;	sub	$parp,r4 / out arg count in r4
  1639                                  ;	bne	1f / any arguments?
  1640                                  ;	clr	r1 / no, line ended with ampersand
  1641                                  ;	rts	pc / return from call
  1642                                  ;1:
  1643                                  ;	jsr	r5,chcom; qchdir / is command chdir?
  1644                                  ;		br 2f / command not chdir
  1645                                  ;	cmp	r4,$4 / prepare to exec chdir, 4=arg count x 2
  1646                                  ;	beq	3f
  1647                                  ;	jsr	r5,error / go to print error
  1648                                  ;		<Arg count\n\0>; .even
  1649                                  ;	br	4f
  1650                                  ;3:
  1651                                  ;	mov	parp+2,0f / more directory name to sys coll
  1652                                  ;	sys	chdir; 0:0 / exec chdir
  1653                                  ;	bec	4f / no error exit
  1654                                  ;	jsr	r5,error / go to print error
  1655                                  ;		<Bad directory\n\0>; .even / this diagnostic
  1656                                  ;4:
  1657                                  ;	clr	r1 / set r1 to zero to dkip wait
  1658                                  ;	rts	pc / and return
  1659                                  ;2:
  1660                                  ;	jsr	r5,chcom; glogin / is command login?
  1661                                  ;		br 2f / not loqin, go to fork
  1662                                  ;	sys	exec; parbuf; parp / exec login
  1663                                  ;	sys	exec; binpb; parp / or /bin/login
  1664                                  ;2: / no error return??
  1665                                  ;	sys	fork / generate sh child process for command
  1666                                  ;		br newproc / exec command with new process
  1667                                  ;	bec	1f / no error exit, old orocess
  1668                                  ;	jsr	r5,error / go to print error
  1669                                  ;		<Try again\n\0>; .even / this diaonostic
  1670                                  ;	jmp	newline / and return for next try
  1671                                  ;1:
  1672                                  ;	mov	r0,r1 / save id of child sh
  1673                                  ;	rts	pc / return to "jsr pc, docom" call in parent sh
  1674                                  ;
  1675                                  ;error:
  1676                                  ;	movb	(r5)+,och / pick up diagnostic character
  1677                                  ;	beq	1f / 0 is end of line
  1678                                  ;	mov	$1,r0 / set for tty output
  1679                                  ;	sys	write; och; 1 / print it
  1680                                  ;	br	error / continue to get characters
  1681                                  ;1:
  1682                                  ;	inc	r5 / inc r5 to point to return
  1683                                  ;	bic	$1,r5 / make it even
  1684                                  ;	clr	r0 / set for input
  1685                                  ;	sys	seek; 0; 2 / exit from runcom. skip to end of
  1686                                  ;		           / input file
  1687                                  ;chcom: / has no effect if tty input
  1688                                  ;	mov	(r5)+,r1 / glogin gchdir r1, bump r5
  1689                                  ;	mov	$parbuf,r2 / command address  r2 'login'
  1690                                  ;1:
  1691                                  ;	movb	 (r1)+,r0 / is this command 'chdir'
  1692                                  ;	cmpb	(r2)+,r0 / compare command name byte with 'login'
  1693                                  ;		         / or 'chdir'
  1694                                  ;	bne	1f / doesn't compare
  1695                                  ;	tst	r0 / is this
  1696                                  ;	bne	1b / end of names
  1697                                  ;	tst	(r5)+ / yes, bump r5 again to execute login
  1698                                  ;		      / chdir
  1699                                  ;1:
  1700                                  ;	rts	r5 / no, return to exec command
  1701                                  ;
  1702                                  ;putc:
  1703                                  ;	cmp	r0,$'' / single quote?
  1704                                  ;	beq	1f / yes
  1705                                  ;	cmp	r0,$'" / double quote
  1706                                  ;	beq	1f / yes
  1707                                  ;	bic	$!177,r0 / no, remove 200, if present
  1708                                  ;	movb	r0,(r3)+ / store character in parbuf
  1709                                  ;	rts	pc
  1710                                  ;1:
  1711                                  ;	mov	r0,-(sp) / push quote mark onto stack
  1712                                  ;1:
  1713                                  ;	jsr	pc,getc / get a quoted character
  1714                                  ;	cmp	r0,$'\n / is it end or line
  1715                                  ;	bne	2f / no
  1716                                  ;	jsr	r5,error / yes, indicate missing quote mark
  1717                                  ;		<"' imbalance\n\0>; .even
  1718                                  ;	jmp	newline / ask for new line
  1719                                  ;2:
  1720                                  ;	cmp	r0,(sp) / is this closing quote mark
  1721                                  ;	beq	1f / yes
  1722                                  ;	bic	$!177,r0 / no, strip off 200 if present
  1723                                  ;	movb	r0,(r3)+ / store quoted character in parbuf
  1724                                  ;	br	1b / continue
  1725                                  ;1:
  1726                                  ;	tst	(sp)+ / pop quote mark off stack
  1727                                  ;	rts	pc / return
  1728                                  ;
  1729                                  ;/ thp`e new process
  1730                                  ;
  1731                                  ;newproc:
  1732                                  ;	mov	infile,0f / move pointer to new file name
  1733                                  ;	beq	1f / branch if no alternate read file given
  1734                                  ;	tstb	*0f
  1735                                  ;	beq	3f / branch if no file name miven
  1736                                  ;	clr	r0 / set tty input file name
  1737                                  ;	sys	close / close it
  1738                                  ;	sys	open; 0:..; 0 / open new input file for reading
  1739                                  ;	bcc	1f / branch if input file ok
  1740                                  ;3:
  1741                                  ;	jsr	r5,error / file not ok, print error
  1742                                  ;		<Input file\n\0>; .even / this diagnostic
  1743                                  ;	sys	exit / terminate this process and make parent sh
  1744                                  ;1:
  1745                                  ;	mov	outfile,r2 / more pointer to new file name
  1746                                  ;	beq	1f / branch if no alternate write file
  1747                                  ;	cmpb	(r2),$'> / is > at beqinning of file name?
  1748                                  ;	bne	4f / branch if it isn't
  1749                                  ;	inc	r2 / yes, increment pointer
  1750                                  ;	mov	r2,0f
  1751                                  ;	sys	open; 0:..; 1 / open file for writing
  1752                                  ;	bec	3f / if no error
  1753                                  ;4:
  1754                                  ;	mov	r2,0f
  1755                                  ;	sys	creat; 0:..; 17 / create new file with this name
  1756                                  ;	bec	3f / branch if no error
  1757                                  ;2:
  1758                                  ;	jsr	r5,error
  1759                                  ;		<Output file\n\0>; .even
  1760                                  ;	sys	exit
  1761                                  ;3:
  1762                                  ;	sys	close / close the new write file
  1763                                  ;	mov	r2,0f / move new name to open
  1764                                  ;	mov	$1,r0 / set ttv file name
  1765                                  ;	sys	close / close it
  1766                                  ;	sys	open; 0:..; 1 / open new output file, it now has
  1767                                  ;		              / file descriptor 1
  1768                                  ;	sys	seek; 0; 2 / set pointer to current end of file
  1769                                  ;1:
  1770                                  ;	tst	glflag / was *, ? or [ encountered?
  1771                                  ;	bne	1f / yes
  1772                                  ;	sys	exec; parbuf; parp / no, execute this commend
  1773                                  ;	sys	exec; binpb; parp / or /bin/this command
  1774                                  ;2:
  1775                                  ;	sys	stat; binpb; inbuf / if can't execute does it
  1776                                  ;		                   / exist?
  1777                                  ;	bes	2f / branch if it doesn't
  1778                                  ;	mov	$shell,parp-2 / does exist, not executable
  1779                                  ;	mov	$binpb,parp / so it must be
  1780                                  ;	sys	exec; shell; parp-2 / a command file, get it with
  1781                                  ;		                    / sh /bin/x (if x name of file)
  1782                                  ;2:
  1783                                  ;	jsr	r5,error / a return for exec is the diagnostic
  1784                                  ;		<No command\n\0>; .even
  1785                                  ;	sys	exit
  1786                                  ;1:
  1787                                  ;	mov	$glob,parp-2 / prepare to process *,?
  1788                                  ;	sys	exec; glob; parp-2 / execute modified command
  1789                                  ;	br	2b
  1790                                  ;
  1791                                  ;delim:
  1792                                  ;	cmp	r0,$'\n / is character a newline
  1793                                  ;	beq	1f
  1794                                  ;	cmp	r0,$'& / is it &
  1795                                  ;	beq	1f / yes
  1796                                  ;	cmp	r0,$'; / is it ;
  1797                                  ;	beq	1f / yes
  1798                                  ;	cmp	r0,$'? / is it ?
  1799                                  ;	beq	3f
  1800                                  ;	cmp	r0,$'[ / is it beginning of character string
  1801                                  ;		       / (for glob)
  1802                                  ;	bne	2f
  1803                                  ;3:
  1804                                  ;	inc	glflag / ? or * or [ set flag
  1805                                  ;2:
  1806                                  ;	tst	(r5)+ / bump to process all except \n,;,&
  1807                                  ;1:
  1808                                  ;	rts	r5
  1809                                  ;
  1810                                  ;blank:
  1811                                  ;	jsr	pc,getc / get next character
  1812                                  ;	cmp	$' ,r0 / leading blanks
  1813                                  ;	beq	blank / yes, 'squeeze out'
  1814                                  ;	cmp	r0,$200+'\n / new-line preceded by \ is translated
  1815                                  ;	beq	blank / into blank
  1816                                  ;	rts	pc
  1817                                  ;getc:
  1818                                  ;	tst	param / are we substituting for $n
  1819                                  ;	bne	2f/ yes
  1820                                  ;	mov	inbufp,r1 / no, move normal input pointer to r1
  1821                                  ;	cmp	r1,einbuf / end of input line?
  1822                                  ;	bne	1f / no
  1823                                  ;	jsr	pc,getbuf / yes, put next console line in buffer
  1824                                  ;	br	getc
  1825                                  ;1:
  1826                                  ;	movb	(r1)+,r0 / move byte from input buffer to r0
  1827                                  ;	mov	r1,inbufp / increment routine
  1828                                  ;	bis	escap,r0 / if last character was \ this adds
  1829                                  ;		         / 200 to current character
  1830                                  ;	clr	escap / clear, so escap normally zero
  1831                                  ;	cmp	r0,$'\\ / note that \\ is equal \ in as
  1832                                  ;	beq	1f
  1833                                  ;	cmp	r0,$'$ / is it $
  1834                                  ;	beq	3f / yes
  1835                                  ;	rts	pc / no
  1836                                  ;1:
  1837                                  ;	mov	$200,escap / mark presence of \ in command line
  1838                                  ;	br	getc / get next character
  1839                                  ;2:
  1840                                  ;	movb	*param,r0 / pick up substitution character put in
  1841                                  ;		          / r0
  1842                                  ;	beq	1f / if end of substitution arg, branch
  1843                                  ;	inc	param / if not end, set for next character
  1844                                  ;	rts	pc / return as though character in ro is normal
  1845                                  ;		   / input
  1846                                  ;1:
  1847                                  ;	clr	param / unset substitution pointer
  1848                                  ;	br	getc / get next char in normal input
  1849                                  ;3:
  1850                                  ;	jsr	pc,getc / get digit after $
  1851                                  ;	sub	$'0,r0 / strip off zone bits
  1852                                  ;	cmp	r0,$9. / compare with digit 9 
  1853                                  ;	blos	1f / less than or equal 9
  1854                                  ;	mov	$9.,r0 / if larger than 9, force 9
  1855                                  ;1:
  1856                                  ;	mov	shellarg,r1 / get pointer to stack for
  1857                                  ;		            / this call of shell
  1858                                  ;	inc	r0 / digit +1
  1859                                  ;	cmp	r0,(r1) / is it less than # of args in this call
  1860                                  ;	bge	getc / no, ignore it. so this $n is not replaced
  1861                                  ;	asl	r0 / yes, multiply by 2 (to skip words)
  1862                                  ;	add	r1,r0 / form pointer to arg pointer (-2)
  1863                                  ;	mov	2(r0),param / move arg pointer to param
  1864                                  ;	br	getc / go to get substitution arg for $n
  1865                                  ;getbuf:
  1866                                  ;	mov	$inbuf,r0 / move input buffer address
  1867                                  ;	mov	r0,inbufp / to input buffer pointer
  1868                                  ;	mov	r0,einbuf / and initialize pointer to end of
  1869                                  ;		          / character string
  1870                                  ;	dec	r0 / decrement pointer so can utilize normal
  1871                                  ;		   / 100p starting at 1f
  1872                                  ;	mov	r0,0f / initialize address for reading 1st char
  1873                                  ;1:
  1874                                  ;	inc	0f / this routine filles inbuf with line from
  1875                                  ;		   / console - if there is cnc
  1876                                  ;	clr	r0 / set for tty input
  1877                                  ;	sys	read; 0:0; 1 / read next char into inbuf
  1878                                  ;	bcs	xit1 / error exit
  1879                                  ;	tst	r0 / a zero input is end of file
  1880                                  ;	beq	xit1 / exit
  1881                                  ;	inc	einbuf / eventually einbuf points to \n
  1882                                  ;		       / (+1) of this line
  1883                                  ;	cmp	0b,$inbuf+256. / have we exceeded input buffer size
  1884                                  ;	bhis	xit1 / if so, exit assume some sort of binary
  1885                                  ;	cmpb	*0b,$'\n / end of line?
  1886                                  ;	bne	1b / no, go to get next char
  1887                                  ;	rts	pc / yes, return
  1888                                  ;
  1889                                  ;xit1:
  1890                                  ;	sys	exit
  1891                                  ;
  1892                                  ;quest:
  1893                                  ;	<?\n>
  1894                                  ;
  1895                                  ;at:
  1896                                  ;	<@ >
  1897                                  ;
  1898                                  ;qchdir:
  1899                                  ;	<chdir\0>
  1900                                  ;glogin:
  1901                                  ;	<login\0>
  1902                                  ;shell:
  1903                                  ;	</bin/sh\0>
  1904                                  ;glob:
  1905                                  ;	</etc/glob\0>
  1906                                  ;binpb:
  1907                                  ;	</bin/>
  1908                                  ;parbuf: .=.+1000.
  1909                                  ;	.even
  1910                                  ;param:	.=.+2
  1911                                  ;glflag: .=.+2
  1912                                  ;infile: .=.+2 
  1913                                  ;outfile:.=.+2
  1914                                  ;	.=.+2 / room for glob
  1915                                  ;parp:	.=.+200.
  1916                                  ;inbuf:	.=.+256.
  1917                                  ;escap:	.=.+2
  1918                                  ;inbufp: .=.+2
  1919                                  ;einbuf: .=.+2
  1920                                  ;och:	.=.+2
  1921                                  ;shellarg:.=.+2
