\_SB_.PCI0.SF0_.S60_
  ë    /* A multi-threaded telnet-like server that gives a Python prompt.

Usage: pysvr [port]

For security reasons, it only accepts requests from the current host.
This can still be insecure, but restricts violations from people who
can log in on your machine.  Use with caution!

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <pthread.h>
#include <getopt.h>

/* XXX Umpfh.
   Python.h defines a typedef destructor, which conflicts with pthread.h.
   So Python.h must be included after pthread.h. */

#include "Python.h"

extern int Py_VerboseFlag;

#ifndef PORT
#define PORT 4000
#endif

struct workorder {
    int conn;
    struct sockaddr_in addr;
};

/* Forward */
static void init_python(void);
static void usage(void);
static void oprogname(void);
static void main_thread(int);
static void create_thread(int, struct sockaddr_in *);
static void *service_thread(struct workorder *);
static void run_interpreter(FILE *, FILE *);
static int run_command(char *, PyObject *);
static void ps(void);

static char *progname = "pysvr";

static PyThreadState *gtstate;

main(int argc, char **argv)
{
    int port = PORT;
    int c;

    if (argc > 0 && argv[0] != NULL && argv[0][0] != '\0')
        progname = argv[0];

    while ((c = getopt(argc, argv, "v")) != EOF) {
        switch (c) {
        case 'v':
            Py_VerboseFlag++;
            break;
        default:
            usage();
        }
    }

    if (optind < argc) {
        if (optind+1 < argc) {
            oprogname();
            fprintf(stderr, "too many arguments\n");
            usage();
        }
        port = atoi(argv[optind]);
        if (port <= 0) {
            fprintf(stderr, "bad port (%s)\n", argv[optind]);
            usage();
        }
    }

    main_thread(port);

    fprintf(stderr, "Bye.\n");

    exit(0);
}

static char usage_line[] = "usage: %s [port]\n";

static void
usage(void)
{
    fprintf(stderr, usage_line, progname);
    exit(2);
}

static void
main_thread(int port)
{
    int sock, conn, size, i;
    struct sockaddr_in addr, clientaddr;

    sock = socket(PF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        oprogname();
        perror("can't create socket");
        exit(1);
    }

#ifdef SO_REUSEADDR
    i = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof i);
#endif

    memset((char *)&addr, '\0', sizeof addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = 0L;
    if (bind(sock, (struct sockaddr *)&addr, sizeof addr) < 0) {
        oprogname();
        perror("can't bind socket to address");
        exit(1);
    }

    if (listen(sock, 5) < 0) {
        oprogname();
        perror("can't listen on socket");
        exit(1);
    }

    fprintf(stderr, "Listening on port %d...\n", port);

    for (i = 0; ; i++) {
        size = sizeof clientaddr;
        memset((char *) &clientaddr, '\0', size);
        conn = accept(sock, (struct sockaddr *) &clientaddr, &size);
        if (conn < 0) {
            oprogname();
            perror("can't accept connection from socket");
            exit(1);
        }

        size = sizeof addr;
        memset((char *) &addr, '\0', size);
        if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) {
            oprogname();
            perror("can't get socket name of connection");
            exit(1);
        }
        if (clientaddr.sin_addr.s_addr != addr.sin_addr.s_addr) {
            oprogname();
            perror("connection from non-local host refused");
            fprintf(stderr, "(addr=%lx, clientaddr=%lx)\n",
                ntohl(addr.sin_addr.s_addr),
                ntohl(clientaddr.sin_addr.s_addr));
            close(conn);
            continue;
        }
        if (i == 4) {
            close(conn);
            break;
        }
        create_thread(conn, &clientaddr);
    }

    close(sock);

    if (gtstate) {
        PyEval_AcquireThread(gtstate);
        gt