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

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

Webmaster