OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [win/] [stub16.c] - Diff between revs 578 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 578 Rev 1765
/*
/*
 * stub16.c
 * stub16.c
 *
 *
 *      A helper program used for running 16-bit DOS applications under
 *      A helper program used for running 16-bit DOS applications under
 *      Windows 95.
 *      Windows 95.
 *
 *
 * Copyright (c) 1996 by Sun Microsystems, Inc.
 * Copyright (c) 1996 by Sun Microsystems, Inc.
 *
 *
 * See the file "license.terms" for information on usage and redistribution
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * RCS: @(#) $Id: stub16.c,v 1.1.1.1 2002-01-16 10:25:38 markom Exp $
 * RCS: @(#) $Id: stub16.c,v 1.1.1.1 2002-01-16 10:25:38 markom Exp $
 */
 */
 
 
#define STRICT
#define STRICT
 
 
#include <windows.h>
#include <windows.h>
#include <stdio.h>
#include <stdio.h>
 
 
static HANDLE           CreateTempFile(void);
static HANDLE           CreateTempFile(void);
 
 
/*
/*
 *---------------------------------------------------------------------------
 *---------------------------------------------------------------------------
 *
 *
 * main
 * main
 *
 *
 *      Entry point for the 32-bit console mode app used by Windows 95 to
 *      Entry point for the 32-bit console mode app used by Windows 95 to
 *      help run the 16-bit program specified on the command line.
 *      help run the 16-bit program specified on the command line.
 *
 *
 *      1. EOF on a pipe that connects a detached 16-bit process and a
 *      1. EOF on a pipe that connects a detached 16-bit process and a
 *      32-bit process is never seen.  So, this process runs the 16-bit
 *      32-bit process is never seen.  So, this process runs the 16-bit
 *      process _attached_, and then it is run detached from the calling
 *      process _attached_, and then it is run detached from the calling
 *      32-bit process.
 *      32-bit process.
 *
 *
 *      2. If a 16-bit process blocks reading from or writing to a pipe,
 *      2. If a 16-bit process blocks reading from or writing to a pipe,
 *      it never wakes up, and eventually brings the whole system down
 *      it never wakes up, and eventually brings the whole system down
 *      with it if you try to kill the process.  This app simulates
 *      with it if you try to kill the process.  This app simulates
 *      pipes.  If any of the stdio handles is a pipe, this program
 *      pipes.  If any of the stdio handles is a pipe, this program
 *      accumulates information into temp files and forwards it to or
 *      accumulates information into temp files and forwards it to or
 *      from the DOS application as appropriate.  This means that this
 *      from the DOS application as appropriate.  This means that this
 *      program must receive EOF from a stdin pipe before it will actually
 *      program must receive EOF from a stdin pipe before it will actually
 *      start the DOS app, and the DOS app must finish generating stdout
 *      start the DOS app, and the DOS app must finish generating stdout
 *      or stderr before the data will be sent to the next stage of the
 *      or stderr before the data will be sent to the next stage of the
 *      pipe.  If the stdio handles are not pipes, no accumulation occurs
 *      pipe.  If the stdio handles are not pipes, no accumulation occurs
 *      and the data is passed straight through to and from the DOS
 *      and the data is passed straight through to and from the DOS
 *      application.
 *      application.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      The child process is created and this process waits for it to
 *      The child process is created and this process waits for it to
 *      complete.
 *      complete.
 *
 *
 *---------------------------------------------------------------------------
 *---------------------------------------------------------------------------
 */
 */
 
 
