Solved

input history?

Posted on 2001-06-08
11
229 Views
Last Modified: 2010-04-15
I've done a program which output strings from the user using the gets() function. Now, as far as I see, with F3 (under DOS) I can print the last entered string, and with the arrows I can edit the string.

Now, I wish to replace the function with something else, which offer me history options, with the up and down arrows, like in doskey, for example, with the minimum changes to the whole program, if any.

Well, I guess TC/BC doesn't has it, so I wish anyone over here to help. Thanks.
0
Comment
Question by:tomercohen
  • 6
  • 3
  • 2
11 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6172275
you'll first need to store however many commands the user enters (you may need to limit this to say 10, etc) then
you can detect the up/down arrow key to traverse that list and output that.

did you want a sample code?
0
 

Author Comment

by:tomercohen
ID: 6173903
Sure I want to. That's emergency. ;-)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6174123
0
 
LVL 5

Accepted Solution

by:
djbusychild earned 200 total points
ID: 6174125
#include <stdlib.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <conio.h>

#define MAX_HISTORY 10
#define MAX_COMMAND_LEN 256

typedef char bool;
#define false 0
#define true 1

int echoCommand(int direction, char* history[],int last_cmd,int num_cmd) {
     if (direction==72) { /* up */
          last_cmd--;
          if (last_cmd<0) { last_cmd=num_cmd-1; }
     } else if (direction==80) { /* down */
          last_cmd++;
          if (last_cmd>(num_cmd-1)) { last_cmd=0; }
     } else { return -1; }

     return last_cmd;
}

int main(int argc,char* argv[])
{
     char *history[MAX_HISTORY]; /* I'm being lazy, you should use a circular queue or something */
     char command[MAX_COMMAND_LEN];
     int last_cmd_index=0;
     int char_buf;
     int num_char=0;
     int num_cmd=0;

     bool command_entered=false;

     memset(command,0,MAX_COMMAND_LEN);
     memset(history,0,MAX_HISTORY*sizeof(char*));

     printf(">> "); /* print cursor */
     while (!_kbhit()) {
               char_buf=_getch();
               switch (char_buf)
               {
                    case 0: /* this is the function key */
                         /* as of now it'll just quite after a function key */
                         
                         break;
                    case 224: /* possibly up,down */
                         last_cmd_index=echoCommand(_getch(),history,last_cmd_index,num_cmd);
                         if (last_cmd_index>=0) {
                              /* wipe out the command line */
                              while (num_char>0) {
                                   printf("\b \b");
                                   num_char--;
                              }
                              num_char=strlen(history[last_cmd_index]);
                              strcpy(command,history[last_cmd_index]);
                              printf("%s",command);
                         } else { last_cmd_index=0; }
                         break;
                    case 8: /* backspace */
                         if (num_char>0) {
                              command[--num_char]='\0';
                              printf("\b \b"); /* back delete back again */
                         }
                         break;
                    case 13: /* newline */
                         if (num_char>0) {
                              num_char=0;
                              if (num_cmd>=MAX_HISTORY) {
                                   num_cmd=0;
                              }
                              if (history[num_cmd]) { /* if there's a command there already, we have to free it */
                                   free(history[num_cmd]);
                                   history[num_cmd]=NULL;
                              }
     
                              history[num_cmd]=(char*)malloc(strlen(command)+1);
                              strcpy(history[num_cmd],command); /* assuming buffer check has already been done by command it self */
                              num_cmd++;
                         }
                         printf("\n>> "); /* print a new cursor */
                         break;
                    default: /* just another key */
                         if ( num_char<(MAX_COMMAND_LEN-1) ) {
                              command[num_char]=char_buf;
                              command[++num_char]='\0'; /* NULL terminate it */
                              printf("%s",&command[num_char-1]); /* just print out the last input */
                         }
                         break;
               }
     }

     printf("\n"); /* end with a carriage return */
}
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6174131
this is a rought, it's not really using a circular queue but just a simple array, so it's not truly circular. I haven't dealt with the situation where you go beyond the history limit and you have to push the others away... Instead I just start overwriting from index 0.

you get the idea, though.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:yabelson
ID: 6198850
as a default,the DOS also has up to 10(if i am not mistaken) commands back.if you press up further,it simply returns you to the last command.this means that if you recieve more hten 10 "up"s you reset your counter,and start all over again.
0
 
LVL 1

Expert Comment

by:yabelson
ID: 6198856
tomer-(fonetic hebrew)tikanes laatar shel rotter,ulai yuchlu laazor lecha sham.yesh sham harbe anashiim shemevinim besi.
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6198871
Hi, did this help at all?
0
 

Author Comment

by:tomercohen
ID: 6200009
Yabelson, I guess you wrong. DOS has no built-in history options at all, except the F3 key, for the last command. This let me use gets() with only one line of history. In other operating system, this is not the same.

Oh, (in fonetic hebrew) - Ani lo ohev et haatar shel rotter. En sham anashim nechmadim. ;-)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6200011
tomercohen, did the code help?
0
 

Author Comment

by:tomercohen
ID: 6203334
djbusychild, not at all, I made it with another way.
Thanks anyway. ;-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now