tpr.cpp 44 KB

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