#!/usr/skunk/bin/gawk -f #!/usr/bin/awk -f # @(#) asc.gawk 1.0 94/01/08 # asc: print ascii values of characters # Use gawk because its printf "%c" works the way we want. # Yes, this is pretty gross... # 91/10/17 john h. dubois iii (john@armory.com) # 92/02/16 added help # 92/05/01 changed to #!gawk script # 92/08/04 changed to use a function # 94/01/01 Added all options. # 94/01/08 Read from stdin if no data given on cmd line. BEGIN { Name = "asc" Usage = "Usage: " Name " [-hbox] [-r] ..." ARGC = Opts(Name,Usage,"hboxr>",0) if ("h" in Options) { print \ Name ": print the ASCII values of characters.\n" \ Usage "\n"\ "The values of the characters in each word are printed together on a line,\n"\ "separated by spaces. If more than one word is given, the values for each\n"\ "are printed on separate lines. If no characters are given on the command\n"\ "line, they are read from the standard input. A blank line is printed\n"\ "between the converted output for each line of input.\n"\ "Options:\n"\ "-h: Print this help.\n"\ "-b: Print values in binary.\n"\ "-o: Print values in octal.\n"\ "-x: Print values in hex.\n"\ "-r: Print values in radix (base) " exit(0) } if ("b" in Options) Base = 2 else if ("o" in Options) Base = 8 else if ("x" in Options) Base = 16 else if ("r" in Options) Base = Options["r"] else Base = 10 if (ARGC < 2) { while (getline == 1) { printf newline # print newline before each group except first for (i = 1; i <= NF; i++) ConvertWord($i,Base) newline = "\n" } } else for (i = 1; i < ARGC; i++) ConvertWord(ARGV[i],Base) } function ConvertWord(s,Base) { len = length(s) for (n = 1; n <= len; n++) printf itoa(asc(substr(s,n,1)),Base) " " print "" } # Converts integer inval to string representation in base radix & returns it. function itoa(inval,radix, Buf,value,neg,dig) { if (!(2 <= radix && radix <= 36)) return "" if (neg = (inval < 0)) value = -inval else value = inval if (value == 0) Buf = "0" while (value > 0) { if ((dig = value % radix) > 9) # Add digit value to 'a' - 10 Buf = sprintf("%c",dig + 87) Buf else # Add digit value to '0' Buf = sprintf("%c",dig + 48) Buf value = int(value / radix) } if (neg) Buf = "-" Buf return Buf } function asc(InC, c,ascval,b) { ascval = 128 b = 128 while ((c = sprintf("%c",ascval)) != InC) if (c < InC) ascval += (b /= 2) else ascval -= (b /= 2) return ascval } # @(#) ProcArgs 1.1 94/01/01 # 92/02/29 john h. dubois iii # 93/07/18 Added "#" arg type # 93/09/26 Don't count -h against MinArgs # 94/01/01 Stop scanning at first non-option arg. Added '>' option type. # Removed meaning of '+' or '-' by itself. # optlist is a string which contains all of the possible command line options. # If a character is followed by a colon, # it indicates that that option takes an argument. # If a character is followed by a pound sign (#), # it indicates that that option takes an integer argument. # Strings in argv[] which begin with "-" or "+" are taken to be # strings of options, except that a string which consists solely of "-" # or "+" is taken to be a non-option string; like other non-option strings, # it stops the scanning of argv and is left in argv[]. # If an option takes an argument, the argument may either immedately # follow it or be given separately. # If an option that does not take an argument is given, # an index with its name is created in options and its value is set to "1". # If an option that does take an argument is given, # an index with its name is created in options and its value # is set to the value of the argument given for it. # Options and their arguments are deleted from argv. # Note that this means that there may be gaps left in the indices of argv[]. # If compress is nonzero, argv[] is packed by moving its elements so that # they have contiguous integer indices starting with 0. # argv[0] is not examined. # An argument of "--" or "++" stops the scanning of argv[]. # The number of arguments left in argc is returned. # If an error occurs, the string OptErr is set to an error message and -1 is # returned. function ProcArgs(argc,argv,optlist,options,compress, ArgNum,ArgsLeft,Arg,ArgLen,ArgInd,Option,Pos) { # ArgNum is the index of the argument being processed. # ArgsLeft is the number of arguments left in argv. # Arg is the argument being processed. # ArgLen is the length of the argument being processed. # ArgInd is the position of the character in Arg being processed. # Option is the character in Arg being processed. # Pos is the position in optlist of the option being processed. ArgsLeft = argc for (ArgNum = 1; ArgNum < argc; ArgNum++) { if ((Arg = argv[ArgNum]) !~ /^[-+]./) break delete argv[ArgNum] ArgsLeft-- if ((Arg == "--") || (Arg == "++")) break ArgLen = length(Arg) for (ArgInd = 2; ArgInd <= ArgLen; ArgInd++) { Option = substr(Arg,ArgInd,1) Pos = index(optlist,Option) if (!Pos) { OptErr = "Invalid option: -" Option return -1 } # If option takes a value... if ((ArgType = substr(optlist,Pos + 1,1)) ~ "[:#>]") { if (ArgInd < ArgLen) # Value is included with option options[Option] = substr(Arg,ArgInd + 1) else { # Value is the next arg after option if (ArgNum < (argc - 1)) { options[Option] = argv[++ArgNum] delete argv[ArgNum] ArgsLeft-- } else { OptErr = "Option -" Option " requires an argument" return -1 } } if (ArgType == "#" && options[Option] !~ "^-?[0-9]+$") { OptErr = \ "Option -" Option " requires an integer argument" return -1 } else if (ArgType == ">" && ( options[Option] !~ "^[0-9]+$" || options[Option] ~ "^0+$")) { OptErr = \ "Option -" Option " requires a positive integer argument" return -1 } break # Used up this option } else options[Option] = 1 } } if (compress != 0) PackArr(argv,ArgsLeft) return ArgsLeft } # Packs Arr to indices starting with 0 # Num should be the number of elements in Arr function PackArr(Arr,Num, NewInd,OldInd) { NewInd = OldInd = 0 for (; Num; Num--) { while (!(OldInd in Arr)) OldInd++ if (NewInd != OldInd) { Arr[NewInd] = Arr[OldInd] delete Arr[OldInd] } OldInd++ NewInd++ } } # Opts: Process command line arguments. # Opts processes command line arguments using ProcArgs() # and checks for errors. If an error occurs, a message is printed # and the program is exited. # # Input variables: # Name is the name of the program, for error messages. # Usage is a usage message, for error messages. # OptList the option description string, as used by ProcArgs(). # MinArgs is the minimum number of non-option arguments that this # program should have, non including ARGV[0] and +h. # If the program does not require any non-option arguments, # MinArgs should be omitted or given as 0. # Global variables: # The command line arguments are taken from ARGV[]. # The arguments that are option specifiers and values are removed from # ARGV[], leaving only ARGV[0] and the non-option arguments. # The number of elements in ARGV[] should be in ARGC. # After processing, ARGC is set to the number of elements left in ARGV[]. # The option values are put in Options[]. # On error, Err is set to 1 so it can be checked for in an END block. # Return value: The number of elements left in ARGV is returned. function Opts(Name,Usage,OptList,MinArgs, ArgsLeft) { if (MinArgs == "") MinArgs = 0 ArgsLeft = ProcArgs(ARGC,ARGV,OptList,Options,1) if ((ArgsLeft + ("h" in Options)) < (MinArgs+1)) { if (ArgsLeft != -1) OptErr = "Not enough arguments" print Name ": " OptErr ". Use -h for help." print Usage Err = 1 exit 1 } return ArgsLeft }