summaryrefslogtreecommitdiff
path: root/support/dktools/dkwxwiz.c
blob: 2c1ae6779126d3891fa149461b15bb0a013f44ce (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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dkwxwiz.ctr
*/

/*
Copyright (C) 2013-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/

/**	@file dkwxwiz.c The dkwxwiz module.
*/


#line 9 "dkwxwiz.ctr"

#include "dk3all.h"
#if 0
#include "dkt-version.h"
#endif
#include "dk4vers.h"




#line 18 "dkwxwiz.ctr"



/**	@defgroup	dkwxwizcmd	Commands for dkwxwiz.
*/
/**@{*/

/**	Show help text.
*/
#define	DKWXWIZ_CMD_HELP	1

/**	Show version information.
*/
#define	DKWXWIZ_CMD_VERSION	2

/**	Show license terms.
*/
#define	DKWXWIZ_CMD_LICENSE	4

/**@}*/


/**	Job structure for dkwxwiz.
*/
typedef struct {
  dk3_app_t		*app;	/**< Application structure. */
  dkChar const * const	*msg;	/**< Localized messages texts. */
  dk3_option_set_t	*opt;	/**< Command line options. */
  dkChar const		*con;	/**< Copyright holder name. */
  dkChar const		*lin;	/**< License name. */
  dkChar const		*pName;	/**< Program name. */
  dkChar const		*aName;	/**< Application class name. */
  dkChar const		*fName;	/**< Frame class name. */
  dkChar const		*xfn;	/**< XPM file name (allocated). */
  dkChar const		*pfn;	/**< Program header file name. */
  dkChar const		*afn;	/**< Application class file name. */
  dkChar const		*ffn;	/**< Frame class file name. */
  dkChar const		*ifn;	/**< Icon file name. */
  dkChar const		*rfn;	/**< RC file name. */
  int			 exv;	/**< Exit value. */
  int			 cmd;	/**< Command to execute. */
  int			 frc;	/**< Force overwriting existing files. */
  int			 obj;	/**< Flag: Use object modules, not libs. */
} DKWXWIZ_JOB;



/**	Macros like $program$ are allowed in the templates.
*/
static char const * const	dkwxwiz_macros[] = {
/* 0 */
"program",

/* 1 */
"application",

/* 2 */
"frame",

/* 3 */
"year",

/* 4 */
"copyrightowner",

/* 5 */
"licensetype",

NULL


#line 94 "dkwxwiz.ctr"
};



/**	Constant dkChar strings, not localized.
*/
static dkChar const * const	dkwxwiz_nl[] = {
/* 0 */
dkT(".h"),

/* 1 */
dkT(".cpt"),

/* 2 */
dkT(".wxc"),

/* 3 */
dkT("dkwxwizh.txt"),

/* 4 */
dkT("dkwxwiza.txt"),

/* 5 */
dkT("dkwxwizf.txt"),

/* 6 */
dkT("r"),

/* 7 */
dkT("w"),

/* 8 */
dkT("Makefile.in"),

/* 9 */
dkT("configure.ac"),

/* 10 */
dkT("dkwxwizm.txt"),

/* 11 */
dkT("dkwxwizc.txt"),

/* 12 */
dkT("wb"),

/* 13 */
dkT("\r\n"),

/* 14 */
dkT("\n"),

/* 15 */
dkT(".xpm"),

/* 16 */
dkT("dkwxwizx.txt"),

/* 17 */
dkT(".ico"),

/* 18 */
dkT("dkwxwiz.ico"),

/* 19 */
dkT("rb"),

/* 20 */
dkT(".rc"),

/* 21 */
dkT("dkwxwizr.txt"),

/* 22 */
dkT("makefile.vc"),

/* 23 */
dkT("dkwxwizv.txt"),

NULL


#line 199 "dkwxwiz.ctr"
};



/**	Constant dkChar strings, localized versions are used if found.
*/
static dkChar const * const	dkwxwiz_msg[]   = {
/* 0 */
dkT("dkwxwiz"),

/* 1 */
dkT("dktools"),

/* 2 */
dkT("dkwxwiz.str"),

/* 3 */
dkT("dkwxwiz.txt"),

/* 4 */
dkT("__CHANGE__ 001:\tCopyright owner"),

/* 5 */
dkT("bsd"),

/* 6 */
dkT("File base name \""),

/* 7 */
dkT("\" too long!"),

/* 8 */
dkT("File \""),

/* 9 */
dkT("\" already exists!"),

/* 10 */
dkT("Unknown macro in line!"),

/* 11 */
dkT("Too few command line arguments!"),

/* 12 */
dkT("You should create/modify the following files:"),

/* 13 */
dkT("Windows help file:     \""),

/* 14 */
dkT("non-Windows help file: \""),

/* 15 */
dkT("Windows icon file:     \""),

/* 16 */
dkT("non-Windows icon file: \""),

/* 17 */
dkT("\"."),

/* 18 */
dkT(".chm"),

/* 19 */
dkT(".htb"),

/* 20 */
dkT(".ico"),

/* 21 */
dkT(".xpm"),

/* 22 */
dkT("Text too long or not ASCII: \""),

/* 23 */
dkT("\"!"),

/* 24 */
dkT("Not a C/C++ identifier: \""),

/* 25 */
dkT("\"!"),

NULL


#line 279 "dkwxwiz.ctr"
};



/**	License conditions shown for dkwxwiz --license-terms.
*/
static dkChar const * const dkwxwiz_license[] = {
dkT(""),
dkT("Copyright (c) 2013-2016, Dirk Krause"),
dkT("All rights reserved."),
dkT(""),
dkT("Redistribution and use in source and binary forms, with or without"),
dkT("modification, are permitted provided that the following conditions are met:"),
dkT(""),
dkT("* Redistributions of source code must retain the above copyright notice,"),
dkT("  this list of conditions and the following disclaimer."),
dkT("* Redistributions in binary form must reproduce the above copyright"),
dkT("  notice, this list of conditions and the following disclaimer in the"),
dkT("  documentation and/or other materials provided with the distribution."),
dkT("* Neither the name of the copyright holder(s) nor the names of"),
dkT("  contributors may be used to endorse or promote products derived from"),
dkT("  this software without specific prior written permission."),
dkT(""),
dkT("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"),
dkT("\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT"),
dkT("LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A"),
dkT("PARTICULAR PURPOSE ARE DISCLAIMED."),
dkT("IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY"),
dkT("DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL"),
dkT("DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS"),
dkT("OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)"),
dkT("HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,"),
dkT("STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN"),
dkT("ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE"),
dkT("POSSIBILITY OF SUCH DAMAGE."),
dkT(""),
NULL


#line 317 "dkwxwiz.ctr"
};


/**	Options allowed for dkwxwiz.
*/
static dk3_option_t const dkwxwiz_options[] = {
  { dkT('h'),  dkT("help"), 0 },
  { dkT('v'),  dkT("version"), 0 },
  { dkT('L'),  dkT("license-terms"), 0 },
  { dkT('f'),  dkT("force"), 0},
  { dkT('l'),  dkT("license"), 1 },
  { dkT('c'),  dkT("copyright-owner"), 1 },
  { dkT('o'),  dkT("object-modules"), 0 }, 
};


/**	Number of options in the dkwxwiz_options array.
*/
static size_t dkwxwiz_sz_options =
sizeof(dkwxwiz_options)/sizeof(dk3_option_t);



/**	Default help text, shown if no help file is found.
*/
static dkChar const * dkwxwiz_help_text[] = {
dkT(""),
dkT("NAME"),
dkT(""),
dkT("  dkwxwiz - Wizard to create a wxWidgets + dkct based project."),
dkT(""),
dkT("SYNOPSIS"),
dkT(""),
dkT("  dkwxwiz [<options>] <program> <application> <window>"),
dkT(""),
dkT("DESCRIPTION"),
dkT(""),
dkT("The program creates source code skeletons for a new GUI application based"),
dkT("on wxWidgets assuming you use the dktools libraries and the dkct program"),
dkT("during development."),
dkT("As arguments you have to specify:"),
dkT(""),
dkT("* Name of the final program (i.e. \"myprog\")."),
dkT(""),
dkT("* Name of the application class derived from wxApp (i.e, \"MyProgApp\")."),
dkT(""),
dkT("* Name of the main window class (i.e. \"MyProgFrame\")."),
dkT(""),
dkT("Valid C/C++ identifiers are required for all three names."),
dkT(""),
dkT("OPTIONS"),
dkT(""),
dkT("-h\t--help"),
dkT("  Show a help text."),
dkT(""),
dkT("-v\t--version"),
dkT("  Show the program version."),
dkT(""),
dkT("-L\t--license-terms"),
dkT("  Show the license conditions."),
dkT(""),
dkT("-f\t--force"),
dkT("  Force the program to overwrite existing files."),
dkT(""),
dkT("-c <copyright owner name>"),
dkT("  Name of the copyright owner."),
dkT(""),
dkT("-l <license type>"),
dkT("  License type (i.e. \"bsd\")."),
dkT(""),
dkT("EXIT STATUS"),
dkT(""),
dkT("0 on success, all other exit status codes indicate an error."),
dkT(""),
dkT("EXAMPLES"),
dkT(""),
dkT("Run"),
dkT("  dkwxwiz testprog TestApp TestFrame"),
dkT("in an empty directory to create a new project."),
dkT(""),
dkT("FILES"),
dkT(""),
dkT("The following files from the dktools resource directory are used:"),
dkT(""),
dkT("dkwxwizh.txt\tTemplate for project header file *.h."),
dkT(""),
dkT("dkwxwiza.txt\tTemplate for application class source *.cpt"),
dkT(""),
dkT("dkwxwizf.txt\tTemplate for top-level window class source *.wxc."),
dkT(""),
dkT("dkwxwizc.txt\tTemplate for configure.ac file for autoconf."),
dkT(""),
dkT("dkwxwizm.txt\tTemplate for the Makefile.in file."),
dkT(""),
dkT("dkwxwizx.txt\tTemplate for the *.xpm icon file."),
dkT(""),
dkT("You can create copies of these files in dkt-3-site and modify them"),
dkT("i.e. to set a default copyright owner and a default license type."),
dkT(""),
dkT("SEE ALSO"),
dkT(""),
dkT("http://dktools.sourceforge.net"),
dkT(""),
dkT("AUTHOR"),
dkT(""),
dkT("Dirk Krause"),
dkT(""),
dkT("LICENSE"),
dkT(""),
dkT("Use"),
dkT("  dkwxwiz --license-terms"),
dkT("to view the license conditions."),
NULL


#line 431 "dkwxwiz.ctr"
};


/**	Initialize job structure.
	@param	job	Job structure to initialize.
	@param	app	Application structure.
	@param	msg	Localized message texts.
*/
static
void
dkwxwiz_job_init(DKWXWIZ_JOB *job, dk3_app_t *app, dkChar const * const *msg)
{
  job->app = app;
  job->msg = msg;
  job->opt = NULL;
  job->pName = NULL;
  job->aName = NULL;
  job->fName = NULL;
  job->xfn = NULL;
  job->pfn = NULL;
  job->afn = NULL;
  job->ffn = NULL;
  job->ifn = NULL;
  job->rfn = NULL;
  job->exv = 1;
  job->cmd = 0;
  job->frc = 0;
  job->obj = 0;
  job->con = NULL;
  job->lin = NULL;
}



/**	Concatenate base name and file suffix.
	@param	job	Job structure.
	@param	bn	Base file name.
	@param	sf	File name suffix.
	@return	Pointer to new allocated file name on success, NULL on error.
*/
static
dkChar const *
dkwxwiz_concatenate(DKWXWIZ_JOB *job, dkChar const *bn, dkChar const *sf)
{
  dkChar		 buf[DK3_MAX_PATH];	/* Temporary name buffer. */
  dkChar const		*back = NULL;
  

#line 478 "dkwxwiz.ctr"
  if(DK3_SIZEOF(buf,dkChar) > (dk3str_len(bn) + dk3str_len(sf))) {
    dk3str_cpy_not_overlapped(buf, bn);
    dk3str_cat(buf, sf);
    back = (dkChar const *)dk3str_dup_app(buf, job->app);
  } else {
    /* ERROR: Base name too long! */
    dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 6, 7, bn);
  } 

#line 486 "dkwxwiz.ctr"
  return back;
}



