summaryrefslogtreecommitdiff
path: root/src/terminal-options.c
blob: 352466f1cc154f24414c140ee6d12fafb460fa31 (plain)
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
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
/*
 * Copyright © 2001, 2002 Havoc Pennington
 * Copyright © 2002 Red Hat, Inc.
 * Copyright © 2002 Sun Microsystems
 * Copyright © 2003 Mariano Suarez-Alvarez
 * Copyright © 2008 Christian Persch
 * Copyright (C) 2012-2021 MATE Developers
 *
 * Mate-terminal is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Mate-terminal 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/>.
 */

#include <config.h>

#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>

#include "terminal-options.h"
#include "terminal-screen.h"
#include "terminal-app.h"
#include "terminal-intl.h"
#include "terminal-util.h"

static GOptionContext *get_goption_context (TerminalOptions *options);

static InitialTab*
initial_tab_new (const char *profile,
                 gboolean    is_id)
{
	InitialTab *it;

	it = g_slice_new (InitialTab);

	it->profile = g_strdup (profile);
	it->profile_is_id = is_id;
	it->exec_argv = NULL;
	it->title = NULL;
	it->working_dir = NULL;
	it->zoom = 1.0;
	it->zoom_set = FALSE;
	it->active = FALSE;
	it->attach_window = FALSE;

	return it;
}

static void
initial_tab_free (InitialTab *it)
{
	g_free (it->profile);
	g_strfreev (it->exec_argv);
	g_free (it->title);
	g_free (it->working_dir);
	g_slice_free (InitialTab, it);
}

static InitialWindow*
initial_window_new (guint source_tag)
{
	InitialWindow *iw;

	iw = g_slice_new0 (InitialWindow);
	iw->source_tag = source_tag;

	return iw;
}

static void
initial_window_free (InitialWindow *iw)
{
	g_list_foreach (iw->tabs, (GFunc) initial_tab_free, NULL);
	g_list_free (iw->tabs);
	g_free (iw->geometry);
	g_free (iw->role);
	g_slice_free (InitialWindow, iw);
}

static void
apply_defaults (TerminalOptions *options,
                InitialWindow        *iw)
{
	if (options->default_role)
	{
		iw->role = options->default_role;
		options->default_role = NULL;
	}

	if (iw->geometry == NULL)
		iw->geometry = g_strdup (options->default_geometry);

	if (options->default_window_menubar_forced)
	{
		iw->force_menubar_state = TRUE;
		iw->menubar_state = options->default_window_menubar_state;

		options->default_window_menubar_forced = FALSE;
	}

	iw->start_fullscreen |= options->default_fullscreen;
	iw->start_maximized |= options->default_maximize;
}

static InitialWindow*
ensure_top_window (TerminalOptions *options)
{
	InitialWindow *iw;

	if (options->initial_windows == NULL)
	{
		iw = initial_window_new (0);
		iw->tabs = g_list_append (NULL, initial_tab_new (NULL, FALSE));
		apply_defaults (options, iw);

		options->initial_windows = g_list_append (options->initial_windows, iw);
	}
	else
	{
		iw = g_list_last (options->initial_windows)->data;
	}

	g_assert (iw->tabs);

	return iw;
}

static InitialTab*
ensure_top_tab (TerminalOptions *options)
{
	InitialWindow *iw;
	InitialTab *it;

	iw = ensure_top_window (options);

	g_assert (iw->tabs);

	it = g_list_last (iw->tabs)->data;

	return it;
}

static InitialWindow*
add_new_window (TerminalOptions *options,
                const char           *profile,
                gboolean              is_id)
{
	InitialWindow *iw;

	iw = initial_window_new (0);
	iw->tabs = g_list_prepend (NULL, initial_tab_new (profile, is_id));
	apply_defaults (options, iw);

	options->initial_windows = g_list_append (options->initial_windows, iw);

	return iw;
}

/* handle deprecated command line options */
static gboolean
unsupported_option_callback (const gchar *option_name,
                             const gchar *value,
                             gpointer     data,
                             GError     **error)
{
	g_printerr (_("Option \"%s\" is no longer supported in this version of mate-terminal;"
	              " you might want to create a profile with the desired setting, and use"
	              " the new '--profile' option\n"), option_name);
	return TRUE; /* we do not want to bail out here but continue */
}

