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