[lcdproc] New Patches for LCDd

David Douthitt n9ubh@callsign.net
Fri, 14 Sep 2001 19:16:04 -0400


--------------ED9DC3D6FEB149911E9E726B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I backtracked one patch (since my LCDd seemed to stop working) and tried
to get things going.

Applying this patch to 0.4.1 should be fine.  It replaces blindly all
sprintf with snprintf in the server, but mainly replaces sections of
code in the MtxOrb driver (MtxOrb.c) like so:

sprintf(out, "%cV%c", 254, 0);
write(fd, out, 3);

...with this...

write(fd, "\x0FEV\x000", 3);

In some cases, this results in out[] becoming unnecessary; many times it
removes the need for a sprintf() call.

I plan to hack on the driver more, especially since I am now the proud
owner of a LKD202-25 :)
--------------ED9DC3D6FEB149911E9E726B
Content-Type: text/plain; charset=us-ascii;
 name="lcdproc-0.4.1-patch4.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="lcdproc-0.4.1-patch4.diff"

diff -uNr lcdproc-0.4.1.orig/clients/lcdproc/main.c lcdproc-0.4.1/clients/lcdproc/main.c
--- lcdproc-0.4.1.orig/clients/lcdproc/main.c	Thu May 17 22:12:58 2001
+++ lcdproc-0.4.1/clients/lcdproc/main.c	Thu Sep 13 15:25:41 2001
@@ -153,7 +153,7 @@
 	usleep (500000);				  // wait for the server to start up
 	sock = sock_connect (server, port);
 	if (sock <= 0) {
-		printf ("Error connecting to server %s on port %i.\n", server, port);
+		printf ("Error connecting to LCD server %s on port %i.\nCheck to see that the server is running and operating normally.\n", server, port);
 		return 0;
 	}
 	sock_send_string (sock, "hello\n");
diff -uNr lcdproc-0.4.1.orig/server/client_functions.c lcdproc-0.4.1/server/client_functions.c
--- lcdproc-0.4.1.orig/server/client_functions.c	Thu May 24 21:23:21 2001
+++ lcdproc-0.4.1/server/client_functions.c	Thu Sep 13 16:45:01 2001
@@ -67,7 +67,7 @@
 	char str[256];
 
 	for (i = 0; i < argc; i++) {
-		sprintf (str, "test_func_func:  %i -> %s\n", i, argv[i]);
+		snprintf (str, sizeof(str), "test_func_func:  %i -> %s\n", i, argv[i]);
 		printf (str);
 		sock_send_string (c->sock, str);
 	}
@@ -88,7 +88,7 @@
 
 	debug ("Hello!\n");
 
-	sprintf (str, "connect LCDproc %s protocol %s lcd wid %i hgt %i cellwid %i cellhgt %i\n", version, protocol_version, lcd.wid, lcd.hgt, lcd.cellwid, lcd.cellhgt);
+	snprintf (str, sizeof(str), "connect LCDproc %s protocol %s lcd wid %i hgt %i cellwid %i cellhgt %i\n", version, protocol_version, lcd.wid, lcd.hgt, lcd.cellwid, lcd.cellhgt);
 	sock_send_string (c->sock, str);
 
 	if (c->data)
diff -uNr lcdproc-0.4.1.orig/server/drivers/MtxOrb.c lcdproc-0.4.1/server/drivers/MtxOrb.c
--- lcdproc-0.4.1.orig/server/drivers/MtxOrb.c	Thu May 17 20:30:17 2001
+++ lcdproc-0.4.1/server/drivers/MtxOrb.c	Fri Sep 14 18:07:26 2001
@@ -16,6 +16,10 @@
 #include "shared/debug.h"
 #include "shared/str.h"
 
+#define DEFAULT_CONTRAST 140
+#define DEFAULT_DEVICE "/dev/lcd"
+#define DEFAULT_BAUD B19200
+
 static int custom = 0;
 static enum {MTXORB_LCD, MTXORB_LKD, MTXORB_VFD, MTXORB_VKD} MtxOrb_type;
 
@@ -81,9 +85,9 @@
 	int i;
 	int tmp;
 
