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