File: input.cc

package info (click to toggle)
gpsim 0.26.1-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,200 kB
  • sloc: cpp: 86,839; sh: 18,875; asm: 16,815; ansic: 4,468; makefile: 1,308; lex: 1,129; yacc: 823; pascal: 177; perl: 93; awk: 44
file content (1003 lines) | stat: -rw-r--r-- 22,801 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
/*
   Copyright (C) 1998 T. Scott Dattalo

This file is part of gpsim.

gpsim 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, or (at your option)
any later version.

gpsim 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 gpsim; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#include <string>
#include <list>
#include <csignal>
#include <iostream>

#ifdef _WIN32
#include <direct.h>
#include "fd2raw.h"
#define GETCWD _getcwd

#else
#include <sys/file.h>
#include <unistd.h>

#if !defined(_MAX_PATH)
#define _MAX_PATH 1024
#endif

#define GETCWD getcwd
// temp fix to over come 23jan05 changes to configure script that prevent
// readline from being found on older systems.
#define HAVE_READLINE
#define HAVE_NSCLEAN_READLINE

#endif

#include "../config.h"

#ifdef HAVE_GUI
#include <glib.h>
#include <gdk/gdktypes.h>
#include <gdk/gdk.h>
#endif

#include "../src/exports.h"
#include "../src/sim_context.h"
#include "../src/gpsim_def.h"
#include "../src/gpsim_classes.h"
#include "../src/icd.h"
#include "../src/pic-processor.h"
#include "../src/breakpoints.h"
#include "../src/cod.h"
#include "command.h"
#include "input.h"
#include "cmd_macro.h"
#include "cmd_run.h"

#ifdef HAVE_LIBREADLINE
#define HAVE_READLINE
#endif

#ifdef HAVE_READLINE
/* See if we have namespace-clean readline or not */
#ifdef HAVE_RL_COMPLETION_MATCHES
#define HAVE_NSCLEAN_READLINE
#endif

extern "C" {
#include <readline/readline.h>
#include <readline/history.h>
}

static GIOChannel *channel;

#endif /* HAVE_READLINE */

void simulation_cleanup();

extern const char *get_dir_delim(const char *path);
int parse_string(const char * str);

//------------------------------------------------------------------------
//
LIBGPSIM_EXPORT bool gUsingThreads(); // in ../src/interface.cc

void initialize_readline (void);
void clear_input_buffer(void);

#ifdef HAVE_GUI
void quit_gui(void);
#endif

void redisplay_prompt(void);

char *gnu_readline (char *s, unsigned int force_readline);

int last_command_is_repeatable=0;
//extern Macro *gCurrentMacro;
extern void  scanPushMacroState(Macro *);

extern int quit_parse;

#ifdef HAVE_SOCKETS
extern void start_server(void);
extern void stop_server(void);
#endif // HAVE_SOCKETS


static Boolean  *s_bSTCEcho = 0;
void EnableSTCEcho(bool bEnable)
{
  *s_bSTCEcho = bEnable;
}

//------------------------------------------------------------------------
// Command Handler - create an interface to the CLI
//------------------------------------------------------------------------
#include "../src/cmd_manager.h"
//class ISimConsole;
class CCliCommandHandler : public ICommandHandler
{
public:
  virtual const char *GetName();
  virtual int Execute(const char * commandline, ISimConsole *out);
  virtual int ExecuteScript(list<string *> &script, ISimConsole *out);
};

// This instantiation will get registered so that code
//  in ../src can get access to this class (and consequently the command line).

static CCliCommandHandler sCliCommandHandler;


//------------------------------------------------------------------------
// LLInput - A class for storing a command line command
// This is private to input.cc
//------------------------------------------------------------------------

class LLInput {
public:

  LLInput();
  LLInput(const char *,Macro *);
  ~LLInput();

  Macro *macro;  // macro generating this text
  char *data;
  LLInput *next_input;
};

