示例#1
0
int create_fd(const char *arg, int is_output, enum proto *proto, char *name, size_t max_name_size)
{
  if (strncmp(arg, "udp:", 4) == 0 || strncmp(arg, "UDP:", 4) == 0) {
    *proto = UDP;
    arg += 4;
  } else if (strncmp(arg, "tcp:", 4) == 0 || strncmp(arg, "TCP:", 4) == 0) {
    *proto = TCP;
    arg += 4;
  } else if (strncmp(arg, "file:", 5) == 0) {
    *proto = File;
    arg += 5;
  } else if (strncmp(arg, "eth:", 4) == 0) {
    *proto = Eth;
    arg += 4;
  } else if (strcmp(arg, "null:") == 0) {
    *proto = File;
    arg = "/dev/null";
  } else if (strcmp(arg, "zero:") == 0) {
    *proto = File;
    arg = "/dev/zero";
  } else if (strcmp(arg, "stdin:") == 0) {
    *proto = StdIn;
    arg = "stdin";
  } else if (strcmp(arg, "stdout:") == 0) {
    *proto = StdOut;
    arg = "stdout";
  } else if (strcmp(arg, "stderr:") == 0) {
    *proto = StdErr;
    arg = "stderr";
  } else if (strchr(arg, ':') != 0) {
    *proto = UDP;
  } else if (strcmp(arg, "-") == 0) {
    *proto = is_output ? StdOut : StdIn;
    arg = is_output ? "stdout" : "stdin";
  } else {
    *proto = File;
  }

  if (name != 0)
    strncpy(name, arg, max_name_size);

  switch (*proto) {
    case UDP	:
    case TCP	: return create_IP_socket(arg, is_output, *proto);

    case File	: return create_file(arg, is_output);

    case Eth	: return create_raw_eth_socket(arg, is_output);

    case StdIn	:
    case StdOut:
    case StdErr	: return create_stdio(is_output, *proto);
    default     : fprintf(stderr, "Cannot create fd for unknown proto %d\n", *proto), exit(1);
  }
}
示例#2
0
文件: test.c 项目: hsuyeep/Afaac_cep
int create_fd(char *arg, int is_output, enum proto *proto)
{
  if (strncmp(arg, "udp:", 4) == 0 || strncmp(arg, "UDP:", 4) == 0) {
    *proto = UDP;
    arg += 4;
  } else if (strncmp(arg, "tcp:", 4) == 0 || strncmp(arg, "TCP:", 4) == 0) {
    *proto = TCP;
    arg += 4;
  } else if (strncmp(arg, "file:", 5) == 0) {
    *proto = File;
    arg += 5;
  } else if (strncmp(arg, "eth:", 4) == 0) {
    *proto = Eth;
    arg += 4;
  } else if (strncmp(arg, "stdin:", 6) == 0) {
    *proto = StdIn;
    arg = "stdin";
  } else if (strncmp(arg, "stdout:", 7) == 0) {
    *proto = StdOut;
    arg = "stdout";
  } else if (strncmp(arg, "stderr:", 7) == 0) {
    *proto = StdErr;
    arg = "stderr";
  } else if (strchr(arg, ':') != 0) {
    *proto = UDP;
  } else if (strcmp(arg, "-") == 0) {
    *proto = is_output ? StdOut : StdIn;
    arg = is_output ? "stdout" : "stdin";
  } else {
    *proto = File;
  }

  if (is_output)
    destination = arg;
  else
    source	= arg;

  switch (*proto) {
    case UDP	:
    case TCP	: return create_IP_socket(arg, is_output, *proto);

    case File	: return create_file(arg, is_output);

    case Eth	: return create_raw_eth_socket(arg, is_output);

    case StdIn	:
    case StdOut:
    case StdErr	: return create_stdio(is_output, *proto);
  }
}
示例#3
0
void
YogBuiltins_boot(YogEnv* env, YogHandle* builtins)
{
    SAVE_LOCALS(env);
    YogVal errno_ = YUNDEF;
    YogVal e = YUNDEF;
    PUSH_LOCALS2(env, errno_, e);

#define DEFINE_FUNCTION(name, f)    do { \
    YogPackage_define_function(env, HDL2VAL(builtins), name, f); \
} while (0)
    DEFINE_FUNCTION("classmethod", classmethod);
    DEFINE_FUNCTION("disable_gc_stress", disable_gc_stress);
    DEFINE_FUNCTION("enable_gc_stress", enable_gc_stress);
    DEFINE_FUNCTION("get_current_thread", get_current_thread);
    DEFINE_FUNCTION("include_module", include_module);
    DEFINE_FUNCTION("join_path", join_path);
    DEFINE_FUNCTION("property", property);
    DEFINE_FUNCTION("puts", puts_);
    DEFINE_FUNCTION("raise_exception", raise_exception);
