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

2.18      frystyk     1: /*                                                                   HTAlert.c
                      2: **     DISPLAYING MESSAGES AND GETTING INPUT FOR LINEMODE BROWSER
                      3: **
2.23      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.18      frystyk     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.21      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.21      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.21      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.21      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.24    ! frystyk    76:     fprintf(TDEST, "%s ", Msg ? Msg : "UNKNOWN");
2.20      frystyk    77:     if (deflt)
2.24    ! frystyk    78:        fprintf(TDEST, "(RETURN for [%s]) ", deflt);
2.20      frystyk    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 */
2.24    ! frystyk    85:        if (*buffer)
        !            86:            StrAllocCopy(reply, buffer);
        !            87:        else if (deflt)
        !            88:            StrAllocCopy(reply, deflt);
2.20      frystyk    89: #endif
                     90:     }
                     91:     return reply;
1.1       timbl      92: }
2.8       luotonen   93: 
                     94: 
2.20      frystyk    95: /*     Prompt for password without echoing the reply. Reply text is
                     96: **     either NULL on error or a dynamic string which the caller must free.
2.8       luotonen   97: */
                     98: PUBLIC char * HTPromptPassword ARGS1(CONST char *, Msg)
                     99: {
2.20      frystyk   100:     char *reply = NULL;
                    101:     if (HTInteractive) {
                    102: #ifndef NO_PASSWD
                    103:        char *pw = (char *) getpass(Msg ? Msg : "Password: ");
                    104:        if (pw)
                    105:            StrAllocCopy(reply, pw);
2.19      roeber    106: #endif
2.20      frystyk   107:     }
                    108:     return reply;
2.11      luotonen  109: }
                    110: 
                    111: 
                    112: /*     Prompt both username and password       HTPromptUsernameAndPassword()
                    113: **     ---------------------------------
                    114: ** On entry,
                    115: **     Msg             is the prompting message.
                    116: **     *username and
                    117: **     *password       are char pointers; they are changed
                    118: **                     to point to result strings.
                    119: **
                    120: **                     If *username is not NULL, it is taken
                    121: **                     to point to  a default value.
                    122: **                     Initial value of *password is
                    123: **                     completely discarded.
                    124: **
                    125: ** On exit,
                    126: **     *username and *password point to newly allocated
                    127: **     strings -- original strings pointed to by them
                    128: **     are NOT freed.
                    129: **     
                    130: */
                    131: PUBLIC void HTPromptUsernameAndPassword ARGS3(CONST char *,    Msg,
                    132:                                              char **,          username,
                    133:                                              char **,          password)
                    134: {
2.21      frystyk   135:     fprintf(TDEST, "%s\n", Msg ? Msg : "UNKNOWN");
2.24    ! frystyk   136:     *username = HTPrompt("Username:", *username);
2.11      luotonen  137:     *password = HTPromptPassword("Password: ");
2.8       luotonen  138: }
                    139: 