//------------------------------------------------------------------------
// LLStack - A class for storing a collection of command line commands.
// This is private to input.cc
//------------------------------------------------------------------------
class LLStack
{
public:
  LLStack();
  ~LLStack();

  void Push();
  void Pop();
  void Append(const char *, Macro *);
  LLInput *GetNext();

  void print();
  int  level() { return msi_StackDepth; }

  LLInput *LLdata;
  LLStack *next_stack;
private:
  static int msi_StackDepth;
};

int LLStack::msi_StackDepth=0;

//------------------------------------------------------------------------
// LLInput - linked list input for commands.

LLInput::LLInput()
  : macro(0),data(0), next_input(0)
{
}

LLInput::LLInput(const char *s,Macro *m)
  : macro(m), next_input(0)
{
  data = strdup(s);
}

LLInput::~LLInput()
{
  if(data)
    free(data);
}

//------------------------------------------------------------------------
// LLStack - linked list stack for commands.

LLStack::LLStack()
  : LLdata(0), next_stack(0)

{
  msi_StackDepth++;
  //  cout << "Stack depth: " << level() << endl;
}

LLStack::~LLStack()
{
  msi_StackDepth--;
}
static LLStack *Stack=0;

//========================================================================
//
// catch_control_c
//
//
void catch_control_c(int sig)
{
#ifdef _WIN32
  CSimulationContext::GetContext()->NotifyUserCanceled();
  if(CSimulationContext::GetContext()->GetActiveCPU()->simulation_mode
    == eSM_RUNNING) {
    // If we get a CTRL->C while processing a command file
    // we should probably stop the command file processing.
    clear_input_buffer();
  }
  ::signal(SIGINT, catch_control_c);
#else
  // JRH - I'll let someone else try the above code under Linux.
  // The readline library appears to call ::signal() itself so
  // the call will not be needed here. I'm guessing that the CTRL->C
  // signal handling in readline did not work under Windows so
  // that is why it was originally ifdef'd out.
  //if(simulation_mode != STOPPED)
  //  {
      cout << "<CTRL C> break\n";
      CSimulationContext::GetContext()->NotifyUserCanceled();
  //  }
  //else {
  //  cout << "caught control c, but it doesn't seem gpsim was simulating\n";
  //  last_command_is_repeatable=0;
  //  redisplay_prompt();

  //}
#endif
}

void initialize_threads(void)
{
#if GLIB_MAJOR_VERSION >= 2
  if( !g_thread_supported() )
  {
    g_thread_init(NULL);
#ifdef HAVE_GUI
    gdk_threads_init();
#endif
  }

#endif
}

void initialize_signals(void)
{
#ifdef _WIN32
  ::signal(SIGINT, catch_control_c);
#else
  static struct sigaction action;

  action.sa_handler = catch_control_c;
  sigemptyset(&action.sa_mask);
  action.sa_flags=0;

  sigaction(SIGINT, &action, 0);
#endif

}
void initialize_CLI()
{
  CCommandManager::GetManager().Register(&sCliCommandHandler);
}
//==============================================================
// initialize_gpsim
//
// Not much initialization is needed now.
//

void initialize_gpsim(void)
{
  s_bSTCEcho = new Boolean("CliTrace", false,
                           "Enable echoing commands from STC files to the console.");


  globalSymbolTable().addSymbol(s_bSTCEcho);

  initialize_CLI();
  if(gUsingThreads())
    initialize_threads();
  initialize_signals();
#ifdef HAVE_SOCKETS
  start_server();
#endif
}


void LLStack::print(void)
{
  if(verbose & 4) {
    LLStack *s = Stack;

    cout << "Current state of input buffer:\n";
    int stack_number=0;
    while (s) {
      LLInput *h = s->LLdata;
      int depth =0;
      while(h) {

        cout << "   " <<stack_number <<':'<<depth << "  "<<  h->data;
        depth++;
        h = h->next_input;
      }
      stack_number++;
      s = s->next_stack;
    }
    cout << "\n ---Leaving dump \n";
  }
}

