This deep dive elegantly bridges the gap between daily CLI usage and low-level systems engineering. It transforms mundane terminal behavior into a compelling lesson on historical design and technical precision.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
The `reset` and `clear` unix terminal commands are complex - let's rewrite them (deep-dive)Added:
All right, so I got a fun one for you guys today. Today I'm going to go over the differences between the clear command on your terminal and the reset command on your terminal. This is going to be a deep dive. We are going to go into all the specifics of we're going to talk about where the commands actually come from, what they do specifically.
We're going to go through some source code and we're going to write our own because they are surprisingly complex. I originally got this question from someone on my short form content. I replied to them and kind of gave a quick synopsis of it and a bunch of people in the comments were like, "Hey Dave, can you like go over the specifics of this?
This seems really cool. seems really interesting, but we want to know more.
So, make a long form video about it. And of course, you know, I'm a man of the people. So, I'll be doing that. So, yeah, we will dive into it and go through the specifics today. So, let's do it. All right. So, here we go. I'm logged into a Linux machine. Let's go ahead and show the two commands so you can see what's going on there. So, if I run a command like, let's say, neoetchet. I fill up the screen. We have a bunch of output here. Beautiful command. Looks amazing. If I run clear, what is going to happen? Clears the screen. That slash up cuz I just accidentally hit it. But if I run anything, if I run neofetch, if I put like ls, I don't know, temp down here.
There we go. I can then run clear and it clears the screen. Does it get rid of the scroll back in this example? Yes.
I'm scrolling my mouse up and down.
There is nothing there. So, that's a little bit different from if I were to run L. It looks like it clears the screen. Notice the scroll bar showed up.
I can still scroll back up. When I run the clear command, it does get rid of the scroll back. That's a little foreshadowing because it doesn't do that on every operating system. But for here it does the opposite one or not the opposite one. The other one that I want to talk about is reset. So what happens if I run reset? Look at that. It cleared the screen. I can still scroll up though. So that's super interesting. So what exactly did it do? Cuz it seems like they did the same thing except one clears the scroll back and the other one doesn't. Let me show you a slightly different situation. I'm going to show you the simple example first, then I'll show you the more complex example. If we were to use a simple example, we can output something like this to the screen. the escape character. Uh, three, I think. Space Q. This is super weird.
This is super strange if you've never seen it. I'm going to run this and it's going to change my cursor. My cursor is now an underline. Remember, it was a block before. It's now an underline. If I switch this back, I think zero will bring give me the block. So, it's a nice little white block. If I go back to three, we have an underline here. So, super weird. Like, you just kind of get used to it. It's like, oh, it's actually kind of cute. There's just like an underline there. That's awesome. If I clear the screen, scroll back's gone. I can't scroll anymore, but the underline is still present. By contrast, if I were to run reset, take a look at this. We're back to the block cursor. The screen's still there. We can scroll back up there, but we have the block cursor. So, clear gets rid of what is on the screen.
Reset resets your terminal back to kind of like factory or sane settings. So, slightly different, very similar, but still slightly different. There's another thing I can do to show this.
I've showed this in a previous uh video.
Thank you bad cop for this website curl- capital l hot dogstand.
llol. Get ready. It's a little bit of a warning here. It's going to be ugly.
Look at this. Horrendous. That's awful.
My terminal looks like this. Let's go ahead and clear the screen. Nope. It's still red. It's still gross. It's still terrible. Let's reset it. There we go.
Beautiful. Gorgeous. Perfect. So, reset is for when your terminal is messed up.
That's a pretty good way of thinking about it. Clear is to get rid of what's on screen. Reset is when your terminal is really, really messed up. Also, here's a fun extra thing for you guys.
You can also curls.sh, which is my website. So, hey, look, my website shows up here. That's beautiful. Slash theme.
Okay, if you curl this, this will apply my theme and fix your coloring if you don't like the current colors you have.
So, if you curled hot dog stand. I think on hers you have to follow redirects, and you're like, "Oh, wow. This is horrendous. This is awful. Go ahead and curl yap.sh/ theme. This applies my theme. You get all of my colors on.
Super useful if you're on someone else's terminal and you're like, I just want like Dave's theme. It's my theme. You get my colors, but you know, hey, it looks pretty good. So, just a fun little extra I thought I'd throw in there. You may have seen it, may not have seen it.
The other way of showing this off is uh there's an STY command. This will come in handy later, but we can do sty echo.
This won't do anything. This turns on echo. So, when I'm typing to the screen, it shows up. When I backspace, it goes away. I can do sty minus echo. This will turn off the terminal's echo. Now, be careful because like I said, the terminal is no longer echoing characters. So, if I type something like you, I typed it in. You just have to trust me. I'm going to hit enter. There we go.
It showed up. You might have gotten yourself in a situation where you're like, "Oh my god, my commands aren't showing up. Let me cd to temp." Okay, that worked. Let me ls. Okay, that worked. Let me go back to my home directory. That worked. But I'm not seeing any of the commands show up.
Reset will get it back. Now, echo has been turned back on. There is also sty sane. This will apply some sane defaults to your terminal. So that's another way to think about it. You can kind of think of reset as being like a function that calls styane. And even on some machines, it's also like calling clear. So we could do this. We could make our own little function here on the terminal.
We'll call it, I don't know, Dave's reset. So here we go. We have the Dave reset function with stdy sane and clear.
If we were to get oursel into a weird situation like no echo or whatever um or actually just minus echo. There we go. I can run Dave reset. This is a terrible example. You guys can't see it. It's not echoing the screen. I can run that and it resets the whole thing. But that's a really bad example because you couldn't see what I typed. So maybe I'll cut this out of the video. So the next obvious question is where do these commands come from? Cuz you might think like oh yeah, I mean they ship with like Linux, they're on Mac, they're on BSD, they're everywhere. They must be like pix commands or something. They're actually not. This is uh this is a little bit surprising to people but reset and clear both come from the same package. You know what package that is? I can show you. Let's do a man on clear. Look at this. We have our clear command here.
Clear-capv will show the version number.
Let's go ahead and search for capv.
Clear-cap report the version of encurs associated with the program and exits with successful exit status. So clear-cap, we get end curses. Guess what? I'm going to spoiler this for you. Reset. Same thing. These come from the end curses package. Encurs is a really cool way of like controlling the terminal. People use it to like build two terminal user interfaces, things like that. These are both from the end curses package. If I do a man on reset, you can see that it actually it's T set and reset show up here. So they have very very similar usages. Are they the same program? Well, let's see. We'll get to that. All right.
Get ready for the light mode jump scare in three, two, one. Here you go. This is the end curses website. We can go here and we can actually pull down the source code. I figure this will be useful here.
So, if we go here, we can see like there's a whole synopsis. Let's go to the stable release page. If we scroll down a little bit, the Encurs distribution is available here at the end curses homepage. Let's go here. And that just brought us back to the main page. Oh, there's a download button.
Okay, I'm super smart, guys. I know what I'm talking about. Let's download Encurs. Here we go. Stable release source. It's been gzipped and tred.
Let's go ahead and copy that link. Then we'll go back to the terminal. I will open up a different website because that is blindingly bright. So, let's make end curses. We'll do all of our work out of here. and we will w get that. That was the URL I copied to my clipboard. So now we have ncurses.tar.gz.
Let's go ahead and untar that. And now we have an encurs 6.6 directory. So let's go in there. Let's do an ls on it.
And look at this. Here's the source code of it. Very, very simple. This is similar to how we built bash. We have a configure script and that'll create a make file. Then we'll run make and it will build the program. So nothing too fancy there. But before we dive into it, I want to show something I kind of hinted at before. If we take a look at the readme here, let's pop open that readme file. Okay, there's a lot of stuff going on here. Let's go down to where it actually shows the programs.
The utilities are as follows. So these are the programs that end cursor will ship with. We have tick info compar.
That's the one command we were talking about. But notice there's no reset. We have tabs, we have tput, we have tow, and we have t set. But we don't have reset. So what's going on there? If we were to do something like which reset, we can see it's installed on our machine as user bin reset. If we were to ls user bin reset, well, it shows up as light blue on my machine. Do you know what that means? I'll show you what that means. If we stat it, we can see that it is a symbolic link. Reset points to T set. So, that is what's going on there.
If we do a read link on that, or maybe easier for you guys to read would just be ls-l on reset.
Let's type it correctly. this time.
There we go. Reset points to T set. So the T set command is acting like reset when it's invoked with the name reset.
So this is weird if you've never seen it before. Um but I can show you a quick example. If you ever use like busy box and you wonder how does one binary work as all these other programs, this is how it does it. It overrides arg. So I'll show you an example of that cuz this might actually kind of this might drill as home. Let me lower the prompt a little bit by going to my home directory. Let's do a shell. So this is the posic shell minus C. And then I can put commands here. So like echo high.
Okay, that makes sense. I can echo arg0 is dollar0. Now here's the cool thing.
Notice it's in single quotes. That means my current shell isn't expanding it. The shell inside sh minus c is getting a literal dollar and zero. So it's expanding it. That's why ar0 is sh. It's being invoked as sh. That's exactly what's going on there. We can override that. You ready for this? This is gnarly. We can run exec this whole thing. And if we run this whole thing, my terminal will close because my current terminal will become this. So we have to put it in a subshell. I hope I'm not losing you guys. Just trust me. I'm a wizard. I know what I'm doing here. We wrap this whole thing in a subshell. We can use exact to call it. And exec takes a minus a argument. So we can put foo here and we can say arg0 is foo. So even though shell is run, shell is run with the first argument of fu. So we can change the argument that a program is run with. Do you see why this is useful?
Let's go ahead and run T set. So T set did nothing. T set on its own did nothing. Nothing visible that we can see. Let's go ahead and do that same thing. Let's wrap it in a subshell.
Let's exec T set and close like this.
This should do the same thing. It should just run it. Okay, no trickery there.
Let's change arg0 to be reset. So we're going to run the T set command with the arg0 of reset and it resets our terminal. So the T set command, I know I'm saying the same word over and over again. It's probably really confusing. T set command looks at R0 to figure out how it's supposed to run. So yes, reset does come from the encursus package. It just doesn't have its own binary. It hijacks the T set binary. So let's go ahead and compile this so we can have our own set of programs. So we can do a nice little configure on here. Uh we are not in the same directory. There we go. Configure.
And we'll let this go through. This will go through and figure out what our system has, what's supported, what's not supported. This will create a make file.
And then we can run make which will actually compile the programs and the library itself. I don't know how long this will take. Hopefully, it'll be pretty quickly. I'll it'll be pretty quick. I'll run it with multiple jobs at once. So, it should just be able to just kind of crank through it. But the power of movie magic. I'm just going to cut this out anyway. So, we'll see.
Okay. There actually was not much to cut out. I might just leave all that in. It was like right after I said it, it came up. So, that's totally cool. We have all these directories here. Nice, nice, nice. This is the configuration summary.
So, this is what it's determined my system looks like. So, if I were to install it on my system, these are the directories that it would use. So, let's go ahead and run make. I'm going to do J5. will do five jobs at the same time simultaneously. So if it can do things asynchronously, it'll do it that way or in parallel, I guess, is a better way to say it. Concurrently, you should probably know the difference between parallel and concurrent. But, you know, hey, we're here now. This will probably get cut anyways. Cool. It compiled. So, if we ls, I don't know where it's outputed the programs. I don't see like an out directory, a build directory, any of the classic directories I'd be looking for. So, let's do some fd for what's in the command? key set. Uh, Pragues. Look at that. Pragues. I'm guessing all the programs are in Prague.
There we go. There we go. There's all the programs I created. So, we can see them because they're green because they have the executable bit set. So, we have a clear program here. And if we run it, it clears the screen. Hey, that's awesome. We don't have a reset program, but we have T set. So, we can do that same trick. Exact minus a reset of T set. Wrap it in parentheses or else it'll close your current terminal and it reset the screen. How beautiful is that?
We can run clear to get rid of the scroll back and we are running the programs in the current directory. So the ones that we have just freshly compiled. The beautiful thing is we can look at their source code. We could pull up clear C. We could pull up T set C.
And we will do that. Also I just thought about this. We could totally um you know there's that T set command there. We could sim link it. ln minus s. The source is t set and the destination is reset. So this will create a file name reset that points to t set. So if we do that we can see we now have a reset file in there. There. If we ls-l, we can see reset points to t set. So if we run reset in our current directory. There we go. Same thing. So that's how that works. Very, very simple. We can just use sim links. Hard link would also work. Just depends on really how you want to install things, how your system's configured. So before we move on, I do want to make things more confusing. So get ready for this. We have reset, right? And we have clear.
Well, we also have the taput command. We could run tput here. We can just run taput that comes with our machine.
There's a lot of things taput can do. I can do like taput bold echo high. And now it echoed high, but it was bold. I could change bold to set AF1 and it will output as red. So you can output ANIE escape sequences, not necessarily ANI, but it can output color code suitable for your terminal. That's a better way of saying it. So we can output other stuff, too, because taput also takes clear and it will do the same thing that the clear command will do. Subtle variations, but they're pretty much the same. And taput also takes reset, which is very similar to just running the reset command. How similar is it? Don't worry, we're going to dive into the source code. We're going to find out. I just thought it'd be fun to confuse you guys by throwing more commands into the mix. All right, so going forward, we're going to first talk about the clear command and then we're going to talk about the reset command. The reason why is clear is significantly simpler than reset. Okay, we're going to start with clear and I'm going to show the clear command on its own and I'm going to show the tput clear, the differences and the similarity, but we're going to start with clear, then we'll go into reset next. So, let's figure that out. First thing we have here obviously is clear.c.
Here's the C code for the clear command.
Okay, so we have some stuff going on here. Scroll down a little bit and I'm just want to point this out. Include clearcomand.h.
So we have clear command.h and you can see that clear command gets called down here. Just keep track of that. Just remember that that we had something there called clearcomand.h and we run clear underbar command inside the clear command. Let's go ahead and open up tput.
That's the compile binary. Let's open up tput.c. All right. Scroll down a little bit and just take note. Look at this.
Clear command.h. H and if the name of the program is clear, then we run clear command. So does that tell you what's going on here? Tput clear and the clear command itself are pretty much identical. They just call the same thing under the hood. So you can see that we have clear command here. The only thing they take is the opt x which appears to be clear scroll back if possible. Oh, hey, that was something we noticed that when we run the clear command, it clears the scroll back. But it looks like there's a -x argument. That's neat. So, if we run clear minus x, we should still have the scroll back. Okay, that's cool.
I actually didn't even know it was a thing. So, that's like the equivalent of pressing control L on my terminal. But if we run clear, then it will attempt to clear the scroll back, in which case it did successfully do that. Neat. You learned something new by looking through the source code. So, yeah, we have this beautiful thing here called clear underbar command, which was defined in clear command.h. So, let's pull that up.
Here's the header file, and all it does is define an extern int of clear command. That means it's defined somewhere else. That's all it means.
It's gonna be somewhere else. So, we're gonna have to find out where this is actually defined. Luckily, I've looked at the source code before. There's clear command h. Guess what? There's clear command. C. There it is. So, here's where it's implemented. So, the clear command and the tput command both run this. When it comes time to actually go, they run this function right here.
That's it. Oh, it looks like they call the optx legacy. I wonder if you're not actually supposed to use that. M how fun.
So there's a lot of stuff going on here.
Remember this is the end curses library.
So things get a little bit fancy. End curses does a lot of stuff for you.
That's a good thing. They've taken very they've taken a lot of care to make sure this stuff works right. Okay. We are going to go through and completely create our own ridiculous version of this and we're going to get 90% of the way there. I'll cover more of that later, but just warning you that like there's going to be a lot here cuz this actually does things the right way, which is super nice. But if you look at this, you can kind of see what's going on here where we have the escape character and then bracket 2 J escape character 3J and then we only have 2J here. So there's some antsy escape sequences going on here under certain conditions of course. So this is it. This is how it works. There's a lot of documentation here. There's a lot of stuff that it does. I don't want to dive into all the specifics here because there's a lot here. Okay, encursus does things right.
we are going to do things a little bit bad, but I'll show you what I mean by that. So, we could run the clear command and we could pipe that because it just goes to the screen. We could pipe that into a program. We could write it to a file. We could call this file.ext. We could vim file.ext. And that's what clear wrote. That's all it did. Clear just writes things. So, if I run clear and pipe it to xxd, we'll get a nice hex dump. This is what it wrote. It wrote 1b is the escape sequence. So if you're in bash, this is just like the e like that where I could do like I don't know 31m hello and then do that and we get it in red. So this is the escape character open bracket h escape character 2j and then escape character 3j. If we were to run clear minus x, I feel like the 3J will be gone. Yeah, it takes out the 3J.
So the 3J is the one to clear the scroll back if possible. These are just antsy escape sequences. We can do this oursel.
I could print f back slashe open square bracket h which moves my cursor to the home position. And then I can do back slashe square bracket 2j which I think just clears the entire screen. So if I run this, there we go. Clears. It didn't get rid of the scroll back because you also need the what was it? 3 j to do that. Now the scroll back is gone. So we can just do that ourself. Like clear is a very very simple program. Also for completeness sake we should probably do tput clear and pipe it to xxd. It's the same thing. There's no trickery here because they're both calling the same underlying internal function. So you get the exact same output here. This is what I would expect. There is a little bit more to the story. Remember how we just did print f to do it in raw antsy escape sequences. Not all terminals are antsy.
Most that I've encountered are, but not all are. So if I were to run something like term equals, let's say dumb, like a dumb terminal, and I were to run clear, guess what happens? Nothing. It prints nothing. There is no way to clear the screen on a dumb terminal. So clear does more like it has the whole anchors library. It does a lot more than just output an to the screen like we did with our print solution. It looks at what kind of terminal running and figures out the codes that are right for that terminal. We could also put Xterm here and you have this. We could also use some like old terminals. Um I have to look off screen for this one. I wrote this down. TV52 or is it VT? I think it's VT52.
Look at that. Completely different codes. If I were to just output to the screen, it doesn't do anything. My terminal doesn't know how to interpret those. So, there are other codes you can use. You can also just give it something like blah and that's invalid. It doesn't know what to do. I talked about this earlier, but I want to stop here and talk about it. Encursus does things right. It is 100% complete because of that. The source code, there's a lot of code there, but it's right. It's the right thing to do. If we were to make our own version of these tools, we could get 90% of the way there in like 10% of the time. And that's what we're going to do for this video. We're going to rewrite these tools, but they are not going to be as feature complete as end curses. I want to stress this. Using end cursors, using these tools that come with your computer, these are the right tools to use. Okay? We are just going to rewrite these so we can learn how it works. We're reinventing the wheel because we're going to study the wheel today. That's what's going on. So, let's write our own clear command. Here we go.
Let's back out of this. We have our own directory here. Vim clear.c. We've already done it in bash with a print f.
It was one line. Let's go ahead and make a C program that can do it, too. So, int main. Simple as that. Let's return zero for success. And we have our simple C program. We need to write to the screen that collection of characters. So const car let's say S equals this. We just need to put the clear sequence in there and we need to write to the screen. So then we are going to write to standard out S and then however many bytes S is. So we'll just put dot dot dot for now. We will need the write SIS call. So let's go ahead and pull that in. That is just in the Unix standard header. Very very simple.
So let's go here. Include that. There we go. So now we just need to fill this out. And that part is not hard to fill out because we've already done it with the print off. Remember this? Well, I I wrote it to the screen.
I wrote it to the screen. It cleared the screen. Yeah, duh. I'm an idiot. We're going to grab this and we're going to put this in clear. C. So our version of clear only works for ANC. Okay.
Hard-coded ANC won't support other terminals. There you go. This is hard-coded ANC. So, even though back slash E is two characters, visually, it actually is only one character when it's stored. So, let's go ahead and count these because we need to know how many bytes to actually write. And don't worry, I'll touch on this later, but let's count these. This is one character. 2 3 4 5 6 7 8 9 10 11. There are 11 characters here. We are going to write 11 characters. So we give it the buffer of s right here. And we say 11 characters there. And we're writing it to one. One is the standard out file descriptor. I have gotten yelled at by people before in the past where I have to do like standard out file. No, I think. And then instead of exit zero down here, we can exit success. I don't know exactly where those are defined. So we'll see if this compiles, but I'll make you guys happy in the comments.
Okay. So we have this hard-coded string here. We write it to standard out and then we exit success. That seems simple enough. Let's go ahead and compile that clear. C into our own program called clear. Uh look at that. Exit success is just this is a really useful help message. I really like this. We need the standard library if we want to have access to exit success. So let's go ahead here. Let's go up here. Let's just do this. Stand lib. There we go.
Recompile. There we go. Now we have our clear command. Let's run it. And it clears the screen. Of course it does because it just writes this to standard out. Now remember our clear command is dumb. Okay, if we say term equals dum, our clear command doesn't look at it.
Okay, this is not as feature complete as the actual clear command that comes with our system. So don't rely on that because like well oh I put all low I put lowercase term. Huh. Oh. There we go.
That's what I expected. It actually exited with this with a failure error code. Good. That's what I would expect.
Okay, cool. All right. Now I want to talk about something else and I wrote it this way on purpose to show this off.
But take a look at this. We are hard coding 11 here, right? We only want to write 11 bytes. This is a string literal in C. So this has all 11 bytes stored in memory and it has a 12 byt. The 12th bite is the null bite. That's how we know the string is done. So we want to account for that. And the way we can do that is we can use the size of operator.
If we were to do size of s that will give us 12. That's how many bytes are there. Minus one will give us that 11.
Maybe. Let's go ahead and run this and actually see what happens. Okay, this is going to be fun. This This is This is going to be fun. This is a fun little This is a fun little side quest in C.
You ready for this? Let's go ahead and run clear and pipe it to XXD. Well, wait, hold on. 1 2 3 4 5 6 7. What's going on here? Why are we not getting all of the data that we would expect?
Why are we getting seven here when we should have what was it? 11. Yeah, we're getting seven. So, what's going on here?
If we're getting seven, that means size of is returning eight and we're subtracting one from it, giving us seven. Well, yes, size of is eight in this example because we're giving it a pointer to a string. And pointers on my system, my 64-bit system are eight bytes. So that's what's going on here.
If we want to get the actual size of it, there's something else we need to do. So what do we do here? The size of operator can technically take a string literal.
If we put it here, it can get the size of it. We don't actually want that. And the reason I wrote it like this is because I wanted to show this off. This is a string. So, we should be using a function that can get the length of a string. Because we're in the world of strings, size of is suddenly like we're looking at sizes of like things in memory or pointers. We don't actually want that. We're mix and matching different types. So, we're in the world of strings. Let's go ahead and use functions that are built for strings.
Sterling. It'll take a string, which in C is contiguous bytes of data until you hit a null bite. So, it's character data until you hit a null bite. So, in this case, we can do sterling of s. And this should just work for us. So, let's go ahead and recompile that. We have to pull in Sterlin. Where is that from?
String.h. Yeah, I had a feeling it was going to be somewhere. I knew it was somewhere. Does that count for something? I hit man. That's awesome.
Let's go ahead and include this. Let's go pull in string.h. There we go.
Recompile it. Run. Clear. Pipeaxxd. And there we go. We got all 11 bytes like we did before, but we didn't have to hardcode 11. That's why in the future if we were to like modify this string here and add like a 12 byt or something like a 13 byt like hi this would all just work just fine cuz Sterlin would work.
Now there is something I want to talk about again. We're going to take a side quest and a side quest. So hopefully you guys are ready for this. Sterling is a function. Sterling runs at runtime. Okay size of is an operator that runs during compile time. So size of is really good because when you compile the program it doesn't run size of when the program is running. No no no. runs size of during the compilation. So it just puts like in this like in a proper case it would put like 11 there. It will just put that number there. Sterling is different. It will run at runtime. So technically you might look at this and think oh we have to calculate the length of the string here. That seems kind of silly. Well get ready because it doesn't actually run at runtime. If we were to compile the program with no optimizations it'll run at runtime. But if we optimize the program, the compiler and the optimizer are smart enough to know that Sterlin of a static string literal just gets turned into its actual size. So it will actually just be baked into the program 11. Let's prove that. You ready for this? So we have the clear source code here. We have the call to Sterlin here, which is a, you know, an outside function call. We didn't write that function. We just assume it's going to exist in our C standard library. That's great. Or string.h or whatever. Let's go ahead and compile this program with -ash capital S. So this will output assembly as opposed to actual comp. It won't compile it to like an executable program. And we can give it optimizations of zero. So no optimizations at all. We're going to compile clear. C. And we're going to write it to clear. S. Let's do uh clear zero. S. There we go. So if we look at clears, I'm going to go and pull it up in Vim.
Don't be afraid. I'm sorry. I know this is scary. This is assembly. This is what our program looks like in assembly without any sort of optimizations. So there's a lot of stuff going on here.
You can see like some kind of weirdness, but look at this. We call Sterlin here.
That's what's going on. Our string is stored in readonly data because it's a string literal. That's where they're stored. But we actually call the external Sterland function. So when we actually link this program, that's what will work just like we call write the SIS call down here. Sterling gets called here. So another way to show this real easily is let's just cat uh clear 0.s grap for sterling. There it is because we're calling it. So let's go ahead and recompile this program but with an optimization level of two. You know pretty good optimization. It's nothing super aggressive. Let's make the file name-2.s.
So now we have new source code. Clear 2.
S. Take a look at it. It's slightly shorter. We don't even have to scroll.
The actual source code is smaller. So here's the really interesting thing. If we were to cat clear toss and gp for sterling, it's not there. Sterling is not being called because it was optimized away. It knows we're calling Sterlin of this static string that's defined right here. So there's no point in doing that at runtime. We'll just go ahead and hardcode it to 11. That's it.
That's all that's saying here. We're moving 11 into edx and we're moving one into EDI. One is file descriptor 1 for standard out. 11 is how many bytes we're actually writing. And then this is how we call the writes as call. So we give it the actual address of the string. We give it file descriptor one. We give it bytes 11. And then we call write. So that's how that works. So that's we went all over the place here.
Hopefully this was useful for you guys.
But this program in C where we have this hard code and then we call this function which would normally run at runtime because the optimizer because of the way that the compiled code is optimized, it gets taken out. So don't worry about that. It just gets replaced with the number 11. But the beauty of this is that in our code, we don't have to hardcode it. this will update with our code. That's the beauty of compiled languages like this. Like what you write is not necessarily what gets run. This is a source of a ton of headaches and bugs with compiled languages because what you write is not necessarily what gets run. So, you know, you win some, you lose some. So, here we go. We have our clear command. We can pipe it to xxd and see what it looks like. We can run clear and it clears the screen. That's super good. The only thing we're missing is we don't do any error checking here.
Notice that we return success here no matter what. We don't error check write because write can technically fail. Now you might be thinking, okay Dave, how does write fail? I'll show you that in a second. But let's go ahead and look at the documentation for the right sys call. It returns an ss size t and it returns the number of bytes that have been written. You can actually just search for return. Look at this. On success, the number of bytes written is returned. On error minus one is returned and error no is set to indicate the error. So what we can do here is we can get the number of bytes that were written. So we could say n equals this.
So n is how many were written and then we need to compare this to how many bytes we wanted to write. So how many bytes do we want to write? Well that's pretty simple. We just do an siz t of len and we take the ster len of s. So the length of the string is how many bytes we want to write. So we can go ahead and swap this out because we have that function ran above put len here. So in our specific example we know that the ster len is 11. So we want to write 11 bytes but we have them as variables here. We have len and we have n. len is how many there are and n is how many were written. So what we do here is we can do a nice little check. We can say len equal equal n which is what we want.
If that's true then we will exit success. Otherwise we will exit failure.
There we go. And oh boy don't forget the semicolon. I've been writing a lot of rust lately. So I almost just omitted the semicolon. Let's put that there. And let's actually put a return here. Okay.
We don't have implicit returns. We're not in rust. I have to keep reminding myself here. This is a simple turnary operator. If len is equal to n, then we'll return success. Otherwise, we will return failure. So, let's go ahead and recompile that uh not to assembly. There we go. Let's just compile it to a binary and let's pipe it to xxd. And that looks good. If we run clear and then we look at the exit code of it, we can see that it exited zero. The zero shows up here.
So, you might be thinking, well, when would it ever fail? Because if we were to take clear and write it to dev null, well, it doesn't fail. that was successfully able to write 11 bytes to dev null. There's another device though that we can use. We could write it to devull. So this is a pseudo device that exists. You can open it just fine, but when you actually go to write to it, you will get a failure. It simulates a device that always fails with e no space. So this is super useful. I could write nothing to devull or I could write something to devull which would be just, you know, without the new line and then we get the error. So that might be confusing what I just did right there.
Let's imagine I wrote absolutely nothing to devol. I just wanted to open the device. That works just fine. But if we put anything in here, anything at all, it fails. So the open is successful. The write is fail. It fails with eno space.
So that's what's happening here in this example. We failed to write to that device. So we get an exit code of one as you can see right here. So that is super useful. We could have also done I think this syntax works. This will close standard out. So clear will run without a standard out. So that actually just fails to write to the device. It's not en no space in this case. I think it'll be eb bad f. Yeah. So this is this is another way to simulate a failure. But this is good. If clear fails to run for whatever reason, we actually output a useful message now. So it's a little bit more useful than just blindly assuming we were able to write to the output device. Cool. So time for the fun one.
Let's talk about reset now cuz things are about to get a little bit wild.
Let's go back into the end curses progs directory. We don't have a reset command, but we do have T set where we know reset was created. So, if we scroll down here, guess what's going to happen?
A little bit of a spoiler here, but remember how there was clear command?
There is reset command. Reset underbar command.h. Let's go into tput.c.
Can you guess what's going to be there?
Reset command.h is there. So, both T set, which is the reset command, and TUT have a reset command.h. If we were to do this reset command, we have theh and C.
So, let's look at theh first. There's some stuff going on here. There's a lot more functions created. We have like send it string, print tty, chars, reset, start, reset, flush. A lot of stuff going on there. Let's open up the C file and see what's actually happening here.
There is a lot of stuff that happens here. I don't know if I'll be able to implement all of it, but we'll talk about some of it because I'm still scrolling down. There is so much stuff that happens in here. It's wild.
Underbar start reset start. Cool. Reset start's a really fun function. It sets global variables and just end. So that's going to be incredibly confusing later.
Uh the global variables it's set it looks like is a file point pointer is reset and is init. So we can look for those and then get an idea of what's going on. Let's go over to T set. So this is the actual reset command.
Remember it's named reset eventually.
And let's call reset start. So if same program name if our program is name restart then we will call it with standard error true false otherwise we will call it with standard error false true. All right so these arguments are going to be useful. Let's open up the tput command and let's look for reset start. So look at this. If it's reset if it's the reset program it goes to standard out with true false. Otherwise it goes to standard out with false true.
So if it's the reset program, it's true false and standard out. So let's go look at the arguments again and remind ourselves. So if it's reset, it's true false. That makes sense. So either it's reset or it's a net. So the program can be named reset or the program can be named nit. That's kind of what's going on here. It's going to be in one of these two modes. And the file pointer in the t set command was standard error and in the tut command was standard out. Why is that important? Let's take a look at this. So remember the tut command was standard out. I know it's going to be confusing. Tput reset goes to standard out, meaning we can redirect it to dev null and it doesn't do anything. Or we could pipe it to xxd. Look at that.
That's a lot of garbage. What if we were to take the reset command? Remember, this one goes to standard error and pipe it to xxd. It still goes to the terminal because it goes to standard error. We would have to redirect standard error to go to where standard out goes in our shell if we wanted to see it show up.
So, this one's different than clear.
This is something to keep in mind with clear. Oh wow, there's even an OD character here. Interesting. Something to keep in mind here, something to be aware of. Clear command and Tput clear were exactly the same. They both went to standard out. Reset goes to standard error. Tap to put reset goes to standard out. My logical thinking for this is that the tput command is built to omit things to the screen. So to put commands are probably always going to go to standard out. The reset command is slightly different. Notice that's going to standard error. Is there a reason for that? Well, if we man reset, let's take a look at this. This program initializes terminals first. T set retrieves the current terminal mode settings for your terminal. It does this by successively testing standard air, standard out, standard in, and ultimately devty. So, this command goes in order. Taput just blast it to your standard out. That's why I can redirect taput and it works just fine. I can do this because I'm saying to taput, hey, I want this to the screen. Pipe it to xxt. Reset is not meant for you to look at. It's meant to fix your terminal. So, it will start with standard error. If that is closed, actually, I don't even know what's going to happen if I do this. Now, it should go to standard out, right? All right.
So, we might not actually be able to test that. That's super interesting. I'd have to think of a contrived example to make that work. Either way, the to command is for emitting things to the screen, emitting things user. The reset command is for when when things go real bad, man. When things get real bad, real messed up, you don't know what's going on. Reset. Enter. Fix your terminal.
That's what happens there. How does it actually work? Oh, there's a whole song and dance. And I'm actually not lying when I say that. Oh, wow. Things got real messed up there. I'm not lying when I say there's a whole song and dance.
There is a whole song and dance because we're talking about 50, 60 years full of terminal history. So, it has to go through a whole seance of things to try and fix your terminal. Some very old stuff and some very new stuff. We'll see that. We'll break down exactly what's happening. Now, I've only showed you the Ansie stuff that it outputs or just what it outputs directly to the screen.
There's still more that's going on.
There actual functions that are being called. So, we're going to break this down into two steps. I'm going to show you what's output to the screen and then I'm going to talk about the functions that are being called under the hood.
So, we're going to break down this. So, as you can see here, 1B shows up a lot.
That's the escape character. Also, 07 shows up. It should show up once. Yes, it shows up once. 07 I know what 07 is.
It's a super annoying character. It's the bell character. So, if you were to print F was like this, it will trigger my bell. Look up here. The bell emoji showed up. normally would make your terminal bonk or like flash or something. It's super annoying. I turn off all visual flashes and noise making, but this is how you make your terminal make sound. It's the bell character. So, there's escape sequences and there's the bell character. What I'm going to do here, trying to think of a clean way to do this. Let's just be dirty about this.
I actually don't care right now. Let's just turn all control characters. So, this will be all control characters.
This is the 1B and the 07. So, the bell and the escape characters. We're going to turn them into new lines. And this is a little more readable. It's still ugly.
Don't get me wrong. This is super duper ugly, but this is more readable what's happening here. So, we have like escape sequence C. We have 104. We have bang P.
We have three semicolon 4L. There's a lot of stuff going on here. Each one of these is going to be some sort of like ANC code or something. There's going to be like CSI OSC commands. All sorts of stuff there. You can look up every one of these on your own time. I'm going to give you the already annotated version cuz I went up and looked all these up.
I'll link a gist where I go over the ANC escape sequences. is I had to look up some like terminal documentation. Some of this stuff is super old. So, let's go ahead and paste this in from my notes over here. Let's do reset.ext.
And this is what the reset command outputs. So, I'll show you what that looks like. Here's it. Here's all the commands you can see. So, E right here with the C. And then we have 104 here, which is 104. And the bell character.
Remember, the bell character was removed. Then we have the escape bang P.
The escape gang bang P. So, I just copy them all over here. And this is what's happening here. So this is there's like the name of the command. This resets the terminal to the initial state. This resets the color palette. It's a OSC command, operating system command. So this is relatively new. That's why I put modern here. This is a soft terminal reset. This is a reset DEC private modes. This is super old. I think I found like references to like the '7s here. So this is a lot of this stuff.
It's a mixture of modern stuff and very very old stuff. So this is like I want to stress this. This is just throwing everything at the wall and seeing what sticks. We're just throwing everything at your terminal and trying our best to come up with some sane defaults. I don't remember if I talked about this earlier in the video. I may have mentioned styane.
This will reset your terminal kind of sane defaults. The nice thing is the reset command kind of does all that for you. So going over the reset command kind of goes over all that. So this is what's going on with the reset command.
This is all the stuff that's being emitted to the screen. So clear is simple. Clear just outputs some things to your screen and that's it. Reset is a seance, dude. It is a song and dance throughout decades and decades of history to try and get your reset to your terminal to make sense. That's the difference there. Also, I show this off.
This is a cool command. STY minus a.
This is the current settings of your terminal. So, this is super cool.
Sometimes I run this, I save it to a file, then I modify some things, the terminal, and I run it again. I can compare the differences. This tells me everything about my terminal, what options I have enabled. Just a super cool command to be aware of. I'm not going to dive into all this. There's so much stuff here. I'm going to use some of this information later. I'm foreshadowing some really awful stuff that I had to work through to get this to work. But we're going to get there.
And when I say we're going to get there, I mean we're going to get there right now. Ready? We are going to make our reset command. Reset C. There we go.
Let's make our reset command. Let's go ahead and return zero here. Uh, I should make you guys happy, right? Exit success. Oh, look how portable I'm being. That's amazing. Um, yeah, let's do that. creset.c C into reset and oh of course right we pulled in that. So let's go ahead and pull in the uh was it standard lib.h I think that's what it was.
There we go. So now if we run our reset command doesn't do anything. We haven't implemented anything yet. So before I continue I want to stress this and I'm going to stress this in comment format.
This only makes sense for Dave's terminal because the reset command looks at my specific terminal and figures out exactly what my terminal supports. This only going to make sense for my terminal. You would never really want to run this on your own terminal. I'm just showing you this for educational purposes. Again, if you want the reset command, run the reset command from men curses. It comes with your system most likely. Use that. So, this is only going to make sense for my terminal. But here's what we're going to do. We're going to take some of that stuff we had before, which was my reset.ext.
This goes over all of the code. So, let's take this and put this all into a big giant string. Actually, I don't even need to copy and paste. I think you can just do what? Reset.ext.
There we go. So, let's do this. We could do a const car s equals this. So, we can make a new string. I think our best bet here is just to use a macro to do this. So, let's create a new macro. Delete these two characters. Open this. Find a space.
Put a new quote here. Find that. Delete it. Put slashes here. Go down. Beginning of the line. That should be it, right?
That should just work, I think. Let's replay that macro. That looks good. All right. So, these all look good. I think in C. I'd have to double check. I think that it won't like these back slash E. I don't know if these are actually valid.
So, I think we can just swap that out with a 1B.
There we go. That's the hex code for that. And then this character is what?
07. I know we're mixing octal and hex, but you know, hey, here we are. So, let's do this. There we go. That looks good. Cool. So, if we write to this to the screen, it should do the same thing.
So, let's go ahead and pull that in.
Include string.h for Sterlin. Let's include the Unix standard header for write.
And then we can just write to standard air file number. So, we're going to go to standard air instead of standard out.
We want this reset command to work even if it's being piped somewhere. We're just going to kind of mimic the other reset command when we do this. We're going to write s out to that. And then we can simply write out stir len s should just work. And then we should probably do the same thing we were doing to Yeah, let's just be good since we're here. Let's just be good. There's a frog outside. You guys hear that frog chirping? I don't know if the mic's going to pick that up.
Well, that's annoying.
But there's wildlife. It's kind of nice.
Um, yeah, let's put this here. And then let's save this as N.
This gets len. And then we can just do the same thing here. Len equal equal N exit success or exit failure.
I can type return. All right.
CC reset into reset. Was that one? Hex escape sequence out of range. Do I need to put this here? I might need to just put that there. I don't actually know.
Oh, you know what? I think I'm just being stupid, actually. I think it's the I think it's this thing I've had. You know what? Okay, that was a previous video where I had issues with this exact same thing and I vowed to change the way I was using it. Look at this. It's eating that C character. I think it's eating the characters because it doesn't know when to stop parsing something as hex. It's not just doing these two characters. So, I think I can swap these out to was it 033? X 1 B hex is 33 octal, right? It's 27 decimal. Yeah, I think it I think that works. Um, I think this will fix the issue. Yeah, I could. There's other ways I could get around it. Let's see if this actually fixes the issue. I'm very curious now.
Okay, that fixes the issue. I want to double check now because I put the semicolon here. I don't mind the semicolon being here. I think it actually is fine. But if I put the semicolon here, this shouldn't be an issue, right?
Yeah. Okay. Okay. I thought that wasn't an issue. If we run reset here, look at that. That's beautiful. It resets the whole screen. If we pipe it to XXD, nothing will happen because we're writing it to standard error. And if we do two greater than one, this is what we're getting. That makes perfect sense because if we take the actual reset command that ships with our system and pipe it to XXD, we get the exact same thing. We get this OD here. I'm not entirely sure what's going on there. I'm going to be honest. Either way, it seems like it's working. What about the reset one that we compiled?
Oh, this is so confusing. I'm stupid. I just realized I made reset. Ciled it into my current directory, but my current directory Oh, yeah. My current directory is not at all what I thought it was. We were inside the encurs program. So, let's fix this. Let's pull reset.star out of this. That's a little bit confusing.
So, what did I do wrong? Oh, yeah. I'm stupid. There we go. There we go. This looks a lot cleaner now. So, now we can CC reset C into reset here. And now we have a nice little reset command that does what we want. Okay, this is a lot nicer. This is a lot nicer.
I'll make all this source code available online. Obviously, if you're watching right now and you like what you see and you like this kind of content, go ahead and hit subscribe, okay? I have 130,000 subscribers at the time of this recording. I love watching that number grow. And uh yeah, just hit the subscribe button, hit like, share with people, share with people you think would enjoy this cuz if you made it this far, you're a super nerd, man. So, I appreciate it. Thank you so much. So, this is cool. We are outputting the exact same thing to the screen as we was being output by the actual reset command that ships with my system. But there's more to the story here. Okay, the reset command is heavier than just outputting things to the screen. It does some gnarly stuff. It actually calls these functions that exist on your system. At the end of the day, they call things like TC get and TC set. So, it's like TC get adder, I think it is. Yeah, this is term iOS stuff. So, we have TC get adder and TC set adder. This is how you get and set attributes about your current terminal. Encursur at the end of the day is going to be doing stuff like that.
I've looked through the source code a little bit. If we look through encurses uh six progs uh what was it? Reset command C. There's a lot of stuff that is going on under the hood here. And there's just a lot of stuff that's going on under the hood here. A lot of it's end cursor specific.
I don't want to dive into it. Instead, we can cheat. So, my initial thought was to go to Alumos and cheat, not Linux, because I could run Drace. I know a lot of you guys are probably watching on Linux. I do want to do a video where I talk about Drace eventually, but for now, because you're watching on Linux, I know it probably makes sense to just uh use the things that are built in. So, you have like Srace and stuff like that.
So, we'll do that cuz I think that would be a lot of fun. So, we can do some fun stuff here. We can run srace and we can srace the reset command. Okay, that makes sense here. We run that and we get a whole bunch of crap to the screen.
That's cool, but there's a lot of stuff going on here. So, let's do some stuff here. Let's write the srace output to a file. So we will call this reset.trace and there are some other flags we can give s trace- e. These will be what we are actually tracing. So we are tracing ioctals. That's because TC set adder and TC get adder actually go through the octal interface. These are sys calls.
It's very low-level stuff. I wouldn't expect you guys to know this. Don't worry. -v so we can actually break apart some of these symbols and - s for a string size of a thousand. By default it's like 64. So we're just bumping this up to make sure we have room for it. And I threw a lot at you. So let me show you. If we go to man s trace we have um what did I say -v -v is cool prints unabbreviated versions of environment stat term iios so it knows the term iOS structures this is going to be useful for us the other thing I mentioned was what - s and uh there was a size next to it where did they put this dash s space stir size specifies the maximum string size to print the default is 32 oh I said it was 64 it's 32 file name is not considered strings and are always printed in full okay cool so what That means is we now have this file called reset.trace. And if we look at it, there's a lot of stuff going on here.
So, let me go ahead and just cat that to the screen. We can kind of see what's going on here. We have a whole bunch of octals. We have some TC get. And if we go down here, we have some TC set. So, basically, we get some attributes. We get some information about the terminal.
We modify it and then we set those attributes back. That's how this command works. There's actually going to be three sets. This This kind of blew my mind. I was surprised. Well, I wasn't surprised. That was just uh I don't know what I expected.
Uh TC set. So if we do this, we can see that there are three lines. You actually call three different sets. And it's so interesting. It sets a flag. I had to like actually break apart these command.
There's a lot going on here. I don't think I I always say I'm going to cut this from the video. I'm not I'm going to leave it in. You guys like when I leave all this stuff in. There was a line that showed up here. I'll show you guys. If we highlight I think it's post.
Yes. Look at this. We turn off post-processing, then we set some stuff that could possibly be messed up by post-processing, and then we turn it back on. So, that's kind of the little trick that's going on here. And a lot of these things, I didn't even know what they meant. I had to look it up.
Documentation was very, very hard to find on this stuff. That's not true. The documentation was actually surprisingly easy to find. It's just in a lot of different places. So, it made things very difficult. And I will show you what I mean by that. White mode jump scare. 3 2 1. Here we go. This is the Pix specification. As you can see, we have the termiosh here. This is the header file. So, you can see what the structure actually looks like. There's a lot of stuff going on here. This was stuff we saw from the srace. Here's all of the options here. I know what all these word means in isolation, but knowing what they all do together was kind of gnarly.
So, to figure out what I actually wanted to set for these, I'll show you what I did instead. So, over here, I went to my machine that's on Alumos. I have the Alumos source code pulled down here. I did a search for the structure name. So it was uh C underbar CC equal I did this and I found this. So in the init.C file.
So this this is ridiculous what's going on here guys. I'm being honest. I found this the term iOS the default term iOS here and here's where all the options were set. So I kind of just used a mixture of the pix header this default on a lumos and what the srace output gave me to make this work. It's weird.
It's stupid. I wish I had better stuff to give you guys, but instead of just giving you guys magical information from on high, like, oh, here I just set this.
I want to show you how I actually look this up. So, I pulled from a bunch of different sources. I'm not an authoritative source. I just messed around until things worked. That's how I approach systems. Let's go back here and let's show you what I'm actually talking about cuz I'm kind of all over the place. Before we write this stuff to the screen, let's actually mess with the terminal first. So before we even write any of this stuff to the screen, let's mess with the terminal. So there's two ways we could do this. I'll show you guys what I mean. We have term iOS here.
We're going to pull uh pull in term iOS here. We might have to No, it's just lib C. We should have to link against it. TC get adder and TC set adder. There's two ways we could do this. We could get the attributes of the current terminal, modify it, and then write them back. Or we could just statically set our own terminal settings. Just say, you know what, you're getting this. you are just being set to this no matter what you currently are. I don't know if one's even better than the other. I think it's easier to start with something that's already set up for us a little bit. So I think we get the attributes first and then we modify them and then we set them. And I will show you what that looks like. So let's first do this strct term iOS. So we make a new variable called T. And then we can do TC get adder. This takes the file descriptor and then the pointer to the T. The file descriptor that we're going to use, we could use standard out. We could use standard in. The order that uh encurs said they do is they go through standard error, then standard out, then standard in, then dev tty. We have so many options here. I'm going to use standard in. I'm going to use the standard in file number, which is zero, because this will most likely work. This means that if you pipe something into our reset command, it won't work. I hope you never do that. An easier way would just be to open dev tty. There's some weirdness that goes on with that. There's really not weirdness that goes on with that now that I say out loud.
Yeah, let's do that. Let's do that.
Let's open up devdty. Let's do that.
Man, open. Open is a super simple command. We need the uh file control header here. Uh open just takes the path and some flags and returns an int. So, we can do this. We can say int fd equals open dev tty. And then we can open it with o read only. There we go. Let's pull this down here. That's not what I wanted. I wanted to sort that. Perfect. Let's open that. If FD less than zero, that means it failed to open. Then we will just return exit failure. We should probably print something to the me to the user.
But for now, we can just do this. So if that works, then we can just do a whole bunch of stuff here. And then we're done with that. So we can close the FD here.
Now we can do TC get adder on that FD with a pointer to the term iOS structure that we made. And now we will have that.
We're not doing anything with that information yet. But let's go ahead and run this code just to make sure it works. So it compiles and it runs successfully. It clears the screen. It runs successfully. That's beautiful.
Figure out what's going on here cuz TC get adder can also fail.
So let's search for return. All other functions return zero on success minus one on failure. So super simple. We can just test for that. We can do this. If TC get adder equal equal minus one, then we know that there was a failure there.
So we can just return exit failure. This is the last video where I'm using exit failure and exit sess. I like just returning numbers.
There we go. So again, same deal. Let's recompile this. We if it clears the screen, then we know that something worked. Otherwise, it wouldn't have been able to get there. Also, here's a little trick. Because we're testing, we can just return 42 here. So, this is a nice little trick. It might yell. Oh, will the compiler yell at me? No, it works just fine. Perfect. Here's a nice trick.
If we get 42, then we know we made it to that. So, that's good. This is a super scuffed way of debugging. So, awesome.
We did a TC get adder. Now, our T structure will have a whole bunch of stuff in it. So, I'm going to write this down. Let's get our current terminal adders.
So, we're getting our current terminal adders.
modify the adders.
Write the adders back. So, we can actually write the adders back right now. There's no reason to do anything else. We can do if TC set adder. This takes an FD. This also takes a pointer to the structure. It takes another argument. I don't remember what it is off the top of my head. So, we'll have to look up what that is, but I know for a fact it takes another one. It always screws me over. I think it's like when you want to have the settings take effect. So, what was it? TC set. Yeah, look at this. Optional actions. Yeah, see, I knew it. I just I had a feeling I've dealt with this term iOS stuff going from uh a Lumos to Linux and compiling software that was written for Linux, but we want it to work on Alumos.
Uh we want this TC as now. So that's super simple. All that means is we just put that as the middle argument here. So we want this to act like right away basically the changes occur immediately. Yeah, simple as that. Let's go ahead and recompile that. Run it. We should get 42. That's awesome. So, we were able to write those settings back. Now, let's modify some attributes. So, I've pulled these off to the side and I'm going to copy them in. Like I said, I pulled them from a mixture of the Termios header, the Alumo source code, and what we got from that srace output. So, here's the first things I'm going to pull in. Let's do this. There we go. So, this is the input flag. Let me pull this up.
Actually, this might be easier for you guys to see because the structure is described.
The structure is described and it might be useful. The CI flag. So, the I flag are the input modes. The O flag are the output modes. C flag is control modes. L flag for logo mo local modes. And then the special characters are CC. So, we're just going to keep track of these.
Actually, you know what? Hold on. Let's do this. Go. That frog's chirping out there.
There we go. So, now we have this. We can take this and we can put this into the input modes. So this means turn on these flags. So the pipe equal you were oring it with this. We're logically oring it with Burke in it with breakant i kernel and I I don't know how to pronounce these words. And then these mean to turn it off. We're logically ending it with the negation of these. So we're turning off these flags. So I'm going to pull up the source code again.
I'm going to pull up the header. So you can see signal interrupt on brake map carriage return to new line on input break the ignore break condition. So there's a lot of stuff here. I'm not going to go into all these. I'll link the documentation below. This is just a mixture of stuff I was able to get like I said from all those three different sources. So I'll pull these in and I'll kind of just go through them real quick.
All right. So here we go. I pulled everything in here. As you can see like it's kind of confusing. Like I don't even know what half this stuff is. I'm going to be so real with you. I just pulled this from things. We can mess with this. And of course, we absolutely absolutely will. But let's go ahead and make sure that what we did makes sense.
So, we can recompile it. We can run it.
And we get 42, right? We get 42. Yeah, I forgot about that. Let's go ahead and get rid of that early return. I mean, hey, at least it worked, right?
There we go. Recompile. Run it. And that should work. That should be a full reset. So, if we were to do something stupid like curl- hot dog stand.
And our terminal looks awful. Now we should be able to run this and we get a nice little reset. That's amazing. If we were to run sty no echo or no minus echo. I always forget. You're not going to see it. I'm running dot slash res.
And there we go. Now we have echo back.
So that works. And you can prove I can press up and see that that was the last command I ran. So that's good. That does what we want. That's amazing. There is one little thing I just want to show you guys that this actually does do something because I pulled up the header. There's a really fun little flag I'm going to set here. You ready for this? This is really stupid. In the output mode where we have O post and O on on LCR. I don't even know what these are. We can add a new thing to this output flag. Check this out. You ready for this? This is really, really, really stupid. We are going to add Ocu.
You ready for this? We're just going to add this. Let's recompile it. Let's run reset. And now it looks like this because everything has been converted to uppercase. Why some of the commands are missing, I have no idea, but everything has just been converted to uppercase and that's at a terminal setting. That's not anti escape sequence that was written to the screen. That's bec Oh no, this is going to be bad. Oh, how are we going to do this?
Oh boy, it's going to be fun. Oh, this is impossible to read. Oh, I don't actually know how we're going to fix this. I'm so dumb. We're just going to run the built-in reset command and it's going to fix it. I am an idiot. Oh my god. Oh, that's so embarrassing. That's so funny. All right, let's get rid of this before it causes issues. And let's do let's do this. Let's do uh try adding uh o lcu lol. There we go. We'll add that there.
There we go. Okay, so now the reset command works as expected. So there you go. There's the deep dive into reset and clear and end curses and terminal settings and all of this fun stuff. I will make all this source code available online so you guys can peruse it at your leisure, of course. And uh yeah, hopefully you guys enjoy this. Let me know how you guys like this style of content where we just kind of deep dive into this fun stuff for fun because it's fun. That's literally why we do it. So yeah, let me know if you guys like it. I think it's super fun. And take a look at this. Of course, I can't end a video without saying thank you to my patrons over at Patreon. You guys are awesome. I quit my job about a month ago and I'm using money I have saved up to just kind of relax, chill, and kind of focus on content. Low-key, just, you know, becoming a content creator kind of. Uh, so if you want to support that journey, feel free to sign on my Patreon. You don't have to. I'm not trying to guilt you into it. This is just if you want your name at the end of my video and you think it's fun, go check it out. Look, Dark Space still messes up the uh Patreon output. That's so funny. It's cuz Patreon lets you put zero width uh spaces in your name. How wild is that? I wonder what other characters they allow.
Please don't break my script.
But yeah, thank you so much my patrons over at Patreon watching. If you're watching right now, thank you so much.
Hit that subscribe button, like, share the video, do whatever you got to do. At the time of this recording, we have 103,000 subscribers and I'll be getting that silver play button soon, so that'll be a ton of fun. I'll annoy you guys when I get it because I'm super excited about that. Thank you everyone for subscribing. Thank you anyone who's made it this far. You guys are awesome. I appreciate it so much. It's super cool that you guys enjoy watching these videos. I I can't believe that you guys find this interesting. I love it. I think it's great. So, thank you very much. I'm going to go figure out why my air conditioning is not working. I think it's 95 degrees in here. That's Fahrenheit, by the way. So, I'm dying. I am sweating if you can't tell. So, thank you everyone. That's it for me. Have a good night. See you.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











