]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/mothra/forms.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / mothra / forms.c
index aa5a8905694b421c6ce95b29efc3a23c7605614f..90cfe4683699429d1df4bfdedab1b8b924e67485 100644 (file)
@@ -101,9 +101,9 @@ void rdform(Hglob *g){
                }
                g->form=emalloc(sizeof(Form));
                s=pl_getattr(g->attr, "action");
-               g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
+               g->form->action=strdup((s && *s) ? s : g->dst->url->fullname);
                s=pl_getattr(g->attr, "method");
-               if(s==0)
+               if(s==0 || *s==0)
                        g->form->method=GET;
                else if(cistrcmp(s, "post")==0)
                        g->form->method=POST;
@@ -124,14 +124,20 @@ void rdform(Hglob *g){
        case Tag_input:
        case Tag_button:
                if(g->form==0){
-               BadTag:
-                       htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
-                               tag[g->tag].name);
-                       break;
-               }
-               f=newfield(g->form);
+                       /* no form, assume link button */
+                       form = emalloc(sizeof(Form));   
+                       form->method = 0;
+                       form->fields = 0;
+                       form->efields = 0;
+                       if(g->state->link[0])
+                               form->action = strdup(g->state->link);
+                       form->next = g->dst->form;
+                       g->dst->form = form;
+                       f=newfield(form);
+               } else
+                       f=newfield(g->form);
                s=pl_getattr(g->attr, "name");
-               if(s==0)
+               if(s==0 || *s == 0)
                        f->name=0;
                else
                        f->name=strdup(s);
@@ -142,12 +148,12 @@ void rdform(Hglob *g){
                        f->value=strdup(s);
                f->checked=pl_hasattr(g->attr, "checked");
                s=pl_getattr(g->attr, "size");
-               if(s==0)
+               if(s==0 || *s==0)
                        f->size=20;
                else
                        f->size=atoi(s);
                s=pl_getattr(g->attr, "maxlength");
-               if(s==0)
+               if(s==0 || *s==0)
                        f->maxlength=0x3fffffff;
                else
                        f->maxlength=atoi(s);
@@ -163,10 +169,27 @@ void rdform(Hglob *g){
                        f->type=RADIO;
                else if(cistrcmp(s, "submit")==0)
                        f->type=SUBMIT;
+               else if(cistrcmp(s, "image")==0){
+                       f->type=SUBMIT;
+                       s=pl_getattr(g->attr, "src");
+                       if(s && *s)
+                               nstrcpy(g->state->image, s, sizeof(g->state->image));
+                       s=pl_getattr(g->attr, "width");
+                       if(s && *s)
+                               g->state->width=strtolength(g, HORIZ, s);
+                       s=pl_getattr(g->attr, "height");
+                       if(s && *s)
+                               g->state->height=strtolength(g, VERT, s);
+                       s=pl_getattr(g->attr, "alt");
+                       if(s==0 || *s == 0) s = f->value;
+                       pl_htmloutput(g, g->nsp, s, f);
+                       g->state->image[0] = 0;
+                       g->state->width=0;
+                       g->state->height=0;
+                       break;
+               }
                else if(cistrcmp(s, "button")==0)
                        f->type=BUTTON;
-               else if(cistrcmp(s, "image")==0)
-                       f->type=FILE;
                else if(cistrcmp(s, "file")==0)
                        f->type=FILE;
                else if(cistrcmp(s, "reset")==0)
@@ -201,10 +224,15 @@ void rdform(Hglob *g){
                        pl_htmloutput(g, g->nsp, f->value[0]?f->value:"blank field", f);
                break;
        case Tag_select:
-               if(g->form==0) goto BadTag;
+               if(g->form==0){
+               BadTag:
+                       htmlerror(g->name, g->lineno, "<%s> not in form, ignored\n",
+                               tag[g->tag].name);
+                       break;
+               }
                f=newfield(g->form);
                s=pl_getattr(g->attr, "name");
-               if(s==0){
+               if(s==0 || *s==0){
                        f->name=strdup("select");
                        htmlerror(g->name, g->lineno, "select has no name=\n");
                }
@@ -246,16 +274,16 @@ void rdform(Hglob *g){
                if(g->form==0) goto BadTag;
                f=newfield(g->form);
                s=pl_getattr(g->attr, "name");
-               if(s==0){
+               if(s==0 || *s==0){
                        f->name=strdup("enter text");
                        htmlerror(g->name, g->lineno, "select has no name=\n");
                }
                else
                        f->name=strdup(s);
                s=pl_getattr(g->attr, "rows");
-               f->rows=s?atoi(s):8;
+               f->rows=(s && *s)?atoi(s):8;
                s=pl_getattr(g->attr, "cols");
-               f->cols=s?atoi(s):30;
+               f->cols=(s && *s)?atoi(s):30;
                f->type=TEXTWIN;
                /* suck up initial text */
                pl_htmloutput(g, g->nsp, f->name, f);
@@ -270,7 +298,7 @@ void rdform(Hglob *g){
                form->fields=0;
                form->efields=0;
                s=pl_getattr(g->attr, "action");
-               form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
+               form->action=strdup((s && *s) ? s : g->dst->url->fullname);
                form->method=GET;
                form->fields=0;
                f=newfield(form);
@@ -395,7 +423,7 @@ void mkfieldpanel(Rtext *t){
                free(t->text);
                t->text=0;
                t->p=f->p;
-               t->hot=1;
+               t->flags|=PL_HOT;
        }
 }
 void h_checkinput(Panel *p, int, int v){
@@ -455,8 +483,15 @@ void h_resetinput(Panel *p, int){
        }
        pldraw(text, screen);
 }
+
 void h_buttoninput(Panel *p, int){
+       Field *f;
+
+       f = p->userp;
+       if(f && f->form && f->form->method != POST && f->form->action)
+               geturl(f->form->action, -1, 0, 0);
 }
+
 void h_fileinput(Panel *p, int){
        char name[NNAME];
        Field *f;
@@ -638,11 +673,19 @@ void h_submitinput(Panel *p, int){
        for(f=form->fields;f;f=f->next)
                if(f->type==SUBMIT)
                        f->state = (f->p == p);
-       if(form->method==GET){
+
+       switch(form->method){
+       case GET:
                strcpy(buf, "/tmp/mfXXXXXXXXXXX");
                fd = create(mktemp(buf), ORDWR|ORCLOSE, 0600);
-       } else
+               break;
+       case POST:
                fd = urlpost(selurl(form->action), form->ctype);
+               break;
+       default:
+               return;
+       }
+
        if(fd < 0){
                message("submit: %r");
                return;
@@ -658,7 +701,6 @@ void h_submitinput(Panel *p, int){
                        return;
                }
                buf[n] = 0;
-               if(debug)fprint(2, "GET %s\n", buf);
                geturl(buf, -1, 0, 0);
        } else {
                /* only set for multipart/form-data */
@@ -666,7 +708,6 @@ void h_submitinput(Panel *p, int){
                        mencodeform(form, fd);
                else
                        uencodeform(form, fd);
-               if(debug)fprint(2, "POST %s\n", form->action);
                geturl(form->action, fd, 0, 0);
        }
 }