void LLStack::Push()
{
  LLStack *s = new LLStack();

  s->next_stack = Stack;
  Stack = s;

  print();
}

void LLStack::Pop()
{
  if (Stack ) {
    if(Stack->next_stack != NULL) {
      LLStack *next = Stack->next_stack;
      delete Stack;
      Stack = next;
    }
  }
}

void LLStack::Append(const char *s, Macro *m)
{

  LLInput *d = new LLInput(s,m);

  LLInput *h = LLdata;

  if (h) {

    /* go to the end of the list */
    while(h->next_input)
      h = h->next_input;

    /* add d to the end */

    h->next_input = d;

  } else
    LLdata = d;


}


LLInput *LLStack::GetNext()
{

  if (Stack) {
    if (Stack->LLdata) {
      LLInput *d = Stack->LLdata;

      // remove this item from the list
      if(d)
        Stack->LLdata = d->next_input;

      return d;
    }
    if(Stack->next_stack != NULL) {
      Pop();

      return GetNext();
    }
  }

  return 0;
}

/*******************************************************
 */
void add_string_to_input_buffer(const char *s, Macro *m=0)
{
  if(!Stack)
    Stack = new LLStack();
  Stack->Append(s,m);

}

/*******************************************************
 */
void start_new_input_stream()
{
  if(!Stack)
    Stack = new LLStack();
  else
    Stack->Push();
}

/*******************************************************
 */
void clear_input_buffer(void)
{
  LLInput * pLine;
  if (Stack)
    while((pLine = Stack->GetNext()) != NULL)
      delete pLine;
}

/*******************************************************
 * start_parse
 *
 * This routine will run a string through the command parser
 *this is useful if you want to execute a command but do not
 *wish to go through the readline stuff.
 */
extern int init_parser();

int start_parse(void)
{

  int retval = init_parser();

  if(quit_parse)
    exit_gpsim(0);

  return retval;
}


int parse_string(const char * str)
{
  add_string_to_input_buffer(str);
  int iReturn = start_parse();
  if(iReturn == 1) {
    // If the str was a 'load c x.stc' file and the parsing
    // was aborted we need to remove the remaining
    // strings.
    clear_input_buffer();
  }
  return iReturn;
}

int parse_string_only(const char * str) {
  LLStack *OldStack= Stack;
  Stack = 0;
  int iRet = parse_string(str);
  delete Stack;
  Stack = OldStack;
  return iRet;
}

void process_command_file(const char * file_name, bool bCanChangeDirectory)
{

  FILE *cmd_file;
  char directory[256];
  const char *dir_path_end;

  if(verbose&4)
    cout << __FUNCTION__ <<"()\n";

  dir_path_end = get_dir_delim(file_name);
  if(dir_path_end && bCanChangeDirectory)
  {
      strncpy(directory,file_name,dir_path_end-file_name);
      directory[dir_path_end-file_name]=0;
      printf("directory is \"%s\"\n",directory);
      chdir(directory);
      file_name=dir_path_end+1;
      printf("file_name is \"%s\"\n",file_name);
  }

  cmd_file = fopen(file_name,"r");

  if(cmd_file)
    {

      if(verbose)
          cout << "processing a command file\n";

      start_new_input_stream();
      //Stack.Push();

      char *s;
      char str[256];

      while( (s = fgets(str, 256, cmd_file)) != 0)
      {
          if(str[0] == 0 ||
             str[0] == '\n' ||
             ((str[1] == '\n' && str[0] == '\r'))) {
              // skip the blank lines
              continue;
          }
#ifndef WIN32
          // Let us be compatible with Windows EOLs
          // on Linux.
          int iLast = strlen(str) - 1;
          if(iLast >= 2 && str[iLast] == '\n' && str[iLast-1] == '\r' ) {
            // Windows type EOL
            // convert <CR><LF> to <LF>
            str[iLast] = '\000';
            str[iLast-1] = '\n';
          }
#endif
          add_string_to_input_buffer(s);
      }

      fclose(cmd_file);
    }
  else
    {
      cout << "failed to open command file ";
      cout << file_name;
      cout << endl;
      char cw[_MAX_PATH];
      GETCWD(cw, _MAX_PATH);
      cout << "current working directory is ";
      cout << cw;
      cout << endl;

    }

  if(Stack)
    Stack->print();

}

