| 1 |
/* @(#)tvutil.c 19.1 (ES0-DMD) 02/25/03 13:58:15 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.IDENTIFICATION tvutil.c |
| 32 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 33 |
.LANGUAGE C |
| 34 |
.KEYWORDS Termcap |
| 35 |
.ENVIRONMENT TermWindows |
| 36 |
.COMMENTS This module provides various utilities related to |
| 37 |
TERMCAPabilities or Terminal manipulations. |
| 38 |
|
| 39 |
.VERSION 2.0 12-Jun-1987: Creation. Extracted from full capability version. |
| 40 |
|
| 41 |
.VERSION 2.2 23-Jun-1987: Adapted to UNIX |
| 42 |
.VERSION 2.3 02-Dec-1987 Use of external function while waiting for input |
| 43 |
.VERSION 2.4 21-Feb-1990 Removed warning |
| 44 |
.VERSION 2.5 10-Jun-1991: Added %p formatting (for HP termcapfiles) |
| 45 |
|
| 46 |
----------------------------------------------------------------------------*/ |
| 47 |
|
| 48 |
#define DEBUG 0 /* For debugging only */ |
| 49 |
|
| 50 |
#define PM_LEVEL LEVEL_TU |
| 51 |
|
| 52 |
#define TW_MACROS 0 /* Don't use TermWindows Macros */ |
| 53 |
#define TW_import 0 |
| 54 |
#define TW_STRUCT 0 /* Do not use window Structures */ |
| 55 |
#include <twset.h> |
| 56 |
|
| 57 |
MID_EXTERN TERM *terms; |
| 58 |
|
| 59 |
/* Macros just to improve readibility */ |
| 60 |
|
| 61 |
#define NCAPS (terms->ns) |
| 62 |
#define CAPLIST (terms->caplist) |
| 63 |
#define CAPSIZE (terms->size) |
| 64 |
#define CAPTOP (terms->captop) |
| 65 |
|
| 66 |
|
| 67 |
MONITOR(TVUTIL); |
| 68 |
|
| 69 |
|
| 70 |
/*========================================================================== |
| 71 |
* tu_format |
| 72 |
*==========================================================================*/ |
| 73 |
int tu_format (out, fm, coo) |
| 74 |
/*++++++++++++++ |
| 75 |
.PURPOSE Edits coordinates according to the coded TERMCAP format. |
| 76 |
.RETURNS Length of edited string |
| 77 |
.REMARKS No test on the length of edited string is performed. |
| 78 |
If the format designates more than 2 arguments, the 3rd is |
| 79 |
the repetition of the first, etc.. |
| 80 |
---------------*/ |
| 81 |
unsigned char *out; /* OUT: The output buffer, receiving edited coordinates */ |
| 82 |
unsigned char *fm; /* IN: The coded TERMCAP format */ |
| 83 |
short int coo[2]; /* IN: Coordinates to edit */ |
| 84 |
{ |
| 85 |
MID_STATIC short int arg[2] = {0,0}; /* Local copy of coordinates */ |
| 86 |
MID_STATIC char digitS[12] = " " ; /* Edition buffer */ |
| 87 |
MID_STATIC unsigned char *digit = (unsigned char *)digitS; |
| 88 |
register unsigned char *p, *pf; |
| 89 |
unsigned char *p_end; |
| 90 |
int j, narg, x, led; |
| 91 |
|
| 92 |
ENTER("+tu_format"); |
| 93 |
|
| 94 |
/* Local copy of arguments */ |
| 95 |
arg[0] = coo[0], arg[1] = coo[1]; |
| 96 |
narg=0; |
| 97 |
|
| 98 |
/* Scan the format string, performing the substitutions when |
| 99 |
the FORMAT char is found */ |
| 100 |
p = out; |
| 101 |
pf = fm + 4; |
| 102 |
p_end = pf + *fm; |
| 103 |
|
| 104 |
while (pf < p_end) |
| 105 |
{ if (*pf != FORMAT) { *(p++) = *(pf++); |
| 106 |
continue; |
| 107 |
} |
| 108 |
/* --- Here, only if FORMAT char was found --- */ |
| 109 |
led = 9; /* Length of argument edition */ |
| 110 |
switch(*(++pf)) |
| 111 |
{ case 'i': /* Increment all arguments */ |
| 112 |
arg[0]++, arg[1]++; |
| 113 |
pf++; break; |
| 114 |
case 'p': /* Argument number as %p[1-9] */ |
| 115 |
arg[narg] = coo[*(++pf) - '1']; |
| 116 |
pf++; break; |
| 117 |
case 'r': /* Swap arguments */ |
| 118 |
x = arg[0]; arg[0] = arg[1]; arg[1] = x; |
| 119 |
pf++; break; |
| 120 |
case 'n': /* Special arguments change */ |
| 121 |
arg[0] ^= 0140; arg[1] ^= 0140; |
| 122 |
pf++; break; |
| 123 |
case '>': /* >xy : if arg>x, add y */ |
| 124 |
if(arg[narg] > (short int)*(pf+1)) |
| 125 |
arg[narg] += *(pf+2); |
| 126 |
pf += 3; break; |
| 127 |
|
| 128 |
/* The following effectively edits arguments */ |
| 129 |
|
| 130 |
case '.': /* Straight transmit */ |
| 131 |
*(p++) = arg[narg]; |
| 132 |
narg ^= 1; pf++; break; |
| 133 |
case '+': /* Transmit argument + x */ |
| 134 |
*(p++) = arg[narg] + *(++pf); |
| 135 |
narg ^= 1; pf++; break; |
| 136 |
case '0': case '1': /* Edit with led chars */ |
| 137 |
case '2': case '3': case '4': |
| 138 |
led = *pf - '0'; |
| 139 |
case 'd': /* Identical with '0' */ |
| 140 |
if (led == 9) led = 0; |
| 141 |
x = arg[narg]; narg ^= 1; j=sizeof(digit); |
| 142 |
while((led>0) || x) /* Edit the argument */ |
| 143 |
{ digit[--j] = '0' + x%10; |
| 144 |
x /= 10; led--; |
| 145 |
} |
| 146 |
while (j<sizeof(digit)) *(p++) = digit[j++]; |
| 147 |
pf++; break; |
| 148 |
default: ERR_ED_STR2("Bad TERMCAP format char =>",(char *)pf,1); |
| 149 |
EXIT(0); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#if DEBUG |
| 154 |
TRACE_ED_STR2("Output after formatting:",out,p-out); |
| 155 |
#endif |
| 156 |
|
| 157 |
EXIT(p-out); /* Length of edited string */ |
| 158 |
} |
| 159 |
|
| 160 |
/*========================================================================== |
| 161 |
* tu_scap |
| 162 |
*==========================================================================*/ |
| 163 |
char *tu_scap (cap2) |
| 164 |
/*++++++++++++++ |
| 165 |
.PURPOSE Retrieve a named capability. |
| 166 |
.RETURNS Address of the capability field (starting with a :); NULL if not found. |
| 167 |
.REMARKS Dichotomic search is performed via the index. |
| 168 |
----------------*/ |
| 169 |
char cap2[2]; /* IN: The cap_code to look for */ |
| 170 |
{ |
| 171 |
register INDEX *low, *high, *middle; |
| 172 |
INDEX *match; |
| 173 |
char *p; |
| 174 |
|
| 175 |
ENTER("*tu_scap"); |
| 176 |
|
| 177 |
TRACE_ED_STR2("... Searching capability =>",cap2,2); |
| 178 |
|
| 179 |
low = (INDEX * ) (CAPLIST + terms->index); |
| 180 |
high = low + (NCAPS-1); |
| 181 |
match= NULL_PTR(INDEX); /* Matching address */ |
| 182 |
|
| 183 |
while( high >= low) /* Check first byte */ |
| 184 |
{ middle = low + ((high-low)/2); |
| 185 |
if ( cap2[0] < middle->cap[0] ) |
| 186 |
{ high = middle-1; continue; } |
| 187 |
if ( cap2[0] > middle->cap[0] ) |
| 188 |
{ low = middle+1; continue; } |
| 189 |
/* Check Second byte */ |
| 190 |
if ( cap2[1] < middle->cap[1] ) |
| 191 |
{ high = middle-1; continue; } |
| 192 |
if ( cap2[1] > middle->cap[1] ) |
| 193 |
{ low = middle+1; continue; } |
| 194 |
match = middle; |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
/* Entries followed by `@' mean "Not such a capability" */ |
| 199 |
if (match) |
| 200 |
{ p = match->icap + CAPLIST; |
| 201 |
if (*(p+3) == '@') p = NULL_PTR(char); |
| 202 |
} |
| 203 |
else p = NULL_PTR(char); |
| 204 |
EXITp(p); |
| 205 |
} |
| 206 |
|
| 207 |
/*========================================================================== |
| 208 |
* tv_ac |
| 209 |
*==========================================================================*/ |
| 210 |
int tv_ac(source, len, dest) |
| 211 |
/*++++++++++++ |
| 212 |
.PURPOSE Converts Achars to ASCII chars. |
| 213 |
.RETURNS Number of Graphic Chars in the source string. |
| 214 |
.REMARKS The converted string is NOT terminated with an EOS. |
| 215 |
Graphic Characters are translated to '+', '-', '|', '~' |
| 216 |
-------------*/ |
| 217 |
ACHAR *source; /* IN: Source string of Achar's */ |
| 218 |
int len; /* IN: Length of strings */ |
| 219 |
char *dest; /* OUT: Converted string */ |
| 220 |
{ |
| 221 |
register int l; |
| 222 |
register ACHAR *pa; |
| 223 |
register char *pc; |
| 224 |
|
| 225 |
ENTER_("+tv_at"); |
| 226 |
|
| 227 |
status = 0; /* Number of Graphic Characters */ |
| 228 |
|
| 229 |
for (pa = source, l = len, pc = dest; --l >=0; pa++, pc++) |
| 230 |
{ *pc = *pa & 0177; |
| 231 |
if (isGraphic(*pa)) |
| 232 |
{ status++; |
| 233 |
switch(*pc) |
| 234 |
{ case RuleChar(_HORIZONTAL_): *pc = '-'; break; |
| 235 |
case RuleChar(_VERTICAL_): *pc = '|'; break; |
| 236 |
case RubbishChar(): *pc = '~'; break; |
| 237 |
default: *pc = '+'; break; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
TRACE_ED_STR2("Converted =>", dest, len); |
| 243 |
EXIT(status); |
| 244 |
} |
| 245 |
|
| 246 |
/*========================================================================== |
| 247 |
* tv_aloc |
| 248 |
*==========================================================================*/ |
| 249 |
int tv_aloc(source, len, achar) |
| 250 |
/*++++++++++++ |
| 251 |
.PURPOSE Locate in source the position of first `ACHAR' having the same |
| 252 |
attribute as `achar' |
| 253 |
.RETURNS Index in source of found character, len if none found. |
| 254 |
.REMARKS |
| 255 |
-------------*/ |
| 256 |
ACHAR *source; /* IN: Source string of Achar's */ |
| 257 |
int len; /* IN: Length of strings */ |
| 258 |
ACHAR achar; /* IN: achar which attribute is to find */ |
| 259 |
{ |
| 260 |
register int l; |
| 261 |
register ACHAR *pa, ac; |
| 262 |
|
| 263 |
ENTER_("+tv_aloc"); |
| 264 |
|
| 265 |
for (pa = source, l = len, ac = achar & 0xff00; |
| 266 |
(--l >=0) && ((*pa & 0xff00) != ac); pa++) ; |
| 267 |
|
| 268 |
EXIT(pa-source); |
| 269 |
} |
| 270 |
|
| 271 |
/*========================================================================== |
| 272 |
* tv_askip |
| 273 |
*==========================================================================*/ |
| 274 |
int tv_askip(source, len, achar) |
| 275 |
/*++++++++++++ |
| 276 |
.PURPOSE Skip in source all `ACHAR's having the same |
| 277 |
attribute as `achar' |
| 278 |
.RETURNS Index in source of differing character, len if none found. |
| 279 |
.REMARKS |
| 280 |
-------------*/ |
| 281 |
ACHAR *source; /* IN: Source string of Achar's */ |
| 282 |
int len; /* IN: Length of strings */ |
| 283 |
ACHAR achar; /* IN: achar which attribute is to find */ |
| 284 |
{ |
| 285 |
register int l; |
| 286 |
register ACHAR *pa, ac; |
| 287 |
|
| 288 |
ENTER_("+tv_askip"); |
| 289 |
|
| 290 |
for (pa = source, l = len, ac = achar & 0xff00; |
| 291 |
(--l >=0) && ((*pa & 0xff00) == ac); pa++) ; |
| 292 |
|
| 293 |
EXIT(pa-source); |
| 294 |
} |
| 295 |
|
| 296 |
/*========================================================================== |
| 297 |
* tv_rb |
| 298 |
*==========================================================================*/ |
| 299 |
int tv_rb(coo, dimb, limits) |
| 300 |
/*+++++++++++++ |
| 301 |
.PURPOSE Verify and updates the coordinates / dimensions of a box |
| 302 |
defined on the screen. |
| 303 |
.RETURNS OK / NOK (dimension too large) |
| 304 |
.REMARKS Not traced. |
| 305 |
---------------*/ |
| 306 |
short int coo[2]; /* MOD: Proposed position of upper left corner \ |
| 307 |
(<0 for position from right) */ |
| 308 |
short int dimb[2]; /* MOD: Proposed dimensions (0 for complete, \ |
| 309 |
<0 for Centered) */ |
| 310 |
short int limits[2]; /* IN: Total size of screen */ |
| 311 |
{ |
| 312 |
register int i; |
| 313 |
|
| 314 |
status = OK; |
| 315 |
|
| 316 |
for (i = 2; --i >= 0; ) |
| 317 |
{ if (dimb[i] < 0) /* Centered */ |
| 318 |
{ dimb[i] = - dimb[i]; |
| 319 |
coo[i] = (limits[i] - dimb[i] + 1)/2; |
| 320 |
if (coo[i]< 0) coo[i] = 0, status = NOK; |
| 321 |
} |
| 322 |
|
| 323 |
if (coo[i] < 0) /* Position from bottom */ |
| 324 |
{ coo[i] += limits[i]; |
| 325 |
if (coo[i] < 0) coo[i] = 0, status = NOK; |
| 326 |
} |
| 327 |
/* Compute size if not defined */ |
| 328 |
if (dimb[i] == 0) dimb[i] = limits[i] - coo[i]; |
| 329 |
if ((dimb[i]+coo[i]) > limits[i]) |
| 330 |
dimb[i] = limits[i] - coo[i], status = NOK; |
| 331 |
if (dimb[i] < 0) status = NOK; |
| 332 |
} |
| 333 |
|
| 334 |
if ( status == NOK) ERROR("Can't fit Box!"); |
| 335 |
|
| 336 |
return(status); |
| 337 |
} |