tpr.cxx 13 KB

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