home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume14 / shellforms / part02 / form.c < prev    next >
C/C++ Source or Header  |  1988-05-09  |  6KB  |  226 lines

  1. /* Last update: 02/08/88  00:18 AM  (Edition: 56) */
  2. #include    <stdio.h>
  3. #include    <ctype.h>
  4. #include    <signal.h>
  5. #include    "form.h"
  6. #include    "field.h"
  7. #include    "term.h"
  8. #include    "basic.h"
  9.  
  10. char    Screen [SCRLINE][SCRCOL+1];
  11. char    Form_name[20] = "";        /* current form name */
  12. char    Exitc;                /* field exit char */
  13.  
  14. char    Enter_mode [20];        /* display before input field */
  15. char    Exit_mode [20];            /* display after input field */
  16.  
  17. struct    field    Field[MAXFIELD];    /* field nodes */
  18. int        Fno;            /* next field to use (or count) */
  19. unsigned char    Fhead[SCRLINE];        /* 1st field index on each line */
  20. unsigned char    Flcount[SCRLINE];    /* number of fields on this line */
  21. unsigned char    Lastdata = 24;        /* last line with data on form */
  22.  
  23. char    Varfile [MAXFNAME];        /* output variable file name */
  24.  
  25. extern    int        Form_msg;    /* defined in msg.c */
  26. extern    unsigned char    Shell;        /* output shell script type */
  27.  
  28. int    sig_interrupt();
  29. int    sig_stop();
  30.  
  31. /*----------------------------------------------------------------------+
  32. |                                    |
  33. |        show_form : display a given form file            |
  34. |                                    |
  35. +----------------------------------------------------------------------*/
  36. show_form (fname)
  37. char    *fname;        /* name of form file, NULL = stdin, 1 = redisplay */
  38.     {
  39.     int        i, j;
  40.     int        idx;        /* index to current field */
  41.     unsigned    size;
  42.     unsigned    off;
  43.     unsigned    len;        /* string length */
  44.     int        pend;        /* previous end position */
  45.     char        *lp;        /* line pointer (for speed) */
  46.     char        *p;
  47.     extern    char    *Prgname;
  48.  
  49.     ENTER(show_form);
  50.     if (fname != (char *)1 &&        /* redisplay */
  51.         (fname == NULL || strcmp (fname, Form_name) != 0)) {
  52.         if (!load_form (fname)) RETURN (0);
  53.         }
  54.  
  55.     screen (SCR_ERASE);
  56.     for (i=0; i<SCRLINE; i++) {
  57.         p = lp = Screen[i];
  58.         if (Flcount[i] != 0) {
  59.             pend = 0;
  60.             for (j=0, idx=Fhead[i]; j<Flcount[i]; j++, idx++) {
  61.                 size = Field[idx].f_len;
  62.                 off = Field[idx].f_off;
  63.                 put_string (p, off-pend);
  64.                 put_string (Enter_mode, 0); 
  65.                 put_string (lp + off, size);
  66.                 put_string (Exit_mode, 0);
  67.                 p = lp + off + size;
  68.                 pend = off + size;
  69.                 }
  70.             }
  71.         if ((len = strlen (p)) == 0) continue;
  72.         put_string (p, len);
  73.         put_char ('\r');
  74.         }
  75.     RETURN (1);
  76.     }
  77.  
  78. /*----------------------------------------------------------------------+
  79. |                                    |
  80. |       edit_form : edit a form on all the input fields        |
  81. |                                    |
  82. +----------------------------------------------------------------------*/
  83. edit_form (fname, action)
  84. char    *fname;        /* name of the form to edit */
  85. int    (*action) ();    /* action routine to check at end of each field */
  86.     {
  87.     unsigned    fdx = 0;    /* current cursor in field index */
  88.     struct    field    *fp;        /* pointer to current edit field */
  89.     int        n;        /* action routine return status */
  90.     int        efopt;        /* option to edit field */
  91.  
  92.     ENTER(edit_form);
  93.     signal (SIGINT, sig_interrupt);
  94.     signal (SIGTSTP, sig_stop);
  95.  
  96.     term_init ();
  97.     cbreakio (1);
  98.     if (!show_form (fname)) goto end;
  99.     if (Fno == 0) goto end;
  100.  
  101.     form_msg ((char *)NULL, 1, 1);
  102.     screen (SCR_KEYXMIT);
  103.     while (1) {
  104.         fp = &Field[fdx];
  105.         efopt = EF_BOF;
  106.         do    {
  107.             if (fp->f_attr & FA_SELECTION) sel_field (fdx);
  108.             else edit_field (fdx, efopt);
  109.             if (!action) break;
  110.             n = (*action) (fdx, &Screen[fp->f_line-1][fp->f_off],
  111.                     fp->f_len, Exitc);
  112.             switch (n) {
  113.                 when EF_ERR:    efopt = EF_BOF;
  114.                 when EF_FILL:    efopt = EF_FILL;
  115.                 }
  116.             } while (n != EF_OK);
  117.  
  118.         if ((Exitc & 0x80) == 0x80) {
  119.             switch (Exitc & 0x7f) {
  120.                 when KU:    Exitc = CTRL('P');
  121.                 when KD:    Exitc = CTRL('N');
  122.                 }
  123.             }
  124.         switch (Exitc) {
  125.             when CTRL('L'):    show_form ((char *)1);
  126.             when CTRL('M'): goto wrout;
  127.             when CTRL('J'):    goto wrout;
  128.             when CTRL('P'):    fdx = closest (fdx, -1);
  129.             when CTRL('N'):    fdx = closest (fdx, 1);
  130.             when CTRL('T'): fdx = (fdx == 0) ? Fno-1 : (fdx-1);
  131.             when CTRL('I'): if (++fdx == Fno) fdx = 0;
  132.             otherwise:    show_summary ();
  133.                     show_form ((char *)1);
  134.             }
  135.         }
  136. wrout:    write_var ();
  137.     poscur (Lastdata, (unsigned char)1, "\n"); screen (SCR_EEOL);
  138. end:
  139.     screen (SCR_NOKEYXMIT);
  140.     cbreakio (0);
  141.     term_close ();
  142.     EXIT;
  143.     }
  144.  
  145. /*-------------------------------------------------------------05/08/86-+
  146. |                                    |
  147. |           write_var : write field variables to a file        |
  148. |                                    |
  149. +----------------------------------------------------------------------*/
  150. write_var ()
  151.     {
  152.     register int    i;
  153.     FILE        *fd;        /* output file pointer */
  154.     char        *p;
  155.     char        *op;
  156.     char        c;
  157.     char        buf[80+1];
  158.     char        obuf[80+1];
  159.     struct    field    *f;
  160.  
  161.     ENTER(write_var);
  162.     if (!strlen (Varfile)) EXIT;
  163.  
  164.     if ((fd = fopen (Varfile, "w")) == NULL) {
  165.         fprintf (stderr, "write_var: can not open file %s for write\r\n",
  166.              Varfile);
  167.         exit (1);
  168.         }
  169.  
  170.     for (i=0; i<Fno; i++) {
  171.         f = &Field[i];
  172.         if (f-> f_var != NULL) {
  173.             get_field (i, buf);
  174.             p = buf;  op = obuf;
  175.             while (c = *p++) {
  176.                 switch (c) {
  177.                     when '\'':    if (Shell== PERL) *op++ = '\\';
  178.                         else    {
  179.                             *op++ ='\'';*op++ ='"';
  180.                             *op++ ='\'';*op++ ='"';
  181.                             }
  182.                     when '!':    if (Shell == CSH) *op++ = '\\';
  183.                     }
  184.                 *op++ = c;
  185.                 }
  186.             *op = EOS;
  187.             fprintf (fd, "%s%s='%s'%s\n",
  188.                  (Shell==CSH)?"set ":((Shell==PERL)?"$":""),
  189.                  f-> f_var, obuf,
  190.                  (Shell == PERL) ? ";" : "");
  191.             }
  192.         }
  193.     fclose (fd);
  194.     EXIT;
  195.     }
  196.  
  197. /*-------------------------------------------------------------01/12/88-+
  198. |                                    |
  199. |         sig_interrupt : Interrupt signal handler        |
  200. |                                    |
  201. +----------------------------------------------------------------------*/
  202. sig_interrupt ()
  203.     {
  204.     ENTER (sig_interrupt);
  205.     form_msg ("Aborted by user\n", 24+1, 1);
  206.     screen (SCR_NORMAL);
  207.     cbreakio (1);
  208.     exit (1);
  209.     }
  210.  
  211. /*-------------------------------------------------------------01/12/88-+
  212. |                                    |
  213. |              sig_stop : Stop signal handler            |
  214. |                                    |
  215. +----------------------------------------------------------------------*/
  216. sig_stop ()
  217.     {
  218.     ENTER (sig_stop);
  219.     form_msg ("stopped by user\n", 24+1, 1);
  220.     screen (SCR_NORMAL);
  221.     cbreakio (0);
  222.     kill (getpid(), SIGSTOP);
  223.     put_string ("Press CTRL L to refresh display: ", 33);
  224.     cbreakio (1);
  225.     }
  226.