www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/07/16:31:36

Date: Tue, 7 May 1996 16:14:50 -0400 (EDT)
From: Justin Ward <justin AT yoss DOT canweb DOT net>
To: djgpp AT delorie DOT com
Subject: totally crazed output
Message-ID: <Pine.BSF.3.91.960507154545.1476A-100000@yoss.canweb.net>
MIME-Version: 1.0

okay I've been getting some REALLY bizarre results from printf.

I have a function which reads a string from the console using getch. It 
takes care of 'special' keys (^C, enter, ^H, etc), and then uses putch
to echo the char to the screen, again handling special keys accordingly.
It then returns the string, as well as putting it into the buffer it takes
as one of its arguments. Now this is all fine well and good. Here comes
the problem though.. printing the string.

I call printf("input: %s\n", s); and things go haywire. It prints "input:
" and then the string, and then the I and the N in input get screwed. If
the string returned has a length of zero (ie, s[0] == NULL), the i becomes
one of the characters used to make a "box" on an ascii text display, and
the n becomes a heart character..

Okay. So then I test it entering ONE character in the string. This time 
the exact same thing happens, only the n stays normal. great.

Next I enter a nice long string.. everything works FINE.

Now I'm thoroughly puzzled. I run another test, this time just using ^G,
which pauses output while it sounds the bell. Get this. It prints
"input: " on the screen, sounds the bell, and THEN the i becomes the box 
char.

Screwy enough for you? wait, there's more. I run the test AGAIN, hit F1 
twice (which, btw, getch doesn't seem to get. any ideas on this?), then 
hit enter. Now the string is empty, but it prints input normally and it 
STAYS normal.

The code below is rather lengthy, I know, but what can I say.. Anyway if 
anybody has ANY ideas on what the hell is going on here, I'm dying to 
figure it out.

Justin
justin AT yoss DOT canweb DOT net
ward AT stealth DOT net

---begin nsh.cc---

/* nsh.c
 * main function and other crap.
 *
 * insert GNU crap here.
 */

#include "nsh.h"

#include <signal.h>
#include <stdio.h>
#include <conio.h>

/*
int GetX()
{
int x,y;

ScreenGetCursor(&x, &y);
return x;
}

int GetY()
{
int x,y;

ScreenGetCursor(&x, &y);
return y;
}
*/

void sig_int(int whoknows)
{
// don't do anything.
}


main(int argc, char *argv[])
{
char input[120];

clrscr();

/* readini(); */
signal(SIGINT, sig_int);

getstring(input, 120);
printf("\ninput: %s\n\n", input);

}

--end nsh.cc--

--begin getstr.cc--

#include "nsh.h"

#include <conio.h>
#include <pc.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

// backspaces on the console one. putch('\b') just moves cursor back, does
// not remove char from screen.
void backspace()
{
putch('\b');
putch(' ');
putch('\b'); // move back again since space moved cursor forwards one.
}

// prints a control char the way it was meant to be printed.
void putctrl(char c)
{
putch('^');

if (c==0)
    putch('@');

    putch(c+64);
} // end function putctrl.

char *getstring(char *s, int len)
{
int index=0;
Bool special=FALSE;

for (;index<len;index++) {
    if (index<0) // some possible fuckups with backspace, ^U, etc...
        index=0;

    s[index]=getch();
    special=FALSE; // reset it.

// special character treatments. i didn't use a switch here because for a
// few of them we need to use some loop variables.

    if (s[index]==NEWLINE) {
        s[index]==NULL;
        return s; // the calling function can print the \n
    }

    if (s[index]==BACKSPC) {
        Bool shouldbackspace=TRUE;
        index-=2; // next read char will replace removed. (looping 
increments)
                  // which is why we take off two, not just one.

        if (index==-2) { // if index was 0 (no keys yet) and ^H entered...
            index=-1; // see above about the -2
            putch(BELL);
            shouldbackspace=FALSE;
        }
        if (shouldbackspace) // will be true if ^H was NOT 1st char entered..
             backspace();

        special=TRUE;
    } // end if backspace

    if (s[index]==CTRLU) {
        int i=0;
        while (i<index) {
            if (iscntrl(s[index-i])) {
                backspace();
            }

            i++;
        } // end while.
    special = TRUE;
    index=-1; // don't forget when it loops that index is incremented.
    bzero(s, len * sizeof(char));
    } // end if control-U

    if (s[index]==CTRLC) {
        putctrl(s[index]);
        return (char *)NULL;
    }

    if (s[index]==CTRLV) {
        char newchar;

        newchar=getch();
        s[index]=newchar;

        if (iscntrl(newchar)) {
            putctrl(newchar);
        }
        else
            putch(newchar);
    special = TRUE;
    } // end if CTRLV

        if (s[index]==NULL) {   // ^@ is a null, but it also puts a ^C
            s[index]=NULL; // will fuckup everything, but that's what you
                           // get for inserting a null in a string.
            getch(); // like i said, it inserts a ^C into the key buffer.
                     // this throws away the ^C.
            special = TRUE;
        } // end if null.

    if ((special == FALSE) && (iscntrl(s[index]))) {
        putctrl(s[index]);
        special=TRUE;
    } // end if special...

if (special == FALSE)
    putch(s[index]);

} // end for loop.
return s; // this will only happen if the max num of chars are put in.
}

--end getstr.cc--

oh yeah and nsh.h, bothing special here but I might as well include it. 
what's 1K by now.

--begin nsh.h--
/* nsh.h
 * header files defining pretty basic stuff.
 *
 * insert GNU stuff here.
 */

#ifndef _NSH_H

#define TRUE 1     /* Pretty standard stuff here i think. */
#define FALSE 0
#define ESC 27
#define CTRLC 3
#define CTRLU 21
#define CTRLV 22
#define NEWLINE 13
#define CTRLH 8
#define CTRLG 7
#define BACKSPC CTRLH
#define BELL CTRLG

typedef short Bool; /* makes reading easier. we really just need one bit. */

// getstr.cc protos

void backspace();
void putctrl(char);

#endif /* _NSH_H */

--end nsh.h---

You're still actually reading this? shit you're bored. :)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019