Annotation of libwww/Library/src/HTUtils.html, revision 2.47

2.6       timbl       1: <HTML>
                      2: <HEAD>
2.35      frystyk     3: <TITLE>Utility macros</TITLE>
2.47    ! frystyk     4: <!-- Changed by: Henrik Frystyk Nielsen, 19-Nov-1995 -->
2.6       timbl       5: </HEAD>
2.3       timbl       6: <BODY>
2.24      frystyk     7: 
2.20      frystyk     8: <H1>Macros for general use</H1>
                      9: 
2.24      frystyk    10: <PRE>
                     11: /*
2.31      frystyk    12: **     (c) COPYRIGHT MIT 1995.
2.24      frystyk    13: **     Please first read the full copyright statement in the file COPYRIGH.
                     14: */
                     15: </PRE>
                     16: 
                     17: This module is a part of the <A
2.44      frystyk    18: HREF="http://www.w3.org/pub/WWW/Library/"> W3C Reference Library</A>.
2.20      frystyk    19: 
2.26      frystyk    20: See also the system dependent file <A HREF="tcp.html">tcp module</A>
                     21: for system specific information. <P>
                     22: 
2.1       timbl      23: <PRE>
                     24: #ifndef HTUTILS_H
                     25: #define HTUTILS_H
                     26: 
2.26      frystyk    27: #if defined(__STDC__) || defined(__cplusplus) || defined(_WINDOWS)
                     28: #define _STANDARD_CODE_
2.1       timbl      29: #endif
2.20      frystyk    30: </PRE>
                     31: 
2.45      frystyk    32: <A NAME="debug"><H2>Debug message control</H2></A>
2.20      frystyk    33: 
2.39      frystyk    34: This is the global flag for setting the WWWTRACE options.
2.20      frystyk    35: 
                     36: <PRE>
2.26      frystyk    37: #ifndef DEBUG
                     38: #define DEBUG  /* No one ever turns this off as trace is too important */
                     39: #endif         /* Keep option for really small memory applications too */
                     40: 
2.44      frystyk    41: #ifdef WWW_WIN_DLL
                     42: extern int * WWW_TraceFlag;                 /* Global flag for all W3 trace */
                     43: #else
2.20      frystyk    44: extern int WWW_TraceFlag;                   /* Global flag for all W3 trace */
2.44      frystyk    45: #endif
                     46: 
2.20      frystyk    47: </PRE>
                     48: 
2.39      frystyk    49: The verbose mode is no longer a simple boolean but a bit field so that
                     50: it is possible to see parts of the output messages. The
                     51: <EM>WWWTRACE</EM> define still outputs all messages if verbose mode is
                     52: active, but in addition the following WWWTRACE defines have been made:
2.1       timbl      53: 
2.20      frystyk    54: <PRE>
2.40      frystyk    55: #define WWWTRACE_FILE  1
                     56: #define WWWTRACE_STDERR        2
                     57: #define WWWTRACE_TTY   3
                     58: 
                     59: #ifndef WWWTRACE_MODE
2.42      frystyk    60: #if (defined _WINDOWS) &amp;&amp; !defined(_CONSOLE)
                     61: #define WWWTRACE_MODE WWWTRACE_TTY
                     62: #else
2.40      frystyk    63: #define WWWTRACE_MODE WWWTRACE_STDERR
                     64: #endif
2.42      frystyk    65: #endif
2.40      frystyk    66: 
                     67: #if WWWTRACE_MODE == WWWTRACE_FILE
2.26      frystyk    68: extern FILE *WWWTrace;
2.46      frystyk    69: #ifndef HT_TRACE_FILE
                     70: #define HT_TRACE_FILE  "WWWTRACE.TXT"
2.26      frystyk    71: #endif
2.40      frystyk    72: #define TTYPrint fprintf
                     73: #define TDEST   WWWTrace
                     74: #endif
                     75: 
                     76: #if WWWTRACE_MODE == WWWTRACE_STDERR
                     77: #define TTYPrint fprintf
                     78: #define TDEST   stderr
