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

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

Webmaster