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