static gboolean G_GNUC_NORETURN
option_version_cb (const gchar *option_name,
                   const gchar *value,
                   gpointer     data,
                   GError     **error)
{
	g_print ("%s %s\n", _("MATE Terminal"), VERSION);

	exit (EXIT_SUCCESS);
}

static gboolean
option_command_callback (const gchar *option_name,
                         const gchar *value,
                         gpointer     data,
                         GError     **error)
{
	TerminalOptions *options = data;
	GError *err = NULL;
	char  **exec_argv;

	if (!g_shell_parse_argv (value, NULL, &exec_argv, &err))
	{
		g_set_error(error,
		            G_OPTION_ERROR,
		            G_OPTION_ERROR_BAD_VALUE,
		            _("Argument to \"%s\" is not a valid command: %s"),
		            "--command/-e",
		            err->message);
		g_error_free (err);
		return FALSE;
	}

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);

		g_strfreev (it->exec_argv);
		it->exec_argv = exec_argv;
	}
	else
	{
		g_strfreev (options->exec_argv);
		options->exec_argv = exec_argv;
	}

	return TRUE;
}

static gboolean
option_profile_cb (const gchar *option_name,
                   const gchar *value,
                   gpointer     data,
                   GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);

		g_free (it->profile);
		it->profile = g_strdup (value);
		it->profile_is_id = FALSE;
	}
	else
	{
		g_free (options->default_profile);
		options->default_profile = g_strdup (value);
		options->default_profile_is_id = FALSE;
	}

	return TRUE;
}

static gboolean
option_profile_id_cb (const gchar *option_name,
                      const gchar *value,
                      gpointer     data,
                      GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);

		g_free (it->profile);
		it->profile = g_strdup (value);
		it->profile_is_id = TRUE;
	}
	else
	{
		g_free (options->default_profile);
		options->default_profile = g_strdup (value);
		options->default_profile_is_id = TRUE;
	}

	return TRUE;
}

static gboolean
option_window_callback (const gchar *option_name,
                        const gchar *value,
                        gpointer     data,
                        GError     **error)
{
	TerminalOptions *options = data;
	gboolean is_profile_id;

	is_profile_id = g_str_has_suffix (option_name, "-with-profile-internal-id");

	add_new_window (options, value, is_profile_id);

	return TRUE;
}

static gboolean
option_tab_callback (const gchar *option_name,
                     const gchar *value,
                     gpointer     data,
                     GError     **error)
{
	TerminalOptions *options = data;
	gboolean is_profile_id;
	InitialWindow *iw;
	InitialTab *it;

	is_profile_id = g_str_has_suffix (option_name, "-with-profile-internal-id");

	if (options->initial_windows)
	{
		iw = g_list_last (options->initial_windows)->data;
		iw->tabs = g_list_append (iw->tabs, initial_tab_new (value, is_profile_id));
	}
	else
	{
		iw = add_new_window (options, value, is_profile_id);
		it = g_list_last(iw->tabs)->data;
		it->attach_window = TRUE;
	}

	return TRUE;
}

static gboolean
option_role_callback (const gchar *option_name,
                      const gchar *value,
                      gpointer     data,
                      GError     **error)
{
	TerminalOptions *options = data;
	InitialWindow *iw;

	if (options->initial_windows)
	{
		iw = g_list_last (options->initial_windows)->data;
		iw->role = g_strdup (value);
	}
	else if (!options->default_role)
		options->default_role = g_strdup (value);
	else
	{
		g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
		             "%s", _("Two roles given for one window"));
		return FALSE;
	}

	return TRUE;
}

static gboolean
option_show_menubar_callback (const gchar *option_name,
                              const gchar *value,
                              gpointer     data,
                              GError     **error)
{
	TerminalOptions *options = data;
	InitialWindow *iw;

	if (options->initial_windows)
	{
		iw = g_list_last (options->initial_windows)->data;
		if (iw->force_menubar_state && iw->menubar_state == TRUE)
		{
			g_printerr (_("\"%s\" option given twice for the same window\n"),
			            "--show-menubar");

			return TRUE;
		}

		iw->force_menubar_state = TRUE;
		iw->menubar_state = TRUE;
	}
	else
	{
		options->default_window_menubar_forced = TRUE;
		options->default_window_menubar_state = TRUE;
	}

	return TRUE;
}

