home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 677 / TSRTOOLS / FILTER.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  11KB  |  332 lines

  1. /*
  2.     filter.C
  3.  
  4.     Version 1.5
  5.  
  6.     Filter BCC generated .asm file into TSR compatible file.
  7.  
  8.     Copyright (C) 1993, Geoff Friesen B.Sc.
  9.     All rights reserved.
  10.  
  11.     Developed with: Borland C++ 3.1
  12. */
  13.  
  14. #if !defined(__LARGE__)
  15. #error Large Memory Model Expected
  16. #endif
  17.  
  18. #include <dir.H>
  19. #include <fcntl.H>
  20. #include <io.H>
  21. #include <process.H>
  22. #include <stdio.H>
  23. #include <string.H>
  24.  
  25. int handle [4];
  26.  
  27. char *banner =
  28.  
  29. "╔═════════════════════════════════════"
  30. "════════════════════════════════════════╗\n"
  31. "║ ▒▒▒▒▒ ▒▒▒▒▒ ▒     ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ "
  32. "                                        ║\n"
  33. "║ ▒       ▒   ▒       ▒   ▒     ▒   ▒ "
  34. "             FILTER v1.5                ║\n"
  35. "║ ▒       ▒   ▒       ▒   ▒     ▒   ▒ "
  36. "                                        ║\n"
  37. "║ ▒▒▒▒    ▒   ▒       ▒   ▒▒▒▒  ▒▒▒▒▒ "
  38. "Copyright (C) 1993, Geoff Friesen B.Sc. ║\n"
  39. "║ ▒       ▒   ▒       ▒   ▒     ▒  ▒  "
  40. "          All rights reserved.          ║\n"
  41. "║ ▒       ▒   ▒       ▒   ▒     ▒   ▒ "
  42. "                                        ║\n"
  43. "║ ▒     ▒▒▒▒▒ ▒▒▒▒▒   ▒   ▒▒▒▒▒ ▒   ▒ "
  44. "                                        ║\n"
  45. "╚═════════════════════════════════════"
  46. "════════════════════════════════════════╝\n\n";
  47.  
  48. void    error        (char *msg);
  49. int    getchr        (int handle);
  50. int    getline        (int handle, char *line);
  51. void    setfileext    (char *filespec, char *newext);
  52.  
  53. void main (int argc, char **argv)
  54. {
  55.    int i;
  56.    struct ffblk f;
  57.    char filespec [MAXPATH], line [256], *ptr;
  58.  
  59.    fprintf (stderr, banner);
  60.  
  61.    if (argc != 2)
  62.        error ("bad command line\nusage: filter filespec");
  63.  
  64.    strcpy (filespec, argv [1]);
  65.    setfileext (filespec, ".asm");
  66.  
  67.    if ((handle [0] = _open (filespec, O_RDONLY)) == -1)
  68.    {
  69.        fprintf (stderr, "filter: unable to open %s\n", filespec);
  70.        exit (1);
  71.    }
  72.  
  73.    if ((handle [1] = _creat ("tsr1.tmp", 0)) == -1)
  74.        error ("unable to create tsr1.tmp");
  75.  
  76.    if ((handle [2] = _creat ("tsr2.tmp", 0)) == -1)
  77.        error ("unable to create tsr2.tmp");
  78.  
  79.    if ((handle [3] = _creat ("tsr3.tmp", 0)) == -1)
  80.        error ("unable to create tsr3.tmp");
  81.  
  82.    while (1)
  83.    {
  84.       if (!getline (handle [0], line))
  85.       break;
  86.  
  87.       if (strstr (line, "extrn"))
  88.       continue;
  89.  
  90.       if (!strnicmp (line, "_TEXT", 5))
  91.       {
  92.       do
  93.       {
  94.          if (!getline (handle [0], line))
  95.          error ("premature EOF");
  96.  
  97.          ptr = strstr (line, ":DGROUP@");
  98.          if (ptr)
  99.          strcpy (ptr, "\n");
  100.  
  101.          ptr = strstr (line, "seg ");
  102.          if (ptr)
  103.          {
  104.          if (_write (handle [1], "mov ax, cs\r\n", 12) == -1)
  105.              error ("unable to write to TSR1.TMP");
  106.          strcpy (ptr, "ax\n");
  107.          }
  108.  
  109.          if (!strnicmp (line, "_TEXT", 5))
  110.          break;
  111.  
  112.          if (_write (handle [1], line, strlen (line)) == -1)
  113.          error ("unable to write to TSR1.TMP");
  114.       }
  115.       while (1);
  116.       continue;
  117.       }
  118.  
  119.       if (!strnicmp (line, "_DATA", 5))
  120.       {
  121.       do
  122.       {
  123.          if (!getline (handle [0], line))
  124.          error ("premature EOF");
  125.  
  126.          if (!strnicmp (line, "_DATA", 5))
  127.          break;
  128.  
  129.          if (_write (handle [2], line, strlen (line)) == -1)
  130.          error ("unable to write to TSR2.TMP");
  131.       }
  132.       while (1);
  133.       continue;
  134.       }
  135.  
  136.       if (!strnicmp (line, "_BSS", 4))
  137.       {
  138.       do
  139.       {
  140.          if (!getline (handle [0], line))
  141.          error ("premature EOF");
  142.  
  143.          if (!strnicmp (line, "_BSS", 4))
  144.          break;
  145.  
  146.          if (_write (handle [3], line, strlen (line)) == -1)
  147.          error ("unable to write to TSR3.TMP");
  148.       }
  149.       while (1);
  150.       continue;
  151.       }
  152.  
  153.       if (strstr (line, "end\r\n"))
  154.       continue;
  155.  
  156.       if (strstr (line, "group"))
  157.       strcpy (line, "DGROUP\tgroup\t_TEXT\r\n");
  158.  
  159.       if (_write (handle [1], line, strlen (line)) == -1)
  160.       error ("unable to write to TSR1.TMP");
  161.    }
  162.  
  163.    (void) lseek (handle [2], 0L, SEEK_SET);
  164.  
  165.    while (getline (handle [2], line))
  166.       if (_write (handle [1], line, strlen (line)) == -1)
  167.       error ("unable to write to TSR1.TMP");
  168.  
  169.    (void) lseek (handle [3], 0L, SEEK_SET);
  170.  
  171.    while (getline (handle [3], line))
  172.       if (_write (handle [1], line, strlen (line)) == -1)
  173.       error ("unable to write to TSR1.TMP");
  174.  
  175.    for (i = 0; i < 4; i++)
  176.     (void) _close (handle [i]);
  177.  
  178.    setfileext (filespec, ".tsr");
  179.  
  180.    if (!findfirst (filespec, &f, 0) && unlink (filespec) == -1)
  181.        error ("unable to erase original file");
  182.  
  183.    if (rename ("tsr1.tmp", filespec) == -1)
  184.        error ("unable to rename tsr1.tmp");
  185.  
  186.    if (unlink ("tsr2.tmp") == -1 || unlink ("tsr3.tmp") == -1)
  187.        error ("unable to erase tsr2.tmp or tsr3.tmp");
  188.  
  189.    setfileext (filespec, ".asm");
  190.    fprintf (stderr, "filter: %s successfully filtered\n", filespec);
  191.  
  192.    exit (0);
  193. }
  194.  
  195. /* ------------------------------------------------------ */
  196. /* Function: error ()                                     */
  197. /*                                                        */
  198. /* Prototype: void error (char *msg);                     */
  199. /*                                                        */
  200. /* Close all open files, display error message, and exit. */
  201. /*                                                        */
  202. /* Arguments:                                             */
  203. /*                                                        */
  204. /* msg - address of error message                         */
  205. /*                                                        */
  206. /* Local Variables:                                       */
  207. /*                                                        */
  208. /* i - loop index                                         */
  209. /* ------------------------------------------------------ */
  210.  
  211. void error (char *msg)
  212. {
  213.    int i;
  214.  
  215.    for (i = 0; i < 4; i++)
  216.     if (handle [i] != -1)
  217.         (void) _close (handle [i]);
  218.  
  219.    fprintf (stderr, "filter: %s\n", msg);
  220.  
  221.    exit (1);
  222. }
  223.  
  224. /* ------------------------------------------------------------ */
  225. /* Function: getchr ()                                          */
  226. /*                                                              */
  227. /* Prototype: int getchr (int handle);                          */
  228. /*                                                              */
  229. /* Get the next character from the file associated with handle. */
  230. /*                                                              */
  231. /* Arguments:                                                   */
  232. /*                                                              */
  233. /* handle - file handle                                         */
  234. /*                                                              */
  235. /* Return: EOF if end of file else character                    */
  236. /*                                                              */
  237. /* Local Variables:                                             */
  238. /*                                                              */
  239. /* n    - current number of characters in buffer                */
  240. /* bufp - pointer to next character in buffer                   */
  241. /* buf  - buffer used for buffered I/O                          */
  242. /* ------------------------------------------------------------ */
  243.  
  244. int getchr (int handle)
  245. {
  246.    static int n = 0;
  247.    static char *bufp;
  248.    static char buf [2048];
  249.  
  250.    if (n <= 0)
  251.    {
  252.        n = _read (handle, buf, 2048);
  253.        bufp = buf;
  254.    }
  255.  
  256.    return ((--n >= 0) ? *bufp++ & 255 : EOF);
  257. }
  258.  
  259. /* -------------------------------------------------------- */
  260. /* Function: getline ()                                     */
  261. /*                                                          */
  262. /* Prototype: int getline (int handle, char *line);         */
  263. /*                                                          */
  264. /* Get a line of text from the file associated with handle. */
  265. /* Each line is assumed to end with a CR/LF combination.    */
  266. /* Any terminating CTRL-Z is ignored.                       */
  267. /*                                                          */
  268. /* Arguments:                                               */
  269. /*                                                          */
  270. /* handle - file handle                                     */
  271. /* line   - buffer for storing line (must be large enough   */
  272. /*          to hold a full 253-character line plus a CR/LF  */
  273. /*          pair and a '\0' or 256 characters).             */
  274. /*                                                          */
  275. /* Local Variables:                                         */
  276. /*                                                          */
  277. /* c - next character read from file                        */
  278. /* i - loop index                                           */
  279. /* -------------------------------------------------------- */
  280.  
  281. int getline (int handle, char *line)
  282. {
  283.    int c, i;
  284.  
  285.    for (i = 0; i < 253 && (c = getchr (handle)) != EOF && c != '\r'; i++)
  286.     line [i] = c;
  287.  
  288.    if (!i || line [0] == 0x1a)
  289.        return 0;
  290.  
  291.    line [i++] = '\r';
  292.    line [i++] = '\n';
  293.    line [i] = '\0';
  294.  
  295.    if (c == '\r')
  296.        getchr (handle);
  297.  
  298.    return i;
  299. }
  300.  
  301. /* ---------------------------------------------------------- */
  302. /* Function: setfileext ()                                    */
  303. /*                                                            */
  304. /* Prototype: void setfileext (char *filespec, char *newext); */
  305. /*                                                            */
  306. /* setfileext () sets the extension of filespec to newext.    */
  307. /* It is assumed that filespec has enough room for MAXPATH    */
  308. /* characters.                                                */
  309. /*                                                            */
  310. /* Arguments:                                                 */
  311. /*                                                            */
  312. /* filespec - filespec to modify                              */
  313. /* newext   - new extension for filespec                      */
  314. /*                                                            */
  315. /* Local Variables:                                           */
  316. /*                                                            */
  317. /* drive - filespec drive (including ':')                     */
  318. /* dir   - filespec directory                                 */
  319. /* file  - filespec file name                                 */
  320. /* ext   - filespec extension                                 */
  321. /* ---------------------------------------------------------- */
  322.  
  323. void setfileext (char *filespec, char *newext)
  324. {
  325.    char drive [MAXDRIVE];
  326.    char dir [MAXDIR];
  327.    char file [MAXFILE];
  328.    char ext [MAXEXT];
  329.  
  330.    fnsplit (filespec, drive, dir, file, ext);
  331.    fnmerge (filespec, drive, dir, file, newext);
  332. }