BlackBoard » Design, Programmierung & Entwicklung » Programmieren » C [tool] rxvt direkt auf einer seriellen Konsole » Hallo Gast [Anmelden|Registrieren]
Letzter Beitrag | Erster ungelesener Beitrag Druckvorschau | An Freund senden | Thema zu Favoriten hinzufügen
Neues Thema erstellen Antwort erstellen
Zum Ende der Seite springen [tool] rxvt direkt auf einer seriellen Konsole
Autor
Beitrag « Vorheriges Thema | Nächstes Thema »
Zirias Zirias ist männlich
BlackBoarder


images/avatars/avatar-450.jpg

Dabei seit: 11.09.2002
Beiträge: 1.217
Herkunft: /dev/urandom

Lampe [tool] rxvt direkt auf einer seriellen Konsole       Zum Anfang der Seite springen

Für ein Projekt ist es wohl zu klein, aber vielleicht kann es jemand brauchen: Dieses tool startet rxvt auf einem beliebigen tty, sinnvollerweise einer seriellen Konsole. Ich benutze es mit einem USB-Serial Konverter (/dev/ttyUSB0) für die Verbindung zu einem headless Server.

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
/* 
 * srxtv -- launch rxvt directly on a serial (RS-232) line
 *
 * PREREQUISITS:
 *     rxvt binary in search path
 *
 * USAGE (example):
 *     stty -F /dev/ttyS0 57600
 *     srxvt /dev/ttyS0
 *
 * 02/2009 Felix M. Palmen <fmp@palmen.homeip.net>
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>

#define RXVT "rxvt"
#define RXVT_LEN 4

int main(int argc, char **argv)
{
  int fd;
  char fdnum[16];
  struct termios tflags;
  struct stat fstat;
  char *path;
  char *envPath;
  char *pathElement;
  char *rxvt = NULL;
  char foundRxvt = 0;

  if (argc != 2)
  {
    fprintf(stderr, "USAGE:   %s [tty-device]\n", *argv);
    fprintf(stderr, "example: %s /dev/ttyS0\n\n", *argv);
    return 1;
  }

  envPath = getenv("PATH");
  path = (char *) malloc((strlen(envPath)+1) * sizeof(char));
  strcpy(path, envPath);
  pathElement = strtok(path, ":");
  while (pathElement)
  {
    rxvt = (char *) realloc(
                rxvt, (strlen(pathElement) + RXVT_LEN + 2) * sizeof(char));
    strcpy(rxvt, pathElement);
    strcpy(rxvt + strlen(pathElement), "/" RXVT);
    if ((stat(rxvt, &fstat) == 0) &&
        (fstat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
    {
      foundRxvt = 1;
      break;
    }
    pathElement = strtok(NULL, ":");
  }
  free(path);
  if (!foundRxvt)
  {
    fprintf(stderr, RXVT " binary not found in path!\n");
    return 1;
  }

  if ((fd = open(argv[1], O_RDWR)) < 0)
  {
    fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno));
    return 1;
  }
  if (tcgetattr(fd, &tflags) != 0)
  {
    fprintf(stderr, "error getting terminal settings of %s: %s\n",
                argv[1], strerror(errno));
    close(fd);
    return 1;
  }
  tflags.c_cflag |= CS8 | CRTSCTS | CLOCAL | CREAD;
  tflags.c_oflag = OPOST | ONLCR;
  tflags.c_iflag = 0;
  tflags.c_lflag = 0;
  if (tcsetattr(fd, TCSAFLUSH, &tflags) != 0)
  {
    fprintf(stderr, "error setting terminal settings of %s: %s\n",
                argv[1], strerror(errno));
    close(fd);
    return 1;
  }
  snprintf(fdnum, 16, "%d", fd);
  execl(rxvt, RXVT, "-pty-fd", &fdnum, NULL);
}


Compilieren:
code:
1:
gcc -osrxvt srxvt.c


Baudrate mit stty einstellen: z.B. stty -F /dev/ttyUSB0 57600
Dann srxvt starten: srxvt /dev/ttyUSB0

Es gibt natürlich noch Probleme: UTF-8 geht gar nicht. Da werde ich VIELLEICHT mal danach schauen, vielleicht hat ja auch hier jemand Lust auf entsprechende Ergänzungen.

__________________
palmen-it.de
GCS/MU d+(++) s+: a C++ UL++++ P+++$ L+++ !E W+++ N+ o? K? w++$ !O M-- V?
PS+ PE++ Y+ PGP++ t !5 X- R- tv b+ DI++ D+ G e++ h r y+
13.02.2009 11:15 Zirias ist offline Homepage von Zirias Beiträge von Zirias suchen
Baumstruktur | Brettstruktur
Gehe zu:
Neues Thema erstellen Antwort erstellen
BlackBoard » Design, Programmierung & Entwicklung » Programmieren » C [tool] rxvt direkt auf einer seriellen Konsole

Forensoftware: Burning Board 2.3.6, entwickelt von WoltLab GmbH