/**	Build file names for output files.
	@param	job	Job structure.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_build_file_names(DKWXWIZ_JOB *job)
{
  int		 back = 0;
  

#line 501 "dkwxwiz.ctr"
  job->pfn = dkwxwiz_concatenate(job, job->pName, dkwxwiz_nl[0]);
  job->afn = dkwxwiz_concatenate(job, job->aName, dkwxwiz_nl[1]);
  job->ffn = dkwxwiz_concatenate(job, job->fName, dkwxwiz_nl[2]);
  job->xfn = dkwxwiz_concatenate(job, job->pName, dkwxwiz_nl[15]);
  job->ifn = dkwxwiz_concatenate(job, job->pName, dkwxwiz_nl[17]);
  job->rfn = dkwxwiz_concatenate(job, job->pName, dkwxwiz_nl[20]);
  if((job->pfn) && (job->afn) && (job->ffn) && (job->xfn) && (job->ifn)) {
    if(job->rfn) {
      back = 1;
    }
  } 

#line 512 "dkwxwiz.ctr"
  return back;
}



/**	Check one file name whether we can write the file.
	@param	job	Job structure.
	@param	fn	Output file name.
	@return	1 if allowed to write the file, 0 otherwise.
*/
static
int
dkwxwiz_check_one_filename(DKWXWIZ_JOB *job, dkChar const *fn)
{
  dk3_stat_t	 stb;		/* File information. */
  int		 back = 0;
  

#line 529 "dkwxwiz.ctr"
  if(0 == dk3sf_stat_app(&stb, fn, NULL)) {
    back = 1;
  } else {
    /* ERROR: File already exists! */
    dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 8, 9, fn);
  } 

