📄 init.c
字号:
/* ---------------------------------------------------------------------------- * init.c * funtions that prepare program workflow and initialization * * Copyright 2003/2004 * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * ----------------------------------------------------------------------------*/#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <getopt.h>#include <errno.h>#include <string.h>#include <ctype.h>#include "isp-at89.h"/* This function cleans up invalid combinations of operation * modes and prints a warning message. */voidcorrectBadConfig(struct ProgrammingData *pdata){ if ((pdata->command == CMD_TEST) & (pdata->flags.verify | pdata->flags.erase | pdata->flags.disassemble)) { printf(_("-- WARNING: Any other flag combined with testmode makes no sense - extra flags disabled.\n")); pdata->flags.verify = 0; pdata->flags.erase = 0; pdata->flags.disassemble = 0; } else if ((pdata->command == CMD_READ) & (pdata->flags.verify | pdata->flags.erase | pdata->flags.disassemble)) { if (pdata->flags.erase) { printf(_("-- WARNING: Read combined with erase makes no sense - erase disabled.\n")); pdata->flags.erase = 0; } if (pdata->flags.verify) { printf(_("-- WARNING: Read combined with verify makes no sense - verify disabled.\n")); pdata->flags.verify = 0; } if (pdata->flags.disassemble && (pdata->datafile[0] != 0)) { printf(_("-- WARNING: Disassembler output can't be written to file directly - disassembling disabled.\n")); pdata->flags.disassemble = 0; } } else if ((pdata->command == CMD_WRITE) & (pdata->flags.disassemble)) { printf(_("-- WARNING: Disassembling in write mode not supported - disassembling disabled.\n")); pdata->flags.disassemble = 0; }}/* This function opens a file handle for writing if the given * filename doesn't exist or belongs to an reguarly file. In * the second case a confirmation is necessary to overwrite * the file. */intopenDataFile (char *name){ int fs, rc = ERROR; char input[2], buffer[STDBUFLEN]; char *yes = _("yes"), *no = _("no"); fs = getFileStatus(name); switch (fs) { case FST_FILE: printf(_("-- WARNING: File %s exits, overwrite? [%c|%c] "), name, yes[0], no[0]); input[0] = getchar(); input[1] = 0; snprintf(buffer, STDBUFLEN, "\r%%%ds\r", strlen(_("-- WARNING: File %s exits, overwrite? [%c|%c] ")) + strlen(name)); printf(buffer, " "); if (strncasecmp(input, yes, 1)) { printf(_(" aborting...\n")); return ERROR; } break; case FST_NOEXIST: break; default: printf (_("-- ERROR: %s is not a regular file.\n"), name); return ERROR; } if ((rc = open(name, O_RDWR | O_CREAT | O_TRUNC, 0644)) > 0) return rc; printf (_("-- ERROR: Can't open %s: %s\n"), name, strerror(errno)); return ERROR;}/* This function checks if the given string is a valid * command. If so the command code is returned. Otherwise * 0 is returned */intgetCommand(char *str){ int rc; if (str != NULL) { if (!strncasecmp (str, "read", strlen(str) < 4 ? strlen(str) : 4)) rc = CMD_READ; else if (!strncasecmp (str, "write", strlen(str) < 5 ? strlen(str) : 5)) rc = CMD_WRITE; else if (!strncasecmp (str, "test", strlen(str) < 4 ? strlen(str) : 4)) rc = CMD_TEST; else if (!strncasecmp (str, "dump", strlen(str) < 4 ? strlen(str) : 4)) rc = CMD_DUMP; else rc = CMD_NONE; } else rc = CMD_NONE; return rc;}voidprintUsage(char *prgname){ printf(_("Usage: %s [OPTIONS] <command> [<file>]\n"), prgname); printf(_("Commands:\n" " test activate the test mode\n" " read read from memory\n" " write write to memory\n" " dump dump file to screen\n")); printf(_("Options:\n" " -%c, --help display this help and exit\n" " -%c, --version display version information and exit\n" " -%c, --ppdev=<dev> set the parport device the prommer is connected to\n" " -%c, --verify verify code/data after writing\n" " -%c, --erase erase code/data memory before writing\n" " -%c, --disassemble disassemble data instead of dumping hex codes\n" " -%c, --memory=<mem> set the memory to act on. default is the internal memory\n"), ARG_HELP, ARG_VERSION, ARG_PPDEV, ARG_VERIFY, ARG_ERASE, ARG_DISASM, ARG_MEMORY);}/* This function evaluates the command line arguments. Some options will be processed directly in this function like parport device and datafiles. Other options only set a flag for pending reactions. */intevaluateArgs(struct ProgrammingData *pdata, int argc, char *argv[]){ struct option const long_options[] = { {"help", no_argument, 0, ARG_HELP}, {"version", no_argument, 0, ARG_VERSION}, {"ppdev", required_argument , 0, ARG_PPDEV}, {"check", no_argument, 0, ARG_VERIFY}, {"erase", no_argument, 0, ARG_ERASE}, {"disassemble", no_argument, 0, ARG_DISASM}, {"memory", required_argument, 0, ARG_MEMORY}, {NULL, 0, NULL, 0} }; int c; char *prgname; if((prgname = strrchr(argv[0],'/')) == NULL) prgname = argv[0]; else prgname++; /* ignore first slash*/ argv[0] = prgname; if (argc == 1) { /* no arguments given ? */ printUsage(prgname); /* then help the user */ return READY; } /* Print intro */ printf(_("%s - An In-System-Programming tool for the ATMEL AT89S8252\n"), prgname); while ((c = getopt_long (argc, argv, ARG_ALL, long_options, (int *) 0)) != EOF) { switch (c) { case ARG_VERSION: printf (_("Version %s, (c) 2003/2004 Matthias Grimm, Andreas Ochmann and Sascha Flohr\n"), VERSION); printf (_(" -- Build from %s at %s\n"),__DATE__,__TIME__); return READY; case ARG_MEMORY: if (!strncasecmp (optarg, "intern", strlen(optarg) < 6 ? strlen(optarg) : 6)) pdata->memory = MEM_INTERN; else if (!strncasecmp (optarg, "extern", strlen(optarg) < 6 ? strlen(optarg) : 6)) pdata->memory = MEM_EXTERN; else if (!strncasecmp (optarg, "eeprom", strlen(optarg) < 6 ? strlen(optarg) : 6)) pdata->memory = MEM_EEPROM; break; case ARG_PPDEV: strncpy(pdata->ppdevname, optarg, STDBUFLEN); break; case ARG_VERIFY: pdata->flags.verify = 1; break; case ARG_ERASE: pdata->flags.erase = 1; break; case ARG_DISASM: pdata->flags.disassemble = 1; break; case ARG_HELP: default: printUsage(prgname); return READY; } } /* check for command */ if ((pdata->command = getCommand(argv[optind])) == CMD_NONE) { printf("-- ERROR: command missing or invalid. Use read, write, dump or test.\n"); printf(" aborting...\n"); return ERROR; } else optind++; /* next option */ /* copy data file name if given */ if (argv[optind] != NULL) strncpy(pdata->datafile, argv[optind], STDBUFLEN); correctBadConfig(pdata); /* correct invalid combination of options */ printFlags(pdata); return OK;}voidsetupProgrammingData(struct ProgrammingData *pdata){ pdata->command = CMD_READ; pdata->memory = MEM_INTERN; strcpy(pdata->ppdevname, "/dev/parport0"); pdata->datafile[0] = 0; pdata->flags.verify = 0; pdata->flags.erase = 0; pdata->flags.disassemble = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -