tpr.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 = 0;
  96. SectionType stype = SECTION_NONE;
  97. // Some defaults are different when opening an existing project.
  98. dest->settings.cc_switches="";
  99. dest->settings.as_switches="";
  100. dest->settings.a68k_switches="";
  101. dest->libopts.use_ti89=0;
  102. dest->libopts.use_ti92p=0;
  103. dest->libopts.use_v200=0;
  104. dest->libopts.use_minams=0;
  105. dest->libopts.save_screen=0;
  106. while(!feof(f))
  107. {
  108. char *p;
  109. // Get a line from file
  110. if(read_line(f, buffer, &l) == EOF) break;
  111. // Search for sections
  112. if( !strcmp(buffer, "[Settings]") )
  113. {
  114. stype = SECTION_SETTINGS;
  115. continue;
  116. }
  117. if( !strcmp(buffer, "[Library Options]") )
  118. {
  119. stype = SECTION_LIBOPTS;
  120. continue;
  121. }
  122. if( !strcmp(buffer, "[File Editing]") )
  123. {
  124. stype = SECTION_FILEEDIT;
  125. continue;
  126. }
  127. if( !strcmp(buffer, "[Included Files]") )
  128. {
  129. stype = SECTION_FILES;
  130. continue;
  131. }
  132. // Keywords in the [Settings] section
  133. if(stype == SECTION_SETTINGS) {
  134. #define boolean_param(token,setting) \
  135. if ( (p=find_param(buffer, token)) ) \
  136. { \
  137. if(!strcmp(p, "0")) dest->settings.setting = FALSE; \
  138. else if(!strcmp(p, "1")) dest->settings.setting = TRUE; \
  139. else return l; \
  140. continue; \
  141. } else
  142. #define string_vparam(token,var) \
  143. if ( (p=find_param(buffer, token)) ) \
  144. { \
  145. if (*p) dest->var = p; \
  146. continue; \
  147. } else
  148. #define string_param(token,setting) string_vparam(token,settings.setting)
  149. #define ignore_param(token) \
  150. if( (p=find_param(buffer, token)) ) \
  151. { \
  152. continue; \
  153. } else
  154. boolean_param("Archive=",archive)
  155. boolean_param("Pack=",pack)
  156. string_param("Packed Variable=",pack_name)
  157. string_vparam("Project Name=",prj_name)
  158. string_param("GCC Switches=",cc_switches)
  159. string_param("Assembler Switches=",a68k_switches)
  160. ignore_param("Linker Switches=") // Obsolete. Ignore.
  161. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  162. string_param("GNU Assembler Switches=",as_switches)
  163. ignore_param("BSR Patch=") // Obsolete. Ignore.
  164. boolean_param("Debug Info=",debug_info)
  165. boolean_param("Standard Library=",std_lib)
  166. string_param("Command Line=",cmd_line)
  167. string_param("Post-Build Process=",post_build)
  168. boolean_param("Use Data Variable=",use_data_var)
  169. string_param("Data Variable=",data_var)
  170. boolean_param("Copy Data Variable=",copy_data_var)
  171. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  172. boolean_param("Optimize NOPs=",optimize_nops)
  173. boolean_param("Optimize Returns=",optimize_returns)
  174. boolean_param("Optimize Branches=",optimize_branches)
  175. boolean_param("Optimize Moves=",optimize_moves)
  176. boolean_param("Optimize Tests=",optimize_tests)
  177. boolean_param("Optimize Calculations=",optimize_calcs)
  178. boolean_param("Remove Unused Sections=",remove_unused)
  179. boolean_param("Binary Output=",outputbin)
  180. boolean_param("Fargo=",fargo)
  181. boolean_param("Flash OS=",flash_os)
  182. boolean_param("Cut Unused Ranges=",cut_ranges)
  183. boolean_param("Reorder Sections=",reorder_sections)
  184. boolean_param("Merge Constants=",merge_constants)
  185. boolean_param("Initialize BSS=",initialize_bss)
  186. return l;
  187. #undef boolean_param
  188. #undef string_vparam
  189. #undef string_param
  190. #undef ignore_param
  191. }
  192. // Keywords in the [Library Options] section
  193. if(stype == SECTION_LIBOPTS) {
  194. #define boolean_param(token,setting) \
  195. if ( (p=find_param(buffer, token)) ) \
  196. { \
  197. if(!strcmp(p, "0")) dest->libopts.setting = FALSE; \
  198. else if(!strcmp(p, "1")) dest->libopts.setting = TRUE; \
  199. else return l; \
  200. continue; \
  201. } else
  202. #define reloc_param(token,setting) \
  203. if ( (p=find_param(buffer, token)) ) \
  204. { \
  205. if (!strcmp(p,"None") || !strcmp(p,"AMS") \
  206. || !strcmp(p,"Direct")) \
  207. dest->libopts.setting = RT_NONE; \
  208. else if (!strcmp(p, "Precomputed")) \
  209. dest->libopts.setting = RT_PRECOMP; \
  210. else if (!strcmp(p, "Kernel")) \
  211. dest->libopts.setting = RT_KERNEL; \
  212. else if (!strcmp(p, "Compressed")) \
  213. dest->libopts.setting = RT_COMPRESSED; \
  214. else if (!strcmp(p, "F-Line")) \
  215. dest->libopts.setting = RT_FLINE; \
  216. else if (strcmp(p, "Unknown")) \
  217. return l; \
  218. continue; \
  219. } else
  220. boolean_param("Use TI-89=",use_ti89)
  221. boolean_param("Use TI-92 Plus=",use_ti92p)
  222. boolean_param("Use V200=",use_v200)
  223. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  224. boolean_param("Use Kernel=",use_kernel)
  225. boolean_param("Use PreOS=",use_preos)
  226. boolean_param("Minimum AMS Version Defined=",use_minams)
  227. if ( (p=find_param(buffer, "Minimum AMS Version=")) )
  228. {
  229. int major, minor;
  230. if ((strlen(p)==4) && (sscanf(p,"%1d.%2d",&major,&minor)==2))
  231. dest->libopts.minams = major * 100 + minor;
  232. else
  233. return l;
  234. continue;
  235. } else
  236. boolean_param("Unofficial OS Support=",unofficial_os)
  237. reloc_param("Reloc Format=",reloc_format)
  238. reloc_param("ROM Call Format=",rom_call_format)
  239. reloc_param("BSS Ref Format=",bss_ref_format)
  240. reloc_param("Data Ref Format=",data_ref_format)
  241. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  242. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  243. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  244. boolean_param("Use Return Value=",use_return_value)
  245. boolean_param("Enable Error Return=",enable_error_return)
  246. boolean_param("Save Screen=",save_screen)
  247. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  248. return l;
  249. #undef boolean_param
  250. #undef reloc_param
  251. }
  252. // Keywords in the [File Editing] section
  253. if(stype == SECTION_FILEEDIT)
  254. {
  255. if ( (p=find_param(buffer, "Open File=")) ) \
  256. { \
  257. if (*p) dest->open_file = p; \
  258. continue; \
  259. } else return l;
  260. }
  261. // Keywords in the [Included Files] section
  262. if(stype == SECTION_FILES)
  263. {
  264. int v;
  265. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  266. {
  267. QString s = convert_path_separators(p);
  268. dest->c_files.path << s;
  269. dest->c_files.folder << QString::null;
  270. continue;
  271. }
  272. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  273. {
  274. dest->c_files.folder[v-1]=p;
  275. continue;
  276. }
  277. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  278. {
  279. QString s = convert_path_separators(p);
  280. dest->s_files.path << s;
  281. dest->s_files.folder << QString::null;
  282. continue;
  283. }
  284. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  285. {
  286. dest->s_files.folder[v-1]=p;
  287. continue;
  288. }
  289. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  290. {
  291. QString s = convert_path_separators(p);
  292. dest->h_files.path << s;
  293. dest->h_files.folder << QString::null;
  294. continue;
  295. }
  296. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  297. {
  298. dest->h_files.folder[v-1]=p;
  299. continue;
  300. }
  301. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  302. {
  303. QString s = convert_path_separators(p);
  304. dest->asm_files.path << s;
  305. dest->asm_files.folder << QString::null;
  306. continue;
  307. }
  308. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  309. {
  310. dest->asm_files.folder[v-1]=p;
  311. continue;
  312. }
  313. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  314. {
  315. QString s = convert_path_separators(p);
  316. dest->o_files.path << s;
  317. dest->o_files.folder << QString::null;
  318. continue;
  319. }
  320. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  321. {
  322. dest->o_files.folder[v-1]=p;
  323. continue;
  324. }
  325. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  326. {
  327. QString s = convert_path_separators(p);
  328. dest->a_files.path << s;
  329. dest->a_files.folder << QString::null;
  330. continue;
  331. }
  332. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  333. {
  334. dest->a_files.folder[v-1]=p;
  335. continue;
  336. }
  337. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  338. {
  339. QString s = convert_path_separators(p);
  340. dest->txt_files.path << s;
  341. dest->txt_files.folder << QString::null;
  342. continue;
  343. }
  344. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  345. {
  346. dest->txt_files.folder[v-1]=p;
  347. continue;
  348. }
  349. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  350. {
  351. QString s = convert_path_separators(p);
  352. dest->quill_files.path << s;
  353. dest->quill_files.folder << QString::null;
  354. continue;
  355. }
  356. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  357. {
  358. dest->quill_files.folder[v-1]=p;
  359. continue;
  360. }
  361. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  362. {
  363. QString s = convert_path_separators(p);
  364. dest->oth_files.path << s;
  365. dest->oth_files.folder << QString::null;
  366. continue;
  367. }
  368. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  369. {
  370. dest->oth_files.folder[v-1]=p;
  371. continue;
  372. }
  373. else return l;
  374. }
  375. }
  376. return 0;
  377. }
  378. //returns 0 on success
  379. int loadTPR(QString &fileName,TPRDataStruct *dest)
  380. {
  381. FILE *f;
  382. int ret;
  383. f = fopen(fileName, "r");
  384. if(f == NULL) {
  385. return -1;
  386. }
  387. ret=parse_file(f,dest);
  388. fclose(f);
  389. return ret;
  390. }
  391. QString loadFileText(const char *fileName)
  392. {
  393. FILE *f;
  394. f=fopen(fileName,"rb");
  395. if (!f)
  396. return QString::null;
  397. fseek(f,0,SEEK_END);
  398. size_t flen=ftell(f);
  399. fseek(f,0,SEEK_SET);
  400. char buffer[flen+1];
  401. memset(buffer,0,flen+1);
  402. fread(buffer,1,flen,f);
  403. return QString(buffer);
  404. }