changes to make cmd compile (not link)
[reactos.git] / reactos / apps / utils / cmd / history.c
1 /*
2 * HISTORY.C - command line history.
3 *
4 *
5 * History:
6 *
7 * 14/01/95 (Tim Norman)
8 * started.
9 *
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
13 *
14 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15 * added config.h include
16 *
17 * 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
18 * Cleanup!
19 * Unicode and redirection safe!
20 */
21
22 #include "config.h"
23
24 #ifdef FEATURE_HISTORY
25
26 #include <windows.h>
27 #include <tchar.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31
32 #define MAXLINES 128
33
34 static INT history_size = 2048; /* make this configurable later */
35
36
37 VOID History (INT dir, LPTSTR commandline)
38 {
39 static LPTSTR history = NULL;
40 static LPTSTR lines[MAXLINES];
41 static INT curline = 0;
42 static INT numlines = 0;
43 static INT maxpos = 0;
44 INT count;
45 INT length;
46
47 if (!history)
48 {
49 history = malloc (history_size * sizeof (TCHAR));
50 lines[0] = history;
51 history[0] = 0;
52 }
53
54 if (dir > 0)
55 {
56 /* next command */
57 if (curline < numlines)
58 {
59 curline++;
60 }
61
62 if (curline == numlines)
63 {
64 commandline[0] = 0;
65 }
66 else
67 {
68 _tcscpy (commandline, lines[curline]);
69 }
70 }
71 else if (dir < 0)
72 {
73 /* prev command */
74 if (curline > 0)
75 {
76 curline--;
77 }
78
79 _tcscpy (commandline, lines[curline]);
80 }
81 else
82 {
83 /* add to history */
84 /* remove oldest string until there's enough room for next one */
85 /* strlen (commandline) must be less than history_size! */
86 while ((maxpos + (INT)_tcslen (commandline) + 1 > history_size) || (numlines >= MAXLINES))
87 {
88 length = _tcslen (lines[0]) + 1;
89
90 for (count = 0; count < maxpos && count + (lines[1] - lines[0]) < history_size; count++)
91 {
92 history[count] = history[count + length];
93 }
94
95 maxpos -= length;
96
97 for (count = 0; count <= numlines && count < MAXLINES; count++)
98 {
99 lines[count] = lines[count + 1] - length;
100 }
101
102 numlines--;
103 #ifdef DEBUG
104 ConOutPrintf (_T("Reduced size: %ld lines\n"), numlines);
105
106 for (count = 0; count < numlines; count++)
107 {
108 ConOutPrintf (_T("%d: %s\n"), count, lines[count]);
109 }
110 #endif
111 }
112
113 _tcscpy (lines[numlines], commandline);
114 numlines++;
115 lines[numlines] = lines[numlines - 1] + _tcslen (commandline) + 1;
116 maxpos += _tcslen (commandline) + 1;
117 /* last line, empty */
118 curline = numlines;
119 }
120
121 return;
122 }
123
124 #endif