File: mozplugger.c

package info (click to toggle)
mozplugger 1.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 416 kB
  • ctags: 1,269
  • sloc: ansic: 4,849; makefile: 178
file content (1784 lines) | stat: -rw-r--r-- 51,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
/*****************************************************************************
 * Authors: Louis Bavoil <bavoil@cs.utah.edu>
 *          Peter Leese <peter@leese.net>
 *          Fredrik Hbinette <hubbe@hubbe.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "mozplugger.h"

#define THIS ((data_t *)(instance->pdata))

#ifndef __GNUC__
#define __inline
#endif

/*****************************************************************************
 * Type declarations
 *****************************************************************************/
typedef struct argument
{
     char *name;
     char *value;
} argument_t;

typedef struct data
{
     Display *display;
     char *displayname;
     NPWindow windata;
     pid_t pid;
     int fd;
     int repeats;
     int cmd_flags;
     const char *command;
     const char *winname;
     int mode_flags;
     char *mimetype;
     char *href;
     char *mms;
     char autostart;
     char autostartNotSeen;
     int num_arguments;
     struct argument *args;
} data_t;

typedef struct mimetype
{
     const char * type;
} mimetype_t;

typedef struct command
{
     int flags;
     const char * cmd;
     const char * winname;
     const char * fmatchStr;
} command_t;

typedef struct handle
{
     int num_types;
     int num_cmds;
     mimetype_t types[MAX_NUM_TYPES_PER_HANDLER];
     command_t cmds[MAX_NUM_CMDS_PER_HANDLER];
} handler_t;

typedef struct
{
     const char *name;
     int value;
} flag_t;

/*****************************************************************************
 * Global variables
 *****************************************************************************/
static const char *config_fname;
static const char *helper_fname;
static const char *controller_fname;
static int num_handlers;
static handler_t handlers[MAX_NUM_HANDLERS];
static int browserApiMajorVer;
static int browserApiMinorVer;

/*****************************************************************************
 * Wrapper for fork() which handles some magic needed to prevent netscape
 * from freaking out. It also prevents interference from signals.
 * Also it closes all those file descriptors inherited from the parent 
 * (except STDIN, STDOUT & STDERR (0,1,2))
 *
 *****************************************************************************/
static pid_t my_fork(NPP instance, int pipeFd)
{
     pid_t pid;
     sigset_t set;
     sigset_t oset;
     int maxFds;

     /* Mask all the signals to avoid being interrupted by a signal */
     sigfillset(&set);
     sigprocmask(SIG_SETMASK, &set, &oset);

#if 0      /* If sysconf not support do this? */
     {
          struct rlimit rl;
          if (getrlimit(RLIMIT_NOFILE, &fdl) < 0)
          {
               maxFds = 256;
          }
          else if (rl.rlim_max == RLIM_INFINITY)
          {
               maxFds = 1024;
          }
          else
          {
              maxFds = rl.rlim_max;
          }
     }
#else
     maxFds = sysconf(_SC_OPEN_MAX);
#endif

     D(">>>>>>>>Forking<<<<<<<<,\n");

     if ((pid = fork()) == -1)
     {
	  return -1;
     }

     if (pid == 0)
     {
          int i;
          int signum;

	  alarm(0);

	  if (!(THIS->cmd_flags & H_DAEMON))
	       setsid();

	  for (signum=0; signum < NSIG; signum++)
	       signal(signum, SIG_DFL);

          /* Close all those File descriptors inherited from the
             parent (except STDIN, STDOUT, STDERR and pipeFd. */
#ifdef DEBUG
	  close_debug();
#endif
          D("Closing up to %i Fds\n", maxFds);
          for (i = 3; i < pipeFd; i++)
          {
               close(i);
          }
          for (i = pipeFd+1; i < maxFds; i++)
          {
               close(i);
          }

#ifdef DEBUG
	  D("Child re-opened ndebug\n");
#endif
     }
     else
     {
	  D("Child running with pid=%d\n", pid);
     }

     /* Restore the signal mask */
     sigprocmask(SIG_SETMASK, &oset, &set);

     return pid;
}

/*****************************************************************************
 * Wrapper for putenv().
 *****************************************************************************/
static void my_putenv(char *buffer, int *offset, const char *var, 
                                                 const char *value)
{
     int l = strlen(var) + strlen(value) + 2;
     if (*offset + l >= ENV_BUFFER_SIZE)
     {
	  D("Buffer overflow in putenv(%s=%s)\n", var, value);
	  return;
     }
     
     snprintf(buffer+*offset, l, "%s=%s", var, value);
     putenv(buffer+*offset);
     *offset += l;
}

/*****************************************************************************
 * Wrapper for execlp() that calls the helper.
 *
 * WARNING: This function runs in the daughter process so one must assume the
 * daughter uses a copy (including) heap memory of the parent's memory space
 * i.e. any write to memory here does not affect the parent memory space. 
 * Since Linux uses copy-on-write, best leave memory read-only and once execlp
 * is called, all daughter memory is wiped anyway (except the stack).
 *****************************************************************************/
