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

  1. /* $Id: tmpthread.c,v 4.4.3.1 1991/11/22 04:12:21 davison Trn $
  2. **
  3. ** $Log: tmpthread.c,v $
  4. ** Revision 4.4.3.1  1991/11/22  04:12:21  davison
  5. ** Trn Release 2.0
  6. ** 
  7. */
  8.  
  9. /* tmpthread.c -- for creating a temporary discussion-thread file
  10. **
  11. ** Usage:  tmpthread [opts] news.group last# first# addmax# /tmp/filename
  12. */
  13.  
  14. #include "EXTERN.h"
  15. #include "common.h"
  16. #include "threads.h"
  17. #ifdef SERVER
  18. #include "server.h"
  19. #endif
  20. #include "INTERN.h"
  21. #define TMPTHREAD
  22. #include "mthreads.h"
  23.  
  24. #ifdef TZSET
  25. #include <time.h>
  26. #else
  27. #include <sys/time.h>
  28. #include <sys/timeb.h>
  29. #endif
  30.  
  31. struct stat filestat;
  32.  
  33. char buf[LBUFLEN+1];
  34. char *filename, *group, *tmpname;
  35.  
  36. int first, last, max, start;
  37. int log_verbosity = 0, debug = 0, slow_down = 0;
  38. int ignore_database = 0, read_from_tmp = 0;
  39.  
  40. char nullstr[1] = "";
  41.  
  42. #ifdef XTHREAD
  43. int size;
  44. #else
  45. FILE *fp;
  46. #endif
  47.  
  48. #ifdef TZSET
  49. time_t tnow;
  50. #else
  51. struct timeb ftnow;
  52. #endif
  53.  
  54. #ifdef SERVER
  55. char *server;
  56. #endif
  57.  
  58. SIGRET int_handler();
  59.  
  60. int
  61. main(argc, argv)
  62. int  argc;
  63. char *argv[];
  64. {
  65.     char *cp;
  66.  
  67.     while (--argc) {
  68.     if (**++argv == '-') {
  69.         while (*++*argv) {
  70.         switch (**argv) {
  71.         case 'D':
  72.             debug++;
  73.             break;
  74.         case 'i':
  75.             ignore_database++;
  76.             break;
  77.         case 's':
  78.             if (*++*argv <= '9' && **argv >= '0') {
  79.             slow_down = atoi(*argv);
  80.             while (*++*argv <= '9' && **argv >= '0') {
  81.                 ;
  82.             }
  83.             } else {
  84.             slow_down = 1L * 1000 * 1000;
  85.             }
  86.             --*argv;
  87.             break;
  88.         case 't':    /* Use tmpfile w/no byte-order changes */
  89.             read_from_tmp = 1;
  90.             break;
  91.         case 'T':    /* Use tmpfile w/possible byte-order changes */
  92.             read_from_tmp = 2;
  93.             break;
  94.         case 'v':
  95.             log_verbosity++;
  96.             break;
  97.         default:
  98.             fprintf(stderr, "Unknown option: '%c'\n", **argv);
  99.             exit(1);
  100.         }
  101.         }
  102.     } else {
  103.         break;
  104.     }
  105.     }
  106.  
  107.     if (argc < 5) {
  108.       give_usage:
  109.     fprintf(stderr,
  110. "Usage:  tmpthread [opts] news.group last# first# addmax# /tmp/filename\n");
  111.     exit(1);
  112.     }
  113.  
  114.     group = argv[0];
  115.     last = atoi(argv[1]);
  116.     first = atoi(argv[2]);
  117.     max = atoi(argv[3]);
  118.     tmpname = argv[4];
  119.  
  120.     if (first < 0 || last < first - 1 || max <= 0) {
  121.     goto give_usage;
  122.     }
  123.  
  124.     /* Initialize umask(), file_exp(), etc. */
  125.     mt_init();
  126.  
  127. #ifdef SIGHUP
  128.     if (sigset(SIGHUP, SIG_IGN) != SIG_IGN) {
  129.     sigset(SIGHUP, int_handler);
  130.     }
  131. #endif
  132.     if (sigset(SIGINT, SIG_IGN) != SIG_IGN) {
  133.     sigset(SIGINT, int_handler);
  134.     }
  135. #ifdef SIGQUIT
  136.     if (sigset(SIGQUIT, SIG_IGN) != SIG_IGN) {
  137.     sigset(SIGQUIT, int_handler);
  138.     }
  139. #endif
  140.     sigset(SIGTERM, int_handler);
  141. #ifdef SIGBUS
  142.     sigset(SIGBUS, int_handler);
  143. #endif
  144.     sigset(SIGSEGV, int_handler);
  145. #ifdef SIGTTIN
  146.     sigset(SIGTTIN, SIG_IGN);
  147.     sigset(SIGTTOU, SIG_IGN);
  148. #endif
  149.  
  150. #ifdef lint
  151.     int_handler(SIGINT);
  152. #endif
  153.  
  154. #ifdef SERVER
  155.     if ((server = get_server_name(0)) == NULL) {
  156.     log_entry("couldn't find name of news server.\n");
  157.     exit(1);
  158.     }
  159.     switch (server_init(server)) {
  160.     case OK_NOPOST:
  161.     case OK_CANPOST:
  162.     break;
  163.     case ERR_ACCESS:
  164.     log_entry("Server %s rejected connection.\n", server);
  165.     exit(1);
  166.     default:
  167.     log_entry("Couldn't connect with server %s.\n", server);
  168.     exit(1);
  169.     }
  170. #endif
  171.  
  172.     /* See if this machine needs byte-order translation for the database */
  173.     word_same = long_same = TRUE;
  174.     if (read_from_tmp != 1) {
  175. #ifdef XTHREAD
  176.     put_server("XTHREAD DBINIT");
  177.     rawcheck_server(buf, sizeof buf);
  178.     size = rawget_server((char*)&mt_bmap, sizeof (BMAP));
  179.     if (size < sizeof (BMAP) - 1) {
  180. #else
  181.     if ((fp = fopen(file_exp(DBINIT), FOPEN_RB)) == Nullfp
  182.      || fread((char*)&mt_bmap, 1, sizeof (BMAP), fp) < sizeof (BMAP)-1) {
  183. #endif
  184.         log_entry("db.init read failed -- assuming no byte-order translations.\n\n");
  185.         mybytemap(&mt_bmap);
  186.     } else {
  187.         int i;
  188.  
  189.         if (mt_bmap.version != DB_VERSION) {
  190.         log_entry("Thread database is not the right version -- ignoring it.\n");
  191.         ignore_database = 1;
  192.         }
  193.         mybytemap(&my_bmap);
  194.         for (i = 0; i < sizeof (LONG); i++) {
  195.         if (i < sizeof (WORD)) {
  196.             if (my_bmap.w[i] != mt_bmap.w[i]) {
  197.             word_same = FALSE;
  198.             }
  199.         }
  200.         if (my_bmap.l[i] != mt_bmap.l[i]) {
  201.             long_same = FALSE;
  202.         }
  203.         }
  204.     }
  205. #ifdef XTHREAD
  206.     while (rawget_server(buf, sizeof buf)) {
  207.         ;        /* trash any extraneous bytes */
  208.     }
  209. #else
  210.     if (fp != Nullfp) {
  211.         fclose(fp);
  212.     }
  213. #endif
  214.     }
  215.  
  216.     /* What time is it? */
  217. #ifdef TZSET
  218.     (void) time(&tnow);
  219.     (void) tzset();
  220. #else
  221.     (void) ftime(&ftnow);
  222. #endif
  223.  
  224. #ifdef SERVER
  225.     sprintf(buf, "GROUP %s", group);
  226.     put_server(buf);
  227.     if (get_server(buf, sizeof buf) < 0 || *buf != CHAR_OK) {
  228.     log_entry("NNTP failure on group `%s'.\n", group);
  229. #else
  230.     strcpy(cp = buf, group);
  231.     while ((cp = index(cp, '.'))) {
  232.     *cp = '/';
  233.     }
  234.     filename = file_exp(buf);        /* relative to spool dir */
  235.     if (chdir(filename) < 0) {
  236.     if (errno != ENOENT) {
  237.         log_entry("Unable to chdir to `%s'.\n", filename);
  238.     }
  239. #endif
  240.     exit(1);
  241.     } else {
  242.     if (read_from_tmp) {
  243.         filename = tmpname;
  244.     } else {
  245. #ifdef XTHREAD
  246.         ignore_database = TRUE;
  247. #else
  248.         filename = thread_name(group);
  249. #endif
  250.     }
  251.     if (ignore_database || !init_data(filename) || !read_data()) {
  252.         (void) init_data(Nullch);
  253.         total.last = first - 1;
  254.         total.first = first;
  255.     }
  256.     start = total.last + 1;
  257.     if (start < last - max + 1) {
  258.         start = last - max + 1;
  259.     }
  260.     process_articles(first, last);
  261.     putchar('\n') FLUSH;
  262.     if (!write_data(tmpname)) {
  263.         exit(1);
  264.     }
  265.     }
  266.     return 0;
  267. }
  268.  
  269. SIGRET
  270. int_handler(sig)
  271. int sig;
  272. {
  273.     /* Simply bug out with an error flag. */
  274.     printf("interrupt %d!\n", sig) FLUSH;
  275.     exit(1);
  276. }
  277.  
  278. void
  279. wrap_it_up(ret)
  280. int ret;
  281. {
  282.     exit(ret);
  283. }
  284.  
  285. /* Generate a "log entry" for the interactive user.
  286. */
  287. /*VARARGS1*/
  288. void
  289. log_entry(fmt, arg1, arg2)
  290. char *fmt;
  291. long arg1;
  292. long arg2;
  293. {
  294.     printf("tmpthread: ");
  295.     printf(fmt, arg1, arg2);
  296.     fflush(stdout);
  297. }
  298.  
  299. /* Generate a "log entry", with newsgroup name for the interactive user.
  300. */
  301. /*VARARGS1*/
  302. void
  303. log_error(fmt, arg1, arg2, arg3)
  304. char *fmt;
  305. long arg1;
  306. long arg2;
  307. long arg3;
  308. {
  309.     log_entry("%s: ", group);
  310.     printf(fmt, arg1, arg2, arg3);
  311.     fflush(stdout);
  312. }
  313.