Skip to content

Commit

Permalink
support '-', '&FD', and pipes more consistently and for more places
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown committed Jan 11, 2007
1 parent ba203ec commit 667fa6e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 72 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -4,6 +4,7 @@ alpha:
fix int/unsigned problem: dvdauthor dumps core on some mpegs (Closes: Debian Bug#345105)
substantially reduce runtime memory requirements
fix numerous memory leaks
support '-', '&FD', and pipes more consistently and for more places


0.6.12: January 7, 2007
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.am
Expand Up @@ -24,10 +24,11 @@ spumux_SOURCES = subgen.c subgen.h rgb.h \
subgen-parse-xml.c readxml.c readxml.h \
subgen-encode.c subgen-image.c \
subconfig.h subglobals.h \
compat.c compat.h \
textsub.c textsub.h subrender.c subrender.h subreader.c subreader.h subfont.c subfont.h
spumux_LDADD = $(XML_LIBS) $(MAGICK_LIBS) $(FREETYPE_LIBS) $(FRIBIDI_LIBS) $(LIBICONV)

spuunmux_SOURCES = spuunmux.c rgb.h
spuunmux_SOURCES = spuunmux.c rgb.h compat.c compat.h
spuunmux_LDADD = -lpng -lz -lm

mpeg2desc_SOURCES = mpeg2desc.c
Expand Down
48 changes: 48 additions & 0 deletions src/compat.c
Expand Up @@ -2,6 +2,9 @@

#include "compat.h"

#include <ctype.h>
#include <fcntl.h>

#ifndef HAVE_STRSEP
/* Match STRING against the filename pattern PATTERN, returning zero if
it matches, nonzero if not. */
Expand All @@ -24,3 +27,48 @@ char *strsep(char **stringp, const char *delim)
return res;
}
#endif /* HAVE_STRSEP */

/* Mode is either O_RDONLY or O_WRONLY, nothing more */
struct vfile varied_open(const char *fname,int mode)
{
struct vfile vf;

if( !strcmp(fname,"-") ) {
vf.ftype=2;
if( mode==O_RDONLY )
vf.h=stdin;
else
vf.h=stdout;
return vf;
} else if( fname[0]=='&' && isdigit(fname[1])) {
vf.ftype=0;
vf.h=fdopen(atoi(fname+1),(mode==O_RDONLY)?"rb":"wb");
return vf;
} else if( mode==O_RDONLY ) {
int l=strlen(fname);
if( l>0 && fname[l-1]=='|' ) {
char *fcopy=strdup(fname);
fcopy[l-1]=0;
vf.ftype=1;
vf.h=popen(fcopy,"rb");
free(fcopy);
return vf;
}
} else if( fname[0]=='|' ) {
vf.ftype=1;
vf.h=popen(fname+1,"wb");
return vf;
}
vf.ftype=0;
vf.h=fopen(fname,(mode==O_RDONLY)?"rb":"wb");
return vf;
}

void varied_close(struct vfile vf)
{
switch( vf.ftype ) {
case 0: fclose(vf.h); break;
case 1: pclose(vf.h); break;
case 2: break;
}
}
7 changes: 7 additions & 0 deletions src/compat.h
Expand Up @@ -106,3 +106,10 @@ char *strsep(char **stringp,const char *delim);
#define FT_FREETYPE_H <freetype/freetype.h>
#define FT_GLYPH_H <freetype/ftglyph.h>
#endif

struct vfile {
FILE *h;
int ftype;
};
extern struct vfile varied_open(const char *fname,int mode);
extern void varied_close(struct vfile vf);
16 changes: 7 additions & 9 deletions src/dvdcli.c
Expand Up @@ -23,6 +23,7 @@

#include <assert.h>
#include <ctype.h>
#include <fcntl.h>

