tpr.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. INSERT_CHAR(c);
  858. if (c!='\n') atLineStart=FALSE;
  859. }
  860. if (c=='\n') {
  861. curLine++;
  862. curCol=0;
  863. } else curCol++;
  864. // Allow the UI to respond, splitting is a lengthy operation.
  865. if (curPos&127)
  866. QApplication::eventLoop()->processEvents(QEventLoop::AllEvents,1000);
  867. }
  868. NEW_LINE();
  869. #undef INSERT_CHAR
  870. #undef INSERT_STRING
  871. #undef ADD_LINE
  872. #undef IS_NEWLINE
  873. #undef ADD_LINE_NEXT
  874. #undef NEW_LINE
  875. #undef SET_MULTI_CHAR_MODE
  876. } else {
  877. if ((addCLineDirective || addASMLineDirective) && settings.debug_info) {
  878. QString escapedFileName=origFileName, lineDirective;
  879. escapedFileName.replace("\\","\\\\");
  880. escapedFileName.replace("\"","\\\"");
  881. // These have no business to be in a file name, but someone somewhere may
  882. // have that very bad idea...
  883. escapedFileName.replace("\r","\\r");
  884. escapedFileName.replace("\n","\\n");
  885. lineDirective=QString(addCLineDirective
  886. ?"#line 1 \"%1\"\n"
  887. :".appfile \"%1\"; .appline 1\n").arg(escapedFileName);
  888. // Don't use calc charset for this, it's a host file name.
  889. const char *s=smartAscii(lineDirective);
  890. size_t l=lineDirective.length();
  891. if (fwrite(s,1,l,f)<l) return -2;
  892. }
  893. if (writeToFile(f,text)) {fclose(f); return -2;}
  894. if (addCLineDirective) {
  895. if (fwrite("\n",1,1,f)<1) {fclose(f); return -2;}
  896. }
  897. }
  898. if (fclose(f)) return -2;
  899. if (pLineStartList) *pLineStartList=lineStartList;
  900. return 0;
  901. }
  902. int saveFileText(const char *fileName,const QString &fileText)
  903. {
  904. return saveAndSplitFileText(fileName,fileText,FALSE,FALSE,FALSE,QString::null,
  905. static_cast<LineStartList *>(NULL));
  906. }
  907. void kurlNewFileName(KURL &dir,const QString &newFileName)
  908. {
  909. if (newFileName[0]=='/')
  910. dir.setPath(newFileName);
  911. else
  912. dir.setFileName(newFileName);
  913. }
  914. static QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  915. {
  916. int a,b;
  917. QString ret;
  918. destFileName=srcFileName;
  919. a=destFileName.findRev('.');
  920. b=destFileName.findRev('/');
  921. if (a<0)
  922. return QString::null;
  923. if (a<b)
  924. return QString::null;
  925. ret=destFileName.mid(a+1);
  926. destFileName.truncate(a);
  927. return ret;
  928. }
  929. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  930. {
  931. int i;
  932. QString fileName_name,fileName_suffix;
  933. QString name,suffix;
  934. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  935. for (i=fileNameList.count()-1;i>=0;i--)
  936. {
  937. suffix=pullOutFileSuffix(fileNameList[i],name);
  938. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  939. {
  940. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  941. {
  942. if (!name.compare(fileName_name))
  943. return 0;
  944. }
  945. }
  946. else
  947. {
  948. if (!fileNameList[i].compare(fileName))
  949. return 0;
  950. }
  951. }
  952. return 1;
  953. }
  954. // returns 0 on success, >0 on read failure, <0 on write failure
  955. int copyFile(const char *src, const char *dest)
  956. {
  957. // This doesn't load everything at once onto the stack because it may be
  958. // used for huge binary files, which don't fit on the stack. So we copy 1KB
  959. // at a time.
  960. FILE *sf=fopen(src,"rb");
  961. if (!sf) return 1;
  962. FILE *df=fopen(dest,"wb");
  963. if (!df) {fclose(sf); return -1;}
  964. char buffer[1024];
  965. while (!ferror(sf) && !feof(sf)) {
  966. size_t bytes_read=fread(buffer,1,1024,sf);
  967. if (fwrite(buffer,1,bytes_read,df)<bytes_read) {
  968. fclose(df);
  969. fclose(sf);
  970. return -2;
  971. }
  972. }
  973. if (ferror(sf)) {
  974. fclose(df);
  975. fclose(sf);
  976. return 2;
  977. }
  978. if (fclose(df)) {fclose(sf); return -3;}
  979. if (fclose(sf)) return 3;
  980. return 0;
  981. }
  982. int getPathType(const QString &thePath)
  983. {
  984. struct stat statvar;
  985. int result=stat(thePath,&statvar);
  986. if (result)
  987. return errno==ENOENT?PATH_NOTFOUND:PATH_ERROR;
  988. if (statvar.st_mode&S_IFDIR)
  989. return PATH_FOLDER;
  990. if (statvar.st_mode&S_IFREG)
  991. return PATH_FILE;
  992. return PATH_ERROR;
  993. }
  994. /*
  995. Build command line arguments (Library Options section)
  996. */
  997. QStringList process_libopts(void)
  998. {
  999. QStringList args;
  1000. if (libopts.use_ti89) {
  1001. args.append("-DUSE_TI89");
  1002. }
  1003. if (libopts.use_ti92p) {
  1004. args.append("-DUSE_TI92PLUS");
  1005. }
  1006. if (libopts.use_v200) {
  1007. args.append("-DUSE_V200");
  1008. }
  1009. if (libopts.opt_calc_consts) {
  1010. args.append("-DOPTIMIZE_CALC_CONSTS");
  1011. }
  1012. if (libopts.use_kernel || libopts.use_preos) {
  1013. args.append("-DUSE_KERNEL");
  1014. }
  1015. if (libopts.use_preos) {
  1016. args.append("-DUSE_PREOS_COMPRESSED_TABLES");
  1017. }
  1018. if (libopts.use_minams) {
  1019. args.append(QString("-DMIN_AMS=%1").arg(libopts.minams));
  1020. }
  1021. if (libopts.unofficial_os) {
  1022. args.append("-DUNOFFICIAL_OS_SUPPORT");
  1023. }
  1024. if (libopts.use_preos) {
  1025. if (libopts.bss_ref_format == RT_NONE)
  1026. args.append("-DMERGE_BSS");
  1027. } else {
  1028. switch (libopts.reloc_format) {
  1029. case RT_KERNEL:
  1030. args.append("-DKERNEL_FORMAT_RELOCS");
  1031. break;
  1032. case RT_COMPRESSED:
  1033. args.append("-DCOMPRESSED_FORMAT_RELOCS");
  1034. break;
  1035. case RT_MLINK:
  1036. args.append("-DMLINK_FORMAT_RELOCS");
  1037. break;
  1038. case RT_FLINE:
  1039. args.append("-DUSE_FLINE_JUMPS");
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. switch (libopts.rom_call_format) {
  1045. case RT_KERNEL:
  1046. args.append("-DKERNEL_FORMAT_ROM_CALLS");
  1047. break;
  1048. case RT_COMPRESSED:
  1049. args.append("-DCOMPRESSED_FORMAT_ROM_CALLS");
  1050. break;
  1051. case RT_MLINK:
  1052. args.append("-DMLINK_FORMAT_ROM_CALLS");
  1053. break;
  1054. case RT_PRECOMP:
  1055. args.append("-DOPTIMIZE_ROM_CALLS");
  1056. break;
  1057. case RT_FLINE:
  1058. args.append("-DUSE_FLINE_ROM_CALLS");
  1059. args.append("-fno-function-cse");
  1060. break;
  1061. default:
  1062. break;
  1063. }
  1064. if (libopts.opt_rom_calls) {
  1065. args.append("-DOPTIMIZE_ROM_CALLS");
  1066. }
  1067. switch (libopts.bss_ref_format) {
  1068. case RT_NONE:
  1069. args.append("-DMERGE_BSS");
  1070. break;
  1071. case RT_KERNEL:
  1072. args.append("-DKERNEL_FORMAT_BSS");
  1073. break;
  1074. case RT_COMPRESSED:
  1075. args.append("-DCOMPRESSED_FORMAT_BSS");
  1076. break;
  1077. case RT_MLINK:
  1078. args.append("-DMLINK_FORMAT_BSS");
  1079. break;
  1080. default:
  1081. break;
  1082. }
  1083. }
  1084. switch (libopts.data_ref_format) {
  1085. case RT_KERNEL:
  1086. args.append("-DKERNEL_FORMAT_DATA_VAR");
  1087. break;
  1088. case RT_COMPRESSED:
  1089. args.append("-DCOMPRESSED_FORMAT_DATA_VAR");
  1090. break;
  1091. case RT_MLINK:
  1092. args.append("-DMLINK_FORMAT_DATA_VAR");
  1093. break;
  1094. default:
  1095. break;
  1096. }
  1097. if (libopts.use_fline_jumps) {
  1098. args.append("-DUSE_FLINE_JUMPS");
  1099. if (libopts.use_4b_fline_jumps) {
  1100. args.append("-DUSE_4BYTE_FLINE_JUMPS");
  1101. }
  1102. }
  1103. if (libopts.use_internal_fline_emu) {
  1104. args.append("-DUSE_INTERNAL_FLINE_EMULATOR");
  1105. }
  1106. if (libopts.use_return_value) {
  1107. args.append("-DRETURN_VALUE");
  1108. }
  1109. if (libopts.enable_error_return) {
  1110. args.append("-DENABLE_ERROR_RETURN");
  1111. }
  1112. if (libopts.save_screen) {
  1113. args.append("-DSAVE_SCREEN");
  1114. }
  1115. return args;
  1116. }
  1117. /*
  1118. Build linker command line arguments
  1119. */
  1120. QStringList process_settings(const QString &prjNameUnicode,
  1121. QCString &projectName, QCString &dataVarName,
  1122. QCString &packName)
  1123. {
  1124. QStringList args;
  1125. // Convert the project name to the calculator charset.
  1126. {
  1127. const unsigned short *utf16=prjNameUnicode.ucs2();
  1128. if (utf16) {
  1129. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1130. projectName=ti; // This is a hidden strdup (see QCString::operator=).
  1131. g_free(ti);
  1132. } else projectName=QCString();
  1133. }
  1134. // Convert the PPG name to the calculator charset.
  1135. {
  1136. const unsigned short *utf16=settings.pack_name.ucs2();
  1137. if (utf16) {
  1138. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1139. packName=ti; // This is a hidden strdup (see QCString::operator=).
  1140. g_free(ti);
  1141. } else packName=QCString();
  1142. }
  1143. if (settings.use_data_var && !settings.data_var.isEmpty()) {
  1144. // We can't just append this to the argument list because this needs to be
  1145. // in the calculator charset.
  1146. const unsigned short *utf16=settings.data_var.ucs2();
  1147. if (utf16) {
  1148. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  1149. dataVarName=ti; // This is a hidden strdup (see QCString::operator=).
  1150. g_free(ti);
  1151. } else dataVarName=QCString();
  1152. if (!settings.copy_data_var) {
  1153. args.append("--data-var-copy=never");
  1154. } else if (!settings.copy_data_var_arc) {
  1155. args.append("--data-var-copy=always");
  1156. }
  1157. } else dataVarName=QCString();
  1158. if (settings.optimize_nops) {
  1159. args.append("--optimize-nops");
  1160. }
  1161. if (settings.optimize_returns) {
  1162. args.append("--optimize-returns");
  1163. }
  1164. if (settings.optimize_branches) {
  1165. args.append("--optimize-branches");
  1166. }
  1167. if (settings.optimize_moves) {
  1168. args.append("--optimize-moves");
  1169. }
  1170. if (settings.optimize_tests) {
  1171. args.append("--optimize-tests");
  1172. }
  1173. if (settings.optimize_calcs) {
  1174. args.append("--optimize-calcs");
  1175. }
  1176. if (settings.remove_unused) {
  1177. args.append("--remove-unused");
  1178. }
  1179. if (settings.cut_ranges) {
  1180. args.append("--cut-ranges");
  1181. }
  1182. if (settings.reorder_sections) {
  1183. args.append("--reorder-sections");
  1184. }
  1185. if (settings.merge_constants) {
  1186. args.append("--merge-constants");
  1187. }
  1188. if (settings.outputbin || settings.pack) {
  1189. args.append("--outputbin");
  1190. }
  1191. if (!settings.initialize_bss) {
  1192. args.append("--omit-bss-init");
  1193. }
  1194. if (settings.fargo) {
  1195. args.append("--fargo");
  1196. }
  1197. if (settings.flash_os) {
  1198. args.append("--flash-os");
  1199. }
  1200. return args;
  1201. }