tpr.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cerrno>
  22. #include <cstring>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <sys/dir.h>
  26. #include <qapplication.h>
  27. #include <qeventloop.h>
  28. #include <kapplication.h>
  29. #include <kcmdlineargs.h>
  30. #include <kaboutdata.h>
  31. #include <qstring.h>
  32. #include <qstringlist.h>
  33. #include <qcstring.h>
  34. #include <qregexp.h>
  35. #include <qtextcodec.h>
  36. #include <glib.h>
  37. #include <ticonv.h>
  38. #include "ktigcc.h"
  39. #include "tpr.h"
  40. #include "preferences.h"
  41. #define find_param(s_,t_) find_param_ex((s_),(t_),sizeof(t_)-1)
  42. static char *find_param_ex(char *s, const char *t, size_t l)
  43. {
  44. if (!strncmp(s,t,l))
  45. return s+l;
  46. else
  47. return NULL;
  48. }
  49. static char *find_numbered_param(char *s, const char*t, int *i)
  50. {
  51. char *p;
  52. int endpos = 0;
  53. int ret = 0;
  54. char arglist[256];
  55. strcpy(arglist, t);
  56. strcat(arglist, "%n");
  57. ret = sscanf(s, arglist, i, &endpos);
  58. if(ret < 1 || !endpos) return NULL;
  59. p = s + endpos;
  60. return p;
  61. }
  62. // converts Windows paths to Unix paths if necessary.
  63. static QString convert_path_separators(const char *file)
  64. {
  65. QString s=file;
  66. int o;
  67. #ifndef __WIN32__
  68. while ((o=s.find('\\',0,TRUE))>=0)
  69. s[o]='/';
  70. #endif
  71. return s;
  72. }
  73. static char* strip(char *str)
  74. {
  75. int len = strlen(str);
  76. if(len > 0)
  77. if( (str[len-1] == '\r') || (str[len-1] == '\n') )
  78. str[len-1] = '\0';
  79. if(len > 1)
  80. if( (str[len-2] == '\r') || (str[len-2] == '\n') )
  81. str[len-2] = '\0';
  82. return str;
  83. }
  84. /*
  85. Read a line from file and do a clean-up
  86. */
  87. static int read_line(FILE *f, char *buffer, int *l)
  88. {
  89. strcpy(buffer, "");
  90. if(feof(f)) return EOF;
  91. if (!fgets(buffer, 256, f)) return feof(f)?EOF:1;
  92. strip(buffer);
  93. (*l)++;
  94. while( (buffer[0] == '\r') || (buffer[0] == '\n') || (buffer[0] == '\0') )
  95. {
  96. if(feof(f)) return EOF;
  97. if (!fgets(buffer, 256, f)) return feof(f)?EOF:1;
  98. strip(buffer);
  99. (*l)++;
  100. }
  101. return 0;
  102. }
  103. //some items from the TSR might still be ignored or not shown here.
  104. //if you need one of them, add an entry to one of the
  105. //definition "tables".
  106. //If you add an entry here, don't forget to add something to
  107. // save_tpr!
  108. static int parse_file(FILE *f,TPRDataStruct *dest)
  109. {
  110. char buffer[256];
  111. int l = 0;
  112. SectionType stype = SECTION_NONE;
  113. // Some defaults are different when opening an existing project.
  114. dest->settings.cc_switches="";
  115. dest->settings.as_switches="";
  116. dest->settings.a68k_switches="";
  117. dest->libopts.use_ti89=0;
  118. dest->libopts.use_ti92p=0;
  119. dest->libopts.use_v200=0;
  120. dest->libopts.use_minams=0;
  121. dest->libopts.save_screen=0;
  122. while(!feof(f))
  123. {
  124. char *p;
  125. // Get a line from file
  126. int result=read_line(f, buffer, &l);
  127. if (result == 1) return -1; // error
  128. if (result == EOF) break;
  129. // Search for sections
  130. if( !strcmp(buffer, "[Settings]") )
  131. {
  132. stype = SECTION_SETTINGS;
  133. continue;
  134. }
  135. if( !strcmp(buffer, "[Library Options]") )
  136. {
  137. stype = SECTION_LIBOPTS;
  138. continue;
  139. }
  140. if( !strcmp(buffer, "[File Editing]") )
  141. {
  142. stype = SECTION_FILEEDIT;
  143. continue;
  144. }
  145. if( !strcmp(buffer, "[Included Files]") )
  146. {
  147. stype = SECTION_FILES;
  148. continue;
  149. }
  150. // Keywords in the [Settings] section
  151. if(stype == SECTION_SETTINGS) {
  152. #define boolean_param(token,setting) \
  153. if ( (p=find_param(buffer, token)) ) \
  154. { \
  155. if(!strcmp(p, "0")) dest->settings.setting = FALSE; \
  156. else if(!strcmp(p, "1")) dest->settings.setting = TRUE; \
  157. else return l; \
  158. continue; \
  159. } else
  160. #define tistring_vparam(token,var) \
  161. if ( (p=find_param(buffer, token)) ) \
  162. { \
  163. if (*p) { \
  164. unsigned short *utf16=ticonv_charset_ti_to_utf16(CALC_TI89,p); \
  165. dest->var = QString::fromUcs2(utf16); \
  166. g_free(utf16); \
  167. } \
  168. continue; \
  169. } else
  170. #define tistring_param(token,setting) tistring_vparam(token,settings.setting)
  171. #define string_param(token,setting) \
  172. if ( (p=find_param(buffer, token)) ) \
  173. { \
  174. if (*p) dest->settings.setting = p; \
  175. continue; \
  176. } else
  177. #define ignore_param(token) \
  178. if( (p=find_param(buffer, token)) ) \
  179. { \
  180. continue; \
  181. } else
  182. boolean_param("Archive=",archive)
  183. boolean_param("Pack=",pack)
  184. tistring_param("Packed Variable=",pack_name)
  185. tistring_vparam("Project Name=",prj_name)
  186. string_param("GCC Switches=",cc_switches)
  187. string_param("Assembler Switches=",a68k_switches)
  188. ignore_param("Linker Switches=") // Obsolete. Ignore.
  189. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  190. string_param("GNU Assembler Switches=",as_switches)
  191. ignore_param("BSR Patch=") // Obsolete. Ignore.
  192. boolean_param("Debug Info=",debug_info)
  193. boolean_param("Standard Library=",std_lib)
  194. tistring_param("Command Line=",cmd_line)
  195. string_param("Post-Build Process=",post_build)
  196. boolean_param("Use Data Variable=",use_data_var)
  197. tistring_param("Data Variable=",data_var)
  198. boolean_param("Copy Data Variable=",copy_data_var)
  199. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  200. boolean_param("Optimize NOPs=",optimize_nops)
  201. boolean_param("Optimize Returns=",optimize_returns)
  202. boolean_param("Optimize Branches=",optimize_branches)
  203. boolean_param("Optimize Moves=",optimize_moves)
  204. boolean_param("Optimize Tests=",optimize_tests)
  205. boolean_param("Optimize Calculations=",optimize_calcs)
  206. boolean_param("Remove Unused Sections=",remove_unused)
  207. boolean_param("Binary Output=",outputbin)
  208. boolean_param("Fargo=",fargo)
  209. boolean_param("Flash OS=",flash_os)
  210. boolean_param("Cut Unused Ranges=",cut_ranges)
  211. boolean_param("Reorder Sections=",reorder_sections)
  212. boolean_param("Merge Constants=",merge_constants)
  213. boolean_param("Initialize BSS=",initialize_bss)
  214. return l;
  215. #undef boolean_param
  216. #undef tistring_vparam
  217. #undef tistring_param
  218. #undef string_param
  219. #undef ignore_param
  220. }
  221. // Keywords in the [Library Options] section
  222. if(stype == SECTION_LIBOPTS) {
  223. #define boolean_param(token,setting) \
  224. if ( (p=find_param(buffer, token)) ) \
  225. { \
  226. if(!strcmp(p, "0")) dest->libopts.setting = FALSE; \
  227. else if(!strcmp(p, "1")) dest->libopts.setting = TRUE; \
  228. else return l; \
  229. continue; \
  230. } else
  231. #define reloc_param(token,setting) \
  232. if ( (p=find_param(buffer, token)) ) \
  233. { \
  234. if (!strcmp(p,"None")) \
  235. dest->libopts.setting = RT_NONE; \
  236. else if (!strcmp(p,"Direct")) \
  237. dest->libopts.setting = RT_DIRECT; \
  238. else if (!strcmp(p,"AMS")) \
  239. dest->libopts.setting = RT_AMS; \
  240. else if (!strcmp(p, "Precomputed")) \
  241. dest->libopts.setting = RT_PRECOMP; \
  242. else if (!strcmp(p, "Kernel")) \
  243. dest->libopts.setting = RT_KERNEL; \
  244. else if (!strcmp(p, "Compressed")) \
  245. dest->libopts.setting = RT_COMPRESSED; \
  246. else if (!strcmp(p, "MLink")) \
  247. dest->libopts.setting = RT_MLINK; \
  248. else if (!strcmp(p, "F-Line")) \
  249. dest->libopts.setting = RT_FLINE; \
  250. else if (strcmp(p, "Unknown")) \
  251. return l; \
  252. continue; \
  253. } else
  254. //this new macro was added to make merging with save_tpr more consistent.
  255. #define minams_param(token,setting) \
  256. if ( (p=find_param(buffer, "Minimum AMS Version=")) ) \
  257. { \
  258. int major, minor; \
  259. if ((strlen(p)==4) \
  260. && (sscanf(p,"%1d.%2d",&major,&minor)==2)) \
  261. dest->libopts.minams = major * 100 + minor; \
  262. else \
  263. return l; \
  264. continue; \
  265. } else
  266. boolean_param("Use TI-89=",use_ti89)
  267. boolean_param("Use TI-92 Plus=",use_ti92p)
  268. boolean_param("Use V200=",use_v200)
  269. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  270. boolean_param("Use Kernel=",use_kernel)
  271. boolean_param("Use PreOS=",use_preos)
  272. boolean_param("Minimum AMS Version Defined=",use_minams)
  273. minams_param("Minimum AMS Version=",minams);
  274. boolean_param("Unofficial OS Support=",unofficial_os)
  275. reloc_param("Reloc Format=",reloc_format)
  276. reloc_param("ROM Call Format=",rom_call_format)
  277. reloc_param("BSS Ref Format=",bss_ref_format)
  278. reloc_param("Data Ref Format=",data_ref_format)
  279. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  280. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  281. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  282. boolean_param("Use Return Value=",use_return_value)
  283. boolean_param("Enable Error Return=",enable_error_return)
  284. boolean_param("Save Screen=",save_screen)
  285. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  286. return l;
  287. #undef boolean_param
  288. #undef reloc_param
  289. #undef minams_param
  290. }
  291. // Keywords in the [File Editing] section
  292. if(stype == SECTION_FILEEDIT)
  293. {
  294. if ( (p=find_param(buffer, "Open File=")) ) \
  295. { \
  296. if (*p) dest->open_file = p; \
  297. continue; \
  298. } else return l;
  299. }
  300. // Keywords in the [Included Files] section
  301. if(stype == SECTION_FILES)
  302. {
  303. int v;
  304. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  305. {
  306. QString s = convert_path_separators(p);
  307. dest->c_files.path << s;
  308. dest->c_files.folder << QString::null;
  309. continue;
  310. }
  311. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  312. {
  313. dest->c_files.folder[v-1]=p;
  314. continue;
  315. }
  316. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  317. {
  318. QString s = convert_path_separators(p);
  319. dest->s_files.path << s;
  320. dest->s_files.folder << QString::null;
  321. continue;
  322. }
  323. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  324. {
  325. dest->s_files.folder[v-1]=p;
  326. continue;
  327. }
  328. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  329. {
  330. QString s = convert_path_separators(p);
  331. dest->h_files.path << s;
  332. dest->h_files.folder << QString::null;
  333. continue;
  334. }
  335. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  336. {
  337. dest->h_files.folder[v-1]=p;
  338. continue;
  339. }
  340. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  341. {
  342. QString s = convert_path_separators(p);
  343. dest->asm_files.path << s;
  344. dest->asm_files.folder << QString::null;
  345. continue;
  346. }
  347. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  348. {
  349. dest->asm_files.folder[v-1]=p;
  350. continue;
  351. }
  352. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  353. {
  354. QString s = convert_path_separators(p);
  355. dest->o_files.path << s;
  356. dest->o_files.folder << QString::null;
  357. continue;
  358. }
  359. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  360. {
  361. dest->o_files.folder[v-1]=p;
  362. continue;
  363. }
  364. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  365. {
  366. QString s = convert_path_separators(p);
  367. dest->a_files.path << s;
  368. dest->a_files.folder << QString::null;
  369. continue;
  370. }
  371. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  372. {
  373. dest->a_files.folder[v-1]=p;
  374. continue;
  375. }
  376. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  377. {
  378. QString s = convert_path_separators(p);
  379. dest->txt_files.path << s;
  380. dest->txt_files.folder << QString::null;
  381. continue;
  382. }
  383. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  384. {
  385. dest->txt_files.folder[v-1]=p;
  386. continue;
  387. }
  388. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  389. {
  390. QString s = convert_path_separators(p);
  391. dest->quill_files.path << s;
  392. dest->quill_files.folder << QString::null;
  393. continue;
  394. }
  395. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  396. {
  397. dest->quill_files.folder[v-1]=p;
  398. continue;
  399. }
  400. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  401. {
  402. QString s = convert_path_separators(p);
  403. dest->oth_files.path << s;
  404. dest->oth_files.folder << QString::null;
  405. continue;
  406. }
  407. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  408. {
  409. dest->oth_files.folder[v-1]=p;
  410. continue;
  411. }
  412. else return l;
  413. }
  414. }
  415. return 0;
  416. }
  417. // returns 0 on success, -1 if it can't open the TPR and a (positive) line
  418. // number if there is an error in the TPR
  419. int loadTPR(const QString &fileName,TPRDataStruct *dest)
  420. {
  421. FILE *f;
  422. int ret;
  423. f = fopen(fileName, "r");
  424. if(f == NULL) {
  425. return -1;
  426. }
  427. ret=parse_file(f,dest);
  428. fclose(f);
  429. return ret;
  430. }
  431. QString loadFileText(const char *fileName)
  432. {
  433. FILE *f;
  434. f=fopen(fileName,"rb");
  435. if (!f)
  436. return QString::null;
  437. fseek(f,0,SEEK_END);
  438. size_t flen=ftell(f);
  439. fseek(f,0,SEEK_SET);
  440. char *buffer = new(std::nothrow) char[flen+1]();
  441. if (!buffer)
  442. return QString::null;
  443. QString ret;
  444. if (fread(buffer,1,flen,f)<flen) {
  445. delete[] buffer;
  446. ret=QString::null;
  447. } else {
  448. if (preferences.useCalcCharset) {
  449. unsigned short *utf16=ticonv_charset_ti_to_utf16(CALC_TI89,buffer);
  450. delete[] buffer;
  451. if (!utf16)
  452. return QString::null;
  453. ret=QString::fromUcs2(utf16);
  454. g_free(utf16);
  455. } else {
  456. ret=buffer;
  457. delete[] buffer;
  458. }
  459. }
  460. if (!ret.isNull()) {
  461. // convert Windows line endings
  462. ret=ret.replace("\r\n","\n");
  463. // interpret remaining \r characters as Mac line endings
  464. ret=ret.replace('\r','\n');
  465. // remove trailing spaces if requested
  466. if (preferences.removeTrailingSpaces) {
  467. ret=ret.replace(QRegExp("((?!\n)\\s)*\n"),"\n");
  468. ret=ret.remove(QRegExp("((?!\n)\\s)*$"));
  469. }
  470. }
  471. return ret;
  472. }
  473. static QString convert_path_separators_save(QString s)
  474. {
  475. int o;
  476. #ifndef __WIN32__
  477. while ((o=s.find('/',0,TRUE))>=0)
  478. s[o]='\\';
  479. #endif
  480. return s;
  481. }
  482. //this is here because QString::ascii() returns "(null)" on a null string.
  483. const char *smartAscii(const QString &s)
  484. {
  485. if (s.isNull())
  486. return "";
  487. return s.ascii();
  488. }
  489. static int save_tpr(FILE *f,TPRDataStruct *dest)
  490. {
  491. unsigned i=0,e;
  492. QString tmp;
  493. #define boolean_param(token,setting) if (fprintf(f,token "%d\r\n",!!dest->settings.setting)<0) return -2;
  494. #define tistring_vparam(token,var) { \
  495. const unsigned short *utf16=dest->var.ucs2(); \
  496. char *ti=utf16?ticonv_charset_utf16_to_ti(CALC_TI89,utf16):reinterpret_cast<char*>(g_malloc0(1)); \
  497. if (fprintf(f,token "%s\r\n",ti)<0) {g_free(ti); return -2;} \
  498. g_free(ti); \
  499. }
  500. #define tistring_param(token,setting) tistring_vparam(token,settings.setting)
  501. #define string_param(token,setting) if (fprintf(f,token "%s\r\n",smartAscii(dest->settings.setting))<0) return -2;
  502. #define ignore_param(token) /**/
  503. if (fputs("[Settings]\r\n",f)<0) return -2;
  504. boolean_param("Archive=",archive)
  505. boolean_param("Pack=",pack)
  506. tistring_param("Packed Variable=",pack_name)
  507. tistring_vparam("Project Name=",prj_name)
  508. string_param("GCC Switches=",cc_switches)
  509. string_param("Assembler Switches=",a68k_switches)
  510. ignore_param("Linker Switches=") // Obsolete. Ignore.
  511. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  512. string_param("GNU Assembler Switches=",as_switches)
  513. ignore_param("BSR Patch=") // Obsolete. Ignore.
  514. boolean_param("Debug Info=",debug_info)
  515. boolean_param("Standard Library=",std_lib)
  516. tistring_param("Command Line=",cmd_line)
  517. string_param("Post-Build Process=",post_build)
  518. boolean_param("Use Data Variable=",use_data_var)
  519. tistring_param("Data Variable=",data_var)
  520. boolean_param("Copy Data Variable=",copy_data_var)
  521. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  522. boolean_param("Optimize NOPs=",optimize_nops)
  523. boolean_param("Optimize Returns=",optimize_returns)
  524. boolean_param("Optimize Branches=",optimize_branches)
  525. boolean_param("Optimize Moves=",optimize_moves)
  526. boolean_param("Optimize Tests=",optimize_tests)
  527. boolean_param("Optimize Calculations=",optimize_calcs)
  528. boolean_param("Remove Unused Sections=",remove_unused)
  529. boolean_param("Binary Output=",outputbin)
  530. boolean_param("Fargo=",fargo)
  531. boolean_param("Flash OS=",flash_os)
  532. boolean_param("Cut Unused Ranges=",cut_ranges)
  533. boolean_param("Reorder Sections=",reorder_sections)
  534. boolean_param("Merge Constants=",merge_constants)
  535. boolean_param("Initialize BSS=",initialize_bss)
  536. #undef boolean_param
  537. #undef tistring_vparam
  538. #undef tistring_param
  539. #undef string_param
  540. #undef ignore_param
  541. #define boolean_param(token,setting) if (fprintf(f,token "%d\r\n",!!dest->libopts.setting)<0) return -2;
  542. #define reloc_param(token,setting) \
  543. switch(dest->libopts.setting) \
  544. { \
  545. case RT_NONE: \
  546. if (fprintf(f,token "None\r\n")<0) return -2; \
  547. break; \
  548. case RT_DIRECT: \
  549. if (fprintf(f,token "Direct\r\n")<0) return -2; \
  550. break; \
  551. case RT_AMS: \
  552. if (fprintf(f,token "AMS\r\n")<0) return -2; \
  553. break; \
  554. case RT_PRECOMP: \
  555. if (fprintf(f,token "Precomputed\r\n")<0) return -2; \
  556. break; \
  557. case RT_KERNEL: \
  558. if (fprintf(f,token "Kernel\r\n")<0) return -2; \
  559. break; \
  560. case RT_COMPRESSED: \
  561. if (fprintf(f,token "Compressed\r\n")<0) return -2; \
  562. break; \
  563. case RT_MLINK: \
  564. if (fprintf(f,token "MLink\r\n")<0) return -2; \
  565. break; \
  566. case RT_FLINE: \
  567. if (fprintf(f,token "F-Line\r\n")<0) return -2; \
  568. break; \
  569. }
  570. #define minams_param(token,setting) \
  571. { \
  572. int major,minor; \
  573. major=dest->libopts.setting/100; \
  574. minor=dest->libopts.setting%100; \
  575. if (fprintf(f,token "%d.%02d\r\n",major,minor)<0) return -2; \
  576. }
  577. if (fputs("\r\n[Library Options]\r\n",f)<0) return -2;
  578. boolean_param("Use TI-89=",use_ti89)
  579. boolean_param("Use TI-92 Plus=",use_ti92p)
  580. boolean_param("Use V200=",use_v200)
  581. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  582. boolean_param("Use Kernel=",use_kernel)
  583. boolean_param("Use PreOS=",use_preos)
  584. boolean_param("Minimum AMS Version Defined=",use_minams)
  585. minams_param("Minimum AMS Version=",minams);
  586. boolean_param("Unofficial OS Support=",unofficial_os)
  587. reloc_param("Reloc Format=",reloc_format)
  588. reloc_param("ROM Call Format=",rom_call_format)
  589. reloc_param("BSS Ref Format=",bss_ref_format)
  590. reloc_param("Data Ref Format=",data_ref_format)
  591. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  592. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  593. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  594. boolean_param("Use Return Value=",use_return_value)
  595. boolean_param("Enable Error Return=",enable_error_return)
  596. boolean_param("Save Screen=",save_screen)
  597. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  598. #undef boolean_param
  599. #undef reloc_param
  600. #undef minams_param
  601. if (fprintf(f,"\r\n[File Editing]\r\nOpen File=%s\r\n\r\n[Included Files]\r\n",smartAscii(dest->open_file))<0) return -2;
  602. #define filepath_param(token,filetype) \
  603. e=dest->filetype.path.count(); \
  604. for(i=0;i<e;i++) \
  605. { \
  606. tmp=convert_path_separators_save(smartAscii(dest->filetype.path[i])); \
  607. if (fprintf(f,token " %u=%s\r\n",i+1,smartAscii(tmp))<0) return -2; \
  608. if (!dest->filetype.folder[i].isEmpty()) \
  609. { \
  610. if (fprintf(f,token " %u Folder=%s\r\n",i+1,smartAscii(dest->filetype.folder[i]))<0) return -2; \
  611. } \
  612. }
  613. filepath_param("C File",c_files)
  614. filepath_param("GNU Assembler File",s_files)
  615. filepath_param("Header File",h_files)
  616. filepath_param("Assembler File",asm_files)
  617. filepath_param("Object File",o_files)
  618. filepath_param("Archive File",a_files)
  619. filepath_param("Text File",txt_files)
  620. filepath_param("Quill File",quill_files)
  621. filepath_param("Other File",oth_files)
  622. #undef filepath_param
  623. if (fputs("\r\n",f)<0) return -2;
  624. return 0;
  625. }
  626. void newSettings(tprSettings *settings,tprLibOpts *libopts)
  627. {
  628. tprSettings newSettings;
  629. tprLibOpts newLibOpts;
  630. *settings=newSettings;
  631. *libopts=newLibOpts;
  632. }
  633. //returns 0 on success, -1 if the file couldn't be created or -2 if fprintf,
  634. //fputs or fclose failed
  635. int saveTPR(const QString &fileName,TPRDataStruct *src)
  636. {
  637. FILE *f;
  638. int ret;
  639. f=fopen(fileName,"wb");
  640. if (!f)
  641. {
  642. return -1;
  643. }
  644. ret=save_tpr(f,src);
  645. if (fclose(f)) return -2;
  646. return ret;
  647. }
  648. void mkdir_multi(const char *fileName)
  649. {
  650. int l=strlen(fileName);
  651. char buffer[l+2];
  652. char *ptr;
  653. #ifdef __WIN32__
  654. ptr=strchr(fileName,'\\');
  655. #else
  656. ptr=strchr(fileName,'/');
  657. #endif
  658. while (ptr)
  659. {
  660. memcpy(buffer,fileName,ptr-fileName);
  661. buffer[ptr-fileName]=0;
  662. mkdir(buffer,S_IRWXU | S_IRWXG | S_IRWXO);
  663. #ifdef __WIN32__
  664. ptr=strchr(ptr+1,'\\');
  665. #else
  666. ptr=strchr(ptr+1,'/');
  667. #endif
  668. }
  669. }
  670. static int writeToFile(FILE *f, const QString &text)
  671. {
  672. if (preferences.useCalcCharset) {
  673. const unsigned short *utf16=text.ucs2();
  674. if (utf16) {
  675. char *s=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  676. size_t l=std::strlen(s);
  677. if (fwrite(s,1,l,f)<l) {
  678. g_free(s);
  679. return -2;
  680. }
  681. g_free(s);
  682. }
  683. } else {
  684. const char *s=smartAscii(text);
  685. size_t l=text.length();
  686. if (fwrite(s,1,l,f)<l) return -2;
  687. }
  688. return 0;
  689. }
  690. enum CharModes {cmNone, cmNormalText, cmNumber, cmMultiSymbol, cmString, cmChar,
  691. cmComment, cmUnchangeableLine, cmExtUnchangeableLine,
  692. cmExtUnchangeableLineString, cmTrigraph};
  693. int saveAndSplitFileText(const char *fileName, const QString &fileText,
  694. bool split, bool addCLineDirective,
  695. bool addASMLineDirective, const QString &origFileName,
  696. LineStartList *pLineStartList)
  697. {
  698. FILE *f;
  699. LineStartList lineStartList;
  700. // remove trailing spaces if requested
  701. QString text=fileText;
  702. if (preferences.removeTrailingSpaces && !text.isNull()) {
  703. text=text.replace(QRegExp("((?!\n)\\s)*\n"),"\n");
  704. text=text.remove(QRegExp("((?!\n)\\s)*$"));
  705. }
  706. f=fopen(fileName,"wb");
  707. if (!f)
  708. {
  709. mkdir_multi(fileName);
  710. f=fopen(fileName,"wb");
  711. if (!f)
  712. return -1;
  713. }
  714. if (split && preferences.splitSourceFiles && !settings.debug_info) {
  715. unsigned curPos=0, curLine=0, curCol=0, l=text.length();
  716. bool atLineStart;
  717. CharModes curMode=cmNone;
  718. #define INSERT_CHAR(ch) do {if (writeToFile(f,QString((ch)))) {fclose(f); return -2;}} while(0)
  719. #define INSERT_STRING(str) do {if (writeToFile(f,(str))) {fclose(f); return -2;}} while(0)
  720. #define ADD_LINE() do {lineStartList.append(qMakePair(curLine,curCol)); atLineStart=TRUE;} while(0)
  721. #define IS_NEWLINE() (text[curPos]=='\n')
  722. #define ADD_LINE_NEXT() do {lineStartList.append(qMakePair(curLine+(unsigned)(IS_NEWLINE()),(IS_NEWLINE())?0u:(curCol+1u))); atLineStart=TRUE;} while(0)
  723. #define NEW_LINE() do {if((!(atLineStart||IS_NEWLINE())) || (text[curPos]=='#')) {INSERT_CHAR(QChar('\n')); ADD_LINE();} curMode=cmNone;} while(0)
  724. #define SET_MULTI_CHAR_MODE(mode) do {if (curMode!=(mode)) {NEW_LINE(); curMode=(mode);}} while(0)
  725. ADD_LINE(); // Line 0
  726. for (; curPos<l; curPos++) {
  727. bool noInsert=FALSE;
  728. QChar c=text[curPos];
  729. if (c=='\n')
  730. ADD_LINE_NEXT();
  731. switch (curMode) {
  732. case cmString:
  733. if (c=='\"') {
  734. bool b=TRUE;
  735. unsigned i=curPos-1;
  736. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  737. b=!b;
  738. if (text[i]=='\\') --i; else i-=3;
  739. }
  740. if (b) {
  741. curMode=cmNone;
  742. noInsert=TRUE;
  743. INSERT_STRING(QString(c)+"\n");
  744. atLineStart=TRUE;
  745. ADD_LINE_NEXT();
  746. }
  747. }
  748. break;
  749. case cmChar:
  750. if (c=='\'') {
  751. bool b=TRUE;
  752. unsigned i=curPos-1;
  753. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  754. b=!b;
  755. if (text[i]=='\\') --i; else i-=3;
  756. }
  757. if (b) curMode=cmNone;
  758. }
  759. break;
  760. case cmComment:
  761. if ((c=='/')&&(text[curPos-1]=='*')) curMode=cmNone;
  762. break;
  763. case cmUnchangeableLine:
  764. if (c=='\n') curMode=cmNone;
  765. break;
  766. case cmExtUnchangeableLine:
  767. if ((c=='\n')&&(text[curPos-1]!='\\')) curMode=cmNone;
  768. else if (c=='\"') curMode=cmExtUnchangeableLineString;
  769. break;
  770. case cmExtUnchangeableLineString:
  771. if (c=='\"') {
  772. bool b=TRUE;
  773. unsigned i=curPos-1;
  774. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  775. b=!b;
  776. if (text[i]=='\\') --i; else i-=3;
  777. }
  778. if (b) curMode=cmExtUnchangeableLine;
  779. }
  780. break;
  781. case cmTrigraph:
  782. if ((c!='?')&&((curPos+1>=l)||(text[curPos+1]!='?'))) curMode=cmNone;
  783. break;
  784. default:
  785. if (!text.mid(curPos,2).compare("//"))
  786. SET_MULTI_CHAR_MODE(cmUnchangeableLine);
  787. else if (!text.mid(curPos,2).compare("/*"))
  788. SET_MULTI_CHAR_MODE(cmComment);
  789. else if (!text.mid(curPos,2).compare("?""?""="))
  790. SET_MULTI_CHAR_MODE(cmExtUnchangeableLine);
  791. else if (!text.mid(curPos,2).compare("?""?")&&(curPos+2<l)
  792. &&QString("()/\'<>!-").contains(text[curPos+2]))
  793. SET_MULTI_CHAR_MODE(cmTrigraph);
  794. else {
  795. switch(c.unicode()) {
  796. case ' ':
  797. case '\t':
  798. if (curPos && text[curPos-1]!=' ' && text[curPos-1]!='\t')
  799. NEW_LINE();
  800. break;
  801. case 'A' ... 'Z':
  802. case 'a' ... 'z':
  803. case '0' ... '9':
  804. case '_':
  805. case '$':
  806. if (curMode!=cmNormalText && curMode!=cmNumber) {
  807. NEW_LINE();
  808. if (c>=QChar('0') && c<=QChar('9'))
  809. curMode=cmNumber;
  810. else
  811. curMode=cmNormalText;
  812. }
  813. break;
  814. case '\"':
  815. SET_MULTI_CHAR_MODE(cmString);
  816. break;
  817. case '\'':
  818. SET_MULTI_CHAR_MODE(cmChar);
  819. break;
  820. case '#':
  821. SET_MULTI_CHAR_MODE(cmExtUnchangeableLine);
  822. break;
  823. case '.':
  824. if (curMode!=cmNumber) {
  825. if ((curPos+1<l)
  826. &&(text[curPos+1]>=QChar('0')
  827. &&text[curPos+1]<=QChar('9'))) {
  828. NEW_LINE();
  829. curMode=cmNumber;
  830. } else SET_MULTI_CHAR_MODE(cmMultiSymbol);
  831. }
  832. break;
  833. case '+':
  834. case '-':
  835. if ((curMode!=cmNumber)||(curPos<=1)
  836. ||!QString("eEpP").contains(text[curPos-1]))
  837. SET_MULTI_CHAR_MODE(cmMultiSymbol);
  838. break;
  839. default:
  840. if (QString(",;()[]{}").contains(c)) {
  841. if (curMode!=cmNone) {
  842. NEW_LINE();
  843. curMode=cmNone;
  844. }
  845. if ((curPos+1<l)&&text[curPos+1]!='\n') {
  846. noInsert=TRUE;
  847. INSERT_STRING(QString(c)+"\n");
  848. ADD_LINE_NEXT();
  849. } else SET_MULTI_CHAR_MODE(cmMultiSymbol);
  850. }
  851. break;
  852. }
  853. }
  854. break;
  855. }
  856. if (!noInsert) {
  857. // Special cases:
  858. switch(c.unicode()) {
  859. // Surrogate pairs.
  860. case 0xd800 ... 0xdbff:
  861. if (curPos<l && text[curPos+1].unicode()>=0xdc00
  862. && text[curPos+1].unicode()<=0xdfff) {
  863. INSERT_STRING(QString(c)+text[curPos+1]);
  864. // Allow the UI to respond, splitting is a lengthy operation.
  865. if ((curPos++)&127)
  866. QApplication::eventLoop()->processEvents(QEventLoop::AllEvents,1000);
  867. curPos++;
  868. curCol++;
  869. break;
  870. } else goto de_fault;
  871. // x-bar and y-bar are special if we use the calculator charset.
  872. case 0x305:
  873. if (preferences.useCalcCharset && curPos<l
  874. && (text[curPos+1]=='x' || text[curPos+1]=='y')) {
  875. INSERT_STRING(QString(c)+text[curPos+1]);
  876. // Allow the UI to respond, splitting is a lengthy operation.
  877. if ((curPos++)&127)
  878. QApplication::eventLoop()->processEvents(QEventLoop::AllEvents,1000);
  879. curCol++;
  880. break;
  881. }
  882. default:
  883. de_fault:
  884. INSERT_CHAR(c);
  885. break;
  886. }
  887. if (c!='\n') atLineStart=FALSE;
  888. }
  889. if (c=='\n') {
  890. curLine++;
  891. curCol=0;
  892. } else curCol++;
  893. // Allow the UI to respond, splitting is a lengthy operation.
  894. if (curPos&127)
  895. QApplication::eventLoop()->processEvents(QEventLoop::AllEvents,1000);
  896. }
  897. NEW_LINE();
  898. #undef INSERT_CHAR
  899. #undef INSERT_STRING
  900. #undef ADD_LINE
  901. #undef IS_NEWLINE
  902. #undef ADD_LINE_NEXT
  903. #undef NEW_LINE
  904. #undef SET_MULTI_CHAR_MODE
  905. } else {
  906. if ((addCLineDirective || addASMLineDirective) && settings.debug_info) {
  907. QString escapedFileName=origFileName, lineDirective;
  908. escapedFileName.replace("\\","\\\\");
  909. escapedFileName.replace("\"","\\\"");
  910. // These have no business to be in a file name, but someone somewhere may
  911. // have that very bad idea...
  912. escapedFileName.replace("\r","\\r");
  913. escapedFileName.replace("\n","\\n");
  914. lineDirective=QString(addCLineDirective
  915. ?"#line 1 \"%1\"\n"
  916. :".appfile \"%1\"; .appline 1\n").arg(escapedFileName);
  917. // Don't use calc charset for this, it's a host file name.
  918. const char *s=smartAscii(lineDirective);
  919. size_t l=lineDirective.length();
  920. if (fwrite(s,1,l,f)<l) return -2;
  921. }
  922. if (writeToFile(f,text)) {fclose(f); return -2;}
  923. if (fwrite("\n",1,1,f)<1) {fclose(f); return -2;}
  924. }
  925. if (fclose(f)) return -2;
  926. if (pLineStartList) *pLineStartList=lineStartList;
  927. return 0;
  928. }
  929. int saveFileText(const char *fileName,const QString &fileText)
  930. {
  931. return saveAndSplitFileText(fileName,fileText,FALSE,FALSE,FALSE,QString::null,
  932. static_cast<LineStartList *>(NULL));
  933. }
  934. void kurlNewFileName(KURL &dir,const QString &newFileName)
  935. {
  936. if (newFileName[0]=='/')
  937. dir.setPath(newFileName);
  938. else
  939. dir.setFileName(newFileName);
  940. }
  941. static QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  942. {
  943. int a,b;
  944. QString ret;
  945. destFileName=srcFileName;
  946. a=destFileName.findRev('.');
  947. b=destFileName.findRev('/');
  948. if (a<0)
  949. return QString::null;
  950. if (a<b)
  951. return QString::null;
  952. ret=destFileName.mid(a+1);
  953. destFileName.truncate(a);
  954. return ret;
  955. }
  956. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  957. {
  958. int i;
  959. QString fileName_name,fileName_suffix;
  960. QString name,suffix;
  961. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  962. for (i=fileNameList.count()-1;i>=0;i--)
  963. {
  964. suffix=pullOutFileSuffix(fileNameList[i],name);
  965. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  966. {
  967. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  968. {
  969. if (!name.compare(fileName_name))
  970. return 0;
  971. }
  972. }
  973. else
  974. {
  975. if (!fileNameList[i].compare(fileName))
  976. return 0;
  977. }
  978. }
  979. return 1;
  980. }
  981. // returns 0 on success, >0 on read failure, <0 on write failure
  982. int copyFile(const char *src, const char *dest)
  983. {
  984. // This doesn't load everything at once onto the stack because it may be
  985. // used for huge binary files, which don't fit on the stack. So we copy 1KB
  986. // at a time.
  987. FILE *sf=fopen(src,"rb");
  988. if (!sf) return 1;
  989. FILE *df=fopen(dest,"wb");
  990. if (!df) {fclose(sf); return -1;}
  991. char buffer[1024];
  992. while (!ferror(sf) && !feof(sf)) {
  993. size_t bytes_read=fread(buffer,1,1024,sf);
  994. if (fwrite(buffer,1,bytes_read,df)<bytes_read) {
  995. fclose(df);
  996. fclose(sf);
  997. return -2;
  998. }
  999. }
  1000. if (ferror(sf)) {
  1001. fclose(df);
  1002. fclose(sf);
  1003. return 2;
  1004. }
  1005. if (fclose(df)) {fclose(sf); return -3;}
  1006. if (fclose(sf)) return 3;
  1007. return 0;
  1008. }
  1009. // Replaces the first occurrence of "tempprog" in a pstarter or PPG with name.
  1010. // returns 0 on success, >0 on read failure, <0 on write failure
  1011. int insertName(const char *src, const char *dest, const char *name)
  1012. {
  1013. FILE *sf=std::fopen(src,"rb");
  1014. if (!sf) return 1;
  1015. std::fseek(sf,0,SEEK_END);
  1016. std::size_t flen=std::ftell(sf);
  1017. std::fseek(sf,0,SEEK_SET);
  1018. char *buffer = new(std::nothrow) char[flen];
  1019. if (!buffer) {std::fclose(sf); return 4;}
  1020. if (std::fread(buffer,1,flen,sf)<flen) {
  1021. delete[] buffer;
  1022. std::fclose(sf);
  1023. return 2;
  1024. }
  1025. if (fclose(sf)) {delete[] buffer; return 3;}
  1026. for (std::size_t i=0; i<=flen-8; i++) {
  1027. if (!std::memcmp(buffer+i,"tempprog",8)) {
  1028. std::strncpy(buffer+i,name,8);
  1029. break; // do only one replacement
  1030. }
  1031. }
  1032. FILE *df=std::fopen(dest,"wb");
  1033. if (!df) {delete[] buffer; return -1;}
  1034. if (std::fwrite(buffer,1,flen,df)<flen) {
  1035. delete[] buffer;
  1036. std::fclose(df);
  1037. return -2;
  1038. }
  1039. delete[] buffer;
  1040. if (std::fclose(df)) return -3;
  1041. return 0;
  1042. }
  1043. int getPathType(const QString &thePath)
  1044. {
  1045. struct stat statvar;
  1046. int result=stat(thePath,&statvar);
  1047. if (result)
  1048. return errno==ENOENT?PATH_NOTFOUND:PATH_ERROR;
  1049. if (statvar.st_mode&S_IFDIR)
  1050. return PATH_FOLDER;
  1051. if (statvar.st_mode&S_IFREG)
  1052. return PATH_FILE;
  1053. return PATH_ERROR;
  1054. }
  1055. /*
  1056. Build command line arguments (Library Options section)
  1057. */
  1058. QStringList process_libopts(void)
  1059. {
  1060. QStringList args;
  1061. if (libopts.use_ti89) {
  1062. args.append("-DUSE_TI89");
  1063. }
  1064. if (libopts.use_ti92p) {
  1065. args.append("-DUSE_TI92PLUS");
  1066. }
  1067. if (libopts.use_v200) {
  1068. args.append("-DUSE_V200");
  1069. }
  1070. if (libopts.opt_calc_consts) {
  1071. args.append("-DOPTIMIZE_CALC_CONSTS");
  1072. }
  1073. if (libopts.use_kernel || libopts.use_preos) {
  1074. args.append("-DUSE_KERNEL");
  1075. }
  1076. if (libopts.use_preos) {
  1077. args.append("-DUSE_PREOS_COMPRESSED_TABLES");
  1078. }
  1079. if (libopts.use_minams) {
  1080. args.append(QString("-DMIN_AMS=%1").arg(libopts.minams));
  1081. }
  1082. if (libopts.unofficial_os) {
  1083. args.append("-DUNOFFICIAL_OS_SUPPORT");
  1084. }
  1085. if (libopts.use_preos) {
  1086. if (libopts.bss_ref_format == RT_NONE)
  1087. args.append("-DMERGE_BSS");
  1088. } else {
  1089. switch (libopts.reloc_format) {
  1090. case RT_KERNEL:
  1091. args.append("-DKERNEL_FORMAT_RELOCS");
  1092. break;
  1093. case RT_COMPRESSED:
  1094. args.append("-DCOMPRESSED_FORMAT_RELOCS");
  1095. break;
  1096. case RT_MLINK:
  1097. args.append("-DMLINK_FORMAT_RELOCS");
  1098. break;
  1099. case RT_FLINE:
  1100. args.append("-DUSE_FLINE_JUMPS");
  1101. break;
  1102. default:
  1103. break;
  1104. }
  1105. switch (libopts.rom_call_format) {
  1106. case RT_KERNEL:
  1107. args.append("-DKERNEL_FORMAT_ROM_CALLS");
  1108. break;
  1109. case RT_COMPRESSED:
  1110. args.append("-DCOMPRESSED_FORMAT_ROM_CALLS");
  1111. break;
  1112. case RT_MLINK:
  1113. args.append("-DMLINK_FORMAT_ROM_CALLS");
  1114. break;
  1115. case RT_PRECOMP:
  1116. args.append("-DOPTIMIZE_ROM_CALLS");
  1117. break;
  1118. case RT_FLINE:
  1119. args.append("-DUSE_FLINE_ROM_CALLS");
  1120. args.append("-fno-function-cse");
  1121. break;
  1122. default:
  1123. break;
  1124. }
  1125. if (libopts.opt_rom_calls) {
  1126. args.append("-DOPTIMIZE_ROM_CALLS");
  1127. }
  1128. switch (libopts.bss_ref_format) {
  1129. case RT_NONE:
  1130. args.append("-DMERGE_BSS");
  1131. break;
  1132. case RT_KERNEL:
  1133. args.append("-DKERNEL_FORMAT_BSS");
  1134. break;
  1135. case RT_COMPRESSED:
  1136. args.append("-DCOMPRESSED_FORMAT_BSS");
  1137. break;
  1138. case RT_MLINK:
  1139. args.append("-DMLINK_FORMAT_BSS");
  1140. break;
  1141. default:
  1142. break;
  1143. }
  1144. }
  1145. switch (libopts.data_ref_format) {
  1146. case RT_KERNEL:
  1147. args.append("-DKERNEL_FORMAT_DATA_VAR");
  1148. break;
  1149. case RT_COMPRESSED:
  1150. args.append("-DCOMPRESSED_FORMAT_DATA_VAR");
  1151. break;
  1152. case RT_MLINK:
  1153. args.append("-DMLINK_FORMAT_DATA_VAR");
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. if (libopts.use_fline_jumps) {
  1159. args.append("-DUSE_FLINE_JUMPS");
  1160. if (libopts.use_4b_fline_jumps) {
  1161. args.append("-DUSE_4BYTE_FLINE_JUMPS");
  1162. }
  1163. }
  1164. if (libopts.use_internal_fline_emu) {
  1165. args.append("-DUSE_INTERNAL_FLINE_EMULATOR");
  1166. }
  1167. if (libopts.use_return_value) {
  1168. args.append("-DRETURN_VALUE");
  1169. }
  1170. if (libopts.enable_error_return) {
  1171. args.append("-DENABLE_ERROR_RETURN");
  1172. }
  1173. if (libopts.save_screen) {
  1174. args.append("-DSAVE_SCREEN");
  1175. }
  1176. return args;
  1177. }
  1178. /*
  1179. Build linker command line arguments
  1180. */
  1181. QStringList process_settings(const QString &prjNameUnicode,
  1182. QCString &projectName, QCString &dataVarName,
  1183. QCString &packFolder, QCString &packName)
  1184. {
  1185. QStringList args;
  1186. // Convert the project name to the calculator charset.
  1187. {
  1188. const unsigned short *utf16=prjNameUnicode.ucs2();
  1189. if (utf16) {
  1190. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1191. projectName=ti; // This is a hidden strdup (see QCString::operator=).
  1192. g_free(ti);
  1193. } else projectName=QCString();
  1194. }
  1195. // Split the PPG name into folder and file.
  1196. QString packFolderUnicode, packNameUnicode;
  1197. int slashPos=settings.pack_name.find('\\');
  1198. if (slashPos>=0) {
  1199. packFolderUnicode=settings.pack_name.left(slashPos);
  1200. packNameUnicode=settings.pack_name.mid(slashPos+1);
  1201. } else {
  1202. packNameUnicode=settings.pack_name;
  1203. }
  1204. // Convert the PPG folder name to the calculator charset.
  1205. {
  1206. const unsigned short *utf16=packFolderUnicode.ucs2();
  1207. if (utf16) {
  1208. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1209. packFolder=ti; // This is a hidden strdup (see QCString::operator=).
  1210. g_free(ti);
  1211. } else packFolder=QCString();
  1212. }
  1213. // Convert the PPG file name to the calculator charset.
  1214. {
  1215. const unsigned short *utf16=packNameUnicode.ucs2();
  1216. if (utf16) {
  1217. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1218. packName=ti; // This is a hidden strdup (see QCString::operator=).
  1219. g_free(ti);
  1220. } else packName=QCString();
  1221. }
  1222. if (settings.use_data_var && !settings.data_var.isEmpty()) {
  1223. // We can't just append this to the argument list because this needs to be
  1224. // in the calculator charset.
  1225. const unsigned short *utf16=settings.data_var.ucs2();
  1226. if (utf16) {
  1227. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1228. dataVarName=ti; // This is a hidden strdup (see QCString::operator=).
  1229. g_free(ti);
  1230. } else dataVarName=QCString();
  1231. if (!settings.copy_data_var) {
  1232. args.append("--data-var-copy=never");
  1233. } else if (!settings.copy_data_var_arc) {
  1234. args.append("--data-var-copy=always");
  1235. }
  1236. } else dataVarName=QCString();
  1237. if (settings.optimize_nops) {
  1238. args.append("--optimize-nops");
  1239. }
  1240. if (settings.optimize_returns) {
  1241. args.append("--optimize-returns");
  1242. }
  1243. if (settings.optimize_branches) {
  1244. args.append("--optimize-branches");
  1245. }
  1246. if (settings.optimize_moves) {
  1247. args.append("--optimize-moves");
  1248. }
  1249. if (settings.optimize_tests) {
  1250. args.append("--optimize-tests");
  1251. }
  1252. if (settings.optimize_calcs) {
  1253. args.append("--optimize-calcs");
  1254. }
  1255. if (settings.remove_unused) {
  1256. args.append("--remove-unused");
  1257. }
  1258. if (settings.cut_ranges) {
  1259. args.append("--cut-ranges");
  1260. }
  1261. if (settings.reorder_sections) {
  1262. args.append("--reorder-sections");
  1263. }
  1264. if (settings.merge_constants) {
  1265. args.append("--merge-constants");
  1266. }
  1267. if (settings.pack) {
  1268. args.append("--outputbin-main-only");
  1269. } else if (settings.outputbin) {
  1270. args.append("--outputbin");
  1271. }
  1272. if (!settings.initialize_bss) {
  1273. args.append("--omit-bss-init");
  1274. }
  1275. if (settings.fargo) {
  1276. args.append("--fargo");
  1277. }
  1278. if (settings.flash_os) {
  1279. args.append("--flash-os");
  1280. }
  1281. return args;
  1282. }