int
int
main()
main()
{
{
    DWORD dwRead, dwWrite;
    DWORD dwRead, dwWrite;
    char *cmdLine;
    char *cmdLine;
    HANDLE hStdInput, hStdOutput, hStdError;
    HANDLE hStdInput, hStdOutput, hStdError;
    HANDLE hFileInput, hFileOutput, hFileError;
    HANDLE hFileInput, hFileOutput, hFileError;
    STARTUPINFO si;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    PROCESS_INFORMATION pi;
    char buf[8192];
    char buf[8192];
    DWORD result;
    DWORD result;
 
 
    hFileInput = INVALID_HANDLE_VALUE;
    hFileInput = INVALID_HANDLE_VALUE;
    hFileOutput = INVALID_HANDLE_VALUE;
    hFileOutput = INVALID_HANDLE_VALUE;
    hFileError = INVALID_HANDLE_VALUE;
    hFileError = INVALID_HANDLE_VALUE;
    result = 1;
    result = 1;
 
 
    /*
    /*
     * Don't get command line from argc, argv, because the command line
     * Don't get command line from argc, argv, because the command line
     * tokenizer will have stripped off all the escape sequences needed
     * tokenizer will have stripped off all the escape sequences needed
     * for quotes and backslashes, and then we'd have to put them all
     * for quotes and backslashes, and then we'd have to put them all
     * back in again.  Get the raw command line and parse off what we
     * back in again.  Get the raw command line and parse off what we
     * want ourselves.  The command line should be of the form:
     * want ourselves.  The command line should be of the form:
     *
     *
     * stub16.exe program arg1 arg2 ...
     * stub16.exe program arg1 arg2 ...
     */
     */
 
 
    cmdLine = strchr(GetCommandLine(), ' ');
    cmdLine = strchr(GetCommandLine(), ' ');
    if (cmdLine == NULL) {
    if (cmdLine == NULL) {
        return 1;
        return 1;
    }
    }
    cmdLine++;
    cmdLine++;
 
 
    hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    hStdError = GetStdHandle(STD_ERROR_HANDLE);
    hStdError = GetStdHandle(STD_ERROR_HANDLE);
 
 
    if (GetFileType(hStdInput) == FILE_TYPE_PIPE) {
    if (GetFileType(hStdInput) == FILE_TYPE_PIPE) {
        hFileInput = CreateTempFile();
        hFileInput = CreateTempFile();
        if (hFileInput == INVALID_HANDLE_VALUE) {
        if (hFileInput == INVALID_HANDLE_VALUE) {
            goto cleanup;
            goto cleanup;
        }
        }
        while (ReadFile(hStdInput, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
        while (ReadFile(hStdInput, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
            if (dwRead == 0) {
            if (dwRead == 0) {
                break;
                break;
            }
            }
            if (WriteFile(hFileInput, buf, dwRead, &dwWrite, NULL) == FALSE) {
            if (WriteFile(hFileInput, buf, dwRead, &dwWrite, NULL) == FALSE) {
                goto cleanup;
                goto cleanup;
            }
            }
        }
        }
        SetFilePointer(hFileInput, 0, 0, FILE_BEGIN);
        SetFilePointer(hFileInput, 0, 0, FILE_BEGIN);
        SetStdHandle(STD_INPUT_HANDLE, hFileInput);
        SetStdHandle(STD_INPUT_HANDLE, hFileInput);
    }
    }
    if (GetFileType(hStdOutput) == FILE_TYPE_PIPE) {
    if (GetFileType(hStdOutput) == FILE_TYPE_PIPE) {
        hFileOutput = CreateTempFile();
        hFileOutput = CreateTempFile();
        if (hFileOutput == INVALID_HANDLE_VALUE) {
        if (hFileOutput == INVALID_HANDLE_VALUE) {
            goto cleanup;
            goto cleanup;
        }
        }
        SetStdHandle(STD_OUTPUT_HANDLE, hFileOutput);
        SetStdHandle(STD_OUTPUT_HANDLE, hFileOutput);
    }
    }
    if (GetFileType(hStdError) == FILE_TYPE_PIPE) {
    if (GetFileType(hStdError) == FILE_TYPE_PIPE) {
        hFileError = CreateTempFile();
        hFileError = CreateTempFile();
        if (hFileError == INVALID_HANDLE_VALUE) {
        if (hFileError == INVALID_HANDLE_VALUE) {
            goto cleanup;
            goto cleanup;
        }
        }
        SetStdHandle(STD_ERROR_HANDLE, hFileError);
        SetStdHandle(STD_ERROR_HANDLE, hFileError);
    }
    }
 
 
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    si.cb = sizeof(si);
    if (CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si,
    if (CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si,
            &pi) == FALSE) {
            &pi) == FALSE) {
        goto cleanup;
        goto cleanup;
    }
    }
 
 
    WaitForInputIdle(pi.hProcess, 5000);
    WaitForInputIdle(pi.hProcess, 5000);
    WaitForSingleObject(pi.hProcess, INFINITE);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hThread);
    result = 0;
    result = 0;
 
 
    if (hFileOutput != INVALID_HANDLE_VALUE) {
    if (hFileOutput != INVALID_HANDLE_VALUE) {
        SetFilePointer(hFileOutput, 0, 0, FILE_BEGIN);
        SetFilePointer(hFileOutput, 0, 0, FILE_BEGIN);
        while (ReadFile(hFileOutput, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
        while (ReadFile(hFileOutput, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
            if (dwRead == 0) {
            if (dwRead == 0) {
                break;
                break;
            }
            }
            if (WriteFile(hStdOutput, buf, dwRead, &dwWrite, NULL) == FALSE) {
            if (WriteFile(hStdOutput, buf, dwRead, &dwWrite, NULL) == FALSE) {
                break;
                break;
            }
            }
        }
        }
    }
    }
    if (hFileError != INVALID_HANDLE_VALUE) {
    if (hFileError != INVALID_HANDLE_VALUE) {
        SetFilePointer(hFileError, 0, 0, FILE_BEGIN);
        SetFilePointer(hFileError, 0, 0, FILE_BEGIN);
        while (ReadFile(hFileError, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
        while (ReadFile(hFileError, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
            if (dwRead == 0) {
            if (dwRead == 0) {
                break;
                break;
            }
            }
            if (WriteFile(hStdError, buf, dwRead, &dwWrite, NULL) == FALSE) {
            if (WriteFile(hStdError, buf, dwRead, &dwWrite, NULL) == FALSE) {
                break;
                break;
            }
            }
        }
        }
    }
    }
 
 
cleanup:
cleanup:
    if (hFileInput != INVALID_HANDLE_VALUE) {
    if (hFileInput != INVALID_HANDLE_VALUE) {
        CloseHandle(hFileInput);
        CloseHandle(hFileInput);
    }
    }
    if (hFileOutput != INVALID_HANDLE_VALUE) {
    if (hFileOutput != INVALID_HANDLE_VALUE) {
        CloseHandle(hFileOutput);
        CloseHandle(hFileOutput);
    }
    }
    if (hFileError != INVALID_HANDLE_VALUE) {
    if (hFileError != INVALID_HANDLE_VALUE) {
        CloseHandle(hFileError);
        CloseHandle(hFileError);
    }
    }
    CloseHandle(hStdInput);
    CloseHandle(hStdInput);
    CloseHandle(hStdOutput);
    CloseHandle(hStdOutput);
    CloseHandle(hStdError);
    CloseHandle(hStdError);
    ExitProcess(result);
    ExitProcess(result);
    return 1;
    return 1;
}
}
 
 
static HANDLE
static HANDLE
CreateTempFile()
CreateTempFile()
{
{
    char name[MAX_PATH];
    char name[MAX_PATH];
    SECURITY_ATTRIBUTES sa;
    SECURITY_ATTRIBUTES sa;
 
 
    if (GetTempPath(sizeof(name), name) == 0) {
    if (GetTempPath(sizeof(name), name) == 0) {
        return INVALID_HANDLE_VALUE;
        return INVALID_HANDLE_VALUE;
    }
    }
    if (GetTempFileName(name, "tcl", 0, name) == 0) {
    if (GetTempFileName(name, "tcl", 0, name) == 0) {
        return INVALID_HANDLE_VALUE;
        return INVALID_HANDLE_VALUE;
    }
    }
 
 
    sa.nLength = sizeof(sa);
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;
    sa.bInheritHandle = TRUE;
    return CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, &sa,
    return CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, &sa,
            CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
            CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
            NULL);
            NULL);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.