#line 535 "dkwxwiz.ctr"
  return back;
}



/**	Check whether we can write all files.
	@param	job	Job structure.
	@return	1 if we are allowed to write/overwrite the files, 0 otherwise.
*/
static
int
dkwxwiz_check_filenames(DKWXWIZ_JOB *job)
{
  int			 back = 0;
  

#line 550 "dkwxwiz.ctr"
  if(job->frc) {
    back = 1;
  } else {
    back = 1;
    if(!dkwxwiz_check_one_filename(job, job->pfn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, job->afn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, job->ffn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, job->xfn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, dkwxwiz_nl[8])) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, dkwxwiz_nl[9])) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, job->ifn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, job->rfn)) {
      back = 0;
    }
    if(!dkwxwiz_check_one_filename(job, dkwxwiz_nl[22])) {
      back = 0;
    }
  } 

#line 582 "dkwxwiz.ctr"
  return back;
}



/**	Write one string as 8-bit character text to output.
	@param	job	Job structure.
	@param	dfile	Destination file.
	@param	txt	Text to write.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_write_c8_string(DKWXWIZ_JOB *job, FILE *dfile, dkChar const *txt)
{
#if DK3_CHAR_SIZE > 1
  char			 buf[1024];	/* Temporary buffer for output. */
#endif
  int			 back = 0;
