home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / RN / INIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-21  |  10.8 KB  |  482 lines

  1. /* $Header: E:\SRC\UUPC\RN\RCS/INIT.C 1.1 1992/11/21 06:14:58 ahd Exp $
  2.  *
  3.  * $Log: INIT.C $
  4.  * Revision 1.1  1992/11/21  06:14:58  ahd
  5.  * Initial
  6.  *
  7.  *
  8.  *    Rev 1.0   18 Nov 1990  0:22:12
  9.  * Initial revision.
  10.  * Revision 4.3.2.6  90/05/08  22:05:55  sob
  11.  * Added quick startup (-q) flag.
  12.  *
  13.  * Revision 4.3.2.5  90/05/04  23:10:01  sob
  14.  * Fix for exiting "second" rn such that tty will be left in correct state.
  15.  * Provided by glenn@mathcs.emory.edu
  16.  *
  17.  * Revision 4.3.2.4  90/03/22  23:04:32  sob
  18.  * Fixes provided by Wayne Davison <drivax!davison>
  19.  *
  20.  * Revision 4.3.2.3  90/03/17  21:34:04  sob
  21.  * Cleaned up a bit.
  22.  *
  23.  * Revision 4.3.2.2  89/11/08  01:17:48  sob
  24.  * Added changes to insure that this will compile for RN or RRN with no
  25.  * changes to the source code.
  26.  *
  27.  * Revision 4.3.2.1  89/11/06  00:39:14  sob
  28.  * Added RRN support from NNTP 1.5
  29.  *
  30.  * Revision 4.3.1.4  86/09/05  14:24:02  lwall
  31.  * Removed net.announce dependency.
  32.  *
  33.  * Revision 4.3.1.3  85/07/23  18:08:36  lwall
  34.  * Fixed up NOLINEBUF option to work.
  35.  *
  36.  * Revision 4.3.1.2  85/05/21  14:22:46  lwall
  37.  * Sped up "rn -c" by avoiding unnecessary initialization.
  38.  *
  39.  * Revision 4.3.1.1  85/05/10  11:33:39  lwall
  40.  * Branch for patches.
  41.  *
  42.  * Revision 4.3  85/05/01  16:16:13  lwall
  43.  * Baseline for release with 4.3bsd.
  44.  *
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <time.h>
  51.  
  52. #include "lib.h"
  53. #include "ssleep.h"
  54.  
  55. currentfile();
  56.  
  57. #ifdef msdos
  58. #include <process.h>
  59.  
  60. extern char **host_argv;
  61.  
  62. #endif                       /* msdos */
  63.  
  64. #include "EXTERN.h"
  65. #include "common.h"
  66. #include "util.h"
  67. #include "final.h"
  68. #include "term.h"
  69. #include "last.h"
  70. #include "rn.h"
  71. #include "rcstuff.h"
  72. #include "ngdata.h"
  73. #include "only.h"
  74. #include "intrp.h"
  75. #include "addng.h"
  76. #include "sw.h"
  77. #include "art.h"
  78. #include "artsrch.h"
  79. #include "artio.h"
  80. #include "backpage.h"
  81. #include "bits.h"
  82. #include "cheat.h"
  83. #include "head.h"
  84. #include "help.h"
  85. #include "kfile.h"
  86. #include "ngsrch.h"
  87. #include "ngstuff.h"
  88. #include "rcln.h"
  89. #include "respond.h"
  90.  
  91. #ifdef SERVER
  92. #include "server.h"
  93. #endif
  94.  
  95. #include "ng.h"
  96. #include "INTERN.h"
  97. #include "init.h"
  98.  
  99.  
  100. bool
  101. initialize(argc, argv)
  102.    int argc;
  103.    char *argv[];
  104. {
  105.    char *tcbuf;
  106.    register bool foundany = FALSE;
  107.    long time();
  108.  
  109. #ifdef SERVER
  110.    char *server;
  111.    int response;
  112.  
  113. #endif                       /* SERVER */
  114.  
  115. #ifdef NOLINEBUF
  116.    static char std_out_buf[BUFSIZ];     /* must be static or
  117.                                          * malloced */
  118.  
  119.    setbuf(stdout, std_out_buf);
  120. #endif
  121.  
  122. #ifdef msdos
  123.    host_argv = argv;
  124. #endif
  125.  
  126.    if (hostinit(FALSE))
  127.       exit(1);
  128.  
  129.    tcbuf = safemalloc(1024); /* make temp buffer for
  130.                               * termcap and */
  131.    /* other initialization stuff */
  132.  
  133.    /* init terminal */
  134.  
  135.    term_init();              /* must precede
  136.                               * sw_init() so that */
  137.    /* ospeed is set for baud-rate */
  138.    /* switches.  Actually terminal */
  139.    /* mode setting is in term_set() */
  140.  
  141.    /* we have to know rnlib to look up global switches in %X/INIT */
  142.  
  143. #ifdef msdos
  144.    if (!lib)
  145.    {
  146.       if (E_newsdir)
  147.          lib = E_newsdir;
  148.    }
  149. #endif                       /* msdos */
  150.  
  151.    if (!lib)
  152.       lib = savestr(filexp(LIB));
  153.    rnlib = savestr(filexp(RNLIB));
  154.  
  155. #ifdef msdos
  156.    if (!tmpdir && E_tempdir)
  157.       tmpdir = E_tempdir;
  158. #endif                       /* msdos */
  159.  
  160.    if (!tmpdir)
  161.       tmpdir = savestr(filexp(TMP));
  162.  
  163.    /* decode switches */
  164.  
  165.    sw_init(argc, argv, &tcbuf); /* must not do %
  166.                                  * interps! */
  167.    /* (but may mung environment) */
  168.  
  169.    /* init signals, status flags */
  170.  
  171.    final_init();
  172.  
  173.    /* start up file expansion and the % interpreter */
  174.  
  175.    intrp_init(tcbuf);
  176.  
  177.    /* now make sure we have a current working directory */
  178.  
  179.    if (!checkflag)
  180.    {
  181.       /* If there was no -d in the options, we supply a default */
  182.       if (!newcwd)
  183.          newcwd = savestr("~/News");
  184.       cwd_check(FALSE);
  185.    }
  186.  
  187.    /* now that we know where to save things, cd to news directory */
  188.  
  189.    if (chdir(spool))
  190.    {
  191.       printerr( spool );
  192.       printf(nocd, spool) FLUSH;
  193.       finalize(1);
  194.    }
  195.  
  196.    /* if we aren't just checking, turn off echo */
  197.  
  198.    if (!checkflag)
  199.       term_set(tcbuf);
  200.  
  201.    /* get info on last rn run, if any */
  202.  
  203.    if (!checkflag)
  204.       last_init(tcbuf);
  205.  
  206.    free(tcbuf);              /* recover 1024 bytes */
  207.  
  208.    /* make sure we are the sole possessors of .newsrc */
  209.  
  210.    if (!checkflag)
  211.       lock_check();
  212.  
  213.    /* check for news news */
  214.  
  215.    if (!checkflag)
  216.       newsnews_check();
  217.  
  218. #ifdef SERVER
  219.    /* open connection to server if appropriate */
  220.  
  221.    server = getserverbyfile(SERVER_FILE);
  222.    if (server == NULL)
  223.    {
  224.       fprintf(stderr, "Can't get the name of the news server from %s\n",
  225.               SERVER_FILE);
  226.       fprintf(stderr,
  227.               "Either fix this file, or put NNTPSERVER in your environment.\n");
  228.       finalize(1);
  229.    }
  230.  
  231.    response = server_init(server);
  232.    if (response < 0)
  233.    {
  234.       fprintf(stderr,
  235.               "Couldn't connect to %s news server, try again later.\n",
  236.               server);
  237.       finalize(1);
  238.    }
  239.  
  240.    if (handle_server_response(response, server) < 0)
  241.       finalize(1);
  242. #endif                       /* SERVER */
  243.  
  244.    /* open active file, etc. */
  245.  
  246.    ngdata_init();
  247.  
  248.    /* now read in the .newsrc file */
  249.  
  250.    foundany = rcstuff_init();
  251.  
  252.    /* it looks like we will actually read something, so init everything */
  253.  
  254.    addng_init();
  255.    art_init();
  256.    artio_init();
  257.    artsrch_init();
  258.    backpage_init();
  259.    bits_init();
  260.    cheat_init();
  261.    head_init();
  262.    help_init();
  263.    kfile_init();
  264.    ng_init();
  265.    ngsrch_init();
  266.    ngstuff_init();
  267.    only_init();
  268.    rcln_init();
  269.    respond_init();
  270.    rn_init();
  271.    search_init();
  272.    util_init();
  273.  
  274. #ifdef FINDNEWNG
  275.    fstat(actfp->_file, &filestat);      /* did active file grow? */
  276.    /* Skip this check if the -q flag was given. */
  277.  
  278.    if (!quickstart && filestat.st_size != lastactsiz)
  279.    {
  280.       long actsiz = filestat.st_size;   /* remember new size */
  281.       NG_NUM oldnext = nextrcline;      /* remember # lines in
  282.                                          * newsrc */
  283.  
  284. #ifdef FASTNEW
  285.       bool munged = writesoft || !lastactsiz;
  286.  
  287.       /* bad soft ptrs -> edited active */
  288. #else
  289.       bool munged = TRUE;    /* just assume .newsrc
  290.                               * munged */
  291.  
  292. #endif
  293.  
  294. #ifdef VERBOSE
  295.       IF(verbose)
  296.          fputs("\nChecking active list for new newsgroups...\n", stdout)
  297.          FLUSH;
  298.       ELSE
  299. #endif
  300.  
  301. #ifdef TERSE
  302.          fputs("\nNew newsgroups:\n", stdout) FLUSH;
  303. #endif
  304.  
  305. #ifdef FASTNEW
  306.       if (!munged)
  307.       {                      /* maybe just do tail of
  308.                               * file? */
  309.          fseek(actfp, lastactsiz - 1, 0);
  310.          fgets(buf, LBUFLEN, actfp);
  311.          munged = (*buf != '\n');
  312.          if (!munged)
  313.             munged = newlist(munged, FALSE);
  314.       }
  315. #endif
  316.  
  317.       if (munged)
  318.       {                      /* must we scan entire
  319.                               * file? */
  320.          fseek(actfp, 0L, 0);/* rewind active file */
  321.          newlist(munged, FALSE);        /* sure hope they use
  322.                                          * hashing... */
  323.       }
  324.       lastactsiz = actsiz;   /* remember for .rnlast */
  325.       if (nextrcline != oldnext)
  326.       {                      /* did we add any new
  327.                               * groups? */
  328.          foundany = TRUE;    /* let main() know */
  329.          starthere = 0;      /* and start ng scan
  330.                               * from the top */
  331.       }
  332.    }
  333. #endif
  334.  
  335.    time(&lasttime);          /* remember when we
  336.                               * inited-- */
  337.    /* ends up back in .rnlast */
  338.    writelast();              /* in fact, put it there
  339.                               * now */
  340.  
  341. #ifdef FINDNEWNG
  342.  
  343. #ifdef ONLY
  344.    if (maxngtodo)            /* patterns on command
  345.                               * line? */
  346.       foundany |= scanactive();
  347. #endif
  348.  
  349. #endif
  350.  
  351.    return foundany;
  352. }
  353.  
  354. /* make sure there is no rn out there already */
  355.  
  356. void
  357.   lock_check()
  358. {
  359.    lockname = savestr(filexp(LOCKNAME));
  360.    if (!checkflag)
  361.    {
  362.       tmpfp = fopen(lockname, "r");
  363.       if (tmpfp != Nullfp)
  364.       {
  365.          int processnum;
  366.  
  367.          fgets(buf, LBUFLEN, tmpfp);
  368.          fclose(tmpfp);
  369.  
  370. #ifdef   unix
  371.          processnum = atoi(buf);
  372.  
  373. #ifdef VERBOSE
  374.          IF(verbose)
  375.             printf("You seem to have left an rn running, process %d.\n",
  376.                    processnum) FLUSH;
  377.          ELSE
  378. #endif
  379.  
  380. #ifdef TERSE
  381.             printf("Rn left running, #%d.\n", processnum) FLUSH;
  382. #endif
  383.  
  384.          if (kill(processnum, SIGEMT))
  385.          {
  386.             /* does process not exist? */
  387.             /* (rn ignores SIGEMT) */
  388.             ssleep(2);
  389.  
  390. #ifdef VERBOSE
  391.             IF(verbose)
  392.                fputs("\n\
  393. That process does not seem to exist anymore.  The count of read articles\n\
  394. may be incorrect in the last newsgroup accessed by that other (defunct)\n\
  395. process.\n\n", stdout) FLUSH;
  396.             ELSE
  397. #endif
  398.  
  399. #ifdef TERSE
  400.                fputs("\nProcess crashed.\n", stdout) FLUSH;
  401. #endif
  402.  
  403. #else                        /* unix */
  404.          printf("Your previous rn died, eh?\n") FLUSH;
  405. #endif                       /* unix */
  406.  
  407.          if (*lastngname)
  408.          {
  409.  
  410. #ifdef VERBOSE
  411.             IF(verbose)
  412.                printf("(The last newsgroup accessed was %s.)\n\n",
  413.                       lastngname) FLUSH;
  414.             ELSE
  415. #endif
  416.  
  417. #ifdef TERSE
  418.                printf("(In %s.)\n\n", lastngname) FLUSH;
  419. #endif
  420.          }
  421.          get_anything();
  422.          putchar('\n') FLUSH;
  423.  
  424. #ifdef   unix
  425.       }
  426.       else
  427.       {
  428.  
  429. #ifdef VERBOSE
  430.          IF(verbose)
  431.             fputs("\n\
  432. You may not have two copies of rn running simultaneously.  Goodbye.\n\
  433. ", stdout) FLUSH;
  434.          ELSE
  435. #endif
  436.  
  437. #ifdef TERSE
  438.             fputs("\nCan't start another.\n", stdout) FLUSH;
  439. #endif
  440.  
  441.          if (bizarre)
  442.             resetty();
  443.          exit(0);
  444.       }
  445. #endif                       /* unix */
  446.    }
  447.    tmpfp = fopen(lockname, "w");
  448.    if (tmpfp == Nullfp)
  449.    {
  450.       printf(cantcreate, lockname) FLUSH;
  451.       sig_catcher(0);
  452.    }
  453.    fprintf(tmpfp, "%d\n", getpid());
  454.    fclose(tmpfp);
  455. }
  456. }
  457.  
  458. void
  459.   newsnews_check()
  460. {
  461.    char *newsnewsname = filexp(NEWSNEWSNAME);
  462.  
  463.    if ((tmpfp = fopen(newsnewsname, "r")) != Nullfp)
  464.    {
  465.  
  466. #ifdef msdos
  467.       fstat(fileno(tmpfp), &filestat);
  468. #else
  469.       fstat(tmpfp->_file, &filestat);
  470. #endif
  471.  
  472.       if (filestat.st_mtime > lasttime)
  473.       {
  474.          while (fgets(buf, sizeof (buf), tmpfp) != Nullch)
  475.             fputs(buf, stdout) FLUSH;
  476.          get_anything();
  477.          putchar('\n') FLUSH;
  478.       }
  479.       fclose(tmpfp);
  480.    }
  481. }
  482.