home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / ARCHIE-1.4 / ARCHIE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-20  |  7.4 KB  |  314 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. /*
  9.  * Archie client using the Prospero protocol.
  10.  *
  11.  * Suggestions and improvements to Brendan Kehoe (brendan@cygnus.com).
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <getopt.h>
  16.  
  17. #if defined(OS2)
  18. # include <pctcp.h>
  19. #endif
  20.  
  21. #ifdef MSDOS
  22. # include <string.h>
  23. # include <stdlib.h>
  24. # ifdef CUTCP
  25. #  include <msdos/cutcp.h>
  26. #  include <msdos/hostform.h>
  27. /* The default stack size for a BC program is 4k; jack it up to 16 and add the
  28.    Check for Stack Overflow option to the compiler.  */
  29. extern unsigned _stklen = 16 * 1024;
  30. # endif
  31. #endif
  32.  
  33. #include <pfs.h>
  34. #include <rdgram.h>
  35. #include <archie.h>
  36. #include <pmachine.h>
  37.  
  38. /* Whether we should produce single-line listings suitable for frobbing by
  39.    other programs, or produce nice clean human output (default).  */
  40. int listflag = 0;
  41.  
  42. /* How to sort the data; 1 means by date, 0 is by inverse hostname.  */
  43. int sortflag = 0;
  44.  
  45. /* Used by CUTCP to see if they specified the host with `-h' or if
  46.    the config.tel file should be consulted.  */ 
  47. int hostset = 0;
  48.  
  49. /* When doing searches, should we make some comments to pacify the user?  */
  50. int verbose = 0;
  51.  
  52. /* Maximum number of hits for this query; pushing this high is very
  53.    anti-social.  */
  54. int max_hits = MAX_HITS;
  55.  
  56. /* The offset for the Prospero query.  */
  57. int offset = 0;
  58.  
  59. /* Display the Alex filename?  */
  60. int alex = 0;
  61.  
  62. /* The default host to query for searches.  */ 
  63. char *host = ARCHIE_HOST;
  64.  
  65. FILE *archie_out;
  66.  
  67. /* The name this program was run with.  */
  68. char *program_name;
  69.  
  70. extern int pfs_debug;
  71. extern int rdgram_priority;
  72.  
  73. void usage ();
  74. extern char *getenv ();
  75.  
  76. void
  77. main (argc, argv)
  78.      int argc;
  79.      char **argv;
  80. {
  81.   Query query = EXACT;
  82.   int optc, tmp;
  83.   /* If true, display the release.  */
  84.   int exitflag = 0;
  85.   /* The file to print the results to.  Defaults to stdout.  */
  86.   char *outfile = (char *)NULL;
  87.   char *p;
  88.   static char *archies[] = { ARCHIES };
  89.  
  90.   program_name = argv[0];
  91.  
  92.   /* Default debugging level.  */
  93.   pfs_debug = 0;
  94.  
  95. #ifdef    CUTCP
  96.   if (getenv ("CONFIGTEL"))
  97.     if (Shostfile (getenv ("CONFIGTEL")) < 0)
  98.       {
  99.     fprintf (stderr, "Error, couldn't open configtel file %s\n",
  100.          getenv ("CONFIGTEL"));
  101.     exit (1);
  102.       }
  103. #endif
  104.  
  105.   if ((p = getenv ("ARCHIE_HOST")) != (char *) NULL)
  106.     host = p;
  107.  
  108. #ifdef CUTCP
  109.   while ((optc = getopt (argc, argv, "D:LHN:O:ceh:alm:o:rstvV")) != EOF)
  110. #else
  111.   while ((optc = getopt (argc, argv, "D:LN:O:ceh:alm:o:rstvV")) != EOF)
  112. #endif
  113.     {
  114.       switch (optc)
  115.     {
  116.     case 'D':
  117.       pfs_debug = atoi (optarg);
  118.       break;
  119.  
  120.     case 'L':
  121.       printf ("Known archie servers:\n");
  122.       for (tmp = 0; tmp < NARCHIES; tmp++)
  123.         printf ("\t%s\n", archies[tmp]);
  124.       printf (" * %s is the default Archie server.\n", ARCHIE_HOST);
  125.       printf (" * For the most up-to-date list, write to an Archie server and give it\n   the command `servers'.\n");
  126.       exitflag = 1;
  127.       break;
  128.  
  129. #ifdef CUTCP
  130.     case 'H':
  131.       if (Shostfile (optarg) < 0)
  132.         {
  133.           fprintf (stderr,
  134.                "%s: couldn't open configtel file %s\n",
  135.                program_name, optarg);
  136.           exit (1);
  137.         }
  138.       break;
  139. #endif
  140.  
  141.     case 'N':
  142.       rdgram_priority = atoi (optarg);
  143.       if (rdgram_priority > RDGRAM_MAX_SPRI)
  144.         rdgram_priority = RDGRAM_MAX_PRI;
  145.       else if (rdgram_priority < RDGRAM_MIN_PRI)
  146.         rdgram_priority = RDGRAM_MIN_PRI;
  147.       break;
  148.  
  149.     case 'c': /* Substring (case-sensitive).  */
  150.       query = SUBSTRING_CASE;
  151.       break;
  152.  
  153.     case 'e': /* Exact match.  */
  154.       query = EXACT;
  155.       break;
  156.  
  157.     case 'h': /* Archie host.  */
  158.       host = optarg;
  159. #ifdef CUTCP
  160.       hostset = 1;
  161. #endif
  162.       break;
  163.  
  164.     case 'a': /* List matches as Alex filenames.  */
  165.       alex = 1;
  166.       break;
  167.  
  168.     case 'l': /* List one match per line.  */
  169.       listflag = 1;
  170.       break;
  171.  
  172.     case 'm': /* Maximum number of hits for the query.  */
  173.       max_hits = atoi (optarg);
  174.       if (max_hits < 1)
  175.         {
  176.           fprintf (stderr,
  177.                "%s: option `-m' requires a max hits value >= 1\n",
  178.                program_name);
  179.           exit (ERROR_EXIT);
  180.         }
  181.       break;
  182.  
  183.     case 'o': /* output file */
  184.       if (outfile)
  185.         {
  186.           fprintf (stderr, "%s: multiple output files specified\n",
  187.                program_name);
  188.           exit (ERROR_EXIT);
  189.         }
  190.       outfile = optarg;
  191.       break;
  192.  
  193.     case 'O': /* Specify the offset.  */
  194.       offset = atoi (optarg);
  195.       break;
  196.  
  197.     case 'r': /* Regexp search.  */
  198.       query = REGEXP;
  199.       break;
  200.  
  201.     case 's': /* Substring (case insensitive).  */
  202.       query = SUBSTRING;
  203.       break;
  204.  
  205.     case 't': /* Sort inverted by date.  */
  206.       sortflag = 1;
  207.       break;
  208.  
  209.     case 'v': /* Display version.  */
  210.       fprintf (stderr,
  211.            "Client version %s based upon Prospero version %s\n",
  212.            CLIENT_VERSION, PFS_RELEASE);
  213.       exitflag = 1;
  214.       break;
  215.  
  216.     case 'V': /* Verbose when search is happening.  */
  217.       verbose = 1;
  218.       break;
  219.  
  220.     default:
  221.       usage ();
  222.     }
  223.     }
  224.  
  225.   if (exitflag)
  226.     exit (0);
  227.   else if (optind == argc)
  228.     usage ();
  229.   else if (alex && listflag)
  230.     {
  231.       fprintf (stderr, "%s: only one of `-a' or `-l' may be used\n",
  232.            program_name);
  233.       exit (ERROR_EXIT);
  234.     }
  235.  
  236.   if (outfile)
  237.     {
  238.       archie_out = fopen (outfile, "w+");
  239.       if (archie_out == (FILE *) NULL)
  240.     {
  241.       fprintf (stderr, "%s: cannot open %s\n", program_name, outfile);
  242.       exit (ERROR_EXIT);
  243.     }
  244.     }
  245.   else
  246.     archie_out = stdout;
  247.  
  248. #ifdef    CUTCP
  249.   if (tmp = Snetinit ())
  250.     {
  251.       fprintf (stderr, " %d from SNetinit (bad or missing config.tel ?)\n",
  252.            tmp);
  253.  
  254.       /* If there was a rarp lookup failure, shut the network card down.  */
  255.       if (tmp == -2)
  256.     netshut ();
  257.       exit (ERROR_EXIT);
  258.     }
  259.  
  260.   if (!hostset)
  261.     {
  262.       /* Look in config.tel if they didn't give us a host with `-h'.  The
  263.          entries appear as "name=archie".  */
  264.       struct machinfo *mp = Shostlook ("archie");
  265.  
  266.       if (mp)
  267.     host = mp->hname ? mp->hname : mp->sname;
  268.     }
  269. #endif
  270.  
  271.   for (; optind < argc; ++optind)
  272.     procquery (host, argv[optind], max_hits, offset, query);
  273.  
  274. #ifdef CUTCP
  275.   netshut ();
  276. #endif
  277.  
  278.   if (outfile)
  279.     fclose (archie_out);
  280.  
  281.   exit (0);
  282. }
  283.  
  284. #ifdef CUTCP
  285. # define HFLAG    "] [H config.tel]"
  286. #else
  287. # define HFLAG    "]"
  288. #endif
  289.  
  290. void
  291. usage ()
  292. {
  293.   fprintf (stderr, "\
  294. Usage: %s [-acelorstvLV] [-m hits%s [-N level] string\n", program_name, HFLAG);
  295.   fprintf (stderr, "          -a : list matches as Alex filenames\n");
  296.   fprintf (stderr, "          -c : case sensitive substring search\n");
  297.   fprintf (stderr, "          -e : exact string match (default)\n");
  298.   fprintf (stderr, "          -r : regular expression search\n");
  299.   fprintf (stderr, "          -s : case insensitive substring search\n");
  300.   fprintf (stderr, "          -l : list one match per line\n");
  301.   fprintf (stderr, "          -t : sort inverted by date\n");
  302.   fprintf (stderr, "     -m hits : specifies maximum number of hits to return (default %d)\n", max_hits);
  303.   fprintf (stderr, " -o filename : specifies file to store results in\n");
  304.   fprintf (stderr, "     -h host : specifies server host\n");
  305.   fprintf (stderr, "          -L : list known servers and current default\n");
  306.   fprintf (stderr, "    -N level : specifies query niceness level (0-35765)\n");
  307. #ifdef CUTCP
  308.   fprintf (stderr, "-H config.tel: specify location of config.tel file\n");
  309. #endif
  310.  
  311.   exit (ERROR_EXIT);
  312. }
  313.  
  314.