PSX: more work on the terminal emulator that runs in the W32 subsystem.
[reactos.git] / posix / apps / csrterm / csrterm.c
index 15862a3..aa34e96 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: csrterm.c,v 1.1 2002/03/17 22:15:39 ea Exp $
+/* $Id: csrterm.c,v 1.4 2002/04/10 21:12:41 ea Exp $
  *
  * PROJECT    : ReactOS Operating System / POSIX+ Environment Subsystem
  * DESCRIPTION: CSRTERM - A DEC VT-100 terminal emulator for the PSX subsystem
 /*** GLOBALS *********************************************************/
 
 PRIVATE LPCSTR MyName = "CSRTERM";
-PRIVATE CSRTERM_SESSION  Session;
+PRIVATE CSRTERM_SESSION  Session =
+{
+    0, //Identifier
+    {  //ServerPort
+        {0,0,NULL},
+        L"\\"PSX_NS_SUBSYSTEM_DIRECTORY_NAME"\\"PSX_NS_SESSIONAPI_PORT_NAME,
+        INVALID_HANDLE_VALUE
+    }
+};
 
 /*** PRIVATE FUNCTIONS ***********************************************/
 VOID STDCALL Debug_Print (LPCSTR Format, ...)
@@ -91,8 +99,8 @@ TRACE;
        TerminalRead.PsxHeader.Procedure = PSX_TERMINAL_INTERRUPT;
        /* Terminal I/O */
         TerminalRead.Size = Size;
-        RtlCopyMemory (TerminalRead.Buffer, Buffer, Size);
 #if 0
