OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [monitor/] [monitor/] [common/] [getline.c] - Blame information for rev 182

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 59 hellwig
/*
2
 * getline.c -- line input
3
 */
4
 
5
 
6
#include "common.h"
7
#include "stdarg.h"
8
#include "romlib.h"
9
#include "getline.h"
10
 
11
 
12
#define MAX_HISTORY     20
13
 
14
 
15
static char history[MAX_HISTORY][80];
16
static int historyIndex;  /* next line to be written */
17
 
18
 
19
/*
20
 * Get a line from the console.
21
 */
22
char *getLine(char *prompt) {
23
  static char line[80];
24
  int index;
25
  int historyPointer;
26
  char c;
27
  int i;
28
 
29
  printf(prompt);
30
  index = 0;
31
  historyPointer = historyIndex;
32
  while (1) {
33
    c = getchar();
34
    switch (c) {
35
      case '\r':
36
        putchar('\n');
37
        line[index] = '\0';
38
        return line;
39
      case '\b':
40
      case 0x7F:
41
        if (index == 0) {
42
          break;
43
        }
44
        putchar('\b');
45
        putchar(' ');
46
        putchar('\b');
47
        index--;
48
        break;
49
      case 'P' & ~0x40:
50
        if (historyPointer == historyIndex) {
51
          line[index] = '\0';
52
          strcpy(history[historyIndex], line);
53
        }
54
        i = historyPointer - 1;
55
        if (i == -1) {
56
          i = MAX_HISTORY - 1;
57
        }
58
        if (i == historyIndex) {
59
          putchar('\a');
60
          break;
61
        }
62
        if (history[i][0] == '\0') {
63
          putchar('\a');
64
          break;
65
        }
66
        historyPointer = i;
67
        strcpy(line, history[historyPointer]);
68
        printf("\r");
69
        for (i = 0; i < 79; i++) {
70
          printf(" ");
71
        }
72
        printf("\r");
73
        printf(prompt);
74
        printf(line);
75
        index = strlen(line);
76
        break;
77
      case 'N' & ~0x40:
78
        if (historyPointer == historyIndex) {
79
          putchar('\a');
80
          break;
81
        }
82
        i = historyPointer + 1;
83
        if (i == MAX_HISTORY) {
84
          i = 0;
85
        }
86
        historyPointer = i;
87
        strcpy(line, history[historyPointer]);
88
        printf("\r");
89
        for (i = 0; i < 79; i++) {
90
          printf(" ");
91
        }
92
        printf("\r");
93
        printf(prompt);
94
        printf(line);
95
        index = strlen(line);
96
        break;
97
      default:
98
        if (c == '\t') {
99
          c = ' ';
100
        }
101
        if (c < 0x20 || c > 0x7E) {
102
          break;
103
        }
104
        putchar(c);
105
        line[index++] = c;
106
        break;
107
    }
108
  }
109
  /* never reached */
110
  return NULL;
111
}
112
 
113
 
114
/*
115
 * Add a line to the history.
116
 * Don't do this if the line is empty, or if its
117
 * contents exactly match the previous line.
118
 */
119
void addHist(char *line) {
120
  int lastWritten;
121
 
122
  if (*line == '\0') {
123
    return;
124
  }
125
  lastWritten = historyIndex - 1;
126
  if (lastWritten == -1) {
127
    lastWritten = MAX_HISTORY - 1;
128
  }
129
  if (strcmp(history[lastWritten], line) == 0) {
130
    return;
131
  }
132
  strcpy(history[historyIndex], line);
133
  if (++historyIndex == MAX_HISTORY) {
134
    historyIndex = 0;
135
  }
136
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.