File: main.cpp

package info (click to toggle)
freemat 4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 174,756 kB
  • ctags: 67,023
  • sloc: cpp: 351,059; ansic: 255,892; sh: 40,590; makefile: 4,387; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (158 lines) | stat: -rw-r--r-- 5,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <QDir>
#include <QtGui>
#include <QDebug>
#if !defined(_MSC_VER ) 
#include <unistd.h>
#else
#include <fstream>
#include <iostream>
#endif
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include "MainApp.hpp"
#include <qapplication.h>
#include "Exception.hpp"
#include "application.hpp"
#include "FuncMode.hpp"


MainApp *m_app;
FuncMode *m_func;

void usage() {
  printf("%s\n  Command Line Help\n",qPrintable(Interpreter::getVersionString()));
  printf(" You can invoke FreeMat with the following command line options:\n");
  printf("     -f <command>  Runs FreeMat in command mode.  FreeMat will \n");
  printf("                   startup, run the given command, and then quit.\n");
  printf("                   Note that this option uses the remainder of the\n");
  printf("                   command line, so use it last.\n");
#ifdef Q_WS_X11
  printf("     -nogui        Suppress the GUI for FreeMat.\n");
  //  printf("     -noplastique  Do not force the plastique style for GUI.\n");
#endif
  printf("     -noX          Disables the graphics subsystem.\n");
  printf("     -e            uses a dumb terminal interface \n");
  printf("                   (no command line editing, etc.)\n");
  printf("                   This flag is primarily used when \n");
  printf("                   you want to capture input/output\n");
  printf("                   to FreeMat from another application.\n");
  printf("     -i <path>     Install FreeMat - provide the path to the\n");
  printf("                   FreeMat data directory (containing the\n");
  printf("                   scripts, help and other files.).  Normally\n");
  printf("                   these are installed in /usr/local/share/\n");
  printf("                   but regardless, you must run FreeMat -i once\n");
  printf("                   to indicate the location of this directory.\n");
  printf("                   Note that in this mode, FreeMat will only \n");
  printf("                   update its internal configuration and then\n");
  printf("                   exit.\n");
  printf("     -p <path>     Set the FreeMat path to the given pathspec.\n");
  printf("     -help         Get this help text\n");
  exit(0);
}


// Search through the arguments to freemat... look for the given
// flag.  if the flagarg variable is true, then an argument must
// be provided to the flag.  If the flag is not found, then a 
// 0 is returned.  Otherwise, the index into argv of the flag is
// returned.
int parseFlagArg(int argc, char *argv[], const char* flagstring, bool flagarg) {
  bool flagFound = false;
  int ndx;
  ndx = 1;
  while (!flagFound && ndx < argc) {
    flagFound = strcmp(argv[ndx],flagstring) == 0;
    if (!flagFound) ndx++;
  }
  if (flagFound && flagarg && (ndx == argc-1)) {
    fprintf(stderr,"Error: flag %s requires an argument!\n",flagstring);
    exit(1);
  }
  if (!flagFound)
    ndx = 0;
  return ndx;
}

void sigDoNothing(int arg) {
}

int main(int argc, char *argv[]) {  
  QCoreApplication *app;
  int nogui = parseFlagArg(argc,argv,"-nogui",false);
  int scriptMode = parseFlagArg(argc,argv,"-e",false); 
  int noX = parseFlagArg(argc,argv,"-noX",false);
  int help = parseFlagArg(argc,argv,"-help",false);
  int help2 = parseFlagArg(argc,argv,"--help",false);
  int funcMode = parseFlagArg(argc,argv,"-f",true);
  int nogreet = parseFlagArg(argc,argv,"-nogreet",false);
  //  int noplastique = parseFlagArg(argc,argv,"-noplastique",false);
  int installMode = parseFlagArg(argc,argv,"-i",true);
  int pathMode = parseFlagArg(argc,argv,"-p",true);

  signal(SIGINT,sigDoNothing);
  
  if (installMode) {
    app = new QCoreApplication(argc, argv);
    QSettings settings("FreeMat", Interpreter::getVersionString());
    settings.setValue("root",argv[installMode+1]);
    dbout << "FreeMat root path set to '" << argv[installMode+1] << "'\n";
    return 0;
  }

  if (help || help2) usage();
  if (!noX) {
    app = new QApplication(argc, argv);
    //#ifdef Q_WS_X11
    //    if (!noplastique)
    //      QApplication::setStyle(new QPlastiqueStyle);
    //#endif
  } else {
    app = new QCoreApplication(argc, argv);
    nogui = true;
  }
  
  if (pathMode) {
    QSettings settings("FreeMat", Interpreter::getVersionString());
    settings.setValue("interpreter/path",QString::fromStdString(argv[pathMode+1]).split(":"));
  }

  if (scriptMode) nogui = 1;
  m_app = new MainApp;
  if (!nogui)
    m_app->SetupGUICase();
  else if (!scriptMode) 
    m_app->SetupInteractiveTerminalCase();
  else
    m_app->SetupDumbTerminalCase();
  m_app->SetGUIMode(!noX);
  m_app->SetSkipGreeting(nogreet);
  m_app->Run();
  //  QTimer::singleShot(0,m_app,SLOT(Run()));
  // In function mode, we need to send a command to the GUI
  if (funcMode) {
    m_func = new FuncMode(argv[funcMode+1]);
    QObject::connect(m_func,SIGNAL(SendCommand(QString)),
 		     m_app->GetKeyManager(),SLOT(QueueSilent(QString)));
    QTimer::singleShot(0,m_func,SLOT(Fire()));
  }
  return app->exec();
}