aboutsummaryrefslogtreecommitdiffhomepage
path: root/print.html
diff options
context:
space:
mode:
authorjohannst <johannst@users.noreply.github.com>2020-09-19 13:57:06 +0000
committerjohannst <johannst@users.noreply.github.com>2020-09-19 13:57:06 +0000
commit28f631e02bbf7e4cbd763fc74c4941d3f55a030f (patch)
treea3633cec0067d5b2e2e97737855b62de90c054b0 /print.html
parenteaaab502c1a10032f4533ca013a39f1a83a7f82b (diff)
downloadnotes-28f631e02bbf7e4cbd763fc74c4941d3f55a030f.tar.gz
notes-28f631e02bbf7e4cbd763fc74c4941d3f55a030f.zip
deploy: 4a5cd61b7c536ecf1bdb288cb6d584c190b1f6c7
Diffstat (limited to 'print.html')
-rw-r--r--print.html82
1 files changed, 78 insertions, 4 deletions
diff --git a/print.html b/print.html
index 3d8d34d..e08e9c0 100644
--- a/print.html
+++ b/print.html
@@ -355,6 +355,42 @@ command 2&gt;&amp;1 &gt;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 &lt;optstring&gt; &lt;param&gt; [&lt;args&gt;]
+</code></pre>
+<ul>
+<li><code>&lt;optstring&gt;</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>&lt;param&gt;</code> specifies a variable name which <code>getopts</code> fills with the last parsed option argument</li>
+<li><code>&lt;args&gt;</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 &quot;f:c&quot; PARAM; do
+ case $PARAM in
+ f) echo &quot;GOT -f $OPTARG&quot;;;
+ c) echo &quot;GOT -c&quot;;;
+ *) echo &quot;ERR: print usage&quot;; 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 &lt;option&gt; &lt;word&gt;
# usefule options:
# -W &lt;list&gt; specify list of possible completions
@@ -390,7 +426,7 @@ compgen -W &quot;foo foobar bar&quot; &quot;f&quot;
# compare &quot;hom&quot; against file/dir names and generate matches
compgen -d -f &quot;hom&quot;
</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 &lt;file&gt; -h
@@ -1221,7 +1257,7 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel
-l &lt;filter&gt; . show who calls into lib matched by &lt;filter&gt;
-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, &quot;ax&quot;, @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, &quot;a&quot;, @progbits
+greeting:
+ .asciz &quot;Hi ASM-World!\n&quot;
+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">&gt; gcc -o greet greet.s -nostartfiles -nostdlib &amp;&amp; ./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>