ktigcc.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2004-2006 Kevin Kofler
  4. Copyright (C) 2006 Joey Adams
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <unistd.h>
  20. #include <qstring.h>
  21. #include <qimage.h>
  22. #include <qdir.h>
  23. #include <kapplication.h>
  24. #include <kcmdlineargs.h>
  25. #include <kaboutdata.h>
  26. #include <qtextcodec.h>
  27. #include <qiconset.h>
  28. #include <qsize.h>
  29. #include <kglobal.h>
  30. #include <kicontheme.h>
  31. #include <kiconloader.h>
  32. #include <kconfig.h>
  33. #include "mainform.h"
  34. using namespace std;
  35. void qCleanupImages_ktigcc();
  36. void qInitImages_ktigcc();
  37. static KCmdLineOptions options[] =
  38. {
  39. { "+[file]", "Project or file to open at startup", 0 },
  40. KCmdLineLastOption
  41. };
  42. const char *tigcc_base;
  43. char tempdir[]="/tmp/ktigccXXXXXX";
  44. char *quill_drv;
  45. bool have_fargo;
  46. bool have_flashos;
  47. KConfig *pconfig;
  48. KAboutData *pabout;
  49. const char *parg;
  50. int main( int argc, char *argv[] )
  51. {
  52. // Match the locale for the default C string <-> QString conversions.
  53. // Hopefully it is a .UTF-8 locale, if it isn't, don't complain about
  54. // characters lost converting!
  55. QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
  56. KAboutData about("ktigcc","TIGCC IDE for KDE","1.01",
  57. "TIGCC C and ASM SDK", KAboutData::License_GPL,
  58. "Copyright (C) 2004-2006 Kevin Kofler and Joey Adams. All rights reserved.\n"
  59. "TIGCC Copyright (C) 1999-2006 The TIGCC Team.",
  60. "Original linker by Xavier and Niklas\n"
  61. "Compiler modifications by Jean, Sebastian and Kevin\n"
  62. "Linker by Sebastian and Kevin\n"
  63. "Documentation by Zeljko\n"
  64. "A68k modifications by Kevin\n"
  65. "Windows IDE by Sebastian\n"
  66. "KTIGCC IDE by Kevin and Joey\n"
  67. "Documentation conversion to CHM by Philipp\n"
  68. "Documentation conversion to Qt Assistant by Kevin",
  69. "http://tigcc.ticalc.org/linux/", "Bugs@tigcc.ticalc.org");
  70. pabout=&about;
  71. KCmdLineArgs::init(argc,argv,&about);
  72. KCmdLineArgs::addCmdLineOptions(options);
  73. KApplication::addCmdLineOptions();
  74. KApplication app;
  75. // Set the preferred large icon size so system toolbar icons don't get
  76. // annoying padding.
  77. int toolbarIconSize=KIconLoader().currentSize(KIcon::MainToolbar);
  78. QIconSet::setIconSize(QIconSet::Large,QSize(toolbarIconSize,toolbarIconSize));
  79. // Readd the images KDE kindly removes...
  80. qCleanupImages_ktigcc();
  81. qInitImages_ktigcc();
  82. about.setProgramLogo(QImage::fromMimeSource("icon.png"));
  83. pconfig=KGlobal::config();
  84. if ((tigcc_base = getenv("TIGCC")) == NULL) {
  85. fprintf(stderr, "Fatal error: TIGCC is not defined in the environment. "
  86. "TIGCC must be defined before tigcc can run.\nFor (ba)sh, "
  87. "try: export TIGCC=/path/to/tigcc\nFor (t)csh, try: setenv "
  88. "TIGCC /path/to/tigcc\n");
  89. exit(-1);
  90. }
  91. quill_drv = (char *) malloc (strlen(tigcc_base) + 25);
  92. if (!quill_drv) {
  93. fprintf(stderr, "Fatal error: not enough free memory\n");
  94. exit(-1);
  95. }
  96. sprintf(quill_drv, "%s/bin/quill.drv", tigcc_base);
  97. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  98. sprintf(quill_drv, "%s/bin/Quill.drv", tigcc_base);
  99. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  100. sprintf(quill_drv, "%s/include/c/quill.drv", tigcc_base);
  101. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  102. sprintf(quill_drv, "%s/include/c/Quill.drv", tigcc_base);
  103. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  104. sprintf(quill_drv, "%s/include/quill/quill.drv", tigcc_base);
  105. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  106. sprintf(quill_drv, "%s/include/quill/Quill.drv", tigcc_base);
  107. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  108. sprintf(quill_drv, "%s/include/Quill/quill.drv", tigcc_base);
  109. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  110. sprintf(quill_drv, "%s/include/Quill/Quill.drv", tigcc_base);
  111. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  112. sprintf(quill_drv, "%s/lib/quill.drv", tigcc_base);
  113. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  114. sprintf(quill_drv, "%s/lib/Quill.drv", tigcc_base);
  115. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  116. free(quill_drv);
  117. quill_drv=NULL;
  118. quill_drv_found:
  119. char runtime_archive[strlen(tigcc_base) + 15];
  120. sprintf(runtime_archive, "%s/lib/fargo.a", tigcc_base);
  121. have_fargo=(access(runtime_archive, F_OK)!=-1);
  122. sprintf(runtime_archive, "%s/lib/flashos.a", tigcc_base);
  123. have_flashos=(access(runtime_archive, F_OK)!=-1);
  124. if (!mkdtemp(tempdir)) exit(1);
  125. KCmdLineArgs *args=KCmdLineArgs::parsedArgs();
  126. if (args->count())
  127. parg=args->arg(0);
  128. else
  129. parg=NULL;
  130. MainForm mainForm;
  131. app.setMainWidget( &mainForm );
  132. mainForm.show();
  133. int exitcode=app.exec();
  134. rmdir(tempdir);
  135. return exitcode;
  136. }
  137. void write_temp_file(const char *filename, const char *data, const size_t len)
  138. {
  139. char buffer[strlen(tempdir)+strlen(filename)+2];
  140. sprintf(buffer,"%s/%s",tempdir,filename);
  141. FILE *f=fopen(buffer,"wb");
  142. if (!f) {error: fputs("Fatal error: Can't write temp file!\n",stderr); exit(1);}
  143. if (len) {
  144. if (fwrite(data,1,len,f)<len) goto error;
  145. } else {
  146. if (fputs(data,f)==EOF) goto error;
  147. }
  148. fclose(f);
  149. }
  150. void delete_temp_file(const char *filename)
  151. {
  152. char buffer[strlen(tempdir)+strlen(filename)+2];
  153. sprintf(buffer,"%s/%s",tempdir,filename);
  154. if (unlink(buffer)) {fputs("Fatal error: Can't delete temp file!\n",stderr); exit(1);}
  155. }
  156. static bool clear_dir(const QString &dirname)
  157. {
  158. QDir qdir(dirname);
  159. QStringList subdirs=qdir.entryList(QDir::Dirs|QDir::Hidden);
  160. for (QStringList::Iterator it=subdirs.begin();it!=subdirs.end();++it) {
  161. if ((*it).compare(".") && (*it).compare("..")) {
  162. QString subdir=dirname+"/"+*it;
  163. if (!clear_dir(subdir)) return FALSE;
  164. if (!qdir.rmdir(subdir)) return FALSE;
  165. }
  166. }
  167. QStringList files=qdir.entryList(QDir::Files|QDir::Hidden);
  168. for (QStringList::Iterator it=files.begin();it!=files.end();++it) {
  169. QString file=dirname+"/"+*it;
  170. if (!qdir.remove(file)) return FALSE;
  171. }
  172. return TRUE;
  173. }
  174. void clear_temp_dir(void)
  175. {
  176. if (!clear_dir(tempdir)) {fputs("Fatal error: Can't delete temp file!\n",stderr); exit(1);};
  177. }
  178. void force_qt_assistant_page(int n)
  179. {
  180. QString fname=QDir::homeDirPath()+"/.qt/qt_assistantrc";
  181. FILE *f=fopen(fname,"r+b");
  182. if (!f) f=fopen(fname,"w+b");
  183. if (!f) exit(1);
  184. fseek(f,0,SEEK_END);
  185. size_t flen=ftell(f);
  186. fseek(f,0,SEEK_SET);
  187. char buffer[flen+1];
  188. memset(buffer,0,flen+1);
  189. if (fread(buffer,1,flen,f)<flen) exit(1);
  190. char *p=strstr(buffer,"SideBarPage=");
  191. if (p) {
  192. fseek(f,p-buffer+12,SEEK_SET);
  193. fputc('0'+n,f);
  194. } else {
  195. fseek(f,0,SEEK_END);
  196. fprintf(f,"\n\n[3.3]\nSideBarPage=%d\n",n);
  197. }
  198. fclose(f);
  199. }
  200. const char *lookup_doc_keyword(const char *keyword)
  201. {
  202. static char filename[256];
  203. memset(filename,0,256);
  204. char fname[strlen(tigcc_base)+27];
  205. sprintf(fname,"%s/doc/html/qt-assistant.adp",tigcc_base);
  206. FILE *f=fopen(fname,"r+b");
  207. if (!f) return "";
  208. char buffer[32768], keywbuf[32768];
  209. while (!feof(f)) {
  210. if (!fgets(buffer,32768,f)) {fclose(f); return "";}
  211. memset(keywbuf,0,32768);
  212. if (sscanf(buffer,"<keyword ref=\"%255[^\"]\">%32767[^<]</keyword>",filename,keywbuf)<2)
  213. continue;
  214. if (!strcmp(keywbuf,keyword)) {fclose(f); return filename;}
  215. }
  216. fclose(f);
  217. return "";
  218. }