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

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

Webmaster