File: hp-mkuri.c

package info (click to toggle)
hplip 3.12.6-3.1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 50,996 kB
  • sloc: ansic: 65,346; cpp: 61,325; python: 58,704; sh: 12,084; perl: 4,659; makefile: 768
file content (551 lines) | stat: -rw-r--r-- 16,012 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*****************************************************************************\

  hp-mkuri.c - make uri with multi-point transport driver (HPMUD)
 
  (c) 2008-2009 Copyright Hewlett-Packard Development Company, LP

  Permission is hereby granted, free of charge, to any person obtaining a copy 
  of this software and associated documentation files (the "Software"), to deal 
  in the Software without restriction, including without limitation the rights 
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
  of the Software, and to permit persons to whom the Software is furnished to do 
  so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

\*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <syslog.h>
#include <dlfcn.h>
#include "hpmud.h"

#define _STRINGIZE(x) #x
#define STRINGIZE(x) _STRINGIZE(x)
//#define BUG(args...) fprintf(stderr, __FILE__ " " STRINGIZE(__LINE__) ": " args)
#define BUG(args...) syslog(LOG_ERR, __FILE__ " " STRINGIZE(__LINE__) ": " args)

static int verbose;
static char homedir[255] = "";

static void usage()
{
   fprintf(stdout, "HPLIP Make URI %s\n", VERSION);
   fprintf(stdout, "(c) 2008 Copyright Hewlett-Packard Development Company, LP\n");
   fprintf(stdout, "usage: hp-mkuri -i ip [-p port]\n");
   fprintf(stdout, "usage: hp-mkuri -z hostname\n");
   fprintf(stdout, "usage: hp-mkuri -b busnum -d devnum\n");
   fprintf(stdout, "usage: hp-mkuri -s serialnum\n");
   fprintf(stdout, "usage: hp-mkuri -l /dev/parportx\n");
   fprintf(stdout, "usage: hp-mkuri -m hostname [-p port]\n");
   fprintf(stdout, "usage: hp-mkuri -o (probe)\n");
   fprintf(stdout, "usage: hp-mkuri -c [-n (no notifier)] (check support)\n");
   fprintf(stdout, "\nSupport matrix:\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "| return value | printer | fax | plugin_required | plugin_optional |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       0      |   yes   |     |                 |                 |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       1      |    *    |  *  |        *        |        *        |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       2      |   yes   |     |       yes       |                 |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       3      |   yes   |     |                 |       yes       |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       4      |   yes   | yes |                 |                 |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       5      |   yes   | yes |       yes       |                 |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "|       6      |   yes   | yes |                 |       yes       |\n");
   fprintf(stdout, "+--------------+---------+-----+-----------------+-----------------+\n");
   fprintf(stdout, "    * no support or error\n");
} /* usage */

static int GetPair(char *buf, int buf_len, char *key, char *value, char **tail)
{
   int i=0, j;

   key[0] = 0;
   value[0] = 0;

   if (buf[i] == '#')
   {
      for (; buf[i] != '\n' && i < buf_len; i++);  /* eat comment line */
      if (buf[i] == '\n')
         i++;   /* bump past '\n' */
   }

   j = 0;
   while ((buf[i] != '=') && (i < buf_len) && (j < HPMUD_LINE_SIZE))
      key[j++] = buf[i++];
   for (j--; key[j] == ' ' && j > 0; j--);  /* eat white space before = */
   key[++j] = 0;

   if (buf[i] == '=')
      for (i++; buf[i] == ' ' && i < buf_len; i++);  /* eat white space after = */

   j = 0;
   while ((buf[i] != '\n') && (i < buf_len) && (j < HPMUD_LINE_SIZE))
      value[j++] = buf[i++];
   for (j--; value[j] == ' ' && j > 0; j--);  /* eat white space before \n */
   value[++j] = 0;

   if (buf[i] == '\n')
     i++;   /* bump past '\n' */

   if (tail != NULL)
      *tail = buf + i;  /* tail points to next line */

   return i;
}