/*********************************************
  Function: gpsim_open()

  Returns:  1   - success
            0   - failure
*/

int gpsim_open(Processor *cpu, const char *pFileName,
               const char * pProcessorType, const char *pProcessorName)
{
  if(!pFileName)
    return 0;

  if (verbose)
    printf (" gpsim_open file:%s proc name:%s\n", pFileName, (pProcessorName ? pProcessorName : "nil"));
  // Check for the command file, file extension.
  if(IsFileExtension(pFileName,"stc") || IsFileExtension(pFileName,"STC")) {
    process_command_file(pFileName,true);
    // A stc file could have any sequence of commands.
    // Just ignore the return value of parse_string().
    parse_string("\n");
    return 1;
  } else {

    // Assume a Program file
    return
      CSimulationContext::GetContext()->LoadProgram(pFileName, pProcessorType, 0, pProcessorName);

  }
  return 0;

}


//*********************************************
// gpsim_read
//
//  This function is called from the parser. It will either read
// a line of text from a command file or from stdin -- depending
// on the current source for data. Once a string is obtained, it
// is copied into a buffer that the parser has passed to us.

int
gpsim_read (char *buf, unsigned max_size)
{


  LLInput *d = Stack ? Stack->GetNext() : 0;

  if (!d || !d->data) {
  if(verbose&4)
    cout <<"gpsim_read -- no more data\n";
    return 0;
  }

  scanPushMacroState(d->macro);
  //gCurrentMacro = d->macro;

  char *cPstr = d->data;
  unsigned int count = strlen(cPstr);
  count = (count < max_size) ? count : max_size;

  strncpy(buf, cPstr, count);
  buf[count] = 0;
  SetLastFullCommand(buf);
  if(*s_bSTCEcho)
    cout << cPstr;

  if(verbose&4) {
    cout <<"gpsim_read returning " << count << ":" << cPstr << endl;
    if (d->macro) {
      cout << "   and it's a macro named:" << d->macro->name() << endl;
    }
  }
  delete (d);

  return count;

}

void have_line(char *);


//**************************************************
void cli_main(void)
{
  do
    {
#ifdef HAVE_READLINE
#ifdef WIN32
      if(channel->is_readable) {
        // Channel is not readable when a Windows
        // stdin is redirected.
        rl_callback_read_char ();
      }
      else {
          char line[256];

          fgets(line, sizeof(line),stdin);
          have_line(line);
      }
#else
      rl_callback_read_char ();
#endif
#else
      char line[256];

      fgets(line, sizeof(line),stdin);
      have_line(line);

#endif
    }
  while(!quit_parse);

}


/* **************************************************************** */
/*                                                                  */
/*                  Interface to Readline Completion                */
/*                                                                  */
/* **************************************************************** */

/* Generator function for command completion.  STATE lets us know whether
   to start from scratch; without any state (i.e. STATE == 0), then we
   start at the top of the list. */
char *
command_generator (const char *text, int state)
{
  //static char *empty="";
  static int i = 0;
  const int cMaxStringLen = 64;

  /* If this is a new word to complete, initialize now.  */
  if (state == 0)
    i = 0;

  /* Return the next name which partially matches from the command list. */
  while( i<number_of_commands)
    {

      if(strstr(command_list[i]->name(), text) == command_list[i]->name())
        return(g_strndup(command_list[i++]->name(), cMaxStringLen));

      i++;
    }

  // If no names matched, and this is the first item on a line (i.e. state==0)
  // then return a copy of the input text. (Note, it was emperically determined
  // that 'something' must be returned if there are no matches at all -
  // otherwise readline crashes on windows.)
#ifdef _WIN32
  if(state == 0)
    return g_strndup(text,cMaxStringLen);
#endif

  return 0;
}

