initedt.c

00001 /* #pragma ident "@(#)initedt.c 1.18 05/30/07 EDT" */
00002 
00003 #include "edtinc.h"
00004 #include "initedt.h"
00005 #include <stdlib.h>
00006 
00007 /******************************************************************************
00008  *                                                                            * 
00009  * initedt:  Support routines for initpcd (or initXXX, XXX is an EDT product. *
00010  *                                                                            * 
00011  * Commands are defined and implemented in initpcd.c and initpcd.h            *
00012  *                                                                            * 
00013  ******************************************************************************/
00014 
00015 /*
00016  * Open file variables.
00017  */
00018 int     edt_unit = 0 ;
00019 char    *initedt_config_file = NULL ;
00020 EdtDev  *initedt_p ;
00021 
00022 
00023 /*
00024  * Flow control and line parser loop variables.
00025  */
00026 int     initedt_verbose = 0 ;
00027 int     dump_cmds       = 0 ;
00028 int     run_cmds        = 1 ;
00029 int     initedt_load_bitfile    = 1 ;
00030 int     lineno          = 0 ;
00031 char    input_line[512] ;
00032 char   *prog_name ;
00033 
00034 
00035 /*
00036  * External declarations from C file defining a config interface.
00037  * Example:  see initpcd.c
00038  */
00039 cmd_operator get_cmd_copy(int) ;
00040 char *get_cmd_name(int cmd_index) ;
00041 int cmd_table_entries() ;
00042 
00043 
00044 /******************************************************************************
00045  *                                                                            * 
00046  * Functions are listed in reverse calling order starting with                *
00047  * main() at the bottom of this file.  Data declarations are listed first.    *
00048  *                                                                            * 
00049  ******************************************************************************/
00050 
00051 cmd_operator *cmd_list = NULL ;
00052 static cmd_operator **cmd_tail = &cmd_list ;
00053 
00054 cmd_operator *
00055 create_queued_command(int cmd_index)
00056 {
00057     cmd_operator *cmd_new ;
00058 
00059     cmd_new = (cmd_operator *) calloc(1, sizeof(cmd_operator)) ;
00060 
00061     *cmd_new = get_cmd_copy(cmd_index) ;
00062 
00063     *cmd_tail = cmd_new ;
00064 
00065     cmd_tail = &(cmd_new->cmd_next) ;
00066 
00067     *cmd_tail = NULL ;
00068 
00069     return cmd_new ;
00070 }
00071 
00072 /******************************************************************************
00073  *                                                                            *
00074  * process_cmds() carries out the work for each line found in the config      *
00075  * file.  Add code for new commands here; see "MATCHES()" below.              *
00076  *                                                                            *
00077  ******************************************************************************/
00078 int
00079 process_cmds()
00080 {
00081     char *cmd_name ;
00082     int (*func)(cmd_operator *) ;
00083     cmd_operator *cmd_ptr, *tmp_ptr ;
00084 
00085     if (initedt_verbose)
00086         puts("\nExecuting commands:\n") ;
00087 
00088     cmd_ptr = cmd_list;
00089     cmd_list = NULL ;
00090     cmd_tail = &cmd_list ;
00091 
00092     while (cmd_ptr)
00093     {
00094         cmd_name = cmd_ptr->cmd_name ;
00095 
00096         if (initedt_verbose)
00097         {
00098             printf("\n%-16s arg1: %-4x arg2: 0x%-4x argf: %-10lf",
00099                 cmd_ptr->cmd_name,
00100                 cmd_ptr->cmd_intval1,
00101                 cmd_ptr->cmd_intval2,
00102                 cmd_ptr->cmd_realval) ;
00103             if (cmd_ptr->cmd_pathval)
00104                 printf(" pathval: %s", cmd_ptr->cmd_pathval) ;
00105             puts("\n") ;
00106         }
00107 
00108 
00109         /*
00110          * Execute the code for this function.
00111          */
00112         func = cmd_ptr->cmd_func ;
00113         if ((*func)(cmd_ptr) != 0)
00114         {
00115             if (initedt_verbose)
00116                 fprintf(stderr, "\n\"%s\": command failed.  Aborting.\n\n",
00117                                                                 cmd_name) ;
00118             return -1 ;
00119         }
00120 
00121         tmp_ptr = cmd_ptr;
00122         cmd_ptr = cmd_ptr->cmd_next;
00123 
00124         if (tmp_ptr->cmd_pathval)
00125         {
00126             free(tmp_ptr->cmd_pathval);
00127         }
00128 
00129         free(tmp_ptr);
00130     }
00131     return 0 ;
00132 }
00133 
00134 void
00135 print_cmds()
00136 {
00137     cmd_operator *cmd_ptr ;
00138 
00139     puts("") ;
00140     for (cmd_ptr = cmd_list; cmd_ptr; cmd_ptr = cmd_ptr->cmd_next)
00141     {
00142         printf("%-16s arg1: %-4x arg2: 0x%-4x argf: %-10lf",
00143             cmd_ptr->cmd_name,
00144             cmd_ptr->cmd_intval1,
00145             cmd_ptr->cmd_intval2,
00146             cmd_ptr->cmd_realval) ;
00147         if (cmd_ptr->cmd_pathval)
00148             printf(" pathval: %s", cmd_ptr->cmd_pathval) ;
00149         puts("") ;
00150     }
00151     puts("") ;
00152 }
00153 
00154 int
00155 get_cmd_index(char *name)
00156 {
00157     int i ;
00158     int retval = -1 ;
00159     int cmd_table_size = cmd_table_entries() ;
00160 
00161     /*
00162      * The list is short, so a linear search will suffice.
00163      */
00164     for (i = 0; i < cmd_table_size; i++)
00165     {
00166         if (strcmp(get_cmd_name(i), name) == 0)
00167             return i ;
00168     }
00169     return retval ;
00170 }
00171 
00172 int
00173 get_pathval(char *cmd, char *s)
00174 {
00175     /* Find the first ':' */
00176     while (*cmd != ':' && *cmd != '\0')
00177         ++ cmd ;
00178     if (*cmd == '\0')
00179         return 1 ;
00180     ++ cmd ;
00181 
00182     /* Now move to the first non-space character */
00183     while ((*cmd == ' ' || *cmd == '\t') && *cmd != '\0')
00184         ++ cmd ;
00185     if (*cmd == '\0')
00186         return 1 ;
00187 
00188     /* Copy from here to end of line; translate $u to unit number */
00189     while (*cmd != '\0')
00190     {
00191         if (*cmd == '$' && *(cmd+1) == 'u')
00192         {
00193             *s++ = '0' + edt_unit ;
00194             cmd += 2 ;
00195         }
00196         else
00197             *s++ = *cmd++;
00198     }
00199     
00200     /* Truncate any white space from end of line */
00201     while (*(s-1) == ' ' || *(s-1) == '\r' || *(s-1) == '\n' || *(s-1) == '\t')
00202         -- s ;
00203     *s = '\0' ;
00204 
00205     return 0 ;
00206 }
00207 
00208 
00209 int
00210 cmd_line_parse(char *cmd_line)
00211 {
00212     char          cmd_name[128] ;
00213     char          cmd_format[128] ;
00214     int           cmd_index ;
00215     u_int         arg1, arg2 ;
00216     double        argf ;
00217     char         *path_ptr ;
00218     cmd_operator *cmd_ptr ;
00219 
00220     /*
00221      * First get the command name.
00222      */
00223     if (sscanf(cmd_line, "%[0-9a-zA-Z_]", cmd_name) != 1)
00224         return 1 ;
00225     
00226     /*
00227      * Next get the command table index for this command.
00228      */
00229     if ((cmd_index = get_cmd_index(cmd_name)) == -1)
00230     {
00231         fprintf(stderr,"\nSyntax error on line %d, no such command:\n", lineno);
00232         fputs("\n\t", stderr) ;
00233         fputs(input_line, stderr) ;
00234         fputs("\n", stderr) ;
00235         fputs("Fatal error.\n\n", stderr);
00236         exit(1);
00237     }
00238 
00239     /*
00240      * Allocate and queue a command operator.
00241      */
00242     cmd_ptr = create_queued_command(cmd_index) ;
00243 
00244 
00245     /*
00246      * Command argument switch:
00247      *     1 = get one hex integer argument,
00248      *     2 = get two hex integer arguments,
00249      *     3 = get one real argument,
00250      *     4 = get one pathname argument.
00251      */
00252     switch (cmd_ptr->cmd_type)
00253     {
00254     case 1:
00255 
00256         /*
00257          * Prepare format string and argument pointers for sscanf().
00258          */
00259         sprintf(cmd_format, "%s: %%x", cmd_ptr->cmd_name) ;
00260 
00261         if (sscanf(cmd_line, cmd_format, &arg1) != 1)
00262         {
00263             fputs("\nExpected one hex argument.", stderr) ;
00264             return 1 ;
00265         }
00266         cmd_ptr->cmd_intval1 =  arg1 ;
00267 
00268         break ;
00269 
00270     case 2:
00271 
00272         /*
00273          * Prepare format string and argument pointers for sscanf().
00274          */
00275         sprintf(cmd_format, "%s: %%x %%x", cmd_ptr->cmd_name) ;
00276 
00277         if (sscanf(cmd_line, cmd_format, &arg1, &arg2) != 2)
00278         {
00279             fputs( "\nExpected two hex integer arguments", stderr) ;
00280             return 1 ;
00281         }
00282 
00283         cmd_ptr->cmd_intval1 =  arg1 ;
00284         cmd_ptr->cmd_intval2 =  arg2 ;
00285 
00286         break ;
00287 
00288     case 3:
00289 
00290         /*
00291          * Prepare format string and argument pointers for sscanf().
00292          */
00293         sprintf(cmd_format, "%s: %%lf", cmd_ptr->cmd_name) ;
00294 
00295         if (sscanf(cmd_line, cmd_format, &argf) != 1)
00296         {
00297             fputs( "\nExpected one real argument", stderr) ;
00298             return -1 ;
00299         }
00300 
00301         cmd_ptr->cmd_realval =  argf ;
00302 
00303         break ;
00304 
00305     case 4:
00306         /*
00307          * Allocate memory to hold path string.
00308          */
00309         path_ptr = (char *) malloc(strlen(cmd_line)) ;
00310         
00311         if (get_pathval(cmd_line, path_ptr))
00312         {
00313             fputs( "\nExpected one pathname or string argument", stderr) ;
00314             return -1 ;
00315         }
00316 
00317         cmd_ptr->cmd_pathval =  path_ptr ;
00318 
00319         break ;
00320     }
00321 
00322     return 0 ;
00323 }
00324 
00325 int
00326 is_comment_or_empty(char *str)
00327 {
00328     while (*str == ' ' || *str == '\t')
00329         ++str ;
00330 
00331     if (*str == '#' || *str == '\n' || *str == '\r' || *str == '\0')
00332         return 1 ;
00333 
00334     return 0 ;
00335 }
00336 
00337 int
00338 input_config_file(char *config_file)
00339 {  
00340     FILE        *cfg_fp ;
00341 
00342     if ((cfg_fp = fopen(config_file, "r")) == NULL)
00343     {
00344         return -1 ;
00345     }
00346 
00347     while (fgets(input_line, 511, cfg_fp))
00348     {
00349         lineno++;
00350 
00351         if (initedt_verbose)
00352             fputs(input_line, stdout);
00353 
00354         if (is_comment_or_empty(input_line))
00355             continue;
00356 
00357         if (cmd_line_parse(input_line))
00358         {
00359             fprintf(stderr, "\nSyntax error on line %d:\n", lineno) ;
00360             fputs("\n\t", stderr) ;
00361             fputs(input_line, stderr) ;
00362             fputs("\n", stderr) ;
00363             fputs("Fatal error.\n\n", stderr);
00364             exit(1);
00365         }
00366     }
00367 
00368     return 0 ;
00369 }
00370 
00371 char *
00372 copy2nl(char *dest, char *source, int maxlen)
00373 {
00374     int count = 0;
00375 
00376     while ((*dest = *source) != '\n' && *dest != '\0')
00377     {
00378         if (++count == maxlen)
00379             break;
00380 
00381         ++ dest;
00382         ++ source;
00383     }
00384 
00385     *dest = '\0';
00386 
00387     if (*source == '\0')
00388         return NULL;
00389     else
00390         return ++source;
00391 }
00392 int
00393 input_config_str(char *config_str)
00394 {  
00395     char *line = config_str;
00396 
00397     for (line = copy2nl(input_line, line, 511); line;
00398          line = copy2nl(input_line, line, 511))
00399     {
00400         lineno++;
00401 
00402         if (is_comment_or_empty(input_line))
00403             continue;
00404 
00405         if (cmd_line_parse(input_line))
00406         {
00407             fprintf(stderr, "\nSyntax error on line %d:\n", lineno) ;
00408             fputs("\n\t", stderr) ;
00409             fputs(input_line, stderr) ;
00410             fputs("\n", stderr) ;
00411             fputs("Fatal error.\n\n", stderr);
00412             exit(1);
00413         }
00414 
00415         if (line && *line)
00416             continue;
00417         else
00418             break;
00419     }
00420 
00421     return 0 ;
00422 }
00423 
00424 extern void initedt_usage();
00425 
00426 int
00427 handle_args(int argc, char **argv)
00428 {
00429     prog_name = argv[0] ;
00430 
00431     --argc;
00432     ++argv;
00433     while (argc && argv[0][0] == '-')
00434     {
00435         switch (argv[0][1])
00436         {
00437 
00438         case 'u':
00439             {
00440                 argv++;
00441                 argc--;
00442                 if (argc > 0)
00443                     edt_unit = atoi(argv[0]);
00444 
00445                 break;
00446             }
00447         case 'v':
00448             {
00449                 initedt_verbose = 1 ;
00450                 break;
00451             }
00452         case 'p':
00453             {
00454                 dump_cmds = 1 ;
00455                 break;
00456             }
00457         case 'n':
00458             {
00459                 run_cmds = 0 ;
00460                 break;
00461             }
00462         case 'N':
00463             {
00464                 initedt_load_bitfile = 0 ;
00465                 break;
00466             }
00467         case 'f':
00468             {
00469                 argv++;
00470                 argc--;
00471                 if (argc > 0)
00472                     initedt_config_file = argv[0];
00473                 break;
00474             }
00475         default:
00476             {
00477                 initedt_usage() ;
00478                 return -1 ;
00479                 break ;
00480             }
00481         }
00482         --argc;
00483         ++argv;
00484     }
00485 
00486     if (initedt_config_file == NULL)
00487     {
00488         fputs("\n", stderr) ;
00489         initedt_usage() ;
00490         return -1 ;
00491     }
00492 
00493     if ((initedt_p = edt_open(EDT_INTERFACE, edt_unit)) == NULL)
00494     {
00495         char str[64] ;
00496         sprintf(str, "\n/dev/%s%d", EDT_INTERFACE, edt_unit) ;
00497         perror(str);
00498         initedt_usage() ;
00499         return -1 ;
00500     }
00501 
00502     return 0 ;
00503 }

Generated on 19 Jun 2015 by  doxygen 1.4.7