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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
|
/*
* nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
* and Linux systems.
*
* Copyright (C) 2004 NVIDIA Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>.
*/
/*
* config-file.c - this source file contains functions for processing
* the NVIDIA X Server control panel configuration file.
*
* The configuration file is simply a newline-separated list of
* attribute strings, where the syntax of an attribute string is
* described in the comments of parse.h
*
* The pound sign ('#') signifies the beginning of a comment; comments
* continue until a newline.
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include "NvCtrlAttributes.h"
#include "config-file.h"
#include "query-assign.h"
#include "parse.h"
#include "msg.h"
typedef struct {
ParsedAttribute a;
int line;
CtrlHandles *h;
} ParsedAttributeWrapper;
static ParsedAttributeWrapper *parse_config_file(char *buf,
const char *file,
const int length,
ConfigProperties *);
static int process_config_file_attributes(const char *file,
ParsedAttributeWrapper *w,
const char *display_name);
static void save_gui_parsed_attributes(ParsedAttributeWrapper *w,
ParsedAttribute *p);
static float get_color_value(int attr,
float c[3], float b[3], float g[3]);
static int parse_config_property(const char *file, const char *line,
ConfigProperties *conf);
static void write_config_properties(FILE *stream, ConfigProperties *conf,
char *locale);
extern int __verbosity;
extern int __verbosity_level_changed;
/*
* nv_read_config_file() - read the specified config file, building a
* list of attributes to send. Once all attributes are read, send
* them to the X server.
*
* mmap(2) the file into memory for easier manipulation.
*
* If an error occurs while parsing the configuration file, an error
* message is printed to stderr, NV_FALSE is returned, and nothing is
* sent to the X server.
*
* NOTE: The conf->locale should have already been setup by calling
* init_config_properties() prior to calling this function.
*
* XXX should we do any sort of versioning to handle compatibility
* problems in the future?
*/
int nv_read_config_file(const char *file, const char *display_name,
ParsedAttribute *p, ConfigProperties *conf)
{
int fd, ret, length;
struct stat stat_buf;
char *buf, *locale;
ParsedAttributeWrapper *w = NULL;
if (!file) {
/*
* file is NULL, likely because tilde_expansion() failed and
* returned NULL; silently fail
*/
return NV_FALSE;
}
/* open the file */
fd = open(file, O_RDONLY);
if (fd == -1) {
/*
* It's OK if the file doesn't exist... but should we print a
* warning?
*/
return NV_FALSE;
}
/* get the size of the file */
ret = fstat(fd, &stat_buf);
if (ret == -1) {
nv_error_msg("Unable to determine size of file '%s' (%s).",
file, strerror(errno));
return NV_FALSE;
}
if (stat_buf.st_size == 0) {
nv_warning_msg("File '%s' has zero size; not reading.", file);
close(fd);
return NV_TRUE;
}
length = stat_buf.st_size;
/* map the file into memory */
buf = mmap(0, length, PROT_READ, MAP_SHARED, fd, 0);
if (buf == (void *) -1) {
nv_error_msg("Unable to mmap file '%s' for reading (%s).",
file, strerror(errno));
return NV_FALSE;
}
/*
* save the current locale, parse the actual text in the file
* and restore the saved locale (could be changed).
*/
locale = strdup(conf->locale);
w = parse_config_file(buf, file, length, conf);
setlocale(LC_NUMERIC, locale);
free(locale);
/* unmap and close the file */
if (munmap (buf, stat_buf.st_size) == -1) {
nv_error_msg("Unable to unmap file '%s' after reading (%s).",
file, strerror(errno));
return NV_FALSE;
}
close(fd);
if (!w) return NV_FALSE;
/* process the parsed attributes */
ret = process_config_file_attributes(file, w, display_name);
/*
* add any relevant parsed attributes back to the list to be
* passed to the gui
*/
save_gui_parsed_attributes(w, p);
if (w) free(w);
return ret;
} /* nv_read_config_file() */
/*
* nv_write_config_file() - write a configuration file to the
* specified filename.
*
* XXX how should this be handled? Currently, we just query all
* writable attributes, writing their current value to file.
*
* XXX should query first, and only once we know we can't fail, then
* write the file (to avoid deleting the existing file).
*/
int nv_write_config_file(const char *filename, CtrlHandles *h,
ParsedAttribute *p, ConfigProperties *conf)
{
int screen, ret, entry, bit, val, display, randr_gamma_available;
FILE *stream;
time_t now;
AttributeTableEntry *a;
ReturnStatus status;
NVCTRLAttributeValidValuesRec valid;
uint32 mask;
CtrlHandleTarget *t;
char *tmp_d_str, *prefix, scratch[4];
const char *tmp;
char *locale = "C";
if (!filename) {
nv_error_msg("Unable to open configuration file for writing.");
return NV_FALSE;
}
stream = fopen(filename, "w");
if (!stream) {
nv_error_msg("Unable to open file '%s' for writing.", filename);
return NV_FALSE;
}
/* write header */
now = time(NULL);
fprintf(stream, "#\n");
fprintf(stream, "# %s\n", filename);
fprintf(stream, "#\n");
fprintf(stream, "# Configuration file for nvidia-settings - the NVIDIA "
"X Server Settings utility\n");
/* NOTE: ctime(3) generates a new line */
fprintf(stream, "# Generated on %s", ctime(&now));
fprintf(stream, "#\n");
/*
* set the locale to "C" before writing the configuration file to
* reduce the risk of locale related parsing problems. Restore
* the original locale before exiting this function.
*/
if (setlocale(LC_NUMERIC, "C") == NULL) {
nv_warning_msg("Error writing configuration file '%s': could "
"not set the locale 'C'.", filename);
locale = conf->locale;
}
/* write the values in ConfigProperties */
write_config_properties(stream, conf, locale);
/* for each screen, query each attribute in the table */
fprintf(stream, "\n");
fprintf(stream, "# Attributes:\n");
fprintf(stream, "\n");
/*
* Note: we only save writable attributes addressable by X screen here
* followed by attributes for display target types.
*/
for (screen = 0; screen < h->targets[X_SCREEN_TARGET].n; screen++) {
t = &h->targets[X_SCREEN_TARGET].t[screen];
/* skip it if we don't have a handle for this screen */
if (!t->h) continue;
/*
* construct the prefix that will be printed in the config
* file infront of each attribute on this screen; this will
* either be "[screen]" or "[displayname]".
*/
if (conf->booleans &
CONFIG_PROPERTIES_INCLUDE_DISPLAY_NAME_IN_CONFIG_FILE) {
prefix = t->name;
} else {
snprintf(scratch, 4, "%d", screen);
prefix = scratch;
}
/* loop over all the entries in the table */
for (entry = 0; attributeTable[entry].name; entry++) {
a = &attributeTable[entry];
/*
* skip all attributes that are not supposed to be written
* to the config file
*/
if (a->flags & NV_PARSER_TYPE_NO_CONFIG_WRITE) continue;
/*
* special case the color attributes because we want to
* print floats
*/
if (a->flags & NV_PARSER_TYPE_COLOR_ATTRIBUTE) {
float c[3], b[3], g[3];
/*
* if we are using RandR gamma, skip saving the color info
*/
status = NvCtrlGetAttribute(t->h, NV_CTRL_ATTR_RANDR_GAMMA_AVAILABLE, &val);
if (status == NvCtrlSuccess && val) continue;
status = NvCtrlGetColorAttributes(t->h, c, b, g);
if (status != NvCtrlSuccess) continue;
fprintf(stream, "%s%c%s=%f\n",
prefix, DISPLAY_NAME_SEPARATOR, a->name,
get_color_value(a->attr, c, b, g));
continue;
}
for (bit = 0; bit < 24; bit++) {
mask = 1 << bit;
/*
* if this bit is not present in the screens's enabled
* display device mask (and the X screen has enabled
* display devices), skip to the next bit
*/
if (((t->d & mask) == 0x0) && (t->d)) continue;
status = NvCtrlGetValidDisplayAttributeValues
(t->h, mask, a->attr, &valid);
if (status != NvCtrlSuccess) goto exit_bit_loop;
if ((valid.permissions & ATTRIBUTE_TYPE_WRITE) == 0x0)
goto exit_bit_loop;
status = NvCtrlGetDisplayAttribute(t->h, mask, a->attr, &val);
if (status != NvCtrlSuccess) goto exit_bit_loop;
if (valid.permissions & ATTRIBUTE_TYPE_DISPLAY) {
tmp_d_str =
display_device_mask_to_display_device_name(mask);
fprintf(stream, "%s%c%s[%s]=%d\n", prefix,
DISPLAY_NAME_SEPARATOR, a->name, tmp_d_str, val);
free(tmp_d_str);
continue;
} else {
fprintf(stream, "%s%c%s=%d\n", prefix,
DISPLAY_NAME_SEPARATOR, a->name, val);
/* fall through to exit_bit_loop */
}
exit_bit_loop:
bit = 25; /* XXX force us out of the display device loop */
} /* bit */
} /* entry */
} /* screen */
/*
* Write attributes addresssable to display targets
*/
for (display = 0; display < h->targets[DISPLAY_TARGET].n; display++) {
t = &h->targets[DISPLAY_TARGET].t[display];
/* skip it if we don't have a handle for this display */
if (!t->h) continue;
/*
* check to see if we have RANDR gamma available. We may
* skip writing attributes if it is missing.
*/
status = NvCtrlGetAttribute(t->h,
NV_CTRL_ATTR_RANDR_GAMMA_AVAILABLE,
&randr_gamma_available);
if (status != NvCtrlSuccess) {
randr_gamma_available = 0;
}
/*
* use the full display name as the prefix for display targets
* until we can parse other display target names
*/
prefix = t->name;
/* loop over all the entries in the table */
for (entry = 0; attributeTable[entry].name; entry++) {
a = &attributeTable[entry];
/*
* skip all attributes that are not supposed to be written
* to the config file
*/
if (a->flags & NV_PARSER_TYPE_NO_CONFIG_WRITE) continue;
/*
* for the display target we only write color attributes for now
*/
if (a->flags & NV_PARSER_TYPE_COLOR_ATTRIBUTE) {
float c[3], b[3], g[3];
if (!randr_gamma_available) continue;
status = NvCtrlGetColorAttributes(t->h, c, b, g);
if (status != NvCtrlSuccess) continue;
fprintf(stream, "%s%c%s=%f\n",
prefix, DISPLAY_NAME_SEPARATOR, a->name,
get_color_value(a->attr, c, b, g));
}
}
}
/*
* loop the ParsedAttribute list, writing the attributes to file.
* note that we ignore conf->include_display_name_in_config_file
* when writing these parsed attributes; this is because parsed
* attributes (like the framelock properties) require a display
* name be specified (since there are multiple X servers
* involved).
*/
while (p) {
char target_str[64];
if (!p->next) {
p = p->next;
continue;
}
tmp = nv_get_attribute_name(p->attr, NV_PARSER_TYPE_STRING_ATTRIBUTE,
p->flags);
if (!tmp) {
nv_error_msg("Failure to save unknown attribute %d.", p->attr);
p = p->next;
continue;
}
/*
* if the parsed attribute has a target specification, and a
* target type other than an X screen, include a target
* specification in what we write to the .rc file.
*/
target_str[0] = '\0';
if ((p->flags & NV_PARSER_HAS_TARGET) &&
(p->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN)) {
TargetTypeEntry *targetTypeEntry;
/* Find the target name of the target type */
targetTypeEntry = nv_get_target_type_entry_by_nvctrl(p->target_type);
if (targetTypeEntry) {
snprintf(target_str, 64, "[%s:%d]",
targetTypeEntry->parsed_name, p->target_id);
}
}
if (p->display_device_mask) {
tmp_d_str = display_device_mask_to_display_device_name
(p->display_device_mask);
fprintf(stream, "%s%s%c%s[%s]=%d\n", p->display, target_str,
DISPLAY_NAME_SEPARATOR, tmp, tmp_d_str, p->val.i);
free(tmp_d_str);
} else {
fprintf(stream, "%s%s%c%s=%d\n", p->display, target_str,
DISPLAY_NAME_SEPARATOR, tmp, p->val.i);
}
p = p->next;
}
setlocale(LC_NUMERIC, conf->locale);
/* close the configuration file */
ret = fclose(stream);
if (ret != 0) {
nv_error_msg("Failure while closing file '%s'.", filename);
return NV_FALSE;
}
return NV_TRUE;
} /* nv_write_config_file() */
/*
* parse_config_file() - scan through the buffer; skipping comment
* lines. Non-comment lines with non-whitespace characters are passed
* on to nv_parse_attribute_string for parsing.
*
* If an error occurs, an error message is printed and NULL is
* returned. If successful, a malloced array of
* ParsedAttributeWrapper structs is returned. The last
* ParsedAttributeWrapper in the array has line == -1. It is the
* caller's responsibility to free the array.
*/
static ParsedAttributeWrapper *parse_config_file(char *buf, const char *file,
const int length,
ConfigProperties *conf)
{
int line, has_data, current_tmp_len, len, n, ret;
char *cur, *c, *comment, *tmp;
ParsedAttributeWrapper *w;
cur = buf;
line = 1;
current_tmp_len = 0;
n = 0;
w = NULL;
tmp = NULL;
while (cur) {
c = cur;
comment = NULL;
has_data = NV_FALSE;;
while (((c - buf) < length) &&
(*c != '\n') &&
(*c != '\0')) {
if (comment) { c++; continue; }
if (*c == '#') { comment = c; continue; }
if (!isspace(*c)) has_data = NV_TRUE;
c++;
}
if (has_data) {
if (!comment) comment = c;
len = comment - cur;
/* grow the tmp buffer if it's too small */
if (len >= current_tmp_len) {
current_tmp_len = len + 1;
if (tmp) {
free(tmp);
}
tmp = malloc(sizeof(char) * current_tmp_len);
}
strncpy (tmp, cur, len);
tmp[len] = '\0';
/* first, see if this line is a config property */
if (!parse_config_property(file, tmp, conf)) {
w = realloc(w, sizeof(ParsedAttributeWrapper) * (n+1));
ret = nv_parse_attribute_string(tmp,
NV_PARSER_ASSIGNMENT,
&w[n].a);
if (ret != NV_PARSER_STATUS_SUCCESS) {
nv_error_msg("Error parsing configuration file '%s' on "
"line %d: '%s' (%s).",
file, line, tmp, nv_parse_strerror(ret));
goto failed;
}
w[n].line = line;
n++;
}
}
if (((c - buf) >= length) || (*c == '\0')) cur = NULL;
else cur = c + 1;
line++;
}
free(tmp);
/* mark the end of the array */
w = realloc(w, sizeof(ParsedAttributeWrapper) * (n+1));
w[n].line = -1;
return w;
failed:
if (w) free(w);
free(tmp);
return NULL;
} /* parse_config_file() */
/*
* process_config_file_attributes() - process the list of
* attributes to be assigned that we acquired in parsing the config
* file.
*/
static int process_config_file_attributes(const char *file,
ParsedAttributeWrapper *w,
const char *display_name)
{
int i, j, found, n = 0;
CtrlHandles **h = NULL;
int old_verbosity = __verbosity;
/* Override the verbosity in the default behavior so
* nvidia-settings isn't so alarmist when loading the RC file.
*/
if (!__verbosity_level_changed) {
__verbosity = VERBOSITY_NONE;
}
/*
* make sure that all ParsedAttributes have displays (this will do
* nothing if we already have a display name
*/
for (i = 0; w[i].line != -1; i++) {
nv_assign_default_display(&w[i].a, display_name);
}
/* build the list of CtrlHandles */
for (i = 0; w[i].line != -1; i++) {
found = NV_FALSE;
for (j = 0; j < n; j++) {
if (nv_strcasecmp(h[j]->display, w[i].a.display)) {
w[i].h = h[j];
found = NV_TRUE;
break;
}
}
/*
* no handle found for this display, need to create a new
* handle.
*
* XXX we should really just build a list of what ctrl_handles
* we need, and what attributes on which ctrl_handles, so that
* we don't have to pass NV_CTRL_ATTRIBUTES_ALL_SUBSYSTEMS to
* NvCtrlAttributeInit (done in nv_alloc_ctrl_handles())
* unless we really need it.
*/
if (!found) {
h = realloc(h, sizeof(CtrlHandles *) * (n + 1));
h[n] = nv_alloc_ctrl_handles(w[i].a.display);
w[i].h = h[n];
n++;
}
}
/* now process each attribute, passing in the correct CtrlHandles */
for (i = 0; w[i].line != -1; i++) {
nv_process_parsed_attribute(&w[i].a, w[i].h, NV_TRUE, NV_FALSE,
"on line %d of configuration file "
"'%s'", w[i].line, file);
/*
* We do not fail if processing the attribute failed. If the
* GPU or the X config changed (for example stereo is
* disabled), some attributes written in the config file may
* not be advertised by the the NVCTRL extension (for example
* the control to force stereo)
*/
}
/* Reset the default verbosity */
if (!__verbosity_level_changed) {
__verbosity = old_verbosity;
}
/* free all the CtrlHandles we allocated */
for (i = 0; i < n; i++) {
nv_free_ctrl_handles(h[i]);
}
if (h) free(h);
return NV_TRUE;
} /* process_config_file_attributes() */
/*
* save_gui_parsed_attributes() - scan through the parsed attribute
* wrappers, and save any relevant attributes to the attribute list to
* be passed to the gui.
*/
static void save_gui_parsed_attributes(ParsedAttributeWrapper *w,
ParsedAttribute *p)
{
int i;
for (i = 0; w[i].line != -1; i++) {
if (w[i].a.flags & NV_PARSER_TYPE_GUI_ATTRIBUTE) {
nv_parsed_attribute_add(p, &w[i].a);
}
}
} /* save_gui_parsed_attributes() */
static float get_color_value(int attr, float c[3], float b[3], float g[3])
{
switch (attr & (ALL_VALUES | ALL_CHANNELS)) {
case (CONTRAST_VALUE | RED_CHANNEL): return c[RED_CHANNEL_INDEX];
case (CONTRAST_VALUE | GREEN_CHANNEL): return c[GREEN_CHANNEL_INDEX];
case (CONTRAST_VALUE | BLUE_CHANNEL): return c[BLUE_CHANNEL_INDEX];
case (BRIGHTNESS_VALUE | RED_CHANNEL): return b[RED_CHANNEL_INDEX];
case (BRIGHTNESS_VALUE | GREEN_CHANNEL): return b[GREEN_CHANNEL_INDEX];
case (BRIGHTNESS_VALUE | BLUE_CHANNEL): return b[BLUE_CHANNEL_INDEX];
case (GAMMA_VALUE | RED_CHANNEL): return g[RED_CHANNEL_INDEX];
case (GAMMA_VALUE | GREEN_CHANNEL): return g[GREEN_CHANNEL_INDEX];
case (GAMMA_VALUE | BLUE_CHANNEL): return g[BLUE_CHANNEL_INDEX];
default: return 0.0;
}
} /* get_color_value() */
/*
* Table of ConfigProperties (properties of the nvidia-settings
* utilities itself, rather than properties of the X screen(s) that
* nvidia-settings is configuring). The table just binds string names
* to the bitmask constants.
*/
typedef struct {
char *name;
unsigned int flag;
} ConfigPropertiesTableEntry;
ConfigPropertiesTableEntry configPropertyTable[] = {
{ "ToolTips", CONFIG_PROPERTIES_TOOLTIPS },
{ "DisplayStatusBar", CONFIG_PROPERTIES_DISPLAY_STATUS_BAR },
{ "SliderTextEntries", CONFIG_PROPERTIES_SLIDER_TEXT_ENTRIES },
{ "IncludeDisplayNameInConfigFile",
CONFIG_PROPERTIES_INCLUDE_DISPLAY_NAME_IN_CONFIG_FILE },
{ "ShowQuitDialog", CONFIG_PROPERTIES_SHOW_QUIT_DIALOG },
{ NULL, 0 }
};
/*
* parse_config_property() - special case the config properties; if
* the given line sets a config property, update conf as appropriate
* and return NV_TRUE. If the given line does not describe a config
* property, return NV_FALSE.
*/
static int parse_config_property(const char *file, const char *line, ConfigProperties *conf)
{
char *no_spaces, *s;
char *locale;
ConfigPropertiesTableEntry *t;
char *timer, *token;
TimerConfigProperty *c = NULL;
int interval;
int ret = NV_FALSE;
unsigned int flag;
no_spaces = remove_spaces(line);
if (!no_spaces) goto done;
s = strchr(no_spaces, '=');
if (!s) goto done;
*s = '\0';
if (nv_strcasecmp(no_spaces, "RcFileLocale")) {
locale = ++s;
if (setlocale(LC_NUMERIC, locale) == NULL) {
nv_warning_msg("Error parsing configuration file '%s': could "
"not set the specified locale '%s'.",
file, locale);
}
} else if (nv_strcasecmp(no_spaces, "Timer")) {
timer = ++s;
token = strtok(timer, ",");
if (!token)
goto done;
c = malloc(sizeof(TimerConfigProperty));
if (!c) {
nv_warning_msg("Error parsing configuration file '%s': could "
"not allocate memory for timer '%s'.",
file, timer);
ret = NV_TRUE;
goto done;
}
c->description = replace_characters(token, '_', ' ');
if (!c->description) {
nv_warning_msg("Error parsing configuration file '%s': could "
"not allocate memory for timer '%s'.",
file, timer);
ret = NV_TRUE;
goto done;
}
token = strtok(NULL, ",");
if (!token)
goto done;
if (nv_strcasecmp(token, "Yes")) {
c->user_enabled = 1;
} else if (nv_strcasecmp(token, "No")) {
c->user_enabled = 0;
} else {
goto done;
}
token = strtok(NULL, ",");
if (!token)
goto done;
parse_read_integer(token, &interval);
c->interval = interval;
c->next = conf->timers;
conf->timers = c;
} else {
for (t = configPropertyTable, flag = 0; t->name; t++) {
if (nv_strcasecmp(no_spaces, t->name)) {
flag = t->flag;
break;
}
}
if (!flag) goto done;
s++;
if (nv_strcasecmp(s, "yes")) {
conf->booleans |= flag;
} else if (nv_strcasecmp(s, "no")) {
conf->booleans &= ~flag;
} else {
goto done;
}
}
ret = NV_TRUE;
done:
if ((ret != NV_TRUE) && c) {
if (c->description)
free(c->description);
free(c);
}
if (no_spaces) free(no_spaces);
return ret;
} /* parse_config_property() */
/*
* write_config_properties() - write the ConfigProperties to file;
* this just amounts to looping through the table, and printing if
* each property is enabled or disabled.
*/
static void write_config_properties(FILE *stream, ConfigProperties *conf, char *locale)
{
ConfigPropertiesTableEntry *t;
TimerConfigProperty *c;
char *description;
fprintf(stream, "\n");
fprintf(stream, "# ConfigProperties:\n");
fprintf(stream, "\n");
fprintf(stream, "RcFileLocale = %s\n", locale);
for (t = configPropertyTable; t->name; t++) {
fprintf(stream, "%s = %s\n", t->name,
(t->flag & conf->booleans) ? "Yes" : "No");
}
for (c = conf->timers; (c != NULL); c = c->next) {
description = replace_characters(c->description, ' ', '_');
if (!description)
continue;
fprintf(stream, "Timer = %s,%s,%u\n",
description, c->user_enabled ? "Yes" : "No",
c->interval);
free(description);
}
} /* write_config_properties()*/
/*
* init_config_properties() - initialize the ConfigProperties
* structure.
*/
void init_config_properties(ConfigProperties *conf)
{
memset(conf, 0, sizeof(ConfigProperties));
conf->booleans =
(CONFIG_PROPERTIES_TOOLTIPS |
CONFIG_PROPERTIES_DISPLAY_STATUS_BAR |
CONFIG_PROPERTIES_SLIDER_TEXT_ENTRIES |
CONFIG_PROPERTIES_SHOW_QUIT_DIALOG);
conf->locale = strdup(setlocale(LC_NUMERIC, NULL));
} /* init_config_properties() */
|