]> git.deb.at Git - pkg/abook.git/blob - options.c
getopt + fixes
[pkg/abook.git] / options.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "abook_curses.h"
14 #include "abook.h"
15 #include "options.h"
16 #ifdef HAVE_CONFIG_H
17 #       include "config.h"
18 #endif
19
20 struct conff_node *abook_config;
21
22 static int      rcfile_exist();
23 static void     default_options();
24
25 extern char *rcfile;
26
27 static char *
28 abook_opt_conff_get_val(char *key)
29 {
30         int tried;
31         char *value = NULL;
32
33
34         for(tried = 0; tried < 2; ) {
35                 if( ( value = conff_get_value(abook_config, key) )
36                                 == 0 ) {
37                         tried ++;
38                         default_options(); /* try with defaults */
39                 } else
40                         return value;
41         }
42         return NULL;
43 }
44
45 int
46 options_get_int(char *key)
47 {
48         char *value;
49         int ret;
50         
51         if( ( value = abook_opt_conff_get_val(key) )
52                         == NULL)
53                 return 1;
54
55         if( !strcasecmp(value, "true") )
56                 ret = 1;
57         else
58         if( !strcasecmp(value, "false") )
59                 ret = 0;
60         else
61                 ret = safe_atoi(value);
62         
63         return ret;
64 }
65         
66 char *
67 options_get_str(char *key)
68 {
69         return abook_opt_conff_get_val(key); 
70 }
71                 
72 void
73 init_options()
74 {
75         abook_config = NULL;
76
77         if( rcfile_exist() )
78                 load_options();
79         else
80                 default_options();
81 }
82
83 void
84 close_config()
85 {
86         /*
87          * we don't save the configuration file since user can specify
88          * alternative configuration files
89          */
90 #if 0
91         save_options();
92 #endif
93
94         conff_free_nodes(abook_config);
95 }
96
97 static int
98 rcfile_exist()
99 {
100         return ( (0 == access(SYSWIDE_RCFILE, F_OK)) ||
101                         (0 == access(rcfile, F_OK)) );
102 }
103
104 void
105 load_options()
106 {
107         int ret;
108         
109         if( (ret = conff_load_file(&abook_config, rcfile,
110                                 REPLACE_KEY)) > 0) {
111                 fprintf(stderr, "%s: parse error at line %d\n", rcfile, ret);
112                 exit(1);
113         }
114
115         if( (ret = conff_load_file(&abook_config, SYSWIDE_RCFILE,
116                                         DONT_REPLACE_KEY )) > 0) {
117                 fprintf(stderr, "%s: parse error at line %d\n",
118                                 SYSWIDE_RCFILE, ret);
119                 exit(1);
120         }
121 }
122
123 #if 0
124 void
125 save_options()
126 {
127         if( rcfile_exist() ) /* don't overwrite existing config */
128                 return;
129
130         conff_save_file(abook_config, rcfile);
131 }
132 #endif
133
134 static void
135 options_add_key(char *key, char *value)
136 {
137         const int flags = DONT_REPLACE_KEY;
138
139         conff_add_key(&abook_config, key, value, flags);
140 }
141
142 static void
143 default_options()
144 {
145         options_add_key("autosave", "true");
146
147         options_add_key("show_all_emails", "true");
148         options_add_key("emailpos", "25");
149         options_add_key("extra_column", "7");
150         options_add_key("extra_alternative", "-1");
151         options_add_key("extrapos", "65");
152
153         options_add_key("mutt_command", "mutt");
154         options_add_key("mutt_return_all_emails", "true");
155
156         options_add_key("print_command", "lpr");
157
158         options_add_key("filesel_sort", "false");
159
160         options_add_key("www_command", "lynx");
161
162         options_add_key("address_style", "eu");
163
164         options_add_key("use_ascii_only", "false");
165 }