website: Add german page with contact information
[ccbib.git] / psutils / pserror.c
blob114d7e1b5fa364fb0af13fc1db84841380d74f54
1 /* pserror.c
2 * Copyright (C) Angus J. C. Duggan 1991-1995
3 * See file LICENSE for details.
5 * Warnings and errors for PS programs
6 */
8 extern char *program ; /* Defined by main program, giving program name */
10 #include "psutil.h"
11 #include "pserror.h"
12 #include "patchlev.h"
14 #include <string.h>
16 /* Message functions; there is a single are varargs functions for messages,
17 warnings, and errors sent to stderr. If called with the flags MESSAGE_EXIT
18 set, the routine does not return */
20 #define MAX_MESSAGE 256 /* maximum formatted message length */
21 #define MAX_FORMAT 16 /* maximum format length */
22 #define MAX_COLUMN 78 /* maximum column to print upto */
24 void message(int flags, char *format, ...)
26 va_list args ;
27 static column = 0 ; /* current screen column for message wrap */
28 char msgbuf[MAX_MESSAGE] ; /* buffer in which to put the message */
29 char *bufptr = msgbuf ; /* message buffer pointer */
31 if ( (flags & MESSAGE_NL) && column != 0 ) { /* new line if not already */
32 putc('\n', stderr) ;
33 column = 0 ;
36 if ( flags & MESSAGE_PROGRAM ) {
37 strcpy(bufptr, program) ;
38 bufptr += strlen(program) ;
39 *bufptr++ = ':' ;
40 *bufptr++ = ' ' ;
43 va_start(args, format) ;
44 if ( format != NULL ) {
45 char c ;
46 while ( (c = *format++) != '\0' ) {
47 if (c == '%') {
48 int done, longform, index ;
49 char fmtbuf[MAX_FORMAT] ;
50 longform = index = 0 ;
51 fmtbuf[index++] = c ;
52 do {
53 done = 1 ;
54 fmtbuf[index++] = c = *format++ ;
55 fmtbuf[index] = '\0' ;
56 switch (c) {
57 case '%':
58 *bufptr++ = '%' ;
59 case '\0':
60 break ;
61 case 'e': case 'E': case 'f': case 'g': case 'G':
63 double d = va_arg(args, double) ;
64 sprintf(bufptr, fmtbuf, d) ;
65 bufptr += strlen(bufptr) ;
67 break ;
68 case 'c': case 'd': case 'i': case 'o':
69 case 'p': case 'u': case 'x': case 'X':
70 if ( longform ) {
71 long l = va_arg(args, long) ;
72 sprintf(bufptr, fmtbuf, l) ;
73 } else {
74 int i = va_arg(args, int) ;
75 sprintf(bufptr, fmtbuf, i) ;
77 bufptr += strlen(bufptr) ;
78 break ;
79 case 's':
81 char *s = va_arg(args, char *) ;
82 sprintf(bufptr, fmtbuf, s) ;
83 bufptr += strlen(bufptr) ;
85 break ;
86 case 'l':
87 longform = 1 ;
88 /* FALLTHRU */
89 default:
90 done = 0 ;
92 } while ( !done ) ;
93 } else if ( c == '\n' ) { /* write out message so far and reset column */
94 int len = bufptr - msgbuf ; /* length of current message */
95 *bufptr++ = '\n' ;
96 *bufptr = '\0' ;
97 if ( column + len > MAX_COLUMN && column > 0 ) {
98 putc('\n', stderr) ;
99 column = 0 ;
101 fputs(bufptr = msgbuf, stderr) ;
102 column = 0 ;
103 } else
104 *bufptr++ = c ;
106 *bufptr = '\0' ;
108 int len = bufptr - msgbuf ; /* length of current message */
109 if ( column + len > MAX_COLUMN && column > 0 ) {
110 putc('\n', stderr) ;
111 column = 0 ;
113 fputs(msgbuf, stderr) ;
114 column += len ;
116 fflush(stderr) ;
118 va_end(args) ;
120 if ( flags & MESSAGE_EXIT ) /* don't return to program */
121 exit(1) ;