Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /***********************************************************************
  2.  *
  3.  *  avra - Assembler for the Atmel AVR microcontroller series
  4.  *
  5.  *  Copyright (C) 1998-2004 Jon Anders Haugum, Tobias Weber
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; see the file COPYING.  If not, write to
  19.  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  *  Boston, MA 02111-1307, USA.
  21.  *
  22.  *
  23.  *  Authors of avra can be reached at:
  24.  *     email: jonah@omegav.ntnu.no, tobiw@suprafluid.com
  25.  *     www: http://sourceforge.net/projects/avra
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include "misc.h"
  33. #include "args.h"
  34.  
  35.  
  36. struct args *alloc_args(int arg_count)
  37. {
  38.         struct args *args;
  39.  
  40.         args = malloc(sizeof(struct args));
  41.         if(args) {
  42.                 args->arg = malloc(sizeof(struct arg) * arg_count);
  43.                 if(args->arg) {
  44.                         args->count = arg_count;
  45.                         args->first_data = NULL;
  46.                         return(args);
  47.                 }
  48.                 free(args);
  49.         }
  50.         printf("Error: Unable to allocate memory\n");
  51.         return(NULL);
  52. }
  53.  
  54.  
  55. int read_args(struct args *args, int argc, char *argv[])
  56. {
  57.         int i, j, k, ok, i_old;
  58.         struct data_list **last_data;
  59.         /*** init ***/
  60.         ok = True;
  61.         args->first_data = NULL;
  62.         /*** end of init ***/
  63.  
  64.         last_data = &args->first_data;
  65.  
  66.         for(i = 1; (i < argc) && ok; i++) {
  67.                 if(argv[i][0] == '-') {
  68.                         last_data = &args->first_data;
  69.                         if(argv[i][1] == 0) {
  70.                                 printf("Error: Unknown option: -\n");
  71.                                 ok = False;
  72.                         } else
  73.                                 if(argv[i][1] == '-') {
  74.                                         j = 0;
  75.                                         while((j != args->count) && strcmp(&argv[i][2], args->arg[j].longarg)) {
  76.                                                 j++;
  77.                                         }
  78.                                         if(j == args->count) {
  79.                                                 printf("Error: Unknown option: %s\n", argv[i]);
  80.                                                 ok = False;
  81.                                         } else {
  82.                                                 switch(args->arg[j].type) {
  83.                                                         case ARGTYPE_STRING:
  84.                                                         case ARGTYPE_STRING_MULTISINGLE:
  85.                                                                 /* if argument is a string parameter we will do this: */
  86.                                                                 if((i + 1) == argc) {
  87.                                                                         printf("Error: No argument supplied with option: %s\n", argv[i]);
  88.                                                                         ok = False;
  89.                                                                 } else
  90.                                                                         if(args->arg[j].type != ARGTYPE_STRING_MULTISINGLE)
  91.                                                                                 args->arg[j].data = argv[++i];
  92.                                                                         else
  93.                                                                                 ok = add_arg((struct data_list **)&args->arg[j].data, argv[++i]);
  94.                                                                 break;
  95.                                                         case ARGTYPE_BOOLEAN:
  96.                                                                 args->arg[j].data = (char *)True;
  97.                                                                 break;
  98.                                                         case ARGTYPE_STRING_MULTI:
  99.                                                                 last_data = (struct data_list **)&args->arg[j].data;
  100.                                                                 break;
  101.                                                 }
  102.                                         }
  103.                                 } else {
  104.                                         for(k = 1, i_old = i; (argv[i][k] != '\0') && ok && (i == i_old); k++) {
  105.                                                 j = 0;
  106.                                                 while((j != args->count) && (argv[i][k] != args->arg[j].letter))
  107.                                                         j++;
  108.                                                 if(j == args->count) {
  109.                                                         printf("Error: Unknown option: -%c\n", argv[i][k]);
  110.                                                         ok = False;
  111.                                                 } else {
  112.                                                         switch(args->arg[j].type) {
  113.                                                                 case ARGTYPE_STRING:
  114.                                                                 case ARGTYPE_STRING_MULTISINGLE:
  115.                                                                         if(argv[i][k + 1] != '\0') {
  116.                                                                                 printf("Error: Option -%c must be followed by it's argument\n", argv[i][k]);
  117.                                                                                 ok = False;
  118.                                                                         } else {
  119.                                                                                 if((i + 1) == argc) {
  120.                                                                                         printf("Error: No argument supplied with option: -%c\n", argv[i][k]);
  121.                                                                                         ok = False;
  122.                                                                                 } else
  123.                                                                                         if(args->arg[j].type != ARGTYPE_STRING_MULTISINGLE)
  124.                                                                                                 args->arg[j].data = argv[++i];
  125.                                                                                         else
  126.                                                                                                 ok = add_arg((struct data_list **)&args->arg[j].data, argv[++i]);
  127.                                                                         }
  128.                                                                         break;
  129.                                                                 case ARGTYPE_BOOLEAN:
  130.                                                                         args->arg[j].data = (char *)True;
  131.                                                                         break;
  132.                                                                 case ARGTYPE_STRING_MULTI:
  133.                                                                         last_data = (struct data_list **)&args->arg[j].data;
  134.                                                                         break;
  135.                                                                 /* Parameters that have only one char attached */
  136.                                                                 case ARGTYPE_CHAR_ATTACHED:
  137.                                                                         if((i + 1) == argc) {
  138.                                                                                 printf("Error: missing arguments: asm file");
  139.                                                                                 ok = False;
  140.                                                                         } else {
  141.                                                                                 switch(argv[i][++k]) {
  142.                                                                                         case 'O':
  143.                                                                                                 args->arg[j].data = (char *)AVRSTUDIO;
  144.                                                                                                 break;
  145.                                                                                         case 'G':
  146.                                                                                                 args->arg[j].data = (char *)GENERIC;
  147.                                                                                                 break;
  148.                                                                                         case 'I':
  149.                                                                                                 args->arg[j].data = (char *)INTEL;
  150.                                                                                                 break;
  151.                                                                                         case 'M':
  152.                                                                                                 args->arg[j].data = (char *)MOTOROLA;
  153.                                                                                                 break;
  154.                                                                                         default:
  155.                                                                                                 printf("Error: wrong file type '%c'",argv[i][2]);
  156.                                                                                                 ok = False;
  157.                                                                                 }
  158.                                                                         }
  159.                                                         }
  160.                                                 }
  161.                                         }
  162.                                 }
  163.                 } else
  164.                         ok = add_arg(last_data, argv[i]);
  165.         }
  166.         return(ok);
  167. }
  168.  
  169.  
  170. int add_arg(struct data_list **last_data, char *argv)
  171. {
  172.         struct data_list *data;
  173.  
  174.         while(*last_data)
  175.                 last_data = &((*last_data)->next);
  176.  
  177.         data = malloc(sizeof(struct data_list));
  178.         if(data) {
  179.                 data->next = NULL;
  180.                 data->data = argv;
  181.                 *last_data = data;
  182.                 last_data = &data->next;
  183.         } else {
  184.                 printf("Error: Unable to allocate memory\n");
  185.                 return(False);
  186.         }
  187.         return(True);
  188. }
  189.  
  190.  
  191. void free_args(struct args *args)
  192. {
  193.         int i;
  194.         struct data_list *data, *temp;
  195.  
  196.         for(data = args->first_data; data;) {
  197.                 temp = data;
  198.                 data = data->next;
  199.                 free(temp);
  200.         }
  201.         for(i = 0; i != args->count; i++)
  202.                 if((args->arg[i].type == ARGTYPE_STRING_MULTI)
  203.                    || (args->arg[i].type == ARGTYPE_STRING_MULTISINGLE))
  204.                         for(data = args->arg[i].data; data;) {
  205.                                 temp = data;
  206.                                 data = data->next;
  207.                                 free(temp);
  208.                         }
  209.         free(args);
  210. }
  211.  
  212.  
  213. void define_arg(struct args *args, int index, int type, char letter, char *longarg, void *def_value)
  214. {
  215.         args->arg[index].type = type;
  216.         args->arg[index].letter = letter;
  217.         args->arg[index].longarg = longarg;
  218.         args->arg[index].data = def_value;
  219. }
  220.  
  221. /* end of args.c */
  222.  
  223.