home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 419b.lha / GNU_Awk_v2.10_Beta / awk5.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  6KB  |  267 lines

  1. /*
  2.  * routines for error messages
  3.  *
  4.  * $Log:    awk5.c,v $
  5.  * Revision 1.15  89/03/31  13:26:11  david
  6.  * GNU license
  7.  * 
  8.  * Revision 1.14  89/03/30  10:22:23  david
  9.  * fixed up varargs usage
  10.  * 
  11.  * Revision 1.13  89/03/29  14:14:37  david
  12.  * delinting
  13.  * 
  14.  * Revision 1.12  89/03/21  18:31:46  david
  15.  * changed defines for system without vprintf()
  16.  * 
  17.  * Revision 1.11  89/03/21  10:52:53  david
  18.  * cleanup
  19.  * 
  20.  * Revision 1.10  88/12/08  11:00:07  david
  21.  * add $Log:    awk5.c,v $
  22.  * Revision 1.15  89/03/31  13:26:11  david
  23.  * GNU license
  24.  * 
  25.  * Revision 1.14  89/03/30  10:22:23  david
  26.  * fixed up varargs usage
  27.  * 
  28.  * Revision 1.13  89/03/29  14:14:37  david
  29.  * delinting
  30.  * 
  31.  * Revision 1.12  89/03/21  18:31:46  david
  32.  * changed defines for system without vprintf()
  33.  * 
  34.  * Revision 1.11  89/03/21  10:52:53  david
  35.  * cleanup
  36.  * 
  37.  * 
  38.  */
  39.  
  40. /* 
  41.  * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  42.  * 
  43.  * This file is part of GAWK, the GNU implementation of the
  44.  * AWK Progamming Language.
  45.  * 
  46.  * GAWK is free software; you can redistribute it and/or modify
  47.  * it under the terms of the GNU General Public License as published by
  48.  * the Free Software Foundation; either version 1, or (at your option)
  49.  * any later version.
  50.  * 
  51.  * GAWK is distributed in the hope that it will be useful,
  52.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  54.  * GNU General Public License for more details.
  55.  * 
  56.  * You should have received a copy of the GNU General Public License
  57.  * along with GAWK; see the file COPYING.  If not, write to
  58.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  59.  */
  60.  
  61. #include "awk.h"
  62.  
  63. int sourceline = 0;
  64. char *source = NULL;
  65.  
  66. /* VARARGS2 */
  67. static void
  68. err(s, msg, argp)
  69. char *s;
  70. char *msg;
  71. va_list *argp;
  72. {
  73.     int line;
  74.     char *file;
  75.  
  76.     (void) fprintf(stderr, "%s: %s ", myname, s);
  77.     vfprintf(stderr, msg, *argp);
  78.     (void) fprintf(stderr, "\n");
  79.     line = (int) FNR_node->var_value->numbr;
  80.     if (line)
  81.         (void) fprintf(stderr, " input line number %d", line);
  82.     file = FILENAME_node->var_value->stptr;
  83.     if (file && !STREQ(file, "-"))
  84.         (void) fprintf(stderr, ", file `%s'", file);
  85.     (void) fprintf(stderr, "\n");
  86.     if (sourceline)
  87.         (void) fprintf(stderr, " source line number %d", sourceline);
  88.     if (source)
  89.         (void) fprintf(stderr, ", file `%s'", source);
  90.     (void) fprintf(stderr, "\n");
  91. }
  92.  
  93. /*VARARGS0*/
  94. void
  95. msg(va_alist)
  96. va_dcl
  97. {
  98.     va_list args;
  99.     char *mesg;
  100.  
  101.     va_start(args);
  102.     mesg = va_arg(args, char *);
  103.     err("", mesg, &args);
  104.     va_end(args);
  105. }
  106.  
  107. /*VARARGS0*/
  108. void
  109. warning(va_alist)
  110. va_dcl
  111. {
  112.     va_list args;
  113.     char *mesg;
  114.  
  115.     va_start(args);
  116.     mesg = va_arg(args, char *);
  117.     err("warning:", mesg, &args);
  118.     va_end(args);
  119. }
  120.  
  121. /*VARARGS0*/
  122. void
  123. fatal(va_alist)
  124. va_dcl
  125. {
  126.     va_list args;
  127.     char *mesg;
  128.  
  129.     va_start(args);
  130.     mesg = va_arg(args, char *);
  131.     err("fatal error:", mesg, &args);
  132.     va_end(args);
  133. #ifdef DEBUG
  134.     abort();
  135. #endif
  136.     exit(1);
  137. }
  138.  
  139. #if defined(HASDOPRNT) && defined(NOVPRINTF)
  140. int
  141. vsprintf(str, fmt, ap)
  142.     char *str, *fmt;
  143.     va_list ap;
  144. {
  145.     FILE f;
  146.     int len;
  147.  
  148.     f._flag = _IOWRT+_IOSTRG;
  149.     f._ptr = (unsigned char *)str;
  150.     f._cnt = 32767;
  151.     len = _doprnt(fmt, ap, &f);
  152.     *f._ptr = 0;
  153.     return (len);
  154. }
  155.  
  156. int
  157. vfprintf(iop, fmt, ap)
  158.     FILE *iop;
  159.     char *fmt;
  160.     va_list ap;
  161. {
  162.     int len;
  163.  
  164.     len = _doprnt(fmt, ap, iop);
  165.     return (ferror(iop) ? EOF : len);
  166. }
  167.  
  168. int
  169. vprintf(fmt, ap)
  170.     char *fmt;
  171.     va_list ap;
  172. {
  173.     int len;
  174.  
  175.     len = _doprnt(fmt, ap, stdout);
  176.     return (ferror(stdout) ? EOF : len);
  177. }
  178. #endif
  179.  
  180. /*** APPEND AFTER 178 IN x:awk5.c ***/
  181.  
  182. #if defined(NOVPRINTF) && !defined(HASDOPRNT) /* version of vfprintf     */
  183.                                               /* from K&R 2nd ed. pp 156 */
  184. int vfprintf(iop, fmt, ap)
  185. FILE *iop;
  186. char *fmt;
  187. va_list ap;
  188. {
  189.    char buf[20], *p, *r;
  190.    int len = 0;
  191.    unsigned short state = 0;
  192.    
  193.    putc('{', iop); /* for testing, to identify vfprintf output */
  194.    
  195.    for (p = fmt; *p; p++) {
  196.       switch (state) {
  197.       case 0: /* not in % format string segment */
  198.          r = buf;
  199.          switch (*p) {
  200.          case '%':
  201.             *r++ = *p;
  202.             state = 1; /* found start of % format string segment (maybe)*/
  203.             break;
  204.             
  205.          default:
  206.             putc(*p, iop);
  207.             break;
  208.          }
  209.          break;
  210.          
  211.       case 1:
  212.          if (*p == '%') {
  213.             putchar('%');
  214.             state = 0;
  215.             break;
  216.          }
  217.          state = 2; /* and fall through */
  218.       
  219.       case 2: /* parse and execute a % format string segment */
  220.          switch (*p) { /* select fprintf() of appropriate type */
  221.          case 'd':
  222.             *r++ = *p;
  223.             *r = '\0';
  224.             len += fprintf(iop, buf, va_arg(ap, int));
  225.             r = buf;
  226.             state = 0;
  227.             break;
  228.             
  229.          case 's':
  230.             *r++ = *p;
  231.             *r = '\0';
  232.             len += fprintf(iop, buf, va_arg(ap, char *));
  233.             state = 0;
  234.             break;
  235.          
  236.          case '%':
  237.             *r++ = *p;
  238.             *r = '\0';
  239.             fprintf(stderr,
  240.                "vfprintf: unsupported or malformed format %s\n", buf);
  241.             r = buf;
  242.             *r++ = *p;
  243.             state = 1;
  244.             break;
  245.             
  246.          default :
  247.             *r++ = *p;
  248.             break;
  249.             
  250.          } /* end switch (*p) */
  251.          
  252.       } /* end switch (state) */
  253.  
  254.    } /* end for() */
  255.    if (state != 0) {
  256.       r = '\0';
  257.       fprintf(stderr, "vfprintf: unsupported or malformed format %s\n", buf);
  258.    }
  259.    putc('}', iop); /* for testing, to identify vfprintf output */
  260.    return len;
  261.    
  262. } /* end vfprintf() */
  263.                
  264. #endif
  265. /*** APPEND AFTER 178 IN x:awk5.c ***/
  266.  
  267.