/[winpt]/trunk/Src/wptGPGParser.cpp
ViewVC logotype

Diff of /trunk/Src/wptGPGParser.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 270 by twoaday, Sat Oct 21 18:08:57 2006 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 2006 UTC
# Line 31  Line 31 
31  #include "wptGPG.h"  #include "wptGPG.h"
32  #include "wptErrors.h"  #include "wptErrors.h"
33    
34    
35    /* Add the option @n to the linked list @list. */
36  static void  static void
37  add_option (gpg_option_t *list, gpg_option_t n)  add_option (conf_option_t *list, conf_option_t n)
38  {  {
39      gpg_option_t t;      conf_option_t t;
40    
41      if (!*list)      if (!*list)
42          *list = n;          *list = n;
# Line 45  add_option (gpg_option_t *list, gpg_opti Line 47  add_option (gpg_option_t *list, gpg_opti
47      }      }
48  }  }
49    
50  static gpg_option_t  /* Allocate a new option and return it. */
51    static conf_option_t
52  new_option (void)  new_option (void)
53  {  {
54      gpg_option_t e;      conf_option_t e;
55    
56      e = new gpg_option_s;      e = new conf_option_s;
57      if (!e)      if (!e)
58          BUG (0);          BUG (0);
59      memset (e, 0, sizeof *e);      memset (e, 0, sizeof *e);
# Line 61  new_option (void) Line 64  new_option (void)
64  static void  static void
65  add_opaque_option (config_file_t opt, const char *line, int used)  add_opaque_option (config_file_t opt, const char *line, int used)
66  {  {
67      gpg_option_t e;      conf_option_t e;
68    
69      e = new_option ();      e = new_option ();
70      e->used = used > 0? 1 : 0;      e->used = used > 0? 1 : 0;
# Line 74  add_opaque_option (config_file_t opt, co Line 77  add_opaque_option (config_file_t opt, co
77  static void  static void
78  add_single_option (config_file_t opt, const char *name, int used)  add_single_option (config_file_t opt, const char *name, int used)
79  {  {
80      gpg_option_t e;      conf_option_t e;
81    
82      e = new_option ();      e = new_option ();
83      e->used = used > 0? 1 : 0;      e->used = used > 0? 1 : 0;
# Line 87  add_single_option (config_file_t opt, co Line 90  add_single_option (config_file_t opt, co
90  static int  static int
91  add_group_option (config_file_t opt, const char *name, int used)  add_group_option (config_file_t opt, const char *name, int used)
92  {  {
93      gpg_option_t e;      conf_option_t e;
94    
95      e = new_option ();      e = new_option ();
96      e->used = used > 0? 1 : 0;      e->used = used > 0? 1 : 0;
# Line 117  count_whitespaces (char *line) Line 120  count_whitespaces (char *line)
120  static int  static int
121  add_multi_option (config_file_t opt, char *line, int used)  add_multi_option (config_file_t opt, char *line, int used)
122  {  {
123      gpg_option_t e;      conf_option_t e;
124      char *p;      char *p;
125      int state = 0;      int state = 0;
126    
# Line 152  add_multi_option (config_file_t opt, cha Line 155  add_multi_option (config_file_t opt, cha
155  }  }
156    
157    
158  static gpg_group_t  static conf_group_t
159  new_group (gpg_group_t *grp, const char *name)  new_group (conf_group_t *grp, const char *name)
160  {  {
161      gpg_group_t g, t;      conf_group_t g, t;
162    
163      g = new gpg_group_s;          g = new conf_group_s;    
164      if (!g)      if (!g)
165          BUG (NULL);          BUG (NULL);
166      memset (g, 0, sizeof *g);      memset (g, 0, sizeof *g);
# Line 178  static const char* Line 181  static const char*
181  group_from_cfgfile (config_file_t opt, const char *line)  group_from_cfgfile (config_file_t opt, const char *line)
182  {  {
183      char *p, *buf, *buf2, *name = NULL;      char *p, *buf, *buf2, *name = NULL;
184      gpg_group_t g;      conf_group_t g;
185      gpg_member_t m = NULL, t;      conf_member_t m = NULL, t;
186    
187      p = strchr (line, '=');      p = strchr (line, '=');
188      if (!p || strncmp (line, "group ", 6))      if (!p || strncmp (line, "group ", 6))
# Line 198  group_from_cfgfile (config_file_t opt, c Line 201  group_from_cfgfile (config_file_t opt, c
201      g = new_group (&opt->grp, name);      g = new_group (&opt->grp, name);
202      buf2 = buf;      buf2 = buf;
203      while ((p = strsep (&buf2, " ")) && *p) {      while ((p = strsep (&buf2, " ")) && *p) {
204          m = new gpg_member_s;          m = new conf_member_s;
205          if (!m)          if (!m)
206              BUG (0);              BUG (0);
207          memset (m, 0, sizeof *m);          memset (m, 0, sizeof *m);
# Line 220  group_from_cfgfile (config_file_t opt, c Line 223  group_from_cfgfile (config_file_t opt, c
223    
224    
225  void  void
226  release_group (gpg_group_t grp)  conf_release_group (conf_group_t grp)
227  {  {
228      gpg_member_t m, m2;      conf_member_t m, m2;
229            
230      m = grp->list;      m = grp->list;
231      while (m) {      while (m) {
# Line 234  release_group (gpg_group_t grp) Line 237  release_group (gpg_group_t grp)
237  }  }
238    
239    
240  void  static void
241  release_option (gpg_option_t opt)  release_option (conf_option_t opt)
242  {  {
243      if (!opt)      if (!opt)
244          return;          return;
# Line 248  release_option (gpg_option_t opt) Line 251  release_option (gpg_option_t opt)
251  void  void
252  release_config (config_file_t opt)  release_config (config_file_t opt)
253  {  {
254      gpg_option_t e, e2;      conf_option_t e, e2;
255      gpg_group_t g, g2;      conf_group_t g, g2;
256    
257      if (!opt)      if (!opt)
258          return;          return;
# Line 262  release_config (config_file_t opt) Line 265  release_config (config_file_t opt)
265      g = opt->grp;      g = opt->grp;
266      while (g) {      while (g) {
267          g2 = g->next;          g2 = g->next;
268          release_group (g);          conf_release_group (g);
269          g = g2;          g = g2;
270      }      }
271      free_if_alloc (opt);      free_if_alloc (opt);
# Line 273  int Line 276  int
276  commit_config (const char *file, config_file_t opt)  commit_config (const char *file, config_file_t opt)
277  {  {
278      FILE *inp;      FILE *inp;
279      gpg_option_t e;      conf_option_t e;
280      gpg_group_t g;      conf_group_t g;
281      gpg_member_t m;      conf_member_t m;
282    
283      inp = fopen (file, "w+b");      inp = fopen (file, "w+b");
284      if (!inp)      if (!inp)
# Line 297  commit_config (const char *file, config_ Line 300  commit_config (const char *file, config_
300              break;              break;
301    
302          case ENTRY_GROUP:          case ENTRY_GROUP:
303              g = find_group (opt, e->name);              g = conf_find_group (opt, e->name);
304              if (g && g->used) {              if (g && g->used) {
305                  fprintf (inp, "group %s=", g->name);                  fprintf (inp, "group %s=", g->name);
306                  for (m = g->list; m; m = m->next) {                  for (m = g->list; m; m = m->next) {
# Line 319  new_config (config_file_t *r_opt) Line 322  new_config (config_file_t *r_opt)
322  {  {
323      config_file_t opt;      config_file_t opt;
324    
325      opt = new gpg_optfile_s;      opt = new conf_file_s;
326      if (!opt)      if (!opt)
327          BUG (NULL);          BUG (NULL);
328      memset (opt, 0, sizeof *opt);      memset (opt, 0, sizeof *opt);
# Line 343  parse_config (const char *file, config_f Line 346  parse_config (const char *file, config_f
346          if (inp == NULL)          if (inp == NULL)
347              return -1;              return -1;
348      }      }
349      opt = new gpg_optfile_s;      opt = new conf_file_s;
350      if (!opt)      if (!opt)
351          BUG (NULL);          BUG (NULL);
352      memset (opt, 0, sizeof *opt);      memset (opt, 0, sizeof *opt);
353      while (!feof (inp))  {      while (!feof (inp))  {
354          p = fgets (buf, sizeof (buf) -1, inp);          p = fgets (buf, DIM (buf) -1, inp);
355          if (!p)          if (!p)
356              break;              break;
357          if (*p == '#' || *p == '\r' || *p == '\n' || *p == '\t') {          if (*p == '#' || *p == '\r' || *p == '\n' || *p == '\t') {
# Line 375  parse_config (const char *file, config_f Line 378  parse_config (const char *file, config_f
378  }  }
379    
380    
381  gpg_option_t  /* Search for an option with the name @str and
382  find_option (config_file_t opt, const char *str)     return this option if it exists.
383       Return value: NULL if not found. */
384    conf_option_t
385    conf_find_option (config_file_t opt, const char *str)
386  {  {
387      gpg_option_t e;      conf_option_t e;
388    
389      for (e = opt->list; e; e = e->next) {      for (e = opt->list; e; e = e->next) {
390          if (e->type == ENTRY_OPAQUE)          if (e->type == ENTRY_OPAQUE)
# Line 391  find_option (config_file_t opt, const ch Line 397  find_option (config_file_t opt, const ch
397  }  }
398    
399    
400  gpg_group_t  conf_group_t
401  find_group (config_file_t opt, const char *str)  conf_find_group (config_file_t opt, const char *str)
402  {  {
403      gpg_group_t g;      conf_group_t g;
404    
405      for (g = opt->grp; g; g = g->next) {      for (g = opt->grp; g; g = g->next) {
406          if (!stricmp( g->name, str))          if (!stricmp( g->name, str))
# Line 405  find_group (config_file_t opt, const cha Line 411  find_group (config_file_t opt, const cha
411  }  }
412    
413    
414  gpg_member_t  conf_member_t
415  find_member (config_file_t opt, const char *grp, const char *str)  conf_find_member (config_file_t opt, const char *grp, const char *str)
416  {  {
417      gpg_member_t m;      conf_member_t m;
418      gpg_group_t g = find_group (opt, grp);      conf_group_t g = conf_find_group (opt, grp);
419    
420      if (!g)      if (!g)
421          return NULL;          return NULL;
# Line 423  find_member (config_file_t opt, const ch Line 429  find_member (config_file_t opt, const ch
429    
430    
431  int  int
432  delete_group (config_file_t opt, const char *str)  conf_delete_group (config_file_t opt, const char *str)
433  {  {
434      gpg_group_t g = find_group (opt, str);      conf_group_t g = conf_find_group (opt, str);
435    
436      if (g)      if (g)
437          g->used = 0;          g->used = 0;
# Line 434  delete_group (config_file_t opt, const c Line 440  delete_group (config_file_t opt, const c
440    
441    
442  int  int
443  delete_member (config_file_t opt, const char *grp, const char *str)  conf_delete_member (config_file_t opt, const char *grp, const char *str)
444  {  {
445      gpg_member_t m = find_member (opt, grp, str);      conf_member_t m = conf_find_member (opt, grp, str);
446    
447      if (m)      if (m)
448          m->used = 0;          m->used = 0;
# Line 445  delete_member (config_file_t opt, const Line 451  delete_member (config_file_t opt, const
451    
452            
453  int  int
454  delete_option (config_file_t opt, const char * str)  conf_delete_option (config_file_t opt, const char * str)
455  {  {
456      gpg_option_t e = find_option (opt, str);      conf_option_t e = conf_find_option (opt, str);
457    
458      if (e)      if (e)
459          e->used = 0;          e->used = 0;
# Line 456  delete_option (config_file_t opt, const Line 462  delete_option (config_file_t opt, const
462    
463    
464  int  int
465  add_entry_int (config_file_t opt, int type, const char *name, int val)  conf_add_entry_int (config_file_t opt, int type, const char *name, int val)
466  {  {
467      char buf[64];      char buf[64];
468    
469      _snprintf (buf, DIM (buf)-1, "%d", val);      _snprintf (buf, DIM (buf)-1, "%d", val);
470      add_entry (opt, type, name, buf);      conf_add_entry (opt, type, name, buf);
471      return 0;      return 0;
472  }  }
473    
474  int  int
475  add_entry (config_file_t opt, int type, const char *name, const char *val)  conf_add_entry (config_file_t opt, int type, const char *name, const char *val)
476  {  {
477      gpg_option_t e;      conf_option_t e;
478    
479      e = new_option ();      e = new_option ();
480      e->used = 1;      e->used = 1;
# Line 480  add_entry (config_file_t opt, int type, Line 486  add_entry (config_file_t opt, int type,
486  }  }
487    
488    
489    /* Modify the value of the entry with the name @name.
490       If the entry were not found, add it to the option list. */
491  int  int
492  modify_entry (config_file_t opt, int type, const char *name, const char *val)  conf_modify_entry (config_file_t opt, int type, const char *name, const char *val)
493  {  {
494      gpg_option_t e;      conf_option_t e;
495      int rc = 0;      int rc = 0;
496    
497      e = find_option (opt, name);      e = conf_find_option (opt, name);
498      if (!e)      if (!e)
499          rc = add_entry (opt, type, name, val);          rc = conf_add_entry (opt, type, name, val);
500      else if (type != ENTRY_SINGLE) {      else if (type != ENTRY_SINGLE && val != NULL) {
501          free_if_alloc (e->val);          free_if_alloc (e->val);
502          e->val = m_strdup (val);          e->val = m_strdup (val);
503          rc = 0;          rc = 0;
# Line 500  modify_entry (config_file_t opt, int typ Line 508  modify_entry (config_file_t opt, int typ
508    
509            
510  int  int
511  add_member (config_file_t opt, const char *grp, const char *str)  conf_add_member (config_file_t opt, const char *grp, const char *str)
512  {  {
513      gpg_group_t g = find_group (opt, grp);      conf_group_t g = conf_find_group (opt, grp);
514      gpg_member_t m, t;      conf_member_t m, t;
515    
516      if (!g)      if (!g)
517          return -1;          return -1;
518            
519      m = new gpg_member_s;          m = new conf_member_s;    
520      if (!m)          if (!m)    
521          BUG (NULL);          BUG (NULL);
522      memset (m, 0, sizeof *m);      memset (m, 0, sizeof *m);
# Line 526  add_member (config_file_t opt, const cha Line 534  add_member (config_file_t opt, const cha
534    
535    
536  int  int
537  add_group (config_file_t opt, const char *str)  conf_add_group (config_file_t opt, const char *str)
538  {  {
539      gpg_group_t g, t;      conf_group_t g, t;
540    
541      g = new gpg_group_s;      g = new conf_group_s;
542      if (!g)      if (!g)
543          BUG (NULL);          BUG (NULL);
544      memset (g, 0, sizeof *g);      memset (g, 0, sizeof *g);
# Line 544  add_group (config_file_t opt, const char Line 552  add_group (config_file_t opt, const char
552              ;              ;
553          t->next = g;          t->next = g;
554      }      }
555      add_entry (opt, ENTRY_GROUP, str, NULL);      conf_add_entry (opt, ENTRY_GROUP, str, NULL);
556      return 0;      return 0;
557  }  }

Legend:
Removed from v.270  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26