File: gle.cpp

package info (click to toggle)
gle-graphics 4.2.4c-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,156 kB
  • ctags: 11,510
  • sloc: cpp: 76,204; sh: 3,151; makefile: 590; perl: 486; lisp: 142; ansic: 8; xml: 7
file content (1305 lines) | stat: -rw-r--r-- 45,343 bytes parent folder | download | duplicates (2)
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
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
/************************************************************************
 *                                                                      *
 * GLE - Graphics Layout Engine <http://www.gle-graphics.org/>          *
 *                                                                      *
 * Modified BSD License                                                 *
 *                                                                      *
 * Copyright (C) 2009 GLE.                                              *
 *                                                                      *
 * Redistribution and use in source and binary forms, with or without   *
 * modification, are permitted provided that the following conditions   *
 * are met:                                                             *
 *                                                                      *
 *    1. Redistributions of source code must retain the above copyright *
 * notice, this list of conditions and the following disclaimer.        *
 *                                                                      *
 *    2. Redistributions in binary form must reproduce the above        *
 * copyright notice, this list of conditions and the following          *
 * disclaimer in the documentation and/or other materials provided with *
 * the distribution.                                                    *
 *                                                                      *
 *    3. The name of the author may not be used to endorse or promote   *
 * products derived from this software without specific prior written   *
 * permission.                                                          *
 *                                                                      *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR   *
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED       *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY       *
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL   *
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE    *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS        *
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER *
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR      *
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN  *
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        *
 *                                                                      *
 ************************************************************************/

#define GLE_MAIN_CPP

#include "all.h"
#include "tokens/Tokenizer.h"
#include "core.h"
#include "mem_limits.h"
#include "file_io.h"
#include "cutils.h"
#include "cmdline.h"
#include "texinterface.h"
#include "config.h"
#include "drawit.h"
#include "gle-interface/gle-interface.h"
#include "gle-poppler.h"

#ifdef __WIN32__
	#include <io.h>
#endif

#include <set>

/*------------ GLOBAL VARIABLES --------------*/
//
// -- these globals are needed. they are called in other source files using extern...
//
int MAX_VECTOR=MAXIMUM_PS_VECTOR;
int gle_debug;
bool control_d = true;
extern int trace_on;
bool BLACKANDWHITE = false;
bool IS_INSTALL = false;
bool GS_PREVIEW = false;

void do_find_deps(CmdLineObj& cmdline);
void gle_as_a_calculator(vector<string>* exprs);

extern string GLE_TOP_DIR;
extern string GLE_BIN_DIR;
string GLE_WORKING_DIR;

class GLEOptions {
public:
	bool ASK_DEBUG;
};

/* Better to put all global options in one class? */
GLEOptions g_Options;
CmdLineObj g_CmdLine;
ConfigCollection g_Config;

void load_one_file(const char* name, CmdLineObj& cmdline, size_t* exit_code);
void load_one_file_stdin(CmdLineObj& cmdline, size_t* exit_code);
void init_option_args(CmdLineObj& cmdline);
void do_gen_inittex(CmdLineObj& cmdline, GLEOptions& options) throw(ParserError);
void process_option_args(CmdLineObj& cmdline, GLEOptions& options);
void do_run_other_version(ConfigCollection& coll, int argc, char **argv);
void gle_do_socket(const string& commands);
void gle_cleanup();
void tex_term();
void do_show_info();
void get_out_name(const string& name, CmdLineObj& cmdline, GLEFileLocation* outname);

#include "glearray.h"

void gle_cat_csv(vector<string>* files) {
	for (unsigned int i = 0; i < files->size(); i++) {
		string fname(files->at(i));
		GLECSVData csvData;
		csvData.read(fname);
		GLECSVError* error = csvData.getError();
		if (error->errorCode != GLECSVErrorNone) {
			cout << "error: " << error->errorString << endl;
		} else {
			csvData.print(cout);
		}
	}
}

