/[smarc]/smarc/src/spasm/main.c
ViewVC logotype

Diff of /smarc/src/spasm/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by batonik, Sat Dec 28 21:29:26 2002 UTC revision 1.3 by batonik, Tue Dec 31 00:38:13 2002 UTC
# Line 7  Line 7 
7  #include <string.h>  #include <string.h>
8  #include <sys/types.h>  #include <sys/types.h>
9  #include <unistd.h>  #include <unistd.h>
10    #include <errno.h>
11    
12  #include "main.h"  #include "main.h"
13  #include "stale.h"  #include "stale.h"
14  #include "funkcje.h"  #include "funkcje.h"
15    #include "serr.h"
16    #include "etykiety.h"
17    #include "kod.h"
18    #include "dane.h"
19    
20  extern int      yydebug;  extern int      yydebug;
21  extern char     *yytext;  extern char     *yytext;
# Line 34  int            bezOstrzezen = 0; Line 39  int            bezOstrzezen = 0;
39    
40  void yyerror (char *s)  void yyerror (char *s)
41  {  {
42          fprintf (stderr, "*** BŁĄD *** linia nr %d: %s, yytext: '%s'\n", numerLinii, s, yytext);          fprintf (stderr, ERROR_PREFIX" line %d: %s, yytext: '%s'\n", numerLinii, s, yytext);
43          wystapilBlad = 1;          wystapilBlad++;
44  }  }
45    
46  void yywarn (char *s)  void yywarn (char *s)
47  {  {
48          if (!bezOstrzezen)          if (!bezOstrzezen)
49                  fprintf(stderr, "*** OSTRZEŻENIE *** linia %d: %s\n", numerLinii, s);                  fprintf(stderr, WARNING_PREFIX" line %d: %s\n", numerLinii, s);
50  }  }
51    
52    // =-=-=-=-=-=-=-=-==-=-=-=-=-=-=--=-=-=
53    
54    int     ScalPlikiTemp(FILE *kod, FILE *wynik)
55    {
56            char bufor[DLUGOSCBUFORA];
57            int ile, czytaj;
58    
59            rewind(plikWynikowyDanych);
60            rewind(kod);
61    
62            fprintf(wynik, "%s", WYNIK_START);      // "SMARC"
63            fprintf(wynik, "%c", SMARC_WERSJA + '0');
64            fprintf(wynik, "%s", WYNIK_DANE);
65            fprintf(wynik, "%c%c%c%c", WORD_4B(daneOffset), WORD_3B(daneOffset),
66                                    WORD_2B(daneOffset), WORD_1B(daneOffset));
67            ile = daneOffset;
68            while (ile)
69            {
70                    if (ile > DLUGOSCBUFORA)
71                            czytaj = DLUGOSCBUFORA;
72                    else
73                            czytaj = ile;
74                    ile = ile - czytaj;
75    
76                    fread(&bufor, czytaj, 1, plikWynikowyDanych);
77                    fwrite(&bufor, czytaj, 1, wynik);
78            }
79    
80            fprintf(wynik, "%s", WYNIK_KOD);
81            ile = numerInstrukcji * 4;
82            fprintf(wynik, "%c%c%c%c", WORD_4B(ile), WORD_3B(ile),
83                                    WORD_2B(ile), WORD_1B(ile));
84            while (ile)
85            {
86                    if (ile > DLUGOSCBUFORA)
87                            czytaj = DLUGOSCBUFORA;
88                    else
89                            czytaj = ile;
90                    ile = ile - czytaj;
91    
92                    fread(&bufor, czytaj, 1, kod);
93                    fwrite(&bufor, czytaj, 1, wynik);
94            }
95            return(0);
96    }
97    
98    
99    
100  // ------------------------------------------  // ------------------------------------------
101    
102  int main (int argc, char **argv)  int main (int argc, char **argv)
103  {  {
104          int     opcja;          int     opcja;
105          char    *nazwaPlikuDanych;          char    *nazwaPlikuDanych, *nazwaPlikuRozkazow;
106            char    *nazwaPlikuWynikowego = NULL;
107            FILE    *plikBin, *binarkaSMARC = NULL;
108    
109          while ((opcja = getopt(argc,argv,"hvo:qz")) != -1)          while ((opcja = getopt(argc,argv,"hvo:qz")) != -1)
110                  switch (opcja)                  switch (opcja)
# Line 64  int main (int argc, char **argv) Line 119  int main (int argc, char **argv)
119                          case 'z':                          case 'z':
120                                          zapiszASCIIZ = 1;                                          zapiszASCIIZ = 1;
121                                          break;                                          break;
122                            case 'o':
123                                            nazwaPlikuWynikowego = optarg;
124                                            break;
125                          case '?':                          case '?':
126                          case 'h':                          case 'h':
127                                          printf(SPASM_POMOC);                                          printf(SPASM_POMOC);
# Line 74  int main (int argc, char **argv) Line 132  int main (int argc, char **argv)
132          freopen("test.asm", "r", stdin);          freopen("test.asm", "r", stdin);
133    
134          Dopisz (&buforZnakowy, "");     //sama alokacja :)          Dopisz (&buforZnakowy, "");     //sama alokacja :)
135          sprintf(buforZnakowy, SPASM_DANE_PREFIX"%d", getpid());          sprintf(buforZnakowy, SPASM_DANE_PREFIKS"%d", getpid());
136          nazwaPlikuDanych = strdup(buforZnakowy);          nazwaPlikuDanych = strdup(buforZnakowy);
137    
138          if ((plikWynikowyDanych = fopen(nazwaPlikuDanych, "w")))          CzyscBufor(&buforZnakowy, DLUGOSCBUFORA);
139            sprintf(buforZnakowy, SPASM_KOD_PREFIKS"%d", getpid());
140            nazwaPlikuRozkazow = strdup(buforZnakowy);
141    
142            if (!nazwaPlikuWynikowego)
143                    nazwaPlikuWynikowego = SPASM_WYNIK;
144    
145            if ((binarkaSMARC = fopen(nazwaPlikuWynikowego, "w")) == NULL)
146            {
147                    fprintf(stderr, ERROR_PREFIX" "E_IORESULT" (%s)\n", strerror(errno));
148                    return(1);
149            }
150    
151            if ((plikWynikowyDanych = fopen(nazwaPlikuDanych, "w+")))
152          {          {
153                  yyparse();                  yyparse();
154                  fclose(plikWynikowyDanych);                  DaneAlign(WORD_ALIGN);
155          }          }
156          else          else
157                  fprintf(stderr, "Błąd otwarcia pliku wynikowego!\n");          {
158                    fprintf(stderr, ERROR_PREFIX" "E_TEMPFILE"\n");
159                    return(1);
160            }
161    
162            if (!wystapilBlad)
163            {
164                    if ((plikBin = fopen(nazwaPlikuRozkazow, "w+")) != NULL)
165                    {
166                            if (ZapiszRozkazyDoPliku(plikBin))
167                            {
168                                    fprintf(stderr, ERROR_PREFIX" "E_IOCODE" (%s)\n", strerror(errno));
169                                    return(1);
170                            }
171                    }
172                    else
173                    {
174                            fprintf(stderr, ERROR_PREFIX" "E_TEMPFILE" (%s)\n", strerror(errno));
175                            return(1);
176                    }
177    
178          printf("plik z danymi: `%s'\n", nazwaPlikuDanych);                  ScalPlikiTemp(plikBin, binarkaSMARC);
179          printf("daneOffset = %d\n", daneOffset);                  fclose(plikBin);
180                    fclose(plikWynikowyDanych);
181                    fclose(binarkaSMARC);
182            }
183    
184            if (yydebug)
185            {
186                    printf("plik z danymi: `%s'\n", nazwaPlikuDanych);
187                    printf("daneOffset = %d\n", daneOffset);
188                    printf("plik z kodem:  `%s'\n", nazwaPlikuRozkazow);
189                    printf("numerInstrukcji = %d\n", numerInstrukcji);
190            }
191    
192            //czyścimy pamięć, itd.
193            UsunEtykiety();         //etykiety.c
194            UsunRozkazy();          //kod.c
195    
196          if (!yydebug)          if (!yydebug)
197            {
198                  unlink (nazwaPlikuDanych);                  unlink (nazwaPlikuDanych);
199                    unlink (nazwaPlikuRozkazow);
200            }
201    
202    
203          return(0);          return(0);
204  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26