~mimose/+junk/hplip-3.16.11

« back to all changes in this revision

Viewing changes to common/utils.c

  • Committer: guoyalong
  • Date: 2017-09-20 10:13:05 UTC
  • Revision ID: guoyalong@kylinos.cn-20170920101305-82zaolzpv1qghz29
Modified debian/control & debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "utils.h"
 
2
#include "string.h"
 
3
#include <dlfcn.h>
 
4
#include <sys/stat.h>
 
5
#include <errno.h>
 
6
#include <stdlib.h>
 
7
 
 
8
extern int errno;
 
9
 
 
10
static int GetPair(char *buf, int buf_len, char *key, char *value, char **tail)
 
11
{
 
12
   int i=0, j;
 
13
 
 
14
   key[0] = 0;
 
15
   value[0] = 0;
 
16
 
 
17
   if (buf[i] == '#')
 
18
   {
 
19
      for (; buf[i] != '\n' && i < buf_len; i++);  /* eat comment line */
 
20
      if (buf[i] == '\n')
 
21
         i++;   /* bump past '\n' */
 
22
   }
 
23
 
 
24
   j = 0;
 
25
   while ((buf[i] != '=') && (i < buf_len) && (j < UTILS_LINE_SIZE))
 
26
      key[j++] = buf[i++];
 
27
   for (j--; key[j] == ' ' && j > 0; j--);  /* eat white space before = */
 
28
   key[++j] = 0;
 
29
 
 
30
   if (buf[i] == '=')
 
31
      for (i++; buf[i] == ' ' && i < buf_len; i++);  /* eat white space after = */
 
32
 
 
33
   j = 0;
 
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 */
 
37
   value[++j] = 0;
 
38
 
 
39
   if (buf[i] == '\n')
 
40
     i++;   /* bump past '\n' */
 
41
 
 
42
   if (tail != NULL)
 
43
      *tail = buf + i;  /* tail points to next line */
 
44
 
 
45
   return i;
 
46
}
 
47
 
 
48
 
 
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)
 
51
{
 
52
   return get_key_value(CONFDIR "/hplip.conf", section, key, value, value_size);
 
53
}
 
54
 
 
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)
 
57
{
 
58
   char new_key[UTILS_LINE_SIZE];
 
59
   char new_value[UTILS_LINE_SIZE];
 
60
   char rcbuf[255];
 
61
   char new_section[32];
 
62
   char *tail;
 
63
   FILE *inFile;
 
64
   enum UTILS_CONF_RESULT stat = UTILS_CONF_DATFILE_ERROR;
 
65
   int i,j;
 
66
 
 
67
   if((inFile = fopen(file, "r")) == NULL) 
 
68
   {
 
69
      BUG("unable to open %s: %m\n", file);
 
70
      goto bugout;
 
71
   } 
 
72
 
 
73
   new_section[0] = 0;
 
74
 
 
75
   /* Read the config file */
 
76
   while ((fgets(rcbuf, sizeof(rcbuf), inFile) != NULL))
 
77
   {
 
78
      if (rcbuf[0] == '[')
 
79
      {
 
80
         i = j = 0;
 
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 */
 
85
         continue;
 
86
      }
 
87
 
 
88
      GetPair(rcbuf, strlen(rcbuf), new_key, new_value, &tail);
 
89
 
 
90
      if ((strcasecmp(new_section, section) == 0) && (strcasecmp(new_key, key) == 0))
 
91
      {
 
92
         strncpy(value, new_value, value_size);
 
93
         stat = UTILS_CONF_OK;
 
94
         break;  /* done */
 
95
      }
 
96
   }
 
97
 
 
98
   if (stat != UTILS_CONF_OK)
 
99
      BUG("unable to find %s %s in %s\n", section, key, file);
 
100
        
 
101
bugout:        
 
102
   if (inFile != NULL)
 
103
      fclose(inFile);
 
104
         
 
105
   return stat;
 
106
}
 
107
 
 
108
 
 
109
enum UTILS_PLUGIN_STATUS validate_plugin_version()
 
110
{
 
111
    char hplip_version[128];
 
112
    char plugin_version[128];
 
113
 
 
114
    if (get_conf("[hplip]", "version", hplip_version, sizeof(hplip_version)) != UTILS_CONF_OK)
 
115
      return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
 
116
 
 
117
    if (get_key_value(HPLIP_PLUGIN_STATE,"[plugin]" , "version", plugin_version, sizeof(plugin_version)) != UTILS_CONF_OK )
 
118
    {
 
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;
 
121
    }
 
122
 
 
123
 
 
124
    if (strcmp(hplip_version, plugin_version) == 0)
 
125
    {
 
126
        return UTILS_PLUGIN_STATUS_OK;
 
127
    }
 
128
    else
 
129
    {
 
130
        BUG("validate_plugin_version() Plugin version[%s] mismatch with HPLIP version[%s]\n",plugin_version, hplip_version);
 
131
        return UTILS_PLUGIN_STATUS_MISMATCH;
 
132
    }
 
133
    return UTILS_PLUGIN_STATUS_NOT_INSTALLED;
 
134
}
 
