ktigcc.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2004-2007 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>
  21. #include <QImage>
  22. #include <QDir>
  23. #include <QSettings>
  24. #include <kapplication.h>
  25. #include <kcmdlineargs.h>
  26. #include <kaboutdata.h>
  27. #include <QTextCodec>
  28. #include <QIcon>
  29. #include <QSize>
  30. #include <QPixmap>
  31. #include <kglobal.h>
  32. #include <kicontheme.h>
  33. #include <kiconloader.h>
  34. #include <kconfig.h>
  35. #include <glib.h>
  36. #include "mainform.h"
  37. #include "tpr.h"
  38. using namespace std;
  39. // convert to KLocalizedString without translating (we don't support NLS)
  40. #define KLS(x) ki18nc("untranslatable",(x))
  41. const char *tigcc_base;
  42. char tempdir[]="/tmp/ktigccXXXXXX";
  43. char *quill_drv;
  44. bool have_fargo;
  45. bool have_flashos;
  46. KSharedConfigPtr pconfig;
  47. KAboutData *pabout;
  48. const char *parg;
  49. static void log_eater(const gchar *log_domain __attribute__((unused)),
  50. GLogLevelFlags log_level __attribute__((unused)),
  51. const gchar *message __attribute__((unused)),
  52. gpointer user_data __attribute__((unused)))
  53. {
  54. // Do nothing.
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. // Create the libticonv text codec.
  59. new TiconvTextCodec();
  60. // Match the locale for the default C string <-> QString conversions.
  61. // Hopefully it is a .UTF-8 locale, if it isn't, don't complain about
  62. // characters lost converting!
  63. QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
  64. KAboutData about("ktigcc", QByteArray(), // catalogName
  65. KLS("TIGCC IDE for KDE"),"1.80",
  66. KLS("TIGCC C and ASM SDK"), KAboutData::License_GPL,
  67. KLS("Copyright (C) 2004-2007 Kevin Kofler and Joey Adams. All rights reserved.\n"
  68. "TIGCC Copyright (C) 1999-2007 The TIGCC Team."),
  69. KLS("Original linker by Xavier and Niklas\n"
  70. "Compiler modifications by Jean, Sebastian and Kevin\n"
  71. "Linker by Sebastian and Kevin\n"
  72. "Documentation by Zeljko\n"
  73. "A68k modifications by Kevin\n"
  74. "Windows IDE by Sebastian\n"
  75. "KTIGCC IDE by Kevin and Joey\n"
  76. "Documentation conversion to CHM by Philipp\n"
  77. "Documentation conversion to Qt Assistant by Kevin"),
  78. "http://tigcc.ticalc.org/linux/", "Bugs@tigcc.ticalc.org");
  79. pabout=&about;
  80. KCmdLineArgs::init(argc,argv,&about);
  81. KCmdLineOptions options;
  82. options.add("+[file]", KLS("Project or file to open at startup"));
  83. KCmdLineArgs::addCmdLineOptions(options);
  84. KCmdLineArgs::addStdCmdLineOptions();
  85. KApplication app;
  86. about.setProgramLogo(QPixmap(":/images/icon.png"));
  87. pconfig=KGlobal::config();
  88. if ((tigcc_base = getenv("TIGCC")) == NULL) {
  89. fprintf(stderr, "Fatal error: TIGCC is not defined in the environment. "
  90. "TIGCC must be defined before tigcc can run.\nFor (ba)sh, "
  91. "try: export TIGCC=/path/to/tigcc\nFor (t)csh, try: setenv "
  92. "TIGCC /path/to/tigcc\n");
  93. exit(-1);
  94. }
  95. quill_drv = (char *) malloc (strlen(tigcc_base) + 25);
  96. if (!quill_drv) {
  97. fprintf(stderr, "Fatal error: not enough free memory\n");
  98. exit(-1);
  99. }
  100. sprintf(quill_drv, "%s/bin/quill.drv", tigcc_base);
  101. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  102. sprintf(quill_drv, "%s/bin/Quill.drv", tigcc_base);
  103. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  104. sprintf(quill_drv, "%s/include/c/quill.drv", tigcc_base);
  105. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  106. sprintf(quill_drv, "%s/include/c/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/include/Quill/quill.drv", tigcc_base);
  113. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  114. sprintf(quill_drv, "%s/include/Quill/Quill.drv", tigcc_base);
  115. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  116. sprintf(quill_drv, "%s/lib/quill.drv", tigcc_base);
  117. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  118. sprintf(quill_drv, "%s/lib/Quill.drv", tigcc_base);
  119. if(access(quill_drv, F_OK) != -1) goto quill_drv_found;
  120. free(quill_drv);
  121. quill_drv=NULL;
  122. quill_drv_found:
  123. char runtime_archive[strlen(tigcc_base) + 15];
  124. sprintf(runtime_archive, "%s/lib/fargo.a", tigcc_base);
  125. have_fargo=(access(runtime_archive, F_OK)!=-1);
  126. sprintf(runtime_archive, "%s/lib/flashos.a", tigcc_base);
  127. have_flashos=(access(runtime_archive, F_OK)!=-1);
  128. if (!mkdtemp(tempdir)) exit(1);
  129. KCmdLineArgs *args=KCmdLineArgs::parsedArgs();
  130. if (args->count())
  131. parg=args->arg(0);
  132. else
  133. parg=NULL;
  134. // Keep the tilibs from spamming the console with INFO messages.
  135. g_log_set_handler("ticables",(GLogLevelFlags)(G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG),
  136. log_eater,NULL);
  137. g_log_set_handler("tifiles",(GLogLevelFlags)(G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG),
  138. log_eater,NULL);
  139. g_log_set_handler("ticalcs",(GLogLevelFlags)(G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG),
  140. log_eater,NULL);
  141. MainForm mainForm;
  142. app.setMainWidget( &mainForm );
  143. mainForm.show();
  144. int exitcode=app.exec();
  145. rmdir(tempdir);
  146. return exitcode;
  147. }
  148. void write_temp_file(const char *filename, const char *data, const size_t len)
  149. {
  150. char buffer[strlen(tempdir)+strlen(filename)+2];
  151. sprintf(buffer,"%s/%s",tempdir,filename);
  152. FILE *f=fopen(buffer,"wb");
  153. if (!f) {error: fputs("Fatal error: Can't write temp file!\n",stderr); exit(1);}
  154. if (len) {
  155. if (fwrite(data,1,len,f)<len) goto error;
  156. } else {
  157. if (fputs(data,f)==EOF) goto error;
  158. }
  159. fclose(f);
  160. }
  161. void delete_temp_file(const char *filename)
  162. {
  163. char buffer[strlen(tempdir)+strlen(filename)+2];
  164. sprintf(buffer,"%s/%s",tempdir,filename);
  165. if (unlink(buffer)) {fputs("Fatal error: Can't delete temp file!\n",stderr); exit(1);}
  166. }
  167. static bool clear_dir(const QString &dirname)
  168. {
  169. QDir qdir(dirname);
  170. QStringList subdirs=qdir.entryList(QDir::Dirs|QDir::Hidden);
  171. for (QStringList::Iterator it=subdirs.begin();it!=subdirs.end();++it) {
  172. if ((*it).compare(".") && (*it).compare("..")) {
  173. QString subdir=dirname+"/"+*it;
  174. if (!clear_dir(subdir)) return FALSE;
  175. if (!qdir.rmdir(subdir)) return FALSE;
  176. }
  177. }
  178. QStringList files=qdir.entryList(QDir::Files|QDir::Hidden);
  179. for (QStringList::Iterator it=files.begin();it!=files.end();++it) {
  180. QString file=dirname+"/"+*it;
  181. if (!qdir.remove(file)) return FALSE;
  182. }
  183. return TRUE;
  184. }
  185. void clear_temp_dir(void)
  186. {
  187. if (!clear_dir(tempdir)) {fputs("Fatal error: Can't delete temp file!\n",stderr); exit(1);};
  188. }
  189. void force_qt_assistant_page(int n)
  190. {
  191. QSettings assistantSettings("Trolltech","Assistant");
  192. const char *qtVersion=qVersion();
  193. int qtMajor, qtMinor;
  194. sscanf(qtVersion, "%d.%d", &qtMajor, &qtMinor);
  195. assistantSettings.beginGroup(QString("%1.%2").arg(qtMajor).arg(qtMinor));
  196. assistantSettings.setValue("SideBarPage",n);
  197. }
  198. const char *lookup_doc_keyword(const char *keyword)
  199. {
  200. static char filename[256];
  201. memset(filename,0,256);
  202. char fname[strlen(tigcc_base)+27];
  203. sprintf(fname,"%s/doc/html/qt-assistant.adp",tigcc_base);
  204. FILE *f=fopen(fname,"r+b");
  205. if (!f) return "";
  206. char buffer[32768], keywbuf[32768];
  207. while (!feof(f)) {
  208. if (!fgets(buffer,32768,f)) {fclose(f); return "";}
  209. memset(keywbuf,0,32768);
  210. if (sscanf(buffer,"<keyword ref=\"%255[^\"]\">%32767[^<]</keyword>",filename,keywbuf)<2)
  211. continue;
  212. if (!strcmp(keywbuf,keyword)) {fclose(f); return filename;}
  213. }
  214. fclose(f);
  215. return "";
  216. }