Annotation of libwww/Library/src/HTAlert.c, revision 2.20.2.1

2.18      frystyk     1: /*                                                                   HTAlert.c
                      2: **     DISPLAYING MESSAGES AND GETTING INPUT FOR LINEMODE BROWSER
                      3: **
                      4: **     (c) COPYRIGHT CERN 1994.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: **     REPLACE THIS MODULE with a GUI version in a GUI environment!
                      8: **
                      9: ** History:
                     10: **        Jun 92 Created May 1992 By C.T. Barker
                     11: **        Feb 93 Simplified, portablised TBL
2.8       luotonen   12: **        Sep 93 Corrected 3 bugs in HTConfirm() :-( AL
1.1       timbl      13: */
                     14: 
2.20      frystyk    15: /* Library include files */
                     16: #include "tcp.h"
                     17: #include "HTUtils.h"
                     18: #include "HTString.h"
2.20.2.1! frystyk    19: #include "HTError.h"                                    /* Implemented here */
        !            20: #include "HTAlert.h"                                    /* Implemented here */
1.1       timbl      21: 
2.16      frystyk    22: PUBLIC BOOL HTInteractive=YES;             /* Any prompts from the Library? */
1.1       timbl      23: 
2.20.2.1! frystyk    24: /* ------------------------------------------------------------------------- */
        !            25: 
        !            26: PUBLIC void HTProgress ARGS1(CONST char *, Msg)
        !            27: {
        !            28:     fprintf(TDEST, "   %s ...\n", Msg ? Msg : "UNKNWON");
        !            29: }
        !            30: 
        !            31: 
1.1       timbl      32: PUBLIC void HTAlert ARGS1(CONST char *, Msg)
                     33: {
2.20      frystyk    34: #ifdef NeXTStep
2.6       timbl      35:     NXRunAlertPanel(NULL, "%s", NULL, NULL, NULL, Msg);
                     36: #else
2.20.2.1! frystyk    37:     fprintf(TDEST, "\nWARNING:  %s\n", Msg ? Msg : "UNKNOWN");
2.6       timbl      38: #endif
1.1       timbl      39: }
                     40: 
                     41: PUBLIC BOOL HTConfirm ARGS1(CONST char *, Msg)
                     42: {
2.8       luotonen   43:   char Reply[4];       /* One more for terminating NULL -- AL */
1.1       timbl      44:   char *URep;
                     45:   
2.20.2.1! frystyk    46:   fprintf(TDEST, "%s (y/n) ", Msg ? Msg : "UNKNOWN");
2.20      frystyk    47: #ifndef NO_STDIO
2.16      frystyk    48:   if (!HTInteractive || !fgets(Reply, 4, stdin))   /* get reply, max 3 chars */
2.20      frystyk    49: #endif
2.14      frystyk    50:       return NO;
1.1       timbl      51:   URep=Reply;
2.8       luotonen   52:   while (*URep) {
2.9       luotonen   53:     if (*URep == '\n') {
2.10      luotonen   54:        *URep = (char)0;        /* Overwrite newline */
2.9       luotonen   55:        break;
                     56:     }
2.8       luotonen   57:     *URep=TOUPPER(*URep);
                     58:     URep++;    /* This was previously embedded in the TOUPPER */
                     59:                 /* call an it became evaluated twice because   */
                     60:                 /* TOUPPER is a macro -- AL */
                     61:   }
                     62: 
1.1       timbl      63:   if ((strcmp(Reply,"YES")==0) || (strcmp(Reply,"Y")==0))
                     64:     return(YES);
                     65:   else
                     66:     return(NO);
                     67: }
                     68: 
