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

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

Webmaster