httpd: sendfile support
[oweals/busybox.git] / networking / telnet.c
index 82dbb588d558867c1129715e51db7fc42a378aa5..4e8b27ba61b1d8f9c91d8710742955d6b26b8bf5 100644 (file)
@@ -24,7 +24,7 @@
 #include <termios.h>
 #include <arpa/telnet.h>
 #include <netinet/in.h>
-#include "busybox.h"
+#include "libbb.h"
 
 #ifdef DOTRACE
 #define TRACE(x, y) do { if (x) printf y; } while (0)
@@ -52,7 +52,6 @@ enum {
 
 typedef unsigned char byte;
 
-
 struct globals {
        int     netfd; /* console fd:s are 0 and 1 (and 2) */
        short   iaclen; /* could even use byte */
@@ -72,14 +71,19 @@ struct globals {
        int     win_width, win_height;
 #endif
        /* same buffer used both for network and console read/write */
-       char    buf[DATABUFSIZE]; /* allocating so static size is smaller */
+       char    buf[DATABUFSIZE];
+       /* buffer to handle telnet negotiations */
        char    iacbuf[IACBUFSIZE];
        struct termios termios_def;
        struct termios termios_raw;
 };
-
-#define G (*(struct globals*)bb_common_bufsiz1)
-
+#define G (*(struct globals*)&bb_common_bufsiz1)
+void BUG_telnet_globals_too_big(void);
+#define INIT_G() do { \
+       if (sizeof(G) > COMMON_BUFSIZE) \
+               BUG_telnet_globals_too_big(); \
+       /* memset(&G, 0, sizeof G); - already is */ \
+} while (0)
 
 /* Function prototypes */
 static void rawmode(void);
@@ -97,8 +101,7 @@ static void iacflush(void)
 
 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
 
-static void    /* buffer to handle telnet negotiations */
-doexit(int ev)
+static void doexit(int ev)
 {
        cookmode();
        exit(ev);
@@ -151,6 +154,7 @@ static void conescape(void)
        G.gotsig = 0;
 
 }
+
 static void handlenetoutput(int len)
 {
        /*      here we could do smart tricks how to handle 0xFF:s in output
@@ -187,11 +191,10 @@ static void handlenetoutput(int len)
                else if (*p == 0x0d)
                        outbuf[j++] = 0x00;
        }
-       if (j > 0 )
+       if (j > 0)
                write(G.netfd, outbuf, j);
 }
 
-
 static void handlenetinput(int len)
 {
        int i;
@@ -266,15 +269,11 @@ static void handlenetinput(int len)
                write(1, G.buf, len);
 }
 
-
-/* ******************************* */
-
 static void putiac(int c)
 {
        G.iacbuf[G.iaclen++] = c;
 }
 
-
 static void putiac2(byte wwdd, byte c)
 {
        if (G.iaclen + 3 > IACBUFSIZE)
@@ -354,24 +353,17 @@ static void putiac_naws(byte c, int x, int y)
 }
 #endif
 
-/* void putiacstring (subneg strings) */
-
-/* ******************************* */
-
-static char const escapecharis[] = "\r\nEscape character is ";
+static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
 
 static void setConMode(void)
 {
-       if (G.telflags & UF_ECHO)
-       {
+       if (G.telflags & UF_ECHO) {
                if (G.charmode == CHM_TRY) {
                        G.charmode = CHM_ON;
                        printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
                        rawmode();
                }
-       }
-       else
-       {
+       } else {
                if (G.charmode != CHM_OFF) {
                        G.charmode = CHM_OFF;
                        printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
@@ -380,8 +372,6 @@ static void setConMode(void)
        }
 }
 
-/* ******************************* */
-
 static void will_charmode(void)
 {
        G.charmode = CHM_TRY;
@@ -404,8 +394,6 @@ static void do_linemode(void)
        iacflush();
 }
 
-/* ******************************* */
-
 static void to_notsup(char c)
 {
        if (G.telwish == WILL)
@@ -517,11 +505,7 @@ static void telopt(byte c)
        }
 }
 
-
-/* ******************************* */
-
 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
-
 static int subneg(byte c)
 {
        switch (G.telstate) {
@@ -548,8 +532,6 @@ static int subneg(byte c)
        return FALSE;
 }
 
-/* ******************************* */
-
 static void fgotsig(int sig)
 {
        G.gotsig = sig;
@@ -558,16 +540,16 @@ static void fgotsig(int sig)
 
 static void rawmode(void)
 {
-       if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
+       if (G.do_termios)
+               tcsetattr(0, TCSADRAIN, &G.termios_raw);
 }
 
 static void cookmode(void)
 {
-       if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
+       if (G.do_termios)
+               tcsetattr(0, TCSADRAIN, &G.termios_def);
 }
 
-void BUG_telnet_globals_too_big(void);
-
 int telnet_main(int argc, char** argv);
 int telnet_main(int argc, char** argv)
 {
@@ -581,9 +563,7 @@ int telnet_main(int argc, char** argv)
        int maxfd;
 #endif
 
-       if (sizeof(G) > sizeof(bb_common_bufsiz1))
-               BUG_telnet_globals_too_big();
-       /* memset(&G, 0, sizeof G); - already is */
+       INIT_G();
 
 #if ENABLE_FEATURE_AUTOWIDTH
        get_terminal_width_height(0, &G.win_width, &G.win_height);