|
| 1 | +#lang pollen |
| 2 | + |
| 3 | +◊page-init{} |
| 4 | +◊define-meta[page-title]{ASCII Table} |
| 5 | +◊define-meta[page-description]{Generate your own ASCII Table} |
| 6 | + |
| 7 | +Traditionally, a book of this sort has an ASCII Table appendix. This |
| 8 | +book does not. Instead, here are several short scripts, each of which |
| 9 | +generates a complete ASCII table. |
| 10 | + |
| 11 | +◊anchored-example[#:anchor "ascii1"]{A script that generates an ASCII |
| 12 | +table} |
| 13 | + |
| 14 | +◊example{ |
| 15 | +#!/bin/bash |
| 16 | +# ascii.sh |
| 17 | +# ver. 0.2, reldate 26 Aug 2008 |
| 18 | +# Patched by ABS Guide author. |
| 19 | + |
| 20 | +# Original script by Sebastian Arming. |
| 21 | +# Used with permission (thanks!). |
| 22 | + |
| 23 | +exec >ASCII.txt # Save stdout to file, |
| 24 | + #+ as in the example scripts |
| 25 | + #+ reassign-stdout.sh and upperconv.sh. |
| 26 | + |
| 27 | +MAXNUM=256 |
| 28 | +COLUMNS=5 |
| 29 | +OCT=8 |
| 30 | +OCTSQU=64 |
| 31 | +LITTLESPACE=-3 |
| 32 | +BIGSPACE=-5 |
| 33 | + |
| 34 | +i=1 # Decimal counter |
| 35 | +o=1 # Octal counter |
| 36 | + |
| 37 | +while [ "$i" -lt "$MAXNUM" ]; do # We don't have to count past 400 octal. |
| 38 | + paddi=" $i" |
| 39 | + echo -n "${paddi: $BIGSPACE} " # Column spacing. |
| 40 | + paddo="00$o" |
| 41 | +# echo -ne "\\${paddo: $LITTLESPACE}" # Original. |
| 42 | + echo -ne "\\0${paddo: $LITTLESPACE}" # Fixup. |
| 43 | +# ^ |
| 44 | + echo -n " " |
| 45 | + if (( i % $COLUMNS == 0)); then # New line. |
| 46 | + echo |
| 47 | + fi |
| 48 | + ((i++, o++)) |
| 49 | + # The octal notation for 8 is 10, and 64 decimal is 100 octal. |
| 50 | + (( i % $OCT == 0)) && ((o+=2)) |
| 51 | + (( i % $OCTSQU == 0)) && ((o+=20)) |
| 52 | +done |
| 53 | + |
| 54 | +exit $? |
| 55 | + |
| 56 | +# Compare this script with the "pr-asc.sh" example. |
| 57 | +# This one handles "unprintable" characters. |
| 58 | + |
| 59 | +# Exercise: |
| 60 | +# Rewrite this script to use decimal numbers, rather than octal. |
| 61 | +} |
| 62 | + |
| 63 | +◊anchored-example[#:anchor "ascii2"]{Another ASCII table script} |
| 64 | + |
| 65 | +◊example{ |
| 66 | +#!/bin/bash |
| 67 | +# Script author: Joseph Steinhauser |
| 68 | +# Lightly edited by ABS Guide author, but not commented. |
| 69 | +# Used in ABS Guide with permission. |
| 70 | + |
| 71 | +#------------------------------------------------------------------------- |
| 72 | +#-- File: ascii.sh Print ASCII chart, base 10/8/16 (JETS-2012) |
| 73 | +#------------------------------------------------------------------------- |
| 74 | +#-- Usage: ascii [oct|dec|hex|help|8|10|16] |
| 75 | +#-- |
| 76 | +#-- This script prints out a summary of ASCII char codes from Zero to 127. |
| 77 | +#-- Numeric values may be printed in Base10, Octal, or Hex. |
| 78 | +#-- |
| 79 | +#-- Format Based on: /usr/share/lib/pub/ascii with base-10 as default. |
| 80 | +#-- For more detail, man ascii . . . |
| 81 | +#------------------------------------------------------------------------- |
| 82 | + |
| 83 | +[ -n "$BASH_VERSION" ] && shopt -s extglob |
| 84 | + |
| 85 | +case "$1" in |
| 86 | + oct|[Oo]?([Cc][Tt])|8) Obase=Octal; Numy=3o;; |
| 87 | + hex|[Hh]?([Ee][Xx])|16|[Xx]) Obase=Hex; Numy=2X;; |
| 88 | + help|?(-)[h?]) sed -n '2,/^[ ]*$/p' $0;exit;; |
| 89 | + code|[Cc][Oo][Dd][Ee])sed -n '/case/,$p' $0;exit;; |
| 90 | + *) Obase=Decimal |
| 91 | +esac # CODE is actually shorter than the chart! |
| 92 | + |
| 93 | +printf "\t\t## $Obase ASCII Chart ##\n\n"; FM1="|%0${Numy:-3d}"; LD=-1 |
| 94 | + |
| 95 | +AB="nul soh stx etx eot enq ack bel bs tab nl vt np cr so si dle" |
| 96 | +AD="dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us sp" |
| 97 | + |
| 98 | +for TOK in $AB $AD; do ABR[$((LD+=1))]=$TOK; done; |
| 99 | +ABR[127]=del |
| 100 | + |
| 101 | +IDX=0 |
| 102 | +while [ $IDX -le 127 ] && CHR="${ABR[$IDX]}" |
| 103 | + do ((${#CHR}))&& FM2='%-3s'|| FM2=`printf '\\\\%o ' $IDX` |
| 104 | + printf "$FM1 $FM2" "$IDX" $CHR; (( (IDX+=1)%8))||echo '|' |
| 105 | + done |
| 106 | + |
| 107 | +exit $? |
| 108 | +} |
| 109 | + |
| 110 | +◊anchored-example[#:anchor "ascii3"]{A third ASCII table script, using |
| 111 | +awk} |
| 112 | + |
| 113 | +◊example{ |
| 114 | +#!/bin/bash |
| 115 | +# ASCII table script, using awk. |
| 116 | +# Author: Joseph Steinhauser |
| 117 | +# Used in ABS Guide with permission. |
| 118 | + |
| 119 | + |
| 120 | +#------------------------------------------------------------------------- |
| 121 | +#-- File: ascii Print ASCII chart, base 10/8/16 (JETS-2010) |
| 122 | +#------------------------------------------------------------------------- |
| 123 | +#-- Usage: ascii [oct|dec|hex|help|8|10|16] |
| 124 | +#-- |
| 125 | +#-- This script prints a summary of ASCII char codes from Zero to 127. |
| 126 | +#-- Numeric values may be printed in Base10, Octal, or Hex (Base16). |
| 127 | +#-- |
| 128 | +#-- Format Based on: /usr/share/lib/pub/ascii with base-10 as default. |
| 129 | +#-- For more detail, man ascii |
| 130 | +#------------------------------------------------------------------------- |
| 131 | + |
| 132 | +[ -n "$BASH_VERSION" ] && shopt -s extglob |
| 133 | + |
| 134 | +case "$1" in |
| 135 | + oct|[Oo]?([Cc][Tt])|8) Obase=Octal; Numy=3o;; |
| 136 | + hex|[Hh]?([Ee][Xx])|16|[Xx]) Obase=Hex; Numy=2X;; |
| 137 | + help|?(-)[h?]) sed -n '2,/^[ ]*$/p' $0;exit;; |
| 138 | + code|[Cc][Oo][Dd][Ee])sed -n '/case/,$p' $0;exit;; |
| 139 | + *) Obase=Decimal |
| 140 | +esac |
| 141 | +export Obase # CODE is actually shorter than the chart! |
| 142 | + |
| 143 | +awk 'BEGIN{print "\n\t\t## "ENVIRON["Obase"]" ASCII Chart ##\n" |
| 144 | + ab="soh,stx,etx,eot,enq,ack,bel,bs,tab,nl,vt,np,cr,so,si,dle," |
| 145 | + ad="dc1,dc2,dc3,dc4,nak,syn,etb,can,em,sub,esc,fs,gs,rs,us,sp" |
| 146 | + split(ab ad,abr,",");abr[0]="nul";abr[127]="del"; |
| 147 | + fm1="|%0'"${Numy:- 4d}"' %-3s" |
| 148 | + for(idx=0;idx<128;idx++){fmt=fm1 (++colz%8?"":"|\n") |
| 149 | + printf(fmt,idx,(idx in abr)?abr[idx]:sprintf("%c",idx))} }' |
| 150 | + |
| 151 | +exit $? |
| 152 | +} |
0 commit comments