2.26      frystyk    79: #endif
2.40      frystyk    80: 
                     81: #if WWWTRACE_MODE == WWWTRACE_TTY
                     82: #if defined(_WINDOWS) && !defined(_CONSOLE) /* EGP */
                     83: /* standard windows 3.x and non-console WIN32 programs */
2.43      frystyk    84: #define TDEST    0
2.44      frystyk    85: #ifdef WWW_WIN_DLL
2.43      frystyk    86: typedef int TTYPrint_t(unsigned int target, const char* fmt, ...);
                     87: extern TTYPrint_t** PTTYPrint;
                     88: #define TTYPrint (**PTTYPrint)
                     89: #else
2.40      frystyk    90: int TTYPrint(unsigned int target, const char* fmt, ...);
2.43      frystyk    91: #endif
2.40      frystyk    92: #else
                     93: /* if there is a real console, us it */
2.43      frystyk    94: #define TDEST    stderr
2.40      frystyk    95: int TTYPrint(FILE* target, const char* fmt, ...);
                     96: #endif
                     97: #endif
2.26      frystyk    98: 
2.20      frystyk    99: typedef enum _HTTraceFlags {
2.44      frystyk   100:     SHOW_UTIL_TRACE    = 0x1,                          /*                 1 */
                    101:     SHOW_APP_TRACE     = 0x2,                          /*                10 */
                    102:     SHOW_CACHE_TRACE   = 0x4,                          /*               100 */
2.32      frystyk   103:     SHOW_SGML_TRACE    = 0x8,                          /*              1000 */
2.29      frystyk   104:     SHOW_BIND_TRACE    = 0x10,                         /*            1.0000 */
2.32      frystyk   105:     SHOW_THREAD_TRACE  = 0x20,                         /*           10.0000 */
2.27      frystyk   106:     SHOW_STREAM_TRACE  = 0x40,                         /*          100.0000 */
2.32      frystyk   107:     SHOW_PROTOCOL_TRACE = 0x80,                                /*         1000.0000 */
                    108:     SHOW_URI_TRACE     = 0x200,                        /*      10.0000.0000 */
                    109:     SHOW_ANCHOR_TRACE  = 0x800,                        /*   10.00.0000.0000 */
2.20      frystyk   110:     SHOW_ALL_TRACE     = 0xFFF                         /*   11.11.1111.1111 */
                    111: } HTTraceFlags;
                    112: </PRE>
                    113: 
                    114: The flags are made so that they can serve as a group flag for correlated
                    115: trace messages, e.g. showing messages for SGML and HTML at the same time. 
                    116: 
                    117: <PRE>
2.1       timbl     118: #ifdef DEBUG
2.44      frystyk   119: #ifdef WWW_WIN_DLL
                    120: #define WWWTRACE       (*WWW_TraceFlag)
                    121: #define UTIL_TRACE     (*WWW_TraceFlag & SHOW_UTIL_TRACE)
                    122: #define APP_TRACE      (*WWW_TraceFlag & SHOW_APP_TRACE)
                    123: #define CACHE_TRACE    (*WWW_TraceFlag & SHOW_CACHE_TRACE)
                    124: #define SGML_TRACE     (*WWW_TraceFlag & SHOW_SGML_TRACE)
                    125: #define BIND_TRACE     (*WWW_TraceFlag & SHOW_BIND_TRACE)
                    126: #define THD_TRACE      (*WWW_TraceFlag & SHOW_THREAD_TRACE)
                    127: #define STREAM_TRACE   (*WWW_TraceFlag & SHOW_STREAM_TRACE)
                    128: #define PROT_TRACE     (*WWW_TraceFlag & SHOW_PROTOCOL_TRACE)
                    129: #define URI_TRACE      (*WWW_TraceFlag & SHOW_URI_TRACE)
                    130: #define ANCH_TRACE     (*WWW_TraceFlag & SHOW_ANCHOR_TRACE)
                    131: #else
2.39      frystyk   132: #define WWWTRACE       (WWW_TraceFlag)
2.44      frystyk   133: #define UTIL_TRACE     (WWW_TraceFlag & SHOW_UTIL_TRACE)
                    134: #define APP_TRACE      (WWW_TraceFlag & SHOW_APP_TRACE)
