человек на солярисе - PullRequest
       2

человек на солярисе

0 голосов
/ 06 апреля 2011

этот код компилируется в Linux, но не в Solaris, поскольку очевидно, что ppoll () специфичен для Linux (я получаю неопределенную ошибку символа в Solaris с GCC).Любая помощь, конвертируя это?Я не думаю, что просто использовать poll () - хорошая идея, но опять же, я не писал этот код.(Я получил это от Написание оболочки командной строки с C; попытка использовать ncurses / C в первый раз )

#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/** VT100 command to clear the screen. Use puts(VT100_CLEAR_SCREEN) to clear
 *  the screen. */
#define VT100_CLEAR_SCREEN "\033[2J"

/** VT100 command to reset the cursor to the top left hand corner of the
 *  screen. */
#define VT100_CURSOR_TO_ORIGIN "\033[H"

struct frame_s
{
    int x;
    int y;
    char *data;
};

static int draw_frame(struct frame_s *frame)
{
    int row;
    char *data;
    int attrib;

    puts(VT100_CLEAR_SCREEN);
    puts(VT100_CURSOR_TO_ORIGIN);

    for (row = 0, data = frame->data; row  < frame->y; row++, data += frame->x)
    {
        /*  0 for normal, 1 for bold, 7 for reverse. */
        attrib = 0;

        /*  The VT100 commands to move the cursor, set the attribute, and the
         *  actual frame line. */
        fprintf(stdout, "\033[%d;%dH\033[0m\033[%dm%.*s", row + 1, 0, attrib, frame->x, data);
        fflush(stdout);
    }

    return (0);
}

int main(void)
{
    const struct timespec timeout = { .tv_sec = 1, .tv_nsec = 0 };
    struct frame_s frame;
    struct termios tty_old;
    struct termios tty_new;
    unsigned char line[128];
    unsigned int count = 0;
    int ret;
    struct pollfd fds[1];
    sigset_t sigmask;
    struct tm *tp;
    time_t current_time;

    /*  Set up a little frame. */
    frame.x = 80;
    frame.y = 5;
    frame.data = malloc(frame.x * frame.y);

    if (frame.data == NULL)
    {
        fprintf(stderr, "No memory\n");
        exit (1);
    }

    memset(frame.data, ' ', frame.x * frame.y);

    /*  Get the terminal state. */
    tcgetattr(STDIN_FILENO, &tty_old);
    tty_new = tty_old;

    /*  Turn off "cooked" mode (line buffering) and set minimum characters
     *  to zero (i.e. non-blocking). */
    tty_new.c_lflag &= ~ICANON;
    tty_new.c_cc[VMIN] = 0;

    /*  Set the terminal attributes. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_new);

    /*  Un-mask all signals while in ppoll() so any signal will cause
     *  ppoll() to return prematurely. */
    sigemptyset(&sigmask);

    fds[0].events = POLLIN;
    fds[0].fd = STDIN_FILENO;

    /*  Loop forever waiting for key presses. Update the output on every key
     *  press and every 1.0s (when ppoll() times out). */
    do
    {
        fds[0].revents = 0;
        ret = ppoll(fds, sizeof(fds) / sizeof(struct pollfd), &timeout, &sigmask);

        if (fds[0].revents & POLLIN)
        {
            ret = read(STDIN_FILENO, &line[count], sizeof(line) - count);

            if (ret > 0)
            {
                line[count + ret] = '\0';

                if (strcmp(&line[count], "\033[A") == 0)
                {
                    snprintf(frame.data, frame.x, "up");
                    count = 0;
                }
                else if (strcmp(&line[count], "\033[B") == 0)
                {
                    snprintf(frame.data, frame.x, "down");
                    count = 0;
                }
                else if (line[count] == '\n')
                {
                    snprintf(frame.data, frame.x, "entered: %s", line);
                    count = 0;
                }
                else
                {
                    count += ret;
                }
            }
        }

        /*  Print the current time to the output buffer. */
        current_time = time(NULL);
        tp = localtime(&current_time);
        strftime(&frame.data[1 * frame.x], frame.x, "%Y/%m/%d %H:%M:%S", tp);

        /*  Print the command line. */
        line[count] = '\0';
        snprintf(&frame.data[(frame.y - 1) * frame.x], frame.x, "$ %s", line);

        draw_frame(&frame);
    }
    while (1);

    /*  Restore terminal and free resources. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_old);
    free(frame.data);

    return (0);
}

Ответы [ 2 ]

2 голосов
/ 06 апреля 2011

Причина, по которой вы можете заменить ppoll() на poll() здесь, на данный момент - , а не , потому что маска сигнала, предоставленная ppoll(), пуста - это действительно обычный случай - но потому чтомаска сигнала до ppoll() также, вероятно, пуста.

Это возможно только потому, что оболочка в настоящее время не пытается обрабатывать какие-либо сигналы.Как только вы захотите подать сигналы оболочки, вам нужно будет использовать следующую последовательность:

/* Block all signals */
sigprocmask(SIG_SETMASK, all_signals);

/* Check and handle any signals that have occured. */
check_signals();

/* Wait for activity on a file descriptor or a signal */
ppoll(..., empty_set);

ppoll() здесь необходимо, потому что в противном случае возникает состояние гонки - сигнал может поступить между check_signals(); и poll() вызов, который будет пропущен.Следовательно, сигналы блокируются на эту длительность, затем атомно разблокируется в ppoll().

Поскольку ppoll() является расширением GNU, которое Solaris не предоставляет, вам необходимо изменить кодиспользовать pselect(), которая является стандартной функцией POSIX.


Чтобы преобразовать ваш код в pselect(), замените начало цикла на:

do
{
    fd_set rdset;
    int nfds = STDIN_FILENO + 1;

    FD_ZERO(&rdset);
    FD_SET(STDIN_FILENO, &rdset);
    ret = pselect(nfds, &rdset, NULL, NULL, &timeout, &sigmask);

    if (ret < 0) {
        if (errno == EINTR)
            continue;
        else
            break;
    }

    if (FD_ISSET(STDIN_FILENO, &rdset))
    {
        ret = read(STDIN_FILENO, &line[count], sizeof(line) - count);

    /* ... */

Youзатем можно удалить переменную fds, поскольку она больше не нужна.

Обратите внимание, что я добавил код, чтобы проверить возвращаемое значение pselect() на наличие ошибок - старый код должен был выполнятьто же самое для ppoll().

0 голосов
/ 06 апреля 2011

Вы не используете тайм-ауты менее миллисекунды, тайм-аут, который может переполнить (int), или непустой sigmask, поэтому poll() подходит для этого.

...