/* Attempt to complete on the contents of TEXT.  START and END show the
   region of TEXT that contains the word to complete.  We can use the
   entire line in case we want to do some simple parsing.  Return the
   array of matches, or NULL if there aren't any. */
char **
gpsim_completion (const char *text, int start, int end)
{
  char **matches;
  matches = 0;

#ifdef HAVE_READLINE
  /* If this word is at the start of the line, then it is a command
     to complete.  Otherwise it is the name of a file in the current
     directory. */
  if (start == 0)
#ifdef HAVE_NSCLEAN_READLINE
    matches = rl_completion_matches (text, command_generator);
#else
    matches = completion_matches ((char *)text, (CPFunction *)command_generator);
#endif
#endif

#if defined(_WIN32) || defined(WIN32)
        if (start) {
                char *empty = strdup("");
                matches = (char **) malloc(2 * (sizeof(&empty)));
                matches[0] = empty;
                matches[1] = 0;
        }
#endif

  return (matches);
}


//#ifdef HAVE_GUI

//============================================================================
//
// keypressed().
//
// When the user presses a key, this function will get called.
// If a simulation is running, then we'll pass the key to
// the stimulus engine.

static gboolean keypressed (GIOChannel *source, GIOCondition condition, gpointer data)
{

#ifdef HAVE_READLINE
    rl_callback_read_char ();
#endif

  return TRUE;
}
//#endif

//============================================================================
//
// have_line
// When <cr> is pressed at the command line, the text string on the command
// is copied into a buffer and passed to the command parser.
//

void have_line(char *s)
{

  if(!s)
    return;

  static char last_line[256] = {0};

  if(strlen(s) == 0) {
    if(*last_line && last_command_is_repeatable)
      add_string_to_input_buffer(last_line);

  } else {
    // save a copy in the history buffer:
    strncpy(last_line,s,256);
#ifdef HAVE_READLINE
    add_history (s);
#endif
    add_string_to_input_buffer(s);
  }

  add_string_to_input_buffer("\n");

  start_parse();

#if defined (_WIN32) || defined (HAS_RL_FREE)
  rl_free(s);
#else
  free(s);
#endif
}

/**********************************************************************
 **/
void exit_cli(void)
{
  if(get_use_icd())
    icd_disconnect();

#ifdef HAVE_GUI
  quit_gui();
#endif

#ifdef HAVE_READLINE
  rl_callback_handler_remove();
#ifdef HAVE_GUI
  g_io_channel_unref(channel);
#endif

#endif

  CSimulationContext::GetContext()->GetContext()->Clear();
#ifdef HAVE_SOCKETS
  stop_server();
#endif
  globalSymbolTable().deleteSymbol("CliTrace");
  cout << "Exiting gpsim\n";
  simulation_cleanup();

}

/* redisplay_prompt will redisplay the current data in the readline buffer.
   This function is used to restore a prompt that's been obliterated by diagnostic
   data.
*/

void redisplay_prompt(void)
{
#ifdef HAVE_READLINE
  rl_forced_update_display();
#endif
}

#if defined HAVE_READLINE && defined HAVE_GUI
static int gpsim_rl_getc(FILE *in)
{
  gchar buf[6];
  gsize bytes_read;

#if GLIB_MAJOR_VERSION >= 2
  g_io_channel_read_chars(channel, buf, 1, &bytes_read, NULL);
#else
  g_io_channel_read(channel, buf, 1, &bytes_read);
#endif
  return buf[0];
  // JRH, 7-1-2005 - I tried this but am not comfortable that it
  // is working as I intended.
  // return bytes_read == 0 ? EOF : (buf[0] & 0x000000ff);
}
#endif

