home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / awk / phf.zip / PHF.AWK
Text File  |  1989-12-08  |  3KB  |  119 lines

  1. #
  2. # phfind.awk -- do searches against phone/address database
  3. #
  4. # required file format: comma delimited fields, \n delimited records.
  5. #
  6. #            $1 == name  (any notation, no commas)
  7. #            $2 == phone (any notation, no commas: e.g. (111) 222-3333 or
  8. #                         111-222-3333)
  9. #            $3 and greater == anything at all, commas allowed
  10. #
  11. # example legal entries:
  12. #
  13. #    post emily, 111-222-3333, what, great, manners
  14. #    george, (111) 222 3333
  15. #    emily carr, , 12 Boatswain Lane, Essex
  16. #
  17. #
  18. # author: Richard J. Reiner, rreiner@vm1.yorku.ca, rreiner@yorkvm1.bitnet,
  19. # grad3077@writer.yorku.ca.  28 Nov 89.
  20. #
  21. # License for personal non-commercial use is granted.  Any commercial
  22. # industrial, or governmental use  is forbidden without express permission
  23. # from the author.
  24. #
  25. # Written and tested with Rob Duff's AWK v2.12.
  26. #
  27.  
  28. BEGIN \
  29.     {
  30.         FS=","
  31.  
  32.         parseargs()
  33.  
  34.         dialprefix="ATDP"    # define modem command strings
  35.         dialsuffix="\n"
  36.     }
  37.  
  38. $0 ~ s \
  39.     {
  40.         if (f != 0)            # full record is wanted
  41.         {
  42.             printf("%s\n",$0)
  43.         }
  44.         else if (d != 0)    # dial command wanted
  45.         {
  46.             printf("%s%s%s",dialprefix,cleanphonenum($2),dialsuffix)
  47.         }
  48.         else                # default: name and phone number only
  49.         {
  50.             printf("%s's phone number is %s\n",$1,$2)
  51.         }
  52.  
  53.         if(++hits == n)        # abort if max reached
  54.             exit(0)
  55.     }
  56.  
  57.  
  58.  
  59. # read and parse argv.  i is local.
  60. function parseargs(i) \
  61. {
  62.     if (ARGC < 2)                # we don't have even a -s
  63.         usageandexit()
  64.  
  65.     for(i=ARGC-1; i>0; i--)
  66.     {
  67.         if (ARGV[i]=="-d")
  68.             d=1
  69.         else if (ARGV[i]=="-f")
  70.             f=1
  71.         else if (index(ARGV[i],"-s")==1)
  72.             s=substr(ARGV[i],3)
  73.         else if (index(ARGV[i],"-n")==1)
  74.             n=substr(ARGV[i],3)
  75.         else
  76.         {
  77.             printf("bad option: %s\n",ARGV[i])
  78.             usageandexit()
  79.         }
  80.     }
  81.  
  82.     ARGC=1                        # don't try to open switches as files
  83.  
  84.     # make n==1 the default.  Also, force n==1 if we have -d.
  85.     if ((d != 0) || (n == 0))
  86.         n=1
  87. }
  88.         
  89.  
  90. # strip nonsense out of phone numbers, for dialer.  Also kill area code,
  91. # so we don't get accidental LD calls.
  92. function cleanphonenum(ph) \
  93. {
  94.     gsub("[ ()-]","",ph)        # remove spaces, lparens, rparens, hyphens
  95.  
  96.     if ((length(ph)!=7) && (length(ph)!=10))    # wrong number of digits or blank
  97.         return("malformed or unknown")
  98.     else
  99.     {
  100.         if (length(ph) == 10)
  101.         {
  102.             ph=substr(ph, 4)    # remove area code
  103.         }
  104.     }
  105.     return (ph)
  106. }
  107.  
  108.  
  109. # show usage and then die with error status
  110. function usageandexit() \
  111. {
  112.     printf("usage:  awk phf {options} <addrfile\n")
  113.     printf("options: -sEXP  expression to match\n")
  114.     printf("         -nNUM  stop after printing n entries (default 1)\n")
  115.     printf("         -d     print modem dial command\n")
  116.     printf("         -f     print full record\n")
  117.     exit(1)
  118. }
  119.