📄 misc.c
字号:
char *addr, *curval; /* malloc and current value */ int i, done = FALSE; int yn; /* * find the symbol, print current value, and * ask for the new value, or initial value, * if any. */ if (strlen (symbolname) > 2) { printf ("\nSymbol name '%s' is too long!\n", symbolname); return; } /* symbolname contains the line just read from stdin */ for (i = 0; tab[i].name != 0; i++) { if (strcmp (tab[i].name, symbolname) == 0) { curval = tab[i].nvalue ? tab[i].nvalue : tab[i].svalue; break; } } if (tab[i].name == 0) { printf("\nSymbol '%s' not found. Use the 'list' command for a\n", symbolname); printf("complete list of all of the symbols and their defaults.\n"); return(ERROR); } /* * got symbol, now prompt for new value */ do { printf ("\nEnter a new value for symbol '%s'? [%s] ", tab[i].name, curval); switch (getsymbol() ) { case QUIT: return (QUIT); break; case HELP: realhelp(i); /* more specific help */ PrintHelp (h_default); break; case NO: /* easily turn this parameter off, ie. set used */ tab[i].used = NO; done = TRUE; break; case NOREPLY: if ((strcmp (curval, "none") != 0)) tab[i].used = YES; done = TRUE; break; case GOT_SYMBOL: default: strcpy (newval, symbolname); printf ("\nNew '%s' is '%s', is this correct? [y] ", tab[i].name, newval); yn = 'y'; if (YesNo (yn) == TRUE) { if (validate(i, newval) < 0) /* check if valid */ continue; if (strcmp (newval, curval) != 0) { if ((addr = (char *) malloc (strlen (newval) + 1)) == NULL) { printf ("\nmalloc: cannot get space for symbol '%s'.\n", tab[i].name); return(ERROR); } tab[i].nvalue = addr; strcpy (tab[i].nvalue, newval); } tab[i].used = YES; done = TRUE; } break; } } while (NOT done);}/* * invalid postscript argument - print error messege + valid args */static void invalid_arg(opt, opt_num)char *opt;int opt_num;{ struct arg_pair *arg_list; int width=0; get_args(opt_num,&arg_list); printf("\nSorry, the value of '%s' must be one of: ", opt); while (arg_list->arg) { printf("%s ", arg_list->arg); arg_list++; } printf("\n");} /* * validate: check that * EITHER * entered value = "none" to remove entry * OR * int's are all digits * baud rates are legal * booleans are on/off * directory names must all start with '/' * (except "lp" for remote should be "null" * or start with a "@/" for tcp/ip connections ) * * returns -1 if bad, else 0. */validate(i,value1)int i;char *value1;{ extern struct table tab[]; int retval = OK; /* return value from this routine */ int k; /* loop counter */ char value[LEN]; /* value of the symbol just entered */ char dummy[LEN]; /* * save the symbol value locally */ if ((strcmp("none",value1)==0) || (strcmp ("no", value1) == 0)) { /* "none" = delete this entry */ tab[i].nun = 1; tab[i].used = NO; strcpy(value1,""); retval = OK; return (retval); } if (value1 != NULL) strcpy(value, value1); else strcpy(value, ""); /* probably can't happen */ switch (tab[i].stype) { case BOOL: /* Booleans can only be on or off */ if (!(!strcmp("on", value) || (! strcmp("ON", value)) || (! strcmp("off", value)) || (! strcmp("OFF", value)))) { printf("\nSorry, boolean symbol '%s' can only be 'on' or 'off'\n", tab[i].name); retval=BAD; } break; case INT: /* check first that we have all digits */ for (k = 0; value[k]!='\0'; k++) { if (! isdigit(value[k])) { printf("\nSorry, integer symbol '%s' must contain only digits (0 - 9).\n", tab[i].name); retval=BAD; break; } } /* * See if BR was specified and check if it is valid * only if we haven't encountered an error yet. */ if ((strcmp("br", tab[i].name) == 0) && (retval != BAD)) { switch(atoi(value)) { case 0: case 50: case 75: case 110: case 134: case 150: case 200: case 300: case 600: case 1200: case 1800: case 2400: case 4800: case 9600: case 19200: case 38400: case 57600: break; /* baud rate OK */ default: printf("\nSorry, illegal baudrate: %s\n", value); printf("\nAvailable baud rates are:\n"); printf("\t 0\t 134\t 600\t 4800\n"); printf("\t 50\t 150\t 1200\t 9600\n"); printf("\t 75\t 200\t 1800\t 19200\n"); printf("\t 110\t 300\t 2400\t 38400\n"); printf("\t 57600\n"); retval = BAD; break; } } break; case STR: /* check if name is special and must start with '/' */ if (!strcmp ("af", tab[i].name) /* accounting file */ || (!strcmp ("cf", tab[i].name)) /* cifplot filter */ || (!strcmp ("df", tab[i].name)) /* TeX DVI filter */ || (!strcmp ("dn", tab[i].name)) /* daemon name */ || (!strcmp ("gf", tab[i].name)) /* plot filter */ || (!strcmp ("lf", tab[i].name)) /* logfile */ || (!strcmp ("lp", tab[i].name)) /* device name */ || (!strcmp ("nf", tab[i].name)) /* ditroff filter */ || (!strcmp ("rf", tab[i].name)) /* FORTRAN filter */ || (!strcmp ("sd", tab[i].name)) /* spool directory */ || (!strcmp ("tf", tab[i].name)) /* troff filter */ || (!strcmp ("vf", tab[i].name)) /* raster filter */ || (!strcmp ("xf", tab[i].name))) /* passthru filter */ { if (value[0] != '/') { printf("\nSorry, the value of symbol '%s' must begin with '/'.\n",tab[i].name); retval=BAD; } break; } /* check arguments to postscript options */ if (!strcmp("It", tab[i].name)) if (check_arg(value, as_input_trays, dummy) != 0) { invalid_arg("It", as_input_trays); retval=BAD; } if (!strcmp("Ml", tab[i].name)) if (check_arg(value, as_messages, dummy) != 0) { invalid_arg("Ml", as_messages); retval=BAD; } if (!strcmp("Or", tab[i].name)) if (check_arg(value, as_orientations, dummy) != 0) { invalid_arg("Or", as_orientations); retval=BAD; } if (!strcmp("Ot", tab[i].name)) if (check_arg(value, as_output_trays, dummy) != 0) { invalid_arg("Ot", as_output_trays); retval=BAD; } if (!strcmp("Ps", tab[i].name)) if (check_arg(value, as_page_sizes, dummy) != 0) { invalid_arg("Ps", as_page_sizes); retval=BAD; } if (!strcmp("Sd", tab[i].name)) if (check_arg(value, as_page_sizes, dummy) != 0) { invalid_arg("Sd", as_page_sizes); retval=BAD; } if (!strcmp("Si", tab[i].name)) if (check_arg(value, as_sides, dummy) != 0) { invalid_arg("Si", as_sides); retval=BAD; } if (!strcmp("Ss", tab[i].name)) if (check_arg(value, as_page_sizes, dummy) != 0) { invalid_arg("Ss", as_page_sizes); retval=BAD; } break; default: printf ("lprsetup: bad type %d for symbol %s.\n",tab[i].stype,tab[i].name); retval=BAD; } return(retval);}/****************************************************** Read a line and return the symbol; only knows about* quit and help, but none of the other commands.*****************************************************/getsymbol (){ extern struct cmdtyp cmdtyp[]; extern char symbolname[]; int i, length, retval; register char *q; char line[BUF_LINE]; /* input line */ char line2[BUF_LINE]; /* saved version of the input line */ if (fgets (line, BUF_LINE, stdin) == NULL) { printf ("\n"); /* EOF (^D) */ return (QUIT); } if (line[0] == '\n') { return (NOREPLY); } if (line[strlen (line) - 1] == '\n') { line[strlen (line) - 1] = NULL; } for (q = line; isspace (*q); q++) { ; /* nop */ } strcpy(line2, line); /* save original entry, including caps */ MapLowerCase(line); length = strlen(line); for (i = 0; cmdtyp[i].cmd_name; i++) { if (strncmp(cmdtyp[i].cmd_name, line, length) == 0) { retval = cmdtyp[i].cmd_id; break; } } strcpy(symbolname, line2); /* save symbol name globaly */ if ((strcmp(symbolname, "help") == 0) || (strcmp(symbolname, "quit") == 0) || (strcmp(symbolname, "?") == 0) || (strcmp(symbolname, "printer?") == 0) || (strcmp(symbolname, "no") == 0) || (strcmp(symbolname, "none") == 0)){ return(retval); /* return command id only if quit or help */ } else { return (GOT_SYMBOL); /* else return a symbol */ }}/****************************************************** Read a line, decode the command, return command id* This routine knows about the full command set.* DJG#2*****************************************************/getcmd (){ extern struct cmdtyp cmdtyp[]; extern char symbolname[]; int i, length; register char *q; char line[BUF_LINE]; /* input line */ char tline[BUF_LINE]; /* tmp copy of input line */ if (fgets (line, BUF_LINE, stdin) == NULL) { /* EOF (^D) */ printf ("\n"); return (QUIT); } if (line[0] == '\n') return (NOREPLY); if (line[strlen (line) - 1] == '\n') line[strlen (line) - 1] = NULL; for (q = line; isspace (*q); q++)/* strip leading blanks */ ; strcpy (tline, line); /* DJG#2 */ MapLowerCase (line); length = strlen (line); for (i = 0; cmdtyp[i].cmd_name; i++) if (strncmp (cmdtyp[i].cmd_name, line, length) == 0) return (cmdtyp[i].cmd_id);/* command id */ strcpy (symbolname, tline); /* save symbol name globaly DJG#2 */ return (GOT_SYMBOL);}/************************************************************* Read a line, decode the printer type, return printer id* This routine knows about all supported printers *************************************************************/getprnttype (){ extern struct prnttyp prnttyp[]; extern char printertype[]; int i, length; register char *q; char line[BUF_LINE]; /* input line */ if (fgets (line, BUF_LINE, stdin) == NULL) { /* EOF (^D) */ printf ("\n"); return (PTQUIT); } if (line[0] == '\n') return (PTNOREPLY); if (line[strlen (line) - 1] == '\n') line[strlen (line) - 1] = NULL; for (q = line; isspace (*q); q++)/* strip leading blanks */ ; MapLowerCase (line); length = strlen (line); for (i = 0; prnttyp[i].prnt_nam; i++) if (strncmp (prnttyp[i].prnt_nam, line, length) == 0) return (prnttyp[i].prnt_id);/* printer id */ strcpy (printertype, line); /* save printer name globaly */ return (NOT_SUPPORTED);}/************************************************************/leave (status)int status;{ exit (status);}/*************************** free nvalue when done**************************/freemem (){ extern struct table tab[]; int i; for (i = 0; tab[i].name != 0; ++i) if (tab[i].nvalue > 0) free (tab[i].nvalue);}/* * finish printer setup * notify user of success */setupdone(){ printf("\nSet up activity is complete for this printer.\n"); printf("Verify that the printer works properly by using\n"); printf("the lpr(1) command to send files to the printer.\n"); return;}/***************************************** edit the ttys file and change the line* to an appropriate setting: * "off" for adding/modifying a printer* "on" for deleting a printer****************************************/fixtty(line,mode)char *line; /* */int mode; /* 0 == off, 1 == on */{ FILE *fp; fp = Popen(EDTTY, "w"); if (fp == NULL) { perror("popen(%s)\n", EDTTY); fprintf(stderr, "\n/etc/ttys not edited (could not change line %s to %s).\n", line, mode ? "on" : "off"); return(OK); } if (mode == 0) { dprintf(stderr, "/^%s[ ]/s/[ ]on[ ]/ off /\n", line); fprintf(fp, "/^%s[ ]/s/[ ]on[ ]/ off /\n", line); } else { dprintf(stderr, "/^%s[ ]/s/[ ]off[ ]/ on /\n", line); fprintf(fp, "/^%s[ ]/s/[ ]off[ ]/ on /\n", line); } fprintf(fp, "w\n"); fprintf(fp, "q\n"); fflush(fp); pclose(fp); kill(1, SIGHUP); /* send hangup to init */ return(OK);}FILE* Popen(s1, s2)char *s1, *s2;{ FILE *fp; close(1); fp = popen(s1, s2); dup2(3, 1); return(fp);}/*********************************** Determines if pname is in the buf* needed by DeleteEntry***********************************/findpname (pname, buf)char pname[], buf[];{ int i,j,done; char *c; i = strlen(pname); if ((strncmp (pname, buf, i) == 0) && ((buf[i] == ':') || (buf[i] == '|'))) { return (TRUE); } else { j = i; done = FALSE; while (!done) { while ((buf[j] != '|') && (buf[j] != ':') && (buf[j] != ' ')) j++; if ((buf[j] != ':') && (buf[j] != ' ')) { j++; c = &buf[j]; if ((strncmp (pname, c, i) == 0) && ((buf[i+j] == '|') || (buf[i+j] == ':'))) { return (TRUE); } } else { return (FALSE); } } }} /* * PrintHelp - Prints the requested help information, * it stops every 23 lines. DJG#3 */intPrintHelp (hlpmsg)char *hlpmsg;{ int done, pos, linecnt; char line[BUFLEN]; pos = 0; done = FALSE; linecnt = 0; GetRows(); /* Do it every time in case window is resized DJG#4 */ while (sgetline (hlpmsg, line, &pos) && !done) { linecnt ++; printf ("%s\n", line); if (linecnt == rows) { printf ("\nPress 'RETURN' to continue or 'quit RETURN' to quit: "); switch (getcmd()) { case QUIT: case GOT_SYMBOL: done = TRUE; break; case NOREPLY: default: linecnt = 0; break; } } }}/* * sgetline - returns in line the next line in buf, reads until a \n is found. * the \n is changed to a \0. Returns true if a line can be read. False if not. * DJG#3 */intsgetline (buf, str, pos)char *buf, *str;int *pos;{ int i; if (buf[*pos] == '\0') { str[0] = '\0'; return (0); } else { for (i = 0; (str[i] = buf[*pos]) != '\n'; i++, (*pos)++); str[i] = '\0'; (*pos)++; return (1); }}/* * GetRows - sets the global variable "rows" to contain the number of rows in * the output window. * DJG#4 */intGetRows (){ struct winsize win; rows = DEFAULTROWS; if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { if (win.ws_row > 0) rows = win.ws_row - 2; else rows = DEFAULTROWS; }}/******************************************************************************* end of misc.c******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -