int main( int argc, char *argv[] ) { int d = 0, c; int nline = 0; while ((c = getopt (argc, argv, "hn")) != -1) switch (c) { case 'h': prhelp(); return 0; case 'n': nline = 1; break; case '?': if (optopt == 'd' || optopt == 'f') { printf("case?\n"); prhelp(); } else if (isprint (optopt)) { fprintf (stderr, "Unknown option `-%c'.\n", optopt); prhelp(); } else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: abort (); } if(optind < argc ) { int index; for (index = optind; index < argc; index++) { char * filename = argv[index]; FILE *fp = fopen(filename, "r"); char *line = NULL; size_t len = 0; ssize_t read; while ((read = getline(&line, &len, fp)) != -1) { if(nline > 0) printf("%d\t", nline++); printf("%s", line); } fclose(fp); } } else { char *line = NULL; size_t len = 0; ssize_t read; while ((read = getline(&line, &len, stdin)) != -1) { if(nline > 0) printf("%d\t", nline++); printf("%s", line); } free(line); exit(EXIT_SUCCESS); } return 0; }
/* Use XrmParseCommand to parse command line options to option variable */ static void doOptMain (int argc, char *argv[]) { /* Initialise resource manager and parse options into database */ XrmInitialize(); XrmParseCommand( &opt_db, opt_tab, sizeof(opt_tab) / sizeof(opt_tab[0]), XC_NAME, &argc, argv ); /* set output level */ if ( XrmGetResource( opt_db, "xclip.olevel", "Xclip.Olevel", &rec_typ, &rec_val ) ) { /* set verbose flag according to option */ if (strcmp(rec_val.addr, "S") == 0) fverb = OSILENT; if (strcmp(rec_val.addr, "Q") == 0) fverb = OQUIET; if (strcmp(rec_val.addr, "V") == 0) fverb = OVERBOSE; } /* set direction flag (in or out) */ if ( XrmGetResource( opt_db, "xclip.direction", "Xclip.Direction", &rec_typ, &rec_val ) ) { if (strcmp(rec_val.addr, "I") == 0) fdiri = T; if (strcmp(rec_val.addr, "O") == 0) fdiri = F; } /* set filter mode */ if ( XrmGetResource( opt_db, "xclip.filter", "Xclip.Filter", &rec_typ, &rec_val ) ) { /* filter mode only allowed in silent mode */ if (fverb == OSILENT) ffilt = T; } /* check for -help and -version */ if ( XrmGetResource( opt_db, "xclip.print", "Xclip.Print", &rec_typ, &rec_val ) ) { if (strcmp(rec_val.addr, "H") == 0) prhelp(argv[0]); if (strcmp(rec_val.addr, "V") == 0) prversion(); } /* check for -display */ if ( XrmGetResource( opt_db, "xclip.display", "Xclip.Display", &rec_typ, &rec_val ) ) { sdisp = rec_val.addr; if (fverb == OVERBOSE) /* print in verbose mode only */ fprintf(stderr, "Display: %s\n", sdisp); } /* check for -loops */ if ( XrmGetResource( opt_db, "xclip.loops", "Xclip.Loops", &rec_typ, &rec_val ) ) { sloop = atoi(rec_val.addr); if (fverb == OVERBOSE) /* print in verbose mode only */ fprintf(stderr, "Loops: %i\n", sloop); } /* Read remaining options (filenames) */ while ( (fil_number + 1) < argc ) { if (fil_number > 0) { fil_names = xcrealloc( fil_names, (fil_number + 1) * sizeof(char*) ); } else { fil_names = xcmalloc(sizeof(char*)); } fil_names[fil_number] = argv[fil_number + 1]; fil_number++; } }
int cmd_help(int ac, char *av[]) { char prnbuf[LINESZ + 8]; int i, j, namemax, optsmax, descmax, len; int ln, siz; int clistno; int expert; const Cmd *CmdTable; namemax = optsmax = descmax = 0; expert = getenv(EXPERT) != 0; clistno = 0; while((CmdTable = CmdList[clistno++]) != 0) { for (i = 1; CmdTable[i].name != 0; i++) { len = strlen (CmdTable[i].name); if(len > namemax) { namemax = len; } len = strlen (CmdTable[i].opts); if(len > optsmax) { optsmax = len; } len = strlen (CmdTable[i].desc); if(len > descmax) { descmax = len; } } } siz = moresz; ioctl (STDIN, CBREAK, NULL); ln = siz; if(ac >= 2 && !strcmp(av[1], "*")) { /* all commands */ clistno = 0; while((CmdTable = CmdList[clistno++]) != 0) { for (i = 1; CmdTable[i].name != 0; i++) { if((CmdTable[i].flag & CMD_HIDE && !expert) || CmdTable[i].flag & CMD_ALIAS) { } else if(prhelp (&CmdTable[i], &ln, siz, namemax, optsmax)) { return(0); } } } } else if(ac >= 2) { /* specific commands */ for (j = 1; j < ac; j++) { clistno = 0; while((CmdTable = CmdList[clistno++]) != 0) { for (i = 1; CmdTable[i].name != 0; i++) { if(!strcmp(CmdTable[i].name, av[j])) { break; } } if(CmdTable[i].name && (!(CmdTable[i].flag & CMD_HIDE) || expert)) { if(prhelp (&CmdTable[i], &ln, siz, namemax, optsmax)) { return(0); } break; } } if(CmdTable == 0) { printf ("%s: not found\n", av[j]); } } } else { /* general help only */ const char *p = ""; clistno = 0; i = 0; j = 0; while((CmdTable = CmdList[clistno++]) != 0) { if(strcmp(CmdTable->name, p)) { if(i % 2 != 0) { if(more(prnbuf, &ln, siz)) { return(0); } } printf("%~70s\n", CmdTable->name); i = 0; j = 0; p = CmdTable->name; } CmdTable++; while(CmdTable->name != 0) { if (!(CmdTable->flag & CMD_ALIAS) && (!(CmdTable->flag & CMD_HIDE) || expert)) { sprintf(prnbuf, "%*s %-*s", namemax, CmdTable->name, descmax, CmdTable->desc); if(i % 2 != 0) { if(more(prnbuf, &ln, siz)) { return(0); } } i++; } CmdTable++; } } if(i % 2 != 0) { printf ("%s\n", prnbuf); } } return (0); }