home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / GENSUP / CHG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-27  |  4.5 KB  |  214 lines

  1. /* ==( gensup/chg.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   Nig   1-Jan-90                        */
  9. /* Modified  Nig   1-Jan-90                        */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13.  
  14. # include <stdio.h>
  15. # include <bench.h>
  16. # include <proc.io>
  17. # include "field.h"
  18. # include "iodef.h"
  19. # include "sup.h"
  20. # include "screen.h"
  21. # include "passwd.h"
  22.  
  23. extern int tot_size;
  24. extern int pending;
  25.  
  26. /*
  27.  * Change Mode !! Revelation
  28. */
  29. int change_mode(tab, head, lptr, table, mode)
  30. struct _table *tab;
  31. struct a_line **head, **lptr;
  32. int table, mode;
  33. {
  34.     int stat, try = 0;
  35.     int cc;
  36.  
  37.     if (!(tab->perms & PERM_CHANGE))
  38.     {
  39.         errmsg(ErrNoChg);
  40.         return (IOPERM);
  41.     }
  42.  
  43.     if (*lptr == ANULL)
  44.         return (IOGOOD);
  45.  
  46.     if ((*lptr)->deleted == TRUE)
  47.     {
  48.         errmsg(ErrDelete);
  49.         return (IOGOOD);
  50.     }
  51.  
  52.     /*
  53.      * Save current Record
  54.     */
  55.    (*lptr)->curr = (struct a_line *) alloc(tot_size);
  56.       bytecpy((*lptr)->curr->rec, tab->rec, tab->size);
  57.  
  58.     /*
  59.      * Lock the required record
  60.     */
  61.     no_msg = !(tab->messages);
  62.  
  63.     stat = lock_rec(tab->fd, tab->rec);
  64.  
  65.     if (stat != IOGOOD)
  66.     {
  67.         free ((*lptr)->curr);
  68.         (*lptr)->curr = ANULL;
  69.         return (do_error(stat));
  70.     }
  71.  
  72.     /*
  73.      * Switch on Transaction Processing for the duration
  74.     */
  75.     if (tab->tp)
  76.         tran_p(tab);
  77.  
  78.     /*
  79.      * Enter Details here
  80.      * and update current record
  81.     */
  82.     do
  83.     {
  84.         ichar = K_CR;
  85.  
  86.         cc = (*tab->inp_fn)(CHG, tab, head, lptr, table);
  87.  
  88.         /*
  89.          * Old Way !!
  90.        if (cc == FALSE && bytecmp(tab->rec, (*lptr)->curr->rec, tab->size))
  91.         */
  92.         if (cc == FALSE && cmp_rec(tab, (*lptr)->curr))
  93.             if (tab->query_box && !warning(99, UpdAbandon))
  94.                 cc = TRUE;
  95.     }
  96.     while (cc != FALSE);
  97.  
  98.     /*
  99.      * If the user hits ESC, then we unlock the record
  100.      * but if he has hit CHG, then it will be unlocked 
  101.      * anyway. Problem - NIG
  102.     */
  103.     no_msg = !(tab->messages);
  104.  
  105.     ulock_rec(tab->fd, tab->rec);
  106.  
  107.     /*
  108.      * Ok, if the user gets here then he has hit ESC or
  109.      * dropped throught normally.  If there are transactions
  110.      * pending, then we need to commit them.
  111.     */
  112.     if (tab->tp)
  113.         comm_p(tab);
  114.  
  115.     /*
  116.      * Restore Previous values if ESC hit.
  117.      * If ESC was not hit, then curr contains
  118.      * the current record anyway.
  119.     */
  120.       bytecpy((*lptr)->rec, (*lptr)->curr->rec, tab->size);
  121.       bytecpy(tab->rec, (*lptr)->rec, tab->size);
  122.  
  123.     /*
  124.      * Re-display the Current Record in 'Selected' attribute
  125.     */
  126.     if (tab->dsp_fn != (void (*)())0) 
  127.           (*tab->dsp_fn)(UNDERLINED, mode);
  128.  
  129.     free ((*lptr)->curr);
  130.     (*lptr)->curr = ANULL;
  131.  
  132.     return (IOGOOD);
  133. }
  134.  
  135. /*
  136.  * Re-write the record if it's value has changed
  137. */
  138. int rewrite_mode(tab, head, lptr)
  139. struct _table *tab;
  140. struct a_line **head, **lptr;
  141. {
  142.     int stat;
  143.  
  144.     /*
  145.      * No change, No update
  146.      *
  147.      * OK, because of the scan_table routine, makes tab->rec & 
  148.      * lptr->rec the same, we also need to check against the 
  149.      * curr->rec value, which is the value of the record before 
  150.      * any changes are made.
  151.     */
  152.     if ((*lptr)->curr == ANULL)
  153.     {
  154.         errmsg(ErrNoRec);
  155.         return (FALSE);
  156.     }
  157.  
  158.     if (!cmp_rec(tab, (*lptr)->curr))
  159.     {
  160.         /*
  161.          * Not a query, but the same sort of message
  162.         */
  163.         if (tab->query_box)
  164.             errmsg(UpdNoChg);
  165.         return (FALSE);
  166.     }
  167.  
  168.     if (tab->query_box && !warning(99, UpdSave))
  169.         return (FALSE);
  170.  
  171.     /*
  172.      * Record should already be Locked at this stage.
  173.     */
  174.     no_msg = !(tab->messages);
  175.  
  176.     /*
  177.      * If you perform an x-ref to the same table, then the current
  178.      * record pointer will no longer be valid here.  If you need the 
  179.      * record to be valid, then the table must have a Unique key.  
  180.      * Select this key, search on the record, copy in the new values 
  181.      * the update. 
  182.      * Re-Select the regular current key afterwards though to make sure
  183.      * that the sequencing is correct.
  184.     */
  185.  
  186.     stat = updrec(tab->fd, tab->rec);
  187.  
  188.     /*
  189.      * Commit or Rollback the pending operations
  190.     */
  191.     pending = TRUE;
  192.  
  193.     if (tab->tp)
  194.     {
  195.         if (stat == IOGOOD)
  196.             comm_p(tab);
  197.         else
  198.             roll_p(tab);
  199.     }
  200.  
  201.     if (stat != IOGOOD)
  202.         lock_rec(tab->fd, tab->rec);
  203.  
  204.     if (do_error(stat) != IOGOOD)
  205.         return (FALSE);
  206.  
  207.     bytecpy((*lptr)->curr->rec, tab->rec, tab->size);
  208.     bytecpy((*lptr)->rec, tab->rec, tab->size);
  209.  
  210.     return (TRUE);
  211. }
  212.  
  213.  
  214.