135
 
 
136
 
 
137
void *load_plugin_library (enum UTILS_PLUGIN_LIBRARY_TYPE eLibraryType, const char *szPluginName)
 
138
{
 
139
    void *pHandler = NULL;
 
140
    char szHome[256];
 
141
    char szLibraryFile[256];
 
142
 
 
143
    if (szPluginName == NULL || szPluginName[0] == '\0')
 
144
    {
 
145
        BUG("Invalid Library name\n");
 
146
        return pHandler;
 
147
    }
 
148
    
 
149
    if (get_conf("[dirs]", "home", szHome, sizeof(szHome)) != UTILS_CONF_OK)
 
150
    {
 
151
        BUG("Failed to find the home directory from hplip.conf file\n");
 
152
        return pHandler;
 
153
    }
 
154
    
 
155
    if (validate_plugin_version() != UTILS_PLUGIN_STATUS_OK )
 
156
    {
 
157
        BUG("Plugin version is not matching \n");
 
158
        return pHandler;
 
159
    }
 
160
    
 
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);
 
167
    else
 
168
    {
 
169
        BUG("Invalid Library Type =%d \n",eLibraryType);
 
170
        return pHandler;
 
171
    }
 
172
 
 
173
    return load_library (szLibraryFile);
 
174
    
 
175
}
 
176
 
 
177
void *load_library (const char *szLibraryFile)
 
178
{
 
179
    void *pHandler = NULL;
 
180
 
 
181
    if (szLibraryFile == NULL || szLibraryFile[0] == '\0')
 
182
    {
 
183
        BUG("Invalid Library name\n");
 
184
        return pHandler;
 
185
    }
 
186
    
 
187
    if ((pHandler = dlopen(szLibraryFile, RTLD_NOW|RTLD_GLOBAL)) == NULL)
 
188
        BUG("unable to load library %s: %s\n", szLibraryFile, dlerror());
 
189
 
 
190
    return pHandler;
 
191
}
 
192
 
 
193
void *get_library_symbol(void *pLibHandler, const char *szSymbol)
 
194
{
 
195
    void *pSymHandler = NULL;
 
196
    if (pLibHandler == NULL)
 
197
    {
 
198
        BUG("Invalid Library hanlder\n");
 
199
        return NULL;
 
200
    }
 
201
 
 
202
    if (szSymbol == NULL || szSymbol[0] == '\0')
 
203
    {
 
204
        BUG("Invalid Library symbol\n");
 
205
        return NULL;
 
206
    }
 
207
 
 
208
    pSymHandler = dlsym(pLibHandler, szSymbol);
 
209
    if (pSymHandler == NULL)
 
210
        BUG("Can't find %s symbol in Library:%s\n",szSymbol,dlerror());
 
211
 
 
212
    return pSymHandler;
 
213
}
 
214
 
 
215
void unload_library(void *pLibHandler)
 
216
{
 
217
    if (pLibHandler)
 
218
        dlclose(pLibHandler);
 
219
    else
 
220
        BUG("Invalid Library hanlder pLibHandler = NULL.\n");
 
221
}
 
222
 
 
223
int createTempFile(char* szFileName, FILE** pFilePtr)
 
224
{
 
225
    int iFD = -1;
 
226
 
 
227
    if (szFileName == NULL || szFileName[0] == '\0' || pFilePtr == NULL)
 
228
    {
 
229
        BUG("Invalid Filename/ pointer\n");
 
230
        return 0;
 
231
    }
 
232
 
 
233
    if (strstr(szFileName,"XXXXXX") == NULL)
 
234
        strcat(szFileName,"_XXXXXX");
 
235
 
 
236
    iFD = mkstemp(szFileName);
 
237
    if(-1 == iFD)
 
238
    {
 
239
        BUG("Failed to create the temp file Name[%s] errno[%d : %s]\n",szFileName,errno,strerror(errno));
 
240
        return 0;
 
241
    }
 
242
    else
 
243
    {
 
244
        *pFilePtr = fdopen(iFD,"w+");
 
245
    }
 
246
 
 
247
    return iFD;
 
248
}
 
249
 
 
250
int getHPLogLevel()
 
251
{
 
252
    FILE    *fp;
 
253
    char    str[258];
 
254
    char    *p;
 
255
    int iLogLevel = 0;
 
256
 
 
257
    fp = fopen ("/etc/cups/cupsd.conf", "r");
 
258
    if (fp == NULL)
 
259
        return 0;
 
260
    while (!feof (fp))
 
261
    {
 
262
        if (!fgets (str, 256, fp))
 
263
        {
 
264
            break;
 
265
        }
 
266
        if ((p = strstr (str, "hpLogLevel")))
 
267
        {
 
268
            p += strlen ("hpLogLevel") + 1;
 
269
            iLogLevel = atoi (p);
 
270
            break;
 
271
        }
 
272
    }
 
273
    fclose (fp);
 
274
    return iLogLevel;
 
275
}
 
276
 
 
277