/* * Function: pop_quit * * Purpose: Quit the connection to the server, * * Arguments: * server The server to quit. * * Return value: 0 for success, non-zero otherwise with error in * pop_error. * * Side Effects: The popserver passed in is unusable after this * function is called, even if an error occurs. */ int pop_quit (popserver server) { int ret = 0; if (server->file >= 0) { if (pop_retrieve_flush (server)) { ret = -1; } if (sendline (server, "QUIT") || getok (server)) { ret = -1; } close (server->file); } free (server->buffer); free ((char *) server); return (ret); }
void perform_sync(int fd) { unsigned char out[9]; memset(out, COMMAND_SYNC, sizeof(out)); out[8] = 0; raw_writebuf(fd, out, sizeof(out)); if (getok(fd, 5, 1000 * 1000) != 0) fprintf(stderr, "Error getting okay during cmd\n"); }
int perform_custom(int fd, char *tag) { int string; int addr; int red; int i; unsigned char out[4]; if (strcmp(tag, "hammer") == 0) { build_bulb(out, 0, 0, 0xcc, 0, 0, 13, 0); for (i = 0; i < 6000; i++) { writebuf(fd, out, 4); flush_buffer(fd); if (getok(fd, 1, 1000 * 1000) < 0) fprintf(stderr, "Error: did not get closing ack\n"); } return 0; } if (strcmp(tag, "one") == 0) { build_bulb(out, 0, 0, 0xcc, 15, 0, 0, 0); writebuf(fd, out, 4); for (red = 0; red < 500; red++) { flush_buffer(fd); usleep(1000); } return 0; } if (strcmp(tag, "red") == 0) { while (1) { for (red = 0; red <= 15; red++) { for (addr = 0; addr <= 34; addr++) { for (string = 0; string <= 5; string++) { build_bulb(out, string, addr, 0xcc, red, 0, 0, string == 5 ? 0 : 1); writebuf(fd, out, 4); } } } } return 0; } return -1; }
void writebuf(int fd, unsigned char *out, int size) { if (raw_writebuf(fd, out, size) == 0) { g_writes++; if (g_confirm_every > 0) if (g_writes % g_confirm_every == 0) if (getok(fd, 1, 1000 * 1000) < 0) fprintf(stderr, "Error: did not get periodic ack\n"); } }
/* * Function: pop_reset * * Purpose: Reset the server to its initial connect state * * Arguments: * server The server. * * Return value: 0 for success, non-0 with error in pop_error * otherwise. * * Side effects: Closes the connection on error. */ int pop_reset (popserver server) { if (pop_retrieve_flush (server)) { return (-1); } if (sendline (server, "RSET") || getok (server)) return (-1); return (0); }
/* * Function: pop_noop * * Purpose: Send a noop command to the server. * * Argument: * server The server to send to. * * Return value: 0 on success, non-zero with error in pop_error * otherwise. * * Side effects: Closes connection on error. */ int pop_noop (popserver server) { if (server->in_multi) { strcpy (pop_error, "In multi-line query in pop_noop"); return (-1); } if (sendline (server, "NOOP") || getok (server)) return (-1); return (0); }
/* Function: pop_delete * * Purpose: Delete a specified message. * * Arguments: * server Server from which to delete the message. * message Message to delete. * * Return value: 0 on success, non-zero with error in pop_error * otherwise. */ int pop_delete (popserver server, int message) { if (server->in_multi) { strcpy (pop_error, "In multi-line query in pop_delete"); return (-1); } sprintf (pop_error, "DELE %d", message); if (sendline (server, pop_error) || getok (server)) return (-1); return (0); }
int perform_cmd(int fd, int cmd) { unsigned char out[4]; memset(out, 0, sizeof(out)); BULB_FLAG_ADDRESS(out) = cmd; if (cmd == COMMAND_SCROLL_DISPLAY) { BULB_BLUE_STRING(out) = g_blue_start << 4; BULB_GREEN_RED(out) = g_green_start << 4 | g_red_start; BULB_BRIGHT(out) = g_bright_start; } writebuf(fd, out, sizeof(out)); if (getok(fd, 5, 1000 * 1000) != 0) { fprintf(stderr, "Error getting okay during cmd\n"); return -1; } return 0; }
/* * Function: pop_open (char *host, char *username, char *password, * int flags) * * Purpose: Establishes a connection with a post-office server, and * completes the authorization portion of the session. * * Arguments: * host The server host with which the connection should be * established. Optional. If omitted, internal * heuristics will be used to determine the server host, * if possible. * username * The username of the mail-drop to access. Optional. * If omitted, internal heuristics will be used to * determine the username, if possible. * password * The password to use for authorization. If omitted, * internal heuristics will be used to determine the * password, if possible. * flags A bit mask containing flags controlling certain * functions of the routine. Valid flags are defined in * the file pop.h * * Return value: Upon successful establishment of a connection, a * non-null popserver will be returned. Otherwise, null will be * returned, and the string variable pop_error will contain an * explanation of the error. */ popserver pop_open (char *host, char *username, char *password, int flags) { int sock; popserver server; /* Determine the user name */ if (! username) { username = getenv ("USER"); if (! (username && *username)) { username = getlogin (); if (! (username && *username)) { struct passwd *passwd; passwd = getpwuid (getuid ()); if (passwd && passwd->pw_name && *passwd->pw_name) { username = passwd->pw_name; } else { strcpy (pop_error, "Could not determine username"); return (0); } } } } /* * Determine the mail host. */ if (! host) { host = getenv ("MAILHOST"); } #ifdef HESIOD if ((! host) && (! (flags & POP_NO_HESIOD))) { struct hes_postoffice *office; office = hes_getmailhost (username); if (office && office->po_type && (! strcmp (office->po_type, "POP")) && office->po_name && *office->po_name && office->po_host && *office->po_host) { host = office->po_host; username = office->po_name; } } #endif #ifdef MAILHOST if (! host) { host = MAILHOST; } #endif if (! host) { strcpy (pop_error, "Could not determine POP server"); return (0); } /* Determine the password */ #ifdef KERBEROS #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS)) #else #define DONT_NEED_PASSWORD 0 #endif if ((! password) && (! DONT_NEED_PASSWORD)) { if (! (flags & POP_NO_GETPASS)) { password = getpass ("Enter POP password:"******"Could not determine POP password"); return (0); } } if (password) /* always true, detected 20060515 */ flags |= POP_NO_KERBEROS; else password = username; /* dead code, detected 20060515 */ /** "kpop" service is never used: look for 20060515 to see why **/ sock = socket_connection (host, flags); if (sock == -1) return (0); server = (popserver) malloc (sizeof (struct _popserver)); if (! server) { strcpy (pop_error, "Out of memory in pop_open"); return (0); } server->buffer = (char *) malloc (GETLINE_MIN); if (! server->buffer) { strcpy (pop_error, "Out of memory in pop_open"); free ((char *) server); return (0); } server->file = sock; server->data = 0; server->buffer_index = 0; server->buffer_size = GETLINE_MIN; server->in_multi = false; server->trash_started = false; if (getok (server)) return (0); /* * I really shouldn't use the pop_error variable like this, but.... */ if (strlen (username) > ERROR_MAX - 6) { pop_close (server); strcpy (pop_error, "Username too long; recompile pop.c with larger ERROR_MAX"); return (0); } sprintf (pop_error, "USER %s", username); if (sendline (server, pop_error) || getok (server)) { return (0); } if (strlen (password) > ERROR_MAX - 6) { pop_close (server); strcpy (pop_error, "Password too long; recompile pop.c with larger ERROR_MAX"); return (0); } sprintf (pop_error, "PASS %s", password); if (sendline (server, pop_error) || getok (server)) { return (0); } return (server); }
Text( ) { int sys, ln, embedded, p, ix; double x, y, theta, size; char s1[40], s2[40]; FILE *fp; gget( Buf, "System" ); if( Buf[0] == 'd' ) { sys = DATA; if( DXlo == 0 && DXhi == 0 ) { fprintf( stderr, "No graphics area.\n" ); gdp_exit(); } } else sys = ABSOLUTE; gget( Buf, "Font" ); NTfont( Buf ); gget( Buf, "Size" ); size = atof( Buf ); NTptsize( size ); gget( Buf, "Position" ); sscanf( Buf, "%lf %lf", &x, &y ); gget( Buf, "Direction" ); theta = atof( Buf ); if( theta != 0 ) NTchardir( theta ); gget( Buf, "Embeddedcoords" ); if( Buf[0] == 'y' ) embedded = 1; else embedded = 0; gget( Buf, "File" ); if( strlen( Buf ) > 0 ) fp = fopen( Buf, "r" ); /* text in a file */ else { /* text given-- put it in a file */ gget( Buf, "Text" ); text_tofile( Buf, Tempfile ); fp = fopen( Tempfile, "r" ); } if( fp == NULL ) { fprintf( stderr, "Cant open text file" ); gdp_exit(); } /* read the file and print the text */ if( !embedded ) { if( sys == DATA ) NTm( x, y ); else NTmov( x, y ); } ln = 1; while( fgets( Buf, 200, fp ) != NULL ) { if( embedded ) { ix = 0; strcpy( s1, getok( Buf, &ix ) ); strcpy( s2, getok( Buf, &ix ) ); if( goodnum( s1, &p ) && goodnum( s2, &p ) ) { x = atof( s1 ); y = atof( s2 ); if( sys == DATA ) NTm( x, y ); else NTmov( x, y ); strcpy( Buf, &Buf[ix] ); strip_ws( Buf ); } } NTtext( Buf ); if( sys == DATA ) NTmov( da_x(x), da_y(y)-(ln*Chh) ); else NTmov( x, y-(ln*Chh) ); ln++; } fclose( fp ); NTchardir( 0 ); }
int main(int argc, char *argv[]) { int fd; char *sp; int c; int i; int perform_init = 0; int addr, addr_start = 0, addr_end = 0; int string, string_start = 0, string_end = 0; int bright; int red; int green; int blue; int cmd = 0; int write_bulbs = 0; unsigned long delay = 100; char custom[256] = {0}; unsigned char out[4]; int rc = 0; static struct option long_options[] = { {"verbose", no_argument, NULL, 'v'}, {"delay", required_argument, NULL, 'd'}, {"every", required_argument, NULL, 'e'}, {"custom", required_argument, NULL, 'c'}, {"addr", required_argument, NULL, 'a'}, {"string", required_argument, NULL, 's'}, {"bright", required_argument, NULL, 'b'}, {"red", required_argument, NULL, 'r'}, {"green", required_argument, NULL, 'g'}, {"blue", required_argument, NULL, 'l'}, {0, 0, 0, 0} }; if (argc <= 1) { usage(argv[0]); return(-1); } while (1) { c = getopt_long(argc, argv, "vd:e:c:a:s:b:r:g:l:", long_options, NULL); if (c == -1) break; switch(c) { case 'i': perform_init = 1; break; case 'v': g_verbose = 1; break; case 'a': parse_range(optarg, &addr_start, &addr_end); break; case 's': parse_range(optarg, &string_start, &string_end); break; case 'b': parse_range(optarg, &g_bright_start, &g_bright_end); break; case 'r': parse_range(optarg, &g_red_start, &g_red_end); break; case 'g': parse_range(optarg, &g_green_start, &g_green_end); break; case 'l': parse_range(optarg, &g_blue_start, &g_blue_end); break; case 'd': delay = atoi(optarg); break; case 'e': g_confirm_every = atoi(optarg); break; case 'm': cmd = atoi(optarg); break; case 'c': strcpy(custom, optarg); break; default: usage(argv[0]); return(-1); } } /*------------------------------------------------------------------------ ** Connect to the arduino **----------------------------------------------------------------------*/ sp = getenv("ARDUINO_SERIAL_PORT"); if (!sp) sp = DEFAULT_ARDUINO_SERIAL_PORT; if (g_verbose) printf("Connecting to %s...\n", sp); fd = open_port(sp); if (fd <= 0) { char buf[256]; sprintf(buf, "Error opening %s:", sp); perror(buf); } if (g_verbose) printf("..open...\n"); flush_buffer(fd); if (g_verbose) printf("..connected\n"); /*------------------------------------------------------------------------ ** Look for and perform commands **----------------------------------------------------------------------*/ for (i = optind; i < argc; i++) { if (strcmp(argv[i], "init") == 0) rc = perform_cmd(fd, COMMAND_INIT); else if (strcmp(argv[i], "clear") == 0) rc = perform_cmd(fd, COMMAND_CLEAR); else if (strcmp(argv[i], "status") == 0) perform_status(fd); else if (strcmp(argv[i], "chase") == 0) rc = perform_cmd(fd, COMMAND_CHASE); else if (strcmp(argv[i], "flood") == 0) rc = perform_cmd(fd, COMMAND_FLOOD); else if (strcmp(argv[i], "display") == 0) rc = perform_cmd(fd, COMMAND_SCROLL_DISPLAY); else if (strcmp(argv[i], "displayoff") == 0) rc = perform_cmd(fd, COMMAND_SCROLL_DISPLAY_OFF); else if (strcmp(argv[i], "sync") == 0) perform_sync(fd); else if (strcmp(argv[i],"bulb") == 0) write_bulbs = 1; else if (perform_custom(fd, argv[i]) != 0) { fprintf(stderr, "Error: unrecognized command '%s'\n", argv[i]); usage(argv[0]); return -1; } } if (optind >= argc) write_bulbs = 1; if (cmd > 0) rc = perform_cmd(fd, cmd); if (write_bulbs) { for (addr = addr_start; addr <= addr_end; addr++) for (bright = g_bright_start; bright <= g_bright_end; bright++) for (red = g_red_start; red <= g_red_end; red++) for (green = g_green_start; green <= g_green_end; green++) for (blue = g_blue_start; blue <= g_blue_end; blue++) { for (string = string_start; string <= string_end; string++) { build_bulb(out, string, addr, bright, red, green, blue, string == string_end ? 0 : 1); writebuf(fd, out, 4); } if (delay > 0) usleep(delay); } } flush_buffer(fd); if (getok(fd, 1, 1000 * 1000) < 0) { fprintf(stderr, "Error: did not get closing ack\n"); rc = -1; } close(fd); if (g_verbose) printf("Wrote %ld bulbs\n", g_writes); return rc; }