10
static int GetPair(char *buf, int buf_len, char *key, char *value, char **tail)
19
for (; buf[i] != '\n' && i < buf_len; i++); /* eat comment line */
21
i++; /* bump past '\n' */
25
while ((buf[i] != '=') && (i < buf_len) && (j < UTILS_LINE_SIZE))
27
for (j--; key[j] == ' ' && j > 0; j--); /* eat white space before = */
31
for (i++; buf[i] == ' ' && i < buf_len; i++); /* eat white space after = */
34
while ((buf[i] != '\n') && (i < buf_len) && (j < UTILS_LINE_SIZE))
35
value[j++] = buf[i++];
36
for (j--; value[j] == ' ' && j > 0; j--); /* eat white space before \n */
40
i++; /* bump past '\n' */
43
*tail = buf + i; /* tail points to next line */
49
/* Get value for specified section and key from hplip.conf. */
50
enum UTILS_CONF_RESULT get_conf(const char *section, const char *key, char *value, int value_size)
52
return get_key_value(CONFDIR "/hplip.conf", section, key, value, value_size);
55
/* Get value for specified section and key from specified file. */
56
enum UTILS_CONF_RESULT get_key_value(const char *file, const char *section, const char *key, char *value, int value_size)
58
char new_key[UTILS_LINE_SIZE];
59
char new_value[UTILS_LINE_SIZE];
64
enum UTILS_CONF_RESULT stat = UTILS_CONF_DATFILE_ERROR;
67
if((inFile = fopen(file, "r")) == NULL)
69
BUG("unable to open %s: %m\n", file);
75
/* Read the config file */
76
while ((fgets(rcbuf, sizeof(rcbuf), inFile) != NULL))
81
while ((rcbuf[i] != ']') && (j < (sizeof(new_section)-2)))
82
new_section[j++] = rcbuf[i++];
83
new_section[j++] = rcbuf[i++]; /* ']' */
84
new_section[j] = 0; /* zero terminate */
88
GetPair(rcbuf, strlen(rcbuf), new_key, new_value, &tail);
90
if ((strcasecmp(new_section, section) == 0) && (strcasecmp(new_key, key) == 0))
92
strncpy(value, new_value, value_size);
98
if (stat != UTILS_CONF_OK)
99
BUG("unable to find %s %s in %s\n", section, key, file);
109
enum UTILS_PLUGIN_STATUS validate_plugin_version()
111
char hplip_version[128];
112
char plugin_version[128];
114
if (get_conf("[hplip]", "version", hplip_version, sizeof(hplip_version)) != UTILS_CONF_OK)
115
return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
117
if (get_key_value(HPLIP_PLUGIN_STATE,"[plugin]" , "version", plugin_version, sizeof(plugin_version)) != UTILS_CONF_OK )
119
BUG("validate_plugin_version() Failed to get Plugin version from [%s]\n", "/var/lib/hp/hplip.state");
120
return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
124
if (strcmp(hplip_version, plugin_version) == 0)
126
return UTILS_PLUGIN_STATUS_OK;
130
BUG("validate_plugin_version() Plugin version[%s] mismatch with HPLIP version[%s]\n",plugin_version, hplip_version);
131
return UTILS_PLUGIN_STATUS_MISMATCH;
133
return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
137
void *load_plugin_library (enum UTILS_PLUGIN_LIBRARY_TYPE eLibraryType, const char *szPluginName)
139
void *pHandler = NULL;
141
char szLibraryFile[256];
143
if (szPluginName == NULL || szPluginName[0] == '\0')
145
BUG("Invalid Library name\n");
149
if (get_conf("[dirs]", "home", szHome, sizeof(szHome)) != UTILS_CONF_OK)
151
BUG("Failed to find the home directory from hplip.conf file\n");
155
if (validate_plugin_version() != UTILS_PLUGIN_STATUS_OK )
157
BUG("Plugin version is not matching \n");
161
if (eLibraryType == UTILS_PRINT_PLUGIN_LIBRARY)
162
snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/prnt/plugins/%s", szHome, szPluginName);
163
else if (eLibraryType == UTILS_SCAN_PLUGIN_LIBRARY)
164
snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/scan/plugins/%s", szHome, szPluginName);
165
else if (eLibraryType == UTILS_FAX_PLUGIN_LIBRARY)
166
snprintf(szLibraryFile, sizeof(szLibraryFile), "%s/fax/plugins/%s", szHome, szPluginName);
169
BUG("Invalid Library Type =%d \n",eLibraryType);
173
return load_library (szLibraryFile);
177
void *load_library (const char *szLibraryFile)
179
void *pHandler = NULL;
181
if (szLibraryFile == NULL || szLibraryFile[0] == '\0')
183
BUG("Invalid Library name\n");
187
if ((pHandler = dlopen(szLibraryFile, RTLD_NOW|RTLD_GLOBAL)) == NULL)
188
BUG("unable to load library %s: %s\n", szLibraryFile, dlerror());
193
void *get_library_symbol(void *pLibHandler, const char *szSymbol)
195
void *pSymHandler = NULL;
196
if (pLibHandler == NULL)
198
BUG("Invalid Library hanlder\n");
202
if (szSymbol == NULL || szSymbol[0] == '\0')
204
BUG("Invalid Library symbol\n");
208
pSymHandler = dlsym(pLibHandler, szSymbol);
209
if (pSymHandler == NULL)
210
BUG("Can't find %s symbol in Library:%s\n",szSymbol,dlerror());
215
void unload_library(void *pLibHandler)
218
dlclose(pLibHandler);
220
BUG("Invalid Library hanlder pLibHandler = NULL.\n");
223
int createTempFile(char* szFileName, FILE** pFilePtr)
227
if (szFileName == NULL || szFileName[0] == '\0' || pFilePtr == NULL)
229
BUG("Invalid Filename/ pointer\n");
233
if (strstr(szFileName,"XXXXXX") == NULL)
234
strcat(szFileName,"_XXXXXX");
236
iFD = mkstemp(szFileName);
239
BUG("Failed to create the temp file Name[%s] errno[%d : %s]\n",szFileName,errno,strerror(errno));
244
*pFilePtr = fdopen(iFD,"w+");
257
fp = fopen ("/etc/cups/cupsd.conf", "r");
262
if (!fgets (str, 256, fp))
266
if ((p = strstr (str, "hpLogLevel")))
268
p += strlen ("hpLogLevel") + 1;
269
iLogLevel = atoi (p);