"Fossies" - the Fresh Open Source Software Archive

Member "gawk-5.1.0/msg.c" (6 Feb 2020, 4334 Bytes) of package /linux/misc/gawk-5.1.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "msg.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.0.1_vs_5.1.0.

    1 /*
    2  * msg.c - routines for error messages.
    3  */
    4 
    5 /*
    6  * Copyright (C) 1986, 1988, 1989, 1991-2001, 2003, 2010-2013, 2017-2019,
    7  * the Free Software Foundation, Inc.
    8  *
    9  * This file is part of GAWK, the GNU implementation of the
   10  * AWK Programming Language.
   11  *
   12  * GAWK is free software; you can redistribute it and/or modify
   13  * it under the terms of the GNU General Public License as published by
   14  * the Free Software Foundation; either version 2, or (at your option)
   15  * any later version.
   16  *
   17  * GAWK is distributed in the hope that it will be useful,
   18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20  * GNU General Public License for more details.
   21  *
   22  * You should have received a copy of the GNU General Public License
   23  * along with this program; if not, write to the Free Software
   24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
   25  */
   26 
   27 #include "awk.h"
   28 
   29 extern FILE *output_fp;
   30 int sourceline = 0;
   31 char *source = NULL;
   32 static const char *srcfile = NULL;
   33 static int srcline;
   34 
   35 jmp_buf fatal_tag;
   36 int fatal_tag_valid = 0;
   37 
   38 /* err --- print an error message with source line and file and record */
   39 
   40 /* VARARGS2 */
   41 void
   42 err(bool isfatal, const char *s, const char *emsg, va_list argp)
   43 {
   44     char *file;
   45     const char *me;
   46 
   47     static bool first = true;
   48     static bool add_src_info = false;
   49     static long lineno_val = 0; // Easter Egg
   50 
   51     if (first) {
   52         first = false;
   53         add_src_info = (getenv("GAWK_MSG_SRC") != NULL);
   54         if (! do_traditional) {
   55             NODE *n = lookup("LINENO");
   56 
   57             if (n != NULL && n->type == Node_var)
   58                 lineno_val = get_number_d(n->var_value);
   59         }
   60     }
   61 
   62     (void) fflush(output_fp);
   63     me = myname;
   64     (void) fprintf(stderr, "%s: ", me);
   65 
   66     if (srcfile != NULL && add_src_info) {
   67         fprintf(stderr, "%s:%d:", srcfile, srcline);
   68         srcfile = NULL;
   69     }
   70 
   71     if (sourceline > 0) {
   72         if (source != NULL)
   73             (void) fprintf(stderr, "%s:", source);
   74         else
   75             (void) fprintf(stderr, _("cmd. line:"));
   76 
   77         (void) fprintf(stderr, "%ld: ", sourceline + lineno_val);
   78     }
   79 
   80 #ifdef HAVE_MPFR
   81     if (FNR_node && is_mpg_number(FNR_node->var_value)) {
   82         NODE *val;
   83         val = mpg_update_var(FNR_node);
   84         assert((val->flags & MPZN) != 0);
   85         if (mpz_sgn(val->mpg_i) > 0) {
   86             int len = FILENAME_node->var_value->stlen;
   87             file = FILENAME_node->var_value->stptr;
   88             (void) putc('(', stderr);
   89             if (file)
   90                 (void) fprintf(stderr, "FILENAME=%.*s ", len, file);
   91             (void) mpfr_fprintf(stderr, "FNR=%Zd) ", val->mpg_i);
   92         }
   93     } else
   94 #endif
   95     if (FNR > 0) {
   96         int len = FILENAME_node->var_value->stlen;
   97         file = FILENAME_node->var_value->stptr;
   98         (void) putc('(', stderr);
   99         if (file)
  100             (void) fprintf(stderr, "FILENAME=%.*s ", len, file);
  101         (void) fprintf(stderr, "FNR=%ld) ", FNR);
  102     }
  103 
  104     (void) fprintf(stderr, "%s", s);
  105     vfprintf(stderr, emsg, argp);
  106     (void) fprintf(stderr, "\n");
  107     (void) fflush(stderr);
  108 
  109     if (isfatal) {
  110 #ifdef GAWKDEBUG
  111         // GLIBC 2.27 doesn't necessarily flush on abort. Sigh.
  112         fflush(NULL);
  113         abort();
  114 #endif
  115         gawk_exit(EXIT_FATAL);
  116     }
  117 }
  118 
  119 /* msg --- take a varargs error message and print it */
  120 
  121 void
  122 msg(const char *mesg, ...)
  123 {
  124     va_list args;
  125     va_start(args, mesg);
  126     err(false, "", mesg, args);
  127     va_end(args);
  128 }
  129 
  130 /* r_warning --- print a warning message */
  131 
  132 void
  133 r_warning(const char *mesg, ...)
  134 {
  135     va_list args;
  136     va_start(args, mesg);
  137     err(false, _("warning: "), mesg, args);
  138     va_end(args);
  139 }
  140 
  141 void
  142 error(const char *mesg, ...)
  143 {
  144     va_list args;
  145     va_start(args, mesg);
  146     err(false, _("error: "), mesg, args);
  147     va_end(args);
  148 }
  149 
  150 /* set_loc --- set location where a fatal error happened */
  151 
  152 void
  153 set_loc(const char *file, int line)
  154 {
  155     srcfile = file;
  156     srcline = line;
  157 
  158     /* This stupid line keeps some compilers happy: */
  159     file = srcfile; line = srcline;
  160 }
  161 
  162 /* r_fatal --- print a fatal error message */
  163 
  164 void
  165 r_fatal(const char *mesg, ...)
  166 {
  167     va_list args;
  168     va_start(args, mesg);
  169     err(true, _("fatal: "), mesg, args);
  170     va_end(args);
  171 }
  172 
  173 /* gawk_exit --- longjmp out if necessary */
  174 
  175 void
  176 gawk_exit(int status)
  177 {
  178     if (fatal_tag_valid) {
  179         exit_val = status;
  180         longjmp(fatal_tag, 1);
  181     }
  182 
  183     final_exit(status);
  184 }
  185 
  186 /* final_exit --- run extension exit handlers and exit */
  187 
  188 void
  189 final_exit(int status)
  190 {
  191     /* run any extension exit handlers */
  192     run_ext_exit_handlers(status);
  193 
  194     /* we could close_io() here */
  195     close_extensions();
  196 
  197     exit(status);
  198 }