1 /****************************************************************************/
3 /* Chaine de verification */
5 /* Produit : AVT Version 1 */
6 /* Fichier : avt_util.c */
8 /* (c) copyright 1998-1999 AVERTEC */
9 /* Tous droits reserves */
10 /* Support : e-mail support@avertec.com */
12 /* Auteur(s) : AUGUSTINS Gilles */
14 /****************************************************************************/
16 #include "avt_headers.h"
21 #define CL_RESET "" // Reset All Attributes (return to normal mode)
22 #define CL_BOLD "1" // Bright (Usually turns on BOLD)
23 #define CL_DIM "2" // Dim
24 #define CL_UDL "3" // Underline
25 #define CL_BLINK "5" // Blink
26 #define CL_REV "7" // Reverse
29 #define FG_BLACK "30" // Black
30 #define FG_RED "31" // Red
31 #define FG_GREEN "32" // Green
32 #define FG_YELLOW "33" // Yellow
33 #define FG_BLUE "34" // Blue
34 #define FG_MAGENTA "35" // Magenta
35 #define FG_CYAN "36" // Cyan
36 #define FG_WHITE "37" // White
38 #define BG_BLACK "40" // Black
39 #define BG_RED "41" // Red
40 #define BG_GREEN "42" // Green
41 #define BG_YELLOW "43" // Yellow
42 #define BG_BLUE "44" // Blue
43 #define BG_MAGENTA "45" // Magenta
44 #define BG_CYAN "46" // Cyan
45 #define BG_WHITE "47" // White
48 #define COLOR(a) "\x1B[" a "m"
62 #define NBCOLORS ((signed)(sizeof(colors)/sizeof(*colors)))
79 for (i
=0;(unsigned)i
<sizeof(colors
)/sizeof(*colors
);i
++)
81 sprintf(temp
,"AVT_COL_%d",i
);
82 e
=avt_gethashvar(temp
);
88 case '+': strcat(temp
,CL_BOLD
); strcat(temp
,";"); e
++; break;
89 case '-': strcat(temp
,CL_DIM
); strcat(temp
,";"); e
++; break;
94 case 'B': strcat(temp
,FG_BLACK
); break;
95 case 'r': strcat(temp
,FG_RED
); break;
96 case 'g': strcat(temp
,FG_GREEN
); break;
97 case 'y': strcat(temp
,FG_YELLOW
); break;
98 case 'b': strcat(temp
,FG_BLUE
); break;
99 case 'm': strcat(temp
,FG_MAGENTA
); break;
100 case 'c': strcat(temp
,FG_CYAN
); break;
101 case 'w': strcat(temp
,FG_WHITE
); break;
105 colors
[i
]=strdup(temp
);
111 void avt_error (char *lib
, int code
, int severity
, char *fmt
, ...)
115 char buf
[8192], error_msg
[8192];
119 vsprintf(buf
, fmt
, pa
);
124 case AVT_ERR
: typemsg
="¤6¤+Error"; break;
125 case AVT_WAR
: typemsg
="¤4Warning"; break;
131 sprintf (error_msg
, "[%s¤.][¤+%s¤.] ", typemsg
, lib
);
133 sprintf (error_msg
, "[%s #%d¤.][¤+%s¤.] ", typemsg
, code
, lib
);
135 car
= strlen (error_msg
);
137 strcpy(&error_msg
[car
], buf
);
138 avt_fprintf (stderr
, "%s", error_msg
);
141 void avt_trace (int level
, FILE *output
, char *fmt
, ...)
145 static int avt_color
= 0;
146 static int color_env
= 1;
150 if ((str
= avt_gethashvar ("AVT_COL")))
151 if (!strcmp (str
, "yes")) {
158 if (isatty (fileno(output
)))
163 if (level
>=0 && (unsigned)level
<sizeof(colors
)/sizeof(*colors
))
164 fprintf (output
, "%s%s",colors
[level
],leveltab
[level
]);
166 fprintf (output
, "%s",leveltab
[0]);
169 fprintf (output
, "%s",level
>=0?leveltab
[level
]:"");
172 vfprintf(output
, fmt
, pa
);
176 fputs(COLOR(CL_RESET
), output
);
179 void avt_trace_va (int level
, FILE *output
, char *fmt
, va_list pa
)
182 static int avt_color
= 0;
183 static int color_env
= 1;
187 if ((str
= avt_gethashvar ("AVT_COL")))
188 if (!strcmp (str
, "yes"))
196 if (isatty (fileno(output
)))
202 if (level
>=0 && (unsigned)level
<sizeof(colors
)/sizeof(*colors
))
203 fprintf (output
, "%s%s",colors
[level
],leveltab
[level
]);
205 fprintf (output
, "%s",leveltab
[0]);
208 fprintf (output
, "%s",level
>=0?leveltab
[level
]:"");
210 vfprintf(output
, fmt
, pa
);
213 fputs(COLOR(CL_RESET
), output
);
216 int avt_terminal(FILE *output
)
218 if (isatty (fileno(output
)))
223 void avt_back_fprintf(FILE *output
, int length
)
225 if (avt_terminal(output
))
228 for (i
=1; i
<=length
;i
++)
229 fprintf (output
, "\b");
233 void avt_fprintf(FILE *output
, char *fmt
, ...)
237 static int avt_color
= 0;
238 static int color_env
= 1;
239 char *str
= NULL
, *e
;
243 if (V_BOOL_TAB
[__AVT_COLOR
].VALUE
)
251 if (avt_terminal(output
))
256 vsprintf(temp
, fmt
, pa
);
263 if (*(e
+1)>='0' && *(e
+1)<='0'+NBCOLORS
-1)
267 if (color
) fputs(colors
[*(e
+1)-'0'], output
);
276 if (color
) fputs(COLOR(CL_BOLD
), output
);
282 if (color
) fputs(COLOR(CL_DIM
), output
);
288 if (color
) fputs(COLOR(CL_RESET
), output
);
294 if (color
) fputs(COLOR(CL_UDL
), output
);
300 if (color
) fputs(COLOR(CL_REV
), output
);
305 fprintf(output
,"%s¤",str
);
313 if (color
&& (ln
=strlen(str
))>0 && str
[ln
-1]=='\n')
314 fputs(COLOR(CL_RESET
), output
);
317 int avt_text_real_length(char *buf
)
322 if (*buf
=='¤' && *(buf
+1)!='\0') cnt
-=1;
329 void avt_format_text(char *resbuf
, char *origbuf
, int decal
, int max
)
331 int i
=0, j
=0, k
, cnt
=decal
;
333 while (origbuf
[i
]!='\0')
335 resbuf
[j
++]=origbuf
[i
];
336 if (origbuf
[i
]=='¤' && origbuf
[i
+1]!='\0') cnt
-=1;
339 if (origbuf
[i
]=='\n' || cnt
>=max
)
342 if (origbuf
[i
]=='\n') i
++;
343 if (origbuf
[i
]!='\0') for (k
=0; k
<decal
; k
++) resbuf
[j
++]=' ';