add comments, use getopt, cleanup, ...

This commit is contained in:
marschap
2006-01-18 21:17:39 +00:00
parent 5b4c15c2a6
commit 34cf0696ae
3 changed files with 163 additions and 158 deletions
+6 -6
View File
@@ -32,7 +32,7 @@
# The name of the driver is exactly the same as the name of the driver
# module that is to be loaded, including the case of the letters !
# (That is: unless a file= option is given in the driver section)
Driver=curses
Driver=CFontzPacket
#Driver=hd44780
#Driver=IOWarrior
#Driver=MtxOrb
@@ -187,13 +187,13 @@ Reboot=yes
[CFontzPacket]
# Select the LCD model [default: 633; legal: 631, 633, 635]
Model=633
Model=635
# Select the output device to use [default: /dev/lcd]
Device=/dev/ttyS0
Device=/dev/ttyUSB0
# Select the LCD size [default: depending on model: 635: 20x4, 631 & 633: 16x2]
Size=16x2
Size=20x4
# Set the initial contrast [default: 140; legal: 0 - 1000]
Contrast=140
Contrast=350
# Set the initial brightness [default: 1000; legal: 0 - 1000]
Brightness=1000
# Set the initial off-brightness [default: 0; legal: 0 - 1000]
@@ -201,7 +201,7 @@ Brightness=1000
# switched off in case LCDd is inactive
OffBrightness=50
# Set the communication speed [default: 9600; legal: 1200, 2400, 9600, 19200, 115200]
Speed=19200
Speed=115200
# Set the firmware version (New means >= 2.0) [default: no; legal: yes, no]
# Currently this flag is not in use, there is no such thing as NewFirmware. ;=)
#NewFirmware=no
+89 -83
View File
@@ -1,3 +1,5 @@
#include "getopt.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
@@ -49,29 +51,29 @@ static int islow = -1;
// 1/8th second is a single time unit...
#define TIME_UNIT 125000
// Contains a list of modes to run
/* list of modes to run */
static mode default_sequence[] =
{
// which on off inv timer/visible
{'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{'A', 999, 9999, 0, 0xffff, 0,}, // [A]bout (credits)
// longname which on off inv timer visible
{ "CPU", 'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{ "Memory", 'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{ "Load", 'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{ "TimeDate", 'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{ "About", 'A', 999, 9999, 0, 0xffff, 0,}, // [A]bout (credits)
{1, 0, 0, 0, 0, 0,}, // Modes after this line will not be run by default...
{ NULL, 1, 0, 0, 0, 0, 0,}, // Modes after this line will not be run by default...
// ... all non-default modes must be in here!
// ... they will not show up otherwise.
{'P', 1, 2, 0, 0xffff, 0,}, // [O]ld Timescreen
{'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{'G', 1, 2, 0, 0xffff, 0,}, // Cpu histogram [G]raph
{'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock
{0, 0, 0, 0, 0,}, // No more.. all done.
{ "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0,}, // CPU_SM[P]
{ "OldTime", 'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{ "BigClock", 'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{ "Uptime", 'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{ "Battery", 'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{ "CPUGraph", 'G', 1, 2, 0, 0xffff, 0,}, // CPU histogram [G]raph
{ "ProcSize", 'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{ "Disk", 'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{ "MiniClock", 'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock
{ NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
};
// TODO: Config file; not just command line
@@ -94,115 +96,114 @@ const char *get_sysrelease()
int
main(int argc, char **argv)
{
int i, j;
int c;
char *server = NULL;
int i, j, seqlen;
int port = LCDPORT;
int daemonize = FALSE;
/* determine length of default_sequence[] */
int seqlen = sizeof(default_sequence) / sizeof(mode);
if(uname(&unamebuf) == -1)
{
/* get uname information */
if (uname(&unamebuf) == -1) {
perror("uname");
return(EXIT_FAILURE);
}
seqlen = 0;
while(default_sequence[seqlen].which != 0)
seqlen++;
seqlen++;
/* allocate sequence */
sequence = (mode *) malloc(seqlen * sizeof(mode));
if(sequence == NULL)
{
if (sequence == NULL) {
perror("sequence malloc");
return(EXIT_FAILURE);
}
for(i = 0; i < seqlen; i++)
{
/* copy default_sequence[] to sequence */
for (i = 0; i < seqlen; i++) {
//sequence[i] = default_sequence[i];
memcpy(&sequence[i], &default_sequence[i], sizeof(mode));
}
// Ctrl-C will cause a clean exit...
signal(SIGINT, exit_program);
// and "kill"...
signal(SIGTERM, exit_program);
// and "kill -HUP" (hangup)...
signal(SIGHUP, exit_program);
// and just in case, "kill -KILL" (which cannot be trapped; but oh well)
signal(SIGKILL, exit_program);
/* setup error handlers */
signal(SIGINT, exit_program); // Ctrl-C
signal(SIGTERM, exit_program); // "regular" kill
signal(SIGHUP, exit_program); // kill -HUP
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
// Command line
for(i = 1, j = 0; i < argc; i++)
{
if(argv[i][0] == '-')
{
switch(argv[i][1])
{
/* No error output from getopt */
opterr = 0;
/* get options */
while ((c = getopt( argc, argv, "s:p:e:d")) > 0) {
switch (c) {
// s is for server
case 'S':
case 's':
if(argc < i + 1)
HelpScreen();
if (server == NULL)
server = argv[++i];
server = optarg;
else
fprintf(stderr, "Ignoring additional server: %s\n", argv[++i]);
fprintf(stderr, "Ignoring additional server: %s\n", optarg);
break;
// p is for port
case 'P':
case 'p':
if(argc < i + 1)
HelpScreen();
port = atoi(argv[++i]);
if(port < 1 && port > 0xffff)
fprintf(stderr, "Warning: Port %d outside of standard range\n", port);
port = atoi(optarg);
if ((port < 1) && (port > 0xFFFF)) {
fprintf(stderr, "Warning: Port %d outside of legal range\n", port);
return(EXIT_FAILURE);
}
break;
case 'e':
case 'E':
if(argc < i + 1)
HelpScreen();
islow = atoi(argv[++i]);
islow = atoi(optarg);
break;
case 'd':
case 'D':
daemonize = TRUE;
break;
// otherwise... Get help!
case '?': // unknown option or missing argument
default:
HelpScreen();
break;
}
}
else if(strlen(argv[i]) == 1)
{
/* parse arguments */
for (i = (optind > 0) ? optind : 1, j = 0; i < argc; i++) {
int shortname = (strlen(argv[i]) == 1) ? toupper(argv[i][0]) : '\0';
int k, found = FALSE;
/* skip this round if we used all allocated slots */
if (j >= seqlen)
continue;
// Grab the mode letter...
sequence[j].which = argv[i][0];
/* ignore already selected modes */
for (k = 0; (k < j) && (sequence[k].which != 0); k++) {
if (((sequence[k].longname != NULL) &&
(0 == strcasecmp(argv[i], sequence[k].longname))) ||
(shortname == sequence[k].which)) {
found = TRUE;
}
}
if (found) {
fprintf(stderr, "Warning: ignoring duplicate mode\n");
continue;
}
for(k = 0; k < seqlen; k++)
{
if(toupper(sequence[j].which) == default_sequence[k].which)
{
/* try to find the mode from the mode list */
for (k = 0; k < seqlen; k++) {
if (((default_sequence[k].longname != NULL) &&
(0 == strcasecmp(argv[i], default_sequence[k].longname))) ||
(shortname == default_sequence[k].which)) {
//sequence[j] = default_sequence[k];
memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
j++;
//sequence[j] = default_sequence[seqlen-1];
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
found = TRUE;
break;
}
}
if(!found)
{
if (!found) {
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
}
else
{
j++;
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
//sequence[j] = default_sequence[seqlen-1];
}
return(EXIT_FAILURE);
}
}
@@ -212,11 +213,13 @@ main(int argc, char **argv)
// Connect to the server...
usleep(500000); // wait for the server to start up
sock = sock_connect(server, port);
if(sock <= 0)
{
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;
if (sock <= 0) {
fprintf(stderr, "Error connecting to LCD server %s on port %d.\n"
"Check to see that the server is running and operating normally.\n",
server, port);
return(EXIT_FAILURE);
}
sock_send_string(sock, "hello\n");
usleep(500000); // wait for the server to say hi.
@@ -226,9 +229,12 @@ main(int argc, char **argv)
lcd_cellwid = 5;
lcd_cellhgt = 8;
if(daemonize)
if(daemon(1,0) != 0)
if (daemonize) {
if (daemon(1,0) != 0) {
fprintf(stderr, "Error: daemonize failed");
return(EXIT_FAILURE);
}
}
// Init the status gatherers...
mode_init();
+1 -2
View File
@@ -32,6 +32,7 @@ extern int lcd_cellhgt;
typedef struct mode
{
char *longname; // Which screen is it?
char which; // Which screen is it?
int on_time; // How often to update while visible?
int off_time; // How often to get stats while not visible?
@@ -62,6 +63,4 @@ const char *get_sysrelease();
# define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#endif