#if defined(HAVE_LIBGLE) && !defined(HAVE_LIBGLE_STATIC)
int GLEMain(int argc, char **argv) {
#else
int main(int argc, char **argv) {
#endif
	try {
      gle_glib_init(argc, argv);
		g_init();

		/* Init and process command line arguments */
		init_config(&g_Config);
		init_option_args(g_CmdLine);
		if (!do_load_config("gle", argv, g_CmdLine, g_Config)) {
			return -1;
		}
		do_run_other_version(g_Config, argc, argv);
		g_CmdLine.parse(argc, argv);

		/* Error in command line arguments? */
		if (g_CmdLine.hasError()) {
			return -1;
		}

		/* Has calculator option? */
		if (g_CmdLine.hasOption(GLE_OPT_CALC)) {
			if (g_CmdLine.getNbMainArgs() == 0) {
				gle_as_a_calculator(NULL);
			} else {
				gle_as_a_calculator(g_CmdLine.getMainArgs());
			}
			return 0;
		}

		/* Cat CSV file to stdout? */
		if (g_CmdLine.hasOption(GLE_OPT_CATCSV) && g_CmdLine.getNbMainArgs() != 0) {
			gle_cat_csv(g_CmdLine.getMainArgs());
			return 0;
		}

		/* Should regenerate inittex.ini? */
		do_gen_inittex(g_CmdLine, g_Options);

		/* Find dependencies */
		do_find_deps(g_CmdLine);

		/* Get working directory */
		GLEGetCrDir(&GLE_WORKING_DIR);

		/* This is used by the GUI */
		if (g_CmdLine.hasOption(GLE_OPT_INFO)) {
			do_show_info();
		}

		if (g_CmdLine.getNbMainArgs() == 0 || g_CmdLine.hasOption(GLE_OPT_HELP)) {
			string version;
			g_get_version(&version);
			cerr << "GLE version " << version << endl;
			cerr << "Usage: gle [options] filename.gle" << endl;
			cerr << "More information: gle " << g_CmdLine.getOptionPrefix() << "help" << endl;
			if (g_CmdLine.hasOption(GLE_OPT_HELP)) {
				g_CmdLine.showHelp(GLE_OPT_HELP);
				if (!g_CmdLine.getOption(GLE_OPT_HELP)->hasArgument()) {
					cerr << "Give more help about a given option: " << g_CmdLine.getOptionPrefix() << "help option" << endl;
				}
			}
			return 0;
		}

		/* Process optional arguments */
		process_option_args(g_CmdLine, g_Options);
	} catch (ParserError err) {
		ostringstream err_str;
		err_str << "Error: ";
		err.write(err_str);
		g_message(err_str.str());
		return -1;
	}

	/* iterate over "file" arguments */
	size_t exit_code = 0;
	if (g_CmdLine.hasStdin()) {
		// read file from standard input (in that case there are no other main arguments)
		load_one_file_stdin(g_CmdLine, &exit_code);
	}
	for (int i = 0; i < g_CmdLine.getNbMainArgs(); i++) {
	  // the standard CMD shell didn't expand the wildcards
#ifdef __WIN32__
		//
		// -- globbing and wildcards
		//
		_finddata_t fileinfo;
		string file_dir, file_pattern;
		SplitFileName(g_CmdLine.getMainArg(i), file_dir, file_pattern);
		intptr_t File = _findfirst((char*)g_CmdLine.getMainArg(i).c_str(), &fileinfo);
		if (File != -1){
			do {
				string combined_name = file_dir + fileinfo.name;
				load_one_file(combined_name.c_str(), g_CmdLine, &exit_code);
			} while(!_findnext(File,&fileinfo));
			_findclose(File);
		} else {
			exit_code = 1;
			cout << "Can't open file: " << g_CmdLine.getMainArg(i) << endl;
		}
#else
#if defined(__OS2__) && defined(__EMX__)
		char **list;
		list = _fnexplode((char*)g_CmdLine.getMainArg(i).c_str());
		if ( list == NULL ) {
			load_one_file( g_CmdLine.getMainArg(i).c_str(), g_CmdLine, &exit_code);
		} else {
			for ( int i = 0; list[i] != NULL; i++ ) {
				load_one_file(list[i], g_CmdLine, &exit_code);
			}
		}
		_fnexplodefree(list);
#else
		// on Unix the shell expands the wildcards for us
		load_one_file(g_CmdLine.getMainArg(i).c_str(), g_CmdLine, &exit_code);
#endif
#endif
	}
	// will exit with the number of files it could NOT open
	gle_cleanup();
	if (exit_code != 0 || g_has_console_output()) {
		do_wait_for_enter();
	}
	return exit_code;
}

void graph_init();

void gle_cleanup() {
	tex_term();
	graph_init();
	delete GLEGetColorList();
	delete GLEGetInterfacePointer();
}

void do_run_other_version(ConfigCollection& coll, int argc, char **argv) {
	// Scan command line for -v option
	// Note: can not use cmdline here because older versions might have different options
	// which would result in an error message
	string version = "";
	for (int i = 1; i < argc-1; i++) {
		if (cmdline_is_option(argv[i], "v") ||
		    cmdline_is_option(argv[i], "version")) {
			version = argv[i+1];
			str_remove_quote(version);
		}
	}
	if (version != "") {
		ConfigSection* gle = coll.getSection(GLE_CONFIG_GLE);
		CmdLineArgSPairList* installs = (CmdLineArgSPairList*)gle->getOption(GLE_CONFIG_GLE_INSTALL)->getArg(0);
		const string* path = installs->lookup(version);
		if (path != NULL) {
			GLESetGLETop(*path);
			ostringstream torun;
			torun << "\"" << (*path) << "\"";
			for (int i = 1; i < argc; i++) {
				string arg = argv[i];
				str_remove_quote(arg);
				if (cmdline_is_option(arg.c_str(), "v")) i++;
				else torun << " \"" << arg << "\"";
			}
			int res = GLESystem(torun.str());
			if (res != GLE_SYSTEM_OK) {
				cerr << "Error while running: " << (*path) << endl;
			}
		} else {
			cerr << "Don't know path for version: '" << version << "'" << endl;
		}
		exit(0);
	}
}

void do_show_info() {
	string version, bdate;
	g_get_version(&version);
	g_get_build_date(&bdate);
	cout << "GLE version:             " << version << endl;
	if (!bdate.empty()) {
		cout << "Build date:              " << bdate << endl;
	}
	cout << "GLE_TOP:                 " << GLE_TOP_DIR << endl;
	cout << "GLE_BIN:                 " << GLE_BIN_DIR << endl;
	/* Location of GhostScript */
	string gs_dir;
	ConfigSection* tools = g_Config.getSection(GLE_CONFIG_TOOLS);
	const string& gs_cmd = ((CmdLineArgString*)tools->getOptionValue(GLE_TOOL_GHOSTSCRIPT_CMD))->getValue();
	if (gs_cmd != "") {
		cout << "GhostScript:             " << gs_cmd << endl;
	}
	CmdLineArgString* gslib = (CmdLineArgString*)tools->getOptionValue(GLE_TOOL_GHOSTSCRIPT_LIB);
	if (!gslib->isDefault()) {
		cout << "GS library:              " << gslib->getValue() << endl;
	}
	/* Bitmap import */
	cout << "Bitmap import:           " << g_bitmap_supported_types() << endl;
	/* Cairo support */
#ifdef HAVE_CAIRO
	cout << "Cairo rendering support: Yes" << endl;
#else
	cout << "Cairo rendering support: No" << endl;
#endif
#ifdef HAVE_POPPLER
	cout << "Poppler PDF support:     Yes" << endl;
#else
	cout << "Poppler PDF support:     No" << endl;
#endif
	do_wait_for_enter_exit(0);
}

void do_gen_inittex(CmdLineObj& cmdline, GLEOptions& options) throw(ParserError) {
	// Generate inittex.ini from init.tex
	if (cmdline.hasOption(GLE_OPT_MKINITTEX)) {
		IS_INSTALL = 1;
		string inittex = GLE_TOP_DIR + DIR_SEP + "init.tex";
		GLERC<GLEScript> script = new GLEScript();
		script->getLocation()->fromFileNameCrDir(inittex);
		script->getSource()->load();
		string inittex_ini = GLE_TOP_DIR + DIR_SEP + "inittex.ini";
		TryDeleteFile(inittex_ini);
		g_select_device(GLE_DEVICE_DUMMY);
		GLEFileLocation output;
		output.createIllegal();
		DrawIt(script.get(), &output, &cmdline);
		exit(0);
	}
}

void process_option_args(CmdLineObj& cmdline, GLEOptions& options) {
	if (cmdline.hasOption(GLE_OPT_COMPAT)) {
		const string& compat = cmdline.getStringValue(GLE_OPT_COMPAT);
		g_set_compatibility(compat);
	} else {
		g_set_compatibility(GLE_COMPAT_MOST_RECENT);
	}
	trace_on = cmdline.hasOption(GLE_OPT_TRACE);
	options.ASK_DEBUG = cmdline.hasOption(GLE_OPT_DEBUG);
	control_d = !cmdline.hasOption(GLE_OPT_NO_CTRL_D);
	if (cmdline.hasOption(GLE_OPT_NO_MAXPATH)) {
		/* Huh - what does this do? */
		MAX_VECTOR = 10*MAXIMUM_PS_VECTOR;
	}
	BLACKANDWHITE = cmdline.hasOption(GLE_OPT_NO_COLOR);
	if (cmdline.hasOption(GLE_OPT_BBTWEAK)) g_psbbtweak();
	GS_PREVIEW = cmdline.hasOption(GLE_OPT_GSPREVIEW);
	// .ps output implies full page
	CmdLineArgSet* device = (CmdLineArgSet*)cmdline.getOption(GLE_OPT_DEVICE)->getArg(0);
	if (device->hasValue(GLE_DEVICE_PS)) {
		cmdline.setHasOption(GLE_OPT_FULL_PAGE, true);
	}
	if (cmdline.hasOption(GLE_OPT_LANDSCAPE)) {
		cmdline.setHasOption(GLE_OPT_FULL_PAGE, true);
	}
	// remove stdin marker "-" from main arguments
	cmdline.checkForStdin();
	// Auto detect extra arguments (after .GLE / .TEX files)
	// Note that extra arguments can also be supplied by means of -args
	if (cmdline.getMainArgSepPos() == -1) {
		int nargs = cmdline.getNbMainArgs();
		for (int i = 0; i < nargs; i++) {
			const string& arg = cmdline.getMainArg(i);
			if (!str_i_ends_with(arg, ".GLE")) {
				if (i != 0) cmdline.setMainArgSepPos(i);
				break;
			}
		}
	}
	// Distable TeX interface
	if (cmdline.hasOption(GLE_OPT_SAFEMODE)) {
		TeXInterface* interface = TeXInterface::getInstance();
		interface->setEnabled(false);
	}
	// Process further args into global config object
	GLEGlobalConfig* conf = GLEGetInterfacePointer()->getConfig();
	conf->initCmdLine();
}

void init_option_args(CmdLineObj& cmdline) {
	CmdLineOption* option;
	CmdLineArgString* strarg;
	CmdLineArgInt* intarg;
	CmdLineArgSet* setarg;
	cmdline.setMainArgType("file name");
	option = new CmdLineOption("help", "h", "?");
	option->setHelp("Shows help about command line options");
	strarg = new CmdLineArgString("option");
	strarg->setHelp("show specific help about 'option'");
	strarg->setCardLimits(0, 1);
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_HELP);
	option = new CmdLineOption("device", "d");
	option->setHelp("Selects output device(s)");
	option->setMinNbArgs(1);
	setarg = new CmdLineArgSet("device-names");
	setarg->setHelp("set output device(s)");
	setarg->setMinCard(1);
	/* Order of values is important! (must match GLE_DEVICE_ constants in core.h) */
	setarg->addPossibleValue("eps");
	setarg->addPossibleValue("ps");
	setarg->addPossibleValue("pdf");
	setarg->addPossibleValue("svg");
	setarg->addPossibleValue("jpg");
	setarg->addPossibleValue("png");
	setarg->addPossibleValue("x11");
	setarg->addPossibleValue("emf");
#ifndef HAVE_X11
	setarg->setUnsupportedValue(GLE_DEVICE_X11);
#endif
#ifndef HAVE_CAIRO
	setarg->setUnsupportedValue(GLE_DEVICE_SVG);
	setarg->setUnsupportedValue(GLE_DEVICE_EMF);
#endif
#ifndef __WIN32__
	setarg->setUnsupportedValue(GLE_DEVICE_EMF);
#endif
	setarg->addDefaultValue(GLE_DEVICE_EPS);
	option->addArg(setarg);
	cmdline.addOption(option, GLE_OPT_DEVICE);
#ifdef HAVE_CAIRO
	option = new CmdLineOption("cairo");
	option->setHelp("Use cairo output device");
	cmdline.addOption(option, GLE_OPT_CAIRO);
#endif
	option = new CmdLineOption("fullpage");
	option->setHelp("Selects full page output");
	cmdline.addOption(option, GLE_OPT_FULL_PAGE);
	option = new CmdLineOption("landscape");
	option->setHelp("Selects full page landscape output");
	cmdline.addOption(option, GLE_OPT_LANDSCAPE);
	option = new CmdLineOption("nocolor", "bw");
	option->setHelp("Forces grayscale output");
	cmdline.addOption(option, GLE_OPT_NO_COLOR);
	option = new CmdLineOption("transparent", "tr");
	option->setHelp("Creates transparent output (with -d png)");
	cmdline.addOption(option, GLE_OPT_TRANSPARENT);
	option = new CmdLineOption("noctrl-d");
	option->setHelp("Excludes CTRL-D from the PostScript output");
	cmdline.addOption(option, GLE_OPT_NO_CTRL_D);
	option = new CmdLineOption("resolution", "r", "dpi");
	option->setHelp("Sets the resolution for bitmap and PDF output");
	intarg = new CmdLineArgInt("dpi");
	intarg->setHelp("set the resolution (measured in dots per inch)");
	intarg->setCardLimits(0, 1);
	intarg->setDefault(72);
	option->addArg(intarg);
	cmdline.addOption(option, GLE_OPT_DPI);
	option = new CmdLineOption("tex");
	option->setHelp("Indicates that the script includes LaTeX expressions");
	cmdline.addOption(option, GLE_OPT_TEX);
	option = new CmdLineOption("nopdftex");
	option->setHelp("Disable PdfLaTeX for .pdf creation");
	option->setExpert(true);
	cmdline.addOption(option, GLE_OPT_NO_PDFTEX);
	option = new CmdLineOption("inc");
	option->setHelp("Creates an .inc file with LaTeX code");
	cmdline.addOption(option, GLE_OPT_CREATE_INC);
	option = new CmdLineOption("texincprefix");
	option->setHelp("Adds the given subdirectory to the path in the .inc file");
	strarg = new CmdLineArgString("path");
	strarg->setHelp("adds 'path' to path in .inc file");
	strarg->setCardLimits(1, 1);
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_TEXINCPREF);
	option = new CmdLineOption("finddeps");
	option->setHelp("Automatically finds dependencies");
	strarg = new CmdLineArgString("path");
	strarg->setHelp("find dependencies in 'path'");
	strarg->setCardLimits(0, 1);
	// Default value should also be set if no value is given?
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_FINDDEPS);
	option = new CmdLineOption("preview", "p");
	option->setHelp("Previews the output with QGLE");
	cmdline.addOption(option, GLE_OPT_PREVIEW);
