-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.c
2154 lines (1984 loc) · 65 KB
/
default.c
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
/* default.c
* (c) 2002 Mikulas Patocka, Petr 'Brain' Kulhavy
* This file is a part of the Links program, released under GPL
*
* Does the configuration file.
*/
#include "links.h"
unsigned char system_name[MAX_STR_LEN];
static void get_system_name(void)
{
#ifdef OS2
if (!os_get_system_name(system_name))
return;
#endif
#if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
{
struct utsname name;
int rs;
memset(&name, 0, sizeof name);
EINTRLOOP(rs, uname(&name));
if (!rs) {
unsigned char *str = init_str();
int l = 0;
add_to_str(&str, &l, cast_uchar name.sysname);
add_to_str(&str, &l, cast_uchar " ");
add_to_str(&str, &l, cast_uchar name.release);
add_to_str(&str, &l, cast_uchar " ");
add_to_str(&str, &l, cast_uchar name.machine);
if (l >= MAX_STR_LEN) str[MAX_STR_LEN - 1] = 0;
strcpy(cast_char system_name, cast_const_char str);
mem_free(str);
return;
}
}
#endif
#ifdef HAVE_POPEN
if (0) {
FILE *f;
unsigned char *p;
memset(system_name, 0, MAX_STR_LEN);
ENULLLOOP(f, popen("uname -srm", "r"));
if (!f) goto fail;
if (fread(system_name, 1, MAX_STR_LEN - 1, f) <= 0) {
pclose(f);
goto fail;
}
pclose(f);
for (p = system_name; *p; p++) if (*p < ' ') {
*p = 0;
break;
}
if (system_name[0]) return;
}
fail:
#endif
strcpy(cast_char system_name, SYSTEM_NAME);
}
unsigned char compiler_name[MAX_STR_LEN];
static void get_compiler_name(void)
{
#if defined(__BORLANDC__)
int w = __BORLANDC__+0;
int v1 = w / 0x100;
int v2 = w / 0x10 % 0x10;
int v3 = w % 0x10;
if (v1 == 4 && v2 < 5) v1 = 3;
if (v1 == 4 && v2 == 5) v2 = 0;
if (!v3) sprintf(cast_char compiler_name, "Borland C %d.%d", v1, v2);
else sprintf(cast_char compiler_name, "Borland C %d.%d.%d", v1, v2, v3);
#elif defined(__clang__)
#if !defined(__clang_major__) || !defined(__clang_minor__)
sprintf(cast_char compiler_name, "LLVM/Clang");
#else
int v1 = __clang_major__+0;
int v2 = __clang_minor__+0;
#ifdef __clang_patchlevel__
int v3 = __clang_patchlevel__+0;
#else
int v3 = 0;
#endif
if (v3 > 0) sprintf(cast_char compiler_name, "LLVM/Clang %d.%d.%d", v1, v2, v3);
else sprintf(cast_char compiler_name, "LLVM/Clang %d.%d", v1, v2);
#endif
#elif defined(__COMO_VERSION__)
int w = __COMO_VERSION__+0;
int v1 = w / 100;
int v2 = w % 100;
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Comeau C %d.%d", v1, v2 / 10);
else sprintf(cast_char compiler_name, "Comeau C %d.%02d", v1, v2);
#elif defined(__convexc__)
sprintf(cast_char compiler_name, "Convex C");
#elif defined(_CRAYC)
#if !defined(_RELEASE) || !defined(_RELEASE_MINOR)
sprintf(cast_char compiler_name, "Cray C");
#else
int v1 = _RELEASE+0;
int v2 = _RELEASE_MINOR+0;
sprintf(cast_char compiler_name, "Cray C %d.%d", v1, v2);
#endif
#elif defined(__DCC__)
#ifndef __VERSION_NUMBER__
sprintf(cast_char compiler_name, "Diab C");
#else
int w = __VERSION_NUMBER__+0;
int v1 = w / 1000;
int v2 = w / 100 % 10;
int v3 = w % 100;
sprintf(cast_char compiler_name, "Diab C %d.%d.%02d", v1, v2, v3);
#endif
#elif defined(__DMC__)
int w = __DMC__+0;
int v1 = w / 0x100;
int v2 = w / 0x10 % 0x10;
int v3 = w % 0x10;
if (!v3) sprintf(cast_char compiler_name, "Digital Mars C %d.%d", v1, v2);
else sprintf(cast_char compiler_name, "Digital Mars C %d.%d.%d", v1, v2, v3);
#elif defined(__DECC_VER)
int w = __DECC_VER+0;
int v1 = w / 10000000;
int v2 = w / 100000 % 100;
int v3 = w % 10000;
if (!(v2 % 10)) sprintf(cast_char compiler_name, "DEC C %d.%d-%d", v1, v2 / 10, v3);
else sprintf(cast_char compiler_name, "DEC C %d.%02d-%d", v1, v2, v3);
#elif defined(__ghs__)
#ifndef __GHS_VERSION_NUMBER__
sprintf(cast_char compiler_name, "Green Hill C");
#else
int w = __GHS_VERSION_NUMBER__+0;
int v1 = w / 100;
int v2 = w / 10 % 10;
int v3 = w % 10;
sprintf(cast_char compiler_name, "Green Hill C %d.%d.%d", v1, v2, v3);
#endif
#elif defined(__HIGHC__)
sprintf(cast_char compiler_name, "MetaWare High C");
#elif defined(__HP_cc)
int w = __HP_cc+0;
int v1 = w / 10000;
int v2 = w / 100 % 100;
int v3 = w % 100;
if (w <= 1) sprintf(cast_char compiler_name, "HP CC");
else sprintf(cast_char compiler_name, "HP CC %d.%02d.%02d", v1, v2, v3);
#elif defined(__xlc__)
int w = __xlc__+0;
int v1 = w / 0x100;
int v2 = w % 0x100;
sprintf(cast_char compiler_name, "IBM XL C %X.%X", v1, v2);
#elif defined(__IBMC__) && defined(__COMPILER_VER__)
unsigned w = __COMPILER_VER__+0;
int v0 = w / 0x10000000;
int v1 = w / 0x1000000 % 0x10;
int v2 = w / 0x10000 % 0x100;
int v3 = w % 0x10000;
unsigned char *os = !v0 ? "S/370" : v0 == 1 ? "OS/390" : v0 == 4 ? "z/OS" : "";
sprintf(cast_char compiler_name, "IBM%s%s XL C %X.%0X.%X", *os ? " " : "", os, v1, v2, v3);
#elif defined(__ICC)
int w = __ICC+0;
int v1 = w / 100;
int v2 = w % 100;
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Intel C %d.%d", v1, v2 / 10);
else sprintf(cast_char compiler_name, "Intel C %d.%02d", v1, v2);
#elif defined(__LCC__)
sprintf(cast_char compiler_name, "LCC");
#elif defined(__NDPC__)
sprintf(cast_char compiler_name, "Microway NDP C");
#elif defined(_MSC_VER)
int w = _MSC_VER+0;
int v1 = w / 100;
int v2 = w % 100;
unsigned char *visual = cast_uchar "";
if (v1 >= 8) {
v1 -= 6;
if (v1 == 2) v1 = 1;
visual = cast_uchar "Visual ";
}
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Microsoft %sC %d.%d", visual, v1, v2 / 10);
else sprintf(cast_char compiler_name, "Microsoft %sC %d.%02d", visual, v1, v2);
#elif defined(__MWERKS__)
int w = __MWERKS__+0;
int v1 = w / 0x1000;
int v2 = w / 0x100 % 0x10;
int v3 = w % 0x100;
if (w <= 1) sprintf(cast_char compiler_name, "Metrowerks CodeWarrior");
sprintf(cast_char compiler_name, "Metrowerks CodeWarrior %x.%x.%x", v1, v2, v3);
#elif defined(__NWCC__)
sprintf(cast_char compiler_name, "NWCC");
#elif defined(__OPEN64__)
unsigned char *n = cast_uchar "Open64 " __OPEN64__;
if (strlen(cast_const_char n) >= sizeof(cast_char compiler_name)) n = cast_uchar "Open64";
strcpy(cast_char compiler_name, cast_const_char n);
#elif defined(__PATHSCALE__)
unsigned char *n = cast_uchar "PathScale " __PATHSCALE__;
if (strlen(cast_const_char n) >= sizeof(cast_char compiler_name)) n = cast_uchar "PathScale";
strcpy(cast_char compiler_name, cast_const_char n);
#elif defined(__PCC__)
int v1 = __PCC__+0;
#ifdef __PCC_MINOR__
int v2 = __PCC_MINOR__+0;
#else
int v2 = 0;
#endif
#ifdef __PCC_MINORMINOR__
int v3 = __PCC_MINORMINOR__+0;
#else
int v3 = 0;
#endif
sprintf(cast_char compiler_name, "PCC %d.%d.%d", v1, v2, v3);
#elif defined(__PGI) || defined(__PGIC__)
#if !defined(__PGIC__) || !defined(__PGIC_MINOR__)
sprintf(cast_char compiler_name, "The Portland Group C");
#else
int v1 = __PGIC__+0;
int v2 = __PGIC_MINOR__+0;
#ifdef __PGIC_PATCHLEVEL__
int v3 = __PGIC_PATCHLEVEL__+0;
#else
int v3 = 0;
#endif
if (v3 > 0) sprintf(cast_char compiler_name, "The Portland Group C %d.%d.%d", v1, v2, v3);
else sprintf(cast_char compiler_name, "The Portland Group C %d.%d", v1, v2);
#endif
#elif defined(__SASC__)
int w = __SASC__+0;
int v1 = w / 100;
int v2 = w % 100;
sprintf(cast_char compiler_name, "SAS C %d.%02d", v1, v2);
#elif (defined(__sgi) && defined(_COMPILER_VERSION)) || defined(_SGI_COMPILER_VERSION)
#ifdef _SGI_COMPILER_VERSION
int w = _SGI_COMPILER_VERSION;
#else
int w = _COMPILER_VERSION;
#endif
int v1 = w / 100;
int v2 = w / 10 % 10;
int v3 = w % 10;
sprintf(cast_char compiler_name, "MIPSpro %d.%d.%d", v1, v2, v3);
#elif defined(__SUNPRO_C)
int w = __SUNPRO_C+0;
int div = w >= 0x1000 ? 0x1000 : 0x100;
int v2_digits = w >= 0x1000 ? 2 : 1;
int v1 = w / div;
int v2 = w % div / 0x10;
int v3 = w % 0x10;
if (!v3) sprintf(cast_char compiler_name, "Sun C %X.%0*X", v1, v2_digits, v2);
else sprintf(cast_char compiler_name, "Sun C %X.%0*X.%X", v1, v2_digits, v2, v3);
#elif defined(__SYSC__) && defined(__SYSC_VER__)
int w = __SYSC_VER__+0;
int v1 = w / 10000;
int v2 = w / 100 % 100;
int v3 = w % 100;
sprintf(cast_char compiler_name, "Dignus Systems C %d.%02d.%02d", v1, v2, v3);
#elif defined(__TenDRA__)
sprintf(cast_char compiler_name, "TenDRA C");
#elif defined(__TINYC__)
sprintf(cast_char compiler_name, "Tiny C");
#elif defined(_UCC)
#if !defined(_MAJOR_REV) || !defined(_MINOR_REV)
sprintf(cast_char compiler_name, "Ultimate C");
#else
int v1 = _MAJOR_REV+0;
int v2 = _MAJOR_REV+0;
sprintf(cast_char compiler_name, "Ultimate C %d.%d", v1, v2);
#endif
#elif defined(__USLC__)
sprintf(cast_char compiler_name, "USL C");
#elif defined(__VAXC)
sprintf(cast_char compiler_name, "VAX C");
#elif defined(__VOSC__)
sprintf(cast_char compiler_name, "Stratus VOS C");
#elif defined(__WATCOMC__)
int w = __WATCOMC__+0;
int v1 = w / 100;
int v2 = w % 100;
unsigned char *op = cast_uchar "";
if (v1 >= 12) {
v1 -= 11;
op = cast_uchar "Open";
}
if (!(v2 % 10)) sprintf(cast_char compiler_name, "%sWatcom C %d.%d", op, v1, v2 / 10);
else sprintf(cast_char compiler_name, "%sWatcom C %d.%02d", op, v1, v2);
#elif defined(__GNUC__)
int v1 = __GNUC__+0;
#ifdef __GNUC_MINOR__
int v2 = __GNUC_MINOR__+0;
#else
int v2 = 0;
#endif
#ifdef __GNUC_PATCHLEVEL__
int v3 = __GNUC_PATCHLEVEL__+0;
#else
int v3 = 0;
#endif
#if defined(__llvm__)
unsigned char *prefix = cast_uchar "LLVM/";
#else
unsigned char *prefix = cast_uchar "";
#endif
if (v1 == 2 && (v2 >= 90 && v2 <= 91)) sprintf(cast_char compiler_name, "%sEGCS 1.%d", prefix, v2 - 90);
else if (v3 > 0) sprintf(cast_char compiler_name, "%sGNU C %d.%d.%d", prefix, v1, v2, v3);
else sprintf(cast_char compiler_name, "%sGNU C %d.%d", prefix, v1, v2);
#else
strcpy(cast_char compiler_name, "unknown compiler");
#endif
}
struct option {
int p;
unsigned char *(*rd_cmd)(struct option *, unsigned char ***, int *);
unsigned char *(*rd_cfg)(struct option *, unsigned char *);
void (*wr_cfg)(struct option *, unsigned char **, int *);
int min, max; /* for double min and max are in 1/100's (e.g. 0.1 is min==10) */
void *ptr;
char *cfg_name;
char *cmd_name;
};
static unsigned char *p_arse_options(int argc, unsigned char *argv[], struct option **opt)
{
unsigned char *e, *u = NULL;
while (argc) {
int i;
argv++, argc--;
if (argv[-1][0] == '-') {
struct option *options;
struct option **op;
for (op = opt; (options = *op); op++) for (i = 0; options[i].p; i++)
if (options[i].rd_cmd && options[i].cmd_name &&
!strcasecmp(cast_const_char options[i].cmd_name, cast_const_char &argv[-1][1])) {
if ((e = options[i].rd_cmd(&options[i], &argv, &argc))) {
if (e[0]) fprintf(stderr, "Error parsing option %s: %s\n", argv[-1], e);
return NULL;
}
goto found;
}
uu:
#ifdef GRDRV_DIRECTFB
if (!strncmp(cast_const_char argv[-1], "--dfb:", 6))
goto found;
#endif
fprintf(stderr, "Unknown option %s\n", argv[-1]);
return NULL;
} else if (!u) u = argv[-1];
else goto uu;
found:;
}
if (u) return u;
return cast_uchar "";
}
static unsigned char *get_token(unsigned char **line)
{
unsigned char *s = NULL;
int l = 0;
int escape = 0;
int quote = 0;
while (**line == ' ' || **line == 9) (*line)++;
if (**line) {
for (s = init_str(); **line; (*line)++) {
if (escape)
escape = 0;
else if (**line == '\\') {
escape = 1;
continue;
}
else if (**line == '"') {
quote = !quote;
continue;
}
else if ((**line == ' ' || **line == 9) && !quote)
break;
add_chr_to_str(&s, &l, **line);
}
}
return s;
}
static void parse_config_file(unsigned char *name, unsigned char *file, struct option **opt)
{
struct option *options;
struct option **op;
int err = 0;
int line = 0;
unsigned char *e;
int i;
unsigned char *n, *p;
unsigned char *tok;
int nl, pl;
while (file[0]) {
line++;
while (file[0] && (file[0] == ' ' || file[0] == 9)) file++;
n = file;
while (file[0] && file[0] > ' ') file++;
if (file == n) {
if (file[0]) file++;
continue;
}
nl = file - n;
while (file[0] == 9 || file[0] == ' ') file++;
p = file;
while (file[0] && file[0] != 10 && file[0] != 13) file++;
pl = file - p;
if (file[0]) {
if ((file[1] == 10 || file[1] == 13) && file[0] != file[1]) file++;
file++;
}
tok = NULL;
if (n[0] == '#') goto f;
if (!(tok = get_token(&n))) goto f;
nl = strlen(cast_const_char tok);
for (op = opt; (options = *op); op++)
for (i = 0; options[i].p; i++) if (options[i].cfg_name && (size_t)nl == strlen(cast_const_char options[i].cfg_name) && !casecmp(tok, cast_uchar options[i].cfg_name, nl)) {
unsigned char *o = memacpy(p, pl);
if ((e = options[i].rd_cfg(&options[i], o))) {
if (e[0]) fprintf(stderr, "Error parsing config file %s, line %d: %s\n", name, line, e), err = 1;
}
mem_free(o);
goto f;
}
fprintf(stderr, "Unknown option in config file %s, line %d\n", name, line);
err = 1;
f:
if (tok) mem_free(tok);
}
if (err) fprintf(stderr, "\007"), sleep(1);
}
static unsigned char *create_config_string(struct option *options)
{
unsigned char *s = init_str();
int l = 0;
int i;
add_to_str(&s, &l, cast_uchar "# This file is automatically generated by Links -- please do not edit.");
for (i = 0; options[i].p; i++) if (options[i].wr_cfg)
options[i].wr_cfg(&options[i], &s, &l);
add_to_str(&s, &l, cast_uchar NEWLINE);
return s;
}
#define FILE_BUF 1024
static unsigned char cfg_buffer[FILE_BUF];
unsigned char *read_config_file(unsigned char *name)
{
int h, r;
int l = 0;
unsigned char *s;
int rs;
EINTRLOOP(h, open(cast_const_char name, O_RDONLY | O_NOCTTY));
if (h == -1) return NULL;
set_bin(h);
s = init_str();
while ((r = hard_read(h, cfg_buffer, FILE_BUF)) > 0) {
int i;
for (i = 0; i < r; i++) if (!cfg_buffer[i]) cfg_buffer[i] = ' ';
add_bytes_to_str(&s, &l, cfg_buffer, r);
}
if (r == -1) mem_free(s), s = NULL;
EINTRLOOP(rs, close(h));
return s;
}
int write_to_config_file(unsigned char *name, unsigned char *c)
{
int rr;
int r;
int h, w;
int count = 0;
int tmp_namel;
unsigned char *tmp_name;
int rs;
try_new_count:
tmp_namel = 0;
tmp_name = init_str();
add_to_str(&tmp_name, &tmp_namel, name);
add_to_str(&tmp_name, &tmp_namel, cast_uchar ".tmp.");
add_num_to_str(&tmp_name, &tmp_namel, count);
EINTRLOOP(h, open(cast_const_char tmp_name, O_WRONLY | O_NOCTTY | O_CREAT | O_TRUNC | O_EXCL, 0600));
if (h == -1) {
if (errno == EEXIST && count < MAXINT) {
count++;
mem_free(tmp_name);
goto try_new_count;
}
mem_free(tmp_name);
return get_error_from_errno(errno);
}
set_bin(h);
rr = strlen(cast_const_char c);
r = rr;
while (r > 0) {
if ((w = hard_write(h, c + rr - r, r)) <= 0) {
int err = !w ? ENOSPC : errno;
EINTRLOOP(rs, close(h));
EINTRLOOP(rs, unlink(cast_const_char tmp_name));
mem_free(tmp_name);
return get_error_from_errno(err);
}
r -= w;
}
EINTRLOOP(rs, close(h));
#ifndef RENAME_OVER_EXISTING_FILES
EINTRLOOP(rs, unlink(cast_const_char name));
#endif
EINTRLOOP(rs, rename(cast_const_char tmp_name, cast_const_char name));
if (rs) {
int err = errno;
EINTRLOOP(rs, unlink(cast_const_char tmp_name));
mem_free(tmp_name);
return get_error_from_errno(err);
}
mem_free(tmp_name);
return 0;
}
static unsigned char *get_home(int *n)
{
struct stat st;
int rs;
unsigned char *home = NULL;
unsigned char *home_links;
unsigned char *config_dir = stracpy(cast_uchar getenv("CONFIG_DIR"));
if (n) *n = 1;
#ifdef WIN32
if (!home) {
home = stracpy(cast_uchar getenv("APPDATA"));
#ifdef HAVE_CYGWIN_CONV_PATH
/*
* Newer Cygwin complains about windows-style path, so
* we have to convert it.
*/
if (home) {
unsigned char *new_path;
ssize_t sz = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, home, NULL, 0);
if (sz < 0)
goto skip_path_conv;
new_path = mem_alloc(sz);
sz = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, home, new_path, sz);
if (sz < 0) {
mem_free(new_path);
goto skip_path_conv;
}
mem_free(home);
home = new_path;
skip_path_conv:;
}
#endif
if (home) {
EINTRLOOP(rs, stat(cast_const_char home, &st));
if (rs || !S_ISDIR(st.st_mode)) {
mem_free(home);
home = NULL;
}
}
}
#endif
if (!home) home = stracpy("/var/tuxbox/config");
#ifdef WIN32
/* When we run in Cygwin without Cygwin environment, it reports home "/".
Unfortunatelly, it can't write anything to that directory */
if (home && !strcmp(cast_const_char home, "/")) {
mem_free(home);
home = NULL;
}
#endif
if (!home) {
int i;
home = stracpy(path_to_exe);
if (!home) {
if (config_dir) mem_free(config_dir);
return NULL;
}
for (i = strlen(cast_const_char home) - 1; i >= 0; i--) if (dir_sep(home[i])) {
home[i + 1] = 0;
goto br;
}
home[0] = 0;
br:;
}
while (home[0] && dir_sep(home[strlen(cast_const_char home) - 1])) home[strlen(cast_const_char home) - 1] = 0;
if (home[0]) add_to_strn(&home, cast_uchar "/");
home_links = stracpy(home);
if (config_dir)
{
add_to_strn(&home_links, config_dir);
while (home_links[0] && dir_sep(home_links[strlen(cast_const_char home_links) - 1])) home_links[strlen(cast_const_char home_links) - 1] = 0;
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (!rs && S_ISDIR(st.st_mode)) {
add_to_strn(&home_links, cast_uchar "/links");
} else {
fprintf(stderr, "CONFIG_DIR set to %s. But directory %s doesn't exist.\n\007", config_dir, home_links);
sleep(3);
mem_free(home_links);
home_links = stracpy(home);
add_to_strn(&home_links, cast_uchar "links");
}
mem_free(config_dir);
} else add_to_strn(&home_links, cast_uchar "links");
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (rs) {
#ifdef HAVE_MKDIR
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#endif
if (config_dir) goto failed;
goto first_failed;
}
if (S_ISDIR(st.st_mode)) goto home_ok;
/* This is a Cygwin hack! Cygwin reports stat for "links" if no
"links" exists and only "links.exe" does. So try to create directory
anyway. */
#ifdef HAVE_MKDIR
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#endif
first_failed:
mem_free(home_links);
home_links = stracpy(home);
add_to_strn(&home_links, cast_uchar "links");
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (rs) {
#ifdef HAVE_MKDIR
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#else
mem_free(home_links);
home_links = stracpy(home);
goto home_ok;
#endif
goto failed;
}
if (S_ISDIR(st.st_mode)) goto home_ok;
#ifdef HAVE_MKDIR
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#endif
failed:
mem_free(home_links);
mem_free(home);
return NULL;
home_ok:
if (n) *n = 0;
home_creat:
#ifdef HAVE_CHMOD
EINTRLOOP(rs, chmod(cast_const_char home_links, 0700));
#endif
add_to_strn(&home_links, cast_uchar "/");
mem_free(home);
return home_links;
}
void init_home(void)
{
get_system_name();
get_compiler_name();
links_home = get_home(&first_use);
if (!links_home) {
fprintf(stderr, "Unable to find or create links config directory. Please check, that you have $HOME variable set correctly and that you have write permission to your home directory.\n\007");
sleep(3);
return;
}
}
/* prefix: directory
* name: name of the configuration file (typ. links.cfg)
*/
static int write_config_data(unsigned char *prefix, unsigned char *name, struct option *o, struct terminal *term)
{
int err;
unsigned char *c, *config_file;
if (!(c = create_config_string(o))) return -1;
config_file = stracpy(prefix);
if (!config_file) {
mem_free(c);
return -1;
}
add_to_strn(&config_file, name);
if ((err = write_to_config_file(config_file, c))) {
if (term) msg_box(term, NULL, TEXT_(T_CONFIG_ERROR), AL_CENTER | AL_EXTD_TEXT, TEXT_(T_UNABLE_TO_WRITE_TO_CONFIG_FILE), cast_uchar ": ", get_err_msg(err), NULL, NULL, 1, TEXT_(T_CANCEL), NULL, B_ENTER | B_ESC);
mem_free(c);
mem_free(config_file);
return -1;
}
mem_free(c);
mem_free(config_file);
return 0;
}
static void add_nm(struct option *o, unsigned char **s, int *l)
{
if (*l) add_to_str(s, l, cast_uchar NEWLINE);
add_to_str(s, l, cast_uchar o->cfg_name);
add_to_str(s, l, cast_uchar " ");
}
static void add_quoted_to_str(unsigned char **s, int *l, unsigned char *q)
{
add_chr_to_str(s, l, '"');
while (*q) {
if (*q == '"' || *q == '\\') add_chr_to_str(s, l, '\\');
add_chr_to_str(s, l, *q);
q++;
}
add_chr_to_str(s, l, '"');
}
static unsigned char *num_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
unsigned char *end;
long l;
if (!tok) return cast_uchar "Missing argument";
l = strtolx(tok, &end);
if (*end) {
mem_free(tok);
return cast_uchar "Number expected";
}
if (l < o->min || l > o->max) {
mem_free(tok);
return cast_uchar "Out of range";
}
*(int *)o->ptr = l;
mem_free(tok);
return NULL;
}
static void num_wr(struct option *o, unsigned char **s, int *l)
{
add_nm(o, s, l);
add_knum_to_str(s, l, *(int *)o->ptr);
}
static unsigned char *dbl_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
unsigned char *end;
double d;
if (!tok) return cast_uchar "Missing argument";
d = strtod(cast_const_char tok, (char **)(void *)&end);
if (*end) {
mem_free(tok);
return cast_uchar "Number expected";
}
if (100*d < o->min || 100*d > o->max) {
mem_free(tok);
return cast_uchar "Out of range";
}
*(double *)o->ptr = d;
mem_free(tok);
return NULL;
}
static void dbl_wr(struct option *o, unsigned char **s, int *l)
{
unsigned char number[80];
snprintf(cast_char number, sizeof number, "%.4f", *(double*)o->ptr);
add_nm(o, s, l);
add_to_str(s, l, number);
}
static unsigned char *str_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
unsigned char *e = NULL;
if (!tok) return NULL;
if (strlen(cast_const_char tok) + 1 > (size_t)o->max) e = cast_uchar "String too long";
else strcpy(cast_char o->ptr, cast_const_char tok);
mem_free(tok);
return e;
}
static void str_wr(struct option *o, unsigned char **s, int *l)
{
add_nm(o, s, l);
if (strlen(cast_const_char o->ptr) + 1 > (size_t)o->max) {
unsigned char *s1 = init_str();
int l1 = 0;
add_bytes_to_str(&s1, &l1, o->ptr, o->max - 1);
add_quoted_to_str(s, l, s1);
mem_free(s1);
}
else add_quoted_to_str(s, l, o->ptr);
}
static unsigned char *cp_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
unsigned char *e = NULL;
int i;
if (!tok) return cast_uchar "Missing argument";
if ((i = get_cp_index(tok)) == -1) e = cast_uchar "Unknown codepage";
#ifndef ENABLE_UTF8
else if (o->min == 1 && i == utf8_table) e = cast_uchar "UTF-8 can't be here";
#endif
else *(int *)o->ptr = i;
mem_free(tok);
return e;
}
static void cp_wr(struct option *o, unsigned char **s, int *l)
{
unsigned char *n = get_cp_mime_name(*(int *)o->ptr);
add_nm(o, s, l);
add_to_str(s, l, n);
}
static unsigned char *lang_rd(struct option *o, unsigned char *c)
{
int i;
unsigned char *tok = get_token(&c);
if (!tok) return cast_uchar "Missing argument";
for (i = 0; i < n_languages(); i++)
if (!(strcasecmp(cast_const_char language_name(i), cast_const_char tok))) {
set_language(i);
mem_free(tok);
return NULL;
}
mem_free(tok);
return cast_uchar "Unknown language";
}
static void lang_wr(struct option *o, unsigned char **s, int *l)
{
add_nm(o, s, l);
add_quoted_to_str(s, l, language_name(current_language));
}
static int getnum(unsigned char *s, int *n, int r1, int r2)
{
unsigned char *e;
long l = strtol(cast_const_char s, (char **)(void *)&e, 10);
if (*e || !*s) return -1;
if (l < r1 || l >= r2) return -1;
*n = (int)l;
return 0;
}
static unsigned char *type_rd(struct option *o, unsigned char *c)
{
unsigned char *err = cast_uchar "Error reading association specification";
struct assoc new;
unsigned char *w;
int n = 0; /* against warning */
memset(&new, 0, sizeof(struct assoc));
if (!(new.label = get_token(&c))) goto err;
if (!(new.ct = get_token(&c))) goto err;
if (!(new.prog = get_token(&c))) goto err;
if (!(w = get_token(&c))) goto err;
if (getnum(w, &n, 0, 128)) goto err_f;
mem_free(w);
new.cons = !!(n & 1);
new.xwin = !!(n & 2);
new.ask = !!(n & 4);
if ((n & 8) || (n & 16)) new.block = !!(n & 16);
else new.block = !new.xwin || new.cons;
new.accept_http = !!(n & 32);
new.accept_ftp = !!(n & 64);
if (!(w = get_token(&c))) goto err;
if (strlen(cast_const_char w) != 1 || w[0] < '0' || w[0] > '9') goto err_f;
new.system = w[0] - '0';
mem_free(w);
update_assoc(&new);
err = NULL;
err:
if (new.label) mem_free(new.label);
if (new.ct) mem_free(new.ct);
if (new.prog) mem_free(new.prog);
return err;
err_f:
mem_free(w);
goto err;
}
static unsigned char *block_rd(struct option *o, unsigned char *c)
{
unsigned char *err = cast_uchar "Error reading image block specification";
unsigned char* url;
if(!(url = get_token(&c)))
return err;
block_url_add(NULL, url);
mem_free(url);
return NULL;
}
static void block_wr(struct option *o, unsigned char **s, int *l)
{
struct block *a;
foreachback(a, blocks) {
add_nm(o, s, l);
add_quoted_to_str(s, l, a->url);
}
}
static void type_wr(struct option *o, unsigned char **s, int *l)
{
struct assoc *a;
foreachback(a, assoc) {
add_nm(o, s, l);
add_quoted_to_str(s, l, a->label);
add_to_str(s, l, cast_uchar " ");
add_quoted_to_str(s, l, a->ct);
add_to_str(s, l, cast_uchar " ");
add_quoted_to_str(s, l, a->prog);
add_to_str(s, l, cast_uchar " ");
add_num_to_str(s, l, (!!a->cons) + (!!a->xwin) * 2 + (!!a->ask) * 4 + (!a->block) * 8 + (!!a->block) * 16 + (!!a->accept_http) * 32 + (!!a->accept_ftp) * 64);
add_to_str(s, l, cast_uchar " ");
add_num_to_str(s, l, a->system);
}
}
static unsigned char *ext_rd(struct option *o, unsigned char *c)
{