static void run(NPP instance, const char *file, int fd)
{
     char buffer[ENV_BUFFER_SIZE];
     char foo[SMALL_BUFFER_SIZE];
     int offset = 0;
     int i;
     int flags = THIS->cmd_flags;
     int autostart = THIS->autostart;

     /* Specially linking objects to external app must use controls */
     if (THIS->mode_flags & H_LINKS)
	  flags |= H_CONTROLS;

     /* If no autostart seen and using controls, dont autostart by default */
     if ((flags & H_CONTROLS) && (THIS->autostartNotSeen))
	  autostart = 0;

     snprintf(buffer, sizeof(buffer), "%d,%d,%d,%lu,%d,%d,%d,%d",
	      flags,
	      THIS->repeats,
	      fd,
	      (unsigned long int) THIS->windata.window,
	      (int) THIS->windata.x,
	      (int) THIS->windata.y,
	      (int) THIS->windata.width,
	      (int) THIS->windata.height);

     D("Executing helper: %s %s %s %s %s %s\n",
       helper_fname,
       buffer,
       file,
       THIS->displayname,
       THIS->command,
       THIS->mimetype);

     offset = strlen(buffer)+1;

     snprintf(foo, sizeof(foo), "%lu", (long unsigned)THIS->windata.window);
     my_putenv(buffer, &offset, "window", foo);

     snprintf(foo, sizeof(foo), "0x%lx", (long unsigned)THIS->windata.window);
     my_putenv(buffer, &offset, "hexwindow", foo);

     snprintf(foo, sizeof(foo), "%ld", (long)THIS->repeats);
     my_putenv(buffer, &offset, "repeats", foo);

     snprintf(foo, sizeof(foo), "%ld", (long)THIS->windata.width);
     my_putenv(buffer, &offset, "width", foo);

     snprintf(foo, sizeof(foo), "%ld", (long)THIS->windata.height);
     my_putenv(buffer, &offset, "height", foo);

     my_putenv(buffer, &offset, "mimetype", THIS->mimetype);
     
     my_putenv(buffer, &offset, "file", file);

     my_putenv(buffer, &offset, "autostart", autostart ? "1" : "0");
      
     if (THIS->winname)
	  my_putenv(buffer, &offset, "winname", THIS->winname);
     
     if (THIS->displayname)
	  my_putenv(buffer, &offset, "DISPLAY", THIS->displayname);

     for (i = 0; i < THIS->num_arguments; i++)
	  if (THIS->args[i].value)
	       my_putenv(buffer, &offset, THIS->args[i].name, THIS->args[i].value);

     if(flags & H_CONTROLS)
     {
          execlp(controller_fname, helper_fname, buffer, THIS->command, NULL);
     }
     else
     {
          execlp(helper_fname, helper_fname, buffer, THIS->command, NULL);
     }
     D("EXECLP FAILED!\n");
     
     _exit(EX_UNAVAILABLE);
}

/*****************************************************************************
 * Check if 'file' is somewhere to be found in the user PATH.
 *****************************************************************************/
static int inpath(char *path, const char *file)
{
     static struct stat filestat;
     static char buf[1024];
     int i;
     int count = 1;

     for (i = strlen(path)-1; i > 0; i--)
     {
	  if (path[i] == ':')
	  {
	       path[i] = 0;
	       count++;
	  }
     }

     for (i = 0; i < count; i++)
     {
	  snprintf(buf, sizeof(buf), "%s/%s", path, file);
	  D("stat(%s)\n", buf);
	  if (stat(buf, &filestat) == 0)
	  {
	       D("yes\n");
	       return 1;
	  }
	  D("no\n");
	  path += (strlen(path) + 1);
     }

     return 0;
}

typedef struct 
{
    char name[SMALL_BUFFER_SIZE];
    short exists;
} cacheEntry_t;

/*****************************************************************************
 * Check if 'file' exists. Uses a cache of previous finds to avoid performing
 * a stat call on the same file over and over again.
 *****************************************************************************/
static int find(const char *file)
{
     static cacheEntry_t cache[FIND_CACHE_SIZE];
     static int cacheSize = 0;
     static int cacheHead = 0;

     struct stat filestat;
     int i;
     int exists;

     D("find(%s)\n", file);

     for(i = 0; i < cacheSize; i++)
     {
         if( strcmp(cache[i].name, file) == 0)
         {
             const int exists = cache[i].exists;
             D("find cache hit exists = %s\n", exists ? "yes" : "no");
             return exists;
         }
     }

     if (file[0] == '/')
     {
	  exists = (stat(file, &filestat) == 0);
     }
     else
     {
          char *path;
          char *env_path;

          /* Get the environment variable PATH */
          if (!(env_path = getenv("PATH")))
          {
	       D("No PATH !\n");
	       exists = 0;
          }
          else
          {
               /* Duplicate to avoid corrupting Mozilla's PATH */
               path = strdup(env_path);
               exists = inpath(path, file);
               free(path);
          }
     }

     strncpy(cache[cacheHead].name, file, SMALL_BUFFER_SIZE);
     cache[cacheHead].name[SMALL_BUFFER_SIZE-1] = '\0';
     cache[cacheHead].exists = exists;

     cacheHead++;
     if(cacheHead > cacheSize)
     {
          cacheSize = cacheHead;
     }
     if(cacheHead >= FIND_CACHE_SIZE)      /* Wrap around the head */
     {
          cacheHead = 0;
     }
     return exists;
}

/*****************************************************************************
 * Delete a mime handler if no command has been found for it.
 *****************************************************************************/
static void filter_previous_handler(void)
{
     handler_t *h;
     if (num_handlers >= 1)
     {
	  h = &handlers[num_handlers-1];
	  if (h->num_cmds == 0)
	  {
	       D("Empty handler: '%s'.\n", h->types[0].type);
	       h->num_types = 0;
	       num_handlers--;
	  }
     }
}

static char staticPool[MAX_STATIC_MEMORY_POOL];
static int  staticPoolIdx = 0;

/*****************************************************************************
 * Allocate some memory from the static pool. We use a static pool for
 * the database because Mozilla can unload the plugin after use and this
 * would lead to memory leaks if we use the heap.
 *****************************************************************************/
static void * allocStaticMem(int size)
{
     void * retVal; 
     const int newIdx = staticPoolIdx + size;

     if(newIdx > MAX_STATIC_MEMORY_POOL)
     {
	  fprintf(stderr, "MozPlugger: Too many entries in mozpluggerc\n");
	  exit(1);
     }

     retVal = &staticPool[staticPoolIdx];
     staticPoolIdx = newIdx;
     return retVal;
}

/*****************************************************************************
 * Given a pointer to a string in temporary memory, return the same string
 * but this time stored in permanent (i.e. static) memory. Will only be deleted
 * when the plugin is unloaded by Mozilla.
 *****************************************************************************/
static const char * makeStrStatic(const char * str, int len)
{
    /* plus one for string terminator */
    char * const buf = allocStaticMem(len + 1);

    strncpy(buf, str, len);
    buf[len] ='\0';
    return (const char *) buf;
}


/*****************************************************************************
 * Match a word to a flag that takes a parameter e.g. swallow(name).
 *****************************************************************************/
static char *get_parameter(char *x, const char *flag, const char **c)
{
     char *end;
     int len;
 
     /* skip spaces or tabs between flag name and parameter */
     while ((*x == ' ' || *x == '\t')) 
         x++;

     if (*x != '(')
     {
	  fprintf(stderr, 
                  "MozPlugger: Warning: Expected '(' after '%s'\n", flag);
	  return NULL;
     }
     x++;
     end = strchr(x,')');
     if (end)
     {
	  len = end - x;
	  *c = makeStrStatic(x, len);
	  x = end+1;
     }
     return x;
}