#ifdef ENABLE_GS_PREVIEW
	option = new CmdLineOption("gs");
	option->setHelp("Previews the output with GhostScript");
	cmdline.addOption(option, GLE_OPT_GSPREVIEW);
#endif
	option = new CmdLineOption("calc", "c");
	option->setHelp("Runs GLE in \"calculator\" mode");
	cmdline.addOption(option, GLE_OPT_CALC);
	option = new CmdLineOption("catcsv", "csv");
	option->setHelp("Pretty print a CSV file to standard output");
	cmdline.addOption(option, GLE_OPT_CATCSV);
	option = new CmdLineOption("output", "o");
	option->setHelp("Specifies the name of the output file");
	strarg = new CmdLineArgString("name");
	strarg->setHelp("writes output to file 'name'");
	strarg->setCardLimits(1, 1);
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_OUTPUT);
	option = new CmdLineOption("nosave");
	option->setHelp("Don't write output file to disk (dry-run)");
	cmdline.addOption(option, GLE_OPT_NOSAVE);
#if defined(__WIN32__) and defined(HAVE_CAIRO)
	option = new CmdLineOption("copy");
	option->setHelp("Copy resulting figure to clipboard (only EMF)");
	cmdline.addOption(option, GLE_OPT_COPY);
#endif
	option = new CmdLineOption("compatibility", "cm");
	option->setHelp("Selects a GLE compatibility mode");
	strarg = new CmdLineArgString("version");
	strarg->setHelp("specifies GLE version to emulate (can be any existing GLE version)");
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_COMPAT);
	option = new CmdLineOption("version", "v");
	option->setHelp("Selects a GLE version to run");
	setarg = new CmdLineArgSet("version");
	setarg->setHelp("run GLE version 'version'");
	setarg->setMinCard(1);
	setarg->setMaxCard(1);
	option->addArg(setarg);
	cmdline.addOption(option, GLE_OPT_VERSION);
	option = new CmdLineOption("noligatures");
	option->setHelp("Disable the use of ligatures for 'fl' and 'fi'");
	cmdline.addOption(option, GLE_OPT_NO_LIGATURES);
	option = new CmdLineOption("gsoptions");
	option->setHelp("Specify additional options for GhostScript");
	strarg = new CmdLineArgString("value");
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_GSOPTIONS);
	option = new CmdLineOption("safemode");
	option->setHelp("Disables reading/writing to the file system");
	cmdline.addOption(option, GLE_OPT_SAFEMODE);
	option = new CmdLineOption("allowread");
	option->setHelp("Allows reading from the given path");
	strarg = new CmdLineArgString("path");
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_ALLOWREAD);
	option = new CmdLineOption("allowwrite");
	option->setHelp("Allows writing to the given path");
	strarg = new CmdLineArgString("path");
	option->addArg(strarg);
	cmdline.addOption(option, GLE_OPT_ALLOWWRITE);
	option = new CmdLineOption("keep");
	option->setHelp("Don't delete temporary files");
	cmdline.addOption(option, GLE_OPT_KEEP);
	option = new CmdLineOption("trace");
	option->setHelp("Trace GLE");
	option->setExpert(true);
	cmdline.addOption(option, GLE_OPT_TRACE);
	option = new CmdLineOption("debug");
	option->setHelp("Debug GLE");
	option->setExpert(true);
	cmdline.addOption(option, GLE_OPT_DEBUG);
	option = new CmdLineOption("nomaxpath");
	option->setHelp("Disables the upper-bound on the drawing path complexity");
	cmdline.addOption(option, GLE_OPT_NO_MAXPATH);
	option = new CmdLineOption("mkinittex");
	option->setHelp("Creates \"inittex.ini\" from \"init.tex\"");
	cmdline.addOption(option, GLE_OPT_MKINITTEX);
	option = new CmdLineOption("info");
	option->setHelp("Outputs software version, build date, GLE_TOP, GLE_BIN, etc.");
	cmdline.addOption(option, GLE_OPT_INFO);
	option = new CmdLineOption("pause");
	option->setHelp("Pause if output has been generated");
	option->setExpert(true);
	cmdline.addOption(option, GLE_OPT_PAUSE);
	option = new CmdLineOption("verbosity", "vb");
	option->setHelp("Sets the verbosity level of GLE console output");
	intarg = new CmdLineArgInt("verbosity");
	intarg->setHelp("sets verbosity level to 'verbosity' (0..20)");
	intarg->setCardLimits(0, 1);
	intarg->setDefault(1);
	option->addArg(intarg);
	cmdline.addOption(option, GLE_OPT_VERBOSITY);
	cmdline.addMainArgSep("args");
	cmdline.addMainArgSep("a");
	cmdline.initOptions();
}

