2 * type=image is treated like submit
12 typedef struct Field Field;
13 typedef struct Option Option;
18 Field *fields, *efields;
27 int size; /* should be a point, but that feature is deprecated */
33 int state; /* is the button marked? */
63 #define BOUNDARY "nAboJ9uN6ZXsqoVGzLAdjKq97TWDTGjo"
65 void h_checkinput(Panel *, int, int);
66 void h_radioinput(Panel *, int, int);
67 void h_submitinput(Panel *, int);
68 void h_buttoninput(Panel *, int);
69 void h_fileinput(Panel *, int);
70 void h_submittype(Panel *, char *);
71 void h_submitindex(Panel *, char *);
72 void h_resetinput(Panel *, int);
73 void h_select(Panel *, int, int);
74 char *selgen(Panel *, int);
75 char *nullgen(Panel *, int);
76 Field *newfield(Form *form){
78 f=emallocz(sizeof(Field), 1);
82 form->efields->next=f;
89 * Called by rdhtml on seeing a forms-related tag
91 void rdform(Hglob *g){
98 fprint(2, "Bad tag <%s> in rdform (Can't happen!)\n", g->token);
102 htmlerror(g->name, g->lineno, "nested forms illegal\n");
105 g->form=emallocz(sizeof(Form), 1);
106 s=pl_getattr(g->attr, "action");
107 g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
108 s=pl_getattr(g->attr, "method");
111 else if(cistrcmp(s, "post")==0)
112 g->form->method=POST;
114 if(cistrcmp(s, "get")!=0)
115 htmlerror(g->name, g->lineno,
116 "unknown form method %s\n", s);
119 s=pl_getattr(g->attr, "enctype");
120 if(s && cistrcmp(s, "multipart/form-data")==0)
121 g->form->ctype = "multipart/form-data; boundary=" BOUNDARY;
124 g->form->next = g->dst->form;
125 g->dst->form = g->form;
132 htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
137 s=pl_getattr(g->attr, "name");
142 s=pl_getattr(g->attr, "value");
147 f->checked=pl_hasattr(g->attr, "checked");
148 s=pl_getattr(g->attr, "size");
153 s=pl_getattr(g->attr, "maxlength");
155 f->maxlength=0x3fffffff;
157 f->maxlength=atoi(s);
158 s=pl_getattr(g->attr, "type");
159 if((g->tag == Tag_button) &&
160 (s==0 || cistrcmp(s, "reset") || cistrcmp(s, "button")))
164 if(cistrcmp(s, "checkbox")==0)
166 else if(cistrcmp(s, "radio")==0)
168 else if(cistrcmp(s, "submit")==0)
170 else if(cistrcmp(s, "button")==0)
172 else if(cistrcmp(s, "image")==0)
174 else if(cistrcmp(s, "file")==0)
176 else if(cistrcmp(s, "reset")==0)
178 else if(cistrcmp(s, "hidden")==0)
182 if(cistrcmp(s, "password")==0)
186 if(s && cistrcmp(s, "isindex")==0)
190 * If there's exactly one attribute, use its value as the name,
191 * regardless of the attribute name. This makes
192 * http://linus.att.com/ias/puborder.html work.
195 if(g->attr[0].name && g->attr[1].name==0)
196 f->name=strdup(g->attr[0].value);
198 f->name=strdup("no-name");
201 if((f->type==CHECK || f->type==RADIO) && !pl_hasattr(g->attr, "value")){
203 f->value=strdup("on");
206 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
209 if(g->form==0) goto BadTag;
211 s=pl_getattr(g->attr, "name");
213 f->name=strdup("select");
214 htmlerror(g->name, g->lineno, "select has no name=\n");
218 s=pl_getattr(g->attr, "size");
222 if(f->size<=0) f->size=1;
224 f->multiple=pl_hasattr(g->attr, "multiple");
232 if(g->form==0) goto BadTag;
233 if((f=g->form->efields)==0) goto BadTag;
234 o=emallocz(sizeof(Option), 1);
235 for(op=&f->options;*op;op=&(*op)->next);
240 g->etext=o->label+NLABEL;
241 memset(o->label, 0, NLABEL+1);
243 o->def=pl_hasattr(g->attr, "selected");
245 if(pl_hasattr(g->attr, "disabled"))
247 s=pl_getattr(g->attr, "value");
254 if(g->form==0) goto BadTag;
256 s=pl_getattr(g->attr, "name");
258 f->name=strdup("enter text");
259 htmlerror(g->name, g->lineno, "select has no name=\n");
263 s=pl_getattr(g->attr, "rows");
265 s=pl_getattr(g->attr, "cols");
266 f->cols=s?atoi(s):30;
268 /* suck up initial text */
269 pl_htmloutput(g, g->nsp, f->name, f);
273 * Make up a form with one tag, of type INDEX
274 * I have seen a page with <ISINDEX PROMPT="Enter a title here ">,
275 * which is nonstandard and not handled here.
277 form=emalloc(sizeof(Form));
280 s=pl_getattr(g->attr, "action");
281 form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
288 f->maxlength=0x3fffffff;
290 pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
295 * Called by rdhtml on seeing a forms-related end tag
297 void endform(Hglob *g){
306 htmlerror(g->name, g->lineno, "</select> not in form, ignored\n");
307 else if((f=g->form->efields)==0)
308 htmlerror(g->name, g->lineno, "spurious </select>\n");
310 pl_htmloutput(g, g->nsp, f->name, f);
316 char *nullgen(Panel *, int ){
319 char *selgen(Panel *p, int index){
324 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
326 a->label[0]=a->selected?'*':' ';
329 char *seloption(Field *f){
331 for(a=f->options;a!=0;a=a->next)
336 void mkfieldpanel(Rtext *t){
338 Panel *win, *scrl, *menu, *pop, *button;
341 if((a = t->user) == nil)
343 if((f = a->field) == nil)
349 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submittype);
352 f->p=plentry(0, USERFL, f->size*chrwidth, f->value, h_submittype);
355 f->p=plcheckbutton(0, 0, "", h_checkinput);
357 plsetbutton(f->p, f->checked);
360 f->p=plradiobutton(0, 0, "", h_radioinput);
362 plsetbutton(f->p, f->checked);
365 f->p=plbutton(0, 0, f->value[0]?f->value:"submit", h_submitinput);
368 f->p=plbutton(0, 0, f->value[0]?f->value:"reset", h_resetinput);
371 f->p=plbutton(0, 0, f->value[0]?f->value:"button", h_buttoninput);
374 f->p=plbutton(0, 0, f->value[0]?f->value:"file", h_fileinput);
377 f->pulldown=plgroup(0,0);
378 scrl=plscrollbar(f->pulldown, PACKW|FILLY);
379 win=pllist(f->pulldown, PACKN, nullgen, f->size, h_select);
381 plinitlist(win, PACKN, selgen, f->size, h_select);
382 plscroll(win, 0, scrl);
383 plpack(f->pulldown, Rect(0,0,1024,1024));
384 f->p=plpulldown(0, FIXEDX, seloption(f), f->pulldown, PACKS);
385 f->p->fixedsize.x=f->pulldown->r.max.x-f->pulldown->r.min.x;
389 pllabel(f->p, PACKN|FILLX, f->name);
390 scrl=plscrollbar(f->p, PACKW|FILLY);
391 f->textwin=pledit(f->p, EXPAND, Pt(f->cols*chrwidth, f->rows*font->height),
394 plscroll(f->textwin, 0, scrl);
397 f->p=plentry(0, 0, f->size*chrwidth, f->value, h_submitindex);
408 void h_checkinput(Panel *p, int, int v){
409 ((Field *)p->userp)->state=v;
411 void h_radioinput(Panel *p, int, int v){
416 for(f=me->form->fields;f;f=f->next)
417 if(f->type==RADIO && f!=me && strcmp(f->name, me->name)==0){
418 plsetbutton(f->p, 0);
420 pldraw(f->p, screen);
424 void h_select(Panel *p, int, int index){
429 if(!f->multiple) for(a=f->options;a;a=a->next) a->selected=0;
430 for(a=f->options;index!=0 && a!=0;--index,a=a->next);
432 a->selected=!a->selected;
433 plinitpulldown(f->p, FIXEDX, seloption(f), f->pulldown, PACKS);
434 pldraw(f->p, screen);
436 void h_resetinput(Panel *p, int){
439 for(f=((Field *)p->userp)->form->fields;f;f=f->next) switch(f->type){
441 plinitentry(f->p, 0, f->size*chrwidth, f->value, 0);
444 plinitentry(f->p, USERFL, f->size*chrwidth, f->value, 0);
449 plsetbutton(f->p, f->checked);
452 for(o=f->options;o;o=o->next)
456 pldraw(text, screen);
458 void h_buttoninput(Panel *p, int){
460 void h_fileinput(Panel *p, int){
465 nstrcpy(name, f->value, sizeof(name));
469 if(eenter("Upload file", name, sizeof(name), &mouse) <= 0)
471 if(access(name, AREAD) == 0){
476 f->value = strdup(name);
477 pldraw(f->p, screen);
481 * If there's exactly one button with type=text, then
482 * a CR in the button is supposed to submit the form.
484 void h_submittype(Panel *p, char *){
488 for(f=((Field *)p->userp)->form->fields;f;f=f->next)
489 if(f->type==TYPEIN || f->type==PASSWD)
491 if(ntype==1) h_submitinput(p, 0);
493 void h_submitindex(Panel *p, char *){
497 void mencodeform(Form *form, int fd){
505 for(f=form->fields;f;f=f->next)switch(f->type){
508 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
509 sep, f->name, plentryval(f->p));
510 sep = "\r\n--" BOUNDARY;
517 if(f->name==0 || f->value==0)
519 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
520 sep, f->name, f->value);
521 sep = "\r\n--" BOUNDARY;
526 for(o=f->options;o;o=o->next)
527 if(o->selected && o->value){
528 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
529 sep, f->name, o->value);
530 sep = "\r\n--" BOUNDARY;
536 n=plelen(f->textwin);
537 rp=pleget(f->textwin);
538 p=b=malloc(UTFmax*n+1);
542 p += runetochar(p, rp);
547 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s",
549 sep = "\r\n--" BOUNDARY;
553 if(f->name==0 || f->value[0]==0)
555 if(p = strrchr(f->value, '/'))
557 if(p == 0 || *p == 0)
559 if((b = malloc(nb = 8192)) == nil)
561 if((ifd = open(f->value, OREAD)) >= 0){
562 if(filetype(ifd, b, nb) < 0)
563 strcpy(b, "application/octet-stream");
564 fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\""
565 "\r\nContent-Type: %s\r\n\r\n", sep, f->name, p, b);
566 sep = "\r\n--" BOUNDARY;
567 while((n = read(ifd, b, nb)) > 0)
568 if(write(fd, b, n) != n)
575 fprint(fd, "%s--\r\n", sep);
578 void uencodeform(Form *form, int fd){
586 for(f=form->fields;f;f=f->next) switch(f->type){
589 fprint(fd, "%s%U=%U", sep, f->name, plentryval(f->p));
593 fprint(fd, "%s%U", sep, plentryval(f->p));
601 if(f->name==0 || f->value==0)
603 fprint(fd, "%s%U=%U", sep, f->name, f->value);
609 for(o=f->options;o;o=o->next)
610 if(o->selected && o->value){
611 fprint(fd, "%s%U=%U", sep, f->name, o->value);
618 n=plelen(f->textwin);
619 rp=pleget(f->textwin);
620 p=b=malloc(UTFmax*n+1);
624 p += runetochar(p, rp);
629 fprint(fd, "%s%U=%U", sep, f->name, b);
636 void h_submitinput(Panel *p, int){
641 form=((Field *)p->userp)->form;
642 if(form->method==GET){
643 strcpy(buf, "/tmp/mfXXXXXXXXXXX");
644 fd = create(mktemp(buf), ORDWR|ORCLOSE, 0600);
646 fd = urlpost(selurl(form->action), form->ctype);
648 message("submit: %r");
651 if(form->method==GET){
652 fprint(fd, "%s?", form->action);
653 uencodeform(form, fd);
655 n = readn(fd, buf, sizeof(buf));
657 if(n < 0 || n >= sizeof(buf)){
658 message("submit: too large");
662 if(debug)fprint(2, "GET %s\n", buf);
663 geturl(buf, -1, 0, 0);
665 /* only set for multipart/form-data */
667 mencodeform(form, fd);
669 uencodeform(form, fd);
670 if(debug)fprint(2, "POST %s\n", form->action);
671 geturl(form->action, fd, 0, 0);
675 void freeform(void *p)
684 while(f = form->fields){
685 form->fields = f->next;
693 while(o = f->options){
694 f->options = o->next;
695 if(o->value != o->label+1)
707 char *s = va_arg(f->args, char*);
709 if(strchr("/$-_@.!*'(),", *s)
710 || 'a'<=*s && *s<='z'
711 || 'A'<=*s && *s<='Z'
712 || '0'<=*s && *s<='9')
713 fmtprint(f, "%c", *s);
717 fmtprint(f, "%%%.2X", (unsigned int)*s);