2.27      frystyk   135: #define CACHE_TRACE    (WWW_TraceFlag & SHOW_CACHE_TRACE)
2.20      frystyk   136: #define SGML_TRACE     (WWW_TraceFlag & SHOW_SGML_TRACE)
2.29      frystyk   137: #define BIND_TRACE     (WWW_TraceFlag & SHOW_BIND_TRACE)
2.23      frystyk   138: #define THD_TRACE      (WWW_TraceFlag & SHOW_THREAD_TRACE)
2.27      frystyk   139: #define STREAM_TRACE   (WWW_TraceFlag & SHOW_STREAM_TRACE)
2.20      frystyk   140: #define PROT_TRACE     (WWW_TraceFlag & SHOW_PROTOCOL_TRACE)
                    141: #define URI_TRACE      (WWW_TraceFlag & SHOW_URI_TRACE)
                    142: #define ANCH_TRACE     (WWW_TraceFlag & SHOW_ANCHOR_TRACE)
2.44      frystyk   143: #endif
2.1       timbl     144: #else
2.39      frystyk   145: #define WWWTRACE       0
2.44      frystyk   146: #define UTIL_TRACE     0
                    147: #define APP_TRACE      0
2.29      frystyk   148: #define CACHE_TRACE    0
2.20      frystyk   149: #define SGML_TRACE     0
2.29      frystyk   150: #define BIND_TRACE     0
2.27      frystyk   151: #define THD_TRACE      0
                    152: #define STREAM_TRACE   0
2.20      frystyk   153: #define PROT_TRACE     0
                    154: #define URI_TRACE      0
                    155: #define ANCH_TRACE     0
2.1       timbl     156: #endif
2.3       timbl     157: </PRE>
2.26      frystyk   158: 
                    159: <H2>Standard C library for malloc() etc</H2>
                    160: 
                    161: Replace memory allocation and free C RTL functions with VAXC$xxx_OPT
                    162: altrenatives for VAXC (but not DECC) on VMS. This makes a big
                    163: performance difference. (Foteos Macrides).
                    164: 
                    165: <PRE>
                    166: #ifdef vax
                    167: #ifdef unix
                    168: #define ultrix /* Assume vax+unix=ultrix */
                    169: #endif
                    170: #endif
                    171: 
                    172: #ifndef VMS
                    173: #ifndef ultrix
                    174: #ifdef NeXT
                    175: #include &lt;libc.h&gt;        /* NeXT */
                    176: #endif
                    177: 
                    178: #ifndef Mips
                    179: #ifndef MACH /* Vincent.Cate@furmint.nectar.cs.cmu.edu */
                    180: #include &lt;stdlib.h&gt;      /* ANSI */
                    181: #endif
                    182: #endif
                    183: 
                    184: #else /* ultrix */
                    185: #include &lt;malloc.h&gt;
                    186: #include &lt;memory.h&gt;
                    187: #include &lt;stdio.h&gt;
                    188: #include &lt;stdlib.h&gt;   /* ANSI */   /* BSN */
                    189: #endif
                    190: 
                    191: #else  /* VMS */
                    192: #include &lt;stdio.h&gt;
                    193: #include &lt;stdlib.h&gt;
                    194: #include &lt;unixlib.h&gt;
                    195: #include &lt;ctype.h&gt;
                    196: #if defined(VAXC) && !defined(__DECC)
                    197: #define malloc VAXC$MALLOC_OPT
                    198: #define calloc VAXC$CALLOC_OPT
                    199: #define free   VAXC$FREE_OPT
                    200: #define cfree  VAXC$CFREE_OPT
                    201: #define realloc        VAXC$REALLOC_OPT
                    202: #endif /* VAXC but not DECC */
                    203: #define unlink remove
                    204: #define gmtime localtime
                    205: #include &lt;stat.h&gt;
                    206: #define S_ISDIR(m)      (((m)&S_IFMT) == S_IFDIR)
                    207: #define S_ISREG(m)      (((m)&S_IFMT) == S_IFREG)
                    208: #define putenv HTVMS_putenv
                    209: #endif
                    210: </PRE>
                    211: 