void gle_preview_file(const char* name, CmdLineObj& cmdline) {
	ostringstream commands;
	commands << "glefile: \"" << name << "\"" << endl;
	if (cmdline.hasOption(GLE_OPT_DPI)) {
		int dpi = ((CmdLineArgInt*)cmdline.getOption(GLE_OPT_DPI)->getArg(0))->getValue();
		commands << "dpi: \"" << dpi << "\"" << endl;
	}
	commands << "*DONE*" << endl;
	// cout << "Sending: " << commands.str() << endl;
	int result = GLESendSocket(commands.str());
	if (result == -3) {
		cerr << "Note: GLE is trying to launch QGLE, the GLE preview application" << endl;
#ifdef __WIN32__
		string qgle = "\"" + GLE_BIN_DIR + DIR_SEP + "qgle.exe\"";
#else
		string qgle = "\"" + GLE_BIN_DIR + DIR_SEP + "qgle\"";
#endif
		int sysres = GLESystem(qgle, false, false, NULL, NULL);
		if (sysres == GLE_SYSTEM_OK) {
			bool done = false;
			while (!done) {
				GLESleep(1000);
				result = GLESendSocket(commands.str());
				if (result != -3) {
					done = true;
				}
			}
		} else {
			cerr << "Error: failed to start QGLE: '" << qgle << "'" << endl;
			result = 0;
		}
	}
	if (result != 0) {
		cerr << "Error: could not connect to GLE preview application, code = " << result << endl;
	}
	cerr << endl;
}

bool has_pdflatex(CmdLineObj* cmdline) {
	if (cmdline->hasOption(GLE_OPT_NO_PDFTEX)) {
		return false;
	}
	ConfigSection* tex = g_Config.getSection(GLE_CONFIG_TEX);
	CmdLineArgSet* texsys =	(CmdLineArgSet*)tex->getOptionValue(GLE_TEX_SYSTEM);
	if (texsys->hasValue(GLE_TEX_SYSTEM_VTEX)) {
		return false;
	} else {
		return true;
	}
}

bool has_eps_or_pdf_based_device(CmdLineArgSet* device, CmdLineObj& cmdline) {
	if (cmdline.hasOption(GLE_OPT_TEX)) return true;
	if (device->hasValue(GLE_DEVICE_EPS)) return true;
	if (device->hasValue(GLE_DEVICE_PDF)) return true;
	if (device->hasValue(GLE_DEVICE_JPEG)) return true;
	if (device->hasValue(GLE_DEVICE_PNG)) return true;
	return false;
}

bool requires_tex(CmdLineArgSet* device, CmdLineObj* cmdline) {
	if (!cmdline->hasOption(GLE_OPT_CREATE_INC)) {
		if (device->hasValue(GLE_DEVICE_EPS)) return true;
		if (device->hasValue(GLE_DEVICE_PDF)) return true;
	}
	if (device->hasValue(GLE_DEVICE_PS)) return true;
	if (device->hasValue(GLE_DEVICE_JPEG)) return true;
	if (device->hasValue(GLE_DEVICE_PNG)) return true;
	return false;
}

bool has_bitmap_or_pdf_device(CmdLineArgSet* device) {
	if (device->hasValue(GLE_DEVICE_JPEG)) return true;
	if (device->hasValue(GLE_DEVICE_PNG)) return true;
	if (device->hasValue(GLE_DEVICE_PDF)) return true;
	return false;
}

