-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforthc.c
83 lines (72 loc) · 2.64 KB
/
forthc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//@+leo-ver=4-thin
//@+node:leonardoce.20090629082550.220:@thin forthc.c
//@@language c
#define EXE_TEMPLATE "forthemb_template.c"
#define BUFFER_LEN 1024
#include <stdio.h>
#include <string.h>
#include "utils.h"
int main(int argc, char **argv) {
if(argc!=3) {
printf("Uso: %s <nomeimmagine> <eseguibile>", argv[0]);
return 1;
}
char *nomeFileC = (char *)malloc(strlen(argv[2])+3);
strcpy(nomeFileC, argv[2]);
strcat(nomeFileC, ".c");
InputStream *in = InputStream_NewForFile(argv[1]);
InputStream *inTemplate = InputStream_NewForFile(EXE_TEMPLATE);
OutputStream *out = OutputStream_NewForFile(nomeFileC);
char pad[BUFFER_LEN];
if(in==NULL) {
printf("Errore in apertura file immagine.\n");
} else if(inTemplate==NULL) {
printf("Errore in apertura file template (%s).\n", EXE_TEMPLATE);
} else {
printf("genero: %s\n", nomeFileC);
//@ @+others
//@+node:leonardoce.20090629082550.223:Scrittura immagine
OutputStream_WriteString(out, "char forthImage[] = {\n");
while(!InputStream_Eof(in)) {
sprintf(pad, "%i,", InputStream_ReadByte(in));
OutputStream_WriteString(out, pad);
}
OutputStream_WriteString(out,"};\n\n");
sprintf(pad, "int forthImageLen = %i;\n\n", in->bufferLen);
OutputStream_WriteString(out, pad);
//@nonl
//@-node:leonardoce.20090629082550.223:Scrittura immagine
//@+node:leonardoce.20090629082550.225:Scrittura template
while(!InputStream_Eof(inTemplate)) {
int letti = InputStream_Read(inTemplate, pad, BUFFER_LEN);
OutputStream_Write(out, pad, letti );
}
//@-node:leonardoce.20090629082550.225:Scrittura template
//@-others
InputStream_Delete(in);
InputStream_Delete(inTemplate);
OutputStream_Delete(out);
//@ <<compilazione>>
//@+node:leonardoce.20090629082550.226:<<compilazione>>
#ifdef OS_WINDOWS
sprintf(pad, "gcc -o %s -DOS_WINDOWS %s -L. -l4th", argv[2], nomeFileC);
#else
sprintf(pad, "gcc -o %s -DOS_LINUX %s -L. -l4th -ldl", argv[2], nomeFileC);
#endif
printf("eseguo: %s\n", pad);
system(pad);
#ifdef OS_WINDOWS
sprintf(pad, "strip -s %s.exe", argv[2]);
#else
sprintf(pad, "strip -s %s", argv[2]);
#endif
printf("eseguo: %s\n", pad);
system(pad);
//@nonl
//@-node:leonardoce.20090629082550.226:<<compilazione>>
//@nl
}
return 0;
}
//@-node:leonardoce.20090629082550.220:@thin forthc.c
//@-leo