#if DK3_CHAR_SIZE > 1
  int			 res = 0;
  

#line 604 "dkwxwiz.ctr"
  res = dk3str_to_c8p_app(
    buf, sizeof(buf), txt, dk3app_get_encoding(job->app), job->app
  );
  if(res) {
    fputs(buf, dfile);
    back = 1;
  } 

#line 611 "dkwxwiz.ctr"
#else
  

#line 613 "dkwxwiz.ctr"
  fputs(txt, dfile);
  back = 1; 

#line 615 "dkwxwiz.ctr"
#endif
  return back;
}



/**	Process one macro.
	@param	job	Job structure.
	@param	dfile	Output (destination) file.
	@param	sfile	Input (source) file.
	@param	cptr	Start of text.
	@param	p1	First '$' character.
	@param	p2	Second '$' character.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_one_macro(
  DKWXWIZ_JOB	*job,
  FILE		*dfile,
  FILE		*sfile,
  char		*cptr,
  char		*p1,
  char		*p2
)
{
  dk3_tm_t		 tm;		/* Local time. */
  dkChar const		*vptr;		/* Pointer to value text. */
  dk3_time_t		 timer;		/* Current time. */
  int			 action;	/* Macro action to perform. */
  int			 back = 1;
  *p1 = '\0';
  fputs(cptr, dfile);
  *p2 = '\0';
#if VERSION_BEFORE_20150821
  cptr = &(p2[1]);
#endif
  p1++;
  action = dk3str_c8_array_index(dkwxwiz_macros, p1, 0);
  switch(action) {
    case 0: {        /* program */
      if(!dkwxwiz_write_c8_string(job, dfile, job->pName)) {
        back = 0;
      }
    } break;
    case 1: {        /* application */
      if(!dkwxwiz_write_c8_string(job, dfile, job->aName)) {
        back = 0;
      }
    } break;
    case 2: {        /* frame */
      if(!dkwxwiz_write_c8_string(job, dfile, job->fName)) {
        back = 0;
      }
    } break;
    case 3: {        /* year */
      if(dk3sf_time(&timer)) {
        if(dk3sf_localtime_app(&tm, &timer, job->app)) {
          fprintf(dfile, "%04d", tm.Y);
        } else {
          back = 0;
        }
      } else {
        /* ERROR: Failed to obtain current time. */
        dk3app_log_i1(job->app, DK3_LL_ERROR, 80);
        back = 0;
      }
    } break;
    case 4: {        /* copyrightowner */
      vptr = job->con;
      if(!(vptr)) { vptr = (job->msg)[4]; }
      if(!dkwxwiz_write_c8_string(job, dfile, vptr)) {
        back = 0;
      }
    } break;
    case 5: {        /* licensetype */
      vptr = job->lin;
      if(!(vptr)) { vptr = (job->msg)[5]; }
      if(!dkwxwiz_write_c8_string(job, dfile, vptr)) {
        back = 0;
      }
    } break;
    default: {
      /* ERROR: Unknown macro */
      dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 10);
      fputc('$', dfile);
      fputs(p1, dfile);
      fputc('$', dfile);
      back = 0;
    } break;
  }
  return back;
}



