File: unixoptions.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (1213 lines) | stat: -rw-r--r-- 32,232 bytes parent folder | download | duplicates (4)
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
/*******************************************************************************
 * unixoptions.cpp
 *
 * Written by Christoph Hormann <chris_hormann@gmx.de>
 * Based on 3.6 elements by Nicolas Calimet
 *
 * Processing system for options in povray.conf, command line
 * and environment variables.
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/vfe/unix/unixoptions.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

#include "unixoptions.h"
#include <fstream>
#include <sys/stat.h>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>

namespace vfePlatform
{
	using std::cerr;
	using std::endl;

	extern bool gShelloutsPermittedFixThis;

	const UnixOptionsProcessor::Option_Info UnixOptionsProcessor::Standard_Options[] =
	{
		// section name, option name, default, has_param, command line parameter, environment variable name, help text
		UnixOptionsProcessor::Option_Info("general", "help", "off", false, "--help|-help|-h|-?", "", "display usage information"),
		UnixOptionsProcessor::Option_Info("general", "temppath", "", true, "", "POV_TEMP_DIR", "directory for temporary files"),
		UnixOptionsProcessor::Option_Info("general", "version", "off", false, "--version|-version|--V", "", "display program version"),
		UnixOptionsProcessor::Option_Info("general", "benchmark", "off", false, "--benchmark|-benchmark", "", "run the standard POV-Ray benchmark"),
		UnixOptionsProcessor::Option_Info("", "", "", false, "", "", "") // has to be last
	};

	// based on 3.6 unix_create_globals()
	UnixOptionsProcessor::UnixOptionsProcessor(vfeSession *session) :
		m_Session(session)
	{
		char* value;
		value = getenv("HOME");
		m_home = value ? value:"";

		// Default values for I/O restrictions: everything is allowed.
		// Any restrictions must come from system or user configuration.
		m_file_io  = IO_UNSET;
		m_shellout = SHL_UNSET;

		// system configuration file
		m_conf    = "";
		m_sysconf = POVCONFDIR "/povray.conf";

		// user configuration file
		if (m_home.length() > 0)
		{
			m_user_dir = m_home + "/." PACKAGE "/" VERSION_BASE;
			m_userconf = m_home + "/." PACKAGE "/" VERSION_BASE "/povray.conf";
		}
		else
		{
			m_user_dir = "";
			m_userconf = "";
		}

		// system ini file
		m_sysini     = POVCONFDIR "/povray.ini";
		m_sysini_old = POVCONFDIR_BACKWARD "/povray.ini";

		// user ini file
		if (m_home.length() > 0)
			m_userini = m_home + "/." PACKAGE "/" VERSION_BASE "/povray.ini";
		else
			m_userini = "";

		if (m_home.length() > 0)
			m_userini_old = m_home + "/.povrayrc";
		else
			m_userini_old = "";

#ifdef UNIX_DEBUG
		cerr << "PATHS" << endl;
		cerr << "  HOME        = " << m_home << endl;
		cerr << "  SYSCONF     = " << m_sysconf << endl;
		cerr << "  USERCONF    = " << m_userconf << endl;
		cerr << "  SYSINI      = " << m_sysini << endl;
		cerr << "  SYSINI_OLD  = " << m_sysini_old << endl;
		cerr << "  USERINI     = " << m_userini << endl;
		cerr << "  USERINI_OLD = " << m_userini_old << endl;
#endif
#if 0
		cerr << "--- tests ---" << endl;
		cerr << "## unix_getcwd: " << unix_getcwd() << endl;
		cerr << "## basename(SYSCONF): " << basename(m_sysconf) << endl;
		cerr << "## dirname(SYSCONF): " << dirname(m_sysconf) << endl;
		cerr << "## basename(/test/path/): " << basename("/test/path/") << endl;
		cerr << "## dirname(/test/path/): " << dirname("/test/path/") << endl;
		cerr << "## CanonicalizePath(../test/to/file/): " << CanonicalizePath("../test/to/file/") << endl;
		cerr << "## CanonicalizePath(/another/dir/../file): " << CanonicalizePath("/another/dir/../file") << endl;
		cerr << "------------" << endl;
#endif

		// register all standard options
		for (int i = 0; Standard_Options[i].Section != ""; i++)
		{
			list<Option_Info>::iterator iter = find(m_user_options.begin(), m_user_options.end(), Standard_Options[i]);

			// add this option if not already there
			if (iter == m_user_options.end())
				m_user_options.push_back(Standard_Options[i]);
		}

		// process system and user povray.conf
		process_povray_conf();
	}

	string UnixOptionsProcessor::GetTemporaryPath(void)
	{
		string path = QueryOptionString("general", "temppath");
		if (path.length() == 0)
		{
			struct stat s;
			path = "/tmp/";
			if (stat (path.c_str(), &s) == 0  &&  S_ISDIR (s.st_mode)  &&  (s.st_mode & S_IRWXU) == S_IRWXU)
				return path;
			path = unix_getcwd();
		}
		if (path[path.length()-1] != '/')
			path = path + "/";
		return path;
	}

	void UnixOptionsProcessor::PrintOptions(void)
	{
		cerr << endl;
		cerr << "Platform specific command line options:" << endl;

		string section("");
		bool section_new = false;

		for (list<Option_Info>::iterator iter = m_user_options.begin(); iter != m_user_options.end(); iter++)
		{
			if ((*iter).Section != section)
			{
				section = (*iter).Section;
				section_new = true;
			}
			if ((*iter).CmdOption != "")
			{
				if (section_new)
				{
					cerr << endl;
					cerr << "  '" << section << "' options:" << endl << endl;
					section_new = false;
				}
				cerr << "    " << boost::format("%1% %|32t|%2%") % (*iter).CmdOption % (*iter).Comment << endl;
			}
		}
		cerr << endl;
	}

	void UnixOptionsProcessor::Register(const Option_Info options[])
	{
		for (int i = 0; options[i].Section != ""; i++)
		{
			list<Option_Info>::iterator iter = find(m_user_options.begin(), m_user_options.end(), options[i]);

			// add this option if not already there
			if (iter == m_user_options.end())
				m_user_options.push_back(options[i]);
		}
	}

	void UnixOptionsProcessor::QueryOption(Option_Info &option)
	{
		list<Option_Info>::iterator iter = find(m_user_options.begin(), m_user_options.end(), option);

		if (iter != m_user_options.end())
			option.Value = (*iter).Value;
		else
			option.Value = "";
	}

	string UnixOptionsProcessor::QueryOptionString(const string &section, const string &name)
	{
		Option_Info opt(section, name);
		QueryOption(opt);
		return opt.Value;
	}

	int UnixOptionsProcessor::QueryOptionInt(const string &section, const string &name, const int dflt)
	{
		int res;
		try
		{
			res = boost::lexical_cast<int>(QueryOptionString(section, name));
		}
		catch(boost::bad_lexical_cast &)
		{
			res = dflt;
		}
		return res;
	}

	float UnixOptionsProcessor::QueryOptionFloat(const string &section, const string &name, const float dflt)
	{
		float res;
		try
		{
			res = boost::lexical_cast<float>(QueryOptionString(section, name));
		}
		catch(boost::bad_lexical_cast &)
		{
			res = dflt;
		}
		return res;
	}

	bool UnixOptionsProcessor::isOptionSet(const Option_Info &option)
	{
		list<Option_Info>::iterator iter = find(m_user_options.begin(), m_user_options.end(), option);

		if (iter != m_user_options.end())
		{
			if (!(*iter).has_param && ((*iter).Value == ""))
				return true;
			string val = (*iter).Value;
			boost::to_lower(val);
			if (val == "yes" || val == "on" || val == "true" || val == "1")
				return true;
		}

		return false;
	}

	bool UnixOptionsProcessor::isOptionSet(const string &section, const string &name)
	{
		for (list<Option_Info>::iterator iter = m_user_options.begin(); iter != m_user_options.end(); iter++)
		{
			if (((*iter).Section == section) && ((*iter).Name == name))
				return isOptionSet(*iter);
		}
		return false;
	}

	void UnixOptionsProcessor::ProcessOptions(int *argc, char **argv[])
	{
		// add custom configuration options found in povray.conf files
		for (list<Conf_Option>::iterator iter_c = m_custom_conf_options.begin(); iter_c != m_custom_conf_options.end(); iter_c++)
		{
			Option_Info new_opt((*iter_c).Section, (*iter_c).Name, (*iter_c).Value, ((*iter_c).Value != ""), "", "");

			list<Option_Info>::iterator iter = find(m_user_options.begin(), m_user_options.end(), new_opt);
			if (iter == m_user_options.end())
				m_user_options.push_back(new_opt);
			else
				(*iter).Value = (*iter_c).Value;
		}

		m_user_options.sort();

#ifdef UNIX_DEBUG
		cerr << "OPTIONS (" << m_user_options.size() << ")" << endl;
#endif

		// check command line options and environment variables of all registered options
		// this overrides povray.conf settings
		for (list<Option_Info>::iterator iter = m_user_options.begin(); iter != m_user_options.end(); iter++)
		{
			// in ascending order of priority:
			// environment variables:
			if ((*iter).EnvVariable != "")
			{
				char *tmp = getenv((*iter).EnvVariable.c_str());
				if (tmp) // variable defined?
					(*iter).Value = tmp;
			}

			// command line options:
			// based on 3.6 XWIN_init_povray()
			if ((*iter).CmdOption != "")
			{
				int oargc = *argc;
				char **oargv = *argv;

				// TODO: free those when no more needed
				char **nargv = (char **)malloc((oargc + 1) * sizeof(char *));
				int nargc = oargc;
				for (int i = 0; i < nargc; i++)
				{
					nargv[i] = (char *)malloc(strlen(oargv[i]) + 1);
					strcpy(nargv[i], oargv[i]);
				}

				nargv[nargc] = NULL;

				vector<string> CmdVariations;
				boost::split(CmdVariations, (*iter).CmdOption, boost::is_any_of("|"));

				for (vector<string>::iterator iter_c = CmdVariations.begin(); iter_c != CmdVariations.end(); iter_c++)
				{
					for (int i = 1; i < nargc;)
					{
						if (string(nargv[i]) == (*iter_c))
						{
							if ((*iter).has_param)
							{
								int j = i + 1;
								if (j < nargc && nargv[j] != NULL)
								{
									(*iter).Value = nargv[j];
									remove_arg(&nargc, nargv, j);
								}
							}
							else
								(*iter).Value = "on";  // FIXME [nc]
							remove_arg(&nargc, nargv, i);
						}
						else
							i++;
						if (nargv[i] == NULL)
							break;
					}
				}

				*argv = nargv;
				*argc = nargc;
			}

#ifdef UNIX_DEBUG
			cerr << "  " << (*iter).Name << " = " << (*iter).Value << "(" << (*iter).CmdOption << ", " << (*iter).EnvVariable << ")" << endl;
#endif
		}
	}

	// based on 3.x RemoveArgs() by Andreas Dilger
	void UnixOptionsProcessor::remove_arg(int *argc, char *argv[], int index)
	{
		if (index >= *argc || index == 0)
			return;

		if (argv[index] != NULL)
			free(argv[index]);

		for (; index < *argc; index++)
			argv[index] = argv[index + 1];

		(*argc)--;
	}

	// based on 3.6 UNIX_getcwd()
	string UnixOptionsProcessor::unix_getcwd(void)
	{
#ifdef HAVE_GETCWD
		size_t len;

		len = 256;  // default buffer size
		char *tmp = (char *) calloc(len, 1);

		while(getcwd(tmp, len) == NULL)  // buffer is too small
		{
			free(tmp);
			len *= 2;  // double buffer size and try again
			tmp = (char *) calloc(len, 1);
		}
#else
		string tmp = getenv("PWD");  // must not be NULL; checked by configure
		if(tmp.length() == 0)        // run-time checks are safer anyway
		{
			// TODO: correct error handling
			char *errormsg =
				"Cannot determine the current working directory.\n"
				"Check that the PWD environment variable does exist and is valid.\n";
			if(no_error_call)
			{
				fprintf(stderr, "%s: %s\n", PACKAGE, errormsg);
				exit(EXIT_FAILURE);
			}
			else
				Error("%s", errormsg);
		}
#endif

		string s = tmp + string("/");  // add final slash

#ifdef HAVE_GETCWD
		free(tmp);
#endif

		return s;
	}

	// based on 3.6 unix_basename()
	string UnixOptionsProcessor::basename(const string &path)
	{
		if(path.length() < 2) // less than two characters
			return path;

		string s = path;
		if (s[s.length()-1] == '/')
			s.erase(s.length()-1);
		string::size_type pos = s.rfind('/');
		if (pos == 0)
			return s;
		s.erase(0, pos+1);
		return s;
	}

	// based on 3.6 unix_dirname()
	string UnixOptionsProcessor::dirname(const string &path)
	{
		if(path.length() < 2)  // less than two characters
			return string("");

		string s = path;
		if (s[s.length()-1] == '/')
			s.erase(s.length()-1);
		string::size_type pos = s.rfind('/');
		if (pos == 0)
			return string("");
		s.erase(pos);
		return s;
	}

	// based on 3.6 unix_readlink()
	string UnixOptionsProcessor::unix_readlink(const string &path)
	{
#ifdef HAVE_READLINK
		char   *tmp;
		size_t  len;
		int     status;

		len = 256;  // default buffer size
		tmp = (char *) calloc(len, 1);  // init with '\0'

		while(true)
		{
			status = readlink(path.c_str(), tmp, len-1);  // without terminating '\0'
			if(status < 0)  // an error occured, return empty string
			{
				free(tmp);
				return string("");
			}
			else if(status == len-1)  // the buffer is probably too small
			{
				free(tmp);
				len *= 2;  // double buffer size and try again
				tmp = (char *) calloc(len, 1);
			}
			else  // all right, let's go further
				break;
		}

		// do we have an absolute path ?
		if(tmp[0] != '/')  // no; concatenate symlink to the path dirname
		{
			string s = dirname(path) + "/" + tmp;
			free(tmp);
			return s;
		}
		else  // yes; just resize buffer
		{
			string s = tmp;
			free(tmp);
			return s;
		}
#else
		return string("");
#endif
	}

	// based on 3.6 UNIX_canonicalize_path()
	string UnixOptionsProcessor::CanonicalizePath(const string &path)
	{
		int   i;
		typedef struct { const char *match, *replace; } subst;
		const subst strings[] = {  // beware: order does matter
			{ "%INSTALLDIR%", POVLIBDIR },
			{ "%HOME%", m_home.c_str() },
			{ "//", "/" },
			{ "/./", "/" },
			{ NULL, NULL }  // sentinel
		};

		// nothing to canonicalize; return an empty string
		if(path.length() == 0)
			return string("");

#ifdef UNIX_DEBUG
		cerr << "  CANONICALIZE '" << path << "'" << endl;
#endif

		string s = path;

		// substitute all occurences of 'match' by 'replace'
		i = 0;
		while(strings[i].match)
		{
			boost::replace_all(s, strings[i].match, strings[i].replace);
			++i;
#ifdef UNIX_DEBUG
			cerr << "    su: " << s << endl;
#endif
		}

		// substitute the current working dir to the first "./"
		// or add the cwd to the first directory or "../"
		if (boost::starts_with(s, "./"))
		{
			s.erase(0, 2);
			s.insert(0, unix_getcwd());
		}
		else if(s[0] != '/' || boost::starts_with(s, "../"))
		{
			s.insert(0, unix_getcwd());
		}

		// iteratively translate all symlinks in the path (dirname and basename)
#ifdef HAVE_READLINK
		i = 0;
		if(s[i] == '/')
			++i;
		do
		{
			while(i < s.length() && s[i] != '/')
				++i;
			string tmp1 = s.substr(0, i);
			string tmp2 = s.substr(i, s.length()-i);
			string tmp3 = unix_readlink(tmp1);
			if (tmp3.length() > 0)
			{
#ifdef UNIX_DEBUG
				cerr << "    ln: " << tmp1 << " -> " << tmp3 << endl;
				cerr << "  " << tmp3 << tmp2 << endl;
#endif
				s = tmp3 + tmp2;
				i = 0;  // start again from beginning
				if(s[i] == '/')
					++i;
			}
			else
			{
#ifdef UNIX_DEBUG
				cerr << "    ln: " << tmp1 << endl;
#endif
				++i;
			}
		} while(i < s.length());
#endif  // HAVE_READLINK

		// substitute all occurences of "dir/.." by ""
		string tmp = s;
		string::size_type pos = s.find("/..");
		while (pos != string::npos)
		{
			string::size_type pos2 = s.rfind('/', pos-1);
			s.erase(pos2+1, pos-pos2+3);
#ifdef UNIX_DEBUG
			cerr << "    su: " << s << endl;
#endif
			pos = s.find("/..", pos+3);
		}

		// remove the last "/." if any
		if (boost::ends_with(s, "/."))
		{
			s.erase(s.length()-3);
#ifdef UNIX_DEBUG
			cerr << "    su: " << s << endl;
#endif
		}

		return s;
	}

	// based on 3.6 pre_process_conf_line()
	string UnixOptionsProcessor::pre_process_conf_line(const string &input)
	{
		string s = boost::trim_copy(input);

		// find comment mark
		string::size_type pos = s.find(";");
		if (pos != string::npos)
		{
			s.erase(pos);
			boost::trim(s);
		}

		return s;
	}

	// based on 3.6 add_permitted_path()
	void UnixOptionsProcessor::add_permitted_path(list<UnixPath> &paths, const string &input, const string &conf_name, unsigned long line_number)
	{
		char quote = 0;
		bool descend = false;
		bool writable = false;

		// read or read+write entry
		if (boost::starts_with(input, "read"))
		{
			int spos = 4;
			// we have a read+write path
			if (boost::starts_with(input, "read+write"))
			{
				writable = true;
				spos = 10;
			}
			// sub-directory search
			if (input[spos] == '*')
				descend = true;

			string::size_type pos = input.find('=');  // find equal sign
			if (pos != string::npos)  // equal sign found
			{
				int i = 0;
				string s = input.substr(pos+1, input.length()-pos-1);
				boost::trim(s);

				if(s[i] == '"' || s[i] == '\'')
				{
					quote = s[i];  // store and skip quote
					++i;
				}
				int begin = i;
				while(i < s.length())  // find next space caracter or closing quote
				{
					if(s[i] == quote  ||  (!quote  &&  isspace((int) s[i])))
						break;
					++i;
				}
				if(quote  &&  s[i] != quote)  // no closing quote
					fprintf(stderr,
						"%s: %s: %lu: ignored entry: missing closing %c quote\n",
						PACKAGE, conf_name.c_str(), line_number, quote
					);
				else if(i-begin)  // store given directory
				{
					string directory = s.substr(begin, i-begin) + "/";
					s = CanonicalizePath(directory);

#ifdef UNIX_DEBUG
					cerr << "PERMITTED ADD '" << s << "'" << endl;
#endif
					paths.push_back(UnixPath(s, descend, writable));
				}
				else  // nothing found after the equal sign
					fprintf(stderr,
						"%s: %s: %lu: ignored entry: missing directory\n",
						PACKAGE, conf_name.c_str(), line_number
					);
			}
			else  // equal sign not found
				fprintf(stderr,
					"%s: %s: %lu: ignored entry: missing equal sign\n",
					PACKAGE, conf_name.c_str(), line_number
				);
		}
		// unknown entry
		else if(input.length() > 0)
			fprintf(stderr,
				"%s: %s: %lu: unknown '%s' setting\n",
				PACKAGE, conf_name.c_str(), line_number, input.c_str()
			);
	}

	// based on 3.6 unix_parse_conf_file()
	void UnixOptionsProcessor::parse_conf_file(std::istream &Stream, const string &conf_name, bool user_mode)
	{
		list<UnixPath> paths;
		string line;
		string Custom_Section;
		unsigned long   line_number;
		bool            user_file_io_rejected;
		FileIO          file_io;
		bool            file_io_is_set;
		ShellOut        shellout;
		bool            shellout_is_set;
		short           i;

		typedef enum { NONE, FILE_IO, SHELLOUT, PERMITTED_PATHS, UNKNOWN } SectionVal;
		SectionVal section;
		typedef struct Section { const char *label; const SectionVal value; } Section;
		const Section sections[] =
		{
			{ ""                   , NONE            },  // init
			{ "[File I/O Security]", FILE_IO         },
			{ "[Shellout Security]", SHELLOUT        },
			{ "[Permitted Paths]"  , PERMITTED_PATHS },
			{ NULL                 , UNKNOWN         }   // sentinel
		};

		typedef struct IOSettings { const char *label; const FileIO value; } IOSettings;
		const IOSettings io_settings[] =
		{
			{ ""          , IO_UNSET      },
			{ "none"      , IO_NONE       },
			{ "read-only" , IO_READONLY   },
			{ "restricted", IO_RESTRICTED },
			{ NULL        , IO_UNKNOWN    }
		};

		typedef struct SHLSettings { const char *label; const ShellOut value; } SHLSettings;
		const SHLSettings shl_settings[] =
		{
			{ ""         , SHL_UNSET     },
			{ "allowed"  , SHL_ALLOWED   },
			{ "forbidden", SHL_FORBIDDEN },
			{ NULL       , SHL_UNKNOWN   }
		};

		// inits
		line_number = 0;
		user_file_io_rejected = false;
		file_io_is_set = shellout_is_set = false;
		section = NONE;
		file_io = IO_UNSET;
		shellout = SHL_UNSET;

#ifdef UNIX_DEBUG
		cerr << "PARSE CONF '" << conf_name << "'" << endl;
#endif

		// Since the file format allows to read permitted paths before
		// setting [File I/O Security], the paths must be stored in a local
		// list which will be used only when the user setting for file I/O
		// is accepted.
		if(!user_mode)
			paths = m_permitted_paths;

		while(std::getline(Stream, line))
		{
			// check for failbit and badbit and eofbit
			if(!Stream.good())
			{
				// Only in case of the badbit we can assume that some lower
				// level system API function has set errno and perror() can be
				// used safely.
				if(Stream.bad())
				{
					fprintf(stderr, "%s: error while reading/opening configuration file ", PACKAGE);
					perror(conf_name.c_str());
				}
				break;
			}
			// preprocess line
			line = pre_process_conf_line(line);
			++line_number;

			// skip empty line
			if(line.length() == 0)
				continue;

			// check section
			if(line[0] == '[')  // new section
			{
				Custom_Section = "";
				// search section
				for(i = 1; sections[i].label; ++i)
					if(boost::starts_with(line, sections[i].label))
						break;

				section = sections[i].value;

				// unknown section
				if(section == UNKNOWN)
				{
					Custom_Section = boost::to_lower_copy(line);
					Custom_Section.erase(0,1);
					Custom_Section.erase(Custom_Section.find(']'));
				}
			}  // check section

			// process custom sections
			else if (Custom_Section.length() != 0)
			{
				// split at '='
				string::size_type pos = line.find('=');
				string option_name;
				string option_val;
				if (pos == string::npos)
				{
					option_name = boost::to_lower_copy(line);
					option_val = "";
				}
				else
				{
					option_name = boost::to_lower_copy(line.substr(0, pos));
					option_val = line.substr(pos+1, line.length()-pos-1);
				}
#ifdef UNIX_DEBUG
				cerr << "CUSTOM OPTION:" << endl;
				cerr << " Section: " << Custom_Section << endl;
				cerr << " Name: " << option_name << endl;
				cerr << " Value: " << option_val << endl;
#endif
				m_custom_conf_options.push_back(Conf_Option(Custom_Section, option_name, option_val));
			}

			// file I/O security
			else if(section == FILE_IO)
			{
				// search setting
				for(i = 0; io_settings[i].label; ++i)
					if(line != string(io_settings[i].label))
						break;
				file_io = io_settings[i].value;

				// multiple settings were found
				if(file_io_is_set)
					fprintf(stderr,
						"%s: %s: %lu: multiple settings for %s\n",
						PACKAGE, conf_name.c_str(), line_number, sections[section].label
					);
				if(file_io != IO_UNSET)
					file_io_is_set = true;

				// unknown setting
				if(file_io == IO_UNKNOWN)
				{
					fprintf(stderr,
						"%s: %s: %lu: unknown '%s' setting for %s: ",
						PACKAGE, conf_name.c_str(), line_number, line.c_str(), sections[section].label
					);
					if(user_mode)
					{
						fprintf(stderr,
							"using system setting '%s'\n",
							io_settings[m_file_io].label
						);
						file_io = m_file_io;
						user_file_io_rejected = true;  // won't account for the user paths
					}
					else
					{
						fprintf(stderr, "I/O restrictions are disabled\n");
						file_io = IO_NONE;
					}
				}

				// user setting not allowed
				if(user_mode  &&  file_io < m_file_io)
				{
					fprintf(stderr,
						"%s: %s: %lu: "
						"the user setting '%s' for %s is less restrictive than "
						"the system setting '%s' in '%s': using system setting\n",
						PACKAGE, conf_name.c_str(), line_number,
						io_settings[  file_io].label, sections[section].label,
						io_settings[m_file_io].label, m_conf.c_str()
					);
					file_io = m_file_io;
					user_file_io_rejected = true;  // won't account for the user paths
				}

				m_file_io = file_io;
			}  // file I/O security

			// shellout security
			else if(section == SHELLOUT)
			{
				// search setting
				for(i = 0; shl_settings[i].label; ++i)
					if(line == string(shl_settings[i].label))
						break;
				shellout = shl_settings[i].value;

				// multiple settings were found
				if(shellout_is_set)
					fprintf(stderr,
						"%s: %s: %lu: multiple settings for %s\n",
						PACKAGE, conf_name.c_str(), line_number, sections[section].label
					);
				if(shellout != SHL_UNSET)
					shellout_is_set = true;

				// unknown setting
				if(shellout == SHL_UNKNOWN)
				{
					fprintf(stderr,
						"%s: %s: %lu: unknown '%s' setting for %s: ",
						PACKAGE, conf_name.c_str(), line_number, line.c_str(), sections[section].label
					);
					if(user_mode)
					{
						fprintf(stderr,
							"using system setting '%s'\n",
							shl_settings[m_shellout].label
						);
						shellout = m_shellout;
					}
					else
					{
						fprintf(stderr, "shellout security is disabled\n");
						shellout = SHL_ALLOWED;
					}
				}

				// user setting not allowed
				if(user_mode
					 && m_shellout == SHL_FORBIDDEN
					 && m_shellout != shellout)
				{
					fprintf(stderr,
						"%s: %s: %lu: "
						"the user setting '%s' for %s is less restrictive than "
						"the system '%s' setting in '%s': using system setting\n",
						PACKAGE, conf_name.c_str(), line_number,
						shl_settings[  shellout].label, sections[section].label,
						shl_settings[m_shellout].label, m_conf.c_str()
					);
					shellout = m_shellout;
				}

				m_shellout = shellout;
				gShelloutsPermittedFixThis = shellout == SHL_ALLOWED;
			}  // shellout security

			// permitted paths
			else if(section == PERMITTED_PATHS)
				add_permitted_path(paths, line, conf_name, line_number);

		}  // Read file loop end

		// Only in case of the badbit we can assume that some lower
		// level system API function has set errno. Then, perror() can be
		// used safely.
		if(Stream.bad())
		{
			fprintf(stderr, "%s: error while reading/opening config file ", PACKAGE);
			perror(conf_name.c_str());
		}

#ifdef UNIX_DEBUG
		fprintf(stderr,
			"I/O RESTRICTIONS\n"
			"  file_io  = %d\tconfig->file_io  = %d\n"
			"  shellout = %d\tconfig->shellout = %d\n",
			file_io, m_file_io,
			shellout, m_shellout
		);
#endif

		// assign user settings and paths
		if(user_mode)
		{
			if(user_file_io_rejected)
			{
				fprintf(stderr,
					"%s: %s: user permitted paths are ignored: using system paths\n",
					PACKAGE, conf_name.c_str()
				);
			}
			else
			{
				m_permitted_paths = paths;  // assign new paths
			}
		}
	}

	// based on 3.6 unix_process_povray_conf()
	void UnixOptionsProcessor::process_povray_conf(void)
	{
		m_Session->ClearPaths();
		m_Session->AddExcludedPath(string(POVCONFDIR));
		if (m_user_dir.length() != 0)
			m_Session->AddExcludedPath(m_user_dir);

		// read system configuration file
		if(m_sysconf.length() != 0)
		{
			std::ifstream stream ( m_sysconf.c_str() );
			if ( stream.is_open() )
			{
				parse_conf_file(stream, m_sysconf, false);
				m_conf = m_sysconf;
			}
			else
			{
				fprintf(stderr, "%s: cannot open the system configuration file ", PACKAGE);
				perror(m_sysconf.c_str());
			}
		}

		// read user configuration file
		if(m_userconf.length() != 0)
		{
			std::ifstream stream ( m_userconf.c_str() );
			if ( stream.is_open() )
			{
				parse_conf_file(stream, m_userconf, true);
				m_conf = m_userconf;
			}
			else
			{
				fprintf(stderr, "%s: cannot open the user configuration file ", PACKAGE);
				perror(m_userconf.c_str());
			}
		}

		// no file was read, disable I/O restrictions
		if(m_conf.length() == 0)
			fprintf(stderr, "%s: I/O restrictions are disabled\n", PACKAGE);

		// if no paths specified, at least include POVLIBDIR and POVCONFDIR
		else if(m_permitted_paths.empty())
		{
			m_permitted_paths.push_back(UnixPath(string(POVLIBDIR "/"), true, false));   // read*
			m_permitted_paths.push_back(UnixPath(string(POVCONFDIR "/"), false, false)); // read
		}

#ifdef UNIX_DEBUG
		cerr << "PERMITTED PATHS" << endl;
#endif

		for(list<UnixPath>::iterator iter = m_permitted_paths.begin(); iter != m_permitted_paths.end(); iter++)
		{
			m_Session->AddReadPath(iter->str, iter->descend);
#ifdef UNIX_DEBUG
			fprintf(stderr,
				"  %s%s = \"%s\"\n",
				iter->writable ? "WRITE" : "READ", iter->descend ? "*" : "", iter->str.c_str()
			);
#endif
		}
	}

	bool UnixOptionsProcessor::file_exist(const string &name)
	{
		FILE *file = fopen(name.c_str(), "r");

		if(file != NULL)
			fclose(file);
		else
			return false;

		return true;
	}

	// based on 3.6 unix_process_povray_ini()
	void UnixOptionsProcessor::Process_povray_ini(vfeRenderOptions &opts)
	{
		// try the file pointed to by POVINI
		string povini;
		char * povini_c = getenv("POVINI");
		if (povini_c)
		{
			povini = povini_c;
			if (file_exist(povini))
			{
				opts.AddINI(povini);
				return;
			}
			else
			{
				fprintf(stderr, "%s: POVINI environment: cannot open ", PACKAGE);
				perror(povini.c_str());
			}
		}

		// try any INI file in the current directory
		povini = "./povray.ini";
		if (file_exist(povini))
		{
			opts.AddINI(povini);
			return;
		}

		// try the user or system INI file
		if ((m_home.length() != 0) && file_exist(m_userini))
		{
			opts.AddINI(m_userini);
			return;
		}
		if (file_exist(m_sysini))
		{
			opts.AddINI(m_sysini);
			return;
		}

		// try older user or system INI files
		if ((m_home.length() != 0) && file_exist(m_userini_old))
		{
			opts.AddINI(m_userini_old);
			return;
		}
		if (file_exist(m_sysini_old))
		{
			opts.AddINI(m_sysini_old);
			return;
		}

		// warn that no INI file was found and add minimal library_path setting
		fprintf(stderr, "%s: cannot open an INI file, adding default library path\n", PACKAGE);
		opts.AddLibraryPath(string(POVLIBDIR "/include"));
	}

#if 0
	// based on 3.6 unix_subdir()
	static bool UnixOptionsProcessor::file_in_permitted_paths (const string &Filename, bool write)
	{
		// NOTE: Filename must be already canonicalized

		// no filename nor paths
		if(Filename.length() == 0  ||  m_permitted_paths.empty())
			return false;

		// scan the list of paths
		for (list<UnixPath>::iterator iter = m_permitted_paths.begin(); iter != m_permitted_paths.end(); iter++)
		{
			if ((*iter).descend)  // allows sub-directories
			{
				if (!write || (*iter).writable)
				{
#ifdef UNIX_DEBUG
					fprintf(stderr,
						"  FILE '%s' <-> %s* '%s'\n",
						Filename.c_str(), path->writable ? "WRITE" : "READ", (*iter).str.c_str()
					);
#endif
					if (boost::starts_with(Filename, (*iter).str))  // match found
					{
#ifdef UNIX_DEBUG
						fprintf(stderr, "  OK\n");
#endif
						return true;
					}
				}
			}
			else  // check for exact match with path->str (without last slash)
			{
				string dirname = dirname(Filename);
				string pathname = (*iter).str;
				pathname.erase(pathname.length()-1);
				if (!write || (*iter).writable)
			 	{
#ifdef UNIX_DEBUG
					fprintf(stderr,
						"  DIRNAME '%s' <-> %s '%s'\n",
						dirname.c_str(), path->writable ? "WRITE" : "READ", pathname.c_str()
					);
#endif
					if(dirname == pathname)
					{
#ifdef UNIX_DEBUG
						fprintf(stderr, "  OK\n");
#endif
						return true;
					}
				}
			}
		}

#ifdef UNIX_DEBUG
		fprintf(stderr, "  BAD\n");
#endif

		return false;
	}
#endif

	bool UnixOptionsProcessor::isIORestrictionsEnabled(bool write)
	{
		if (IO_RESTRICTIONS_DISABLED)
			return false;
		if (!write &&  m_file_io < IO_RESTRICTED)
			return false;
		if(m_file_io < IO_READONLY)
			return false;
		return true;
	}

	void UnixOptionsProcessor::IORestrictionsError(const string &fnm, bool write, bool is_user_setting)
	{
		if (is_user_setting)
		{
			if (write)
				fprintf(stderr, "%s: writing to '%s' is not permitted; check the configuration in '%s'\n", PACKAGE, fnm.c_str(), m_conf.c_str());
			else
				fprintf(stderr, "%s: reading from '%s' is not permitted; check the configuration in '%s'\n", PACKAGE, fnm.c_str(), m_conf.c_str());
		}
		else
		{
			fprintf(stderr, "%s: writing to '%s' is not permitted\n", PACKAGE, fnm.c_str());
		}

	}

}