wmtv: Config file handling.

Patch by Yann Vernier <yann@algonet.se>.  From [1]:

   wmtv failed to read my .wmtvrc (hand written) correctly, I tracked it
   down to incorrect memory management (sizeof() instead of strlen()).
   The code was so bad that I chose to rewrite it to a simpler version,
   patch below.

   Other bugs fixed include some of the memory leaks (which are quite
   numerous) and truncating the .wmtvrc when writing to it.

   Cosmetic changes include keeping comments in .wmtvrc, C++-style
   comments changed to C style, and numbering channels from 1.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=105325
This commit is contained in:
Doug Torrance 2016-02-01 00:45:08 -05:00 committed by Carlos R. Mafra
parent 0703ba0b82
commit 61e2c5f228

View file

@ -75,7 +75,7 @@
#define SETUNE 2 #define SETUNE 2
#define SETSPD 3 #define SETSPD 3
#define MAXCHAN 100 #define MAXCHAN 99
#define OPTIONS "hvd:g:e:b:" #define OPTIONS "hvd:g:e:b:"
#define TELEVISION 0 #define TELEVISION 0
@ -115,7 +115,6 @@ int fsheight = 0;
unsigned long ccrfreq; unsigned long ccrfreq;
unsigned long rfreq; unsigned long rfreq;
unsigned long st; unsigned long st;
unsigned long offset;
char *norm = NULL; char *norm = NULL;
char *source; char *source;
@ -123,8 +122,8 @@ char *mode = NULL;
char *fullscreen = NULL; char *fullscreen = NULL;
int freqnorm = -1; int freqnorm = -1;
char *cname[MAXCHAN]; char *cname[MAXCHAN];
char *wcname[MAXCHAN]; char *comment[MAXCHAN];
int ftune[MAXCHAN]; long ftune[MAXCHAN];
char *progname; char *progname;
char *dev = "/dev/video"; char *dev = "/dev/video";
@ -209,7 +208,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, c, opind; int i, c, opind;
// pid_t pid; /* pid_t pid; */
char cfile[128]; char cfile[128];
static struct option long_options[] = { static struct option long_options[] = {
{"display", 1, 0, 'd'}, {"display", 1, 0, 'd'},
@ -402,8 +401,8 @@ main(int argc, char *argv[])
} }
else if (ntfb_status == SETUNE) { else if (ntfb_status == SETUNE) {
if (!btime) { if (!btime) {
offset = (rfreq - ccrfreq); ftune[cchannel] = (rfreq - ccrfreq);
// fprintf(stderr, "wmtv: finetune offset = %ld\n", offset); /* fprintf(stderr, "wmtv: finetune offset = %ld\n", ftune[cchannel]); */
WriteRCFile(cfile); WriteRCFile(cfile);
ntfb_status = SETON; ntfb_status = SETON;
DrawPresetChan(cchannel); DrawPresetChan(cchannel);
@ -441,10 +440,10 @@ main(int argc, char *argv[])
ntfb_status = SETOFF; ntfb_status = SETOFF;
TVOff(); TVOff();
system(exe); system(exe);
/* #if 0
pid = fork(); pid = fork();
// child /* child */
if (pid == (pid_t) 0) { if (pid == (pid_t) 0) {
execlp("xawtv", "xawtv", "&", (char *) 0); execlp("xawtv", "xawtv", "&", (char *) 0);
} }
@ -453,11 +452,11 @@ main(int argc, char *argv[])
perror("fork"); perror("fork");
} }
// parent /* parent */
else { else {
if (waitpid(pid, NULL, 0) < 0) { if (waitpid(pid, NULL, 0) < 0) {
perror("waitpid"); perror("waitpid");
*/ #endif
} }
else { else {
DoFullScreen(); DoFullScreen();
@ -808,7 +807,7 @@ MuteAudio(void)
{ {
if (vchn.flags & VIDEO_VC_AUDIO) { if (vchn.flags & VIDEO_VC_AUDIO) {
vaud.audio = tvsource; vaud.audio = tvsource;
// vaud.volume = 0; /* vaud.volume = 0; */
vaud.flags |= VIDEO_AUDIO_MUTE; vaud.flags |= VIDEO_AUDIO_MUTE;
if (ioctl(tfd, VIDIOCSAUDIO, &vaud) < 0) if (ioctl(tfd, VIDIOCSAUDIO, &vaud) < 0)
perror("ioctl VIDIOCSAUDIO"); perror("ioctl VIDIOCSAUDIO");
@ -823,7 +822,7 @@ UnMuteAudio(void)
{ {
if ((vchn.flags & VIDEO_VC_AUDIO) && (vaud.flags & VIDEO_AUDIO_MUTE)) { if ((vchn.flags & VIDEO_VC_AUDIO) && (vaud.flags & VIDEO_AUDIO_MUTE)) {
vaud.audio = tvsource; vaud.audio = tvsource;
// vaud.volume = (0xFFFF/2)+1; /* vaud.volume = (0xFFFF/2)+1; */
vaud.flags &= ~VIDEO_AUDIO_MUTE; vaud.flags &= ~VIDEO_AUDIO_MUTE;
if (ioctl(tfd, VIDIOCSAUDIO, &vaud) < 0) if (ioctl(tfd, VIDIOCSAUDIO, &vaud) < 0)
perror("ioctl VIDIOCSAUDIO"); perror("ioctl VIDIOCSAUDIO");
@ -900,7 +899,7 @@ void
ChanUp(void) ChanUp(void)
{ {
int i; int i;
if (cchannel != maxpst) if (cchannel < maxpst)
++cchannel; ++cchannel;
for (i=0; i < CHAN_ENTRIES; i++) { for (i=0; i < CHAN_ENTRIES; i++) {
if (!strcmp(cname[cchannel], tvtuner[i].name)) { if (!strcmp(cname[cchannel], tvtuner[i].name)) {
@ -947,7 +946,7 @@ DrawPresetChan(int cchannel)
int k=6; int k=6;
if (isource == TELEVISION) { if (isource == TELEVISION) {
sprintf(temp, "%02d", cchannel); sprintf(temp, "%02d", cchannel+1);
if (*p == '0') { if (*p == '0') {
copyXPMArea(66, 79, 5, 7, k, 50); copyXPMArea(66, 79, 5, 7, k, 50);
@ -980,7 +979,7 @@ DrawPresetChan(int cchannel)
void void
ParseRCFile(const char *filename, rckeys *keys) ParseRCFile(const char *filename, rckeys *keys)
{ {
char *p,*q; char *p;
char temp[128]; char temp[128];
char *tokens = " =\t\n"; char *tokens = " =\t\n";
FILE *fp; FILE *fp;
@ -993,20 +992,17 @@ ParseRCFile(const char *filename, rckeys *keys)
} }
norcfile = 0; norcfile = 0;
while (fgets(temp, 128, fp)) { while (fgets(temp, 128, fp)) {
key = 0; if (temp[0] != '\n') {
q = strdup(temp); p = strtok(temp, tokens);
if (*q != '\n') { for (key=0; keys[key].label; key++) {
q = strtok(q, tokens); if ((!strcmp(p, keys[key].label))) {
while (key >= 0 && keys[key].label) {
if ((!strcmp(q, keys[key].label))) {
p = strtok(NULL, tokens); p = strtok(NULL, tokens);
free(*keys[key].var); free(*keys[key].var);
*keys[key].var = strdup(p); *keys[key].var = strdup(p);
key = -1; break;
} else key++; }
} }
} }
free(q);
} }
fclose(fp); fclose(fp);
} }
@ -1017,12 +1013,9 @@ void
ParseRCFile2(const char *filename) ParseRCFile2(const char *filename)
{ {
int menu = FALSE; int menu = FALSE;
char temp[128]; char temp[128], name[128];
char tp[10];
char *tokens = " \t\n()";
char *q, *p;
FILE *fp; FILE *fp;
int i = 0; int len, i = 0;
if ((fp = fopen(filename, "r")) == NULL) { if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "wmtv: %s\n", strerror(errno)); fprintf(stderr, "wmtv: %s\n", strerror(errno));
@ -1031,42 +1024,22 @@ ParseRCFile2(const char *filename)
} }
norcfile = 0; norcfile = 0;
while (fgets(temp, 128, fp)) { while (fgets(temp, 128, fp)) {
q = strdup(temp); if (*temp != '\n') {
if (*q != '\n') {
q = strtok(q, tokens);
if (menu) { if (menu) {
cname[i] = (char *)malloc(sizeof(q)); ftune[i]=0;
strncpy(cname[i], q, sizeof(q)); if(sscanf(temp, " %s %n(%ld) %n", name, &len, &ftune[i], &len)>=1) {
p = q; cname[i]=strdup(name);
p = strtok(NULL, tokens); comment[i]=temp[len]?strdup(temp+len):"\n";
if (p != NULL) {
if (*p == '-') {
p++;
ftune[i] = -1*atoi(p);
}
else if (*p == '+') {
p++;
ftune[i] = atoi(p);
}
else {
ftune[i] = atoi(p);
}
} }
else { if(++i>=MAXCHAN)
ftune[i] = 0; break;
}
wcname[i] = (char *)malloc(sizeof(cname[i])+sizeof(p));
wcname[i] = strdup(cname[i]);
sprintf(tp, " (%d) ", ftune[i]);
strcat(wcname[i], tp);
i++;
tpst = i;
} }
if ((q = strchr(q, '[')) != NULL) { else if (strchr(temp, '[')) {
menu = TRUE; menu = TRUE;
} }
} }
} }
tpst = i;
fclose(fp); fclose(fp);
} }
@ -1075,11 +1048,8 @@ ParseRCFile2(const char *filename)
void void
WriteRCFile(const char *filename) WriteRCFile(const char *filename)
{ {
int i; long i;
char temp[128]; char temp[128];
char tp[10];
char *tokens = " \t\n()";
char *q;
FILE *fp; FILE *fp;
if ((fp = fopen(filename, "r+")) == NULL) { if ((fp = fopen(filename, "r+")) == NULL) {
@ -1087,24 +1057,17 @@ WriteRCFile(const char *filename)
return; return;
} }
while (fgets(temp, 128, fp)) { while (fgets(temp, 128, fp)) {
q = strdup(temp); if (*temp != '\n' && strchr(temp, '[')) {
if (*q != '\n') { fseek(fp, 0L, SEEK_CUR); /* required between read and write */
q = strtok(temp, tokens); for (i = 0; i < maxpst; i++)
if ((q = strchr(q, '[')) != NULL) { fprintf(fp, "%s (%ld)\t%s", cname[i], ftune[i], comment[i]);
for (i = 0; i <= maxpst; i++) { break;
// fprintf(stderr, "offset is %ld\n", offset);
sprintf(tp, " (%ld) ", offset);
strtok(wcname[cchannel], tokens);
strcat(wcname[cchannel], tp);
fputs(wcname[i], fp);
fputs("\n", fp);
}
}
} }
} }
i=ftell(fp);
fclose(fp); fclose(fp);
truncate(filename, i);
} }