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

📄 slsnif.c

📁 项目描述: slsnif is a serial port logging utility. It listens to the specified serial port and logs all
💻 C
📖 第 1 页 / 共 2 页
字号:
    wait(&status);}char *getColor(char *name) {/* returns escape sequence that corresponds to a given color name */    int i = 0;    while (colors[i].name && strcmp(name, colors[i].name)) i++;    if (colors[i].name) return colors[i].color;    return NULL;}int main(int argc, char *argv[]) {    int             i, j, maxfd, optret, needsync = 1;    char            *logName = NULL, baudstr[7], *ptr1, *ptr2;    struct termios  tty_state;    fd_set          rset;#ifdef HAVE_GETOPT_LONG    struct option longopts[] = {        {"help",       0, NULL, 'h'},        {"log",        1, NULL, 'l'},        {"nolock",     0, NULL, 'n'},        {"port2",      1, NULL, 'p'},        {"speed",      1, NULL, 's'},        {"bytes",      0, NULL, 'b'},        {"timestamp",  0, NULL, 't'},        {"color",      1, NULL, 'x'},        {"timecolor",  1, NULL, 'y'},        {"bytescolor", 1, NULL, 'z'},        {"in-tee",     1, NULL, 'i'},        {"out-tee",    1, NULL, 'o'},        {NULL,         0, NULL,   0}    };#endif    /* don't lose last chunk of data when output is non-interactive */    setvbuf(stdout,NULL,_IONBF,0);    setvbuf(stderr,NULL,_IONBF,0);    /* initialize variables */    baudstr[0] = 0;    tee_files[0] = tee_files[1] = NULL;    tty_data.portName = tty_data.ptyName = NULL;    tty_data.ptyfd = tty_data.portfd = tty_data.logfd = -1;    tty_data.ptyraw = tty_data.portraw = tty_data.nolock = FALSE;    tty_data.dspbytes = tty_data.tstamp = FALSE;    tty_data.ptypipefd[0] = tty_data.ptypipefd[1] = -1;    tty_data.portpipefd[0] = tty_data.portpipefd[1] = -1;    tty_data.baudrate = DEFBAUDRATE;    tty_data.clr[0] = tty_data.bclr[0] = tty_data.tclr[0] = 0;    /* parse rc-file */    readRC(&tty_data);    /* activate signal handlers */    signal(SIGINT, sigintP);    signal(SIGCHLD, sigchldP);    signal(SIGHUP, sighupP);    /* register closeAll() function to be called on normal termination */    atexit(closeAll);    /* process command line arguments */#ifdef HAVE_GETOPT_LONG    while ((optret = getopt_long(argc, argv, OPTSTR, longopts, NULL)) != -1) {#else    while ((optret = getopt(argc, argv, OPTSTR)) != -1) {#endif        switch (optret) {            case 'b':                tty_data.dspbytes = TRUE;                break;            case 't':                tty_data.tstamp = TRUE;                break;            case 'n':                tty_data.nolock = TRUE;                break;            case 'l':                logName = (optarg[0] == '=' ? optarg + 1 : optarg);                break;            case 's':                i = 0;                while (baudrates[i].spdstr &&                        strcmp(optarg, baudrates[i].spdstr)) i++;                if (baudrates[i].spdstr) {                    tty_data.baudrate = baudrates[i].speed;                    strcat(baudstr, baudrates[i].spdstr);                }                break;            case 'p':                tty_data.ptyName = (optarg[0] == '=' ? optarg + 1 : optarg);                break;            case 'x':                ptr1 = getColor(optarg);                if (ptr1) {                    tty_data.clr[0] = 0;                    strcat(tty_data.clr, ptr1);                }                break;            case 'y':                ptr1 = getColor(optarg);                if (ptr1) {                    tty_data.tclr[0] = 0;                    strcat(tty_data.tclr, ptr1);                }                break;            case 'z':                ptr1 = getColor(optarg);                if (ptr1) {                    tty_data.bclr[0] = 0;                    strcat(tty_data.bclr, ptr1);                }                break;            case 'i':            case 'o':                    if (!(entry = malloc(sizeof(tee_entry)))) fatalError(MEMFAIL);                    entryp = (optret == 'i' ? &tee_files[0] : &tee_files[1]);                    if (!(entry->name = malloc((strlen(optarg) + 1) * sizeof(char))))                        fatalError(MEMFAIL);                    strcpy(entry->name, optarg);                    entry->next = *entryp;                    *entryp = entry;                break;            case 'h':            case '?':            default :                usage();                tty_data.ptyName = NULL;                return -1;        }    }    if (optind < argc) {	   if (!(tty_data.portName = malloc(PORTNAMELEN))) fatalError(MEMFAIL);	   ptr1 = argv[optind];	   ptr2 = tty_data.portName;	   while((*ptr1 == '/' || isalnum(*ptr1))                && ptr2 - tty_data.portName < PORTNAMELEN - 1) *ptr2++ = *ptr1++;	   *ptr2 = 0;    }    if (!tty_data.portName || !tty_data.portName[0]) {        usage();        tty_data.ptyName = NULL;        return -1;    }    if (tty_data.ptyName && !strncmp(tty_data.portName, tty_data.ptyName, strlen(tty_data.portName))) {        /* first and second port point to the same device */	errno = EINVAL;        perror(DIFFFAIL);        return -1;    }    copyright();    /* open logfile */    if (!logName)        tty_data.logfd = STDOUT_FILENO;    else {        if((tty_data.logfd = open(logName,                    O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {            perror(LOGFAIL);            tty_data.logfd = STDOUT_FILENO;        } else {            printf("Started logging data into file '%s'.\n", logName);        }    }    /* open tee files */    for (i = 0; i < 2; i++) {        entry = tee_files[i];        while (entry) {            if ((entry->fd = open(entry->name, O_WRONLY|O_NOCTTY|O_CREAT|O_TRUNC, 0644)) < 0) {                perror(TEEFAIL);            } else {                printf("Started logging raw data from %s into file '%s'.\n",                                            i ? "host" : "device", entry->name);            }            entry = entry->next;        }    }    /* create pipe */    if (pipe(tty_data.ptypipefd) < 0 || pipe(tty_data.portpipefd) < 0) {        perror(PIPEFAIL);        return -1;    }    /* fork child process */    switch (pid = fork()) {    case 0:        /* child process */        /* close write pipe */        close(tty_data.ptypipefd[1]);        close(tty_data.portpipefd[1]);        signal(SIGINT, sigintC);        signal(SIGHUP, sighupC);        pipeReader(&tty_data);        break;    case -1:        /* fork() failed */        perror(FORKFAIL);        return -1;    default:        /* parent process */        /* close read pipe */        close(tty_data.ptypipefd[0]);        close(tty_data.portpipefd[0]);        break;    }    if (!tty_data.ptyName) {        /* open master side of the pty */        /* Search for a free pty */        if (!(tty_data.ptyName = strdup(DEFPTRNAME))) fatalError(MEMFAIL);        for(i = 0; i < 16 && tty_data.ptyfd < 0; i++) {            tty_data.ptyName[8] = "pqrstuvwxyzPQRST"[i];            for(j = 0; j < 16 && tty_data.ptyfd < 0; j++) {                tty_data.ptyName[9] = "0123456789abcdef"[j];                /* try to open master side */                tty_data.ptyfd = open(tty_data.ptyName, O_RDWR|O_NONBLOCK|O_NOCTTY);            }         }        if (tty_data.ptyfd < 0) {            /* failed to find an available pty */            free(tty_data.ptyName);            /*set pointer to NULL as it will be checked in closeAll() */            tty_data.ptyName = NULL;            perror(PTYFAIL);            return -1;        }        /* create the name of the slave pty */        tty_data.ptyName[5] = 't';        printf("Opened pty: %s\n", tty_data.ptyName);        free(tty_data.ptyName);        /*set pointer to NULL as it will be checked in closeAll() */        tty_data.ptyName = NULL;    } else {        /* open port2 instead of pty */        /* lock port2 */        if (!tty_data.nolock && dev_setlock(tty_data.ptyName) == -1) {            /* couldn't lock the device */            return -1;        }        /* try to open port2 */        if ((tty_data.ptyfd = open(tty_data.ptyName, O_RDWR|O_NONBLOCK)) < 0) {            perror(PORTFAIL);            return -1;        }        printf("Opened port: %s\n", tty_data.ptyName);    }    /* set raw mode on pty */    if(!setRaw(tty_data.ptyfd, &tty_data.ptystate_orig)) {        perror(RAWFAIL);        return -1;    }    tty_data.ptyraw = TRUE;    /* lock port */    if (!tty_data.nolock && dev_setlock(tty_data.portName) == -1) {        /* couldn't lock the device */        return -1;    }    /* try to open port */    if ((tty_data.portfd = open(tty_data.portName, O_RDWR|O_NONBLOCK)) < 0) {        perror(PORTFAIL);        return -1;    }    printf("Opened port: %s\n", tty_data.portName);    /* set raw mode on port */    if (!setRaw(tty_data.portfd, &tty_data.portstate_orig)) {        perror(RAWFAIL);        return -1;    }    tty_data.portraw = TRUE;    printf("Baudrate is set to %s baud%s.\n",                baudstr[0] ? baudstr : "9600",                baudstr[0] ? "" : " (default)");    /* start listening to the slave and port */    maxfd = max(tty_data.ptyfd, tty_data.portfd);    while (TRUE) {        FD_ZERO(&rset);        FD_SET(tty_data.ptyfd, &rset);        FD_SET(tty_data.portfd, &rset);        if (select(maxfd + 1, &rset, NULL, NULL, NULL) < 0) {	    if (errno != EINTR) {                perror(SELFAIL);                return -1;            } else {	        continue;	    }        }        if (FD_ISSET(tty_data.portfd, &rset)) {            /* data coming from device */            writeData(tty_data.portfd, tty_data.ptyfd, tty_data.portpipefd[1], 1);        } else {            if (FD_ISSET(tty_data.ptyfd, &rset)) {                /* data coming from host */                if (needsync) {                    printf("Syncronizing ports...");                    if (tcgetattr(tty_data.ptyfd, &tty_state) ||                            tcsetattr(tty_data.portfd, TCSANOW, &tty_state)) {                        perror(SYNCFAIL);                        return -1;                    }                    needsync = 0;                    printf("Done!\n");                }                writeData(tty_data.ptyfd, tty_data.portfd, tty_data.ptypipefd[1], 2);            }        }    }    return 1;}

⌨️ 快捷键说明

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