From 1fd800abf1ddd571978b8f93f07fffe4ca7f6880 Mon Sep 17 00:00:00 2001 From: TrudeEH Date: Mon, 14 Apr 2025 15:08:48 +0100 Subject: [PATCH] Misc fixes; Update index; Add meson to compiling note --- content/.obsidian/workspace.json | 31 ++++--- content/drafts/bash.md | 104 +++++++++++++----------- content/drafts/rust.md | 135 +++++++++++++++++++++++++------ content/notes/compiling.md | 40 ++++++++- content/notes/index/index.md | 23 +++--- content/notes/linux/index.md | 4 +- 6 files changed, 241 insertions(+), 96 deletions(-) diff --git a/content/.obsidian/workspace.json b/content/.obsidian/workspace.json index 1a85e5c..6c55bf5 100644 --- a/content/.obsidian/workspace.json +++ b/content/.obsidian/workspace.json @@ -13,13 +13,27 @@ "state": { "type": "markdown", "state": { - "file": "notes/linux/index.md", + "file": "notes/index/index.md", "mode": "source", "source": false }, "icon": "lucide-file", "title": "index" } + }, + { + "id": "2c7f99d6e12af978", + "type": "leaf", + "state": { + "type": "markdown", + "state": { + "file": "notes/compiling.md", + "mode": "source", + "source": false + }, + "icon": "lucide-file", + "title": "compiling" + } } ] } @@ -141,7 +155,7 @@ "state": { "type": "outline", "state": { - "file": "notes/linux/index.md", + "file": "notes/index/index.md", "followCursor": false, "showSearch": false, "searchQuery": "" @@ -161,7 +175,7 @@ } } ], - "currentTab": 4 + "currentTab": 3 } ], "direction": "horizontal", @@ -181,9 +195,13 @@ }, "active": "fc8813233e11f75b", "lastOpenFiles": [ + "notes/compiling.md", "notes/index/index.md", - "notes/firewall.md", "notes/linux/index.md", + "drafts/rust.md", + "notes/c-language.md", + "drafts/bash.md", + "notes/firewall.md", "notes/how_to_computer/index.md", "posts/linux-starter-guide/index.md", "drafts/linux/plasma6.jpg", @@ -195,14 +213,11 @@ "notes/git.md", "posts/dotfiles.md", "drafts/linux-encrypt/index.md", - "drafts/bash.md", - "notes/c-language.md", "notes/linux/image7.png", "notes/linux/image6.png", "notes/linux/image5.png", "notes/linux/Pasted image 20250401143509.png", "drafts/swift.md", - "drafts/rust.md", "drafts/nvim.md", "drafts/macOS.md", "drafts/lua.md", @@ -212,12 +227,10 @@ "drafts/assembly.md", "drafts", "notes/drafts/bash.md", - "notes/bash.md", "notes/drafts/linux/index.md", "notes/drafts/rust.md", "notes/drafts/swift.md", "notes/drafts/linux-encrypt/index.md", - "notes/compiling.md", "notes/gdb.md", "debian-12.10.0-amd64-netinst.iso", "notes/encryption.md", diff --git a/content/drafts/bash.md b/content/drafts/bash.md index b75860d..4f94520 100644 --- a/content/drafts/bash.md +++ b/content/drafts/bash.md @@ -8,21 +8,19 @@ author: TrudeEH showToc: true --- -## Bash Language - -### Strings +## Strings - `""` Defines a string which supports substitutions (`$` and `\`, for example). - `''` Defines a string, but preserves its actual value (substitutions are treated as regular characters). - [ANSI Escape Sequences](c-language.md#ANSI%20Escape%20Sequences) apply when using `""`. -### Comments +## Comments ```bash # comment ``` -### Commands +## Commands A shell command consists of the command itself, followed by its arguments. @@ -32,7 +30,7 @@ command "arg1" "arg2" If the first word of a command is a reserved word, bash handles the command, otherwise, it searches for an executable on the system's `$PATH`, a list of directories where a binary could be located. -#### Reserved Words +### Reserved Words | | | | | | | |---|---|---|---|---|---| @@ -41,16 +39,16 @@ If the first word of a command is a reserved word, bash handles the command, oth |`case`|`esac`|`coproc`|`select`|`function`| |`{`|`}`|`[[`|`]]`|`!`| -### List of Commands +## List of Commands - `command1 ; command2` Execute command2 after command1, sequentially. - `command1 &` Execute command1 asynchronously in a subshell. - `command1 && command2` *AND*: Only execute command2 if command1 returns 0 (success). - `command1 || command2` *OR*: Only execute command2 if command1 returns a non-zero exit value (failure). -### Loops +## Loops -#### `until` +### `until` ```bash until test-commands; do @@ -60,7 +58,7 @@ done Execute the code in `...` for as long as `test-commands` return non-zero. -#### `while` +### `while` ```bash while test-commands; do @@ -70,11 +68,11 @@ done Execute `...` for as long as `test-commands` return 0. -#### `for` +### `for` Expand `words` and execute `...` for each member in the resultant list, with `name` bound to the current member. -##### Iterate through List +#### Iterate through List ```bash for item in list; do @@ -82,7 +80,7 @@ for item in list; do done ``` -##### C-like Loop +#### C-like Loop ```bash for (( i=1; i<=10; i++ )); do @@ -90,7 +88,7 @@ for (( i=1; i<=10; i++ )); do done ``` -##### Infinite Loop +#### Infinite Loop ```bash for (( ; ; )); do @@ -98,9 +96,9 @@ for (( ; ; )); do done ``` -### Conditional Constructs +## Conditional Constructs -#### `if` +### `if` ```bash if test-commands; then @@ -115,7 +113,7 @@ fi Execute the first `...` if `test-commands` returns 0, and evaluate the next condition otherwise. This process repeats until `else` is found, or one of the `tests` evaluates to a 0. Once any `...` executes, the remaining `if` construct is skipped. -#### `case` +### `case` ```bash case word in @@ -132,7 +130,7 @@ The `|` operator separates multiple patterns, and each clause can end with `;;`, Using `;&` instead of `;;` would cause the next `...` to be executed as well, and `;;&` would test the next clause, instead of immediately exiting. -#### `select` +### `select` ```bash PS3="Enter a number: " @@ -157,29 +155,29 @@ Output: Enter a number: ``` -#### `((...))` +### `((...))` The arithmetic expression is evaluated according to the rules described below (see [Shell Arithmetic]() TODO link to shell arithmetic). -#### `[[...]]` +### `[[...]]` Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in [Bash Conditional Expressions](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions). -#### Combine Expressions +### Combine Expressions - `( expression )` Returns the value of expression. (Can be used to override precedence). - `! expression` *NOT* an expression. (`true` if expression is `false`). - `exp1 && exp2` *AND* - `true` if both expressions are `true`. - `exp1 || exp2` *OR* - `true` if either expressions are `true`. -#### Grouping Commands +### Grouping Commands Bash allows for commands to be grouped as a single unit. That way, if the group is redirected, the output of every command in the list is passed to a single stream. - `( list )` Create a subshell (variables created inside it can't be accessed outside). - `{ list; }` No subshell is created. -### Functions +## Functions ```bash fname() { @@ -199,7 +197,7 @@ fname Any variables defined inside the function -#### Arguments +### Arguments ```bash fname() { @@ -209,7 +207,7 @@ fname() { fname "a" "b" ``` -#### Scope +### Scope ```bash var1='A' @@ -228,7 +226,7 @@ echo "var1: $var1, var2: $var2" # A, D Defining a variable inside the function overwrites the global scope. To prevent this, use the `local` keyword. -#### `return` +### `return` ```bash fname() { @@ -241,7 +239,7 @@ echo $? # 1 Use the `return` command to exit the function and return a value. -### Variables (Parameters) +## Variables (Parameters) ```bash name="Trude" @@ -254,7 +252,7 @@ echo ${name} Variables can be of any type, and grow to any needed size. -#### Special Variables +### Special Variables - `$*` Expands to every positional parameter: `$1$2$3`. - `$@` Expands to every positional parameter, separated by spaces: `"$1" "$2" "$3"`. @@ -265,21 +263,21 @@ Variables can be of any type, and grow to any needed size. - `$!` Process ID of the latest job placed into the background. - `$0` Name of the shell or script. -### Shell Expansions +## Shell Expansions -#### Brace Expansion +### Brace Expansion ```bash echo a{d,c,b}e # ade ace abe ``` -#### Tilde Expansion +### Tilde Expansion - `~` = `$HOME` - `~+` = `$PWD` - `~-` = `$OLDPWD` -#### Shell Parameter Expansion +### Shell Parameter Expansion - `${var}` Braces are required if the variable is positional and over one digit, or if it is followed by a character that is not part of its name. - `${!var}` Access the value of `var`, and checks if it is the name of another variable. If so, expands that variable. (Pointer) @@ -316,7 +314,7 @@ echo a{d,c,b}e # ade ace abe - `a` Output the flags corresponding to `var`'s attributes. - `k` Same as `K`, but separates keys and values using spaces, making it easier to loop through them. -#### Command Substitution +### Command Substitution ```bash echo "$(command)" @@ -325,7 +323,7 @@ echo "`command`" Execute a command and substitute itself with the command's result. -#### Arithmetic Expansion +### Arithmetic Expansion ```bash echo "$(( expression ))" @@ -333,7 +331,7 @@ echo "$(( expression ))" Performs an arithmetic expression and substitutes itself with the result. -#### Process Substitution +### Process Substitution ```bash cat <(command) >(command) @@ -347,7 +345,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary > Neither `cat` nor `gzip` are `bash` commands ('builtins'), but external programs. -#### Pattern Matching +### Pattern Matching - `*` Matches any string. - `?` Matches any single character. @@ -358,7 +356,7 @@ The `>()` substitution is also executed asynchronously, and creates a temporary - `@(pattern-list)` Matches one of the given patterns. - `!(pattern-list)` Matches anything except the given patterns. -### Redirections +## Redirections - `command > dest` Redirect the output of a command to a destination: A file, device, command, list, etc. - `command >> dest` Append to the destination instead of overwriting. @@ -379,12 +377,12 @@ string EOF ``` -### Shell Builtin Commands +## Shell Builtin Commands This section is an introduction to every command available in `bash`. To learn more about some command, run `help command`. -#### Bourne Shell Commands +### Bourne Shell Commands - `: arguments` Do nothing beyond expanding arguments and performing redirections. - `. file` Read and execute commands from `file`. @@ -419,7 +417,7 @@ To learn more about some command, run `help command`. - `umask` Defines which permissions should be removed from newly created files. - `unset` Remove a variable or function name. (Use `-f` to remove the actual function definition) -#### Bash Commands +### Bash Commands - `alias` Prints the list of aliases or defines new ones (with `alias name=value`). - `bind` Displays or sets key and function bindings for Readline. @@ -444,9 +442,9 @@ To learn more about some command, run `help command`. - `ulimit` Controls resource limits for processes created by the shell. - `unalias` Removes defined aliases, with an option to remove all. -### Shell Variables +## Shell Variables -#### Bourne Shell Variables +### Bourne Shell Variables - `CDPATH` Search path directories for the `cd` command. - `HOME` Current user's home directory, default for `cd`. @@ -459,7 +457,7 @@ To learn more about some command, run `help command`. - `PS1` Primary prompt string displayed interactively. - `PS2` Secondary prompt string for continued commands. -#### Bash Variables +### Bash Variables - `_` Pathname of invoked shell/script, or last argument of previous command. - `BASH` Full pathname used to execute the current Bash instance. @@ -559,7 +557,7 @@ To learn more about some command, run `help command`. - `TMPDIR` Directory used for creating temporary files. - `UID` Numeric real user ID of the current user (readonly). -### Parse Arguments +## Parse Arguments ```bash while getopts "ab:c" opt; do @@ -673,5 +671,21 @@ Arithmetic is performed using `(())`, `let` and `declare -i`. - `=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` Assignment - `expr1 , expr2` Comma +## Arrays + +### Indexed arrays + +```bash +declare -a name +name[]=value +``` + +### Associative arrays + +```bash +declare -A name +name=(value1 value2 ...) +``` + 6.7 Arrays -https://www.gnu.org/software/bash/manual/bash.html#Bash-Features +https://www.gnu.org/software/bash/manual/bash.html#Bash-Features \ No newline at end of file diff --git a/content/drafts/rust.md b/content/drafts/rust.md index 539be96..4b4a23e 100644 --- a/content/drafts/rust.md +++ b/content/drafts/rust.md @@ -1,50 +1,133 @@ --- -title: "rust" +title: Rust description: draft: true tags: author: TrudeEH showToc: true +summary: --- -## Vocabulary - -|Command / Word|Action / Meaning|Example| -|---|---|---| -|Statement|Performs an action, but does not return a value.|Function definitions, code that ends with `;`.| -|Expression|Evaluate to a resultant value.|Tests, math.| - ## Tools -- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf | sh` - - `rustup` - - `rustc` - - `cargo` +- Install Rust: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` + - `rustup` + - `rustc` + - `cargo` -## Hello World! +## Hello World ```rust fn main() { - println!("Hello world!"); + println!("Hello world!"); // Macro to print text } ``` -## Variables +## Comments + +- *Regular comments* which are ignored by the compiler: + - `// Line comments which go to the end of the line.` + - `/* Block comments which go to the closing delimiter. */` +- *Doc comments* which are parsed into HTML library documentation: + - `/// Generate library docs for the following item.` + - `//! Generate library docs for the enclosing item.` + +## Formatted Print + +## Primitives (Variables) + +### Scalar Types + +- Signed integers (default to `i32`): `i8`, `i16`, `i32`, `i64`, `i128` and `isize` (pointer size) +- Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128` and `usize` (pointer size) +- Floating point (default to `f64`): `f32`, `f64` +- `char` Unicode scalar values like `'a'`, `'α'` and `'∞'` (4 bytes each) +- `bool` either `true` or `false` +- The unit type `()`, whose only possible value is an empty tuple: `()` + +### Compound Types + +- Arrays: `[1, 2, 3]` +- Tuples: `(1, true)` + +### Examples + +Variables can either be *type annotated*, or infered by context. By default, a variable is always constant, and can be made mutable with the `mut` keyword. After creating a variable, its data type cannot be changed. ```rust -let x: i32; // A variable can only be used if it has been initialized (contains a value) - -let y: i8 = 5; // All variables are constant by default. -let mut z = 1; // The mut keyword makes the variable mutable. (Explicit type annotation is not necessary, but recommended). - -let (k, f); //Same as "let k; let f;" - -let t = { // Initialize a variable as the result of an expression. - let squared = y * y; - squared -}; +fn main() { + let x: i32; // Declare a variable + + let a_float: f64 = 1.0; // Regular annotation + let an_integer = 5i32; // Suffix annotation + + let default_float = 3.0; // `f64` + let default_integer = 7; // `i32` + + // A type can also be inferred from context. + let mut inferred_type = 12; // Type i64 is inferred from another line. + inferred_type = 4294967296i64; + + let mut mutable = 12; // Mutable `i32` + mutable = 21; + + // Variables can be overwritten with shadowing. + let mutable = true; + + // Array signature consists of Type T and length as [T; length]. + let my_array: [i32; 5] = [1, 2, 3, 4, 5]; + + // Tuple is a collection of values of different types + // and is constructed using parentheses (). + let my_tuple = (5u32, 1u8, true, -5.04f32); + + let (k, f); //Same as "let k; let f;" + + let t = { // Initialize a variable as the result of an expression. + let squared = y * y; + squared + }; +} ``` +> A variable can only be used after it has been initialized (contains a value). + +### Literals and Operators + +Integers can also be expressed using hexadecimal `0x`, octal `0o` or binary `0b`. +To improve readability, `_` can be added to numbers: `1_000` is the same as `1000`. +Scientific e-notation such as `1e6`, `7.4e-4` is also supported, and defaults to `f64`. + +The operators available and their precedence are similar to `C`: + +```Rust +fn main() { + println!("1 + 2 = {}", 1u32 + 2); // Integer addition + println!("1 - 2 = {}", 1i32 - 2); // Integer subtraction + // Changing `1i32` to `1u32` would causa an integer underflow. + + // Scientific notation + println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3); + + // Short-circuiting boolean logic + println!("true AND false is {}", true && false); + println!("true OR false is {}", true || false); + println!("NOT true is {}", !true); + + // Bitwise operations + println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101); + println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101); + println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101); + println!("1 << 5 is {}", 1u32 << 5); + println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2); + + // Use underscores to improve readability! + println!("One million is written as {}", 1_000_000u32); +} +``` + +--- + ## Data Types ### Integer Types diff --git a/content/notes/compiling.md b/content/notes/compiling.md index 992ff9e..85b0b35 100644 --- a/content/notes/compiling.md +++ b/content/notes/compiling.md @@ -64,16 +64,50 @@ gcc -o hello hello.c -lcs50 ## Make `Make` Is a build automation tool that automates the process of compiling, linking and building executables. -An example `Makefile` could look like the following: +An example `Makefile` (for an app using `GTK4`) could be as follows: ```Makefile -hello: - gcc -o hello hello.c -lcs50 +CC = gcc +CFLAGS = $(shell pkg-config --cflags gtk4) +LIBS = $(shell pkg-config --libs gtk4) + +hello: hello.c + $(CC) $(CFLAGS) -o hello hello.c $(LIBS) -O2 + +debug: hello.c + $(CC) $(CFLAGS) -o hello hello.c $(LDFLAGS) -Wall -Wextra -Og -g + clean: rm -f hello ``` ```Shell make # Compiles hello.c +make debug # Compiles hello.c using debug flags make clean # Removes the executable (hello) generated by the make command. ``` + +## Meson + +While `make` automates compiling, on larger projects, makefiles can grow hard to read quickly, and dependency resolution has to be done using external tools (such as `pkg-config`). + +Meson is a more modern alternative, which is faster, and uses declarative syntax (describe *what* to build, instead of *how* to build it). Meson can automatically manage dependencies, header files, and compiler flags. + +When `meson` is executed, it generates a `ninja.build` file, which is then parsed by `ninja`, a lower-level build tool. + +```meson.build +project('hello', 'c') + +gtk4_dep = dependency('gtk4') + +executable('hello', 'hello.c', + dependencies: gtk4_dep) +``` + +```bash +mkdir build +meson setup build # Generate files for Ninja in the build dir +meson compile -C build # Compile the project (same as `ninja -C build`) +./build/hello +ninja clean -C build # Clean executable files in the build directory +``` diff --git a/content/notes/index/index.md b/content/notes/index/index.md index 2187906..d147afd 100644 --- a/content/notes/index/index.md +++ b/content/notes/index/index.md @@ -10,29 +10,30 @@ weight: "1" --- - [Building a Computer From Scratch](../how_to_computer/) -- [Operating Systems](../linux/) `Linux` -- Shell Scripting `BASH` - `Coming Soon!` -- [C](../c-language/) - - [Compiling](../compiling/) `Make` `GCC` `Clang` +- [Operating System](../linux/) `Linux` +- [C Language](../c-language/) + - [Algorithms & Data Structures](../algorithms_and_data/) + - [Compiling](../compiling/) `GCC` `Make` `Meson` - [Debugging](../gdb/) `GDB` - [Snippets](../c-snippets/) -- [Algorithms & Data Structures](../algorithms_and_data/) -- Rust Lang - `Coming Soon!` -- GUI Apps `GTK4` - `Coming Soon!` -- [Python](../python/) - - [Flask](../flask/) -- [Databases](../databases/) `SQL` +- GUI Apps + - Object-Oriented C `GObject` - `Coming Soon!` + - GUI Toolkit `GTK4` - `Coming Soon!` +- Shell Scripting `BASH` - `Coming Soon!` - Networking - [HTTP](../http/) `CURL` - [Encryption](../encryption/) `GPG` `Cryptsetup` - [HTTPS and SSL Certificates](../https-ssl-certs/) `Certbot` - [SSH](../ssh/) - [Firewall](../firewall/) `UFW` - - [IRC](../irc/) + - [Instant Messaging](../irc/) `IRC` - Web Development - [HTML](../html/) - CSS - `Coming Soon!` - JS - `Coming Soon!` +- [Python](../python/) + - [Flask](../flask/) +- [Databases](../databases/) `SQL` - Tools - [Version Control](../git/) `GIT` - [Password Manager](../pass/) `PASS` diff --git a/content/notes/linux/index.md b/content/notes/linux/index.md index 444d6bd..7c3270b 100644 --- a/content/notes/linux/index.md +++ b/content/notes/linux/index.md @@ -204,7 +204,7 @@ The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but ## Shell (`bash`) -The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for [its own programming language](../bash), which can be used to write scripts and automate tasks. +The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for its own programming language, which can be used to write scripts and automate tasks. ### Compiling @@ -910,7 +910,7 @@ Then, after the installation finishes, install your preferred desktop environmen ```bash sudo apt update -sudo apt install gnome-core # change to kde-plasma-desktop +sudo apt install gnome-core # or kde-plasma-desktop sudo systemctl reboot ```