root/WizardsToolkit/trunk/utilities/keyring.c

Revision 245, 18.5 KB (checked in by cristy, 8 weeks ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%               K   K  EEEEE  Y   Y  RRRR   IIIII  N   N   GGGG               %
7%               K  K   E       Y Y   R   R    I    NN  N  G                   %
8%               KKK    EEE      Y    RRRR     I    N N N  G GGG               %
9%               K  K   E        Y    R R      I    N  NN  G   G               %
10%               K   K  EEEEE    Y    R  R   IIIII  N   N   GGGG               %
11%                                                                             %
12%                                                                             %
13%          Import, Export, or List Properties of Keys in the Keyring          %
14%                                                                             %
15%                              Software Design                                %
16%                                John Cristy                                  %
17%                                January 2008                                 %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
21%  dedicated to making software imaging solutions freely available.           %
22%                                                                             %
23%  You may not use this file except in compliance with the License.  You may  %
24%  obtain a copy of the License at                                            %
25%                                                                             %
26%    http://www.wizards-toolkit.org/script/license.php                        %
27%                                                                             %
28%  Unless required by applicable law or agreed to in writing, software        %
29%  distributed under the License is distributed on an "AS IS" BASIS,          %
30%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31%  See the License for the specific language governing permissions and        %
32%  limitations under the License.                                             %
33%                                                                             %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40  Include declarations.
41*/
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46#include "wizard/studio.h"
47#include "wizard/WizardsToolkit.h"
48#include "wizard/blob-private.h"
49#include "wizard/exception-private.h"
50#if defined(WIZARDSTOOLKIT_HAVE_UTIME)
51#if defined(WIZARDSTOOLKIT_HAVE_UTIME_H)
52#include <utime.h>
53#else
54#include <sys/utime.h>
55#endif
56#endif
57#if defined(__WINDOWS__)
58#include <windows.h>
59#endif
60#include "content.h"
61#include "utility_.h"
62
63/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65%                                                                             %
66%                                                                             %
67%                                                                             %
68+   K e y r i n g C o m m a n d                                               %
69%                                                                             %
70%                                                                             %
71%                                                                             %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74%  KeyringCommand() imports, exports, or lists properties of keys in your
75%  keyring.
76%
77%  The format of the KeyringCommand method is:
78%
79%      WizardBooleanType KeyringCommand(int argc,char **argv,
80%        ExceptionInfo *exception)
81%
82%  A description of each parameter follows:
83%
84%    o argc: The number of elements in the argument vector.
85%
86%    o argv: A text array containing the command line arguments.
87%
88%    o exception: Return any errors or warnings in this structure.
89%
90*/
91
92static void KeyringUsage()
93{
94  static const char
95    *options[]=
96    {
97      "-debug events        display copious debugging information",
98      "-export id           export a key from your keyring",
99      "-help                print program options",
100      "-list type           print a list of supported option arguments",
101      "-log format          format of debugging information",
102      "-version             print version information",
103      (char *) NULL
104    };
105
106  const char
107    **p;
108
109  (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
110    (unsigned long *) NULL));
111  (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
112  (void) fprintf(stdout,"Usage: %s [options ...] [ keyring ...] keyring.rdf\n",
113    GetClientName());
114  (void) fprintf(stdout,"       %s [options ...] -export id [ keyring ...] "
115    "keyring.xdm\n",GetClientName());
116  (void) fprintf(stdout,"\nWhere options include:\n");
117  for (p=options; *p != (char *) NULL; p++)
118    (void) fprintf(stdout,"  %s\n",*p);
119  exit(0);
120}
121
122WizardExport WizardBooleanType ExportKeyring(int argc,char **argv,
123  ExceptionInfo *exception)
124{
125#define DestroyKeyring() \
126{ \
127  for (i=0; i < (long) argc; i++) \
128    argv[i]=DestroyString(argv[i]); \
129  argv=(char **) RelinquishWizardMemory(argv); \
130}
131#define ThrowKeyringException(asperity,tag,context) \
132{ \
133  (void) ThrowWizardException(exception,GetWizardModule(),asperity,tag, \
134    context); \
135  DestroyKeyring(); \
136  return(WizardFalse); \
137}
138#define ThrowInvalidArgumentException(option,argument) \
139{ \
140  (void) ThrowWizardException(exception,GetWizardModule(),OptionError, \
141    "invalid argument: `%s': %s",argument,option); \
142  DestroyKeyring(); \
143  return(WizardFalse); \
144}
145
146  const char
147    *filename,
148    *keyring,
149    *option;
150
151  KeyringInfo
152    *keyring_info;
153
154  register long
155    i;
156
157  StringInfo
158    *id;
159
160  WizardBooleanType
161    status;
162
163  /*
164    Parse command-line options.
165  */
166  status=WizardFalse;
167  keyring=(const char *) NULL;
168  id=(StringInfo *) NULL;
169  filename=argv[argc-1];
170  for (i=1; i < (long) (argc-1); i++)
171  {
172    option=argv[i];
173    if (IsWizardOption(option) != WizardFalse)
174      switch (*(option+1))
175      {
176        case 'd':
177        {
178          if (LocaleCompare(option,"-debug") == 0)
179            {
180              LogEventType
181                event_mask;
182
183              i++;
184              if (i == (long) argc)
185                ThrowKeyringException(OptionError,"missing log event mask: "
186                  "`%s'",option);
187              event_mask=SetLogEventMask(argv[i]);
188              if (event_mask == UndefinedEvents)
189                ThrowKeyringException(OptionFatalError,"unrecognized log event "
190                  "type: `%s'",argv[i]);
191              break;
192            }
193          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
194            option);
195          break;
196        }
197        case 'e':
198        {
199          if (LocaleCompare("export",option+1) == 0)
200            {
201              if (*option == '+')
202                break;
203              i++;
204              if (i == (long) argc)
205                ThrowKeyringException(OptionError,"missing key id: `%s'",
206                  option);
207              id=HexStringToStringInfo(argv[i]);
208              break;
209            }
210          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
211            option);
212          break;
213        }
214        case 'h':
215        {
216          if ((LocaleCompare("help",option+1) == 0) ||
217              (LocaleCompare("-help",option+1) == 0))
218            KeyringUsage();
219          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
220            option);
221          break;
222        }
223        case 'l':
224        {
225          if (LocaleCompare(option,"-list") == 0)
226            {
227              long
228                list;
229
230              if (*option == '+')
231                break;
232              i++;
233              if (i == (long) argc)
234                ThrowKeyringException(OptionError,"missing list type: `%s'",
235                  option);
236              if (LocaleCompare(argv[i],"configure") == 0)
237                {
238                  (void) ListConfigureInfo((FILE *) NULL,exception);
239                  Exit(0);
240                }
241              list=ParseWizardOption(WizardListOptions,WizardFalse,argv[i]);
242              if (list < 0)
243                ThrowKeyringException(OptionFatalError,"unrecognized list "
244                  "type: `%s'",argv[i]);
245              (void) ListWizardOptions((FILE *) NULL,(WizardOption) list,
246                exception);
247              Exit(0);
248              break;
249            }
250          if (LocaleCompare("log",option+1) == 0)
251            {
252              if (*option == '+')
253                break;
254              i++;
255              if ((i == (long) argc) ||
256                  (strchr(argv[i],'%') == (char *) NULL))
257                ThrowKeyringException(OptionFatalError,"missing argument: `%s'",
258                  option);
259              break;
260            }
261          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
262            option);
263          break;
264        }
265        case 'v':
266        {
267          if (LocaleCompare(option,"-version") == 0)
268            {
269              (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
270                (unsigned long *) NULL));
271              (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
272              exit(0);
273            }
274          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
275            option);
276          break;
277        }
278        default:
279        {
280          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
281            option);
282          break;
283        }
284      }
285    /*
286      Export key from keyring.
287    */
288    keyring_info=AcquireKeyringInfo(keyring);
289    SetKeyringId(keyring_info,id);
290    if (ExportKeyringKey(keyring_info,exception) == WizardFalse)
291      {
292        char
293          *hex_id;
294
295        hex_id=StringInfoToHexString(id);
296        (void) ThrowWizardException(exception,GetWizardModule(),KeyringError,
297          "no such key `%s': `%s'",hex_id,keyring);
298        hex_id=DestroyString(hex_id);
299      }
300    else
301      {
302        SetKeyringPath(keyring_info,filename);
303        status=ImportKeyringKey(keyring_info,exception);
304      }
305    id=DestroyStringInfo(id);
306    keyring_info=DestroyKeyringInfo(keyring_info);
307  }
308  /*
309    Free resources.
310  */
311  DestroyKeyring();
312  return(status);
313}
314
315WizardExport WizardBooleanType KeyringCommand(int argc,char **argv,
316  ExceptionInfo *exception)
317{
318#define DestroyKeyring() \
319{ \
320  for (i=0; i < (long) argc; i++) \
321    argv[i]=DestroyString(argv[i]); \
322  argv=(char **) RelinquishWizardMemory(argv); \
323}
324#define ThrowKeyringException(asperity,tag,context) \
325{ \
326  (void) ThrowWizardException(exception,GetWizardModule(),asperity,tag, \
327    context); \
328  DestroyKeyring(); \
329  return(WizardFalse); \
330}
331#define ThrowInvalidArgumentException(option,argument) \
332{ \
333  (void) ThrowWizardException(exception,GetWizardModule(),OptionError, \
334    "invalid argument: `%s': %s",argument,option); \
335  DestroyKeyring(); \
336  return(WizardFalse); \
337}
338
339  BlobInfo
340    *keyring_blob;
341
342  const char
343    *option;
344
345  register long
346    i;
347
348  WizardBooleanType
349    status;
350
351  /*
352    Parse command-line options.
353  */
354  if (argc == 2)
355    {
356      option=argv[1];
357      if ((LocaleCompare("version",option+1) == 0) ||
358          (LocaleCompare("-version",option+1) == 0))
359        {
360          (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
361            (unsigned long *) NULL));
362          (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
363          return(WizardTrue);
364        }
365    }
366  if (argc < 2)
367    KeyringUsage();
368  status=ExpandFilenames(&argc,&argv);
369  if (status == WizardFalse)
370    ThrowKeyringException(ResourceError,"memory allocation failed: `%s'",
371      strerror(errno));
372  for (i=1; i < (argc-1); i++)
373  {
374    option=argv[i];
375    if (LocaleCompare(option+1,"export") == 0)
376      {
377        status=ExportKeyring(argc,argv,exception);
378        DestroyKeyring();
379        return(status);
380      }
381  }
382  keyring_blob=OpenBlob(argv[argc-1],WriteBinaryBlobMode,WizardTrue,exception);
383  if (keyring_blob == (BlobInfo *) NULL)
384    return(WizardFalse);
385  (void) WriteBlobString(keyring_blob,"<?xml version=\"1.0\"?>\n");
386  (void) WriteBlobString(keyring_blob,"<rdf:RDF xmlns:rdf=\""
387    "http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n");
388  (void) WriteBlobString(keyring_blob,"         xmlns:keyring=\""
389    "http://www.wizards-toolkit.org/keyring/1.0/\">\n");
390  for (i=1; i < (long) (argc-1); i++)
391  {
392    option=argv[i];
393    if (IsWizardOption(option) != WizardFalse)
394      switch (*(option+1))
395      {
396        case 'd':
397        {
398          if (LocaleCompare(option,"-debug") == 0)
399            {
400              LogEventType
401                event_mask;
402
403              i++;
404              if (i == (long) argc)
405                ThrowKeyringException(OptionError,"missing log event mask: "
406                  "`%s'",option);
407              event_mask=SetLogEventMask(argv[i]);
408              if (event_mask == UndefinedEvents)
409                ThrowKeyringException(OptionFatalError,"unrecognized log event "
410                  "type: `%s'",argv[i]);
411              break;
412            }
413          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
414            option);
415          break;
416        }
417        case 'h':
418        {
419          if ((LocaleCompare("help",option+1) == 0) ||
420              (LocaleCompare("-help",option+1) == 0))
421            KeyringUsage();
422          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
423            option);
424          break;
425        }
426        case 'l':
427        {
428          if (LocaleCompare(option,"-list") == 0)
429            {
430              long
431                list;
432
433              if (*option == '+')
434                break;
435              i++;
436              if (i == (long) argc)
437                ThrowKeyringException(OptionError,"missing list type: `%s'",
438                  option);
439              if (LocaleCompare(argv[i],"configure") == 0)
440                {
441                  (void) ListConfigureInfo((FILE *) NULL,exception);
442                  Exit(0);
443                }
444              list=ParseWizardOption(WizardListOptions,WizardFalse,argv[i]);
445              if (list < 0)
446                ThrowKeyringException(OptionFatalError,"unrecognized list "
447                  "type: `%s'",argv[i]);
448              (void) ListWizardOptions((FILE *) NULL,(WizardOption) list,
449                exception);
450              Exit(0);
451              break;
452            }
453          if (LocaleCompare("log",option+1) == 0)
454            {
455              if (*option == '+')
456                break;
457              i++;
458              if ((i == (long) argc) ||
459                  (strchr(argv[i],'%') == (char *) NULL))
460                ThrowKeyringException(OptionFatalError,"missing argument: `%s'",
461                  option);
462              break;
463            }
464          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
465            option);
466          break;
467        }
468        case 'v':
469        {
470          if (LocaleCompare(option,"-version") == 0)
471            {
472              (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
473                (unsigned long *) NULL));
474              (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
475              exit(0);
476            }
477          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
478            option);
479          break;
480        }
481        default:
482        {
483          ThrowKeyringException(OptionFatalError,"unrecognized option: `%s'",
484            option);
485          break;
486        }
487      }
488    status=PrintKeyringProperties(argv[i],keyring_blob,exception);
489  }
490  if (argc == 2)
491    status=PrintKeyringProperties((const char *) NULL,keyring_blob,exception);
492  (void) WriteBlobString(keyring_blob,"</rdf:RDF>\n");
493  status=CloseBlob(keyring_blob);
494  /*
495    Free resources.
496  */
497  DestroyKeyring();
498  return(status);
499}
500
501/*
502%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503%                                                                             %
504%                                                                             %
505%                                                                             %
506%  M a i n                                                                    %
507%                                                                             %
508%                                                                             %
509%                                                                             %
510%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511%
512%
513*/
514int main(int argc,char **argv)
515{
516  char
517    *option;
518
519  double
520    elapsed_time,
521    user_time;
522
523  ExceptionInfo
524    *exception;
525
526  register long
527    i;
528
529  TimerInfo
530    *timer;
531
532  unsigned int
533    iterations;
534
535  WizardBooleanType
536    regard_warnings,
537    status;
538
539  WizardsToolkitGenesis(*argv);
540  exception=AcquireExceptionInfo();
541  iterations=1;
542  status=WizardTrue;
543  regard_warnings=WizardFalse;
544  for (i=1; i < (long) (argc-1); i++)
545  {
546    option=argv[i];
547    if ((strlen(option) == 1) || ((*option != '-') && (*option != '+')))
548      continue;
549    if (LocaleCompare("bench",option+1) == 0)
550      iterations=(unsigned int) atol(argv[++i]);
551    if (LocaleCompare("debug",option+1) == 0)
552      (void) SetLogEventMask(argv[++i]);
553    if (LocaleCompare("regard-warnings",option+1) == 0)
554      regard_warnings=WizardTrue;
555  }
556  timer=(TimerInfo *) NULL;
557  if (iterations > 1)
558    timer=AcquireTimerInfo();
559  for (i=0; i < (long) iterations; i++)
560  {
561    status=KeyringCommand(argc,argv,exception);
562    if ((status == WizardFalse) ||
563        (GetExceptionSeverity(exception) != UndefinedException))
564      {
565        if ((GetExceptionSeverity(exception) < ErrorException) &&
566            (regard_warnings == WizardFalse))
567          status=WizardTrue;
568        CatchException(exception);
569      }
570  }
571  if (iterations > 1)
572    {
573      elapsed_time=GetElapsedTime(timer);
574      user_time=GetUserTime(timer);
575      (void) fprintf(stderr,"Performance: %ui %gips %0.3fu %ld:%02ld.%03ld\n",
576        iterations,1.0*iterations/elapsed_time,user_time,(long)
577        (elapsed_time/60.0+0.5),(long) floor(fmod(elapsed_time,60.0)),
578        (long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
579      timer=DestroyTimerInfo(timer);
580    }
581  exception=DestroyExceptionInfo(exception);
582  WizardsToolkitTerminus();
583  return(status == WizardFalse ? 1 : 0);
584}
Note: See TracBrowser for help on using the browser.