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
|
/* types.c
* (c) 2002 Mikulas Patocka
* This file is a part of the Links program, released under GPL.
*/
#include "links.h"
/*------------------------ ASSOCIATIONS -----------------------*/
/* DECLARATIONS */
static void assoc_edit_item(struct dialog_data *, struct list *, void (*)(struct dialog_data *, struct list *, struct list *, struct list_description *), struct list *, unsigned char);
static void assoc_copy_item(struct list *, struct list *);
static struct list *assoc_new_item(void *);
static void assoc_delete_item(struct list *);
static struct list *assoc_find_item(struct list *start, unsigned char *str, int direction);
static unsigned char *assoc_type_item(struct terminal *, struct list *, int);
struct list assoc = { init_list_1st(&assoc.list_entry) 0, -1, NULL, init_list_last(&assoc.list_entry) };
static struct history assoc_search_history = { 0, { &assoc_search_history.items, &assoc_search_history.items } };
struct assoc_ok_struct {
void (*fn)(struct dialog_data *, struct list *, struct list *, struct list_description *);
struct list *data;
struct dialog_data *dlg;
};
static struct list_description assoc_ld={
0, /* 0= flat; 1=tree */
&assoc, /* list */
assoc_new_item,
assoc_edit_item,
NULL,
assoc_delete_item,
assoc_copy_item,
assoc_type_item,
assoc_find_item,
&assoc_search_history,
0, /* this is set in init_assoc function */
15, /* # of items in main window */
T_ASSOCIATION,
T_ASSOCIATIONS_ALREADY_IN_USE,
T_ASSOCIATIONS_MANAGER,
T_DELETE_ASSOCIATION,
0, /* no button */
NULL, /* no button */
NULL, /* no save*/
NULL,NULL,0,0, /* internal vars */
0, /* modified */
NULL,
NULL,
1,
};
static struct list *assoc_new_item(void *ignore)
{
struct assoc *neww;
neww = mem_calloc(sizeof(struct assoc));
neww->label = stracpy(cast_uchar "");
neww->ct = stracpy(cast_uchar "");
neww->prog = stracpy(cast_uchar "");
neww->block = neww->xwin = neww->cons = 1;
neww->ask = 1;
neww->accept_http = 0;
neww->accept_ftp = 0;
neww->head.type = 0;
neww->system = SYSTEM_ID;
return &neww->head;
}
static void assoc_delete_item(struct list *data)
{
struct assoc *del = get_struct(data, struct assoc, head);
if (del->head.list_entry.next) del_from_list(&del->head);
mem_free(del->label);
mem_free(del->ct);
mem_free(del->prog);
mem_free(del);
}
static void assoc_copy_item(struct list *in, struct list *out)
{
struct assoc *item_in = get_struct(in, struct assoc, head);
struct assoc *item_out = get_struct(out, struct assoc, head);
item_out->cons = item_in->cons;
item_out->xwin = item_in->xwin;
item_out->block = item_in->block;
item_out->ask = item_in->ask;
item_out->accept_http = item_in->accept_http;
item_out->accept_ftp = item_in->accept_ftp;
item_out->system = item_in->system;
mem_free(item_out->label);
mem_free(item_out->ct);
mem_free(item_out->prog);
item_out->label = stracpy(item_in->label);
item_out->ct = stracpy(item_in->ct);
item_out->prog = stracpy(item_in->prog);
}
/* allocate string and print association into it */
/* x: 0=type all, 1=type title only */
static unsigned char *assoc_type_item(struct terminal *term, struct list *data, int x)
{
unsigned char *txt, *txt1;
struct assoc *item;
if (data == &assoc) return stracpy(get_text_translation(TEXT_(T_ASSOCIATIONS), term));
item = get_struct(data, struct assoc, head);
txt = stracpy(cast_uchar "");
if (item->system != SYSTEM_ID) add_to_strn(&txt, cast_uchar "XX ");
add_to_strn(&txt, item->label);
add_to_strn(&txt, cast_uchar ": ");
add_to_strn(&txt, item->ct);
if (!x) {
add_to_strn(&txt, cast_uchar " -> ");
if (item->prog) add_to_strn(&txt, item->prog);
}
txt1 = convert(assoc_ld.codepage, term_charset(term), txt, NULL);
mem_free(txt);
return txt1;
}
void menu_assoc_manager(struct terminal *term, void *fcp, void *ses_)
{
struct session *ses = (struct session *)ses_;
create_list_window(&assoc_ld,&assoc, term, ses);
}
static unsigned char * const ct_msg[] = {
TEXT_(T_LABEL),
TEXT_(T_CONTENT_TYPES),
TEXT_(T_PROGRAM__IS_REPLACED_WITH_FILE_NAME),
#ifdef ASSOC_BLOCK
TEXT_(T_BLOCK_TERMINAL_WHILE_PROGRAM_RUNNING),
#endif
#ifdef ASSOC_CONS_XWIN
TEXT_(T_RUN_ON_TERMINAL),
TEXT_(T_RUN_IN_XWINDOW),
#endif
TEXT_(T_ASK_BEFORE_OPENING),
TEXT_(T_ACCEPT_HTTP),
TEXT_(T_ACCEPT_FTP),
};
static void assoc_edit_item_fn(struct dialog_data *dlg)
{
struct terminal *term = dlg->win->term;
int max = 0, min = 0;
int w, rw;
int y = gf_val(-1, -G_BFU_FONT_SIZE);
int p = 3;
if (dlg->win->term->spec->braille) y += gf_val(1, G_BFU_FONT_SIZE);
#ifdef ASSOC_BLOCK
p++;
#endif
#ifdef ASSOC_CONS_XWIN
p += 2;
#endif
max_text_width(term, ct_msg[0], &max, AL_LEFT);
min_text_width(term, ct_msg[0], &min, AL_LEFT);
max_text_width(term, ct_msg[1], &max, AL_LEFT);
min_text_width(term, ct_msg[1], &min, AL_LEFT);
max_text_width(term, ct_msg[2], &max, AL_LEFT);
min_text_width(term, ct_msg[2], &min, AL_LEFT);
max_group_width(term, ct_msg + 3, dlg->items + 3, p, &max);
min_group_width(term, ct_msg + 3, dlg->items + 3, p, &min);
max_buttons_width(term, dlg->items + 3 + p, 2, &max);
min_buttons_width(term, dlg->items + 3 + p, 2, &min);
w = term->x * 9 / 10 - 2 * DIALOG_LB;
if (w > max) w = max;
if (w < min) w = min;
if (w > term->x - 2 * DIALOG_LB) w = term->x - 2 * DIALOG_LB;
if (w < 1) w = 1;
rw = 0;
dlg_format_text_and_field(dlg, NULL, get_text_translation(ct_msg[0], term), &dlg->items[0], 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE * 1);
dlg_format_text_and_field(dlg, NULL, get_text_translation(ct_msg[1], term), &dlg->items[1], 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE * 1);
dlg_format_text_and_field(dlg, NULL, get_text_translation(ct_msg[2], term), &dlg->items[2], 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE * 1);
dlg_format_group(dlg, NULL, ct_msg + 3, dlg->items + 3, p, 0, &y, w, &rw);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_buttons(dlg, NULL, dlg->items + 3 + p, 2, 0, &y, w, &rw, AL_CENTER);
w = rw;
dlg->xw = w + 2 * DIALOG_LB;
dlg->yw = y + 2 * DIALOG_TB;
center_dlg(dlg);
draw_dlg(dlg);
y = dlg->y + DIALOG_TB;
if (dlg->win->term->spec->braille) y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_text_and_field(dlg, term, ct_msg[0], &dlg->items[0], dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_text_and_field(dlg, term, ct_msg[1], &dlg->items[1], dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_text_and_field(dlg, term, ct_msg[2], &dlg->items[2], dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_group(dlg, term, ct_msg + 3, &dlg->items[3], p, dlg->x + DIALOG_LB, &y, w, NULL);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_buttons(dlg, term, &dlg->items[3 + p], 2, dlg->x + DIALOG_LB, &y, w, NULL, AL_CENTER);
}
/* Puts url and title into the bookmark item */
static void assoc_edit_done(void *data)
{
struct dialog *d = (struct dialog *)data;
struct assoc *item = (struct assoc *)d->udata;
struct assoc_ok_struct *s = (struct assoc_ok_struct *)d->udata2;
unsigned char *txt;
unsigned char *label, *ct, *prog;
label = (unsigned char *)&d->items[12];
ct = label + MAX_STR_LEN;
prog = ct + MAX_STR_LEN;
txt = convert(term_charset(s->dlg->win->term), assoc_ld.codepage, label, NULL);
mem_free(item->label);
item->label = txt;
txt = convert(term_charset(s->dlg->win->term), assoc_ld.codepage, ct, NULL);
mem_free(item->ct);
item->ct = txt;
txt = convert(term_charset(s->dlg->win->term), assoc_ld.codepage, prog, NULL);
mem_free(item->prog);
item->prog = txt;
s->fn(s->dlg, s->data, &item->head, &assoc_ld);
d->udata = NULL; /* for abort function */
}
/* destroys an item, this function is called when edit window is aborted */
static void assoc_edit_abort(struct dialog_data *data)
{
struct dialog *dlg = data->dlg;
struct assoc *item = (struct assoc *)dlg->udata;
mem_free(dlg->udata2);
if (item) assoc_delete_item(&item->head);
}
static void assoc_edit_item(struct dialog_data *dlg, struct list *data, void (*ok_fn)(struct dialog_data *, struct list *, struct list *, struct list_description *), struct list *ok_arg, unsigned char dlg_title)
{
int p;
struct assoc *neww = get_struct(data, struct assoc, head);
struct terminal *term = dlg->win->term;
struct dialog *d;
struct assoc_ok_struct *s;
unsigned char *ct, *prog, *label;
d = mem_calloc(sizeof(struct dialog) + 11 * sizeof(struct dialog_item) + 3 * MAX_STR_LEN);
label = (unsigned char *)&d->items[12];
ct = label + MAX_STR_LEN;
prog = ct + MAX_STR_LEN;
safe_strncpy(label, neww->label, MAX_STR_LEN);
safe_strncpy(ct, neww->ct, MAX_STR_LEN);
safe_strncpy(prog, neww->prog, MAX_STR_LEN);
/* Create the dialog */
s = mem_alloc(sizeof(struct assoc_ok_struct));
s->fn = ok_fn;
s->data = ok_arg;
s->dlg = dlg;
switch (dlg_title) {
case TITLE_EDIT:
d->title = TEXT_(T_EDIT_ASSOCIATION);
break;
case TITLE_ADD:
d->title = TEXT_(T_ADD_ASSOCIATION);
break;
default:
internal_error("Unsupported dialog title.\n");
}
d->udata = neww;
d->udata2 = s;
d->fn = assoc_edit_item_fn;
d->abort = assoc_edit_abort;
d->refresh = assoc_edit_done;
d->refresh_data = d;
d->items[0].type = D_FIELD;
d->items[0].dlen = MAX_STR_LEN;
d->items[0].data = label;
d->items[0].fn = check_nonempty;
d->items[1].type = D_FIELD;
d->items[1].dlen = MAX_STR_LEN;
d->items[1].data = ct;
d->items[1].fn = check_nonempty;
d->items[2].type = D_FIELD;
d->items[2].dlen = MAX_STR_LEN;
d->items[2].data = prog;
d->items[2].fn = check_nonempty;
p = 3;
#ifdef ASSOC_BLOCK
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->block;
d->items[p++].dlen = sizeof(int);
#endif
#ifdef ASSOC_CONS_XWIN
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->cons;
d->items[p++].dlen = sizeof(int);
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->xwin;
d->items[p++].dlen = sizeof(int);
#endif
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->ask;
d->items[p++].dlen = sizeof(int);
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->accept_http;
d->items[p++].dlen = sizeof(int);
d->items[p].type = D_CHECKBOX;
d->items[p].data = (unsigned char *)&neww->accept_ftp;
d->items[p++].dlen = sizeof(int);
d->items[p].type = D_BUTTON;
d->items[p].gid = B_ENTER;
d->items[p].fn = ok_dialog;
d->items[p++].text = TEXT_(T_OK);
d->items[p].type = D_BUTTON;
d->items[p].gid = B_ESC;
d->items[p].text = TEXT_(T_CANCEL);
d->items[p++].fn = cancel_dialog;
d->items[p++].type = D_END;
do_dialog(term, d, getml(d, NULL));
}
static int assoc_test_entry(struct list *e, unsigned char *str)
{
struct assoc *a = get_struct(e, struct assoc, head);
return casestrstr(a->label, str) || casestrstr(a->ct, str);
}
static struct list *assoc_find_item(struct list *s, unsigned char *str, int direction)
{
struct list *e;
if (direction >= 0) {
for (e = list_next(s); e != s; e = list_next(e)) {
if (e->depth >= 0 && assoc_test_entry(e, str))
return e;
}
} else {
for (e = list_prev(s); e != s; e = list_prev(e)) {
if (e->depth >= 0 && assoc_test_entry(e, str))
return e;
}
}
if (e->depth >= 0 && assoc_test_entry(e, str))
return e;
return NULL;
}
void update_assoc(struct assoc *neww)
{
struct assoc *repl;
struct list *r;
struct list_head *lr;
if (!neww->label[0] || !neww->ct[0] || !neww->prog[0]) return;
foreach(struct list, r, lr, assoc.list_entry) {
repl = get_struct(r, struct assoc, head);
if (!strcmp(cast_const_char repl->label, cast_const_char neww->label)
&& !strcmp(cast_const_char repl->ct, cast_const_char neww->ct)
&& !strcmp(cast_const_char repl->prog, cast_const_char neww->prog)
&& repl->block == neww->block
&& repl->cons == neww->cons
&& repl->xwin == neww->xwin
&& repl->ask == neww->ask
&& repl->accept_http == neww->accept_http
&& repl->accept_ftp == neww->accept_ftp
&& repl->system == neww->system) {
del_from_list(&repl->head);
add_to_list(assoc.list_entry, &repl->head);
return;
}
}
repl = mem_calloc(sizeof(struct assoc));
repl->label = stracpy(neww->label);
repl->ct = stracpy(neww->ct);
repl->prog = stracpy(neww->prog);
repl->block = neww->block;
repl->cons = neww->cons;
repl->xwin = neww->xwin;
repl->ask = neww->ask;
repl->accept_http = neww->accept_http;
repl->accept_ftp = neww->accept_ftp;
repl->system = neww->system;
repl->head.type = 0;
add_to_list(assoc.list_entry, &repl->head);
}
/*------------------------ EXTENSIONS -----------------------*/
/* DECLARATIONS */
static void ext_edit_item(struct dialog_data *, struct list *, void (*)(struct dialog_data *, struct list *, struct list *, struct list_description *), struct list *, unsigned char);
static void ext_copy_item(struct list *, struct list *);
static struct list *ext_new_item(void *);
static void ext_delete_item(struct list *);
static struct list *ext_find_item(struct list *start, unsigned char *str, int direction);
static unsigned char *ext_type_item(struct terminal *, struct list *, int);
struct list extensions = { init_list_1st(&extensions.list_entry) 0, -1, NULL, init_list_last(&extensions.list_entry) };
static struct history ext_search_history = { 0, { &ext_search_history.items, &ext_search_history.items } };
static struct list_description ext_ld={
0, /* 0= flat; 1=tree */
&extensions, /* list */
ext_new_item,
ext_edit_item,
NULL,
ext_delete_item,
ext_copy_item,
ext_type_item,
ext_find_item,
&ext_search_history,
0, /* this is set in init_assoc function */
15, /* # of items in main window */
T_eXTENSION,
T_EXTENSIONS_ALREADY_IN_USE,
T_EXTENSIONS_MANAGER,
T_DELETE_EXTENSION,
0, /* no button */
NULL, /* no button */
NULL, /* no save*/
NULL,NULL,0,0, /* internal vars */
0, /* modified */
NULL,
NULL,
0,
};
static struct list *ext_new_item(void *ignore)
{
struct extension *neww;
neww = mem_calloc(sizeof(struct extension));
neww->ext = stracpy(cast_uchar "");
neww->ct = stracpy(cast_uchar "");
neww->head.type = 0;
return &neww->head;
}
static void ext_delete_item(struct list *data)
{
struct extension *del = get_struct(data, struct extension, head);
if (del->head.list_entry.next) del_from_list(&del->head);
if (del->ext) mem_free(del->ext);
if (del->ct) mem_free(del->ct);
mem_free(del);
}
static void ext_copy_item(struct list *in, struct list *out)
{
struct extension *item_in = get_struct(in, struct extension, head);
struct extension *item_out = get_struct(out, struct extension, head);
mem_free(item_out->ext);
mem_free(item_out->ct);
item_out->ext = stracpy(item_in->ext);
item_out->ct = stracpy(item_in->ct);
}
/* allocate string and print extension into it */
/* x: 0=type all, 1=type title only */
static unsigned char *ext_type_item(struct terminal *term, struct list *data, int x)
{
unsigned char *txt, *txt1;
struct extension *item;
if (data == &extensions) return stracpy(get_text_translation(TEXT_(T_FILE_EXTENSIONS), term));
item = get_struct(data, struct extension, head);
txt = stracpy(item->ext);
add_to_strn(&txt, cast_uchar ": ");
add_to_strn(&txt, item->ct);
txt1 = convert(assoc_ld.codepage, term_charset(term), txt, NULL);
mem_free(txt);
return txt1;
}
void menu_ext_manager(struct terminal *term, void *fcp, void *ses_)
{
struct session *ses = (struct session *)ses_;
create_list_window(&ext_ld, &extensions, term, ses);
}
static unsigned char * const ext_msg[] = {
TEXT_(T_EXTENSION_S),
TEXT_(T_CONTENT_TYPE),
};
static void ext_edit_item_fn(struct dialog_data *dlg)
{
struct terminal *term = dlg->win->term;
int max = 0, min = 0;
int w, rw;
int y = gf_val(-1, -G_BFU_FONT_SIZE);
if (dlg->win->term->spec->braille) y += gf_val(1, G_BFU_FONT_SIZE);
max_text_width(term, ext_msg[0], &max, AL_LEFT);
min_text_width(term, ext_msg[0], &min, AL_LEFT);
max_text_width(term, ext_msg[1], &max, AL_LEFT);
min_text_width(term, ext_msg[1], &min, AL_LEFT);
max_buttons_width(term, dlg->items + 2, 2, &max);
min_buttons_width(term, dlg->items + 2, 2, &min);
w = term->x * 9 / 10 - 2 * DIALOG_LB;
if (w > max) w = max;
if (w < min) w = min;
if (w > term->x - 2 * DIALOG_LB) w = term->x - 2 * DIALOG_LB;
if (w < 1) w = 1;
rw = 0;
dlg_format_text_and_field(dlg, NULL, ext_msg[0], &dlg->items[0], 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE * 1);
dlg_format_text_and_field(dlg, NULL, ext_msg[1], &dlg->items[1], 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE * 1);
dlg_format_buttons(dlg, NULL, dlg->items + 2, 2, 0, &y, w, &rw, AL_CENTER);
w = rw;
dlg->xw = w + 2 * DIALOG_LB;
dlg->yw = y + 2 * DIALOG_TB;
center_dlg(dlg);
draw_dlg(dlg);
y = dlg->y + DIALOG_TB;
if (dlg->win->term->spec->braille) y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_text_and_field(dlg, term, ext_msg[0], &dlg->items[0], dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_text_and_field(dlg, term, ext_msg[1], &dlg->items[1], dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT);
y += gf_val(1, G_BFU_FONT_SIZE);
dlg_format_buttons(dlg, term, &dlg->items[2], 2, dlg->x + DIALOG_LB, &y, w, NULL, AL_CENTER);
}
/* Puts url and title into the bookmark item */
static void ext_edit_done(void *data)
{
struct dialog *d = (struct dialog*)data;
struct extension *item = (struct extension *)d->udata;
struct assoc_ok_struct *s = (struct assoc_ok_struct *)d->udata2;
unsigned char *txt;
unsigned char *ext, *ct;
ext = (unsigned char *)&d->items[5];
ct = ext + MAX_STR_LEN;
txt = convert(term_charset(s->dlg->win->term), ext_ld.codepage,ext, NULL);
mem_free(item->ext);
item->ext = txt;
txt = convert(term_charset(s->dlg->win->term), ext_ld.codepage,ct, NULL);
mem_free(item->ct);
item->ct=txt;
s->fn(s->dlg, s->data, &item->head, &ext_ld);
d->udata = NULL; /* for abort function */
}
/* destroys an item, this function is called when edit window is aborted */
static void ext_edit_abort(struct dialog_data *data)
{
struct dialog *dlg = data->dlg;
struct extension *item = (struct extension *)dlg->udata;
mem_free(dlg->udata2);
if (item) ext_delete_item(&item->head);
}
static void ext_edit_item(struct dialog_data *dlg, struct list *data, void (*ok_fn)(struct dialog_data *, struct list *, struct list *, struct list_description *), struct list *ok_arg, unsigned char dlg_title)
{
struct extension *neww = get_struct(data, struct extension, head);
struct terminal *term = dlg->win->term;
struct dialog *d;
struct assoc_ok_struct *s;
unsigned char *ext;
unsigned char *ct;
d = mem_calloc(sizeof(struct dialog) + 4 * sizeof(struct dialog_item) + 2 * MAX_STR_LEN);
ext = (unsigned char *)&d->items[5];
ct = ext + MAX_STR_LEN;
safe_strncpy(ext, neww->ext, MAX_STR_LEN);
safe_strncpy(ct, neww->ct, MAX_STR_LEN);
/* Create the dialog */
s = mem_alloc(sizeof(struct assoc_ok_struct));
s->fn = ok_fn;
s->data = ok_arg;
s->dlg = dlg;
switch (dlg_title) {
case TITLE_EDIT:
d->title = TEXT_(T_EDIT_EXTENSION);
break;
case TITLE_ADD:
d->title = TEXT_(T_ADD_EXTENSION);
break;
default:
internal_error("Unsupported dialog title.\n");
}
d->udata = neww;
d->udata2 = s;
d->abort = ext_edit_abort;
d->refresh = ext_edit_done;
d->refresh_data = d;
d->title = TEXT_(T_EXTENSION);
d->fn = ext_edit_item_fn;
d->items[0].type = D_FIELD;
d->items[0].dlen = MAX_STR_LEN;
d->items[0].data = ext;
d->items[0].fn = check_nonempty;
d->items[1].type = D_FIELD;
d->items[1].dlen = MAX_STR_LEN;
d->items[1].data = ct;
d->items[1].fn = check_nonempty;
d->items[2].type = D_BUTTON;
d->items[2].gid = B_ENTER;
d->items[2].fn = ok_dialog;
d->items[2].text = TEXT_(T_OK);
d->items[3].type = D_BUTTON;
d->items[3].gid = B_ESC;
d->items[3].text = TEXT_(T_CANCEL);
d->items[3].fn = cancel_dialog;
d->items[4].type = D_END;
do_dialog(term, d, getml(d, NULL));
}
static int ext_test_entry(struct list *e, unsigned char *str)
{
struct extension *ext = get_struct(e, struct extension, head);
return casestrstr(ext->ext, str) || casestrstr(ext->ct, str);
}
static struct list *ext_find_item(struct list *s, unsigned char *str, int direction)
{
struct list *e;
if (direction >= 0) {
for (e = list_next(s); e != s; e = list_next(e)) {
if (e->depth >= 0 && ext_test_entry(e, str))
return e;
}
} else {
for (e = list_prev(s); e != s; e = list_prev(e)) {
if (e->depth >= 0 && ext_test_entry(e, str))
return e;
}
}
if (e->depth >= 0 && ext_test_entry(e, str))
return e;
return NULL;
}
void update_ext(struct extension *neww)
{
struct extension *repl;
struct list *r;
struct list_head *lr;
if (!neww->ext[0] || !neww->ct[0]) return;
foreach(struct list, r, lr, extensions.list_entry) {
repl = get_struct(r, struct extension, head);
if (!strcmp(cast_const_char repl->ext, cast_const_char neww->ext)
&& !strcmp(cast_const_char repl->ct, cast_const_char neww->ct)) {
del_from_list(&repl->head);
add_to_list(extensions.list_entry, &repl->head);
return;
}
}
repl = mem_calloc(sizeof(struct extension));
repl->ext = stracpy(neww->ext);
repl->ct = stracpy(neww->ct);
repl->head.type = 0;
add_to_list(extensions.list_entry, &repl->head);
}
void update_prog(struct list_head *l, unsigned char *p, int s)
{
struct protocol_program *repl;
struct list_head *lrepl;
foreach(struct protocol_program, repl, lrepl, *l) if (repl->system == s) {
mem_free(repl->prog);
goto ss;
}
repl = mem_alloc(sizeof(struct protocol_program));
add_to_list(*l, repl);
repl->system = s;
ss:
repl->prog = mem_alloc(MAX_STR_LEN);
safe_strncpy(repl->prog, p, MAX_STR_LEN);
}
unsigned char *get_prog(struct list_head *l)
{
struct protocol_program *repl;
struct list_head *lrepl;
foreach(struct protocol_program, repl, lrepl, *l) if (repl->system == SYSTEM_ID) return repl->prog;
update_prog(l, cast_uchar "", SYSTEM_ID);
foreach(struct protocol_program, repl, lrepl, *l) if (repl->system == SYSTEM_ID) return repl->prog;
internal_error("get_prog: program was not added");
return cast_uchar "";;
}
/* creates default extensions if extension list is empty */
void create_initial_extensions(void)
{
struct extension ext;
if (!list_empty(extensions.list_entry)) return;
/* here you can add any default extension you want */
ext.ext = cast_uchar "xpm", ext.ct=cast_uchar "image/x-xpixmap", update_ext(&ext);
ext.ext = cast_uchar "xls", ext.ct=cast_uchar "application/excel", update_ext(&ext);
ext.ext = cast_uchar "xbm", ext.ct=cast_uchar "image/x-xbitmap", update_ext(&ext);
ext.ext = cast_uchar "webp", ext.ct=cast_uchar "image/webp", update_ext(&ext);
ext.ext = cast_uchar "wav", ext.ct=cast_uchar "audio/x-wav", update_ext(&ext);
ext.ext = cast_uchar "tiff,tif", ext.ct=cast_uchar "image/tiff", update_ext(&ext);
ext.ext = cast_uchar "tga", ext.ct=cast_uchar "image/targa", update_ext(&ext);
ext.ext = cast_uchar "sxw", ext.ct=cast_uchar "application/x-openoffice", update_ext(&ext);
ext.ext = cast_uchar "swf", ext.ct=cast_uchar "application/x-shockwave-flash", update_ext(&ext);
ext.ext = cast_uchar "svg", ext.ct=cast_uchar "image/svg+xml", update_ext(&ext);
ext.ext = cast_uchar "sch", ext.ct=cast_uchar "application/gschem", update_ext(&ext);
ext.ext = cast_uchar "rtf", ext.ct=cast_uchar "application/rtf", update_ext(&ext);
ext.ext = cast_uchar "ra,rm,ram", ext.ct=cast_uchar "audio/x-pn-realaudio", update_ext(&ext);
ext.ext = cast_uchar "qt,mov", ext.ct=cast_uchar "video/quicktime", update_ext(&ext);
ext.ext = cast_uchar "ps,eps,ai", ext.ct=cast_uchar "application/postscript", update_ext(&ext);
ext.ext = cast_uchar "ppt", ext.ct=cast_uchar "application/powerpoint", update_ext(&ext);
ext.ext = cast_uchar "ppm", ext.ct=cast_uchar "image/x-portable-pixmap", update_ext(&ext);
ext.ext = cast_uchar "pnm", ext.ct=cast_uchar "image/x-portable-anymap", update_ext(&ext);
ext.ext = cast_uchar "png", ext.ct=cast_uchar "image/png", update_ext(&ext);
ext.ext = cast_uchar "pgp", ext.ct=cast_uchar "application/pgp-signature", update_ext(&ext);
ext.ext = cast_uchar "pgm", ext.ct=cast_uchar "image/x-portable-graymap", update_ext(&ext);
ext.ext = cast_uchar "pdf", ext.ct=cast_uchar "application/pdf", update_ext(&ext);
ext.ext = cast_uchar "pcb", ext.ct=cast_uchar "application/pcb", update_ext(&ext);
ext.ext = cast_uchar "pbm", ext.ct=cast_uchar "image/x-portable-bitmap", update_ext(&ext);
ext.ext = cast_uchar "mpeg,mpg,mpe", ext.ct=cast_uchar "video/mpeg", update_ext(&ext);
ext.ext = cast_uchar "mp3", ext.ct=cast_uchar "audio/mpeg", update_ext(&ext);
ext.ext = cast_uchar "mid,midi", ext.ct=cast_uchar "audio/midi", update_ext(&ext);
ext.ext = cast_uchar "jpg,jpeg,jpe", ext.ct=cast_uchar "image/jpeg", update_ext(&ext);
ext.ext = cast_uchar "grb", ext.ct=cast_uchar "application/gerber", update_ext(&ext);
ext.ext = cast_uchar "gl", ext.ct=cast_uchar "video/gl", update_ext(&ext);
ext.ext = cast_uchar "gif", ext.ct=cast_uchar "image/gif", update_ext(&ext);
ext.ext = cast_uchar "gbr", ext.ct=cast_uchar "application/gerber", update_ext(&ext);
ext.ext = cast_uchar "g", ext.ct=cast_uchar "application/brlcad", update_ext(&ext);
ext.ext = cast_uchar "fli", ext.ct=cast_uchar "video/fli", update_ext(&ext);
ext.ext = cast_uchar "dxf", ext.ct=cast_uchar "application/dxf", update_ext(&ext);
ext.ext = cast_uchar "dvi", ext.ct=cast_uchar "application/x-dvi", update_ext(&ext);
ext.ext = cast_uchar "dl", ext.ct=cast_uchar "video/dl", update_ext(&ext);
ext.ext = cast_uchar "deb", ext.ct=cast_uchar "application/x-debian-package", update_ext(&ext);
ext.ext = cast_uchar "avif", ext.ct=cast_uchar "image/avif", update_ext(&ext);
ext.ext = cast_uchar "avi", ext.ct=cast_uchar "video/x-msvideo", update_ext(&ext);
ext.ext = cast_uchar "au,snd", ext.ct=cast_uchar "audio/basic", update_ext(&ext);
ext.ext = cast_uchar "aif,aiff,aifc", ext.ct=cast_uchar "audio/x-aiff", update_ext(&ext);
}
/* --------------------------- PROG -----------------------------*/
struct list_head mailto_prog = { &mailto_prog, &mailto_prog };
struct list_head telnet_prog = { &telnet_prog, &telnet_prog };
struct list_head tn3270_prog = { &tn3270_prog, &tn3270_prog };
struct list_head mms_prog = { &mms_prog, &mms_prog };
struct list_head magnet_prog = { &magnet_prog, &magnet_prog };
struct list_head gopher_prog = { &gopher_prog, &gopher_prog };
static int is_in_list(unsigned char *list, unsigned char *str, int l)
{
unsigned char *l2, *l3;
if (!l) return 0;
rep:
while (*list && *list <= ' ') list++;
if (!*list) return 0;
for (l2 = list; *l2 && *l2 != ','; l2++)
;
for (l3 = l2 - 1; l3 >= list && *l3 <= ' '; l3--)
;
l3++;
if (l3 - list == l && !casecmp(str, list, l)) return 1;
list = l2;
if (*list == ',') list++;
goto rep;
}
static unsigned char *canonical_compressed_ext(unsigned char *ext, unsigned char *ext_end)
{
size_t len;
if (!ext_end) ext_end = cast_uchar strchr(cast_const_char ext, 0);
len = ext_end - ext;
if (len == 3 && !casecmp(ext, cast_uchar "tgz", 3)) return cast_uchar "gz";
if (len == 3 && !casecmp(ext, cast_uchar "tbz", 3)) return cast_uchar "bz2";
if (len == 3 && !casecmp(ext, cast_uchar "txz", 3)) return cast_uchar "xz";
if (len == 6 && !casecmp(ext, cast_uchar "tar-gz", 3)) return cast_uchar "gz";
if (len == 7 && !casecmp(ext, cast_uchar "tar-bz2", 3)) return cast_uchar "bz2";
if (len == 6 && !casecmp(ext, cast_uchar "tar-xz", 3)) return cast_uchar "xz";
return NULL;
}
unsigned char *get_compress_by_extension(unsigned char *ext, unsigned char *ext_end)
{
size_t len;
unsigned char *x;
if ((x = canonical_compressed_ext(ext, ext_end))) {
ext = x;
ext_end = cast_uchar strchr(cast_const_char x, 0);
}
len = ext_end - ext;
if (len == 1 && !casecmp(ext, cast_uchar "z", 1)) return cast_uchar "compress";
if (len == 2 && !casecmp(ext, cast_uchar "gz", 2)) return cast_uchar "gzip";
if (len == 2 && !casecmp(ext, cast_uchar "br", 2)) return cast_uchar "br";
if (len == 3 && !casecmp(ext, cast_uchar "zst", 3)) return cast_uchar "zstd";
if (len == 3 && !casecmp(ext, cast_uchar "bz2", 3)) return cast_uchar "bzip2";
if (len == 4 && !casecmp(ext, cast_uchar "lzma", 4)) return cast_uchar "lzma";
if (len == 2 && !casecmp(ext, cast_uchar "xz", 2)) return cast_uchar "lzma2";
if (len == 2 && !casecmp(ext, cast_uchar "lz", 2)) return cast_uchar "lzip";
return NULL;
}
unsigned char *get_content_type_by_extension(unsigned char *url)
{
struct list *l;
struct list_head *ll;
unsigned char *ct, *eod, *ext, *exxt;
int extl, el;
ext = NULL, extl = 0;
if (!(ct = get_url_data(url))) ct = url;
for (eod = ct; *eod && !end_of_dir(url, *eod); eod++)
;
for (; ct < eod; ct++)
if (*ct == '.') {
if (ext) {
if (get_compress_by_extension(ct + 1, eod))
break;
}
ext = ct + 1;
} else if (dir_sep(*ct)) {
ext = NULL;
}
if (ext) while (ext[extl] && ext[extl] != '.' && !dir_sep(ext[extl]) && !end_of_dir(url, ext[extl])) extl++;
if ((extl == 3 && !casecmp(ext, cast_uchar "htm", 3)) ||
(extl == 4 && !casecmp(ext, cast_uchar "html", 4)) ||
(extl == 5 && !casecmp(ext, cast_uchar "xhtml", 5))) return stracpy(cast_uchar "text/html");
foreach(struct list, l, ll, extensions.list_entry) {
struct extension *e = get_struct(l, struct extension, head);
unsigned char *fname = NULL;
if (!(ct = get_url_data(url))) ct = url;
for (; *ct && !end_of_dir(url, *ct); ct++)
if (dir_sep(*ct)) fname = ct + 1;
if (!fname) {
if (is_in_list(e->ext, ext, extl)) return stracpy(e->ct);
} else {
int fnlen = 0;
int x;
while (fname[fnlen] && !end_of_dir(url, fname[fnlen])) fnlen++;
for (x = 0; x < fnlen; x++) if (fname[x] == '.') if (is_in_list(e->ext, fname + x + 1, fnlen - x - 1)) return stracpy(e->ct);
}
}
if ((extl == 3 && !casecmp(ext, cast_uchar "jpg", 3)) ||
(extl == 4 && !casecmp(ext, cast_uchar "pjpg", 4))||
(extl == 4 && !casecmp(ext, cast_uchar "jpeg", 4))||
(extl == 5 && !casecmp(ext, cast_uchar "pjpeg", 5))) return stracpy(cast_uchar "image/jpeg");
if ((extl == 3 && !casecmp(ext, cast_uchar "png", 3))) return stracpy(cast_uchar "image/png");
if ((extl == 3 && !casecmp(ext, cast_uchar "gif", 3))) return stracpy(cast_uchar "image/gif");
if ((extl == 3 && !casecmp(ext, cast_uchar "xbm", 3))) return stracpy(cast_uchar "image/x-xbitmap");
if ((extl == 3 && !casecmp(ext, cast_uchar "tif", 3)) ||
(extl == 4 && !casecmp(ext, cast_uchar "tiff", 4))) return stracpy(cast_uchar "image/tiff");
if ((extl == 3 && !casecmp(ext, cast_uchar "svg", 3))) return stracpy(cast_uchar "image/svg+xml");
if ((extl == 4 && !casecmp(ext, cast_uchar "webp", 4))) return stracpy(cast_uchar "image/webp");
if ((extl == 4 && !casecmp(ext, cast_uchar "avif", 4))) return stracpy(cast_uchar "image/avif");
exxt = init_str(); el = 0;
add_to_str(&exxt, &el, cast_uchar "application/x-");
add_bytes_to_str(&exxt, &el, ext, extl);
foreach(struct list, l, ll, assoc.list_entry) {
struct assoc *a = get_struct(l, struct assoc, head);
if (is_in_list(a->ct, exxt, el))
return exxt;
}
mem_free(exxt);
return NULL;
}
static unsigned char *get_content_type_by_header_and_extension(unsigned char *head, unsigned char *url)
{
unsigned char *ct, *file;
ct = get_content_type_by_extension(url);
if (ct) return ct;
file = get_filename_from_header(head);
if (file) {
ct = get_content_type_by_extension(file);
mem_free(file);
if (ct) return ct;
}
return NULL;
}
static unsigned char *get_extension_by_content_type(unsigned char *ct)
{
struct list *l;
struct list_head *ll;
unsigned char *x, *y;
if (is_html_type(ct)) return stracpy(cast_uchar "html");
foreach(struct list, l, ll, extensions.list_entry) {
struct extension *e = get_struct(l, struct extension, head);
if (!casestrcmp(e->ct, ct)) {
x = stracpy(e->ext);
if ((y = cast_uchar strchr(cast_const_char x, ','))) *y = 0;
return x;
}
}
if (!casestrcmp(ct, cast_uchar "image/jpeg") ||
!casestrcmp(ct, cast_uchar "image/jpg") ||
!casestrcmp(ct, cast_uchar "image/jpe") ||
!casestrcmp(ct, cast_uchar "image/pjpe") ||
!casestrcmp(ct, cast_uchar "image/pjpeg") ||
!casestrcmp(ct, cast_uchar "image/pjpg"))
return stracpy(cast_uchar "jpg");
if (!casestrcmp(ct, cast_uchar "image/png") ||
!casestrcmp(ct, cast_uchar "image/x-png"))
return stracpy(cast_uchar "png");
if (!casestrcmp(ct, cast_uchar "image/gif"))
return stracpy(cast_uchar "gif");
if (!casestrcmp(ct, cast_uchar "image/x-bitmap"))
return stracpy(cast_uchar "xbm");
if (!casestrcmp(ct, cast_uchar "image/tiff") ||
!casestrcmp(ct, cast_uchar "image/tif"))
return stracpy(cast_uchar "tiff");
if (!casestrcmp(ct, cast_uchar "image/svg") ||
!casestrcmp(ct, cast_uchar "image/svg+xml"))
return stracpy(cast_uchar "svg");
if (!casestrcmp(ct, cast_uchar "image/webp"))
return stracpy(cast_uchar "webp");
if (!casestrcmp(ct, cast_uchar "image/avif"))
return stracpy(cast_uchar "avif");
if (!cmpbeg(ct, cast_uchar "application/x-")) {
x = ct + strlen("application/x-");
if (casestrcmp(x, cast_uchar "z") &&
casestrcmp(x, cast_uchar "gz") &&
casestrcmp(x, cast_uchar "gzip") &&
casestrcmp(x, cast_uchar "br") &&
casestrcmp(x, cast_uchar "zst") &&
casestrcmp(x, cast_uchar "bz2") &&
casestrcmp(x, cast_uchar "bzip2") &&
casestrcmp(x, cast_uchar "lzma") &&
casestrcmp(x, cast_uchar "lzma2") &&
casestrcmp(x, cast_uchar "xz") &&
casestrcmp(x, cast_uchar "lz") &&
!strchr(cast_const_char x, '-') &&
strlen(cast_const_char x) <= 4) {
return stracpy(x);
}
}
return NULL;
}
static unsigned char *get_content_encoding_from_content_type(unsigned char *ct)
{
if (!casestrcmp(ct, cast_uchar "application/x-gzip") ||
!casestrcmp(ct, cast_uchar "application/x-tgz") ||
!casestrcmp(ct, cast_uchar "application/x-gtar")) return cast_uchar "gzip";
if (!casestrcmp(ct, cast_uchar "application/x-br")) return cast_uchar "br";
if (!casestrcmp(ct, cast_uchar "application/x-zstd") ||
!casestrcmp(ct, cast_uchar "application/zstd")) return cast_uchar "zstd";
if (!casestrcmp(ct, cast_uchar "application/x-bzip2") ||
!casestrcmp(ct, cast_uchar "application/x-bzip")) return cast_uchar "bzip2";
if (!casestrcmp(ct, cast_uchar "application/x-lzma")) return cast_uchar "lzma";
if (!casestrcmp(ct, cast_uchar "application/x-lzma2") ||
!casestrcmp(ct, cast_uchar "application/x-xz")) return cast_uchar "lzma2";
if (!casestrcmp(ct, cast_uchar "application/x-lz") ||
!casestrcmp(ct, cast_uchar "application/x-lzip")) return cast_uchar "lzip";
return NULL;
}
unsigned char *get_content_type(unsigned char *head, unsigned char *url)
{
unsigned char *ct;
int code;
if ((ct = parse_http_header(head, cast_uchar "Content-Type", NULL))) {
unsigned char *s;
if ((s = cast_uchar strchr(cast_const_char ct, ';'))) *s = 0;
while (*ct && ct[strlen(cast_const_char ct) - 1] <= ' ') ct[strlen(cast_const_char ct) - 1] = 0;
if (*ct == '"' && ct[1] && ct[strlen(cast_const_char ct) - 1] == '"') {
memmove(ct, ct + 1, strlen(cast_const_char ct));
ct[strlen(cast_const_char ct) - 1] = 0;
}
if (!casestrcmp(ct, cast_uchar "text/plain") ||
!casestrcmp(ct, cast_uchar "application/octet-stream") ||
!casestrcmp(ct, cast_uchar "application/octetstream") ||
!casestrcmp(ct, cast_uchar "application/octet_stream") ||
!casestrcmp(ct, cast_uchar "application/binary") ||
!casestrcmp(ct, cast_uchar "application/x-www-form-urlencoded") ||
get_content_encoding_from_content_type(ct)) {
unsigned char *ctt;
if (!get_http_code(head, &code, NULL) && code >= 300)
goto no_code_by_extension;
ctt = get_content_type_by_header_and_extension(head, url);
if (ctt) {
mem_free(ct);
return ctt;
}
}
no_code_by_extension:
if (!*ct) mem_free(ct);
else return ct;
}
if (!get_http_code(head, &code, NULL) && code >= 300)
return stracpy(cast_uchar "text/html");
ct = get_content_type_by_header_and_extension(head, url);
if (ct) return ct;
return !force_html ? stracpy(cast_uchar "text/plain") : stracpy(cast_uchar "text/html");
}
unsigned char *get_content_encoding(unsigned char *head, unsigned char *url, int just_ce)
{
unsigned char *ce, *ct, *ext, *extd;
unsigned char *u;
int code;
if ((ce = parse_http_header(head, cast_uchar "Content-Encoding", NULL))) return ce;
if (just_ce) return NULL;
if ((ct = parse_http_header(head, cast_uchar "Content-Type", NULL))) {
unsigned char *s;
if ((s = cast_uchar strchr(cast_const_char ct, ';'))) *s = 0;
ce = get_content_encoding_from_content_type(ct);
if (ce) {
mem_free(ct);
return stracpy(ce);
}
if (is_html_type(ct)) {
mem_free(ct);
return NULL;
}
mem_free(ct);
}
if (!get_http_code(head, &code, NULL) && code >= 300) return NULL;
if (!(ext = get_url_data(url))) ext = url;
for (u = ext; *u; u++) if (end_of_dir(url, *u)) goto skip_ext;
extd = cast_uchar strrchr(cast_const_char ext, '.');
if (extd) {
ce = get_compress_by_extension(extd + 1, cast_uchar strchr(cast_const_char(extd + 1), 0));
if (ce) return stracpy(ce);
}
skip_ext:
if ((ext = get_filename_from_header(head))) {
extd = cast_uchar strrchr(cast_const_char ext, '.');
if (extd) {
ce = get_compress_by_extension(extd + 1, cast_uchar strchr(cast_const_char(extd + 1), 0));
if (ce) {
mem_free(ext);
return stracpy(ce);
}
}
mem_free(ext);
}
return NULL;
}
unsigned char *encoding_2_extension(unsigned char *encoding)
{
if (!casestrcmp(encoding, cast_uchar "gzip") ||
!casestrcmp(encoding, cast_uchar "x-gzip")) return cast_uchar "gz";
if (!casestrcmp(encoding, cast_uchar "compress") ||
!casestrcmp(encoding, cast_uchar "x-compress")) return cast_uchar "Z";
if (!casestrcmp(encoding, cast_uchar "br")) return cast_uchar "br";
if (!casestrcmp(encoding, cast_uchar "zstd")) return cast_uchar "zst";
if (!casestrcmp(encoding, cast_uchar "bzip2")) return cast_uchar "bz2";
if (!casestrcmp(encoding, cast_uchar "lzma")) return cast_uchar "lzma";
if (!casestrcmp(encoding, cast_uchar "lzma2")) return cast_uchar "xz";
if (!casestrcmp(encoding, cast_uchar "lzip")) return cast_uchar "lz";
return NULL;
}
/* returns field with associations */
struct assoc *get_type_assoc(struct terminal *term, unsigned char *type, int *n)
{
struct assoc *assoc_array;
struct list *l;
struct list_head *ll;
int count=0;
foreach(struct list, l, ll, assoc.list_entry) {
struct assoc *a = get_struct(l, struct assoc, head);
if (a->system == SYSTEM_ID && (term->environment & ENV_XWIN ? a->xwin : a->cons) && is_in_list(a->ct, type, (int)strlen(cast_const_char type))) {
if (count == MAXINT) overalloc();
count++;
}
}
*n = count;
if (!count) return NULL;
if ((unsigned)count > MAXINT / sizeof(struct assoc)) overalloc();
assoc_array = mem_alloc(count * sizeof(struct assoc));
count = 0;
foreach(struct list, l, ll, assoc.list_entry) {
struct assoc *a = get_struct(l, struct assoc, head);
if (a->system == SYSTEM_ID && (term->environment & ENV_XWIN ? a->xwin : a->cons) && is_in_list(a->ct, type, (int)strlen(cast_const_char type))) {
assoc_array[count++] = *a;
}
}
return assoc_array;
}
int is_html_type(unsigned char *ct)
{
return !casestrcmp(ct, cast_uchar "text/html") ||
!casestrcmp(ct, cast_uchar "text-html") ||
!casestrcmp(ct, cast_uchar "text/x-server-parsed-html") ||
!casestrcmp(ct, cast_uchar "text/xml") ||
!casecmp(ct, cast_uchar "application/xhtml", strlen("application/xhtml"));
}
unsigned char *get_filename_from_header(unsigned char *head)
{
int extended = 0;
unsigned char *ct, *x, *y, *codepage;
int ly;
int cp_idx;
if ((ct = parse_http_header(head, cast_uchar "Content-Disposition", NULL))) {
x = parse_header_param(ct, cast_uchar "filename*", 1);
if (x) extended = 1;
else x = parse_header_param(ct, cast_uchar "filename", 1);
mem_free(ct);
if (x) {
if (*x) goto ret_x;
mem_free(x);
}
}
if ((ct = parse_http_header(head, cast_uchar "Content-Type", NULL))) {
x = parse_header_param(ct, cast_uchar "name*", 0);
if (x) extended = 1;
else x = parse_header_param(ct, cast_uchar "name", 0);
mem_free(ct);
if (x) {
if (*x) goto ret_x;
mem_free(x);
}
}
return NULL;
ret_x:
codepage = NULL;
if (extended) {
unsigned char *ap1, *ap2;
ap1 = cast_uchar strchr(cast_const_char x, '\'');
if (!ap1)
goto no_extended;
ap2 = cast_uchar strchr(cast_const_char (ap1 + 1), '\'');
if (ap2) ap2++;
else ap2 = ap1 + 1;
codepage = memacpy(x, ap1 - x);
memmove(x, ap2, strlen(cast_const_char ap2) + 1);
}
no_extended:
y = init_str();
ly = 0;
add_conv_str(&y, &ly, x, (int)strlen(cast_const_char x), -2);
mem_free(x);
x = y;
cp_idx = -1;
if (codepage) {
cp_idx = get_cp_index(codepage);
mem_free(codepage);
}
if (cp_idx < 0) {
cp_idx = get_cp_index(cast_uchar "iso-8859-1");
if (cp_idx < 0) cp_idx = 0;
}
y = convert(cp_idx, 0, x, NULL);
mem_free(x);
x = y;
for (y = x; *y; y++) if (dir_sep(*y)
#if defined(DOS_FS) || defined(SPAD)
|| *y == ':'
#endif
) *y = '-';
return x;
}
unsigned char *get_filename_from_url(unsigned char *url, unsigned char *head, int tmp)
{
int ll = 0;
unsigned char *u, *s, *e, *f, *x, *ww;
unsigned char *ct, *want_ext;
if (!casecmp(url, cast_uchar "data:", 5)) {
url = cast_uchar "data:/data";
}
want_ext = stracpy(cast_uchar "");
f = get_filename_from_header(head);
if (f) {
goto no_ct;
}
if (!(u = get_url_data(url))) u = url;
for (e = s = u; *e && !end_of_dir(url, *e); e++) {
if (dir_sep(*e)) s = e + 1;
}
ll = 0;
f = init_str();
add_conv_str(&f, &ll, s, (int)(e - s), -2);
if (!(ct = parse_http_header(head, cast_uchar "Content-Type", NULL))) goto no_ct;
mem_free(ct);
ct = get_content_type(head, url);
if (ct) {
x = get_extension_by_content_type(ct);
if (x) {
add_to_strn(&want_ext, cast_uchar ".");
add_to_strn(&want_ext, x);
mem_free(x);
}
mem_free(ct);
}
no_ct:
if (!*want_ext) {
x = cast_uchar strrchr(cast_const_char f, '.');
if (x) {
mem_free(want_ext);
want_ext = stracpy(x);
}
}
ct = get_content_encoding(head, url, 0);
if (ct) {
x = encoding_2_extension(ct);
if (!tmp) {
unsigned char *ct1;
ct1 = get_content_encoding(head, url, 1);
if (ct1) {
mem_free(ct1);
} else if (x) {
unsigned char *w = cast_uchar strrchr(cast_const_char want_ext, '.');
if (w && (ww = canonical_compressed_ext(w + 1, NULL)) && !casestrcmp(x, ww))
goto skip_want_ext;
if (w && !casestrcmp(w + 1, x))
goto skip_want_ext;
add_to_strn(&want_ext, cast_uchar ".");
add_to_strn(&want_ext, x);
skip_want_ext:;
}
} else {
if (x) {
if (strlen(cast_const_char x) + 1 < strlen(cast_const_char f) && f[strlen(cast_const_char f) - strlen(cast_const_char x) - 1] == '.' && !casestrcmp(f + strlen(cast_const_char f) - strlen(cast_const_char x), x)) {
f[strlen(cast_const_char f) - strlen(cast_const_char x) - 1] = 0;
}
}
}
mem_free(ct);
}
if (strlen(cast_const_char want_ext) > strlen(cast_const_char f) || casestrcmp(want_ext, f + strlen(cast_const_char f) - strlen(cast_const_char want_ext))) {
x = cast_uchar strrchr(cast_const_char f, '.');
if (x && (ww = canonical_compressed_ext(x + 1, NULL)) && want_ext[0] == '.' && !casestrcmp(want_ext + 1, ww))
goto skip_tgz_2;
if (x) *x = 0;
add_to_strn(&f, want_ext);
skip_tgz_2:;
}
mem_free(want_ext);
return f;
}
static void free_prog_list(struct list_head *l)
{
struct list_head *lp;
struct protocol_program *p;
foreach(struct protocol_program, p, lp, *l)
mem_free(p->prog);
free_list(struct protocol_program, *l);
}
void free_types(void)
{
struct list *l;
struct list_head *ll;
foreach(struct list, l, ll, assoc.list_entry) {
struct assoc *a = get_struct(l, struct assoc, head);
mem_free(a->ct);
mem_free(a->prog);
mem_free(a->label);
ll = ll->prev;
del_from_list(&a->head);
mem_free(a);
}
foreach(struct list, l, ll, extensions.list_entry) {
struct extension *e = get_struct(l, struct extension, head);
mem_free(e->ext);
mem_free(e->ct);
ll = ll->prev;
del_from_list(&e->head);
mem_free(e);
}
free_prog_list(&mailto_prog);
free_prog_list(&telnet_prog);
free_prog_list(&tn3270_prog);
free_prog_list(&mms_prog);
free_prog_list(&magnet_prog);
free_prog_list(&gopher_prog);
free_history(ext_search_history);
free_history(assoc_search_history);
}
|