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

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

Webmaster