⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecosynth.c

📁 开放源码实时操作系统源码.
💻 C
📖 第 1 页 / 共 2 页
字号:
//============================================================================
//
//     ecosynth.c
//
//     The eCos synthetic target I/O auxiliary
//
//============================================================================
//####COPYRIGHTBEGIN####
//                                                                          
// ----------------------------------------------------------------------------
// Copyright (C) 2002 Bart Veer
//
// This file is part of the eCos host tools.
//
// This program is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the Free 
// Software Foundation; either version 2 of the License, or (at your option) 
// any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
// more details.
// 
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ----------------------------------------------------------------------------
//                                                                          
//####COPYRIGHTEND####
//============================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):   bartv
// Contact(s):  bartv
// Date:        2002-08-05
// Version:     0.01
// Description:
//
// The main module for the eCos synthetic target auxiliary. This
// program is fork'ed and execve'd during the initialization phase of
// any synthetic target application, and is primarily responsible for
// I/O operations. This program should never be run directly by a
// user.
//
//####DESCRIPTIONEND####
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
// Avoid compatibility problems with Tcl 8.4 vs. earlier
#define USE_NON_CONST
#include <tcl.h>
#include <tk.h>

// The protocol between host and target is defined by a private
// target-side header.
#include "../src/synth_protocol.h"

// ----------------------------------------------------------------------------
// Statics etc.
#define PROGNAME    "Synthetic target auxiliary"

static int  no_windows = 0; // -nw arg

static pid_t    parent_pid;

// ----------------------------------------------------------------------------
// Overview.
//
// When a synthetic target application is executed, whether directly
// or from inside gdb, it can fork() and execve() the synthetic
// target auxiliary: this program, ecosynth. For now this is disabled
// by default but can be enabled using a --io option on the command line.
// In future it may be enabled by default, but can be suppressed using
// --nio.
//
// stdin, stdout, and stderr will be inherited from the eCos application, so
// that attempts to use printf() or fprintf(stderr, ) from inside the
// auxiliary or its helper applications work as expected. In addition file
// descriptor 3 will be a pipe from the eCos application, and file descriptor
// 4 will be a pipe to the application. The protocol defined in
// ../src/synth_protocol.h runs over these pip
//
// argv[] and environ are also as per the application, with the exception
// of argv[0] which is the full path to this application.
//
// The bulk of the hard work is done inside Tcl. The C++ code is
// responsible only for initialization and for implementing some
// additional commands, for example to send a SIGIO signal to the
// parent when there is a new pending interrupt. The primary script is
// ecosynth.tcl which should be installed alongside the executable.
//
// This code makes use of the standard Tcl initialization facilities:
//
// 1) main() calls Tcl_Main() with the command-line arguments and an
//    application-specific initialization routine ecosynth_appinit().
//
// 2) Tcl_Main() goes through the initialization sequence in generic/tclMain.c.
//    There is one slight complication: Tcl_main() interprets arguments in
//    a way that makes sense for tclsh, but not for ecosynth. Specially if
//    argv[1] exists and does not begin with a - then it will be interpreted
//    as a script to be executed. Hence argv[] is re-allocated and the name
//    of a suitable script is inserted.
//
// 3) ecosynth_appinit() initializes the Tcl interpreter, and optionally
//    the Tk interpreter as well. This is controlled by the presence of
//    a -nw argument on the command line. This initialization routine
//    also takes care of adding some commands to the Tcl interpreter.
//
// 4) Tcl_Main() will now proceed to execute the startup script, which
//    is eccentric.tcl installed in the libexec directory.

static int  ecosynth_appinit(Tcl_Interp*);

int
main(int argc, char** argv)
{
    char    ecosynth_tcl_path[_POSIX_PATH_MAX];
    char**  new_argv;
    int     i;

    parent_pid = getppid();

    // The various core Tcl scripts are installed in the same
    // directory as ecosynth. The Tcl script itself will check whether
    // there is a newer version of itself in the source tree and
    // switch to that instead.
    assert((strlen(LIBEXECDIR) + strlen(PACKAGE_INSTALL) + 20) < _POSIX_PATH_MAX);
    strcpy(ecosynth_tcl_path, LIBEXECDIR);
    strcat(ecosynth_tcl_path, "/ecos/");
    strcat(ecosynth_tcl_path, PACKAGE_INSTALL);
    strcat(ecosynth_tcl_path, "/ecosynth.tcl");

    // Installation sanity checks.
    if (0 != access(ecosynth_tcl_path, F_OK)) {
        fprintf(stderr, PROGNAME ": error, a required Tcl script has not been installed.\n");
        fprintf(stderr, "    The script is \"%s\"\n", ecosynth_tcl_path);
        exit(EXIT_FAILURE);
    }
    if (0 != access(ecosynth_tcl_path, R_OK)) {
        fprintf(stderr, PROGNAME ": error, no read access to a required Tcl script.\n");
        fprintf(stderr, "    The script is \"%s\"\n", ecosynth_tcl_path);
        exit(EXIT_FAILURE);
    }

    // Look for options -nw and -w. This information is needed by the appinit() routine.
    no_windows = 0;
    for (i = 1; i < argc; i++) {
        if ((0 == strcmp("-nw", argv[i])) || (0 == strcmp("--nw", argv[i])) ||
            (0 == strcmp("-no-windows", argv[i])) || (0 == strcmp("--no-windows", argv[i]))) {
            no_windows = 1;
        } else if ((0 == strcmp("-w", argv[i])) || (0 == strcmp("--w", argv[i])) ||
                   (0 == strcmp("-windows", argv[i])) || (0 == strcmp("--windows", argv[i]))) {
            no_windows = 0;
        }
    }
    
    new_argv = malloc((argc+2) * sizeof(char*));
    if (NULL == new_argv) {
        fprintf(stderr, PROGNAME ": internal error, out of memory.\n");
        exit(EXIT_FAILURE);
    }
    new_argv[0] = argv[0];
    new_argv[1] = ecosynth_tcl_path;
    for (i = 1; i < argc; i++) {
        new_argv[i+1] = argv[i];
    }
    new_argv[i+1] = NULL;

    // Ignore SIGINT requests. Those can happen if e.g. the application is
    // ctrl-C'd or if a gdb session is interrupted because this process is
    // a child of the eCos application. Instead the normal code for handling
    // application termination needs to run.
    signal(SIGINT, SIG_IGN);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -