home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part03 / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  6.5 KB  |  314 lines

  1. /* $Id: init.c,v 4.4 1991/09/09 20:18:23 sob Exp sob $
  2.  *
  3.  * $Log: init.c,v $
  4.  * Revision 4.4  1991/09/09  20:18:23  sob
  5.  * release 4.4
  6.  *
  7.  *
  8.  * 
  9.  */
  10. /* This software is Copyright 1991 by Stan Barber. 
  11.  *
  12.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  13.  * use this software as long as: there is no monetary profit gained
  14.  * specifically from the use or reproduction of this software, it is not
  15.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  16.  * included prominently in any copy made. 
  17.  *
  18.  * The author make no claims as to the fitness or correctness of this software
  19.  * for any use whatsoever, and it is provided as is. Any use of this software
  20.  * is at the user's own risk. 
  21.  */
  22.  
  23. #include "EXTERN.h"
  24. #include "common.h"
  25. #include "util.h"
  26. #include "final.h"
  27. #include "term.h"
  28. #include "last.h"
  29. #include "rn.h"
  30. #include "rcstuff.h"
  31. #include "ngdata.h"
  32. #include "only.h"
  33. #include "intrp.h"
  34. #include "addng.h"
  35. #include "sw.h"
  36. #include "art.h"
  37. #include "artsrch.h"
  38. #include "artio.h"
  39. #include "backpage.h"
  40. #include "bits.h"
  41. #include "cheat.h"
  42. #include "head.h"
  43. #include "help.h"
  44. #include "kfile.h"
  45. #include "ngsrch.h"
  46. #include "ngstuff.h"
  47. #include "rcln.h"
  48. #include "respond.h"
  49. #ifdef SERVER
  50. #include "server.h"
  51. #endif
  52. #ifdef USETHREADS
  53. #include "threads.h"
  54. #include "rthreads.h"
  55. #endif
  56. #include "ng.h"
  57. #include "decode.h"
  58. #include "INTERN.h"
  59. #include "init.h"
  60.  
  61. bool
  62. initialize(argc,argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     char *tcbuf;
  67.     register bool foundany = FALSE;
  68.     long time();
  69. #ifdef SERVER
  70.     char *server;
  71.     int response;
  72. #endif
  73. #ifdef NOLINEBUF
  74.     static char std_out_buf[BUFSIZ];    /* must be static or malloced */
  75.  
  76.     setbuf(stdout, std_out_buf);
  77. #endif
  78.  
  79.     tcbuf = safemalloc(1024);        /* make temp buffer for termcap and */
  80.                     /* other initialization stuff */
  81.     
  82.     /* init terminal */
  83.     
  84.     term_init();            /* must precede sw_init() so that */
  85.                     /* ospeed is set for baud-rate */
  86.                     /* switches.  Actually terminal */
  87.                     /* mode setting is in term_set() */
  88.  
  89.     /* we have to know rnlib to look up global switches in %X/INIT */
  90.  
  91.     lib = savestr(filexp(LIB));
  92.     rnlib = savestr(filexp(RNLIB));
  93.  
  94.     /* decode switches */
  95.  
  96.     sw_init(argc,argv,&tcbuf);          /* must not do % interps! */
  97.                     /* (but may mung environment) */
  98.  
  99.     /* init signals, status flags */
  100.  
  101.     final_init();
  102.     
  103.     /* start up file expansion and the % interpreter */
  104.  
  105.     intrp_init(tcbuf);
  106.     
  107.     /* now make sure we have a current working directory */
  108.  
  109.     if (!checkflag)
  110.     cwd_check();
  111.     
  112.     /* now that we know where to save things, cd to news directory */
  113.  
  114.     if (chdir(spool)) {
  115.     printf(nocd,spool) FLUSH;
  116.     finalize(1);
  117.     }
  118.  
  119.     /* if we aren't just checking, turn off echo */
  120.  
  121.     if (!checkflag)
  122.     term_set(tcbuf);
  123.  
  124.     /* get info on last rn run, if any */
  125.  
  126.     if (!checkflag)
  127.     last_init(tcbuf);
  128.  
  129.     free(tcbuf);            /* recover 1024 bytes */
  130.  
  131.     /* make sure we are the sole possessors of .newsrc */
  132.  
  133.     if (!checkflag)
  134.     lock_check();
  135.  
  136.     /* check for news news */
  137.  
  138.     if (!checkflag)
  139.     newsnews_check();
  140.  
  141. #ifdef SERVER
  142.  
  143.     /* open connection to server if appropriate */
  144.  
  145.     if ((server = get_server_name(1)) == NULL)
  146.     finalize(1);
  147.  
  148.     response = server_init(server);
  149.     if (response < 0) {
  150.     fprintf(stderr,
  151.         "Couldn't connect to %s news server, try again later.\n",
  152.         server);
  153.     finalize(1);
  154.     }
  155.  
  156.     if (handle_server_response(response, server) < 0)
  157.     finalize(1);
  158.  
  159. #endif
  160.  
  161.     /* open active file, etc. */
  162.  
  163.     ngdata_init();
  164.  
  165.     /* now read in the .newsrc file */
  166.  
  167.     foundany = rcstuff_init();
  168.  
  169.     /* it looks like we will actually read something, so init everything */
  170.  
  171.     addng_init();
  172.     art_init();
  173.     artio_init();
  174.     artsrch_init();
  175.     backpage_init();
  176.     bits_init();
  177.     cheat_init();
  178.     head_init();
  179.     help_init();
  180.     kfile_init();
  181.     ng_init();
  182.     ngsrch_init();
  183.     ngstuff_init();
  184.     only_init();
  185.     rcln_init();
  186.     respond_init();
  187.     rn_init();
  188.     search_init();
  189.     decode_init();
  190. #ifdef USETHREADS
  191.     thread_init();
  192. #endif
  193.     util_init();
  194.  
  195. #ifdef FINDNEWNG
  196.     /*
  197.      * Skip this check if the -q flag was given.
  198.      */
  199.     if (!quickstart) {
  200.     if (find_new_groups()) {    /* did we add any new groups? */
  201.         foundany = TRUE;        /* let main() know */
  202.         starthere = 0;        /* start ng scan from the top */
  203.     }
  204.     }
  205. #endif
  206.     time(&lasttime);            /* remember when we inited-- */
  207.                     /* ends up back in .rnlast */
  208.     writelast();            /* in fact, put it there now */
  209.  
  210. #ifdef FINDNEWNG
  211. # ifdef ONLY
  212.     if (maxngtodo)            /* patterns on command line? */
  213.     foundany |= scanactive();
  214. # endif
  215. #endif
  216.  
  217.     return foundany;
  218. }
  219.  
  220. /* make sure there is no rn out there already */
  221.  
  222. void
  223. lock_check()
  224. {
  225.     lockname = savestr(filexp(LOCKNAME));
  226.     if (!checkflag) {
  227.     tmpfp = fopen(lockname,"r");
  228.     if (tmpfp != Nullfp) {
  229.         int processnum;
  230.     
  231.         fgets(buf,LBUFLEN,tmpfp);
  232.         fclose(tmpfp);
  233.         processnum = atoi(buf);
  234. #ifdef VERBOSE
  235.         IF(verbose)
  236.         printf("You seem to have left a trn running, process %d.\n",
  237.             processnum) FLUSH;
  238.         ELSE
  239. #endif
  240. #ifdef TERSE
  241.         printf("Trn left running, #%d.\n", processnum) FLUSH;
  242. #endif
  243.         if (kill(processnum, SIGEMT)) {
  244.                     /* does process not exist? */
  245.                     /* (rn ignores SIGEMT) */
  246.         sleep(2);
  247. #ifdef VERBOSE
  248.         IF(verbose)
  249.             fputs("\n\
  250. That process does not seem to exist anymore.  The count of read articles\n\
  251. may be incorrect in the last newsgroup accessed by that other (defunct)\n\
  252. process.\n\n",stdout) FLUSH;
  253.         ELSE
  254. #endif
  255. #ifdef TERSE
  256.             fputs("\nProcess crashed.\n",stdout) FLUSH;
  257. #endif
  258.         if (*lastngname) {
  259. #ifdef VERBOSE
  260.             IF(verbose)
  261.             printf("(The last newsgroup accessed was %s.)\n\n",
  262.             lastngname) FLUSH;
  263.             ELSE
  264. #endif
  265. #ifdef TERSE
  266.             printf("(In %s.)\n\n",lastngname) FLUSH;
  267. #endif
  268.         }
  269.         get_anything();
  270.         putchar('\n') FLUSH;
  271.         }
  272.         else {
  273. #ifdef VERBOSE
  274.         IF(verbose)
  275.             fputs("\n\
  276. You may not have two copies of [t]rn running simultaneously.  Goodbye.\n\
  277. ",stdout) FLUSH;
  278.         ELSE
  279. #endif
  280. #ifdef TERSE
  281.             fputs("\nCan't start another.\n",stdout) FLUSH;
  282. #endif
  283.                if (bizarre)
  284.                  resetty();
  285.         exit(0);
  286.         }
  287.     }
  288.     tmpfp = fopen(lockname,"w");
  289.     if (tmpfp == Nullfp) {
  290.         printf(cantcreate,lockname) FLUSH;
  291.         sig_catcher(0);
  292.     }
  293.     fprintf(tmpfp,"%d\n",getpid());
  294.     fclose(tmpfp);
  295.     }
  296. }
  297.  
  298. void
  299. newsnews_check()
  300. {
  301.     char *newsnewsname = filexp(NEWSNEWSNAME);
  302.  
  303.     if ((tmpfp = fopen(newsnewsname,"r")) != Nullfp) {
  304.     fstat(fileno(tmpfp),&filestat);
  305.     if (filestat.st_mtime > lasttime) {
  306.         while (fgets(buf,sizeof(buf),tmpfp) != Nullch)
  307.         fputs(buf,stdout) FLUSH;
  308.         get_anything();
  309.         putchar('\n') FLUSH;
  310.     }
  311.     fclose(tmpfp);
  312.     }
  313. }
  314.