From 28f631e02bbf7e4cbd763fc74c4941d3f55a030f Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 19 Sep 2020 13:57:06 +0000 Subject: deploy: 4a5cd61b7c536ecf1bdb288cb6d584c190b1f6c7 --- print.html | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) (limited to 'print.html') diff --git a/print.html b/print.html index 3d8d34d..e08e9c0 100644 --- a/print.html +++ b/print.html @@ -355,6 +355,42 @@ command 2>&1 >file
  • duplicate fd 1 to fd 2, effectively redirecting stderr to stdout
  • redirect stdout to file
  • +

    Argument parsing with getopts

    +

    The getopts builtin uses following global variables:

    + +
    getopts <optstring> <param> [<args>]
    +
    + +

    Example

    +
    #!/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
    +

    Completion

    The complete builtin is used to interact with the completion system.

    complete                    # print currently installed completion handler
    @@ -375,7 +411,7 @@ COMPREPLY       # array with possible completions
     

    The compgen builtin is used to generate possible matches by comparing word against words generated by option.

    -
    compgen [option] [word]
    +
    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"
     
    -

    Example

    +

    Example

    Skeleton to copy/paste for writing simple completions.

    Assume a program foo with the following interface:

    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
     
    -

    Example

    +

    Example

    List which program/libs call into libstdc++:

    ltrace -l '*libstdc++*' -C -o ltrace.log ./main
     
    @@ -1480,7 +1516,7 @@ void run1(int x) {
  • run0: bar is on the path without branch
  • run1: foo is on the path without branch
  • -
    run0:
    +
    run0:
             test    edi, edi
             jne     .L4
             xor     eax, eax
    @@ -1842,6 +1878,44 @@ LO ADDR
     
    +

    ASM skeleton

    +

    Small assembler skeleton, ready to use with following properties:

    +
      +
    • use raw Linux syscalls (man 2 syscall for ABI)
    • +
    • no C runtime (crt)
    • +
    • gnu assembler gas
    • +
    • intel syntax
    • +
    +
    # 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
    +
    +
    +

    Syscall numbers are defined in /usr/include/asm/unistd.h.

    +
    +

    To compile and run:

    +
    > gcc -o greet greet.s -nostartfiles -nostdlib && ./greet
    +Hi ASM-World!
    +

    References