static gboolean
option_hide_menubar_callback (const gchar *option_name,
                              const gchar *value,
                              gpointer     data,
                              GError     **error)
{
	TerminalOptions *options = data;
	InitialWindow *iw;

	if (options->initial_windows)
	{
		iw = g_list_last (options->initial_windows)->data;

		if (iw->force_menubar_state && iw->menubar_state == FALSE)
		{
			g_printerr (_("\"%s\" option given twice for the same window\n"),
			            "--hide-menubar");
			return TRUE;
		}

		iw->force_menubar_state = TRUE;
		iw->menubar_state = FALSE;
	}
	else
	{
		options->default_window_menubar_forced = TRUE;
		options->default_window_menubar_state = FALSE;
	}

	return TRUE;
}

static gboolean
option_maximize_callback (const gchar *option_name,
                          const gchar *value,
                          gpointer     data,
                          GError     **error)
{
	TerminalOptions *options = data;
	InitialWindow *iw;

	if (options->initial_windows)
	{
		iw = g_list_last (options->initial_windows)->data;
		iw->start_maximized = TRUE;
	}
	else
		options->default_maximize = TRUE;

	return TRUE;
}

static gboolean
option_fullscreen_callback (const gchar *option_name,
                            const gchar *value,
                            gpointer     data,
                            GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialWindow *iw;

		iw = g_list_last (options->initial_windows)->data;
		iw->start_fullscreen = TRUE;
	}
	else
		options->default_fullscreen = TRUE;

	return TRUE;
}

static gboolean
option_geometry_callback (const gchar *option_name,
                          const gchar *value,
                          gpointer     data,
                          GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialWindow *iw;

		iw = g_list_last (options->initial_windows)->data;
		iw->geometry = g_strdup (value);
	}
	else
		options->default_geometry = g_strdup (value);

	return TRUE;
}

static gboolean
option_disable_factory_callback (const gchar *option_name,
                                 const gchar *value,
                                 gpointer     data,
                                 GError     **error)
{
	TerminalOptions *options = data;

	options->use_factory = FALSE;

	return TRUE;
}

static gboolean
option_load_save_config_cb (const gchar *option_name,
                            const gchar *value,
                            gpointer     data,
                            GError     **error)
{
	TerminalOptions *options = data;

	if (options->config_file)
	{
		g_set_error_literal (error, TERMINAL_OPTION_ERROR, TERMINAL_OPTION_ERROR_EXCLUSIVE_OPTIONS,
		                     "Options \"--load-config\" and \"--save-config\" are mutually exclusive");
		return FALSE;
	}

	options->config_file = terminal_util_resolve_relative_path (options->default_working_dir, value);
	options->load_config = strcmp (option_name, "--load-config") == 0;
	options->save_config = strcmp (option_name, "--save-config") == 0;

	return TRUE;
}

static gboolean
option_title_callback (const gchar *option_name,
                       const gchar *value,
                       gpointer     data,
                       GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);

		g_free (it->title);
		it->title = g_strdup (value);
	}
	else
	{
		g_free (options->default_title);
		options->default_title = g_strdup (value);
	}

	return TRUE;
}

static gboolean
option_working_directory_callback (const gchar *option_name,
                                   const gchar *value,
                                   gpointer     data,
                                   GError     **error)
{
	TerminalOptions *options = data;

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);

		g_free (it->working_dir);
		it->working_dir = g_strdup (value);
	}
	else
	{
		g_free (options->default_working_dir);
		options->default_working_dir = g_strdup (value);
	}

	return TRUE;
}

static gboolean
option_active_callback (const gchar *option_name,
                        const gchar *value,
                        gpointer     data,
                        GError     **error)
{
	TerminalOptions *options = data;
	InitialTab *it;

	it = ensure_top_tab (options);
	it->active = TRUE;

	return TRUE;
}