/**	Find '$' character starting a dkwxwiz macro.
	@param	txt	Text to search for macro.
	@return	Pointer to first macro found on success, NULL if no macro found.
*/
static
char *
dkwxwiz_find_macro_start(char *txt)
{
  char		*p1;		/* Probably start of new macro. */
  char		*back = NULL;
  p1 = dk3str_c8_chr(txt, '$');
  while((p1) && (!(back))) {
    switch(p1[1]) {
      case '!': case '?':
      case '(': case '{':
      case '@': case '*':
      case '<': case '>':{
        p1 = dk3str_c8_chr(&(p1[1]), '$');
      } break;
      default: {
        back = p1;
      } break;
    }
  }
  return back;
}



/**	Transfer one source file contents to destination file.
	@param	job	Job structure.
	@param	dfile	Destination file.
	@param	sfile	Template file.
	@param	iswin	Flag: Insist on Windows line ends.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_transfer_file(DKWXWIZ_JOB *job, FILE *dfile, FILE *sfile, int iswin)
{
  char			 buf[4096];		/* Line buffer. */
  char			*cptr;			/* Remaining line to process. */
  char			*p1;			/* Start of macro. */
  char			*p2;			/* End of macro. */
  unsigned long		 oldsourceline;		/* Old source line number. */
  unsigned long		 lineno = 0UL;		/* Current source line. */
  int			 back = 1;
  

#line 759 "dkwxwiz.ctr"
  oldsourceline = dk3app_get_source_line(job->app);
  while(fgets(buf, sizeof(buf), sfile)) {
    lineno++;
    dk3app_set_source_line(job->app, lineno);
    dk3str_c8_delnl(buf);
    cptr = buf;
#if 0
    while(cptr) {
      p1 = dk3str_c8_chr(cptr, '$');
      if(p1) {
        switch(p1[1]) {
	  case '!': case '?': case '(': case '{': {
	    p1 = dk3str_c8_chr(&(p1[1]), '$');
	    if(p1) {
	      p2 = dk3str_c8_chr(&(p1[1]), '$');
	      if(p2) {
	        if(!dkwxwiz_one_macro(job, dfile, sfile, cptr, p1, p2)) {
		  back = 0;
		}
		cptr = ++p2;
	      } else {
	        fputs(cptr, dfile);
	        cptr = NULL;
	      }
	    } else {
	      fputs(cptr, dfile);
	      cptr = NULL;
	    }
	  } break;
	  default: {
	    p2 = dk3str_c8_chr(&(p1[1]), '$');
	    if(p2) {
	      if(!dkwxwiz_one_macro(job, dfile, sfile, cptr, p1, p2)) {
	        back = 0;
	      }
	      cptr = ++p2;
	    } else {
	      fputs(cptr, dfile);
	      cptr = NULL;
	    }
	  } break;
	}
      } else {
        fputs(cptr, dfile);
	cptr = NULL;
      }
    }
#else
    while(cptr) {
      p1 = dkwxwiz_find_macro_start(cptr);
      if(p1) {
        p2 = dk3str_c8_chr(&(p1[1]), '$');
	if(p2) {
	  if(!dkwxwiz_one_macro(job, dfile, sfile, cptr, p1, p2)) {
	    back = 0;
	  }
	  cptr = p2;
	  cptr++;
	} else {
	  fputs(cptr, dfile);
	  cptr = NULL;
	}
      } else {
        fputs(cptr, dfile),
	cptr = NULL;
      }
    }
#endif
    /* fputs(dkwxwiz_nl[(iswin) ? 13 : 14], dfile); */
    if(iswin) {
      fputc(0x0d, dfile);
    }
    fputc(0x0a, dfile);
  }
  dk3app_set_source_line(job->app, oldsourceline);
  

#line 835 "dkwxwiz.ctr"
  return back;
}



/**	Produce one output file.
	@param	job	Job structure.
	@param	dn	Destination file name.
	@param	sn	Source file name (template).
	@param	isbin	Flag: Open output for binary writing.
	@param	iswin	Flag: Use Windows line ends.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_one_output_file(
  DKWXWIZ_JOB	*job,
  dkChar const	*dn,
  dkChar const	*sn,
  int		 isbin,
  int		 iswin
)
{
  dk3_search_t		*sres;		/* Search result set. */
  FILE			*sfile;		/* Source file. */
  FILE			*dfile;		/* Destination file. */
  dkChar const		*srcname;	/* Source file name. */
  dkChar const		*oldsourcename;	/* Old source file name. */
  int			 back = 0;
  