2.20      frystyk    69: /*     Prompt for answer and get text back. Reply text is either NULL on
                     70: **     error or a dynamic string which the caller must free.
1.1       timbl      71: */
                     72: PUBLIC char * HTPrompt ARGS2(CONST char *, Msg, CONST char *, deflt)
                     73: {
2.20      frystyk    74:     char buffer[200];
                     75:     char *reply = NULL;
2.20.2.1! frystyk    76:     fprintf(TDEST, "%s", Msg ? Msg : "UNKNOWN");
2.20      frystyk    77:     if (deflt)
                     78:        fprintf(TDEST, " (RETURN for [%s]) ", deflt);
                     79: 
                     80:     if (HTInteractive) {
                     81: #ifndef NO_STDIO
                     82:        if (!fgets(buffer, 200, stdin))
                     83:            return NULL;                     /* NULL string on error, Henrik */
                     84:        buffer[strlen(buffer)-1] = '\0';                /* Overwrite newline */
                     85:        StrAllocCopy(reply, *buffer ? buffer : deflt);
                     86: #endif
                     87:     }
                     88:     return reply;
1.1       timbl      89: }
2.8       luotonen   90: 
                     91: 
2.20      frystyk    92: /*     Prompt for password without echoing the reply. Reply text is
                     93: **     either NULL on error or a dynamic string which the caller must free.
2.8       luotonen   94: */
                     95: PUBLIC char * HTPromptPassword ARGS1(CONST char *, Msg)
                     96: {
2.20      frystyk    97:     char *reply = NULL;
                     98:     if (HTInteractive) {
                     99: #ifndef NO_PASSWD
                    100:        char *pw = (char *) getpass(Msg ? Msg : "Password: ");
                    101:        if (pw)
                    102:            StrAllocCopy(reply, pw);
2.19      roeber    103: #endif
2.20      frystyk   104:     }
                    105:     return reply;
2.11      luotonen  106: }
                    107: 
                    108: 
                    109: /*     Prompt both username and password       HTPromptUsernameAndPassword()
                    110: **     ---------------------------------
                    111: ** On entry,
                    112: **     Msg             is the prompting message.
                    113: **     *username and
                    114: **     *password       are char pointers; they are changed
                    115: **                     to point to result strings.
                    116: **
                    117: **                     If *username is not NULL, it is taken
                    118: **                     to point to  a default value.
                    119: **                     Initial value of *password is
                    120: **                     completely discarded.
                    121: **
                    122: ** On exit,
                    123: **     *username and *password point to newly allocated
                    124: **     strings -- original strings pointed to by them
                    125: **     are NOT freed.
                    126: **     
                    127: */
                    128: PUBLIC void HTPromptUsernameAndPassword ARGS3(CONST char *,    Msg,
                    129:                                              char **,          username,
                    130:                                              char **,          password)
                    131: {
2.20.2.1! frystyk   132:     fprintf(TDEST, "%s\n", Msg ? Msg : "UNKNOWN");
2.11      luotonen  133:     *username = HTPrompt("Username: ", *username);
                    134:     *password = HTPromptPassword("Password: ");
2.8       luotonen  135: }
                    136: 