static gboolean
option_zoom_callback (const gchar *option_name,
                      const gchar *value,
                      gpointer     data,
                      GError     **error)
{
	TerminalOptions *options = data;
	double zoom;
	char *end;

	/* Try reading a locale-style double first, in case it was
	  * typed by a person, then fall back to ascii_strtod (we
	  * always save session in C locale format)
	  */
	end = NULL;
	errno = 0;
	zoom = g_strtod (value, &end);
	if (end == NULL || *end != '\0')
	{
		g_set_error (error,
		             G_OPTION_ERROR,
		             G_OPTION_ERROR_BAD_VALUE,
		             _("\"%s\" is not a valid zoom factor"),
		             value);
		return FALSE;
	}

	if (zoom < (TERMINAL_SCALE_MINIMUM + 1e-6))
	{
		g_printerr (_("Zoom factor \"%g\" is too small, using %g\n"),
		            zoom,
		            TERMINAL_SCALE_MINIMUM);
		zoom = TERMINAL_SCALE_MINIMUM;
	}

	if (zoom > (TERMINAL_SCALE_MAXIMUM - 1e-6))
	{
		g_printerr (_("Zoom factor \"%g\" is too large, using %g\n"),
		            zoom,
		            TERMINAL_SCALE_MAXIMUM);
		zoom = TERMINAL_SCALE_MAXIMUM;
	}

	if (options->initial_windows)
	{
		InitialTab *it = ensure_top_tab (options);
		it->zoom = zoom;
		it->zoom_set = TRUE;
	}
	else
		options->zoom = zoom;

	return TRUE;
}

/* Evaluation of the arguments given to the command line options */
static gboolean
digest_options_callback (GOptionContext *context,
                         GOptionGroup *group,
                         gpointer      data,
                         GError      **error)
{
	TerminalOptions *options = data;
	InitialTab    *it;

	if (options->execute)
	{
		if (options->exec_argv == NULL)
		{
			g_set_error (error,
			             G_OPTION_ERROR,
			             G_OPTION_ERROR_BAD_VALUE,
			             _("Option \"%s\" requires specifying the command to run"
			               " on the rest of the command line"),
			             "--execute/-x");
			return FALSE;
		}

		/* Apply -x/--execute command only to the first tab */
		it = ensure_top_tab (options);
		it->exec_argv = options->exec_argv;
		options->exec_argv = NULL;
	}

	return TRUE;
}

/**
 * terminal_options_parse:
 * @working_directory: the default working directory
 * @display_name: the default X display name
 * @startup_id: the startup notification ID
 * @env: the environment as variable=value pairs
 * @remote_arguments: whether the caller is the factory process or not
 * @ignore_unknown_options: whether to ignore unknown options when parsing
 *   the arguments
 * @argcp: (inout) address of the argument count. Changed if any arguments were handled
 * @argvp: (inout) address of the argument vector. Any parameters understood by
 *   the terminal #GOptionContext are removed
 * @error: a #GError to fill in
 * @...: a %NULL terminated list of extra #GOptionGroup<!-- -->s
 *
 * Parses the argument vector *@argvp.
 *
 * Returns: a new #TerminalOptions containing the windows and tabs to open,
 *   or %NULL on error.
 */
TerminalOptions *
terminal_options_parse (const char *working_directory,
                        const char *display_name,
                        const char *startup_id,
                        char **env,
                        gboolean remote_arguments,
                        gboolean ignore_unknown_options,
                        int *argcp,
                        char ***argvp,
                        GError **error,
                        ...)
{
	TerminalOptions *options;
	GOptionContext *context;
	GOptionGroup *extra_group;
	va_list va_args;
	gboolean retval;
	int i;
	char **argv = *argvp;

	options = g_slice_new0 (TerminalOptions);

	options->remote_arguments = remote_arguments;
	options->default_window_menubar_forced = FALSE;
	options->default_window_menubar_state = TRUE;
	options->default_fullscreen = FALSE;
	options->default_maximize = FALSE;
	options->execute = FALSE;
	options->use_factory = TRUE;
	options->initial_workspace = -1;

	options->env = g_strdupv (env);
	options->startup_id = g_strdup (startup_id && startup_id[0] ? startup_id : NULL);
	options->display_name = g_strdup (display_name);
	options->initial_windows = NULL;
	options->default_role = NULL;
	options->default_geometry = NULL;
	options->default_title = NULL;
	options->zoom = 1.0;

	options->default_working_dir = g_strdup (working_directory);

	/* The old -x/--execute option is broken, so we need to pre-scan for it. */
	/* We now also support passing the command after the -- switch. */
	options->exec_argv = NULL;
	for (i = 1 ; i < *argcp; ++i)
	{
		gboolean is_execute;
		gboolean is_dashdash;
		int j, last;

		is_execute = strcmp (argv[i], "-x") == 0 || strcmp (argv[i], "--execute") == 0;
		is_dashdash = strcmp (argv[i], "--") == 0;

		if (!is_execute && !is_dashdash)
			continue;

		options->execute = is_execute;

		/* Skip the switch */
		last = i;
		++i;
		if (i == *argcp)
			break; /* we'll complain about this later for -x/--execute; it's fine for -- */

		/* Collect the args, and remove them from argv */
		options->exec_argv = g_new0 (char*, *argcp - i + 1);
		for (j = 0; i < *argcp; ++i, ++j)
			options->exec_argv[j] = g_strdup (argv[i]);
		options->exec_argv[j] = NULL;

		*argcp = last;
		break;
	}

	context = get_goption_context (options);

	g_option_context_set_ignore_unknown_options (context, ignore_unknown_options);

	va_start (va_args, error);
	extra_group = va_arg (va_args, GOptionGroup*);
	while (extra_group != NULL)
	{
		g_option_context_add_group (context, extra_group);
		extra_group = va_arg (va_args, GOptionGroup*);
	}
	va_end (va_args);

	retval = g_option_context_parse (context, argcp, argvp, error);
	g_option_context_free (context);

	if (retval)
		return options;

	terminal_options_free (options);
	return NULL;
}