static int ReadConfig()
{
   char key[HPMUD_LINE_SIZE];
   char value[HPMUD_LINE_SIZE];
   char rcbuf[255];
   char section[32];
   char *tail;
   FILE *inFile = NULL;
   int stat=1;

   homedir[0] = 0;
        
   if((inFile = fopen(CONFDIR "/hplip.conf", "r")) == NULL) 
   {
      BUG("unable to open %s: %m\n", CONFDIR "/hplip.conf");
      goto bugout;
   } 

   section[0] = 0;

   /* Read the config file */
   while ((fgets(rcbuf, sizeof(rcbuf), inFile) != NULL))
   {
      if (rcbuf[0] == '[')
      {
         strncpy(section, rcbuf, sizeof(section)); /* found new section */
         continue;
      }

      GetPair(rcbuf, strlen(rcbuf), key, value, &tail);

      if ((strncasecmp(section, "[dirs]", 6) == 0) && (strcasecmp(key, "home") == 0))
      {
         strncpy(homedir, value, sizeof(homedir));
         break;  /* done */
      }
   }
        
   stat = 0;

bugout:        
   if (inFile != NULL)
      fclose(inFile);
         
   return stat;
}

static int generalize_model(const char *sz, char *buf, int bufSize)
{
   const char *pMd=sz;
   int i, j, dd=0;

   for (i=0; pMd[i] == ' ' && i < bufSize; i++);  /* eat leading white space */

   for (j=0; (pMd[i] != 0) && (pMd[i] != ';') && (j < bufSize); i++)
   {
      if (pMd[i]==' ' || pMd[i]=='/')
      {
         /* Remove double spaces. */
         if (!dd)
         { 
            buf[j++] = '_';   /* convert space to "_" */
            dd=1;              
         }
      }
      else
      {
         buf[j++] = tolower(pMd[i]);
         dd=0;       
      }
   }

   for (j--; buf[j] == '_' && j > 0; j--);  /* eat trailing white space */

   buf[++j] = 0;

   return j;   /* length does not include zero termination */
}

static int set_x_environment(void)
{
   DIR *dir=NULL;
   FILE *file=NULL;
   struct dirent *entry;
   char path[32], line[256], cookie[128], *p;
   int i, c, stat=1;

   if ((dir = opendir("/proc"))==NULL)
   {
      BUG("unable to open /proc: %m\n");
      goto bugout;
   }

   while ((entry = readdir(dir)) != NULL) 
   {
      if (!isdigit(*entry->d_name))
         continue;

      /* Get command line for this PID. */
      snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name);
      if ((file = fopen(path, "r")) == NULL)
         continue;
      for (i=0; ((c = getc(file)) != EOF) && (i < (sizeof(line)-1)); i++)
      {
         if (c == 0)
            c = ' ';
         line[i] = c;
      }
      line[i]=0;
      fclose(file);
      if ((p = strstr(line, "-auth ")))
      {
         /* Found X server. */
         for (p+=6; (*p == ' ') && (*p != 0); p++);  /* eat any white space before cookie */
         for (i=0; (*(p+i) != ' ') && (*(p+i) != 0) && i < (sizeof(cookie)-1); i++)
            cookie[i] = *(p+i);
         cookie[i]=0;
         setenv("XAUTHORITY", cookie, 1);
         setenv("DISPLAY", ":0.0", 1);
         break; 
      }
   } /* while ((entry = readdir(dir)) != NULL) */ 

   stat = 0;

bugout:
   if (dir)
      closedir(dir);
   return(stat);
}  /* set_x_environment */

