
/* 
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/
#ident "$Header: xsar2.c 1.1 90/01/21 $@(#)xsar2.c	1.1 89/12/01"
#include <stdio.h>
#include "xsar.h"

char *gettxt();

char msg_msgI1[] = "$ M ";
char msg_msgI2[] = "$ =====================================================";
char msg_hdrI1[] = "$ MESSAGES FILES";
char msg_hdrI2[] = "$ SYMBOL TABLES";
char msg_symI1[] = "$ S ";
char msg_symI2[] = "$ =====================================================";
char msg_hdrE1[] = "$set 1";
char msg_hdrE2[] = "$ SYMBOL TABLE";
char msg_startE[]= "$quote \"";
char msg_fct[]   = "$ F ";
char msg_def[]   = "$ D ";
char msg_undef[] = "$ U ";
char msg_init[]  = "$ I ";
char msg_uninit[]= "$ V ";

int len_msgI1 = sizeof(msg_msgI1) - 1;
int len_msgI2 = sizeof(msg_msgI2) - 1;
int len_hdrI1 = sizeof(msg_hdrI1) - 1;
int len_hdrI2 = sizeof(msg_hdrI2) - 1;
int len_symI1 = sizeof(msg_symI1) - 1;
int len_symI2 = sizeof(msg_symI2) - 1;
int len_hdrE1 = sizeof(msg_hdrE1) - 1;
int len_hdrE2 = sizeof(msg_hdrE2) - 1;
int len_startE= sizeof(msg_startE)- 1;
int len_fct   = sizeof(msg_fct)   - 1;
int len_def   = sizeof(msg_def)   - 1;
int len_undef = sizeof(msg_undef) - 1;
int len_init  = sizeof(msg_init)  - 1;
int len_uninit= sizeof(msg_uninit)- 1;

/*
 * Return TRUE if current buf is hdrE1
 */
is_hdrE1()
{
  return (strncmp(buf, msg_hdrE1, len_hdrE1) == 0);
}

/*
 * Return TRUE if current buf is hdrE2
 */
is_hdrE2()
{
  return (strncmp(buf, msg_hdrE2, len_hdrE2) == 0);
}

/*
 * Position file after hdrE1
 */
skip_hdrE1(fd)
  FILE *fd;
{
  while (fgets(buf, BUFSIZ, fd) != 0){
    if (is_hdrE1())
      return;
  }
}

/*
 * Position file after hdrI1
 */
skip_hdrI1(fd)
  FILE *fd;
{
  if (fd == NULL)
    return;
    
  while (fgets(buf, BUFSIZ, fd) != NULL){
    if (is_hdrI1())
      return;
  }
}

/*
 * Return TRUE if current buf is hdrI1
 */
is_hdrI1()
{
  return (strncmp(buf, msg_hdrI1, len_hdrI1) == 0);
}

/*
 * Return TRUE if current buf is msgI1
 */
is_msgI1()
{
  return (strncmp(buf, msg_msgI1, len_msgI1) == 0);
}

/*
 * Return TRUE if current buf is msgI1
 */
is_msgI2()
{
  return (strncmp(buf, msg_msgI2, len_msgI2) == 0);
}

/*
 * Return TRUE if current buf is hdrI2
 */
is_hdrI2()
{
  return (strncmp(buf, msg_hdrI2, len_hdrI2) == 0);
}

/*
 * Seek next symtab in archive
 */
sym_next(fd)
  FILE *fd;
{
  if (fd == NULL)
    return;
    
  if (fgets(buf, BUFSIZ, fd) == NULL)
    return;
  sym_next1(fd);
}

sym_next1(fd)
  FILE *fd;
{
  long curoff = ftell(fd);
  
  while (fgets(buf, BUFSIZ, fd) != NULL){
    if (is_symI1()){
      if (fseek(fd, curoff, 0) != 0){
        perror(gettxt("xsar:24","seek error 4"));
        exit(1);
      }
      return;
    }
    curoff = ftell(fd);
  }
}

/*
 * Return TRUE if current buf is fct
 */
is_fct()
{
  return (strncmp(buf, msg_fct, len_fct) == 0);
}

/*
 * Return TRUE if current buf is symI1
 */
is_symI1()
{
  return (strncmp(buf, msg_symI1, len_symI1) == 0);
}

/*
 * Return TRUE if current buf is symI2
 */
is_symI2()
{
  return (strncmp(buf, msg_symI2, len_symI2) == 0);
}

/*
 * Return TRUE if current buf is def
 */
is_def()
{
  return (strncmp(buf, msg_def, len_def) == 0);
}

/*
 * Return TRUE if current buf is undef
 */
is_undef()
{
  return (strncmp(buf, msg_undef, len_undef) == 0);
}

/*
 * Return TRUE if current buf is init
 */
is_init()
{
  return (strncmp(buf, msg_init, len_init) == 0);
}

/*
 * Return TRUE if current buf is uninit
 */
is_uninit()
{
  return (strncmp(buf, msg_uninit, len_uninit) == 0);
}

/*
 * Copy len from src to dst
 */
copy_len(dst, src, len)
  FILE *dst, *src;
  int len;
{
  int slen;
  while ((slen = len > BUFSIZ ? BUFSIZ : len) > 0){
    if (fread(buf, 1, slen, src) != slen){
      perror(gettxt("xsar:25","read error"));
      exit(1);
    }
    if (fwrite(buf, 1, slen, dst) != slen){
      perror(gettxt("xsar:26","write error"));
      exit(1);
    }
    len -= slen;
  }
}
