home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / ARCHIE-1.2 / ARCHIE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-04  |  7.4 KB  |  288 lines

  1. /*
  2.  * Copyright (c) 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. #include <copyright.h>
  9.  
  10. /*
  11.  * Archie client using the Prospero protocol.
  12.  *
  13.  * Suggestions and improvements to Brendan Kehoe (brendan@cs.widener.edu).
  14.  */
  15.  
  16. #include <stdio.h>
  17. #if defined(OS2)
  18. # include <pctcp.h>
  19. #endif
  20. #ifdef MSDOS
  21. # include <string.h>
  22. # include <stdlib.h>
  23. # ifdef CUTCP
  24. #  include <msdos/cutcp.h>
  25. #  include <msdos/hostform.h>
  26. /* The default stack size for a BC program is 4k; jack it up to 16 and add the
  27.    Check for Stack Overflow option to the compiler.  */
  28. extern unsigned _stklen = 16 * 1024;
  29. # endif
  30. #endif
  31.  
  32. #include <pfs.h>
  33. #include <rdgram.h>
  34. #include <archie.h>
  35. #include <pmachine.h>
  36.  
  37. int        listflag = 0;
  38. int        sortflag = 0;   /* 1 = by date                    */
  39. char        *progname;
  40. #ifdef DEBUG
  41. extern int    pfs_debug;
  42. #endif
  43. extern int    rdgram_priority;
  44.  
  45. /* To keep the code clean.. */
  46. #ifdef VMS
  47. # define NFLAG    "\"N\""
  48. # define LFLAG    "\"L\""
  49. # define A_EXIT    SS$_NORMAL
  50. #else
  51. # define NFLAG    "N"
  52. # define LFLAG    "L"
  53. # define A_EXIT    1
  54. #endif
  55. #ifdef CUTCP
  56. # define HFLAG    "[H config.tel]]"
  57. #else
  58. # define HFLAG    "]"
  59. #endif
  60.  
  61. main(argc,argv)
  62.     int        argc;
  63.     char    *argv[];
  64.     {
  65.     char        *cur_arg;
  66.     char        qtype = '=';    /* Default to exact string match  */
  67.     char        etype = '=';    /* Type if only -e is specified   */
  68.     int        eflag = 0;    /* Exact flag specified          */
  69.     int        max_hits = MAX_HITS;
  70.     int        offset = 0;
  71.     int        exitflag = 0;    /* Display release identifier     */
  72.     int        tmp;
  73.     char        *host = ARCHIE_HOST;
  74.     char        *p;
  75.     static char *archies[] = { ARCHIES };
  76. #ifdef CUTCP
  77.     int        hostset = 0;
  78. #endif
  79.     char        *getenv();
  80.  
  81.     progname = *argv;
  82.     argc--; argv++;
  83.  
  84. #ifdef    CUTCP
  85.     if(getenv("CONFIGTEL"))
  86.         if(Shostfile(getenv("CONFIGTEL")) < 0) {
  87.             fprintf(stderr,"Error, couldn't open configtel file %s\n",
  88.                 getenv("CONFIGTEL"));
  89.             exit(1);
  90.         }
  91. #endif
  92.  
  93.     if ((p = getenv("ARCHIE_HOST")) != (char *)NULL)
  94.       host = p;
  95.         
  96.     while (argc > 0 && **argv == '-') {
  97.         cur_arg = argv[0]+1;
  98.  
  99.         /* If a - by itself, or --, then no more arguments */
  100.         if(!*cur_arg || ((*cur_arg == '-') && (!*(cur_arg+1)))) {
  101.             argc--, argv++;
  102.         goto scandone;
  103.         }
  104.  
  105.         while (*cur_arg) {
  106.         switch (*cur_arg++) {
  107. #ifdef DEBUG        
  108.         case 'D':  /* Debug level */
  109.             pfs_debug = 1; /* Default debug level */
  110.             if(*cur_arg && index("0123456789",*cur_arg)) {
  111.             sscanf(cur_arg,"%d",&pfs_debug);
  112.             cur_arg += strspn(cur_arg,"0123456789");
  113.             }
  114.             else if(argc > 2) {
  115.                 tmp = sscanf(argv[1],"%d",&pfs_debug);
  116.             if (tmp == 1) {argc--;argv++;}
  117.             }
  118.             break;
  119. #endif
  120. #ifdef    CUTCP
  121.         case 'H' :
  122.             if(Shostfile(argv[1]) < 0) {
  123.                 fprintf(stderr,"Error, couldn't open configtel file %s\n",argv[1]);
  124.                 exit(1);
  125.             }
  126.              argc--;argv++;
  127.             break;
  128. #endif
  129. #ifndef XARCHIE
  130.         case 'L':
  131.             printf("Known archie servers:\n");
  132.             for (tmp = 0; tmp < NARCHIES; tmp++)
  133.             printf("\t%s\n", archies[tmp]);
  134.             printf(" * %s is the default Archie server.\n", ARCHIE_HOST);
  135.             printf(" * For the most up-to-date list, write to an Archie server and give it\n   the command `servers'.\n");
  136.             exitflag = 1;
  137.             break;
  138. #endif
  139.  
  140.         case 'N':  /* Priority (nice) */
  141.             rdgram_priority = RDGRAM_MAX_PRI; /* Use this if no # */
  142.             if(*cur_arg && index("-0123456789",*cur_arg)) {
  143.             sscanf(cur_arg,"%d",&rdgram_priority);
  144.             cur_arg += strspn(cur_arg,"-0123456789");
  145.             }
  146.             else if(argc > 2) {
  147.                 tmp = sscanf(argv[1],"%d",&rdgram_priority);
  148.             if (tmp == 1) {argc--;argv++;}
  149.             }
  150.             if(rdgram_priority > RDGRAM_MAX_SPRI) 
  151.             rdgram_priority = RDGRAM_MAX_PRI;
  152.             if(rdgram_priority < RDGRAM_MIN_PRI) 
  153.             rdgram_priority = RDGRAM_MIN_PRI;
  154.               break;
  155.  
  156.         case 'c':  /* substring (case sensitive) */
  157.             qtype = 'C';
  158.             etype = 'c';
  159.             break;
  160.  
  161.         case 'e':  /* Exact match */
  162.             /* If -e specified by itself, then we use the  */
  163.             /* default value of etype which must be '='    */
  164.             eflag++;
  165.             break;
  166.  
  167.         case 'h':  /* Host */
  168.             host = argv[1];
  169. #ifdef CUTCP
  170.             hostset++;
  171. #endif
  172.             argc--; argv++;
  173.             break;
  174.  
  175.         case 'l':  /* List one match per line */
  176.             listflag++;
  177.             break;
  178.  
  179.         case '0': case '1': case '2': case '3': case '4':
  180.         case '5': case '6': case '7': case '8': case '9':
  181.             cur_arg--;
  182.         case 'm':  /* Max hits */
  183.             max_hits = -1;  
  184.             if(*cur_arg && index("0123456789",*cur_arg)) {
  185.             sscanf(cur_arg,"%d",&max_hits);
  186.             cur_arg += strspn(cur_arg,"0123456789");
  187.             }
  188.             else if(argc > 1) {
  189.                 tmp = sscanf(argv[1],"%d",&max_hits);
  190.             if (tmp == 1) {argc--;argv++;}
  191.             }
  192.             if (max_hits < 1) {
  193.             fprintf(stderr, "%s: -m option requires a value for max hits (>= 1)\n",
  194.                 progname);
  195.             exit(A_EXIT);
  196.             }
  197.             break;
  198.  
  199.         case 'o':  /* Offset */
  200.             if(argc > 1) {
  201.               tmp = sscanf(argv[1],"%d",&offset);
  202.               if (tmp != 1)
  203.             argc = -1;
  204.               else {
  205.             argc--; argv++;
  206.               }
  207.             }
  208.             break;
  209.  
  210.         case 'r':  /* Regular expression search */
  211.             qtype = 'R';
  212.             etype = 'r';
  213.             break;
  214.  
  215.         case 's':  /* substring (case insensitive) */
  216.             qtype = 'S';
  217.             etype = 's';
  218.             break;
  219.  
  220.         case 't':  /* Sort inverted by date */
  221.             sortflag = 1;
  222.             break;
  223.  
  224.         case 'v':  /* Display version */
  225.             fprintf(stderr,
  226.             "Client version %s based upon Prospero version %s\n",
  227.                 CLIENT_VERSION, PFS_RELEASE);
  228.             exitflag++;
  229.             break;
  230.  
  231.         default:
  232.             fprintf(stderr,"Usage: %s [-[cers][l][t][m#][h host][%s][%s#]%s string\n", progname, LFLAG, NFLAG, HFLAG);
  233.             exit(A_EXIT);
  234.         }
  235.         }
  236.         argc--; argv++;
  237.     }
  238.  
  239.       scandone:
  240.  
  241.     if (eflag) qtype = etype;
  242.  
  243.     if ((argc != 1) && exitflag) exit(0);
  244.  
  245.     if (argc != 1) {
  246.         fprintf(stderr, "Usage: %s [-[cers][l][t][m#][h host][%s][%s#]%s string\n", progname, LFLAG, NFLAG, HFLAG);
  247.         fprintf(stderr,"       -c : case sensitive substring search\n");
  248.         fprintf(stderr,"       -e : exact string match (default)\n");
  249.         fprintf(stderr,"       -r : regular expression search\n");
  250.         fprintf(stderr,"       -s : case insensitive substring search\n");
  251.         fprintf(stderr,"       -l : list one match per line\n");
  252.         fprintf(stderr,"       -t : sort inverted by date\n");
  253.         fprintf(stderr,"      -m# : specifies maximum number of hits to return (default %d)\n", max_hits);
  254.         fprintf(stderr,"  -h host : specifies server host\n");
  255.         fprintf(stderr,"       -%s : list known servers and current default\n", LFLAG);
  256.         fprintf(stderr,"      -%s# : specifies query niceness level (0-35765)\n", NFLAG);
  257. #ifdef CUTCP
  258.         fprintf(stderr,"-H config.tel: specify location of config.tel file\n");
  259. #endif
  260.         exit(A_EXIT);
  261.     }
  262.  
  263. #ifdef    CUTCP
  264.     if(argc = Snetinit()) {
  265.                fprintf(stderr,"Error %d from SNetinit (bad or missing config.tel ?)\n",argc);
  266.         if(argc == -2)
  267.                    netshut();    /* rarp lookup failure */
  268.         exit(1);
  269.     }
  270.     if(!hostset) {        /* if no host on command line, look in config.tel file
  271.                           for name=archie */
  272.         struct machinfo *mp;
  273.  
  274.          mp = Shostlook("archie");
  275.         if(mp) {
  276.                    host = mp->hname ? mp->hname : mp->sname;
  277.         }
  278.        }
  279. #endif
  280.  
  281.     procquery(host, argv[0], max_hits, offset, qtype, sortflag, listflag);
  282.  
  283. #ifdef CUTCP
  284.     netshut();
  285. #endif
  286.     exit(0);
  287.     }
  288.