bool is_bitmap_device(int device) {
	if (device == GLE_DEVICE_JPEG) return true;
	if (device == GLE_DEVICE_PNG) return true;
	return false;
}

void force_device(int dev, CmdLineObj& cmdline) {
	CmdLineArgSet* device = (CmdLineArgSet*)cmdline.getOption(GLE_OPT_DEVICE)->getArg(0);
	device->removeValue(GLE_DEVICE_EPS);
	device->addValue(dev);
}

void get_out_name(GLEFileLocation* inname, CmdLineObj& cmdline, GLEFileLocation* outname) {
	if (cmdline.hasOption(GLE_OPT_OUTPUT)) {
		const string& o_file = cmdline.getOptionString(GLE_OPT_OUTPUT);
		if (str_i_equals(o_file, "STDOUT")) {
			outname->createStdout();
		} else {
			if (str_i_ends_with(o_file, ".ps"))  force_device(GLE_DEVICE_PS,   cmdline);
			if (str_i_ends_with(o_file, ".pdf")) force_device(GLE_DEVICE_PDF,  cmdline);
			if (str_i_ends_with(o_file, ".svg")) force_device(GLE_DEVICE_SVG,  cmdline);
			if (str_i_ends_with(o_file, ".jpg")) force_device(GLE_DEVICE_JPEG, cmdline);
			if (str_i_ends_with(o_file, ".png")) force_device(GLE_DEVICE_PNG,  cmdline);
			string main_name;
			GetMainName(o_file, main_name);
			outname->fromFileNameDir(main_name, GLE_WORKING_DIR);
		}
	} else {
		if (inname->isStdin()) {
			outname->createStdout();
		} else {
			string main_name;
			GetMainNameExt(inname->getFullPath(), ".gle", main_name);
			outname->fromAbsolutePath(main_name);
		}
	}
}

GLERC<GLEScript> load_gle_code_sub(const char* name, CmdLineObj& cmdline) throw(ParserError) {
	string in_name = name;
	GLERC<GLEScript> script = new GLEScript();
	GLEFileLocation* loc = script->getLocation();
	loc->fromFileNameDir(in_name, GLE_WORKING_DIR);
	script->getSource()->load();
	return script;
}

GLERC<GLEScript> load_gle_code_sub_stdin(CmdLineObj& cmdline) throw(ParserError) {
	GLERC<GLEScript> script = new GLEScript();
	GLEFileLocation* loc = script->getLocation();
	loc->createStdin();
	loc->setDirectory(GLE_WORKING_DIR);
	script->getSource()->load();
	return script;
}

void delete_temp_file(const string& file, const char* ext) {
	int verbosity = g_verbosity();
	bool keep = g_CmdLine.hasOption(GLE_OPT_KEEP);
	if ((verbosity >= 5 && keep) || verbosity > 10) {
		string fname = file + ext;
		ostringstream todel;
		if (keep) {
			todel << "keep: " << fname;
		} else {
			todel << "delete: " << fname;
		}
		g_message(todel.str());
	}
	if (!g_CmdLine.hasOption(GLE_OPT_KEEP)) {
		DeleteFileWithExt(file, ext);
	}
}

void writeRecordedOutputFile(const string& fname, int deviceCode, string* buffer) throw (ParserError) {
	string outf = fname;
	outf.append(g_device_to_ext(deviceCode));
	ofstream out(outf.c_str(), ios::out | ios::binary);
	if (!out.is_open()) {
		g_throw_parser_error("failed to create file '", outf.c_str(), "'");
	}
	out.write(buffer->data(), buffer->size());
	out.close();
}

void writeRecordedOutputFile(const string& fname, int deviceCode, GLEScript* script) throw (ParserError) {
	string* buffer = script->getRecordedBytesBuffer(deviceCode);
	writeRecordedOutputFile(fname, deviceCode, buffer);
}

class GLELoadOneFileManager {
protected:
	GLEScript* m_Script;
	CmdLineObj* m_CmdLine;
	GLEFileLocation* m_OutName;
	GLEDevice* m_Device;
	GLEFileLocation m_IncName;
	bool m_HasTeXFile;
	bool m_HasTempDotDir;
	bool m_HasTempFile;
	std::set<int> m_hasGenerated;
	std::set<int> m_hasFile;
	std::set<int> m_hasIncFile;
public:
	GLELoadOneFileManager(GLEScript* script, CmdLineObj* cmdline, GLEFileLocation* outname);
	~GLELoadOneFileManager();
	void update_bounding_box();
	void delete_previous_output(int deviceCode);
	void create_cairo_eps() throw(ParserError);
	bool process_one_file_tex() throw(ParserError);
	void do_output_type(const char* type);
	void cat_stdout(const char* ext);
	void cat_stdout_and_del(const char* ext);
	void create_latex_eps_ps_pdf() throw(ParserError);
	void convert_eps_to_pdf_no_latex() throw(ParserError);
	istream* get_eps_stream();
	bool hasGenerated(int deviceCode);
	bool hasFile(int deviceCode);
	bool hasIncFile(int deviceCode);
	void setHasGenerated(int deviceCode, bool value);
	void setHasFile(int deviceCode, bool value);
	void setHasIncFile(int deviceCode, bool value);
	void write_recorded_data(int deviceCode) throw(ParserError);
	void clean_tex_temp_files();
	void clean_inc_file(int deviceCode);
	void delete_original_eps_pdf();
	void delete_original_eps_pdf_impl(int device);
	bool requires_tex_eps(CmdLineArgSet* device, CmdLineObj* cmdline);
	bool requires_tex_pdf(CmdLineArgSet* device, CmdLineObj* cmdline);
	bool has_cairo_pdf_based_device(CmdLineArgSet* device, CmdLineObj* cmdline);
};

GLELoadOneFileManager::GLELoadOneFileManager(GLEScript* script, CmdLineObj* cmdline, GLEFileLocation* outname) {
	m_Script = script;
	m_CmdLine = cmdline;
	m_OutName = outname;
	m_Device = NULL;
	m_HasTeXFile = false;
	m_HasTempDotDir = false;
	m_HasTempFile = false;
}

GLELoadOneFileManager::~GLELoadOneFileManager() {
}



void GLELoadOneFileManager::update_bounding_box() {
	double size_x, size_y;
	g_get_usersize(&size_x, &size_y);
	m_Script->setSize(size_x, size_y);
	PSGLEDevice* psdev = (PSGLEDevice*)g_get_device_ptr();
	m_Script->setBoundingBox(psdev->getBoundingBox()->getX(), psdev->getBoundingBox()->getY());
	m_Script->setBoundingBoxOrigin(0.0, 0.0);
}

void GLELoadOneFileManager::delete_previous_output(int deviceCode) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	if (device->hasValue(deviceCode) && !m_OutName->isStdout()) {
		/* Delete output so that QGLE does not display previous rendering if there are errors */
		DeleteFileWithExt(m_OutName->getFullPath(), g_device_to_ext(deviceCode));
	}
}

