tpr.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. tpr handling routines adapted from tprbuilder
  4. Copyright (C) 2002 Romain Liévin
  5. Copyright (C) 2002-2006 Kevin Kofler
  6. Copyright (C) 2006 Joey Adams
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software Foundation,
  17. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <unistd.h>
  22. #include <kapplication.h>
  23. #include <kcmdlineargs.h>
  24. #include <kaboutdata.h>
  25. #include <qtextcodec.h>
  26. #include "tpr.h"
  27. #define find_param(s_,t_) find_param_ex((s_),(t_),sizeof(t_)-1)
  28. char *find_param_ex(char *s, const char *t, size_t l)
  29. {
  30. if (!strncmp(s,t,l))
  31. return s+l;
  32. else
  33. return NULL;
  34. }
  35. char *find_numbered_param(char *s, const char*t, int *i)
  36. {
  37. char *p;
  38. int endpos = 0;
  39. int ret = 0;
  40. char arglist[256];
  41. strcpy(arglist, t);
  42. strcat(arglist, "%n");
  43. ret = sscanf(s, arglist, i, &endpos);
  44. if(ret < 1 || !endpos) return NULL;
  45. p = s + endpos;
  46. return p;
  47. }
  48. // converts Windows paths to Unix paths if necessary.
  49. QString convert_path_separators(const char *file)
  50. {
  51. QString s=file;
  52. int o;
  53. #ifndef __WIN32__
  54. while ((o=s.find('\\',0,TRUE))>=0)
  55. s[o]='/';
  56. #endif
  57. return s;
  58. }
  59. char* strip(char *str)
  60. {
  61. int len = strlen(str);
  62. if(len > 0)
  63. if( (str[len-1] == '\r') || (str[len-1] == '\n') )
  64. str[len-1] = '\0';
  65. if(len > 1)
  66. if( (str[len-2] == '\r') || (str[len-2] == '\n') )
  67. str[len-2] = '\0';
  68. return str;
  69. }
  70. /*
  71. Read a line from file and do a clean-up
  72. */
  73. int read_line(FILE *f, char *buffer, int *l)
  74. {
  75. strcpy(buffer, "");
  76. if(feof(f)) return EOF;
  77. fgets(buffer, 256, f);
  78. strip(buffer);
  79. (*l)++;
  80. while( (buffer[0] == '\r') || (buffer[0] == '\n') || (buffer[0] == '\0') )
  81. {
  82. if(feof(f)) return EOF;
  83. fgets(buffer, 256, f);
  84. strip(buffer);
  85. (*l)++;
  86. }
  87. return 0;
  88. }
  89. //some items from the TSR might still be ignored or not shown here.
  90. //if you need one of them, add an entry to one of the
  91. //definition "tables".
  92. int parse_file(FILE *f,TPRDataStruct *dest)
  93. {
  94. char buffer[256];
  95. int l = 1;
  96. SectionType stype = SECTION_NONE;
  97. while(!feof(f))
  98. {
  99. char *p;
  100. // Get a line from file
  101. if(read_line(f, buffer, &l) == EOF) break;
  102. // Search for sections
  103. if( !strcmp(buffer, "[Settings]") )
  104. {
  105. stype = SECTION_SETTINGS;
  106. continue;
  107. }
  108. if( !strcmp(buffer, "[Library Options]") )
  109. {
  110. stype = SECTION_LIBOPTS;
  111. continue;
  112. }
  113. if( !strcmp(buffer, "[File Editing]") )
  114. {
  115. stype = SECTION_FILEEDIT;
  116. continue;
  117. }
  118. if( !strcmp(buffer, "[Included Files]") )
  119. {
  120. stype = SECTION_FILES;
  121. continue;
  122. }
  123. // Keywords in the [Settings] section
  124. if(stype == SECTION_SETTINGS) {
  125. #define boolean_param(token,setting) \
  126. if ( (p=find_param(buffer, token)) ) \
  127. { \
  128. if(!strcmp(p, "0")) dest->settings.setting = FALSE; \
  129. else if(!strcmp(p, "1")) dest->settings.setting = TRUE; \
  130. else return l; \
  131. continue; \
  132. } else
  133. #define string_vparam(token,var) \
  134. if ( (p=find_param(buffer, token)) ) \
  135. { \
  136. if (*p) dest->var = p; \
  137. continue; \
  138. } else
  139. #define string_param(token,setting) string_vparam(token,settings.setting)
  140. #define ignore_param(token) \
  141. if( (p=find_param(buffer, token)) ) \
  142. { \
  143. continue; \
  144. } else
  145. boolean_param("Archive=",archive)
  146. boolean_param("Pack=",pack)
  147. string_param("Packed Variable=",pack_name)
  148. string_vparam("Project Name=",prj_name)
  149. string_param("GCC Switches=",cc_switches)
  150. string_param("Assembler Switches=",a68k_switches)
  151. ignore_param("Linker Switches=") // Obsolete. Ignore.
  152. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  153. string_param("GNU Assembler Switches=",as_switches)
  154. ignore_param("BSR Patch=") // Obsolete. Ignore.
  155. boolean_param("Debug Info=",debug_info)
  156. boolean_param("Standard Library=",std_lib)
  157. string_param("Command Line=",cmd_line)
  158. string_param("Post-Build Process=",post_build)
  159. boolean_param("Use Data Variable=",use_data_var)
  160. string_param("Data Variable=",data_var)
  161. boolean_param("Copy Data Variable=",copy_data_var)
  162. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  163. boolean_param("Optimize NOPs=",optimize_nops)
  164. boolean_param("Optimize Returns=",optimize_returns)
  165. boolean_param("Optimize Branches=",optimize_branches)
  166. boolean_param("Optimize Moves=",optimize_moves)
  167. boolean_param("Optimize Tests=",optimize_tests)
  168. boolean_param("Optimize Calculations=",optimize_calcs)
  169. boolean_param("Remove Unused Sections=",remove_unused)
  170. boolean_param("Binary Output=",outputbin)
  171. boolean_param("Fargo=",fargo)
  172. boolean_param("Flash OS=",flash_os)
  173. boolean_param("Cut Unused Ranges=",cut_ranges)
  174. boolean_param("Reorder Sections=",reorder_sections)
  175. boolean_param("Merge Constants=",merge_constants)
  176. boolean_param("Initialize BSS=",initialize_bss)
  177. return l;
  178. #undef boolean_param
  179. #undef string_vparam
  180. #undef string_param
  181. #undef ignore_param
  182. }
  183. // Keywords in the [Library Options] section
  184. if(stype == SECTION_LIBOPTS) {
  185. #define boolean_param(token,setting) \
  186. if ( (p=find_param(buffer, token)) ) \
  187. { \
  188. if(!strcmp(p, "0")) dest->libopts.setting = FALSE; \
  189. else if(!strcmp(p, "1")) dest->libopts.setting = TRUE; \
  190. else return l; \
  191. continue; \
  192. } else
  193. #define reloc_param(token,setting) \
  194. if ( (p=find_param(buffer, token)) ) \
  195. { \
  196. if (!strcmp(p,"None") || !strcmp(p,"AMS") \
  197. || !strcmp(p,"Direct")) \
  198. dest->libopts.setting = RT_NONE; \
  199. else if (!strcmp(p, "Precomputed")) \
  200. dest->libopts.setting = RT_PRECOMP; \
  201. else if (!strcmp(p, "Kernel")) \
  202. dest->libopts.setting = RT_KERNEL; \
  203. else if (!strcmp(p, "Compressed")) \
  204. dest->libopts.setting = RT_COMPRESSED; \
  205. else if (!strcmp(p, "F-Line")) \
  206. dest->libopts.setting = RT_FLINE; \
  207. else if (strcmp(p, "Unknown")) \
  208. return l; \
  209. continue; \
  210. } else
  211. boolean_param("Use TI-89=",use_ti89)
  212. boolean_param("Use TI-92 Plus=",use_ti92p)
  213. boolean_param("Use V200=",use_v200)
  214. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  215. boolean_param("Use Kernel=",use_kernel)
  216. boolean_param("Use PreOS=",use_preos)
  217. boolean_param("Minimum AMS Version Defined=",use_minams)
  218. if ( (p=find_param(buffer, "Minimum AMS Version=")) )
  219. {
  220. int major, minor;
  221. if ((strlen(p)==4) && (sscanf(p,"%1d.%2d",&major,&minor)==2))
  222. dest->libopts.minams = major * 100 + minor;
  223. else
  224. return l;
  225. continue;
  226. } else
  227. boolean_param("Unofficial OS Support=",unofficial_os)
  228. reloc_param("Reloc Format=",reloc_format)
  229. reloc_param("ROM Call Format=",rom_call_format)
  230. reloc_param("BSS Ref Format=",bss_ref_format)
  231. reloc_param("Data Ref Format=",data_ref_format)
  232. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  233. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  234. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  235. boolean_param("Use Return Value=",use_return_value)
  236. boolean_param("Enable Error Return=",enable_error_return)
  237. boolean_param("Save Screen=",save_screen)
  238. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  239. return l;
  240. #undef boolean_param
  241. #undef reloc_param
  242. }
  243. // Ignore [File Editing] section, it is used only for editing.
  244. // Keywords in the [Included Files] section
  245. if(stype == SECTION_FILES)
  246. {
  247. int v;
  248. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  249. {
  250. QString s = convert_path_separators(p);
  251. dest->c_files.path << s;
  252. dest->c_files.folder << QString::null;
  253. continue;
  254. }
  255. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  256. {
  257. continue;
  258. }
  259. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  260. {
  261. QString s = convert_path_separators(p);
  262. dest->s_files.path << s;
  263. dest->s_files.folder << QString::null;
  264. continue;
  265. }
  266. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  267. { // ignore folder specification for now
  268. continue;
  269. }
  270. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  271. {
  272. QString s = convert_path_separators(p);
  273. dest->h_files.path << s;
  274. dest->h_files.folder << QString::null;
  275. continue;
  276. }
  277. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  278. { // ignore folder specification for now
  279. continue;
  280. }
  281. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  282. {
  283. QString s = convert_path_separators(p);
  284. dest->asm_files.path << s;
  285. dest->asm_files.folder << QString::null;
  286. continue;
  287. }
  288. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  289. { // ignore folder specification for now
  290. continue;
  291. }
  292. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  293. {
  294. QString s = convert_path_separators(p);
  295. dest->o_files.path << s;
  296. dest->o_files.folder << QString::null;
  297. continue;
  298. }
  299. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  300. { // ignore folder specification for now
  301. continue;
  302. }
  303. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  304. {
  305. QString s = convert_path_separators(p);
  306. dest->a_files.path << s;
  307. dest->a_files.folder << QString::null;
  308. continue;
  309. }
  310. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  311. { // ignore folder specification for now
  312. continue;
  313. }
  314. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  315. {
  316. QString s = convert_path_separators(p);
  317. dest->txt_files.path << s;
  318. dest->txt_files.folder << QString::null;
  319. continue;
  320. }
  321. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  322. { // ignore folder specification for now
  323. continue;
  324. }
  325. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  326. {
  327. QString s = convert_path_separators(p);
  328. dest->quill_files.path << s;
  329. dest->quill_files.folder << QString::null;
  330. continue;
  331. }
  332. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  333. { // ignore folder specification for now
  334. continue;
  335. }
  336. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  337. {
  338. QString s = convert_path_separators(p);
  339. dest->oth_files.path << s;
  340. dest->oth_files.folder << QString::null;
  341. continue;
  342. }
  343. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  344. { // ignore folder specification for now
  345. continue;
  346. }
  347. else return 1;
  348. }
  349. }
  350. return 0;
  351. }
  352. //returns 0 on success
  353. int loadTPR(QString &fileName,TPRDataStruct *dest)
  354. {
  355. FILE *f;
  356. int ret;
  357. f = fopen(fileName, "r");
  358. if(f == NULL) {
  359. return -1;
  360. }
  361. ret=parse_file(f,dest);
  362. fclose(f);
  363. return ret;
  364. }
  365. QString loadFileText(const char *fileName)
  366. {
  367. FILE *f;
  368. f=fopen(fileName,"rb");
  369. if (!f)
  370. return QString::null;
  371. fseek(f,0,SEEK_END);
  372. size_t flen=ftell(f);
  373. fseek(f,0,SEEK_SET);
  374. char buffer[flen+1];
  375. memset(buffer,0,flen+1);
  376. fread(buffer,1,flen,f);
  377. return QString(buffer);
  378. }