-	int contrast = 140;
-	char device[256] = "/dev/lcd";
-	int speed = B19200;
+	int contrast = DEFAULT_CONTRAST;
+	char device[256] = DEFAULT_DEVICE;
+	int speed = DEFAULT_BAUD;
 
 	MtxOrb = driver;
 
@@ -98,6 +102,10 @@
 	   }
 	 */
 
+	/*
+	 * This option processing; can it be replaced with getopt(3)?
+	 */
+
 	for (i = 0; i < argc; i++) {
 		//printf("Arg(%i): %s\n", i, argv[i]);
 		if (0 == strcmp (argv[i], "-d") || 0 == strcmp (argv[i], "--device")) {
@@ -162,10 +170,19 @@
 
 	}
 
+	/*
+	 * End of option processing...
+	 */
+
 	// Set up io port correctly, and open it...
 	fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY);
 	if (fd == -1) {
-		fprintf (stderr, "MtxOrb_init: failed (%s)\n", strerror (errno));
+		switch (errno) {
+			case ENOENT: fprintf(stderr, "MtxOrb_init: %s device file missing!\n", device);
+				break;
+			default: fprintf (stderr, "MtxOrb_init: failed (%s)\n", strerror (errno));
+				break;
+		}
 		return -1;
 	}
 	//else fprintf(stderr, "MtxOrb_init: opened device %s\n", device);
@@ -267,7 +284,7 @@
 //  printf("Flush (%i,%i)-(%i,%i)\n", lft, top, rgt, bot);
 
 	for (y = top; y <= bot; y++) {
-		sprintf (out, "%cG%c%c", 254, lft, y);
+		sprintf (out, "\x0FEG%c%c", lft, y);
 		write (fd, out, 4);
 		write (fd, lcd.framebuf + (y * lcd.wid) + lft, rgt - lft + 1);
 
@@ -302,7 +319,7 @@
 	if ( ((MtxOrb_type == MTXORB_LCD) || (MtxOrb_type == MTXORB_LKD)) &&
 	     (contrast > 0) ) {
 		status = contrast;
-		sprintf (out, "%cP%c", 254, status);
+		sprintf (out, "\x0FEP%c", status);
 		write (fd, out, 3);
 	}
 
@@ -318,14 +335,10 @@
 void
 MtxOrb_backlight (int on)
 {
-	char out[4];
 	if (on) {
-		sprintf (out, "%cB%c", 254, 0);
-		write (fd, out, 3);
-	} else {
-		sprintf (out, "%cF", 254);
-		write (fd, out, 2);
-	}
+		write (fd, "\x0FEB\x000", 3);
+	} else
+		write (fd, "\x0FEF", 2);
 }
 
 /////////////////////////////////////////////////////////////////
