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

  1. /* $Id: sw.c,v 4.4.3.1 1992/02/01 03:09:32 sob PATCH_3 sob $
  2.  *
  3.  * $Log: sw.c,v $
  4.  * Revision 4.4.3.1  1992/02/01  03:09:32  sob
  5.  * Release 4.4 Patchlevel 3
  6.  *
  7.  * Revision 4.4  1991/09/09  20:27:37  sob
  8.  * release 4.4
  9.  *
  10.  *
  11.  * 
  12.  */
  13. /* This software is Copyright 1991 by Stan Barber. 
  14.  *
  15.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  16.  * use this software as long as: there is no monetary profit gained
  17.  * specifically from the use or reproduction of this software, it is not
  18.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  19.  * included prominently in any copy made. 
  20.  *
  21.  * The author make no claims as to the fitness or correctness of this software
  22.  * for any use whatsoever, and it is provided as is. Any use of this software
  23.  * is at the user's own risk. 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "util.h"
  29. #include "head.h"
  30. #include "only.h"
  31. #include "term.h"
  32. #include "ng.h"
  33. #include "intrp.h"
  34. #include "INTERN.h"
  35. #include "sw.h"
  36.  
  37. void
  38. sw_init(argc,argv,tcbufptr)
  39. int argc;
  40. char *argv[];
  41. char **tcbufptr;
  42. {
  43.     register int i;
  44.  
  45.     if (argc >= 2 && strEQ(argv[1],"-c"))
  46.     checkflag=TRUE;            /* so we can optimize for -c */
  47.     interp(*tcbufptr,1024,GLOBINIT);
  48.     sw_file(tcbufptr,FALSE);
  49. #ifdef USETHREADS
  50.     if (!use_threads || !*safecpy(*tcbufptr,getenv("TRNINIT"),1024))
  51. #endif
  52.     safecpy(*tcbufptr,getenv("RNINIT"),1024);
  53.     change_bsl2sl(*tcbufptr);
  54.     if (**tcbufptr) {
  55.     if ((**tcbufptr == '/') || (**tcbufptr == '\\') ||
  56.         (*((*tcbufptr)+2) == '/') || (*((*tcbufptr)+2) == '\\')) {
  57.         sw_file(tcbufptr,TRUE);
  58.     }
  59.     else
  60.         sw_list(*tcbufptr);
  61.     }
  62.  
  63.     for (i = 1; i < argc; i++)
  64.     decode_switch(argv[i]);
  65. }
  66.  
  67. void
  68. sw_file(tcbufptr,bleat)
  69. char **tcbufptr;
  70. bool_int bleat;
  71. {
  72.     int initfd = open(*tcbufptr,0);
  73.     
  74.     if (initfd >= 0) {
  75.     fstat(initfd,&filestat);
  76.     if (filestat.st_size > 1024)
  77.         *tcbufptr = saferealloc(*tcbufptr,(MEM_SIZE)filestat.st_size);
  78.     if (filestat.st_size) {
  79.         read(initfd,*tcbufptr,(int)filestat.st_size);
  80.         (*tcbufptr)[filestat.st_size-1] = '\0';
  81.                 /* wipe out last newline */
  82.         sw_list(*tcbufptr);
  83.     }
  84.     else
  85.         **tcbufptr = '\0';
  86.     close(initfd);
  87.     }
  88.     else {
  89.     if (bleat)
  90.         printf(cantopen,*tcbufptr) ; FLUSH;
  91.     **tcbufptr = '\0';
  92.     }
  93. }
  94.  
  95. /* decode a list of space separated switches */
  96.  
  97. void
  98. sw_list(swlist)
  99. char *swlist;
  100. {
  101.     char *tmplist = safemalloc((MEM_SIZE) strlen(swlist) + 2);
  102.                     /* semi-automatic string */
  103.     register char *p, inquote = 0;
  104.  
  105.     strcpy(tmplist,swlist);
  106.     for (p=tmplist; isspace(*p); p++) ;    /* skip any initial spaces */
  107.     while (*p) {            /* "String, or nothing" */
  108.     if (!inquote && isspace(*p)) {    /* word delimiter? */
  109.         *p++ = '\0';        /* chop here */
  110.         while (isspace(*p))        /* these will be ignored later */
  111.         p++;
  112.     }
  113.     else if (inquote == *p) {
  114.         strcpy(p,p+1);        /* delete trailing quote */
  115.         inquote = 0;        /* no longer quoting */
  116.     }
  117.     else if (!inquote && *p == '"' || *p == '\'') {
  118.                     /* OK, I know when I am not wanted */
  119.         inquote = *p;        /* remember single or double */
  120.         strcpy(p,p+1);        /* delete the quote */
  121.     }                /* (crude, but effective) */
  122.     else if (*p == '\\') {        /* quoted something? */
  123.         if (p[1] == '\n')        /* newline? */
  124.         strcpy(p,p+2);        /* "I didn't see anything" */
  125.         else {
  126.         strcpy(p,p+1);        /* delete the backwhack */
  127.         p++;            /* leave the whatever alone */
  128.         }
  129.     }
  130.     else
  131.         p++;            /* normal char, leave it alone */
  132.     }
  133.     *++p = '\0';            /* put an extra null on the end */
  134.     if (inquote)
  135.     printf("Unmatched %c in switch\n",inquote) ; FLUSH;
  136.     for (p = tmplist; *p; /* p += strlen(p)+1 */ ) {
  137.     decode_switch(p);
  138.     while (*p++) ;            /* point at null + 1 */
  139.     }
  140.     free(tmplist);            /* this oughta be in Ada */
  141. }
  142.  
  143. /* decode a single switch */
  144.  
  145. void
  146. decode_switch(s)
  147. register char *s;
  148. {
  149.     while (isspace(*s))            /* ignore leading spaces */
  150.     s++;
  151. #ifdef DEBUGGING
  152.     if (debug)
  153.     printf("Switch: %s\n",s) ; FLUSH;
  154. #endif
  155.     if (*s != '-' && *s != '+') {    /* newsgroup pattern */
  156.     setngtodo(s);
  157.     }
  158.     else {                /* normal switch */
  159.     bool upordown = *s == '-' ? TRUE : FALSE;
  160.     char tmpbuf[LBUFLEN];
  161.  
  162.     s++;
  163.     switch (*s) {
  164. #ifdef TERMMOD
  165.     case '=': {
  166.         char *beg = s+1;
  167.  
  168.         while (*s && *s != '-' && *s != '+') s++;
  169.         cpytill(tmpbuf,beg,*s);
  170.         if (upordown ? strEQ(getenv("TERM"),tmpbuf)
  171.                  : strNE(getenv("TERM"),tmpbuf) ) {
  172.         decode_switch(s);
  173.         }
  174.         break;
  175.     }
  176. #endif
  177. #ifdef BAUDMOD
  178.     case '0': case '1': case '2': case '3': case '4':
  179.     case '5': case '6': case '7': case '8': case '9':
  180.         if (upordown ? (just_a_sec*10 <= atoi(s))
  181.                  : (just_a_sec*10 >= atoi(s)) ) {
  182.         while (isdigit(*s)) s++;
  183.         decode_switch(s);
  184.         }
  185.         break;
  186. #endif
  187.     case '/':
  188.         if (checkflag)
  189.         break;
  190. #ifdef SETENV
  191.         setenv("SAVEDIR",  upordown ? "%p/%c" : "%p" );
  192.         setenv("SAVENAME", upordown ? "%a"    : "%^C");
  193. #else
  194.         notincl("-/");
  195. #endif
  196.         break;
  197.     case 'a':
  198. #ifdef USETHREADS
  199.         thread_always = upordown;
  200. #else
  201.         notincl("-a");
  202. #endif
  203.         break;
  204.     case 'b':
  205. #ifdef USETHREADS
  206.         breadth_first = upordown;
  207. #else
  208.         notincl("-b");
  209. #endif
  210.         break;
  211.     case 'c':
  212.         checkflag = upordown;
  213.         break;
  214.     case 'C':
  215.         s++;
  216.         if (*s == '=') s++;
  217.         docheckwhen = atoi(s);
  218.         break;
  219.     case 'd': {
  220.         if (checkflag)
  221.         break;
  222.         s++;
  223.         if (*s == '=') s++;
  224.         if (cwd) {
  225.         chdir(cwd);
  226.         free(cwd);
  227.         }
  228.         cwd = savestr(s);
  229.         change_bsl2sl(cwd);
  230.         break;
  231.     }
  232. #ifdef DEBUGGING
  233.     case 'D':
  234.         s++;
  235.         if (*s == '=') s++;
  236.         if (*s)
  237.         if (upordown)
  238.             debug |= atoi(s);
  239.         else
  240.             debug &= ~atoi(s);
  241.         else
  242.         if (upordown)
  243.             debug |= 1;
  244.         else
  245.             debug = 0;
  246.         break;
  247. #endif
  248.     case 'e':
  249.         erase_screen = upordown;
  250.         break;
  251.     case 'E':
  252. #ifdef SETENV
  253.         s++;
  254.         if (*s == '=')
  255.         s++;
  256.         strcpy(tmpbuf,s);
  257.         s = index(tmpbuf,'=');
  258.         if (s) {
  259.         *s++ = '\0';
  260.         setenv(tmpbuf,s);
  261.         }
  262.         else
  263.         setenv(tmpbuf,nullstr);
  264. #else
  265.         notincl("-E");
  266. #endif
  267.         break;
  268.     case 'F':
  269.         s++;
  270.         indstr = savestr(s);
  271.         break;
  272. #ifdef INNERSEARCH
  273.     case 'g':
  274.         gline = atoi(s+1)-1;
  275.         break;
  276. #endif
  277.     case 'H':
  278.     case 'h': {
  279.         register int len, i;
  280.         char *t;
  281.         int flag = (*s == 'h' ? HT_HIDE : HT_MAGIC);
  282.         
  283.         if (checkflag)
  284.         break;
  285.         s++;
  286.         len = strlen(s);
  287.         for (t=s; *t; t++)
  288.         if (isupper(*t))
  289.            *t = tolower(*t);
  290.         for (i=HEAD_FIRST; i<HEAD_LAST; i++)
  291.         if (!len || strnEQ(s,htype[i].ht_name,len))
  292.             if (upordown)
  293.             htype[i].ht_flags |= flag;
  294.             else
  295.             htype[i].ht_flags &= ~flag;
  296.         break;
  297.     }
  298.     case 'i':
  299.         s++;
  300.         if (*s == '=') s++;
  301.         initlines = atoi(s);
  302.         initlines_specified = TRUE;
  303.         break;
  304.     case 'j':
  305.         dont_filter_control = TRUE;
  306.         break;
  307.     case 'l':
  308.         muck_up_clear = upordown;
  309.         break;
  310.     case 'L':
  311. #ifdef CLEAREOL
  312.         can_home_clear = upordown;
  313. #else
  314.         notincl("-L");
  315. #endif
  316.         break;
  317.     case 'M':
  318.         mbox_always = upordown;
  319.         break;
  320.     case 'm':
  321.         s++;
  322.         if (*s == '=') s++;
  323.         if (!upordown)
  324.         marking = NOMARKING;
  325.         else if (*s == 'u')
  326.         marking = UNDERLINE;
  327.         else {
  328.         marking = STANDOUT;
  329.         }
  330.         break;
  331.     case 'N':
  332.         norm_always = upordown;
  333.         break;
  334. #ifdef VERBOSE
  335.     case 'n':
  336.         fputs("This isn't readnews.  Don't use -n.\n\n",stdout) ; FLUSH;
  337.         break;
  338. #endif
  339.     case 'o':
  340.         s++;
  341.         if (*s == '=') s++;
  342.         if (*s <= '9' && *s >= '0') {
  343.         olden_days = atoi(s);
  344.         do {
  345.             s++;
  346.         } while (*s <= '9' && *s >= '0');
  347.         } else
  348.         olden_days = upordown;
  349.         break;
  350.     case 'r':
  351.         findlast = upordown;
  352.         break;
  353.     case 's':
  354.         s++;
  355.         if (*s == '=') s++;
  356.         if (*s) {
  357.         countdown = atoi(s);
  358.         suppress_cn = FALSE;
  359.         }
  360.         else {
  361.         if (!upordown)
  362.             countdown = 5;
  363.         suppress_cn = upordown;
  364.         }
  365.         break;
  366.     case 'S':
  367. #ifdef ARTSEARCH
  368.         s++;
  369.         if (*s == '=') s++;
  370.         if (*s)
  371.         scanon = atoi(s);
  372.         else
  373.         scanon = upordown*3;
  374. #else
  375.         notincl("-S");
  376. #endif
  377.         break;
  378.     case 't':
  379. #ifdef VERBOSE
  380. #ifdef TERSE
  381.         verbose = !upordown;
  382. #else
  383.         notincl("+t");
  384. #endif
  385. #else
  386.         notincl("+t");
  387. #endif
  388.         break;
  389.     case 'T':
  390.         typeahead = upordown;
  391.         break;
  392.     case 'v':
  393. #ifdef VERIFY
  394.         verify = upordown;
  395. #else
  396.         notincl("-v");
  397. #endif
  398.         break;
  399.     case 'x':
  400. #ifdef USETHREADS
  401.         s++;
  402.         if (*s == '=') s++;
  403.         if (*s <= '9' && *s >= '0') {
  404.         if ((max_tree_lines = atoi(s)) > 11)
  405.             max_tree_lines = 11;
  406.         do {
  407.             s++;
  408.         } while (*s <= '9' && *s >= '0');
  409.         } else
  410.         max_tree_lines = 6;
  411.         if (*s)
  412.         strncpy(select_order, s, 3);
  413.         use_threads = upordown;
  414. #else
  415.         notincl("-x");
  416. #endif
  417.         break;
  418.     case 'X':
  419. #ifdef USETHREADS
  420.         s++;
  421.         if (*s == '=') s++;
  422.         if (*s <= '9' && *s >= '0') {
  423.         select_on = atoi(s);
  424.         do {
  425.             s++;
  426.         } while (*s <= '9' && *s >= '0');
  427.         } else
  428.         select_on = upordown;
  429.         if (*s)
  430.         end_select = *s++;
  431.         if (*s)
  432.         page_select = *s;
  433. #else
  434.         notincl("-X");
  435. #endif
  436.         break;
  437.     /*
  438.      * People want a way to avoid checking for new newsgroups on startup.
  439.      */
  440.     case 'q':
  441.         quickstart = upordown;
  442.         break;
  443.     default:
  444. #ifdef VERBOSE
  445.         IF(verbose) {
  446.         printf("\nIgnoring unrecognized switch: -%c\n", *s) ; FLUSH;
  447.         }
  448.         ELSE
  449. #endif
  450. #ifdef TERSE
  451.         printf("\nIgnoring -%c\n", *s) ; FLUSH;
  452. #endif
  453.         break;
  454.     }
  455.     }
  456. }
  457.  
  458. /* print current switch values */
  459.  
  460. void
  461. pr_switches()
  462. {
  463.     static char mp[2] = {'+','-'};
  464.     register int i;
  465.     
  466.     fputs("\nCurrent switch settings:\n",stdout);
  467.     printf("%c/ ", mp[strEQ(getval("SAVEDIR",SAVEDIR),"%p/%c")]);
  468.     printf("%ca ", mp[thread_always]);
  469.     printf("%cb ", mp[breadth_first]);
  470.     printf("%cc ", mp[checkflag]);
  471.     printf("-C%d ", docheckwhen);
  472.     printf("-d%s ", cwd);
  473. #ifdef DEBUGGING
  474.     if (debug)
  475.     printf("-D%d ", debug);
  476. #endif
  477.     printf("%ce ", mp[erase_screen]);
  478.     printf("-F\"%s\" ", indstr);
  479. #ifdef INNERSEARCH
  480.     printf("-g%d", gline);
  481. #endif
  482.     putchar('\n');
  483. #ifdef VERBOSE
  484.     if (verbose) {
  485.     for (i=HEAD_FIRST; i<HEAD_LAST; i++)
  486.         printf("%ch%s%c",
  487.         mp[htype[i].ht_flags & HT_HIDE], htype[i].ht_name,
  488.         (! (i % 5) ? '\n' : ' ') );
  489.     }
  490. #endif
  491.     printf("-i%d ", initlines);
  492.     printf("%cj ", mp[dont_filter_control]);
  493.     printf("%cl ", mp[muck_up_clear]);
  494. #ifdef CLEAREOL
  495.     printf("%cL ", mp[can_home_clear]);
  496. #endif /* CLEAREOL */
  497.     if (marking)
  498.     printf("-m%c ",marking==UNDERLINE?'u':'s');
  499.     else
  500.     printf("+m ");
  501.     printf("%cM ", mp[mbox_always]);
  502.     printf("%cN ", mp[norm_always]);
  503.     if (olden_days)
  504.     printf("-o%d ", olden_days);
  505.     else
  506.     printf("+o ");
  507.     printf("%cr ", mp[findlast]);
  508.     if (countdown)
  509.     printf("-s%d ", countdown);
  510.     else
  511.     printf("%cs ", mp[suppress_cn]);
  512. #ifdef ARTSEARCH
  513.     if (scanon)
  514.     printf("-S%d ",scanon);
  515.     else
  516.     printf("+S ");
  517. #endif
  518. #ifdef VERBOSE
  519. #ifdef TERSE
  520.     printf("%ct ", mp[!verbose]);
  521. #endif
  522. #endif
  523.     printf("%cT ", mp[typeahead]);
  524. #ifdef VERIFY
  525.     printf("%cv ", mp[verify]);
  526. #endif
  527. #ifdef USETHREADS
  528.     if (use_threads)
  529.     printf("-x%d%s ",max_tree_lines,select_order);
  530.     else
  531.     printf("+x ");
  532.     if (select_on)
  533.     printf("-X%d%c%c ",select_on,end_select,page_select);
  534.     else
  535.     printf("+X ");
  536. #endif
  537.     fputs("\n\n",stdout) ; FLUSH;
  538. #ifdef ONLY
  539.     if (maxngtodo) {
  540. #ifdef VERBOSE
  541.     IF(verbose)
  542.         fputs("Current restriction:",stdout);
  543.     ELSE
  544. #endif
  545. #ifdef TERSE
  546.         fputs("Only:",stdout);
  547. #endif
  548.     for (i=0; i<maxngtodo; i++)
  549.         printf(" %s",ngtodo[i]);
  550.     fputs("\n\n",stdout) ; FLUSH;
  551.     }
  552. #ifdef VERBOSE
  553.     else if (verbose)
  554.     fputs("No restriction.\n\n",stdout) ; FLUSH;
  555. #endif
  556. #endif
  557. }
  558.  
  559. void
  560. cwd_check()
  561. {
  562.     char tmpbuf[LBUFLEN];
  563.  
  564.     if (!cwd)
  565.     cwd = savestr(filexp("~/News"));
  566.     change_bsl2sl(cwd);
  567.     strcpy(tmpbuf,cwd);
  568.     if (chdir(cwd)) {
  569.     safecpy(tmpbuf,filexp(cwd),sizeof tmpbuf);
  570.     if (makedir(tmpbuf,MD_DIR) < 0 || chdir(tmpbuf) < 0) {
  571.         interp(cmd_buf, (sizeof cmd_buf), "%~/News");
  572.         if (makedir(cmd_buf,MD_DIR) < 0)
  573.         strcpy(tmpbuf,homedir);
  574.         else
  575.         strcpy(tmpbuf,cmd_buf);
  576.         chdir(tmpbuf);
  577. #ifdef VERBOSE
  578.         IF(verbose) {
  579.         printf("\
  580. Cannot make directory %s--\n\
  581.     articles will be saved to %s\n\
  582. \n\
  583. ",cwd,tmpbuf) ; FLUSH;
  584.         }
  585.         ELSE
  586. #endif
  587. #ifdef TERSE
  588.         printf("\
  589. Can't make %s--\n\
  590.     using %s\n\
  591. \n\
  592. ",cwd,tmpbuf) ; FLUSH;
  593. #endif
  594.     }
  595.     }
  596.     free(cwd);
  597.     _getcwd2(tmpbuf, 512);
  598.     if (eaccess(tmpbuf,2)) {
  599. #ifdef VERBOSE
  600.     IF(verbose) {
  601.         printf("\
  602. Current directory %s is not writeable--\n\
  603.     articles will be saved to home directory\n\n\
  604. ",tmpbuf) ; FLUSH;
  605.     }
  606.     ELSE
  607. #endif
  608. #ifdef TERSE
  609.         printf("%s not writeable--using ~\n\n",tmpbuf) ; FLUSH;
  610. #endif
  611.     strcpy(tmpbuf,homedir);
  612.     }
  613.     cwd = savestr(tmpbuf);
  614.     change_bsl2sl(cwd);
  615. }
  616.