+        RtlCopyMemory (TerminalRead.Buffer, Buffer, Size);
         Status = NtRequestWaitReplyPort (
                        Session.ServerPort.Handle,
                        & TerminalRead
@@ -115,9 +123,21 @@ TRACE;
  *     Initialize our data for managing the control connection
  *     initiated by the PSXSS.EXE process.
  */
-PRIVATE NTSTATUS STDCALL ProcessConnectionRequest (PPSX_MAX_MESSAGE Request)
+PRIVATE NTSTATUS STDCALL ProcessConnectionRequest (PLPC_MAX_MESSAGE Request)
 {
+    PPSX_CONNECT_PORT_DATA ConnectData = (PPSX_CONNECT_PORT_DATA) & Request->Data;
+
 TRACE;
+    if (PSX_CONNECTION_TYPE_SERVER != ConnectData->ConnectionType)
+    {
+        
+        return STATUS_UNSUCCESSFUL;
+    }
+    if (PSX_LPC_PROTOCOL_VERSION != ConnectData->Version)
+    {
+        
+        return STATUS_UNSUCCESSFUL;
+    }
     Session.SsLinkIsActive = TRUE;
     return STATUS_SUCCESS;
 }
@@ -159,6 +179,7 @@ TRACE;
     while (TRUE)
     {
         Reply = NULL;
+        NullReply = FALSE;
         while (!NullReply)
         {
             Status = NtReplyWaitReceivePort (
@@ -175,7 +196,7 @@ TRACE;
             switch (RequestType)
             {
             case LPC_CONNECTION_REQUEST:
-                ProcessConnectionRequest (& Request);
+                ProcessConnectionRequest ((PLPC_MAX_MESSAGE) & Request);
                 NullReply = TRUE;
                 continue;
             case LPC_CLIENT_DIED:
@@ -219,6 +240,7 @@ PRIVATE NTSTATUS STDCALL CreateSessionObjects (DWORD Pid)
     NTSTATUS          Status;
     ULONG             Id = 0;
     OBJECT_ATTRIBUTES Oa;
+    LARGE_INTEGER     SectionSize =  {PSX_TERMINAL_SECTION_SIZE,0};
 
 TRACE;
 
@@ -240,6 +262,7 @@ TRACE;
         PSX_NS_SESSION_DIRECTORY_NAME,
         Pid
         );
+    OutputDebugStringW(Session.Port.NameBuffer);
     RtlInitUnicodeString (& Session.Port.Name, Session.Port.NameBuffer);
     InitializeObjectAttributes (& Oa, & Session.Port.Name, 0, NULL, NULL);
     Status = NtCreatePort (& Session.Port.Handle, & Oa, 0, 0, 0x10000);
@@ -276,55 +299,56 @@ TRACE;
         PSX_NS_SESSION_DIRECTORY_NAME,
         Pid
     );
+    OutputDebugStringW(Session.Section.NameBuffer);
     RtlInitUnicodeString (& Session.Section.Name, Session.Section.NameBuffer); 
     InitializeObjectAttributes (& Oa, & Session.Section.Name, 0, 0, 0);
     Status = NtCreateSection (
                 & Session.Section.Handle,
-                0, /* DesiredAccess */
+                SECTION_ALL_ACCESS, /* DesiredAccess */
                 & Oa,
-                NULL, /* SectionSize OPTIONAL */
+                & SectionSize,
                 PAGE_READWRITE, /* Protect 4 */
                 SEC_COMMIT, /* Attributes */
                 0 /* FileHandle: 0=pagefile.sys */
                 );
     if (!NT_SUCCESS(Status))
-       {
-               NtClose (Session.Port.Handle);
-               NtTerminateThread (Session.Port.Thread.Handle, Status);
-               RtlDeleteCriticalSection (& Session.Lock);
-               vtprintf ("%s: %s: NtCreateSection failed with %08x\n",
-                    MyName, __FUNCTION__, Status);
-               return Status;
-       }
-       Session.Section.BaseAddress = NULL;
-       Session.Section.ViewSize = 0;
-       Status = NtMapViewOfSection (
-                       Session.Section.Handle,
-                       NtCurrentProcess(),
-                       & Session.Section.BaseAddress,
-                       0, /* ZeroBits */
-                       0, /* Commitsize */
-                       0, /* SectionOffset */
-                       & Session.Section.ViewSize,
-                       ViewUnmap,
-                       0, /* AllocationType */
-                       PAGE_READWRITE /* Protect 4 */
-                       );
-       if (!NT_SUCCESS(Status))
-       {
-               NtClose (Session.Port.Handle);
-               NtTerminateThread (Session.Port.Thread.Handle, Status);
-               NtClose (Session.Section.Handle);
-               RtlDeleteCriticalSection (& Session.Lock);
-               vtprintf ("%s: %s: NtMapViewOfSection failed with %08x\n",
-                    MyName, __FUNCTION__, Status);
-               return Status;
-       }
-       return Status;
+    {
+        NtClose (Session.Port.Handle);
+        NtTerminateThread (Session.Port.Thread.Handle, Status);
+        RtlDeleteCriticalSection (& Session.Lock);
+        vtprintf ("%s: %s: NtCreateSection failed with %08x\n",
+            MyName, __FUNCTION__, Status);
+        return Status;
+    }
+    Session.Section.BaseAddress = NULL;
+    Session.Section.ViewSize = SectionSize.u.LowPart;
+    Status = NtMapViewOfSection (
+                Session.Section.Handle,
+                NtCurrentProcess(),
+                & Session.Section.BaseAddress,
+                0, /* ZeroBits */
+                0, /* Commitsize */
+                0, /* SectionOffset */
+                & Session.Section.ViewSize,
+                ViewUnmap,
+                0, /* AllocationType */
+                PAGE_READWRITE /* Protect 4 */
+                );
+    if (!NT_SUCCESS(Status))
+    {
+        NtClose (Session.Port.Handle);
+        NtTerminateThread (Session.Port.Thread.Handle, Status);
+        NtClose (Session.Section.Handle);
+        RtlDeleteCriticalSection (& Session.Lock);
+        vtprintf ("%s: %s: NtMapViewOfSection failed with %08x\n",
+        MyName, __FUNCTION__, Status);
+        return Status;
+    }
+    return Status;
 }
 
 /**********************************************************************
- *     CreateTerminalToPsxChannel/0                                    PRIVATE
+ *     CreateTerminalToPsxChannel/0                            PRIVATE
  *
  * DESCRIPTION
  *
@@ -348,6 +372,8 @@ TRACE;
     /*
      * Try connecting to \POSIX+\SessionPort.
      */
+    RtlInitUnicodeString (& Session.ServerPort.Name, Session.ServerPort.NameBuffer);
+    OutputDebugStringW(Session.ServerPort.Name.Buffer);
     Status = NtConnectPort (
                 & Session.ServerPort.Handle,
                 & Session.ServerPort.Name,
@@ -408,15 +434,16 @@ TRACE;
  */
 PRIVATE NTSTATUS STDCALL PsxCreateLeaderProcess (char * Command)
 {
-
+        NTSTATUS Status;
 TRACE;
 
        if (NULL == Command)
        {
-               Command = "/bin/sh";
+               Command = "sh";
        }
        /* TODO: request PSXSS to init the process slot */
-       return STATUS_NOT_IMPLEMENTED;
+       vtprintf ("%s: %s: calling CSRSS not implemented!", MyName, __FUNCTION__);
+       return STATUS_SUCCESS;
 }
 /**********************************************************************
  *     PrintInformationProcess/0