/*****************************************************************************
 * Match two words.
 *****************************************************************************/
__inline
static int match_word(const char *line, const char *kw)
{
     return !strncasecmp(line, kw, strlen(kw)) && !isalnum(line[strlen(kw)]);
}

/*****************************************************************************
 * Scan a line for all the possible flags.
 *****************************************************************************/
static int parse_flags(char **x, command_t *commandp)
{
     static flag_t flags[] = {
	  { "repeat", 		H_REPEATCOUNT 	},
	  { "loop", 		H_LOOP 		},
	  { "stream", 		H_STREAM 	},
	  { "ignore_errors",	H_IGNORE_ERRORS },
	  { "exits", 		H_DAEMON 	},
	  { "nokill", 		H_DAEMON 	},
	  { "maxaspect",	H_MAXASPECT 	},
	  { "fill",		H_FILL 		},
	  { "noisy",		H_NOISY 	},
	  { "embed",            H_EMBED         },
	  { "noembed",          H_NOEMBED       },
          { "links",            H_LINKS         },
	  { "hidden",           H_HIDDEN        },
          { "controls",         H_CONTROLS      },
          { "swallow",          H_SWALLOW       },
          { "fmatch",           H_FMATCH        },
	  { NULL, 		0 		}
     };
     flag_t *f;

     for (f = flags; f->name; f++)
     {
	  if (match_word(*x, f->name))
	  {
	       *x += strlen(f->name);
	       commandp->flags |= f->value;

               /* Get parameter for thoses commands with parameters */
               if(f->value & H_SWALLOW)
               {
	            if ((*x = get_parameter(*x, f->name, &commandp->winname)))
                        return 1;
               }
               else if(f->value & H_FMATCH)
               {
	            if ((*x = get_parameter(*x, f->name, &commandp->fmatchStr)))
                        return 1;
               }
               else
	            return 1;
	  }
     }
     return 0;
}

/*****************************************************************************
 * Read the configuration file into memory.
 *****************************************************************************/
static void read_config(FILE *f)
{
     handler_t *handler = NULL;
     command_t *cmd = NULL;
     mimetype_t *type = NULL;
     char buffer[LARGE_BUFFER_SIZE];
     char file[SMALL_BUFFER_SIZE];
     int have_commands = 1;

     D("read_config\n");

     while (fgets(buffer, sizeof(buffer), f))
     {
	  D("::: %s", buffer);

	  if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\0')
	       continue;

	  if (buffer[strlen(buffer)-1] == '\n')
	       buffer[strlen(buffer)-1] = 0;

	  if (!isspace(buffer[0]))
	  {
	       /* Mime type */

	       if (have_commands)
	       {
		    D("-------------------------------------------\n");
		    D("New handler\n");
		    D("-------------------------------------------\n");

		    filter_previous_handler();

		    handler = &handlers[num_handlers++];

		    if (!(num_handlers < MAX_NUM_HANDLERS))
		    {
			 fprintf(stderr, "MozPlugger: Error: Too many handlers (%d)\n", num_handlers);
			 exit(1);
		    }

		    have_commands = 0;
	       }

	       D("New mime type\n");

	       type = &handler->types[handler->num_types++];

	       if (!(handler->num_types < MAX_NUM_TYPES_PER_HANDLER))
	       {
		    fprintf(stderr, "MozPlugger: Error: Too many types (%d) for handler %d (%s)\n",
			    handler->num_types, num_handlers, handler->types[0].type);
		    exit(1);
	       }
	       type->type = makeStrStatic(buffer, strlen(buffer));
	  }
	  else
	  {
	       /* Command */

	       char *x = buffer + 1;
	       while (isspace(*x)) x++;
	       if (!*x)
	       {
		    D("Empty command.\n");
		    have_commands++;
		    continue;
	       }

	       D("New command\n");

	       have_commands++;

	       cmd = &handler->cmds[handler->num_cmds];
	       memset(cmd, 0, sizeof(command_t));

	       D("Parsing %s\n", x);

	       while (*x != ':' && *x)
	       {
		    if (*x == ',' || *x == ' ' || *x == '\t')
		    {
			 x++;
		    }
		    else if (!parse_flags(&x, cmd))
		    {
			 fprintf(stderr, "MozPlugger: Warning: Unknown directive: %s\n", x);
			 x += strlen(x);
		    }
	       }
	       if (*x == ':')
	       {
		    x++;
		    while (isspace(*x)) x++;

		    if (sscanf(x, "if %"SMALL_BUFFER_SIZE_STR"s", file) != 1
			&& sscanf(x, "%"SMALL_BUFFER_SIZE_STR"s", file) != 1)
			 continue;

		    if (!find(file))
			 continue;

		    cmd->cmd = makeStrStatic(x, strlen(x));
	       }
	       else
	       {
		    D("No column? (%s)\n", x);
	       }

	       handler->num_cmds++;

	       if (!(handler->num_cmds < MAX_NUM_CMDS_PER_HANDLER))
	       {
		    fprintf(stderr, "MozPlugger: Error: Too many commands (%d) for handler %d (%s)\n",
			    handler->num_cmds, num_handlers, handler->types[0].type);
		    exit(1);
	       }
	  }
     }

     filter_previous_handler();

     D("Num handlers: %d\n", num_handlers);
}

/*****************************************************************************
 * Find the config file or the helper file in function of the function cb.
 *****************************************************************************/
