home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / hcesource / fd2stubs / fd2stubs.c next >
C/C++ Source or Header  |  1992-09-02  |  11KB  |  393 lines

  1. /*
  2.  * Program FD2Stubs
  3.  * ----------------
  4.  * Creates interface stubs for HCC and CClib.library from .fd-files
  5.  *
  6.  * written in 4/90 by
  7.  *
  8.  * Detlef Wuerkner
  9.  * Asterweg 3
  10.  * D-6301 Wettenberg-Launsbach
  11.  * West Germany
  12.  *
  13.  * Version 1.1
  14.  *
  15.  * 15-01-91 TetiSoft Only A0,A1,D0,D1 are scratch (CClib.library V3.0)
  16.  * 20-05-91 TetiSoft A2 now register variable
  17.  *
  18.  * This is Public Domain
  19.  */
  20.  
  21.  
  22. /* The following MUST be declared in the same manner than in
  23.  * PARAM.H for the compiler itself!!!
  24.  *
  25.  * TetiSoft We will save D3 since CClib.library V3.0
  26.  * no longer destroys it like V1.0, so we will use it for
  27.  * register variables.
  28.  *
  29.  * D2 is used by HCC for Data Shifts (ASL etc).
  30.  * So we must not save it, even when CClib.library V3.0 seems to keep it.
  31.  */
  32.  
  33. #define ARV_START '2'    /* A0-A1 are allowed to be destroyed */
  34. #define DRV_START '3'    /* D0-D2 are allowed to be destroyed */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40.  
  41. FILE *fdfile, *script, *stubfile;
  42. char *fdname, *stub, *stubs, *scriptname, *libname, *basename;
  43. char linebuf[1000], s1[1000], regsave[1000], regmove[1000], movem[1000];
  44. char *getline();
  45. long liboffset, linenr;
  46. int  warnings, nrprivates, public = 1;
  47.  
  48. main(argc, argv)
  49. char *argv[];
  50. {
  51.   register int i,nr;
  52.   register char *s;
  53.   int first=1;
  54.  
  55.   if (argc==0)
  56.      exit(); /* Don't run from Workbench */
  57.  
  58.   if (argc==1 || argc>2) {
  59.      printf("FD2Stubs - create interface stubs from .fd-files\n\n");
  60.      printf("Usage: %s libraryname (_lib.fd will be added)\n\n",argv[0]);
  61.      printf("DON'T specify a path name, CD to the .fd-directory!\n");
  62.      printf("The program creates many files 'T:librarynameNNN',\n");
  63.      printf("one file for each library function, and a script file\n");
  64.      printf("'T:Makelibraryname' which will, if called via Execute,\n");
  65.      printf("generate the file 'T:libraryname.stubs'. You can 'Join'\n");
  66.      printf("that file together with those generated from other .fd-files\n");
  67.      printf("to the finally file 'stubs.lib'. Assign T: to RAM:T,\n");
  68.      printf("copy A68k, Join, Rename and Delete to RAM: and type\n");
  69.      printf("'path RAM: add' before running the produced scriptfile.\n");
  70.      exit(EXIT_FAILURE);
  71.   } else {
  72.      printf("FD2Stubs - create interface stubs from .fd-files\n");
  73.      printf("Version 1.1 by Detlef W\374rkner\n");
  74.      printf("This is Public Domain.\n");
  75.   }
  76.  
  77.   libname = argv[1];
  78.  
  79.   fdname = (char *)malloc (strlen(libname) + strlen("_lib.fd") + 1);
  80.   fdname = strcpy (fdname, libname);
  81.   fdname = strcat (fdname, "_lib.fd");
  82.  
  83.   stubs = (char *)malloc (strlen(libname) + strlen("T:") +1);
  84.   stubs = strcpy (stubs, "T:");
  85.   stubs = strcat (stubs, libname);
  86.  
  87.   if (!(fdfile = fopen (fdname, "r"))) {
  88.      printf ("Fatal error: Can't open input file '%s'\n", fdname);
  89.      exit (EXIT_FAILURE);
  90.   };
  91.   setbuf (fdfile, malloc(BUFSIZ));
  92.  
  93.   scriptname = (char *)malloc (strlen(libname) + strlen("T:Make") + 1);
  94.   scriptname = strcpy (scriptname, "T:Make");
  95.   scriptname = strcat (scriptname, libname);
  96.  
  97.   if (!(script = fopen(scriptname, "w"))) {
  98.      printf ("Fatal error: Can't open output file '%s'\n", scriptname);
  99.      exit (EXIT_FAILURE);
  100.   };
  101.   setbuf (script, malloc(BUFSIZ));
  102.  
  103. /* fprintf(script, "Echo >T:%s.stubs %c%c noline ;create empty file\n",
  104.  *                                                           libname, 34, 34);
  105.  */
  106.  
  107.   for(nr=1; s=getline(); nr++) {
  108.      openstubfile(nr);
  109.      dofunction(s);
  110.      fclose(stubfile);
  111.      if (public) {
  112.        if (first) {
  113.          first = 0;
  114.          fprintf(script,"\nA68k -q %s %s.stubs\n", stub, stubs);
  115.          fprintf(script,"Delete %s\n", stub);
  116.        } else {
  117.          fprintf(script,"\nA68k -q %s %s.o\n", stub, stub);
  118.          fprintf(script,"Join %s.stubs %s.o as %s.new\n", stubs, stub, stubs);
  119.          fprintf(script,"Delete %s.stubs %s.o %s\n", stubs, stub, stub);
  120.          fprintf(script,"Rename %s.new %s.stubs\n", stubs, stubs);
  121.        }
  122.      }
  123.      else {
  124.         fprintf(script, "\nDelete %s\n", stub);
  125.         nrprivates++;
  126.      }
  127.   }
  128.  
  129.   fclose(fdfile);
  130.   fclose(script);
  131. /* SetProtection(scriptname, 0x40L); */ /* set script bit */
  132.   if (nrprivates) {
  133.      printf("Access denied to %ld private functions of the %s.library.\n",
  134.             nrprivates, libname);
  135.   }
  136.   printf("You made a simple program very happy.\n");
  137.   if (warnings)
  138.      exit(EXIT_WARN);
  139. }
  140.  
  141.  
  142. openstubfile(nr)
  143. {
  144.    stub = (char *)malloc (strlen(libname) + strlen("T:NNN") + 1);
  145.    stub = strcpy (stub, "T:");
  146.    stub = strcat (stub, libname);
  147.    stub = strcat (stub, ltoa(nr,"NNN"));
  148.  
  149.    if (!(stubfile = fopen (stub, "w"))) {
  150.       printf ("Fatal error: Can't open output file '%s'\n", stub);
  151.       exit (EXIT_FAILURE);
  152.    };
  153. /*   setbuf (stubfile, malloc(BUFSIZ)); */
  154. };
  155.  
  156.  
  157. char *getline()
  158. {
  159.    register char *s;
  160.    register int i;
  161.  
  162.    for (;;) {                /* loop until EOF or function */
  163.       linenr++;
  164.       if (!(fgets(linebuf, 1000, fdfile))) {
  165.      warn("##end missing");
  166.          return NULL;
  167.       }
  168.       s = linebuf;
  169.       while (*s && isspace(*s))
  170.          s++;
  171.       if (*s == '\0')            /* Empty line */
  172.          continue;
  173.       if (*s == '*')            /* Comment line */
  174.          continue;
  175.  
  176.       if (*s != '#')
  177.          return s;            /* Function description ? */
  178.  
  179.       i = 0;                /* Instruction line */
  180.       while (*s && !isspace(*s))
  181.          s1[i++] = *s++;
  182.       s1[i] = '\0';
  183.       if (strcmp(s1, "##end") == 0)
  184.          return NULL;
  185.       if (strcmp(s1, "##public") == 0) {
  186.          public = 1;
  187.          continue;
  188.       }
  189.       if (strcmp(s1, "##private") == 0) {
  190.          public = 0;
  191.          continue;
  192.       }
  193.       if (strcmp(s1, "##bias") == 0) {
  194.          if (liboffset != NULL)
  195.             error("Second declaration of ##bias");
  196.          else {
  197.             while (*s && isspace(*s))
  198.                s++;
  199.             i = 0;
  200.             while (*s && isdigit(*s))
  201.                s1[i++] = *s++;
  202.             s1[i] = '\0';
  203.             if (s1[0] == '\0')
  204.                error("##bias: number expected");
  205.             liboffset -= atol(s1);
  206.             continue;
  207.          }
  208.       }
  209.       if (strcmp(s1, "##base") == 0) {
  210.          if (basename != NULL)
  211.             error("Second declaration of ##base");
  212.          else {
  213.             while (*s && isspace(*s))
  214.                s++;
  215.             i = 0;
  216.             while (*s && !isspace(*s))
  217.                s1[i++] = *s++;
  218.             s1[i] = '\0';
  219.             if (s1[0] == '\0')
  220.                error("##base: basename expected");
  221.             basename = strcpy((char *)malloc(strlen(s1)+1),s1);
  222.             continue;
  223.          }
  224.       }
  225.       error("Unknown # command");
  226.    }
  227. }
  228.  
  229.  
  230. error(s)
  231. char *s;
  232. {
  233.    printf("Error in %s in line %ld:\n%s\n", fdname, linenr, s);
  234.    exit(EXIT_ERROR);
  235. }
  236.  
  237. warn(s)
  238. char *s;
  239. {
  240.    printf("Warning in %s in line %ld:\n%s\n", fdname, linenr, s);
  241.    warnings++;
  242. }
  243.  
  244.  
  245. dofunction(s)
  246. register char    *s;
  247. {
  248.    char *rs, *rm, *move, *help;
  249.    register int i;
  250.    int savenr, slashnr, numargs1, numargs2;
  251.    register char c;
  252.  
  253.    rs = regsave;
  254.    *rs = '\0';
  255.    rm = regmove;
  256.    *rm = '\0';
  257.    move = movem;
  258.    *move = '\0';
  259.    savenr = 0;
  260.    slashnr = 0;
  261.    numargs1 = 0;
  262.    numargs2 = 0;
  263.  
  264.    if (liboffset == 0)
  265.       error("##bias expected");
  266.    if (basename == NULL)
  267.       error("##base expected");
  268.  
  269.    i = 0;
  270.    while (*s && *s != '(')    /* get funcname */
  271.       s1[i++] = *s++;
  272.    s1[i] = '\0';
  273.  
  274.    if (s1[0] == '\0')
  275.       error("functionname expected");
  276.    if (*s != '(')
  277.       error("'(' expected");
  278.  
  279.    fprintf(stubfile, "\tXREF\t%s\n", basename);
  280.    fprintf(stubfile, "\tXDEF\t_%s\n\n", s1);
  281.    fprintf(stubfile, "_%s:\n", s1);
  282.    fprintf(stubfile, "\tmove.l\t%s,A6\n", basename);
  283.  
  284.    *s++; 
  285.    if (isalpha(*s))
  286.       numargs1 = 1;
  287.    else if (*s != ')')
  288.       error("expected a letter or ')' after '('");
  289.  
  290.    while (*s && *s != ')') {        /* count args */
  291.       *s++;
  292.       if (*s == ',') {
  293.          numargs1++;
  294.          *s++;
  295.          while (*s == ' ')
  296.            *s++;
  297.          if (!isalpha(*s))
  298.             error("expected an argument after ','");
  299.       }
  300.    }
  301.  
  302.    while (*s && *s != '(')        /* get register list */
  303.       *s++;
  304.    if (*s == '\0') {
  305.       if( numargs1 == 0) {
  306.          fprintf(stubfile,"\tjmp\t%ld(A6)\n\n\tEND\n", liboffset);
  307.          liboffset -=