/**
 * terminal_options_merge_config:
 * @options:
 * @key_file: a #GKeyFile containing to merge the options from
 * @source_tag: a source_tag to use in new #InitialWindow<!-- -->s
 * @error: a #GError to fill in
 *
 * Merges the saved options from @key_file into @options.
 *
 * Returns: %TRUE if @key_file was a valid key file containing a stored
 *   terminal configuration, or %FALSE on error
 */
gboolean
terminal_options_merge_config (TerminalOptions *options,
                               GKeyFile *key_file,
                               guint source_tag,
                               GError **error)
{
	int version, compat_version;
	char **groups;
	guint i;
	gboolean have_error = FALSE;
	GList *initial_windows = NULL;

	if (!g_key_file_has_group (key_file, TERMINAL_CONFIG_GROUP))
	{
		g_set_error_literal (error, TERMINAL_OPTION_ERROR,
		                     TERMINAL_OPTION_ERROR_INVALID_CONFIG_FILE,
		                     _("Not a valid terminal config file."));
		return FALSE;
	}

	version = g_key_file_get_integer (key_file, TERMINAL_CONFIG_GROUP, TERMINAL_CONFIG_PROP_VERSION, NULL);
	compat_version = g_key_file_get_integer (key_file, TERMINAL_CONFIG_GROUP, TERMINAL_CONFIG_PROP_COMPAT_VERSION, NULL);

	if (version <= 0 ||
	        compat_version <= 0 ||
	        compat_version > TERMINAL_CONFIG_COMPAT_VERSION)
	{
		g_set_error_literal (error, TERMINAL_OPTION_ERROR,
		                     TERMINAL_OPTION_ERROR_INCOMPATIBLE_CONFIG_FILE,
		                     _("Incompatible terminal config file version."));
		return FALSE;
	}

	groups = g_key_file_get_string_list (key_file, TERMINAL_CONFIG_GROUP, TERMINAL_CONFIG_PROP_WINDOWS, NULL, error);
	if (!groups)
		return FALSE;

	for (i = 0; groups[i]; ++i)
	{
		const char *window_group = groups[i];
		char **tab_groups;
		InitialWindow *iw;
		guint j;

		tab_groups = g_key_file_get_string_list (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_TABS, NULL, error);
		if (!tab_groups)
			continue; /* no tabs in this window, skip it */

		iw = initial_window_new (source_tag);
		initial_windows = g_list_append (initial_windows, iw);
		apply_defaults (options, iw);

		iw->role = g_key_file_get_string (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_ROLE, NULL);
		iw->geometry = g_key_file_get_string (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_GEOMETRY, NULL);
		iw->start_fullscreen = g_key_file_get_boolean (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_FULLSCREEN, NULL);
		iw->start_maximized = g_key_file_get_boolean (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_MAXIMIZED, NULL);
		if (g_key_file_has_key (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_MENUBAR_VISIBLE, NULL))
		{
			iw->force_menubar_state = TRUE;
			iw->menubar_state = g_key_file_get_boolean (key_file, window_group, TERMINAL_CONFIG_WINDOW_PROP_MENUBAR_VISIBLE, NULL);
		}

		for (j = 0; tab_groups[j]; ++j)
		{
			const char *tab_group = tab_groups[j];
			InitialTab *it;
			char *profile;

			profile = g_key_file_get_string (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_PROFILE_ID, NULL);
			it = initial_tab_new (profile, TRUE);
			g_free (profile);

			iw->tabs = g_list_append (iw->tabs, it);

			/*          it->width = g_key_file_get_integer (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_WIDTH, NULL);
			          it->height = g_key_file_get_integer (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_HEIGHT, NULL);*/
			it->working_dir = terminal_util_key_file_get_string_unescape (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_WORKING_DIRECTORY, NULL);
			it->title = g_key_file_get_string (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_TITLE, NULL);

			if (g_key_file_has_key (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_COMMAND, NULL) &&
			        !(it->exec_argv = terminal_util_key_file_get_argv (key_file, tab_group, TERMINAL_CONFIG_TERMINAL_PROP_COMMAND, NULL, error)))
			{
				have_error = TRUE;
				break;
			}
		}

		g_strfreev (tab_groups);

		if (have_error)
			break;
	}

	g_strfreev (groups);

	if (have_error)
	{
		g_list_foreach (initial_windows, (GFunc) initial_window_free, NULL);
		g_list_free (initial_windows);
		return FALSE;
	}

	options->initial_windows = g_list_concat (options->initial_windows, initial_windows);

	return TRUE;
}

