home *** CD-ROM | disk | FTP | other *** search
- # include <stdio.h>
- # include <bench.h>
- # include <proc.io>
- # include "field.h"
- # include "screen.h"
- # include "iodef.h"
- # include "sup.h"
- # include "passwd.h"
-
- /*
- * Free up the memory used by a list
- */
- void freelist_mode(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- struct a_line *tmp;
-
- /*
- * Get straight out if the list is empty
- */
- if (*lptr == ANULL)
- return;
-
- /*
- * Find the end of the list
- */
- if (*lptr != ANULL)
- while ((*lptr)->next != ANULL)
- *lptr = (*lptr)->next;
-
- /*
- * Free each record in turn
- */
- while (*lptr != ANULL)
- {
- tmp = (*lptr)->prev;
- free((char *)*lptr);
- *lptr = tmp;
- }
-
- /*
- * Reset pointers
- */
- *lptr = *head = ANULL;
-
- /*
- * Clear the display
- */
- do_page_mode(tab, head, lptr, CLR);
-
- *(tab->index) = 0; /* If we clear the list, then reset the pointer */
- }
-
- /*
- * Driving routine behind building a list of records
- *
- * Used for doing X-Ref Read/Write & Read, at 1st
- * sub level where linkage can be 1 -> N
- */
- int fill_list_mode(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- int stat, try = 0, maxread = 0;
-
- /*
- * Make sure we are searching on the right key
- */
- selectinx(tab->fd, *(tab->keynum), *(tab->keymatch), tab->retry);
-
- no_msg = !(tab->messages);
-
- /*
- * Get first record in sequence
- */
- switch (tab->mode)
- {
- case K_HOME :
- stat = firstkey(tab->fd, tab->rec);
- break;
- case K_END :
- stat = lastkey(tab->fd, tab->rec);
- break;
- default :
- stat = findkey(tab->fd, tab->rec);
- }
-
- /*
- * Try & Lock retry times before failing
- */
- while (stat != IOGOOD && ++try <= tab->retry)
- {
- if (stat != IOGOOD && stat != IOLOCKED)
- {
- if (tab->dsp_fn != (void (*)())0)
- (*tab->dsp_fn)(UNDERLINED, CLR);
- return (do_error(stat));
- }
-
- stat = lock_rec(tab->fd, tab->rec);
- }
-
- if (stat != IOGOOD)
- return (do_error(stat));
-
- /*
- * Unlock if previously locked
- */
- if (tab->retry != 0)
- ulock_rec(tab->fd, tab->rec);
-
- /*
- * We found at least one record, so free up the list
- * and build it again
- */
- freelist_mode(tab, head, lptr);
-
- /*
- * Having found the 1st record ok, the messages can be
- * disabled here to avoid ugly messages
- */
- no_msg = TRUE;
-
- /*
- * Get rest of records to maximum display size
- */
- if (*(tab->seq) == ASCENDING)
- do
- new_line(tab, head, lptr);
- while (++maxread < tab->maximum && (stat = nextrec(tab->fd, tab->rec)) == IOGOOD);
- else
- do
- new_line(tab, head, lptr);
- while (++maxread < tab->maximum && (stat = prevrec(tab->fd, tab->rec)) == IOGOOD);
-
- /*
- * Reset Current Record if more
- * than 1 record in list
- */
- if (tab->maximum > 1 && *head != ANULL)
- {
- bytecpy(tab->rec, (*head)->rec, tab->size);
-
- /*
- * Don't forget to reset the current record
- */
- switch (tab->mode)
- {
- case K_HOME :
- stat = firstkey(tab->fd, tab->rec);
- break;
- case K_END :
- stat = lastkey(tab->fd, tab->rec);
- break;
- default :
- stat = findkey(tab->fd, tab->rec);
- }
-
- /*
- * Try & Lock retry times before failing
- */
- while (stat != IOGOOD && ++try <= tab->retry)
- {
- if (stat != IOGOOD && stat != IOLOCKED)
- {
- if (tab->dsp_fn != (void (*)())0)
- (*tab->dsp_fn)(UNDERLINED, CLR);
- return (do_error(stat));
- }
-
- stat = lock_rec(tab->fd, tab->rec);
- }
-
- if (stat != IOGOOD)
- return (do_error(stat));
-
- /*
- * Unlock if previously locked
- */
- if (tab->retry != 0)
- ulock_rec(tab->fd, tab->rec);
- }
- return(IOGOOD);
- }
-
- /*
- * Size of the biggest record in any program
- * using a union of the record structures
- */
- extern int tot_size;
-
- void new_record(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- struct a_line *tmp, *ptr;
-
- /*
- * Allocate enough room for the record & pointers
- */
- tmp = (struct a_line *) alloc(tot_size);
- tmp->deleted = FALSE;
-
- tmp->prev = tmp->next = ANULL;
-
- bytecpy(tmp->rec, tab->rec, tab->size);
-
- /*
- * Adjust pointers depending on the direction
- * we are adding this record
- */
- ptr = *lptr;
-
- if (*(tab->seq) == ASCENDING)
- {
- if (ptr != ANULL)
- while (ptr->next != ANULL)
- ptr = ptr->next;
-
- if (ptr != ANULL)
- ptr->next = tmp;
- tmp->prev = ptr;
- tmp->next = ANULL;
- }
- else /* up */
- {
- if (ptr != ANULL)
- while (ptr->prev != ANULL)
- ptr = ptr->prev;
-
- if (ptr != ANULL)
- ptr->prev = tmp;
- tmp->next = ptr;
- tmp->prev = ANULL;
- }
-
- *lptr = tmp;
-
- /*
- * Find the head of the list
- */
- for (*head = *lptr; (*head)->prev != ANULL; (*head) = (*head)->prev)
- ;
- }
-
- /*
- * Insert a new record into the list
- */
- void new_line(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- new_record(tab, head, lptr);
-
- (*(tab->index))++;
- }
-
- /*
- * Same as above, but don't increment the index
- */
- void new_line_2(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- new_record(tab, head, lptr);
- }
-
- /*
- * Read records into list starting from the top of file
- */
- int first_mode(tab, head, lptr, table, mode)
- struct _table *tab;
- struct a_line **head, **lptr;
- int table, mode;
- {
- int stat;
-
- if (!(tab->perms & PERM_INQUIRE))
- {
- errmsg(ERR_NOINQ);
- return (IOPERM);
- }
-
- zerorec(tab->rec, tab->size);
-
- *(tab->seq) = ASCENDING;
- tab->mode = K_HOME;
- *(tab->index) = 0;
-
- if ((stat = fill_list_mode(tab, head, lptr)) != IOGOOD)
- return (stat);
-
- *(tab->index) = 0;
-
- head_list(tab, head, lptr, mode);
-
- return (stat);
- }
-
- /*
- * Read records into list starting from the end of file
- */
- int last_mode(tab, head, lptr, table, mode)
- struct _table *tab;
- struct a_line **head, **lptr;
- int table, mode;
- {
- int stat;
-
- if (!(tab->perms & PERM_INQUIRE))
- {
- errmsg(ERR_NOINQ);
- return (IOPERM);
- }
-
- zerorec(tab->rec, tab->size);
-
- *(tab->seq) = DESCENDING;
- tab->mode = K_END;
- *(tab->index) = 0;
-
- if ((stat = fill_list_mode(tab, head, lptr)) != IOGOOD)
- return(stat);
-
- (*(tab->index))--;
-
- /* *(tab->index) = 0; */
-
- head_list(tab, head, lptr, mode);
-
- return(stat);
- }
-
- /*
- * Read the file, using a user entered
- * value as the first record to find
- */
- int inquire_mode(tab, head, lptr, table, mode)
- struct _table *tab;
- struct a_line **head, **lptr;
- int table, mode;
- {
- int stat, try = 0, curidx; /*TVE*/
- struct a_line *curr = ANULL;
- struct a_line *top = ANULL; /*TVE*/
-
- if (!(tab->perms & PERM_INQUIRE))
- {
- errmsg(ERR_NOINQ);
- return (IOPERM);
- }
-
- /*
- * Take copy of current record
- * and top record (TVE)
- */
- if (*lptr != ANULL)
- {
- curr = (struct a_line *) alloc(tot_size);
- bytecpy(curr->rec, (*lptr)->rec, tab->size);
- curidx = *(tab->index); /*TVE*/
- top = (struct a_line *) alloc(tot_size); /*TVE*/
- bytecpy(top->rec, (*head)->rec, tab->size); /*TVE*/
- }
-
- /*
- * get search value
- *
- * build key is generated in the screen program
- */
- ichar = K_CR;
- if (tab->key_fn != (void (*)())0)
- (*tab->key_fn)(*(tab->keynum));
-
- if (ichar == K_ESC)
- {
- if (curr != ANULL)
- {
- bytecpy((*lptr)->rec, curr->rec, tab->size);
- bytecpy(tab->rec, (*lptr)->rec, tab->size);
- bytecpy((*head)->rec, top->rec, tab->size); /*TVE*/
- do_page_mode(tab, head, lptr, mode); /*TVE*/
- /*if (tab->dsp_fn != (void (*)())0)
- (*tab->dsp_fn)(UNDERLINED, mode);*/
-
- free (curr);
- curr = ANULL;
- free (top); /*TVE*/
- top = ANULL; /*TVE*/
- }
- return (IOGOOD);
- }
-
- *(tab->seq) = ASCENDING;
- tab->mode = 0;
- *(tab->index) = 0;
-
- /*
- * Load 'em up, and restore old value if none to load
- */
- if ((stat = fill_list_mode(tab, head, lptr)) != IOGOOD)
- {
- if (curr != ANULL)
- {
- bytecpy((*lptr)->rec, curr->rec, tab->size);
- bytecpy(tab->rec, (*lptr)->rec, tab->size);
- bytecpy((*head)->rec, top->rec, tab->size); /*TVE*/
- *(tab->index) = curidx; /*TVE*/
- if (tab->dsp_fn != (void (*)())0)
- (*tab->dsp_fn)(UNDERLINED, mode);
-
- /*
- * Re-find the current record
- */
- stat = findkey(tab->fd, tab->rec);
-
- /*
- * Try & Lock retry times before failing
- */
- while (stat != IOGOOD && ++try <= tab->retry)
- {
- if (stat != IOGOOD && stat != IOLOCKED)
- return (do_error(stat));
-
- stat = lock_rec(tab->fd, tab->rec);
- }
-
- if (stat != IOGOOD)
- return (do_error(stat));
-
- /*
- * If we didn't find right one, (because of dup keys)
- * we better make sure the one we found
- * is the current one.
- * Also, since it's not the right one,
- * might as well make it the top one in the list
- * - will often get lucky, and this will be the previous top record,
- * - or it will be easier for the user to use next & prevs
- * to re-order the list properly
- * TVE
- * could use fill_list to rebuild the list?
- * - but should do same as add_mode to be consistent
- *
- * Now does the same as Add mode. Again this is only
- * called from the main table and update links, so
- * fill_list is safe.
- * NIG
- */
- if (memcmp((*lptr)->rec, tab->rec, tab->size) != 0)
- {
- if (tab->messages)
- errmsg(NO_CURR_EST);
- bytecpy((*head)->rec, tab->rec, tab->size);
- *lptr = *head;
- *(tab->index) = 0;
-
- fill_list_mode(tab, head, lptr);
- }
-
- free (top); /*TVE*/
- top = ANULL; /*TVE*/
- free (curr);
- curr = ANULL;
-
- do_page_mode(tab, head, lptr, mode); /*TVE*/
- }
- return (do_error(stat));
- }
-
- *(tab->index) = 0;
-
- if (curr != ANULL)
- {
- free (curr);
- curr = ANULL;
- }
-
- head_list(tab, head, lptr, mode);
-
- return (stat);
- }
-
- /*
- * Driving routine behind building a list of records
- *
- * Used for doing X-Ref Read Only stuff at sub-sub levels
- * where linkage is basically 1 -> 1.
- */
- int make_list(tab, head, lptr)
- struct _table *tab;
- struct a_line **head, **lptr;
- {
- int stat, read = 0;
- struct a_line *tmp;
-
- /*
- * Make sure we are searching on the right key
- */
- selectinx(tab->fd, *(tab->keynum), *(tab->keymatch), tab->retry);
-
- no_msg = !(tab->messages);
-
- /*
- * Get record
- */
- stat = findkey(tab->fd, tab->rec);
-
- if (do_error(stat) != IOGOOD)
- zerorec(tab->rec, tab->size);
-
- /*
- * Reset index if empty list
- */
- if (*head == ANULL)
- *(tab->index) = 0;
-
- /*
- * Check for already in the list
- */
- for (read = 0, tmp = *head; tmp != ANULL; tmp = tmp->next, read++)
- {
- if (read == *(tab->index))
- {
- bytecpy(tmp->rec, tab->rec, tab->size);
- *lptr = tmp;
- (*lptr)->deleted = (stat != IOGOOD); /*TVE*/
- return (stat); /*TVE*/
- }
- }
-
- /*
- * New one so Add on the list
- */
- new_line_2(tab, head, lptr);
-
- (*lptr)->deleted = (stat != IOGOOD); /*TVE*/
-
- return (stat);
- }
-
-