static int notify(const char *summary, const char *message, int ms_timeout) 
{
    void *handle=NULL, *n;
    int stat=1; 

    typedef void  (*notify_init_t)(char *);
    typedef void *(*notify_notification_new_t)(const char *, const char *, const char *, void *);
    typedef void  (*notify_notification_set_timeout_t)(void *, int);
    typedef void (*notify_notification_show_t)(void *, char *);

    notify_init_t n_init;
    notify_notification_new_t n_new;
    notify_notification_set_timeout_t n_timeout;
    notify_notification_show_t n_show;

    set_x_environment();

    /* Bypass glib build dependencies by loading libnotify manually. */  

    if ((handle = dlopen("libnotify.so.4", RTLD_LAZY)) == NULL)
    {
       BUG("failed to open libnotify: %m\n");
       goto bugout;
    }

    if ((n_init = (notify_init_t)dlsym(handle, "notify_init")) == NULL)
    {
       BUG("failed to find notify_init: %m\n");
       goto bugout; 
    }
    n_init("Basics");

    if ((n_new = (notify_notification_new_t)dlsym(handle, "notify_notification_new")) == NULL)
    {
       BUG("failed to find notify_notification_new: %m\n");
       goto bugout;
    }
    n = n_new(summary, message, NULL, NULL);

    if ((n_timeout = (notify_notification_set_timeout_t)dlsym(handle, "notify_notification_set_timeout")) == NULL)
    {
        BUG("failed to find notify_notification_set_timeout: %m\n");
        goto bugout;
    }
    n_timeout(n, ms_timeout);

    if ((n_show = (notify_notification_show_t)dlsym(handle, "notify_notification_show")) == NULL)
    {
       BUG("failed to find notify_notification_show: %m\n");
       goto bugout;
    }
    n_show(n, NULL);

    stat=0;

bugout:
    if (handle)
       dlclose(handle);

    return stat;
} /* notify */

static int check_support(int send_notify)
{
   struct stat sb;
   char model[256];
   int ret=1, plugin_installed=1;
   const char *pm;
   char m[256];
   char datfile[256];
   char value[32];
   int support, plugin, fax;

   /* Get hp model from environment variables. */
   if ((pm = getenv("hp_model")))
   {
      strncpy(model, pm, sizeof(model));
   }
   else
   {
      fprintf(stderr, "error no hp_model environment variable set\n");
      BUG("error no hp_model environment variable set\n");
      goto bugout;
   }

   if (model[0]==0)
   {
      BUG("invalid parameter(s)\n");
      usage();
      goto bugout;
   } 

   generalize_model(model, m, sizeof(m));
   snprintf(model, sizeof(model), "[%s]", m);

   if (ReadConfig())
      goto bugout;

   snprintf(datfile, sizeof(datfile), "%s/data/models/models.dat", homedir);

   if (hpmud_get_key_value(datfile, model, "support-type", value, sizeof(value)) != HPMUD_R_OK)
      goto bugout;
   support = strtol(value, NULL, 10);
   if (hpmud_get_key_value(datfile, model, "plugin", value, sizeof(value)) != HPMUD_R_OK)
      goto bugout;
   plugin = strtol(value, NULL, 10);
   if (hpmud_get_key_value(datfile, model, "fax-type", value, sizeof(value)) != HPMUD_R_OK)
      goto bugout;
   fax = strtol(value, NULL, 10);
   if (hpmud_get_key_value(datfile, model, "model1", m, sizeof(m)) != HPMUD_R_OK)
      goto bugout;

   /* See if device is supported by hplip. */
   if (support == HPMUD_SUPPORT_TYPE_NONE)
   {
      BUG("%s is not supported by HPLIP %s\n", pm, VERSION);
      goto bugout;
   }

   if (stat("/etc/udev/rules.d/86-hpmud-hp_laserjet_1018.rules", &sb) == -1) 
      plugin_installed=0;

   if (send_notify && !plugin_installed)
   {
      /* See if device requires a Plugin. */
      switch (plugin)
      {
         case HPMUD_PLUGIN_TYPE_REQUIRED:
            BUG("%s requires a proprietary plugin\n", m);
            notify(m, "requires a proprietary plugin, downloading and installing the plugin ...", 30000);
            break;
         case HPMUD_PLUGIN_TYPE_OPTIONAL:
            BUG("%s has an optional proprietary plugin\n", m);
            notify(m, "has an optional proprietary plugin, run hp-plugin-ubuntu", 30000);
            break;
         default:
            break;
      }         
   }

   ret = 0;
   if (!plugin_installed)
   {
      if (plugin == HPMUD_PLUGIN_TYPE_REQUIRED)
         ret = 2;
      else if (plugin == HPMUD_PLUGIN_TYPE_OPTIONAL)
         ret = 3;
   }
   if (fax > 0)
   {
      ret = 4;
      if (!plugin_installed)
      {
	 if (plugin == HPMUD_PLUGIN_TYPE_REQUIRED)
	    ret = 5;
	 else if (plugin == HPMUD_PLUGIN_TYPE_OPTIONAL)
	    ret = 6;
      }
   }

bugout:
   return ret;
} /* check_support */

