| [2945] | 1 | #include "sopnamsp.h"
 | 
|---|
 | 2 | #include "piacmdrdr.h"
 | 
|---|
 | 3 | #include <iostream> 
 | 
|---|
 | 4 | #include <unistd.h> 
 | 
|---|
 | 5 | #include <stdio.h> 
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | #ifdef WITH_GNUREADLINE
 | 
|---|
 | 8 | #include "readline/readline.h"
 | 
|---|
 | 9 | #include "readline/history.h"
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | PIACmdReader::PIACmdReader(PIStdImgApp* imgapp)
 | 
|---|
 | 12 | {
 | 
|---|
 | 13 |   cout << " PIACmdReader::PIACmdReader() Using GNU readline library " << endl;
 | 
|---|
 | 14 |   if (imgapp) _imgapp = imgapp;
 | 
|---|
 | 15 |   else throw NullPtrError("PIACmdReader::PIACmdReader(NULL) ! ");
 | 
|---|
 | 16 | }
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | void PIACmdReader::run()
 | 
|---|
 | 19 | {
 | 
|---|
 | 20 |   char * line = NULL;
 | 
|---|
 | 21 |   bool fgok = true;
 | 
|---|
 | 22 |   char prompt[128];
 | 
|---|
 | 23 |   int nc = 0;
 | 
|---|
 | 24 |   while (fgok) {
 | 
|---|
 | 25 |     usleep(100000);
 | 
|---|
 | 26 |     sprintf(prompt,"piapp[%d] ", nc);
 | 
|---|
 | 27 |     line = readline(prompt);
 | 
|---|
 | 28 |     if (!line) continue;
 | 
|---|
 | 29 |     else if (line[0] != '\0') {
 | 
|---|
 | 30 |       nc++;
 | 
|---|
 | 31 |       if (strcmp(line,"history") == 0) {
 | 
|---|
 | 32 |         cout << " ===> Command history ... " << endl;
 | 
|---|
 | 33 |         HIST_ENTRY **list;
 | 
|---|
 | 34 |         int i;
 | 
|---|
 | 35 |         list = history_list ();
 | 
|---|
 | 36 |         if (list)
 | 
|---|
 | 37 |           for (int i = 0; list[i]; i++)
 | 
|---|
 | 38 |             cout << i << "- " << list[i]->line << endl;
 | 
|---|
 | 39 |       }       
 | 
|---|
 | 40 |       else { 
 | 
|---|
 | 41 |         printf("[%d] %s\n", nc-1, line);
 | 
|---|
 | 42 |         string s = line;
 | 
|---|
 | 43 |         _imgapp->CmdInterpreter()->AddInputLine(s);
 | 
|---|
 | 44 |         add_history (line);
 | 
|---|
 | 45 |       }
 | 
|---|
 | 46 |     }
 | 
|---|
 | 47 |     free(line); 
 | 
|---|
 | 48 |   }
 | 
|---|
 | 49 | }
 | 
|---|
 | 50 | #else
 | 
|---|
 | 51 | //  ------ SANS GNU readline --------
 | 
|---|
 | 52 | PIACmdReader::PIACmdReader(PIStdImgApp* imgapp)
 | 
|---|
 | 53 | {
 | 
|---|
 | 54 |   cout << " PIACmdReader::PIACmdReader() Using fgets() - without GNU readline " << endl;
 | 
|---|
 | 55 |   if (imgapp) _imgapp = imgapp;
 | 
|---|
 | 56 |   else throw NullPtrError("PIACmdReader::PIACmdReader(NULL) ! ");
 | 
|---|
 | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | void PIACmdReader::run()
 | 
|---|
 | 60 | {
 | 
|---|
 | 61 |   char line[1024];
 | 
|---|
 | 62 |   bool fgok = true;
 | 
|---|
 | 63 |   int nc = 0;
 | 
|---|
 | 64 |   while (fgok) {
 | 
|---|
 | 65 |     usleep(100000);
 | 
|---|
 | 66 |     printf("piapp[%d] ", nc);
 | 
|---|
 | 67 |     line[0] = '\0';
 | 
|---|
 | 68 |     char* rs = fgets(line, 1024, stdin);
 | 
|---|
 | 69 |     if (rs == NULL) continue;
 | 
|---|
 | 70 |     int l = strlen(line);
 | 
|---|
 | 71 |     if (l < 1)  continue; 
 | 
|---|
 | 72 |     if ( (line[l-1] == '\n') || (line[l-1] == '\r') )  line[l-1] = '\0';
 | 
|---|
 | 73 |     if (line[0] != '\0') {
 | 
|---|
 | 74 |       printf("[%d] %s\n", nc, line);
 | 
|---|
 | 75 |       nc++;
 | 
|---|
 | 76 |       string s = line;
 | 
|---|
 | 77 |       _imgapp->CmdInterpreter()->AddInputLine(s);
 | 
|---|
 | 78 |     }
 | 
|---|
 | 79 |   }
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | #endif
 | 
|---|
 | 83 | 
 | 
|---|