9 typedef struct Field Field;
10 typedef struct Option Option;
15 Field *fields, *efields;
24 int size; /* should be a point, but that feature is deprecated */
30 int state; /* is the button marked? */
60 #define BOUNDARY "nAboJ9uN6ZXsqoVGzLAdjKq97TWDTGjo"
62 void h_checkinput(Panel *, int, int);
63 void h_radioinput(Panel *, int, int);
64 void h_submitinput(Panel *, int);
65 void h_buttoninput(Panel *, int);
66 void h_fileinput(Panel *, int);
67 void h_submittype(Panel *, char *);
68 void h_submitindex(Panel *, char *);
69 void h_resetinput(Panel *, int);
70 void h_select(Panel *, int, int);
71 char *selgen(Panel *, int);
72 char *nullgen(Panel *, int);
73 Field *newfield(Form *form){
75 f=emallocz(sizeof(Field), 1);
79 form->efields->next=f;
86 * Called by rdhtml on seeing a forms-related tag
88 void rdform(Hglob *g){
95 fprint(2, "Bad tag <%s> in rdform (Can't happen!)\n", g->token);
99 htmlerror(g->name, g->lineno, "nested forms illegal\n");
102 g->form=emallocz(sizeof(Form), 1);
103 s=pl_getattr(g->attr, "action");
104 g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
105 s=pl_getattr(g->attr, "method");
108 else if(cistrcmp(s, "post")==0)
109 g->form->method=POST;
111 if(cistrcmp(s, "get")!=0)
112 htmlerror(g->name, g->lineno,
113 "unknown form method %s\n", s);
116 s=pl_getattr(g->attr, "enctype");
117 if(s && cistrcmp(s, "multipart/form-data")==0)
118 g->form->ctype = "multipart/form-data; boundary=" BOUNDARY;
121 g->form->next = g->dst->form;
122 g->dst->form = g->form;
129 htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
134 s=pl_getattr(g->attr, "name");
139 s=pl_getattr(g->attr, "value");
144 f->checked=pl_hasattr(g->attr, "checked");
145 s=pl_getattr(g->attr, "size");
150 s=pl_getattr(g->attr, "maxlength");
152 f->maxlength=0x3fffffff;
154 f->maxlength=atoi(s);
155 s=pl_getattr(g->attr, "type");
156 if((g->tag == Tag_button) &&
157 (s==0 || cistrcmp(s, "reset") || cistrcmp(s, "button")))
161 if(cistrcmp(s, "checkbox")==0)
163 else if(cistrcmp(s, "radio")==0)
165 else if(cistrcmp(s, "submit")==0)
167 else if(cistrcmp(s, "button")==0)
169 else if(cistrcmp(s, "image")==0)
171 else if(cistrcmp(s, "file")==0)
173 else if(cistrcmp(s, "reset")==0)
175 else if(cistrcmp(s, "hidden")==0)
179 if(cistrcmp(s, "password")==0)
183 if(s && cistrcmp(s, "isindex")==0)
187 * If there's exactly one attribute, use its value as the name,
188 * regardless of the attribute name. This makes
189 * http://linus.att.com/ias/puborder.html work.
192 if(g->attr[0].name && g->attr[1].name==0)
193 f->name=strdup(g->attr[0].value);
195 f->name=strdup("no-name");
198 if((f->type==CHECK || f->type==RADIO) && !pl_hasattr(g->attr, "value")){
200 f->value=strdup("on");
203 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
206 if(g->form==0) goto BadTag;
208 s=pl_getattr(g->attr, "name");
210 f->name=strdup("select");
211 htmlerror(g->name, g->lineno, "select has no name=\n");
215 s=pl_getattr(g->attr, "size");
219 if(f->size<=0) f->size=1;
221 f->multiple=pl_hasattr(g->attr, "multiple");
229 if(g->form==0) goto BadTag;
230 if((f=g->form->efields)==0) goto BadTag;
231 o=emallocz(sizeof(Option), 1);
232 for(op=&f->options;*op;op=&(*op)->next);
237 g->etext=o->label+NLABEL;
238 memset(o->label, 0, NLABEL+1);
240 o->def=pl_hasattr(g->attr, "selected");
242 if(pl_hasattr(g->attr, "disabled"))
244 s=pl_getattr(g->attr, "value");
251 if(g->form==0) goto BadTag;
253 s=pl_getattr(g->attr, "name");
255 f->name=strdup("enter text");
256 htmlerror(g->name, g->lineno, "select has no name=\n");
260 s=pl_getattr(g->attr, "rows");
262 s=pl_getattr(g->attr, "cols");
263 f->cols=s?atoi(s):30;
265 /* suck up initial text */
266 pl_htmloutput(g, g->nsp, f->name, f);
270 * Make up a form with one tag, of type INDEX
271 * I have seen a page with <ISINDEX PROMPT="Enter a title here ">,
272 * which is nonstandard and not handled here.
274 form=emalloc(sizeof(Form));
277 s=pl_getattr(g->attr, "action");
278 form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
285 f->maxlength=0x3fffffff;
287 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
292 * Called by rdhtml on seeing a forms-related end tag
294 void endform(Hglob *g){
303 htmlerror(g->name, g->lineno, "</select> not in form, ignored\n");
304 else if((f=g->form->efields)==0)
305 htmlerror(g->name, g->lineno, "spurious </select>\n");
307 pl_htmloutput(g, g->nsp, f->name, f);
313 char *nullgen(Panel *, int ){
316 char *selgen(Panel *p, int index){
321 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
323 a->label[0]=a->selected?'*':' ';
326 char *seloption(Field *f){
328 for(a=f->options;a!=0;a=a->next)
333 void mkfieldpanel(Rtext *t){
335 Panel *win, *scrl, *menu, *pop, *button;
338 if((a = t->user) == nil)
340 if((f = a->field) == nil)
346 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submittype);
349 f->p=plentry(0, USERFL, f->size*chrwidth, f->value, h_submittype);
352 f->p=plcheckbutton(0, 0, "", h_checkinput);
354 plsetbutton(f->p, f->checked);
357 f->p=plradiobutton(0, 0, "", h_radioinput);
359 plsetbutton(f->p, f->checked);
362 f->p=plbutton(0, 0, f->value[0]?f->value:"submit", h_submitinput);
365 f->p=plbutton(0, 0, f->value[0]?f->value:"reset", h_resetinput);
368 f->p=plbutton(0, 0, f->value[0]?f->value:"button", h_buttoninput);
371 f->p=plbutton(0, 0, f->value[0]?f->value:"file", h_fileinput);
374 f->pulldown=plgroup(0,0);
375 scrl=plscrollbar(f->pulldown, PACKW|FILLY);
376 win=pllist(f->pulldown, PACKN, nullgen, f->size, h_select);
378 plinitlist(win, PACKN, selgen, f->size, h_select);
379 plscroll(win, 0, scrl);
380 plpack(f->pulldown, Rect(0,0,1024,1024));
381 f->p=plpulldown(0, FIXEDX, seloption(f), f->pulldown, PACKS);
382 f->p->fixedsize.x=f->pulldown->r.max.x-f->pulldown->r.min.x;
386 pllabel(f->p, PACKN|FILLX, f->name);
387 scrl=plscrollbar(f->p, PACKW|FILLY);
388 f->textwin=pledit(f->p, EXPAND, Pt(f->cols*chrwidth, f->rows*font->height),
391 plscroll(f->textwin, 0, scrl);
394 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submitindex);
405 void h_checkinput(Panel *p, int, int v){
406 ((Field *)p->userp)->state=v;
408 void h_radioinput(Panel *p, int, int v){
413 for(f=me->form->fields;f;f=f->next)
414 if(f->type==RADIO && f!=me && strcmp(f->name, me->name)==0){
415 plsetbutton(f->p, 0);
417 pldraw(f->p, screen);
421 void h_select(Panel *p, int, int index){
426 if(!f->multiple) for(a=f->options;a;a=a->next) a->selected=0;
427 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
429 a->selected=!a->selected;
430 plinitpulldown(f->p, FIXEDX, seloption(f), f->pulldown, PACKS);
431 pldraw(f->p, screen);
433 void h_resetinput(Panel *p, int){
436 for(f=((Field *)p->userp)->form->fields;f;f=f->next) switch(f->type){
438 plinitentry(f->p, 0, f->size*chrwidth, f->value, 0);
441 plinitentry(f->p, USERFL, f->size*chrwidth, f->value, 0);
446 plsetbutton(f->p, f->checked);
449 for(o=f->options;o;o=o->next)
453 pldraw(text, screen);
455 void h_buttoninput(Panel *p, int){
457 void h_fileinput(Panel *p, int){
462 nstrcpy(name, f->value, sizeof(name));
464 if(eenter("Upload file", name, sizeof(name), &mouse) <= 0)
466 if(access(name, AREAD) == 0)
470 f->value = strdup(name);
471 p->state = name[0] != 0;
472 pldraw(f->p, screen);
476 * If there's exactly one button with type=text, then
477 * a CR in the button is supposed to submit the form.
479 void h_submittype(Panel *p, char *){
483 for(f=((Field *)p->userp)->form->fields;f;f=f->next)
484 if(f->type==TYPEIN || f->type==PASSWD)
486 if(ntype==1) h_submitinput(p, 0);
488 void h_submitindex(Panel *p, char *){
492 void mencodeform(Form *form, int fd){
500 for(f=form->fields;f;f=f->next)switch(f->type){
503 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
504 sep, f->name, plentryval(f->p));
505 sep = "\r\n--" BOUNDARY;
512 if(f->name==0 || f->value==0)
514 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
515 sep, f->name, f->value);
516 sep = "\r\n--" BOUNDARY;
521 for(o=f->options;o;o=o->next)
522 if(o->selected && o->value){
523 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
524 sep, f->name, o->value);
525 sep = "\r\n--" BOUNDARY;
531 n=plelen(f->textwin);
532 rp=pleget(f->textwin);
533 p=b=malloc(UTFmax*n+1);
537 p += runetochar(p, rp);
542 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
544 sep = "\r\n--" BOUNDARY;
548 if(f->name==0 || f->value[0]==0)
550 if(p = strrchr(f->value, '/'))
552 if(p == 0 || *p == 0)
554 if((b = malloc(nb = 8192)) == nil)
556 if((ifd = open(f->value, OREAD)) >= 0){
557 if(filetype(ifd, b, nb) < 0)
558 strcpy(b, "application/octet-stream");
559 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\""
560 "\r\nContent-Type: %s\r\n\r\n", sep, f->name, p, b);
561 sep = "\r\n--" BOUNDARY;
562 while((n = read(ifd, b, nb)) > 0)
563 if(write(fd, b, n) != n)
570 fprint(fd, "%s--\r\n", sep);
573 void uencodeform(Form *form, int fd){
581 for(f=form->fields;f;f=f->next) switch(f->type){
584 fprint(fd, "%s%U=%U", sep, f->name, plentryval(f->p));
588 fprint(fd, "%s%U", sep, plentryval(f->p));
596 if(f->name==0 || f->value==0)
598 fprint(fd, "%s%U=%U", sep, f->name, f->value);
604 for(o=f->options;o;o=o->next)
605 if(o->selected && o->value){
606 fprint(fd, "%s%U=%U", sep, f->name, o->value);
613 n=plelen(f->textwin);
614 rp=pleget(f->textwin);
615 p=b=malloc(UTFmax*n+1);
619 p += runetochar(p, rp);
624 fprint(fd, "%s%U=%U", sep, f->name, b);
631 void h_submitinput(Panel *p, int){
636 form=((Field *)p->userp)->form;
637 if(form->method==GET){
638 strcpy(buf, "/tmp/mfXXXXXXXXXXX");
639 fd = create(mktemp(buf), ORDWR|ORCLOSE, 0600);
641 fd = urlpost(selurl(form->action), form->ctype);
643 message("submit: %r");
646 if(form->method==GET){
647 fprint(fd, "%s?", form->action);
648 uencodeform(form, fd);
650 n = readn(fd, buf, sizeof(buf));
652 if(n < 0 || n >= sizeof(buf)){
653 message("submit: too large");
657 if(debug)fprint(2, "GET %s\n", buf);
658 geturl(buf, -1, 0, 0);
660 /* only set for multipart/form-data */
662 mencodeform(form, fd);
664 uencodeform(form, fd);
665 if(debug)fprint(2, "POST %s\n", form->action);
666 geturl(form->action, fd, 0, 0);
670 void freeform(void *p)
679 while(f = form->fields){
680 form->fields = f->next;
688 while(o = f->options){
689 f->options = o->next;
690 if(o->value != o->label+1)
702 char *s = va_arg(f->args, char*);
704 if(strchr("/$-_@.!*'(),", *s)
705 || 'a'<=*s && *s<='z'
706 || 'A'<=*s && *s<='Z'
707 || '0'<=*s && *s<='9')
708 fmtprint(f, "%c", *s);
712 fmtprint(f, "%%%.2X", *s & 0xFF);