#undef DEFINE_FUNCTION
#define DEFINE_FUNCTION2(name, f, ...) do { \
    YogPackage_define_function2(env, builtins, (name), (f), __VA_ARGS__); \
} while (0)
    DEFINE_FUNCTION2("import_package", import_package, "name", NULL);
    DEFINE_FUNCTION2("load_lib", load_lib, "path", NULL);
    DEFINE_FUNCTION2("locals", locals, NULL);
    DEFINE_FUNCTION2("major_gc", major_gc, NULL);
    DEFINE_FUNCTION2("minor_gc", minor_gc, NULL);
    DEFINE_FUNCTION2("mkdir", mkdir_, "path", NULL);
    DEFINE_FUNCTION2("print", print, "*", NULL);
#undef DEFINE_FUNCTION2

#define REGISTER_CLASS(c)   do { \
    YogVal klass = env->vm->c; \
    ID name = PTR_AS(YogClass, klass)->name; \
    YogObj_set_attr_id(env, HDL2VAL(builtins), name, klass); \
} while (0)
    REGISTER_CLASS(cArray);
    REGISTER_CLASS(cBinary);
    REGISTER_CLASS(cBuffer);
    REGISTER_CLASS(cCoroutine);
    REGISTER_CLASS(cDatetime);
    REGISTER_CLASS(cDict);
    REGISTER_CLASS(cDir);
    REGISTER_CLASS(cFile);
    REGISTER_CLASS(cInt);
    REGISTER_CLASS(cObject);
    REGISTER_CLASS(cPath);
    REGISTER_CLASS(cPointer);
    REGISTER_CLASS(cProcess);
    REGISTER_CLASS(cRegexp);
    REGISTER_CLASS(cSet);
    REGISTER_CLASS(cString);
    REGISTER_CLASS(cStructClass);
    REGISTER_CLASS(cSymbol);
    REGISTER_CLASS(cThread);
    REGISTER_CLASS(cUnionClass);
    REGISTER_CLASS(eAttributeError);
    REGISTER_CLASS(eException);
    REGISTER_CLASS(eImportError);
    REGISTER_CLASS(eIndexError);
    REGISTER_CLASS(eKeyError);
    REGISTER_CLASS(eNotImplementedError);
    REGISTER_CLASS(eSyntaxError);
    REGISTER_CLASS(eSystemError);
    REGISTER_CLASS(eValueError);
#undef REGISTER_CLASS
#define REGISTER_MODULE(m) do { \
    YogVal mod = env->vm->m; \
    ID name = PTR_AS(YogModule, mod)->name; \
    YogObj_set_attr_id(env, HDL2VAL(builtins), name, mod); \
} while (0)
    REGISTER_MODULE(mComparable);
    REGISTER_MODULE(mEnumerable);
#undef REGISTER_MODULE

    e = YogEnv_new(env);
    YogObj_set_attr(env, HDL2VAL(builtins), "ENV", e);
    YogObj_set_attr(env, HDL2VAL(builtins), "ENCODINGS", env->vm->encodings);
    YogVal enc = env->vm->default_encoding;
    YogObj_set_attr(env, HDL2VAL(builtins), "DEFAULT_ENCODING", enc);
    YogVal STDIN = create_stdio(env, stdin);
    YogObj_set_attr(env, HDL2VAL(builtins), "STDIN", STDIN);
    YogVal STDOUT = create_stdio(env, stdout);
    YogObj_set_attr(env, HDL2VAL(builtins), "STDOUT", STDOUT);
    YogVal STDERR = create_stdio(env, stderr);
    YogObj_set_attr(env, HDL2VAL(builtins), "STDERR", STDERR);

    set_path_separator(env, builtins);

#define REGISTER_ERRNO(e) do { \
    errno_ = YogVal_from_int(env, (e)); \
    YogObj_set_attr(env, HDL2VAL(builtins), #e, errno_); \
} while (0)
#include "errno.inc"
#undef REGISTER_ERRNO

    const char* src = 
