test/pipe.c: Avoid printing hundreds of thousand output lines.
[AROS.git] / tools / genmodule / config.c
blob2808604d4744356b1d5c0c68af11aac9fad7349e
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Code to parse the command line options and the module config file for
6 the genmodule program
7 */
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #define __USE_XOPEN
13 #include <time.h>
14 #include <unistd.h>
15 #include <limits.h>
17 #include "functionhead.h"
18 #include "config.h"
20 const static char bannertemplate[] =
21 "/*\n"
22 " *** Automatically generated from '%s'. Edits will be lost. ***\n"
23 " Copyright © 1995-%4u, The AROS Development Team. All rights reserved.\n"
24 "*/\n";
26 char*
27 getBanner(struct config* config)
29 static unsigned currentyear = 0;
31 int bannerlength = strlen(config->conffile) + strlen(bannertemplate);
32 // No additional bytes needed because
33 // + 1 (NUL) + 4 (4 digit year) - strlen("%s") - strlen("%4u) = 0
35 char * banner = malloc(bannerlength);
37 if (currentyear == 0)
39 time_t rawtime;
40 time(&rawtime);
41 struct tm *utctm = gmtime(&rawtime);
42 currentyear = utctm->tm_year + 1900;
45 snprintf (banner, bannerlength, bannertemplate, config->conffile, currentyear);
47 return(banner);
50 void
51 freeBanner(char *banner)
53 free((void *)banner);
56 const static char usage[] =
57 "\n"
58 "Usage: genmodule [-c conffile] [-s suffix] [-d gendir] [-n]\n"
59 " {writefiles|writemakefile|writeincludes|writelibdefs|writefunclist|writefd|writeskel} modname modtype\n"
62 static void readconfig(struct config *);
63 static struct classinfo *newclass(struct config *);
64 static struct handlerinfo *newhandler(struct config *);
65 static struct interfaceinfo *newinterface(struct config *);
67 /* the method prefices for the supported classes */
68 static const char *muimprefix[] =
70 "__OM_",
71 "__MUIM_",
72 NULL
74 static const char *gadgetmprefix[] =
76 "__OM_",
77 "__GM_",
78 "__AROSM_",
79 NULL
81 static const char *dtmprefix[] =
83 "__OM_",
84 "__GM_",
85 "__DTM_",
86 "__PDTM_",
87 NULL
90 /* Create a config struct. Initialize with the values from the programs command
91 * line arguments and the contents of the modules .conf file
93 struct config *initconfig(int argc, char **argv)
95 struct config *cfg;
96 char *s, **argvit = argv + 1;
97 int hassuffix = 0, c;
99 cfg = malloc(sizeof(struct config));
100 if (cfg == NULL)
102 fprintf(stderr, "Out of memory\n");
103 exit(20);
106 memset(cfg, 0, sizeof(struct config));
108 while ((c = getopt(argc, argv, ":c:s:d:v:")) != -1)
110 if (c == ':')
112 fprintf(stderr, "Option -%c needs an argument\n",optopt);
113 exit(20);
116 switch (c)
118 case 'c':
119 cfg->conffile = optarg;
120 break;
122 case 's':
123 cfg->suffix = optarg;
124 hassuffix = 1;
125 break;
127 case 'd':
128 /* Remove / at end if present */
129 if ((optarg)[strlen(*argvit)-1]=='/') (optarg)[strlen(optarg)-1]='\0';
130 cfg->gendir = optarg;
131 break;
133 case 'v':
134 cfg->versionextra = optarg;
135 break;
137 default:
138 fprintf(stderr, "Internal error: Unhandled option\n");
139 exit(20);
143 if (optind + 3 != argc)
145 fprintf(stderr, "Wrong number of arguments.\n%s", usage);
146 exit(20);
149 if (strcmp(argv[optind], "writefiles") == 0)
151 cfg->command = FILES;
153 else if (strcmp(argv[optind], "writemakefile") == 0)
155 cfg->command = MAKEFILE;
157 else if (strcmp(argv[optind], "writeincludes") == 0)
159 cfg->command = INCLUDES;
161 else if (strcmp(argv[optind], "writelibdefs") == 0)
163 cfg->command = LIBDEFS;
165 else if (strcmp(argv[optind], "writefunclist") == 0)
167 cfg->command = WRITEFUNCLIST;
169 else if (strcmp(argv[optind], "writefd") == 0)
171 cfg->command = WRITEFD;
173 else if (strcmp(argv[optind], "writeskel") == 0)
175 cfg->command = WRITESKEL;
177 else
179 fprintf(stderr, "Unrecognized argument \"%s\"\n%s", argv[optind], usage);
180 exit(20);
183 cfg->modulename = argv[optind+1];
184 cfg->modulenameupper = strdup(cfg->modulename);
185 for (s=cfg->modulenameupper; *s!='\0'; *s = toupper(*s), s++) {
186 if (!isalnum(*s)) *s = '_';
189 if (strcmp(argv[optind+2],"library")==0)
191 cfg->modtype = LIBRARY;
192 cfg->moddir = "Libs";
194 else if (strcmp(argv[optind+2],"mcc")==0)
196 cfg->modtype = MCC;
197 cfg->moddir = "Classes/Zune";
199 else if (strcmp(argv[optind+2],"mui")==0)
201 cfg->modtype = MUI;
202 cfg->moddir = "Classes/Zune";
204 else if (strcmp(argv[optind+2],"mcp")==0)
206 cfg->modtype = MCP;
207 cfg->moddir = "Classes/Zune";
209 else if (strcmp(argv[optind+2], "device")==0)
211 cfg->modtype = DEVICE;
212 cfg->moddir = "Devs";
214 else if (strcmp(argv[optind+2], "resource")==0)
216 cfg->modtype = RESOURCE;
217 cfg->moddir = "Devs";
219 else if (strcmp(argv[optind+2], "gadget")==0)
221 cfg->modtype = GADGET;
222 cfg->moddir = "Classes/Gadgets";
224 else if (strcmp(argv[optind+2], "datatype")==0)
226 cfg->modtype = DATATYPE;
227 cfg->moddir = "Classes/DataTypes";
229 else if (strcmp(argv[optind+2], "usbclass")==0)
231 cfg->modtype = USBCLASS;
232 cfg->moddir = "Classes/USB";
233 if(!hassuffix)
235 cfg->suffix = "class";
236 hassuffix = 1;
239 else if (strcmp(argv[optind+2], "hidd")==0)
241 cfg->modtype = HIDD;
242 cfg->moddir = "Devs/Drivers";
244 else if (strcmp(argv[optind+2], "handler")==0)
246 cfg->modtype = HANDLER;
247 cfg->moddir = "$(AROS_DIR_FS)";
249 else if (strcmp(argv[optind+2], "hook")==0)
251 cfg->modtype = HANDLER;
252 cfg->moddir = "Devs";
254 else
256 fprintf(stderr, "Unknown modtype \"%s\" specified for second argument\n", argv[optind+2]);
257 exit(20);
259 cfg->modtypestr = argv[optind+2];
261 if (!hassuffix)
262 cfg->suffix = argv[optind+2];
264 /* Fill fields with default value if not specified on the command line */
266 char tmpbuf[256];
268 if (cfg->conffile == NULL)
270 snprintf(tmpbuf, sizeof(tmpbuf), "%s.conf", cfg->modulename);
271 cfg->conffile = strdup(tmpbuf);
274 if (cfg->gendir == NULL)
275 cfg->gendir = ".";
278 readconfig(cfg);
280 /* For a device add the functions given in beginiofunc and abortiofunc to the functionlist
281 * if they are provided
283 if (cfg->beginiofunc != NULL)
285 struct functionhead *funchead;
287 cfg->intcfg |= CFG_NOREADFUNCS;
289 /* Add beginio_func to the list of functions */
290 funchead = newfunctionhead(cfg->beginiofunc, REGISTERMACRO);
291 funchead->type = strdup("void");
292 funchead->lvo = 5;
293 funcaddarg(funchead, "struct IORequest *ioreq", "A1");
295 funchead->next = cfg->funclist;
296 cfg->funclist = funchead;
298 /* Add abortio_func to the list of functions */
299 funchead = newfunctionhead(cfg->abortiofunc, REGISTERMACRO);
300 funchead->type = strdup("LONG");
301 funchead->lvo = 6;
302 funcaddarg(funchead, "struct IORequest *ioreq", "A1");
304 funchead->next = cfg->funclist->next;
305 cfg->funclist->next = funchead;
307 else if (cfg->modtype == DEVICE && cfg->intcfg & CFG_NOREADFUNCS)
309 fprintf
311 stderr,
312 "beginio_func and abortio_func missing for a device with a non empty function list\n"
314 exit(20);
317 /* See if we have any stackcall options */
318 if (cfg->funclist) {
319 struct functionhead *funchead;
321 for (funchead = cfg->funclist; funchead; funchead = funchead->next) {
322 if (funchead->libcall == STACK) {
323 cfg->options |= OPTION_STACKCALL;
324 break;
329 /* Verify that a handler has a handler */
330 if (cfg->modtype == HANDLER) {
331 if (cfg->handlerfunc == NULL) {
332 fprintf(stderr, "handler modules require a 'handler_func' ##config option\n");
333 exit(20);
335 cfg->options |= OPTION_NOAUTOLIB | OPTION_NOEXPUNGE | OPTION_NOOPENCLOSE;
338 return cfg;
341 /* Functions to read configuration from the configuration file */
343 #include "fileread.h"
345 static char *readsections(struct config *, struct classinfo *cl, struct interfaceinfo *in, int inclass);
346 static void readsectionconfig(struct config *, struct classinfo *cl, struct interfaceinfo *in, int inclass);
347 static void readsectioncdef(struct config *);
348 static void readsectioncdefprivate(struct config *);
349 static void readsectionstartup(struct config *);
350 static void readsectionfunctionlist(const char *type, struct functionhead **funclistptr, unsigned int firstlvo, int isattribute);
351 static void readsectionclass_methodlist(struct classinfo *);
352 static void readsectionclass(struct config *);
353 static void readsectionhandler(struct config *);
354 static void readsectioninterface(struct config *);
356 static void readconfig(struct config *cfg)
358 struct classinfo *mainclass = NULL;
360 /* Create a classinfo structure if this module is a class */
361 switch (cfg->modtype)
363 case LIBRARY:
364 case DEVICE:
365 case RESOURCE:
366 case USBCLASS:
367 case HANDLER:
368 break;
370 case MCC:
371 case MUI:
372 case MCP:
373 case GADGET:
374 case DATATYPE:
375 case HIDD:
376 mainclass = newclass(cfg);
377 mainclass->classtype = cfg->modtype;
378 break;
380 default:
381 fprintf(stderr, "Internal error: unsupported modtype for classinfo creation\n");
382 exit(20);
385 switch (cfg->modtype)
387 case LIBRARY:
388 case USBCLASS:
389 cfg->firstlvo = 5;
390 break;
391 case DEVICE:
392 cfg->firstlvo = 7;
393 break;
394 case MCC:
395 case MUI:
396 case MCP:
397 cfg->firstlvo = 6;
398 mainclass->boopsimprefix = muimprefix;
399 break;
400 case HANDLER:
401 case RESOURCE:
402 cfg->firstlvo = 1;
403 break;
404 case GADGET:
405 cfg->firstlvo = 5;
406 mainclass->boopsimprefix = gadgetmprefix;
407 break;
408 case DATATYPE:
409 cfg->firstlvo = 6;
410 mainclass->boopsimprefix = dtmprefix;
411 break;
412 case HIDD:
413 cfg->firstlvo = 5;
414 /* FIXME: need boopsimprefix ? */
415 break;
416 default:
417 fprintf(stderr, "Internal error: unsupported modtype for firstlvo\n");
418 exit(20);
421 if (!fileopen(cfg->conffile))
423 fprintf(stderr, "In readconfig: Could not open %s\n", cfg->conffile);
424 exit(20);
427 /* Read all sections and see that we are at the end of the file */
428 if (readsections(cfg, mainclass, NULL, 0) != NULL)
429 exitfileerror(20, "Syntax error");
431 fileclose();
434 /* readsections will scan through all the sections in the config file.
435 * arguments:
436 * struct config *cfg: The module config data which may be updated by
437 * the information in the sections
438 * struct classinfo *cl: The classdata to be filled with data from the sections.
439 * This may be NULL if this is the main part of the configuration file and the
440 * type of the module is not a class
441 * int inclass: Boolean to indicate if we are in a class part. If not we are in the main
442 * part of the config file.
444 static char *readsections(struct config *cfg, struct classinfo *cl, struct interfaceinfo *in, int inclass)
446 char *line, *s, *s2;
447 int hasconfig = 0;
449 while ((line=readline())!=NULL)
451 if (strncmp(line, "##", 2)==0)
453 static char *parts[] =
455 "config", "cdefprivate", "cdef", "startup", "functionlist", "methodlist", "class", "handler", "interface", "attributelist"
457 const unsigned int nums = sizeof(parts)/sizeof(char *);
458 unsigned int partnum;
459 int i, atend = 0;
461 s = line+2;
462 while (isspace(*s)) s++;
464 if (strncmp(s, "begin", 5)!=0)
465 return line;
467 s += 5;
468 if (!isspace(*s))
469 exitfileerror(20, "space after begin expected\n");
470 while (isspace(*s)) s++;
472 for (i = 0, partnum = 0; partnum==0 && i<nums; i++)
474 if (strncmp(s, parts[i], strlen(parts[i]))==0)
476 partnum = i+1;
477 s += strlen(parts[i]);
478 while (isspace(*s)) s++;
479 if (*s!='\0')
480 exitfileerror(20, "unexpected character on position %d\n", s-line);
483 if (partnum==0)
484 exitfileerror(20, "unknown start of section\n");
485 switch (partnum)
487 case 1: /* config */
488 readsectionconfig(cfg, cl, in, inclass);
489 hasconfig = 1;
490 break;
492 case 2: /* cdefprivate */
493 if (inclass)
494 exitfileerror(20, "cdefprivate section not allowed in class section\n");
495 readsectioncdefprivate(cfg);
496 break;
498 case 3: /* cdef */
499 if (inclass)
500 exitfileerror(20, "cdef section not allowed in class section\n");
501 readsectioncdef(cfg);
502 break;
504 case 4: /* startup */
505 if (inclass)
506 exitfileerror(20, "startup section not allowed in class section\n");
507 readsectionstartup(cfg);
508 break;
510 case 5: /* functionlist */
511 if (inclass)
512 exitfileerror(20, "functionlist section not allow in class section\n");
513 if (cfg->basename==NULL)
514 exitfileerror(20, "section functionlist has to come after section config\n");
516 readsectionfunctionlist("functionlist", &cfg->funclist, cfg->firstlvo, 0);
517 cfg->intcfg |= CFG_NOREADFUNCS;
518 break;
520 case 6: /* methodlist */
521 if (cl == NULL && in == NULL)
522 exitfileerror(20, "methodlist section when not in a class or interface\n");
523 if (cl)
524 readsectionclass_methodlist(cl);
525 else
526 readsectionfunctionlist("methodlist", &in->methodlist, 0, 0);
527 cfg->intcfg |= CFG_NOREADFUNCS;
528 break;
530 case 7: /* class */
531 if (inclass)
532 exitfileerror(20, "class section may not be in nested\n");
533 readsectionclass(cfg);
534 break;
535 case 8: /* handler */
536 readsectionhandler(cfg);
537 break;
538 case 9: /* interface */
539 if (inclass)
540 exitfileerror(20, "interface section may not be nested\n");
541 readsectioninterface(cfg);
542 break;
543 case 10: /* attributelist */
544 if (!in)
545 exitfileerror(20, "attributelist only valid in interface sections\n");
546 readsectionfunctionlist("attributelist", &in->attributelist, 0, 1);
547 break;
550 else if (strlen(line)!=0)
551 filewarning("warning line outside section ignored\n");
554 if(!inclass)
556 if (!hasconfig)
557 exitfileerror(20, "No config section in conffile\n");
559 /* If no indication was given for generating includes or not
560 decide on module type and if there are functions
562 if(!((cfg->options & OPTION_INCLUDES) || (cfg->options & OPTION_NOINCLUDES)))
564 switch (cfg->modtype)
566 case LIBRARY:
567 case RESOURCE:
568 cfg->options |= OPTION_INCLUDES;
569 break;
571 case HANDLER:
572 case MCC:
573 case MUI:
574 case MCP:
575 case USBCLASS:
576 cfg->options |= OPTION_NOINCLUDES;
577 break;
579 case DEVICE:
580 cfg->options |= (
581 (cfg->funclist != NULL)
582 || (cfg->cdeflines != NULL)
583 || strcmp(cfg->libbasetypeptrextern, "struct Device *") != 0
584 ) ? OPTION_INCLUDES : OPTION_NOINCLUDES;
585 break;
587 case GADGET:
588 case DATATYPE:
589 case HIDD:
590 cfg->options |= (
591 (cfg->funclist != NULL)
592 ) ? OPTION_INCLUDES : OPTION_NOINCLUDES;
593 break;
595 default:
596 fprintf(stderr, "Internal error writemakefile: unhandled modtype for includes\n");
597 exit(20);
598 break;
602 /* If no indication was given for not generating stubs only generate them if
603 * the module has functions
605 if(!((cfg->options & OPTION_STUBS) || (cfg->options & OPTION_NOSTUBS)))
607 switch (cfg->modtype)
609 case LIBRARY:
610 cfg->options |= (cfg->funclist != NULL) ? OPTION_STUBS : OPTION_NOSTUBS;
611 break;
613 case USBCLASS:
614 case RESOURCE:
615 case GADGET:
616 case DEVICE:
617 case DATATYPE:
618 case MCC:
619 case MUI:
620 case MCP:
621 case HIDD:
622 case HANDLER:
623 cfg->options |= OPTION_NOSTUBS;
624 break;
626 default:
627 fprintf(stderr, "Internal error writemakefile: unhandled modtype for stubs\n");
628 exit(20);
629 break;
633 /* If no indication was given for generating autoinit code or not
634 decide on module type
636 if(!((cfg->options & OPTION_AUTOINIT) || (cfg->options & OPTION_NOAUTOINIT)))
638 switch (cfg->modtype)
640 case LIBRARY:
641 cfg->options |= OPTION_AUTOINIT;
642 break;
644 case USBCLASS:
645 case RESOURCE:
646 case GADGET:
647 case DEVICE:
648 case DATATYPE:
649 case MCC:
650 case MUI:
651 case MCP:
652 case HIDD:
653 case HANDLER:
654 cfg->options |= OPTION_NOAUTOINIT;
655 break;
657 default:
658 fprintf(stderr, "Internal error writemakefile: unhandled modtype for autoinit\n");
659 exit(20);
660 break;
664 if ((cfg->modtype == RESOURCE) || (cfg->modtype == HANDLER))
665 /* Enforce noopenclose for resources and handlers */
666 cfg->options |= OPTION_NOOPENCLOSE;
667 else if (!(cfg->options & OPTION_SELFINIT))
668 /* Enforce using RTF_AUTOINIT for everything except resources */
669 cfg->options |= OPTION_RESAUTOINIT;
672 return NULL;
675 static void readsectionconfig(struct config *cfg, struct classinfo *cl, struct interfaceinfo *in, int inclass)
677 int atend = 0, i;
678 char *line, *s, *s2, *libbasetypeextern = NULL;
679 struct tm date;
681 while (!atend)
683 line = readline();
684 if (line==NULL)
685 exitfileerror(20, "unexpected end of file in section config\n");
687 if (strncmp(line, "##", 2)!=0)
689 const char *names[] =
691 "basename", "libbase", "libbasetype", "libbasetypeextern",
692 "version", "date", "copyright", "libcall", "forcebase", "superclass",
693 "superclass_field", "residentpri", "options", "sysbase_field",
694 "seglist_field", "rootbase_field", "classptr_field", "classptr_var",
695 "classid", "classdatatype", "beginio_func", "abortio_func", "dispatcher",
696 "initpri", "type", "addromtag", "oopbase_field",
697 "rellib", "interfaceid", "interfacename",
698 "methodstub", "methodbase", "attributebase", "handler_func"
700 const unsigned int namenums = sizeof(names)/sizeof(char *);
701 unsigned int namenum;
703 for (i = 0, namenum = 0; namenum==0 && i<namenums; i++)
707 strncmp(line, names[i], strlen(names[i]))==0
708 && isspace(*(line+strlen(names[i])))
710 namenum = i+1;
712 if (namenum==0)
713 exitfileerror(20, "unrecognized configuration option\n");
715 s = line + strlen(names[namenum-1]);
716 if (!isspace(*s))
717 exitfileerror(20, "space character expected after \"%s\"\n", names[namenum-1]);
719 while (isspace(*s)) s++;
720 if (*s=='\0')
721 exitfileerror(20, "unexpected end of line\n");
723 s2 = s + strlen(s);
724 while (isspace(*(s2-1))) s2--;
725 *s2 = '\0';
727 switch (namenum)
729 case 1: /* basename */
730 if (!inclass)
731 cfg->basename = strdup(s);
732 if (cl != NULL)
733 cl->basename = strdup(s);
734 if (in != NULL)
735 exitfileerror(20, "basename not valid config option when in a interface section\n");
736 break;
738 case 2: /* libbase */
739 if (inclass)
740 exitfileerror(20, "libbase not valid config option when in a class section\n");
741 cfg->libbase = strdup(s);
742 break;
744 case 3: /* libbasetype */
745 if (inclass)
746 exitfileerror(20, "libbasetype not valid config option when in a class section\n");
747 cfg->libbasetype = strdup(s);
748 break;
750 case 4: /* libbasetypeextern */
751 if (inclass)
752 exitfileerror(20, "libbasetype not valid config option when in a class section\n");
753 libbasetypeextern = strdup(s);
754 break;
756 case 5: /* version */
757 if (inclass)
758 exitfileerror(20, "version not valid config option when in a class section\n");
759 if (sscanf(s, "%u.%u", &cfg->majorversion, &cfg->minorversion)!=2)
760 exitfileerror(20, "wrong version string \"%s\"\n", s);
761 break;
763 case 6: /* date */
764 if (inclass)
765 exitfileerror(20, "date not valid config option when in a class section\n");
766 #ifndef _WIN32
767 if (strptime(s, "%e.%m.%Y", &date) == NULL)
769 exitfileerror(20, "date string has to have d.m.yyyy format\n");
771 #endif
772 cfg->datestring = strdup(s);
773 break;
775 case 7: /* copyright */
776 if (inclass)
777 exitfileerror(20, "copyright not valid config option when in a class section\n");
778 cfg->copyright = strdup(s);
779 break;
781 case 8: /* libcall */
782 fprintf(stderr, "libcall specification is deprecated and ignored\n");
783 break;
785 case 9: /* forcebase */
786 if (inclass)
787 exitfileerror(20, "forcebase not valid config option when in a class section\n");
788 slist_append(&cfg->forcelist, s);
789 break;
791 case 10: /* superclass */
792 if (cl == NULL)
793 exitfileerror(20, "superclass specified when not a BOOPSI class\n");
794 cl->superclass = strdup(s);
795 break;
797 case 11: /* superclass_field */
798 if (cl == NULL)
799 exitfileerror(20, "superclass_field specified when not a BOOPSI class\n");
800 cl->superclass_field = strdup(s);
801 break;
803 case 12: /* residentpri */
804 if (!inclass)
806 int count;
807 char dummy;
809 count = sscanf(s, "%d%c", &cfg->residentpri, &dummy);
810 if (count != 1 ||
811 cfg->residentpri < -128 || cfg->residentpri > 127
814 exitfileerror(20, "residentpri number format error\n");
817 else
818 exitfileerror(20, "residentpri not valid config option when in a class section\n");
819 break;
821 case 13: /* options */
822 if (!inclass)
824 static const char *optionnames[] =
826 "noautolib", "noexpunge", "noresident", "peropenerbase",
827 "pertaskbase", "includes", "noincludes", "nostubs",
828 "autoinit", "noautoinit", "resautoinit", "noopenclose",
829 "selfinit"
831 const unsigned int optionnums = sizeof(optionnames)/sizeof(char *);
832 int optionnum;
836 for (i = 0, optionnum = 0; optionnum==0 && i<optionnums; i++)
838 if (strncmp(s, optionnames[i], strlen(optionnames[i]))==0)
840 optionnum = i + 1;
841 s += strlen(optionnames[i]);
842 while (isspace(*s)) s++;
843 if (*s == ',')
844 s++;
845 else if (*s != '\0')
846 exitfileerror(20, "Unrecognized option\n");
849 if (optionnum == 0)
850 exitfileerror(20, "Unrecognized option\n");
851 switch (optionnum)
853 case 1: /* noautolib */
854 cfg->options |= OPTION_NOAUTOLIB;
855 break;
856 case 2: /* noexpunge */
857 cfg->options |= OPTION_NOEXPUNGE;
858 break;
859 case 3: /* noresident */
860 cfg->options |= OPTION_NORESIDENT;
861 cfg->firstlvo = 1;
862 break;
863 case 5: /* pertaskbase */
864 cfg->options |= OPTION_PERTASKBASE;
865 /* Fall through */
866 case 4: /* peropenerbase */
867 if (cfg->options & OPTION_DUPBASE)
868 exitfileerror(20, "Only one option peropenerbase or pertaskbase allowed\n");
869 cfg->options |= OPTION_DUPBASE;
870 break;
871 case 6: /* includes */
872 if (cfg->options & OPTION_NOINCLUDES)
873 exitfileerror(20, "option includes and noincludes are incompatible\n");
874 cfg->options |= OPTION_INCLUDES;
875 break;
876 case 7: /* noincludes */
877 if (cfg->options & OPTION_INCLUDES)
878 exitfileerror(20, "option includes and noincludes are incompatible\n");
879 cfg->options |= OPTION_NOINCLUDES;
880 break;
881 case 8: /* nostubs */
882 cfg->options |= OPTION_NOSTUBS;
883 break;
884 case 9: /* autoinit */
885 if (cfg->options & OPTION_NOAUTOINIT)
886 exitfileerror(20, "option autoinit and noautoinit are incompatible\n");
887 cfg->options |= OPTION_AUTOINIT;
888 break;
889 case 10: /* noautoinit */
890 if (cfg->options & OPTION_AUTOINIT)
891 exitfileerror(20, "option autoinit and noautoinit are incompatible\n");
892 cfg->options |= OPTION_NOAUTOINIT;
893 break;
894 case 11: /* resautoinit */
895 if (cfg->options & OPTION_SELFINIT)
896 exitfileerror(20, "option resautoinit and selfinit are incompatible\n");
897 cfg->options |= OPTION_RESAUTOINIT;
898 break;
899 case 12:
900 cfg->options |= OPTION_NOOPENCLOSE;
901 break;
902 case 13: /* noresautoinit */
903 if (cfg->options & OPTION_RESAUTOINIT)
904 exitfileerror(20, "option resautoinit and selfinit are incompatible\n");
905 cfg->options |= OPTION_SELFINIT;
906 break;
908 while (isspace(*s)) s++;
909 } while(*s !='\0');
911 else
913 static const char *optionnames[] =
915 "private"
917 const unsigned int optionnums = sizeof(optionnames)/sizeof(char *);
918 int optionnum;
922 for (i = 0, optionnum = 0; optionnum==0 && i<optionnums; i++)
924 if (strncmp(s, optionnames[i], strlen(optionnames[i]))==0)
926 optionnum = i + 1;
927 s += strlen(optionnames[i]);
928 while (isspace(*s)) s++;
929 if (*s == ',')
930 s++;
931 else if (*s != '\0')
932 exitfileerror(20, "Unrecognized option\n");
935 if (optionnum == 0)
936 exitfileerror(20, "Unrecognized option\n");
937 switch (optionnum)
939 case 1: /* private */
940 cl->options |= COPTION_PRIVATE;
941 break;
943 while (isspace(*s)) s++;
944 } while(*s !='\0');
946 break;
948 case 14: /* sysbase_field */
949 if (inclass)
950 exitfileerror(20, "sysbase_field not valid config option when in a class section\n");
951 cfg->sysbase_field = strdup(s);
952 break;
954 case 15: /* seglist_field */
955 if (inclass)
956 exitfileerror(20, "seglist_field not valid config option when in a class section\n");
957 cfg->seglist_field = strdup(s);
958 break;
960 case 16: /* rootbase_field */
961 if (inclass)
962 exitfileerror(20, "rootbase_field not valid config option when in a class section\n");
963 cfg->rootbase_field = strdup(s);
964 break;
966 case 17: /* classptr_field */
967 if (cl == NULL)
969 exitfileerror
972 "classptr_field specified when not a BOOPSI class\n"
975 cl->classptr_field = strdup(s);
976 break;
978 case 18: /* classptr_var */
979 if (cl == NULL)
981 exitfileerror
984 "classptr_var specified when not a BOOPSI class\n"
987 cl->classptr_var = strdup(s);
988 break;
990 case 19: /* classid */
991 if (cl == NULL)
992 exitfileerror(20, "classid specified when not a BOOPSI class\n");
993 if (cl->classid != NULL)
994 exitfileerror(20, "classid specified twice\n");
995 cl->classid = strdup(s);
996 if (strcmp(cl->classid, "NULL") == 0)
997 cl->options |= COPTION_PRIVATE;
998 break;
1000 case 20: /* classdatatype */
1001 if (cl == NULL)
1002 exitfileerror(20, "classdatatype specified when not a BOOPSI class\n");
1003 cl->classdatatype = strdup(s);
1004 break;
1006 case 21: /* beginio_func */
1007 if (inclass)
1008 exitfileerror(20, "beginio_func not valid config option when in a class section\n");
1009 if (cfg->modtype != DEVICE)
1010 exitfileerror(20, "beginio_func specified when not a device\n");
1011 cfg->beginiofunc = strdup(s);
1012 break;
1014 case 22: /* abortio_func */
1015 if (inclass)
1016 exitfileerror(20, "abortio_func not valid config option when in a class section\n");
1017 if (cfg->modtype != DEVICE)
1018 exitfileerror(20, "abortio_func specified when not a device\n");
1019 cfg->abortiofunc = strdup(s);
1020 break;
1022 case 23: /* dispatcher */
1023 if (cl == NULL)
1024 exitfileerror(20, "dispatcher specified when not a BOOPSI class\n");
1025 cl->dispatcher = strdup(s);
1026 /* function references are not needed when dispatcher is specified */
1027 cfg->intcfg |= CFG_NOREADFUNCS;
1028 break;
1030 case 24: /* initpri */
1031 if (cl != NULL)
1033 int count;
1034 char dummy;
1036 count = sscanf(s, "%d%c", &cl->initpri, &dummy);
1037 if (count != 1 ||
1038 cl->initpri < -128 || cl->initpri > 127
1041 exitfileerror(20, "initpri number format error\n");
1044 else
1045 exitfileerror(20, "initpri only valid config option for a BOOPSI class\n");
1046 break;
1048 case 25: /* type */
1049 if (!inclass)
1050 exitfileerror(20, "type only valid config option in a class section\n");
1051 if (strcmp(s,"mcc")==0)
1052 cl->classtype = MCC;
1053 else if (strcmp(s,"mui")==0)
1054 cl->classtype = MUI;
1055 else if (strcmp(s,"mcp")==0)
1056 cl->classtype = MCP;
1057 else if (strcmp(s, "image")==0)
1058 cl->classtype = IMAGE;
1059 else if (strcmp(s, "gadget")==0)
1060 cl->classtype = GADGET;
1061 else if (strcmp(s, "datatype")==0)
1062 cl->classtype = DATATYPE;
1063 else if (strcmp(s, "usbclass")==0)
1064 cl->classtype = USBCLASS;
1065 else if (strcmp(s, "class")==0)
1066 cl->classtype = CLASS;
1067 else if (strcmp(s, "hidd")==0)
1068 cl->classtype = HIDD;
1069 else
1071 fprintf(stderr, "Unknown type \"%s\" specified\n", s);
1072 exit(20);
1074 break;
1076 case 26: /* addromtag */
1077 cfg->addromtag = strdup(s);
1078 break;
1080 case 27: /* oopbase_field */
1081 cfg->oopbase_field = strdup(s);
1082 break;
1083 case 28: /* rellib */
1084 slist_append(&cfg->rellibs, s);
1085 break;
1086 case 29: /* interfaceid */
1087 if (!in)
1088 exitfileerror(20, "interfaceid only valid config option for an interface\n");
1089 in->interfaceid = strdup(s);
1090 break;
1091 case 30: /* interfacename */
1092 if (!in)
1093 exitfileerror(20, "interfacename only valid config option for an interface\n");
1094 in->interfacename = strdup(s);
1095 break;
1096 case 31: /* methodstub */
1097 if (!in)
1098 exitfileerror(20, "methodstub only valid config option for an interface\n");
1099 in->methodstub = strdup(s);
1100 break;
1101 case 32: /* methodbase */
1102 if (!in)
1103 exitfileerror(20, "methodbase only valid config option for an interface\n");
1104 in->methodbase = strdup(s);
1105 break;
1106 case 33: /* attributebase */
1107 if (!in)
1108 exitfileerror(20, "attributebase only valid config option for an interface\n");
1109 in->attributebase = strdup(s);
1110 break;
1111 case 34: /* handler_func */
1112 if (cfg->modtype != HANDLER)
1113 exitfileerror(20, "handler specified when not a handler\n");
1114 cfg->handlerfunc = strdup(s);
1115 break;
1118 else /* Line starts with ## */
1120 s = line+2;
1121 while (isspace(*s)) s++;
1122 if (strncmp(s, "end", 3)!=0)
1123 exitfileerror(20, "\"##end config\" expected\n");
1125 s += 3;
1126 if (!isspace(*s))
1127 exitfileerror(20, "\"##end config\" expected\n");
1129 while (isspace(*s)) s++;
1130 if (strncmp(s, "config", 6)!=0)
1131 exitfileerror(20, "\"##end config\" expected\n");
1133 s += 6;
1134 while (isspace(*s)) s++;
1135 if (*s!='\0')
1136 exitfileerror(20, "\"##end config\" expected\n");
1138 atend = 1;
1142 /* When not in a class section fill in default values for fields in cfg */
1143 if (!inclass)
1145 if (cfg->basename==NULL)
1147 cfg->basename = strdup(cfg->modulename);
1148 *cfg->basename = toupper(*cfg->basename);
1150 if (cfg->libbase==NULL)
1152 unsigned int len = strlen(cfg->basename)+5;
1153 cfg->libbase = malloc(len);
1154 snprintf(cfg->libbase, len, "%sBase", cfg->basename);
1156 if (cfg->libbasetype == NULL && libbasetypeextern != NULL)
1157 cfg->libbasetype = strdup(libbasetypeextern);
1158 if (cfg->sysbase_field != NULL && cfg->libbasetype == NULL)
1159 exitfileerror(20, "sysbase_field specified when no libbasetype is given\n");
1160 if (cfg->seglist_field != NULL && cfg->libbasetype == NULL)
1161 exitfileerror(20, "seglist_field specified when no libbasetype is given\n");
1162 if (cfg->oopbase_field != NULL && cfg->libbasetype == NULL)
1163 exitfileerror(20, "oopbase_field specified when no libbasetype is given\n");
1164 /* rootbase_field only allowed when duplicating base */
1165 if (cfg->rootbase_field != NULL && !(cfg->options & OPTION_DUPBASE))
1166 exitfileerror(20, "rootbasefield only valid for option peropenerbase or pertaskbase\n");
1168 /* Set default date to current date */
1169 if (cfg->datestring == NULL)
1171 char tmpbuf[256];
1172 time_t now = time(NULL);
1173 struct tm *ltime = localtime(&now);
1175 snprintf(tmpbuf, sizeof(tmpbuf), "%u.%u.%u",
1176 ltime->tm_mday, 1 + ltime->tm_mon, 1900 + ltime->tm_year);
1178 cfg->datestring = strdup(tmpbuf);
1181 if (cfg->copyright == NULL)
1182 cfg->copyright = "";
1184 if ( (cfg->beginiofunc != NULL && cfg->abortiofunc == NULL)
1185 || (cfg->beginiofunc == NULL && cfg->abortiofunc != NULL)
1187 exitfileerror(20, "please specify both beginio_func and abortio_func\n");
1189 if (libbasetypeextern==NULL)
1191 switch (cfg->modtype)
1193 case DEVICE:
1194 cfg->libbasetypeptrextern = "struct Device *";
1195 break;
1196 case HANDLER:
1197 case RESOURCE:
1198 cfg->libbasetypeptrextern = "APTR ";
1199 break;
1200 case LIBRARY:
1201 case MUI:
1202 case MCP:
1203 case MCC:
1204 case GADGET:
1205 case DATATYPE:
1206 case USBCLASS:
1207 case HIDD:
1208 cfg->libbasetypeptrextern = "struct Library *";
1209 break;
1210 default:
1211 fprintf(stderr, "Internal error: Unsupported modtype for libbasetypeptrextern\n");
1212 exit(20);
1215 else
1217 cfg->libbasetypeptrextern = malloc(strlen(libbasetypeextern)+3);
1218 strcpy(cfg->libbasetypeptrextern, libbasetypeextern);
1219 strcat(cfg->libbasetypeptrextern, " *");
1220 free(libbasetypeextern);
1224 /* When class was given too fill in some defaults when not specified */
1225 if (cl != NULL)
1227 if (cl->classtype == UNSPECIFIED)
1228 cl->classtype = CLASS;
1230 if (cl->basename == NULL)
1232 if (!inclass)
1233 cl->basename = cfg->basename;
1234 else
1235 exitfileerror(20, "basename has to be specified in the config section inside of a class section\n");
1238 /* MUI classes are always private */
1239 if (cl->classtype == MUI || cl->classtype == MCC || cl->classtype == MCP)
1240 cl->options |= COPTION_PRIVATE;
1242 if (cl->classid == NULL
1243 && (cl->classtype != MUI && cl->classtype != MCC && cl->classtype != MCP)
1246 if (cl->classtype == HIDD)
1248 cl->options &= !COPTION_PRIVATE;
1250 else if (cl->options & COPTION_PRIVATE)
1252 cl->classid = "NULL";
1254 else
1256 char s[256] = "";
1258 if (cl->classtype == GADGET || cl->classtype == IMAGE || cl->classtype == CLASS || cl->classtype == USBCLASS)
1260 sprintf(s, "\"%sclass\"", inclass ? cl->basename : cfg->modulename);
1262 else if (cl->classtype == DATATYPE)
1264 sprintf(s, "\"%s.datatype\"", inclass ? cl->basename : cfg->modulename);
1266 cl->classid = strdup(s);
1270 /* Only specify superclass or superclass_field */
1271 if (cl->superclass != NULL && cl->superclass_field != NULL)
1272 exitfileerror(20, "Only specify one of superclass or superclass_field in config section\n");
1274 /* Give default value to superclass if it is not specified */
1275 if (cl->superclass == NULL && cl->superclass == NULL)
1277 switch (cl->classtype)
1279 case MUI:
1280 case MCC:
1281 cl->superclass = "MUIC_Area";
1282 break;
1283 case MCP:
1284 cl->superclass = "MUIC_Mccprefs";
1285 break;
1286 case IMAGE:
1287 cl->superclass = "IMAGECLASS";
1288 break;
1289 case GADGET:
1290 cl->superclass = "GADGETCLASS";
1291 break;
1292 case DATATYPE:
1293 cl->superclass = "DATATYPESCLASS";
1294 break;
1295 case CLASS:
1296 cl->superclass = "ROOTCLASS";
1297 break;
1298 case HIDD:
1299 cl->superclass = "CLID_Root";
1300 break;
1301 default:
1302 exitfileerror(20, "Internal error: unhandled classtype in readsectionconfig\n");
1303 break;
1309 static void readsectioncdef(struct config *cfg)
1311 int atend = 0;
1312 char *line, *s;
1314 while (!atend)
1316 line = readline();
1317 if (line==NULL)
1318 exitfileerror(20, "unexptected end of file in section cdef\n");
1320 if (strncmp(line, "##", 2)!=0)
1322 slist_append(&cfg->cdeflines, line);
1324 else
1326 s = line+2;
1327 while (isspace(*s)) s++;
1328 if (strncmp(s, "end", 3)!=0)
1329 exitfileerror(20, "\"##end cdef\" expected\n");
1331 s += 3;
1332 while (isspace(*s)) s++;
1333 if (strncmp(s, "cdef", 4)!=0)
1334 exitfileerror(20, "\"##end cdef\" expected\n");
1336 s += 5;
1337 while (isspace(*s)) s++;
1338 if (*s!='\0')
1339 exitfileerror(20, "unexpected character at position %d\n");
1341 atend = 1;
1346 static void readsectioncdefprivate(struct config *cfg)
1348 int atend = 0;
1349 char *line, *s;
1351 while (!atend)
1353 line = readline();
1354 if (line==NULL)
1355 exitfileerror(20, "unexptected end of file in section cdef\n");
1357 if (strncmp(line, "##", 2)!=0)
1359 slist_append(&cfg->cdefprivatelines, line);
1361 else
1363 s = line+2;
1364 while (isspace(*s)) s++;
1365 if (strncmp(s, "end", 3)!=0)
1366 exitfileerror(20, "\"##end cdefprivate\" expected\n");
1368 s += 3;
1369 while (isspace(*s)) s++;
1370 if (strncmp(s, "cdefprivate", 11)!=0)
1371 exitfileerror(20, "\"##end cdefprivate\" expected\n");
1373 s += 11;
1374 while (isspace(*s)) s++;
1375 if (*s!='\0')
1376 exitfileerror(20, "unexpected character at position %d\n");
1378 atend = 1;
1383 static void readsectionstartup(struct config *cfg)
1385 int atend = 0;
1386 char *line, *s;
1388 while (!atend)
1390 line = readline();
1391 if (line==NULL)
1392 exitfileerror(20, "unexptected end of file in section startup\n");
1394 if (strncmp(line, "##", 2)!=0)
1396 slist_append(&cfg->startuplines, line);
1398 else
1400 s = line+2;
1401 while (isspace(*s)) s++;
1402 if (strncmp(s, "end", 3)!=0)
1403 exitfileerror(20, "\"##end startup\" expected\n");
1405 s += 3;
1406 while (isspace(*s)) s++;
1407 if (strncmp(s, "startup", 7)!=0)
1408 exitfileerror(20, "\"##end startup\" expected\n");
1410 s += 7;
1411 while (isspace(*s)) s++;
1412 if (*s!='\0')
1413 exitfileerror(20, "unexpected character at position %d\n");
1415 atend = 1;
1420 static void readsectionfunctionlist(const char *type, struct functionhead **funclistptr, unsigned int firstlvo, int isattribute)
1422 int atend = 0, i;
1423 char *line, *s, *s2;
1424 unsigned int lvo = firstlvo;
1425 int minversion = 0;
1427 while (!atend)
1429 line = readline();
1430 if (line==NULL)
1431 exitfileerror(20, "unexpected EOF in functionlist section\n");
1432 if (strlen(line)==0)
1434 if (*funclistptr != NULL)
1435 funclistptr = &((*funclistptr)->next);
1436 lvo++;
1438 else if (isspace(*line))
1440 s = line;
1441 while (isspace(*s)) s++;
1442 if (*s=='\0')
1444 if (*funclistptr != NULL)
1445 funclistptr = &((*funclistptr)->next);
1446 lvo++;
1448 else
1449 exitfileerror(20, "no space allowed before functionname\n");
1451 else if (strncmp(line, "##", 2)==0)
1453 s = line+2;
1454 while (isspace(*s)) s++;
1455 if (strncmp(s, "end", 3)!=0)
1456 exitfileerror(20, "\"##end %s\" expected\n", type);
1458 s += 3;
1459 while (isspace(*s)) s++;
1460 if (strncmp(s, type, strlen(type))!=0)
1461 exitfileerror(20, "\"##end %s\" expected\n", type);
1463 s += strlen(type);
1464 while (isspace(*s)) s++;
1465 if (*s!='\0')
1466 exitfileerror(20, "unexpected character on position %d\n", s-line);
1468 atend = 1;
1470 else if (*line=='.')
1472 s = line+1;
1473 if (strncmp(s, "skip", 4)==0)
1475 int n;
1477 s += 4;
1478 if (!isspace(*s))
1479 exitfileerror(20, "syntax is '.skip n'\n");
1481 n=strtol(s, &s2, 10);
1482 if (s2==NULL)
1483 exitfileerror(20, "positive number expected\n");
1485 while (isspace(*s2)) s2++;
1486 if ((*s2 != '\0') && (*s2 != '#'))
1487 exitfileerror(20, "syntax is '.skip n'\n");
1488 if (*funclistptr != NULL)
1489 funclistptr = &((*funclistptr)->next);
1490 lvo += n;
1492 else if (strncmp(s, "alias", 5)==0)
1494 s += 5;
1496 if (!isspace(*s))
1497 exitfileerror(20, "syntax is '.alias name'\n");
1499 while (isspace(*s)) s++;
1500 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1501 exitfileerror(20, "syntax is '.alias name'\n");
1503 s2 = s;
1504 s++;
1505 while (isalnum(*s) || *s == '_') s++;
1507 if (isspace(*s))
1509 *s = '\0';
1510 do {
1511 s++;
1512 } while (isspace(*s));
1515 if (*s != '\0')
1516 exitfileerror(20, "syntax is '.alias name'\n");
1518 if (*funclistptr == NULL)
1519 exitfileerror(20, ".alias has to come after a function declaration\n");
1521 slist_append(&(*funclistptr)->aliases, s2);
1523 else if (strncmp(s, "function", 8) == 0)
1525 s += 8;
1527 if (!isspace(*s))
1528 exitfileerror(20, "Syntax error\n");
1530 while (isspace(*s)) s++;
1531 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1532 exitfileerror(20, "syntax is '.function name'\n");
1534 s2 = s;
1535 s++;
1536 while (isalnum(*s) || *s == '_') s++;
1538 if (isspace(*s))
1540 *s = '\0';
1541 do {
1542 s++;
1543 } while (isspace(*s));
1546 if (*s != '\0')
1547 exitfileerror(20, "syntax is '.function name'\n");
1549 if (*funclistptr == NULL)
1550 exitfileerror(20, ".function has to come after a function declaration\n");
1552 funcsetinternalname(*funclistptr, s2);
1554 else if (strncmp(s, "cfunction", 9)==0)
1556 if (*funclistptr == NULL)
1557 exitfileerror(20, ".cfunction has to come after a function declaration\n");
1559 (*funclistptr)->libcall = REGISTER;
1561 else if (strncmp(s, "private", 7)==0)
1563 if (*funclistptr == NULL)
1564 exitfileerror(20, ".private has to come after a function declaration\n");
1566 (*funclistptr)->priv = 1;
1568 else if (strncmp(s, "novararg", 8)==0)
1570 if (*funclistptr == NULL)
1571 exitfileerror(20, ".novararg has to come after a function declaration\n");
1573 (*funclistptr)->novararg = 1;
1575 else if (strncmp(s, "version", 7) == 0)
1577 /* Mark version number for the following
1578 * functions, so that the automatic OpenLibrary()
1579 * will know what version to use.
1581 char *tmp;
1582 int ver;
1584 s += 7;
1586 while (isspace(*s)) s++;
1587 ver = (int)strtol(s, &tmp, 0);
1589 if (s == tmp)
1590 exitfileerror(20, ".version expects an integer\n");
1592 s = tmp;
1593 while (isspace(*s)) s++;
1595 if (*s && *s != '#')
1596 exitfileerror(20, ".version has junk after the version number\n");
1598 minversion = ver;
1600 else if (strncmp(s, "unusedlibbase", 13) == 0)
1602 if (*funclistptr == NULL)
1603 exitfileerror(20, ".unusedlibbase has to come after a function declaration\n");
1604 (*funclistptr)->unusedlibbase = 1;
1606 else
1607 exitfileerror(20, "Syntax error");
1609 else if (*line!='#') /* Ignore line that is a comment, e.g. that starts with a # */
1611 /* The line is a function or attribute prototype.
1612 * A function can have one of two syntaxes:
1613 * type funcname(argproto1, argproto2, ...)
1614 * type funcname(argproto1, argproto2, ...) (reg1, reg2, ...)
1615 * The former is for C type function argument passing, the latter for
1616 * register argument passing.
1617 * An attribute has the following syntax:
1618 * type attribute
1620 char c, *args[64], *regs[64], *funcname, *cp;
1621 int len, argcount = 0, regcount = 0, brcount = 0;
1623 cp = strchr(line,'#');
1624 if (cp)
1625 *(cp++) = 0;
1627 /* Parse 'type functionname' at the beginning of the line */
1628 if (isattribute) {
1629 s = line + strlen(line);
1630 } else {
1631 s = strchr(line, '(');
1632 if (s == NULL)
1633 exitfileerror(20, "( expected at position %d\n", strlen(line) + 1);
1636 s2 = s;
1637 while (isspace(*(s2-1)))
1638 s2--;
1639 *s2 = '\0';
1641 while (s2 > line && !isspace(*(s2-1)) && !(*(s2-1) == '*'))
1642 s2--;
1644 if (s2 == line)
1645 exitfileerror(20, "No type specifier before %s name\n", isattribute ? "attribute" : "function");
1647 if (*funclistptr != NULL)
1648 funclistptr = &((*funclistptr)->next);
1649 *funclistptr = newfunctionhead(s2, STACK);
1651 if (cp)
1652 (*funclistptr)->comment = strdup(cp);
1653 else
1654 (*funclistptr)->comment = NULL;
1656 while (isspace(*(s2-1)))
1657 s2--;
1658 *s2 = '\0';
1659 (*funclistptr)->type = strdup(line);
1660 (*funclistptr)->lvo = lvo;
1661 (*funclistptr)->version = minversion;
1662 lvo++;
1664 if (isattribute)
1665 continue;
1667 /* Parse function prototype */
1668 s++;
1669 while (isspace(*s))
1670 s++;
1671 c = *s;
1673 while (c != ')')
1675 while (isspace(*s))
1676 s++;
1678 args[argcount] = s;
1679 argcount++;
1681 while
1683 *s != '\0'
1684 && !(brcount == 0 && (*s == ',' || *s == ')'))
1687 if (*s == '(')
1688 brcount++;
1689 if (*s == ')')
1691 if (brcount > 0)
1692 brcount--;
1693 else
1694 exitfileerror(20, "Unexected ')' at position %d\n", s-line+1);
1696 s++;
1699 c = *s;
1700 if (c == '\0')
1701 exitfileerror(20, "'(' without ')'");
1703 s2 = s;
1704 while (isspace(*(s2-1)))
1705 s2--;
1706 *s2 = '\0';
1708 if (!(s2 > args[argcount - 1]))
1709 exitfileerror(20, "Syntax error in function prototype\n");
1711 s++;
1714 s++;
1715 while (*s != '\0' && isspace(*s))
1716 s++;
1718 if (*s == '(')
1720 /* Parse registers specifications if available otherwise this prototype for C type argument passing */
1722 /* There may be no register specified with () so be sure then c is == ')' */
1723 s++;
1724 while(isspace(*s))
1725 s++;
1727 c = *s;
1729 while (c != ')')
1731 while (isspace(*s))
1732 s++;
1734 regs[regcount] = s;
1735 regcount++;
1737 if (memchr("AD",s[0],2)!=NULL && memchr("01234567",s[1],8)!=NULL)
1739 s += 2;
1740 c = *s;
1741 if (c == '/')
1743 s++;
1744 if (s[0] == s[-3] && s[1] == s[-2] + 1)
1746 s += 2;
1747 c = *s;
1749 else
1750 exitfileerror(20,
1751 "wrong register specification \"%s\" for argument %u\n",
1752 regs[regcount-1], regcount
1755 if (regcount > 4)
1756 exitfileerror(20, "maximum four arguments passed in two registers allowed (%d, %s) \n", regcount, regs[regcount-1]);
1758 *s = '\0';
1760 else
1761 exitfileerror(20,
1762 "wrong register \"%s\" for argument %u\n",
1763 regs[regcount-1], regcount
1766 while (isspace(c))
1768 s++;
1769 c = *s;
1771 if (c == '\0')
1772 exitfileerror(20, "'(' without ')'\n");
1773 if (c != ',' && c != ')')
1774 exitfileerror(20, "',' or ')' expected at position %d\n", s-line+1);
1776 s++;
1779 s++;
1780 while (isspace(*s)) s++;
1781 if (*s!='\0')
1782 exitfileerror(20, "wrong char '%c' at position %d\n", *s, (int)(s-line) + 1);
1784 if (argcount != regcount)
1785 exitfileerror(20, "Number of arguments (%d) and registers (%d) mismatch\n",
1786 argcount, regcount
1789 (*funclistptr)->libcall = REGISTERMACRO;
1790 for (i = 0; i < argcount; i++)
1791 funcaddarg(*funclistptr, args[i], regs[i]);
1793 else if (*s == '\0')
1794 { /* No registers specified */
1795 for (i = 0; i < argcount; i++)
1796 funcaddarg(*funclistptr, args[i], NULL);
1798 else
1799 exitfileerror(20, "wrong char '%c' at position %d\n", *s, (int)(s-line) + 1);
1804 static void readsectionclass_methodlist(struct classinfo *cl)
1806 int atend = 0, i;
1807 char *line, *s, *s2;
1808 struct functionhead **methlistptr = &cl->methlist;
1809 struct stringlist *interface = NULL;
1811 if (cl->basename==NULL)
1812 exitfileerror(20, "section methodlist has to come after section config\n");
1814 while (!atend)
1816 line = readline();
1817 if (line==NULL)
1818 exitfileerror(20, "unexptected EOF in methodlist section\n");
1820 /* Ignore empty lines or lines that qre a comment, e.g. that starts with a # */
1821 if (strlen(line)==0 || (line[0] == '#' && line[1] != '#'))
1822 continue;
1824 if (isspace(*line))
1825 exitfileerror(20, "No space allowed at start of the line\n");
1827 if (strncmp(line, "##", 2)==0) /* Is this the end ? */
1829 s = line+2;
1830 while (isspace(*s)) s++;
1831 if (strncmp(s, "end", 3)!=0)
1832 exitfileerror(20, "\"##end methodlist\" expected\n");
1834 s += 3;
1835 while (isspace(*s)) s++;
1836 if (strncmp(s, "methodlist", 10)!=0)
1837 exitfileerror(20, "\"##end methodlist\" expected\n");
1839 s += 10;
1840 while (isspace(*s)) s++;
1841 if (*s!='\0')
1842 exitfileerror(20, "unexpected character on position %d\n", s-line);
1844 atend = 1;
1846 continue;
1849 if (*line=='.')
1851 s = line+1;
1852 if (strncmp(s, "alias", 5)==0)
1854 s += 5;
1856 if (!isspace(*s))
1857 exitfileerror(20, "syntax is '.alias name'\n");
1859 while (isspace(*s)) s++;
1860 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1861 exitfileerror(20, "syntax is '.alias name'\n");
1863 s2 = s;
1864 s++;
1865 while (isalnum(*s) || *s == '_') s++;
1867 if (isspace(*s))
1869 *s = '\0';
1870 do {
1871 s++;
1872 } while (isspace(*s));
1875 if (*s != '\0')
1876 exitfileerror(20, "syntax is '.alias name'\n");
1878 if (*methlistptr == NULL)
1879 exitfileerror(20, ".alias has to come after a function declaration\n");
1881 slist_append(&(*methlistptr)->aliases, s2);
1883 else if (strncmp(s, "function", 8) == 0)
1885 s += 8;
1887 if (!isspace(*s))
1888 exitfileerror(20, "Syntax error\n");
1890 while (isspace(*s)) s++;
1891 if (*s == '\0' || !(isalpha(*s) || *s == '_'))
1892 exitfileerror(20, "syntax is '.function name'\n");
1894 s2 = s;
1895 s++;
1896 while (isalnum(*s) || *s == '_') s++;
1898 if (isspace(*s))
1900 *s = '\0';
1901 do {
1902 s++;
1903 } while (isspace(*s));
1906 if (*s != '\0')
1907 exitfileerror(20, "syntax is '.function name'\n");
1909 if (*methlistptr == NULL)
1910 exitfileerror(20, ".function has to come after a function declaration\n");
1912 funcsetinternalname(*methlistptr, s2);
1914 else if (strncmp(s, "interface", 9) == 0)
1916 if (cl->classtype != HIDD)
1917 exitfileerror(20, "interface only valid for a HIDD\n");
1919 s += 9;
1921 if (!isspace(*s))
1922 exitfileerror(20, "Syntax error\n");
1924 while (isspace(*s)) s++;
1925 if (*s == '\0' || !isalpha(*s))
1926 exitfileerror(20, "syntax is '.interface name'\n");
1928 s2 = s;
1929 s++;
1930 while (isalnum(*s) || *s == '_') s++;
1932 if (isspace(*s))
1934 *s = '\0';
1935 do {
1936 s++;
1937 } while (isspace(*s));
1940 if (*s != '\0')
1941 exitfileerror(20, "syntax is '.interface name'\n");
1943 interface = slist_append(&cl->interfaces, s2);
1945 else
1946 exitfileerror(20, "Syntax error");
1948 else if (isalpha(*line))
1950 char stmp[256];
1952 for (s = line + 1; isalnum(*s) || *s == '_'; s++)
1955 if (cl->classtype == HIDD && interface == NULL)
1956 exitfileerror(20, "For a HIDD the first method has to come after an .interface line\n");
1958 if (*s != '\0')
1959 exitfileerror(20, "Only letters, digits and an underscore allowed in a methodname\n");
1961 if (*methlistptr != NULL)
1962 methlistptr = &((*methlistptr)->next);
1963 if (cl->classtype != HIDD)
1965 if (snprintf(stmp, 256, "%s__%s", cl->basename, line) >= 256)
1966 exitfileerror(20, "Method name too large\n");
1968 *methlistptr = newfunctionhead(stmp, STACK);
1969 (*methlistptr)->type = "IPTR";
1970 funcaddarg(*methlistptr, "Class *cl", NULL);
1971 funcaddarg(*methlistptr, "Object *o", NULL);
1972 funcaddarg(*methlistptr, "Msg msg", NULL);
1974 else
1976 if (snprintf(stmp, 256, "%s__%s__%s", cl->basename, interface->s, line) >= 256)
1977 exitfileerror(20, "Method name too large\n");
1979 *methlistptr = newfunctionhead(stmp, STACK);
1980 (*methlistptr)->type = "IPTR";
1981 funcaddarg(*methlistptr, "OOP_Class *cl", NULL);
1982 funcaddarg(*methlistptr, "OOP_Object *o", NULL);
1983 funcaddarg(*methlistptr, "OOP_Msg msg", NULL);
1984 (*methlistptr)->interface = interface;
1985 if (snprintf(stmp, 256, "mo%s_%s", interface->s, line) >= 256)
1986 exitfileerror(20, "Method name too large\n");
1987 (*methlistptr)->method = strdup(stmp);
1989 slist_append(&(*methlistptr)->aliases, line);
1991 else
1992 exitfileerror(20, "Methodname has to begin with a letter\n");
1996 static void
1997 readsectioninterface(struct config *cfg)
1999 char *s;
2000 struct interfaceinfo *in;
2002 in = newinterface(cfg);
2003 s = readsections(cfg, NULL, in, 1);
2004 if (s == NULL)
2005 exitfileerror(20, "Unexpected end of file\n");
2007 if (strncmp(s, "##", 2) != 0)
2008 exitfileerror(20, "'##end interface' expected\n");
2009 s += 2;
2011 while (isspace(*s)) s++;
2013 if (strncmp(s, "end", 3) != 0)
2014 exitfileerror(20, "'##end interface' expected\n");
2015 s += 3;
2017 if (!isspace(*s))
2018 exitfileerror(20, "'##end interface' expected\n");
2019 while (isspace(*s)) s++;
2021 if (strncmp(s, "interface", 9) != 0)
2022 exitfileerror(20, "'##end interface' expected\n");
2023 s += 9;
2025 while (isspace(*s)) s++;
2026 if (*s != '\0')
2027 exitfileerror(20, "'##end interface' expected\n");
2029 if (!in->interfaceid)
2030 exitfileerror(20, "interface has no 'interfaceid' defined!\n");
2032 if (!in->interfacename)
2033 exitfileerror(20, "interface has no 'interfacename' defined!\n");
2035 if (!in->methodstub)
2036 in->methodstub = strdup(in->interfacename);
2038 if (!in->methodbase) {
2039 int len = strlen(in->interfacename);
2040 in->methodbase = malloc(len + 4 + 1);
2041 strcpy(in->methodbase, in->interfacename);
2042 strcat(in->methodbase, "Base");
2045 if (!in->attributebase) {
2046 int len = strlen(in->interfacename);
2047 in->attributebase = malloc(len + 4 + 4 + 1);
2048 strcpy(in->attributebase, in->interfacename);
2049 strcat(in->attributebase, "AttrBase");
2053 static void
2054 readsectionclass(struct config *cfg)
2056 char *s;
2057 struct classinfo *cl;
2059 cl = newclass(cfg);
2060 s = readsections(cfg, cl, NULL, 1);
2061 if (s == NULL)
2062 exitfileerror(20, "Unexpected end of file\n");
2064 if (strncmp(s, "##", 2) != 0)
2065 exitfileerror(20, "'##end class' expected\n");
2066 s += 2;
2068 while (isspace(*s)) s++;
2070 if (strncmp(s, "end", 3) != 0)
2071 exitfileerror(20, "'##end class' expected\n");
2072 s += 3;
2074 if (!isspace(*s))
2075 exitfileerror(20, "'##end class' expected\n");
2076 while (isspace(*s)) s++;
2078 if (strncmp(s, "class", 5) != 0)
2079 exitfileerror(20, "'##end class' expected\n");
2080 s += 5;
2082 while (isspace(*s)) s++;
2083 if (*s != '\0')
2084 exitfileerror(20, "'##end class' expected\n");
2087 static struct classinfo *newclass(struct config *cfg)
2089 struct classinfo *cl, *classlistit;
2091 cl = malloc(sizeof(struct classinfo));
2092 if (cl == NULL)
2094 fprintf(stderr, "Out of memory\n");
2095 exit(20);
2097 memset(cl, 0, sizeof(struct classinfo));
2099 /* By default the classes are initialized with a priority of 1 so they
2100 * are initialized before any user added initialization with priority 1
2102 cl->initpri = 1;
2104 if (cfg->classlist == NULL)
2105 cfg->classlist = cl;
2106 else
2110 classlistit = cfg->classlist;
2111 classlistit->next != NULL;
2112 classlistit = classlistit->next
2115 classlistit->next = cl;
2118 return cl;
2121 static struct handlerinfo *newhandler(struct config *cfg)
2123 struct handlerinfo *hl;
2125 hl = calloc(1,sizeof(*hl));
2126 hl->next = cfg->handlerlist;
2127 cfg->handlerlist = hl;
2128 return hl;
2131 static struct interfaceinfo *newinterface(struct config *cfg)
2133 struct interfaceinfo *in, *interfacelistit;
2135 in = malloc(sizeof(struct interfaceinfo));
2136 if (in == NULL)
2138 fprintf(stderr, "Out of memory\n");
2139 exit(20);
2141 memset(in, 0, sizeof(struct interfaceinfo));
2143 if (cfg->interfacelist == NULL)
2144 cfg->interfacelist = in;
2145 else
2149 interfacelistit = cfg->interfacelist;
2150 interfacelistit->next != NULL;
2151 interfacelistit = interfacelistit->next
2154 interfacelistit->next = in;
2157 return in;
2161 static int getdirective(char *s, const char *directive, int range_min, int range_max, int *val)
2163 char *tmp;
2164 int newval;
2166 if (strncmp(s, directive, strlen(directive)) != 0)
2167 return 0;
2169 s += strlen(directive);
2170 if (*s && !isspace(*s))
2171 exitfileerror(20, "Unrecognized directive \".%s\"\n", directive);
2173 while (isspace(*s)) s++;
2174 if (!*s)
2175 exitfileerror(20, "No .%s value specified\n", directive);
2177 newval = strtol(s, &tmp, 0);
2178 if (s == tmp || !(newval >= range_min && newval <= range_max)) {
2179 tmp = s;
2180 while (*tmp && !isspace(*tmp)) tmp++;
2181 exitfileerror(20, "Invalid .%s value of %.*s\n", directive, tmp - s, s);
2184 *val = newval;
2185 return 1;
2188 static void
2189 readsectionhandler(struct config *cfg)
2191 char *line = NULL, *s;
2192 struct handlerinfo *hl;
2193 unsigned char autolevel = 0;
2194 unsigned int stacksize = 0;
2195 int startup = 0;
2196 char priority = 10;
2197 int bootpri = -128;
2198 int has_filesystem = 0;
2200 for (;;)
2202 char *function;
2203 int function_len;
2204 char *tmp;
2206 s = line = readline();
2208 if (s==NULL)
2209 exitfileerror(20, "unexpected end of file in section hanlder\n");
2211 if (strncmp(s, "##", 2)==0)
2212 break;
2214 /* Ignore comments */
2215 if (strncmp(s, "#", 1)==0)
2216 continue;
2218 /* Skip ahead to function name */
2219 while (*s && isspace(*s)) s++;
2221 /* Permit blank lines */
2222 if (!*s)
2223 continue;
2225 if (*s == '.') {
2226 int val;
2227 s++;
2229 if (getdirective(s, "autodetect", 0, 127, &val)) {
2230 autolevel = val;
2231 } else if (getdirective(s, "stacksize", 0, INT_MAX, &val)) {
2232 stacksize = val;
2233 } else if (getdirective(s, "priority", -128, 127, &val)) {
2234 priority = val;
2235 } else if (getdirective(s, "bootpri", -128, 127, &val)) {
2236 bootpri = val;
2237 } else if (getdirective(s, "startup", INT_MIN, INT_MAX, &val)) {
2238 startup = val;
2239 } else {
2240 exitfileerror(20, "Unrecognized directive \"%s\"\n", line);
2242 continue;
2245 do {
2246 unsigned int id = 0;
2248 if (strncasecmp(s,"resident=",9)==0) {
2249 char *res;
2251 s = strchr(s, '=') + 1;
2252 res = s;
2253 while (*s && !isspace(*s)) s++;
2254 if (res == s)
2255 exitfileerror(20, "Empty resident= is not permitted\n");
2257 if (*s)
2258 *(s++) = 0;
2260 hl = newhandler(cfg);
2261 hl->type = HANDLER_RESIDENT;
2262 hl->id = 0;
2263 hl->name = strdup(res);
2264 hl->autodetect = autolevel--;
2265 hl->stacksize = stacksize;
2266 hl->priority = priority;
2267 hl->startup = startup;
2268 } else if (strncasecmp(s,"dosnode=",8)==0) {
2269 char *dev;
2271 s = strchr(s, '=') + 1;
2272 dev = s;
2273 while (*s && !isspace(*s)) s++;
2274 if (dev == s)
2275 exitfileerror(20, "Empty dosnode= is not permitted\n");
2277 if (*s)
2278 *(s++) = 0;
2280 hl = newhandler(cfg);
2281 hl->type = HANDLER_DOSNODE;
2282 hl->id = 0;
2283 hl->name = strdup(dev);
2284 hl->autodetect = autolevel ? autolevel-- : 0;
2285 hl->stacksize = stacksize;
2286 hl->priority = priority;
2287 hl->startup = startup;
2288 hl->bootpri = bootpri;
2289 } else if (strncasecmp(s,"dostype=",8) == 0) {
2290 s = strchr(s, '=') + 1;
2292 id = (unsigned int)strtoul(s, &tmp, 0);
2294 if (s == tmp) {
2295 while (*tmp && !isspace(*tmp))
2296 tmp++;
2297 exitfileerror(20, "\"%.*s\" is not a numerical DOS ID\n", (tmp -s), s);
2299 s = tmp;
2301 if (id == 0 || id == ~0) {
2302 exitfileerror(20, "DOS ID 0x%08x is not permitted\n", id);
2305 hl = newhandler(cfg);
2306 hl->type = HANDLER_DOSTYPE;
2307 hl->id = id;
2308 hl->name = NULL;
2309 hl->autodetect = autolevel ? autolevel-- : 0;
2310 hl->stacksize = stacksize;
2311 hl->priority = priority;
2312 hl->startup = startup;
2313 } else {
2314 for (tmp = s; !isspace(*tmp); tmp++);
2315 exitfileerror(20, "Unknown option \"%.*s\"\n", tmp - s, s);
2318 /* Advance to next ID */
2319 while (*s && isspace(*s)) s++;
2321 } while (*s);
2324 if (s == NULL)
2325 exitfileerror(20, "Unexpected end of file\n");
2327 if (strncmp(s, "##", 2) != 0)
2328 exitfileerror(20, "'##end handler' expected\n");
2329 s += 2;
2331 while (isspace(*s)) s++;
2333 if (strncmp(s, "end", 3) != 0)
2334 exitfileerror(20, "'##end handler' expected\n");
2335 s += 3;
2337 while (isspace(*s)) s++;
2339 if (strncmp(s, "handler", 7) != 0)
2340 exitfileerror(20, "'##end handler' expected\n");
2341 s += 7;
2343 while (isspace(*s)) s++;
2344 if (*s != '\0')
2345 exitfileerror(20, "'##end handler' expected\n");