home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / ngstuff.c < prev    next >
C/C++ Source or Header  |  1993-03-01  |  13KB  |  569 lines

  1. /* $Id: ngstuff.c,v 4.4 1991/09/09 20:23:31 sob Exp sob $
  2.  *
  3.  * $Log: ngstuff.c,v $
  4.  * Revision 4.4  1991/09/09  20:23:31  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 "term.h"
  26. #include "util.h"
  27. #include "ng.h"
  28. #include "bits.h"
  29. #include "intrp.h"
  30. #include "cheat.h"
  31. #include "head.h"
  32. #include "final.h"
  33. #include "sw.h"
  34. #ifdef USETHREADS
  35. #include "threads.h"
  36. #include "rthreads.h"
  37. #include "rn.h"
  38. #include "rcstuff.h"
  39. #endif
  40. #include "decode.h"
  41. #include "INTERN.h"
  42. #include "ngstuff.h"
  43.  
  44. void
  45. ngstuff_init()
  46. {
  47.     ;
  48. }
  49.  
  50. /* do a shell escape */
  51.  
  52. int
  53. escapade()
  54. {
  55.     register char *s;
  56.     bool interactive = (buf[1] == FINISHCMD);
  57.     bool docd;
  58.     char whereiam[512];
  59.  
  60.     if (!finish_command(interactive))    /* get remainder of command */
  61.     return -1;
  62.     change_bsl2sl(buf);
  63.     s = buf+1;
  64.     docd = *s != '!';
  65.     if (!docd) {
  66.     s++;
  67.     }
  68.     else {
  69.     _getcwd2(whereiam, 512);
  70.     if (chdir(cwd)) {
  71.         printf(nocd,cwd) ; FLUSH;
  72.         sig_catcher(0);
  73.     }
  74.     }
  75.     while (*s == ' ') s++;
  76.                     /* skip leading spaces */
  77.     interp(cmd_buf, (sizeof cmd_buf), s);/* interpret any % escapes */
  78.     change_sl2bsl(cmd_buf);
  79.     resetty();                /* make sure tty is friendly */
  80.     doshell(Nullch,cmd_buf);    /* invoke the shell */
  81.     noecho();                /* and make terminal */
  82.     crmode();                /*   unfriendly again */
  83.     termlib_init();
  84.     clear();
  85.     if (docd) {
  86.     if (chdir(whereiam)) {
  87.         printf(nocd,whereiam) ; FLUSH;
  88.         sig_catcher(0);
  89.     }
  90.     }
  91. #ifdef MAILCALL
  92.     mailcount = 0;            /* force recheck */
  93. #endif
  94.     return 0;
  95. }
  96.  
  97. /* process & command */
  98.  
  99. int
  100. switcheroo()
  101. {
  102.     if (!finish_command(TRUE)) /* get rest of command */
  103.     return -1;    /* if rubbed out, try something else */
  104.     if (!buf[1])
  105.     pr_switches();
  106. #ifdef PUSHBACK
  107.     else if (buf[1] == '&') {
  108.     if (!buf[2]) {
  109.         page_init();
  110.         show_macros();
  111.     }
  112.     else {
  113.         char tmpbuf[LBUFLEN];
  114.         register char *s;
  115.  
  116.         for (s=buf+2; isspace(*s); s++);
  117.         mac_line(s,tmpbuf,(sizeof tmpbuf));
  118.     }
  119.     }
  120. #endif
  121.     else {
  122.     bool docd = (instr(buf,"-d", TRUE) != Nullch);
  123.      char whereami[512];
  124.  
  125.     if (docd)
  126.         _getcwd2(whereami);
  127.     sw_list(buf+1);
  128.     if (docd) {
  129.         cwd_check();
  130.         if (chdir(whereami)) {        /* -d does chdirs */
  131.         printf(nocd,whereami) ; FLUSH;
  132.         sig_catcher(0);
  133.         }
  134.     }
  135.     }
  136.     return 0;
  137. }
  138.  
  139. /* process range commands */
  140.  
  141. int
  142. numnum()
  143. {
  144.     ART_NUM min, max;
  145.     char *cmdlst = Nullch;
  146.     register char *s, *c;
  147.     ART_NUM oldart = art;
  148.     char tmpbuf[LBUFLEN];
  149.     bool justone = TRUE;        /* assume only one article */
  150.  
  151.     perform_cnt = 0;
  152.     if (!finish_command(TRUE))    /* get rest of command */
  153.     return NN_INP;
  154.     if (lastart < 1) {
  155.         fputs("\nNo articles\n",stdout) ; FLUSH;
  156.         return NN_ASK;
  157.     }
  158. #ifdef ARTSRCH
  159.     if (srchahead)
  160.     srchahead = -1;
  161. #endif
  162.     for (s=buf; *s && (isdigit(*s) || index(" ,-.$",*s)); s++)
  163.     if (!isdigit(*s))
  164.         justone = FALSE;
  165.     if (*s) {
  166.     cmdlst = savestr(s);
  167.     justone = FALSE;
  168.     }
  169.     else if (!justone)
  170.     cmdlst = savestr("m");
  171.     *s++ = ',';
  172.     *s = '\0';
  173.     safecpy(tmpbuf,buf,LBUFLEN);
  174.     for (s = tmpbuf; c = index(s,','); s = ++c) {
  175.     *c = '\0';
  176.     if (*s == '.')
  177.         min = oldart;
  178.     else
  179.         min = atol(s);
  180. #ifdef USETHREADS
  181.     if (min<absfirst && justone) {
  182.         int r;
  183.  
  184.         /* Check if this is a root number */
  185.         for (r = 0; r < total.root; r++) {
  186.         if (p_roots[r].root_num == min) {
  187.             p_art = p_articles + p_roots[r].articles;
  188.             art = p_art->num;
  189.             if (p_art->subject == -1) {
  190.             follow_thread('N');
  191.             }
  192.             return NN_REREAD;
  193.         }
  194.         }
  195.     }
  196. #endif
  197.     if (min<absfirst) {        /* make sure it is reasonable */
  198.         min = absfirst;
  199.         printf("(First article is %ld)\n",(long)absfirst) ; FLUSH;
  200.         pad(just_a_sec/3);
  201.     }
  202.     if ((s=index(s,'-')) != Nullch) {
  203.         s++;
  204.         if (*s == '$')
  205.         max = lastart;
  206.         else if (*s == '.')
  207.         max = oldart;
  208.         else
  209.         max = atol(s);
  210.     }
  211.     else
  212.         max = min;
  213.     if (max>lastart) {
  214.         max = lastart;
  215.         if (min > max)
  216.         min = max;
  217.         printf("(Last article is %ld)\n",(long)lastart) ; FLUSH;
  218.         pad(just_a_sec/3);
  219.     }
  220.     if (max < min) {
  221.         fputs("\nBad range\n",stdout) ; FLUSH;
  222.         if (cmdlst)
  223.         free(cmdlst);
  224.         return NN_ASK;
  225.     }
  226.     if (justone) {
  227.         art = min;
  228.         return NN_REREAD;
  229.     }
  230.     check_first(min);
  231.     for (art=min; art<=max; art++) {
  232.         if (perform(cmdlst,TRUE)) {
  233. #ifdef VERBOSE
  234.         IF(verbose) {
  235.             printf("\n(Interrupted at article %ld)\n",(long)art)
  236.               ; FLUSH;
  237.         }
  238.         ELSE
  239. #endif
  240. #ifdef TERSE
  241.             printf("\n(Intr at %ld)\n",(long)art) ; FLUSH;
  242. #endif
  243.         if (cmdlst)
  244.             free(cmdlst);
  245.         return NN_ASK;
  246.         }
  247.     }
  248.     }
  249.     art = oldart;
  250.     if (cmdlst)
  251.     free(cmdlst);
  252.     return NN_NORM;
  253. }
  254.  
  255. #ifdef USETHREADS
  256. int
  257. use_selected()
  258. {
  259.     PACKED_ARTICLE *root_limit;
  260.     register char ch;
  261.     register int r;
  262.     char *cmdstr;
  263.     int ret = 1, orig_root_cnt = selected_root_cnt;
  264.  
  265.     if (!finish_command(TRUE))    /* get rest of command */
  266.     return 0;
  267.     if (!(ch = buf[1]))
  268.     return -1;
  269.     cmdstr = savestr(buf+1);
  270.  
  271.     perform_cnt = 0;
  272.     page_line = 1;
  273.  
  274.     /* Multiple commands and commands that operate on individual articles
  275.     ** use the article loop.
  276.     */
  277.     if (strlen(cmdstr) > 1 || index("ejmMsSwW|=", ch)) {
  278.     bool want_unread = (unread_selector || ch == 'm');
  279.  
  280.     for (r = 0; r < total.root; r++) {
  281.         if (scan_all_roots
  282.          || (!orig_root_cnt&&root_article_cnts[r]&&!(selected_roots[r]&4))
  283.          || (selected_roots[r] & (unread_selector+1))) {
  284.         p_art = p_articles + p_roots[r].articles;
  285.         root_limit = upper_limit( p_art, 0 );
  286.         for (; p_art < root_limit; p_art++) {
  287.             art = p_art->num;
  288.             if (p_art->subject != -1
  289.              && (!was_read(art) ^ want_unread)) {
  290.             if (perform(cmdstr, TRUE)) {
  291.                 fputs("\nInterrupted\n", stdout) ; FLUSH;
  292.                 goto break_out;
  293.             }
  294.             }
  295.             if (p_art == Nullart)
  296.             break;
  297.         }/* for all articles */
  298.         }/* if selected */
  299.     }/* for all threads */
  300.     }                /* other commands get the root loop */
  301.     else if (ch == '+' || ch == '-' || ch == 'J' || ch == 'T' || ch == 't') {
  302.     for (r = 0; r < total.root; r++) {
  303.         if (scan_all_roots
  304.          || (!orig_root_cnt&&root_article_cnts[r]&&!(selected_roots[r]&4))
  305.          || (selected_roots[r] & (unread_selector+1))) {
  306.         if (mode != 't' && ch != 't') {
  307.             printf("T%-5ld ", (long)p_roots[r].root_num);
  308.         }
  309.         p_art = p_articles + p_roots[r].articles;
  310.         art = p_art->num;
  311.         if (perform(cmdstr, FALSE)) {
  312.             fputs("\nInterrupted\n", stdout) ; FLUSH;
  313.             goto break_out;
  314.         }
  315. #ifdef VERBOSE
  316.         IF(verbose)
  317.             if (mode != 't' && ch != 't' && ch != 'T')
  318.             putchar('\n') ; FLUSH;
  319. #endif
  320.         }
  321.     }
  322.     }
  323.     else if (ch == 'E') {    /* one command needs no looping at all */
  324.     if (decode_fp) {
  325.         decode_end();
  326.     } else {
  327.         ret = 2;
  328.     }
  329.     }
  330.     else {
  331.     printf("???%s\n",cmdstr);
  332.     ret = -1;
  333.     }
  334.   break_out:
  335.     free(cmdstr);
  336.     return ret;
  337. }
  338. #endif
  339.  
  340. int
  341. perform(cmdlst,toplevel)
  342. register char *cmdlst;
  343. int toplevel;
  344. {
  345.     register int ch;
  346.     char     *original_cmdlst;
  347.     char     to_be_restored_cmdlst[1024];
  348.     bool     restore_cmdlst = FALSE;
  349.  
  350.     original_cmdlst = cmdlst;
  351.     strncpy(to_be_restored_cmdlst, cmdlst, 1023);
  352.  
  353.     if (toplevel) {
  354.     printf("%-6ld ",art);
  355.     fflush(stdout);
  356.     }
  357.     perform_cnt++;
  358.     for (; ch = *cmdlst; cmdlst++) {
  359.     if (isspace(ch) || ch == ':')
  360.         continue;
  361.     if (ch == 'j') {
  362.         if (!was_read(art)) {
  363.         mark_as_read();
  364. #ifdef VERBOSE
  365.         IF(verbose)
  366.             fputs("\tJunked",stdout);
  367. #endif
  368.         }
  369. #ifdef USETHREADS
  370.         else if (unread_selector)
  371.         goto unselect_it;
  372. #endif
  373.     }
  374. #ifdef USETHREADS
  375.     else if (ch == '+') {
  376.       register char mask = unread_selector+1;
  377.         find_article(art);
  378.         if (p_art && !(selected_roots[p_art->root] & mask)) {
  379.           register int r = p_art->root;
  380.         selected_roots[r] |= mask;
  381.         selected_root_cnt++;
  382.         if (mode == 't') {
  383.             selected_count += root_article_cnts[r];
  384.         } else {
  385.             selected_count += count_one_root(r);
  386. #ifdef VERBOSE
  387.             IF(verbose)
  388.             fputs("\tSelected",stdout);
  389. #endif
  390.         }
  391.         }
  392.     }
  393.     else if (ch == '-') {
  394.       register char mask;
  395.       unselect_it:
  396.         mask = unread_selector+1;
  397.         find_article(art);
  398.         if (p_art && selected_root_cnt
  399.          && (selected_roots[p_art->root] & mask)) {
  400.           register int r = p_art->root;
  401.         selected_roots[r] &= ~mask;
  402.         if (unread_selector)
  403.             selected_roots[r] |= 4;
  404.         selected_root_cnt--;
  405.         if (mode == 't') {
  406.             selected_count -= root_article_cnts[r];
  407.         } else {
  408.             selected_count -= count_one_root(r);
  409. #ifdef VERBOSE
  410.             IF(verbose)
  411.             fputs("\tDeselected",stdout);
  412. #endif
  413.         }
  414.         }
  415.     }
  416.     else if (ch == 't') {
  417.         find_article(art);
  418.         entire_tree();
  419.     }
  420.     else if (ch == 'J' || ch == 'T' || ch == ',') {
  421.         char tmpbuf[128];
  422.         ART_NUM oldart = art;
  423.  
  424.         find_article(art);
  425.         if (ch == ',')
  426.         mark_as_read();
  427.         if (p_art) {
  428.         if (ch == 'T') {
  429.             sprintf(tmpbuf,"T%ld\t# %s",
  430.             (long)p_roots[p_art->root].root_num,
  431.             subject_ptrs[p_art->subject]);
  432.             fputs(tmpbuf,stdout);
  433.             kf_append(tmpbuf);
  434.         }
  435.         follow_thread(ch == ',' ? 'K' : 'J');
  436.         art = oldart;
  437.         }
  438.     }
  439. #endif
  440.     else if (ch == 'm') {
  441.         if (was_read(art)) {
  442.         unmark_as_read();
  443. #ifdef VERBOSE
  444.         IF(verbose)
  445.             fputs("\tMarked unread",stdout);
  446. #endif
  447.         }
  448.     }
  449.     else if (ch == 'M') {
  450. #ifdef DELAYMARK
  451.         delay_unmark(art);
  452. #ifdef VERBOSE
  453.         IF(verbose)
  454.         fputs("\tWill return",stdout);
  455. #endif
  456. #else
  457.         notincl("M");
  458.         return -1;
  459. #endif
  460.     }
  461.     else if (ch == '=') {
  462.         printf("\t%s",fetchsubj(art,FALSE,FALSE));
  463. #ifdef VERBOSE
  464.         IF(verbose)
  465.         ;
  466.         ELSE
  467. #endif
  468.         putchar('\n') ; FLUSH;        /* ghad! */
  469.     }
  470.     else if (ch == 'C') {
  471. #ifdef ASYNC_PARSE
  472.         printf("\t%sancelled",(cancel_article() ? "Not c" : "C"));
  473. #else
  474.         notincl("C");
  475.         return -1;
  476. #endif
  477.     }
  478.     else if (ch == '%') {
  479. #ifdef ASYNC_PARSE
  480.         char tmpbuf[512];
  481.  
  482.         if (one_command)
  483.         interp(tmpbuf, (sizeof tmpbuf), cmdlst);
  484.         else
  485.         cmdlst = dointerp(tmpbuf, (sizeof tmpbuf), cmdlst, ":") - 1;
  486.         perform_cnt--;
  487.         if (perform(tmpbuf,FALSE))
  488.         return -1;
  489. #else
  490.         notincl("%");
  491.         return -1;
  492. #endif
  493.     }
  494.     else if (index("!&sSwWe|",ch)) {
  495. /** OS2: unter normal TRN, it is possible to enter the following
  496.          command    range{,range}:command{:command}     This is now
  497.          no longer possible, because the ':' for the second command
  498.          can not be correctly detected. A ':' can also be part of a
  499.          filename that is entered for the first command.
  500.  
  501.          This is the old code:
  502.  
  503.         if (one_command)
  504.         strcpy(buf,cmdlst);
  505.         else
  506.         cmdlst = cpytill(buf,cmdlst,':') - 1;
  507.  
  508.          This is the new code:    **/
  509.  
  510.             strcpy(buf,cmdlst);
  511.             *(cmdlst+1) = '\0';
  512.             restore_cmdlst = TRUE;
  513.  
  514. /** OS2: end of patch **/
  515.  
  516.         /* we now have the command in buf */
  517.         if (ch == '!') {
  518.         escapade();
  519. #ifdef VERBOSE
  520.         IF(verbose)
  521.             fputs("\tShell escaped",stdout);
  522. #endif
  523.         }
  524.         else if (ch == '&') {
  525.         switcheroo();
  526. #ifdef VERBOSE
  527.         IF(verbose)
  528.             if (buf[1] && buf[1] != '&')
  529.             fputs("\tSwitched",stdout);
  530. #endif
  531.         }
  532.         else {
  533.         putchar('\t');
  534.         save_article();
  535. #ifdef VERBOSE
  536.         IF(verbose)
  537.             ;
  538.         ELSE
  539. #endif
  540.             putchar('\n') ; FLUSH;
  541.         }
  542.     }
  543.     else {
  544.         printf("\t???%s\n",cmdlst);
  545.         return -1;
  546.     }
  547. #ifdef VERBOSE
  548.     fflush(stdout);
  549. #endif
  550.     if (one_command)
  551.         break;
  552.     }
  553.     if (toplevel) {
  554. #ifdef VERBOSE
  555.     IF(verbose)
  556.         putchar('\n') ; FLUSH;
  557. #endif
  558.     }
  559.     if( int_count ) {
  560.     int_count = 0;
  561.     return -1;
  562.     }
  563.     if (restore_cmdlst)
  564.     {   cmdlst = original_cmdlst;
  565.         strcpy(cmdlst, to_be_restored_cmdlst);
  566.     }
  567.     return 0;
  568. }
  569.