#include "builtins.inc"
    ;
    YogMisc_eval_source(env, builtins, src);

    RETURN_VOID(env);
}
示例#4
0
/* Initialize sys.stdin, stdout, stderr and builtins.open */
static int
initstdio(void)
{
    PyObject *iomod = NULL, *wrapper;
    PyObject *bimod = NULL;
    PyObject *m;
    PyObject *std = NULL;
    int status = 0, fd;
    PyObject * encoding_attr;
    char *pythonioencoding = NULL, *encoding, *errors;

    /* Hack to avoid a nasty recursion issue when Python is invoked
       in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
    if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
        goto error;
    }
    Py_DECREF(m);

    if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
        goto error;
    }
    Py_DECREF(m);

    if (!(bimod = PyImport_ImportModule("builtins"))) {
        goto error;
    }

    if (!(iomod = PyImport_ImportModule("io"))) {
        goto error;
    }
    if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
        goto error;
    }

    /* Set builtins.open */
    if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
        Py_DECREF(wrapper);
        goto error;
    }
    Py_DECREF(wrapper);

    encoding = _Py_StandardStreamEncoding;
    errors = _Py_StandardStreamErrors;
    if (!encoding || !errors) {
        pythonioencoding = Py_GETENV("PYTHONIOENCODING");
        if (pythonioencoding) {
            char *err;
            pythonioencoding = _PyMem_Strdup(pythonioencoding);
            if (pythonioencoding == NULL) {
                PyErr_NoMemory();
                goto error;
            }
            err = strchr(pythonioencoding, ':');
            if (err) {
                *err = '\0';
                err++;
                if (*err && !errors) {
                    errors = err;
                }
            }
            if (*pythonioencoding && !encoding) {
                encoding = pythonioencoding;
            }
        }
        if (!errors && !(pythonioencoding && *pythonioencoding)) {
            /* When the LC_CTYPE locale is the POSIX locale ("C locale"),
               stdin and stdout use the surrogateescape error handler by
               default, instead of the strict error handler. */
            char *loc = setlocale(LC_CTYPE, NULL);
            if (loc != NULL && strcmp(loc, "C") == 0)
                errors = "surrogateescape";
        }
    }

    /* Set sys.stdin */
    fd = fileno(stdin);
    /* Under some conditions stdin, stdout and stderr may not be connected
     * and fileno() may point to an invalid file descriptor. For example
     * GUI apps don't have valid standard streams by default.
     */
    std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
    if (std == NULL)
        goto error;
    PySys_SetObject("__stdin__", std);
    _PySys_SetObjectId(&PyId_stdin, std);
    Py_DECREF(std);

    /* Set sys.stdout */
    fd = fileno(stdout);
    std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
    if (std == NULL)
        goto error;
    PySys_SetObject("__stdout__", std);
    _PySys_SetObjectId(&PyId_stdout, std);
    Py_DECREF(std);

#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
    /* Set sys.stderr, replaces the preliminary stderr */
    fd = fileno(stderr);
    std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
    if (std == NULL)
        goto error;

    /* Same as hack above, pre-import stderr's codec to avoid recursion
       when import.c tries to write to stderr in verbose mode. */
    encoding_attr = PyObject_GetAttrString(std, "encoding");
    if (encoding_attr != NULL) {
        const char * std_encoding;
        std_encoding = _PyUnicode_AsString(encoding_attr);
        if (std_encoding != NULL) {
            PyObject *codec_info = _PyCodec_Lookup(std_encoding);
            Py_XDECREF(codec_info);
        }
        Py_DECREF(encoding_attr);
    }
    PyErr_Clear();  /* Not a fatal error if codec isn't available */

    if (PySys_SetObject("__stderr__", std) < 0) {
        Py_DECREF(std);
        goto error;
    }
    if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
        Py_DECREF(std);
        goto error;
    }
    Py_DECREF(std);
#endif

    if (0) {
  error:
        status = -1;
    }

    /* We won't need them anymore. */
    if (_Py_StandardStreamEncoding) {
        PyMem_RawFree(_Py_StandardStreamEncoding);
        _Py_StandardStreamEncoding = NULL;
    }
    if (_Py_StandardStreamErrors) {
        PyMem_RawFree(_Py_StandardStreamErrors);
        _Py_StandardStreamErrors = NULL;
    }
    PyMem_Free(pythonioencoding);
    Py_XDECREF(bimod);
    Py_XDECREF(iomod);
    return status;
}