-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.c
228 lines (201 loc) · 4.73 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Image plugin to VDR (C++)
*
* (c) 2004-2011 Andreas Brachold <vdr07 at deltab.de>
*
* This code is distributed under the terms and conditions of the
* GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
*
*/
#include <malloc.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "commands.h"
#include "setup-image.h"
// --- cImageCommand -------------------------------------------------------------
char *cImageCommand::m_szLastResult = NULL;
cImageCommand::cImageCommand(void)
: m_szTitle(NULL)
, m_szCommand(NULL)
, m_bConfirm(false)
{
}
cImageCommand::~cImageCommand()
{
if(m_szTitle)
{
free(m_szTitle);
m_szTitle = NULL;
}
if(m_szCommand)
{
free(m_szCommand);
m_szCommand = NULL;
}
}
bool cImageCommand::Parse(const char *s)
{
const char *p = strchr(s, ':');
if(p)
{
int l = p - s;
if(l > 0)
{
m_szTitle = MALLOC(char, l + 1);
stripspace(strn0cpy(m_szTitle, s, l + 1));
if(!isempty(m_szTitle))
{
int l = strlen(m_szTitle);
if(l > 1 && m_szTitle[l - 1] == '?')
{
m_bConfirm = true;
m_szTitle[l - 1] = 0;
}
m_szCommand = stripspace(strdup(skipspace(p + 1)));
return !isempty(m_szCommand);
}
}
}
return false;
}
const char *cImageCommand::Execute(const char *szFileName)
{
char *szCmdBuf = NULL;
if(m_szLastResult)
{
free(m_szLastResult);
m_szLastResult = NULL;
}
// Combine command and filename
if(szFileName && m_szCommand) {
if(NULL == strstr(m_szCommand, "%s")) {
/// Merge command and filename e.g : identify 'my_image.png'
asprintf(&szCmdBuf, "%s \'%s\'", m_szCommand, szFileName);
}
else {
/// Replace Mode, replace any %s with filename
/// e.g. : jpegtran -rotate 90 %s > $CONVERT_TEMPDIR/tmp.jpg && mv $CONVERT_TEMPDIR/tmp.jpg %s
/// would to jpegtran -rotate 90 'my_image.png' > $CONVERT_TEMPDIR/tmp.jpg && mv $CONVERT_TEMPDIR/tmp.jpg 'my_image.png'
char *szF;
asprintf(&szF, "\'%s\'", szFileName);
if(szF) {
szCmdBuf = (char*)calloc(PATH_MAX,1);
if(szCmdBuf)
{
char* d = szCmdBuf;
char* s = m_szCommand;
for(;d-szCmdBuf < PATH_MAX-1 && *s != '\0';++s,++d)
{
if(*s == '%' && *(s+1) == 's')
{
strncat(szCmdBuf,szF,PATH_MAX-1);
d += strlen(szF)-1;
++s;
}
else
*d = *s;
}
}
}
free(szF);
}
}
const char *szCmd = szCmdBuf ? szCmdBuf : m_szCommand;
dsyslog("imageplugin: executing command '%s'", szCmd);
// Set environment
ImageSetup.SetEnv();
FILE *p = popen(szCmd, "r");
if(p)
{
int l = 0;
int c;
while((c = fgetc(p)) != EOF)
{
if(l % 20 == 0) {
m_szLastResult = (char *)realloc(m_szLastResult, l + 21);
if(!m_szLastResult)
break;
}
m_szLastResult[l++] = c;
}
if(m_szLastResult)
m_szLastResult[l] = 0;
pclose(p);
}
else
esyslog("imageplugin: can't open pipe for command '%s'", szCmd);
if(szCmdBuf)
free(szCmdBuf);
return m_szLastResult;
}
cImageCommands::cImageCommands(void)
: m_szFileName(NULL)
{
}
cImageCommands::~ cImageCommands()
{
Clear();
}
void cImageCommands::Clear(void)
{
if(m_szFileName)
free(m_szFileName);
m_szFileName = NULL;
cList < cImageCommand >::Clear();
}
bool cImageCommands::Load(const char *szFileName/* = NULL*/, bool bAllowComments /*=true*/, bool bMustExist /*= false*/)
{
Clear();
if(szFileName)
{
m_szFileName = strdup(szFileName);
m_bAllowComments = bAllowComments;
}
bool bRet = !bMustExist;
if(m_szFileName && access(m_szFileName, F_OK) == 0)
{
isyslog("imageplugin: loading %s", m_szFileName);
FILE *f = fopen(m_szFileName, "r");
if(f)
{
int n = 0;
char szBuf[8192];
bRet = true;
while(fgets(szBuf, sizeof(szBuf), f) != NULL)
{
++n;
if(m_bAllowComments)
{
char *p = strchr(szBuf, '#');
if(p)
*p = 0;
}
stripspace(szBuf);
if(!isempty(szBuf))
{
cImageCommand *l = new cImageCommand;
if(l->Parse(szBuf))
{
Add(l);
}
else
{
esyslog("imageplugin: error in %s, line %d\n",m_szFileName, n);
delete l;
bRet = false;
break;
}
}
}
fclose(f);
} else
{
esyslog("imageplugin: error %s: %m", m_szFileName);
bRet = false;
}
}
if(!bRet)
esyslog("imageplugin: error while reading '%s'\n", m_szFileName);
return bRet;
}