home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / trn / src / tmpthrea.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-04  |  7.0 KB  |  322 lines

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