#line 865 "dkwxwiz.ctr"
  oldsourcename = dk3app_get_source_file(job->app);
  sres = dk3app_find_config_file_revers(job->app, sn, 0);
  if(sres) {				

#line 868 "dkwxwiz.ctr"
    dk3search_reset(sres);
    srcname = dk3search_next(sres);
    if(srcname) {			

#line 871 "dkwxwiz.ctr"
      sfile = dk3sf_fopen_app(srcname, dkwxwiz_nl[6], job->app);
      if(sfile) {			

#line 873 "dkwxwiz.ctr"
        dfile = dk3sf_fopen_app(dn, dkwxwiz_nl[(isbin) ? 12 : 7], job->app);
	if(dfile) {			

#line 875 "dkwxwiz.ctr"
	  dk3app_set_source_file(job->app, sn);
	  back = dkwxwiz_transfer_file(job, dfile, sfile, iswin);
	  if(!dk3sf_fclose_fn_app(dfile, dn, job->app)) {
	    back = 0;
	  }
	} else {			

#line 881 "dkwxwiz.ctr"
	}
        fclose(sfile);
      } else {				

#line 884 "dkwxwiz.ctr"
      }
    } else {				

#line 886 "dkwxwiz.ctr"
      /* ERROR: File sn not found! */
      dk3app_log_i3(job->app, DK3_LL_ERROR, 150, 151, sn);
    }
    dk3search_close(sres);
  } else {				

#line 891 "dkwxwiz.ctr"
  }
  dk3app_set_source_file(job->app, oldsourcename);
  

#line 894 "dkwxwiz.ctr"
  return back;
}



/**	Copy a binary file directly.
	@param	job	Job structure.
	@param	dn	Destination file name.
	@param	sn	Source file name.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_copy_binary(DKWXWIZ_JOB *job, dkChar const *dn, dkChar const *sn)
{
  char			 buf[4096];	/* Data buffer for copying. */
  dk3_search_t		*sres;		/* Search result set. */
  dkChar const		*srcname;	/* Source file name. */
  FILE			*sfile;		/* Source file. */
  FILE			*dfile;		/* Destination file. */
  size_t		 rdbytes;	/* Number of bytes read. */
  size_t		 wrbytes;	/* Number of bytes written. */
  int			 back	= 0;

  sres = dk3app_find_config_file_revers(job->app, sn, 0);
  if(sres) {
    dk3search_reset(sres);
    srcname = dk3search_next(sres);
    if(srcname) {
      sfile = dk3sf_fopen_app(srcname, dkwxwiz_nl[19], job->app);
      if(sfile) {
        dfile = dk3sf_fopen_app(dn, dkwxwiz_nl[12], job->app);
	if(dfile) {
	  back = 1;
	  do {
	    rdbytes = fread(buf, 1, sizeof(buf), sfile);
	    if(rdbytes > 0) {
	      wrbytes = fwrite(buf, 1, rdbytes, dfile);
	      if(wrbytes != rdbytes) {
	        back = 0;
		/* ERROR: Write operation failed! */
		dk3app_log_i1(job->app, DK3_LL_ERROR, 120);
	      }
	    }
	  } while((rdbytes > 0) && (1 == back));
	  if(!dk3sf_fclose_fn_app(dfile, dn, job->app)) {
	    back = 0;
	  }
	}
        fclose(sfile);
      }
    } else {
      dk3app_log_i3(job->app, DK3_LL_ERROR, 150, 151, sn);
    }
    dk3search_close(sres);
  }
  return back;
}



/**	Produce output files.
	@param	job	Job structure.
*/
static
void
dkwxwiz_produce_output(DKWXWIZ_JOB *job)
{
  

#line 963 "dkwxwiz.ctr"
  if(dkwxwiz_one_output_file(job, job->pfn, dkwxwiz_nl[3], 0, 0))
  {
    if(dkwxwiz_one_output_file(job, job->afn, dkwxwiz_nl[4], 0, 0))
    {
      if(dkwxwiz_one_output_file(job, job->ffn, dkwxwiz_nl[5], 0, 0))
      {
        if(dkwxwiz_one_output_file(job, dkwxwiz_nl[8], dkwxwiz_nl[10], 0, 0))
	{
	  if(dkwxwiz_one_output_file(job, dkwxwiz_nl[9], dkwxwiz_nl[11],0,0))
	  {
	    if(dkwxwiz_one_output_file(job, job->xfn, dkwxwiz_nl[16], 1, 0))
	    {
	      if(dkwxwiz_copy_binary(job, job->ifn, dkwxwiz_nl[18])) {
	      if(dkwxwiz_one_output_file(job,dkwxwiz_nl[22],dkwxwiz_nl[23],1,1))
	      {
	        if(dkwxwiz_one_output_file(job,job->rfn,dkwxwiz_nl[21],1,1)) {
                  job->exv = 0;
	          /* You should provide the following files: */
	          dk3sf_initialize_stdout();
	          dk3sf_fputs((job->msg)[12], stdout);
	          dk3sf_fputc(dkT('\n'), stdout);
	          /* Windows help file: */
	          dk3sf_fputs((job->msg)[13], stdout);
	          dk3sf_fputs(job->aName, stdout);
	          dk3sf_fputs((job->msg)[18], stdout);
	          dk3sf_fputs((job->msg)[17], stdout);
	          dk3sf_fputc(dkT('\n'), stdout);
	          /* non-Windows help file: */
	          dk3sf_fputs((job->msg)[14], stdout);
	          dk3sf_fputs(job->aName, stdout);
	          dk3sf_fputs((job->msg)[19], stdout);
	          dk3sf_fputs((job->msg)[17], stdout);
	          dk3sf_fputc(dkT('\n'), stdout);
	          /* Windows icon file (ico) */
	          dk3sf_fputs((job->msg)[15], stdout);
	          dk3sf_fputs(job->aName, stdout);
	          dk3sf_fputs((job->msg)[20], stdout);
	          dk3sf_fputs((job->msg)[17], stdout);
	          dk3sf_fputc(dkT('\n'), stdout);
	          /* non-Windows icon file (xpm) */
	          dk3sf_fputs((job->msg)[16], stdout);
	          dk3sf_fputs(job->aName, stdout);
	          dk3sf_fputs((job->msg)[21], stdout);
	          dk3sf_fputs((job->msg)[17], stdout);
	          dk3sf_fputc(dkT('\n'), stdout);
		}
	      }
	      }
	    }
	  }
	}
      }
    }
  } 

#line 1017 "dkwxwiz.ctr"
}




