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

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

Webmaster