ttppggen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttppggen.c
  5. * initial date: 28/09/2000
  6. * authors: thomas.nussbaumer@gmx.net
  7. * description: executes all steps to convert a TI binary to a PPG file
  8. * (PPG == packed program used in combination with ttstart)
  9. *
  10. ******************************************************************************/
  11. /*
  12. This file is part of TI-68k Developer Utilities.
  13. This file is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. As a special exception, UNMODIFIED copies of ttppggen may also be
  18. redistributed or sold without source code, for any purpose. (The Lesser
  19. General Public License restrictions do apply in other respects; for example,
  20. they cover modification of the program.) This exception notice must be
  21. removed on modified copies of this file.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. Lesser General Public License for more details.
  26. You should have received a copy of the GNU Lesser General Public
  27. License along with this library; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <time.h>
  35. #include "tt.h" // generic defines
  36. #include "ttversion.h" // TI-68k Developer Utilities version info
  37. #include "revtools.h" // automatic cvs version extraction
  38. #ifdef FILE_REVISION
  39. #undef FILE_REVISION
  40. #endif
  41. #define FILE_REVISION "1.8"
  42. #define EMBEDDED_USE
  43. #include "ttbin2oth.c"
  44. #include "packhead.h" // compressed header definition
  45. #include "ttpack.c"
  46. #include "ttstrip.c"
  47. #undef EMBEDDED_USE
  48. //#define NO_KERNEL_PROGS 1 // disables generation of kernel-dependent ppgs
  49. //=============================================================================
  50. // outputs usage information of this tool
  51. //=============================================================================
  52. void PrintUsage() {
  53. PRINT_ID("TTPPGGen");
  54. #ifdef NO_KERNEL_PROGS
  55. fprintf(USAGE_OUT, "Usage: ttppggen [flags] <infile> <varname>\n\n"\
  56. "-quiet ... don't output standard messages\n"\
  57. "-nowarn ... optional flag to turn off signature missmatch warning\n\n"\
  58. "infile ... a valid 89z or 9xz NOSTUB program\n"\
  59. "varname ... name of output ppg (without extension)\n\n"\
  60. "NOTE: the type of file (89y or 9xy) which will be generated depends\n"\
  61. " on the extension of the given infile\n\n");
  62. #else
  63. fprintf(USAGE_OUT, "Usage: ttppggen [flags] <infile> <varname>\n\n"\
  64. "-quiet ... don't output standard messages\n"\
  65. "-nowarn ... optional flag to turn off signature missmatch warning\n\n"\
  66. "infile ... a valid 89z or 9xz program (NOSTUB or kernel-dependent)\n"\
  67. "varname ... name of output ppg (without extension)\n\n"\
  68. "NOTE: the type of file (89y or 9xy) which will be generated depends\n"\
  69. " on the extension of the given infile\n\n");
  70. #endif
  71. }
  72. //=============================================================================
  73. // outputs a message with a special prefix
  74. //=============================================================================
  75. void OutMsg(char*s) {
  76. fprintf(stdout,"%s\n",s);
  77. }
  78. //=============================================================================
  79. // delete temporary files
  80. //=============================================================================
  81. void CleanUp() {
  82. remove("tmp_ppg.bin");
  83. remove("tmp_ppg.pck");
  84. }
  85. //=============================================================================
  86. // as usual: the main entry point ...
  87. //=============================================================================
  88. int main(int argc, char* argv[]) {
  89. char* infilename = NULL;
  90. char* varname = NULL;
  91. //char* description = 0;
  92. int ti89_version = 0;
  93. int no_warn = 0;
  94. int quiet = 0;
  95. char buffer[1000];
  96. int n;
  97. char* tool_params[6];
  98. FILE* fp;
  99. long inputsize;
  100. long outputsize;
  101. if (argc < 3) {
  102. PrintUsage();
  103. return 1;
  104. }
  105. // parse arguments
  106. for (n=1; n<argc; n++) {
  107. if (!strcmp(argv[n], "-quiet")) quiet = 1;
  108. else if (!strcmp(argv[n], "-nowarn")) no_warn = 1;
  109. else if (argv[n][0] == '-') {
  110. fprintf(stderr,"ERROR: invalid option %s",argv[n]);
  111. return 1;
  112. }
  113. else if (!infilename) infilename = argv[n];
  114. else if (!varname) varname = argv[n];
  115. else {
  116. #ifndef EMBEDDED_USE
  117. PrintUsage();
  118. #endif
  119. return 1;
  120. }
  121. }
  122. if (!infilename || !varname) {
  123. PrintUsage();
  124. return 1;
  125. }
  126. if (!quiet) PRINT_ID("TTPPGGen");
  127. //-------------------------------------------------------------------------
  128. // check infilename for minimum length (1 character + ".89z" or ".9xz"
  129. //-------------------------------------------------------------------------
  130. n = strlen(infilename);
  131. if (n<5) {
  132. fprintf(stderr,"ERROR: inputfile name too short\n");
  133. return 1;
  134. }
  135. //-------------------------------------------------------------------------
  136. // check extension of infilename
  137. //-------------------------------------------------------------------------
  138. if (!strncmp(&infilename[n-4],".89z",4)) ti89_version = 1;
  139. else if (!strncmp(&infilename[n-4],".9xz",4)) ti89_version = 0;
  140. else {
  141. fprintf(stderr,"ERROR: infile has neither extension '.89z' nor '.9xz'\n");
  142. return 1;
  143. }
  144. //-------------------------------------------------------------------------
  145. // check signature of infile
  146. //-------------------------------------------------------------------------
  147. if (!(fp = fopen(infilename,"rb"))) {
  148. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infilename);
  149. return 1;
  150. }
  151. if (fread(buffer,8,1,fp) != 1) {
  152. fprintf(stderr,"ERROR: cannot read 8 characters from inputfile %s\n",infilename);
  153. fclose(fp);
  154. return 1;
  155. }
  156. if (!no_warn) {
  157. if (strncmp(buffer,ti89_version ? SIGNATURE_TI89 : SIGNATURE_TI92P,8)) {
  158. fprintf(stderr,"WARNING: calctype signature does not match with extension\n");
  159. // it's just a warning now, because if a file is loaded back from the
  160. // calc to the pc, for both calcs the same signature will be used ... argghh TI
  161. }
  162. }
  163. //-------------------------------------------------------------------------
  164. // check if infile is a DOORS program which cannot be used with ExePack
  165. //-------------------------------------------------------------------------
  166. fseek(fp,92,SEEK_SET);
  167. if (fread(buffer,4,1,fp) != 1) {
  168. fprintf(stderr,"ERROR: cannot extract possible DOORS signature from inputfile %s\n",infilename);
  169. fclose(fp);
  170. return 1;
  171. }
  172. #ifdef NO_KERNEL_PROGS
  173. if (!strncmp(buffer,"68kP",4) || !strncmp(buffer,"68kL",4)) {
  174. fprintf(stderr,"ERROR: Only NOSTUB programs can be used with ExePack !!\n");
  175. fclose(fp);
  176. return 1;
  177. }
  178. #else
  179. if (!strncmp(buffer,"68kL",4)) {
  180. fprintf(stderr,"ERROR: Only programs (no libs) can be used with ExePack !!\n");
  181. fclose(fp);
  182. return 1;
  183. }
  184. #endif
  185. // get input size
  186. fseek(fp,0,SEEK_END);
  187. inputsize = ftell(fp);
  188. fclose(fp);
  189. //--------------------------------------------------------
  190. // stripping pc header
  191. //--------------------------------------------------------
  192. if (!quiet) fprintf(stdout,"stripping pc header .....\n");
  193. tool_params[0] = "";
  194. tool_params[1] = "-quiet";
  195. tool_params[2] = infilename;
  196. tool_params[3] = "tmp_ppg.bin";
  197. if (TTStrip(4,tool_params)) {
  198. CleanUp();
  199. return 1;
  200. }
  201. //--------------------------------------------------------
  202. // packing temporary file
  203. //--------------------------------------------------------
  204. if (!quiet) fprintf(stdout,"packing result .....\n");
  205. tool_params[0] = "";
  206. tool_params[1] = "tmp_ppg.bin";
  207. tool_params[2] = "tmp_ppg.pck";
  208. if (TTPack(3,tool_params)) {
  209. CleanUp();
  210. return 1;
  211. }
  212. //--------------------------------------------------------
  213. // encapsulate packed file into OTH
  214. //--------------------------------------------------------
  215. if (!quiet) fprintf(stdout,"convert packed file to OTH file .....\n");
  216. if (ti89_version) tool_params[2] = "-89";
  217. else tool_params[2] = "-92";
  218. tool_params[0] = "";
  219. tool_params[1] = "-quiet";
  220. tool_params[3] = "ppg";
  221. tool_params[4] = "tmp_ppg.pck";
  222. tool_params[5] = varname;
  223. if (TTBin2OTH(6,tool_params)) {
  224. CleanUp();
  225. return 1;
  226. }
  227. // get output size
  228. if (ti89_version) sprintf(buffer,"%s.89y",varname);
  229. else sprintf(buffer,"%s.9xy",varname);
  230. if (!quiet) {
  231. if ((fp=fopen(buffer,"rb"))) {
  232. fseek(fp,0,SEEK_END);
  233. outputsize = ftell(fp);
  234. fclose(fp);
  235. fprintf(stdout,"%s (size=%ld) written to %s (size=%ld) - ratio=%lf\n",
  236. infilename,inputsize,buffer,outputsize,(double)outputsize/(double)inputsize);
  237. }
  238. else {
  239. fprintf(stderr,"ERROR: cannot open successully generated file? Should NEVER happen\n");
  240. CleanUp();
  241. return 1;
  242. }
  243. }
  244. CleanUp();
  245. return 0;
  246. }
  247. //#############################################################################
  248. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  249. //#############################################################################
  250. //
  251. //=============================================================================
  252. // Revision History
  253. //=============================================================================
  254. //
  255. // Revision 1.8 2009/01/25 Lionel Debroux
  256. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  257. // Adapt to new version display (revtools.h).
  258. //
  259. // Revision 1.7 2002/03/13 21:32:25 tnussb
  260. // (1) parameter handling of -quiet added
  261. // (2) use "-quiet" as parameter for ttbin2oth and ttstrip calls
  262. // (3) printing of statistics if successfully completes added
  263. // (4) checking of inputfile extension fixed
  264. // (5) usage text modified
  265. //
  266. // Revision 1.6 2002/03/04 14:35:02 tnussb
  267. // No use of external TIGCC Tools anymore, but using now compiled-in versions.
  268. // This way this tool can be distributed by its own without the need of a
  269. // TIGCC Tools Suite installation.
  270. //
  271. // Revision 1.5 2002/02/07 09:49:37 tnussb
  272. // all local includes changed, because header files are now located in pctools folder
  273. //
  274. // Revision 1.4 2002/02/07 09:15:45 tnussb
  275. // conversion of kernelbased programs enabled
  276. //
  277. // Revision 1.2 2000/11/28 00:08:29 Thomas Nussbaumer
  278. // using now USAGE_OUT stream for usage info
  279. //
  280. // Revision 1.1 2000/10/01 15:01:59 Thomas Nussbaumer
  281. // initial version
  282. //