@@ -339,18 +352,17 @@
 	char out[5];
 	if ( ((MtxOrb_type == MTXORB_LCD) || (MtxOrb_type == MTXORB_VFD)) ) {
 		if (on) {
-			sprintf (out, "%cW", 254);
+			write (fd, "\x0FEW", 2);
 		} else {
-			sprintf (out, "%cV", 254);
+			write (fd, "\x0FEV", 2);
 		}
-		write (fd, out, 2);
 	} else {
 		int i;
 		for(i=0; i<6; i++) {
 			if ( on & (1 << i) ) {
-				sprintf (out, "%cW%c", 254, i+1);
+				sprintf (out, "\x0FEW%c", i+1);
 			} else {
-				sprintf (out, "%cV%c", 254, i+1);
+				sprintf (out, "\x0FEV%c", i+1);
 			}
 			write (fd, out, 3);
 		}
@@ -363,12 +375,10 @@
 static void
 MtxOrb_linewrap (int on)
 {
-	char out[4];
 	if (on)
-		sprintf (out, "%cC", 254);
+		write (fd, "\x0FEC", 2);
 	else
-		sprintf (out, "%cD", 254);
-	write (fd, out, 2);
+		write (fd, "\x0FED", 2);
 }
 
 /////////////////////////////////////////////////////////////////
@@ -377,12 +387,10 @@
 static void
 MtxOrb_autoscroll (int on)
 {
-	char out[4];
 	if (on)
-		sprintf (out, "%cQ", 254);
+		write (fd, "\x0FEQ", 2);
 	else
-		sprintf (out, "%cR", 254);
-	write (fd, out, 2);
+		write (fd, "\x0FER", 2);
 }
 
 // TODO: make sure this doesn't mess up non-VFD displays
@@ -394,10 +402,9 @@
 {
 	char out[4];
 	if (on)
-		sprintf (out, "%cS", 254);
+		write (fd, "\x0FES", 2);
 	else
-		sprintf (out, "%cT", 254);
-	write (fd, out, 2);
+		write (fd, "\x0FET", 2);
 }
 
 //// TODO: Might not be needed anymore...
@@ -407,6 +414,7 @@
 void
 MtxOrb_init_vbar ()
 {
+// Isn't this function supposed to go away?
 	MtxOrb_init_all (vbar);
 }
 
@@ -417,6 +425,7 @@
 void
 MtxOrb_init_hbar ()
 {
+// Isn't this function supposed to go away?
 	MtxOrb_init_all (hbar);
 }
 
@@ -507,8 +516,7 @@
 {
 	char out[3];
 	if (custom != bign) {
-		sprintf (out, "%cn", 254);
-		write (fd, out, 2);
+		write (fd, "\x0FEn", 2);
 		custom = bign;
 	}
 }
@@ -522,7 +530,7 @@
 MtxOrb_num (int x, int num)
 {
 	char out[5];
-	sprintf (out, "%c#%c%c", 254, x, num);
+	sprintf (out, "\x0FE#%c%c", x, num);
 	write (fd, out, 4);
 }
 
@@ -547,7 +555,7 @@
 	if (!dat)
 		return;
 
-	sprintf (out, "%cN%c", 254, n);
+	sprintf (out, "\x0FEN%c", n);
 	write (fd, out, 3);
 
 	for (row = 0; row < lcd.cellhgt; row++) {
@@ -623,7 +631,7 @@
         write(fd, dat, lcd.wid*lcd.hgt);
 */
 	for (i = 0; i < lcd.hgt; i++) {
-		sprintf (out, "%cG%c%c", 254, 1, i + 1);
+		sprintf (out, "\x0FEG\x001%c", i + 1);
 		write (fd, out, 4);
 		write (fd, dat + (lcd.wid * i), lcd.wid);
 	}
@@ -1031,6 +1039,8 @@
 
 // TODO: Remove this code wich was use for developpement.
 // PS: There might be reference to this code left, so keep it for some time.
+//
+// MtxOrb_init_hbar and MtxOrb_init_vbar use it; it's prototyped in MtxOrb.h ...
 void
 MtxOrb_init_all (int type)
 {
diff -uNr lcdproc-0.4.1.orig/server/drivers/MtxOrb.h lcdproc-0.4.1/server/drivers/MtxOrb.h
--- lcdproc-0.4.1.orig/server/drivers/MtxOrb.h	Thu Mar 30 14:43:33 2000
+++ lcdproc-0.4.1/server/drivers/MtxOrb.h	Fri Sep 14 18:07:14 2001
@@ -23,8 +23,9 @@
 void MtxOrb_draw_frame (char *dat);
 char MtxOrb_getkey ();
 
-void MtxOrb_init_all (int type);
 int MtxOrb_ask_bar (int type);
 void MtxOrb_set_known_char (int car, int type);
 
+// Isn't this function supposed to go away?
+void MtxOrb_init_all (int type);
 #endif
diff -uNr lcdproc-0.4.1.orig/server/drivers/joy.c lcdproc-0.4.1/server/drivers/joy.c
--- lcdproc-0.4.1.orig/server/drivers/joy.c	Thu May 17 20:01:40 2001
+++ lcdproc-0.4.1/server/drivers/joy.c	Thu Sep 13 15:25:41 2001
@@ -9,6 +9,9 @@
 #include <sys/types.h>
 
 #include <linux/joystick.h>
+#ifndef JSIOCGNAME
+#define JSIOCGNAME(len)           _IOC(_IOC_READ, 'j', 0x13, len)         /* get identifier string */
+#endif
 
 #include "shared/debug.h"
 #include "shared/str.h"
diff -uNr lcdproc-0.4.1.orig/server/input.c lcdproc-0.4.1/server/input.c
--- lcdproc-0.4.1.orig/server/input.c	Thu May 24 23:59:05 2001
+++ lcdproc-0.4.1/server/input.c	Thu Sep 13 16:45:26 2001
@@ -71,7 +71,7 @@
 		s = screenlist_current ();
 		if( s->keys && strchr( s->keys, key ) ) {
 			// This screen wants this key.  Tell it we got one
-			sprintf(str, "key %c\n", key);
+			snprintf(str, sizeof(str), "key %c\n", key);
 			sock_send_string(s->parent->sock, str);
 			// Nobody else gets this key
 		} else {
@@ -81,7 +81,7 @@
 				// If the client should have this keypress...
 				if( c->data->client_keys && strchr(c->data->client_keys, key) ) {
 					// Send keypress to client
-					sprintf(str, "key %c\n", key);
+					snprintf(str, sizeof(str), "key %c\n", key);
 					sock_send_string(c->sock, str);
 				};
 				c = (client *)LL_GetNext(clients);
diff -uNr lcdproc-0.4.1.orig/server/main.c lcdproc-0.4.1/server/main.c
--- lcdproc-0.4.1.orig/server/main.c	Thu May 17 20:01:40 2001
+++ lcdproc-0.4.1/server/main.c	Thu Sep 13 15:25:41 2001
@@ -36,6 +36,17 @@
 char *protocol_version = PROTOCOL_VERSION;
 char *build_date = __DATE__;
 
+/* Socket to bind to...
+
+   Using loopback is much more secure; it means that this port is
+   accessible only to programs running locally on the same host as LCDd.
+
+   Using variables for these means that (later) we can select which port
+   and which address to bind to at run time. */
+
+char *bind_addr = "127.0.0.1";
+int lcd_port = LCDPORT;
+
 /* no longer needed
 static screen_size sizes[] =
 {
diff -uNr lcdproc-0.4.1.orig/server/menu.c lcdproc-0.4.1/server/menu.c
--- lcdproc-0.4.1.orig/server/menu.c	Fri May 11 14:53:44 2001
+++ lcdproc-0.4.1/server/menu.c	Thu Sep 13 16:45:39 2001
@@ -303,7 +303,7 @@
 		value = readfunc (MENU_READ);
 		if (value < 0 || value >= MENU_CLOSE)
 			return value;
-		sprintf (str, "%i", value);
+		snprintf (str, sizeof(str), "%i", value);
 		if (lcd.hgt >= 4) {
 			lcd.string (8, 4, str);
 			value = (lcd.wid * lcd.cellwid * value / 256);
diff -uNr lcdproc-0.4.1.orig/server/parse.c lcdproc-0.4.1/server/parse.c
--- lcdproc-0.4.1.orig/server/parse.c	Thu May 17 20:01:40 2001
+++ lcdproc-0.4.1/server/parse.c	Thu Sep 13 16:46:00 2001
@@ -87,7 +87,7 @@
 					}
 				}
 				if (inquote) {
-					sprintf (errmsg, "huh? Unterminated string: missing %c\n", rightquote[inquote]);
+					snprintf (errmsg, sizeof(errmsg), "huh? Unterminated string: missing %c\n", rightquote[inquote]);
 					sock_send_string (c->sock, errmsg);
 					continue;
 				}
@@ -117,7 +117,7 @@
 				}
 				if (invalid) {
 					// FIXME:  Check for buffer overflows here...
-					sprintf (errmsg, "huh? Invalid command \"%s\"\n", argv[0]);
+					snprintf (errmsg, sizeof(errmsg), "huh? Invalid command \"%s\"\n", argv[0]);
 					sock_send_string (c->sock, errmsg);
 				}
 
diff -uNr lcdproc-0.4.1.orig/server/screenlist.c lcdproc-0.4.1/server/screenlist.c
--- lcdproc-0.4.1.orig/server/screenlist.c	Thu May 17 20:01:40 2001
+++ lcdproc-0.4.1/server/screenlist.c	Thu Sep 13 16:46:21 2001
@@ -112,7 +112,7 @@
 				c = old_s->parent;
 				if (c)				  // Tell the client we're not listening any more...
 				{
-					sprintf (str, "ignore %s\n", old_s->id);
+					snprintf (str, sizeof(str), "ignore %s\n", old_s->id);
 					sock_send_string (c->sock, str);
 				} else				  // The server has the display, so do nothing
 				{
@@ -125,7 +125,7 @@
 			c = s->parent;
 			if (c)					  // Tell the client we're paying attention...
 			{
-				sprintf (str, "listen %s\n", s->id);
+				snprintf (str, sizeof(str), "listen %s\n", s->id);
 				sock_send_string (c->sock, str);
 			} else					  // The server has the display, so do nothing
 			{
diff -uNr lcdproc-0.4.1.orig/server/serverscreens.c lcdproc-0.4.1/server/serverscreens.c
--- lcdproc-0.4.1.orig/server/serverscreens.c	Mon May 14 20:47:38 2001
+++ lcdproc-0.4.1/server/serverscreens.c	Thu Sep 13 16:46:58 2001
@@ -123,13 +123,13 @@
 	} while (LL_Next (clients) == 0);
 
 	if (lcd.hgt >= 3) {
-		sprintf (one, "Clients: %i", num_clients);
-		sprintf (two, "Screens: %i", num_screens);
+		snprintf (one, sizeof(one), "Clients: %i", num_clients);
+		snprintf (two, sizeof(two), "Screens: %i", num_screens);
 	} else {
 		if (lcd.wid >= 20)
-			sprintf (one, "%i Client%s, %i Screen%s", num_clients, (num_clients == 1) ? "" : "s", num_screens, (num_screens == 1) ? "" : "s");
+			snprintf (one, sizeof(one), "%i Client%s, %i Screen%s", num_clients, (num_clients == 1) ? "" : "s", num_screens, (num_screens == 1) ? "" : "s");
 		else							  // 16x2 size
-			sprintf (one, "%i Cli%s, %i Scr%s", num_clients, (num_clients == 1) ? "" : "s", num_screens, (num_screens == 1) ? "" : "s");
+			snprintf (one, sizeof(one), "%i Cli%s, %i Scr%s", num_clients, (num_clients == 1) ? "" : "s", num_screens, (num_screens == 1) ? "" : "s");
 	}
 
 	return 0;
diff -uNr lcdproc-0.4.1.orig/server/sock.c lcdproc-0.4.1/server/sock.c
--- lcdproc-0.4.1.orig/server/sock.c	Fri May 11 14:45:41 2001
+++ lcdproc-0.4.1/server/sock.c	Thu Sep 13 16:44:12 2001
@@ -18,6 +18,9 @@
 #include "clients.h"
 #include "shared/debug.h"
 
+extern char *bind_addr;
+extern int lcd_port;
+
 /**************************************************
   LCDproc sockets code...
 
@@ -34,7 +37,7 @@
 
 // Creates a socket in internet space
 int
-sock_create_inet_socket (unsigned short int port)
+sock_create_inet_socket (char * addr, unsigned short int port)
 {
 	struct sockaddr_in name;
 	int sock;
@@ -53,7 +56,8 @@
 	//debug("Binding Inet Socket\n");
 	name.sin_family = AF_INET;
 	name.sin_port = htons (port);
-	name.sin_addr.s_addr = htonl (INADDR_ANY);
+	inet_aton(addr, &name.sin_addr);
+
 	if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
 		perror ("Error binding socket");
 		return -1;
@@ -72,7 +76,7 @@
 	debug ("sock_create_server()\n");
 
 	/* Create the socket and set it up to accept connections. */
-	sock = sock_create_inet_socket (LCDPORT);
+	sock = sock_create_inet_socket (bind_addr, lcd_port);
 	if (sock < 0) {
 		perror ("sock_create_server: Error creating socket");
 		return -1;
@@ -235,7 +239,7 @@
 
 	for (i = 0; i < FD_SETSIZE; i++) {
 		// TODO:  Destroy a "client" here...?  Nope.
-		sock_send_string (i, "bye\n");
+		//sock_send_string (i, "bye\n");
 		close (i);
 		FD_CLR (i, &active_fd_set);
 		debug ("sock_close_all: Closed connection %i\n", i);


--------------ED9DC3D6FEB149911E9E726B
Content-Type: text/plain; charset=


-----------------------------------------------------------
To unsubscribe from this list send a blank message to
lcdproc-unsubscribe@lists.omnipotent.net
--------------ED9DC3D6FEB149911E9E726B--