// getcode.c                       1031                          9/9/98

// Get "code" portion of "asm" file "filename" in "filename.cod".

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *infile;
FILE *outfile;

char copy[250], filename[20], instring[250];
char *label, *optok;
int foundmain, length;

void clear_copy(void);        // clear "copy" string

void main(int argc, char *argv[])
     {
     if(argc < 2)
          {
          printf("\n\n\t\aUSAGE: getcode <filename>.\n\n");
          exit(1);
          }
          
     sprintf(filename, "%s.asm", argv[1]);
     infile = fopen(filename, "rt");
     if(!infile)
          {
          printf("\n\n\t\aCan't open %s!n\n", filename);
          exit(1);
          }          
          
     sprintf(filename, "%s.cod", argv[1]);
     outfile = fopen(filename, "wt");
     if(!outfile)
          {
          printf("\n\n\t\aCan't open %s!n\n", filename);
          fclose(infile);
          exit(1);
          }
          
     while(!foundmain)
          {
          if(feof(infile))
               {
               fclose(infile);
               fclose(outfile);
               exit(1);
               }
          fgets(instring, 250, infile);
          if(strstr(instring, "main:")) foundmain = 1;
          }          
          
     fputs(instring, outfile);

     while(!feof(infile))
          {
          fgets(instring, 250, infile);
          if(instring[0] == '\n') continue;
          if(isgraph(instring[0]))
               {
               clear_copy();
               strcpy(copy, instring);
               label = strtok(copy, " \t\n");
               optok = strtok(NULL, " \t\n");
               if(!optok) continue; // "orphan" label
               }
          fputs(instring, outfile);
          }
          
     fclose(infile);
     fclose(outfile);
     }

void clear_copy(void)         // clear "copy" string
     {
     int n;
     
     for(n=0; n < 250; n++) copy[n] = 0;
     }