Skip to content

Commit

Permalink
parse commandline, create symbolic links - used posix functions
Browse files Browse the repository at this point in the history
  • Loading branch information
anbr committed Dec 24, 2010
1 parent 50209cf commit fa36276
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 91 deletions.
122 changes: 65 additions & 57 deletions dvdplugin.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <vdr/remote.h>

#include <vdr/plugin.h>
#include <vdr/player.h>
#include <unistd.h>
Expand Down Expand Up @@ -62,63 +62,68 @@ cDVDPluginThread *cDVDPlugin::thread = NULL;

void cDVDPlugin::DetectDevice(void)
{
dsyslog("dvdswitch: Scan for DVD Device");
char *cmd = NULL;
char *output = NULL;
dsyslog("dvdswitch: parse cmdline of dvd-plugin for DVD Device");
char *file = NULL;
char *dvd = NULL;

if(0 < asprintf(&cmd, "ps -p %i -o cmd --no-header", getpid())) {
dsyslog("dvdswitch: Command: %s", cmd);

FILE *p = popen(cmd, "r");
if(p)
{
#if VDRVERSNUM >= 10318
cReadLine rl;
output = rl.Read(p);
#else
output = readline(p);
#endif
pclose(p);
if(0 < asprintf(&file, "/proc/%i/cmdline", getpid())) {
/* read /proc/`pidof vdr`/cmdline */
char buffer[1024];
int len = -1;

int fd = open(file,O_RDONLY);
if (fd >= 0) {
do {
len = read(fd,buffer,sizeof(buffer)); }
while (len == -1 && errno == EINTR);
close(fd);
} else {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not open %s:%s", file, szErr ? szErr : "");
if(szErr) free(szErr);
}

cTokenizer *token = new cTokenizer(output, " ");
for(int i = 1; i <= token->Count(); i++)
{
if(RegIMatch(token->GetToken(i), "^--plugin=dvd") ||
(RegIMatch(token->GetToken(i - 1), "^-P$") &&
RegIMatch(token->GetToken(i), "^dvd$")))
{
if(RegIMatch(token->GetToken(i + 1), "^-C$") &&
token->GetToken(i + 2))
{
dvd = strdup(token->GetToken(i + 2));
break;
}
if(RegIMatch(token->GetToken(i + 1), "^--dvd="))
{
const char *p = strchr(token->GetToken(i + 1), '=');
p++;
dvd = strdup(p);
break;
free(file);

if (len < 0) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not read %s:%s", file, szErr ? szErr : "");
if(szErr) free(szErr);
} else {
char *output = NULL;
for(int n = 0; n < len && !dvd;n += (strlen(output) + 1)) {
output = buffer + n;
//dsyslog("dvdswitch: parse '%s'", output);
if(0 == strncmp(output, "--plugin=dvd ",13)
|| 0 == strncmp(output, "-Pdvd ",6)
|| 0 == strncmp(output, "-P dvd ",7)
|| 0 == strncmp(output, "-P'dvd ",7)
|| 0 == strncmp(output, "-P\"dvd ",7)) {

cTokenizer *token = new cTokenizer(output, " ");
for(int i = 0; i <= token->Count(); i++) {
if(RegIMatch(token->GetToken(i), "^-C$") &&
token->GetToken(i + 1)) {
dvd = strdup(token->GetToken(i + 1));
break;
}
if(RegIMatch(token->GetToken(i), "^--dvd=")) {
const char *p = strchr(token->GetToken(i), '=');
dvd = strdup(p + 1);
break;
}
}
DELETENULL(token);
}
}
}
DELETENULL(token);

if(dvd)
{
isyslog("dvdswitch: Used DVD Device: %s", dvd);
if(!dvd) {
dvd = strdup("/dev/dvd");
}
if(dvd) {
isyslog("dvdswitch: use DVD Device: %s", dvd);
DVDSwitchSetup.SetDVDDevice(dvd);
free(dvd);
}
else
{
isyslog("dvdswitch: Use Default-DVD Device /dev/dvd");
DVDSwitchSetup.SetDVDDevice("/dev/dvd");
}

free(cmd);
}
}

Expand Down Expand Up @@ -163,14 +168,17 @@ void cDVDPlugin::ChangeLink(char *target)
{
if(target)
{
char *cmd = NULL;
int rc = 0;

if(0 < asprintf(&cmd, LINK, target, DVDSwitchSetup.DVDLink)) {
dsyslog("dvdswitch: Change link: %s", cmd);
rc = system(cmd);
dsyslog("dvdswitch: Change link got: %i", rc);
free(cmd);
isyslog("dvdswitch: create link %s to %s", DVDSwitchSetup.DVDLink, target);

if(-1 == unlink(DVDSwitchSetup.DVDLink) && errno != ENOENT) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not remove symbolic link %s : %s (%d)", DVDSwitchSetup.DVDLink, szErr ? szErr : "", errno);
if(szErr) free(szErr);
}
if(-1 == symlink(target, DVDSwitchSetup.DVDLink)) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not create link %s to %s :%s (%d)", DVDSwitchSetup.DVDLink, target, szErr ? szErr : "", errno);
if(szErr) free(szErr);
}
}
}
2 changes: 0 additions & 2 deletions dvdplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "helpers.h"
#include "setup.h"

#define LINK "ln -nfs '%s' '%s' 2> /dev/null"

class cDVDPluginThread : public cThread
{
private:
Expand Down
6 changes: 2 additions & 4 deletions dvdswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
#error "VDR-1.6.0 API version or greater is required!"
#endif

static const char *VERSION = "0.1.6";
static const char *DESCRIPTION = tr("Allowed to play DVD-Images");
//static const char *MAINMENUENTRY = tr("DVDSwitch");
static const char *VERSION = "0.1.7";

class cPluginDvdswitch : public cPlugin {
private:
Expand All @@ -33,7 +31,7 @@ class cPluginDvdswitch : public cPlugin {
cPluginDvdswitch(void);
virtual ~cPluginDvdswitch();
virtual const char *Version(void) { return VERSION; }
virtual const char *Description(void) { return tr(DESCRIPTION); }
virtual const char *Description(void) { return tr("Allows playback of DVD images"); }
virtual const char *CommandLineHelp(void);
virtual bool ProcessArgs(int argc, char *argv[]);
virtual bool Initialize(void);
Expand Down
34 changes: 15 additions & 19 deletions helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ void OsdMsg(eMessageType Type, const char *Msg)
#endif
}

void OSDErrorNumMsg(int err, const char* szDef)
{
char szErr[128];
int nErr = err;
szErr[sizeof(szErr)-1] = '\0';
if(0 != strerror_r(nErr,szErr,sizeof(szErr)-1)) {
szErr[0] = '\0';
}
esyslog(szErr[0] != '\0'?szErr:szDef);
OsdMsg(mtError, szErr[0] != '\0'?szErr:szDef);
}
char *get_strerror(int n)
{
char *s;
size_t size = 128;
s = (char*)malloc(size);
if (s == NULL)
return NULL;
strerror_r(n, s, size);
return s;
}

void SysLogErrorNumMsg(int err, const char* szDef)
void OSDErrorNumMsg(int err, const char* szDef)
{
char szErr[128];
int nErr = err;
szErr[sizeof(szErr)-1] = '\0';
if(0 != strerror_r(nErr,szErr,sizeof(szErr)-1)) {
szErr[0] = '\0';
}
esyslog(szErr[0] != '\0'?szErr:szDef);
char* szErr = get_strerror(err);
esyslog("dvdswitch: %s :%s", szDef, szErr ? szErr : "");
OsdMsg(mtError, szErr ? szErr : szDef);
if(szErr) free(szErr);
}

void ChangeChars(char *name, char *chars)
Expand Down
2 changes: 1 addition & 1 deletion helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

void OsdMsg(eMessageType Type, const char *Msg);
void OSDErrorNumMsg(int err, const char* szDef);
void SysLogErrorNumMsg(int err, const char* szDef);
void ChangeChars(char *name, char *chars);
void StrRepeat(const char *input, int count, char *dest);
bool RegIMatch(const char *string,const char *pattern);
char *get_strerror(int n);

// --- cStringValue -------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions po/de_DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ msgid ""
msgstr ""
"Project-Id-Version: vdr-dvdswitch-plugin 0.1.6\n"
"Report-Msgid-Bugs-To: <see README>\n"
"POT-Creation-Date: 2010-12-21 21:35+0100\n"
"POT-Creation-Date: 2010-12-23 20:45+0100\n"
"PO-Revision-Date: 2009-10-03 14:03+0200\n"
"Last-Translator:\n"
"Language-Team: <[email protected]>\n"
Expand Down Expand Up @@ -140,7 +140,7 @@ msgstr "Es wurde kein Verzeichnis ausgew
msgid "Now Read?"
msgstr "Jetzt einlesen?"

msgid "Allowed to play DVD-Images"
msgid "Allows playback of DVD images"
msgstr "Erlaubt das Abspielen von DVD-Abbildern"

msgid "Image Directory not readable or not exist"
Expand Down
4 changes: 2 additions & 2 deletions po/fr_FR.po
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ msgid ""
msgstr ""
"Project-Id-Version: vdr-dvdswitch-plugin 0.1.6\n"
"Report-Msgid-Bugs-To: <see README>\n"
"POT-Creation-Date: 2010-12-21 21:35+0100\n"
"POT-Creation-Date: 2010-12-23 20:45+0100\n"
"PO-Revision-Date: 2010-12-21 12:59+0100\n"
"Last-Translator: Nicolas Huillard <[email protected]>\n"
"Language-Team: <[email protected]>\n"
Expand Down Expand Up @@ -149,7 +149,7 @@ msgstr "Aucun dossier n'est choisi. Utiliser le standard?"
msgid "Now Read?"
msgstr "Lire maintenant?"

msgid "Allowed to play DVD-Images"
msgid "Allows playback of DVD images"
msgstr ""

msgid "Image Directory not readable or not exist"
Expand Down
14 changes: 10 additions & 4 deletions tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ void cFileDelThread::Action(void)
if(File) {
dsyslog("dvdswitch: Execute remove %s",File);
errno = 0;
if(!cFileCMD::Del(File))
SysLogErrorNumMsg(errno,"Operation file remove failed");
if(!cFileCMD::Del(File)) {
char* err = get_strerror(errno);
esyslog("dvdswitch: could not remove failed %s :%s", File, err ? err : "");
if(err) free(err);
}
}
delete(this);
};
Expand Down Expand Up @@ -134,8 +137,11 @@ void cFileMoveThread::Action(void)
if(0 < asprintf(&buffer, "%s/%s", Dest, FileName)) {
dsyslog("dvdswitch: Execute move %s to %s",File, buffer);
errno = 0;
if(!cFileCMD::Rn(File, buffer))
SysLogErrorNumMsg(errno,"Operation file remove failed");
if(!cFileCMD::Rn(File, buffer)) {
char* err = get_strerror(errno);
esyslog("dvdswitch: could not move file failed %s to %s :%s", File, buffer, err ? err : "");
if(err) free(err);
}
free(buffer);
}
}
Expand Down

0 comments on commit fa36276

Please sign in to comment.