2.29      frystyk   212: <H3>Safe Cleaning Memory</H3>
                    213: 
                    214: <PRE>
                    215: #define FREE(x)        if (x) {free(x); x=NULL;}
                    216: </PRE>
                    217: 
                    218: <A NAME="declaration"><H2>Macros for declarations</H2></A>
2.26      frystyk   219: 
                    220: <PRE>
                    221: #define PUBLIC                 /* Accessible outside this module     */
2.1       timbl     222: #define PRIVATE static         /* Accessible only within this module */
                    223: 
2.26      frystyk   224: #ifdef _STANDARD_CODE_
                    225: 
2.34      frystyk   226: /* The sco ODT 3.0 CC compiler has some trouble with  const */
                    227: #if !defined(sco) || defined(sco_os5) 
2.26      frystyk   228: #define CONST const            /* "const" only exists in STDC */
                    229: #else
                    230: #define CONST
                    231: #endif
                    232: 
2.47    ! frystyk   233: #if 0
2.1       timbl     234: #define NOPARAMS (void)
                    235: #define PARAMS(parameter_list) parameter_list
                    236: #define NOARGS (void)
                    237: #define ARGS1(t,a) \
                    238:                (t a)
                    239: #define ARGS2(t,a,u,b) \
                    240:                (t a, u b)
                    241: #define ARGS3(t,a,u,b,v,c) \
                    242:                (t a, u b, v c)
                    243: #define ARGS4(t,a,u,b,v,c,w,d) \
                    244:                (t a, u b, v c, w d)
                    245: #define ARGS5(t,a,u,b,v,c,w,d,x,e) \
                    246:                (t a, u b, v c, w d, x e)
                    247: #define ARGS6(t,a,u,b,v,c,w,d,x,e,y,f) \
                    248:                (t a, u b, v c, w d, x e, y f)
                    249: #define ARGS7(t,a,u,b,v,c,w,d,x,e,y,f,z,g) \
                    250:                (t a, u b, v c, w d, x e, y f, z g)
                    251: #define ARGS8(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h) \
                    252:                (t a, u b, v c, w d, x e, y f, z g, s h)
                    253: #define ARGS9(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h,r,i) \
                    254:                (t a, u b, v c, w d, x e, y f, z g, s h, r i)
                    255: #define ARGS10(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h,r,i,q,j) \
                    256:                (t a, u b, v c, w d, x e, y f, z g, s h, r i, q j)
2.47    ! frystyk   257: #endif
2.1       timbl     258: 
                    259: #else  /* not ANSI */
                    260: 
2.26      frystyk   261: #define CONST
                    262: 
2.47    ! frystyk   263: #if 0
2.1       timbl     264: #define NOPARAMS ()
                    265: #define PARAMS(parameter_list) ()
                    266: #define NOARGS ()
                    267: #define ARGS1(t,a) (a) \
                    268:                t a;
                    269: #define ARGS2(t,a,u,b) (a,b) \
                    270:                t a; u b;
                    271: #define ARGS3(t,a,u,b,v,c) (a,b,c) \
                    272:                t a; u b; v c;
                    273: #define ARGS4(t,a,u,b,v,c,w,d) (a,b,c,d) \
                    274:                t a; u b; v c; w d;
                    275: #define ARGS5(t,a,u,b,v,c,w,d,x,e) (a,b,c,d,e) \
                    276:                t a; u b; v c; w d; x e;
                    277: #define ARGS6(t,a,u,b,v,c,w,d,x,e,y,f) (a,b,c,d,e,f) \
                    278:                t a; u b; v c; w d; x e; y f;
                    279: #define ARGS7(t,a,u,b,v,c,w,d,x,e,y,f,z,g) (a,b,c,d,e,f,g) \
                    280:                t a; u b; v c; w d; x e; y f; z g;
                    281: #define ARGS8(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h) (a,b,c,d,e,f,g,h) \
                    282:                t a; u b; v c; w d; x e; y f; z g; s h;
                    283: #define ARGS9(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h,r,i) (a,b,c,d,e,f,g,h,i) \
                    284:                t a; u b; v c; w d; x e; y f; z g; s h; r i;
                    285: #define ARGS10(t,a,u,b,v,c,w,d,x,e,y,f,z,g,s,h,r,i,q,j) (a,b,c,d,e,f,g,h,i,j) \