static int find_helper_file(const char *basename, int (*cb)(const char *))
{
     static char fname[LARGE_BUFFER_SIZE];
     char *tmp;

     D("find_helper_file '%s'\n", basename);

     if ((tmp = getenv("HOME")))
     {
	  snprintf(fname, sizeof(fname), "%s/.netscape/%s", tmp, basename);
	  if (cb(fname)) return 1;

	  snprintf(fname, sizeof(fname), "%s/.mozilla/%s", tmp, basename);
	  if (cb(fname)) return 1;

	  snprintf(fname, sizeof(fname), "%s/.opera/%s", tmp, basename);
	  if (cb(fname)) return 1;
     }

     if ((tmp = getenv("MOZILLA_HOME")))
     {
	  snprintf(fname, sizeof(fname), "%s/%s", tmp, basename);
	  if (cb(fname)) return 1;
     }

     if ((tmp = getenv("OPERA_DIR")))
     {
	  snprintf(fname, sizeof(fname), "%s/%s", tmp, basename);
	  if (cb(fname)) return 1;
     }

     snprintf(fname, sizeof(fname), "/etc/%s", basename);
     if (cb(fname)) return 1;

     snprintf(fname, sizeof(fname), "/usr/etc/%s", basename);
     if (cb(fname)) return 1;

     snprintf(fname, sizeof(fname), "/usr/local/mozilla/%s", basename);
     if (cb(fname)) return 1;

     snprintf(fname, sizeof(fname), "/usr/local/netscape/%s", basename);
     if (cb(fname)) return 1;

     if (cb(basename)) return 1;
  
     return 0;
}

/*****************************************************************************
 * Open and run the configuration file (fname) through M4 and then call 
 * read_config() to parse the output
 ****************************************************************************/
static int read_config_cb(const char *fname)
{
     int m4out[2];
     pid_t pid;
     int fd;
     FILE *fp;

     D("READ_CONFIG(%s)\n", fname);

     fd = open(fname, O_RDONLY);
     if (fd < 0) return 0;

     if (pipe(m4out) < 0)
     {
	  perror("pipe");
	  return 0;
     }

     if ((pid = fork()) == -1)
	  return 0;

     if (!pid)
     {
	  close(m4out[0]);

	  dup2(m4out[1], 1);
	  close(m4out[1]);

	  dup2(fd, 0);
	  close(fd);

	  execlp("m4", "m4", NULL);
	  fprintf(stderr, "MozPlugger: Error: Failed to execute m4.\n");
	  exit(1);
     }
     else
     {
	  close(m4out[1]);
	  close(fd);
	  
	  fp = fdopen(m4out[0], "r");
	  if (!fp) return 0;
	  read_config(fp);
	  fclose(fp);
	  
	  waitpid(pid, 0, 0);
	  config_fname = makeStrStatic(fname, strlen(fname));

     }
     return 1;
}

/*****************************************************************************
 * Check if the helper executable exists (using stat), if so save the filename
 * to the global 'helper_fname' variable
 *****************************************************************************/
static int find_plugger_helper_cb(const char *fname)
{
     struct stat buf;
     if (stat(fname, &buf) != 0) 
     {
          return 0;
     }
     helper_fname = makeStrStatic(fname, strlen(fname));
     return 1;
}

/*****************************************************************************
 * Check if the controller executable exists (using stat), if so save the
 * filename to the global 'controller_fname' variable
 *****************************************************************************/
static int find_plugger_controller_cb(const char *fname)
{
     struct stat buf;
     if (stat(fname, &buf) != 0) 
     {
          return 0;
     }
     controller_fname = makeStrStatic(fname, strlen(fname));
     return 1;
}

/*****************************************************************************
 * Find configuration file, helper and controller executables. Call the
 * appropriate xxx_cb function to handle the action (e.g. for configuration
 * file, the parsering and reading into memory).
 *****************************************************************************/
static void do_read_config(void)
{
     if (num_handlers > 0) return;

     D("do_read_config\n");
  
     if (!find_helper_file("mozpluggerrc", read_config_cb))
     {
	  fprintf(stderr, "MozPlugger: Warning: Unable to find the mozpluggerrc file.\n");
	  return;
     }

     if (!find_helper_file("mozplugger-helper", find_plugger_helper_cb))
     {
	  if (find("mozplugger-helper")) 
          {
	       helper_fname = "mozplugger-helper";
	  } 
          else 
          { 
	       fprintf(stderr, "MozPlugger: Warning: Unable to find mozplugger-helper.\n");
	       return;
	  }
     }

     if (!find_helper_file("mozplugger-controller", find_plugger_controller_cb))
     {
	  if (find("mozplugger-controller")) 
          {
	       controller_fname = "mozplugger-controller";
	  } 
          else 
          {
	       fprintf(stderr, "MozPlugger: Warning: Unable to find mozplugger-controller.\n");
	       return;
	  }
     }

     D("do_read_config done\n");
}

/*****************************************************************************
 * Since href's are passed to an app as an argument, just check for ways that 
 * a shell can be tricked into executing a command.
 *****************************************************************************/
static int safeURL(const char* url)
{
     int  i = 0; 
     int  len = strlen(url);
    
     if (url[0] == '/')
	  return 0;

     for (i = 0; i < len; i++)
     {
	  if (url[i] == '`' || url[i] == ';')
	  {
	       /* Somebody's trying to do something naughty. */
	       return 0;
	  }
     }
     return 1;
}

/*****************************************************************************
 * See if the URL matches out match criteria.
 *****************************************************************************/
__inline
static int match_url(const char * matchStr, const char * url)
{
     int matchStrLen;
     const char * end;

     switch (matchStr[0])
     {
     case '*':
          /* Does the URL start with the match String */
          matchStr++;     /* Step over the asterisk */
	  return (strncasecmp(matchStr, url, strlen(matchStr)) == 0);

     case '%':
          /* Does the URL end with the match String */
          matchStr++;     /* Step over the percent sign */

          /* Need to find the end of the url, before any 
           * extra params i.e'?=xxx' or '#yyy' */
          end = strchr(url, '?');
          if(end == NULL)
          {
               end = strchr(url, '#');
               if(end == NULL)
               {
                    end = &url[strlen(url)];
               }
          }
          matchStrLen = strlen(matchStr);
          if(end - matchStrLen < url)
          {
               return 0;
          }
	  return (strncasecmp(matchStr, end-matchStrLen, matchStrLen) == 0);
 
     default:
          /* Is the match string anywhere in the URL */
	  return (strstr(url, matchStr) != NULL);
     }
}

/*****************************************************************************
 * Go through the commands in the config file and find one that fits our needs.
 *****************************************************************************/
