More to come...
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

318 wiersze
20KB

  1. #ifndef CHAPTER_2_HEADER
  2. #define CHAPTER_2_HEADER
  3. #include <stdio.h> // We need this header file for functions 'puts' and 'printf' in 'hello_world' functions below.
  4. #include <unistd.h> // And in this header file we have write system call.
  5. /*
  6. In this chapter, you'll learn about:
  7. - Curses library (libncurses "clone")
  8. - More on function declarations
  9. - Escape sequences
  10. - General scope and design layout
  11. - Function and variable definitions
  12. - Terminal input and output
  13. Lets talk about function declaration. In C, sometimes you need to declare functions that you'll use before they're called. I prefer to always declare functions except when I'm
  14. quickly prototyping something out. Function declarations are property of old programming languages, you don't have to use them if you're calling every function after it's defined
  15. (but not declared, there's a difference), but if that function is only used in one file, you should use 'static' keyword like in the example below. Keyword 'extern' tells the
  16. compiler that your function will be used in other files, or if you're compiling your files separately, it won't add the function address (in some cases). In any case, function
  17. declarations should be in C header file, and function definitions should be in C source file. You can see how I declare functions in '.h' files, and define them in '.c' files,
  18. unless I don't want to make some function external in some '.c' file, I use 'static' there.
  19. @C
  20. // Function declaration: // # // Output: // Input:
  21. extern void function_0 (void); // 0 // undefined (none) // undefined (none)
  22. extern float function_1 (double a); // 1 // ieee754_32b // ieee754_64b
  23. extern int function_2 (int a, int b); // 2 // integer_32b // integer_32b, integer_32b
  24. static unsigned int function_3 (char * a, int b, char c); // 3 // natural_32b // integer_8b_pointer, integer_32b, integer_8b
  25. static char * function_4 (char * a); // 4 // integer_8b_pointer // integer_8b_pointer
  26. static void * function_5 (struct s * a, void * b); // 5 // undefined_pointer // [structure]_pointer, undefined_pointer
  27. extern unsigned long int * PLEASE_NO (const signed short int, void *, const char const * *, long double []);
  28. @
  29. So, lets go through those 6 normal functions, and then lets see what's wrong with 'PLEASE_NO' function.
  30. 0) This is the simplest form of function, which can be used to make the code more clear (sometimes) by breaking down long functions into smaller ones, and calling them in certain
  31. order. However, don't abuse it for code refactoring, because most of the time, procedural code is the easiest to read, write and debug.
  32. 1) In C (and in non-embedded environment, also known as your normal post-2012 laptop or computer), keyword 'float' is (almost) real number encoded in IEEE754, 32 bits or 4 bytes
  33. long, while keyword 'double' is same as float, but enlarged to 64 bits or 8 bytes. They are pain to use in some cases because of limited precision.
  34. 2) Functions can have multiple arguments (input variables), but only one return value (output "variable"). In C, you can name the arguments, but not the return value, also keep in
  35. mind that you should choose descriptive names (in most cases) for function arguments, not name them a, b, c, d like in these examples.
  36. 3) If you really want the compiler to verify that some return value is of type 'unsigned int' aka 'uint32_t', you can do so, I prefer 'int' since all number literals in C are
  37. integers by default ('int'). We'll use terms like 'int / integer/ int32_t' interchangably, so keep that in mind in future chapters.
  38. 4) Also, type 'char *' can be pointer to (address of) of some 'char' typed variable or array of type 'char'. We'll talk more about arrays in future chapters, but know that we
  39. could use 'char a []' in functions 3 and 4 as well, it's same. You could use them both in order to know which is array and which is pointer.
  40. 5) Lastly, this function returns pointer to any type, hence compiler won't do type-checking, it's necesary to this sometimes. It also accepts pointer to (previously defined)
  41. structure 's', and if you used 'typedef' with it, you could just say 's * a', we'll talk more about those.
  42. Now, lets talk very briefly about what's wrong with 'PLEASE_NO':
  43. - Don't use long types 'unsigned long int', if you really type with those characteristics, use 'size_t : stdlib', 'uint64_t : stdint' or 'unsigned long' in worst case.
  44. - Don't use 'const' keyword at all, that's my honest advice, it won't catch that many potential bugs in C, it'll just cause useless compiler warnings. Live unsafe, think more.
  45. - Don't use 'signed', since it's default for any integer type, as well as you shouldn't use keywords 'register', 'volatile', 'auto', but more on that later...
  46. - You can use '[]' in function declarations, but it doesn't mean much since array is passed as pointer to first element in C (array decay), so '*' is enough.
  47. - Keep in mind that newer (non-ANSI) C standards allow some of the things that I'm not using here, but I don't personally like newer C standards, I'll mention that a lot.
  48. - Last one is tricky, you should name function agruments in function declarations, but some linters will warn you not to do it, since some compiler don't check them.
  49. Very soon, you'll be able to write your own small C programs, so prepare for it. Also, the most important chemical element for organisms is, you guessed it, C (carbon). It's
  50. the same in programming too, C is the most important language that a good programmer should know. Other languages are just extra. Now:
  51. This is probably the first program new programmers write in language they're learning. It simply prints text to standard output. As C is very old programming language, you have a
  52. lot of ways to do it, so we'll simply put all of them into array of function pointers, and call them sequentially in loop in our main function.
  53. */
  54. extern void hello_world_0 (void); // All 4 functions take no agruments, and return nothing. They just execute the code that's defined in them.
  55. extern void hello_world_1 (void);
  56. extern void hello_world_2 (void);
  57. extern void hello_world_3 (void);
  58. extern void (* hello_world [4]) (void); // External (global) variable with name 'hello_world' of type 'array of 4 function pointers with no input/output'.
  59. /*
  60. Alright, we'll never use those 4 functions, but now you know how to declare any function, it's time for more serious examples. Below you'll see some scary looking cursed functions
  61. that interact with the terminal in which we're executing this program. We'll change the terminal from default (sane) mode to raw (insane) mode. That means the characters we press
  62. on our keyboard won't be printed in terminal, like usual. That'll allow us to make cursed C program that'll scare away all GUI fanatics. In the ancient times, when people used
  63. teletypewriters, terminal was obscure magic. Nowdays, parents scare their children into obedience by showing them terminals. Like, if you don't behave, I'll edit X11 scripts and
  64. you won't be able to see icons, you won't pass the Login text on your screen, you'll never open your web browser again (lynx rules).
  65. */
  66. #include "chapter_0.h" // We use functions declared in these two header files, so in order to make them visible, we included those files here.
  67. #include "chapter_1.h"
  68. #define SIGNAL_ARROW_UP (0X415B1B) // Just hardcoding some arrow key ASCII escape sequences, we'll explain them in later chapters.
  69. #define SIGNAL_ARROW_DOWN (0X425B1B)
  70. #define SIGNAL_ARROW_RIGHT (0X435B1B)
  71. #define SIGNAL_ARROW_LEFT (0X445B1B)
  72. #define SIGNAL_CONTROL (0X1000000) // We're also defining some signal masks, they'll be used later to mark which key combination is pressed.
  73. #define SIGNAL_SHIFT (0X2000000)
  74. #define SIGNAL_ALTERNATE (0X4000000)
  75. #define SIGNAL_SYSTEM (0X8000000)
  76. enum {
  77. // This enumeration will be used for signal processing, I put some special cases on their own line.
  78. SIGNAL_NONE,
  79. SIGNAL_ANY,
  80. SIGNAL_A, SIGNAL_B, SIGNAL_C, SIGNAL_D, SIGNAL_E, SIGNAL_F, SIGNAL_G, SIGNAL_H,
  81. SIGNAL_I, SIGNAL_J, SIGNAL_K, SIGNAL_L, SIGNAL_M, SIGNAL_N, SIGNAL_O, SIGNAL_P,
  82. SIGNAL_Q, SIGNAL_R, SIGNAL_S, SIGNAL_T, SIGNAL_U, SIGNAL_V, SIGNAL_W, SIGNAL_X,
  83. SIGNAL_Y, SIGNAL_Z, SIGNAL_0, SIGNAL_1, SIGNAL_2, SIGNAL_3, SIGNAL_4, SIGNAL_5,
  84. SIGNAL_6, SIGNAL_7, SIGNAL_8, SIGNAL_9, SIGNAL_ESCAPE, SIGNAL_TABULATOR, SIGNAL_RETURN, SIGNAL_NEW_LINE,
  85. SIGNAL_SLASH, SIGNAL_BACKSLASH, SIGNAL_QUOTE, SIGNAL_BACKQUOTE, SIGNAL_SPACE, SIGNAL_BACKSPACE, SIGNAL_DOT, SIGNAL_COMMA,
  86. SIGNAL_QUOTATION, SIGNAL_CAPS_LOCK, SIGNAL_L_BRACKET, SIGNAL_R_BRACKET, SIGNAL_MINUS, SIGNAL_EQUAL,
  87. SIGNAL_COUNT
  88. };
  89. /*
  90. These below are external variables, they can be accessed and modified in any file where they're (re)declared. We're compiling C source files (.c) separately, so the compiler will
  91. output object files (.o), and then we can link them together (most compilers are also linkers, this is nothing out of ordinary) into final executable. We can, for example, use
  92. functions declared in "chapter_2.h" and defined in "chapter_2.c" in completely separate file "chapter_3.c", if we have included it with '#include "chapter_2.h". Keep in mind that
  93. header files can be included recursively, that's why we use those header guards, '#ifndef SOMETHING', '#define SOMETHING' and '#endif' at the end. That way, they'll be included
  94. only once, so compiler won't be confused and spit out errors. You can see how I use those chapters to create new programs, and all that is in 'program/' folder. You can also just
  95. open some 'compile.sh' file or 'makefile' and see how I compile and link.
  96. Now, lets talk about ASCII escape sequences. Most terminals support some special combination of characters, that aren't printed like you'd normally expect. We call them escape
  97. sequences because they all start with character literal '\033', '\x1b' or '\e', which is same as decimal 27, and in our character enumeration 'CHARACTER_ESCAPE'. They are somewhat
  98. standardized way of interacting with your terminal. I'll use '\033' here because it does well with C strings and it's the most widely supported. Also, we'll use formatting of
  99. 'printf' function from <stdio.h>, which we'll need to cover later in more details.
  100. "\033[H": Set cursor position to upper left corner (position 1, 1).
  101. "\033[%i;%iH": Set cursor position at Y and X coordinates, ranging from 1 to N (I simply limit N as 1000, and they can be prefixed with '0's).
  102. "\033[6n": Get current cursor position, this will output response string defined just below.
  103. "\033[%i;%iR": Response is similar to 'set-cursor-position' string, except it's 'R' instead of 'H', so you'll need to parse it.
  104. "\033[2K": Clear an entire line.
  105. "\033[2J": Clear an entire screen.
  106. "\033[c": Reset terminal to initial state, we won't use this string, but 'tcsetattr' function.
  107. "\033[%dA": Move cursor N characters up, if you don't provide N, it's assumed to be equal to 1.
  108. "\033[%dB": Move cursor N characters down, if you don't provide N, it's assumed to be equal to 1.
  109. "\033[%dC": Move cursor N characters right, if you don't provide N, it's assumed to be equal to 1.
  110. "\033[%dD": Move cursor N characters left, if you don't provide N, it's assumed to be equal to 1. Multiple cursors edited this file...
  111. "\033[%d;3%dm": Set character attributes (effect and colour) to types described below, first is effect, second is colour.
  112. "\033[0m": Reset character attributes to default state.
  113. Effects: Colours:
  114. - Normal: 0 - Grey: 0
  115. - Bold: 1 - Red: 1
  116. - Italic: 3 - Green: 2
  117. - Underline: 4 - Yellow: 3
  118. - Blink: 5 - Blue: 4
  119. - Reverse: 7 - Pink: 5
  120. - Cyan: 6
  121. - White: 7
  122. Instead of hardcoding values yourself and remembering those, you can just use enumeration identifiers, like, COLOUR_BLUE and EFFECT_BOLD.
  123. */
  124. extern int curses_character; // Literal character (integer) that user has pressed, some of them are more than single byte, like arrow keys defined above.
  125. extern int curses_signal; // Converted value of 'curses_character' into values of macros and enumeration values named as 'SIGNAL_*'.
  126. extern int curses_screen_width; // Width of terminal window in which we're rendering.
  127. extern int curses_screen_height; // Height of terminal window in which we're rendering.
  128. extern int curses_active; // As long as there's no termination signal, this variable will be different from zero. We use it to exit the program.
  129. /*
  130. Now, lets talk about scope. Roughly speaking, every variable, constant, function, enumeration, structure, union, definition, declaration, file, anything really has some scope,
  131. where it is known to the compiler, visible. If, for example you're using 'printf' function from <stdio.h> header file, and you didn't write "#include <stdio.h>" before that line
  132. in which you're using it (and outside of function definition, that's K&R thing, I'll explain later), compiler will get scared and confused, and it'll just drop dead. You're left
  133. with no executable, compiler spat out some error messages, you don't know what they mean, it's over.
  134. So, if you have something like this:
  135. @C
  136. int F (int A) {
  137. int V = randomize (0, A);
  138. return (A + V);
  139. }
  140. @
  141. Variable 'V' is local to function 'F', and to no other function, you can only use variable 'V' inside function 'F', but you can name other local variable as 'V' too, no problems.
  142. Inside file named "a.c", for example:
  143. @C
  144. static int X = 100;
  145. static int Y = 200;
  146. static int Z = 300;
  147. @
  148. Variables 'X', 'Y' and 'Z' are only visible in file "a.c", and no other file. You can modify them inside any function in that file, but don't name any local variables as that.
  149. You want to use global variables? You should have declarations in header file, and definitions in source file:
  150. @C
  151. // a.h
  152. extern char * A;
  153. extern char * B;
  154. extern char * C;
  155. @
  156. @C
  157. // a.c
  158. #include "path/to/file/a.h"
  159. // ...
  160. char * A = "Heyo";
  161. char * B = "Cyaa";
  162. char * C = "Meme";
  163. @
  164. In this case, you can modify variables 'A', 'B' and 'C' in any file that has included header file "a.h", not only "a.c", but you must define them only once, as shown here.
  165. When you forget to include some header file, provide a variable, constant, function declaration or definition, compiler will warn you about it, because it can guess in simple
  166. cases where that function is declared, but it can't (shouldn't) scan your entire SSD or HDD to find it, because it would take too long and maybe you just mistyped the name of it.
  167. If you forget to write "#include <unistd.h>", and use write or read "functions", the compiler will kindly tell you to include it, but it would assume you want to use that very
  168. functions, and it will compile it anyway, giving you your executable in hope it's correct. It can't do that always...
  169. In K&R C, aka old C, it was something like this (don't mind the stupid example):
  170. @C
  171. // K&R C (with a lot of implcit state) // Sadly new C standards (verbose and "safe")
  172. putchar (c) { extern int putint (int file, int data, int size);
  173. extern putint;
  174. static int putchar (int c) {
  175. putint (1, c, 1); return (putint (1, c, 1));
  176. } }
  177. @
  178. Essentially, you'll get the feeling for it after few compiler warnings, don't be afraid to make mistakes that compiler can catch.
  179. About my preffered program layout, here's what I think about it:
  180. @C
  181. // License notice if you want.
  182. // Header guards or definitions.
  183. #ifndef BLA_BLA_HEADER
  184. #define BLA_BLA_HEADER
  185. #define _DEFAULT_SOURCE
  186. // Standard library C header files.
  187. #include <stdarg.h>
  188. #include <stdlib.h>
  189. #include <unistd.h>
  190. // User provided C source or header files.
  191. #include "heyo.h"
  192. #include "cyaa.c"
  193. // Macros that you really want to have.
  194. #define UNUSED(x) (void) x
  195. // Internal function declarations then variable definitions.
  196. static void uberheyo (void);
  197. // Or:
  198. static void ubercyaa (void) {
  199. // ...
  200. }
  201. static int h = 0;
  202. static int c = 1;
  203. // External function then variable declarations.
  204. extern void heyo (void);
  205. extern void cyaa (void);
  206. extern int H;
  207. extern int c;
  208. // Internal function definition, if not defined above...
  209. void uberheyo (void) {
  210. // ...
  211. }
  212. // External function then variable definition, if not in included C source file.
  213. void heyo (void) {
  214. // ...
  215. }
  216. void cyaa (void) {
  217. // ...
  218. }
  219. int H = 0;
  220. int C = 1;
  221. // Main function.
  222. int main (void) {
  223. // ...
  224. }
  225. // Or:
  226. int main (int argc, char * * argv) {
  227. // ...
  228. }
  229. // You can also define internal or external functions here if you want to.
  230. // End of header guards.
  231. #endif
  232. @
  233. Find one style, and try to be consistent about it, I have to show-case more example of how things look like, so I use more approaches...
  234. */
  235. extern void curses_configure (void); // I like using the approach of "just do it" instead of "begin" and "end", due to that we'll see more internal functions and variables.
  236. extern void curses_synchronize (void); // This function needs to be explained in more details, that'll be done in file "chapter_2.c".
  237. extern void curses_bind (int signal, void (* action) (void)); // Binding specific function pointer to some key, so each time key is pressed, function is executed.
  238. extern void curses_unbind (int signal); // Unbinding any function that was bound to value of 'signal'.
  239. extern void curses_render_cursor (int x, int y); // Render terminal cursor at position X and Y.
  240. extern void curses_render_character (char character, int colour, int effect, int x, int y); // Render single character at position X and Y.
  241. extern void curses_render_background (char character, int colour, int effect); // Render entire buffer with the same character.
  242. extern void curses_render_rectangle_fill (char character, int colour, int effect, int x, int y, int width, int height); // Guess what these functions do...?
  243. extern void curses_render_rectangle_line (char character, int colour, int effect, int x, int y, int width, int height);
  244. // Remember that in chapter zero, I've separated 'string_*' and 'string_*_limit' functions. Now, there's always more ways to logically organize your code, for example, as below:
  245. extern int curses_render_string (char * string, int colour, int effect, int x, int y);
  246. extern int curses_render_number (int number, int colour, int effect, int x, int y);
  247. extern int curses_render_string_limit (char * string, int limit, int colour, int effect, int x, int y);
  248. extern int curses_render_number_limit (int number, int limit, int colour, int effect, int x, int y);
  249. // I really hope that you already know what these functions do just by looking at the names of the arguments...
  250. #endif