tpr.cpp 44 KB

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