int main(int argc, char *argv[])
{
   char ip[HPMUD_LINE_SIZE];  /* internet address */
   char bn[HPMUD_LINE_SIZE];  /* usb bus number */
   char dn[HPMUD_LINE_SIZE];  /* usb device number */
   char sn[HPMUD_LINE_SIZE];  /* usb serial number */
   char pp[HPMUD_LINE_SIZE];  /* parallel port device */
   char uri[HPMUD_LINE_SIZE];
   char host[HPMUD_LINE_SIZE];
   int i, port=1, ret=1, probe=0, support=0, send_notify=1;
   enum HPMUD_RESULT stat;
   char buf[HPMUD_LINE_SIZE*64];
   int cnt, bytes_read;

   ip[0] = bn[0] = dn[0] = pp[0] = uri[0] = sn[0] = host[0] = 0;
   while ((i = getopt(argc, argv, "vhocni:p:b:d:l:s:z:")) != -1)
   {
      switch (i)
      {
      case 'i':
         strncpy(ip, optarg, sizeof(ip));
         break;
      case 'z':
         strncpy(host, optarg, sizeof(host));
         break;
      case 'p':
         port = strtol(optarg, NULL, 10);
         break;
      case 'b':
         strncpy(bn, optarg, sizeof(bn));
         break;
      case 'd':
         strncpy(dn, optarg, sizeof(dn));
         break;
      case 'l':
         strncpy(pp, optarg, sizeof(pp));
         break;
      case 's':
         strncpy(sn, optarg, sizeof(sn));
         break;
      case 'o':
         probe++;
         break;
      case 'c':
         support++;
         break;
      case 'v':
         verbose++;
         break;
      case 'n':
         send_notify=0;
         break;
      case 'h':
         usage();
         exit(0);
      case '?':
         usage();
         fprintf(stderr, "unknown argument: %s\n", argv[1]);
         exit(-1);
      default:
         break;
      }
   }

   if (ip[0]==0 && (!(bn[0] && dn[0])) && pp[0]==0 && probe==0 && sn[0]==0 && support==0 && host[0]==0)
   {
      fprintf(stderr, "invalid command parameter(s)\n");
      usage();
      goto bugout;
   }   

   if (probe)
   {
      hpmud_probe_devices(HPMUD_BUS_ALL, buf, sizeof(buf), &cnt, &bytes_read);
      if (bytes_read)
         fprintf(stdout, "%s", buf);
   }

#ifdef HAVE_LIBNETSNMP
   if (ip[0])
   {
      stat = hpmud_make_net_uri(ip, port, uri, sizeof(uri), &bytes_read);
      if (stat == HPMUD_R_OK)
      {
         fprintf(stdout, "%s\n", uri);
         fprintf(stdout, "hpaio%s\n", &uri[2]);
      }
   }
   if (host[0])
   {
      stat = hpmud_make_mdns_uri(host, port, uri, sizeof(uri), &bytes_read);
      if (stat == HPMUD_R_OK)
      {
         fprintf(stdout, "%s\n", uri);
         fprintf(stdout, "hpaio%s\n", &uri[2]);
      }
   }
#endif

   if (bn[0] && dn[0])
   {
      stat = hpmud_make_usb_uri(bn, dn, uri, sizeof(uri), &bytes_read);
      if (stat == HPMUD_R_OK)
      {
         fprintf(stdout, "%s\n", uri);
         fprintf(stdout, "hpaio%s\n", &uri[2]);
      }
   }

   if (sn[0])
   {
      stat = hpmud_make_usb_serial_uri(sn, uri, sizeof(uri), &bytes_read);
      if (stat == HPMUD_R_OK)
      {
         fprintf(stdout, "%s\n", uri);
         fprintf(stdout, "hpaio%s\n", &uri[2]);
      }
   }

#ifdef HAVE_PPORT
   if (pp[0])
   {
      stat = hpmud_make_par_uri(pp, uri, sizeof(uri), &bytes_read);
      if (stat == HPMUD_R_OK)
      {
         fprintf(stdout, "%s\n", uri);
         fprintf(stdout, "hpaio%s\n", &uri[2]);
      }
   }
#endif

   ret = 0;

   if (support)
      ret = check_support(send_notify);

bugout:
   exit(ret);
} /* main */