/**
 * terminal_options_ensure_window:
 * @options:
 *
 * Ensure that @options will contain at least one window to open.
 */
void
terminal_options_ensure_window (TerminalOptions *options)
{
	ensure_top_window (options);
}

/**
 * terminal_options_free:
 * @options:
 *
 * Frees @options.
 */
void
terminal_options_free (TerminalOptions *options)
{
	g_list_foreach (options->initial_windows, (GFunc) initial_window_free, NULL);
	g_list_free (options->initial_windows);

	g_strfreev (options->env);
	g_free (options->default_role);
	g_free (options->default_geometry);
	g_free (options->default_working_dir);
	g_free (options->default_title);
	g_free (options->default_profile);

	g_strfreev (options->exec_argv);

	g_free (options->display_name);
	g_free (options->startup_id);

	g_slice_free (TerminalOptions, options);
}

static GOptionContext *
get_goption_context (TerminalOptions *options)
{
	const GOptionEntry global_unique_goptions[] =
	{
		{
			"disable-factory",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_disable_factory_callback,
			N_("Do not register with the activation nameserver, do not re-use an active terminal"),
			NULL
		},
		{
			"load-config",
			0,
			G_OPTION_FLAG_FILENAME,
			G_OPTION_ARG_CALLBACK,
			option_load_save_config_cb,
			N_("Load a terminal configuration file"),
			N_("FILE")
		},
		{
			"save-config",
			0,
			G_OPTION_FLAG_FILENAME,
			G_OPTION_ARG_CALLBACK,
			option_load_save_config_cb,
			N_("Save the terminal configuration to a file"),
			N_("FILE")
		},
		{ "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
		{ NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	const GOptionEntry global_multiple_goptions[] =
	{
		{
			"window",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_window_callback,
			N_("Open a new window containing a tab with the default profile"),
			NULL
		},
		{
			"tab",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_tab_callback,
			N_("Open a new tab in the last-opened window with the default profile"),
			NULL
		},
		{ NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	const GOptionEntry window_goptions[] =
	{
		{
			"show-menubar",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_show_menubar_callback,
			N_("Turn on the menubar"),
			NULL
		},
		{
			"hide-menubar",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_hide_menubar_callback,
			N_("Turn off the menubar"),
			NULL
		},
		{
			"maximize",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_maximize_callback,
			N_("Maximize the window"),
			NULL
		},
		{
			"full-screen",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_fullscreen_callback,
			N_("Full-screen the window"),
			NULL
		},
		{
			"geometry",
			0,
			0,
			G_OPTION_ARG_CALLBACK,
			option_geometry_callback,
			N_("Set the window size; for example: 80x24, or 80x24+200+200 (COLSxROWS+X+Y)"),
			N_("GEOMETRY")
		},
		{
			"role",
			0,
			0,
			G_OPTION_ARG_CALLBACK,
			option_role_callback,
			N_("Set the window role"),
			N_("ROLE")
		},
		{
			"active",
			0,
			G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			option_active_callback,
			N_("Set the last specified tab as the active one in its window"),
			NULL
		},
		{ NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	const GOptionEntry terminal_goptions[] =
	{
		{
			"command",
			'e',
			G_OPTION_FLAG_FILENAME,
			G_OPTION_ARG_CALLBACK,
			option_command_callback,
			N_("Execute the argument to this option inside the terminal"),
			NULL
		},
		{
			"profile",
			0,
			0,
			G_OPTION_ARG_CALLBACK,
			option_profile_cb,
			N_("Use the given profile instead of the default profile"),
			N_("PROFILE-NAME")
		},
		{
			"title",
			't',
			0,
			G_OPTION_ARG_CALLBACK,
			option_title_callback,
			N_("Set the terminal title"),
			N_("TITLE")
		},
		{
			"working-directory",
			0,
			G_OPTION_FLAG_FILENAME,
			G_OPTION_ARG_CALLBACK,
			option_working_directory_callback,
			N_("Set the working directory"),
			N_("DIRNAME")
		},
		{
			"zoom",
			0,
			0,
			G_OPTION_ARG_CALLBACK,
			option_zoom_callback,
			N_("Set the terminal's zoom factor (1.0 = normal size)"),
			N_("ZOOM")
		},
		{ NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	const GOptionEntry internal_goptions[] =
	{
		{
			"profile-id",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_CALLBACK,
			option_profile_id_cb,
			NULL, NULL
		},
		{
			"window-with-profile",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_CALLBACK,
			option_window_callback,
			NULL, NULL
		},
		{
			"tab-with-profile",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_CALLBACK,
			option_tab_callback,
			NULL, NULL
		},
		{
			"window-with-profile-internal-id",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_CALLBACK,
			option_window_callback,
			NULL, NULL
		},
		{
			"tab-with-profile-internal-id",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_CALLBACK,
			option_tab_callback,
			NULL, NULL
		},
		{
			"default-working-directory",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_FILENAME,
			&options->default_working_dir,
			NULL, NULL,
		},
		{
			"use-factory",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_NONE,
			&options->use_factory,
			NULL, NULL
		},
		{
			"startup-id",
			0,
			G_OPTION_FLAG_HIDDEN,
			G_OPTION_ARG_STRING,
			&options->startup_id,
			NULL,
			NULL
		},
		/*
		 * Crappy old compat args
		 */
		{
			"tclass",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"font",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"nologin",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"login",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"foreground",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"background",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"solid",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"bgscroll",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"bgnoscroll",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"shaded",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"noshaded",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"transparent",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"utmp",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"noutmp",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"wtmp",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"nowtmp",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"lastlog",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"nolastlog",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"icon",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"termname",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{
			"start-factory-server",
			0,
			G_OPTION_FLAG_HIDDEN | G_OPTION_FLAG_NO_ARG,
			G_OPTION_ARG_CALLBACK,
			unsupported_option_callback,
			NULL, NULL
		},
		{ NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	GOptionContext *context;
	GOptionGroup *group;

	context = g_option_context_new (NULL);
	g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
	g_option_context_set_description (context, N_("MATE Terminal Emulator"));

	group = g_option_group_new ("mate-terminal",
	                            N_("MATE Terminal Emulator"),
	                            N_("Show MATE Terminal options"),
	                            options,
	                            NULL);
	g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
	g_option_group_add_entries (group, global_unique_goptions);
	g_option_group_add_entries (group, internal_goptions);
	g_option_group_set_parse_hooks (group, NULL, digest_options_callback);
	g_option_context_set_main_group (context, group);

	group = g_option_group_new ("terminal",
	                            N_("Options to open new windows or terminal tabs; more than one of these may be specified:"),
	                            N_("Show terminal options"),
	                            options,
	                            NULL);
	g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
	g_option_group_add_entries (group, global_multiple_goptions);
	g_option_context_add_group (context, group);

	group = g_option_group_new ("window-options",
	                            N_("Window options; if used before the first --window or --tab argument, sets the default for all windows:"),
	                            N_("Show per-window options"),
	                            options,
	                            NULL);
	g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
	g_option_group_add_entries (group, window_goptions);
	g_option_context_add_group (context, group);

	group = g_option_group_new ("terminal-options",
	                            N_("Terminal options; if used before the first --window or --tab argument, sets the default for all terminals:"),
	                            N_("Show per-terminal options"),
	                            options,
	                            NULL);
	g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
	g_option_group_add_entries (group, terminal_goptions);
	g_option_context_add_group (context, group);

	return context;
}