void GLELoadOneFileManager::create_cairo_eps() throw(ParserError) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	if (!hasGenerated(GLE_DEVICE_EPS) && device->hasValue(GLE_DEVICE_EPS)) {
		setHasGenerated(GLE_DEVICE_EPS, true);
		m_Device = g_select_device(GLE_DEVICE_CAIRO_EPS);
		m_Device->setRecordingEnabled(true);
		if (g_verbosity() > 0) {
			cerr << endl;
		}
		DrawIt(m_Script, m_OutName, m_CmdLine);
		m_Device->getRecordedBytes(m_Script->getRecordedBytesBuffer(GLE_DEVICE_EPS));
	}
}

bool GLELoadOneFileManager::process_one_file_tex() throw(ParserError) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	delete_previous_output(GLE_DEVICE_EPS);
	delete_previous_output(GLE_DEVICE_PDF);
	if (m_CmdLine->hasOption(GLE_OPT_CAIRO)) {
		if (has_cairo_pdf_based_device(device, m_CmdLine)) {
			setHasGenerated(GLE_DEVICE_PDF, true);
			m_Device = g_select_device(GLE_DEVICE_CAIRO_PDF);
		} else {
			setHasGenerated(GLE_DEVICE_EPS, true);
			m_Device = g_select_device(GLE_DEVICE_CAIRO_EPS);
		}
	} else {
		setHasGenerated(GLE_DEVICE_EPS, true);
		m_Device = g_select_device(GLE_DEVICE_EPS);
	}
	if (m_CmdLine->hasOption(GLE_OPT_DPI)) {
		m_Device->setResolution(((CmdLineArgInt*)m_CmdLine->getOption(GLE_OPT_DPI)->getArg(0))->getValue());
	}
	m_Device->setRecordingEnabled(true);
	/* In some cases two passes are required to measure size of TeX objects */
	int done = 1;
	TeXInterface* interface = TeXInterface::getInstance();
	if (m_Script->getLocation()->isStdin()) {
		interface->initialize(m_OutName, m_OutName);
	} else {
		interface->initialize(m_Script->getLocation(), m_OutName);
	}
	int iter = 0;
	do {
		interface->reset();
		if (iter != 0 && g_verbosity() > 0) cerr << endl;
		DrawIt(m_Script, m_OutName, m_CmdLine);
		if (get_nb_errors() > 0) {
			return false;
		}
		if (iter == 0 && interface->hasObjects() && m_OutName->isStdout()) {
			/* this means we need temporary files */
			m_HasTempFile = true;
			m_OutName->setFullPath(GLETempName());
			if (m_Script->getLocation()->isStdin()) {
				m_HasTempDotDir = true;
				interface->updateNames(m_OutName, m_OutName);
			} else {
				interface->updateOutName(m_OutName);
			}
		}
		done = interface->tryCreateHash();
		if (done == 2) {
			inc_nb_errors();
			return false;
		}
		iter++;
	} while (done == 1);
	/* get recorded PostScript code */
	if (hasGenerated(GLE_DEVICE_PDF)) {
		m_Device->getRecordedBytes(m_Script->getRecordedBytesBuffer(GLE_DEVICE_PDF));
	} else {
		m_Device->getRecordedBytes(m_Script->getRecordedBytesBuffer(GLE_DEVICE_EPS));
	}
	update_bounding_box();
	interface->checkObjectDimensions();
	create_cairo_eps();
	if (m_CmdLine->hasOption(GLE_OPT_CREATE_INC)) {
		interface->createInc(m_CmdLine->getStringValue(GLE_OPT_TEXINCPREF));
	}
	if (interface->hasObjects() && requires_tex(device, m_CmdLine)) {
		bool use_geom = false;
		bool has_pdftex = has_pdflatex(m_CmdLine);
		bool create_inc = m_CmdLine->hasOption(GLE_OPT_CREATE_INC);
		if (device->hasValue(GLE_DEVICE_PS)) use_geom = true;
		if (has_pdftex && !create_inc) use_geom = true;
		interface->createTeX(use_geom);
		m_HasTeXFile = true;
	}
	if (!interface->isEnabled()) {
		/* No TeX objects in safe mode */
		return false;
	}
	if (m_CmdLine->hasOption(GLE_OPT_CREATE_INC)) {
		/* See bug #2165591 */
		return true;
	}
	return interface->hasObjects();
}

void GLELoadOneFileManager::do_output_type(const char* type) {
	if (g_verbosity() > 0) {
		cerr << "[" << type << "]";
		g_set_console_output(false);
	}
}

void GLELoadOneFileManager::cat_stdout(const char* ext) {
	string outf = m_OutName->getFullPath() + ext;
	ifstream in(outf.c_str(), ios::in | ios::binary);
	GLECopyStream(in, cout);
	in.close();
}

void GLELoadOneFileManager::cat_stdout_and_del(const char* ext) {
	cat_stdout(ext);
	delete_temp_file(m_OutName->getFullPath(), ext);
}

void GLELoadOneFileManager::convert_eps_to_pdf_no_latex() throw(ParserError) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	if (device->hasValue(GLE_DEVICE_PDF) && !hasGenerated(GLE_DEVICE_PDF)) {
		setHasFile(GLE_DEVICE_PDF, true);
		int dpi = m_CmdLine->getIntValue(GLE_OPT_DPI);
		create_pdf_file_ghostscript(m_OutName, dpi, m_Script);
		do_output_type(".pdf");
	}
}

bool GLELoadOneFileManager::requires_tex_eps(CmdLineArgSet* device, CmdLineObj* cmdline) {
	if (!cmdline->hasOption(GLE_OPT_CREATE_INC)) {
		if (device->hasValue(GLE_DEVICE_EPS)) return true;
		if (device->hasValue(GLE_DEVICE_PDF) && !has_pdflatex(cmdline)) return true;
	}
	if (!hasGenerated(GLE_DEVICE_PDF)) {
		if (device->hasValue(GLE_DEVICE_JPEG)) return true;
		if (device->hasValue(GLE_DEVICE_PNG)) return true;
	}
	return false;
}

bool GLELoadOneFileManager::has_cairo_pdf_based_device(CmdLineArgSet* device, CmdLineObj* cmdline) {
	if (m_CmdLine->hasOption(GLE_OPT_CAIRO)) {
		if (device->hasValue(GLE_DEVICE_PDF)) return true;
		if (device->hasValue(GLE_DEVICE_JPEG)) return true;
		if (device->hasValue(GLE_DEVICE_PNG)) return true;
	}
	return false;
}

bool GLELoadOneFileManager::requires_tex_pdf(CmdLineArgSet* device, CmdLineObj* cmdline) {
	if (!cmdline->hasOption(GLE_OPT_CREATE_INC)) {
		if (device->hasValue(GLE_DEVICE_PDF)) return true;
	}
	if (hasGenerated(GLE_DEVICE_PDF)) {
		if (device->hasValue(GLE_DEVICE_JPEG)) return true;
		if (device->hasValue(GLE_DEVICE_PNG)) return true;
	}
	return false;
}

