tpr.cxx 13 KB

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