__inline
static int match_command(NPP instance, int streaming, 
                         command_t *c, const char * url)
{
#define MODE_MASK (H_NOEMBED | H_EMBED | H_LINKS)

     D("Checking command: %s\n", c->cmd);

     /* If command is specific to a particular mode... */
     if (c->flags & MODE_MASK)
     {
          /* Check it matches the current mode... */
          if ( (THIS->mode_flags & MODE_MASK) != (c->flags & MODE_MASK) )
          {
	       D("Flag mismatch: mode different %x != %x\n",
                     THIS->mode_flags & MODE_MASK,  c->flags & MODE_MASK);
	       return 0;
          }
     }

     if ((c->flags & H_LOOP) && (THIS->repeats != MAXINT))
     {
	  D("Flag mismatch: loop\n");
	  return 0;
     }
     if ((!!streaming) != (!!(c->flags & H_STREAM)))
     {
	  D("Flag mismatch: stream\n");
	  return 0;
     }

     if(c->fmatchStr)
     {
          if(!match_url(c->fmatchStr, url))
          {
               D("fmatch mismatch: url '%s' doesnt have '%s'\n", 
                                              url, c->fmatchStr);
               return 0;
          }
     }
     D("Match found!\n");
     return 1;
}

/*****************************************************************************
 * See if mimetype matches.
 *****************************************************************************/
__inline
static int match_mime_type(NPP instance, mimetype_t *m)
{
     char mimetype[SMALL_BUFFER_SIZE];
     sscanf(m->type, "%"SMALL_BUFFER_SIZE_STR"[^:]", mimetype);
     sscanf(mimetype, "%s", mimetype);

     D("Checking '%s' ?= '%s'\n", mimetype, THIS->mimetype);
     if ((strcasecmp(mimetype, THIS->mimetype) != 0) 
             && (strcmp(mimetype,"*") != 0))
     {
	  D("Not same.\n");
	  return 0;
     }

     D("Same.\n");
     return 1;
}

/*****************************************************************************
 * See if handler matches.
 *****************************************************************************/
__inline
static int match_handler(handler_t *h, NPP instance, 
                         int streaming, const char *url)
{
     mimetype_t *m;
     command_t *c;
     int mid, cid;

     D("-------------------------------------------\n");
     D("Commands for this handle at (%p):\n", h->cmds);

     for (mid = 0; mid < h->num_types; mid++)
     {
	  m = &h->types[mid];
	  if (match_mime_type(instance, m))
	  {
	       for (cid = 0; cid < h->num_cmds; cid++)
	       {
		    c = &h->cmds[cid];
		    if (match_command(instance, streaming, c, url))
		    {
			 THIS->cmd_flags = c->flags;
			 THIS->command = c->cmd;
			 THIS->winname = c->winname;
			 return 1;
		    }
	       }
	  }
     }
     return 0;
}

/*****************************************************************************
 * Find the appropriate command
 *****************************************************************************/
static int find_command(NPP instance, int streaming, const char * url)
{
     int hid;

     D("find_command...\n");

     do_read_config();

     for (hid = 0; hid < num_handlers; hid++)
     {
	  if (match_handler(&handlers[hid], instance, streaming, url))
	  {
	       D("Command found.\n");
	       return 1;
	  }
     }

     D("No command found.\n");
     return 0;
}

/*****************************************************************************
 * Construct a MIME Description string for netscape so that mozilla shall know
 * when to call us back.
 *****************************************************************************/
char *NPP_GetMIMEDescription(void)
{
     char *x,*y;
     handler_t *h;
     mimetype_t *m;
     int size_required;
     int hid, mid;

     D("GetMIMEDescription\n");

     do_read_config();

     size_required = 0;

     for (hid = 0; hid < num_handlers; hid++)
     {
	  h = &handlers[hid];
	  for (mid = 0; mid < h->num_types; mid++)
	  {
	       m = &h->types[mid];
	       size_required += strlen(m->type)+1;
	  }
     }

     D("Size required=%d\n", size_required);

     if (!(x = (char *)malloc(size_required+1)))
	  return 0;

     D("Malloc did not fail\n");

     y = x;

     for (hid = 0; hid < num_handlers; hid++)
     {
	  h = &handlers[hid];
	  for (mid = 0; mid < h->num_types; mid++)
	  {
	       m = &h->types[mid];
	       memcpy(y,m->type,strlen(m->type));
	       y+=strlen(m->type);
	       *(y++)=';';
	  }
     }
     if (x != y) y--;
     *(y++) = 0;

     D("Getmimedescription done: %s\n", x);

     return x;
}

/*****************************************************************************
 * Let Mozilla know things about mozplugger.
 *****************************************************************************/
NPError NPP_GetValue(void *instance, NPPVariable variable, void *value)
{
     static char desc_buffer[8192];
     NPError err = NPERR_NO_ERROR;

     D("Getvalue %d\n", variable);
  
     switch (variable)
     {
     case NPPVpluginNameString:
	  D("GET Plugin name\n");
	  *((char **)value) = "MozPlugger "
	       VERSION
	       " handles QuickTime and Windows Media Player Plugin"
	       ;
	  break;

     case NPPVpluginDescriptionString:
	  D("GET Plugin description\n");
	  snprintf(desc_buffer, sizeof(desc_buffer),
		   "MozPlugger version "
		   VERSION
		   ", written by Fredrik H&uuml;binette, Louis Bavoil and Peter Leese.<br>"
		   "For documentation on how to configure mozplugger, "
		   "check the man page. (type <tt>man&nbsp;mozplugger</tt>)"
		   " <table> "
		   " <tr><td>Configuration file:</td><td>%s</td></tr> "
		   " <tr><td>Helper binary:</td><td>%s</td></tr> "
		   " <tr><td>Controller binary:</td><td>%s</td></tr> "
#ifdef DEBUG
		   " <tr><td>Debug file:</td><td><a href=\"file:///home/user/tmp/mozdebug\">$HOME/tmp/mozdebug</a></td></tr> "
#endif
		   " </table> "
		   "<br clear=all>",
		   config_fname ? config_fname : "Not found!",
		   helper_fname ? helper_fname : "Not found!",
		   controller_fname ? controller_fname : "Not found!");
	  *((char **)value) = desc_buffer;
	  break;

     default:
	  err = NPERR_GENERIC_ERROR;
     }
     return err;
}

/*****************************************************************************
 * Convert a string to an integer.
 * The string can be true, false, yes or no.
 *****************************************************************************/