/**	Check whether character starts or continues an identifier.
	@param	c	Character to process.
	@param	isfirst	Flag: Is start of string.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_valid_identifier_char(char c, int isfirst)
{
  int		 back = 0;
  if('_' == c) {
    back = 1;
  } else {
    if(('a' <= c) && ('z' >= c)) {
      back = 1;
    } else {
      if(('A' <= c) && ('Z' >= c)) {
        back = 1;
      } else {
        if(!(isfirst)) {
	  if('0' == c) {
	    back = 1;
	  } else {
	    if(('1' <= c) && ('9' >= c)) {
	      back = 1;
	    }
	  }
	}
      }
    }
  }
  return back;
}



/**	Check ASCII string whether it contains a C/C++ identifier
	@param	job	Job structure.
	@param	txt	Text to check.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_check_ascii_c_identifier(DKWXWIZ_JOB *job, char const *txt)
{
  char const	*cptr;		/* Current character to process. */
  int		 isfirst = 1;	/* Flag: Is first character. */
  int		 back = 1;

  cptr = txt;
  while((*cptr) && (back)) {
    if(!dkwxwiz_valid_identifier_char(*cptr, isfirst)) {
      back = 0;
    }
    isfirst = 0;
    cptr++;
  }
  return back;
}



/**	Check one name whether or not it is a C/C++ identifier.
	@param	job	Job structure.
	@param	txt	Text to check.
	@return	1 on success, 0 on error.
*/
static
int
dkwxwiz_check_one_c_identifier(DKWXWIZ_JOB *job, dkChar const *txt)
{
  char			 buf[DK3_MAX_PATH];	/* Conversion buffer. */
  int			 res;			/* Conversion result. */
  int		 	 back = 0;

  res = dk3str_to_c8p_app(
    buf, sizeof(buf), txt, dk3app_get_encoding(job->app), NULL
  );
  if(res) {
    if(dkwxwiz_check_ascii_c_identifier(job, buf)) {
      back = 1;
    } else {
      /* ERROR: Not a C/C++ identifier! */
      dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 24, 25, txt);
    }
  } else {
    /* ERROR: Text is too long or contains non-ASCII characters! */
    dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 22, 23, txt);
  }
  return back;
}



/**	Check program name, application class name, window class name.
	@param	job	Job structure.
	@return	1 on success (all names are valid C/C++ identifiers),
	0 on error.
*/
static
int
dkwxwiz_check_c_identifiers(DKWXWIZ_JOB *job)
{
  int		 back = 1;
  if(!dkwxwiz_check_one_c_identifier(job, job->pName)) {
    back = 0;
  }
  if(!dkwxwiz_check_one_c_identifier(job, job->aName)) {
    back = 0;
  }
  if(!dkwxwiz_check_one_c_identifier(job, job->fName)) {
    back = 0;
  }
  return back;
}



/**	Run with known program, application and frame name.
	@param	job	Job structure.
*/
static
void
dkwxwiz_run_with_names(DKWXWIZ_JOB *job)
{
  

#line 1148 "dkwxwiz.ctr"
  if(dkwxwiz_build_file_names(job)) {
    if(dkwxwiz_check_filenames(job)) {
      if(dkwxwiz_check_c_identifiers(job)) {
        dkwxwiz_produce_output(job);
      }
    }
  }
  dk3_release(job->ffn);
  dk3_release(job->afn);
  dk3_release(job->pfn);
  dk3_release(job->xfn);
  dk3_release(job->ifn);
  dk3_release(job->rfn);
  

#line 1162 "dkwxwiz.ctr"
}



