summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xapps/plugins/doom/d_deh.c166
1 files changed, 93 insertions, 73 deletions
diff --git a/apps/plugins/doom/d_deh.c b/apps/plugins/doom/d_deh.c
index 632146868e..a5aee07848 100755
--- a/apps/plugins/doom/d_deh.c
+++ b/apps/plugins/doom/d_deh.c
@@ -61,42 +61,73 @@ char* strlwr(char* str)
61// (e.g. from wads) 61// (e.g. from wads)
62 62
63typedef struct { 63typedef struct {
64 const byte *inp, *lump; // Pointer to string or FILE 64 const byte *inp; // Pointer to string
65 long size; 65 size_t size; // Bytes remaining in string
66 int fd; // Current file descriptor
66} DEHFILE; 67} DEHFILE;
67 68
68// killough 10/98: emulate IO whether input really comes from a file or not 69// killough 10/98: emulate IO whether input really comes from a file or not
69 70
70char *dehfgets(char *buf, size_t n, DEHFILE *fp) 71char *dehfgets(char *buf, size_t n, DEHFILE *fp)
71{ 72{
72// if (!fp->lump) // If this is a real file, 73 char *p;
73// return (fgets)(buf, n, (FILE *) fp->inp); // return regular fgets 74
74 if (!n || !*fp->inp || fp->size<=0) // If no more characters 75 if (fp->fd >= 0)
76 { // If this is a real file,
77 int r = read_line(fp->fd, buf, (unsigned)n); // return regular line read
78 return (r > 0) ? buf : NULL;
79 }
80
81 n = MIN(fp->size, n);
82
83 if (n == 0 || *fp->inp == '\0') // If no more characters
75 return NULL; 84 return NULL;
76 if (n==1) 85
77 fp->size--, *buf = *fp->inp++; 86 p = buf;
78 else 87
79 { // copy buffer 88 while (--n > 0)
80 char *p = buf; 89 {
81 while (n>1 && *fp->inp && fp->size && 90 unsigned char c = *fp->inp++;
82 (n--, fp->size--, *p++ = *fp->inp++) != '\n') 91 fp->size--;
83 ; 92
84 *p = 0; 93 if ( c == '\n' )
94 break;
95
96 if ( c != '\r' )
97 *p++ = c;
85 } 98 }
86 return buf; // Return buffer pointer 99
100 *p = '\0';
101
102 return buf; // Return buffer pointer
87} 103}
88 104
89int dehfeof(DEHFILE *fp) 105int dehfeof(DEHFILE *fp)
90{ 106{
91 return //!fp->lump ? (feof)((FILE *) fp->inp) : 107 if (fp->fd >= 0)
92!*fp->inp || fp->size<=0; 108 {
109 off_t size = filesize(fp->fd);
110 off_t offset = lseek(fp->fd, 0, SEEK_CUR);
111 return (size <= 0 || offset < 0 || offset >= size) ? 1 : 0;
112 }
113
114 return (fp->size == 0 || *fp->inp == '\0') ? 1 : 0;
93} 115}
94 116
95int dehfgetc(DEHFILE *fp) 117int dehfgetc(DEHFILE *fp)
96{ 118{
97 return //!fp->lump ? (fgetc)((FILE *) fp->inp) : 119 if (fp->fd >= 0)
98fp->size > 0 ? 120 {
99 fp->size--, *fp->inp++ : EOF; 121 unsigned char c;
122 if (read(fp->fd, &c, 1) == 1)
123 return (unsigned int)c;
124 }
125 else if (fp->size > 0)
126 {
127 return fp->size--, *fp->inp++;
128 }
129
130 return EOF;
100} 131}
101 132
102 133
@@ -953,7 +984,6 @@ const char **const mapnamest[] = // TNT WAD map names.
953 }; 984 };
954 985
955// Function prototypes 986// Function prototypes
956void lfstrip(char *); // strip the \r and/or \n off of a line
957void rstrip(char *); // strip trailing whitespace 987void rstrip(char *); // strip trailing whitespace
958char * ptr_lstrip(char *); // point past leading whitespace 988char * ptr_lstrip(char *); // point past leading whitespace
959boolean deh_GetData(char *, char *, uint_64_t *, char **, int ); 989boolean deh_GetData(char *, char *, uint_64_t *, char **, int );
@@ -1443,45 +1473,48 @@ static actionf_t deh_codeptr[NUMSTATES];
1443 1473
1444void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum) 1474void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1445{ 1475{
1446 static int fileout; // In case -dehout was used 1476 static int fileout = -1; // In case -dehout was used
1447 DEHFILE infile, *filein = &infile; // killough 10/98 1477 DEHFILE infile, *filein = &infile; // killough 10/98
1448 char inbuffer[DEH_BUFFERMAX]; // Place to put the primary infostring 1478 char inbuffer[DEH_BUFFERMAX]; // Place to put the primary infostring
1449 1479
1450 // Open output file if we're writing output 1480 // Open output file if we're writing output
1451 if (outfilename && *outfilename && !fileout) 1481 if (outfilename && *outfilename && fileout < 0)
1452 { 1482 {
1453 static boolean firstfile = true; // to allow append to output log 1483 static boolean firstfile = true; // to allow append to output log
1454 if (!strcmp(outfilename, "-")) 1484 if (strcmp(outfilename, "-"))
1455 fileout = 0; 1485 {
1456 else 1486 fileout = open(outfilename, firstfile ? O_WRONLY | O_CREAT :
1457 if ((fileout=open(outfilename, firstfile ? O_WRONLY | O_CREAT : O_RDONLY)) < 0 ) 1487 O_WRONLY | O_APPEND);
1488 if (fileout < 0)
1458 { 1489 {
1459 printf( "Could not open -dehout file %s\n... using stdout.\n", 1490 printf( "Could not open -dehout file %s\n... using stdout.\n",
1460 outfilename); 1491 outfilename);
1461 fileout = 0;
1462 } 1492 }
1493 }
1463 firstfile = false; 1494 firstfile = false;
1464 } 1495 }
1465 1496
1466 // killough 10/98: allow DEH files to come from wad lumps 1497 // killough 10/98: allow DEH files to come from wad lumps
1467 if (filename) 1498 if (filename)
1468 { 1499 {
1469 if ((intptr_t)(infile.inp = (void *)(intptr_t)open(filename,O_RDONLY))<0) 1500 if ((infile.fd = open(filename,O_RDONLY)) < 0)
1470 { 1501 {
1471 printf( "-deh file %s not found\n",filename); 1502 printf( "-deh file %s not found\n",filename);
1472 return; // should be checked up front anyway 1503 return; // should be checked up front anyway
1473 } 1504 }
1474 infile.lump = NULL; 1505 infile.inp = NULL;
1475 } 1506 }
1476 else // DEH file comes from lump indicated by third argument 1507 else // DEH file comes from lump indicated by third argument
1477 { 1508 {
1478 infile.size = W_LumpLength(lumpnum); 1509 int size = W_LumpLength(lumpnum);
1479 infile.inp = infile.lump = W_CacheLumpNum(lumpnum); 1510 infile.size = (size < 0) ? 0 : (ssize_t)size;
1511 infile.inp = W_CacheLumpNum(lumpnum);
1480 filename = "(WAD)"; 1512 filename = "(WAD)";
1481 } 1513 }
1482 1514
1483 printf("Loading DEH file %s\n",filename); 1515 printf("Loading DEH file %s\n",filename);
1484 if (fileout) fdprintf(fileout,"\nLoading DEH file %s\n\n",filename); 1516 if (fileout >= 0)
1517 fdprintf(fileout,"\nLoading DEH file %s\n\n",filename);
1485 1518
1486 { 1519 {
1487 static int i; // killough 10/98: only run once, by keeping index static 1520 static int i; // killough 10/98: only run once, by keeping index static
@@ -1495,8 +1528,9 @@ void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1495 { 1528 {
1496 unsigned int i; 1529 unsigned int i;
1497 1530
1498 lfstrip(inbuffer); 1531 if (fileout >= 0)
1499 if (fileout) fdprintf(fileout,"Line='%s'\n",inbuffer); 1532 fdprintf(fileout,"Line='%s'\n",inbuffer);
1533
1500 if (!*inbuffer || *inbuffer == '#' || *inbuffer == ' ') 1534 if (!*inbuffer || *inbuffer == '#' || *inbuffer == ' ')
1501 continue; /* Blank line or comment line */ 1535 continue; /* Blank line or comment line */
1502 1536
@@ -1517,11 +1551,12 @@ void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1517 // killough 10/98: exclude if inside wads (only to discourage 1551 // killough 10/98: exclude if inside wads (only to discourage
1518 // the practice, since the code could otherwise handle it) 1552 // the practice, since the code could otherwise handle it)
1519 1553
1520 if (infile.lump) 1554 if (infile.inp)
1521 { 1555 {
1522 if (fileout) 1556 if (fileout >= 0)
1523 fdprintf(fileout, 1557 fdprintf(fileout,
1524 "No files may be included from wads: %s\n",inbuffer); 1558 "No files may be included from wads: %s\n",
1559 inbuffer);
1525 continue; 1560 continue;
1526 } 1561 }
1527 1562
@@ -1531,7 +1566,7 @@ void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1531 if (!strncasecmp(nextfile = ptr_lstrip(inbuffer+7),"NOTEXT",6)) 1566 if (!strncasecmp(nextfile = ptr_lstrip(inbuffer+7),"NOTEXT",6))
1532 includenotext = true, nextfile = ptr_lstrip(nextfile+6); 1567 includenotext = true, nextfile = ptr_lstrip(nextfile+6);
1533 1568
1534 if (fileout) 1569 if (fileout >= 0)
1535 fdprintf(fileout,"Branching to include file %s...\n", nextfile); 1570 fdprintf(fileout,"Branching to include file %s...\n", nextfile);
1536 1571
1537 // killough 10/98: 1572 // killough 10/98:
@@ -1540,14 +1575,15 @@ void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1540 ProcessDehFile(nextfile,NULL,0); // do the included file 1575 ProcessDehFile(nextfile,NULL,0); // do the included file
1541 1576
1542 includenotext = oldnotext; 1577 includenotext = oldnotext;
1543 if (fileout) fdprintf(fileout,"...continuing with %s\n",filename); 1578 if (fileout >= 0)
1579 fdprintf(fileout,"...continuing with %s\n",filename);
1544 continue; 1580 continue;
1545 } 1581 }
1546 1582
1547 for (i=0; i<DEH_BLOCKMAX; i++) 1583 for (i=0; i<DEH_BLOCKMAX; i++)
1548 if (!strncasecmp(inbuffer,deh_blocks[i].key,strlen(deh_blocks[i].key))) 1584 if (!strncasecmp(inbuffer,deh_blocks[i].key,strlen(deh_blocks[i].key)))
1549 { // matches one 1585 { // matches one
1550 if (fileout) 1586 if (fileout >= 0)
1551 fdprintf(fileout,"Processing function [%d] for %s\n", 1587 fdprintf(fileout,"Processing function [%d] for %s\n",
1552 i, deh_blocks[i].key); 1588 i, deh_blocks[i].key);
1553 deh_blocks[i].fptr(filein,fileout,inbuffer); // call function 1589 deh_blocks[i].fptr(filein,fileout,inbuffer); // call function
@@ -1555,12 +1591,22 @@ void ProcessDehFile(const char *filename, const char *outfilename, int lumpnum)
1555 } 1591 }
1556 } 1592 }
1557 1593
1558 if (infile.lump) 1594 if (infile.inp)
1559 W_UnlockLumpNum(lumpnum); // Mark purgable 1595 {
1560 else 1596 W_UnlockLumpNum(lumpnum); // Mark purgable
1561 close((int)(intptr_t) infile.inp); // Close real file 1597 }
1598
1599 if (infile.fd >= 0)
1600 {
1601 close(infile.fd); // Close real file
1602 infile.fd = -1;
1603 }
1562 1604
1563 close(fileout); 1605 if (fileout >= 0)
1606 {
1607 close(fileout);
1608 fileout = -1;
1609 }
1564} 1610}
1565 1611
1566// ==================================================================== 1612// ====================================================================
@@ -1587,7 +1633,6 @@ void deh_procBexCodePointers(DEHFILE *fpin, int fpout, char *line)
1587 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 1633 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
1588 { 1634 {
1589 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 1635 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
1590 lfstrip(inbuffer);
1591 if (!*inbuffer) break; // killough 11/98: really exit on blank line 1636 if (!*inbuffer) break; // killough 11/98: really exit on blank line
1592 1637
1593 // killough 8/98: allow hex numbers in input: 1638 // killough 8/98: allow hex numbers in input:
@@ -1760,7 +1805,6 @@ void deh_procThing(DEHFILE *fpin, int fpout, char *line)
1760 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 1805 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
1761 { 1806 {
1762 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 1807 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
1763 lfstrip(inbuffer); // toss the end of line
1764 1808
1765 // killough 11/98: really bail out on blank lines (break != continue) 1809 // killough 11/98: really bail out on blank lines (break != continue)
1766 if (!*inbuffer) break; // bail out with blank line between sections 1810 if (!*inbuffer) break; // bail out with blank line between sections
@@ -1866,7 +1910,6 @@ void deh_procFrame(DEHFILE *fpin, int fpout, char *line)
1866 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 1910 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
1867 { 1911 {
1868 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 1912 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
1869 lfstrip(inbuffer);
1870 if (!*inbuffer) break; // killough 11/98 1913 if (!*inbuffer) break; // killough 11/98
1871 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 1914 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
1872 { 1915 {
@@ -1957,7 +2000,6 @@ void deh_procPointer(DEHFILE *fpin, int fpout, char *line) // done
1957 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2000 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
1958 { 2001 {
1959 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2002 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
1960 lfstrip(inbuffer);
1961 if (!*inbuffer) break; // killough 11/98 2003 if (!*inbuffer) break; // killough 11/98
1962 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 2004 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
1963 { 2005 {
@@ -2023,7 +2065,6 @@ void deh_procSounds(DEHFILE *fpin, int fpout, char *line)
2023 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2065 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2024 { 2066 {
2025 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2067 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2026 lfstrip(inbuffer);
2027 if (!*inbuffer) break; // killough 11/98 2068 if (!*inbuffer) break; // killough 11/98
2028 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 2069 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
2029 { 2070 {
@@ -2091,7 +2132,6 @@ void deh_procAmmo(DEHFILE *fpin, int fpout, char *line)
2091 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2132 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2092 { 2133 {
2093 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2134 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2094 lfstrip(inbuffer);
2095 if (!*inbuffer) break; // killough 11/98 2135 if (!*inbuffer) break; // killough 11/98
2096 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 2136 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
2097 { 2137 {
@@ -2137,7 +2177,6 @@ void deh_procWeapon(DEHFILE *fpin, int fpout, char *line)
2137 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2177 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2138 { 2178 {
2139 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2179 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2140 lfstrip(inbuffer);
2141 if (!*inbuffer) break; // killough 11/98 2180 if (!*inbuffer) break; // killough 11/98
2142 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 2181 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
2143 { 2182 {
@@ -2193,7 +2232,6 @@ void deh_procSprite(DEHFILE *fpin, int fpout, char *line) // Not supported
2193 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2232 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2194 { 2233 {
2195 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2234 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2196 lfstrip(inbuffer);
2197 if (!*inbuffer) break; // killough 11/98 2235 if (!*inbuffer) break; // killough 11/98
2198 // ignore line 2236 // ignore line
2199 if (fpout) fdprintf(fpout,"- %s\n",inbuffer); 2237 if (fpout) fdprintf(fpout,"- %s\n",inbuffer);
@@ -2237,7 +2275,7 @@ void deh_procPars(DEHFILE *fpin, int fpout, char *line) // extension
2237 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2275 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2238 { 2276 {
2239 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2277 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2240 lfstrip(strlwr(inbuffer)); // lowercase it 2278 strlwr(inbuffer); // lowercase it
2241 if (!*inbuffer) break; // killough 11/98 2279 if (!*inbuffer) break; // killough 11/98
2242 if (3 != sscanf(inbuffer,"par %d %d %d",&episode, &level, &partime)) 2280 if (3 != sscanf(inbuffer,"par %d %d %d",&episode, &level, &partime))
2243 { // not 3 2281 { // not 3
@@ -2314,7 +2352,6 @@ void deh_procCheat(DEHFILE *fpin, int fpout, char *line) // done
2314 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2352 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2315 { 2353 {
2316 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2354 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2317 lfstrip(inbuffer);
2318 if (!*inbuffer) break; // killough 11/98 2355 if (!*inbuffer) break; // killough 11/98
2319 if (!deh_GetData(inbuffer,key,&value,&strval,fpout)) // returns TRUE if ok 2356 if (!deh_GetData(inbuffer,key,&value,&strval,fpout)) // returns TRUE if ok
2320 { 2357 {
@@ -2386,7 +2423,6 @@ void deh_procMisc(DEHFILE *fpin, int fpout, char *line) // done
2386 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' ')) 2423 while (!dehfeof(fpin) && *inbuffer && (*inbuffer != ' '))
2387 { 2424 {
2388 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2425 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2389 lfstrip(inbuffer);
2390 if (!*inbuffer) break; // killough 11/98 2426 if (!*inbuffer) break; // killough 11/98
2391 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok 2427 if (!deh_GetData(inbuffer,key,&value,NULL,fpout)) // returns TRUE if ok
2392 { 2428 {
@@ -2630,7 +2666,6 @@ void deh_procStrings(DEHFILE *fpin, int fpout, char *line)
2630 { 2666 {
2631 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break; 2667 if (!dehfgets(inbuffer, sizeof(inbuffer), fpin)) break;
2632 if (*inbuffer == '#') continue; // skip comment lines 2668 if (*inbuffer == '#') continue; // skip comment lines
2633 lfstrip(inbuffer);
2634 if (!*inbuffer) break; // killough 11/98 2669 if (!*inbuffer) break; // killough 11/98
2635 if (!*holdstring) // first one--get the key 2670 if (!*holdstring) // first one--get the key
2636 { 2671 {
@@ -2776,21 +2811,6 @@ char *dehReformatStr(char *string)
2776} 2811}
2777 2812
2778// ==================================================================== 2813// ====================================================================
2779// lfstrip
2780// Purpose: Strips CR/LF off the end of a string
2781// Args: s -- the string to work on
2782// Returns: void -- the string is modified in place
2783//
2784// killough 10/98: only strip at end of line, not entire string
2785
2786void lfstrip(char *s) // strip the \r and/or \n off of a line
2787{
2788 char *p = s+strlen(s);
2789 while (p > s && (*--p=='\r' || *p=='\n'))
2790 *p = 0;
2791}
2792
2793// ====================================================================
2794// rstrip 2814// rstrip
2795// Purpose: Strips trailing blanks off a string 2815// Purpose: Strips trailing blanks off a string
2796// Args: s -- the string to work on 2816// Args: s -- the string to work on