52 #include "variables.h" |
52 #include "variables.h" |
53 |
53 |
54 #include "bots.h" |
54 #include "bots.h" |
55 #include "botcommands.h" |
55 #include "botcommands.h" |
56 |
56 |
|
57 bool fexists (char* path) { |
|
58 if (FILE* test = fopen (path, "r")) { |
|
59 fclose (test); |
|
60 return true; |
|
61 } |
|
62 return false; |
|
63 } |
|
64 |
57 int main (int argc, char** argv) { |
65 int main (int argc, char** argv) { |
58 // Intepret command-line parameters: |
66 // Intepret command-line parameters: |
59 // -l: list commands |
67 // -l: list commands |
60 if (argc == 2 && !strcmp (argv[1], "-l")) { |
68 if (argc == 2 && !strcmp (argv[1], "-l")) { |
61 ReadCommands (); |
69 ReadCommands (); |
77 header.appendformat ("%s version %d.%d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION); |
85 header.appendformat ("%s version %d.%d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION); |
78 headerline.repeat ((header.len()/2)-1); |
86 headerline.repeat ((header.len()/2)-1); |
79 headerline += '-'; |
87 headerline += '-'; |
80 printf ("%s\n%s\n", header.chars(), headerline.chars()); |
88 printf ("%s\n%s\n", header.chars(), headerline.chars()); |
81 |
89 |
82 if (argc != 3) { |
90 if (argc < 2) { |
83 fprintf (stderr, "usage: %s: <infile> <outFile>\n", argv[0]); |
91 fprintf (stderr, "usage: %s: <infile> [outfile]\n", argv[0]); |
84 exit (1); |
92 exit (1); |
|
93 } |
|
94 |
|
95 str outfile; |
|
96 if (argc < 3) |
|
97 outfile = ObjectFileName (argv[1]); |
|
98 else |
|
99 outfile = argv[2]; |
|
100 |
|
101 // If we'd end up writing into an existing file, |
|
102 // ask the user if we want to overwrite it |
|
103 if (fexists (outfile)) { |
|
104 // Additional warning if the paths are the same |
|
105 str warning; |
|
106 #ifdef FILE_CASEINSENSITIVE |
|
107 if (!outfile.icompare (argv[1])) |
|
108 #else |
|
109 if (!outfile.compare (argv[1])) |
|
110 #endif |
|
111 { |
|
112 warning = "\nWARNING: Output file is the same as the input file. "; |
|
113 warning += "Answering yes here will destroy the source!\n"; |
|
114 warning += "Continue nevertheless?"; |
|
115 } |
|
116 printf ("output file `%s` already exists! overwrite?%s (y/n) ", outfile.chars(), warning.chars()); |
|
117 |
|
118 char ans; |
|
119 fgets (&ans, 2, stdin); |
|
120 if (ans != 'y') { |
|
121 printf ("abort\n"); |
|
122 exit (1); |
|
123 } |
85 } |
124 } |
86 |
125 |
87 // Read definitions |
126 // Read definitions |
88 printf ("Reading definitions...\n"); |
127 printf ("Reading definitions...\n"); |
89 ReadEvents (); |
128 ReadEvents (); |
93 InitStringTable(); |
132 InitStringTable(); |
94 InitVariables (); |
133 InitVariables (); |
95 |
134 |
96 // Prepare reader and writer |
135 // Prepare reader and writer |
97 ScriptReader *r = new ScriptReader (argv[1]); |
136 ScriptReader *r = new ScriptReader (argv[1]); |
98 ObjWriter *w = new ObjWriter (argv[2]); |
137 ObjWriter *w = new ObjWriter (outfile); |
99 |
138 |
100 // We're set, begin parsing :) |
139 // We're set, begin parsing :) |
101 printf ("Parsing script..\n"); |
140 printf ("Parsing script..\n"); |
102 r->BeginParse (w); |
141 r->BeginParse (w); |
103 |
142 |
104 // Parse done, print statistics |
143 // Parse done, print statistics and write to file |
105 unsigned int globalcount = CountGlobalVars (); |
144 unsigned int globalcount = CountGlobalVars (); |
106 printf ("%u global variable%s\n", globalcount, PLURAL (globalcount)); |
145 printf ("%u global variable%s\n", globalcount, PLURAL (globalcount)); |
107 printf ("%d state%s written\n", g_NumStates, PLURAL (g_NumStates)); |
146 printf ("%d state%s written\n", g_NumStates, PLURAL (g_NumStates)); |
108 printf ("%d event%s written\n", g_NumEvents, PLURAL (g_NumEvents)); |
147 printf ("%d event%s written\n", g_NumEvents, PLURAL (g_NumEvents)); |
109 printf ("-- %u byte%s written to %s\n", w->numWrittenBytes, PLURAL (w->numWrittenBytes), argv[2]); |
148 w->WriteToFile (); |
110 |
149 |
111 // Clear out the junk |
150 // Clear out the junk |
112 delete r; |
151 delete r; |
113 delete w; |
152 delete w; |
114 } |
153 } |
116 void error (const char* text, ...) { |
155 void error (const char* text, ...) { |
117 PERFORM_FORMAT (text, c); |
156 PERFORM_FORMAT (text, c); |
118 fprintf (stderr, "error: %s", c); |
157 fprintf (stderr, "error: %s", c); |
119 exit (1); |
158 exit (1); |
120 } |
159 } |
|
160 |
|
161 char* ObjectFileName (str s) { |
|
162 // Locate the extension and chop it out |
|
163 unsigned int extdot = s.last ("."); |
|
164 if (extdot != s.len() && extdot >= s.len()-4) |
|
165 s.trim (s.len() - extdot); |
|
166 |
|
167 // Add new ".o" extension |
|
168 s += ".o"; |
|
169 |
|
170 return s.chars(); |
|
171 } |