2.20.2.1! frystyk   137: 
        !           138: /*                                                             HTErrorMsg
        !           139: **
        !           140: **     Default function that creates an error message using HTAlert() to
        !           141: **     put out the contents of the error_stack messages. Furthermore, the
        !           142: **     error_info structure contains a name of a help file that might be put
        !           143: **     up as a link. This file can then be multi-linguistic.
        !           144: **
        !           145: **     This function might be overwritten by a smart server or client.
        !           146: */
        !           147: PUBLIC void HTErrorMsg ARGS1(HTRequest *, request)
        !           148: {
        !           149:     HTList *cur = request->error_stack;
        !           150:     BOOL highest = YES;
        !           151:     HTChunk *chunk;
        !           152:     HTErrorInfo *pres;
        !           153:     if (!request) {
        !           154:        if (TRACE) fprintf(TDEST, "HTErrorMsg.. Bad argument!\n");
        !           155:        return;
        !           156:     }
        !           157: 
        !           158:     /* This check is only necessary if the error message is put down the
        !           159:        stream, because we have to know if a stream has been put up and/or
        !           160:        taken down again. Here it is only put as an example */
        !           161: #if 0
        !           162:     if (request->error_block) {
        !           163:        if (TRACE) fprintf(TDEST, "HTErrorMsg.. No messages are printed as no stream is available.\n");
        !           164:        return;
        !           165:     }
        !           166: #endif
        !           167: 
        !           168:     /* Output messages */
        !           169:     chunk = HTChunkCreate(128);
        !           170:     while ((pres = (HTErrorInfo *) HTList_nextObject(cur))) {
        !           171: 
        !           172:        /* Check if we are going to show the message */
        !           173:        if ((!pres->ignore || HTErrorShowMask & HT_ERR_SHOW_IGNORE) && 
        !           174:            (HTErrorShowMask & pres->severity)) {
        !           175: 
        !           176:            /* Output code number */
        !           177:            if (highest) {                          /* If first time through */
        !           178:                if (TRACE)
        !           179:                    fprintf(TDEST,
        !           180:                            "HTError..... Generating message.\n");
        !           181:                
        !           182:                /* Output title */
        !           183:                if (pres->severity == ERR_WARN)
        !           184:                    HTChunkPuts(chunk, "Warning ");
        !           185:                else if (pres->severity == ERR_NON_FATAL)
        !           186:                    HTChunkPuts(chunk, "Non Fatal Error ");
        !           187:                else if (pres->severity == ERR_FATAL)
        !           188:                    HTChunkPuts(chunk, "Fatal Error ");
        !           189:                else if (pres->severity == ERR_INFO)
        !           190:                    HTChunkPuts(chunk, "Information ");
        !           191:                else {
        !           192:                    if (TRACE)
        !           193:                        fprintf(TDEST, "HTError..... Unknown Classification of Error (%d)...\n", pres->severity);
        !           194:                    HTChunkFree(chunk);
        !           195:                    return;
        !           196:                }
        !           197: 
        !           198:                /* Only output error code if it is a real HTTP code */
        !           199:                if (pres->element < HTERR_HTTP_CODES_END) {
        !           200:                    char codestr[10];
        !           201:                    sprintf(codestr, "%d ", error_info[pres->element].code);
        !           202:                    HTChunkPuts(chunk, codestr);
        !           203:                }
        !           204:                highest = NO;
        !           205:            } else
        !           206:                HTChunkPuts(chunk, "\nReason: ");
        !           207: 
        !           208:            /* Output error message */
        !           209:            if (pres->element != HTERR_SYSTEM) {
        !           210:                HTChunkPuts(chunk, error_info[pres->element].msg);
        !           211:                HTChunkPutc(chunk, ' ');
        !           212:            }
        !           213: 
        !           214:            /* Output parameters */
        !           215:            if (pres->par && HTErrorShowMask & HT_ERR_SHOW_PARS) {
        !           216:                unsigned int cnt;
        !           217:                char ch;
        !           218:                HTChunkPutc(chunk, '(');
        !           219:                for (cnt=0; cnt<pres->par_length; cnt++) {
        !           220:                    ch = *((char *)(pres->par)+cnt);
        !           221:                    if (ch < 0x20 || ch >= 0x7F)
        !           222:                        HTChunkPutc(chunk, '#'); /* Can't print real content */
        !           223:                    else
        !           224:                        HTChunkPutc(chunk, ch);
        !           225:                }
        !           226:                HTChunkPutc(chunk, ')');
        !           227:            }
        !           228: 
        !           229:            /* Output location */
        !           230:            if (pres->where && HTErrorShowMask & HT_ERR_SHOW_LOCATION) {
        !           231:                HTChunkPuts(chunk, "This occured in ");
        !           232:                HTChunkPuts(chunk, pres->where);
        !           233:                HTChunkPutc(chunk, '\n');
        !           234:            }
        !           235: 
        !           236:            /* We don't want the message more than once */
        !           237:            HTErrorIgnore(request, pres->handle);
        !           238:            
        !           239:            /* If we only are going to show the higest entry */
        !           240:            if (HTErrorShowMask & HT_ERR_SHOW_FIRST)
        !           241:                break;
        !           242:        }
        !           243:     }
        !           244:     HTChunkPutc(chunk,  '\n');
        !           245:     HTChunkTerminate(chunk);
        !           246:     if (chunk->size > 2)
        !           247:        HTAlert(chunk->data);
        !           248:     HTChunkFree(chunk);
        !           249:     return;
        !           250: }

Webmaster