diff options
author | johannst <johannst@users.noreply.github.com> | 2020-09-19 13:57:06 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2020-09-19 13:57:06 +0000 |
commit | 28f631e02bbf7e4cbd763fc74c4941d3f55a030f (patch) | |
tree | a3633cec0067d5b2e2e97737855b62de90c054b0 /print.html | |
parent | eaaab502c1a10032f4533ca013a39f1a83a7f82b (diff) | |
download | notes-28f631e02bbf7e4cbd763fc74c4941d3f55a030f.tar.gz notes-28f631e02bbf7e4cbd763fc74c4941d3f55a030f.zip |
deploy: 4a5cd61b7c536ecf1bdb288cb6d584c190b1f6c7
Diffstat (limited to 'print.html')
-rw-r--r-- | print.html | 82 |
1 files changed, 78 insertions, 4 deletions
@@ -355,6 +355,42 @@ command 2>&1 >file <li>duplicate <code>fd 1</code> to <code>fd 2</code>, effectively redirecting <code>stderr</code> to <code>stdout</code></li> <li>redirect <code>stdout</code> to <code>file</code></li> </ol> +<h2><a class="header" href="#argument-parsing-with-getopts" id="argument-parsing-with-getopts">Argument parsing with <code>getopts</code></a></h2> +<p>The <code>getopts</code> builtin uses following global variables:</p> +<ul> +<li><code>OPTARG</code>, value of last option argument</li> +<li><code>OPTIND</code>, index of the next argument to process (user must reset)</li> +<li><code>OPTERR</code>, display errors if set to <code>1</code> </li> +</ul> +<pre><code class="language-bash">getopts <optstring> <param> [<args>] +</code></pre> +<ul> +<li><code><optstring></code> specifies the names of supported options, eg <code>f:c</code> +<ul> +<li><code>f:</code> means <code>-f</code> option with an argument</li> +<li><code>c</code> means <code>-c</code> option without an argument</li> +</ul> +</li> +<li><code><param></code> specifies a variable name which <code>getopts</code> fills with the last parsed option argument</li> +<li><code><args></code> optionally specify argument string to parse, by default <code>getopts</code> parses <code>$@</code></li> +</ul> +<h3><a class="header" href="#example-1" id="example-1">Example</a></h3> +<pre><code class="language-bash">#!/bin/bash +function parse_args() { + while getopts "f:c" PARAM; do + case $PARAM in + f) echo "GOT -f $OPTARG";; + c) echo "GOT -c";; + *) echo "ERR: print usage"; exit 1;; + esac + done + # users responsibility to reset OPTIND + OPTIND=0 +} + +parse_args -f xxx -c +parse_args -f yyy +</code></pre> <h2><a class="header" href="#completion-1" id="completion-1">Completion</a></h2> <p>The <code>complete</code> builtin is used to interact with the completion system.</p> <pre><code class="language-bash">complete # print currently installed completion handler @@ -375,7 +411,7 @@ COMPREPLY # array with possible completions </code></pre> <p>The <code>compgen</code> builtin is used to generate possible matches by comparing <code>word</code> against words generated by <code>option</code>.</p> -<pre><code class="language-bash">compgen [option] [word] +<pre><code class="language-bash">compgen <option> <word> # usefule options: # -W <list> specify list of possible completions @@ -390,7 +426,7 @@ compgen -W "foo foobar bar" "f" # compare "hom" against file/dir names and generate matches compgen -d -f "hom" </code></pre> -<h3><a class="header" href="#example-1" id="example-1">Example</a></h3> +<h3><a class="header" href="#example-2" id="example-2">Example</a></h3> <p>Skeleton to copy/paste for writing simple completions.</p> <p>Assume a program <code>foo</code> with the following interface:</p> <pre><code class="language-bash">foo -c green|red|blue -s low|high -f <file> -h @@ -1221,7 +1257,7 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel -l <filter> . show who calls into lib matched by <filter> -C .......... demangle </code></pre> -<h1><a class="header" href="#example-2" id="example-2">Example</a></h1> +<h1><a class="header" href="#example-3" id="example-3">Example</a></h1> <p>List which program/libs call into <code>libstdc++</code>:</p> <pre><code class="language-bash">ltrace -l '*libstdc++*' -C -o ltrace.log ./main </code></pre> @@ -1480,7 +1516,7 @@ void run1(int x) { <li><code>run0</code>: <code>bar</code> is on the path without branch</li> <li><code>run1</code>: <code>foo</code> is on the path without branch</li> </ul> -<pre><code class="language-c">run0: +<pre><code class="language-x86asm">run0: test edi, edi jne .L4 xor eax, eax @@ -1842,6 +1878,44 @@ LO ADDR </code></pre> </li> </ul> +<h2><a class="header" href="#asm-skeleton" id="asm-skeleton">ASM skeleton</a></h2> +<p>Small assembler skeleton, ready to use with following properties:</p> +<ul> +<li>use raw Linux syscalls (<code>man 2 syscall</code> for ABI)</li> +<li>no <code>C runtime (crt)</code></li> +<li>gnu assembler <a href="https://sourceware.org/binutils/docs/as"><code>gas</code></a></li> +<li>intel syntax</li> +</ul> +<pre><code class="language-x86asm"># file: greet.s + + .intel_syntax noprefix + + .section .text, "ax", @progbits + .global _start +_start: + mov rdi, 1 # fd + lea rsi, [rip + greeting] # buf + mov rdx, [rip + greeting_len] # count + mov rax, 1 # write(2) syscall nr + syscall + + mov rdi, 0 # exit code + mov rax, 1 # exit(2) syscall nr + syscall + + .section .rdonly, "a", @progbits +greeting: + .asciz "Hi ASM-World!\n" +greeting_len: + .int .-greeting +</code></pre> +<blockquote> +<p>Syscall numbers are defined in <code>/usr/include/asm/unistd.h</code>.</p> +</blockquote> +<p>To compile and run:</p> +<pre><code class="language-bash">> gcc -o greet greet.s -nostartfiles -nostdlib && ./greet +Hi ASM-World! +</code></pre> <h2><a class="header" href="#references" id="references">References</a></h2> <ul> <li><a href="https://www.uclibc.org/docs/psABI-x86_64.pdf">SystemV AMD64 ABI</a></li> |