2.4       timbl     286:                t a; u b; v c; w d; x e; y f; z g; s h; r i; q j;
2.47    ! frystyk   287: #endif         
2.1       timbl     288:        
2.21      frystyk   289: #endif /* _STANDARD_CODE_ (ANSI) */
2.1       timbl     290: 
                    291: #ifndef NULL
                    292: #define NULL ((void *)0)
                    293: #endif
2.26      frystyk   294: </PRE>
2.1       timbl     295: 
2.3       timbl     296: <H2>Booleans</H2>
2.26      frystyk   297: 
                    298: <PRE>
2.30      frystyk   299: #ifndef BOOLEAN_DEFINED
2.26      frystyk   300: typedef char   BOOLEAN;                                    /* Logical value */
2.30      frystyk   301: #endif
2.1       timbl     302: 
                    303: #ifndef CURSES
                    304: #ifndef TRUE
                    305: #define TRUE   (BOOLEAN)1
                    306: #define        FALSE   (BOOLEAN)0
                    307: #endif
2.3       timbl     308: #endif  /*  CURSES  */
2.1       timbl     309: 
                    310: #ifndef BOOL
                    311: #define BOOL BOOLEAN
                    312: #endif
                    313: #ifndef YES
2.14      timbl     314: #define YES             (BOOL)1
                    315: #define NO              (BOOL)0
2.1       timbl     316: #endif
                    317: 
2.11      timbl     318: #ifndef HTMIN 
                    319: #define HTMIN(a,b) ((a) &lt;= (b) ? (a) : (b))
                    320: #define HTMAX(a,b) ((a) >= (b) ? (a) : (b))
2.1       timbl     321: #endif
2.26      frystyk   322: </PRE>
2.1       timbl     323: 
2.35      frystyk   324: <A NAME="ReturnCodes"><H2>Return Codes for Protocol Modules and Streams</H2></A>
                    325: 
                    326: Theese are the codes returned from the protocol modules, and the
                    327: stream modules. Success are (&gt;=0) and failure are (&lt;0)
2.1       timbl     328: 
2.3       timbl     329: <PRE>
2.23      frystyk   330: #define HT_OK                  0       /* Generic success */
2.37      frystyk   331: #define HT_ALL                 1       /* Used by Net Manager */
                    332: 
2.38      frystyk   333: #define HT_PERSISTENT          29995   /* Wait for persistent connection */
2.37      frystyk   334: #define HT_IGNORE              29996   /* Ignore this in the Net manager */
2.35      frystyk   335: #define HT_NO_DATA             29997   /* OK but no data was loaded */
                    336: #define HT_REDIRECTION_ON_FLY   29998  /* Redo the retrieve with a new URL */
2.44      frystyk   337: #define HT_REDIRECT            29998   /* Redo the retrieve with a new URL */
2.23      frystyk   338: #define HT_LOADED              29999   /* Instead of a socket */
                    339: 
                    340: #define HT_ERROR               -1      /* Generic failure */
                    341: #define HT_NO_ACCESS           -10     /* Access not available */
                    342: #define HT_FORBIDDEN           -11     /* Access forbidden */
                    343: #define HT_INTERNAL            -12     /* Weird -- should never happen. */
2.35      frystyk   344: 
                    345: #define HT_RETRY               -29996  /* If service isn't available */
2.23      frystyk   346: #define HT_WOULD_BLOCK         -29997  /* If we are in a select */
2.15      frystyk   347: #define HT_INTERRUPTED                 -29998  /* Note the negative value! */
2.35      frystyk   348: </PRE>
                    349: 
                    350: The following codes are positive values that will be parsed back
                    351: through streams. This can be used to introduce stream specific return
                    352: values
2.1       timbl     353: 
2.35      frystyk   354: <PRE>
                    355: #define HT_RELOAD              29996   /* If we must reload the document */
                    356: </PRE>
                    357: 
                    358: <H2>Other stuff</H2>
                    359: 
                    360: <PRE>