gint g_iWatchSourceID = 0;
/* Tell the GNU Readline library how to complete.  We want to try to complete
   on command names if this is the first word in the line, or on filenames
   if not. */
void initialize_readline (void)
{
  const char *gpsim_prompt="gpsim> ";
  const char *gpsim_cli_prompt="**gpsim> ";

  const char *prompt = get_interface().bUsingGUI() ? gpsim_prompt : gpsim_cli_prompt;

#ifdef HAVE_READLINE
  // Lets us have a gpsim section to .inputrc
  // JRH - not tested
//  rl_terminal_name = "gpsim";

#ifdef _WIN32
  /* set console to raw mode */
  win32_fd_to_raw(fileno(stdin));
#endif

#if defined HAVE_READLINE && defined HAVE_GUI
  rl_getc_function = gpsim_rl_getc;
  channel = g_io_channel_unix_new (fileno(stdin));
#endif

#ifdef _WIN32
#if GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 6)
  /* set console to raw mode */
  win32_set_is_readable(channel);
#endif

  // The channel is not readable if it is a redirected
  // standard input, in which case we will not be getting
  // any keypress events.
  if(channel->is_readable) {
    g_iWatchSourceID = g_io_add_watch (channel, G_IO_IN, keypressed, NULL);
  }
#else
  #if defined HAVE_READLINE && defined HAVE_GUI
    g_iWatchSourceID = g_io_add_watch (channel, G_IO_IN, keypressed, NULL);
  #endif
#endif
  rl_callback_handler_install (prompt, have_line);

  /* Tell the completer that we want a crack first. */
  rl_attempted_completion_function = gpsim_completion;

#endif //HAVE_READLINE
}

#if defined(HAVE_READLINE) && defined(HAVE_PERL)
// JRH - An experiment
void EnableKeypressHook(bool bEnable) {
  if(bEnable) {
    g_iWatchSourceID = g_io_add_watch (channel, G_IO_IN, keypressed, NULL);
  }
  else {
//    g_source_remove_by_funcs_user_data(keypressed, NULL);
    bEnable = g_source_remove(g_iWatchSourceID);
    g_io_channel_unref(channel);
  }
}
#endif

//------------------------------------------------------------------------
// CLI command handler
//
// The command handler is an interface that gets 'registered' with gpsim.
// This means that clients interested in gpsim's cli can look up this
// registered handler and get access to the command line. This is primarily
// used by symbol files that embed gpsim scripts. See src/modules.cc.
//

const char *CCliCommandHandler::GetName()
{
  return "gpsimCLI";
}

int CCliCommandHandler::Execute(const char * commandline, ISimConsole *out)
{
  add_string_to_input_buffer("\n");
  start_new_input_stream();
  parse_string_only(commandline);
  add_string_to_input_buffer("\n");
  return 1;
}
int CCliCommandHandler::ExecuteScript(list<string *> &script, ISimConsole *out)
{
  if (verbose & 4)
    cout << "GCLICommandHandler::Execute Script:" << endl;

  if (script.size() == 0)
    return CMD_ERR_OK;

  // We need to execute the script now. There may be other commands
  // currently pending, so a new command stream is created and the
  // commands that are in this script are placed there. The current
  // command stream is temporarily disabled and then re-enabled at
  // the end of this function.

  LLStack *saveStack = Stack;
  Stack = 0;

  start_new_input_stream();
  add_string_to_input_buffer("\n");

  list <string *> :: iterator command_iterator;

  for (command_iterator = script.begin();
       command_iterator != script.end();
       ++command_iterator) {

    string *cmd = *command_iterator;
    add_string_to_input_buffer((char *) cmd->c_str());
  }

  // Start parsing the script that we just placed into the command stream
  start_parse();
  delete Stack;

  // Restore the original command stream.
  Stack = saveStack;

  return CMD_ERR_OK;
}