2.21      frystyk   140: 
                    141: /*                                                             HTErrorMsg
                    142: **
                    143: **     Default function that creates an error message using HTAlert() to
                    144: **     put out the contents of the error_stack messages. Furthermore, the
                    145: **     error_info structure contains a name of a help file that might be put
                    146: **     up as a link. This file can then be multi-linguistic.
                    147: **
                    148: **     This function might be overwritten by a smart server or client.
                    149: */
                    150: PUBLIC void HTErrorMsg ARGS1(HTRequest *, request)
                    151: {
                    152:     HTList *cur = request->error_stack;
                    153:     BOOL highest = YES;
                    154:     HTChunk *chunk;
                    155:     HTErrorInfo *pres;
                    156:     if (!request) {
                    157:        if (TRACE) fprintf(TDEST, "HTErrorMsg.. Bad argument!\n");
                    158:        return;
                    159:     }
                    160: 
                    161:     /* This check is only necessary if the error message is put down the
                    162:        stream, because we have to know if a stream has been put up and/or
                    163:        taken down again. Here it is only put as an example */
                    164: #if 0
                    165:     if (request->error_block) {
                    166:        if (TRACE) fprintf(TDEST, "HTErrorMsg.. No messages are printed as no stream is available.\n");
                    167:        return;
                    168:     }
                    169: #endif
                    170: 
                    171:     /* Output messages */
                    172:     chunk = HTChunkCreate(128);
                    173:     while ((pres = (HTErrorInfo *) HTList_nextObject(cur))) {
                    174: 
                    175:        /* Check if we are going to show the message */
                    176:        if ((!pres->ignore || HTErrorShowMask & HT_ERR_SHOW_IGNORE) && 
                    177:            (HTErrorShowMask & pres->severity)) {
                    178: 
                    179:            /* Output code number */
                    180:            if (highest) {                          /* If first time through */
                    181:                if (TRACE)
                    182:                    fprintf(TDEST,
                    183:                            "HTError..... Generating message.\n");
                    184:                
                    185:                /* Output title */
                    186:                if (pres->severity == ERR_WARN)
                    187:                    HTChunkPuts(chunk, "Warning ");
                    188:                else if (pres->severity == ERR_NON_FATAL)
                    189:                    HTChunkPuts(chunk, "Non Fatal Error ");
                    190:                else if (pres->severity == ERR_FATAL)
                    191:                    HTChunkPuts(chunk, "Fatal Error ");
                    192:                else if (pres->severity == ERR_INFO)
                    193:                    HTChunkPuts(chunk, "Information ");
                    194:                else {
                    195:                    if (TRACE)
                    196:                        fprintf(TDEST, "HTError..... Unknown Classification of Error (%d)...\n", pres->severity);
                    197:                    HTChunkFree(chunk);
                    198:                    return;
                    199:                }
                    200: 
                    201:                /* Only output error code if it is a real HTTP code */
                    202:                if (pres->element < HTERR_HTTP_CODES_END) {
                    203:                    char codestr[10];
                    204:                    sprintf(codestr, "%d ", error_info[pres->element].code);
                    205:                    HTChunkPuts(chunk, codestr);
                    206:                }
                    207:                highest = NO;
                    208:            } else
                    209:                HTChunkPuts(chunk, "\nReason: ");
                    210: 
                    211:            /* Output error message */
                    212:            if (pres->element != HTERR_SYSTEM) {
                    213:                HTChunkPuts(chunk, error_info[pres->element].msg);
                    214:                HTChunkPutc(chunk, ' ');
                    215:            }
                    216: 
                    217:            /* Output parameters */
                    218:            if (pres->par && HTErrorShowMask & HT_ERR_SHOW_PARS) {
                    219:                unsigned int cnt;
                    220:                char ch;
2.22      frystyk   221:                HTChunkPutc(chunk, '(');
2.21      frystyk   222:                for (cnt=0; cnt<pres->par_length; cnt++) {
                    223:                    ch = *((char *)(pres->par)+cnt);
                    224:                    if (ch < 0x20 || ch >= 0x7F)
                    225:                        HTChunkPutc(chunk, '#'); /* Can't print real content */
                    226:                    else
                    227:                        HTChunkPutc(chunk, ch);
                    228:                }
2.22      frystyk   229:                HTChunkPutc(chunk, ')');
2.21      frystyk   230:            }
                    231: 
                    232:            /* Output location */
                    233:            if (pres->where && HTErrorShowMask & HT_ERR_SHOW_LOCATION) {
                    234:                HTChunkPuts(chunk, "This occured in ");
                    235:                HTChunkPuts(chunk, pres->where);
                    236:                HTChunkPutc(chunk, '\n');
                    237:            }
                    238: 
                    239:            /* We don't want the message more than once */
                    240:            HTErrorIgnore(request, pres->handle);
                    241:            
                    242:            /* If we only are going to show the higest entry */
                    243:            if (HTErrorShowMask & HT_ERR_SHOW_FIRST)
                    244:                break;
                    245:        }
                    246:     }
                    247:     HTChunkPutc(chunk,  '\n');
                    248:     HTChunkTerminate(chunk);
                    249:     if (chunk->size > 2)
                    250:        HTAlert(chunk->data);
                    251:     HTChunkFree(chunk);
                    252:     return;
                    253: }

Webmaster