#include "conffile.h"
#include "dvdauthor.h"
Expand Down Expand Up @@ -172,14 +173,11 @@ static void parsechapters(char *o,struct source *src,int pauselen)
static void readpalette(struct pgc *p,const char *fname)
{
int i,rgbf;
FILE *h;
struct vfile h;

if(fname[0] == '&' && isdigit(fname[1]) )
h=fdopen(atoi(&fname[1]),"rb");
else
h=fopen(fname,"rb");
h=varied_open(fname,O_RDONLY);

if( !h ) {
if( !h.h ) {
fprintf(stderr,"ERR: Cannot open palette file '%s'\n",fname);
exit(1);
}
Expand All @@ -188,7 +186,7 @@ static void readpalette(struct pgc *p,const char *fname)
rgbf=( i>=4 && !strcasecmp(fname+i-4,".rgb") );
for( i=0; i<16; i++ ) {
int pcolor;
fscanf(h, "%x", &pcolor);
fscanf(h.h, "%x", &pcolor);
if( rgbf ) {
int r=(pcolor>>16)&255,
g=(pcolor>>8)&255,
Expand All @@ -204,7 +202,7 @@ static void readpalette(struct pgc *p,const char *fname)

memset(groups,0,24);
i=0;
while(fscanf(h,"%x",&b) == 1 && i<24 ) {
while(fscanf(h.h,"%x",&b) == 1 && i<24 ) {
groups[i++]=b>>24;
groups[i++]=b>>16;
groups[i++]=b>>8;
Expand All @@ -213,7 +211,7 @@ static void readpalette(struct pgc *p,const char *fname)
pgc_set_buttongroup(p,(i-8)/8,groups+(i-8));
}
#endif
fclose(h);
varied_close(h);
}

static void usage()
Expand Down
55 changes: 9 additions & 46 deletions src/dvdvob.c
Expand Up @@ -573,33 +573,6 @@ static void printpts(pts_t pts)
fprintf(stderr,"%d.%03d",(int)(pts/90000),(int)((pts/90)%1000));
}

static FILE *openvob(char *f,int *ispipe)
{
FILE *h;
int l=strlen(f);

if( l>0 && f[l-1]=='|' ) {

f[l-1]=0;
h=popen(f,"r");
ispipe[0]=1;
} else if( !strcmp(f,"-") ) {
h=stdin;
ispipe[0]=2;
} else if( f[0]=='&' && isdigit(f[1]) ) {
h=fdopen(atoi(&f[1]),"rb");
ispipe[0]=0;
} else {
h=fopen(f,"rb");
ispipe[0]=0;
}
if( !h ) {
fprintf(stderr,"ERR: Error opening %s: %s\n",f,strerror(errno));
exit(1);
}
return h;
}

enum { CR_BEGIN0, CR_BEGIN1, CR_BEGIN2, CR_BEGIN3, CR_SKIP0,
CR_SKIP1, CR_NEXTOFFS0, CR_NEXTOFFS1, CR_WAIT, CR_CMD,
CR_SKIPWAIT, CR_COL0, CR_COL1, CR_CHGARG, CR_CHGLN0,
Expand Down Expand Up @@ -846,9 +819,8 @@ static void audio_scan_pcm(struct audchannel *ach,unsigned char *buf,int len)
int FindVobus(char *fbase,struct vobgroup *va,int ismenu)
{
unsigned char *buf;
FILE *h;
int cursect=0,fsect=-1,vnum,outnum=-ismenu+1;
int ispipe,vobid=0;
int vobid=0;
struct mp2info {
int hdrptr;
unsigned char buf[6];
Expand All @@ -863,6 +835,7 @@ int FindVobus(char *fbase,struct vobgroup *va,int ismenu)
struct vob *s=va->vobs[vnum];
int prevvidsect=-1;
struct vscani vsi;
struct vfile vf;

vsi.lastrefsect=0;
for( i=0; i<32; i++ )
Expand All @@ -873,7 +846,11 @@ int FindVobus(char *fbase,struct vobgroup *va,int ismenu)
vsi.firstgop=1;

fprintf(stderr,"\nSTAT: Processing %s...\n",s->fname);
h=openvob(s->fname,&ispipe);
vf=varied_open(s->fname,O_RDONLY);
if( !vf.h ) {
fprintf(stderr,"ERR: Error opening %s: %s\n",s->fname,strerror(errno));
exit(1);
}
memset(mp2hdr,0,8*sizeof(struct mp2info));
while(1) {
if( fsect == 524272 ) {
Expand All @@ -887,7 +864,7 @@ int FindVobus(char *fbase,struct vobgroup *va,int ismenu)
}
buf=writegrabbuf();

i=fread(buf,1,2048,h);
i=fread(buf,1,2048,vf.h);
if( i!=2048 ) {
if( i==-1 ) {
fprintf(stderr,"\nERR: Error while reading: %s\n",strerror(errno));
Expand Down Expand Up @@ -1251,21 +1228,7 @@ int FindVobus(char *fbase,struct vobgroup *va,int ismenu)
cursect++;
fsect++;
}
switch(ispipe) {
case 0:
if (fclose(h)) {
fprintf(stderr,"\nERR: Error reading from file: %s\n",strerror(errno));
exit(1);
}
break;
case 1:
if (pclose(h)) {
fprintf(stderr,"\nERR: Error reading from pipe: %s\n",strerror(errno));
exit(1);
}
break;
case 2: break;
}
varied_close(vf);
if( s->numvobus ) {
int i;
pts_t finalaudiopts;
Expand Down
19 changes: 15 additions & 4 deletions src/readxml.c
Expand Up @@ -24,6 +24,7 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>

#include <libxml/xmlreader.h>

Expand All @@ -38,15 +39,25 @@
int parser_err=0, parser_acceptbody=0;
char *parser_body=0;

static int xml_varied_read(void *context,char *buffer,int len)
{
return fread(buffer,1,len,((struct vfile *)context)->h);
}

static int xml_varied_close(void *context)
{
varied_close(*((struct vfile *)context));
return 0;
}

int readxml(const char *xmlfile,struct elemdesc *elems,struct elemattr *attrs)
{
int curstate=0,statehistory[10];
xmlTextReaderPtr f;
struct vfile fd;

if( xmlfile[0]=='&' && isdigit(xmlfile[1]) )
f=xmlReaderForFd(atoi(xmlfile+1),xmlfile,NULL,0);
else
f=xmlNewTextReaderFilename(xmlfile);
fd=varied_open(xmlfile,O_RDONLY);
f=xmlReaderForIO(xml_varied_read,xml_varied_close,&fd,xmlfile,NULL,0);
if(!f) {
fprintf(stderr,"ERR: Unable to open XML file %s\n",xmlfile);
return 1;
Expand Down
24 changes: 12 additions & 12 deletions src/spuunmux.c
Expand Up @@ -697,7 +697,7 @@ static void usage(void)

int main(int argc, char **argv)
{
int fd;
struct vfile fd;
int option, n;
int rgb;
char *temp;
Expand Down Expand Up @@ -816,8 +816,8 @@ int main(int argc, char **argv)
add_offset = 450; // for rounding purposes

while (inc < Inc) {
fd = open(iname[inc], O_RDONLY | O_BINARY);
if (fd < 0) {
fd = varied_open(iname[inc], O_RDONLY);
if (fd.h == 0) {
fprintf(stderr, "error opening file %s\n", iname[inc]);

exit(-1);
Expand All @@ -828,7 +828,7 @@ int main(int argc, char **argv)

inc++;

while (read(fd, &c, 4) == 4) {
while (fread(&c, 1, 4, fd.h) == 4) {
c=ntohl(c);
if (c == 0x000001ba) { // start PS (Program stream)
static unsigned int old_system_time = -1;
Expand All @@ -837,7 +837,7 @@ int main(int argc, char **argv)
if (debug > 5)
fprintf(stderr, "pack_start_code\n");

if (read(fd, psbuf, PSBUFSIZE) < 1)
if (fread(psbuf, 1, PSBUFSIZE, fd.h) < 1)
break;

new_system_time = (psbuf[4] >> 3) + (psbuf[3] * 32) +
Expand Down Expand Up @@ -866,7 +866,7 @@ int main(int argc, char **argv)
if (debug > 5)
fprintf(stderr, "end packet\n");
} else {
read(fd, &package_length, 2);
fread(&package_length, 1, 2, fd.h);
package_length=ntohs(package_length);
if (package_length != 0) {

Expand All @@ -882,7 +882,7 @@ int main(int argc, char **argv)
case 0x01bd:
if (debug > 5)
fprintf(stderr, "private stream\n");
read(fd, cbuf, package_length);
fread(cbuf, 1, package_length, fd.h);

next_word = getpts(cbuf);
if (next_word != -1) {
Expand Down Expand Up @@ -953,7 +953,7 @@ int main(int argc, char **argv)
break;
case 0x01e0:
if( firstvideo==-1 ) {
read(fd, cbuf, package_length);
fread(cbuf, 1, package_length, fd.h);
firstvideo=getpts(cbuf);
add_offset-=firstvideo;
package_length=0;
Expand Down Expand Up @@ -983,7 +983,7 @@ int main(int argc, char **argv)
if (debug > 5)
fprintf(stderr, "padding stream %d bytes\n",
package_length);
read(fd, cbuf, package_length);
fread(cbuf, 1, package_length, fd.h);
if( package_length > 30 ) {
int i;

Expand Down Expand Up @@ -1112,7 +1112,7 @@ int main(int argc, char **argv)
package_length = 2;
while (next_word != 0x1ba) {
next_word = next_word << 8;
if (read(fd, &next_word, 1) < 1)
if (fread(&next_word, 1, 1, fd.h) < 1)
break;
package_length++;
}
Expand All @@ -1123,14 +1123,14 @@ int main(int argc, char **argv)
package_length);
goto l_01ba;
} /* end switch */
read(fd, cbuf, package_length);
fread(cbuf, 1, package_length, fd.h);
}

} /* end if 0xbd010000 */

} /* end while read 4 */

close(fd);
varied_close(fd);
} /* end while inc < Inc */

flushspus(0x7fffffff);
Expand Down

0 comments on commit 667fa6e

Please sign in to comment.