tpr.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. tpr handling routines adapted from tprbuilder
  4. Copyright (C) 2002 Romain Liévin
  5. Copyright (C) 2002-2006 Kevin Kofler
  6. Copyright (C) 2006 Joey Adams
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software Foundation,
  17. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cerrno>
  22. #include <cstring>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <sys/dir.h>
  26. #include <kapplication.h>
  27. #include <kcmdlineargs.h>
  28. #include <kaboutdata.h>
  29. #include <qstring.h>
  30. #include <qregexp.h>
  31. #include <qtextcodec.h>
  32. #include <glib.h>
  33. #include <ticonv.h>
  34. #include "ktigcc.h"
  35. #include "tpr.h"
  36. #include "preferences.h"
  37. #define find_param(s_,t_) find_param_ex((s_),(t_),sizeof(t_)-1)
  38. static char *find_param_ex(char *s, const char *t, size_t l)
  39. {
  40. if (!strncmp(s,t,l))
  41. return s+l;
  42. else
  43. return NULL;
  44. }
  45. static char *find_numbered_param(char *s, const char*t, int *i)
  46. {
  47. char *p;
  48. int endpos = 0;
  49. int ret = 0;
  50. char arglist[256];
  51. strcpy(arglist, t);
  52. strcat(arglist, "%n");
  53. ret = sscanf(s, arglist, i, &endpos);
  54. if(ret < 1 || !endpos) return NULL;
  55. p = s + endpos;
  56. return p;
  57. }
  58. // converts Windows paths to Unix paths if necessary.
  59. static QString convert_path_separators(const char *file)
  60. {
  61. QString s=file;
  62. int o;
  63. #ifndef __WIN32__
  64. while ((o=s.find('\\',0,TRUE))>=0)
  65. s[o]='/';
  66. #endif
  67. return s;
  68. }
  69. static char* strip(char *str)
  70. {
  71. int len = strlen(str);
  72. if(len > 0)
  73. if( (str[len-1] == '\r') || (str[len-1] == '\n') )
  74. str[len-1] = '\0';
  75. if(len > 1)
  76. if( (str[len-2] == '\r') || (str[len-2] == '\n') )
  77. str[len-2] = '\0';
  78. return str;
  79. }
  80. /*
  81. Read a line from file and do a clean-up
  82. */
  83. static int read_line(FILE *f, char *buffer, int *l)
  84. {
  85. strcpy(buffer, "");
  86. if(feof(f)) return EOF;
  87. if (!fgets(buffer, 256, f)) return feof(f)?EOF:1;
  88. strip(buffer);
  89. (*l)++;
  90. while( (buffer[0] == '\r') || (buffer[0] == '\n') || (buffer[0] == '\0') )
  91. {
  92. if(feof(f)) return EOF;
  93. if (!fgets(buffer, 256, f)) return feof(f)?EOF:1;
  94. strip(buffer);
  95. (*l)++;
  96. }
  97. return 0;
  98. }
  99. //some items from the TSR might still be ignored or not shown here.
  100. //if you need one of them, add an entry to one of the
  101. //definition "tables".
  102. //If you add an entry here, don't forget to add something to
  103. // save_tpr!
  104. static int parse_file(FILE *f,TPRDataStruct *dest)
  105. {
  106. char buffer[256];
  107. int l = 0;
  108. SectionType stype = SECTION_NONE;
  109. // Some defaults are different when opening an existing project.
  110. dest->settings.cc_switches="";
  111. dest->settings.as_switches="";
  112. dest->settings.a68k_switches="";
  113. dest->libopts.use_ti89=0;
  114. dest->libopts.use_ti92p=0;
  115. dest->libopts.use_v200=0;
  116. dest->libopts.use_minams=0;
  117. dest->libopts.save_screen=0;
  118. while(!feof(f))
  119. {
  120. char *p;
  121. // Get a line from file
  122. int result=read_line(f, buffer, &l);
  123. if (result == 1) return -1; // error
  124. if (result == EOF) break;
  125. // Search for sections
  126. if( !strcmp(buffer, "[Settings]") )
  127. {
  128. stype = SECTION_SETTINGS;
  129. continue;
  130. }
  131. if( !strcmp(buffer, "[Library Options]") )
  132. {
  133. stype = SECTION_LIBOPTS;
  134. continue;
  135. }
  136. if( !strcmp(buffer, "[File Editing]") )
  137. {
  138. stype = SECTION_FILEEDIT;
  139. continue;
  140. }
  141. if( !strcmp(buffer, "[Included Files]") )
  142. {
  143. stype = SECTION_FILES;
  144. continue;
  145. }
  146. // Keywords in the [Settings] section
  147. if(stype == SECTION_SETTINGS) {
  148. #define boolean_param(token,setting) \
  149. if ( (p=find_param(buffer, token)) ) \
  150. { \
  151. if(!strcmp(p, "0")) dest->settings.setting = FALSE; \
  152. else if(!strcmp(p, "1")) dest->settings.setting = TRUE; \
  153. else return l; \
  154. continue; \
  155. } else
  156. #define tistring_vparam(token,var) \
  157. if ( (p=find_param(buffer, token)) ) \
  158. { \
  159. if (*p) { \
  160. unsigned short *utf16=ticonv_charset_ti_to_utf16(CALC_TI89,p); \
  161. dest->var = QString::fromUcs2(utf16); \
  162. g_free(utf16); \
  163. } \
  164. continue; \
  165. } else
  166. #define string_param(token,setting) \
  167. if ( (p=find_param(buffer, token)) ) \
  168. { \
  169. if (*p) dest->settings.setting = p; \
  170. continue; \
  171. } else
  172. #define ignore_param(token) \
  173. if( (p=find_param(buffer, token)) ) \
  174. { \
  175. continue; \
  176. } else
  177. boolean_param("Archive=",archive)
  178. boolean_param("Pack=",pack)
  179. string_param("Packed Variable=",pack_name)
  180. tistring_vparam("Project Name=",prj_name)
  181. string_param("GCC Switches=",cc_switches)
  182. string_param("Assembler Switches=",a68k_switches)
  183. ignore_param("Linker Switches=") // Obsolete. Ignore.
  184. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  185. string_param("GNU Assembler Switches=",as_switches)
  186. ignore_param("BSR Patch=") // Obsolete. Ignore.
  187. boolean_param("Debug Info=",debug_info)
  188. boolean_param("Standard Library=",std_lib)
  189. string_param("Command Line=",cmd_line)
  190. string_param("Post-Build Process=",post_build)
  191. boolean_param("Use Data Variable=",use_data_var)
  192. string_param("Data Variable=",data_var)
  193. boolean_param("Copy Data Variable=",copy_data_var)
  194. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  195. boolean_param("Optimize NOPs=",optimize_nops)
  196. boolean_param("Optimize Returns=",optimize_returns)
  197. boolean_param("Optimize Branches=",optimize_branches)
  198. boolean_param("Optimize Moves=",optimize_moves)
  199. boolean_param("Optimize Tests=",optimize_tests)
  200. boolean_param("Optimize Calculations=",optimize_calcs)
  201. boolean_param("Remove Unused Sections=",remove_unused)
  202. boolean_param("Binary Output=",outputbin)
  203. boolean_param("Fargo=",fargo)
  204. boolean_param("Flash OS=",flash_os)
  205. boolean_param("Cut Unused Ranges=",cut_ranges)
  206. boolean_param("Reorder Sections=",reorder_sections)
  207. boolean_param("Merge Constants=",merge_constants)
  208. boolean_param("Initialize BSS=",initialize_bss)
  209. return l;
  210. #undef boolean_param
  211. #undef tistring_vparam
  212. #undef string_param
  213. #undef ignore_param
  214. }
  215. // Keywords in the [Library Options] section
  216. if(stype == SECTION_LIBOPTS) {
  217. #define boolean_param(token,setting) \
  218. if ( (p=find_param(buffer, token)) ) \
  219. { \
  220. if(!strcmp(p, "0")) dest->libopts.setting = FALSE; \
  221. else if(!strcmp(p, "1")) dest->libopts.setting = TRUE; \
  222. else return l; \
  223. continue; \
  224. } else
  225. #define reloc_param(token,setting) \
  226. if ( (p=find_param(buffer, token)) ) \
  227. { \
  228. if (!strcmp(p,"None")) \
  229. dest->libopts.setting = RT_NONE; \
  230. else if (!strcmp(p,"Direct")) \
  231. dest->libopts.setting = RT_DIRECT; \
  232. else if (!strcmp(p,"AMS")) \
  233. dest->libopts.setting = RT_AMS; \
  234. else if (!strcmp(p, "Precomputed")) \
  235. dest->libopts.setting = RT_PRECOMP; \
  236. else if (!strcmp(p, "Kernel")) \
  237. dest->libopts.setting = RT_KERNEL; \
  238. else if (!strcmp(p, "Compressed")) \
  239. dest->libopts.setting = RT_COMPRESSED; \
  240. else if (!strcmp(p, "MLink")) \
  241. dest->libopts.setting = RT_MLINK; \
  242. else if (!strcmp(p, "F-Line")) \
  243. dest->libopts.setting = RT_FLINE; \
  244. else if (strcmp(p, "Unknown")) \
  245. return l; \
  246. continue; \
  247. } else
  248. //this new macro was added to make merging with save_tpr more consistent.
  249. #define minams_param(token,setting) \
  250. if ( (p=find_param(buffer, "Minimum AMS Version=")) ) \
  251. { \
  252. int major, minor; \
  253. if ((strlen(p)==4) \
  254. && (sscanf(p,"%1d.%2d",&major,&minor)==2)) \
  255. dest->libopts.minams = major * 100 + minor; \
  256. else \
  257. return l; \
  258. continue; \
  259. } else
  260. boolean_param("Use TI-89=",use_ti89)
  261. boolean_param("Use TI-92 Plus=",use_ti92p)
  262. boolean_param("Use V200=",use_v200)
  263. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  264. boolean_param("Use Kernel=",use_kernel)
  265. boolean_param("Use PreOS=",use_preos)
  266. boolean_param("Minimum AMS Version Defined=",use_minams)
  267. minams_param("Minimum AMS Version=",minams);
  268. boolean_param("Unofficial OS Support=",unofficial_os)
  269. reloc_param("Reloc Format=",reloc_format)
  270. reloc_param("ROM Call Format=",rom_call_format)
  271. reloc_param("BSS Ref Format=",bss_ref_format)
  272. reloc_param("Data Ref Format=",data_ref_format)
  273. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  274. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  275. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  276. boolean_param("Use Return Value=",use_return_value)
  277. boolean_param("Enable Error Return=",enable_error_return)
  278. boolean_param("Save Screen=",save_screen)
  279. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  280. return l;
  281. #undef boolean_param
  282. #undef reloc_param
  283. #undef minams_param
  284. }
  285. // Keywords in the [File Editing] section
  286. if(stype == SECTION_FILEEDIT)
  287. {
  288. if ( (p=find_param(buffer, "Open File=")) ) \
  289. { \
  290. if (*p) dest->open_file = p; \
  291. continue; \
  292. } else return l;
  293. }
  294. // Keywords in the [Included Files] section
  295. if(stype == SECTION_FILES)
  296. {
  297. int v;
  298. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  299. {
  300. QString s = convert_path_separators(p);
  301. dest->c_files.path << s;
  302. dest->c_files.folder << QString::null;
  303. continue;
  304. }
  305. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  306. {
  307. dest->c_files.folder[v-1]=p;
  308. continue;
  309. }
  310. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  311. {
  312. QString s = convert_path_separators(p);
  313. dest->s_files.path << s;
  314. dest->s_files.folder << QString::null;
  315. continue;
  316. }
  317. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  318. {
  319. dest->s_files.folder[v-1]=p;
  320. continue;
  321. }
  322. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  323. {
  324. QString s = convert_path_separators(p);
  325. dest->h_files.path << s;
  326. dest->h_files.folder << QString::null;
  327. continue;
  328. }
  329. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  330. {
  331. dest->h_files.folder[v-1]=p;
  332. continue;
  333. }
  334. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  335. {
  336. QString s = convert_path_separators(p);
  337. dest->asm_files.path << s;
  338. dest->asm_files.folder << QString::null;
  339. continue;
  340. }
  341. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  342. {
  343. dest->asm_files.folder[v-1]=p;
  344. continue;
  345. }
  346. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  347. {
  348. QString s = convert_path_separators(p);
  349. dest->o_files.path << s;
  350. dest->o_files.folder << QString::null;
  351. continue;
  352. }
  353. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  354. {
  355. dest->o_files.folder[v-1]=p;
  356. continue;
  357. }
  358. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  359. {
  360. QString s = convert_path_separators(p);
  361. dest->a_files.path << s;
  362. dest->a_files.folder << QString::null;
  363. continue;
  364. }
  365. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  366. {
  367. dest->a_files.folder[v-1]=p;
  368. continue;
  369. }
  370. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  371. {
  372. QString s = convert_path_separators(p);
  373. dest->txt_files.path << s;
  374. dest->txt_files.folder << QString::null;
  375. continue;
  376. }
  377. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  378. {
  379. dest->txt_files.folder[v-1]=p;
  380. continue;
  381. }
  382. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  383. {
  384. QString s = convert_path_separators(p);
  385. dest->quill_files.path << s;
  386. dest->quill_files.folder << QString::null;
  387. continue;
  388. }
  389. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  390. {
  391. dest->quill_files.folder[v-1]=p;
  392. continue;
  393. }
  394. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  395. {
  396. QString s = convert_path_separators(p);
  397. dest->oth_files.path << s;
  398. dest->oth_files.folder << QString::null;
  399. continue;
  400. }
  401. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  402. {
  403. dest->oth_files.folder[v-1]=p;
  404. continue;
  405. }
  406. else return l;
  407. }
  408. }
  409. return 0;
  410. }
  411. // returns 0 on success, -1 if it can't open the TPR and a (positive) line
  412. // number if there is an error in the TPR
  413. int loadTPR(const QString &fileName,TPRDataStruct *dest)
  414. {
  415. FILE *f;
  416. int ret;
  417. f = fopen(fileName, "r");
  418. if(f == NULL) {
  419. return -1;
  420. }
  421. ret=parse_file(f,dest);
  422. fclose(f);
  423. return ret;
  424. }
  425. QString loadFileText(const char *fileName)
  426. {
  427. FILE *f;
  428. f=fopen(fileName,"rb");
  429. if (!f)
  430. return QString::null;
  431. fseek(f,0,SEEK_END);
  432. size_t flen=ftell(f);
  433. fseek(f,0,SEEK_SET);
  434. char *buffer = new(std::nothrow) char[flen+1]();
  435. if (!buffer)
  436. return QString::null;
  437. QString ret;
  438. if (fread(buffer,1,flen,f)<flen) {
  439. delete[] buffer;
  440. ret=QString::null;
  441. } else {
  442. if (preferences.useCalcCharset) {
  443. unsigned short *utf16=ticonv_charset_ti_to_utf16(CALC_TI89,buffer);
  444. delete[] buffer;
  445. if (!utf16)
  446. return QString::null;
  447. ret=QString::fromUcs2(utf16);
  448. g_free(utf16);
  449. } else {
  450. ret=buffer;
  451. delete[] buffer;
  452. }
  453. }
  454. if (!ret.isNull()) {
  455. // convert Windows line endings
  456. ret=ret.replace("\r\n","\n");
  457. // interpret remaining \r characters as Mac line endings
  458. ret=ret.replace('\r','\n');
  459. // remove trailing spaces if requested
  460. if (preferences.removeTrailingSpaces) {
  461. ret=ret.replace(QRegExp("((?!\n)\\s)*\n"),"\n");
  462. ret=ret.remove(QRegExp("((?!\n)\\s)*$"));
  463. }
  464. }
  465. return ret;
  466. }
  467. static QString convert_path_separators_save(QString s)
  468. {
  469. int o;
  470. #ifndef __WIN32__
  471. while ((o=s.find('/',0,TRUE))>=0)
  472. s[o]='\\';
  473. #endif
  474. return s;
  475. }
  476. //this is here because QString::ascii() returns "(null)" on a null string.
  477. const char *smartAscii(const QString &s)
  478. {
  479. if (s.isNull())
  480. return "";
  481. return s.ascii();
  482. }
  483. static int save_tpr(FILE *f,TPRDataStruct *dest)
  484. {
  485. unsigned i=0,e;
  486. QString tmp;
  487. #define boolean_param(token,setting) if (fprintf(f,token "%d\r\n",!!dest->settings.setting)<0) return -2;
  488. #define tistring_vparam(token,var) { \
  489. char *ti=ticonv_charset_utf16_to_ti(CALC_TI89,dest->var.ucs2()); \
  490. if (fprintf(f,token "%s\r\n",ti)<0) {g_free(ti); return -2;} \
  491. g_free(ti); \
  492. }
  493. #define string_param(token,setting) if (fprintf(f,token "%s\r\n",smartAscii(dest->settings.setting))<0) return -2;
  494. #define ignore_param(token) /**/
  495. if (fputs("[Settings]\r\n",f)<0) return -2;
  496. boolean_param("Archive=",archive)
  497. boolean_param("Pack=",pack)
  498. string_param("Packed Variable=",pack_name)
  499. tistring_vparam("Project Name=",prj_name)
  500. string_param("GCC Switches=",cc_switches)
  501. string_param("Assembler Switches=",a68k_switches)
  502. ignore_param("Linker Switches=") // Obsolete. Ignore.
  503. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  504. string_param("GNU Assembler Switches=",as_switches)
  505. ignore_param("BSR Patch=") // Obsolete. Ignore.
  506. boolean_param("Debug Info=",debug_info)
  507. boolean_param("Standard Library=",std_lib)
  508. string_param("Command Line=",cmd_line)
  509. string_param("Post-Build Process=",post_build)
  510. boolean_param("Use Data Variable=",use_data_var)
  511. string_param("Data Variable=",data_var)
  512. boolean_param("Copy Data Variable=",copy_data_var)
  513. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  514. boolean_param("Optimize NOPs=",optimize_nops)
  515. boolean_param("Optimize Returns=",optimize_returns)
  516. boolean_param("Optimize Branches=",optimize_branches)
  517. boolean_param("Optimize Moves=",optimize_moves)
  518. boolean_param("Optimize Tests=",optimize_tests)
  519. boolean_param("Optimize Calculations=",optimize_calcs)
  520. boolean_param("Remove Unused Sections=",remove_unused)
  521. boolean_param("Binary Output=",outputbin)
  522. boolean_param("Fargo=",fargo)
  523. boolean_param("Flash OS=",flash_os)
  524. boolean_param("Cut Unused Ranges=",cut_ranges)
  525. boolean_param("Reorder Sections=",reorder_sections)
  526. boolean_param("Merge Constants=",merge_constants)
  527. boolean_param("Initialize BSS=",initialize_bss)
  528. #undef boolean_param
  529. #undef tistring_vparam
  530. #undef string_param
  531. #undef ignore_param
  532. #define boolean_param(token,setting) if (fprintf(f,token "%d\r\n",!!dest->libopts.setting)<0) return -2;
  533. #define reloc_param(token,setting) \
  534. switch(dest->libopts.setting) \
  535. { \
  536. case RT_NONE: \
  537. if (fprintf(f,token "None\r\n")<0) return -2; \
  538. break; \
  539. case RT_DIRECT: \
  540. if (fprintf(f,token "Direct\r\n")<0) return -2; \
  541. break; \
  542. case RT_AMS: \
  543. if (fprintf(f,token "AMS\r\n")<0) return -2; \
  544. break; \
  545. case RT_PRECOMP: \
  546. if (fprintf(f,token "Precomputed\r\n")<0) return -2; \
  547. break; \
  548. case RT_KERNEL: \
  549. if (fprintf(f,token "Kernel\r\n")<0) return -2; \
  550. break; \
  551. case RT_COMPRESSED: \
  552. if (fprintf(f,token "Compressed\r\n")<0) return -2; \
  553. break; \
  554. case RT_MLINK: \
  555. if (fprintf(f,token "MLink\r\n")<0) return -2; \
  556. break; \
  557. case RT_FLINE: \
  558. if (fprintf(f,token "F-Line\r\n")<0) return -2; \
  559. break; \
  560. }
  561. #define minams_param(token,setting) \
  562. { \
  563. int major,minor; \
  564. major=dest->libopts.setting/100; \
  565. minor=dest->libopts.setting%100; \
  566. if (fprintf(f,token "%d.%02d\r\n",major,minor)<0) return -2; \
  567. }
  568. if (fputs("\r\n[Library Options]\r\n",f)<0) return -2;
  569. boolean_param("Use TI-89=",use_ti89)
  570. boolean_param("Use TI-92 Plus=",use_ti92p)
  571. boolean_param("Use V200=",use_v200)
  572. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  573. boolean_param("Use Kernel=",use_kernel)
  574. boolean_param("Use PreOS=",use_preos)
  575. boolean_param("Minimum AMS Version Defined=",use_minams)
  576. minams_param("Minimum AMS Version=",minams);
  577. boolean_param("Unofficial OS Support=",unofficial_os)
  578. reloc_param("Reloc Format=",reloc_format)
  579. reloc_param("ROM Call Format=",rom_call_format)
  580. reloc_param("BSS Ref Format=",bss_ref_format)
  581. reloc_param("Data Ref Format=",data_ref_format)
  582. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  583. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  584. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  585. boolean_param("Use Return Value=",use_return_value)
  586. boolean_param("Enable Error Return=",enable_error_return)
  587. boolean_param("Save Screen=",save_screen)
  588. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  589. #undef boolean_param
  590. #undef reloc_param
  591. #undef minams_param
  592. 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;
  593. #define filepath_param(token,filetype) \
  594. e=dest->filetype.path.count(); \
  595. for(i=0;i<e;i++) \
  596. { \
  597. tmp=convert_path_separators_save(smartAscii(dest->filetype.path[i])); \
  598. if (fprintf(f,token " %u=%s\r\n",i+1,smartAscii(tmp))<0) return -2; \
  599. if (!dest->filetype.folder[i].isEmpty()) \
  600. { \
  601. if (fprintf(f,token " %u Folder=%s\r\n",i+1,smartAscii(dest->filetype.folder[i]))<0) return -2; \
  602. } \
  603. }
  604. filepath_param("C File",c_files)
  605. filepath_param("GNU Assembler File",s_files)
  606. filepath_param("Header File",h_files)
  607. filepath_param("Assembler File",asm_files)
  608. filepath_param("Object File",o_files)
  609. filepath_param("Archive File",a_files)
  610. filepath_param("Text File",txt_files)
  611. filepath_param("Quill File",quill_files)
  612. filepath_param("Other File",oth_files)
  613. #undef filepath_param
  614. if (fputs("\r\n",f)<0) return -2;
  615. return 0;
  616. }
  617. void newSettings(tprSettings *settings,tprLibOpts *libopts)
  618. {
  619. tprSettings newSettings;
  620. tprLibOpts newLibOpts;
  621. *settings=newSettings;
  622. *libopts=newLibOpts;
  623. }
  624. //returns 0 on success, -1 if the file couldn't be created or -2 if fprintf,
  625. //fputs or fclose failed
  626. int saveTPR(const QString &fileName,TPRDataStruct *src)
  627. {
  628. FILE *f;
  629. int ret;
  630. f=fopen(fileName,"wb");
  631. if (!f)
  632. {
  633. return -1;
  634. }
  635. ret=save_tpr(f,src);
  636. if (fclose(f)) return -2;
  637. return ret;
  638. }
  639. static void mkdir_multi(const char *fileName)
  640. {
  641. int l=strlen(fileName);
  642. char buffer[l+2];
  643. char *ptr;
  644. #ifdef __WIN32__
  645. ptr=strchr(fileName,'\\');
  646. #else
  647. ptr=strchr(fileName,'/');
  648. #endif
  649. while (ptr)
  650. {
  651. memcpy(buffer,fileName,ptr-fileName);
  652. buffer[ptr-fileName]=0;
  653. mkdir(buffer,S_IRWXU | S_IRWXG | S_IRWXO);
  654. #ifdef __WIN32__
  655. ptr=strchr(ptr+1,'\\');
  656. #else
  657. ptr=strchr(ptr+1,'/');
  658. #endif
  659. }
  660. }
  661. static int writeToFile(FILE *f, const QString &text)
  662. {
  663. if (preferences.useCalcCharset) {
  664. const unsigned short *utf16=text.ucs2();
  665. if (utf16) {
  666. char *s=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
  667. size_t l=std::strlen(s);
  668. if (fwrite(s,1,l,f)<l) {
  669. g_free(s);
  670. return -2;
  671. }
  672. g_free(s);
  673. }
  674. } else {
  675. const char *s=smartAscii(text);
  676. size_t l=text.length();
  677. if (fwrite(s,1,l,f)<l) return -2;
  678. }
  679. return 0;
  680. }
  681. enum CharModes {cmNone, cmNormalText, cmNumber, cmMultiSymbol, cmString, cmChar,
  682. cmComment, cmUnchangeableLine, cmExtUnchangeableLine,
  683. cmExtUnchangeableLineString, cmTrigraph};
  684. int saveAndSplitFileText(const char *fileName, const QString &fileText,
  685. bool split, bool addCLineDirective,
  686. bool addASMLineDirective, const QString &origFileName,
  687. LineStartList *pLineStartList)
  688. {
  689. FILE *f;
  690. LineStartList lineStartList;
  691. // remove trailing spaces if requested
  692. QString text=fileText;
  693. if (preferences.removeTrailingSpaces && !text.isNull()) {
  694. text=text.replace(QRegExp("((?!\n)\\s)*\n"),"\n");
  695. text=text.remove(QRegExp("((?!\n)\\s)*$"));
  696. }
  697. f=fopen(fileName,"wb");
  698. if (!f)
  699. {
  700. mkdir_multi(fileName);
  701. f=fopen(fileName,"wb");
  702. if (!f)
  703. return -1;
  704. }
  705. if (split && preferences.splitSourceFiles && !settings.debug_info) {
  706. unsigned curPos=0, curLine=0, curCol=0, l=text.length();
  707. bool atLineStart;
  708. CharModes curMode=cmNone;
  709. #define INSERT_CHAR(ch) do {if (writeToFile(f,QString((ch)))) {fclose(f); return -2;}} while(0)
  710. #define INSERT_STRING(str) do {if (writeToFile(f,(str))) {fclose(f); return -2;}} while(0)
  711. #define ADD_LINE() do {lineStartList.append(qMakePair(curLine,curCol)); atLineStart=TRUE;} while(0)
  712. #define IS_NEWLINE() (text[curPos]=='\n')
  713. #define ADD_LINE_NEXT() do {lineStartList.append(qMakePair(curLine+(unsigned)(IS_NEWLINE()),(IS_NEWLINE())?0u:(curCol+1u))); atLineStart=TRUE;} while(0)
  714. #define NEW_LINE() do {if((!(atLineStart||IS_NEWLINE())) || (text[curPos]=='#')) {INSERT_CHAR(QChar('\n')); ADD_LINE();} curMode=cmNone;} while(0)
  715. #define SET_MULTI_CHAR_MODE(mode) do {if (curMode!=(mode)) {NEW_LINE(); curMode=(mode);}} while(0)
  716. ADD_LINE(); // Line 0
  717. ADD_LINE(); // Line 1
  718. for (; curPos<l; curPos++) {
  719. bool noInsert=FALSE;
  720. QChar c=text[curPos];
  721. if (c=='\n')
  722. ADD_LINE_NEXT();
  723. switch (curMode) {
  724. case cmString:
  725. if (c=='\"') {
  726. bool b=TRUE;
  727. unsigned i=curPos-1;
  728. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  729. b=!b;
  730. if (text[i]=='\\') --i; else i-=3;
  731. }
  732. if (b) {
  733. curMode=cmNone;
  734. noInsert=TRUE;
  735. INSERT_STRING(QString(c)+"\n");
  736. atLineStart=TRUE;
  737. ADD_LINE_NEXT();
  738. }
  739. }
  740. break;
  741. case cmChar:
  742. if (c=='\'') {
  743. bool b=TRUE;
  744. unsigned i=curPos-1;
  745. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  746. b=!b;
  747. if (text[i]=='\\') --i; else i-=3;
  748. }
  749. if (b) curMode=cmNone;
  750. }
  751. break;
  752. case cmComment:
  753. if ((c=='/')&&(text[curPos-1]=='*')) curMode=cmNone;
  754. break;
  755. case cmUnchangeableLine:
  756. if (c=='\n') curMode=cmNone;
  757. break;
  758. case cmExtUnchangeableLine:
  759. if ((c=='\n')&&(text[curPos-1]!='\\')) curMode=cmNone;
  760. else if (c=='\"') curMode=cmExtUnchangeableLineString;
  761. break;
  762. case cmExtUnchangeableLineString:
  763. if (c=='\"') {
  764. bool b=TRUE;
  765. unsigned i=curPos-1;
  766. while (i && ((text[i]=='\\')||(i>=2&&!text.mid(i-2,3).compare("?""?""/")))) {
  767. b=!b;
  768. if (text[i]=='\\') --i; else i-=3;
  769. }
  770. if (b) curMode=cmExtUnchangeableLine;
  771. }
  772. break;
  773. case cmTrigraph:
  774. if ((c!='?')&&((curPos+1>=l)||(text[curPos+1]!='?'))) curMode=cmNone;
  775. break;
  776. default:
  777. if (!text.mid(curPos,2).compare("//"))
  778. SET_MULTI_CHAR_MODE(cmUnchangeableLine);
  779. else if (!text.mid(curPos,2).compare("/*"))
  780. SET_MULTI_CHAR_MODE(cmComment);
  781. else if (!text.mid(curPos,2).compare("?""?""="))
  782. SET_MULTI_CHAR_MODE(cmExtUnchangeableLine);
  783. else if (!text.mid(curPos,2).compare("?""?")&&(curPos+2<l)
  784. &&QString("()/\'<>!-").contains(text[curPos+2]))
  785. SET_MULTI_CHAR_MODE(cmTrigraph);
  786. else {
  787. switch(c.unicode()) {
  788. case ' ':
  789. case '\t':
  790. if (curPos && text[curPos-1]!=' ' && text[curPos-1]!='\t')
  791. NEW_LINE();
  792. break;
  793. case 'A' ... 'Z':
  794. case 'a' ... 'z':
  795. case '0' ... '9':
  796. case '_':
  797. case '$':
  798. if (curMode!=cmNormalText && curMode!=cmNumber) {
  799. NEW_LINE();
  800. if (c>=QChar('0') && c<=QChar('9'))
  801. curMode=cmNumber;
  802. else
  803. curMode=cmNormalText;
  804. }
  805. break;
  806. case '\"':
  807. SET_MULTI_CHAR_MODE(cmString);
  808. break;
  809. case '\'':
  810. SET_MULTI_CHAR_MODE(cmChar);
  811. break;
  812. case '#':
  813. SET_MULTI_CHAR_MODE(cmExtUnchangeableLine);
  814. break;
  815. case '.':
  816. if (curMode!=cmNumber) {
  817. if ((curPos+1<l)
  818. &&(text[curPos+1]>=QChar('0')
  819. &&text[curPos+1]<=QChar('9'))) {
  820. NEW_LINE();
  821. curMode=cmNumber;
  822. } else SET_MULTI_CHAR_MODE(cmMultiSymbol);
  823. }
  824. break;
  825. case '+':
  826. case '-':
  827. if ((curMode!=cmNumber)||(curPos<=1)
  828. ||!QString("eEpP").contains(text[curPos-1]))
  829. SET_MULTI_CHAR_MODE(cmMultiSymbol);
  830. break;
  831. default:
  832. if (QString(",;()[]{}").contains(c)) {
  833. if (curMode!=cmNone) {
  834. NEW_LINE();
  835. curMode=cmNone;
  836. }
  837. if ((curPos+1<l)&&text[curPos+1]!='\n') {
  838. noInsert=TRUE;
  839. INSERT_STRING(QString(c)+"\n");
  840. ADD_LINE_NEXT();
  841. } else SET_MULTI_CHAR_MODE(cmMultiSymbol);
  842. }
  843. break;
  844. }
  845. }
  846. break;
  847. }
  848. if (!noInsert) {
  849. INSERT_CHAR(c);
  850. if (c!='\n') atLineStart=FALSE;
  851. }
  852. if (c=='\n') {
  853. curLine++;
  854. curCol=0;
  855. } else curCol++;
  856. }
  857. NEW_LINE();
  858. #undef INSERT_CHAR
  859. #undef INSERT_STRING
  860. #undef ADD_LINE
  861. #undef IS_NEWLINE
  862. #undef ADD_LINE_NEXT
  863. #undef NEW_LINE
  864. #undef SET_MULTI_CHAR_MODE
  865. } else {
  866. if ((addCLineDirective || addASMLineDirective) && settings.debug_info) {
  867. QString escapedFileName=origFileName, lineDirective;
  868. escapedFileName.replace("\\","\\\\");
  869. escapedFileName.replace("\"","\\\"");
  870. // These have no business to be in a file name, but someone somewhere may
  871. // have that very bad idea...
  872. escapedFileName.replace("\r","\\r");
  873. escapedFileName.replace("\n","\\n");
  874. lineDirective=QString(addCLineDirective
  875. ?"#line 1 \"%1\"\n"
  876. :".appfile \"%1\"; .appline 1\n").arg(escapedFileName);
  877. // Don't use calc charset for this, it's a host file name.
  878. const char *s=smartAscii(escapedFileName);
  879. size_t l=escapedFileName.length();
  880. if (fwrite(s,1,l,f)<l) return -2;
  881. }
  882. if (writeToFile(f,text)) {fclose(f); return -2;}
  883. if (addCLineDirective) {
  884. if (fwrite("\n",1,1,f)<1) {fclose(f); return -2;}
  885. }
  886. }
  887. if (fclose(f)) return -2;
  888. if (pLineStartList) *pLineStartList=lineStartList;
  889. return 0;
  890. }
  891. int saveFileText(const char *fileName,const QString &fileText)
  892. {
  893. return saveAndSplitFileText(fileName,fileText,FALSE,FALSE,FALSE,QString::null,
  894. static_cast<LineStartList *>(NULL));
  895. }
  896. void kurlNewFileName(KURL &dir,const QString &newFileName)
  897. {
  898. if (newFileName[0]=='/')
  899. dir.setPath(newFileName);
  900. else
  901. dir.setFileName(newFileName);
  902. }
  903. static QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  904. {
  905. int a,b;
  906. QString ret;
  907. destFileName=srcFileName;
  908. a=destFileName.findRev('.');
  909. b=destFileName.findRev('/');
  910. if (a<0)
  911. return QString::null;
  912. if (a<b)
  913. return QString::null;
  914. ret=destFileName.mid(a+1);
  915. destFileName.truncate(a);
  916. return ret;
  917. }
  918. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  919. {
  920. int i;
  921. QString fileName_name,fileName_suffix;
  922. QString name,suffix;
  923. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  924. for (i=fileNameList.count()-1;i>=0;i--)
  925. {
  926. suffix=pullOutFileSuffix(fileNameList[i],name);
  927. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  928. {
  929. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  930. {
  931. if (!name.compare(fileName_name))
  932. return 0;
  933. }
  934. }
  935. else
  936. {
  937. if (!fileNameList[i].compare(fileName))
  938. return 0;
  939. }
  940. }
  941. return 1;
  942. }
  943. // returns 0 on success, >0 on read failure, <0 on write failure
  944. int copyFile(const char *src, const char *dest)
  945. {
  946. // This doesn't load everything at once onto the stack because it may be
  947. // used for huge binary files, which don't fit on the stack. So we copy 1KB
  948. // at a time.
  949. FILE *sf=fopen(src,"rb");
  950. if (!sf) return 1;
  951. FILE *df=fopen(dest,"wb");
  952. if (!df) {fclose(sf); return -1;}
  953. char buffer[1024];
  954. while (!ferror(sf) && !feof(sf)) {
  955. size_t bytes_read=fread(buffer,1,1024,sf);
  956. if (fwrite(buffer,1,bytes_read,df)<bytes_read) {
  957. fclose(df);
  958. fclose(sf);
  959. return -2;
  960. }
  961. }
  962. if (ferror(sf)) {
  963. fclose(df);
  964. fclose(sf);
  965. return 2;
  966. }
  967. if (fclose(df)) {fclose(sf); return -3;}
  968. if (fclose(sf)) return 3;
  969. return 0;
  970. }
  971. int getPathType(const QString &thePath)
  972. {
  973. struct stat statvar;
  974. int result=stat(thePath,&statvar);
  975. if (result)
  976. return errno==ENOENT?PATH_NOTFOUND:PATH_ERROR;
  977. if (statvar.st_mode&S_IFDIR)
  978. return PATH_FOLDER;
  979. if (statvar.st_mode&S_IFREG)
  980. return PATH_FILE;
  981. return PATH_ERROR;
  982. }