tpr.cxx 13 KB

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