2.26      frystyk   361: #ifdef _STANDARD_CODE_
                    362: #include &lt;stdarg.h&gt;
                    363: #else
                    364: #include &lt;varargs.h&gt;
                    365: #endif
2.1       timbl     366: 
                    367: #ifdef CURSES
2.26      frystyk   368:        /* htbrowse.c; */
                    369: #ifdef ULTRIX      /* or DECSTATION */
                    370: #include &lt;cursesX.h&gt; /* Extended curses under X. Only decent one :lou. */
                    371: #else
                    372: #include &lt;curses.h&gt;
                    373: #endif /* ULTRIX */
2.1       timbl     374:        extern        WINDOW  *w_top, *w_text, *w_prompt;
                    375:        extern        void    user_message PARAMS((const char *fmt, ...));
                    376:        extern        void    prompt_set PARAMS((CONST char * msg));
                    377:        extern        void    prompt_count PARAMS((long kb));
                    378: #else
                    379: #define user_message printf
                    380: #endif
                    381: 
2.3       timbl     382: </PRE>
2.26      frystyk   383: 
                    384: <H2>Out Of Memory checking for malloc() return:</H2>
                    385: 
                    386: <PRE>
                    387: #ifndef __FILE__
2.1       timbl     388: #define __FILE__ ""
                    389: #define __LINE__ ""
                    390: #endif
                    391: 
                    392: #define outofmem(file, func) \
2.26      frystyk   393:  { fprintf(TDEST, "%s %s: out of memory.\nProgram aborted.\n", file, func); \
2.1       timbl     394:   exit(1);}
                    395: 
2.26      frystyk   396: </PRE>
2.1       timbl     397: 
2.26      frystyk   398: <H2>Upper- and Lowercase macros</H2>
                    399: 
                    400: The problem here is that toupper(x) is not defined officially unless
                    401: isupper(x) is.  These macros are CERTAINLY needed on #if defined(pyr)
                    402: || define(mips) or BDSI platforms. For safefy, we make them mandatory.
2.1       timbl     403: 
2.25      roeber    404: <PRE>
2.26      frystyk   405: #include &lt;ctype.h&gt;
2.1       timbl     406: 
                    407: #ifndef TOLOWER
2.27      frystyk   408: #define TOLOWER(c) tolower(c)
                    409: #define TOUPPER(c) toupper(c)
2.29      frystyk   410: #endif
                    411: </PRE>
                    412: 
                    413: <H2>Max and Min values for Integers and Floating Point</H2>
                    414: 
                    415: <PRE>
                    416: #ifdef FLT_EPSILON                                 /* The ANSI C way define */
                    417: #define HT_EPSILON FLT_EPSILON
                    418: #else
                    419: #define HT_EPSILON 0.00000001
2.27      frystyk   420: #endif
                    421: </PRE>
                    422: 
                    423: <H2>White Characters</H2>
                    424: 
                    425: Is character <B>c</B> white space?
                    426: 
                    427: <PRE>
                    428: #ifdef OLD_CODE
                    429: #define WHITE(c) (((unsigned char)(TOASCII(c))) &lt;= 32)
                    430: #else
                    431: #define WHITE(c) isspace(c)
                    432: #endif
2.26      frystyk   433: </PRE>
                    434: 
                    435: <H2>The local equivalents of CR and LF</H2>
2.1       timbl     436: 
2.26      frystyk   437: We can check for these after net ascii text has been converted to the
                    438: local representation. Similarly, we include them in strings to be sent
                    439: as net ascii after translation.
                    440: 
                    441: <PRE>
                    442: #define LF   FROMASCII('\012')  /* ASCII line feed LOCAL EQUIVALENT */
                    443: #define CR   FROMASCII('\015')  /* Will be converted to ^M for transmission */
2.6       timbl     444: 
2.1       timbl     445: #endif /* HTUTILS_H */
2.26      frystyk   446: </PRE>
2.1       timbl     447: 
2.26      frystyk   448: end of utilities
                    449: </BODY>
2.6       timbl     450: </HTML>

Webmaster