static int my_atoi(const char *s, int my_true, int my_false)
{
     switch (s[0])
     {
     case 't': case 'T': case 'y': case 'Y':
	  return my_true;
     case 'f': case 'F': case 'n': case 'N':
	  return my_false;
     case '0': case '1': case '2': case '3': case '4':
     case '5': case '6': case '7': case '8': case '9':
	  return atoi(s);
     }
     return -1;
}

/*****************************************************************************
 * Initialize another instance of mozplugger. It is important to know
 * that there might be several instances going at one time.
 *****************************************************************************/
NPError NPP_New(NPMIMEType pluginType,
		NPP instance,
		uint16 mode,
		int16 argc,
		char* argn[],
		char* argv[],
		NPSavedData* saved)
{
     int e;

     int src_idx = -1;
     int href_idx = -1;
     int data_idx = -1;
     int alt_idx = -1;
     int autostart_idx = -1;
     int autohref_idx = -1;
     int target_idx = -1;

     char *url = NULL;

     D("NEW (%s) - instance=%p\n", pluginType, instance);

     if (!instance)
     {
	  return NPERR_INVALID_INSTANCE_ERROR;
     }

     if (!pluginType)
     {
	  return NPERR_INVALID_INSTANCE_ERROR;
     }

     instance->pdata = NPN_MemAlloc(sizeof(data_t));
     if (instance->pdata == NULL) 
          return NPERR_OUT_OF_MEMORY_ERROR;


     memset((void *)THIS, 0, sizeof(data_t));

     THIS->windata.window = 0;
     THIS->display = NULL;
     THIS->pid = -1;
     THIS->fd = -1;
     THIS->repeats = 1;
     THIS->autostart = 1;
     THIS->autostartNotSeen = 1;
     if(mode == NP_EMBED)
     {
        THIS->mode_flags = H_EMBED;
     }
     else
     {
        THIS->mode_flags = H_NOEMBED;
     }

     if (!(THIS->mimetype = strdup(pluginType)))
	  return NPERR_OUT_OF_MEMORY_ERROR;

     THIS->num_arguments = argc;
     if (!(THIS->args = (argument_t *)NPN_MemAlloc(
                                          (uint32)(sizeof(argument_t) * argc))))
	  return NPERR_OUT_OF_MEMORY_ERROR;
     
     for (e = 0; e < argc; e++)
     {
	  if (strcasecmp("loop", argn[e]) == 0)
	  {
	       THIS->repeats = my_atoi(argv[e], MAXINT, 1);
	  }
          /* realplayer also uses numloop tag */
          /* windows media player uses playcount */
          else if((strcasecmp("numloop", argn[e]) == 0) ||
                  (strcasecmp("playcount", argn[e]) == 0))
          {
	       THIS->repeats = atoi(argv[e]);
          }
	  else if((strcasecmp("autostart", argn[e]) == 0) ||
	          (strcasecmp("autoplay", argn[e]) == 0))
	  {
               autostart_idx = e;
	  }
	  /* get the index of the src attribute if this is a 'embed' tag */
	  else if (strcasecmp("src", argn[e]) == 0)
	  {
	       src_idx = e;
	  }
	  /* get the index of the data attribute if this is a 'object' tag */
          else if (strcasecmp("data", argn[e]) == 0)
          {
               data_idx = e;
          }
          /* Special case for quicktime. If there's an href or qtsrc attribute,
           * remember it for now */
          else if (((strcasecmp("href", argn[e]) == 0) ||
	            (strcasecmp("qtsrc", argn[e]) == 0)) &&
	            href_idx == -1)
          {
               href_idx = e;
          }
          else if (((strcasecmp("filename", argn[e]) == 0) ||
	            (strcasecmp("url", argn[e]) == 0) ||
	            (strcasecmp("location", argn[e]) == 0)) &&
                    alt_idx == -1)
          {
               alt_idx = e;
          }
          /* Special case for quicktime. If there's an autohref or target
           * attributes remember them for now */
          else if (strcasecmp("target", argn[e]) == 0)
          {
               target_idx = e;
          }
	  else if(strcasecmp("autohref", argn[e]) == 0)
	  {
               autohref_idx = e;
	  }

	  /* copy the flag to put it into the environment later */
	  D("VAR_%s=%s\n", argn[e], argv[e]);
          {
               const int len = strlen(argn[e]) + 5;

    	       if (!(THIS->args[e].name = (char *)malloc(len)))
	             return NPERR_OUT_OF_MEMORY_ERROR;
	       snprintf(THIS->args[e].name, len, "VAR_%s", argn[e]);
 	       THIS->args[e].value = argv[e] ? strdup(argv[e]) : NULL;
          }
     }

     if (src_idx >= 0)
     {
          url = THIS->args[src_idx].value;
          /* Special case for quicktime. If there's an href or qtsrc 
           * attribute, we want that instead of src but we HAVE to
           * have a src first. */
          if (href_idx >= 0)
          {
	       D("Special case QT detected\n");
	       THIS->href = THIS->args[href_idx].value;

               autostart_idx = autohref_idx;

               if(target_idx >= 0)
               {
                   /* One of those clickable Quicktime linking objects! */
                   THIS->mode_flags &= ~(H_EMBED | H_NOEMBED);
                   THIS->mode_flags |= H_LINKS;
               }

          }
     }
     else if (data_idx >= 0)
     {
          D("Looks like an object tag with data attribute\n");
          url = THIS->args[data_idx].value;
     }
     else if (alt_idx >= 0)
     {
          D("Fall-back use alternative tags\n");
          url = THIS->args[alt_idx].value;
     }

     /* Do the autostart check here, AFTER we have processed the QT special
      * case which can change the autostart attribute */
     if(autostart_idx > 0)
     {
	  THIS->autostart = !!my_atoi(argv[autostart_idx], 1, 0);
	  THIS->autostartNotSeen = 0;
     }

     if (url)
     {
          /* Mozilla does not support the following protocols directly and
           * so it never calls NPP_NewStream for these protocols */
	  if(   (strncmp(url, "mms://", 6) == 0)        
             || (strncmp(url, "mmsu://", 7) == 0)    /* MMS over UDP */
             || (strncmp(url, "mmst://", 7) == 0)    /* MMS over TCP */
             || (strncmp(url, "rtsp://", 7) == 0)
             || (strncmp(url, "rtspu://", 8) == 0)   /* RTSP over UDP */
             || (strncmp(url, "rtspt://", 8) == 0))  /* RTSP over TCP */
	  {
	       D("Detected MMS -> url=%s\n", url);
	       THIS->mms = url;
	  }
          else 
          {
               /* For protocols that Mozilla does support, sometimes
                * the browser will call NPP_NewStream straight away, some
                * times it wont (depends on the nature of the tag). So that
                * it works in all cases call NPP_GetURL, this may result
                * in NPP_NewStream() being called twice (i.e. if this is an
                * embed tag with src attribute or object tag with data
                * attribute) */
               if (mode == NP_EMBED)
               {
                    const NPError retVal = NPN_GetURL(instance, url, 0);
                    if(retVal != NPERR_NO_ERROR)
                    {
                         D("NPN_GetURL(%s) failed with %i\n", url, retVal);

	                 fprintf(stderr, "MozPlugger: Warning: Couldn't get"
                                 "%s\n", url);
                    }
               }
          }
     }

     D("New finished\n");

     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * Free data, kill processes, it is time for this instance to die.
 *****************************************************************************/
NPError NPP_Destroy(NPP instance, NPSavedData** save)
{
     int e;

     D("Destroy() - instance=%p\n", instance);

     if (!instance)
	  return NPERR_INVALID_INSTANCE_ERROR;

     if (THIS)
     {
	  /* Kill the mozplugger-helper process and his sons */
 	  if (THIS->pid > 0)
	       my_kill(-THIS->pid);

	  if (THIS->fd > 0)
	       close(THIS->fd);

	  for (e = 0; e < THIS->num_arguments; e++)
	  {
	       free((char *)THIS->args[e].name);
	       free((char *)THIS->args[e].value);
	  }

	  NPN_MemFree((char *)THIS->args);

	  free(THIS->mimetype);
	  THIS->href = NULL;
	  THIS->mms = NULL;

	  NPN_MemFree(instance->pdata);
	  instance->pdata = NULL;
     }

     D("Destroy finished\n");
  
     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * Check that no child is already running before forking one.
 *****************************************************************************/
static void new_child(NPP instance, const char* fname)
{
     int pipe[2];

     D("NEW_CHILD(%s)\n", fname);

     if (!instance || !fname)
	  return;

     if (THIS->pid != -1)
	  return;

     /* Guard against spawning helper if no command! */
     if(THIS->command == 0)
         return;

     if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) < 0)
     {
	  NPN_Status(instance, "MozPlugger: Failed to create a pipe!");
	  return;
     }

     if ((THIS->pid = my_fork(instance, pipe[1])) == -1)
     {
	  NPN_Status(instance, "MozPlugger: Failed to fork helper!");
	  return;
     }
     
     if (!THIS->pid)
     {
	  close(pipe[0]);

	  D("CHILD RUNNING run() [2]\n");
	  run(instance, fname, pipe[1]);

	  _exit(EX_UNAVAILABLE);
     }
     else
     {
	  THIS->fd = pipe[0];
	  close(pipe[1]);
     }
}

