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

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

Webmaster