void GLELoadOneFileManager::create_latex_eps_ps_pdf() throw(ParserError) {
	/* m_OutName has no path and no extension */
	m_IncName.fromAbsolutePath(m_OutName->getFullPath() + "_inc");
	/* includegraphics does not handle "." in filenames */
	FileNameDotToUnderscore(m_IncName.getFullPathNC());
	bool create_inc = m_CmdLine->hasOption(GLE_OPT_CREATE_INC);
	bool has_pdftex = has_pdflatex(m_CmdLine);
	int dpi = m_CmdLine->getIntValue(GLE_OPT_DPI);
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	/* Create "_inc.eps" */
	if (device->hasOnlyValue(GLE_DEVICE_PDF) && (has_pdftex || create_inc)) {
		/* "_inc.eps" not required in this case */
	} else if (hasGenerated(GLE_DEVICE_EPS)) {
		setHasIncFile(GLE_DEVICE_EPS, true);
		writeRecordedOutputFile(m_IncName.getFullPath(), GLE_DEVICE_EPS, m_Script);
	}
	/* Create "_inc.pdf" */
	if ((device->hasValue(GLE_DEVICE_PDF) || hasGenerated(GLE_DEVICE_PDF)) && (has_pdftex || create_inc)) {
		setHasIncFile(GLE_DEVICE_PDF, true);
		if (hasGenerated(GLE_DEVICE_PDF)) {
			writeRecordedOutputFile(m_IncName.getFullPath(), GLE_DEVICE_PDF, m_Script);
		} else {
			create_pdf_file_ghostscript(&m_IncName, dpi, m_Script);
			do_output_type(".pdf");
		}
	}
	/* Exit if no EPS/PS/PDF required */
	if (!requires_tex_eps(device, m_CmdLine) &&
		!requires_tex_pdf(device, m_CmdLine) &&
	    !device->hasValue(GLE_DEVICE_PS)) {
			return;
	}
	/* Create EPS/PS/PDF based on .TeX */
	string out_name_dir, out_name_np;
	SplitFileName(m_OutName->getFullPath(), out_name_dir, out_name_np);
	/* LaTeX does not like to run with absolute paths */
	GLEChDir(out_name_dir);
	if (requires_tex_eps(device, m_CmdLine)) {
		create_eps_file_latex_dvips(out_name_np, m_Script);
		writeRecordedOutputFile(m_OutName->getFullPath(), GLE_DEVICE_EPS, m_Script);
		setHasFile(GLE_DEVICE_EPS, true);
	}
	if ((device->hasValue(GLE_DEVICE_PDF) && !create_inc) || requires_tex_pdf(device, m_CmdLine)) {
		setHasFile(GLE_DEVICE_PDF, true);
		if (has_pdftex) {
			create_pdf_file_pdflatex(out_name_np, m_Script);
		} else {
			create_pdf_file_ghostscript(m_OutName, dpi, m_Script);
			do_output_type(".pdf");
		}
	}
	if (device->hasValue(GLE_DEVICE_PS)) {
		create_ps_file_latex_dvips(out_name_np);
		if (m_OutName->isStdout()) cat_stdout_and_del(".ps");
		do_output_type(".ps");
	}
	GLEChDir(m_Script->getLocation()->getDirectory());
}

istream* GLELoadOneFileManager::get_eps_stream() {
	return NULL;
}

bool GLELoadOneFileManager::hasGenerated(int deviceCode) {
	return m_hasGenerated.find(deviceCode) != m_hasGenerated.end();
}

bool GLELoadOneFileManager::hasFile(int deviceCode) {
	return m_hasFile.find(deviceCode) != m_hasFile.end();
}

bool GLELoadOneFileManager::hasIncFile(int deviceCode) {
	return m_hasIncFile.find(deviceCode) != m_hasIncFile.end();
}

void GLELoadOneFileManager::setHasGenerated(int deviceCode, bool value) {
	if (value) {
		m_hasGenerated.insert(deviceCode);
	} else {
		m_hasGenerated.erase(deviceCode);
	}
}

void GLELoadOneFileManager::setHasFile(int deviceCode, bool value) {
	if (value) {
		m_hasFile.insert(deviceCode);
	} else {
		m_hasFile.erase(deviceCode);
	}
}

void GLELoadOneFileManager::setHasIncFile(int deviceCode, bool value) {
	if (value) {
		m_hasIncFile.insert(deviceCode);
	} else {
		m_hasIncFile.erase(deviceCode);
	}
}

void GLELoadOneFileManager::write_recorded_data(int deviceCode) throw (ParserError) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	if (!device->hasValue(deviceCode)) {
		return;
	}
	if (m_CmdLine->hasOption(GLE_OPT_CREATE_INC) || m_CmdLine->hasOption(GLE_OPT_NOSAVE)) {
		return;
	}
	if (m_OutName->isStdout()) {
		/* Write to stdout */
		if (hasFile(deviceCode)) {
			cat_stdout(g_device_to_ext(deviceCode));
		} else {
			string* buffer = m_Script->getRecordedBytesBuffer(deviceCode);
			cout.write(buffer->data(), buffer->size());
		}
	} else {
		if (!hasFile(deviceCode)) {
			writeRecordedOutputFile(m_OutName->getFullPath(), deviceCode, m_Script);
		}
	}
}

void GLELoadOneFileManager::clean_inc_file(int deviceCode) {
	/* Remove _inc.* if not required anymore */
	bool create_inc = m_CmdLine->hasOption(GLE_OPT_CREATE_INC);
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	if (hasIncFile(deviceCode) && (!create_inc || !device->hasValue(deviceCode))) {
		delete_temp_file(m_IncName.getFullPath(), g_device_to_ext(deviceCode));
	}
}

void GLELoadOneFileManager::clean_tex_temp_files() {
	clean_inc_file(GLE_DEVICE_EPS);
	clean_inc_file(GLE_DEVICE_PDF);
	/* Remove .tex file */
	if (m_HasTeXFile) {
		delete_temp_file(m_OutName->getFullPath(), ".tex");
	}
	/* Remove files in ".gle" directory */
	if (m_HasTempDotDir) {
		TeXInterface* interface = TeXInterface::getInstance();
		interface->removeDotFiles();
	}
}

void GLELoadOneFileManager::delete_original_eps_pdf_impl(int deviceCode) {
	CmdLineArgSet* device = (CmdLineArgSet*)m_CmdLine->getOption(GLE_OPT_DEVICE)->getArg(0);
	bool should_del = hasFile(deviceCode);
	if (device->hasValue(deviceCode)) {
		/* don't delete .eps/.pdf if we wanted this as output */
		/* and we did not wanted this printed to stout */
		if (!m_OutName->isStdout() && !m_CmdLine->hasOption(GLE_OPT_CREATE_INC)) {
			should_del = false;
		}
	}
	if (should_del) {
		/* delete original .eps/.pdf file */
		delete_temp_file(m_OutName->getFullPath(), g_device_to_ext(deviceCode));
	}
}