/*****************************************************************************
 * Open a new stream.
 * Each instance can only handle one stream at a time.
 *****************************************************************************/
NPError NPP_NewStream(NPP instance,
		      NPMIMEType type,
		      NPStream *stream, 
		      NPBool seekable,
		      uint16 *stype)
{
     char * savedMimetype = NULL;

     D("NewStream() - instance=%p\n", instance);

     if (instance == NULL)
	  return NPERR_INVALID_INSTANCE_ERROR;

     if (THIS->pid != -1)
     {
          D("NewStream() exiting process already running\n");
	  return NPERR_INVALID_INSTANCE_ERROR;
     }

     /* This is a stupid special case and should be coded into
      * mozpluggerc instead... */
     if (!strncasecmp("image/", type, 6) ||
	 !strncasecmp("x-image/", type, 6))
	  THIS->repeats = 1;

     /*  Replace the stream's URL with the URL in THIS->href if it
      *  exists.  Since we're going to be replacing <embed>'s src with
      *  href, we want to make sure that the URL is not going to try
      *  anything dirty. */
     if (THIS->href != NULL && safeURL(THIS->href))
     {
	  D("Replacing SRC with HREF... \n");
	  stream->url = THIS->href;
     }

     D("Mime type %s\n", type);
     D("Url is %s (seekable=%d)\n", stream->url, seekable);

     /* Ocassionally the MIME type here is different to that passed to the
      * NEW function - this is because of either badly configure web server
      * who's HTTP response content-type does not match the mimetype in the
      * preceding embebbed, object or link tag. Or badly constructed embedded
      * tag or ambiguity in the file extension to mime type mapping. Lets
      * first assume the HTTP response was correct and if not fall back to
      * the original tag in the mime type. */
     if(strcmp(type, THIS->mimetype) != 0)
     {
         D("Mismatching mimetype reported, originally was \'%s\' now '\%s' "
           "for url %s\n", THIS->mimetype, type, stream->url);
         savedMimetype = THIS->mimetype;
         THIS->mimetype = strdup(type);
     }

     /* Find a suitable command, fall back to savedMimeType if defined */
     while(!find_command(instance, 1, stream->url) 
                                 && !find_command(instance, 0, stream->url))
     {
          if(savedMimetype)       /* Lets try the saved one */
          {
              free(THIS->mimetype);
              THIS->mimetype = savedMimetype;
              savedMimetype = NULL;
          }
          else 
          {
	      NPN_Status(instance, 
                         "MozPlugger: No appropriate application found.");
	      return NPERR_GENERIC_ERROR;
          }
     }
     free(savedMimetype);     /* discard the saved Mime type */


     if ((THIS->cmd_flags & H_STREAM)
	 && strncasecmp(stream->url, "file:", 5)
	 && strncasecmp(stream->url, "imap:", 5)
	 && strncasecmp(stream->url, "mailbox:", 8))
     {
	  *stype = NP_NORMAL;
	  new_child(instance, stream->url);
     }
     else
     {
	  *stype = NP_ASFILEONLY;
     }

     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * Called after NPP_NewStream if *stype = NP_ASFILEONLY.
 *****************************************************************************/
void NPP_StreamAsFile(NPP instance,
		      NPStream *stream,
		      const char* fname)
{
     D("StreamAsFile() - instance=%p\n", instance);
     new_child(instance, fname);
}

/*****************************************************************************
 * The browser should have resized the window for us, but this function was
 * added because of a bug in Mozilla 1.7 (see Mozdev bug #7734) and
 * https://bugzilla.mozilla.org/show_bug.cgi?id=201158
 *
 * Bug was fixed in Mozilla CVS repositary in version 1.115 of
 * ns4xPluginInstance.cpp (13 Nov 2003), at the time version was 0.13.
 * version 0.14 happened on 14th July 2004
 ****************************************************************************/
static void resize_window(NPP instance)
{
     XSetWindowAttributes attrib;

     /* Mozilla work around not require for versions > 0.13 */
     if((browserApiMajorVer > 0) || (browserApiMinorVer > 13))
         return;

     attrib.override_redirect = True;
     XChangeWindowAttributes(THIS->display, (Window)THIS->windata.window,
			     (unsigned long) CWOverrideRedirect, &attrib);
     
     D("Bug #7734 work around - resizing WIN 0x%x to %dx%d!?\n", 
            THIS->windata.window,
            THIS->windata.width, THIS->windata.height);

     XResizeWindow(THIS->display, (Window)THIS->windata.window,
		   (unsigned) THIS->windata.width, 
                   (unsigned) THIS->windata.height);
}

/*****************************************************************************
 * The browser calls NPP_SetWindow after creating the instance to allow drawing
 * to begin. Subsequent calls to NPP_SetWindow indicate changes in size or 
 * position. If the window handle is set to null, the window is destroyed. In
 * this case, the plug-in must not perform any additional graphics operations
 * on the window and should free any associated resources.
  ***************************************************************************/
NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
     D("SetWindow() - instance=%p\n", instance);

     if (!instance)
	  return NPERR_INVALID_INSTANCE_ERROR;

     if (!window)
	  return NPERR_NO_ERROR;
 
     /* TODO - Should really pass to helper / controller the fact that the
      * window has disappeared! - need to check the consequences first
      * before deleting these 2 lines */
     if (!window->window)
	  return NPERR_NO_ERROR;

     if (!window->ws_info)
	  return NPERR_NO_ERROR;
     
     THIS->display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
     THIS->displayname = XDisplayName(DisplayString(THIS->display));
     THIS->windata = *window;

     if (THIS->mms)
     {
          if(THIS->command == 0)
          {
               /* Can only use streaming commands, as Mozilla cannot handle
                * these types (mms) of urls */
               if (!find_command(instance, 1, THIS->mms))
               {
	           THIS->mms = NULL;
	           NPN_Status(instance, 
                          "MozPlugger: No appropriate application found.");
 	           return NPERR_GENERIC_ERROR;
               }
          }
	  new_child(instance, THIS->mms);
	  THIS->mms = NULL;
	  return NPERR_NO_ERROR;
     }

     if (THIS->fd != -1)
     {
	  D("Writing WIN 0x%x to fd %d\n", window->window, THIS->fd);
	  write(THIS->fd, (char *)window, sizeof(*window));
     }

     resize_window(instance);

     /* In case Mozilla would call NPP_SetWindow() in a loop. */
     usleep(4000);
     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * Called from the Browser when the streaming has been completed by the Browser
 * (the reason code indicates whether this was due to a User action, Network
 * issue or that streaming has been done.
 *****************************************************************************/
NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
{
     D("DestroyStream() - instance=%p\n", instance);
     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * The browser calls this function only once; when the plug-in is loaded, 
 * before the first instance is created. NPP_Initialize tells the plug-in that
 * the browser has loaded it.
 *****************************************************************************/
NPError NPP_Initialize(void)
{
     int pluginApiMajorVer;
     int pluginApiMinorVer;

     NPN_Version(&pluginApiMajorVer,  &pluginApiMinorVer,
                 &browserApiMajorVer, &browserApiMinorVer);

     D("Initialize - API versions plugin=%d.%d Browser=%d.%d\n", 
             pluginApiMajorVer, pluginApiMinorVer,
             browserApiMajorVer, browserApiMinorVer);

     do_read_config();

#ifdef DEBUG
     {
         const int free = MAX_STATIC_MEMORY_POOL - staticPoolIdx;
         D("Static Pool used=%i, free=%i\n", staticPoolIdx, free);
     }
#endif

     return NPERR_NO_ERROR;
}

/*****************************************************************************
 * The browser calls this function once; when the plug-in is loaded. It returns
 * the Java Class associated with this plug-in. In this case there is None.
 *****************************************************************************/
jref NPP_GetJavaClass()
{
     D("GetJavaClass\n");
     return NULL;
}

/*****************************************************************************
 * The browser calls this function just before it unloads the plugin from
 * memory. So this function should do any tidy up - in this case nothing is
 * required.
 *****************************************************************************/
void NPP_Shutdown(void)
{
     D("Shutdown\n");
}

/*****************************************************************************
 * Called when user as requested to print the webpage that contains a visible
 * plug-in. For mozplugger this is ignored.
 *****************************************************************************/
void NPP_Print(NPP instance, NPPrint* printInfo)
{
     D("Print() - instance=%p\n", instance);
}

/*****************************************************************************
 * Called when the Browser wishes to deliver a block of data from a stream to
 * the plugin. Since streaming is handled directly by the application specificed
 * in the configuration file, mozplugger has no need for this data. Here it
 * just pretends the data has been taken by returning 'len' to indicate all 
 * bytes consumed. Actaully this function should never be called by the 
 * Browser.
 *****************************************************************************/
int32 NPP_Write(NPP instance,
		NPStream *stream,
		int32 offset,
		int32 len,
		void *buf)
{
     D("Write(%d) - instance=%p\n", len, instance);
     return len;
}

/*****************************************************************************
 * Browser calls this function before calling NPP_Write to see how much data
 * the plugin is ready to accept. Since streaming is handled directly by the
 * application specified in the configuration file, mozplugger does not need
 * the stream data from the Browser, therefore this fucntion terminates
 * the streaming by calling NPN_DestoryStream.
 *****************************************************************************/
int32 NPP_WriteReady(NPP instance, NPStream *stream)
{
    D("WriteReady() - instance=%p\n", instance);
    if (instance)
    {
	 /* Tell the browser that it can finish with the stream
	    (actually we just wanted the name of the stream!)
	    And not the stream data. */
        NPN_DestroyStream(instance, stream, NPRES_DONE);
    }
    return 0;
}