/**	Run the program normally (no help/version/license).
	@param	job	Job structure.
*/
static
void
dkwxwiz_run_normally(DKWXWIZ_JOB *job)
{
  int		nargs;		/* Number of command line arguments. */
  

#line 1175 "dkwxwiz.ctr"
  nargs = dk3opt_get_num_args(job->opt);
  if(3 <= nargs) {
    job->pName = dk3opt_get_arg(job->opt, 0);
    job->aName = dk3opt_get_arg(job->opt, 1);
    job->fName = dk3opt_get_arg(job->opt, 2);
    if((job->pName) && (job->aName) && (job->fName)) {
      if(dk3opt_is_set(job->opt, dkT('f'))) {
        job->frc = 1;
      }
      if(dk3opt_is_set(job->opt, dkT('o'))) {
        job->obj = 1;
      }
      job->con = dk3opt_get_short_arg(job->opt, dkT('c'));
      job->lin = dk3opt_get_short_arg(job->opt, dkT('l'));
      dkwxwiz_run_with_names(job);
    } else {
      /* ERROR: Too few arguments. */
      dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 11);
      dk3app_help(job->app, dkwxwiz_msg[3], dkwxwiz_help_text);
    }
  } else {
    /* ERROR: Too few arguments. */
    dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 11);
    dk3app_help(job->app, dkwxwiz_msg[3], dkwxwiz_help_text);
  } 

#line 1200 "dkwxwiz.ctr"
}



/**	Run the program.
	@param	job	Job structure.
*/
static
void
dkwxwiz_run(DKWXWIZ_JOB *job)
{
  dkChar const * const	*sptr;	/* Pointer to traverse text. */
  

#line 1213 "dkwxwiz.ctr"
  job->opt = dk3opt_open_from_app(
    dkwxwiz_options,
    dkwxwiz_sz_options,
    0x00,
    NULL,
    job->app
  );
  if(job->opt) {
    if(dk3opt_get_error_code(job->opt) == 0) {
      if(dk3opt_is_set(job->opt, dkT('h'))) {
        job->cmd |= DKWXWIZ_CMD_HELP;
      }
      if(dk3opt_is_set(job->opt, dkT('v'))) {
        job->cmd |= DKWXWIZ_CMD_VERSION;
      }
      if(dk3opt_is_set(job->opt, dkT('L'))) {
        job->cmd |= DKWXWIZ_CMD_LICENSE;
      }
      if(job->cmd) {
        /*	Version.
	*/
	dk3sf_initialize_stdout();
	dk3sf_fputs(dkwxwiz_msg[0], stdout);
	dk3sf_fputc(dkT(' '), stdout);
	dk3sf_fputs( DKT_VERSION_DK , stdout);
	dk3sf_fputc(dkT('\n'), stdout);
	/*	Help text.
	*/
        if(DKWXWIZ_CMD_HELP & (job->cmd)) {
	  dk3app_help(job->app, dkwxwiz_msg[3], dkwxwiz_help_text);
	}
	/*	License.
	*/
	if(DKWXWIZ_CMD_LICENSE & (job->cmd)) {
	  sptr = dkwxwiz_license;
	  while(*sptr) {
	    dk3sf_fputs(*(sptr++), stdout);
	    dk3sf_fputc(dkT('\n'), stdout);
	  }
	}
      } else {
        dkwxwiz_run_normally(job);
      }
    }
    dk3opt_close(job->opt);
    job->opt = NULL;
  } 

#line 1260 "dkwxwiz.ctr"
}



/**	Program entry point.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, any other value indicates an error.
*/
DK3_MAIN
{
  DKWXWIZ_JOB		 job;		/* Job structure. */
  dk3_app_t		*app = NULL;	/* Application structure. */
  dkChar const * const	*msg = NULL;	/* Localized messages. */
  int			 exval;		/* No success yet. */

  

#line 1277 "dkwxwiz.ctr"
  

#line 1278 "dkwxwiz.ctr"
#if DK3_HAVE_TZSET
  tzset();
#endif
  job.exv = 1;
  app = dk3app_open_command(
    argc, (dkChar const * const *)argv, dkwxwiz_msg[1]
  );
  if(app) {
    msg = dk3app_messages(app, dkwxwiz_msg[2], (dkChar const **)dkwxwiz_msg);
    dkwxwiz_job_init(&job, app, msg);
    dkwxwiz_run(&job);
    dk3app_close(app);
  } else {
    fputs("dkwxwiz: ERROR: Not enough memory!\n", stderr);
    fflush(stderr);
  }
  exval = job.exv;
  

#line 1296 "dkwxwiz.ctr"
  

#line 1297 "dkwxwiz.ctr"
  fflush(stdout);
  exit(exval); return exval;
}