void GLELoadOneFileManager::delete_original_eps_pdf() {
	delete_original_eps_pdf_impl(GLE_DEVICE_EPS);
	delete_original_eps_pdf_impl(GLE_DEVICE_PDF);
	/* has temporary output file */
	if (m_HasTempFile) {
		#ifdef __WIN32__
			delete_temp_file(m_OutName->getFullPath(), ".tmp");
		#else
			delete_temp_file(m_OutName->getFullPath(), "");
		#endif
	}
}

void complain_latex_not_supported(int device) {
	if (TeXInterface::getInstance()->hasObjects()) {
		g_throw_parser_error(">> LaTeX expressions not supported in '", g_device_to_ext(device), "' output");
	}
}

void load_one_file_sub(GLEScript* script, CmdLineObj& cmdline, size_t* exit_code) throw(ParserError) {
	GLEFileLocation out_name; /* out_name has no extension */
	GLEGetInterfacePointer()->getConfig()->setAllowConfigBlocks(false);
	GLEChDir(script->getLocation()->getDirectory());
	get_out_name(script->getLocation(), cmdline, &out_name);
	g_set_console_output(false);
	g_message_first_newline(true);
	GLEGetColorList()->reset();
	if (cmdline.hasOption(GLE_OPT_DEBUG)){
		printf("Debug options 16=do_pcode, 8=pass 4=polish, 2=eval ");
		printf("Debug "); scanf("%d",&gle_debug);
		printf("Trace "); scanf("%d",&trace_on);
	}
	GLELoadOneFileManager manager(script, &cmdline, &out_name);
	CmdLineArgSet* device = (CmdLineArgSet*)cmdline.getOption(GLE_OPT_DEVICE)->getArg(0);
	if (has_eps_or_pdf_based_device(device, cmdline)) {
		bool has_tex = manager.process_one_file_tex();
		/* Return if there were errors */
		if (get_nb_errors() > 0) {
			if (g_verbosity() > 0) cerr << endl;
			(*exit_code)++;
			return;
		}
		/* Get device information */
		int dpi = cmdline.getIntValue(GLE_OPT_DPI);
		/* File has TeX code -> move it and create again */
		if (has_tex) {
			manager.create_latex_eps_ps_pdf();
		} else {
			manager.convert_eps_to_pdf_no_latex();
		}
		/* Create bitmap outputs */
		int output_options = 0;
		if (cmdline.hasOption(GLE_OPT_NO_COLOR)) {
			output_options |= GLE_OUTPUT_OPTION_GRAYSCALE;
		}
		if (cmdline.hasOption(GLE_OPT_TRANSPARENT)) {
			output_options |= GLE_OUTPUT_OPTION_TRANSPARENT;
		}
		for (int i = 0; i < device->getNbValues(); i++) {
			if (is_bitmap_device(i) && device->hasValue(i)) {
				create_bitmap_file(&out_name, i, dpi, output_options, script);
				manager.do_output_type(g_device_to_ext(i));
			}
		}
		/* Output .eps to stdout? */
		manager.write_recorded_data(GLE_DEVICE_EPS);
		manager.write_recorded_data(GLE_DEVICE_PDF);
		manager.delete_original_eps_pdf();
		if (has_tex) manager.clean_tex_temp_files();
		if (g_verbosity() > 0) cerr << endl;
	}
	if (device->hasValue(GLE_DEVICE_PS) && !cmdline.hasOption(GLE_OPT_TEX)) {
		GLEDevice* psdev = g_select_device(GLE_DEVICE_PS);
		DrawIt(script, &out_name, &cmdline);
		if (TeXInterface::getInstance()->hasObjects()) {
			g_message(">> To include LaTeX expressions, use \"gle -tex -d ps file.gle\"");
		}
		if (psdev->isRecordingEnabled()) {
			std::string recorded;
			psdev->getRecordedBytes(&recorded);
			writeRecordedOutputFile(out_name.getFullPath(), GLE_DEVICE_PS, &recorded);
		}
		if (out_name.isStdout()) {
			manager.cat_stdout_and_del(".ps");
		}
		cerr << endl;
	}
	if (device->hasValue(GLE_DEVICE_SVG)) {
		g_select_device(GLE_DEVICE_CAIRO_SVG);
		DrawIt(script, &out_name, &cmdline);
		complain_latex_not_supported(GLE_DEVICE_SVG);
		if (out_name.isStdout()) {
			manager.cat_stdout_and_del(".svg");
		}
		cerr << endl;
	}
	if (device->hasValue(GLE_DEVICE_X11)) {
		g_select_device(GLE_DEVICE_X11);
		DrawIt(script, &out_name, &cmdline);
	}
#if defined(__WIN32__) && defined(HAVE_CAIRO)
	if (device->hasValue(GLE_DEVICE_EMF)) {
		GLECairoDeviceEMF* dev = (GLECairoDeviceEMF*)g_select_device(GLE_DEVICE_EMF);
		if (cmdline.hasOption(GLE_OPT_COPY)) {
			dev->setCopyClipboard(true);
		}
		if (cmdline.hasOption(GLE_OPT_DPI)) {
			int dpi = ((CmdLineArgInt*)cmdline.getOption(GLE_OPT_DPI)->getArg(0))->getValue();
			dev->setDPI(dpi);
		} else {
			dev->setDPI(2000);
		}
		DrawIt(script, &out_name, &cmdline);
		complain_latex_not_supported(GLE_DEVICE_EMF);
		cerr << endl;
	}
#endif
}

void load_one_file(const char* name, CmdLineObj& cmdline, size_t* exit_code) {
	if (cmdline.hasOption(GLE_OPT_PREVIEW)) {
		GLEFileLocation loc;
		loc.fromFileNameDir(name, GLE_WORKING_DIR);
		gle_preview_file(loc.getFullPath().c_str(), cmdline);
		return;
	}
	try {
		GLERC<GLEScript> script = load_gle_code_sub(name, cmdline);
		load_one_file_sub(script.get(), cmdline, exit_code);
	} catch (ParserError err) {
		string err_str;
		err.toString(err_str);
		str_uppercase_initial_capital(err_str);
		g_message(err_str);
		(*exit_code)++;
	}
}

void load_one_file_stdin(CmdLineObj& cmdline, size_t* exit_code) {
	try {
		GLERC<GLEScript> script = load_gle_code_sub_stdin(cmdline);
		load_one_file_sub(script.get(), cmdline, exit_code);
	} catch (ParserError err) {
		string err_str;
		err.toString(err_str);
		str_uppercase_initial_capital(err_str);
		g_message(err_str);
		(*exit_code)++;
	}
}